diff options
| author | 2017-06-09 14:25:41 -0500 | |
|---|---|---|
| committer | 2017-07-11 19:39:15 +0300 | |
| commit | be25e78b07140cb745387f757001dd04b3b4cc64 (patch) | |
| tree | d136030d3a3c2591f30318b23845b39aec5d25ed | |
| parent | SwRasterizer: Implement primary fragment color. (diff) | |
| download | yuzu-be25e78b07140cb745387f757001dd04b3b4cc64.tar.gz yuzu-be25e78b07140cb745387f757001dd04b3b4cc64.tar.xz yuzu-be25e78b07140cb745387f757001dd04b3b4cc64.zip | |
SwRasterizer: Calculate specular_0 for fragment lighting.
| -rw-r--r-- | src/video_core/swrasterizer/rasterizer.cpp | 107 |
1 files changed, 94 insertions, 13 deletions
diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp index 1ab41c2df..34b84b0af 100644 --- a/src/video_core/swrasterizer/rasterizer.cpp +++ b/src/video_core/swrasterizer/rasterizer.cpp | |||
| @@ -115,6 +115,20 @@ 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); | 115 | return std::make_tuple(x / z * half + half, y / z * half + half, addr); |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | |||
| 119 | float LookupLightingLut(size_t lut_index, float index) { | ||
| 120 | unsigned index_i = static_cast<unsigned>(MathUtil::Clamp(floor(index * 256), 0.0f, 1.0f)); | ||
| 121 | |||
| 122 | float index_f = index - index_i; | ||
| 123 | |||
| 124 | ASSERT_MSG(lut_index < g_state.lighting.luts.size(), "Out of range lut"); | ||
| 125 | |||
| 126 | float lut_value = g_state.lighting.luts[lut_index][index_i].ToFloat(); | ||
| 127 | float lut_diff = g_state.lighting.luts[lut_index][index_i].DiffToFloat(); | ||
| 128 | |||
| 129 | return lut_value + lut_diff * index_f; | ||
| 130 | } | ||
| 131 | |||
| 118 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view) { | 132 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view) { |
| 119 | const auto& lighting = g_state.regs.lighting; | 133 | const auto& lighting = g_state.regs.lighting; |
| 120 | 134 | ||
| @@ -133,9 +147,9 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(const Math::Qu | |||
| 133 | auto normal = Math::QuaternionRotate(normquat, surface_normal); | 147 | auto normal = Math::QuaternionRotate(normquat, surface_normal); |
| 134 | 148 | ||
| 135 | Math::Vec3<float> light_vector = {}; | 149 | Math::Vec3<float> light_vector = {}; |
| 136 | Math::Vec3<float> diffuse_sum = {}; | 150 | Math::Vec4<float> diffuse_sum = {0.f, 0.f, 0.f, 1.f}; |
| 137 | // TODO(Subv): Calculate specular | 151 | // TODO(Subv): Calculate specular |
| 138 | Math::Vec3<float> specular_sum = {}; | 152 | Math::Vec4<float> specular_sum = {0.f, 0.f, 0.f, 1.f}; |
| 139 | 153 | ||
| 140 | for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) { | 154 | for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) { |
| 141 | unsigned num = lighting.light_enable.GetNum(light_index); | 155 | unsigned num = lighting.light_enable.GetNum(light_index); |
| @@ -150,7 +164,8 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(const Math::Qu | |||
| 150 | 164 | ||
| 151 | light_vector.Normalize(); | 165 | light_vector.Normalize(); |
| 152 | 166 | ||
| 153 | auto dot_product = Math::Dot(light_vector, normal); | 167 | auto LV_N = Math::Dot(light_vector, normal); |
| 168 | auto dot_product = LV_N; | ||
| 154 | 169 | ||
| 155 | if (light_config.config.two_sided_diffuse) | 170 | if (light_config.config.two_sided_diffuse) |
| 156 | dot_product = std::abs(dot_product); | 171 | dot_product = std::abs(dot_product); |
| @@ -165,26 +180,92 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(const Math::Qu | |||
| 165 | size_t lut = static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num; | 180 | size_t lut = static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num; |
| 166 | 181 | ||
| 167 | float sample_loc = scale * distance + bias; | 182 | float sample_loc = scale * distance + bias; |
| 168 | unsigned index_i = static_cast<unsigned>(MathUtil::Clamp(floor(sample_loc * 256), 0.0f, 1.0f)); | 183 | dist_atten = LookupLightingLut(lut, sample_loc); |
| 184 | } | ||
| 185 | |||
| 186 | float clamp_highlights = 1.0f; | ||
| 187 | |||
| 188 | if (lighting.config0.clamp_highlights) { | ||
| 189 | if (LV_N <= 0.f) | ||
| 190 | clamp_highlights = 0.f; | ||
| 191 | else | ||
| 192 | clamp_highlights = 1.f; | ||
| 193 | } | ||
| 194 | |||
| 195 | auto GetLutIndex = [&](unsigned num, LightingRegs::LightingLutInput input, | ||
| 196 | bool abs) -> float { | ||
| 197 | |||
| 198 | Math::Vec3<float> norm_view = view.Normalized(); | ||
| 199 | Math::Vec3<float> half_angle = (norm_view + light_vector).Normalized(); | ||
| 200 | float result = 0.0f; | ||
| 201 | |||
| 202 | switch (input) { | ||
| 203 | case LightingRegs::LightingLutInput::NH: | ||
| 204 | result = Math::Dot(normal, half_angle); | ||
| 205 | break; | ||
| 169 | 206 | ||
| 170 | float index_f = sample_loc - index_i; | 207 | case LightingRegs::LightingLutInput::VH: |
| 208 | result = Math::Dot(norm_view, half_angle); | ||
| 209 | break; | ||
| 171 | 210 | ||
| 172 | ASSERT_MSG(lut < g_state.lighting.luts.size(), "Out of range lut"); | 211 | case LightingRegs::LightingLutInput::NV: |
| 212 | result = Math::Dot(normal, norm_view); | ||
| 213 | break; | ||
| 173 | 214 | ||
| 174 | float lut_value = g_state.lighting.luts[lut][index_i].ToFloat(); | 215 | case LightingRegs::LightingLutInput::LN: |
| 175 | float lut_diff = g_state.lighting.luts[lut][index_i].DiffToFloat(); | 216 | result = Math::Dot(light_vector, normal); |
| 217 | break; | ||
| 176 | 218 | ||
| 177 | dist_atten = lut_value + lut_diff * index_f; | 219 | default: |
| 220 | LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %d\n", (int)input); | ||
| 221 | UNIMPLEMENTED(); | ||
| 222 | result = 0.f; | ||
| 223 | } | ||
| 224 | |||
| 225 | if (abs) { | ||
| 226 | if (light_config.config.two_sided_diffuse) | ||
| 227 | result = std::abs(result); | ||
| 228 | else | ||
| 229 | result = std::max(result, 0.0f); | ||
| 230 | } else { | ||
| 231 | if (result < 0.f) | ||
| 232 | result += 2.f; | ||
| 233 | |||
| 234 | result /= 2.f; | ||
| 235 | } | ||
| 236 | |||
| 237 | return MathUtil::Clamp(result, 0.0f, 1.0f); | ||
| 238 | }; | ||
| 239 | |||
| 240 | // Specular 0 component | ||
| 241 | float d0_lut_value = 1.0f; | ||
| 242 | if (lighting.config1.disable_lut_d0 == 0 && | ||
| 243 | LightingRegs::IsLightingSamplerSupported( | ||
| 244 | lighting.config0.config, LightingRegs::LightingSampler::Distribution0)) { | ||
| 245 | |||
| 246 | // Lookup specular "distribution 0" LUT value | ||
| 247 | float index = GetLutIndex(num, lighting.lut_input.d0.Value(), lighting.abs_lut_input.disable_d0 == 0); | ||
| 248 | |||
| 249 | float scale = lighting.lut_scale.GetScale(lighting.lut_scale.d0); | ||
| 250 | |||
| 251 | d0_lut_value = scale * LookupLightingLut(static_cast<size_t>(LightingRegs::LightingSampler::Distribution0), index); | ||
| 178 | } | 252 | } |
| 179 | 253 | ||
| 254 | Math::Vec3<float> specular_0 = d0_lut_value * light_config.specular_0.ToVec3f(); | ||
| 255 | |||
| 256 | // TODO(Subv): Specular 1 | ||
| 257 | Math::Vec3<float> specular_1 = {}; | ||
| 258 | |||
| 180 | auto diffuse = light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f(); | 259 | auto diffuse = light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f(); |
| 181 | diffuse_sum += diffuse * dist_atten; | 260 | diffuse_sum += Math::MakeVec(diffuse * dist_atten, 0.0f); |
| 261 | |||
| 262 | specular_sum += Math::MakeVec((specular_0 + specular_1) * clamp_highlights * dist_atten, 0.f); | ||
| 182 | } | 263 | } |
| 183 | 264 | ||
| 184 | diffuse_sum += lighting.global_ambient.ToVec3f(); | 265 | diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f); |
| 185 | return { | 266 | return { |
| 186 | Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, 255).Cast<u8>(), | 267 | Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255, MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255, MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255, MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255).Cast<u8>(), |
| 187 | Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, 255).Cast<u8>() | 268 | Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255, MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255, MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255, MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255).Cast<u8>() |
| 188 | }; | 269 | }; |
| 189 | } | 270 | } |
| 190 | 271 | ||