diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/color.h | 27 | ||||
| -rw-r--r-- | src/video_core/pica.h | 43 | ||||
| -rw-r--r-- | src/video_core/rasterizer.cpp | 142 |
3 files changed, 199 insertions, 13 deletions
diff --git a/src/common/color.h b/src/common/color.h index 422fdc8af..9dafdca0c 100644 --- a/src/common/color.h +++ b/src/common/color.h | |||
| @@ -208,7 +208,32 @@ inline void EncodeD24(u32 value, u8* bytes) { | |||
| 208 | * @param bytes Pointer where to store the encoded value | 208 | * @param bytes Pointer where to store the encoded value |
| 209 | */ | 209 | */ |
| 210 | inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { | 210 | inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { |
| 211 | *reinterpret_cast<u32_le*>(bytes) = (stencil << 24) | depth; | 211 | bytes[0] = depth & 0xFF; |
| 212 | bytes[1] = (depth >> 8) & 0xFF; | ||
| 213 | bytes[2] = (depth >> 16) & 0xFF; | ||
| 214 | bytes[3] = stencil; | ||
| 215 | } | ||
| 216 | |||
| 217 | /** | ||
| 218 | * Encode a 24 bit depth value as D24X8 format (32 bits per pixel with 8 bits unused) | ||
| 219 | * @param depth 24 bit source depth value to encode | ||
| 220 | * @param bytes Pointer where to store the encoded value | ||
| 221 | * @note unused bits will not be modified | ||
| 222 | */ | ||
| 223 | inline void EncodeD24X8(u32 depth, u8* bytes) { | ||
| 224 | bytes[0] = depth & 0xFF; | ||
| 225 | bytes[1] = (depth >> 8) & 0xFF; | ||
| 226 | bytes[2] = (depth >> 16) & 0xFF; | ||
| 227 | } | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Encode an 8 bit stencil value as X24S8 format (32 bits per pixel with 24 bits unused) | ||
| 231 | * @param stencil 8 bit source stencil value to encode | ||
| 232 | * @param bytes Pointer where to store the encoded value | ||
| 233 | * @note unused bits will not be modified | ||
| 234 | */ | ||
| 235 | inline void EncodeX24S8(u8 stencil, u8* bytes) { | ||
| 236 | bytes[3] = stencil; | ||
| 212 | } | 237 | } |
| 213 | 238 | ||
| 214 | } // namespace | 239 | } // namespace |
diff --git a/src/video_core/pica.h b/src/video_core/pica.h index feb20214a..46a7b21dc 100644 --- a/src/video_core/pica.h +++ b/src/video_core/pica.h | |||
| @@ -420,6 +420,11 @@ struct Regs { | |||
| 420 | GreaterThanOrEqual = 7, | 420 | GreaterThanOrEqual = 7, |
| 421 | }; | 421 | }; |
| 422 | 422 | ||
| 423 | enum class StencilAction : u32 { | ||
| 424 | Keep = 0, | ||
| 425 | Xor = 5, | ||
| 426 | }; | ||
| 427 | |||
| 423 | struct { | 428 | struct { |
| 424 | union { | 429 | union { |
| 425 | // If false, logic blending is used | 430 | // If false, logic blending is used |
| @@ -454,15 +459,35 @@ struct Regs { | |||
| 454 | BitField< 8, 8, u32> ref; | 459 | BitField< 8, 8, u32> ref; |
| 455 | } alpha_test; | 460 | } alpha_test; |
| 456 | 461 | ||
| 457 | union { | 462 | struct { |
| 458 | BitField< 0, 1, u32> stencil_test_enable; | 463 | union { |
| 459 | BitField< 4, 3, CompareFunc> stencil_test_func; | 464 | // If true, enable stencil testing |
| 460 | BitField< 8, 8, u32> stencil_replacement_value; | 465 | BitField< 0, 1, u32> enable; |
| 461 | BitField<16, 8, u32> stencil_reference_value; | ||
| 462 | BitField<24, 8, u32> stencil_mask; | ||
| 463 | } stencil_test; | ||
| 464 | 466 | ||
| 465 | INSERT_PADDING_WORDS(0x1); | 467 | // Comparison operation for stencil testing |
| 468 | BitField< 4, 3, CompareFunc> func; | ||
| 469 | |||
| 470 | // Value to calculate the new stencil value from | ||
| 471 | BitField< 8, 8, u32> replacement_value; | ||
| 472 | |||
| 473 | // Value to compare against for stencil testing | ||
| 474 | BitField<16, 8, u32> reference_value; | ||
| 475 | |||
| 476 | // Mask to apply on stencil test inputs | ||
| 477 | BitField<24, 8, u32> mask; | ||
| 478 | }; | ||
| 479 | |||
| 480 | union { | ||
| 481 | // Action to perform when the stencil test fails | ||
| 482 | BitField< 0, 3, StencilAction> action_stencil_fail; | ||
| 483 | |||
| 484 | // Action to perform when stencil testing passed but depth testing fails | ||
| 485 | BitField< 4, 3, StencilAction> action_depth_fail; | ||
| 486 | |||
| 487 | // Action to perform when both stencil and depth testing pass | ||
| 488 | BitField< 8, 3, StencilAction> action_depth_pass; | ||
| 489 | }; | ||
| 490 | } stencil_test; | ||
| 466 | 491 | ||
| 467 | union { | 492 | union { |
| 468 | BitField< 0, 1, u32> depth_test_enable; | 493 | BitField< 0, 1, u32> depth_test_enable; |
| @@ -512,7 +537,7 @@ struct Regs { | |||
| 512 | struct { | 537 | struct { |
| 513 | INSERT_PADDING_WORDS(0x6); | 538 | INSERT_PADDING_WORDS(0x6); |
| 514 | 539 | ||
| 515 | DepthFormat depth_format; | 540 | DepthFormat depth_format; // TODO: Should be a BitField! |
| 516 | BitField<16, 3, ColorFormat> color_format; | 541 | BitField<16, 3, ColorFormat> color_format; |
| 517 | 542 | ||
| 518 | INSERT_PADDING_WORDS(0x4); | 543 | INSERT_PADDING_WORDS(0x4); |
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index 70b115744..c381c2bd9 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp | |||
| @@ -126,6 +126,30 @@ static u32 GetDepth(int x, int y) { | |||
| 126 | } | 126 | } |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | static u8 GetStencil(int x, int y) { | ||
| 130 | const auto& framebuffer = g_state.regs.framebuffer; | ||
| 131 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); | ||
| 132 | u8* depth_buffer = Memory::GetPhysicalPointer(addr); | ||
| 133 | |||
| 134 | y = framebuffer.height - y; | ||
| 135 | |||
| 136 | const u32 coarse_y = y & ~7; | ||
| 137 | u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(framebuffer.depth_format); | ||
| 138 | u32 stride = framebuffer.width * bytes_per_pixel; | ||
| 139 | |||
| 140 | u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; | ||
| 141 | u8* src_pixel = depth_buffer + src_offset; | ||
| 142 | |||
| 143 | switch (framebuffer.depth_format) { | ||
| 144 | case Regs::DepthFormat::D24S8: | ||
| 145 | return Color::DecodeD24S8(src_pixel).y; | ||
| 146 | |||
| 147 | default: | ||
| 148 | LOG_WARNING(HW_GPU, "GetStencil called for function which doesn't have a stencil component (format %u)", framebuffer.depth_format); | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 129 | static void SetDepth(int x, int y, u32 value) { | 153 | static void SetDepth(int x, int y, u32 value) { |
| 130 | const auto& framebuffer = g_state.regs.framebuffer; | 154 | const auto& framebuffer = g_state.regs.framebuffer; |
| 131 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); | 155 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); |
| @@ -144,13 +168,46 @@ static void SetDepth(int x, int y, u32 value) { | |||
| 144 | case Regs::DepthFormat::D16: | 168 | case Regs::DepthFormat::D16: |
| 145 | Color::EncodeD16(value, dst_pixel); | 169 | Color::EncodeD16(value, dst_pixel); |
| 146 | break; | 170 | break; |
| 171 | |||
| 147 | case Regs::DepthFormat::D24: | 172 | case Regs::DepthFormat::D24: |
| 148 | Color::EncodeD24(value, dst_pixel); | 173 | Color::EncodeD24(value, dst_pixel); |
| 149 | break; | 174 | break; |
| 175 | |||
| 150 | case Regs::DepthFormat::D24S8: | 176 | case Regs::DepthFormat::D24S8: |
| 151 | // TODO(Subv): Implement the stencil buffer | 177 | Color::EncodeD24X8(value, dst_pixel); |
| 152 | Color::EncodeD24S8(value, 0, dst_pixel); | ||
| 153 | break; | 178 | break; |
| 179 | |||
| 180 | default: | ||
| 181 | LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); | ||
| 182 | UNIMPLEMENTED(); | ||
| 183 | break; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | static void SetStencil(int x, int y, u8 value) { | ||
| 188 | const auto& framebuffer = g_state.regs.framebuffer; | ||
| 189 | const PAddr addr = framebuffer.GetDepthBufferPhysicalAddress(); | ||
| 190 | u8* depth_buffer = Memory::GetPhysicalPointer(addr); | ||
| 191 | |||
| 192 | y = framebuffer.height - y; | ||
| 193 | |||
| 194 | const u32 coarse_y = y & ~7; | ||
| 195 | u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(framebuffer.depth_format); | ||
| 196 | u32 stride = framebuffer.width * bytes_per_pixel; | ||
| 197 | |||
| 198 | u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; | ||
| 199 | u8* dst_pixel = depth_buffer + dst_offset; | ||
| 200 | |||
| 201 | switch (framebuffer.depth_format) { | ||
| 202 | case Pica::Regs::DepthFormat::D16: | ||
| 203 | case Pica::Regs::DepthFormat::D24: | ||
| 204 | // Nothing to do | ||
| 205 | break; | ||
| 206 | |||
| 207 | case Pica::Regs::DepthFormat::D24S8: | ||
| 208 | Color::EncodeX24S8(value, dst_pixel); | ||
| 209 | break; | ||
| 210 | |||
| 154 | default: | 211 | default: |
| 155 | LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); | 212 | LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", framebuffer.depth_format); |
| 156 | UNIMPLEMENTED(); | 213 | UNIMPLEMENTED(); |
| @@ -158,6 +215,22 @@ static void SetDepth(int x, int y, u32 value) { | |||
| 158 | } | 215 | } |
| 159 | } | 216 | } |
| 160 | 217 | ||
| 218 | // TODO: Should the stencil mask be applied to the "dest" or "ref" operands? Most likely not! | ||
| 219 | static u8 PerformStencilAction(Regs::StencilAction action, u8 dest, u8 ref) { | ||
| 220 | switch (action) { | ||
| 221 | case Regs::StencilAction::Keep: | ||
| 222 | return dest; | ||
| 223 | |||
| 224 | case Regs::StencilAction::Xor: | ||
| 225 | return dest ^ ref; | ||
| 226 | |||
| 227 | default: | ||
| 228 | LOG_CRITICAL(HW_GPU, "Unknown stencil action %x", (int)action); | ||
| 229 | UNIMPLEMENTED(); | ||
| 230 | return 0; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 161 | // NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values | 234 | // NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values |
| 162 | struct Fix12P4 { | 235 | struct Fix12P4 { |
| 163 | Fix12P4() {} | 236 | Fix12P4() {} |
| @@ -276,6 +349,9 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 276 | auto textures = regs.GetTextures(); | 349 | auto textures = regs.GetTextures(); |
| 277 | auto tev_stages = regs.GetTevStages(); | 350 | auto tev_stages = regs.GetTevStages(); |
| 278 | 351 | ||
| 352 | bool stencil_action_enable = g_state.regs.output_merger.stencil_test.enable && g_state.regs.framebuffer.depth_format == Regs::DepthFormat::D24S8; | ||
| 353 | const auto stencil_test = g_state.regs.output_merger.stencil_test; | ||
| 354 | |||
| 279 | // Enter rasterization loop, starting at the center of the topleft bounding box corner. | 355 | // Enter rasterization loop, starting at the center of the topleft bounding box corner. |
| 280 | // TODO: Not sure if looping through x first might be faster | 356 | // TODO: Not sure if looping through x first might be faster |
| 281 | for (u16 y = min_y + 8; y < max_y; y += 0x10) { | 357 | for (u16 y = min_y + 8; y < max_y; y += 0x10) { |
| @@ -647,6 +723,7 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 647 | } | 723 | } |
| 648 | 724 | ||
| 649 | const auto& output_merger = regs.output_merger; | 725 | const auto& output_merger = regs.output_merger; |
| 726 | // TODO: Does alpha testing happen before or after stencil? | ||
| 650 | if (output_merger.alpha_test.enable) { | 727 | if (output_merger.alpha_test.enable) { |
| 651 | bool pass = false; | 728 | bool pass = false; |
| 652 | 729 | ||
| @@ -688,6 +765,54 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 688 | continue; | 765 | continue; |
| 689 | } | 766 | } |
| 690 | 767 | ||
| 768 | u8 old_stencil = 0; | ||
| 769 | if (stencil_action_enable) { | ||
| 770 | old_stencil = GetStencil(x >> 4, y >> 4); | ||
| 771 | u8 dest = old_stencil & stencil_test.mask; | ||
| 772 | u8 ref = stencil_test.reference_value & stencil_test.mask; | ||
| 773 | |||
| 774 | bool pass = false; | ||
| 775 | switch (stencil_test.func) { | ||
| 776 | case Regs::CompareFunc::Never: | ||
| 777 | pass = false; | ||
| 778 | break; | ||
| 779 | |||
| 780 | case Regs::CompareFunc::Always: | ||
| 781 | pass = true; | ||
| 782 | break; | ||
| 783 | |||
| 784 | case Regs::CompareFunc::Equal: | ||
| 785 | pass = (ref == dest); | ||
| 786 | break; | ||
| 787 | |||
| 788 | case Regs::CompareFunc::NotEqual: | ||
| 789 | pass = (ref != dest); | ||
| 790 | break; | ||
| 791 | |||
| 792 | case Regs::CompareFunc::LessThan: | ||
| 793 | pass = (ref < dest); | ||
| 794 | break; | ||
| 795 | |||
| 796 | case Regs::CompareFunc::LessThanOrEqual: | ||
| 797 | pass = (ref <= dest); | ||
| 798 | break; | ||
| 799 | |||
| 800 | case Regs::CompareFunc::GreaterThan: | ||
| 801 | pass = (ref > dest); | ||
| 802 | break; | ||
| 803 | |||
| 804 | case Regs::CompareFunc::GreaterThanOrEqual: | ||
| 805 | pass = (ref >= dest); | ||
| 806 | break; | ||
| 807 | } | ||
| 808 | |||
| 809 | if (!pass) { | ||
| 810 | u8 new_stencil = PerformStencilAction(stencil_test.action_stencil_fail, old_stencil, stencil_test.replacement_value); | ||
| 811 | SetStencil(x >> 4, y >> 4, new_stencil); | ||
| 812 | continue; | ||
| 813 | } | ||
| 814 | } | ||
| 815 | |||
| 691 | // TODO: Does depth indeed only get written even if depth testing is enabled? | 816 | // TODO: Does depth indeed only get written even if depth testing is enabled? |
| 692 | if (output_merger.depth_test_enable) { | 817 | if (output_merger.depth_test_enable) { |
| 693 | unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format); | 818 | unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format); |
| @@ -732,11 +857,22 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
| 732 | break; | 857 | break; |
| 733 | } | 858 | } |
| 734 | 859 | ||
| 735 | if (!pass) | 860 | if (!pass) { |
| 861 | if (stencil_action_enable) { | ||
| 862 | u8 new_stencil = PerformStencilAction(stencil_test.action_depth_fail, old_stencil, stencil_test.replacement_value); | ||
| 863 | SetStencil(x >> 4, y >> 4, new_stencil); | ||
| 864 | } | ||
| 736 | continue; | 865 | continue; |
| 866 | } | ||
| 737 | 867 | ||
| 738 | if (output_merger.depth_write_enable) | 868 | if (output_merger.depth_write_enable) |
| 739 | SetDepth(x >> 4, y >> 4, z); | 869 | SetDepth(x >> 4, y >> 4, z); |
| 870 | |||
| 871 | if (stencil_action_enable) { | ||
| 872 | // TODO: What happens if stencil testing is enabled, but depth testing is not? Will stencil get updated anyway? | ||
| 873 | u8 new_stencil = PerformStencilAction(stencil_test.action_depth_pass, old_stencil, stencil_test.replacement_value); | ||
| 874 | SetStencil(x >> 4, y >> 4, new_stencil); | ||
| 875 | } | ||
| 740 | } | 876 | } |
| 741 | 877 | ||
| 742 | auto dest = GetPixel(x >> 4, y >> 4); | 878 | auto dest = GetPixel(x >> 4, y >> 4); |