diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/fragment_lighting.cpp | 250 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/fragment_lighting.h | 18 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/rasterizer.cpp | 241 |
4 files changed, 271 insertions, 240 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 0961a3251..b2280f2ef 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -14,6 +14,7 @@ set(SRCS | |||
| 14 | shader/shader.cpp | 14 | shader/shader.cpp |
| 15 | shader/shader_interpreter.cpp | 15 | shader/shader_interpreter.cpp |
| 16 | swrasterizer/clipper.cpp | 16 | swrasterizer/clipper.cpp |
| 17 | swrasterizer/fragment_lighting.cpp | ||
| 17 | swrasterizer/framebuffer.cpp | 18 | swrasterizer/framebuffer.cpp |
| 18 | swrasterizer/proctex.cpp | 19 | swrasterizer/proctex.cpp |
| 19 | swrasterizer/rasterizer.cpp | 20 | swrasterizer/rasterizer.cpp |
| @@ -54,6 +55,7 @@ set(HEADERS | |||
| 54 | shader/shader.h | 55 | shader/shader.h |
| 55 | shader/shader_interpreter.h | 56 | shader/shader_interpreter.h |
| 56 | swrasterizer/clipper.h | 57 | swrasterizer/clipper.h |
| 58 | swrasterizer/fragment_lighting.h | ||
| 57 | swrasterizer/framebuffer.h | 59 | swrasterizer/framebuffer.h |
| 58 | swrasterizer/proctex.h | 60 | swrasterizer/proctex.h |
| 59 | swrasterizer/rasterizer.h | 61 | swrasterizer/rasterizer.h |
diff --git a/src/video_core/swrasterizer/fragment_lighting.cpp b/src/video_core/swrasterizer/fragment_lighting.cpp new file mode 100644 index 000000000..45a86b5cd --- /dev/null +++ b/src/video_core/swrasterizer/fragment_lighting.cpp | |||
| @@ -0,0 +1,250 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/math_util.h" | ||
| 6 | #include "video_core/swrasterizer/fragment_lighting.h" | ||
| 7 | |||
| 8 | namespace Pica { | ||
| 9 | |||
| 10 | static float LookupLightingLut(const Pica::State::Lighting& lighting, size_t lut_index, u8 index, | ||
| 11 | float delta) { | ||
| 12 | ASSERT_MSG(lut_index < lighting.luts.size(), "Out of range lut"); | ||
| 13 | ASSERT_MSG(index < lighting.luts[lut_index].size(), "Out of range index"); | ||
| 14 | |||
| 15 | const auto& lut = lighting.luts[lut_index][index]; | ||
| 16 | |||
| 17 | float lut_value = lut.ToFloat(); | ||
| 18 | float lut_diff = lut.DiffToFloat(); | ||
| 19 | |||
| 20 | return lut_value + lut_diff * delta; | ||
| 21 | } | ||
| 22 | |||
| 23 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( | ||
| 24 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, | ||
| 25 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view) { | ||
| 26 | |||
| 27 | // TODO(Subv): Bump mapping | ||
| 28 | Math::Vec3<float> surface_normal = {0.0f, 0.0f, 1.0f}; | ||
| 29 | |||
| 30 | if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) { | ||
| 31 | LOG_CRITICAL(HW_GPU, "unimplemented bump mapping"); | ||
| 32 | UNIMPLEMENTED(); | ||
| 33 | } | ||
| 34 | |||
| 35 | // Use the normalized the quaternion when performing the rotation | ||
| 36 | auto normal = Math::QuaternionRotate(normquat, surface_normal); | ||
| 37 | |||
| 38 | Math::Vec4<float> diffuse_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
| 39 | Math::Vec4<float> specular_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
| 40 | |||
| 41 | for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) { | ||
| 42 | unsigned num = lighting.light_enable.GetNum(light_index); | ||
| 43 | const auto& light_config = lighting.light[num]; | ||
| 44 | |||
| 45 | Math::Vec3<float> refl_value = {}; | ||
| 46 | Math::Vec3<float> position = {float16::FromRaw(light_config.x).ToFloat32(), | ||
| 47 | float16::FromRaw(light_config.y).ToFloat32(), | ||
| 48 | float16::FromRaw(light_config.z).ToFloat32()}; | ||
| 49 | Math::Vec3<float> light_vector; | ||
| 50 | |||
| 51 | if (light_config.config.directional) | ||
| 52 | light_vector = position; | ||
| 53 | else | ||
| 54 | light_vector = position + view; | ||
| 55 | |||
| 56 | light_vector.Normalize(); | ||
| 57 | |||
| 58 | float dist_atten = 1.0f; | ||
| 59 | if (!lighting.IsDistAttenDisabled(num)) { | ||
| 60 | auto distance = (-view - position).Length(); | ||
| 61 | float scale = Pica::float20::FromRaw(light_config.dist_atten_scale).ToFloat32(); | ||
| 62 | float bias = Pica::float20::FromRaw(light_config.dist_atten_bias).ToFloat32(); | ||
| 63 | size_t lut = | ||
| 64 | static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num; | ||
| 65 | |||
| 66 | float sample_loc = MathUtil::Clamp(scale * distance + bias, 0.0f, 1.0f); | ||
| 67 | |||
| 68 | u8 lutindex = | ||
| 69 | static_cast<u8>(MathUtil::Clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f)); | ||
| 70 | float delta = sample_loc * 256 - lutindex; | ||
| 71 | dist_atten = LookupLightingLut(lighting_state, lut, lutindex, delta); | ||
| 72 | } | ||
| 73 | |||
| 74 | auto GetLutValue = [&](LightingRegs::LightingLutInput input, bool abs, | ||
| 75 | LightingRegs::LightingScale scale_enum, | ||
| 76 | LightingRegs::LightingSampler sampler) { | ||
| 77 | Math::Vec3<float> norm_view = view.Normalized(); | ||
| 78 | Math::Vec3<float> half_angle = (norm_view + light_vector).Normalized(); | ||
| 79 | float result = 0.0f; | ||
| 80 | |||
| 81 | switch (input) { | ||
| 82 | case LightingRegs::LightingLutInput::NH: | ||
| 83 | result = Math::Dot(normal, half_angle); | ||
| 84 | break; | ||
| 85 | |||
| 86 | case LightingRegs::LightingLutInput::VH: | ||
| 87 | result = Math::Dot(norm_view, half_angle); | ||
| 88 | break; | ||
| 89 | |||
| 90 | case LightingRegs::LightingLutInput::NV: | ||
| 91 | result = Math::Dot(normal, norm_view); | ||
| 92 | break; | ||
| 93 | |||
| 94 | case LightingRegs::LightingLutInput::LN: | ||
| 95 | result = Math::Dot(light_vector, normal); | ||
| 96 | break; | ||
| 97 | |||
| 98 | default: | ||
| 99 | LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input)); | ||
| 100 | UNIMPLEMENTED(); | ||
| 101 | result = 0.0f; | ||
| 102 | } | ||
| 103 | |||
| 104 | u8 index; | ||
| 105 | float delta; | ||
| 106 | |||
| 107 | if (abs) { | ||
| 108 | if (light_config.config.two_sided_diffuse) | ||
| 109 | result = std::abs(result); | ||
| 110 | else | ||
| 111 | result = std::max(result, 0.0f); | ||
| 112 | |||
| 113 | float flr = std::floor(result * 256.0f); | ||
| 114 | index = static_cast<u8>(MathUtil::Clamp(flr, 0.0f, 255.0f)); | ||
| 115 | delta = result * 256 - index; | ||
| 116 | } else { | ||
| 117 | float flr = std::floor(result * 128.0f); | ||
| 118 | s8 signed_index = static_cast<s8>(MathUtil::Clamp(flr, -128.0f, 127.0f)); | ||
| 119 | delta = result * 128.0f - signed_index; | ||
| 120 | index = static_cast<u8>(signed_index); | ||
| 121 | } | ||
| 122 | |||
| 123 | float scale = lighting.lut_scale.GetScale(scale_enum); | ||
| 124 | return scale * | ||
| 125 | LookupLightingLut(lighting_state, static_cast<size_t>(sampler), index, delta); | ||
| 126 | }; | ||
| 127 | |||
| 128 | // Specular 0 component | ||
| 129 | float d0_lut_value = 1.0f; | ||
| 130 | if (lighting.config1.disable_lut_d0 == 0 && | ||
| 131 | LightingRegs::IsLightingSamplerSupported( | ||
| 132 | lighting.config0.config, LightingRegs::LightingSampler::Distribution0)) { | ||
| 133 | d0_lut_value = | ||
| 134 | GetLutValue(lighting.lut_input.d0, lighting.abs_lut_input.disable_d0 == 0, | ||
| 135 | lighting.lut_scale.d0, LightingRegs::LightingSampler::Distribution0); | ||
| 136 | } | ||
| 137 | |||
| 138 | Math::Vec3<float> specular_0 = d0_lut_value * light_config.specular_0.ToVec3f(); | ||
| 139 | |||
| 140 | // If enabled, lookup ReflectRed value, otherwise, 1.0 is used | ||
| 141 | if (lighting.config1.disable_lut_rr == 0 && | ||
| 142 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 143 | LightingRegs::LightingSampler::ReflectRed)) { | ||
| 144 | refl_value.x = | ||
| 145 | GetLutValue(lighting.lut_input.rr, lighting.abs_lut_input.disable_rr == 0, | ||
| 146 | lighting.lut_scale.rr, LightingRegs::LightingSampler::ReflectRed); | ||
| 147 | } else { | ||
| 148 | refl_value.x = 1.0f; | ||
| 149 | } | ||
| 150 | |||
| 151 | // If enabled, lookup ReflectGreen value, otherwise, ReflectRed value is used | ||
| 152 | if (lighting.config1.disable_lut_rg == 0 && | ||
| 153 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 154 | LightingRegs::LightingSampler::ReflectGreen)) { | ||
| 155 | refl_value.y = | ||
| 156 | GetLutValue(lighting.lut_input.rg, lighting.abs_lut_input.disable_rg == 0, | ||
| 157 | lighting.lut_scale.rg, LightingRegs::LightingSampler::ReflectGreen); | ||
| 158 | } else { | ||
| 159 | refl_value.y = refl_value.x; | ||
| 160 | } | ||
| 161 | |||
| 162 | // If enabled, lookup ReflectBlue value, otherwise, ReflectRed value is used | ||
| 163 | if (lighting.config1.disable_lut_rb == 0 && | ||
| 164 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 165 | LightingRegs::LightingSampler::ReflectBlue)) { | ||
| 166 | refl_value.z = | ||
| 167 | GetLutValue(lighting.lut_input.rb, lighting.abs_lut_input.disable_rb == 0, | ||
| 168 | lighting.lut_scale.rb, LightingRegs::LightingSampler::ReflectBlue); | ||
| 169 | } else { | ||
| 170 | refl_value.z = refl_value.x; | ||
| 171 | } | ||
| 172 | |||
| 173 | // Specular 1 component | ||
| 174 | float d1_lut_value = 1.0f; | ||
| 175 | if (lighting.config1.disable_lut_d1 == 0 && | ||
| 176 | LightingRegs::IsLightingSamplerSupported( | ||
| 177 | lighting.config0.config, LightingRegs::LightingSampler::Distribution1)) { | ||
| 178 | d1_lut_value = | ||
| 179 | GetLutValue(lighting.lut_input.d1, lighting.abs_lut_input.disable_d1 == 0, | ||
| 180 | lighting.lut_scale.d1, LightingRegs::LightingSampler::Distribution1); | ||
| 181 | } | ||
| 182 | |||
| 183 | Math::Vec3<float> specular_1 = | ||
| 184 | d1_lut_value * refl_value * light_config.specular_1.ToVec3f(); | ||
| 185 | |||
| 186 | // Fresnel | ||
| 187 | if (lighting.config1.disable_lut_fr == 0 && | ||
| 188 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 189 | LightingRegs::LightingSampler::Fresnel)) { | ||
| 190 | |||
| 191 | float lut_value = | ||
| 192 | GetLutValue(lighting.lut_input.fr, lighting.abs_lut_input.disable_fr == 0, | ||
| 193 | lighting.lut_scale.fr, LightingRegs::LightingSampler::Fresnel); | ||
| 194 | |||
| 195 | // Enabled for diffuse lighting alpha component | ||
| 196 | if (lighting.config0.fresnel_selector == | ||
| 197 | LightingRegs::LightingFresnelSelector::PrimaryAlpha || | ||
| 198 | lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { | ||
| 199 | diffuse_sum.a() *= lut_value; | ||
| 200 | } | ||
| 201 | |||
| 202 | // Enabled for the specular lighting alpha component | ||
| 203 | if (lighting.config0.fresnel_selector == | ||
| 204 | LightingRegs::LightingFresnelSelector::SecondaryAlpha || | ||
| 205 | lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { | ||
| 206 | specular_sum.a() *= lut_value; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | auto dot_product = Math::Dot(light_vector, normal); | ||
| 211 | |||
| 212 | // Calculate clamp highlights before applying the two-sided diffuse configuration to the dot | ||
| 213 | // product. | ||
| 214 | float clamp_highlights = 1.0f; | ||
| 215 | if (lighting.config0.clamp_highlights) { | ||
| 216 | if (dot_product <= 0.0f) | ||
| 217 | clamp_highlights = 0.0f; | ||
| 218 | else | ||
| 219 | clamp_highlights = 1.0f; | ||
| 220 | } | ||
| 221 | |||
| 222 | if (light_config.config.two_sided_diffuse) | ||
| 223 | dot_product = std::abs(dot_product); | ||
| 224 | else | ||
| 225 | dot_product = std::max(dot_product, 0.0f); | ||
| 226 | |||
| 227 | auto diffuse = | ||
| 228 | light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f(); | ||
| 229 | diffuse_sum += Math::MakeVec(diffuse * dist_atten, 0.0f); | ||
| 230 | |||
| 231 | specular_sum += | ||
| 232 | Math::MakeVec((specular_0 + specular_1) * clamp_highlights * dist_atten, 0.0f); | ||
| 233 | } | ||
| 234 | |||
| 235 | diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f); | ||
| 236 | |||
| 237 | auto diffuse = Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, | ||
| 238 | MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, | ||
| 239 | MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, | ||
| 240 | MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255) | ||
| 241 | .Cast<u8>(); | ||
| 242 | auto specular = Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, | ||
| 243 | MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, | ||
| 244 | MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, | ||
| 245 | MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255) | ||
| 246 | .Cast<u8>(); | ||
| 247 | return {diffuse, specular}; | ||
| 248 | } | ||
| 249 | |||
| 250 | } // namespace Pica | ||
diff --git a/src/video_core/swrasterizer/fragment_lighting.h b/src/video_core/swrasterizer/fragment_lighting.h new file mode 100644 index 000000000..438dca926 --- /dev/null +++ b/src/video_core/swrasterizer/fragment_lighting.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <tuple> | ||
| 8 | #include "common/quaternion.h" | ||
| 9 | #include "common/vector_math.h" | ||
| 10 | #include "video_core/pica_state.h" | ||
| 11 | |||
| 12 | namespace Pica { | ||
| 13 | |||
| 14 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( | ||
| 15 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, | ||
| 16 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view); | ||
| 17 | |||
| 18 | } // namespace Pica | ||
diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp index aee630954..bc7e1c56c 100644 --- a/src/video_core/swrasterizer/rasterizer.cpp +++ b/src/video_core/swrasterizer/rasterizer.cpp | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include "video_core/regs_rasterizer.h" | 24 | #include "video_core/regs_rasterizer.h" |
| 25 | #include "video_core/regs_texturing.h" | 25 | #include "video_core/regs_texturing.h" |
| 26 | #include "video_core/shader/shader.h" | 26 | #include "video_core/shader/shader.h" |
| 27 | #include "video_core/swrasterizer/fragment_lighting.h" | ||
| 27 | #include "video_core/swrasterizer/framebuffer.h" | 28 | #include "video_core/swrasterizer/framebuffer.h" |
| 28 | #include "video_core/swrasterizer/proctex.h" | 29 | #include "video_core/swrasterizer/proctex.h" |
| 29 | #include "video_core/swrasterizer/rasterizer.h" | 30 | #include "video_core/swrasterizer/rasterizer.h" |
| @@ -115,246 +116,6 @@ static std::tuple<float24, float24, PAddr> ConvertCubeCoord(float24 u, float24 v | |||
| 115 | return std::make_tuple(x / z * half + half, y / z * half + half, addr); | 116 | return std::make_tuple(x / z * half + half, y / z * half + half, addr); |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 118 | static float LookupLightingLut(const Pica::State::Lighting& lighting, size_t lut_index, u8 index, | ||
| 119 | float delta) { | ||
| 120 | ASSERT_MSG(lut_index < lighting.luts.size(), "Out of range lut"); | ||
| 121 | ASSERT_MSG(index < lighting.luts[lut_index].size(), "Out of range index"); | ||
| 122 | |||
| 123 | const auto& lut = lighting.luts[lut_index][index]; | ||
| 124 | |||
| 125 | float lut_value = lut.ToFloat(); | ||
| 126 | float lut_diff = lut.DiffToFloat(); | ||
| 127 | |||
| 128 | return lut_value + lut_diff * delta; | ||
| 129 | } | ||
| 130 | |||
| 131 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( | ||
| 132 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, | ||
| 133 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view) { | ||
| 134 | |||
| 135 | // TODO(Subv): Bump mapping | ||
| 136 | Math::Vec3<float> surface_normal = {0.0f, 0.0f, 1.0f}; | ||
| 137 | |||
| 138 | if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) { | ||
| 139 | LOG_CRITICAL(HW_GPU, "unimplemented bump mapping"); | ||
| 140 | UNIMPLEMENTED(); | ||
| 141 | } | ||
| 142 | |||
| 143 | // Use the normalized the quaternion when performing the rotation | ||
| 144 | auto normal = Math::QuaternionRotate(normquat, surface_normal); | ||
| 145 | |||
| 146 | Math::Vec4<float> diffuse_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
| 147 | Math::Vec4<float> specular_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
| 148 | |||
| 149 | for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) { | ||
| 150 | unsigned num = lighting.light_enable.GetNum(light_index); | ||
| 151 | const auto& light_config = lighting.light[num]; | ||
| 152 | |||
| 153 | Math::Vec3<float> refl_value = {}; | ||
| 154 | Math::Vec3<float> position = {float16::FromRaw(light_config.x).ToFloat32(), | ||
| 155 | float16::FromRaw(light_config.y).ToFloat32(), | ||
| 156 | float16::FromRaw(light_config.z).ToFloat32()}; | ||
| 157 | Math::Vec3<float> light_vector; | ||
| 158 | |||
| 159 | if (light_config.config.directional) | ||
| 160 | light_vector = position; | ||
| 161 | else | ||
| 162 | light_vector = position + view; | ||
| 163 | |||
| 164 | light_vector.Normalize(); | ||
| 165 | |||
| 166 | float dist_atten = 1.0f; | ||
| 167 | if (!lighting.IsDistAttenDisabled(num)) { | ||
| 168 | auto distance = (-view - position).Length(); | ||
| 169 | float scale = Pica::float20::FromRaw(light_config.dist_atten_scale).ToFloat32(); | ||
| 170 | float bias = Pica::float20::FromRaw(light_config.dist_atten_bias).ToFloat32(); | ||
| 171 | size_t lut = | ||
| 172 | static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num; | ||
| 173 | |||
| 174 | float sample_loc = MathUtil::Clamp(scale * distance + bias, 0.0f, 1.0f); | ||
| 175 | |||
| 176 | u8 lutindex = | ||
| 177 | static_cast<u8>(MathUtil::Clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f)); | ||
| 178 | float delta = sample_loc * 256 - lutindex; | ||
| 179 | dist_atten = LookupLightingLut(lighting_state, lut, lutindex, delta); | ||
| 180 | } | ||
| 181 | |||
| 182 | auto GetLutValue = [&](LightingRegs::LightingLutInput input, bool abs, | ||
| 183 | LightingRegs::LightingScale scale_enum, | ||
| 184 | LightingRegs::LightingSampler sampler) { | ||
| 185 | Math::Vec3<float> norm_view = view.Normalized(); | ||
| 186 | Math::Vec3<float> half_angle = (norm_view + light_vector).Normalized(); | ||
| 187 | float result = 0.0f; | ||
| 188 | |||
| 189 | switch (input) { | ||
| 190 | case LightingRegs::LightingLutInput::NH: | ||
| 191 | result = Math::Dot(normal, half_angle); | ||
| 192 | break; | ||
| 193 | |||
| 194 | case LightingRegs::LightingLutInput::VH: | ||
| 195 | result = Math::Dot(norm_view, half_angle); | ||
| 196 | break; | ||
| 197 | |||
| 198 | case LightingRegs::LightingLutInput::NV: | ||
| 199 | result = Math::Dot(normal, norm_view); | ||
| 200 | break; | ||
| 201 | |||
| 202 | case LightingRegs::LightingLutInput::LN: | ||
| 203 | result = Math::Dot(light_vector, normal); | ||
| 204 | break; | ||
| 205 | |||
| 206 | default: | ||
| 207 | LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input)); | ||
| 208 | UNIMPLEMENTED(); | ||
| 209 | result = 0.0f; | ||
| 210 | } | ||
| 211 | |||
| 212 | u8 index; | ||
| 213 | float delta; | ||
| 214 | |||
| 215 | if (abs) { | ||
| 216 | if (light_config.config.two_sided_diffuse) | ||
| 217 | result = std::abs(result); | ||
| 218 | else | ||
| 219 | result = std::max(result, 0.0f); | ||
| 220 | |||
| 221 | float flr = std::floor(result * 256.0f); | ||
| 222 | index = static_cast<u8>(MathUtil::Clamp(flr, 0.0f, 255.0f)); | ||
| 223 | delta = result * 256 - index; | ||
| 224 | } else { | ||
| 225 | float flr = std::floor(result * 128.0f); | ||
| 226 | s8 signed_index = static_cast<s8>(MathUtil::Clamp(flr, -128.0f, 127.0f)); | ||
| 227 | delta = result * 128.0f - signed_index; | ||
| 228 | index = static_cast<u8>(signed_index); | ||
| 229 | } | ||
| 230 | |||
| 231 | float scale = lighting.lut_scale.GetScale(scale_enum); | ||
| 232 | return scale * | ||
| 233 | LookupLightingLut(lighting_state, static_cast<size_t>(sampler), index, delta); | ||
| 234 | }; | ||
| 235 | |||
| 236 | // Specular 0 component | ||
| 237 | float d0_lut_value = 1.0f; | ||
| 238 | if (lighting.config1.disable_lut_d0 == 0 && | ||
| 239 | LightingRegs::IsLightingSamplerSupported( | ||
| 240 | lighting.config0.config, LightingRegs::LightingSampler::Distribution0)) { | ||
| 241 | d0_lut_value = | ||
| 242 | GetLutValue(lighting.lut_input.d0, lighting.abs_lut_input.disable_d0 == 0, | ||
| 243 | lighting.lut_scale.d0, LightingRegs::LightingSampler::Distribution0); | ||
| 244 | } | ||
| 245 | |||
| 246 | Math::Vec3<float> specular_0 = d0_lut_value * light_config.specular_0.ToVec3f(); | ||
| 247 | |||
| 248 | // If enabled, lookup ReflectRed value, otherwise, 1.0 is used | ||
| 249 | if (lighting.config1.disable_lut_rr == 0 && | ||
| 250 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 251 | LightingRegs::LightingSampler::ReflectRed)) { | ||
| 252 | refl_value.x = | ||
| 253 | GetLutValue(lighting.lut_input.rr, lighting.abs_lut_input.disable_rr == 0, | ||
| 254 | lighting.lut_scale.rr, LightingRegs::LightingSampler::ReflectRed); | ||
| 255 | } else { | ||
| 256 | refl_value.x = 1.0f; | ||
| 257 | } | ||
| 258 | |||
| 259 | // If enabled, lookup ReflectGreen value, otherwise, ReflectRed value is used | ||
| 260 | if (lighting.config1.disable_lut_rg == 0 && | ||
| 261 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 262 | LightingRegs::LightingSampler::ReflectGreen)) { | ||
| 263 | refl_value.y = | ||
| 264 | GetLutValue(lighting.lut_input.rg, lighting.abs_lut_input.disable_rg == 0, | ||
| 265 | lighting.lut_scale.rg, LightingRegs::LightingSampler::ReflectGreen); | ||
| 266 | } else { | ||
| 267 | refl_value.y = refl_value.x; | ||
| 268 | } | ||
| 269 | |||
| 270 | // If enabled, lookup ReflectBlue value, otherwise, ReflectRed value is used | ||
| 271 | if (lighting.config1.disable_lut_rb == 0 && | ||
| 272 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 273 | LightingRegs::LightingSampler::ReflectBlue)) { | ||
| 274 | refl_value.z = | ||
| 275 | GetLutValue(lighting.lut_input.rb, lighting.abs_lut_input.disable_rb == 0, | ||
| 276 | lighting.lut_scale.rb, LightingRegs::LightingSampler::ReflectBlue); | ||
| 277 | } else { | ||
| 278 | refl_value.z = refl_value.x; | ||
| 279 | } | ||
| 280 | |||
| 281 | // Specular 1 component | ||
| 282 | float d1_lut_value = 1.0f; | ||
| 283 | if (lighting.config1.disable_lut_d1 == 0 && | ||
| 284 | LightingRegs::IsLightingSamplerSupported( | ||
| 285 | lighting.config0.config, LightingRegs::LightingSampler::Distribution1)) { | ||
| 286 | d1_lut_value = | ||
| 287 | GetLutValue(lighting.lut_input.d1, lighting.abs_lut_input.disable_d1 == 0, | ||
| 288 | lighting.lut_scale.d1, LightingRegs::LightingSampler::Distribution1); | ||
| 289 | } | ||
| 290 | |||
| 291 | Math::Vec3<float> specular_1 = | ||
| 292 | d1_lut_value * refl_value * light_config.specular_1.ToVec3f(); | ||
| 293 | |||
| 294 | // Fresnel | ||
| 295 | if (lighting.config1.disable_lut_fr == 0 && | ||
| 296 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 297 | LightingRegs::LightingSampler::Fresnel)) { | ||
| 298 | |||
| 299 | float lut_value = | ||
| 300 | GetLutValue(lighting.lut_input.fr, lighting.abs_lut_input.disable_fr == 0, | ||
| 301 | lighting.lut_scale.fr, LightingRegs::LightingSampler::Fresnel); | ||
| 302 | |||
| 303 | // Enabled for diffuse lighting alpha component | ||
| 304 | if (lighting.config0.fresnel_selector == | ||
| 305 | LightingRegs::LightingFresnelSelector::PrimaryAlpha || | ||
| 306 | lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { | ||
| 307 | diffuse_sum.a() *= lut_value; | ||
| 308 | } | ||
| 309 | |||
| 310 | // Enabled for the specular lighting alpha component | ||
| 311 | if (lighting.config0.fresnel_selector == | ||
| 312 | LightingRegs::LightingFresnelSelector::SecondaryAlpha || | ||
| 313 | lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { | ||
| 314 | specular_sum.a() *= lut_value; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | auto dot_product = Math::Dot(light_vector, normal); | ||
| 319 | |||
| 320 | // Calculate clamp highlights before applying the two-sided diffuse configuration to the dot | ||
| 321 | // product. | ||
| 322 | float clamp_highlights = 1.0f; | ||
| 323 | if (lighting.config0.clamp_highlights) { | ||
| 324 | if (dot_product <= 0.0f) | ||
| 325 | clamp_highlights = 0.0f; | ||
| 326 | else | ||
| 327 | clamp_highlights = 1.0f; | ||
| 328 | } | ||
| 329 | |||
| 330 | if (light_config.config.two_sided_diffuse) | ||
| 331 | dot_product = std::abs(dot_product); | ||
| 332 | else | ||
| 333 | dot_product = std::max(dot_product, 0.0f); | ||
| 334 | |||
| 335 | auto diffuse = | ||
| 336 | light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f(); | ||
| 337 | diffuse_sum += Math::MakeVec(diffuse * dist_atten, 0.0f); | ||
| 338 | |||
| 339 | specular_sum += | ||
| 340 | Math::MakeVec((specular_0 + specular_1) * clamp_highlights * dist_atten, 0.0f); | ||
| 341 | } | ||
| 342 | |||
| 343 | diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f); | ||
| 344 | |||
| 345 | auto diffuse = Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, | ||
| 346 | MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, | ||
| 347 | MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, | ||
| 348 | MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255) | ||
| 349 | .Cast<u8>(); | ||
| 350 | auto specular = Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, | ||
| 351 | MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, | ||
| 352 | MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, | ||
| 353 | MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255) | ||
| 354 | .Cast<u8>(); | ||
| 355 | return {diffuse, specular}; | ||
| 356 | } | ||
| 357 | |||
| 358 | MICROPROFILE_DEFINE(GPU_Rasterization, "GPU", "Rasterization", MP_RGB(50, 50, 240)); | 119 | MICROPROFILE_DEFINE(GPU_Rasterization, "GPU", "Rasterization", MP_RGB(50, 50, 240)); |
| 359 | 120 | ||
| 360 | /** | 121 | /** |