aboutsummaryrefslogtreecommitdiff
path: root/src/video_core/swrasterizer/lighting.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/swrasterizer/lighting.cpp')
-rw-r--r--src/video_core/swrasterizer/lighting.cpp42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/video_core/swrasterizer/lighting.cpp b/src/video_core/swrasterizer/lighting.cpp
index d61e6d572..39a3e396d 100644
--- a/src/video_core/swrasterizer/lighting.cpp
+++ b/src/video_core/swrasterizer/lighting.cpp
@@ -55,6 +55,9 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
light_vector.Normalize();
+ Math::Vec3<float> norm_view = view.Normalized();
+ Math::Vec3<float> half_vector = norm_view + light_vector;
+
float dist_atten = 1.0f;
if (!lighting.IsDistAttenDisabled(num)) {
auto distance = (-view - position).Length();
@@ -74,17 +77,15 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
auto GetLutValue = [&](LightingRegs::LightingLutInput input, bool abs,
LightingRegs::LightingScale scale_enum,
LightingRegs::LightingSampler sampler) {
- Math::Vec3<float> norm_view = view.Normalized();
- Math::Vec3<float> half_angle = (norm_view + light_vector).Normalized();
float result = 0.0f;
switch (input) {
case LightingRegs::LightingLutInput::NH:
- result = Math::Dot(normal, half_angle);
+ result = Math::Dot(normal, half_vector.Normalized());
break;
case LightingRegs::LightingLutInput::VH:
- result = Math::Dot(norm_view, half_angle);
+ result = Math::Dot(norm_view, half_vector.Normalized());
break;
case LightingRegs::LightingLutInput::NV:
@@ -95,6 +96,12 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
result = Math::Dot(light_vector, normal);
break;
+ case LightingRegs::LightingLutInput::SP: {
+ Math::Vec3<s32> spot_dir{light_config.spot_x.Value(), light_config.spot_y.Value(),
+ light_config.spot_z.Value()};
+ result = Math::Dot(light_vector, spot_dir.Cast<float>() / 2047.0f);
+ break;
+ }
default:
LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input));
UNIMPLEMENTED();
@@ -125,6 +132,16 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
LookupLightingLut(lighting_state, static_cast<size_t>(sampler), index, delta);
};
+ // If enabled, compute spot light attenuation value
+ float spot_atten = 1.0f;
+ if (!lighting.IsSpotAttenDisabled(num) &&
+ LightingRegs::IsLightingSamplerSupported(
+ lighting.config0.config, LightingRegs::LightingSampler::SpotlightAttenuation)) {
+ auto lut = LightingRegs::SpotlightAttenuationSampler(num);
+ spot_atten = GetLutValue(lighting.lut_input.sp, lighting.abs_lut_input.disable_sp == 0,
+ lighting.lut_scale.sp, lut);
+ }
+
// Specular 0 component
float d0_lut_value = 1.0f;
if (lighting.config1.disable_lut_d0 == 0 &&
@@ -224,12 +241,23 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
else
dot_product = std::max(dot_product, 0.0f);
+ if (light_config.config.geometric_factor_0 || light_config.config.geometric_factor_1) {
+ float geo_factor = half_vector.Length2();
+ geo_factor = geo_factor == 0.0f ? 0.0f : std::min(dot_product / geo_factor, 1.0f);
+ if (light_config.config.geometric_factor_0) {
+ specular_0 *= geo_factor;
+ }
+ if (light_config.config.geometric_factor_1) {
+ specular_1 *= geo_factor;
+ }
+ }
+
auto diffuse =
light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f();
- diffuse_sum += Math::MakeVec(diffuse * dist_atten, 0.0f);
+ diffuse_sum += Math::MakeVec(diffuse * dist_atten * spot_atten, 0.0f);
- specular_sum +=
- Math::MakeVec((specular_0 + specular_1) * clamp_highlights * dist_atten, 0.0f);
+ specular_sum += Math::MakeVec(
+ (specular_0 + specular_1) * clamp_highlights * dist_atten * spot_atten, 0.0f);
}
diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f);