summaryrefslogtreecommitdiff
path: root/src/video_core/rasterizer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/rasterizer.cpp')
-rw-r--r--src/video_core/rasterizer.cpp142
1 files changed, 139 insertions, 3 deletions
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
129static 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
129static void SetDepth(int x, int y, u32 value) { 153static 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
187static 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!
219static 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
162struct Fix12P4 { 235struct 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);