From 3f04ec5bb5e00c401f73e14d7f11af41d7a4b444 Mon Sep 17 00:00:00 2001 From: MysterD Date: Wed, 14 Jun 2023 23:04:28 -0700 Subject: [PATCH] Fix noise rendering for dx' --- src/pc/gfx/gfx_direct3d12.cpp | 5 +++-- src/pc/gfx/gfx_direct3d_common.cpp | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/pc/gfx/gfx_direct3d12.cpp b/src/pc/gfx/gfx_direct3d12.cpp index 9b49f1ba..c25428c8 100644 --- a/src/pc/gfx/gfx_direct3d12.cpp +++ b/src/pc/gfx/gfx_direct3d12.cpp @@ -56,6 +56,7 @@ struct ShaderProgramD3D12 { bool used_textures[2]; uint8_t num_floats; uint8_t num_attribs; + bool do_noise; ComPtr vertex_shader; ComPtr pixel_shader; @@ -260,6 +261,7 @@ static struct ShaderProgram *gfx_direct3d12_create_and_load_new_shader(struct Co prg->used_textures[0] = cc_features.used_textures[0]; prg->used_textures[1] = cc_features.used_textures[1]; prg->num_floats = num_floats; + prg->do_noise = cc_features.do_noise; d3d.must_reload_pipeline = true; return (struct ShaderProgram *)(d3d.shader_program = prg); @@ -449,7 +451,6 @@ static void gfx_direct3d12_set_use_alpha(bool use_alpha) { static void gfx_direct3d12_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_t buf_vbo_num_tris) { struct ShaderProgramD3D12 *prg = d3d.shader_program; - if (d3d.must_reload_pipeline) { ComPtr& pipeline_state = d3d.pipeline_states[PipelineDesc{ prg->hash, @@ -528,7 +529,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_dither) { + if ((prg->cc.cm.use_alpha && prg->cc.cm.use_dither) || prg->do_noise) { 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 9ba08fe6..bc3a277d 100644 --- a/src/pc/gfx/gfx_direct3d_common.cpp +++ b/src/pc/gfx/gfx_direct3d_common.cpp @@ -51,6 +51,8 @@ static const char *shader_item_to_str(int32_t item, bool with_alpha, bool only_a return with_alpha ? "texel" : "texel.rgb"; case SHADER_COMBINEDA: return hint_single_element ? "texel.a" : (with_alpha ? "float4(texel.a, texel.a, texel.a, texel.a)" : "float3(texel.a, texel.a, texel.a)"); + case SHADER_NOISE: + return with_alpha ? "float4(noise, noise, noise, noise)" : "float3(noise, noise, noise)"; } } else { switch (item) { @@ -87,6 +89,8 @@ static const char *shader_item_to_str(int32_t item, bool with_alpha, bool only_a return "texel.a"; case SHADER_COMBINEDA: return "texel.a"; + case SHADER_NOISE: + return "noise"; } } } @@ -126,7 +130,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_dither) { + if ((cc.cm.use_alpha && cc.cm.use_dither) || ccf.do_noise) { append_str(buf, &len, ",CBV(b0, visibility = SHADER_VISIBILITY_PIXEL)"); } if (ccf.used_textures[0]) { @@ -146,7 +150,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_dither) { + if ((cc.cm.use_alpha && cc.cm.use_dither) || ccf.do_noise) { append_line(buf, &len, " float4 screenPos : TEXCOORD1;"); } if (cc.cm.use_fog) { @@ -176,7 +180,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_dither) { + if ((cc.cm.use_alpha && cc.cm.use_dither) || ccf.do_noise) { append_line(buf, &len, "cbuffer PerFrameCB : register(b0) {"); append_line(buf, &len, " uint noise_frame;"); append_line(buf, &len, " float2 noise_scale;"); @@ -229,7 +233,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_dither) { + if ((cc.cm.use_alpha && cc.cm.use_dither) || ccf.do_noise) { append_line(buf, &len, " result.screenPos = position;"); } if (ccf.used_textures[0] || ccf.used_textures[1]) { @@ -252,6 +256,12 @@ void gfx_direct3d_common_build_shader(char buf[4096], size_t& len, size_t& num_f append_line(buf, &len, "[RootSignature(RS)]"); } append_line(buf, &len, "float4 PSMain(PSInput input) : SV_TARGET {"); + + if ((cc.cm.use_alpha && cc.cm.use_dither) || ccf.do_noise) { + append_line(buf, &len, " float2 coords = (input.screenPos.xy / input.screenPos.w) * noise_scale;"); + append_line(buf, &len, " float noise = round(random(float3(floor(coords), noise_frame)));"); + } + if (ccf.used_textures[0]) { if (three_point_filtering) { append_line(buf, &len, " float4 texVal0;"); @@ -320,8 +330,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_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)));"); + append_line(buf, &len, " texel.a *= noise;"); } if (cc.cm.use_alpha) {