summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Tony Wasserka2015-07-13 23:05:04 +0200
committerGravatar Tony Wasserka2015-07-13 23:05:04 +0200
commitbf80ae6a1ad328814df332c1136fe14e836053c6 (patch)
treedd61e70261a8f3488344f0ce90df6c5bb67e6c81
parentMerge pull request #702 from neobrain/citrace (diff)
parentPica: Implement stencil testing. (diff)
downloadyuzu-bf80ae6a1ad328814df332c1136fe14e836053c6.tar.gz
yuzu-bf80ae6a1ad328814df332c1136fe14e836053c6.tar.xz
yuzu-bf80ae6a1ad328814df332c1136fe14e836053c6.zip
Merge pull request #793 from neobrain/stencil
Pica: Implement stencil testing.
Diffstat (limited to '')
-rw-r--r--src/citra_qt/debugger/graphics_framebuffer.cpp62
-rw-r--r--src/citra_qt/debugger/graphics_framebuffer.h4
-rw-r--r--src/common/color.h27
-rw-r--r--src/video_core/pica.h43
-rw-r--r--src/video_core/rasterizer.cpp142
5 files changed, 258 insertions, 20 deletions
diff --git a/src/citra_qt/debugger/graphics_framebuffer.cpp b/src/citra_qt/debugger/graphics_framebuffer.cpp
index 6bbe7572c..39eefbf75 100644
--- a/src/citra_qt/debugger/graphics_framebuffer.cpp
+++ b/src/citra_qt/debugger/graphics_framebuffer.cpp
@@ -55,7 +55,9 @@ GraphicsFramebufferWidget::GraphicsFramebufferWidget(std::shared_ptr<Pica::Debug
55 framebuffer_format_control->addItem(tr("RGBA4")); 55 framebuffer_format_control->addItem(tr("RGBA4"));
56 framebuffer_format_control->addItem(tr("D16")); 56 framebuffer_format_control->addItem(tr("D16"));
57 framebuffer_format_control->addItem(tr("D24")); 57 framebuffer_format_control->addItem(tr("D24"));
58 framebuffer_format_control->addItem(tr("D24S8")); 58 framebuffer_format_control->addItem(tr("D24X8"));
59 framebuffer_format_control->addItem(tr("X24S8"));
60 framebuffer_format_control->addItem(tr("(unknown)"));
59 61
60 // TODO: This QLabel should shrink the image to the available space rather than just expanding... 62 // TODO: This QLabel should shrink the image to the available space rather than just expanding...
61 framebuffer_picture_label = new QLabel; 63 framebuffer_picture_label = new QLabel;
@@ -184,8 +186,32 @@ void GraphicsFramebufferWidget::OnUpdate()
184 framebuffer_address = framebuffer.GetColorBufferPhysicalAddress(); 186 framebuffer_address = framebuffer.GetColorBufferPhysicalAddress();
185 framebuffer_width = framebuffer.GetWidth(); 187 framebuffer_width = framebuffer.GetWidth();
186 framebuffer_height = framebuffer.GetHeight(); 188 framebuffer_height = framebuffer.GetHeight();
187 // TODO: It's unknown how this format is actually specified 189
188 framebuffer_format = Format::RGBA8; 190 switch (framebuffer.color_format) {
191 case Pica::Regs::ColorFormat::RGBA8:
192 framebuffer_format = Format::RGBA8;
193 break;
194
195 case Pica::Regs::ColorFormat::RGB8:
196 framebuffer_format = Format::RGB8;
197 break;
198
199 case Pica::Regs::ColorFormat::RGB5A1:
200 framebuffer_format = Format::RGB5A1;
201 break;
202
203 case Pica::Regs::ColorFormat::RGB565:
204 framebuffer_format = Format::RGB565;
205 break;
206
207 case Pica::Regs::ColorFormat::RGBA4:
208 framebuffer_format = Format::RGBA4;
209 break;
210
211 default:
212 framebuffer_format = Format::Unknown;
213 break;
214 }
189 215
190 break; 216 break;
191 } 217 }
@@ -197,7 +223,24 @@ void GraphicsFramebufferWidget::OnUpdate()
197 framebuffer_address = framebuffer.GetDepthBufferPhysicalAddress(); 223 framebuffer_address = framebuffer.GetDepthBufferPhysicalAddress();
198 framebuffer_width = framebuffer.GetWidth(); 224 framebuffer_width = framebuffer.GetWidth();
199 framebuffer_height = framebuffer.GetHeight(); 225 framebuffer_height = framebuffer.GetHeight();
200 framebuffer_format = Format::D16; 226
227 switch (framebuffer.depth_format) {
228 case Pica::Regs::DepthFormat::D16:
229 framebuffer_format = Format::D16;
230 break;
231
232 case Pica::Regs::DepthFormat::D24:
233 framebuffer_format = Format::D24;
234 break;
235
236 case Pica::Regs::DepthFormat::D24S8:
237 framebuffer_format = Format::D24X8;
238 break;
239
240 default:
241 framebuffer_format = Format::Unknown;
242 break;
243 }
201 244
202 break; 245 break;
203 } 246 }
@@ -258,7 +301,7 @@ void GraphicsFramebufferWidget::OnUpdate()
258 color.b() = (data >> 16) & 0xFF; 301 color.b() = (data >> 16) & 0xFF;
259 break; 302 break;
260 } 303 }
261 case Format::D24S8: 304 case Format::D24X8:
262 { 305 {
263 Math::Vec2<u32> data = Color::DecodeD24S8(pixel); 306 Math::Vec2<u32> data = Color::DecodeD24S8(pixel);
264 color.r() = data.x & 0xFF; 307 color.r() = data.x & 0xFF;
@@ -266,6 +309,12 @@ void GraphicsFramebufferWidget::OnUpdate()
266 color.b() = (data.x >> 16) & 0xFF; 309 color.b() = (data.x >> 16) & 0xFF;
267 break; 310 break;
268 } 311 }
312 case Format::X24S8:
313 {
314 Math::Vec2<u32> data = Color::DecodeD24S8(pixel);
315 color.r() = color.g() = color.b() = data.y;
316 break;
317 }
269 default: 318 default:
270 qDebug() << "Unknown fb color format " << static_cast<int>(framebuffer_format); 319 qDebug() << "Unknown fb color format " << static_cast<int>(framebuffer_format);
271 break; 320 break;
@@ -286,7 +335,8 @@ void GraphicsFramebufferWidget::OnUpdate()
286u32 GraphicsFramebufferWidget::BytesPerPixel(GraphicsFramebufferWidget::Format format) { 335u32 GraphicsFramebufferWidget::BytesPerPixel(GraphicsFramebufferWidget::Format format) {
287 switch (format) { 336 switch (format) {
288 case Format::RGBA8: 337 case Format::RGBA8:
289 case Format::D24S8: 338 case Format::D24X8:
339 case Format::X24S8:
290 return 4; 340 return 4;
291 case Format::RGB8: 341 case Format::RGB8:
292 case Format::D24: 342 case Format::D24:
diff --git a/src/citra_qt/debugger/graphics_framebuffer.h b/src/citra_qt/debugger/graphics_framebuffer.h
index 4cb396ffe..e9eae679f 100644
--- a/src/citra_qt/debugger/graphics_framebuffer.h
+++ b/src/citra_qt/debugger/graphics_framebuffer.h
@@ -35,7 +35,9 @@ class GraphicsFramebufferWidget : public BreakPointObserverDock {
35 RGBA4 = 4, 35 RGBA4 = 4,
36 D16 = 5, 36 D16 = 5,
37 D24 = 6, 37 D24 = 6,
38 D24S8 = 7 38 D24X8 = 7,
39 X24S8 = 8,
40 Unknown = 9
39 }; 41 };
40 42
41 static u32 BytesPerPixel(Format format); 43 static u32 BytesPerPixel(Format format);
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 */
210inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { 210inline 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 */
223inline 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 */
235inline 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
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);