diff options
| author | 2017-08-30 21:07:30 -0400 | |
|---|---|---|
| committer | 2017-08-30 21:07:30 -0400 | |
| commit | f0e461bf6f9d38c35c581e75731ac80fca3f3172 (patch) | |
| tree | 0a54a98595c313afd9002ed9964c94255a88abc4 /src | |
| parent | Merge pull request #2899 from wwylele/touch-refactor (diff) | |
| parent | gl_rasterizer/lighting: more accurate CP formula (diff) | |
| download | yuzu-f0e461bf6f9d38c35c581e75731ac80fca3f3172.tar.gz yuzu-f0e461bf6f9d38c35c581e75731ac80fca3f3172.tar.xz yuzu-f0e461bf6f9d38c35c581e75731ac80fca3f3172.zip | |
Merge pull request #2891 from wwylele/sw-bump
SwRasterizer/Lighting: implement bump mapping
Diffstat (limited to '')
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.cpp | 4 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/lighting.cpp | 39 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/lighting.h | 3 | ||||
| -rw-r--r-- | src/video_core/swrasterizer/rasterizer.cpp | 4 |
4 files changed, 40 insertions, 10 deletions
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 015e69da9..3f390491a 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp | |||
| @@ -594,8 +594,8 @@ static void WriteLighting(std::string& out, const PicaShaderConfig& config) { | |||
| 594 | // Note: even if the normal vector is modified by normal map, which is not the | 594 | // Note: even if the normal vector is modified by normal map, which is not the |
| 595 | // normal of the tangent plane anymore, the half angle vector is still projected | 595 | // normal of the tangent plane anymore, the half angle vector is still projected |
| 596 | // using the modified normal vector. | 596 | // using the modified normal vector. |
| 597 | std::string half_angle_proj = "normalize(half_vector) - normal / dot(normal, " | 597 | std::string half_angle_proj = |
| 598 | "normal) * dot(normal, normalize(half_vector))"; | 598 | "normalize(half_vector) - normal * dot(normal, normalize(half_vector))"; |
| 599 | // Note: the half angle vector projection is confirmed not normalized before the dot | 599 | // Note: the half angle vector projection is confirmed not normalized before the dot |
| 600 | // product. The result is in fact not cos(phi) as the name suggested. | 600 | // product. The result is in fact not cos(phi) as the name suggested. |
| 601 | index = "dot(" + half_angle_proj + ", tangent)"; | 601 | index = "dot(" + half_angle_proj + ", tangent)"; |
diff --git a/src/video_core/swrasterizer/lighting.cpp b/src/video_core/swrasterizer/lighting.cpp index 39a3e396d..b38964530 100644 --- a/src/video_core/swrasterizer/lighting.cpp +++ b/src/video_core/swrasterizer/lighting.cpp | |||
| @@ -22,18 +22,37 @@ static float LookupLightingLut(const Pica::State::Lighting& lighting, size_t lut | |||
| 22 | 22 | ||
| 23 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( | 23 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( |
| 24 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, | 24 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, |
| 25 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view) { | 25 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view, |
| 26 | const Math::Vec4<u8> (&texture_color)[4]) { | ||
| 26 | 27 | ||
| 27 | // TODO(Subv): Bump mapping | 28 | Math::Vec3<float> surface_normal; |
| 28 | Math::Vec3<float> surface_normal = {0.0f, 0.0f, 1.0f}; | 29 | Math::Vec3<float> surface_tangent; |
| 29 | 30 | ||
| 30 | if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) { | 31 | if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) { |
| 31 | LOG_CRITICAL(HW_GPU, "unimplemented bump mapping"); | 32 | Math::Vec3<float> perturbation = |
| 32 | UNIMPLEMENTED(); | 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); | ||
| 33 | } | 51 | } |
| 34 | 52 | ||
| 35 | // Use the normalized the quaternion when performing the rotation | 53 | // Use the normalized the quaternion when performing the rotation |
| 36 | auto normal = Math::QuaternionRotate(normquat, surface_normal); | 54 | auto normal = Math::QuaternionRotate(normquat, surface_normal); |
| 55 | auto tangent = Math::QuaternionRotate(normquat, surface_tangent); | ||
| 37 | 56 | ||
| 38 | Math::Vec4<float> diffuse_sum = {0.0f, 0.0f, 0.0f, 1.0f}; | 57 | 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}; | 58 | Math::Vec4<float> specular_sum = {0.0f, 0.0f, 0.0f, 1.0f}; |
| @@ -102,6 +121,16 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( | |||
| 102 | result = Math::Dot(light_vector, spot_dir.Cast<float>() / 2047.0f); | 121 | result = Math::Dot(light_vector, spot_dir.Cast<float>() / 2047.0f); |
| 103 | break; | 122 | break; |
| 104 | } | 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; | ||
| 105 | default: | 134 | default: |
| 106 | LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input)); | 135 | LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input)); |
| 107 | UNIMPLEMENTED(); | 136 | UNIMPLEMENTED(); |
diff --git a/src/video_core/swrasterizer/lighting.h b/src/video_core/swrasterizer/lighting.h index 438dca926..d807a3d94 100644 --- a/src/video_core/swrasterizer/lighting.h +++ b/src/video_core/swrasterizer/lighting.h | |||
| @@ -13,6 +13,7 @@ namespace Pica { | |||
| 13 | 13 | ||
| 14 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( | 14 | std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors( |
| 15 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, | 15 | const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state, |
| 16 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view); | 16 | const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view, |
| 17 | const Math::Vec4<u8> (&texture_color)[4]); | ||
| 17 | 18 | ||
| 18 | } // namespace Pica | 19 | } // namespace Pica |
diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp index fdc1df199..862135614 100644 --- a/src/video_core/swrasterizer/rasterizer.cpp +++ b/src/video_core/swrasterizer/rasterizer.cpp | |||
| @@ -437,8 +437,8 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve | |||
| 437 | GetInterpolatedAttribute(v0.view.y, v1.view.y, v2.view.y).ToFloat32(), | 437 | GetInterpolatedAttribute(v0.view.y, v1.view.y, v2.view.y).ToFloat32(), |
| 438 | GetInterpolatedAttribute(v0.view.z, v1.view.z, v2.view.z).ToFloat32(), | 438 | GetInterpolatedAttribute(v0.view.z, v1.view.z, v2.view.z).ToFloat32(), |
| 439 | }; | 439 | }; |
| 440 | std::tie(primary_fragment_color, secondary_fragment_color) = | 440 | std::tie(primary_fragment_color, secondary_fragment_color) = ComputeFragmentsColors( |
| 441 | ComputeFragmentsColors(g_state.regs.lighting, g_state.lighting, normquat, view); | 441 | g_state.regs.lighting, g_state.lighting, normquat, view, texture_color); |
| 442 | } | 442 | } |
| 443 | 443 | ||
| 444 | for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); | 444 | for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); |