diff options
Diffstat (limited to 'src/video_core/swrasterizer/lighting.cpp')
| -rw-r--r-- | src/video_core/swrasterizer/lighting.cpp | 308 |
1 files changed, 0 insertions, 308 deletions
diff --git a/src/video_core/swrasterizer/lighting.cpp b/src/video_core/swrasterizer/lighting.cpp deleted file mode 100644 index 5fa748611..000000000 --- a/src/video_core/swrasterizer/lighting.cpp +++ /dev/null | |||
| @@ -1,308 +0,0 @@ | |||
| 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/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 | const Math::Vec4<u8> (&texture_color)[4]) { | ||
| 27 | |||
| 28 | Math::Vec3<float> surface_normal; | ||
| 29 | Math::Vec3<float> surface_tangent; | ||
| 30 | |||
| 31 | if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) { | ||
| 32 | Math::Vec3<float> perturbation = | ||
| 33 | texture_color[lighting.config0.bump_selector].xyz().Cast<float>() / 127.5f - | ||
| 34 | Math::MakeVec(1.0f, 1.0f, 1.0f); | ||
| 35 | if (lighting.config0.bump_mode == LightingRegs::LightingBumpMode::NormalMap) { | ||
| 36 | if (!lighting.config0.disable_bump_renorm) { | ||
| 37 | const float z_square = 1 - perturbation.xy().Length2(); | ||
| 38 | perturbation.z = std::sqrt(std::max(z_square, 0.0f)); | ||
| 39 | } | ||
| 40 | surface_normal = perturbation; | ||
| 41 | surface_tangent = Math::MakeVec(1.0f, 0.0f, 0.0f); | ||
| 42 | } else if (lighting.config0.bump_mode == LightingRegs::LightingBumpMode::TangentMap) { | ||
| 43 | surface_normal = Math::MakeVec(0.0f, 0.0f, 1.0f); | ||
| 44 | surface_tangent = perturbation; | ||
| 45 | } else { | ||
| 46 | LOG_ERROR(HW_GPU, "Unknown bump mode %u", lighting.config0.bump_mode.Value()); | ||
| 47 | } | ||
| 48 | } else { | ||
| 49 | surface_normal = Math::MakeVec(0.0f, 0.0f, 1.0f); | ||
| 50 | surface_tangent = Math::MakeVec(1.0f, 0.0f, 0.0f); | ||
| 51 | } | ||
| 52 | |||
| 53 | // Use the normalized the quaternion when performing the rotation | ||
| 54 | auto normal = Math::QuaternionRotate(normquat, surface_normal); | ||
| 55 | auto tangent = Math::QuaternionRotate(normquat, surface_tangent); | ||
| 56 | |||
| 57 | Math::Vec4<float> diffuse_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
| 58 | Math::Vec4<float> specular_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
| 59 | |||
| 60 | for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) { | ||
| 61 | unsigned num = lighting.light_enable.GetNum(light_index); | ||
| 62 | const auto& light_config = lighting.light[num]; | ||
| 63 | |||
| 64 | Math::Vec3<float> refl_value = {}; | ||
| 65 | Math::Vec3<float> position = {float16::FromRaw(light_config.x).ToFloat32(), | ||
| 66 | float16::FromRaw(light_config.y).ToFloat32(), | ||
| 67 | float16::FromRaw(light_config.z).ToFloat32()}; | ||
| 68 | Math::Vec3<float> light_vector; | ||
| 69 | |||
| 70 | if (light_config.config.directional) | ||
| 71 | light_vector = position; | ||
| 72 | else | ||
| 73 | light_vector = position + view; | ||
| 74 | |||
| 75 | light_vector.Normalize(); | ||
| 76 | |||
| 77 | Math::Vec3<float> norm_view = view.Normalized(); | ||
| 78 | Math::Vec3<float> half_vector = norm_view + light_vector; | ||
| 79 | |||
| 80 | float dist_atten = 1.0f; | ||
| 81 | if (!lighting.IsDistAttenDisabled(num)) { | ||
| 82 | auto distance = (-view - position).Length(); | ||
| 83 | float scale = Pica::float20::FromRaw(light_config.dist_atten_scale).ToFloat32(); | ||
| 84 | float bias = Pica::float20::FromRaw(light_config.dist_atten_bias).ToFloat32(); | ||
| 85 | size_t lut = | ||
| 86 | static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num; | ||
| 87 | |||
| 88 | float sample_loc = MathUtil::Clamp(scale * distance + bias, 0.0f, 1.0f); | ||
| 89 | |||
| 90 | u8 lutindex = | ||
| 91 | static_cast<u8>(MathUtil::Clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f)); | ||
| 92 | float delta = sample_loc * 256 - lutindex; | ||
| 93 | dist_atten = LookupLightingLut(lighting_state, lut, lutindex, delta); | ||
| 94 | } | ||
| 95 | |||
| 96 | auto GetLutValue = [&](LightingRegs::LightingLutInput input, bool abs, | ||
| 97 | LightingRegs::LightingScale scale_enum, | ||
| 98 | LightingRegs::LightingSampler sampler) { | ||
| 99 | float result = 0.0f; | ||
| 100 | |||
| 101 | switch (input) { | ||
| 102 | case LightingRegs::LightingLutInput::NH: | ||
| 103 | result = Math::Dot(normal, half_vector.Normalized()); | ||
| 104 | break; | ||
| 105 | |||
| 106 | case LightingRegs::LightingLutInput::VH: | ||
| 107 | result = Math::Dot(norm_view, half_vector.Normalized()); | ||
| 108 | break; | ||
| 109 | |||
| 110 | case LightingRegs::LightingLutInput::NV: | ||
| 111 | result = Math::Dot(normal, norm_view); | ||
| 112 | break; | ||
| 113 | |||
| 114 | case LightingRegs::LightingLutInput::LN: | ||
| 115 | result = Math::Dot(light_vector, normal); | ||
| 116 | break; | ||
| 117 | |||
| 118 | case LightingRegs::LightingLutInput::SP: { | ||
| 119 | Math::Vec3<s32> spot_dir{light_config.spot_x.Value(), light_config.spot_y.Value(), | ||
| 120 | light_config.spot_z.Value()}; | ||
| 121 | result = Math::Dot(light_vector, spot_dir.Cast<float>() / 2047.0f); | ||
| 122 | break; | ||
| 123 | } | ||
| 124 | case LightingRegs::LightingLutInput::CP: | ||
| 125 | if (lighting.config0.config == LightingRegs::LightingConfig::Config7) { | ||
| 126 | const Math::Vec3<float> norm_half_vector = half_vector.Normalized(); | ||
| 127 | const Math::Vec3<float> half_vector_proj = | ||
| 128 | norm_half_vector - normal * Math::Dot(normal, norm_half_vector); | ||
| 129 | result = Math::Dot(half_vector_proj, tangent); | ||
| 130 | } else { | ||
| 131 | result = 0.0f; | ||
| 132 | } | ||
| 133 | break; | ||
| 134 | default: | ||
| 135 | LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input)); | ||
| 136 | UNIMPLEMENTED(); | ||
| 137 | result = 0.0f; | ||
| 138 | } | ||
| 139 | |||
| 140 | u8 index; | ||
| 141 | float delta; | ||
| 142 | |||
| 143 | if (abs) { | ||
| 144 | if (light_config.config.two_sided_diffuse) | ||
| 145 | result = std::abs(result); | ||
| 146 | else | ||
| 147 | result = std::max(result, 0.0f); | ||
| 148 | |||
| 149 | float flr = std::floor(result * 256.0f); | ||
| 150 | index = static_cast<u8>(MathUtil::Clamp(flr, 0.0f, 255.0f)); | ||
| 151 | delta = result * 256 - index; | ||
| 152 | } else { | ||
| 153 | float flr = std::floor(result * 128.0f); | ||
| 154 | s8 signed_index = static_cast<s8>(MathUtil::Clamp(flr, -128.0f, 127.0f)); | ||
| 155 | delta = result * 128.0f - signed_index; | ||
| 156 | index = static_cast<u8>(signed_index); | ||
| 157 | } | ||
| 158 | |||
| 159 | float scale = lighting.lut_scale.GetScale(scale_enum); | ||
| 160 | return scale * | ||
| 161 | LookupLightingLut(lighting_state, static_cast<size_t>(sampler), index, delta); | ||
| 162 | }; | ||
| 163 | |||
| 164 | // If enabled, compute spot light attenuation value | ||
| 165 | float spot_atten = 1.0f; | ||
| 166 | if (!lighting.IsSpotAttenDisabled(num) && | ||
| 167 | LightingRegs::IsLightingSamplerSupported( | ||
| 168 | lighting.config0.config, LightingRegs::LightingSampler::SpotlightAttenuation)) { | ||
| 169 | auto lut = LightingRegs::SpotlightAttenuationSampler(num); | ||
| 170 | spot_atten = GetLutValue(lighting.lut_input.sp, lighting.abs_lut_input.disable_sp == 0, | ||
| 171 | lighting.lut_scale.sp, lut); | ||
| 172 | } | ||
| 173 | |||
| 174 | // Specular 0 component | ||
| 175 | float d0_lut_value = 1.0f; | ||
| 176 | if (lighting.config1.disable_lut_d0 == 0 && | ||
| 177 | LightingRegs::IsLightingSamplerSupported( | ||
| 178 | lighting.config0.config, LightingRegs::LightingSampler::Distribution0)) { | ||
| 179 | d0_lut_value = | ||
| 180 | GetLutValue(lighting.lut_input.d0, lighting.abs_lut_input.disable_d0 == 0, | ||
| 181 | lighting.lut_scale.d0, LightingRegs::LightingSampler::Distribution0); | ||
| 182 | } | ||
| 183 | |||
| 184 | Math::Vec3<float> specular_0 = d0_lut_value * light_config.specular_0.ToVec3f(); | ||
| 185 | |||
| 186 | // If enabled, lookup ReflectRed value, otherwise, 1.0 is used | ||
| 187 | if (lighting.config1.disable_lut_rr == 0 && | ||
| 188 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 189 | LightingRegs::LightingSampler::ReflectRed)) { | ||
| 190 | refl_value.x = | ||
| 191 | GetLutValue(lighting.lut_input.rr, lighting.abs_lut_input.disable_rr == 0, | ||
| 192 | lighting.lut_scale.rr, LightingRegs::LightingSampler::ReflectRed); | ||
| 193 | } else { | ||
| 194 | refl_value.x = 1.0f; | ||
| 195 | } | ||
| 196 | |||
| 197 | // If enabled, lookup ReflectGreen value, otherwise, ReflectRed value is used | ||
| 198 | if (lighting.config1.disable_lut_rg == 0 && | ||
| 199 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 200 | LightingRegs::LightingSampler::ReflectGreen)) { | ||
| 201 | refl_value.y = | ||
| 202 | GetLutValue(lighting.lut_input.rg, lighting.abs_lut_input.disable_rg == 0, | ||
| 203 | lighting.lut_scale.rg, LightingRegs::LightingSampler::ReflectGreen); | ||
| 204 | } else { | ||
| 205 | refl_value.y = refl_value.x; | ||
| 206 | } | ||
| 207 | |||
| 208 | // If enabled, lookup ReflectBlue value, otherwise, ReflectRed value is used | ||
| 209 | if (lighting.config1.disable_lut_rb == 0 && | ||
| 210 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 211 | LightingRegs::LightingSampler::ReflectBlue)) { | ||
| 212 | refl_value.z = | ||
| 213 | GetLutValue(lighting.lut_input.rb, lighting.abs_lut_input.disable_rb == 0, | ||
| 214 | lighting.lut_scale.rb, LightingRegs::LightingSampler::ReflectBlue); | ||
| 215 | } else { | ||
| 216 | refl_value.z = refl_value.x; | ||
| 217 | } | ||
| 218 | |||
| 219 | // Specular 1 component | ||
| 220 | float d1_lut_value = 1.0f; | ||
| 221 | if (lighting.config1.disable_lut_d1 == 0 && | ||
| 222 | LightingRegs::IsLightingSamplerSupported( | ||
| 223 | lighting.config0.config, LightingRegs::LightingSampler::Distribution1)) { | ||
| 224 | d1_lut_value = | ||
| 225 | GetLutValue(lighting.lut_input.d1, lighting.abs_lut_input.disable_d1 == 0, | ||
| 226 | lighting.lut_scale.d1, LightingRegs::LightingSampler::Distribution1); | ||
| 227 | } | ||
| 228 | |||
| 229 | Math::Vec3<float> specular_1 = | ||
| 230 | d1_lut_value * refl_value * light_config.specular_1.ToVec3f(); | ||
| 231 | |||
| 232 | // Fresnel | ||
| 233 | // Note: only the last entry in the light slots applies the Fresnel factor | ||
| 234 | if (light_index == lighting.max_light_index && lighting.config1.disable_lut_fr == 0 && | ||
| 235 | LightingRegs::IsLightingSamplerSupported(lighting.config0.config, | ||
| 236 | LightingRegs::LightingSampler::Fresnel)) { | ||
| 237 | |||
| 238 | float lut_value = | ||
| 239 | GetLutValue(lighting.lut_input.fr, lighting.abs_lut_input.disable_fr == 0, | ||
| 240 | lighting.lut_scale.fr, LightingRegs::LightingSampler::Fresnel); | ||
| 241 | |||
| 242 | // Enabled for diffuse lighting alpha component | ||
| 243 | if (lighting.config0.fresnel_selector == | ||
| 244 | LightingRegs::LightingFresnelSelector::PrimaryAlpha || | ||
| 245 | lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { | ||
| 246 | diffuse_sum.a() = lut_value; | ||
| 247 | } | ||
| 248 | |||
| 249 | // Enabled for the specular lighting alpha component | ||
| 250 | if (lighting.config0.fresnel_selector == | ||
| 251 | LightingRegs::LightingFresnelSelector::SecondaryAlpha || | ||
| 252 | lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) { | ||
| 253 | specular_sum.a() = lut_value; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | auto dot_product = Math::Dot(light_vector, normal); | ||
| 258 | |||
| 259 | // Calculate clamp highlights before applying the two-sided diffuse configuration to the dot | ||
| 260 | // product. | ||
| 261 | float clamp_highlights = 1.0f; | ||
| 262 | if (lighting.config0.clamp_highlights) { | ||
| 263 | if (dot_product <= 0.0f) | ||
| 264 | clamp_highlights = 0.0f; | ||
| 265 | else | ||
| 266 | clamp_highlights = 1.0f; | ||
| 267 | } | ||
| 268 | |||
| 269 | if (light_config.config.two_sided_diffuse) | ||
| 270 | dot_product = std::abs(dot_product); | ||
| 271 | else | ||
| 272 | dot_product = std::max(dot_product, 0.0f); | ||
| 273 | |||
| 274 | if (light_config.config.geometric_factor_0 || light_config.config.geometric_factor_1) { | ||
| 275 | float geo_factor = half_vector.Length2(); | ||
| 276 | geo_factor = geo_factor == 0.0f ? 0.0f : std::min(dot_product / geo_factor, 1.0f); | ||
| 277 | if (light_config.config.geometric_factor_0) { | ||
| 278 | specular_0 *= geo_factor; | ||
| 279 | } | ||
| 280 | if (light_config.config.geometric_factor_1) { | ||
| 281 | specular_1 *= geo_factor; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | auto diffuse = | ||
| 286 | light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f(); | ||
| 287 | diffuse_sum += Math::MakeVec(diffuse * dist_atten * spot_atten, 0.0f); | ||
| 288 | |||
| 289 | specular_sum += Math::MakeVec( | ||
| 290 | (specular_0 + specular_1) * clamp_highlights * dist_atten * spot_atten, 0.0f); | ||
| 291 | } | ||
| 292 | |||
| 293 | diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f); | ||
| 294 | |||
| 295 | auto diffuse = Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, | ||
| 296 | MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, | ||
| 297 | MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, | ||
| 298 | MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255) | ||
| 299 | .Cast<u8>(); | ||
| 300 | auto specular = Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, | ||
| 301 | MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, | ||
| 302 | MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, | ||
| 303 | MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255) | ||
| 304 | .Cast<u8>(); | ||
| 305 | return std::make_tuple(diffuse, specular); | ||
| 306 | } | ||
| 307 | |||
| 308 | } // namespace Pica | ||