From 529058ad771a8392d88817e4969afd508449165e Mon Sep 17 00:00:00 2001 From: MysterD Date: Wed, 14 Jun 2023 22:04:55 -0700 Subject: [PATCH] Add support for noise in color combiners --- src/pc/discord/discord.c | 2 +- src/pc/gfx/gfx_cc.c | 5 +++-- src/pc/gfx/gfx_cc.h | 5 ++++- src/pc/gfx/gfx_direct3d12.cpp | 2 +- src/pc/gfx/gfx_direct3d_common.cpp | 10 +++++----- src/pc/gfx/gfx_opengl.c | 21 +++++++++++++++------ src/pc/gfx/gfx_pc.c | 5 ++++- 7 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/pc/discord/discord.c b/src/pc/discord/discord.c index 5ca20204..6c14ea2a 100644 --- a/src/pc/discord/discord.c +++ b/src/pc/discord/discord.c @@ -36,7 +36,7 @@ void discord_fatal(int rc) { } if (rc != DiscordResult_Ok) { - static sDiscordReported = false; + static bool sDiscordReported = false; if (!sDiscordReported) { sDiscordReported = true; LOG_ERROR("Discord threw an error. RC: %d", rc); diff --git a/src/pc/gfx/gfx_cc.c b/src/pc/gfx/gfx_cc.c index 1463bde5..a67ffec0 100644 --- a/src/pc/gfx/gfx_cc.c +++ b/src/pc/gfx/gfx_cc.c @@ -29,6 +29,7 @@ void gfx_cc_get_features(struct ColorCombiner* cc, struct CCFeatures* ccf) { } ccf->used_textures[0] = ccf->used_textures[0] || c == SHADER_TEXEL0 || c == SHADER_TEXEL0A; ccf->used_textures[1] = ccf->used_textures[1] || c == SHADER_TEXEL1 || c == SHADER_TEXEL1A; + ccf->do_noise = ccf->do_noise || c == SHADER_NOISE; } // figure out optimizations @@ -112,6 +113,7 @@ void gfx_cc_precomp(void) { gfx_pc_precomp_shader(0x04060401, 0x05000000, 0x04060402, 0x05000b0b, 0x00000001); // 1d970841b086b2e6 gfx_pc_precomp_shader(0x01000000, 0x04000000, 0x02000000, 0x04000b0b, 0x00000001); // 410000008f86b2e6 gfx_pc_precomp_shader(0x04060401, 0x05000000, 0x04060402, 0x05000b0b, 0x00000009); // 1d970841b086b2ee + gfx_pc_precomp_shader(0x00040001, 0x00040001, 0x00040002, 0x0b040b02, 0x00000009); // 110404410c0ab30f sAllowCCPrint = 1; } @@ -126,10 +128,9 @@ static uint8_t color_comb_component_a(uint32_t v, uint8_t cycle) { case G_CCMUX_SHADE: return CC_SHADE; case G_CCMUX_ENVIRONMENT: return CC_ENV; case G_CCMUX_1: return CC_1; - //case G_CCMUX_NOISE: return CC_NOISE; case G_CCMUX_0: return CC_0; - case G_CCMUX_COMBINED_ALPHA: return cycle ? CC_COMBINEDA : CC_0; + case G_CCMUX_COMBINED_ALPHA: return cycle ? CC_COMBINEDA : CC_NOISE; case G_CCMUX_TEXEL0_ALPHA: return cycle ? CC_TEXEL1A : CC_TEXEL0A; case G_CCMUX_TEXEL1_ALPHA: return cycle ? CC_TEXEL0A : CC_TEXEL1A; case G_CCMUX_PRIMITIVE_ALPHA: return CC_PRIMA; diff --git a/src/pc/gfx/gfx_cc.h b/src/pc/gfx/gfx_cc.h index 68f3aed8..336a237b 100644 --- a/src/pc/gfx/gfx_cc.h +++ b/src/pc/gfx/gfx_cc.h @@ -20,6 +20,7 @@ enum { CC_PRIMA, CC_SHADEA, CC_ENVA, + CC_NOISE, CC_ENUM_MAX, }; @@ -40,6 +41,7 @@ enum { SHADER_1, SHADER_COMBINED, SHADER_COMBINEDA, + SHADER_NOISE, }; #define SHADER_OPT_ALPHA (1 << 24) @@ -54,6 +56,7 @@ struct CCFeatures { bool do_multiply[4]; bool do_mix[4]; bool color_alpha_same[2]; + bool do_noise; }; #pragma pack(1) @@ -72,7 +75,7 @@ struct CombineMode { uint8_t use_alpha : 1; uint8_t use_fog : 1; uint8_t texture_edge : 1; - uint8_t use_noise : 1; + uint8_t use_dither : 1; uint8_t use_2cycle : 1; uint8_t light_map : 1; }; diff --git a/src/pc/gfx/gfx_direct3d12.cpp b/src/pc/gfx/gfx_direct3d12.cpp index 72ad859d..9b49f1ba 100644 --- a/src/pc/gfx/gfx_direct3d12.cpp +++ b/src/pc/gfx/gfx_direct3d12.cpp @@ -528,7 +528,7 @@ static void gfx_direct3d12_draw_triangles(float buf_vbo[], size_t buf_vbo_len, s int root_param_index = 0; - if (prg->cc.cm.use_alpha && prg->cc.cm.use_noise) { + if (prg->cc.cm.use_alpha && prg->cc.cm.use_dither) { d3d.command_list->SetGraphicsRootConstantBufferView(root_param_index++, d3d.noise_cb->GetGPUVirtualAddress()); } diff --git a/src/pc/gfx/gfx_direct3d_common.cpp b/src/pc/gfx/gfx_direct3d_common.cpp index 289271b7..9ba08fe6 100644 --- a/src/pc/gfx/gfx_direct3d_common.cpp +++ b/src/pc/gfx/gfx_direct3d_common.cpp @@ -126,7 +126,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.cm.use_alpha && cc.cm.use_noise) { + if (cc.cm.use_alpha && cc.cm.use_dither) { append_str(buf, &len, ",CBV(b0, visibility = SHADER_VISIBILITY_PIXEL)"); } if (ccf.used_textures[0]) { @@ -146,7 +146,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f append_line(buf, &len, " float2 uv : TEXCOORD;"); num_floats += 2; } - if (cc.cm.use_alpha && cc.cm.use_noise) { + if (cc.cm.use_alpha && cc.cm.use_dither) { append_line(buf, &len, " float4 screenPos : TEXCOORD1;"); } if (cc.cm.use_fog) { @@ -176,7 +176,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f // Constant buffer and random function - if (cc.cm.use_alpha && cc.cm.use_noise) { + if (cc.cm.use_alpha && cc.cm.use_dither) { append_line(buf, &len, "cbuffer PerFrameCB : register(b0) {"); append_line(buf, &len, " uint noise_frame;"); append_line(buf, &len, " float2 noise_scale;"); @@ -229,7 +229,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f append_line(buf, &len, ") {"); append_line(buf, &len, " PSInput result;"); append_line(buf, &len, " result.position = position;"); - if (cc.cm.use_alpha && cc.cm.use_noise) { + if (cc.cm.use_alpha && cc.cm.use_dither) { append_line(buf, &len, " result.screenPos = position;"); } if (ccf.used_textures[0] || ccf.used_textures[1]) { @@ -319,7 +319,7 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f } } - if (cc.cm.use_alpha && cc.cm.use_noise) { + if (cc.cm.use_alpha && cc.cm.use_dither) { append_line(buf, &len, " float2 coords = (input.screenPos.xy / input.screenPos.w) * noise_scale;"); append_line(buf, &len, " texel.a *= round(random(float3(floor(coords), noise_frame)));"); } diff --git a/src/pc/gfx/gfx_opengl.c b/src/pc/gfx/gfx_opengl.c index b0d5a8b9..26734241 100644 --- a/src/pc/gfx/gfx_opengl.c +++ b/src/pc/gfx/gfx_opengl.c @@ -170,6 +170,8 @@ static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_ case SHADER_COMBINEDA: return hint_single_element ? "texel.a" : (with_alpha ? "vec4(texel.a, texel.a, texel.a, texel.a)" : "vec3(texel.a, texel.a, texel.a)"); + case SHADER_NOISE: + return with_alpha ? "vec4(noise)" : "vec3(noise)"; } } else { switch (item) { @@ -205,6 +207,8 @@ static const char *shader_item_to_str(uint32_t item, bool with_alpha, bool only_ return "texel.a"; case SHADER_COMBINEDA: return "texel.a"; + case SHADER_NOISE: + return "noise.a"; } } return "unknown"; @@ -248,9 +252,9 @@ static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(struct ColorC bool opt_light_map = cc->cm.light_map; #ifdef USE_GLES - bool opt_noise = false; + bool opt_dither = false; #else - bool opt_noise = cc->cm.use_noise; + bool opt_dither = cc->cm.use_dither; #endif char vs_buf[1024]; @@ -361,7 +365,7 @@ static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(struct ColorC } } - if (opt_alpha && opt_noise) { + if ((opt_alpha && opt_dither) || ccf.do_noise) { append_line(fs_buf, &fs_len, "uniform float frame_count;"); append_line(fs_buf, &fs_len, "float random(in vec3 value) {"); @@ -372,6 +376,10 @@ static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(struct ColorC append_line(fs_buf, &fs_len, "void main() {"); + if ((opt_alpha && opt_dither) || ccf.do_noise) { + append_line(fs_buf, &fs_len, "float noise = floor(random(floor(vec3(gl_FragCoord.xy, frame_count))) + 0.5);"); + } + if (ccf.used_textures[0]) { append_line(fs_buf, &fs_len, "vec4 texVal0 = sampleTex(uTex0, vTexCoord, uTex0Size, uTex0Filter);"); } @@ -417,8 +425,9 @@ static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(struct ColorC } } - if (opt_alpha && opt_noise) - append_line(fs_buf, &fs_len, "texel.a *= floor(random(floor(vec3(gl_FragCoord.xy, frame_count))) + 0.5);"); + if (opt_alpha && opt_dither) { + append_line(fs_buf, &fs_len, "texel.a *= noise;"); + } if (opt_alpha) { append_line(fs_buf, &fs_len, "gl_FragColor = texel;"); @@ -532,7 +541,7 @@ static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(struct ColorC glUniform1i(sampler_location, 1); } - if (opt_alpha && opt_noise) { + if ((opt_alpha && opt_dither) || ccf.do_noise) { prg->uniform_locations[4] = glGetUniformLocation(shader_program, "frame_count"); prg->used_noise = true; } else { diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index 27324e07..ee13eafe 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -304,6 +304,9 @@ static void gfx_generate_cc(struct ColorCombiner *cc) { case CC_COMBINEDA: shader_cmd = cc->cm.use_2cycle ? SHADER_COMBINEDA : SHADER_0; break; + case CC_NOISE: + shader_cmd = SHADER_NOISE; + break; case CC_PRIM: case CC_PRIMA: case CC_SHADE: @@ -1043,7 +1046,7 @@ static void OPTIMIZE_O3 gfx_sp_tri1(uint8_t vtx1_idx, uint8_t vtx2_idx, uint8_t cm->use_alpha = (rdp.other_mode_l & (G_BL_A_MEM << 18)) == 0; cm->texture_edge = (rdp.other_mode_l & CVG_X_ALPHA) == CVG_X_ALPHA; - cm->use_noise = (rdp.other_mode_l & G_AC_DITHER) == G_AC_DITHER; + cm->use_dither = (rdp.other_mode_l & G_AC_DITHER) == G_AC_DITHER; cm->use_2cycle = (rdp.other_mode_h & (3U << G_MDSFT_CYCLETYPE)) == G_CYC_2CYCLE; cm->use_fog = (rdp.other_mode_l >> 30) == G_BL_CLR_FOG; cm->light_map = (rsp.geometry_mode & G_LIGHT_MAP_EXT) == G_LIGHT_MAP_EXT;