Implement G_CCMUX_NOISE (#1731)

This commit is contained in:
Rozelette 2022-10-11 18:32:51 -05:00 committed by GitHub
parent e7ea2a3ae1
commit 825af33b6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 39 deletions

View File

@ -29,7 +29,8 @@ enum {
SHADER_TEXEL1,
SHADER_TEXEL1A,
SHADER_1,
SHADER_COMBINED
SHADER_COMBINED,
SHADER_NOISE
};
#define SHADER_OPT_ALPHA (1 << 0)

View File

@ -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);

View File

@ -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;
}

View File

@ -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: