diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/clipper.cpp | 4 | ||||
| -rw-r--r-- | src/video_core/pica.h | 20 | ||||
| -rw-r--r-- | src/video_core/rasterizer.cpp | 26 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.cpp | 13 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.h | 5 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.cpp | 11 |
6 files changed, 65 insertions, 14 deletions
diff --git a/src/video_core/clipper.cpp b/src/video_core/clipper.cpp index 2bc747102..db99ce666 100644 --- a/src/video_core/clipper.cpp +++ b/src/video_core/clipper.cpp | |||
| @@ -75,8 +75,6 @@ static void InitScreenCoordinates(OutputVertex& vtx) | |||
| 75 | viewport.halfsize_y = float24::FromRaw(regs.viewport_size_y); | 75 | viewport.halfsize_y = float24::FromRaw(regs.viewport_size_y); |
| 76 | viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.viewport_corner.x)); | 76 | viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.viewport_corner.x)); |
| 77 | viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.viewport_corner.y)); | 77 | viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.viewport_corner.y)); |
| 78 | viewport.zscale = float24::FromRaw(regs.viewport_depth_range); | ||
| 79 | viewport.offset_z = float24::FromRaw(regs.viewport_depth_far_plane); | ||
| 80 | 78 | ||
| 81 | float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w; | 79 | float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w; |
| 82 | vtx.color *= inv_w; | 80 | vtx.color *= inv_w; |
| @@ -89,7 +87,7 @@ static void InitScreenCoordinates(OutputVertex& vtx) | |||
| 89 | 87 | ||
| 90 | vtx.screenpos[0] = (vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x; | 88 | vtx.screenpos[0] = (vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x; |
| 91 | vtx.screenpos[1] = (vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y; | 89 | vtx.screenpos[1] = (vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y; |
| 92 | vtx.screenpos[2] = viewport.offset_z + vtx.pos.z * inv_w * viewport.zscale; | 90 | vtx.screenpos[2] = vtx.pos.z * inv_w; |
| 93 | } | 91 | } |
| 94 | 92 | ||
| 95 | void ProcessTriangle(const OutputVertex &v0, const OutputVertex &v1, const OutputVertex &v2) { | 93 | void ProcessTriangle(const OutputVertex &v0, const OutputVertex &v1, const OutputVertex &v2) { |
diff --git a/src/video_core/pica.h b/src/video_core/pica.h index 5891fb72a..a81a7b984 100644 --- a/src/video_core/pica.h +++ b/src/video_core/pica.h | |||
| @@ -70,7 +70,7 @@ struct Regs { | |||
| 70 | INSERT_PADDING_WORDS(0x9); | 70 | INSERT_PADDING_WORDS(0x9); |
| 71 | 71 | ||
| 72 | BitField<0, 24, u32> viewport_depth_range; // float24 | 72 | BitField<0, 24, u32> viewport_depth_range; // float24 |
| 73 | BitField<0, 24, u32> viewport_depth_far_plane; // float24 | 73 | BitField<0, 24, u32> viewport_depth_near_plane; // float24 |
| 74 | 74 | ||
| 75 | BitField<0, 3, u32> vs_output_total; | 75 | BitField<0, 3, u32> vs_output_total; |
| 76 | 76 | ||
| @@ -122,7 +122,20 @@ struct Regs { | |||
| 122 | BitField<16, 10, s32> y; | 122 | BitField<16, 10, s32> y; |
| 123 | } viewport_corner; | 123 | } viewport_corner; |
| 124 | 124 | ||
| 125 | INSERT_PADDING_WORDS(0x17); | 125 | INSERT_PADDING_WORDS(0x1); |
| 126 | |||
| 127 | //TODO: early depth | ||
| 128 | INSERT_PADDING_WORDS(0x1); | ||
| 129 | |||
| 130 | INSERT_PADDING_WORDS(0x2); | ||
| 131 | |||
| 132 | enum DepthBuffering : u32 { | ||
| 133 | WBuffering = 0, | ||
| 134 | ZBuffering = 1, | ||
| 135 | }; | ||
| 136 | BitField< 0, 1, DepthBuffering> depthmap_enable; | ||
| 137 | |||
| 138 | INSERT_PADDING_WORDS(0x12); | ||
| 126 | 139 | ||
| 127 | struct TextureConfig { | 140 | struct TextureConfig { |
| 128 | enum WrapMode : u32 { | 141 | enum WrapMode : u32 { |
| @@ -1279,10 +1292,11 @@ ASSERT_REG_POSITION(cull_mode, 0x40); | |||
| 1279 | ASSERT_REG_POSITION(viewport_size_x, 0x41); | 1292 | ASSERT_REG_POSITION(viewport_size_x, 0x41); |
| 1280 | ASSERT_REG_POSITION(viewport_size_y, 0x43); | 1293 | ASSERT_REG_POSITION(viewport_size_y, 0x43); |
| 1281 | ASSERT_REG_POSITION(viewport_depth_range, 0x4d); | 1294 | ASSERT_REG_POSITION(viewport_depth_range, 0x4d); |
| 1282 | ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e); | 1295 | ASSERT_REG_POSITION(viewport_depth_near_plane, 0x4e); |
| 1283 | ASSERT_REG_POSITION(vs_output_attributes[0], 0x50); | 1296 | ASSERT_REG_POSITION(vs_output_attributes[0], 0x50); |
| 1284 | ASSERT_REG_POSITION(vs_output_attributes[1], 0x51); | 1297 | ASSERT_REG_POSITION(vs_output_attributes[1], 0x51); |
| 1285 | ASSERT_REG_POSITION(viewport_corner, 0x68); | 1298 | ASSERT_REG_POSITION(viewport_corner, 0x68); |
| 1299 | ASSERT_REG_POSITION(depthmap_enable, 0x6D); | ||
| 1286 | ASSERT_REG_POSITION(texture0_enable, 0x80); | 1300 | ASSERT_REG_POSITION(texture0_enable, 0x80); |
| 1287 | ASSERT_REG_POSITION(texture0, 0x81); | 1301 | ASSERT_REG_POSITION(texture0, 0x81); |
| 1288 | ASSERT_REG_POSITION(texture0_format, 0x8e); | 1302 | ASSERT_REG_POSITION(texture0_format, 0x8e); |
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index df67b9081..80cad9056 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp | |||
| @@ -862,10 +862,30 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, | |||
| 862 | } | 862 | } |
| 863 | } | 863 | } |
| 864 | 864 | ||
| 865 | // interpolated_z = z / w | ||
| 866 | float interpolated_z_over_w = (v0.screenpos[2].ToFloat32() * w0 + | ||
| 867 | v1.screenpos[2].ToFloat32() * w1 + | ||
| 868 | v2.screenpos[2].ToFloat32() * w2) / wsum; | ||
| 869 | |||
| 870 | // Not fully accurate. About 3 bits in precision are missing. | ||
| 871 | // Z-Buffer (z / w * scale + offset) | ||
| 872 | float depth_scale = float24::FromRaw(regs.viewport_depth_range).ToFloat32(); | ||
| 873 | float depth_offset = float24::FromRaw(regs.viewport_depth_near_plane).ToFloat32(); | ||
| 874 | float depth = interpolated_z_over_w * depth_scale + depth_offset; | ||
| 875 | |||
| 876 | // Potentially switch to W-Buffer | ||
| 877 | if (regs.depthmap_enable == Pica::Regs::DepthBuffering::WBuffering) { | ||
| 878 | |||
| 879 | // W-Buffer (z * scale + w * offset = (z / w * scale + offset) * w) | ||
| 880 | depth *= interpolated_w_inverse.ToFloat32() * wsum; | ||
| 881 | } | ||
| 882 | |||
| 883 | // Clamp the result | ||
| 884 | depth = MathUtil::Clamp(depth, 0.0f, 1.0f); | ||
| 885 | |||
| 886 | // Convert float to integer | ||
| 865 | unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format); | 887 | unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format); |
| 866 | u32 z = (u32)((v0.screenpos[2].ToFloat32() * w0 + | 888 | u32 z = (u32)(depth * ((1 << num_bits) - 1)); |
| 867 | v1.screenpos[2].ToFloat32() * w1 + | ||
| 868 | v2.screenpos[2].ToFloat32() * w2) * ((1 << num_bits) - 1) / wsum); | ||
| 869 | 889 | ||
| 870 | if (output_merger.depth_test_enable) { | 890 | if (output_merger.depth_test_enable) { |
| 871 | u32 ref_z = GetDepth(x >> 4, y >> 4); | 891 | u32 ref_z = GetDepth(x >> 4, y >> 4); |
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 0b471dfd2..d1d9beccb 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp | |||
| @@ -256,10 +256,15 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) { | |||
| 256 | 256 | ||
| 257 | // Depth modifiers | 257 | // Depth modifiers |
| 258 | case PICA_REG_INDEX(viewport_depth_range): | 258 | case PICA_REG_INDEX(viewport_depth_range): |
| 259 | case PICA_REG_INDEX(viewport_depth_far_plane): | 259 | case PICA_REG_INDEX(viewport_depth_near_plane): |
| 260 | SyncDepthModifiers(); | 260 | SyncDepthModifiers(); |
| 261 | break; | 261 | break; |
| 262 | 262 | ||
| 263 | // Depth buffering | ||
| 264 | case PICA_REG_INDEX(depthmap_enable): | ||
| 265 | shader_dirty = true; | ||
| 266 | break; | ||
| 267 | |||
| 263 | // Blending | 268 | // Blending |
| 264 | case PICA_REG_INDEX(output_merger.alphablend_enable): | 269 | case PICA_REG_INDEX(output_merger.alphablend_enable): |
| 265 | SyncBlendEnabled(); | 270 | SyncBlendEnabled(); |
| @@ -910,10 +915,10 @@ void RasterizerOpenGL::SyncCullMode() { | |||
| 910 | } | 915 | } |
| 911 | 916 | ||
| 912 | void RasterizerOpenGL::SyncDepthModifiers() { | 917 | void RasterizerOpenGL::SyncDepthModifiers() { |
| 913 | float depth_scale = -Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32(); | 918 | float depth_scale = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32(); |
| 914 | float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_far_plane).ToFloat32() / 2.0f; | 919 | float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_near_plane).ToFloat32(); |
| 915 | 920 | ||
| 916 | // TODO: Implement scale modifier | 921 | uniform_block_data.data.depth_scale = depth_scale; |
| 917 | uniform_block_data.data.depth_offset = depth_offset; | 922 | uniform_block_data.data.depth_offset = depth_offset; |
| 918 | uniform_block_data.dirty = true; | 923 | uniform_block_data.dirty = true; |
| 919 | } | 924 | } |
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 5afcf296d..b9315ed33 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h | |||
| @@ -56,6 +56,8 @@ union PicaShaderConfig { | |||
| 56 | 56 | ||
| 57 | const auto& regs = Pica::g_state.regs; | 57 | const auto& regs = Pica::g_state.regs; |
| 58 | 58 | ||
| 59 | state.depthmap_enable = regs.depthmap_enable; | ||
| 60 | |||
| 59 | state.alpha_test_func = regs.output_merger.alpha_test.enable ? | 61 | state.alpha_test_func = regs.output_merger.alpha_test.enable ? |
| 60 | regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always; | 62 | regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always; |
| 61 | 63 | ||
| @@ -171,6 +173,8 @@ union PicaShaderConfig { | |||
| 171 | std::array<TevStageConfigRaw, 6> tev_stages; | 173 | std::array<TevStageConfigRaw, 6> tev_stages; |
| 172 | u8 combiner_buffer_input; | 174 | u8 combiner_buffer_input; |
| 173 | 175 | ||
| 176 | Pica::Regs::DepthBuffering depthmap_enable; | ||
| 177 | |||
| 174 | struct { | 178 | struct { |
| 175 | struct { | 179 | struct { |
| 176 | unsigned num; | 180 | unsigned num; |
| @@ -315,6 +319,7 @@ private: | |||
| 315 | GLvec4 const_color[6]; | 319 | GLvec4 const_color[6]; |
| 316 | GLvec4 tev_combiner_buffer_color; | 320 | GLvec4 tev_combiner_buffer_color; |
| 317 | GLint alphatest_ref; | 321 | GLint alphatest_ref; |
| 322 | GLfloat depth_scale; | ||
| 318 | GLfloat depth_offset; | 323 | GLfloat depth_offset; |
| 319 | alignas(16) GLvec3 lighting_global_ambient; | 324 | alignas(16) GLvec3 lighting_global_ambient; |
| 320 | LightSrc light_src[8]; | 325 | LightSrc light_src[8]; |
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 0890adb12..0c3153e8f 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp | |||
| @@ -540,6 +540,7 @@ layout (std140) uniform shader_data { | |||
| 540 | vec4 const_color[NUM_TEV_STAGES]; | 540 | vec4 const_color[NUM_TEV_STAGES]; |
| 541 | vec4 tev_combiner_buffer_color; | 541 | vec4 tev_combiner_buffer_color; |
| 542 | int alphatest_ref; | 542 | int alphatest_ref; |
| 543 | float depth_scale; | ||
| 543 | float depth_offset; | 544 | float depth_offset; |
| 544 | vec3 lighting_global_ambient; | 545 | vec3 lighting_global_ambient; |
| 545 | LightSrc light_src[NUM_LIGHTS]; | 546 | LightSrc light_src[NUM_LIGHTS]; |
| @@ -581,7 +582,15 @@ vec4 secondary_fragment_color = vec4(0.0); | |||
| 581 | } | 582 | } |
| 582 | 583 | ||
| 583 | out += "color = last_tex_env_out;\n"; | 584 | out += "color = last_tex_env_out;\n"; |
| 584 | out += "gl_FragDepth = gl_FragCoord.z + depth_offset;\n}"; | 585 | |
| 586 | out += "float z_over_w = 1.0 - gl_FragCoord.z * 2.0;\n"; | ||
| 587 | out += "float depth = z_over_w * depth_scale + depth_offset;\n"; | ||
| 588 | if (state.depthmap_enable == Pica::Regs::DepthBuffering::WBuffering) { | ||
| 589 | out += "depth /= gl_FragCoord.w;\n"; | ||
| 590 | } | ||
| 591 | out += "gl_FragDepth = depth;\n"; | ||
| 592 | |||
| 593 | out += "}"; | ||
| 585 | 594 | ||
| 586 | return out; | 595 | return out; |
| 587 | } | 596 | } |