From b5f2318ae7a02ba5bc0113018acd82ad129ae97a Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Sat, 2 Feb 2019 20:39:51 -0500 Subject: [PATCH] gl_rasterizer: change shadow_texture_bias from shader config var to shader uniform Games can frequently change this register. Using it as shader config var would generates a lot of shaders --- src/video_core/renderer_opengl/gl_rasterizer.cpp | 14 ++++++++++++++ src/video_core/renderer_opengl/gl_rasterizer.h | 3 +++ src/video_core/renderer_opengl/gl_shader_gen.cpp | 8 +++----- src/video_core/renderer_opengl/gl_shader_gen.h | 1 - src/video_core/renderer_opengl/gl_shader_manager.h | 1 + 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 50f89a656..c0c536572 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -203,6 +203,7 @@ void RasterizerOpenGL::SyncEntireState() { SyncProcTexNoise(); SyncProcTexBias(); SyncShadowBias(); + SyncShadowTextureBias(); } /** @@ -903,6 +904,11 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) { SyncBlendColor(); break; + // Shadow texture + case PICA_REG_INDEX(texturing.shadow): + SyncShadowTextureBias(); + break; + // Fog state case PICA_REG_INDEX(texturing.fog_color): SyncFogColor(); @@ -1906,6 +1912,14 @@ void RasterizerOpenGL::SyncShadowBias() { } } +void RasterizerOpenGL::SyncShadowTextureBias() { + GLint bias = Pica::g_state.regs.texturing.shadow.bias << 1; + if (bias != uniform_block_data.data.shadow_texture_bias) { + uniform_block_data.data.shadow_texture_bias = bias; + uniform_block_data.dirty = true; + } +} + void RasterizerOpenGL::SyncAndUploadLUTs() { constexpr std::size_t max_size = sizeof(GLvec2) * 256 * Pica::LightingRegs::NumLightingSampler + sizeof(GLvec2) * 128 + // fog diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 807337f75..730364819 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -214,6 +214,9 @@ private: /// Syncs the shadow rendering bias to match the PICA register void SyncShadowBias(); + /// Syncs the shadow texture bias to match the PICA register + void SyncShadowTextureBias(); + /// Syncs and uploads the lighting, fog and proctex LUTs void SyncAndUploadLUTs(); diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 62b1162ec..521f568a0 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp @@ -63,6 +63,7 @@ layout (std140) uniform shader_data { int proctex_lut_offset; int proctex_diff_lut_offset; float proctex_bias; + int shadow_texture_bias; ivec4 lighting_lut_offset[NUM_LIGHTING_SAMPLERS / 4]; vec3 fog_color; vec2 proctex_noise_f; @@ -240,7 +241,6 @@ PicaFSConfig PicaFSConfig::BuildFromRegs(const Pica::Regs& regs) { FramebufferRegs::FragmentOperationMode::Shadow; state.shadow_texture_orthographic = regs.texturing.shadow.orthographic != 0; - state.shadow_texture_bias = regs.texturing.shadow.bias << 1; return res; } @@ -1358,8 +1358,7 @@ vec4 shadowTexture(vec2 uv, float w) { if (!config.state.shadow_texture_orthographic) { out += "uv /= w;"; } - out += "uint z = uint(max(0, int(min(abs(w), 1.0) * 0xFFFFFF) - " + - std::to_string(state.shadow_texture_bias) + "));"; + out += "uint z = uint(max(0, int(min(abs(w), 1.0) * 0xFFFFFF) - shadow_texture_bias));"; out += R"( vec2 coord = vec2(imageSize(shadow_texture_px)) * uv - vec2(0.5); vec2 coord_floor = floor(coord); @@ -1391,8 +1390,7 @@ vec4 shadowTextureCube(vec2 uv, float w) { if (c.z > 0.0) uv.x = -uv.x; } )"; - out += "uint z = uint(max(0, int(min(w, 1.0) * 0xFFFFFF) - " + - std::to_string(state.shadow_texture_bias) + "));"; + out += "uint z = uint(max(0, int(min(w, 1.0) * 0xFFFFFF) - shadow_texture_bias));"; out += R"( vec2 coord = vec2(size) * (uv / w * vec2(0.5) + vec2(0.5)) - vec2(0.5); vec2 coord_floor = floor(coord); diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h index df27b3aaf..cd1264bdb 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.h +++ b/src/video_core/renderer_opengl/gl_shader_gen.h @@ -118,7 +118,6 @@ struct PicaFSConfigState { bool shadow_rendering; bool shadow_texture_orthographic; - u32 shadow_texture_bias; }; /** diff --git a/src/video_core/renderer_opengl/gl_shader_manager.h b/src/video_core/renderer_opengl/gl_shader_manager.h index 95c4a5f37..e160bd0e7 100644 --- a/src/video_core/renderer_opengl/gl_shader_manager.h +++ b/src/video_core/renderer_opengl/gl_shader_manager.h @@ -49,6 +49,7 @@ struct UniformData { GLint proctex_lut_offset; GLint proctex_diff_lut_offset; GLfloat proctex_bias; + GLint shadow_texture_bias; alignas(16) GLivec4 lighting_lut_offset[Pica::LightingRegs::NumLightingSampler / 4]; alignas(16) GLvec3 fog_color; alignas(8) GLvec2 proctex_noise_f;