mirror of
				https://github.com/PabloMK7/citra.git
				synced 2025-10-31 05:40:04 +00:00 
			
		
		
		
	Merge pull request #643 from Subv/dem_feels
GPU: Implemented more depth buffer formats.
This commit is contained in:
		
						commit
						ec5bc54575
					
				
					 5 changed files with 200 additions and 18 deletions
				
			
		|  | @ -100,6 +100,33 @@ inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) { | |||
|         Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) }; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Decode a depth value stored in D16 format | ||||
|  * @param bytes Pointer to encoded source value | ||||
|  * @return Depth value as an u32 | ||||
|  */ | ||||
| inline u32 DecodeD16(const u8* bytes) { | ||||
|     return *reinterpret_cast<const u16_le*>(bytes); | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Decode a depth value stored in D24 format | ||||
|  * @param bytes Pointer to encoded source value | ||||
|  * @return Depth value as an u32 | ||||
|  */ | ||||
| inline u32 DecodeD24(const u8* bytes) { | ||||
|     return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Decode a depth value and a stencil value stored in D24S8 format | ||||
|  * @param bytes Pointer to encoded source values | ||||
|  * @return Resulting values stored as a Math::Vec2 | ||||
|  */ | ||||
| inline const Math::Vec2<u32> DecodeD24S8(const u8* bytes) { | ||||
|     return { (bytes[2] << 16) | (bytes[1] << 8) | bytes[0], bytes[3] }; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Encode a color as RGBA8 format | ||||
|  * @param color Source color to encode | ||||
|  | @ -153,4 +180,34 @@ inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) { | |||
|         (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Encode a 16 bit depth value as D16 format | ||||
|  * @param value 16 bit source depth value to encode | ||||
|  * @param bytes Pointer where to store the encoded value | ||||
|  */ | ||||
| inline void EncodeD16(u32 value, u8* bytes) { | ||||
|     *reinterpret_cast<u16_le*>(bytes) = value & 0xFFFF; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Encode a 24 bit depth value as D24 format | ||||
|  * @param value 24 bit source depth value to encode | ||||
|  * @param bytes Pointer where to store the encoded value | ||||
|  */ | ||||
| inline void EncodeD24(u32 value, u8* bytes) { | ||||
|     bytes[0] = value & 0xFF; | ||||
|     bytes[1] = (value >> 8) & 0xFF; | ||||
|     bytes[2] = (value >> 16) & 0xFF; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Encode a 24 bit depth and 8 bit stencil values as D24S8 format | ||||
|  * @param depth 24 bit source depth value to encode | ||||
|  * @param stencil 8 bit source stencil value to encode | ||||
|  * @param bytes Pointer where to store the encoded value | ||||
|  */ | ||||
| inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { | ||||
|     *reinterpret_cast<u32_le*>(bytes) = (stencil << 24) | depth; | ||||
| } | ||||
| 
 | ||||
| } // namespace
 | ||||
|  |  | |||
|  | @ -393,7 +393,15 @@ struct Regs { | |||
|             BitField< 8, 8, u32> ref; | ||||
|         } alpha_test; | ||||
| 
 | ||||
|         INSERT_PADDING_WORDS(0x2); | ||||
|         union { | ||||
|             BitField< 0, 1, u32> stencil_test_enable; | ||||
|             BitField< 4, 3, CompareFunc> stencil_test_func; | ||||
|             BitField< 8, 8, u32> stencil_replacement_value; | ||||
|             BitField<16, 8, u32> stencil_reference_value; | ||||
|             BitField<24, 8, u32> stencil_mask; | ||||
|         } stencil_test; | ||||
| 
 | ||||
|         INSERT_PADDING_WORDS(0x1); | ||||
| 
 | ||||
|         union { | ||||
|             BitField< 0, 1, u32> depth_test_enable; | ||||
|  | @ -408,6 +416,30 @@ struct Regs { | |||
|         INSERT_PADDING_WORDS(0x8); | ||||
|     } output_merger; | ||||
| 
 | ||||
|     enum DepthFormat : u32 { | ||||
|         D16    = 0, | ||||
| 
 | ||||
|         D24    = 2, | ||||
|         D24S8  = 3 | ||||
|     }; | ||||
| 
 | ||||
|     /*
 | ||||
|      * Returns the number of bytes in the specified depth format | ||||
|      */ | ||||
|     static u32 BytesPerDepthPixel(DepthFormat format) { | ||||
|         switch (format) { | ||||
|         case DepthFormat::D16: | ||||
|             return 2; | ||||
|         case DepthFormat::D24: | ||||
|             return 3; | ||||
|         case DepthFormat::D24S8: | ||||
|             return 4; | ||||
|         default: | ||||
|             LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format); | ||||
|             UNIMPLEMENTED(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     struct { | ||||
|         // Components are laid out in reverse byte order, most significant bits first.
 | ||||
|         enum ColorFormat : u32 { | ||||
|  | @ -420,7 +452,7 @@ struct Regs { | |||
| 
 | ||||
|         INSERT_PADDING_WORDS(0x6); | ||||
| 
 | ||||
|         u32 depth_format; | ||||
|         DepthFormat depth_format; | ||||
|         BitField<16, 3, u32> color_format; | ||||
| 
 | ||||
|         INSERT_PADDING_WORDS(0x4); | ||||
|  |  | |||
|  | @ -91,7 +91,7 @@ static const Math::Vec4<u8> GetPixel(int x, int y) { | |||
|     } | ||||
| 
 | ||||
|     return {}; | ||||
|  } | ||||
| } | ||||
| 
 | ||||
| static u32 GetDepth(int x, int y) { | ||||
|     const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress(); | ||||
|  | @ -100,23 +100,55 @@ static u32 GetDepth(int x, int y) { | |||
|     y = (registers.framebuffer.height - y); | ||||
|      | ||||
|     const u32 coarse_y = y & ~7; | ||||
|     u32 stride = registers.framebuffer.width * 2; | ||||
|     u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(registers.framebuffer.depth_format); | ||||
|     u32 stride = registers.framebuffer.width * bytes_per_pixel; | ||||
| 
 | ||||
|     // Assuming 16-bit depth buffer format until actual format handling is implemented
 | ||||
|     return *(u16*)(depth_buffer + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * stride); | ||||
|     u32 src_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; | ||||
|     u8* src_pixel = depth_buffer + src_offset; | ||||
| 
 | ||||
|     switch (registers.framebuffer.depth_format) { | ||||
|         case Pica::Regs::DepthFormat::D16: | ||||
|             return Color::DecodeD16(src_pixel); | ||||
|         case Pica::Regs::DepthFormat::D24: | ||||
|             return Color::DecodeD24(src_pixel); | ||||
|         case Pica::Regs::DepthFormat::D24S8: | ||||
|             return Color::DecodeD24S8(src_pixel).x; | ||||
|         default: | ||||
|             LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", registers.framebuffer.depth_format); | ||||
|             UNIMPLEMENTED(); | ||||
|             return 0; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| static void SetDepth(int x, int y, u16 value) { | ||||
| static void SetDepth(int x, int y, u32 value) { | ||||
|     const PAddr addr = registers.framebuffer.GetDepthBufferPhysicalAddress(); | ||||
|     u8* depth_buffer = Memory::GetPointer(PAddrToVAddr(addr)); | ||||
| 
 | ||||
|     y = (registers.framebuffer.height - y); | ||||
| 
 | ||||
|     const u32 coarse_y = y & ~7; | ||||
|     u32 stride = registers.framebuffer.width * 2; | ||||
|     u32 bytes_per_pixel = Pica::Regs::BytesPerDepthPixel(registers.framebuffer.depth_format); | ||||
|     u32 stride = registers.framebuffer.width * bytes_per_pixel; | ||||
| 
 | ||||
|     // Assuming 16-bit depth buffer format until actual format handling is implemented
 | ||||
|     *(u16*)(depth_buffer + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * stride) = value; | ||||
|     u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * stride; | ||||
|     u8* dst_pixel = depth_buffer + dst_offset; | ||||
| 
 | ||||
|     switch (registers.framebuffer.depth_format) { | ||||
|         case Pica::Regs::DepthFormat::D16: | ||||
|             Color::EncodeD16(value, dst_pixel); | ||||
|             break; | ||||
|         case Pica::Regs::DepthFormat::D24: | ||||
|             Color::EncodeD24(value, dst_pixel); | ||||
|             break; | ||||
|         case Pica::Regs::DepthFormat::D24S8: | ||||
|             // TODO(Subv): Implement the stencil buffer
 | ||||
|             Color::EncodeD24S8(value, 0, dst_pixel); | ||||
|             break; | ||||
|         default: | ||||
|             LOG_CRITICAL(HW_GPU, "Unimplemented depth format %u", registers.framebuffer.depth_format); | ||||
|             UNIMPLEMENTED(); | ||||
|             break; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values
 | ||||
|  | @ -595,7 +627,7 @@ static void ProcessTriangleInternal(const VertexShader::OutputVertex& v0, | |||
|                 u16 z = (u16)((v0.screenpos[2].ToFloat32() * w0 + | ||||
|                             v1.screenpos[2].ToFloat32() * w1 + | ||||
|                             v2.screenpos[2].ToFloat32() * w2) * 65535.f / wsum); | ||||
|                 u16 ref_z = GetDepth(x >> 4, y >> 4); | ||||
|                 u32 ref_z = GetDepth(x >> 4, y >> 4); | ||||
| 
 | ||||
|                 bool pass = false; | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue