diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_cc.h b/libultraship/libultraship/Lib/Fast3D/gfx_cc.h index f0a9f2eca..1046ed83a 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_cc.h +++ b/libultraship/libultraship/Lib/Fast3D/gfx_cc.h @@ -29,7 +29,8 @@ enum { SHADER_TEXEL1, SHADER_TEXEL1A, SHADER_1, - SHADER_COMBINED + SHADER_COMBINED, + SHADER_NOISE }; #define SHADER_OPT_ALPHA (1 << 0) diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp index 2c10e6605..dd17af0a0 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_direct3d_common.cpp @@ -15,6 +15,8 @@ static void append_line(char *buf, size_t *len, const char *str) { buf[(*len)++] = '\n'; } +#define RAND_NOISE "((random(float3(floor(screenSpace.xy * noise_scale), noise_frame)) + 1.0) / 2.0)" + static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_alpha, bool inputs_have_alpha, bool hint_single_element) { if (!only_alpha) { switch (item) { @@ -41,6 +43,8 @@ static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_ return with_alpha ? "texVal1" : "texVal1.rgb"; case SHADER_COMBINED: return with_alpha ? "texel" : "texel.rgb"; + case SHADER_NOISE: + return with_alpha ? "float4(" RAND_NOISE ", " RAND_NOISE ", " RAND_NOISE ", " RAND_NOISE ")" : "float3(" RAND_NOISE ", " RAND_NOISE ", " RAND_NOISE ")"; } } else { switch (item) { @@ -67,10 +71,14 @@ static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_ return "texVal1.a"; case SHADER_COMBINED: return "texel.a"; + case SHADER_NOISE: + return RAND_NOISE; } } } +#undef RAND_NOISE + static void append_formula(char *buf, size_t *len, const uint8_t c[2][4], bool do_single, bool do_multiply, bool do_mix, bool with_alpha, bool only_alpha, bool opt_alpha) { if (do_single) { append_str(buf, len, shader_item_to_str(c[only_alpha][3], with_alpha, only_alpha, opt_alpha, false)); @@ -106,9 +114,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f if (include_root_signature) { append_str(buf, &len, "#define RS \"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | DENY_VERTEX_SHADER_ROOT_ACCESS)"); - if (cc_features.opt_alpha && cc_features.opt_noise) { - append_str(buf, &len, ",CBV(b0, visibility = SHADER_VISIBILITY_PIXEL)"); - } + append_str(buf, &len, ",CBV(b0, visibility = SHADER_VISIBILITY_PIXEL)"); if (cc_features.used_textures[0]) { append_str(buf, &len, ",DescriptorTable(SRV(t0), visibility = SHADER_VISIBILITY_PIXEL)"); append_str(buf, &len, ",DescriptorTable(Sampler(s0), visibility = SHADER_VISIBILITY_PIXEL)"); @@ -161,17 +167,15 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f // Constant buffer and random function - if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(buf, &len, "cbuffer PerFrameCB : register(b0) {"); - append_line(buf, &len, " uint noise_frame;"); - append_line(buf, &len, " float noise_scale;"); - append_line(buf, &len, "}"); + append_line(buf, &len, "cbuffer PerFrameCB : register(b0) {"); + append_line(buf, &len, " uint noise_frame;"); + append_line(buf, &len, " float noise_scale;"); + append_line(buf, &len, "}"); - append_line(buf, &len, "float random(in float3 value) {"); - append_line(buf, &len, " float random = dot(value, float3(12.9898, 78.233, 37.719));"); - append_line(buf, &len, " return frac(sin(random) * 143758.5453);"); - append_line(buf, &len, "}"); - } + append_line(buf, &len, "float random(in float3 value) {"); + append_line(buf, &len, " float random = dot(value, float3(12.9898, 78.233, 37.719));"); + append_line(buf, &len, " return frac(sin(random) * 143758.5453);"); + append_line(buf, &len, "}"); // 3 point texture filtering // Original author: ArthurCarvalho @@ -248,11 +252,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f if (include_root_signature) { append_line(buf, &len, "[RootSignature(RS)]"); } - if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(buf, &len, "float4 PSMain(PSInput input, float4 screenSpace : SV_Position) : SV_TARGET {"); - } else { - append_line(buf, &len, "float4 PSMain(PSInput input) : SV_TARGET {"); - } + append_line(buf, &len, "float4 PSMain(PSInput input, float4 screenSpace : SV_Position) : SV_TARGET {"); for (int i = 0; i < 2; i++) { if (cc_features.used_textures[i]) { len += sprintf(buf + len, " float2 tc%d = input.uv%d;\r\n", i, i); diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp index 7d2428a45..1a7a62885 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_opengl.cpp @@ -57,7 +57,6 @@ struct ShaderProgram { GLint attrib_locations[16]; uint8_t attrib_sizes[16]; uint8_t num_attribs; - bool used_noise; GLint frame_count_location; GLint noise_scale_location; }; @@ -104,10 +103,8 @@ static void gfx_opengl_vertex_array_set_attribs(struct ShaderProgram *prg) { } static void gfx_opengl_set_uniforms(struct ShaderProgram *prg) { - if (prg->used_noise) { - glUniform1i(prg->frame_count_location, frame_count); - glUniform1f(prg->noise_scale_location, current_noise_scale); - } + glUniform1i(prg->frame_count_location, frame_count); + glUniform1f(prg->noise_scale_location, current_noise_scale); } static void gfx_opengl_unload_shader(struct ShaderProgram *old_prg) { @@ -134,6 +131,8 @@ static void append_line(char *buf, size_t *len, const char *str) { buf[(*len)++] = '\n'; } +#define RAND_NOISE "((random(vec3(floor(gl_FragCoord.xy * noise_scale), float(frame_count))) + 1.0) / 2.0)" + static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_alpha, bool inputs_have_alpha, bool hint_single_element) { if (!only_alpha) { switch (item) { @@ -161,6 +160,8 @@ static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_ return with_alpha ? "texVal1" : "texVal1.rgb"; case SHADER_COMBINED: return with_alpha ? "texel" : "texel.rgb"; + case SHADER_NOISE: + return with_alpha ? "vec4(" RAND_NOISE ", " RAND_NOISE ", " RAND_NOISE ", " RAND_NOISE ")" : "vec3(" RAND_NOISE ", " RAND_NOISE ", " RAND_NOISE ")"; } } else { switch (item) { @@ -186,11 +187,15 @@ static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_ return "texVal1.a"; case SHADER_COMBINED: return "texel.a"; + case SHADER_NOISE: + return RAND_NOISE; } } return ""; } +#undef RAND_NOISE + static void append_formula(char *buf, size_t *len, uint8_t c[2][4], bool do_single, bool do_multiply, bool do_mix, bool with_alpha, bool only_alpha, bool opt_alpha) { if (do_single) { append_str(buf, len, shader_item_to_str(c[only_alpha][3], with_alpha, only_alpha, opt_alpha, false)); @@ -368,15 +373,13 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad append_line(fs_buf, &fs_len, "uniform sampler2D uTex1;"); } - if (cc_features.opt_alpha && cc_features.opt_noise) { - append_line(fs_buf, &fs_len, "uniform int frame_count;"); - append_line(fs_buf, &fs_len, "uniform float noise_scale;"); + append_line(fs_buf, &fs_len, "uniform int frame_count;"); + append_line(fs_buf, &fs_len, "uniform float noise_scale;"); - append_line(fs_buf, &fs_len, "float random(in vec3 value) {"); - append_line(fs_buf, &fs_len, " float random = dot(sin(value), vec3(12.9898, 78.233, 37.719));"); - append_line(fs_buf, &fs_len, " return fract(sin(random) * 143758.5453);"); - append_line(fs_buf, &fs_len, "}"); - } + append_line(fs_buf, &fs_len, "float random(in vec3 value) {"); + append_line(fs_buf, &fs_len, " float random = dot(sin(value), vec3(12.9898, 78.233, 37.719));"); + append_line(fs_buf, &fs_len, " return fract(sin(random) * 143758.5453);"); + append_line(fs_buf, &fs_len, "}"); if (current_filter_mode == FILTER_THREE_POINT) { #if __APPLE__ @@ -604,13 +607,8 @@ static struct ShaderProgram* gfx_opengl_create_and_load_new_shader(uint64_t shad glUniform1i(sampler_location, 1); } - if (cc_features.opt_alpha && cc_features.opt_noise) { - prg->frame_count_location = glGetUniformLocation(shader_program, "frame_count"); - prg->noise_scale_location = glGetUniformLocation(shader_program, "noise_scale"); - prg->used_noise = true; - } else { - prg->used_noise = false; - } + prg->frame_count_location = glGetUniformLocation(shader_program, "frame_count"); + prg->noise_scale_location = glGetUniformLocation(shader_program, "noise_scale"); return prg; } diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp index 4c193fa4a..c8a866afc 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp @@ -413,6 +413,9 @@ static void gfx_generate_cc(struct ColorCombiner *comb, uint64_t cc_id) { val = SHADER_TEXEL1A; used_textures[1] = true; break; + case G_CCMUX_NOISE: + val = SHADER_NOISE; + break; case G_CCMUX_PRIMITIVE: case G_CCMUX_PRIMITIVE_ALPHA: case G_CCMUX_PRIM_LOD_FRAC: