From 480604ec72433f8cde3a8f6d22d3f8c86bea402f Mon Sep 17 00:00:00 2001 From: GPUCode <47210458+GPUCode@users.noreply.github.com> Date: Wed, 31 Jan 2024 23:29:39 +0200 Subject: [PATCH] glsl_shader_fs_gen: Apply shadow before ambient light (#7404) --- .../renderer_software/sw_lighting.cpp | 25 ++++++++++--------- .../shader/generator/glsl_fs_shader_gen.cpp | 4 +-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/video_core/renderer_software/sw_lighting.cpp b/src/video_core/renderer_software/sw_lighting.cpp index 5ddf4d617..03142b0d5 100644 --- a/src/video_core/renderer_software/sw_lighting.cpp +++ b/src/video_core/renderer_software/sw_lighting.cpp @@ -285,19 +285,20 @@ std::pair, Common::Vec4> ComputeFragmentsColors( } } - auto diffuse = - (light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f()) * - dist_atten * spot_atten; - auto specular = (specular_0 + specular_1) * clamp_highlights * dist_atten * spot_atten; + const bool shadow_primary_enable = + lighting.config0.shadow_primary && !lighting.IsShadowDisabled(num); + const bool shadow_secondary_enable = + lighting.config0.shadow_secondary && !lighting.IsShadowDisabled(num); + const auto shadow_primary = + shadow_primary_enable ? shadow.xyz() : Common::MakeVec(1.f, 1.f, 1.f); + const auto shadow_secondary = + shadow_secondary_enable ? shadow.xyz() : Common::MakeVec(1.f, 1.f, 1.f); - if (!lighting.IsShadowDisabled(num)) { - if (lighting.config0.shadow_primary) { - diffuse = diffuse * shadow.xyz(); - } - if (lighting.config0.shadow_secondary) { - specular = specular * shadow.xyz(); - } - } + const auto diffuse = (light_config.diffuse.ToVec3f() * dot_product * shadow_primary + + light_config.ambient.ToVec3f()) * + dist_atten * spot_atten; + const auto specular = (specular_0 + specular_1) * clamp_highlights * dist_atten * + spot_atten * shadow_secondary; diffuse_sum += Common::MakeVec(diffuse, 0.0f); specular_sum += Common::MakeVec(specular, 0.0f); diff --git a/src/video_core/shader/generator/glsl_fs_shader_gen.cpp b/src/video_core/shader/generator/glsl_fs_shader_gen.cpp index 45a49e7ff..c5847ab99 100644 --- a/src/video_core/shader/generator/glsl_fs_shader_gen.cpp +++ b/src/video_core/shader/generator/glsl_fs_shader_gen.cpp @@ -810,8 +810,8 @@ void FragmentModule::WriteLighting() { // Compute primary fragment color (diffuse lighting) function out += fmt::format( - "diffuse_sum.rgb += (({}.diffuse * dot_product) + {}.ambient) * {} * {}{};\n", - light_src, light_src, dist_atten, spot_atten, shadow_primary); + "diffuse_sum.rgb += (({}.diffuse * dot_product{}) + {}.ambient) * {} * {};\n", + light_src, shadow_primary, light_src, dist_atten, spot_atten); // Compute secondary fragment color (specular lighting) function out += fmt::format("specular_sum.rgb += ({} + {}) * clamp_highlights * {} * {}{};\n",