From ce1d5be212e6f71a7ca32c3bd7b48e32d9f51b9a Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sat, 8 Sep 2018 14:51:50 -0300 Subject: Move GPU emulation from Ryujinx.HLE to Ryujinx.Graphics and misc changes (#402) * Move GPU LLE emulation from HLE to Graphics * Graphics: Move Gal/Texture to Texture * Remove Engines/ directory and namespace * Use tables for image formats * Abstract OpCode decoding * Simplify image table * Do not leak Read* symbols in TextureReader * Fixups * Rename IGalFrameBuffer -> IGalRenderTarget * Remove MaxBpp hardcoded value * Change yet again texture data and add G8R8 flipping * Rename GalFrameBufferFormat to GalSurfaceFormat * Unident EnsureSetup in ImageHandler * Add IsCompressed * Address some feedback --- Ryujinx.Graphics/Gal/GalFrameBufferFormat.cs | 2 +- Ryujinx.Graphics/Gal/GalImageFormat.cs | 275 ++-- Ryujinx.Graphics/Gal/IGalFrameBuffer.cs | 51 - Ryujinx.Graphics/Gal/IGalRenderTarget.cs | 51 + Ryujinx.Graphics/Gal/IGalRenderer.cs | 2 +- Ryujinx.Graphics/Gal/ImageFormatConverter.cs | 279 ---- Ryujinx.Graphics/Gal/OpenGL/ImageHandler.cs | 143 +- Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs | 111 +- Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs | 496 ------- Ryujinx.Graphics/Gal/OpenGL/OGLRenderTarget.cs | 497 +++++++ Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs | 4 +- Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs | 99 +- Ryujinx.Graphics/Gal/Shader/ShaderDecodeAlu.cs | 400 +++--- Ryujinx.Graphics/Gal/Shader/ShaderDecodeFlow.cs | 12 +- Ryujinx.Graphics/Gal/Shader/ShaderDecodeHelper.cs | 238 ---- Ryujinx.Graphics/Gal/Shader/ShaderDecodeMem.cs | 76 +- Ryujinx.Graphics/Gal/Shader/ShaderDecodeMove.cs | 98 +- Ryujinx.Graphics/Gal/Shader/ShaderDecodeOpCode.cs | 260 ++++ Ryujinx.Graphics/Gal/Shader/ShaderDecodeSpecial.cs | 12 +- Ryujinx.Graphics/Gal/Texture/ASTCDecoder.cs | 1384 -------------------- Ryujinx.Graphics/Gal/Texture/ASTCPixel.cs | 138 -- Ryujinx.Graphics/Gal/Texture/BitArrayStream.cs | 120 -- Ryujinx.Graphics/Gal/Texture/IntegerEncoded.cs | 269 ---- Ryujinx.Graphics/INvGpuEngine.cs | 11 + Ryujinx.Graphics/MacroInterpreter.cs | 418 ++++++ Ryujinx.Graphics/Memory/NvGpuBufferType.cs | 11 + Ryujinx.Graphics/Memory/NvGpuPBEntry.cs | 23 + Ryujinx.Graphics/Memory/NvGpuPushBuffer.cs | 101 ++ Ryujinx.Graphics/Memory/NvGpuVmm.cs | 377 ++++++ Ryujinx.Graphics/Memory/NvGpuVmmCache.cs | 309 +++++ Ryujinx.Graphics/NvGpu.cs | 27 + Ryujinx.Graphics/NvGpuEngine.cs | 11 + Ryujinx.Graphics/NvGpuEngine2d.cs | 208 +++ Ryujinx.Graphics/NvGpuEngine2dReg.cs | 25 + Ryujinx.Graphics/NvGpuEngine3d.cs | 898 +++++++++++++ Ryujinx.Graphics/NvGpuEngine3dReg.cs | 101 ++ Ryujinx.Graphics/NvGpuEngineDma.cs | 143 ++ Ryujinx.Graphics/NvGpuEngineDmaReg.cs | 22 + Ryujinx.Graphics/NvGpuFifo.cs | 198 +++ Ryujinx.Graphics/NvGpuFifoMeth.cs | 11 + Ryujinx.Graphics/NvGpuMethod.cs | 6 + Ryujinx.Graphics/Texture/ASTCDecoder.cs | 1384 ++++++++++++++++++++ Ryujinx.Graphics/Texture/ASTCPixel.cs | 138 ++ Ryujinx.Graphics/Texture/BitArrayStream.cs | 121 ++ Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs | 59 + Ryujinx.Graphics/Texture/ISwizzle.cs | 7 + Ryujinx.Graphics/Texture/ImageConverter.cs | 24 + Ryujinx.Graphics/Texture/ImageUtils.cs | 357 +++++ Ryujinx.Graphics/Texture/IntegerEncoded.cs | 269 ++++ Ryujinx.Graphics/Texture/LinearSwizzle.cs | 19 + Ryujinx.Graphics/Texture/TextureFactory.cs | 132 ++ Ryujinx.Graphics/Texture/TextureHelper.cs | 45 + Ryujinx.Graphics/Texture/TextureInfo.cs | 60 + Ryujinx.Graphics/Texture/TextureReader.cs | 398 ++++++ Ryujinx.Graphics/Texture/TextureSwizzle.cs | 11 + Ryujinx.Graphics/Texture/TextureWriter.cs | 35 + Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs | 11 - Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs | 418 ------ Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs | 11 - Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs | 208 --- Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs | 25 - Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs | 895 ------------- Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs | 101 -- Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs | 143 -- Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs | 22 - Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs | 198 --- Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs | 11 - Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs | 6 - Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs | 11 - Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs | 23 - Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs | 101 -- Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs | 377 ------ Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs | 308 ----- Ryujinx.HLE/Gpu/NvGpu.cs | 27 - Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs | 59 - Ryujinx.HLE/Gpu/Texture/ISwizzle.cs | 7 - Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs | 19 - Ryujinx.HLE/Gpu/Texture/TextureFactory.cs | 125 -- Ryujinx.HLE/Gpu/Texture/TextureHelper.cs | 204 --- Ryujinx.HLE/Gpu/Texture/TextureInfo.cs | 60 - Ryujinx.HLE/Gpu/Texture/TextureReader.cs | 366 ------ Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs | 11 - Ryujinx.HLE/Gpu/Texture/TextureWriter.cs | 33 - Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASCtx.cs | 2 +- .../HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs | 2 +- .../Nv/NvHostChannel/NvHostChannelIoctl.cs | 2 +- Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs | 2 +- Ryujinx.HLE/HOS/Services/Vi/NvFlinger.cs | 8 +- Ryujinx.HLE/Switch.cs | 2 +- Ryujinx/Ui/GLScreen.cs | 6 +- 90 files changed, 7355 insertions(+), 7425 deletions(-) delete mode 100644 Ryujinx.Graphics/Gal/IGalFrameBuffer.cs create mode 100644 Ryujinx.Graphics/Gal/IGalRenderTarget.cs delete mode 100644 Ryujinx.Graphics/Gal/ImageFormatConverter.cs delete mode 100644 Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs create mode 100644 Ryujinx.Graphics/Gal/OpenGL/OGLRenderTarget.cs create mode 100644 Ryujinx.Graphics/Gal/Shader/ShaderDecodeOpCode.cs delete mode 100644 Ryujinx.Graphics/Gal/Texture/ASTCDecoder.cs delete mode 100644 Ryujinx.Graphics/Gal/Texture/ASTCPixel.cs delete mode 100644 Ryujinx.Graphics/Gal/Texture/BitArrayStream.cs delete mode 100644 Ryujinx.Graphics/Gal/Texture/IntegerEncoded.cs create mode 100644 Ryujinx.Graphics/INvGpuEngine.cs create mode 100644 Ryujinx.Graphics/MacroInterpreter.cs create mode 100644 Ryujinx.Graphics/Memory/NvGpuBufferType.cs create mode 100644 Ryujinx.Graphics/Memory/NvGpuPBEntry.cs create mode 100644 Ryujinx.Graphics/Memory/NvGpuPushBuffer.cs create mode 100644 Ryujinx.Graphics/Memory/NvGpuVmm.cs create mode 100644 Ryujinx.Graphics/Memory/NvGpuVmmCache.cs create mode 100644 Ryujinx.Graphics/NvGpu.cs create mode 100644 Ryujinx.Graphics/NvGpuEngine.cs create mode 100644 Ryujinx.Graphics/NvGpuEngine2d.cs create mode 100644 Ryujinx.Graphics/NvGpuEngine2dReg.cs create mode 100644 Ryujinx.Graphics/NvGpuEngine3d.cs create mode 100644 Ryujinx.Graphics/NvGpuEngine3dReg.cs create mode 100644 Ryujinx.Graphics/NvGpuEngineDma.cs create mode 100644 Ryujinx.Graphics/NvGpuEngineDmaReg.cs create mode 100644 Ryujinx.Graphics/NvGpuFifo.cs create mode 100644 Ryujinx.Graphics/NvGpuFifoMeth.cs create mode 100644 Ryujinx.Graphics/NvGpuMethod.cs create mode 100644 Ryujinx.Graphics/Texture/ASTCDecoder.cs create mode 100644 Ryujinx.Graphics/Texture/ASTCPixel.cs create mode 100644 Ryujinx.Graphics/Texture/BitArrayStream.cs create mode 100644 Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs create mode 100644 Ryujinx.Graphics/Texture/ISwizzle.cs create mode 100644 Ryujinx.Graphics/Texture/ImageConverter.cs create mode 100644 Ryujinx.Graphics/Texture/ImageUtils.cs create mode 100644 Ryujinx.Graphics/Texture/IntegerEncoded.cs create mode 100644 Ryujinx.Graphics/Texture/LinearSwizzle.cs create mode 100644 Ryujinx.Graphics/Texture/TextureFactory.cs create mode 100644 Ryujinx.Graphics/Texture/TextureHelper.cs create mode 100644 Ryujinx.Graphics/Texture/TextureInfo.cs create mode 100644 Ryujinx.Graphics/Texture/TextureReader.cs create mode 100644 Ryujinx.Graphics/Texture/TextureSwizzle.cs create mode 100644 Ryujinx.Graphics/Texture/TextureWriter.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs delete mode 100644 Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs delete mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs delete mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs delete mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs delete mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs delete mode 100644 Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs delete mode 100644 Ryujinx.HLE/Gpu/NvGpu.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/ISwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/TextureFactory.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/TextureHelper.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/TextureInfo.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/TextureReader.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs delete mode 100644 Ryujinx.HLE/Gpu/Texture/TextureWriter.cs diff --git a/Ryujinx.Graphics/Gal/GalFrameBufferFormat.cs b/Ryujinx.Graphics/Gal/GalFrameBufferFormat.cs index 3180aeff..08bd622b 100644 --- a/Ryujinx.Graphics/Gal/GalFrameBufferFormat.cs +++ b/Ryujinx.Graphics/Gal/GalFrameBufferFormat.cs @@ -1,6 +1,6 @@ namespace Ryujinx.Graphics.Gal { - public enum GalFrameBufferFormat + public enum GalSurfaceFormat { Bitmap = 0x1c, Unknown1D = 0x1d, diff --git a/Ryujinx.Graphics/Gal/GalImageFormat.cs b/Ryujinx.Graphics/Gal/GalImageFormat.cs index 4e84067b..ba555684 100644 --- a/Ryujinx.Graphics/Gal/GalImageFormat.cs +++ b/Ryujinx.Graphics/Gal/GalImageFormat.cs @@ -1,204 +1,93 @@ -namespace Ryujinx.Graphics.Gal +using System; + +namespace Ryujinx.Graphics.Gal { - //These are Vulkan-based enumerations, do not take them as Tegra values + [Flags] public enum GalImageFormat { - Undefined = 0, - - R4G4_UNORM_PACK8 = 1, - R4G4B4A4_UNORM_PACK16 = 2, - B4G4R4A4_UNORM_PACK16 = 3, - R5G6B5_UNORM_PACK16 = 4, - B5G6R5_UNORM_PACK16 = 5, - R5G5B5A1_UNORM_PACK16 = 6, - B5G5R5A1_UNORM_PACK16 = 7, - A1R5G5B5_UNORM_PACK16 = 8, - R8_UNORM = 9, - R8_SNORM = 10, - R8_USCALED = 11, - R8_SSCALED = 12, - R8_UINT = 13, - R8_SINT = 14, - R8_SRGB = 15, - R8G8_UNORM = 16, - R8G8_SNORM = 17, - R8G8_USCALED = 18, - R8G8_SSCALED = 19, - R8G8_UINT = 20, - R8G8_SINT = 21, - R8G8_SRGB = 22, - R8G8B8_UNORM = 23, - R8G8B8_SNORM = 24, - R8G8B8_USCALED = 25, - R8G8B8_SSCALED = 26, - R8G8B8_UINT = 27, - R8G8B8_SINT = 28, - R8G8B8_SRGB = 29, - B8G8R8_UNORM = 30, - B8G8R8_SNORM = 31, - B8G8R8_USCALED = 32, - B8G8R8_SSCALED = 33, - B8G8R8_UINT = 34, - B8G8R8_SINT = 35, - B8G8R8_SRGB = 36, - R8G8B8A8_UNORM = 37, - R8G8B8A8_SNORM = 38, - R8G8B8A8_USCALED = 39, - R8G8B8A8_SSCALED = 40, - R8G8B8A8_UINT = 41, - R8G8B8A8_SINT = 42, - R8G8B8A8_SRGB = 43, - B8G8R8A8_UNORM = 44, - B8G8R8A8_SNORM = 45, - B8G8R8A8_USCALED = 46, - B8G8R8A8_SSCALED = 47, - B8G8R8A8_UINT = 48, - B8G8R8A8_SINT = 49, - B8G8R8A8_SRGB = 50, - A8B8G8R8_UNORM_PACK32 = 51, - A8B8G8R8_SNORM_PACK32 = 52, - A8B8G8R8_USCALED_PACK32 = 53, - A8B8G8R8_SSCALED_PACK32 = 54, - A8B8G8R8_UINT_PACK32 = 55, - A8B8G8R8_SINT_PACK32 = 56, - A8B8G8R8_SRGB_PACK32 = 57, - A2R10G10B10_UNORM_PACK32 = 58, - A2R10G10B10_SNORM_PACK32 = 59, - A2R10G10B10_USCALED_PACK32 = 60, - A2R10G10B10_SSCALED_PACK32 = 61, - A2R10G10B10_UINT_PACK32 = 62, - A2R10G10B10_SINT_PACK32 = 63, - A2B10G10R10_UNORM_PACK32 = 64, - A2B10G10R10_SNORM_PACK32 = 65, - A2B10G10R10_USCALED_PACK32 = 66, - A2B10G10R10_SSCALED_PACK32 = 67, - A2B10G10R10_UINT_PACK32 = 68, - A2B10G10R10_SINT_PACK32 = 69, - R16_UNORM = 70, - R16_SNORM = 71, - R16_USCALED = 72, - R16_SSCALED = 73, - R16_UINT = 74, - R16_SINT = 75, - R16_SFLOAT = 76, - R16G16_UNORM = 77, - R16G16_SNORM = 78, - R16G16_USCALED = 79, - R16G16_SSCALED = 80, - R16G16_UINT = 81, - R16G16_SINT = 82, - R16G16_SFLOAT = 83, - R16G16B16_UNORM = 84, - R16G16B16_SNORM = 85, - R16G16B16_USCALED = 86, - R16G16B16_SSCALED = 87, - R16G16B16_UINT = 88, - R16G16B16_SINT = 89, - R16G16B16_SFLOAT = 90, - R16G16B16A16_UNORM = 91, - R16G16B16A16_SNORM = 92, - R16G16B16A16_USCALED = 93, - R16G16B16A16_SSCALED = 94, - R16G16B16A16_UINT = 95, - R16G16B16A16_SINT = 96, - R16G16B16A16_SFLOAT = 97, - R32_UINT = 98, - R32_SINT = 99, - R32_SFLOAT = 100, - R32G32_UINT = 101, - R32G32_SINT = 102, - R32G32_SFLOAT = 103, - R32G32B32_UINT = 104, - R32G32B32_SINT = 105, - R32G32B32_SFLOAT = 106, - R32G32B32A32_UINT = 107, - R32G32B32A32_SINT = 108, - R32G32B32A32_SFLOAT = 109, - R64_UINT = 110, - R64_SINT = 111, - R64_SFLOAT = 112, - R64G64_UINT = 113, - R64G64_SINT = 114, - R64G64_SFLOAT = 115, - R64G64B64_UINT = 116, - R64G64B64_SINT = 117, - R64G64B64_SFLOAT = 118, - R64G64B64A64_UINT = 119, - R64G64B64A64_SINT = 120, - R64G64B64A64_SFLOAT = 121, - B10G11R11_UFLOAT_PACK32 = 122, - E5B9G9R9_UFLOAT_PACK32 = 123, - D16_UNORM = 124, - X8_D24_UNORM_PACK32 = 125, - D32_SFLOAT = 126, - S8_UINT = 127, - D16_UNORM_S8_UINT = 128, - D24_UNORM_S8_UINT = 129, - D32_SFLOAT_S8_UINT = 130, - BC1_RGB_UNORM_BLOCK = 131, - BC1_RGB_SRGB_BLOCK = 132, - BC1_RGBA_UNORM_BLOCK = 133, - BC1_RGBA_SRGB_BLOCK = 134, - BC2_UNORM_BLOCK = 135, - BC2_SRGB_BLOCK = 136, - BC3_UNORM_BLOCK = 137, - BC3_SRGB_BLOCK = 138, - BC4_UNORM_BLOCK = 139, - BC4_SNORM_BLOCK = 140, - BC5_UNORM_BLOCK = 141, - BC5_SNORM_BLOCK = 142, - BC6H_UFLOAT_BLOCK = 143, - BC6H_SFLOAT_BLOCK = 144, - BC7_UNORM_BLOCK = 145, - BC7_SRGB_BLOCK = 146, - ETC2_R8G8B8_UNORM_BLOCK = 147, - ETC2_R8G8B8_SRGB_BLOCK = 148, - ETC2_R8G8B8A1_UNORM_BLOCK = 149, - ETC2_R8G8B8A1_SRGB_BLOCK = 150, - ETC2_R8G8B8A8_UNORM_BLOCK = 151, - ETC2_R8G8B8A8_SRGB_BLOCK = 152, - EAC_R11_UNORM_BLOCK = 153, - EAC_R11_SNORM_BLOCK = 154, - EAC_R11G11_UNORM_BLOCK = 155, - EAC_R11G11_SNORM_BLOCK = 156, + Snorm = 1 << 27, + Unorm = 1 << 28, + Sint = 1 << 29, + Uint = 1 << 30, + Sfloat = 1 << 31, - ASTC_BEGIN = ASTC_4x4_UNORM_BLOCK, + TypeMask = Snorm | Unorm | Sint | Uint | Sfloat, - ASTC_4x4_UNORM_BLOCK = 157, - ASTC_4x4_SRGB_BLOCK = 158, - ASTC_5x4_UNORM_BLOCK = 159, - ASTC_5x4_SRGB_BLOCK = 160, - ASTC_5x5_UNORM_BLOCK = 161, - ASTC_5x5_SRGB_BLOCK = 162, - ASTC_6x5_UNORM_BLOCK = 163, - ASTC_6x5_SRGB_BLOCK = 164, - ASTC_6x6_UNORM_BLOCK = 165, - ASTC_6x6_SRGB_BLOCK = 166, - ASTC_8x5_UNORM_BLOCK = 167, - ASTC_8x5_SRGB_BLOCK = 168, - ASTC_8x6_UNORM_BLOCK = 169, - ASTC_8x6_SRGB_BLOCK = 170, - ASTC_8x8_UNORM_BLOCK = 171, - ASTC_8x8_SRGB_BLOCK = 172, - ASTC_10x5_UNORM_BLOCK = 173, - ASTC_10x5_SRGB_BLOCK = 174, - ASTC_10x6_UNORM_BLOCK = 175, - ASTC_10x6_SRGB_BLOCK = 176, - ASTC_10x8_UNORM_BLOCK = 177, - ASTC_10x8_SRGB_BLOCK = 178, - ASTC_10x10_UNORM_BLOCK = 179, - ASTC_10x10_SRGB_BLOCK = 180, - ASTC_12x10_UNORM_BLOCK = 181, - ASTC_12x10_SRGB_BLOCK = 182, - ASTC_12x12_UNORM_BLOCK = 183, - ASTC_12x12_SRGB_BLOCK = 184, + FormatMask = ~TypeMask, - ASTC_END = ASTC_12x12_SRGB_BLOCK, + ASTC_BEGIN = ASTC_4x4, - REVERSED_BEGIN, + ASTC_4x4 = 1, + ASTC_5x4, + ASTC_5x5, + ASTC_6x5, + ASTC_6x6, + ASTC_8x5, + ASTC_8x6, + ASTC_8x8, + ASTC_10x5, + ASTC_10x6, + ASTC_10x8, + ASTC_10x10, + ASTC_12x10, + ASTC_12x12, - R4G4B4A4_UNORM_PACK16_REVERSED = REVERSED_BEGIN, + ASTC_END = ASTC_12x12, - REVERSED_END + R4G4, + R4G4B4A4, + B4G4R4A4, + A4B4G4R4, + R5G6B5, + B5G6R5, + R5G5B5A1, + B5G5R5A1, + A1R5G5B5, + R8, + R8G8, + G8R8, + R8G8B8, + B8G8R8, + R8G8B8A8, + B8G8R8A8, + A8B8G8R8, + A8B8G8R8_SRGB, + A2R10G10B10, + A2B10G10R10, + R16, + R16G16, + R16G16B16, + R16G16B16A16, + R32, + R32G32, + R32G32B32, + R32G32B32A32, + R64, + R64G64, + R64G64B64, + R64G64B64A64, + B10G11R11, + E5B9G9R9, + D16, + X8_D24, + D32, + S8, + D16_S8, + D24_S8, + D32_S8, + BC1_RGB, + BC1_RGBA, + BC2, + BC3, + BC4, + BC5, + BC6H_SF16, + BC6H_UF16, + BC7, + ETC2_R8G8B8, + ETC2_R8G8B8A1, + ETC2_R8G8B8A8, + EAC_R11, + EAC_R11G11, } } \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalFrameBuffer.cs b/Ryujinx.Graphics/Gal/IGalFrameBuffer.cs deleted file mode 100644 index 108d3d9b..00000000 --- a/Ryujinx.Graphics/Gal/IGalFrameBuffer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; - -namespace Ryujinx.Graphics.Gal -{ - public interface IGalFrameBuffer - { - void BindColor(long Key, int Attachment); - - void UnbindColor(int Attachment); - - void BindZeta(long Key); - - void UnbindZeta(); - - void BindTexture(long Key, int Index); - - void Set(long Key); - - void Set(byte[] Data, int Width, int Height); - - void SetMap(int[] Map); - - void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom); - - void SetWindowSize(int Width, int Height); - - void SetViewport(int X, int Y, int Width, int Height); - - void Render(); - - void Copy( - long SrcKey, - long DstKey, - int SrcX0, - int SrcY0, - int SrcX1, - int SrcY1, - int DstX0, - int DstY0, - int DstX1, - int DstY1); - - void GetBufferData(long Key, Action Callback); - - void SetBufferData( - long Key, - int Width, - int Height, - byte[] Buffer); - } -} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalRenderTarget.cs b/Ryujinx.Graphics/Gal/IGalRenderTarget.cs new file mode 100644 index 00000000..c44434ef --- /dev/null +++ b/Ryujinx.Graphics/Gal/IGalRenderTarget.cs @@ -0,0 +1,51 @@ +using System; + +namespace Ryujinx.Graphics.Gal +{ + public interface IGalRenderTarget + { + void BindColor(long Key, int Attachment); + + void UnbindColor(int Attachment); + + void BindZeta(long Key); + + void UnbindZeta(); + + void BindTexture(long Key, int Index); + + void Set(long Key); + + void Set(byte[] Data, int Width, int Height); + + void SetMap(int[] Map); + + void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom); + + void SetWindowSize(int Width, int Height); + + void SetViewport(int X, int Y, int Width, int Height); + + void Render(); + + void Copy( + long SrcKey, + long DstKey, + int SrcX0, + int SrcY0, + int SrcX1, + int SrcY1, + int DstX0, + int DstY0, + int DstX1, + int DstY1); + + void GetBufferData(long Key, Action Callback); + + void SetBufferData( + long Key, + int Width, + int Height, + byte[] Buffer); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/IGalRenderer.cs b/Ryujinx.Graphics/Gal/IGalRenderer.cs index b47ac71d..41e95a87 100644 --- a/Ryujinx.Graphics/Gal/IGalRenderer.cs +++ b/Ryujinx.Graphics/Gal/IGalRenderer.cs @@ -10,7 +10,7 @@ namespace Ryujinx.Graphics.Gal IGalConstBuffer Buffer { get; } - IGalFrameBuffer FrameBuffer { get; } + IGalRenderTarget RenderTarget { get; } IGalRasterizer Rasterizer { get; } diff --git a/Ryujinx.Graphics/Gal/ImageFormatConverter.cs b/Ryujinx.Graphics/Gal/ImageFormatConverter.cs deleted file mode 100644 index 7aa39f5f..00000000 --- a/Ryujinx.Graphics/Gal/ImageFormatConverter.cs +++ /dev/null @@ -1,279 +0,0 @@ -using System; - -namespace Ryujinx.Graphics.Gal -{ - public static class ImageFormatConverter - { - public static GalImageFormat ConvertTexture( - GalTextureFormat Format, - GalTextureType RType, - GalTextureType GType, - GalTextureType BType, - GalTextureType AType) - { - if (RType != GType || RType != BType || RType != AType) - { - throw new NotImplementedException("Per component types are not implemented"); - } - - GalTextureType Type = RType; - - switch (Type) - { - case GalTextureType.Snorm: - switch (Format) - { - case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SNORM; - case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_SNORM_PACK32; - case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_SNORM_PACK32; - case GalTextureFormat.G8R8: return GalImageFormat.R8G8_SNORM; - case GalTextureFormat.R16: return GalImageFormat.R16_SNORM; - case GalTextureFormat.R8: return GalImageFormat.R8_SNORM; - case GalTextureFormat.BC4: return GalImageFormat.BC4_SNORM_BLOCK; - case GalTextureFormat.BC5: return GalImageFormat.BC5_SNORM_BLOCK; - } - break; - - case GalTextureType.Unorm: - switch (Format) - { - case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_UNORM; - case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_UNORM_PACK32; - case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_UNORM_PACK32; - case GalTextureFormat.A4B4G4R4: return GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED; - case GalTextureFormat.A1B5G5R5: return GalImageFormat.A1R5G5B5_UNORM_PACK16; - case GalTextureFormat.B5G6R5: return GalImageFormat.B5G6R5_UNORM_PACK16; - case GalTextureFormat.BC7U: return GalImageFormat.BC7_UNORM_BLOCK; - case GalTextureFormat.G8R8: return GalImageFormat.R8G8_UNORM; - case GalTextureFormat.R16: return GalImageFormat.R16_UNORM; - case GalTextureFormat.R8: return GalImageFormat.R8_UNORM; - case GalTextureFormat.BC1: return GalImageFormat.BC1_RGBA_UNORM_BLOCK; - case GalTextureFormat.BC2: return GalImageFormat.BC2_UNORM_BLOCK; - case GalTextureFormat.BC3: return GalImageFormat.BC3_UNORM_BLOCK; - case GalTextureFormat.BC4: return GalImageFormat.BC4_UNORM_BLOCK; - case GalTextureFormat.BC5: return GalImageFormat.BC5_UNORM_BLOCK; - case GalTextureFormat.Z24S8: return GalImageFormat.D24_UNORM_S8_UINT; - case GalTextureFormat.ZF32_X24S8: return GalImageFormat.D32_SFLOAT_S8_UINT; - case GalTextureFormat.Astc2D4x4: return GalImageFormat.ASTC_4x4_UNORM_BLOCK; - case GalTextureFormat.Astc2D5x5: return GalImageFormat.ASTC_5x5_UNORM_BLOCK; - case GalTextureFormat.Astc2D6x6: return GalImageFormat.ASTC_6x6_UNORM_BLOCK; - case GalTextureFormat.Astc2D8x8: return GalImageFormat.ASTC_8x8_UNORM_BLOCK; - case GalTextureFormat.Astc2D10x10: return GalImageFormat.ASTC_10x10_UNORM_BLOCK; - case GalTextureFormat.Astc2D12x12: return GalImageFormat.ASTC_12x12_UNORM_BLOCK; - case GalTextureFormat.Astc2D5x4: return GalImageFormat.ASTC_5x4_UNORM_BLOCK; - case GalTextureFormat.Astc2D6x5: return GalImageFormat.ASTC_6x5_UNORM_BLOCK; - case GalTextureFormat.Astc2D8x6: return GalImageFormat.ASTC_8x6_UNORM_BLOCK; - case GalTextureFormat.Astc2D10x8: return GalImageFormat.ASTC_10x8_UNORM_BLOCK; - case GalTextureFormat.Astc2D12x10: return GalImageFormat.ASTC_12x10_UNORM_BLOCK; - case GalTextureFormat.Astc2D8x5: return GalImageFormat.ASTC_8x5_UNORM_BLOCK; - case GalTextureFormat.Astc2D10x5: return GalImageFormat.ASTC_10x5_UNORM_BLOCK; - case GalTextureFormat.Astc2D10x6: return GalImageFormat.ASTC_10x6_UNORM_BLOCK; - } - break; - - case GalTextureType.Sint: - switch (Format) - { - case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_SINT; - case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SINT; - case GalTextureFormat.R32G32: return GalImageFormat.R32G32_SINT; - case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_SINT_PACK32; - case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_SINT_PACK32; - case GalTextureFormat.R32: return GalImageFormat.R32_SINT; - case GalTextureFormat.G8R8: return GalImageFormat.R8G8_SINT; - case GalTextureFormat.R16: return GalImageFormat.R16_SINT; - case GalTextureFormat.R8: return GalImageFormat.R8_SINT; - } - break; - - case GalTextureType.Uint: - switch (Format) - { - case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_UINT; - case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_UINT; - case GalTextureFormat.R32G32: return GalImageFormat.R32G32_UINT; - case GalTextureFormat.A8B8G8R8: return GalImageFormat.A8B8G8R8_UINT_PACK32; - case GalTextureFormat.A2B10G10R10: return GalImageFormat.A2B10G10R10_UINT_PACK32; - case GalTextureFormat.R32: return GalImageFormat.R32_UINT; - case GalTextureFormat.G8R8: return GalImageFormat.R8G8_UINT; - case GalTextureFormat.R16: return GalImageFormat.R16_UINT; - case GalTextureFormat.R8: return GalImageFormat.R8_UINT; - } - break; - - case GalTextureType.Snorm_Force_Fp16: - //TODO - break; - - case GalTextureType.Unorm_Force_Fp16: - //TODO - break; - - case GalTextureType.Float: - switch (Format) - { - case GalTextureFormat.R32G32B32A32: return GalImageFormat.R32G32B32A32_SFLOAT; - case GalTextureFormat.R16G16B16A16: return GalImageFormat.R16G16B16A16_SFLOAT; - case GalTextureFormat.R32G32: return GalImageFormat.R32G32_SFLOAT; - case GalTextureFormat.R32: return GalImageFormat.R32_SFLOAT; - case GalTextureFormat.BC6H_SF16: return GalImageFormat.BC6H_SFLOAT_BLOCK; - case GalTextureFormat.BC6H_UF16: return GalImageFormat.BC6H_UFLOAT_BLOCK; - case GalTextureFormat.R16: return GalImageFormat.R16_SFLOAT; - case GalTextureFormat.BF10GF11RF11: return GalImageFormat.B10G11R11_UFLOAT_PACK32; - case GalTextureFormat.ZF32: return GalImageFormat.D32_SFLOAT; - } - break; - } - - throw new NotImplementedException("0x" + ((int)Format).ToString("x2") + " " + Type.ToString()); - } - - public static GalImageFormat ConvertFrameBuffer(GalFrameBufferFormat Format) - { - switch (Format) - { - case GalFrameBufferFormat.R32Float: return GalImageFormat.R32_SFLOAT; - case GalFrameBufferFormat.RGB10A2Unorm: return GalImageFormat.A2B10G10R10_UNORM_PACK32; - case GalFrameBufferFormat.RGBA8Srgb: return GalImageFormat.A8B8G8R8_SRGB_PACK32; - case GalFrameBufferFormat.RGBA16Float: return GalImageFormat.R16G16B16A16_SFLOAT; - case GalFrameBufferFormat.R16Float: return GalImageFormat.R16_SFLOAT; - case GalFrameBufferFormat.R8Unorm: return GalImageFormat.R8_UNORM; - case GalFrameBufferFormat.RGBA8Unorm: return GalImageFormat.A8B8G8R8_UNORM_PACK32; - case GalFrameBufferFormat.R11G11B10Float: return GalImageFormat.B10G11R11_UFLOAT_PACK32; - case GalFrameBufferFormat.RGBA32Float: return GalImageFormat.R32G32B32A32_SFLOAT; - case GalFrameBufferFormat.RG16Snorm: return GalImageFormat.R16G16_SNORM; - case GalFrameBufferFormat.RG16Float: return GalImageFormat.R16G16_SFLOAT; - case GalFrameBufferFormat.RG8Snorm: return GalImageFormat.R8_SNORM; - case GalFrameBufferFormat.RGBA8Snorm: return GalImageFormat.A8B8G8R8_SNORM_PACK32; - case GalFrameBufferFormat.RG8Unorm: return GalImageFormat.R8G8_UNORM; - case GalFrameBufferFormat.BGRA8Unorm: return GalImageFormat.A8B8G8R8_UNORM_PACK32; - case GalFrameBufferFormat.BGRA8Srgb: return GalImageFormat.A8B8G8R8_SRGB_PACK32; - case GalFrameBufferFormat.RG32Float: return GalImageFormat.R32G32_SFLOAT; - case GalFrameBufferFormat.RG32Sint: return GalImageFormat.R32G32_SINT; - case GalFrameBufferFormat.RG32Uint: return GalImageFormat.R32G32_UINT; - } - - throw new NotImplementedException(Format.ToString()); - } - - public static GalImageFormat ConvertZeta(GalZetaFormat Format) - { - switch (Format) - { - case GalZetaFormat.Z32Float: return GalImageFormat.D32_SFLOAT; - case GalZetaFormat.S8Z24Unorm: return GalImageFormat.D24_UNORM_S8_UINT; - case GalZetaFormat.Z16Unorm: return GalImageFormat.D16_UNORM; - case GalZetaFormat.Z32S8X24Float: return GalImageFormat.D32_SFLOAT_S8_UINT; - } - - throw new NotImplementedException(Format.ToString()); - } - - public static bool HasColor(GalImageFormat Format) - { - switch (Format) - { - case GalImageFormat.R32G32B32A32_SFLOAT: - case GalImageFormat.R32G32B32A32_SINT: - case GalImageFormat.R32G32B32A32_UINT: - case GalImageFormat.R16G16B16A16_SFLOAT: - case GalImageFormat.R16G16B16A16_SINT: - case GalImageFormat.R16G16B16A16_UINT: - case GalImageFormat.R32G32_SFLOAT: - case GalImageFormat.R32G32_SINT: - case GalImageFormat.R32G32_UINT: - case GalImageFormat.A8B8G8R8_SNORM_PACK32: - case GalImageFormat.A8B8G8R8_UNORM_PACK32: - case GalImageFormat.A8B8G8R8_SINT_PACK32: - case GalImageFormat.A8B8G8R8_UINT_PACK32: - case GalImageFormat.A2B10G10R10_SINT_PACK32: - case GalImageFormat.A2B10G10R10_SNORM_PACK32: - case GalImageFormat.A2B10G10R10_UINT_PACK32: - case GalImageFormat.A2B10G10R10_UNORM_PACK32: - case GalImageFormat.R32_SFLOAT: - case GalImageFormat.R32_SINT: - case GalImageFormat.R32_UINT: - case GalImageFormat.BC6H_SFLOAT_BLOCK: - case GalImageFormat.BC6H_UFLOAT_BLOCK: - case GalImageFormat.A1R5G5B5_UNORM_PACK16: - case GalImageFormat.B5G6R5_UNORM_PACK16: - case GalImageFormat.BC7_UNORM_BLOCK: - case GalImageFormat.R16G16_SFLOAT: - case GalImageFormat.R16G16_SINT: - case GalImageFormat.R16G16_SNORM: - case GalImageFormat.R16G16_UNORM: - case GalImageFormat.R8G8_SINT: - case GalImageFormat.R8G8_SNORM: - case GalImageFormat.R8G8_UINT: - case GalImageFormat.R8G8_UNORM: - case GalImageFormat.R16_SFLOAT: - case GalImageFormat.R16_SINT: - case GalImageFormat.R16_SNORM: - case GalImageFormat.R16_UINT: - case GalImageFormat.R16_UNORM: - case GalImageFormat.R8_SINT: - case GalImageFormat.R8_SNORM: - case GalImageFormat.R8_UINT: - case GalImageFormat.R8_UNORM: - case GalImageFormat.B10G11R11_UFLOAT_PACK32: - case GalImageFormat.BC1_RGBA_UNORM_BLOCK: - case GalImageFormat.BC2_UNORM_BLOCK: - case GalImageFormat.BC3_UNORM_BLOCK: - case GalImageFormat.BC4_UNORM_BLOCK: - case GalImageFormat.BC5_UNORM_BLOCK: - case GalImageFormat.ASTC_4x4_UNORM_BLOCK: - case GalImageFormat.ASTC_5x5_UNORM_BLOCK: - case GalImageFormat.ASTC_6x6_UNORM_BLOCK: - case GalImageFormat.ASTC_8x8_UNORM_BLOCK: - case GalImageFormat.ASTC_10x10_UNORM_BLOCK: - case GalImageFormat.ASTC_12x12_UNORM_BLOCK: - case GalImageFormat.ASTC_5x4_UNORM_BLOCK: - case GalImageFormat.ASTC_6x5_UNORM_BLOCK: - case GalImageFormat.ASTC_8x6_UNORM_BLOCK: - case GalImageFormat.ASTC_10x8_UNORM_BLOCK: - case GalImageFormat.ASTC_12x10_UNORM_BLOCK: - case GalImageFormat.ASTC_8x5_UNORM_BLOCK: - case GalImageFormat.ASTC_10x5_UNORM_BLOCK: - case GalImageFormat.ASTC_10x6_UNORM_BLOCK: - case GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED: - return true; - - case GalImageFormat.D24_UNORM_S8_UINT: - case GalImageFormat.D32_SFLOAT: - case GalImageFormat.D16_UNORM: - case GalImageFormat.D32_SFLOAT_S8_UINT: - return false; - } - - throw new NotImplementedException(Format.ToString()); - } - - public static bool HasDepth(GalImageFormat Format) - { - switch (Format) - { - case GalImageFormat.D24_UNORM_S8_UINT: - case GalImageFormat.D32_SFLOAT: - case GalImageFormat.D16_UNORM: - case GalImageFormat.D32_SFLOAT_S8_UINT: - return true; - } - - //Depth formats are fewer than colors, so it's harder to miss one - //Instead of checking for individual formats, return false - return false; - } - - public static bool HasStencil(GalImageFormat Format) - { - switch (Format) - { - case GalImageFormat.D24_UNORM_S8_UINT: - case GalImageFormat.D32_SFLOAT_S8_UINT: - return true; - } - - return false; - } - } -} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/OpenGL/ImageHandler.cs b/Ryujinx.Graphics/Gal/OpenGL/ImageHandler.cs index 74f18dcd..dda82538 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/ImageHandler.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/ImageHandler.cs @@ -1,13 +1,11 @@ using OpenTK.Graphics.OpenGL; +using Ryujinx.Graphics.Texture; using System; namespace Ryujinx.Graphics.Gal.OpenGL { class ImageHandler { - //TODO: Use a variable value here - public const int MaxBpp = 16; - private static int CopyBuffer = 0; private static int CopyBufferSize = 0; @@ -38,87 +36,124 @@ namespace Ryujinx.Graphics.Gal.OpenGL this.Image = Image; } - public void EnsureSetup(GalImage Image) + public void EnsureSetup(GalImage NewImage) { - if (Width != Image.Width || - Height != Image.Height || - Format != Image.Format || - !Initialized) + if (Width == NewImage.Width && + Height == NewImage.Height && + Format == NewImage.Format && + Initialized) { - (PixelInternalFormat InternalFormat, PixelFormat PixelFormat, PixelType PixelType) = - OGLEnumConverter.GetImageFormat(Image.Format); + return; + } - GL.BindTexture(TextureTarget.Texture2D, Handle); + PixelInternalFormat InternalFmt; + PixelFormat PixelFormat; + PixelType PixelType; - if (Initialized) - { - if (CopyBuffer == 0) - { - CopyBuffer = GL.GenBuffer(); - } + if (ImageUtils.IsCompressed(NewImage.Format)) + { + InternalFmt = (PixelInternalFormat)OGLEnumConverter.GetCompressedImageFormat(NewImage.Format); + + PixelFormat = default(PixelFormat); + PixelType = default(PixelType); + } + else + { + (InternalFmt, PixelFormat, PixelType) = OGLEnumConverter.GetImageFormat(NewImage.Format); + } - int MaxWidth = Math.Max(Image.Width, Width); - int MaxHeight = Math.Max(Image.Height, Height); + GL.BindTexture(TextureTarget.Texture2D, Handle); - int CurrentSize = MaxWidth * MaxHeight * MaxBpp; + if (Initialized) + { + if (CopyBuffer == 0) + { + CopyBuffer = GL.GenBuffer(); + } - GL.BindBuffer(BufferTarget.PixelPackBuffer, CopyBuffer); - GL.BindBuffer(BufferTarget.PixelUnpackBuffer, CopyBuffer); + int CurrentSize = Math.Max(ImageUtils.GetSize(NewImage), + ImageUtils.GetSize(Image)); - if (CopyBufferSize < CurrentSize) - { - CopyBufferSize = CurrentSize; + GL.BindBuffer(BufferTarget.PixelPackBuffer, CopyBuffer); + GL.BindBuffer(BufferTarget.PixelUnpackBuffer, CopyBuffer); - GL.BufferData(BufferTarget.PixelPackBuffer, CurrentSize, IntPtr.Zero, BufferUsageHint.StreamCopy); - } + if (CopyBufferSize < CurrentSize) + { + CopyBufferSize = CurrentSize; + GL.BufferData(BufferTarget.PixelPackBuffer, CurrentSize, IntPtr.Zero, BufferUsageHint.StreamCopy); + } + + if (ImageUtils.IsCompressed(Image.Format)) + { + GL.GetCompressedTexImage(TextureTarget.Texture2D, 0, IntPtr.Zero); + } + else + { GL.GetTexImage(TextureTarget.Texture2D, 0, this.PixelFormat, this.PixelType, IntPtr.Zero); + } - GL.DeleteTexture(Handle); + GL.DeleteTexture(Handle); - Handle = GL.GenTexture(); + Handle = GL.GenTexture(); - GL.BindTexture(TextureTarget.Texture2D, Handle); - } + GL.BindTexture(TextureTarget.Texture2D, Handle); + } - const int MinFilter = (int)TextureMinFilter.Linear; - const int MagFilter = (int)TextureMagFilter.Linear; + const int MinFilter = (int)TextureMinFilter.Linear; + const int MagFilter = (int)TextureMagFilter.Linear; - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, MinFilter); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, MagFilter); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, MinFilter); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, MagFilter); - const int Level = 0; - const int Border = 0; + const int Level = 0; + const int Border = 0; + if (ImageUtils.IsCompressed(NewImage.Format)) + { + Console.WriteLine("Hit"); + + GL.CompressedTexImage2D( + TextureTarget.Texture2D, + Level, + (InternalFormat)InternalFmt, + NewImage.Width, + NewImage.Height, + Border, + ImageUtils.GetSize(NewImage), + IntPtr.Zero); + } + else + { GL.TexImage2D( TextureTarget.Texture2D, Level, - InternalFormat, - Image.Width, - Image.Height, + InternalFmt, + NewImage.Width, + NewImage.Height, Border, PixelFormat, PixelType, IntPtr.Zero); + } - if (Initialized) - { - GL.BindBuffer(BufferTarget.PixelPackBuffer, 0); - GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0); - } + if (Initialized) + { + GL.BindBuffer(BufferTarget.PixelPackBuffer, 0); + GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0); + } - this.Image = Image; + Image = NewImage; - this.InternalFormat = InternalFormat; - this.PixelFormat = PixelFormat; - this.PixelType = PixelType; + this.InternalFormat = InternalFmt; + this.PixelFormat = PixelFormat; + this.PixelType = PixelType; - Initialized = true; - } + Initialized = true; } - public bool HasColor { get => ImageFormatConverter.HasColor(Format); } - public bool HasDepth { get => ImageFormatConverter.HasDepth(Format); } - public bool HasStencil { get => ImageFormatConverter.HasStencil(Format); } + public bool HasColor => ImageUtils.HasColor(Image.Format); + public bool HasDepth => ImageUtils.HasDepth(Image.Format); + public bool HasStencil => ImageUtils.HasStencil(Image.Format); } } diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs index 959d0e32..1d4f4cf7 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLEnumConverter.cs @@ -129,52 +129,51 @@ namespace Ryujinx.Graphics.Gal.OpenGL { switch (Format) { - case GalImageFormat.R32G32B32A32_SFLOAT: return (PixelInternalFormat.Rgba32f, PixelFormat.Rgba, PixelType.Float); - case GalImageFormat.R32G32B32A32_SINT: return (PixelInternalFormat.Rgba32i, PixelFormat.RgbaInteger, PixelType.Int); - case GalImageFormat.R32G32B32A32_UINT: return (PixelInternalFormat.Rgba32ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt); - case GalImageFormat.R16G16B16A16_SFLOAT: return (PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.HalfFloat); - case GalImageFormat.R16G16B16A16_SINT: return (PixelInternalFormat.Rgba16i, PixelFormat.RgbaInteger, PixelType.Short); - case GalImageFormat.R16G16B16A16_UINT: return (PixelInternalFormat.Rgba16ui, PixelFormat.RgbaInteger, PixelType.UnsignedShort); - case GalImageFormat.R32G32_SFLOAT: return (PixelInternalFormat.Rg32f, PixelFormat.Rg, PixelType.Float); - case GalImageFormat.R32G32_SINT: return (PixelInternalFormat.Rg32i, PixelFormat.RgInteger, PixelType.Int); - case GalImageFormat.R32G32_UINT: return (PixelInternalFormat.Rg32ui, PixelFormat.RgInteger, PixelType.UnsignedInt); - case GalImageFormat.A8B8G8R8_SNORM_PACK32: return (PixelInternalFormat.Rgba8Snorm, PixelFormat.Rgba, PixelType.Byte); - case GalImageFormat.A8B8G8R8_UNORM_PACK32: return (PixelInternalFormat.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte); - case GalImageFormat.A8B8G8R8_SINT_PACK32: return (PixelInternalFormat.Rgba8i, PixelFormat.RgbaInteger, PixelType.Byte); - case GalImageFormat.A8B8G8R8_UINT_PACK32: return (PixelInternalFormat.Rgba8ui, PixelFormat.RgbaInteger, PixelType.UnsignedByte); - case GalImageFormat.A8B8G8R8_SRGB_PACK32: return (PixelInternalFormat.Srgb8Alpha8, PixelFormat.Rgba, PixelType.UnsignedByte); - case GalImageFormat.A2B10G10R10_UINT_PACK32: return (PixelInternalFormat.Rgb10A2ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt2101010Reversed); - case GalImageFormat.A2B10G10R10_UNORM_PACK32: return (PixelInternalFormat.Rgb10A2, PixelFormat.Rgba, PixelType.UnsignedInt2101010Reversed); - case GalImageFormat.R32_SFLOAT: return (PixelInternalFormat.R32f, PixelFormat.Red, PixelType.Float); - case GalImageFormat.R32_SINT: return (PixelInternalFormat.R32i, PixelFormat.Red, PixelType.Int); - case GalImageFormat.R32_UINT: return (PixelInternalFormat.R32ui, PixelFormat.Red, PixelType.UnsignedInt); - case GalImageFormat.A1R5G5B5_UNORM_PACK16: return (PixelInternalFormat.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort5551); - case GalImageFormat.B5G6R5_UNORM_PACK16: return (PixelInternalFormat.Rgba, PixelFormat.Rgb, PixelType.UnsignedShort565); - case GalImageFormat.R16G16_SFLOAT: return (PixelInternalFormat.Rg16f, PixelFormat.Rg, PixelType.HalfFloat); - case GalImageFormat.R16G16_SINT: return (PixelInternalFormat.Rg16i, PixelFormat.RgInteger, PixelType.Short); - case GalImageFormat.R16G16_SNORM: return (PixelInternalFormat.Rg16Snorm, PixelFormat.Rg, PixelType.Byte); - case GalImageFormat.R16G16_UNORM: return (PixelInternalFormat.Rg16, PixelFormat.Rg, PixelType.UnsignedShort); - case GalImageFormat.R8G8_SINT: return (PixelInternalFormat.Rg8i, PixelFormat.RgInteger, PixelType.Byte); - case GalImageFormat.R8G8_SNORM: return (PixelInternalFormat.Rg8Snorm, PixelFormat.Rg, PixelType.Byte); - case GalImageFormat.R8G8_UINT: return (PixelInternalFormat.Rg8ui, PixelFormat.RgInteger, PixelType.UnsignedByte); - case GalImageFormat.R8G8_UNORM: return (PixelInternalFormat.Rg8, PixelFormat.Rg, PixelType.UnsignedByte); - case GalImageFormat.R16_SFLOAT: return (PixelInternalFormat.R16f, PixelFormat.Red, PixelType.HalfFloat); - case GalImageFormat.R16_SINT: return (PixelInternalFormat.R16i, PixelFormat.RedInteger, PixelType.Short); - case GalImageFormat.R16_SNORM: return (PixelInternalFormat.R16Snorm, PixelFormat.Red, PixelType.Byte); - case GalImageFormat.R16_UINT: return (PixelInternalFormat.R16ui, PixelFormat.RedInteger, PixelType.UnsignedShort); - case GalImageFormat.R16_UNORM: return (PixelInternalFormat.R16, PixelFormat.Red, PixelType.UnsignedShort); - case GalImageFormat.R8_SINT: return (PixelInternalFormat.R8i, PixelFormat.RedInteger, PixelType.Byte); - case GalImageFormat.R8_SNORM: return (PixelInternalFormat.R8Snorm, PixelFormat.Red, PixelType.Byte); - case GalImageFormat.R8_UINT: return (PixelInternalFormat.R8ui, PixelFormat.RedInteger, PixelType.UnsignedByte); - case GalImageFormat.R8_UNORM: return (PixelInternalFormat.R8, PixelFormat.Red, PixelType.UnsignedByte); - case GalImageFormat.B10G11R11_UFLOAT_PACK32: return (PixelInternalFormat.R11fG11fB10f, PixelFormat.Rgb, PixelType.UnsignedInt10F11F11FRev); - - case GalImageFormat.R4G4B4A4_UNORM_PACK16_REVERSED: return (PixelInternalFormat.Rgba4, PixelFormat.Rgba, PixelType.UnsignedShort4444Reversed); - - case GalImageFormat.D24_UNORM_S8_UINT: return (PixelInternalFormat.Depth24Stencil8, PixelFormat.DepthStencil, PixelType.UnsignedInt248); - case GalImageFormat.D32_SFLOAT: return (PixelInternalFormat.DepthComponent32f, PixelFormat.DepthComponent, PixelType.Float); - case GalImageFormat.D16_UNORM: return (PixelInternalFormat.DepthComponent16, PixelFormat.DepthComponent, PixelType.UnsignedShort); - case GalImageFormat.D32_SFLOAT_S8_UINT: return (PixelInternalFormat.Depth32fStencil8, PixelFormat.DepthStencil, PixelType.Float32UnsignedInt248Rev); + case GalImageFormat.R32G32B32A32 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rgba32f, PixelFormat.Rgba, PixelType.Float); + case GalImageFormat.R32G32B32A32 | GalImageFormat.Sint: return (PixelInternalFormat.Rgba32i, PixelFormat.RgbaInteger, PixelType.Int); + case GalImageFormat.R32G32B32A32 | GalImageFormat.Uint: return (PixelInternalFormat.Rgba32ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt); + case GalImageFormat.R16G16B16A16 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.HalfFloat); + case GalImageFormat.R16G16B16A16 | GalImageFormat.Sint: return (PixelInternalFormat.Rgba16i, PixelFormat.RgbaInteger, PixelType.Short); + case GalImageFormat.R16G16B16A16 | GalImageFormat.Uint: return (PixelInternalFormat.Rgba16ui, PixelFormat.RgbaInteger, PixelType.UnsignedShort); + case GalImageFormat.R32G32 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rg32f, PixelFormat.Rg, PixelType.Float); + case GalImageFormat.R32G32 | GalImageFormat.Sint: return (PixelInternalFormat.Rg32i, PixelFormat.RgInteger, PixelType.Int); + case GalImageFormat.R32G32 | GalImageFormat.Uint: return (PixelInternalFormat.Rg32ui, PixelFormat.RgInteger, PixelType.UnsignedInt); + case GalImageFormat.A8B8G8R8 | GalImageFormat.Snorm: return (PixelInternalFormat.Rgba8Snorm, PixelFormat.Rgba, PixelType.Byte); + case GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte); + case GalImageFormat.A8B8G8R8 | GalImageFormat.Sint: return (PixelInternalFormat.Rgba8i, PixelFormat.RgbaInteger, PixelType.Byte); + case GalImageFormat.A8B8G8R8 | GalImageFormat.Uint: return (PixelInternalFormat.Rgba8ui, PixelFormat.RgbaInteger, PixelType.UnsignedByte); + case GalImageFormat.A8B8G8R8_SRGB: return (PixelInternalFormat.Srgb8Alpha8, PixelFormat.Rgba, PixelType.UnsignedByte); + case GalImageFormat.A4B4G4R4 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgba4, PixelFormat.Rgba, PixelType.UnsignedShort4444Reversed); + case GalImageFormat.A2B10G10R10 | GalImageFormat.Uint: return (PixelInternalFormat.Rgb10A2ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt2101010Reversed); + case GalImageFormat.A2B10G10R10 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgb10A2, PixelFormat.Rgba, PixelType.UnsignedInt2101010Reversed); + case GalImageFormat.R32 | GalImageFormat.Sfloat: return (PixelInternalFormat.R32f, PixelFormat.Red, PixelType.Float); + case GalImageFormat.R32 | GalImageFormat.Sint: return (PixelInternalFormat.R32i, PixelFormat.Red, PixelType.Int); + case GalImageFormat.R32 | GalImageFormat.Uint: return (PixelInternalFormat.R32ui, PixelFormat.Red, PixelType.UnsignedInt); + case GalImageFormat.A1R5G5B5 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort5551); + case GalImageFormat.B5G6R5 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgba, PixelFormat.Rgb, PixelType.UnsignedShort565); + case GalImageFormat.R16G16 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rg16f, PixelFormat.Rg, PixelType.HalfFloat); + case GalImageFormat.R16G16 | GalImageFormat.Sint: return (PixelInternalFormat.Rg16i, PixelFormat.RgInteger, PixelType.Short); + case GalImageFormat.R16G16 | GalImageFormat.Snorm: return (PixelInternalFormat.Rg16Snorm, PixelFormat.Rg, PixelType.Byte); + case GalImageFormat.R16G16 | GalImageFormat.Unorm: return (PixelInternalFormat.Rg16, PixelFormat.Rg, PixelType.UnsignedShort); + case GalImageFormat.R8G8 | GalImageFormat.Sint: return (PixelInternalFormat.Rg8i, PixelFormat.RgInteger, PixelType.Byte); + case GalImageFormat.R8G8 | GalImageFormat.Snorm: return (PixelInternalFormat.Rg8Snorm, PixelFormat.Rg, PixelType.Byte); + case GalImageFormat.R8G8 | GalImageFormat.Uint: return (PixelInternalFormat.Rg8ui, PixelFormat.RgInteger, PixelType.UnsignedByte); + case GalImageFormat.R8G8 | GalImageFormat.Unorm: return (PixelInternalFormat.Rg8, PixelFormat.Rg, PixelType.UnsignedByte); + case GalImageFormat.R16 | GalImageFormat.Sfloat: return (PixelInternalFormat.R16f, PixelFormat.Red, PixelType.HalfFloat); + case GalImageFormat.R16 | GalImageFormat.Sint: return (PixelInternalFormat.R16i, PixelFormat.RedInteger, PixelType.Short); + case GalImageFormat.R16 | GalImageFormat.Snorm: return (PixelInternalFormat.R16Snorm, PixelFormat.Red, PixelType.Byte); + case GalImageFormat.R16 | GalImageFormat.Uint: return (PixelInternalFormat.R16ui, PixelFormat.RedInteger, PixelType.UnsignedShort); + case GalImageFormat.R16 | GalImageFormat.Unorm: return (PixelInternalFormat.R16, PixelFormat.Red, PixelType.UnsignedShort); + case GalImageFormat.R8 | GalImageFormat.Sint: return (PixelInternalFormat.R8i, PixelFormat.RedInteger, PixelType.Byte); + case GalImageFormat.R8 | GalImageFormat.Snorm: return (PixelInternalFormat.R8Snorm, PixelFormat.Red, PixelType.Byte); + case GalImageFormat.R8 | GalImageFormat.Uint: return (PixelInternalFormat.R8ui, PixelFormat.RedInteger, PixelType.UnsignedByte); + case GalImageFormat.R8 | GalImageFormat.Unorm: return (PixelInternalFormat.R8, PixelFormat.Red, PixelType.UnsignedByte); + case GalImageFormat.B10G11R11 | GalImageFormat.Sfloat: return (PixelInternalFormat.R11fG11fB10f, PixelFormat.Rgb, PixelType.UnsignedInt10F11F11FRev); + + case GalImageFormat.D24_S8 | GalImageFormat.Unorm: return (PixelInternalFormat.Depth24Stencil8, PixelFormat.DepthStencil, PixelType.UnsignedInt248); + case GalImageFormat.D32 | GalImageFormat.Sfloat: return (PixelInternalFormat.DepthComponent32f, PixelFormat.DepthComponent, PixelType.Float); + case GalImageFormat.D16 | GalImageFormat.Unorm: return (PixelInternalFormat.DepthComponent16, PixelFormat.DepthComponent, PixelType.UnsignedShort); + case GalImageFormat.D32_S8 | GalImageFormat.Uint: return (PixelInternalFormat.Depth32fStencil8, PixelFormat.DepthStencil, PixelType.Float32UnsignedInt248Rev); } throw new NotImplementedException(Format.ToString()); @@ -184,16 +183,16 @@ namespace Ryujinx.Graphics.Gal.OpenGL { switch (Format) { - case GalImageFormat.BC6H_UFLOAT_BLOCK: return InternalFormat.CompressedRgbBptcUnsignedFloat; - case GalImageFormat.BC6H_SFLOAT_BLOCK: return InternalFormat.CompressedRgbBptcSignedFloat; - case GalImageFormat.BC7_UNORM_BLOCK: return InternalFormat.CompressedRgbaBptcUnorm; - case GalImageFormat.BC1_RGBA_UNORM_BLOCK: return InternalFormat.CompressedRgbaS3tcDxt1Ext; - case GalImageFormat.BC2_UNORM_BLOCK: return InternalFormat.CompressedRgbaS3tcDxt3Ext; - case GalImageFormat.BC3_UNORM_BLOCK: return InternalFormat.CompressedRgbaS3tcDxt5Ext; - case GalImageFormat.BC4_SNORM_BLOCK: return InternalFormat.CompressedSignedRedRgtc1; - case GalImageFormat.BC4_UNORM_BLOCK: return InternalFormat.CompressedRedRgtc1; - case GalImageFormat.BC5_SNORM_BLOCK: return InternalFormat.CompressedSignedRgRgtc2; - case GalImageFormat.BC5_UNORM_BLOCK: return InternalFormat.CompressedRgRgtc2; + case GalImageFormat.BC6H_UF16 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbBptcUnsignedFloat; + case GalImageFormat.BC6H_SF16 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbBptcSignedFloat; + case GalImageFormat.BC7 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaBptcUnorm; + case GalImageFormat.BC1_RGBA | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt1Ext; + case GalImageFormat.BC2 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt3Ext; + case GalImageFormat.BC3 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt5Ext; + case GalImageFormat.BC4 | GalImageFormat.Snorm: return InternalFormat.CompressedSignedRedRgtc1; + case GalImageFormat.BC4 | GalImageFormat.Unorm: return InternalFormat.CompressedRedRgtc1; + case GalImageFormat.BC5 | GalImageFormat.Snorm: return InternalFormat.CompressedSignedRgRgtc2; + case GalImageFormat.BC5 | GalImageFormat.Unorm: return InternalFormat.CompressedRgRgtc2; } throw new NotImplementedException(Format.ToString()); diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs deleted file mode 100644 index 12239c4f..00000000 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLFrameBuffer.cs +++ /dev/null @@ -1,496 +0,0 @@ -using OpenTK.Graphics.OpenGL; -using System; - -namespace Ryujinx.Graphics.Gal.OpenGL -{ - class OGLFrameBuffer : IGalFrameBuffer - { - private struct Rect - { - public int X { get; private set; } - public int Y { get; private set; } - public int Width { get; private set; } - public int Height { get; private set; } - - public Rect(int X, int Y, int Width, int Height) - { - this.X = X; - this.Y = Y; - this.Width = Width; - this.Height = Height; - } - } - - private const int NativeWidth = 1280; - private const int NativeHeight = 720; - - private const GalImageFormat RawFormat = GalImageFormat.A8B8G8R8_UNORM_PACK32; - - private OGLTexture Texture; - - private ImageHandler RawTex; - private ImageHandler ReadTex; - - private Rect Viewport; - private Rect Window; - - private bool FlipX; - private bool FlipY; - - private int CropTop; - private int CropLeft; - private int CropRight; - private int CropBottom; - - //This framebuffer is used to attach guest rendertargets, - //think of it as a dummy OpenGL VAO - private int DummyFrameBuffer; - - //These framebuffers are used to blit images - private int SrcFb; - private int DstFb; - - //Holds current attachments, used to avoid unnecesary calls to OpenGL - private int[] ColorAttachments; - - private int DepthAttachment; - private int StencilAttachment; - - public OGLFrameBuffer(OGLTexture Texture) - { - ColorAttachments = new int[8]; - - this.Texture = Texture; - } - - public void BindColor(long Key, int Attachment) - { - if (Texture.TryGetImage(Key, out ImageHandler Tex)) - { - EnsureFrameBuffer(); - - Attach(ref ColorAttachments[Attachment], Tex.Handle, FramebufferAttachment.ColorAttachment0 + Attachment); - } - else - { - UnbindColor(Attachment); - } - } - - public void UnbindColor(int Attachment) - { - EnsureFrameBuffer(); - - Attach(ref ColorAttachments[Attachment], 0, FramebufferAttachment.ColorAttachment0 + Attachment); - } - - public void BindZeta(long Key) - { - if (Texture.TryGetImage(Key, out ImageHandler Tex)) - { - EnsureFrameBuffer(); - - if (Tex.HasDepth && Tex.HasStencil) - { - if (DepthAttachment != Tex.Handle || - StencilAttachment != Tex.Handle) - { - GL.FramebufferTexture( - FramebufferTarget.DrawFramebuffer, - FramebufferAttachment.DepthStencilAttachment, - Tex.Handle, - 0); - - DepthAttachment = Tex.Handle; - - StencilAttachment = Tex.Handle; - } - } - else if (Tex.HasDepth) - { - Attach(ref DepthAttachment, Tex.Handle, FramebufferAttachment.DepthAttachment); - - Attach(ref StencilAttachment, 0, FramebufferAttachment.StencilAttachment); - } - else if (Tex.HasStencil) - { - Attach(ref DepthAttachment, 0, FramebufferAttachment.DepthAttachment); - - Attach(ref StencilAttachment, Tex.Handle, FramebufferAttachment.StencilAttachment); - } - else - { - throw new InvalidOperationException(); - } - } - else - { - UnbindZeta(); - } - } - - public void UnbindZeta() - { - EnsureFrameBuffer(); - - if (DepthAttachment != 0 || - StencilAttachment != 0) - { - GL.FramebufferTexture( - FramebufferTarget.DrawFramebuffer, - FramebufferAttachment.DepthStencilAttachment, - 0, - 0); - - DepthAttachment = 0; - - StencilAttachment = 0; - } - } - - public void BindTexture(long Key, int Index) - { - if (Texture.TryGetImage(Key, out ImageHandler Tex)) - { - GL.ActiveTexture(TextureUnit.Texture0 + Index); - - GL.BindTexture(TextureTarget.Texture2D, Tex.Handle); - } - } - - public void Set(long Key) - { - if (Texture.TryGetImage(Key, out ImageHandler Tex)) - { - ReadTex = Tex; - } - } - - public void Set(byte[] Data, int Width, int Height) - { - if (RawTex == null) - { - RawTex = new ImageHandler(); - } - - RawTex.EnsureSetup(new GalImage(Width, Height, RawFormat)); - - GL.BindTexture(TextureTarget.Texture2D, RawTex.Handle); - - GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, Width, Height, RawTex.PixelFormat, RawTex.PixelType, Data); - - ReadTex = RawTex; - } - - public void SetMap(int[] Map) - { - if (Map != null && Map.Length > 0) - { - DrawBuffersEnum[] Mode = new DrawBuffersEnum[Map.Length]; - - for (int i = 0; i < Map.Length; i++) - { - Mode[i] = DrawBuffersEnum.ColorAttachment0 + Map[i]; - } - - GL.DrawBuffers(Mode.Length, Mode); - } - else - { - GL.DrawBuffer(DrawBufferMode.ColorAttachment0); - } - } - - public void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom) - { - this.FlipX = FlipX; - this.FlipY = FlipY; - - CropTop = Top; - CropLeft = Left; - CropRight = Right; - CropBottom = Bottom; - } - - public void SetWindowSize(int Width, int Height) - { - Window = new Rect(0, 0, Width, Height); - } - - public void SetViewport(int X, int Y, int Width, int Height) - { - Viewport = new Rect(X, Y, Width, Height); - - SetViewport(Viewport); - } - - private void SetViewport(Rect Viewport) - { - GL.Viewport( - Viewport.X, - Viewport.Y, - Viewport.Width, - Viewport.Height); - } - - public void Render() - { - if (ReadTex == null) - { - return; - } - - int SrcX0, SrcX1, SrcY0, SrcY1; - - if (CropLeft == 0 && CropRight == 0) - { - SrcX0 = 0; - SrcX1 = ReadTex.Width; - } - else - { - SrcX0 = CropLeft; - SrcX1 = CropRight; - } - - if (CropTop == 0 && CropBottom == 0) - { - SrcY0 = 0; - SrcY1 = ReadTex.Height; - } - else - { - SrcY0 = CropTop; - SrcY1 = CropBottom; - } - - float RatioX = MathF.Min(1f, (Window.Height * (float)NativeWidth) / ((float)NativeHeight * Window.Width)); - float RatioY = MathF.Min(1f, (Window.Width * (float)NativeHeight) / ((float)NativeWidth * Window.Height)); - - int DstWidth = (int)(Window.Width * RatioX); - int DstHeight = (int)(Window.Height * RatioY); - - int DstPaddingX = (Window.Width - DstWidth) / 2; - int DstPaddingY = (Window.Height - DstHeight) / 2; - - int DstX0 = FlipX ? Window.Width - DstPaddingX : DstPaddingX; - int DstX1 = FlipX ? DstPaddingX : Window.Width - DstPaddingX; - - int DstY0 = FlipY ? DstPaddingY : Window.Height - DstPaddingY; - int DstY1 = FlipY ? Window.Height - DstPaddingY : DstPaddingY; - - if (SrcFb == 0) SrcFb = GL.GenFramebuffer(); - - GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); - - GL.Viewport(0, 0, Window.Width, Window.Height); - - GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb); - - GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, ReadTex.Handle, 0); - - GL.ReadBuffer(ReadBufferMode.ColorAttachment0); - GL.DrawBuffer(DrawBufferMode.ColorAttachment0); - - GL.Clear(ClearBufferMask.ColorBufferBit); - - GL.BlitFramebuffer( - SrcX0, SrcY0, SrcX1, SrcY1, - DstX0, DstY0, DstX1, DstY1, - ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear); - - EnsureFrameBuffer(); - } - - public void Copy( - long SrcKey, - long DstKey, - int SrcX0, - int SrcY0, - int SrcX1, - int SrcY1, - int DstX0, - int DstY0, - int DstX1, - int DstY1) - { - if (Texture.TryGetImage(SrcKey, out ImageHandler SrcTex) && - Texture.TryGetImage(DstKey, out ImageHandler DstTex)) - { - if (SrcTex.HasColor != DstTex.HasColor || - SrcTex.HasDepth != DstTex.HasDepth || - SrcTex.HasStencil != DstTex.HasStencil) - { - throw new NotImplementedException(); - } - - if (SrcTex.HasColor) - { - CopyTextures( - SrcX0, SrcY0, SrcX1, SrcY1, - DstX0, DstY0, DstX1, DstY1, - SrcTex.Handle, - DstTex.Handle, - FramebufferAttachment.ColorAttachment0, - ClearBufferMask.ColorBufferBit, - true); - } - else if (SrcTex.HasDepth && SrcTex.HasStencil) - { - CopyTextures( - SrcX0, SrcY0, SrcX1, SrcY1, - DstX0, DstY0, DstX1, DstY1, - SrcTex.Handle, - DstTex.Handle, - FramebufferAttachment.DepthStencilAttachment, - ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit, - false); - } - else if (SrcTex.HasDepth) - { - CopyTextures( - SrcX0, SrcY0, SrcX1, SrcY1, - DstX0, DstY0, DstX1, DstY1, - SrcTex.Handle, - DstTex.Handle, - FramebufferAttachment.DepthAttachment, - ClearBufferMask.DepthBufferBit, - false); - } - else if (SrcTex.HasStencil) - { - CopyTextures( - SrcX0, SrcY0, SrcX1, SrcY1, - DstX0, DstY0, DstX1, DstY1, - SrcTex.Handle, - DstTex.Handle, - FramebufferAttachment.StencilAttachment, - ClearBufferMask.StencilBufferBit, - false); - } - else - { - throw new InvalidOperationException(); - } - } - } - - public void GetBufferData(long Key, Action Callback) - { - if (Texture.TryGetImage(Key, out ImageHandler Tex)) - { - byte[] Data = new byte[Tex.Width * Tex.Height * ImageHandler.MaxBpp]; - - GL.BindTexture(TextureTarget.Texture2D, Tex.Handle); - - GL.GetTexImage( - TextureTarget.Texture2D, - 0, - Tex.PixelFormat, - Tex.PixelType, - Data); - - Callback(Data); - } - } - - public void SetBufferData( - long Key, - int Width, - int Height, - byte[] Buffer) - { - if (Texture.TryGetImage(Key, out ImageHandler Tex)) - { - GL.BindTexture(TextureTarget.Texture2D, Tex.Handle); - - const int Level = 0; - const int Border = 0; - - GL.TexImage2D( - TextureTarget.Texture2D, - Level, - Tex.InternalFormat, - Width, - Height, - Border, - Tex.PixelFormat, - Tex.PixelType, - Buffer); - } - } - - private void EnsureFrameBuffer() - { - if (DummyFrameBuffer == 0) - { - DummyFrameBuffer = GL.GenFramebuffer(); - } - - GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DummyFrameBuffer); - } - - private void Attach(ref int OldHandle, int NewHandle, FramebufferAttachment FbAttachment) - { - if (OldHandle != NewHandle) - { - GL.FramebufferTexture( - FramebufferTarget.DrawFramebuffer, - FbAttachment, - NewHandle, - 0); - - OldHandle = NewHandle; - } - } - - private void CopyTextures( - int SrcX0, - int SrcY0, - int SrcX1, - int SrcY1, - int DstX0, - int DstY0, - int DstX1, - int DstY1, - int SrcTexture, - int DstTexture, - FramebufferAttachment Attachment, - ClearBufferMask Mask, - bool Color) - { - if (SrcFb == 0) SrcFb = GL.GenFramebuffer(); - if (DstFb == 0) DstFb = GL.GenFramebuffer(); - - GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb); - GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DstFb); - - GL.FramebufferTexture( - FramebufferTarget.ReadFramebuffer, - Attachment, - SrcTexture, - 0); - - GL.FramebufferTexture( - FramebufferTarget.DrawFramebuffer, - Attachment, - DstTexture, - 0); - - if (Color) - { - GL.DrawBuffer(DrawBufferMode.ColorAttachment0); - } - - GL.Clear(Mask); - - GL.BlitFramebuffer( - SrcX0, SrcY0, SrcX1, SrcY1, - DstX0, DstY0, DstX1, DstY1, - Mask, - Color ? BlitFramebufferFilter.Linear : BlitFramebufferFilter.Nearest); - - EnsureFrameBuffer(); - } - } -} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLRenderTarget.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLRenderTarget.cs new file mode 100644 index 00000000..99bfa350 --- /dev/null +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLRenderTarget.cs @@ -0,0 +1,497 @@ +using OpenTK.Graphics.OpenGL; +using Ryujinx.Graphics.Texture; +using System; + +namespace Ryujinx.Graphics.Gal.OpenGL +{ + class OGLRenderTarget : IGalRenderTarget + { + private struct Rect + { + public int X { get; private set; } + public int Y { get; private set; } + public int Width { get; private set; } + public int Height { get; private set; } + + public Rect(int X, int Y, int Width, int Height) + { + this.X = X; + this.Y = Y; + this.Width = Width; + this.Height = Height; + } + } + + private const int NativeWidth = 1280; + private const int NativeHeight = 720; + + private const GalImageFormat RawFormat = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm; + + private OGLTexture Texture; + + private ImageHandler RawTex; + private ImageHandler ReadTex; + + private Rect Viewport; + private Rect Window; + + private bool FlipX; + private bool FlipY; + + private int CropTop; + private int CropLeft; + private int CropRight; + private int CropBottom; + + //This framebuffer is used to attach guest rendertargets, + //think of it as a dummy OpenGL VAO + private int DummyFrameBuffer; + + //These framebuffers are used to blit images + private int SrcFb; + private int DstFb; + + //Holds current attachments, used to avoid unnecesary calls to OpenGL + private int[] ColorAttachments; + + private int DepthAttachment; + private int StencilAttachment; + + public OGLRenderTarget(OGLTexture Texture) + { + ColorAttachments = new int[8]; + + this.Texture = Texture; + } + + public void BindColor(long Key, int Attachment) + { + if (Texture.TryGetImage(Key, out ImageHandler Tex)) + { + EnsureFrameBuffer(); + + Attach(ref ColorAttachments[Attachment], Tex.Handle, FramebufferAttachment.ColorAttachment0 + Attachment); + } + else + { + UnbindColor(Attachment); + } + } + + public void UnbindColor(int Attachment) + { + EnsureFrameBuffer(); + + Attach(ref ColorAttachments[Attachment], 0, FramebufferAttachment.ColorAttachment0 + Attachment); + } + + public void BindZeta(long Key) + { + if (Texture.TryGetImage(Key, out ImageHandler Tex)) + { + EnsureFrameBuffer(); + + if (Tex.HasDepth && Tex.HasStencil) + { + if (DepthAttachment != Tex.Handle || + StencilAttachment != Tex.Handle) + { + GL.FramebufferTexture( + FramebufferTarget.DrawFramebuffer, + FramebufferAttachment.DepthStencilAttachment, + Tex.Handle, + 0); + + DepthAttachment = Tex.Handle; + + StencilAttachment = Tex.Handle; + } + } + else if (Tex.HasDepth) + { + Attach(ref DepthAttachment, Tex.Handle, FramebufferAttachment.DepthAttachment); + + Attach(ref StencilAttachment, 0, FramebufferAttachment.StencilAttachment); + } + else if (Tex.HasStencil) + { + Attach(ref DepthAttachment, 0, FramebufferAttachment.DepthAttachment); + + Attach(ref StencilAttachment, Tex.Handle, FramebufferAttachment.StencilAttachment); + } + else + { + throw new InvalidOperationException(); + } + } + else + { + UnbindZeta(); + } + } + + public void UnbindZeta() + { + EnsureFrameBuffer(); + + if (DepthAttachment != 0 || + StencilAttachment != 0) + { + GL.FramebufferTexture( + FramebufferTarget.DrawFramebuffer, + FramebufferAttachment.DepthStencilAttachment, + 0, + 0); + + DepthAttachment = 0; + + StencilAttachment = 0; + } + } + + public void BindTexture(long Key, int Index) + { + if (Texture.TryGetImage(Key, out ImageHandler Tex)) + { + GL.ActiveTexture(TextureUnit.Texture0 + Index); + + GL.BindTexture(TextureTarget.Texture2D, Tex.Handle); + } + } + + public void Set(long Key) + { + if (Texture.TryGetImage(Key, out ImageHandler Tex)) + { + ReadTex = Tex; + } + } + + public void Set(byte[] Data, int Width, int Height) + { + if (RawTex == null) + { + RawTex = new ImageHandler(); + } + + RawTex.EnsureSetup(new GalImage(Width, Height, RawFormat)); + + GL.BindTexture(TextureTarget.Texture2D, RawTex.Handle); + + GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, Width, Height, RawTex.PixelFormat, RawTex.PixelType, Data); + + ReadTex = RawTex; + } + + public void SetMap(int[] Map) + { + if (Map != null && Map.Length > 0) + { + DrawBuffersEnum[] Mode = new DrawBuffersEnum[Map.Length]; + + for (int i = 0; i < Map.Length; i++) + { + Mode[i] = DrawBuffersEnum.ColorAttachment0 + Map[i]; + } + + GL.DrawBuffers(Mode.Length, Mode); + } + else + { + GL.DrawBuffer(DrawBufferMode.ColorAttachment0); + } + } + + public void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom) + { + this.FlipX = FlipX; + this.FlipY = FlipY; + + CropTop = Top; + CropLeft = Left; + CropRight = Right; + CropBottom = Bottom; + } + + public void SetWindowSize(int Width, int Height) + { + Window = new Rect(0, 0, Width, Height); + } + + public void SetViewport(int X, int Y, int Width, int Height) + { + Viewport = new Rect(X, Y, Width, Height); + + SetViewport(Viewport); + } + + private void SetViewport(Rect Viewport) + { + GL.Viewport( + Viewport.X, + Viewport.Y, + Viewport.Width, + Viewport.Height); + } + + public void Render() + { + if (ReadTex == null) + { + return; + } + + int SrcX0, SrcX1, SrcY0, SrcY1; + + if (CropLeft == 0 && CropRight == 0) + { + SrcX0 = 0; + SrcX1 = ReadTex.Width; + } + else + { + SrcX0 = CropLeft; + SrcX1 = CropRight; + } + + if (CropTop == 0 && CropBottom == 0) + { + SrcY0 = 0; + SrcY1 = ReadTex.Height; + } + else + { + SrcY0 = CropTop; + SrcY1 = CropBottom; + } + + float RatioX = MathF.Min(1f, (Window.Height * (float)NativeWidth) / ((float)NativeHeight * Window.Width)); + float RatioY = MathF.Min(1f, (Window.Width * (float)NativeHeight) / ((float)NativeWidth * Window.Height)); + + int DstWidth = (int)(Window.Width * RatioX); + int DstHeight = (int)(Window.Height * RatioY); + + int DstPaddingX = (Window.Width - DstWidth) / 2; + int DstPaddingY = (Window.Height - DstHeight) / 2; + + int DstX0 = FlipX ? Window.Width - DstPaddingX : DstPaddingX; + int DstX1 = FlipX ? DstPaddingX : Window.Width - DstPaddingX; + + int DstY0 = FlipY ? DstPaddingY : Window.Height - DstPaddingY; + int DstY1 = FlipY ? Window.Height - DstPaddingY : DstPaddingY; + + if (SrcFb == 0) SrcFb = GL.GenFramebuffer(); + + GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); + + GL.Viewport(0, 0, Window.Width, Window.Height); + + GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb); + + GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, ReadTex.Handle, 0); + + GL.ReadBuffer(ReadBufferMode.ColorAttachment0); + GL.DrawBuffer(DrawBufferMode.ColorAttachment0); + + GL.Clear(ClearBufferMask.ColorBufferBit); + + GL.BlitFramebuffer( + SrcX0, SrcY0, SrcX1, SrcY1, + DstX0, DstY0, DstX1, DstY1, + ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear); + + EnsureFrameBuffer(); + } + + public void Copy( + long SrcKey, + long DstKey, + int SrcX0, + int SrcY0, + int SrcX1, + int SrcY1, + int DstX0, + int DstY0, + int DstX1, + int DstY1) + { + if (Texture.TryGetImage(SrcKey, out ImageHandler SrcTex) && + Texture.TryGetImage(DstKey, out ImageHandler DstTex)) + { + if (SrcTex.HasColor != DstTex.HasColor || + SrcTex.HasDepth != DstTex.HasDepth || + SrcTex.HasStencil != DstTex.HasStencil) + { + throw new NotImplementedException(); + } + + if (SrcTex.HasColor) + { + CopyTextures( + SrcX0, SrcY0, SrcX1, SrcY1, + DstX0, DstY0, DstX1, DstY1, + SrcTex.Handle, + DstTex.Handle, + FramebufferAttachment.ColorAttachment0, + ClearBufferMask.ColorBufferBit, + true); + } + else if (SrcTex.HasDepth && SrcTex.HasStencil) + { + CopyTextures( + SrcX0, SrcY0, SrcX1, SrcY1, + DstX0, DstY0, DstX1, DstY1, + SrcTex.Handle, + DstTex.Handle, + FramebufferAttachment.DepthStencilAttachment, + ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit, + false); + } + else if (SrcTex.HasDepth) + { + CopyTextures( + SrcX0, SrcY0, SrcX1, SrcY1, + DstX0, DstY0, DstX1, DstY1, + SrcTex.Handle, + DstTex.Handle, + FramebufferAttachment.DepthAttachment, + ClearBufferMask.DepthBufferBit, + false); + } + else if (SrcTex.HasStencil) + { + CopyTextures( + SrcX0, SrcY0, SrcX1, SrcY1, + DstX0, DstY0, DstX1, DstY1, + SrcTex.Handle, + DstTex.Handle, + FramebufferAttachment.StencilAttachment, + ClearBufferMask.StencilBufferBit, + false); + } + else + { + throw new InvalidOperationException(); + } + } + } + + public void GetBufferData(long Key, Action Callback) + { + if (Texture.TryGetImage(Key, out ImageHandler Tex)) + { + byte[] Data = new byte[ImageUtils.GetSize(Tex.Image)]; + + GL.BindTexture(TextureTarget.Texture2D, Tex.Handle); + + GL.GetTexImage( + TextureTarget.Texture2D, + 0, + Tex.PixelFormat, + Tex.PixelType, + Data); + + Callback(Data); + } + } + + public void SetBufferData( + long Key, + int Width, + int Height, + byte[] Buffer) + { + if (Texture.TryGetImage(Key, out ImageHandler Tex)) + { + GL.BindTexture(TextureTarget.Texture2D, Tex.Handle); + + const int Level = 0; + const int Border = 0; + + GL.TexImage2D( + TextureTarget.Texture2D, + Level, + Tex.InternalFormat, + Width, + Height, + Border, + Tex.PixelFormat, + Tex.PixelType, + Buffer); + } + } + + private void EnsureFrameBuffer() + { + if (DummyFrameBuffer == 0) + { + DummyFrameBuffer = GL.GenFramebuffer(); + } + + GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DummyFrameBuffer); + } + + private void Attach(ref int OldHandle, int NewHandle, FramebufferAttachment FbAttachment) + { + if (OldHandle != NewHandle) + { + GL.FramebufferTexture( + FramebufferTarget.DrawFramebuffer, + FbAttachment, + NewHandle, + 0); + + OldHandle = NewHandle; + } + } + + private void CopyTextures( + int SrcX0, + int SrcY0, + int SrcX1, + int SrcY1, + int DstX0, + int DstY0, + int DstX1, + int DstY1, + int SrcTexture, + int DstTexture, + FramebufferAttachment Attachment, + ClearBufferMask Mask, + bool Color) + { + if (SrcFb == 0) SrcFb = GL.GenFramebuffer(); + if (DstFb == 0) DstFb = GL.GenFramebuffer(); + + GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb); + GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DstFb); + + GL.FramebufferTexture( + FramebufferTarget.ReadFramebuffer, + Attachment, + SrcTexture, + 0); + + GL.FramebufferTexture( + FramebufferTarget.DrawFramebuffer, + Attachment, + DstTexture, + 0); + + if (Color) + { + GL.DrawBuffer(DrawBufferMode.ColorAttachment0); + } + + GL.Clear(Mask); + + GL.BlitFramebuffer( + SrcX0, SrcY0, SrcX1, SrcY1, + DstX0, DstY0, DstX1, DstY1, + Mask, + Color ? BlitFramebufferFilter.Linear : BlitFramebufferFilter.Nearest); + + EnsureFrameBuffer(); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs index 985f1086..a23541f3 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLRenderer.cs @@ -7,7 +7,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL { public IGalConstBuffer Buffer { get; private set; } - public IGalFrameBuffer FrameBuffer { get; private set; } + public IGalRenderTarget RenderTarget { get; private set; } public IGalRasterizer Rasterizer { get; private set; } @@ -25,7 +25,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL Texture = new OGLTexture(); - FrameBuffer = new OGLFrameBuffer(Texture as OGLTexture); + RenderTarget = new OGLRenderTarget(Texture as OGLTexture); Rasterizer = new OGLRasterizer(); diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs index e4d4bd64..82f9c913 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs @@ -1,5 +1,5 @@ using OpenTK.Graphics.OpenGL; -using Ryujinx.Graphics.Gal.Texture; +using Ryujinx.Graphics.Texture; using System; namespace Ryujinx.Graphics.Gal.OpenGL @@ -39,7 +39,11 @@ namespace Ryujinx.Graphics.Gal.OpenGL const int Level = 0; //TODO: Support mipmap textures. const int Border = 0; - if (IsCompressedTextureFormat(Image.Format)) + GalImageFormat TypeLess = Image.Format & GalImageFormat.FormatMask; + + bool IsASTC = TypeLess >= GalImageFormat.ASTC_BEGIN && TypeLess <= GalImageFormat.ASTC_END; + + if (ImageUtils.IsCompressed(Image.Format) && !IsASTC) { InternalFormat InternalFmt = OGLEnumConverter.GetCompressedImageFormat(Image.Format); @@ -55,7 +59,8 @@ namespace Ryujinx.Graphics.Gal.OpenGL } else { - if (Image.Format >= GalImageFormat.ASTC_BEGIN && Image.Format <= GalImageFormat.ASTC_END) + //TODO: Use KHR_texture_compression_astc_hdr when available + if (IsASTC) { int TextureBlockWidth = GetAstcBlockWidth(Image.Format); int TextureBlockHeight = GetAstcBlockHeight(Image.Format); @@ -67,7 +72,17 @@ namespace Ryujinx.Graphics.Gal.OpenGL Image.Width, Image.Height, 1); - Image.Format = GalImageFormat.A8B8G8R8_UNORM_PACK32; + Image.Format = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm; + } + else if (TypeLess == GalImageFormat.G8R8) + { + Data = ImageConverter.G8R8ToR8G8( + Data, + Image.Width, + Image.Height, + 1); + + Image.Format = GalImageFormat.R8G8 | (Image.Format & GalImageFormat.FormatMask); } (PixelInternalFormat InternalFormat, PixelFormat Format, PixelType Type) = OGLEnumConverter.GetImageFormat(Image.Format); @@ -123,20 +138,20 @@ namespace Ryujinx.Graphics.Gal.OpenGL { switch (Format) { - case GalImageFormat.ASTC_4x4_UNORM_BLOCK: return 4; - case GalImageFormat.ASTC_5x5_UNORM_BLOCK: return 5; - case GalImageFormat.ASTC_6x6_UNORM_BLOCK: return 6; - case GalImageFormat.ASTC_8x8_UNORM_BLOCK: return 8; - case GalImageFormat.ASTC_10x10_UNORM_BLOCK: return 10; - case GalImageFormat.ASTC_12x12_UNORM_BLOCK: return 12; - case GalImageFormat.ASTC_5x4_UNORM_BLOCK: return 5; - case GalImageFormat.ASTC_6x5_UNORM_BLOCK: return 6; - case GalImageFormat.ASTC_8x6_UNORM_BLOCK: return 8; - case GalImageFormat.ASTC_10x8_UNORM_BLOCK: return 10; - case GalImageFormat.ASTC_12x10_UNORM_BLOCK: return 12; - case GalImageFormat.ASTC_8x5_UNORM_BLOCK: return 8; - case GalImageFormat.ASTC_10x5_UNORM_BLOCK: return 10; - case GalImageFormat.ASTC_10x6_UNORM_BLOCK: return 10; + case GalImageFormat.ASTC_4x4 | GalImageFormat.Unorm: return 4; + case GalImageFormat.ASTC_5x5 | GalImageFormat.Unorm: return 5; + case GalImageFormat.ASTC_6x6 | GalImageFormat.Unorm: return 6; + case GalImageFormat.ASTC_8x8 | GalImageFormat.Unorm: return 8; + case GalImageFormat.ASTC_10x10 | GalImageFormat.Unorm: return 10; + case GalImageFormat.ASTC_12x12 | GalImageFormat.Unorm: return 12; + case GalImageFormat.ASTC_5x4 | GalImageFormat.Unorm: return 5; + case GalImageFormat.ASTC_6x5 | GalImageFormat.Unorm: return 6; + case GalImageFormat.ASTC_8x6 | GalImageFormat.Unorm: return 8; + case GalImageFormat.ASTC_10x8 | GalImageFormat.Unorm: return 10; + case GalImageFormat.ASTC_12x10 | GalImageFormat.Unorm: return 12; + case GalImageFormat.ASTC_8x5 | GalImageFormat.Unorm: return 8; + case GalImageFormat.ASTC_10x5 | GalImageFormat.Unorm: return 10; + case GalImageFormat.ASTC_10x6 | GalImageFormat.Unorm: return 10; } throw new ArgumentException(nameof(Format)); @@ -146,20 +161,20 @@ namespace Ryujinx.Graphics.Gal.OpenGL { switch (Format) { - case GalImageFormat.ASTC_4x4_UNORM_BLOCK: return 4; - case GalImageFormat.ASTC_5x5_UNORM_BLOCK: return 5; - case GalImageFormat.ASTC_6x6_UNORM_BLOCK: return 6; - case GalImageFormat.ASTC_8x8_UNORM_BLOCK: return 8; - case GalImageFormat.ASTC_10x10_UNORM_BLOCK: return 10; - case GalImageFormat.ASTC_12x12_UNORM_BLOCK: return 12; - case GalImageFormat.ASTC_5x4_UNORM_BLOCK: return 4; - case GalImageFormat.ASTC_6x5_UNORM_BLOCK: return 5; - case GalImageFormat.ASTC_8x6_UNORM_BLOCK: return 6; - case GalImageFormat.ASTC_10x8_UNORM_BLOCK: return 8; - case GalImageFormat.ASTC_12x10_UNORM_BLOCK: return 10; - case GalImageFormat.ASTC_8x5_UNORM_BLOCK: return 5; - case GalImageFormat.ASTC_10x5_UNORM_BLOCK: return 5; - case GalImageFormat.ASTC_10x6_UNORM_BLOCK: return 6; + case GalImageFormat.ASTC_4x4 | GalImageFormat.Unorm: return 4; + case GalImageFormat.ASTC_5x5 | GalImageFormat.Unorm: return 5; + case GalImageFormat.ASTC_6x6 | GalImageFormat.Unorm: return 6; + case GalImageFormat.ASTC_8x8 | GalImageFormat.Unorm: return 8; + case GalImageFormat.ASTC_10x10 | GalImageFormat.Unorm: return 10; + case GalImageFormat.ASTC_12x12 | GalImageFormat.Unorm: return 12; + case GalImageFormat.ASTC_5x4 | GalImageFormat.Unorm: return 4; + case GalImageFormat.ASTC_6x5 | GalImageFormat.Unorm: return 5; + case GalImageFormat.ASTC_8x6 | GalImageFormat.Unorm: return 6; + case GalImageFormat.ASTC_10x8 | GalImageFormat.Unorm: return 8; + case GalImageFormat.ASTC_12x10 | GalImageFormat.Unorm: return 10; + case GalImageFormat.ASTC_8x5 | GalImageFormat.Unorm: return 5; + case GalImageFormat.ASTC_10x5 | GalImageFormat.Unorm: return 5; + case GalImageFormat.ASTC_10x6 | GalImageFormat.Unorm: return 6; } throw new ArgumentException(nameof(Format)); @@ -216,25 +231,5 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, Color); } - - private static bool IsCompressedTextureFormat(GalImageFormat Format) - { - switch (Format) - { - case GalImageFormat.BC6H_UFLOAT_BLOCK: - case GalImageFormat.BC6H_SFLOAT_BLOCK: - case GalImageFormat.BC7_UNORM_BLOCK: - case GalImageFormat.BC1_RGBA_UNORM_BLOCK: - case GalImageFormat.BC2_UNORM_BLOCK: - case GalImageFormat.BC3_UNORM_BLOCK: - case GalImageFormat.BC4_SNORM_BLOCK: - case GalImageFormat.BC4_UNORM_BLOCK: - case GalImageFormat.BC5_SNORM_BLOCK: - case GalImageFormat.BC5_UNORM_BLOCK: - return true; - } - - return false; - } } } diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeAlu.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeAlu.cs index afff7e9b..c2ee474b 100644 --- a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeAlu.cs +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeAlu.cs @@ -33,20 +33,20 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Fadd_I32(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode OperA = GetOperGpr8 (OpCode); - ShaderIrNode OperB = GetOperImmf32_20(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); + ShaderIrNode OperB = OpCode.Immf32_20(); - bool NegB = ((OpCode >> 53) & 1) != 0; - bool AbsA = ((OpCode >> 54) & 1) != 0; - bool NegA = ((OpCode >> 56) & 1) != 0; - bool AbsB = ((OpCode >> 57) & 1) != 0; + bool NegB = OpCode.Read(53); + bool AbsA = OpCode.Read(54); + bool NegA = OpCode.Read(56); + bool AbsB = OpCode.Read(57); OperA = GetAluFabsFneg(OperA, AbsA, NegA); OperB = GetAluFabsFneg(OperB, AbsB, NegB); ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Fadd, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } public static void Fadd_R(ShaderIrBlock Block, long OpCode, long Position) @@ -91,12 +91,12 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Fmul_I32(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode OperA = GetOperGpr8 (OpCode); - ShaderIrNode OperB = GetOperImmf32_20(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); + ShaderIrNode OperB = OpCode.Immf32_20(); ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Fmul, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } public static void Fmul_C(ShaderIrBlock Block, long OpCode, long Position) @@ -156,16 +156,16 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Iadd_I32(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode OperA = GetOperGpr8 (OpCode); - ShaderIrNode OperB = GetOperImm32_20(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); + ShaderIrNode OperB = OpCode.Imm32_20(); - bool NegA = ((OpCode >> 56) & 1) != 0; + bool NegA = OpCode.Read(56); OperA = GetAluIneg(OperA, NegA); ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Add, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } public static void Iadd_R(ShaderIrBlock Block, long OpCode, long Position) @@ -205,16 +205,16 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Ipa(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode OperA = GetOperAbuf28(OpCode); - ShaderIrNode OperB = GetOperGpr20 (OpCode); + ShaderIrNode OperA = OpCode.Abuf28(); + ShaderIrNode OperB = OpCode.Gpr20(); - ShaderIpaMode Mode = (ShaderIpaMode)((OpCode >> 54) & 3); + ShaderIpaMode Mode = (ShaderIpaMode)(OpCode.Read(54, 3)); ShaderIrMetaIpa Meta = new ShaderIrMetaIpa(Mode); ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Ipa, OperA, OperB, null, Meta); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } public static void Iscadd_C(ShaderIrBlock Block, long OpCode, long Position) @@ -264,10 +264,10 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Lop_I32(ShaderIrBlock Block, long OpCode, long Position) { - int SubOp = (int)(OpCode >> 53) & 3; + int SubOp = OpCode.Read(53, 3); - bool InvA = ((OpCode >> 55) & 1) != 0; - bool InvB = ((OpCode >> 56) & 1) != 0; + bool InvA = OpCode.Read(55); + bool InvB = OpCode.Read(56); ShaderIrInst Inst = 0; @@ -278,21 +278,21 @@ namespace Ryujinx.Graphics.Gal.Shader case 2: Inst = ShaderIrInst.Xor; break; } - ShaderIrNode OperB = GetAluNot(GetOperImm32_20(OpCode), InvB); + ShaderIrNode OperB = GetAluNot(OpCode.Imm32_20(), InvB); //SubOp == 3 is pass, used by the not instruction //which just moves the inverted register value. if (SubOp < 3) { - ShaderIrNode OperA = GetAluNot(GetOperGpr8(OpCode), InvA); + ShaderIrNode OperA = GetAluNot(OpCode.Gpr8(), InvA); ShaderIrOp Op = new ShaderIrOp(Inst, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } else { - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperB), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OperB))); } } @@ -313,10 +313,10 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Mufu(ShaderIrBlock Block, long OpCode, long Position) { - int SubOp = (int)(OpCode >> 20) & 0xf; + int SubOp = OpCode.Read(20, 0xf); - bool AbsA = ((OpCode >> 46) & 1) != 0; - bool NegA = ((OpCode >> 48) & 1) != 0; + bool AbsA = OpCode.Read(46); + bool NegA = OpCode.Read(48); ShaderIrInst Inst = 0; @@ -333,23 +333,23 @@ namespace Ryujinx.Graphics.Gal.Shader default: throw new NotImplementedException(SubOp.ToString()); } - ShaderIrNode OperA = GetOperGpr8(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); ShaderIrOp Op = new ShaderIrOp(Inst, GetAluFabsFneg(OperA, AbsA, NegA)); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } public static void Psetp(ShaderIrBlock Block, long OpCode, long Position) { - bool NegA = ((OpCode >> 15) & 1) != 0; - bool NegB = ((OpCode >> 32) & 1) != 0; - bool NegP = ((OpCode >> 42) & 1) != 0; + bool NegA = OpCode.Read(15); + bool NegB = OpCode.Read(32); + bool NegP = OpCode.Read(42); - ShaderIrInst LopInst = GetBLop24(OpCode); + ShaderIrInst LopInst = OpCode.BLop24(); - ShaderIrNode OperA = GetOperPred12(OpCode); - ShaderIrNode OperB = GetOperPred29(OpCode); + ShaderIrNode OperA = OpCode.Pred12(); + ShaderIrNode OperB = OpCode.Pred29(); if (NegA) { @@ -363,13 +363,13 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrOp Op = new ShaderIrOp(LopInst, OperA, OperB); - ShaderIrOperPred P0Node = GetOperPred3 (OpCode); - ShaderIrOperPred P1Node = GetOperPred0 (OpCode); - ShaderIrOperPred P2Node = GetOperPred39(OpCode); + ShaderIrOperPred P0Node = OpCode.Pred3(); + ShaderIrOperPred P1Node = OpCode.Pred0(); + ShaderIrOperPred P2Node = OpCode.Pred39(); - Block.AddNode(GetPredNode(new ShaderIrAsg(P0Node, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op))); - LopInst = GetBLop45(OpCode); + LopInst = OpCode.BLop45(); if (LopInst == ShaderIrInst.Band && P1Node.IsConst && P2Node.IsConst) { @@ -387,11 +387,11 @@ namespace Ryujinx.Graphics.Gal.Shader Op = new ShaderIrOp(LopInst, Op, P2NNode); - Block.AddNode(GetPredNode(new ShaderIrAsg(P1Node, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P1Node, Op))); Op = new ShaderIrOp(LopInst, P0Node, P2NNode); - Block.AddNode(GetPredNode(new ShaderIrAsg(P0Node, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op))); } public static void Rro_C(ShaderIrBlock Block, long OpCode, long Position) @@ -441,33 +441,33 @@ namespace Ryujinx.Graphics.Gal.Shader private static ShaderIrInst GetShrInst(long OpCode) { - bool Signed = ((OpCode >> 48) & 1) != 0; + bool Signed = OpCode.Read(48); return Signed ? ShaderIrInst.Asr : ShaderIrInst.Lsr; } public static void Vmad(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode OperA = GetOperGpr8(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); ShaderIrNode OperB; - if (((OpCode >> 50) & 1) != 0) + if (OpCode.Read(50)) { - OperB = GetOperGpr20(OpCode); + OperB = OpCode.Gpr20(); } else { - OperB = GetOperImm19_20(OpCode); + OperB = OpCode.Imm19_20(); } - ShaderIrOperGpr OperC = GetOperGpr39(OpCode); + ShaderIrOperGpr OperC = OpCode.Gpr39(); ShaderIrNode Tmp = new ShaderIrOp(ShaderIrInst.Mul, OperA, OperB); ShaderIrNode Final = new ShaderIrOp(ShaderIrInst.Add, Tmp, OperC); - int Shr = (int)((OpCode >> 51) & 3); + int Shr = OpCode.Read(51, 3); if (Shr != 0) { @@ -478,7 +478,7 @@ namespace Ryujinx.Graphics.Gal.Shader Block.AddNode(new ShaderIrCmnt("Stubbed. Instruction is reduced to a * b + c")); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Final), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Final))); } public static void Xmad_CR(ShaderIrBlock Block, long OpCode, long Position) @@ -507,20 +507,20 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderOper Oper, ShaderIrInst Inst) { - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } ShaderIrNode Op = new ShaderIrOp(Inst, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitBfe(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -528,23 +528,23 @@ namespace Ryujinx.Graphics.Gal.Shader //TODO: Handle the case where position + length //is greater than the word size, in this case the sign bit //needs to be replicated to fill the remaining space. - bool NegB = ((OpCode >> 48) & 1) != 0; - bool NegA = ((OpCode >> 49) & 1) != 0; + bool NegB = OpCode.Read(48); + bool NegA = OpCode.Read(49); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } ShaderIrNode Op; - bool Signed = ((OpCode >> 48) & 1) != 0; //? + bool Signed = OpCode.Read(48); //? if (OperB is ShaderIrOperImm PosLen) { @@ -576,25 +576,25 @@ namespace Ryujinx.Graphics.Gal.Shader Op = ExtendTo32(Op, Signed, OpLen); } - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitFadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - bool NegB = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 46) & 1) != 0; - bool NegA = ((OpCode >> 48) & 1) != 0; - bool AbsB = ((OpCode >> 49) & 1) != 0; + bool NegB = OpCode.Read(45); + bool AbsA = OpCode.Read(46); + bool NegA = OpCode.Read(48); + bool AbsB = OpCode.Read(49); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; OperA = GetAluFabsFneg(OperA, AbsA, NegA); switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperB = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -603,20 +603,20 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrNode Op = new ShaderIrOp(ShaderIrInst.Fadd, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitFmul(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - bool NegB = ((OpCode >> 48) & 1) != 0; + bool NegB = OpCode.Read(48); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperB = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -625,22 +625,22 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrNode Op = new ShaderIrOp(ShaderIrInst.Fmul, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitFfma(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - bool NegB = ((OpCode >> 48) & 1) != 0; - bool NegC = ((OpCode >> 49) & 1) != 0; + bool NegB = OpCode.Read(48); + bool NegC = OpCode.Read(49); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB, OperC; + ShaderIrNode OperA = OpCode.Gpr8(), OperB, OperC; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperB = GetOperImmf19_20(OpCode); break; - case ShaderOper.RC: OperB = GetOperGpr39 (OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break; + case ShaderOper.RC: OperB = OpCode.Gpr39(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -649,62 +649,62 @@ namespace Ryujinx.Graphics.Gal.Shader if (Oper == ShaderOper.RC) { - OperC = GetAluFneg(GetOperCbuf34(OpCode), NegC); + OperC = GetAluFneg(OpCode.Cbuf34(), NegC); } else { - OperC = GetAluFneg(GetOperGpr39(OpCode), NegC); + OperC = GetAluFneg(OpCode.Gpr39(), NegC); } ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Ffma, OperA, OperB, OperC); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitIadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - ShaderIrNode OperA = GetOperGpr8(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); ShaderIrNode OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } - bool NegA = ((OpCode >> 49) & 1) != 0; - bool NegB = ((OpCode >> 48) & 1) != 0; + bool NegA = OpCode.Read(49); + bool NegB = OpCode.Read(48); OperA = GetAluIneg(OperA, NegA); OperB = GetAluIneg(OperB, NegB); ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Add, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitIadd3(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - int Mode = (int)((OpCode >> 37) & 3); + int Mode = OpCode.Read(37, 3); - bool Neg1 = ((OpCode >> 51) & 1) != 0; - bool Neg2 = ((OpCode >> 50) & 1) != 0; - bool Neg3 = ((OpCode >> 49) & 1) != 0; + bool Neg1 = OpCode.Read(51); + bool Neg2 = OpCode.Read(50); + bool Neg3 = OpCode.Read(49); - int Height1 = (int)((OpCode >> 35) & 3); - int Height2 = (int)((OpCode >> 33) & 3); - int Height3 = (int)((OpCode >> 31) & 3); + int Height1 = OpCode.Read(35, 3); + int Height2 = OpCode.Read(33, 3); + int Height3 = OpCode.Read(31, 3); ShaderIrNode OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -726,9 +726,9 @@ namespace Ryujinx.Graphics.Gal.Shader } } - ShaderIrNode Src1 = GetAluIneg(ApplyHeight(GetOperGpr8(OpCode), Height1), Neg1); + ShaderIrNode Src1 = GetAluIneg(ApplyHeight(OpCode.Gpr8(), Height1), Neg1); ShaderIrNode Src2 = GetAluIneg(ApplyHeight(OperB, Height2), Neg2); - ShaderIrNode Src3 = GetAluIneg(ApplyHeight(GetOperGpr39(OpCode), Height3), Neg3); + ShaderIrNode Src3 = GetAluIneg(ApplyHeight(OpCode.Gpr39(), Height3), Neg3); ShaderIrOp Sum = new ShaderIrOp(ShaderIrInst.Add, Src1, Src2); @@ -744,23 +744,23 @@ namespace Ryujinx.Graphics.Gal.Shader //Note: Here there should be a "+ 1" when carry flag is set //but since carry is mostly ignored by other instructions, it's excluded for now - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), new ShaderIrOp(ShaderIrInst.Add, Sum, Src3)), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), new ShaderIrOp(ShaderIrInst.Add, Sum, Src3)))); } private static void EmitIscadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - bool NegB = ((OpCode >> 48) & 1) != 0; - bool NegA = ((OpCode >> 49) & 1) != 0; + bool NegB = OpCode.Read(48); + bool NegA = OpCode.Read(49); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; - ShaderIrOperImm Scale = GetOperImm5_39(OpCode); + ShaderIrOperImm Scale = OpCode.Imm5_39(); switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -771,7 +771,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrOp ScaleOp = new ShaderIrOp(ShaderIrInst.Lsl, OperA, Scale); ShaderIrOp AddOp = new ShaderIrOp(ShaderIrInst.Add, OperB, ScaleOp); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), AddOp), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), AddOp))); } private static void EmitFmnmx(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -786,12 +786,12 @@ namespace Ryujinx.Graphics.Gal.Shader private static void EmitMnmx(ShaderIrBlock Block, long OpCode, bool IsFloat, ShaderOper Oper) { - bool NegB = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 46) & 1) != 0; - bool NegA = ((OpCode >> 48) & 1) != 0; - bool AbsB = ((OpCode >> 49) & 1) != 0; + bool NegB = OpCode.Read(45); + bool AbsA = OpCode.Read(46); + bool NegA = OpCode.Read(48); + bool AbsB = OpCode.Read(49); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; if (IsFloat) { @@ -804,10 +804,10 @@ namespace Ryujinx.Graphics.Gal.Shader switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20 (OpCode); break; - case ShaderOper.Immf: OperB = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -821,7 +821,7 @@ namespace Ryujinx.Graphics.Gal.Shader OperB = GetAluIabsIneg(OperB, AbsB, NegB); } - ShaderIrOperPred Pred = GetOperPred39(OpCode); + ShaderIrOperPred Pred = OpCode.Pred39(); ShaderIrOp Op; @@ -830,26 +830,26 @@ namespace Ryujinx.Graphics.Gal.Shader if (Pred.IsConst) { - bool IsMax = ((OpCode >> 42) & 1) != 0; + bool IsMax = OpCode.Read(42); Op = new ShaderIrOp(IsMax ? MaxInst : MinInst, OperA, OperB); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } else { - ShaderIrNode PredN = GetOperPred39N(OpCode); + ShaderIrNode PredN = OpCode.Pred39N(); ShaderIrOp OpMax = new ShaderIrOp(MaxInst, OperA, OperB); ShaderIrOp OpMin = new ShaderIrOp(MinInst, OperA, OperB); - ShaderIrAsg AsgMax = new ShaderIrAsg(GetOperGpr0(OpCode), OpMax); - ShaderIrAsg AsgMin = new ShaderIrAsg(GetOperGpr0(OpCode), OpMin); + ShaderIrAsg AsgMax = new ShaderIrAsg(OpCode.Gpr0(), OpMax); + ShaderIrAsg AsgMin = new ShaderIrAsg(OpCode.Gpr0(), OpMin); - Block.AddNode(GetPredNode(new ShaderIrCond(PredN, AsgMax, Not: true), OpCode)); - Block.AddNode(GetPredNode(new ShaderIrCond(PredN, AsgMin, Not: false), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrCond(PredN, AsgMax, Not: true))); + Block.AddNode(OpCode.PredNode(new ShaderIrCond(PredN, AsgMin, Not: false))); } } @@ -857,16 +857,16 @@ namespace Ryujinx.Graphics.Gal.Shader { //Note: this is a range reduction instruction and is supposed to //be used with Mufu, here it just moves the value and ignores the operation. - bool NegA = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 49) & 1) != 0; + bool NegA = OpCode.Read(45); + bool AbsA = OpCode.Read(49); ShaderIrNode OperA; switch (Oper) { - case ShaderOper.CR: OperA = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperA = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperA = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperA = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperA = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperA = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -875,7 +875,7 @@ namespace Ryujinx.Graphics.Gal.Shader Block.AddNode(new ShaderIrCmnt("Stubbed.")); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OperA))); } private static void EmitFset(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -890,21 +890,21 @@ namespace Ryujinx.Graphics.Gal.Shader private static void EmitSet(ShaderIrBlock Block, long OpCode, bool IsFloat, ShaderOper Oper) { - bool NegA = ((OpCode >> 43) & 1) != 0; - bool AbsB = ((OpCode >> 44) & 1) != 0; - bool NegB = ((OpCode >> 53) & 1) != 0; - bool AbsA = ((OpCode >> 54) & 1) != 0; + bool NegA = OpCode.Read(43); + bool AbsB = OpCode.Read(44); + bool NegB = OpCode.Read(53); + bool AbsA = OpCode.Read(54); - bool BoolFloat = ((OpCode >> (IsFloat ? 52 : 44)) & 1) != 0; + bool BoolFloat = OpCode.Read(IsFloat ? 52 : 44); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20 (OpCode); break; - case ShaderOper.Immf: OperB = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -916,18 +916,18 @@ namespace Ryujinx.Graphics.Gal.Shader OperA = GetAluFabsFneg(OperA, AbsA, NegA); OperB = GetAluFabsFneg(OperB, AbsB, NegB); - CmpInst = GetCmpF(OpCode); + CmpInst = OpCode.CmpF(); } else { - CmpInst = GetCmp(OpCode); + CmpInst = OpCode.Cmp(); } ShaderIrOp Op = new ShaderIrOp(CmpInst, OperA, OperB); - ShaderIrInst LopInst = GetBLop45(OpCode); + ShaderIrInst LopInst = OpCode.BLop45(); - ShaderIrOperPred PNode = GetOperPred39(OpCode); + ShaderIrOperPred PNode = OpCode.Pred39(); ShaderIrNode Imm0, Imm1; @@ -942,8 +942,8 @@ namespace Ryujinx.Graphics.Gal.Shader Imm1 = new ShaderIrOperImm(-1); } - ShaderIrNode Asg0 = new ShaderIrAsg(GetOperGpr0(OpCode), Imm0); - ShaderIrNode Asg1 = new ShaderIrAsg(GetOperGpr0(OpCode), Imm1); + ShaderIrNode Asg0 = new ShaderIrAsg(OpCode.Gpr0(), Imm0); + ShaderIrNode Asg1 = new ShaderIrAsg(OpCode.Gpr0(), Imm1); if (LopInst != ShaderIrInst.Band || !PNode.IsConst) { @@ -958,8 +958,8 @@ namespace Ryujinx.Graphics.Gal.Shader Asg1 = new ShaderIrCond(Op, Asg1, Not: false); } - Block.AddNode(GetPredNode(Asg0, OpCode)); - Block.AddNode(GetPredNode(Asg1, OpCode)); + Block.AddNode(OpCode.PredNode(Asg0)); + Block.AddNode(OpCode.PredNode(Asg1)); } private static void EmitFsetp(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -974,19 +974,19 @@ namespace Ryujinx.Graphics.Gal.Shader private static void EmitSetp(ShaderIrBlock Block, long OpCode, bool IsFloat, ShaderOper Oper) { - bool AbsA = ((OpCode >> 7) & 1) != 0; - bool NegP = ((OpCode >> 42) & 1) != 0; - bool NegA = ((OpCode >> 43) & 1) != 0; - bool AbsB = ((OpCode >> 44) & 1) != 0; + bool AbsA = OpCode.Read(7); + bool NegP = OpCode.Read(42); + bool NegA = OpCode.Read(43); + bool AbsB = OpCode.Read(44); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB; + ShaderIrNode OperA = OpCode.Gpr8(), OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20 (OpCode); break; - case ShaderOper.Immf: OperB = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -998,22 +998,22 @@ namespace Ryujinx.Graphics.Gal.Shader OperA = GetAluFabsFneg(OperA, AbsA, NegA); OperB = GetAluFabs (OperB, AbsB); - CmpInst = GetCmpF(OpCode); + CmpInst = OpCode.CmpF(); } else { - CmpInst = GetCmp(OpCode); + CmpInst = OpCode.Cmp(); } ShaderIrOp Op = new ShaderIrOp(CmpInst, OperA, OperB); - ShaderIrOperPred P0Node = GetOperPred3 (OpCode); - ShaderIrOperPred P1Node = GetOperPred0 (OpCode); - ShaderIrOperPred P2Node = GetOperPred39(OpCode); + ShaderIrOperPred P0Node = OpCode.Pred3(); + ShaderIrOperPred P1Node = OpCode.Pred0(); + ShaderIrOperPred P2Node = OpCode.Pred39(); - Block.AddNode(GetPredNode(new ShaderIrAsg(P0Node, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op))); - ShaderIrInst LopInst = GetBLop45(OpCode); + ShaderIrInst LopInst = OpCode.BLop45(); if (LopInst == ShaderIrInst.Band && P1Node.IsConst && P2Node.IsConst) { @@ -1031,19 +1031,19 @@ namespace Ryujinx.Graphics.Gal.Shader Op = new ShaderIrOp(LopInst, Op, P2NNode); - Block.AddNode(GetPredNode(new ShaderIrAsg(P1Node, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P1Node, Op))); Op = new ShaderIrOp(LopInst, P0Node, P2NNode); - Block.AddNode(GetPredNode(new ShaderIrAsg(P0Node, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op))); } private static void EmitLop(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - int SubOp = (int)(OpCode >> 41) & 3; + int SubOp = OpCode.Read(41, 3); - bool InvA = ((OpCode >> 39) & 1) != 0; - bool InvB = ((OpCode >> 40) & 1) != 0; + bool InvA = OpCode.Read(39); + bool InvB = OpCode.Read(40); ShaderIrInst Inst = 0; @@ -1054,14 +1054,14 @@ namespace Ryujinx.Graphics.Gal.Shader case 2: Inst = ShaderIrInst.Xor; break; } - ShaderIrNode OperA = GetAluNot(GetOperGpr8(OpCode), InvA); + ShaderIrNode OperA = GetAluNot(OpCode.Gpr8(), InvA); ShaderIrNode OperB; switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -1081,23 +1081,23 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrNode Compare = new ShaderIrOp(ShaderIrInst.Cne, Op, new ShaderIrOperImm(0)); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperPred48(OpCode), Compare), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Pred48(), Compare))); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitXmad(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { //TODO: Confirm SignAB/C, it is just a guess. //TODO: Implement Mode 3 (CSFU), what it does? - bool SignAB = ((OpCode >> 48) & 1) != 0; - bool SignC = ((OpCode >> 49) & 1) != 0; - bool HighB = ((OpCode >> 52) & 1) != 0; - bool HighA = ((OpCode >> 53) & 1) != 0; + bool SignAB = OpCode.Read(48); + bool SignC = OpCode.Read(49); + bool HighB = OpCode.Read(52); + bool HighA = OpCode.Read(53); - int Mode = (int)(OpCode >> 50) & 7; + int Mode = OpCode.Read(50, 7); - ShaderIrNode OperA = GetOperGpr8(OpCode), OperB, OperC; + ShaderIrNode OperA = OpCode.Gpr8(), OperB, OperC; ShaderIrOperImm Imm16 = new ShaderIrOperImm(16); ShaderIrOperImm ImmMsk = new ShaderIrOperImm(0xffff); @@ -1112,10 +1112,10 @@ namespace Ryujinx.Graphics.Gal.Shader switch (Oper) { - case ShaderOper.CR: OperB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperB = GetOperImm19_20(OpCode); break; - case ShaderOper.RC: OperB = GetOperGpr39 (OpCode); break; - case ShaderOper.RR: OperB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break; + case ShaderOper.RC: OperB = OpCode.Gpr39(); break; + case ShaderOper.RR: OperB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -1124,14 +1124,14 @@ namespace Ryujinx.Graphics.Gal.Shader if (Oper == ShaderOper.RC) { - OperC = GetOperCbuf34(OpCode); + OperC = OpCode.Cbuf34(); } else { - OperC = GetOperGpr39(OpCode); + OperC = OpCode.Gpr39(); - ProductShiftLeft = ((OpCode >> 36) & 1) != 0; - Merge = ((OpCode >> 37) & 1) != 0; + ProductShiftLeft = OpCode.Read(36); + Merge = OpCode.Read(37); } switch (Mode) @@ -1172,7 +1172,7 @@ namespace Ryujinx.Graphics.Gal.Shader AddOp = new ShaderIrOp(ShaderIrInst.Or, AddOp, OperB); } - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), AddOp), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), AddOp))); } } } \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeFlow.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeFlow.cs index 2c699a1b..dfd10e00 100644 --- a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeFlow.cs +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeFlow.cs @@ -15,11 +15,11 @@ namespace Ryujinx.Graphics.Gal.Shader throw new NotImplementedException(); } - int Target = ((int)(OpCode >> 20) << 8) >> 8; + int Target = OpCode.Branch(); ShaderIrOperImm Imm = new ShaderIrOperImm(Target); - Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Bra, Imm), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrOp(ShaderIrInst.Bra, Imm))); } public static void Exit(ShaderIrBlock Block, long OpCode, long Position) @@ -29,14 +29,14 @@ namespace Ryujinx.Graphics.Gal.Shader //TODO: Figure out what the other condition codes mean... if (CCode == 0xf) { - Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Exit), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrOp(ShaderIrInst.Exit))); } } public static void Kil(ShaderIrBlock Block, long OpCode, long Position) { - Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Kil), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrOp(ShaderIrInst.Kil))); } public static void Ssy(ShaderIrBlock Block, long OpCode, long Position) @@ -48,7 +48,7 @@ namespace Ryujinx.Graphics.Gal.Shader throw new NotImplementedException(); } - int Offset = ((int)(OpCode >> 20) << 8) >> 8; + int Offset = OpCode.Branch(); int Target = (int)(Position + Offset); @@ -61,7 +61,7 @@ namespace Ryujinx.Graphics.Gal.Shader { //TODO: Implement Sync condition codes - Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Sync), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrOp(ShaderIrInst.Sync))); } } } \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeHelper.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeHelper.cs index 2093f070..010f06aa 100644 --- a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeHelper.cs +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeHelper.cs @@ -4,244 +4,6 @@ namespace Ryujinx.Graphics.Gal.Shader { static class ShaderDecodeHelper { - public static ShaderIrOperAbuf[] GetOperAbuf20(long OpCode) - { - int Abuf = (int)(OpCode >> 20) & 0x3ff; - int Size = (int)(OpCode >> 47) & 3; - - ShaderIrOperGpr Vertex = GetOperGpr39(OpCode); - - ShaderIrOperAbuf[] Opers = new ShaderIrOperAbuf[Size + 1]; - - for (int Index = 0; Index <= Size; Index++) - { - Opers[Index] = new ShaderIrOperAbuf(Abuf + Index * 4, Vertex); - } - - return Opers; - } - - public static ShaderIrOperAbuf GetOperAbuf28(long OpCode) - { - int Abuf = (int)(OpCode >> 28) & 0x3ff; - - return new ShaderIrOperAbuf(Abuf, GetOperGpr39(OpCode)); - } - - public static ShaderIrOperCbuf GetOperCbuf34(long OpCode) - { - return new ShaderIrOperCbuf( - (int)(OpCode >> 34) & 0x1f, - (int)(OpCode >> 20) & 0x3fff); - } - - public static ShaderIrOperGpr GetOperGpr8(long OpCode) - { - return new ShaderIrOperGpr((int)(OpCode >> 8) & 0xff); - } - - public static ShaderIrOperGpr GetOperGpr20(long OpCode) - { - return new ShaderIrOperGpr((int)(OpCode >> 20) & 0xff); - } - - public static ShaderIrOperGpr GetOperGpr39(long OpCode) - { - return new ShaderIrOperGpr((int)(OpCode >> 39) & 0xff); - } - - public static ShaderIrOperGpr GetOperGpr0(long OpCode) - { - return new ShaderIrOperGpr((int)(OpCode >> 0) & 0xff); - } - - public static ShaderIrOperGpr GetOperGpr28(long OpCode) - { - return new ShaderIrOperGpr((int)(OpCode >> 28) & 0xff); - } - - public static ShaderIrOperImm GetOperImm5_39(long OpCode) - { - return new ShaderIrOperImm((int)(OpCode >> 39) & 0x1f); - } - - public static ShaderIrOperImm GetOperImm13_36(long OpCode) - { - return new ShaderIrOperImm((int)(OpCode >> 36) & 0x1fff); - } - - public static ShaderIrOperImm GetOperImm32_20(long OpCode) - { - return new ShaderIrOperImm((int)(OpCode >> 20)); - } - - public static ShaderIrOperImmf GetOperImmf32_20(long OpCode) - { - return new ShaderIrOperImmf(BitConverter.Int32BitsToSingle((int)(OpCode >> 20))); - } - - public static ShaderIrOperImm GetOperImm19_20(long OpCode) - { - int Value = (int)(OpCode >> 20) & 0x7ffff; - - bool Neg = ((OpCode >> 56) & 1) != 0; - - if (Neg) - { - Value = -Value; - } - - return new ShaderIrOperImm((int)Value); - } - - public static ShaderIrOperImmf GetOperImmf19_20(long OpCode) - { - uint Imm = (uint)(OpCode >> 20) & 0x7ffff; - - bool Neg = ((OpCode >> 56) & 1) != 0; - - Imm <<= 12; - - if (Neg) - { - Imm |= 0x80000000; - } - - float Value = BitConverter.Int32BitsToSingle((int)Imm); - - return new ShaderIrOperImmf(Value); - } - - public static ShaderIrOperPred GetOperPred0(long OpCode) - { - return new ShaderIrOperPred((int)(OpCode >> 0) & 7); - } - - public static ShaderIrOperPred GetOperPred3(long OpCode) - { - return new ShaderIrOperPred((int)(OpCode >> 3) & 7); - } - - public static ShaderIrOperPred GetOperPred12(long OpCode) - { - return new ShaderIrOperPred((int)(OpCode >> 12) & 7); - } - - public static ShaderIrOperPred GetOperPred29(long OpCode) - { - return new ShaderIrOperPred((int)(OpCode >> 29) & 7); - } - - public static ShaderIrNode GetOperPred39N(long OpCode) - { - ShaderIrNode Node = GetOperPred39(OpCode); - - if (((OpCode >> 42) & 1) != 0) - { - Node = new ShaderIrOp(ShaderIrInst.Bnot, Node); - } - - return Node; - } - - public static ShaderIrOperPred GetOperPred39(long OpCode) - { - return new ShaderIrOperPred((int)(OpCode >> 39) & 7); - } - - public static ShaderIrOperPred GetOperPred48(long OpCode) - { - return new ShaderIrOperPred((int)((OpCode >> 48) & 7)); - } - - public static ShaderIrInst GetCmp(long OpCode) - { - switch ((int)(OpCode >> 49) & 7) - { - case 1: return ShaderIrInst.Clt; - case 2: return ShaderIrInst.Ceq; - case 3: return ShaderIrInst.Cle; - case 4: return ShaderIrInst.Cgt; - case 5: return ShaderIrInst.Cne; - case 6: return ShaderIrInst.Cge; - } - - throw new ArgumentException(nameof(OpCode)); - } - - public static ShaderIrInst GetCmpF(long OpCode) - { - switch ((int)(OpCode >> 48) & 0xf) - { - case 0x1: return ShaderIrInst.Fclt; - case 0x2: return ShaderIrInst.Fceq; - case 0x3: return ShaderIrInst.Fcle; - case 0x4: return ShaderIrInst.Fcgt; - case 0x5: return ShaderIrInst.Fcne; - case 0x6: return ShaderIrInst.Fcge; - case 0x7: return ShaderIrInst.Fcnum; - case 0x8: return ShaderIrInst.Fcnan; - case 0x9: return ShaderIrInst.Fcltu; - case 0xa: return ShaderIrInst.Fcequ; - case 0xb: return ShaderIrInst.Fcleu; - case 0xc: return ShaderIrInst.Fcgtu; - case 0xd: return ShaderIrInst.Fcneu; - case 0xe: return ShaderIrInst.Fcgeu; - } - - throw new ArgumentException(nameof(OpCode)); - } - - public static ShaderIrInst GetBLop45(long OpCode) - { - switch ((int)(OpCode >> 45) & 3) - { - case 0: return ShaderIrInst.Band; - case 1: return ShaderIrInst.Bor; - case 2: return ShaderIrInst.Bxor; - } - - throw new ArgumentException(nameof(OpCode)); - } - - public static ShaderIrInst GetBLop24(long OpCode) - { - switch ((int)(OpCode >> 24) & 3) - { - case 0: return ShaderIrInst.Band; - case 1: return ShaderIrInst.Bor; - case 2: return ShaderIrInst.Bxor; - } - - throw new ArgumentException(nameof(OpCode)); - } - - public static ShaderIrNode GetPredNode(ShaderIrNode Node, long OpCode) - { - ShaderIrOperPred Pred = GetPredNode(OpCode); - - if (Pred.Index != ShaderIrOperPred.UnusedIndex) - { - bool Inv = ((OpCode >> 19) & 1) != 0; - - Node = new ShaderIrCond(Pred, Node, Inv); - } - - return Node; - } - - private static ShaderIrOperPred GetPredNode(long OpCode) - { - int Pred = (int)(OpCode >> 16) & 0xf; - - if (Pred != 0xf) - { - Pred &= 7; - } - - return new ShaderIrOperPred(Pred); - } - public static ShaderIrNode GetAluFabsFneg(ShaderIrNode Node, bool Abs, bool Neg) { return GetAluFneg(GetAluFabs(Node, Abs), Neg); diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMem.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMem.cs index 2ae58bf8..508a0205 100644 --- a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMem.cs +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMem.cs @@ -33,28 +33,28 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Ld_A(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode[] Opers = GetOperAbuf20(OpCode); + ShaderIrNode[] Opers = OpCode.Abuf20(); //Used by GS - ShaderIrOperGpr Vertex = GetOperGpr39(OpCode); + ShaderIrOperGpr Vertex = OpCode.Gpr39(); int Index = 0; foreach (ShaderIrNode OperA in Opers) { - ShaderIrOperGpr OperD = GetOperGpr0(OpCode); + ShaderIrOperGpr OperD = OpCode.Gpr0(); OperD.Index += Index++; - Block.AddNode(GetPredNode(new ShaderIrAsg(OperD, OperA), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperD, OperA))); } } public static void Ld_C(ShaderIrBlock Block, long OpCode, long Position) { - int CbufPos = (int)(OpCode >> 22) & 0x3fff; - int CbufIndex = (int)(OpCode >> 36) & 0x1f; - int Type = (int)(OpCode >> 48) & 7; + int CbufPos = OpCode.Read(22, 0x3fff); + int CbufIndex = OpCode.Read(36, 0x1f); + int Type = OpCode.Read(48, 7); if (Type > 5) { @@ -63,7 +63,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrOperGpr Temp = ShaderIrOperGpr.MakeTemporary(); - Block.AddNode(new ShaderIrAsg(Temp, GetOperGpr8(OpCode))); + Block.AddNode(new ShaderIrAsg(Temp, OpCode.Gpr8())); int Count = Type == 5 ? 2 : 1; @@ -71,7 +71,7 @@ namespace Ryujinx.Graphics.Gal.Shader { ShaderIrOperCbuf OperA = new ShaderIrOperCbuf(CbufIndex, CbufPos, Temp); - ShaderIrOperGpr OperD = GetOperGpr0(OpCode); + ShaderIrOperGpr OperD = OpCode.Gpr0(); OperA.Pos += Index; OperD.Index += Index; @@ -93,43 +93,43 @@ namespace Ryujinx.Graphics.Gal.Shader Node = ExtendTo32(Node, Signed, Size); } - Block.AddNode(GetPredNode(new ShaderIrAsg(OperD, Node), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperD, Node))); } } public static void St_A(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode[] Opers = GetOperAbuf20(OpCode); + ShaderIrNode[] Opers = OpCode.Abuf20(); int Index = 0; foreach (ShaderIrNode OperA in Opers) { - ShaderIrOperGpr OperD = GetOperGpr0(OpCode); + ShaderIrOperGpr OperD = OpCode.Gpr0(); OperD.Index += Index++; - Block.AddNode(GetPredNode(new ShaderIrAsg(OperA, OperD), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperA, OperD))); } } public static void Texq(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrNode OperD = GetOperGpr0(OpCode); - ShaderIrNode OperA = GetOperGpr8(OpCode); + ShaderIrNode OperD = OpCode.Gpr0(); + ShaderIrNode OperA = OpCode.Gpr8(); - ShaderTexqInfo Info = (ShaderTexqInfo)((OpCode >> 22) & 0x1f); + ShaderTexqInfo Info = (ShaderTexqInfo)(OpCode.Read(22, 0x1f)); ShaderIrMetaTexq Meta0 = new ShaderIrMetaTexq(Info, 0); ShaderIrMetaTexq Meta1 = new ShaderIrMetaTexq(Info, 1); - ShaderIrNode OperC = GetOperImm13_36(OpCode); + ShaderIrNode OperC = OpCode.Imm13_36(); ShaderIrOp Op0 = new ShaderIrOp(ShaderIrInst.Texq, OperA, null, OperC, Meta0); ShaderIrOp Op1 = new ShaderIrOp(ShaderIrInst.Texq, OperA, null, OperC, Meta1); - Block.AddNode(GetPredNode(new ShaderIrAsg(OperD, Op0), OpCode)); - Block.AddNode(GetPredNode(new ShaderIrAsg(OperA, Op1), OpCode)); //Is this right? + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperD, Op0))); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperA, Op1))); //Is this right? } public static void Tex(ShaderIrBlock Block, long OpCode, long Position) @@ -149,7 +149,7 @@ namespace Ryujinx.Graphics.Gal.Shader for (int Index = 0; Index < Coords.Length; Index++) { - Coords[Index] = GetOperGpr8(OpCode); + Coords[Index] = OpCode.Gpr8(); Coords[Index].Index += Index; @@ -159,11 +159,11 @@ namespace Ryujinx.Graphics.Gal.Shader } } - int ChMask = (int)(OpCode >> 31) & 0xf; + int ChMask = OpCode.Read(31, 0xf); ShaderIrNode OperC = GprHandle - ? (ShaderIrNode)GetOperGpr20 (OpCode) - : (ShaderIrNode)GetOperImm13_36(OpCode); + ? (ShaderIrNode)OpCode.Gpr20() + : (ShaderIrNode)OpCode.Imm13_36(); ShaderIrInst Inst = GprHandle ? ShaderIrInst.Texb : ShaderIrInst.Texs; @@ -175,7 +175,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrOp Op = new ShaderIrOp(Inst, Coords[0], Coords[1], OperC, Meta); - Block.AddNode(GetPredNode(new ShaderIrAsg(Dst, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(Dst, Op))); } int RegInc = 0; @@ -189,7 +189,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrOperGpr Src = new ShaderIrOperGpr(TempRegStart + Ch); - ShaderIrOperGpr Dst = GetOperGpr0(OpCode); + ShaderIrOperGpr Dst = OpCode.Gpr0(); Dst.Index += RegInc++; @@ -198,7 +198,7 @@ namespace Ryujinx.Graphics.Gal.Shader continue; } - Block.AddNode(GetPredNode(new ShaderIrAsg(Dst, Src), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(Dst, Src))); } } @@ -215,14 +215,14 @@ namespace Ryujinx.Graphics.Gal.Shader private static void EmitTexs(ShaderIrBlock Block, long OpCode, ShaderIrInst Inst) { //TODO: Support other formats. - ShaderIrNode OperA = GetOperGpr8 (OpCode); - ShaderIrNode OperB = GetOperGpr20 (OpCode); - ShaderIrNode OperC = GetOperImm13_36(OpCode); + ShaderIrNode OperA = OpCode.Gpr8(); + ShaderIrNode OperB = OpCode.Gpr20(); + ShaderIrNode OperC = OpCode.Imm13_36(); int LutIndex; - LutIndex = GetOperGpr0 (OpCode).Index != ShaderIrOperGpr.ZRIndex ? 1 : 0; - LutIndex |= GetOperGpr28(OpCode).Index != ShaderIrOperGpr.ZRIndex ? 2 : 0; + LutIndex = OpCode.Gpr0 ().Index != ShaderIrOperGpr.ZRIndex ? 1 : 0; + LutIndex |= OpCode.Gpr28().Index != ShaderIrOperGpr.ZRIndex ? 2 : 0; if (LutIndex == 0) { @@ -231,7 +231,7 @@ namespace Ryujinx.Graphics.Gal.Shader return; } - int ChMask = MaskLut[LutIndex, (OpCode >> 50) & 7]; + int ChMask = MaskLut[LutIndex, OpCode.Read(50, 7)]; for (int Ch = 0; Ch < 4; Ch++) { @@ -241,7 +241,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrOp Op = new ShaderIrOp(Inst, OperA, OperB, OperC, Meta); - Block.AddNode(GetPredNode(new ShaderIrAsg(Dst, Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(Dst, Op))); } int RegInc = 0; @@ -252,11 +252,11 @@ namespace Ryujinx.Graphics.Gal.Shader switch (LutIndex) { - case 1: Dst = GetOperGpr0 (OpCode); break; - case 2: Dst = GetOperGpr28(OpCode); break; + case 1: Dst = OpCode.Gpr0(); break; + case 2: Dst = OpCode.Gpr28(); break; case 3: Dst = (RegInc >> 1) != 0 - ? GetOperGpr28(OpCode) - : GetOperGpr0 (OpCode); break; + ? OpCode.Gpr28() + : OpCode.Gpr0 (); break; default: throw new InvalidOperationException(); } @@ -279,7 +279,7 @@ namespace Ryujinx.Graphics.Gal.Shader if (Dst.Index != ShaderIrOperGpr.ZRIndex) { - Block.AddNode(GetPredNode(new ShaderIrAsg(Dst, Src), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(Dst, Src))); } } } diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMove.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMove.cs index aef92c5a..add39402 100644 --- a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMove.cs +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeMove.cs @@ -92,35 +92,35 @@ namespace Ryujinx.Graphics.Gal.Shader Block.AddNode(new ShaderIrCmnt("Stubbed.")); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), GetOperGpr8(OpCode)), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OpCode.Gpr8()))); } public static void Mov_C(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrOperCbuf Cbuf = GetOperCbuf34(OpCode); + ShaderIrOperCbuf Cbuf = OpCode.Cbuf34(); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Cbuf), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Cbuf))); } public static void Mov_I(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrOperImm Imm = GetOperImm19_20(OpCode); + ShaderIrOperImm Imm = OpCode.Imm19_20(); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Imm), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Imm))); } public static void Mov_I32(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrOperImm Imm = GetOperImm32_20(OpCode); + ShaderIrOperImm Imm = OpCode.Imm32_20(); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Imm), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Imm))); } public static void Mov_R(ShaderIrBlock Block, long OpCode, long Position) { - ShaderIrOperGpr Gpr = GetOperGpr20(OpCode); + ShaderIrOperGpr Gpr = OpCode.Gpr20(); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Gpr), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Gpr))); } public static void Sel_C(ShaderIrBlock Block, long OpCode, long Position) @@ -145,21 +145,21 @@ namespace Ryujinx.Graphics.Gal.Shader //Zero is used as a special number to get a valid "0 * 0 + VertexIndex" in a GS ShaderIrNode Source = new ShaderIrOperImm(0); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Source), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Source))); } private static void EmitF2f(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - bool NegA = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 49) & 1) != 0; + bool NegA = OpCode.Read(45); + bool AbsA = OpCode.Read(49); ShaderIrNode OperA; switch (Oper) { - case ShaderOper.CR: OperA = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperA = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperA = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperA = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperA = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperA = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -173,7 +173,7 @@ namespace Ryujinx.Graphics.Gal.Shader OperA = new ShaderIrOp(RoundInst, OperA); } - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OperA))); } private static void EmitF2i(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -188,16 +188,16 @@ namespace Ryujinx.Graphics.Gal.Shader throw new NotImplementedException(); } - bool NegA = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 49) & 1) != 0; + bool NegA = OpCode.Read(45); + bool AbsA = OpCode.Read(49); ShaderIrNode OperA; switch (Oper) { - case ShaderOper.CR: OperA = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperA = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperA = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperA = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperA = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperA = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -242,7 +242,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrNode Op = new ShaderIrOp(Inst, OperA); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitI2f(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -257,18 +257,18 @@ namespace Ryujinx.Graphics.Gal.Shader throw new NotImplementedException(); } - int Sel = (int)(OpCode >> 41) & 3; + int Sel = OpCode.Read(41, 3); - bool NegA = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 49) & 1) != 0; + bool NegA = OpCode.Read(45); + bool AbsA = OpCode.Read(49); ShaderIrNode OperA; switch (Oper) { - case ShaderOper.CR: OperA = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: OperA = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: OperA = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperA = OpCode.Cbuf34(); break; + case ShaderOper.Imm: OperA = OpCode.Imm19_20(); break; + case ShaderOper.RR: OperA = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -297,7 +297,7 @@ namespace Ryujinx.Graphics.Gal.Shader ShaderIrNode Op = new ShaderIrOp(Inst, OperA); - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), Op), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op))); } private static void EmitI2i(ShaderIrBlock Block, long OpCode, ShaderOper Oper) @@ -312,19 +312,19 @@ namespace Ryujinx.Graphics.Gal.Shader throw new NotImplementedException(); } - int Sel = (int)(OpCode >> 41) & 3; + int Sel = OpCode.Read(41, 3); - bool NegA = ((OpCode >> 45) & 1) != 0; - bool AbsA = ((OpCode >> 49) & 1) != 0; - bool SatA = ((OpCode >> 50) & 1) != 0; + bool NegA = OpCode.Read(45); + bool AbsA = OpCode.Read(49); + bool SatA = OpCode.Read(50); ShaderIrNode OperA; switch (Oper) { - case ShaderOper.CR: OperA = GetOperCbuf34 (OpCode); break; - case ShaderOper.Immf: OperA = GetOperImmf19_20(OpCode); break; - case ShaderOper.RR: OperA = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: OperA = OpCode.Cbuf34(); break; + case ShaderOper.Immf: OperA = OpCode.Immf19_20(); break; + case ShaderOper.RR: OperA = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } @@ -372,36 +372,36 @@ namespace Ryujinx.Graphics.Gal.Shader } } - Block.AddNode(GetPredNode(new ShaderIrAsg(GetOperGpr0(OpCode), OperA), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OperA))); } private static void EmitSel(ShaderIrBlock Block, long OpCode, ShaderOper Oper) { - ShaderIrOperGpr Dst = GetOperGpr0 (OpCode); - ShaderIrNode Pred = GetOperPred39N(OpCode); + ShaderIrOperGpr Dst = OpCode.Gpr0(); + ShaderIrNode Pred = OpCode.Pred39N(); - ShaderIrNode ResultA = GetOperGpr8(OpCode); + ShaderIrNode ResultA = OpCode.Gpr8(); ShaderIrNode ResultB; switch (Oper) { - case ShaderOper.CR: ResultB = GetOperCbuf34 (OpCode); break; - case ShaderOper.Imm: ResultB = GetOperImm19_20(OpCode); break; - case ShaderOper.RR: ResultB = GetOperGpr20 (OpCode); break; + case ShaderOper.CR: ResultB = OpCode.Cbuf34(); break; + case ShaderOper.Imm: ResultB = OpCode.Imm19_20(); break; + case ShaderOper.RR: ResultB = OpCode.Gpr20(); break; default: throw new ArgumentException(nameof(Oper)); } - Block.AddNode(GetPredNode(new ShaderIrCond(Pred, new ShaderIrAsg(Dst, ResultA), false), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrCond(Pred, new ShaderIrAsg(Dst, ResultA), false))); - Block.AddNode(GetPredNode(new ShaderIrCond(Pred, new ShaderIrAsg(Dst, ResultB), true), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrCond(Pred, new ShaderIrAsg(Dst, ResultB), true))); } private static IntType GetIntType(long OpCode) { - bool Signed = ((OpCode >> 13) & 1) != 0; + bool Signed = OpCode.Read(13); - IntType Type = (IntType)((OpCode >> 10) & 3); + IntType Type = (IntType)(OpCode.Read(10, 3)); if (Signed) { @@ -413,12 +413,12 @@ namespace Ryujinx.Graphics.Gal.Shader private static FloatType GetFloatType(long OpCode) { - return (FloatType)((OpCode >> 8) & 3); + return (FloatType)(OpCode.Read(8, 3)); } private static ShaderIrInst GetRoundInst(long OpCode) { - switch ((OpCode >> 39) & 3) + switch (OpCode.Read(39, 3)) { case 1: return ShaderIrInst.Floor; case 2: return ShaderIrInst.Ceil; diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeOpCode.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeOpCode.cs new file mode 100644 index 00000000..3af17cae --- /dev/null +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeOpCode.cs @@ -0,0 +1,260 @@ +using System; + +namespace Ryujinx.Graphics.Gal.Shader +{ + static partial class ShaderDecode + { + private static int Read(this long OpCode, int Position, int Mask) + { + return (int)(OpCode >> Position) & Mask; + } + + private static bool Read(this long OpCode, int Position) + { + return ((OpCode >> Position) & 1) != 0; + } + + private static int Branch(this long OpCode) + { + return ((int)(OpCode >> 20) << 8) >> 8; + } + + private static ShaderIrOperAbuf[] Abuf20(this long OpCode) + { + int Abuf = OpCode.Read(20, 0x3ff); + int Size = OpCode.Read(47, 3); + + ShaderIrOperGpr Vertex = OpCode.Gpr39(); + + ShaderIrOperAbuf[] Opers = new ShaderIrOperAbuf[Size + 1]; + + for (int Index = 0; Index <= Size; Index++) + { + Opers[Index] = new ShaderIrOperAbuf(Abuf + Index * 4, Vertex); + } + + return Opers; + } + + private static ShaderIrOperAbuf Abuf28(this long OpCode) + { + int Abuf = OpCode.Read(28, 0x3ff); + + return new ShaderIrOperAbuf(Abuf, OpCode.Gpr39()); + } + + private static ShaderIrOperCbuf Cbuf34(this long OpCode) + { + return new ShaderIrOperCbuf( + OpCode.Read(34, 0x1f), + OpCode.Read(20, 0x3fff)); + } + + private static ShaderIrOperGpr Gpr8(this long OpCode) + { + return new ShaderIrOperGpr(OpCode.Read(8, 0xff)); + } + + private static ShaderIrOperGpr Gpr20(this long OpCode) + { + return new ShaderIrOperGpr(OpCode.Read(20, 0xff)); + } + + private static ShaderIrOperGpr Gpr39(this long OpCode) + { + return new ShaderIrOperGpr(OpCode.Read(39, 0xff)); + } + + private static ShaderIrOperGpr Gpr0(this long OpCode) + { + return new ShaderIrOperGpr(OpCode.Read(0, 0xff)); + } + + private static ShaderIrOperGpr Gpr28(this long OpCode) + { + return new ShaderIrOperGpr(OpCode.Read(28, 0xff)); + } + + private static ShaderIrOperImm Imm5_39(this long OpCode) + { + return new ShaderIrOperImm(OpCode.Read(39, 0x1f)); + } + + private static ShaderIrOperImm Imm13_36(this long OpCode) + { + return new ShaderIrOperImm(OpCode.Read(36, 0x1fff)); + } + + private static ShaderIrOperImm Imm32_20(this long OpCode) + { + return new ShaderIrOperImm((int)(OpCode >> 20)); + } + + private static ShaderIrOperImmf Immf32_20(this long OpCode) + { + return new ShaderIrOperImmf(BitConverter.Int32BitsToSingle((int)(OpCode >> 20))); + } + + private static ShaderIrOperImm Imm19_20(this long OpCode) + { + int Value = OpCode.Read(20, 0x7ffff); + + bool Neg = OpCode.Read(56); + + if (Neg) + { + Value = -Value; + } + + return new ShaderIrOperImm(Value); + } + + private static ShaderIrOperImmf Immf19_20(this long OpCode) + { + uint Imm = (uint)(OpCode >> 20) & 0x7ffff; + + bool Neg = OpCode.Read(56); + + Imm <<= 12; + + if (Neg) + { + Imm |= 0x80000000; + } + + float Value = BitConverter.Int32BitsToSingle((int)Imm); + + return new ShaderIrOperImmf(Value); + } + + private static ShaderIrOperPred Pred0(this long OpCode) + { + return new ShaderIrOperPred(OpCode.Read(0, 7)); + } + + private static ShaderIrOperPred Pred3(this long OpCode) + { + return new ShaderIrOperPred(OpCode.Read(3, 7)); + } + + private static ShaderIrOperPred Pred12(this long OpCode) + { + return new ShaderIrOperPred(OpCode.Read(12, 7)); + } + + private static ShaderIrOperPred Pred29(this long OpCode) + { + return new ShaderIrOperPred(OpCode.Read(29, 7)); + } + + private static ShaderIrNode Pred39N(this long OpCode) + { + ShaderIrNode Node = OpCode.Pred39(); + + if (OpCode.Read(42)) + { + Node = new ShaderIrOp(ShaderIrInst.Bnot, Node); + } + + return Node; + } + + private static ShaderIrOperPred Pred39(this long OpCode) + { + return new ShaderIrOperPred(OpCode.Read(39, 7)); + } + + private static ShaderIrOperPred Pred48(this long OpCode) + { + return new ShaderIrOperPred(OpCode.Read(48, 7)); + } + + private static ShaderIrInst Cmp(this long OpCode) + { + switch (OpCode.Read(49, 7)) + { + case 1: return ShaderIrInst.Clt; + case 2: return ShaderIrInst.Ceq; + case 3: return ShaderIrInst.Cle; + case 4: return ShaderIrInst.Cgt; + case 5: return ShaderIrInst.Cne; + case 6: return ShaderIrInst.Cge; + } + + throw new ArgumentException(nameof(OpCode)); + } + + private static ShaderIrInst CmpF(this long OpCode) + { + switch (OpCode.Read(48, 0xf)) + { + case 0x1: return ShaderIrInst.Fclt; + case 0x2: return ShaderIrInst.Fceq; + case 0x3: return ShaderIrInst.Fcle; + case 0x4: return ShaderIrInst.Fcgt; + case 0x5: return ShaderIrInst.Fcne; + case 0x6: return ShaderIrInst.Fcge; + case 0x7: return ShaderIrInst.Fcnum; + case 0x8: return ShaderIrInst.Fcnan; + case 0x9: return ShaderIrInst.Fcltu; + case 0xa: return ShaderIrInst.Fcequ; + case 0xb: return ShaderIrInst.Fcleu; + case 0xc: return ShaderIrInst.Fcgtu; + case 0xd: return ShaderIrInst.Fcneu; + case 0xe: return ShaderIrInst.Fcgeu; + } + + throw new ArgumentException(nameof(OpCode)); + } + + private static ShaderIrInst BLop45(this long OpCode) + { + switch (OpCode.Read(45, 3)) + { + case 0: return ShaderIrInst.Band; + case 1: return ShaderIrInst.Bor; + case 2: return ShaderIrInst.Bxor; + } + + throw new ArgumentException(nameof(OpCode)); + } + + private static ShaderIrInst BLop24(this long OpCode) + { + switch (OpCode.Read(24, 3)) + { + case 0: return ShaderIrInst.Band; + case 1: return ShaderIrInst.Bor; + case 2: return ShaderIrInst.Bxor; + } + + throw new ArgumentException(nameof(OpCode)); + } + + private static ShaderIrNode PredNode(this long OpCode, ShaderIrNode Node) + { + ShaderIrOperPred Pred = OpCode.PredNode(); + + if (Pred.Index != ShaderIrOperPred.UnusedIndex) + { + bool Inv = OpCode.Read(19); + + Node = new ShaderIrCond(Pred, Node, Inv); + } + + return Node; + } + + private static ShaderIrOperPred PredNode(this long OpCode) + { + int Pred = OpCode.Read(16, 0xf); + + if (Pred != 0xf) + { + Pred &= 7; + } + + return new ShaderIrOperPred(Pred); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeSpecial.cs b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeSpecial.cs index 4300c32e..c3e42654 100644 --- a/Ryujinx.Graphics/Gal/Shader/ShaderDecodeSpecial.cs +++ b/Ryujinx.Graphics/Gal/Shader/ShaderDecodeSpecial.cs @@ -7,20 +7,20 @@ namespace Ryujinx.Graphics.Gal.Shader public static void Out_R(ShaderIrBlock Block, long OpCode, long Position) { //TODO: Those registers have to be used for something - ShaderIrOperGpr Gpr0 = GetOperGpr0(OpCode); - ShaderIrOperGpr Gpr8 = GetOperGpr8(OpCode); - ShaderIrOperGpr Gpr20 = GetOperGpr20(OpCode); + ShaderIrOperGpr Gpr0 = OpCode.Gpr0(); + ShaderIrOperGpr Gpr8 = OpCode.Gpr8(); + ShaderIrOperGpr Gpr20 = OpCode.Gpr20(); - int Type = (int)((OpCode >> 39) & 3); + int Type = OpCode.Read(39, 3); if ((Type & 1) != 0) { - Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Emit), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrOp(ShaderIrInst.Emit))); } if ((Type & 2) != 0) { - Block.AddNode(GetPredNode(new ShaderIrOp(ShaderIrInst.Cut), OpCode)); + Block.AddNode(OpCode.PredNode(new ShaderIrOp(ShaderIrInst.Cut))); } } } diff --git a/Ryujinx.Graphics/Gal/Texture/ASTCDecoder.cs b/Ryujinx.Graphics/Gal/Texture/ASTCDecoder.cs deleted file mode 100644 index da1b9ef4..00000000 --- a/Ryujinx.Graphics/Gal/Texture/ASTCDecoder.cs +++ /dev/null @@ -1,1384 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; - -namespace Ryujinx.Graphics.Gal.Texture -{ - public class ASTCDecoderException : Exception - { - public ASTCDecoderException(string ExMsg) : base(ExMsg) { } - } - - //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp - public static class ASTCDecoder - { - struct TexelWeightParams - { - public int Width; - public int Height; - public bool DualPlane; - public int MaxWeight; - public bool Error; - public bool VoidExtentLDR; - public bool VoidExtentHDR; - - public int GetPackedBitSize() - { - // How many indices do we have? - int Indices = Height * Width; - - if (DualPlane) - { - Indices *= 2; - } - - IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(MaxWeight); - - return IntEncoded.GetBitLength(Indices); - } - - public int GetNumWeightValues() - { - int Ret = Width * Height; - - if (DualPlane) - { - Ret *= 2; - } - - return Ret; - } - } - - public static byte[] DecodeToRGBA8888( - byte[] InputBuffer, - int BlockX, - int BlockY, - int BlockZ, - int X, - int Y, - int Z) - { - using (MemoryStream InputStream = new MemoryStream(InputBuffer)) - { - BinaryReader BinReader = new BinaryReader(InputStream); - - if (BlockX > 12 || BlockY > 12) - { - throw new ASTCDecoderException("Block size unsupported!"); - } - - if (BlockZ != 1 || Z != 1) - { - throw new ASTCDecoderException("3D compressed textures unsupported!"); - } - - using (MemoryStream OutputStream = new MemoryStream()) - { - int BlockIndex = 0; - - for (int j = 0; j < Y; j += BlockY) - { - for (int i = 0; i < X; i += BlockX) - { - int[] DecompressedData = new int[144]; - - DecompressBlock(BinReader.ReadBytes(0x10), DecompressedData, BlockX, BlockY); - - int DecompressedWidth = Math.Min(BlockX, X - i); - int DecompressedHeight = Math.Min(BlockY, Y - j); - int BaseOffsets = (j * X + i) * 4; - - for (int jj = 0; jj < DecompressedHeight; jj++) - { - OutputStream.Seek(BaseOffsets + jj * X * 4, SeekOrigin.Begin); - - byte[] OutputBuffer = new byte[DecompressedData.Length * sizeof(int)]; - Buffer.BlockCopy(DecompressedData, 0, OutputBuffer, 0, OutputBuffer.Length); - - OutputStream.Write(OutputBuffer, jj * BlockX * 4, DecompressedWidth * 4); - } - - BlockIndex++; - } - } - - return OutputStream.ToArray(); - } - } - } - - public static bool DecompressBlock( - byte[] InputBuffer, - int[] OutputBuffer, - int BlockWidth, - int BlockHeight) - { - BitArrayStream BitStream = new BitArrayStream(new BitArray(InputBuffer)); - TexelWeightParams TexelParams = DecodeBlockInfo(BitStream); - - if (TexelParams.Error) - { - throw new ASTCDecoderException("Invalid block mode"); - } - - if (TexelParams.VoidExtentLDR) - { - FillVoidExtentLDR(BitStream, OutputBuffer, BlockWidth, BlockHeight); - - return true; - } - - if (TexelParams.VoidExtentHDR) - { - throw new ASTCDecoderException("HDR void extent blocks are unsupported!"); - } - - if (TexelParams.Width > BlockWidth) - { - throw new ASTCDecoderException("Texel weight grid width should be smaller than block width"); - } - - if (TexelParams.Height > BlockHeight) - { - throw new ASTCDecoderException("Texel weight grid height should be smaller than block height"); - } - - // Read num partitions - int NumberPartitions = BitStream.ReadBits(2) + 1; - Debug.Assert(NumberPartitions <= 4); - - if (NumberPartitions == 4 && TexelParams.DualPlane) - { - throw new ASTCDecoderException("Dual plane mode is incompatible with four partition blocks"); - } - - // Based on the number of partitions, read the color endpoint mode for - // each partition. - - // Determine partitions, partition index, and color endpoint modes - int PlaneIndices = -1; - int PartitionIndex; - uint[] ColorEndpointMode = { 0, 0, 0, 0 }; - - BitArrayStream ColorEndpointStream = new BitArrayStream(new BitArray(16 * 8)); - - // Read extra config data... - uint BaseColorEndpointMode = 0; - - if (NumberPartitions == 1) - { - ColorEndpointMode[0] = (uint)BitStream.ReadBits(4); - PartitionIndex = 0; - } - else - { - PartitionIndex = BitStream.ReadBits(10); - BaseColorEndpointMode = (uint)BitStream.ReadBits(6); - } - - uint BaseMode = (BaseColorEndpointMode & 3); - - // Remaining bits are color endpoint data... - int NumberWeightBits = TexelParams.GetPackedBitSize(); - int RemainingBits = 128 - NumberWeightBits - BitStream.Position; - - // Consider extra bits prior to texel data... - uint ExtraColorEndpointModeBits = 0; - - if (BaseMode != 0) - { - switch (NumberPartitions) - { - case 2: ExtraColorEndpointModeBits += 2; break; - case 3: ExtraColorEndpointModeBits += 5; break; - case 4: ExtraColorEndpointModeBits += 8; break; - default: Debug.Assert(false); break; - } - } - - RemainingBits -= (int)ExtraColorEndpointModeBits; - - // Do we have a dual plane situation? - int PlaneSelectorBits = 0; - - if (TexelParams.DualPlane) - { - PlaneSelectorBits = 2; - } - - RemainingBits -= PlaneSelectorBits; - - // Read color data... - int ColorDataBits = RemainingBits; - - while (RemainingBits > 0) - { - int NumberBits = Math.Min(RemainingBits, 8); - int Bits = BitStream.ReadBits(NumberBits); - ColorEndpointStream.WriteBits(Bits, NumberBits); - RemainingBits -= 8; - } - - // Read the plane selection bits - PlaneIndices = BitStream.ReadBits(PlaneSelectorBits); - - // Read the rest of the CEM - if (BaseMode != 0) - { - uint ExtraColorEndpointMode = (uint)BitStream.ReadBits((int)ExtraColorEndpointModeBits); - uint TempColorEndpointMode = (ExtraColorEndpointMode << 6) | BaseColorEndpointMode; - TempColorEndpointMode >>= 2; - - bool[] C = new bool[4]; - - for (int i = 0; i < NumberPartitions; i++) - { - C[i] = (TempColorEndpointMode & 1) != 0; - TempColorEndpointMode >>= 1; - } - - byte[] M = new byte[4]; - - for (int i = 0; i < NumberPartitions; i++) - { - M[i] = (byte)(TempColorEndpointMode & 3); - TempColorEndpointMode >>= 2; - Debug.Assert(M[i] <= 3); - } - - for (int i = 0; i < NumberPartitions; i++) - { - ColorEndpointMode[i] = BaseMode; - if (!(C[i])) ColorEndpointMode[i] -= 1; - ColorEndpointMode[i] <<= 2; - ColorEndpointMode[i] |= M[i]; - } - } - else if (NumberPartitions > 1) - { - uint TempColorEndpointMode = BaseColorEndpointMode >> 2; - - for (uint i = 0; i < NumberPartitions; i++) - { - ColorEndpointMode[i] = TempColorEndpointMode; - } - } - - // Make sure everything up till here is sane. - for (int i = 0; i < NumberPartitions; i++) - { - Debug.Assert(ColorEndpointMode[i] < 16); - } - Debug.Assert(BitStream.Position + TexelParams.GetPackedBitSize() == 128); - - // Decode both color data and texel weight data - int[] ColorValues = new int[32]; // Four values * two endpoints * four maximum partitions - DecodeColorValues(ColorValues, ColorEndpointStream.ToByteArray(), ColorEndpointMode, NumberPartitions, ColorDataBits); - - ASTCPixel[][] EndPoints = new ASTCPixel[4][]; - EndPoints[0] = new ASTCPixel[2]; - EndPoints[1] = new ASTCPixel[2]; - EndPoints[2] = new ASTCPixel[2]; - EndPoints[3] = new ASTCPixel[2]; - - int ColorValuesPosition = 0; - - for (int i = 0; i < NumberPartitions; i++) - { - ComputeEndpoints(EndPoints[i], ColorValues, ColorEndpointMode[i], ref ColorValuesPosition); - } - - // Read the texel weight data. - byte[] TexelWeightData = (byte[])InputBuffer.Clone(); - - // Reverse everything - for (int i = 0; i < 8; i++) - { - byte a = ReverseByte(TexelWeightData[i]); - byte b = ReverseByte(TexelWeightData[15 - i]); - - TexelWeightData[i] = b; - TexelWeightData[15 - i] = a; - } - - // Make sure that higher non-texel bits are set to zero - int ClearByteStart = (TexelParams.GetPackedBitSize() >> 3) + 1; - TexelWeightData[ClearByteStart - 1] &= (byte)((1 << (TexelParams.GetPackedBitSize() % 8)) - 1); - - int cLen = 16 - ClearByteStart; - for (int i = ClearByteStart; i < ClearByteStart + cLen; i++) TexelWeightData[i] = 0; - - List TexelWeightValues = new List(); - BitArrayStream WeightBitStream = new BitArrayStream(new BitArray(TexelWeightData)); - - IntegerEncoded.DecodeIntegerSequence(TexelWeightValues, WeightBitStream, TexelParams.MaxWeight, TexelParams.GetNumWeightValues()); - - // Blocks can be at most 12x12, so we can have as many as 144 weights - int[][] Weights = new int[2][]; - Weights[0] = new int[144]; - Weights[1] = new int[144]; - - UnquantizeTexelWeights(Weights, TexelWeightValues, TexelParams, BlockWidth, BlockHeight); - - // Now that we have endpoints and weights, we can interpolate and generate - // the proper decoding... - for (int j = 0; j < BlockHeight; j++) - { - for (int i = 0; i < BlockWidth; i++) - { - int Partition = Select2DPartition(PartitionIndex, i, j, NumberPartitions, ((BlockHeight * BlockWidth) < 32)); - Debug.Assert(Partition < NumberPartitions); - - ASTCPixel Pixel = new ASTCPixel(0, 0, 0, 0); - for (int Component = 0; Component < 4; Component++) - { - int Component0 = EndPoints[Partition][0].GetComponent(Component); - Component0 = BitArrayStream.Replicate(Component0, 8, 16); - int Component1 = EndPoints[Partition][1].GetComponent(Component); - Component1 = BitArrayStream.Replicate(Component1, 8, 16); - - int Plane = 0; - - if (TexelParams.DualPlane && (((PlaneIndices + 1) & 3) == Component)) - { - Plane = 1; - } - - int Weight = Weights[Plane][j * BlockWidth + i]; - int FinalComponent = (Component0 * (64 - Weight) + Component1 * Weight + 32) / 64; - - if (FinalComponent == 65535) - { - Pixel.SetComponent(Component, 255); - } - else - { - double FinalComponentFloat = FinalComponent; - Pixel.SetComponent(Component, (int)(255.0 * (FinalComponentFloat / 65536.0) + 0.5)); - } - } - - OutputBuffer[j * BlockWidth + i] = Pixel.Pack(); - } - } - - return true; - } - - private static int Select2DPartition(int Seed, int X, int Y, int PartitionCount, bool IsSmallBlock) - { - return SelectPartition(Seed, X, Y, 0, PartitionCount, IsSmallBlock); - } - - private static int SelectPartition(int Seed, int X, int Y, int Z, int PartitionCount, bool IsSmallBlock) - { - if (PartitionCount == 1) - { - return 0; - } - - if (IsSmallBlock) - { - X <<= 1; - Y <<= 1; - Z <<= 1; - } - - Seed += (PartitionCount - 1) * 1024; - - int RightNum = Hash52((uint)Seed); - byte Seed01 = (byte)(RightNum & 0xF); - byte Seed02 = (byte)((RightNum >> 4) & 0xF); - byte Seed03 = (byte)((RightNum >> 8) & 0xF); - byte Seed04 = (byte)((RightNum >> 12) & 0xF); - byte Seed05 = (byte)((RightNum >> 16) & 0xF); - byte Seed06 = (byte)((RightNum >> 20) & 0xF); - byte Seed07 = (byte)((RightNum >> 24) & 0xF); - byte Seed08 = (byte)((RightNum >> 28) & 0xF); - byte Seed09 = (byte)((RightNum >> 18) & 0xF); - byte Seed10 = (byte)((RightNum >> 22) & 0xF); - byte Seed11 = (byte)((RightNum >> 26) & 0xF); - byte Seed12 = (byte)(((RightNum >> 30) | (RightNum << 2)) & 0xF); - - Seed01 *= Seed01; Seed02 *= Seed02; - Seed03 *= Seed03; Seed04 *= Seed04; - Seed05 *= Seed05; Seed06 *= Seed06; - Seed07 *= Seed07; Seed08 *= Seed08; - Seed09 *= Seed09; Seed10 *= Seed10; - Seed11 *= Seed11; Seed12 *= Seed12; - - int SeedHash1, SeedHash2, SeedHash3; - - if ((Seed & 1) != 0) - { - SeedHash1 = (Seed & 2) != 0 ? 4 : 5; - SeedHash2 = (PartitionCount == 3) ? 6 : 5; - } - else - { - SeedHash1 = (PartitionCount == 3) ? 6 : 5; - SeedHash2 = (Seed & 2) != 0 ? 4 : 5; - } - - SeedHash3 = (Seed & 0x10) != 0 ? SeedHash1 : SeedHash2; - - Seed01 >>= SeedHash1; Seed02 >>= SeedHash2; Seed03 >>= SeedHash1; Seed04 >>= SeedHash2; - Seed05 >>= SeedHash1; Seed06 >>= SeedHash2; Seed07 >>= SeedHash1; Seed08 >>= SeedHash2; - Seed09 >>= SeedHash3; Seed10 >>= SeedHash3; Seed11 >>= SeedHash3; Seed12 >>= SeedHash3; - - int a = Seed01 * X + Seed02 * Y + Seed11 * Z + (RightNum >> 14); - int b = Seed03 * X + Seed04 * Y + Seed12 * Z + (RightNum >> 10); - int c = Seed05 * X + Seed06 * Y + Seed09 * Z + (RightNum >> 6); - int d = Seed07 * X + Seed08 * Y + Seed10 * Z + (RightNum >> 2); - - a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F; - - if (PartitionCount < 4) d = 0; - if (PartitionCount < 3) c = 0; - - if (a >= b && a >= c && a >= d) return 0; - else if (b >= c && b >= d) return 1; - else if (c >= d) return 2; - return 3; - } - - static int Hash52(uint Val) - { - Val ^= Val >> 15; Val -= Val << 17; Val += Val << 7; Val += Val << 4; - Val ^= Val >> 5; Val += Val << 16; Val ^= Val >> 7; Val ^= Val >> 3; - Val ^= Val << 6; Val ^= Val >> 17; - - return (int)Val; - } - - static void UnquantizeTexelWeights( - int[][] OutputBuffer, - List Weights, - TexelWeightParams TexelParams, - int BlockWidth, - int BlockHeight) - { - int WeightIndices = 0; - int[][] Unquantized = new int[2][]; - Unquantized[0] = new int[144]; - Unquantized[1] = new int[144]; - - for (int i = 0; i < Weights.Count; i++) - { - Unquantized[0][WeightIndices] = UnquantizeTexelWeight(Weights[i]); - - if (TexelParams.DualPlane) - { - i++; - Unquantized[1][WeightIndices] = UnquantizeTexelWeight(Weights[i]); - - if (i == Weights.Count) - { - break; - } - } - - if (++WeightIndices >= (TexelParams.Width * TexelParams.Height)) break; - } - - // Do infill if necessary (Section C.2.18) ... - int Ds = (1024 + (BlockWidth / 2)) / (BlockWidth - 1); - int Dt = (1024 + (BlockHeight / 2)) / (BlockHeight - 1); - - int PlaneScale = TexelParams.DualPlane ? 2 : 1; - - for (int Plane = 0; Plane < PlaneScale; Plane++) - { - for (int t = 0; t < BlockHeight; t++) - { - for (int s = 0; s < BlockWidth; s++) - { - int cs = Ds * s; - int ct = Dt * t; - - int gs = (cs * (TexelParams.Width - 1) + 32) >> 6; - int gt = (ct * (TexelParams.Height - 1) + 32) >> 6; - - int js = gs >> 4; - int fs = gs & 0xF; - - int jt = gt >> 4; - int ft = gt & 0x0F; - - int w11 = (fs * ft + 8) >> 4; - int w10 = ft - w11; - int w01 = fs - w11; - int w00 = 16 - fs - ft + w11; - - int v0 = js + jt * TexelParams.Width; - - int p00 = 0; - int p01 = 0; - int p10 = 0; - int p11 = 0; - - if (v0 < (TexelParams.Width * TexelParams.Height)) - { - p00 = Unquantized[Plane][v0]; - } - - if (v0 + 1 < (TexelParams.Width * TexelParams.Height)) - { - p01 = Unquantized[Plane][v0 + 1]; - } - - if (v0 + TexelParams.Width < (TexelParams.Width * TexelParams.Height)) - { - p10 = Unquantized[Plane][v0 + TexelParams.Width]; - } - - if (v0 + TexelParams.Width + 1 < (TexelParams.Width * TexelParams.Height)) - { - p11 = Unquantized[Plane][v0 + TexelParams.Width + 1]; - } - - OutputBuffer[Plane][t * BlockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4; - } - } - } - } - - static int UnquantizeTexelWeight(IntegerEncoded IntEncoded) - { - int BitValue = IntEncoded.BitValue; - int BitLength = IntEncoded.NumberBits; - - int A = BitArrayStream.Replicate(BitValue & 1, 1, 7); - int B = 0, C = 0, D = 0; - - int Result = 0; - - switch (IntEncoded.GetEncoding()) - { - case IntegerEncoded.EIntegerEncoding.JustBits: - Result = BitArrayStream.Replicate(BitValue, BitLength, 6); - break; - - case IntegerEncoded.EIntegerEncoding.Trit: - { - D = IntEncoded.TritValue; - Debug.Assert(D < 3); - - switch (BitLength) - { - case 0: - { - int[] Results = { 0, 32, 63 }; - Result = Results[D]; - - break; - } - - case 1: - { - C = 50; - break; - } - - case 2: - { - C = 23; - int b = (BitValue >> 1) & 1; - B = (b << 6) | (b << 2) | b; - - break; - } - - case 3: - { - C = 11; - int cb = (BitValue >> 1) & 3; - B = (cb << 5) | cb; - - break; - } - - default: - throw new ASTCDecoderException("Invalid trit encoding for texel weight"); - } - - break; - } - - case IntegerEncoded.EIntegerEncoding.Quint: - { - D = IntEncoded.QuintValue; - Debug.Assert(D < 5); - - switch (BitLength) - { - case 0: - { - int[] Results = { 0, 16, 32, 47, 63 }; - Result = Results[D]; - - break; - } - - case 1: - { - C = 28; - - break; - } - - case 2: - { - C = 13; - int b = (BitValue >> 1) & 1; - B = (b << 6) | (b << 1); - - break; - } - - default: - throw new ASTCDecoderException("Invalid quint encoding for texel weight"); - } - - break; - } - } - - if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && BitLength > 0) - { - // Decode the value... - Result = D * C + B; - Result ^= A; - Result = (A & 0x20) | (Result >> 2); - } - - Debug.Assert(Result < 64); - - // Change from [0,63] to [0,64] - if (Result > 32) - { - Result += 1; - } - - return Result; - } - - static byte ReverseByte(byte b) - { - // Taken from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits - return (byte)((((b) * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32); - } - - static uint[] ReadUintColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) - { - uint[] Ret = new uint[Number]; - - for (int i = 0; i < Number; i++) - { - Ret[i] = (uint)ColorValues[ColorValuesPosition++]; - } - - return Ret; - } - - static int[] ReadIntColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) - { - int[] Ret = new int[Number]; - - for (int i = 0; i < Number; i++) - { - Ret[i] = ColorValues[ColorValuesPosition++]; - } - - return Ret; - } - - static void ComputeEndpoints( - ASTCPixel[] EndPoints, - int[] ColorValues, - uint ColorEndpointMode, - ref int ColorValuesPosition) - { - switch (ColorEndpointMode) - { - case 0: - { - uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[0], (short)Val[0]); - EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[1], (short)Val[1]); - - break; - } - - - case 1: - { - uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); - int L0 = (int)((Val[0] >> 2) | (Val[1] & 0xC0)); - int L1 = (int)Math.Max(L0 + (Val[1] & 0x3F), 0xFFU); - - EndPoints[0] = new ASTCPixel(0xFF, (short)L0, (short)L0, (short)L0); - EndPoints[1] = new ASTCPixel(0xFF, (short)L1, (short)L1, (short)L1); - - break; - } - - case 4: - { - uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); - EndPoints[1] = new ASTCPixel((short)Val[3], (short)Val[1], (short)Val[1], (short)Val[1]); - - break; - } - - case 5: - { - int[] Val = ReadIntColorValues(4, ColorValues, ref ColorValuesPosition); - - BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); - BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); - - EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); - EndPoints[1] = new ASTCPixel((short)(Val[2] + Val[3]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1])); - - EndPoints[0].ClampByte(); - EndPoints[1].ClampByte(); - - break; - } - - case 6: - { - uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel(0xFF, (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); - EndPoints[1] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[1], (short)Val[2]); - - break; - } - - case 8: - { - uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); - - if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) - { - EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); - EndPoints[1] = ASTCPixel.BlueContract(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); - } - - break; - } - - case 9: - { - int[] Val = ReadIntColorValues(6, ColorValues, ref ColorValuesPosition); - - BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); - BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); - BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); - - if (Val[1] + Val[3] + Val[5] >= 0) - { - EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel(0xFF, (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract(0xFF, Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); - EndPoints[1] = ASTCPixel.BlueContract(0xFF, Val[0], Val[2], Val[4]); - } - - EndPoints[0].ClampByte(); - EndPoints[1].ClampByte(); - - break; - } - - case 10: - { - uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); - - EndPoints[0] = new ASTCPixel((short)Val[4], (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); - EndPoints[1] = new ASTCPixel((short)Val[5], (short)Val[0], (short)Val[1], (short)Val[2]); - - break; - } - - case 12: - { - uint[] Val = ReadUintColorValues(8, ColorValues, ref ColorValuesPosition); - - if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) - { - EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); - EndPoints[1] = ASTCPixel.BlueContract((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); - } - - break; - } - - case 13: - { - int[] Val = ReadIntColorValues(8, ColorValues, ref ColorValuesPosition); - - BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); - BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); - BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); - BitArrayStream.BitTransferSigned(ref Val[7], ref Val[6]); - - if (Val[1] + Val[3] + Val[5] >= 0) - { - EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); - EndPoints[1] = new ASTCPixel((short)(Val[7] + Val[6]), (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); - } - else - { - EndPoints[0] = ASTCPixel.BlueContract(Val[6] + Val[7], Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); - EndPoints[1] = ASTCPixel.BlueContract(Val[6], Val[0], Val[2], Val[4]); - } - - EndPoints[0].ClampByte(); - EndPoints[1].ClampByte(); - - break; - } - - default: - throw new ASTCDecoderException("Unsupported color endpoint mode (is it HDR?)"); - } - } - - static void DecodeColorValues( - int[] OutputValues, - byte[] InputData, - uint[] Modes, - int NumberPartitions, - int NumberBitsForColorData) - { - // First figure out how many color values we have - int NumberValues = 0; - - for (int i = 0; i < NumberPartitions; i++) - { - NumberValues += (int)((Modes[i] >> 2) + 1) << 1; - } - - // Then based on the number of values and the remaining number of bits, - // figure out the max value for each of them... - int Range = 256; - - while (--Range > 0) - { - IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(Range); - int BitLength = IntEncoded.GetBitLength(NumberValues); - - if (BitLength <= NumberBitsForColorData) - { - // Find the smallest possible range that matches the given encoding - while (--Range > 0) - { - IntegerEncoded NewIntEncoded = IntegerEncoded.CreateEncoding(Range); - if (!NewIntEncoded.MatchesEncoding(IntEncoded)) - { - break; - } - } - - // Return to last matching range. - Range++; - break; - } - } - - // We now have enough to decode our integer sequence. - List IntegerEncodedSequence = new List(); - BitArrayStream ColorBitStream = new BitArrayStream(new BitArray(InputData)); - - IntegerEncoded.DecodeIntegerSequence(IntegerEncodedSequence, ColorBitStream, Range, NumberValues); - - // Once we have the decoded values, we need to dequantize them to the 0-255 range - // This procedure is outlined in ASTC spec C.2.13 - int OutputIndices = 0; - - foreach (IntegerEncoded IntEncoded in IntegerEncodedSequence) - { - int BitLength = IntEncoded.NumberBits; - int BitValue = IntEncoded.BitValue; - - Debug.Assert(BitLength >= 1); - - int A = 0, B = 0, C = 0, D = 0; - // A is just the lsb replicated 9 times. - A = BitArrayStream.Replicate(BitValue & 1, 1, 9); - - switch (IntEncoded.GetEncoding()) - { - case IntegerEncoded.EIntegerEncoding.JustBits: - { - OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8); - - break; - } - - case IntegerEncoded.EIntegerEncoding.Trit: - { - D = IntEncoded.TritValue; - - switch (BitLength) - { - case 1: - { - C = 204; - - break; - } - - case 2: - { - C = 93; - // B = b000b0bb0 - int b = (BitValue >> 1) & 1; - B = (b << 8) | (b << 4) | (b << 2) | (b << 1); - - break; - } - - case 3: - { - C = 44; - // B = cb000cbcb - int cb = (BitValue >> 1) & 3; - B = (cb << 7) | (cb << 2) | cb; - - break; - } - - - case 4: - { - C = 22; - // B = dcb000dcb - int dcb = (BitValue >> 1) & 7; - B = (dcb << 6) | dcb; - - break; - } - - case 5: - { - C = 11; - // B = edcb000ed - int edcb = (BitValue >> 1) & 0xF; - B = (edcb << 5) | (edcb >> 2); - - break; - } - - case 6: - { - C = 5; - // B = fedcb000f - int fedcb = (BitValue >> 1) & 0x1F; - B = (fedcb << 4) | (fedcb >> 4); - - break; - } - - default: - throw new ASTCDecoderException("Unsupported trit encoding for color values!"); - } - - break; - } - - case IntegerEncoded.EIntegerEncoding.Quint: - { - D = IntEncoded.QuintValue; - - switch (BitLength) - { - case 1: - { - C = 113; - - break; - } - - case 2: - { - C = 54; - // B = b0000bb00 - int b = (BitValue >> 1) & 1; - B = (b << 8) | (b << 3) | (b << 2); - - break; - } - - case 3: - { - C = 26; - // B = cb0000cbc - int cb = (BitValue >> 1) & 3; - B = (cb << 7) | (cb << 1) | (cb >> 1); - - break; - } - - case 4: - { - C = 13; - // B = dcb0000dc - int dcb = (BitValue >> 1) & 7; - B = (dcb << 6) | (dcb >> 1); - - break; - } - - case 5: - { - C = 6; - // B = edcb0000e - int edcb = (BitValue >> 1) & 0xF; - B = (edcb << 5) | (edcb >> 3); - - break; - } - - default: - throw new ASTCDecoderException("Unsupported quint encoding for color values!"); - } - break; - } - } - - if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits) - { - int T = D * C + B; - T ^= A; - T = (A & 0x80) | (T >> 2); - - OutputValues[OutputIndices++] = T; - } - } - - // Make sure that each of our values is in the proper range... - for (int i = 0; i < NumberValues; i++) - { - Debug.Assert(OutputValues[i] <= 255); - } - } - - static void FillVoidExtentLDR(BitArrayStream BitStream, int[] OutputBuffer, int BlockWidth, int BlockHeight) - { - // Don't actually care about the void extent, just read the bits... - for (int i = 0; i < 4; ++i) - { - BitStream.ReadBits(13); - } - - // Decode the RGBA components and renormalize them to the range [0, 255] - ushort R = (ushort)BitStream.ReadBits(16); - ushort G = (ushort)BitStream.ReadBits(16); - ushort B = (ushort)BitStream.ReadBits(16); - ushort A = (ushort)BitStream.ReadBits(16); - - int RGBA = (R >> 8) | (G & 0xFF00) | ((B) & 0xFF00) << 8 | ((A) & 0xFF00) << 16; - - for (int j = 0; j < BlockHeight; j++) - { - for (int i = 0; i < BlockWidth; i++) - { - OutputBuffer[j * BlockWidth + i] = RGBA; - } - } - } - - static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream) - { - TexelWeightParams TexelParams = new TexelWeightParams(); - - // Read the entire block mode all at once - ushort ModeBits = (ushort)BitStream.ReadBits(11); - - // Does this match the void extent block mode? - if ((ModeBits & 0x01FF) == 0x1FC) - { - if ((ModeBits & 0x200) != 0) - { - TexelParams.VoidExtentHDR = true; - } - else - { - TexelParams.VoidExtentLDR = true; - } - - // Next two bits must be one. - if ((ModeBits & 0x400) == 0 || BitStream.ReadBits(1) == 0) - { - TexelParams.Error = true; - } - - return TexelParams; - } - - // First check if the last four bits are zero - if ((ModeBits & 0xF) == 0) - { - TexelParams.Error = true; - return TexelParams; - } - - // If the last two bits are zero, then if bits - // [6-8] are all ones, this is also reserved. - if ((ModeBits & 0x3) == 0 && (ModeBits & 0x1C0) == 0x1C0) - { - TexelParams.Error = true; - - return TexelParams; - } - - // Otherwise, there is no error... Figure out the layout - // of the block mode. Layout is determined by a number - // between 0 and 9 corresponding to table C.2.8 of the - // ASTC spec. - int Layout = 0; - - if ((ModeBits & 0x1) != 0 || (ModeBits & 0x2) != 0) - { - // layout is in [0-4] - if ((ModeBits & 0x8) != 0) - { - // layout is in [2-4] - if ((ModeBits & 0x4) != 0) - { - // layout is in [3-4] - if ((ModeBits & 0x100) != 0) - { - Layout = 4; - } - else - { - Layout = 3; - } - } - else - { - Layout = 2; - } - } - else - { - // layout is in [0-1] - if ((ModeBits & 0x4) != 0) - { - Layout = 1; - } - else - { - Layout = 0; - } - } - } - else - { - // layout is in [5-9] - if ((ModeBits & 0x100) != 0) - { - // layout is in [7-9] - if ((ModeBits & 0x80) != 0) - { - // layout is in [7-8] - Debug.Assert((ModeBits & 0x40) == 0); - - if ((ModeBits & 0x20) != 0) - { - Layout = 8; - } - else - { - Layout = 7; - } - } - else - { - Layout = 9; - } - } - else - { - // layout is in [5-6] - if ((ModeBits & 0x80) != 0) - { - Layout = 6; - } - else - { - Layout = 5; - } - } - } - - Debug.Assert(Layout < 10); - - // Determine R - int R = (ModeBits >> 4) & 1; - if (Layout < 5) - { - R |= (ModeBits & 0x3) << 1; - } - else - { - R |= (ModeBits & 0xC) >> 1; - } - - Debug.Assert(2 <= R && R <= 7); - - // Determine width & height - switch (Layout) - { - case 0: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x3; - - TexelParams.Width = B + 4; - TexelParams.Height = A + 2; - - break; - } - - case 1: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x3; - - TexelParams.Width = B + 8; - TexelParams.Height = A + 2; - - break; - } - - case 2: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x3; - - TexelParams.Width = A + 2; - TexelParams.Height = B + 8; - - break; - } - - case 3: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x1; - - TexelParams.Width = A + 2; - TexelParams.Height = B + 6; - - break; - } - - case 4: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 7) & 0x1; - - TexelParams.Width = B + 2; - TexelParams.Height = A + 2; - - break; - } - - case 5: - { - int A = (ModeBits >> 5) & 0x3; - - TexelParams.Width = 12; - TexelParams.Height = A + 2; - - break; - } - - case 6: - { - int A = (ModeBits >> 5) & 0x3; - - TexelParams.Width = A + 2; - TexelParams.Height = 12; - - break; - } - - case 7: - { - TexelParams.Width = 6; - TexelParams.Height = 10; - - break; - } - - case 8: - { - TexelParams.Width = 10; - TexelParams.Height = 6; - break; - } - - case 9: - { - int A = (ModeBits >> 5) & 0x3; - int B = (ModeBits >> 9) & 0x3; - - TexelParams.Width = A + 6; - TexelParams.Height = B + 6; - - break; - } - - default: - //Don't know this layout... - TexelParams.Error = true; - break; - } - - // Determine whether or not we're using dual planes - // and/or high precision layouts. - bool D = ((Layout != 9) && ((ModeBits & 0x400) != 0)); - bool H = (Layout != 9) && ((ModeBits & 0x200) != 0); - - if (H) - { - int[] MaxWeights = { 9, 11, 15, 19, 23, 31 }; - TexelParams.MaxWeight = MaxWeights[R - 2]; - } - else - { - int[] MaxWeights = { 1, 2, 3, 4, 5, 7 }; - TexelParams.MaxWeight = MaxWeights[R - 2]; - } - - TexelParams.DualPlane = D; - - return TexelParams; - } - } -} diff --git a/Ryujinx.Graphics/Gal/Texture/ASTCPixel.cs b/Ryujinx.Graphics/Gal/Texture/ASTCPixel.cs deleted file mode 100644 index 4a299818..00000000 --- a/Ryujinx.Graphics/Gal/Texture/ASTCPixel.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Diagnostics; - -namespace Ryujinx.Graphics.Gal.Texture -{ - class ASTCPixel - { - public short R { get; set; } - public short G { get; set; } - public short B { get; set; } - public short A { get; set; } - - byte[] BitDepth = new byte[4]; - - public ASTCPixel(short _A, short _R, short _G, short _B) - { - A = _A; - R = _R; - G = _G; - B = _B; - - for (int i = 0; i < 4; i++) - BitDepth[i] = 8; - } - - public void ClampByte() - { - R = Math.Min(Math.Max(R, (short)0), (short)255); - G = Math.Min(Math.Max(G, (short)0), (short)255); - B = Math.Min(Math.Max(B, (short)0), (short)255); - A = Math.Min(Math.Max(A, (short)0), (short)255); - } - - public short GetComponent(int Index) - { - switch(Index) - { - case 0: return A; - case 1: return R; - case 2: return G; - case 3: return B; - } - - return 0; - } - - public void SetComponent(int Index, int Value) - { - switch (Index) - { - case 0: - A = (short)Value; - break; - case 1: - R = (short)Value; - break; - case 2: - G = (short)Value; - break; - case 3: - B = (short)Value; - break; - } - } - - public void ChangeBitDepth(byte[] Depth) - { - for(int i = 0; i< 4; i++) - { - int Value = ChangeBitDepth(GetComponent(i), BitDepth[i], Depth[i]); - - SetComponent(i, Value); - BitDepth[i] = Depth[i]; - } - } - - short ChangeBitDepth(short Value, byte OldDepth, byte NewDepth) - { - Debug.Assert(NewDepth <= 8); - Debug.Assert(OldDepth <= 8); - - if (OldDepth == NewDepth) - { - // Do nothing - return Value; - } - else if (OldDepth == 0 && NewDepth != 0) - { - return (short)((1 << NewDepth) - 1); - } - else if (NewDepth > OldDepth) - { - return (short)BitArrayStream.Replicate(Value, OldDepth, NewDepth); - } - else - { - // oldDepth > newDepth - if (NewDepth == 0) - { - return 0xFF; - } - else - { - byte BitsWasted = (byte)(OldDepth - NewDepth); - short TempValue = Value; - - TempValue = (short)((TempValue + (1 << (BitsWasted - 1))) >> BitsWasted); - TempValue = Math.Min(Math.Max((short)0, TempValue), (short)((1 << NewDepth) - 1)); - - return (byte)(TempValue); - } - } - } - - public int Pack() - { - ASTCPixel NewPixel = new ASTCPixel(A, R, G, B); - byte[] eightBitDepth = { 8, 8, 8, 8 }; - - NewPixel.ChangeBitDepth(eightBitDepth); - - return (byte)NewPixel.A << 24 | - (byte)NewPixel.B << 16 | - (byte)NewPixel.G << 8 | - (byte)NewPixel.R << 0; - } - - // Adds more precision to the blue channel as described - // in C.2.14 - public static ASTCPixel BlueContract(int a, int r, int g, int b) - { - return new ASTCPixel((short)(a), - (short)((r + b) >> 1), - (short)((g + b) >> 1), - (short)(b)); - } - } -} diff --git a/Ryujinx.Graphics/Gal/Texture/BitArrayStream.cs b/Ryujinx.Graphics/Gal/Texture/BitArrayStream.cs deleted file mode 100644 index eb2204c4..00000000 --- a/Ryujinx.Graphics/Gal/Texture/BitArrayStream.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System; -using System.Collections; - -namespace Ryujinx.Graphics.Gal.Texture -{ - public class BitArrayStream - { - public BitArray BitsArray; - public int Position { get; private set; } - - public BitArrayStream(BitArray BitArray) - { - BitsArray = BitArray; - Position = 0; - } - - public short ReadBits(int Length) - { - int RetValue = 0; - for (int i = Position; i < Position + Length; i++) - { - if (BitsArray[i]) - { - RetValue |= 1 << (i - Position); - } - } - - Position += Length; - return (short)RetValue; - } - - public int ReadBits(int Start, int End) - { - int RetValue = 0; - for (int i = Start; i <= End; i++) - { - if (BitsArray[i]) - { - RetValue |= 1 << (i - Start); - } - } - - return RetValue; - } - - public int ReadBit(int Index) - { - return Convert.ToInt32(BitsArray[Index]); - } - - public void WriteBits(int Value, int Length) - { - for (int i = Position; i < Position + Length; i++) - { - BitsArray[i] = ((Value >> (i - Position)) & 1) != 0; - } - - Position += Length; - } - - public byte[] ToByteArray() - { - byte[] RetArray = new byte[(BitsArray.Length + 7) / 8]; - BitsArray.CopyTo(RetArray, 0); - return RetArray; - } - - public static int Replicate(int Value, int NumberBits, int ToBit) - { - if (NumberBits == 0) return 0; - if (ToBit == 0) return 0; - - int TempValue = Value & ((1 << NumberBits) - 1); - int RetValue = TempValue; - int ResLength = NumberBits; - - while (ResLength < ToBit) - { - int Comp = 0; - if (NumberBits > ToBit - ResLength) - { - int NewShift = ToBit - ResLength; - Comp = NumberBits - NewShift; - NumberBits = NewShift; - } - RetValue <<= NumberBits; - RetValue |= TempValue >> Comp; - ResLength += NumberBits; - } - return RetValue; - } - - public static int PopCnt(int Number) - { - int Counter; - for (Counter = 0; Number != 0; Counter++) - { - Number &= Number - 1; - } - return Counter; - } - - public static void Swap(ref T lhs, ref T rhs) - { - T Temp = lhs; - lhs = rhs; - rhs = Temp; - } - - // Transfers a bit as described in C.2.14 - public static void BitTransferSigned(ref int a, ref int b) - { - b >>= 1; - b |= a & 0x80; - a >>= 1; - a &= 0x3F; - if ((a & 0x20) != 0) a -= 0x40; - } - } -} diff --git a/Ryujinx.Graphics/Gal/Texture/IntegerEncoded.cs b/Ryujinx.Graphics/Gal/Texture/IntegerEncoded.cs deleted file mode 100644 index 0adabe17..00000000 --- a/Ryujinx.Graphics/Gal/Texture/IntegerEncoded.cs +++ /dev/null @@ -1,269 +0,0 @@ -using System.Collections; -using System.Collections.Generic; - -namespace Ryujinx.Graphics.Gal.Texture -{ - public struct IntegerEncoded - { - public enum EIntegerEncoding - { - JustBits, - Quint, - Trit - } - - EIntegerEncoding Encoding; - public int NumberBits { get; private set; } - public int BitValue { get; private set; } - public int TritValue { get; private set; } - public int QuintValue { get; private set; } - - public IntegerEncoded(EIntegerEncoding _Encoding, int NumBits) - { - Encoding = _Encoding; - NumberBits = NumBits; - BitValue = 0; - TritValue = 0; - QuintValue = 0; - } - - public bool MatchesEncoding(IntegerEncoded Other) - { - return Encoding == Other.Encoding && NumberBits == Other.NumberBits; - } - - public EIntegerEncoding GetEncoding() - { - return Encoding; - } - - public int GetBitLength(int NumberVals) - { - int TotalBits = NumberBits * NumberVals; - if (Encoding == EIntegerEncoding.Trit) - { - TotalBits += (NumberVals * 8 + 4) / 5; - } - else if (Encoding == EIntegerEncoding.Quint) - { - TotalBits += (NumberVals * 7 + 2) / 3; - } - return TotalBits; - } - - public static IntegerEncoded CreateEncoding(int MaxVal) - { - while (MaxVal > 0) - { - int Check = MaxVal + 1; - - // Is maxVal a power of two? - if ((Check & (Check - 1)) == 0) - { - return new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(MaxVal)); - } - - // Is maxVal of the type 3*2^n - 1? - if ((Check % 3 == 0) && ((Check / 3) & ((Check / 3) - 1)) == 0) - { - return new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(Check / 3 - 1)); - } - - // Is maxVal of the type 5*2^n - 1? - if ((Check % 5 == 0) && ((Check / 5) & ((Check / 5) - 1)) == 0) - { - return new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(Check / 5 - 1)); - } - - // Apparently it can't be represented with a bounded integer sequence... - // just iterate. - MaxVal--; - } - - return new IntegerEncoded(EIntegerEncoding.JustBits, 0); - } - - public static void DecodeTritBlock( - BitArrayStream BitStream, - List ListIntegerEncoded, - int NumberBitsPerValue) - { - // Implement the algorithm in section C.2.12 - int[] m = new int[5]; - int[] t = new int[5]; - int T; - - // Read the trit encoded block according to - // table C.2.14 - m[0] = BitStream.ReadBits(NumberBitsPerValue); - T = BitStream.ReadBits(2); - m[1] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(2) << 2; - m[2] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(1) << 4; - m[3] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(2) << 5; - m[4] = BitStream.ReadBits(NumberBitsPerValue); - T |= BitStream.ReadBits(1) << 7; - - int C = 0; - - BitArrayStream Tb = new BitArrayStream(new BitArray(new int[] { T })); - if (Tb.ReadBits(2, 4) == 7) - { - C = (Tb.ReadBits(5, 7) << 2) | Tb.ReadBits(0, 1); - t[4] = t[3] = 2; - } - else - { - C = Tb.ReadBits(0, 4); - if (Tb.ReadBits(5, 6) == 3) - { - t[4] = 2; - t[3] = Tb.ReadBit(7); - } - else - { - t[4] = Tb.ReadBit(7); - t[3] = Tb.ReadBits(5, 6); - } - } - - BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); - if (Cb.ReadBits(0, 1) == 3) - { - t[2] = 2; - t[1] = Cb.ReadBit(4); - t[0] = (Cb.ReadBit(3) << 1) | (Cb.ReadBit(2) & ~Cb.ReadBit(3)); - } - else if (Cb.ReadBits(2, 3) == 3) - { - t[2] = 2; - t[1] = 2; - t[0] = Cb.ReadBits(0, 1); - } - else - { - t[2] = Cb.ReadBit(4); - t[1] = Cb.ReadBits(2, 3); - t[0] = (Cb.ReadBit(1) << 1) | (Cb.ReadBit(0) & ~Cb.ReadBit(1)); - } - - for (int i = 0; i < 5; i++) - { - IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Trit, NumberBitsPerValue) - { - BitValue = m[i], - TritValue = t[i] - }; - ListIntegerEncoded.Add(IntEncoded); - } - } - - public static void DecodeQuintBlock( - BitArrayStream BitStream, - List ListIntegerEncoded, - int NumberBitsPerValue) - { - // Implement the algorithm in section C.2.12 - int[] m = new int[3]; - int[] q = new int[3]; - int Q; - - // Read the trit encoded block according to - // table C.2.15 - m[0] = BitStream.ReadBits(NumberBitsPerValue); - Q = BitStream.ReadBits(3); - m[1] = BitStream.ReadBits(NumberBitsPerValue); - Q |= BitStream.ReadBits(2) << 3; - m[2] = BitStream.ReadBits(NumberBitsPerValue); - Q |= BitStream.ReadBits(2) << 5; - - BitArrayStream Qb = new BitArrayStream(new BitArray(new int[] { Q })); - if (Qb.ReadBits(1, 2) == 3 && Qb.ReadBits(5, 6) == 0) - { - q[0] = q[1] = 4; - q[2] = (Qb.ReadBit(0) << 2) | ((Qb.ReadBit(4) & ~Qb.ReadBit(0)) << 1) | (Qb.ReadBit(3) & ~Qb.ReadBit(0)); - } - else - { - int C = 0; - if (Qb.ReadBits(1, 2) == 3) - { - q[2] = 4; - C = (Qb.ReadBits(3, 4) << 3) | ((~Qb.ReadBits(5, 6) & 3) << 1) | Qb.ReadBit(0); - } - else - { - q[2] = Qb.ReadBits(5, 6); - C = Qb.ReadBits(0, 4); - } - - BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); - if (Cb.ReadBits(0, 2) == 5) - { - q[1] = 4; - q[0] = Cb.ReadBits(3, 4); - } - else - { - q[1] = Cb.ReadBits(3, 4); - q[0] = Cb.ReadBits(0, 2); - } - } - - for (int i = 0; i < 3; i++) - { - IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Quint, NumberBitsPerValue) - { - BitValue = m[i], - QuintValue = q[i] - }; - ListIntegerEncoded.Add(IntEncoded); - } - } - - public static void DecodeIntegerSequence( - List DecodeIntegerSequence, - BitArrayStream BitStream, - int MaxRange, - int NumberValues) - { - // Determine encoding parameters - IntegerEncoded IntEncoded = CreateEncoding(MaxRange); - - // Start decoding - int NumberValuesDecoded = 0; - while (NumberValuesDecoded < NumberValues) - { - switch (IntEncoded.GetEncoding()) - { - case EIntegerEncoding.Quint: - { - DecodeQuintBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); - NumberValuesDecoded += 3; - - break; - } - - case EIntegerEncoding.Trit: - { - DecodeTritBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); - NumberValuesDecoded += 5; - - break; - } - - case EIntegerEncoding.JustBits: - { - IntEncoded.BitValue = BitStream.ReadBits(IntEncoded.NumberBits); - DecodeIntegerSequence.Add(IntEncoded); - NumberValuesDecoded++; - - break; - } - } - } - } - } -} diff --git a/Ryujinx.Graphics/INvGpuEngine.cs b/Ryujinx.Graphics/INvGpuEngine.cs new file mode 100644 index 00000000..810303b9 --- /dev/null +++ b/Ryujinx.Graphics/INvGpuEngine.cs @@ -0,0 +1,11 @@ +using Ryujinx.Graphics.Memory; + +namespace Ryujinx.Graphics +{ + interface INvGpuEngine + { + int[] Registers { get; } + + void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/MacroInterpreter.cs b/Ryujinx.Graphics/MacroInterpreter.cs new file mode 100644 index 00000000..20e7895b --- /dev/null +++ b/Ryujinx.Graphics/MacroInterpreter.cs @@ -0,0 +1,418 @@ +using Ryujinx.Graphics.Memory; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics +{ + class MacroInterpreter + { + private enum AssignmentOperation + { + IgnoreAndFetch = 0, + Move = 1, + MoveAndSetMaddr = 2, + FetchAndSend = 3, + MoveAndSend = 4, + FetchAndSetMaddr = 5, + MoveAndSetMaddrThenFetchAndSend = 6, + MoveAndSetMaddrThenSendHigh = 7 + } + + private enum AluOperation + { + AluReg = 0, + AddImmediate = 1, + BitfieldReplace = 2, + BitfieldExtractLslImm = 3, + BitfieldExtractLslReg = 4, + ReadImmediate = 5 + } + + private enum AluRegOperation + { + Add = 0, + AddWithCarry = 1, + Subtract = 2, + SubtractWithBorrow = 3, + BitwiseExclusiveOr = 8, + BitwiseOr = 9, + BitwiseAnd = 10, + BitwiseAndNot = 11, + BitwiseNotAnd = 12 + } + + private NvGpuFifo PFifo; + private INvGpuEngine Engine; + + public Queue Fifo { get; private set; } + + private int[] Gprs; + + private int MethAddr; + private int MethIncr; + + private bool Carry; + + private int OpCode; + + private int PipeOp; + + private int Pc; + + public MacroInterpreter(NvGpuFifo PFifo, INvGpuEngine Engine) + { + this.PFifo = PFifo; + this.Engine = Engine; + + Fifo = new Queue(); + + Gprs = new int[8]; + } + + public void Execute(NvGpuVmm Vmm, int[] Mme, int Position, int Param) + { + Reset(); + + Gprs[1] = Param; + + Pc = Position; + + FetchOpCode(Mme); + + while (Step(Vmm, Mme)); + + //Due to the delay slot, we still need to execute + //one more instruction before we actually exit. + Step(Vmm, Mme); + } + + private void Reset() + { + for (int Index = 0; Index < Gprs.Length; Index++) + { + Gprs[Index] = 0; + } + + MethAddr = 0; + MethIncr = 0; + + Carry = false; + } + + private bool Step(NvGpuVmm Vmm, int[] Mme) + { + int BaseAddr = Pc - 1; + + FetchOpCode(Mme); + + if ((OpCode & 7) < 7) + { + //Operation produces a value. + AssignmentOperation AsgOp = (AssignmentOperation)((OpCode >> 4) & 7); + + int Result = GetAluResult(); + + switch (AsgOp) + { + //Fetch parameter and ignore result. + case AssignmentOperation.IgnoreAndFetch: + { + SetDstGpr(FetchParam()); + + break; + } + + //Move result. + case AssignmentOperation.Move: + { + SetDstGpr(Result); + + break; + } + + //Move result and use as Method Address. + case AssignmentOperation.MoveAndSetMaddr: + { + SetDstGpr(Result); + + SetMethAddr(Result); + + break; + } + + //Fetch parameter and send result. + case AssignmentOperation.FetchAndSend: + { + SetDstGpr(FetchParam()); + + Send(Vmm, Result); + + break; + } + + //Move and send result. + case AssignmentOperation.MoveAndSend: + { + SetDstGpr(Result); + + Send(Vmm, Result); + + break; + } + + //Fetch parameter and use result as Method Address. + case AssignmentOperation.FetchAndSetMaddr: + { + SetDstGpr(FetchParam()); + + SetMethAddr(Result); + + break; + } + + //Move result and use as Method Address, then fetch and send paramter. + case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend: + { + SetDstGpr(Result); + + SetMethAddr(Result); + + Send(Vmm, FetchParam()); + + break; + } + + //Move result and use as Method Address, then send bits 17:12 of result. + case AssignmentOperation.MoveAndSetMaddrThenSendHigh: + { + SetDstGpr(Result); + + SetMethAddr(Result); + + Send(Vmm, (Result >> 12) & 0x3f); + + break; + } + } + } + else + { + //Branch. + bool OnNotZero = ((OpCode >> 4) & 1) != 0; + + bool Taken = OnNotZero + ? GetGprA() != 0 + : GetGprA() == 0; + + if (Taken) + { + Pc = BaseAddr + GetImm(); + + bool NoDelays = (OpCode & 0x20) != 0; + + if (NoDelays) + { + FetchOpCode(Mme); + } + + return true; + } + } + + bool Exit = (OpCode & 0x80) != 0; + + return !Exit; + } + + private void FetchOpCode(int[] Mme) + { + OpCode = PipeOp; + + PipeOp = Mme[Pc++]; + } + + private int GetAluResult() + { + AluOperation Op = (AluOperation)(OpCode & 7); + + switch (Op) + { + case AluOperation.AluReg: + { + AluRegOperation AluOp = (AluRegOperation)((OpCode >> 17) & 0x1f); + + return GetAluResult(AluOp, GetGprA(), GetGprB()); + } + + case AluOperation.AddImmediate: + { + return GetGprA() + GetImm(); + } + + case AluOperation.BitfieldReplace: + case AluOperation.BitfieldExtractLslImm: + case AluOperation.BitfieldExtractLslReg: + { + int BfSrcBit = (OpCode >> 17) & 0x1f; + int BfSize = (OpCode >> 22) & 0x1f; + int BfDstBit = (OpCode >> 27) & 0x1f; + + int BfMask = (1 << BfSize) - 1; + + int Dst = GetGprA(); + int Src = GetGprB(); + + switch (Op) + { + case AluOperation.BitfieldReplace: + { + Src = (int)((uint)Src >> BfSrcBit) & BfMask; + + Dst &= ~(BfMask << BfDstBit); + + Dst |= Src << BfDstBit; + + return Dst; + } + + case AluOperation.BitfieldExtractLslImm: + { + Src = (int)((uint)Src >> Dst) & BfMask; + + return Src << BfDstBit; + } + + case AluOperation.BitfieldExtractLslReg: + { + Src = (int)((uint)Src >> BfSrcBit) & BfMask; + + return Src << Dst; + } + } + + break; + } + + case AluOperation.ReadImmediate: + { + return Read(GetGprA() + GetImm()); + } + } + + throw new ArgumentException(nameof(OpCode)); + } + + private int GetAluResult(AluRegOperation AluOp, int A, int B) + { + switch (AluOp) + { + case AluRegOperation.Add: + { + ulong Result = (ulong)A + (ulong)B; + + Carry = Result > 0xffffffff; + + return (int)Result; + } + + case AluRegOperation.AddWithCarry: + { + ulong Result = (ulong)A + (ulong)B + (Carry ? 1UL : 0UL); + + Carry = Result > 0xffffffff; + + return (int)Result; + } + + case AluRegOperation.Subtract: + { + ulong Result = (ulong)A - (ulong)B; + + Carry = Result < 0x100000000; + + return (int)Result; + } + + case AluRegOperation.SubtractWithBorrow: + { + ulong Result = (ulong)A - (ulong)B - (Carry ? 0UL : 1UL); + + Carry = Result < 0x100000000; + + return (int)Result; + } + + case AluRegOperation.BitwiseExclusiveOr: return A ^ B; + case AluRegOperation.BitwiseOr: return A | B; + case AluRegOperation.BitwiseAnd: return A & B; + case AluRegOperation.BitwiseAndNot: return A & ~B; + case AluRegOperation.BitwiseNotAnd: return ~(A & B); + } + + throw new ArgumentOutOfRangeException(nameof(AluOp)); + } + + private int GetImm() + { + //Note: The immediate is signed, the sign-extension is intended here. + return OpCode >> 14; + } + + private void SetMethAddr(int Value) + { + MethAddr = (Value >> 0) & 0xfff; + MethIncr = (Value >> 12) & 0x3f; + } + + private void SetDstGpr(int Value) + { + Gprs[(OpCode >> 8) & 7] = Value; + } + + private int GetGprA() + { + return GetGprValue((OpCode >> 11) & 7); + } + + private int GetGprB() + { + return GetGprValue((OpCode >> 14) & 7); + } + + private int GetGprValue(int Index) + { + return Index != 0 ? Gprs[Index] : 0; + } + + private int FetchParam() + { + int Value; + + //If we don't have any parameters in the FIFO, + //keep running the PFIFO engine until it writes the parameters. + while (!Fifo.TryDequeue(out Value)) + { + if (!PFifo.Step()) + { + return 0; + } + } + + return Value; + } + + private int Read(int Reg) + { + return Engine.Registers[Reg]; + } + + private void Send(NvGpuVmm Vmm, int Value) + { + NvGpuPBEntry PBEntry = new NvGpuPBEntry(MethAddr, 0, Value); + + Engine.CallMethod(Vmm, PBEntry); + + MethAddr += MethIncr; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Memory/NvGpuBufferType.cs b/Ryujinx.Graphics/Memory/NvGpuBufferType.cs new file mode 100644 index 00000000..6f0d2571 --- /dev/null +++ b/Ryujinx.Graphics/Memory/NvGpuBufferType.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.Graphics.Memory +{ + public enum NvGpuBufferType + { + Index, + Vertex, + Texture, + ConstBuffer, + Count + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Memory/NvGpuPBEntry.cs b/Ryujinx.Graphics/Memory/NvGpuPBEntry.cs new file mode 100644 index 00000000..6b93c169 --- /dev/null +++ b/Ryujinx.Graphics/Memory/NvGpuPBEntry.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.ObjectModel; + +namespace Ryujinx.Graphics.Memory +{ + public struct NvGpuPBEntry + { + public int Method { get; private set; } + + public int SubChannel { get; private set; } + + private int[] m_Arguments; + + public ReadOnlyCollection Arguments => Array.AsReadOnly(m_Arguments); + + public NvGpuPBEntry(int Method, int SubChannel, params int[] Arguments) + { + this.Method = Method; + this.SubChannel = SubChannel; + this.m_Arguments = Arguments; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Memory/NvGpuPushBuffer.cs b/Ryujinx.Graphics/Memory/NvGpuPushBuffer.cs new file mode 100644 index 00000000..0902ebfc --- /dev/null +++ b/Ryujinx.Graphics/Memory/NvGpuPushBuffer.cs @@ -0,0 +1,101 @@ +using System.Collections.Generic; +using System.IO; + +namespace Ryujinx.Graphics.Memory +{ + public static class NvGpuPushBuffer + { + private enum SubmissionMode + { + Incrementing = 1, + NonIncrementing = 3, + Immediate = 4, + IncrementOnce = 5 + } + + public static NvGpuPBEntry[] Decode(byte[] Data) + { + using (MemoryStream MS = new MemoryStream(Data)) + { + BinaryReader Reader = new BinaryReader(MS); + + List PushBuffer = new List(); + + bool CanRead() => MS.Position + 4 <= MS.Length; + + while (CanRead()) + { + int Packed = Reader.ReadInt32(); + + int Meth = (Packed >> 0) & 0x1fff; + int SubC = (Packed >> 13) & 7; + int Args = (Packed >> 16) & 0x1fff; + int Mode = (Packed >> 29) & 7; + + switch ((SubmissionMode)Mode) + { + case SubmissionMode.Incrementing: + { + for (int Index = 0; Index < Args && CanRead(); Index++, Meth++) + { + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); + } + + break; + } + + case SubmissionMode.NonIncrementing: + { + int[] Arguments = new int[Args]; + + for (int Index = 0; Index < Arguments.Length; Index++) + { + if (!CanRead()) + { + break; + } + + Arguments[Index] = Reader.ReadInt32(); + } + + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Arguments)); + + break; + } + + case SubmissionMode.Immediate: + { + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Args)); + + break; + } + + case SubmissionMode.IncrementOnce: + { + if (CanRead()) + { + PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); + } + + if (CanRead() && Args > 1) + { + int[] Arguments = new int[Args - 1]; + + for (int Index = 0; Index < Arguments.Length && CanRead(); Index++) + { + Arguments[Index] = Reader.ReadInt32(); + } + + PushBuffer.Add(new NvGpuPBEntry(Meth + 1, SubC, Arguments)); + } + + break; + } + } + } + + return PushBuffer.ToArray(); + } + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Memory/NvGpuVmm.cs b/Ryujinx.Graphics/Memory/NvGpuVmm.cs new file mode 100644 index 00000000..0082fb27 --- /dev/null +++ b/Ryujinx.Graphics/Memory/NvGpuVmm.cs @@ -0,0 +1,377 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using System; + +namespace Ryujinx.Graphics.Memory +{ + public class NvGpuVmm : IAMemory, IGalMemory + { + public const long AddrSize = 1L << 40; + + private const int PTLvl0Bits = 14; + private const int PTLvl1Bits = 14; + private const int PTPageBits = 12; + + private const int PTLvl0Size = 1 << PTLvl0Bits; + private const int PTLvl1Size = 1 << PTLvl1Bits; + public const int PageSize = 1 << PTPageBits; + + private const int PTLvl0Mask = PTLvl0Size - 1; + private const int PTLvl1Mask = PTLvl1Size - 1; + public const int PageMask = PageSize - 1; + + private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; + private const int PTLvl1Bit = PTPageBits; + + public AMemory Memory { get; private set; } + + private NvGpuVmmCache Cache; + + private const long PteUnmapped = -1; + private const long PteReserved = -2; + + private long[][] PageTable; + + public NvGpuVmm(AMemory Memory) + { + this.Memory = Memory; + + Cache = new NvGpuVmmCache(); + + PageTable = new long[PTLvl0Size][]; + } + + public long Map(long PA, long VA, long Size) + { + lock (PageTable) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PA + Offset); + } + } + + return VA; + } + + public long Map(long PA, long Size) + { + lock (PageTable) + { + long VA = GetFreePosition(Size); + + if (VA != -1) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PA + Offset); + } + } + + return VA; + } + } + + public long ReserveFixed(long VA, long Size) + { + lock (PageTable) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + if (IsPageInUse(VA + Offset)) + { + return -1; + } + } + + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PteReserved); + } + } + + return VA; + } + + public long Reserve(long Size, long Align) + { + lock (PageTable) + { + long Position = GetFreePosition(Size, Align); + + if (Position != -1) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(Position + Offset, PteReserved); + } + } + + return Position; + } + } + + public void Free(long VA, long Size) + { + lock (PageTable) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + SetPte(VA + Offset, PteUnmapped); + } + } + } + + private long GetFreePosition(long Size, long Align = 1) + { + //Note: Address 0 is not considered valid by the driver, + //when 0 is returned it's considered a mapping error. + long Position = PageSize; + long FreeSize = 0; + + if (Align < 1) + { + Align = 1; + } + + Align = (Align + PageMask) & ~PageMask; + + while (Position + FreeSize < AddrSize) + { + if (!IsPageInUse(Position + FreeSize)) + { + FreeSize += PageSize; + + if (FreeSize >= Size) + { + return Position; + } + } + else + { + Position += FreeSize + PageSize; + FreeSize = 0; + + long Remainder = Position % Align; + + if (Remainder != 0) + { + Position = (Position - Remainder) + Align; + } + } + } + + return -1; + } + + public long GetPhysicalAddress(long VA) + { + long BasePos = GetPte(VA); + + if (BasePos < 0) + { + return -1; + } + + return BasePos + (VA & PageMask); + } + + public bool IsRegionFree(long VA, long Size) + { + for (long Offset = 0; Offset < Size; Offset += PageSize) + { + if (IsPageInUse(VA + Offset)) + { + return false; + } + } + + return true; + } + + private bool IsPageInUse(long VA) + { + if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0) + { + return false; + } + + long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask; + long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask; + + if (PageTable[L0] == null) + { + return false; + } + + return PageTable[L0][L1] != PteUnmapped; + } + + private long GetPte(long Position) + { + long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; + long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; + + if (PageTable[L0] == null) + { + return -1; + } + + return PageTable[L0][L1]; + } + + private void SetPte(long Position, long TgtAddr) + { + long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; + long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; + + if (PageTable[L0] == null) + { + PageTable[L0] = new long[PTLvl1Size]; + + for (int Index = 0; Index < PTLvl1Size; Index++) + { + PageTable[L0][Index] = PteUnmapped; + } + } + + PageTable[L0][L1] = TgtAddr; + } + + public bool IsRegionModified(long PA, long Size, NvGpuBufferType BufferType) + { + return Cache.IsRegionModified(Memory, BufferType, PA, Size); + } + + public IntPtr GetHostAddress(long Position, long Size) + { + return Memory.GetHostAddress(GetPhysicalAddress(Position), Size); + } + + public byte ReadByte(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadByte(Position); + } + + public ushort ReadUInt16(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadUInt16(Position); + } + + public uint ReadUInt32(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadUInt32(Position); + } + + public ulong ReadUInt64(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadUInt64(Position); + } + + public sbyte ReadSByte(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadSByte(Position); + } + + public short ReadInt16(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadInt16(Position); + } + + public int ReadInt32(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadInt32(Position); + } + + public long ReadInt64(long Position) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadInt64(Position); + } + + public byte[] ReadBytes(long Position, long Size) + { + Position = GetPhysicalAddress(Position); + + return Memory.ReadBytes(Position, Size); + } + + public void WriteByte(long Position, byte Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteByte(Position, Value); + } + + public void WriteUInt16(long Position, ushort Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteUInt16(Position, Value); + } + + public void WriteUInt32(long Position, uint Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteUInt32(Position, Value); + } + + public void WriteUInt64(long Position, ulong Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteUInt64(Position, Value); + } + + public void WriteSByte(long Position, sbyte Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteSByte(Position, Value); + } + + public void WriteInt16(long Position, short Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteInt16(Position, Value); + } + + public void WriteInt32(long Position, int Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteInt32(Position, Value); + } + + public void WriteInt64(long Position, long Value) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteInt64(Position, Value); + } + + public void WriteBytes(long Position, byte[] Data) + { + Position = GetPhysicalAddress(Position); + + Memory.WriteBytes(Position, Data); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Memory/NvGpuVmmCache.cs b/Ryujinx.Graphics/Memory/NvGpuVmmCache.cs new file mode 100644 index 00000000..56979e1f --- /dev/null +++ b/Ryujinx.Graphics/Memory/NvGpuVmmCache.cs @@ -0,0 +1,309 @@ +using ChocolArm64.Memory; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Memory +{ + class NvGpuVmmCache + { + private const long RamSize = 4L * 1024 * 1024 * 1024; + + private const int MaxCpCount = 10000; + private const int MaxCpTimeDelta = 60000; + + private class CachedPage + { + private struct Range + { + public long Start; + public long End; + + public Range(long Start, long End) + { + this.Start = Start; + this.End = End; + } + } + + private List[] Regions; + + private HashSet ResidencyKeys; + + public LinkedListNode Node { get; set; } + + public int Timestamp { get; private set; } + + public CachedPage() + { + Regions = new List[(int)NvGpuBufferType.Count]; + + for (int Index = 0; Index < Regions.Length; Index++) + { + Regions[Index] = new List(); + } + + ResidencyKeys = new HashSet(); + } + + public void AddResidency(long Key) + { + ResidencyKeys.Add(Key); + } + + public void RemoveResidency(HashSet[] Residency, long PageSize) + { + for (int i = 0; i < (int)NvGpuBufferType.Count; i++) + { + foreach (Range Region in Regions[i]) + { + foreach (long Key in ResidencyKeys) + { + Residency[Region.Start / PageSize].Remove(Key); + } + } + } + } + + public bool AddRange(long Start, long End, NvGpuBufferType BufferType) + { + List BtRegions = Regions[(int)BufferType]; + + for (int Index = 0; Index < BtRegions.Count; Index++) + { + Range Rg = BtRegions[Index]; + + if (Start >= Rg.Start && End <= Rg.End) + { + return false; + } + + if (Start <= Rg.End && Rg.Start <= End) + { + long MinStart = Math.Min(Rg.Start, Start); + long MaxEnd = Math.Max(Rg.End, End); + + BtRegions[Index] = new Range(MinStart, MaxEnd); + + Timestamp = Environment.TickCount; + + return true; + } + } + + BtRegions.Add(new Range(Start, End)); + + Timestamp = Environment.TickCount; + + return true; + } + + public int GetTotalCount() + { + int Count = 0; + + for (int Index = 0; Index < Regions.Length; Index++) + { + Count += Regions[Index].Count; + } + + return Count; + } + } + + private Dictionary Cache; + + private LinkedList SortedCache; + + private HashSet[] Residency; + + private long ResidencyPageSize; + + private int CpCount; + + public NvGpuVmmCache() + { + Cache = new Dictionary(); + + SortedCache = new LinkedList(); + } + + public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size) + { + (bool[] Modified, long ModifiedCount) = Memory.IsRegionModified(PA, Size); + + PA = Memory.GetPhysicalAddress(PA); + + ClearCachedPagesIfNeeded(); + + long PageSize = AMemory.PageSize; + + EnsureResidencyInitialized(PageSize); + + bool HasResidents = AddResidency(PA, Size); + + if (!HasResidents && ModifiedCount == 0) + { + return false; + } + + long Mask = PageSize - 1; + + long ResidencyKey = PA; + + long PAEnd = PA + Size; + + bool RegMod = false; + + int Index = 0; + + while (PA < PAEnd) + { + long Key = PA & ~AMemory.PageMask; + + long PAPgEnd = Math.Min((PA + AMemory.PageSize) & ~AMemory.PageMask, PAEnd); + + bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp); + + if (IsCached) + { + CpCount -= Cp.GetTotalCount(); + + SortedCache.Remove(Cp.Node); + } + else + { + Cp = new CachedPage(); + + Cache.Add(Key, Cp); + } + + if (Modified[Index++] && IsCached) + { + Cp = new CachedPage(); + + Cache[Key] = Cp; + } + + Cp.AddResidency(ResidencyKey); + + Cp.Node = SortedCache.AddLast(Key); + + RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType); + + CpCount += Cp.GetTotalCount(); + + PA = PAPgEnd; + } + + return RegMod; + } + + private bool AddResidency(long PA, long Size) + { + long PageSize = ResidencyPageSize; + + long Mask = PageSize - 1; + + long Key = PA; + + bool ResidentFound = false; + + for (long Cursor = PA & ~Mask; Cursor < ((PA + Size + PageSize - 1) & ~Mask); Cursor += PageSize) + { + long PageIndex = Cursor / PageSize; + + Residency[PageIndex].Add(Key); + + if (Residency[PageIndex].Count > 1) + { + ResidentFound = true; + } + } + + return ResidentFound; + } + + private void EnsureResidencyInitialized(long PageSize) + { + if (Residency == null) + { + Residency = new HashSet[RamSize / PageSize]; + + for (int i = 0; i < Residency.Length; i++) + { + Residency[i] = new HashSet(); + } + + ResidencyPageSize = PageSize; + } + else + { + if (ResidencyPageSize != PageSize) + { + throw new InvalidOperationException("Tried to change residency page size"); + } + } + } + + private void ClearCachedPagesIfNeeded() + { + if (CpCount <= MaxCpCount) + { + return; + } + + int Timestamp = Environment.TickCount; + + int TimeDelta; + + do + { + if (!TryPopOldestCachedPageKey(Timestamp, out long Key)) + { + break; + } + + CachedPage Cp = Cache[Key]; + + Cp.RemoveResidency(Residency, ResidencyPageSize); + + Cache.Remove(Key); + + CpCount -= Cp.GetTotalCount(); + + TimeDelta = RingDelta(Cp.Timestamp, Timestamp); + } + while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta); + } + + private bool TryPopOldestCachedPageKey(int Timestamp, out long Key) + { + LinkedListNode Node = SortedCache.First; + + if (Node == null) + { + Key = 0; + + return false; + } + + SortedCache.Remove(Node); + + Key = Node.Value; + + return true; + } + + private int RingDelta(int Old, int New) + { + if ((uint)New < (uint)Old) + { + return New + (~Old + 1); + } + else + { + return New - Old; + } + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpu.cs b/Ryujinx.Graphics/NvGpu.cs new file mode 100644 index 00000000..7ba700b8 --- /dev/null +++ b/Ryujinx.Graphics/NvGpu.cs @@ -0,0 +1,27 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics; + +namespace Ryujinx.Graphics +{ + public class NvGpu + { + public IGalRenderer Renderer { get; private set; } + + public NvGpuFifo Fifo { get; private set; } + + public NvGpuEngine2d Engine2d { get; private set; } + public NvGpuEngine3d Engine3d { get; private set; } + public NvGpuEngineDma EngineDma { get; private set; } + + public NvGpu(IGalRenderer Renderer) + { + this.Renderer = Renderer; + + Fifo = new NvGpuFifo(this); + + Engine2d = new NvGpuEngine2d(this); + Engine3d = new NvGpuEngine3d(this); + EngineDma = new NvGpuEngineDma(this); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuEngine.cs b/Ryujinx.Graphics/NvGpuEngine.cs new file mode 100644 index 00000000..3d5e118a --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngine.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.Graphics +{ + enum NvGpuEngine + { + _2d = 0x902d, + _3d = 0xb197, + Compute = 0xb1c0, + Kepler = 0xa140, + Dma = 0xb0b5 + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuEngine2d.cs b/Ryujinx.Graphics/NvGpuEngine2d.cs new file mode 100644 index 00000000..f26b0020 --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngine2d.cs @@ -0,0 +1,208 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; +using Ryujinx.Graphics.Texture; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics +{ + public class NvGpuEngine2d : INvGpuEngine + { + private enum CopyOperation + { + SrcCopyAnd, + RopAnd, + Blend, + SrcCopy, + Rop, + SrcCopyPremult, + BlendPremult + } + + public int[] Registers { get; private set; } + + private NvGpu Gpu; + + private Dictionary Methods; + + public NvGpuEngine2d(NvGpu Gpu) + { + this.Gpu = Gpu; + + Registers = new int[0xe00]; + + Methods = new Dictionary(); + + void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + { + while (Count-- > 0) + { + Methods.Add(Meth, Method); + + Meth += Stride; + } + } + + AddMethod(0xb5, 1, 1, TextureCopy); + } + + public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) + { + Method(Vmm, PBEntry); + } + else + { + WriteRegister(PBEntry); + } + } + + private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation); + + bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; + int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); + int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); + int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch); + int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions); + + bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; + int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); + int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight); + int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); + int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); + + TextureSwizzle SrcSwizzle = SrcLinear + ? TextureSwizzle.Pitch + : TextureSwizzle.BlockLinear; + + TextureSwizzle DstSwizzle = DstLinear + ? TextureSwizzle.Pitch + : TextureSwizzle.BlockLinear; + + int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); + int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + + long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); + long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); + + long SrcKey = Vmm.GetPhysicalAddress(SrcAddress); + long DstKey = Vmm.GetPhysicalAddress(DstAddress); + + bool IsSrcFb = Gpu.Engine3d.IsFrameBufferPosition(SrcKey); + bool IsDstFb = Gpu.Engine3d.IsFrameBufferPosition(DstKey); + + TextureInfo SrcTexture() + { + return new TextureInfo( + SrcAddress, + SrcWidth, + SrcHeight, + SrcPitch, + SrcBlockHeight, 1, + SrcSwizzle, + GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm); + } + + TextureInfo DstTexture() + { + return new TextureInfo( + DstAddress, + DstWidth, + DstHeight, + DstPitch, + DstBlockHeight, 1, + DstSwizzle, + GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm); + } + + //TODO: fb -> fb copies, tex -> fb copies, formats other than RGBA8, + //make it throw for unimpl stuff (like the copy mode)... + if (IsSrcFb && IsDstFb) + { + //Frame Buffer -> Frame Buffer copy. + Gpu.Renderer.RenderTarget.Copy( + SrcKey, + DstKey, + 0, + 0, + SrcWidth, + SrcHeight, + 0, + 0, + DstWidth, + DstHeight); + } + if (IsSrcFb) + { + //Frame Buffer -> Texture copy. + Gpu.Renderer.RenderTarget.GetBufferData(SrcKey, (byte[] Buffer) => + { + TextureInfo Src = SrcTexture(); + TextureInfo Dst = DstTexture(); + + if (Src.Width != Dst.Width || + Src.Height != Dst.Height) + { + throw new NotImplementedException("Texture resizing is not supported"); + } + + TextureWriter.Write(Vmm, Dst, Buffer); + }); + } + else if (IsDstFb) + { + byte[] Buffer = TextureReader.Read(Vmm, SrcTexture()); + + Gpu.Renderer.RenderTarget.SetBufferData( + DstKey, + DstWidth, + DstHeight, + Buffer); + } + else + { + //Texture -> Texture copy. + TextureInfo Src = SrcTexture(); + TextureInfo Dst = DstTexture(); + + if (Src.Width != Dst.Width || + Src.Height != Dst.Height) + { + throw new NotImplementedException("Texture resizing is not supported"); + } + + TextureWriter.Write(Vmm, Dst, TextureReader.Read(Vmm, Src)); + } + } + + private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg) + { + return + (long)Registers[(int)Reg + 0] << 32 | + (uint)Registers[(int)Reg + 1]; + } + + private void WriteRegister(NvGpuPBEntry PBEntry) + { + int ArgsCount = PBEntry.Arguments.Count; + + if (ArgsCount > 0) + { + Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; + } + } + + private int ReadRegister(NvGpuEngine2dReg Reg) + { + return Registers[(int)Reg]; + } + + private void WriteRegister(NvGpuEngine2dReg Reg, int Value) + { + Registers[(int)Reg] = Value; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuEngine2dReg.cs b/Ryujinx.Graphics/NvGpuEngine2dReg.cs new file mode 100644 index 00000000..00f6f578 --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngine2dReg.cs @@ -0,0 +1,25 @@ +namespace Ryujinx.Graphics +{ + enum NvGpuEngine2dReg + { + DstFormat = 0x80, + DstLinear = 0x81, + DstBlockDimensions = 0x82, + DstDepth = 0x83, + DstLayer = 0x84, + DstPitch = 0x85, + DstWidth = 0x86, + DstHeight = 0x87, + DstAddress = 0x88, + SrcFormat = 0x8c, + SrcLinear = 0x8d, + SrcBlockDimensions = 0x8e, + SrcDepth = 0x8f, + SrcLayer = 0x90, + SrcPitch = 0x91, + SrcWidth = 0x92, + SrcHeight = 0x93, + SrcAddress = 0x94, + CopyOperation = 0xab + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuEngine3d.cs b/Ryujinx.Graphics/NvGpuEngine3d.cs new file mode 100644 index 00000000..624eddae --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngine3d.cs @@ -0,0 +1,898 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; +using Ryujinx.Graphics.Texture; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics +{ + public class NvGpuEngine3d : INvGpuEngine + { + public int[] Registers { get; private set; } + + private NvGpu Gpu; + + private Dictionary Methods; + + private struct ConstBuffer + { + public bool Enabled; + public long Position; + public int Size; + } + + private ConstBuffer[][] ConstBuffers; + + private HashSet FrameBuffers; + + private List[] UploadedKeys; + + private int CurrentInstance = 0; + + public NvGpuEngine3d(NvGpu Gpu) + { + this.Gpu = Gpu; + + Registers = new int[0xe00]; + + Methods = new Dictionary(); + + void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + { + while (Count-- > 0) + { + Methods.Add(Meth, Method); + + Meth += Stride; + } + } + + AddMethod(0x585, 1, 1, VertexEndGl); + AddMethod(0x674, 1, 1, ClearBuffers); + AddMethod(0x6c3, 1, 1, QueryControl); + AddMethod(0x8e4, 16, 1, CbData); + AddMethod(0x904, 5, 8, CbBind); + + ConstBuffers = new ConstBuffer[6][]; + + for (int Index = 0; Index < ConstBuffers.Length; Index++) + { + ConstBuffers[Index] = new ConstBuffer[18]; + } + + FrameBuffers = new HashSet(); + + UploadedKeys = new List[(int)NvGpuBufferType.Count]; + + for (int i = 0; i < UploadedKeys.Length; i++) + { + UploadedKeys[i] = new List(); + } + } + + public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) + { + Method(Vmm, PBEntry); + } + else + { + WriteRegister(PBEntry); + } + } + + public void ResetCache() + { + foreach (List Uploaded in UploadedKeys) + { + Uploaded.Clear(); + } + } + + private void VertexEndGl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + LockCaches(); + + GalPipelineState State = new GalPipelineState(); + + SetFlip(State); + SetFrontFace(State); + SetCullFace(State); + SetDepth(State); + SetStencil(State); + SetAlphaBlending(State); + SetPrimitiveRestart(State); + + for (int FbIndex = 0; FbIndex < 8; FbIndex++) + { + SetFrameBuffer(Vmm, 0); + } + + SetZeta(Vmm); + + SetRenderTargets(); + + long[] Keys = UploadShaders(Vmm); + + Gpu.Renderer.Shader.BindProgram(); + + UploadTextures(Vmm, State, Keys); + UploadConstBuffers(Vmm, State, Keys); + UploadVertexArrays(Vmm, State); + + DispatchRender(Vmm, State); + + UnlockCaches(); + } + + private void LockCaches() + { + Gpu.Renderer.Buffer.LockCache(); + Gpu.Renderer.Rasterizer.LockCaches(); + Gpu.Renderer.Texture.LockCache(); + } + + private void UnlockCaches() + { + Gpu.Renderer.Buffer.UnlockCache(); + Gpu.Renderer.Rasterizer.UnlockCaches(); + Gpu.Renderer.Texture.UnlockCache(); + } + + private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + int Arg0 = PBEntry.Arguments[0]; + + int FbIndex = (Arg0 >> 6) & 0xf; + + GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f); + + float Red = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 0); + float Green = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 1); + float Blue = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 2); + float Alpha = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 3); + + float Depth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth); + + int Stencil = ReadRegister(NvGpuEngine3dReg.ClearStencil); + + SetFrameBuffer(Vmm, FbIndex); + + SetZeta(Vmm); + + Gpu.Renderer.Rasterizer.ClearBuffers( + Flags, + FbIndex, + Red, Green, Blue, Alpha, + Depth, + Stencil); + } + + private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex) + { + long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10); + + int Format = ReadRegister(NvGpuEngine3dReg.FrameBufferNFormat + FbIndex * 0x10); + + if (VA == 0 || Format == 0) + { + Gpu.Renderer.RenderTarget.UnbindColor(FbIndex); + + return; + } + + long Key = Vmm.GetPhysicalAddress(VA); + + FrameBuffers.Add(Key); + + int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10); + int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10); + + float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 8); + float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 8); + + float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 8); + float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 8); + + int VpX = (int)MathF.Max(0, TX - MathF.Abs(SX)); + int VpY = (int)MathF.Max(0, TY - MathF.Abs(SY)); + + int VpW = (int)(TX + MathF.Abs(SX)) - VpX; + int VpH = (int)(TY + MathF.Abs(SY)) - VpY; + + GalImageFormat ImageFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)Format); + + GalImage Image = new GalImage(Width, Height, ImageFormat); + + long Size = ImageUtils.GetSize(Image); + + Gpu.Renderer.Texture.CreateFb(Key, Size, Image); + + Gpu.Renderer.RenderTarget.BindColor(Key, FbIndex); + + Gpu.Renderer.RenderTarget.SetViewport(VpX, VpY, VpW, VpH); + } + + private void SetZeta(NvGpuVmm Vmm) + { + long ZA = MakeInt64From2xInt32(NvGpuEngine3dReg.ZetaAddress); + + int Format = ReadRegister(NvGpuEngine3dReg.ZetaFormat); + + bool ZetaEnable = (ReadRegister(NvGpuEngine3dReg.ZetaEnable) & 1) != 0; + + if (ZA == 0 || Format == 0 || !ZetaEnable) + { + Gpu.Renderer.RenderTarget.UnbindZeta(); + + return; + } + + long Key = Vmm.GetPhysicalAddress(ZA); + + int Width = ReadRegister(NvGpuEngine3dReg.ZetaHoriz); + int Height = ReadRegister(NvGpuEngine3dReg.ZetaVert); + + GalImageFormat ImageFormat = ImageUtils.ConvertZeta((GalZetaFormat)Format); + + GalImage Image = new GalImage(Width, Height, ImageFormat); + + long Size = ImageUtils.GetSize(Image); + + Gpu.Renderer.Texture.CreateFb(Key, Size, Image); + + Gpu.Renderer.RenderTarget.BindZeta(Key); + } + + private long[] UploadShaders(NvGpuVmm Vmm) + { + long[] Keys = new long[5]; + + long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + + int Index = 1; + + int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl); + + bool VpAEnable = (VpAControl & 1) != 0; + + if (VpAEnable) + { + //Note: The maxwell supports 2 vertex programs, usually + //only VP B is used, but in some cases VP A is also used. + //In this case, it seems to function as an extra vertex + //shader stage. + //The graphics abstraction layer has a special overload for this + //case, which should merge the two shaders into one vertex shader. + int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset); + int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10); + + long VpAPos = BasePosition + (uint)VpAOffset; + long VpBPos = BasePosition + (uint)VpBOffset; + + Keys[(int)GalShaderType.Vertex] = VpBPos; + + Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex); + Gpu.Renderer.Shader.Bind(VpBPos); + + Index = 2; + } + + for (; Index < 6; Index++) + { + GalShaderType Type = GetTypeFromProgram(Index); + + int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10); + int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10); + + //Note: Vertex Program (B) is always enabled. + bool Enable = (Control & 1) != 0 || Index == 1; + + if (!Enable) + { + Gpu.Renderer.Shader.Unbind(Type); + + continue; + } + + long Key = BasePosition + (uint)Offset; + + Keys[(int)Type] = Key; + + Gpu.Renderer.Shader.Create(Vmm, Key, Type); + Gpu.Renderer.Shader.Bind(Key); + } + + return Keys; + } + + private static GalShaderType GetTypeFromProgram(int Program) + { + switch (Program) + { + case 0: + case 1: return GalShaderType.Vertex; + case 2: return GalShaderType.TessControl; + case 3: return GalShaderType.TessEvaluation; + case 4: return GalShaderType.Geometry; + case 5: return GalShaderType.Fragment; + } + + throw new ArgumentOutOfRangeException(nameof(Program)); + } + + private void SetFlip(GalPipelineState State) + { + State.FlipX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); + State.FlipY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); + } + + private void SetFrontFace(GalPipelineState State) + { + float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); + float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); + + GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace); + + //Flipping breaks facing. Flipping front facing too fixes it + if (SignX != SignY) + { + switch (FrontFace) + { + case GalFrontFace.CW: + FrontFace = GalFrontFace.CCW; + break; + + case GalFrontFace.CCW: + FrontFace = GalFrontFace.CW; + break; + } + } + + State.FrontFace = FrontFace; + } + + private void SetCullFace(GalPipelineState State) + { + State.CullFaceEnabled = (ReadRegister(NvGpuEngine3dReg.CullFaceEnable) & 1) != 0; + + if (State.CullFaceEnabled) + { + State.CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace); + } + } + + private void SetDepth(GalPipelineState State) + { + State.DepthTestEnabled = (ReadRegister(NvGpuEngine3dReg.DepthTestEnable) & 1) != 0; + + if (State.DepthTestEnabled) + { + State.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction); + } + } + + private void SetStencil(GalPipelineState State) + { + State.StencilTestEnabled = (ReadRegister(NvGpuEngine3dReg.StencilEnable) & 1) != 0; + + if (State.StencilTestEnabled) + { + State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc); + State.StencilBackFuncRef = ReadRegister(NvGpuEngine3dReg.StencilBackFuncRef); + State.StencilBackFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackFuncMask); + State.StencilBackOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpFail); + State.StencilBackOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZFail); + State.StencilBackOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZPass); + State.StencilBackMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackMask); + + State.StencilFrontFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncFunc); + State.StencilFrontFuncRef = ReadRegister(NvGpuEngine3dReg.StencilFrontFuncRef); + State.StencilFrontFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncMask); + State.StencilFrontOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpFail); + State.StencilFrontOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZFail); + State.StencilFrontOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZPass); + State.StencilFrontMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontMask); + } + } + + private void SetAlphaBlending(GalPipelineState State) + { + //TODO: Support independent blend properly. + State.BlendEnabled = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0; + + if (State.BlendEnabled) + { + State.BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0; + + State.BlendEquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb); + State.BlendFuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb); + State.BlendFuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb); + State.BlendEquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha); + State.BlendFuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha); + State.BlendFuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha); + } + } + + private void SetPrimitiveRestart(GalPipelineState State) + { + State.PrimitiveRestartEnabled = (ReadRegister(NvGpuEngine3dReg.PrimRestartEnable) & 1) != 0; + + if (State.PrimitiveRestartEnabled) + { + State.PrimitiveRestartIndex = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex); + } + } + + private void SetRenderTargets() + { + bool SeparateFragData = (ReadRegister(NvGpuEngine3dReg.RTSeparateFragData) & 1) != 0; + + if (SeparateFragData) + { + uint Control = (uint)(ReadRegister(NvGpuEngine3dReg.RTControl)); + + uint Count = Control & 0xf; + + int[] Map = new int[Count]; + + for (int i = 0; i < Count; i++) + { + int Shift = 4 + i * 3; + + Map[i] = (int)((Control >> Shift) & 7); + } + + Gpu.Renderer.RenderTarget.SetMap(Map); + } + else + { + Gpu.Renderer.RenderTarget.SetMap(null); + } + } + + private void UploadTextures(NvGpuVmm Vmm, GalPipelineState State, long[] Keys) + { + long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); + + int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); + + int TexIndex = 0; + + for (int Index = 0; Index < Keys.Length; Index++) + { + foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index])) + { + long Position; + + if (DeclInfo.IsCb) + { + Position = ConstBuffers[Index][DeclInfo.Cbuf].Position; + } + else + { + Position = ConstBuffers[Index][TextureCbIndex].Position; + } + + int TextureHandle = Vmm.ReadInt32(Position + DeclInfo.Index * 4); + + UploadTexture(Vmm, TexIndex, TextureHandle); + + TexIndex++; + } + } + } + + private void UploadTexture(NvGpuVmm Vmm, int TexIndex, int TextureHandle) + { + if (TextureHandle == 0) + { + //TODO: Is this correct? + //Some games like puyo puyo will have 0 handles. + //It may be just normal behaviour or a bug caused by sync issues. + //The game does initialize the value properly after through. + return; + } + + int TicIndex = (TextureHandle >> 0) & 0xfffff; + int TscIndex = (TextureHandle >> 20) & 0xfff; + + long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset); + long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset); + + TicPosition += TicIndex * 0x20; + TscPosition += TscIndex * 0x20; + + GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition); + + long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff; + + Key = Vmm.GetPhysicalAddress(Key); + + if (Key == -1) + { + //FIXME: Should'nt ignore invalid addresses. + return; + } + + if (IsFrameBufferPosition(Key)) + { + //This texture is a frame buffer texture, + //we shouldn't read anything from memory and bind + //the frame buffer texture instead, since we're not + //really writing anything to memory. + Gpu.Renderer.RenderTarget.BindTexture(Key, TexIndex); + } + else + { + GalImage NewImage = TextureFactory.MakeTexture(Vmm, TicPosition); + + long Size = (uint)ImageUtils.GetSize(NewImage); + + bool HasCachedTexture = false; + + if (Gpu.Renderer.Texture.TryGetCachedTexture(Key, Size, out GalImage Image)) + { + if (NewImage.Equals(Image) && !QueryKeyUpload(Vmm, Key, Size, NvGpuBufferType.Texture)) + { + Gpu.Renderer.Texture.Bind(Key, TexIndex); + + HasCachedTexture = true; + } + } + + if (!HasCachedTexture) + { + byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition); + + Gpu.Renderer.Texture.Create(Key, Data, NewImage); + } + + Gpu.Renderer.Texture.Bind(Key, TexIndex); + } + + Gpu.Renderer.Texture.SetSampler(Sampler); + } + + private void UploadConstBuffers(NvGpuVmm Vmm, GalPipelineState State, long[] Keys) + { + for (int Stage = 0; Stage < Keys.Length; Stage++) + { + foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetConstBufferUsage(Keys[Stage])) + { + ConstBuffer Cb = ConstBuffers[Stage][DeclInfo.Cbuf]; + + if (!Cb.Enabled) + { + continue; + } + + long Key = Vmm.GetPhysicalAddress(Cb.Position); + + if (QueryKeyUpload(Vmm, Key, Cb.Size, NvGpuBufferType.ConstBuffer)) + { + IntPtr Source = Vmm.GetHostAddress(Cb.Position, Cb.Size); + + Gpu.Renderer.Buffer.SetData(Key, Cb.Size, Source); + } + + State.ConstBufferKeys[Stage][DeclInfo.Cbuf] = Key; + } + } + } + + private void UploadVertexArrays(NvGpuVmm Vmm, GalPipelineState State) + { + long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); + + long IboKey = Vmm.GetPhysicalAddress(IndexPosition); + + int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); + int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); + + GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt; + + int IndexEntrySize = 1 << IndexEntryFmt; + + if (IndexEntrySize > 4) + { + throw new InvalidOperationException(); + } + + if (IndexCount != 0) + { + int IbSize = IndexCount * IndexEntrySize; + + bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize); + + if (!IboCached || QueryKeyUpload(Vmm, IboKey, (uint)IbSize, NvGpuBufferType.Index)) + { + IntPtr DataAddress = Vmm.GetHostAddress(IndexPosition, IbSize); + + Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, DataAddress); + } + + Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat); + } + + List[] Attribs = new List[32]; + + for (int Attr = 0; Attr < 16; Attr++) + { + int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr); + + int ArrayIndex = Packed & 0x1f; + + if (Attribs[ArrayIndex] == null) + { + Attribs[ArrayIndex] = new List(); + } + + Attribs[ArrayIndex].Add(new GalVertexAttrib( + Attr, + ((Packed >> 6) & 0x1) != 0, + (Packed >> 7) & 0x3fff, + (GalVertexAttribSize)((Packed >> 21) & 0x3f), + (GalVertexAttribType)((Packed >> 27) & 0x7), + ((Packed >> 31) & 0x1) != 0)); + } + + State.VertexBindings = new GalVertexBinding[32]; + + for (int Index = 0; Index < 32; Index++) + { + if (Attribs[Index] == null) + { + continue; + } + + int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4); + + bool Enable = (Control & 0x1000) != 0; + + if (!Enable) + { + continue; + } + + long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4); + long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2); + + int VertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + Index * 4); + + bool Instanced = (ReadRegister(NvGpuEngine3dReg.VertexArrayNInstance + Index) & 1) != 0; + + int Stride = Control & 0xfff; + + if (Instanced && VertexDivisor != 0) + { + VertexPosition += Stride * (CurrentInstance / VertexDivisor); + } + + if (VertexPosition > VertexEndPos) + { + //Instance is invalid, ignore the draw call + continue; + } + + long VboKey = Vmm.GetPhysicalAddress(VertexPosition); + + long VbSize = (VertexEndPos - VertexPosition) + 1; + + bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, VbSize); + + if (!VboCached || QueryKeyUpload(Vmm, VboKey, VbSize, NvGpuBufferType.Vertex)) + { + IntPtr DataAddress = Vmm.GetHostAddress(VertexPosition, VbSize); + + Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, DataAddress); + } + + State.VertexBindings[Index].Enabled = true; + State.VertexBindings[Index].Stride = Stride; + State.VertexBindings[Index].VboKey = VboKey; + State.VertexBindings[Index].Instanced = Instanced; + State.VertexBindings[Index].Divisor = VertexDivisor; + State.VertexBindings[Index].Attribs = Attribs[Index].ToArray(); + } + } + + private void DispatchRender(NvGpuVmm Vmm, GalPipelineState State) + { + int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); + int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); + + GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); + + bool InstanceNext = ((PrimCtrl >> 26) & 1) != 0; + bool InstanceCont = ((PrimCtrl >> 27) & 1) != 0; + + if (InstanceNext && InstanceCont) + { + throw new InvalidOperationException("GPU tried to increase and reset instance count at the same time"); + } + + if (InstanceNext) + { + CurrentInstance++; + } + else if (!InstanceCont) + { + CurrentInstance = 0; + } + + State.Instance = CurrentInstance; + + Gpu.Renderer.Pipeline.Bind(State); + + if (IndexCount != 0) + { + int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); + int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); + int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase); + + long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); + + long IboKey = Vmm.GetPhysicalAddress(IndexPosition); + + Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType); + } + else + { + int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); + int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); + + Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType); + } + + //Is the GPU really clearing those registers after draw? + WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0); + WriteRegister(NvGpuEngine3dReg.IndexBatchCount, 0); + } + + private enum QueryMode + { + WriteSeq, + Sync, + WriteCounterAndTimestamp + } + + private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + WriteRegister(PBEntry); + + long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress); + + int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence]; + int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl]; + + QueryMode Mode = (QueryMode)(Ctrl & 3); + + switch (Mode) + { + case QueryMode.WriteSeq: Vmm.WriteInt32(Position, Seq); break; + + case QueryMode.WriteCounterAndTimestamp: + { + //TODO: Implement counters. + long Counter = 1; + + long Timestamp = (uint)Environment.TickCount; + + Timestamp = (long)(Timestamp * 615384.615385); + + Vmm.WriteInt64(Position + 0, Counter); + Vmm.WriteInt64(Position + 8, Timestamp); + + break; + } + } + } + + private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); + + int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset); + + foreach (int Arg in PBEntry.Arguments) + { + Vmm.WriteInt32(Position + Offset, Arg); + + Offset += 4; + } + + WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset); + + UploadedKeys[(int)NvGpuBufferType.ConstBuffer].Clear(); + } + + private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + int Stage = (PBEntry.Method - 0x904) >> 3; + + int Index = PBEntry.Arguments[0]; + + bool Enabled = (Index & 1) != 0; + + Index = (Index >> 4) & 0x1f; + + long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); + + long CbKey = Vmm.GetPhysicalAddress(Position); + + int Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize); + + if (!Gpu.Renderer.Buffer.IsCached(CbKey, Size)) + { + Gpu.Renderer.Buffer.Create(CbKey, Size); + } + + ConstBuffer Cb = ConstBuffers[Stage][Index]; + + if (Cb.Position != Position || Cb.Enabled != Enabled || Cb.Size != Size) + { + ConstBuffers[Stage][Index].Position = Position; + ConstBuffers[Stage][Index].Enabled = Enabled; + ConstBuffers[Stage][Index].Size = Size; + } + } + + private float GetFlipSign(NvGpuEngine3dReg Reg) + { + return MathF.Sign(ReadRegisterFloat(Reg)); + } + + private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg) + { + return + (long)Registers[(int)Reg + 0] << 32 | + (uint)Registers[(int)Reg + 1]; + } + + private void WriteRegister(NvGpuPBEntry PBEntry) + { + int ArgsCount = PBEntry.Arguments.Count; + + if (ArgsCount > 0) + { + Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; + } + } + + private int ReadRegister(NvGpuEngine3dReg Reg) + { + return Registers[(int)Reg]; + } + + private float ReadRegisterFloat(NvGpuEngine3dReg Reg) + { + return BitConverter.Int32BitsToSingle(ReadRegister(Reg)); + } + + private void WriteRegister(NvGpuEngine3dReg Reg, int Value) + { + Registers[(int)Reg] = Value; + } + + public bool IsFrameBufferPosition(long Position) + { + return FrameBuffers.Contains(Position); + } + + private bool QueryKeyUpload(NvGpuVmm Vmm, long Key, long Size, NvGpuBufferType Type) + { + List Uploaded = UploadedKeys[(int)Type]; + + if (Uploaded.Contains(Key)) + { + return false; + } + + Uploaded.Add(Key); + + return Vmm.IsRegionModified(Key, Size, Type); + } + } +} diff --git a/Ryujinx.Graphics/NvGpuEngine3dReg.cs b/Ryujinx.Graphics/NvGpuEngine3dReg.cs new file mode 100644 index 00000000..f96e7115 --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngine3dReg.cs @@ -0,0 +1,101 @@ +namespace Ryujinx.Graphics +{ + enum NvGpuEngine3dReg + { + FrameBufferNAddress = 0x200, + FrameBufferNWidth = 0x202, + FrameBufferNHeight = 0x203, + FrameBufferNFormat = 0x204, + ViewportNScaleX = 0x280, + ViewportNScaleY = 0x281, + ViewportNScaleZ = 0x282, + ViewportNTranslateX = 0x283, + ViewportNTranslateY = 0x284, + ViewportNTranslateZ = 0x285, + ViewportNHoriz = 0x300, + ViewportNVert = 0x301, + VertexArrayFirst = 0x35d, + VertexArrayCount = 0x35e, + ClearNColor = 0x360, + ClearDepth = 0x364, + ClearStencil = 0x368, + StencilBackFuncRef = 0x3d5, + StencilBackMask = 0x3d6, + StencilBackFuncMask = 0x3d7, + RTSeparateFragData = 0x3eb, + ZetaAddress = 0x3f8, + ZetaFormat = 0x3fa, + ZetaBlockDimensions = 0x3fb, + ZetaLayerStride = 0x3fc, + VertexAttribNFormat = 0x458, + RTControl = 0x487, + ZetaHoriz = 0x48a, + ZetaVert = 0x48b, + ZetaArrayMode = 0x48c, + DepthTestEnable = 0x4b3, + IBlendEnable = 0x4b9, + DepthTestFunction = 0x4c3, + BlendSeparateAlpha = 0x4cf, + BlendEquationRgb = 0x4d0, + BlendFuncSrcRgb = 0x4d1, + BlendFuncDstRgb = 0x4d2, + BlendEquationAlpha = 0x4d3, + BlendFuncSrcAlpha = 0x4d4, + BlendFuncDstAlpha = 0x4d6, + BlendEnableMaster = 0x4d7, + IBlendNEnable = 0x4d8, + StencilEnable = 0x4e0, + StencilFrontOpFail = 0x4e1, + StencilFrontOpZFail = 0x4e2, + StencilFrontOpZPass = 0x4e3, + StencilFrontFuncFunc = 0x4e4, + StencilFrontFuncRef = 0x4e5, + StencilFrontFuncMask = 0x4e6, + StencilFrontMask = 0x4e7, + VertexArrayElemBase = 0x50d, + VertexArrayInstBase = 0x50e, + ZetaEnable = 0x54e, + TexHeaderPoolOffset = 0x55d, + TexSamplerPoolOffset = 0x557, + StencilTwoSideEnable = 0x565, + StencilBackOpFail = 0x566, + StencilBackOpZFail = 0x567, + StencilBackOpZPass = 0x568, + StencilBackFuncFunc = 0x569, + ShaderAddress = 0x582, + VertexBeginGl = 0x586, + PrimRestartEnable = 0x591, + PrimRestartIndex = 0x592, + IndexArrayAddress = 0x5f2, + IndexArrayEndAddr = 0x5f4, + IndexArrayFormat = 0x5f6, + IndexBatchFirst = 0x5f7, + IndexBatchCount = 0x5f8, + VertexArrayNInstance = 0x620, + CullFaceEnable = 0x646, + FrontFace = 0x647, + CullFace = 0x648, + QueryAddress = 0x6c0, + QuerySequence = 0x6c2, + QueryControl = 0x6c3, + VertexArrayNControl = 0x700, + VertexArrayNAddress = 0x701, + VertexArrayNDivisor = 0x703, + IBlendNSeparateAlpha = 0x780, + IBlendNEquationRgb = 0x781, + IBlendNFuncSrcRgb = 0x782, + IBlendNFuncDstRgb = 0x783, + IBlendNEquationAlpha = 0x784, + IBlendNFuncSrcAlpha = 0x785, + IBlendNFuncDstAlpha = 0x786, + VertexArrayNEndAddr = 0x7c0, + ShaderNControl = 0x800, + ShaderNOffset = 0x801, + ShaderNMaxGprs = 0x803, + ShaderNType = 0x804, + ConstBufferSize = 0x8e0, + ConstBufferAddress = 0x8e1, + ConstBufferOffset = 0x8e3, + TextureCbIndex = 0x982 + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuEngineDma.cs b/Ryujinx.Graphics/NvGpuEngineDma.cs new file mode 100644 index 00000000..04be742f --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngineDma.cs @@ -0,0 +1,143 @@ +using Ryujinx.Graphics.Memory; +using Ryujinx.Graphics.Texture; +using System.Collections.Generic; + +namespace Ryujinx.Graphics +{ + public class NvGpuEngineDma : INvGpuEngine + { + public int[] Registers { get; private set; } + + private NvGpu Gpu; + + private Dictionary Methods; + + public NvGpuEngineDma(NvGpu Gpu) + { + this.Gpu = Gpu; + + Registers = new int[0x1d6]; + + Methods = new Dictionary(); + + void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) + { + while (Count-- > 0) + { + Methods.Add(Meth, Method); + + Meth += Stride; + } + } + + AddMethod(0xc0, 1, 1, Execute); + } + + public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) + { + Method(Vmm, PBEntry); + } + else + { + WriteRegister(PBEntry); + } + } + + private void Execute(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + int Control = PBEntry.Arguments[0]; + + bool SrcLinear = ((Control >> 7) & 1) != 0; + bool DstLinear = ((Control >> 8) & 1) != 0; + + long SrcAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.SrcAddress); + long DstAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.DstAddress); + + int SrcPitch = ReadRegister(NvGpuEngineDmaReg.SrcPitch); + int DstPitch = ReadRegister(NvGpuEngineDmaReg.DstPitch); + + int DstBlkDim = ReadRegister(NvGpuEngineDmaReg.DstBlkDim); + int DstSizeX = ReadRegister(NvGpuEngineDmaReg.DstSizeX); + int DstSizeY = ReadRegister(NvGpuEngineDmaReg.DstSizeY); + int DstSizeZ = ReadRegister(NvGpuEngineDmaReg.DstSizeZ); + int DstPosXY = ReadRegister(NvGpuEngineDmaReg.DstPosXY); + int DstPosZ = ReadRegister(NvGpuEngineDmaReg.DstPosZ); + + int SrcBlkDim = ReadRegister(NvGpuEngineDmaReg.SrcBlkDim); + int SrcSizeX = ReadRegister(NvGpuEngineDmaReg.SrcSizeX); + int SrcSizeY = ReadRegister(NvGpuEngineDmaReg.SrcSizeY); + int SrcSizeZ = ReadRegister(NvGpuEngineDmaReg.SrcSizeZ); + int SrcPosXY = ReadRegister(NvGpuEngineDmaReg.SrcPosXY); + int SrcPosZ = ReadRegister(NvGpuEngineDmaReg.SrcPosZ); + + int DstPosX = (DstPosXY >> 0) & 0xffff; + int DstPosY = (DstPosXY >> 16) & 0xffff; + + int SrcPosX = (SrcPosXY >> 0) & 0xffff; + int SrcPosY = (SrcPosXY >> 16) & 0xffff; + + int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); + int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); + + ISwizzle SrcSwizzle; + + if (SrcLinear) + { + SrcSwizzle = new LinearSwizzle(SrcPitch, 1); + } + else + { + SrcSwizzle = new BlockLinearSwizzle(SrcSizeX, 1, SrcBlockHeight); + } + + ISwizzle DstSwizzle; + + if (DstLinear) + { + DstSwizzle = new LinearSwizzle(DstPitch, 1); + } + else + { + DstSwizzle = new BlockLinearSwizzle(DstSizeX, 1, DstBlockHeight); + } + + for (int Y = 0; Y < DstSizeY; Y++) + for (int X = 0; X < DstSizeX; X++) + { + long SrcOffset = SrcAddress + (uint)SrcSwizzle.GetSwizzleOffset(X, Y); + long DstOffset = DstAddress + (uint)DstSwizzle.GetSwizzleOffset(X, Y); + + Vmm.WriteByte(DstOffset, Vmm.ReadByte(SrcOffset)); + } + } + + private long MakeInt64From2xInt32(NvGpuEngineDmaReg Reg) + { + return + (long)Registers[(int)Reg + 0] << 32 | + (uint)Registers[(int)Reg + 1]; + } + + private void WriteRegister(NvGpuPBEntry PBEntry) + { + int ArgsCount = PBEntry.Arguments.Count; + + if (ArgsCount > 0) + { + Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; + } + } + + private int ReadRegister(NvGpuEngineDmaReg Reg) + { + return Registers[(int)Reg]; + } + + private void WriteRegister(NvGpuEngineDmaReg Reg, int Value) + { + Registers[(int)Reg] = Value; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuEngineDmaReg.cs b/Ryujinx.Graphics/NvGpuEngineDmaReg.cs new file mode 100644 index 00000000..b0fa1fbf --- /dev/null +++ b/Ryujinx.Graphics/NvGpuEngineDmaReg.cs @@ -0,0 +1,22 @@ +namespace Ryujinx.Graphics +{ + enum NvGpuEngineDmaReg + { + SrcAddress = 0x100, + DstAddress = 0x102, + SrcPitch = 0x104, + DstPitch = 0x105, + DstBlkDim = 0x1c3, + DstSizeX = 0x1c4, + DstSizeY = 0x1c5, + DstSizeZ = 0x1c6, + DstPosZ = 0x1c7, + DstPosXY = 0x1c8, + SrcBlkDim = 0x1ca, + SrcSizeX = 0x1cb, + SrcSizeY = 0x1cc, + SrcSizeZ = 0x1cd, + SrcPosZ = 0x1ce, + SrcPosXY = 0x1cf + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuFifo.cs b/Ryujinx.Graphics/NvGpuFifo.cs new file mode 100644 index 00000000..3b79a055 --- /dev/null +++ b/Ryujinx.Graphics/NvGpuFifo.cs @@ -0,0 +1,198 @@ +using Ryujinx.Graphics.Memory; +using System.Collections.Concurrent; +using System.Threading; + +namespace Ryujinx.Graphics +{ + public class NvGpuFifo + { + private const int MacrosCount = 0x80; + private const int MacroIndexMask = MacrosCount - 1; + + //Note: The size of the macro memory is unknown, we just make + //a guess here and use 256kb as the size. Increase if needed. + private const int MmeWords = 256 * 256; + + private NvGpu Gpu; + + private ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry[])> BufferQueue; + + private NvGpuEngine[] SubChannels; + + public AutoResetEvent Event { get; private set; } + + private struct CachedMacro + { + public int Position { get; private set; } + + private MacroInterpreter Interpreter; + + public CachedMacro(NvGpuFifo PFifo, INvGpuEngine Engine, int Position) + { + this.Position = Position; + + Interpreter = new MacroInterpreter(PFifo, Engine); + } + + public void PushParam(int Param) + { + Interpreter?.Fifo.Enqueue(Param); + } + + public void Execute(NvGpuVmm Vmm, int[] Mme, int Param) + { + Interpreter?.Execute(Vmm, Mme, Position, Param); + } + } + + private int CurrMacroPosition; + private int CurrMacroBindIndex; + + private CachedMacro[] Macros; + + private int[] Mme; + + public NvGpuFifo(NvGpu Gpu) + { + this.Gpu = Gpu; + + BufferQueue = new ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry[])>(); + + SubChannels = new NvGpuEngine[8]; + + Macros = new CachedMacro[MacrosCount]; + + Mme = new int[MmeWords]; + + Event = new AutoResetEvent(false); + } + + public void PushBuffer(NvGpuVmm Vmm, NvGpuPBEntry[] Buffer) + { + BufferQueue.Enqueue((Vmm, Buffer)); + + Event.Set(); + } + + public void DispatchCalls() + { + while (Step()); + } + + private (NvGpuVmm Vmm, NvGpuPBEntry[] Pb) Curr; + + private int CurrPbEntryIndex; + + public bool Step() + { + while (Curr.Pb == null || Curr.Pb.Length <= CurrPbEntryIndex) + { + if (!BufferQueue.TryDequeue(out Curr)) + { + return false; + } + + Gpu.Engine3d.ResetCache(); + + CurrPbEntryIndex = 0; + } + + CallMethod(Curr.Vmm, Curr.Pb[CurrPbEntryIndex++]); + + return true; + } + + private void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (PBEntry.Method < 0x80) + { + switch ((NvGpuFifoMeth)PBEntry.Method) + { + case NvGpuFifoMeth.BindChannel: + { + NvGpuEngine Engine = (NvGpuEngine)PBEntry.Arguments[0]; + + SubChannels[PBEntry.SubChannel] = Engine; + + break; + } + + case NvGpuFifoMeth.SetMacroUploadAddress: + { + CurrMacroPosition = PBEntry.Arguments[0]; + + break; + } + + case NvGpuFifoMeth.SendMacroCodeData: + { + foreach (int Arg in PBEntry.Arguments) + { + Mme[CurrMacroPosition++] = Arg; + } + break; + } + + case NvGpuFifoMeth.SetMacroBindingIndex: + { + CurrMacroBindIndex = PBEntry.Arguments[0]; + + break; + } + + case NvGpuFifoMeth.BindMacro: + { + int Position = PBEntry.Arguments[0]; + + Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position); + + break; + } + } + } + else + { + switch (SubChannels[PBEntry.SubChannel]) + { + case NvGpuEngine._2d: Call2dMethod (Vmm, PBEntry); break; + case NvGpuEngine._3d: Call3dMethod (Vmm, PBEntry); break; + case NvGpuEngine.Dma: CallDmaMethod(Vmm, PBEntry); break; + } + } + } + + private void Call2dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + Gpu.Engine2d.CallMethod(Vmm, PBEntry); + } + + private void Call3dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + if (PBEntry.Method < 0xe00) + { + Gpu.Engine3d.CallMethod(Vmm, PBEntry); + } + else + { + int MacroIndex = (PBEntry.Method >> 1) & MacroIndexMask; + + if ((PBEntry.Method & 1) != 0) + { + foreach (int Arg in PBEntry.Arguments) + { + Macros[MacroIndex].PushParam(Arg); + } + } + else + { + Macros[MacroIndex].Execute(Vmm, Mme, PBEntry.Arguments[0]); + } + } + } + + private void CallDmaMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) + { + Gpu.EngineDma.CallMethod(Vmm, PBEntry); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuFifoMeth.cs b/Ryujinx.Graphics/NvGpuFifoMeth.cs new file mode 100644 index 00000000..c5cb6e94 --- /dev/null +++ b/Ryujinx.Graphics/NvGpuFifoMeth.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.Graphics +{ + enum NvGpuFifoMeth + { + BindChannel = 0, + SetMacroUploadAddress = 0x45, + SendMacroCodeData = 0x46, + SetMacroBindingIndex = 0x47, + BindMacro = 0x48 + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/NvGpuMethod.cs b/Ryujinx.Graphics/NvGpuMethod.cs new file mode 100644 index 00000000..5babf2c3 --- /dev/null +++ b/Ryujinx.Graphics/NvGpuMethod.cs @@ -0,0 +1,6 @@ +using Ryujinx.Graphics.Memory; + +namespace Ryujinx.Graphics +{ + delegate void NvGpuMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ASTCDecoder.cs b/Ryujinx.Graphics/Texture/ASTCDecoder.cs new file mode 100644 index 00000000..1efa0255 --- /dev/null +++ b/Ryujinx.Graphics/Texture/ASTCDecoder.cs @@ -0,0 +1,1384 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; + +namespace Ryujinx.Graphics.Texture +{ + public class ASTCDecoderException : Exception + { + public ASTCDecoderException(string ExMsg) : base(ExMsg) { } + } + + //https://github.com/GammaUNC/FasTC/blob/master/ASTCEncoder/src/Decompressor.cpp + public static class ASTCDecoder + { + struct TexelWeightParams + { + public int Width; + public int Height; + public bool DualPlane; + public int MaxWeight; + public bool Error; + public bool VoidExtentLDR; + public bool VoidExtentHDR; + + public int GetPackedBitSize() + { + // How many indices do we have? + int Indices = Height * Width; + + if (DualPlane) + { + Indices *= 2; + } + + IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(MaxWeight); + + return IntEncoded.GetBitLength(Indices); + } + + public int GetNumWeightValues() + { + int Ret = Width * Height; + + if (DualPlane) + { + Ret *= 2; + } + + return Ret; + } + } + + public static byte[] DecodeToRGBA8888( + byte[] InputBuffer, + int BlockX, + int BlockY, + int BlockZ, + int X, + int Y, + int Z) + { + using (MemoryStream InputStream = new MemoryStream(InputBuffer)) + { + BinaryReader BinReader = new BinaryReader(InputStream); + + if (BlockX > 12 || BlockY > 12) + { + throw new ASTCDecoderException("Block size unsupported!"); + } + + if (BlockZ != 1 || Z != 1) + { + throw new ASTCDecoderException("3D compressed textures unsupported!"); + } + + using (MemoryStream OutputStream = new MemoryStream()) + { + int BlockIndex = 0; + + for (int j = 0; j < Y; j += BlockY) + { + for (int i = 0; i < X; i += BlockX) + { + int[] DecompressedData = new int[144]; + + DecompressBlock(BinReader.ReadBytes(0x10), DecompressedData, BlockX, BlockY); + + int DecompressedWidth = Math.Min(BlockX, X - i); + int DecompressedHeight = Math.Min(BlockY, Y - j); + int BaseOffsets = (j * X + i) * 4; + + for (int jj = 0; jj < DecompressedHeight; jj++) + { + OutputStream.Seek(BaseOffsets + jj * X * 4, SeekOrigin.Begin); + + byte[] OutputBuffer = new byte[DecompressedData.Length * sizeof(int)]; + Buffer.BlockCopy(DecompressedData, 0, OutputBuffer, 0, OutputBuffer.Length); + + OutputStream.Write(OutputBuffer, jj * BlockX * 4, DecompressedWidth * 4); + } + + BlockIndex++; + } + } + + return OutputStream.ToArray(); + } + } + } + + public static bool DecompressBlock( + byte[] InputBuffer, + int[] OutputBuffer, + int BlockWidth, + int BlockHeight) + { + BitArrayStream BitStream = new BitArrayStream(new BitArray(InputBuffer)); + TexelWeightParams TexelParams = DecodeBlockInfo(BitStream); + + if (TexelParams.Error) + { + throw new ASTCDecoderException("Invalid block mode"); + } + + if (TexelParams.VoidExtentLDR) + { + FillVoidExtentLDR(BitStream, OutputBuffer, BlockWidth, BlockHeight); + + return true; + } + + if (TexelParams.VoidExtentHDR) + { + throw new ASTCDecoderException("HDR void extent blocks are unsupported!"); + } + + if (TexelParams.Width > BlockWidth) + { + throw new ASTCDecoderException("Texel weight grid width should be smaller than block width"); + } + + if (TexelParams.Height > BlockHeight) + { + throw new ASTCDecoderException("Texel weight grid height should be smaller than block height"); + } + + // Read num partitions + int NumberPartitions = BitStream.ReadBits(2) + 1; + Debug.Assert(NumberPartitions <= 4); + + if (NumberPartitions == 4 && TexelParams.DualPlane) + { + throw new ASTCDecoderException("Dual plane mode is incompatible with four partition blocks"); + } + + // Based on the number of partitions, read the color endpoint mode for + // each partition. + + // Determine partitions, partition index, and color endpoint modes + int PlaneIndices = -1; + int PartitionIndex; + uint[] ColorEndpointMode = { 0, 0, 0, 0 }; + + BitArrayStream ColorEndpointStream = new BitArrayStream(new BitArray(16 * 8)); + + // Read extra config data... + uint BaseColorEndpointMode = 0; + + if (NumberPartitions == 1) + { + ColorEndpointMode[0] = (uint)BitStream.ReadBits(4); + PartitionIndex = 0; + } + else + { + PartitionIndex = BitStream.ReadBits(10); + BaseColorEndpointMode = (uint)BitStream.ReadBits(6); + } + + uint BaseMode = (BaseColorEndpointMode & 3); + + // Remaining bits are color endpoint data... + int NumberWeightBits = TexelParams.GetPackedBitSize(); + int RemainingBits = 128 - NumberWeightBits - BitStream.Position; + + // Consider extra bits prior to texel data... + uint ExtraColorEndpointModeBits = 0; + + if (BaseMode != 0) + { + switch (NumberPartitions) + { + case 2: ExtraColorEndpointModeBits += 2; break; + case 3: ExtraColorEndpointModeBits += 5; break; + case 4: ExtraColorEndpointModeBits += 8; break; + default: Debug.Assert(false); break; + } + } + + RemainingBits -= (int)ExtraColorEndpointModeBits; + + // Do we have a dual plane situation? + int PlaneSelectorBits = 0; + + if (TexelParams.DualPlane) + { + PlaneSelectorBits = 2; + } + + RemainingBits -= PlaneSelectorBits; + + // Read color data... + int ColorDataBits = RemainingBits; + + while (RemainingBits > 0) + { + int NumberBits = Math.Min(RemainingBits, 8); + int Bits = BitStream.ReadBits(NumberBits); + ColorEndpointStream.WriteBits(Bits, NumberBits); + RemainingBits -= 8; + } + + // Read the plane selection bits + PlaneIndices = BitStream.ReadBits(PlaneSelectorBits); + + // Read the rest of the CEM + if (BaseMode != 0) + { + uint ExtraColorEndpointMode = (uint)BitStream.ReadBits((int)ExtraColorEndpointModeBits); + uint TempColorEndpointMode = (ExtraColorEndpointMode << 6) | BaseColorEndpointMode; + TempColorEndpointMode >>= 2; + + bool[] C = new bool[4]; + + for (int i = 0; i < NumberPartitions; i++) + { + C[i] = (TempColorEndpointMode & 1) != 0; + TempColorEndpointMode >>= 1; + } + + byte[] M = new byte[4]; + + for (int i = 0; i < NumberPartitions; i++) + { + M[i] = (byte)(TempColorEndpointMode & 3); + TempColorEndpointMode >>= 2; + Debug.Assert(M[i] <= 3); + } + + for (int i = 0; i < NumberPartitions; i++) + { + ColorEndpointMode[i] = BaseMode; + if (!(C[i])) ColorEndpointMode[i] -= 1; + ColorEndpointMode[i] <<= 2; + ColorEndpointMode[i] |= M[i]; + } + } + else if (NumberPartitions > 1) + { + uint TempColorEndpointMode = BaseColorEndpointMode >> 2; + + for (uint i = 0; i < NumberPartitions; i++) + { + ColorEndpointMode[i] = TempColorEndpointMode; + } + } + + // Make sure everything up till here is sane. + for (int i = 0; i < NumberPartitions; i++) + { + Debug.Assert(ColorEndpointMode[i] < 16); + } + Debug.Assert(BitStream.Position + TexelParams.GetPackedBitSize() == 128); + + // Decode both color data and texel weight data + int[] ColorValues = new int[32]; // Four values * two endpoints * four maximum partitions + DecodeColorValues(ColorValues, ColorEndpointStream.ToByteArray(), ColorEndpointMode, NumberPartitions, ColorDataBits); + + ASTCPixel[][] EndPoints = new ASTCPixel[4][]; + EndPoints[0] = new ASTCPixel[2]; + EndPoints[1] = new ASTCPixel[2]; + EndPoints[2] = new ASTCPixel[2]; + EndPoints[3] = new ASTCPixel[2]; + + int ColorValuesPosition = 0; + + for (int i = 0; i < NumberPartitions; i++) + { + ComputeEndpoints(EndPoints[i], ColorValues, ColorEndpointMode[i], ref ColorValuesPosition); + } + + // Read the texel weight data. + byte[] TexelWeightData = (byte[])InputBuffer.Clone(); + + // Reverse everything + for (int i = 0; i < 8; i++) + { + byte a = ReverseByte(TexelWeightData[i]); + byte b = ReverseByte(TexelWeightData[15 - i]); + + TexelWeightData[i] = b; + TexelWeightData[15 - i] = a; + } + + // Make sure that higher non-texel bits are set to zero + int ClearByteStart = (TexelParams.GetPackedBitSize() >> 3) + 1; + TexelWeightData[ClearByteStart - 1] &= (byte)((1 << (TexelParams.GetPackedBitSize() % 8)) - 1); + + int cLen = 16 - ClearByteStart; + for (int i = ClearByteStart; i < ClearByteStart + cLen; i++) TexelWeightData[i] = 0; + + List TexelWeightValues = new List(); + BitArrayStream WeightBitStream = new BitArrayStream(new BitArray(TexelWeightData)); + + IntegerEncoded.DecodeIntegerSequence(TexelWeightValues, WeightBitStream, TexelParams.MaxWeight, TexelParams.GetNumWeightValues()); + + // Blocks can be at most 12x12, so we can have as many as 144 weights + int[][] Weights = new int[2][]; + Weights[0] = new int[144]; + Weights[1] = new int[144]; + + UnquantizeTexelWeights(Weights, TexelWeightValues, TexelParams, BlockWidth, BlockHeight); + + // Now that we have endpoints and weights, we can interpolate and generate + // the proper decoding... + for (int j = 0; j < BlockHeight; j++) + { + for (int i = 0; i < BlockWidth; i++) + { + int Partition = Select2DPartition(PartitionIndex, i, j, NumberPartitions, ((BlockHeight * BlockWidth) < 32)); + Debug.Assert(Partition < NumberPartitions); + + ASTCPixel Pixel = new ASTCPixel(0, 0, 0, 0); + for (int Component = 0; Component < 4; Component++) + { + int Component0 = EndPoints[Partition][0].GetComponent(Component); + Component0 = BitArrayStream.Replicate(Component0, 8, 16); + int Component1 = EndPoints[Partition][1].GetComponent(Component); + Component1 = BitArrayStream.Replicate(Component1, 8, 16); + + int Plane = 0; + + if (TexelParams.DualPlane && (((PlaneIndices + 1) & 3) == Component)) + { + Plane = 1; + } + + int Weight = Weights[Plane][j * BlockWidth + i]; + int FinalComponent = (Component0 * (64 - Weight) + Component1 * Weight + 32) / 64; + + if (FinalComponent == 65535) + { + Pixel.SetComponent(Component, 255); + } + else + { + double FinalComponentFloat = FinalComponent; + Pixel.SetComponent(Component, (int)(255.0 * (FinalComponentFloat / 65536.0) + 0.5)); + } + } + + OutputBuffer[j * BlockWidth + i] = Pixel.Pack(); + } + } + + return true; + } + + private static int Select2DPartition(int Seed, int X, int Y, int PartitionCount, bool IsSmallBlock) + { + return SelectPartition(Seed, X, Y, 0, PartitionCount, IsSmallBlock); + } + + private static int SelectPartition(int Seed, int X, int Y, int Z, int PartitionCount, bool IsSmallBlock) + { + if (PartitionCount == 1) + { + return 0; + } + + if (IsSmallBlock) + { + X <<= 1; + Y <<= 1; + Z <<= 1; + } + + Seed += (PartitionCount - 1) * 1024; + + int RightNum = Hash52((uint)Seed); + byte Seed01 = (byte)(RightNum & 0xF); + byte Seed02 = (byte)((RightNum >> 4) & 0xF); + byte Seed03 = (byte)((RightNum >> 8) & 0xF); + byte Seed04 = (byte)((RightNum >> 12) & 0xF); + byte Seed05 = (byte)((RightNum >> 16) & 0xF); + byte Seed06 = (byte)((RightNum >> 20) & 0xF); + byte Seed07 = (byte)((RightNum >> 24) & 0xF); + byte Seed08 = (byte)((RightNum >> 28) & 0xF); + byte Seed09 = (byte)((RightNum >> 18) & 0xF); + byte Seed10 = (byte)((RightNum >> 22) & 0xF); + byte Seed11 = (byte)((RightNum >> 26) & 0xF); + byte Seed12 = (byte)(((RightNum >> 30) | (RightNum << 2)) & 0xF); + + Seed01 *= Seed01; Seed02 *= Seed02; + Seed03 *= Seed03; Seed04 *= Seed04; + Seed05 *= Seed05; Seed06 *= Seed06; + Seed07 *= Seed07; Seed08 *= Seed08; + Seed09 *= Seed09; Seed10 *= Seed10; + Seed11 *= Seed11; Seed12 *= Seed12; + + int SeedHash1, SeedHash2, SeedHash3; + + if ((Seed & 1) != 0) + { + SeedHash1 = (Seed & 2) != 0 ? 4 : 5; + SeedHash2 = (PartitionCount == 3) ? 6 : 5; + } + else + { + SeedHash1 = (PartitionCount == 3) ? 6 : 5; + SeedHash2 = (Seed & 2) != 0 ? 4 : 5; + } + + SeedHash3 = (Seed & 0x10) != 0 ? SeedHash1 : SeedHash2; + + Seed01 >>= SeedHash1; Seed02 >>= SeedHash2; Seed03 >>= SeedHash1; Seed04 >>= SeedHash2; + Seed05 >>= SeedHash1; Seed06 >>= SeedHash2; Seed07 >>= SeedHash1; Seed08 >>= SeedHash2; + Seed09 >>= SeedHash3; Seed10 >>= SeedHash3; Seed11 >>= SeedHash3; Seed12 >>= SeedHash3; + + int a = Seed01 * X + Seed02 * Y + Seed11 * Z + (RightNum >> 14); + int b = Seed03 * X + Seed04 * Y + Seed12 * Z + (RightNum >> 10); + int c = Seed05 * X + Seed06 * Y + Seed09 * Z + (RightNum >> 6); + int d = Seed07 * X + Seed08 * Y + Seed10 * Z + (RightNum >> 2); + + a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F; + + if (PartitionCount < 4) d = 0; + if (PartitionCount < 3) c = 0; + + if (a >= b && a >= c && a >= d) return 0; + else if (b >= c && b >= d) return 1; + else if (c >= d) return 2; + return 3; + } + + static int Hash52(uint Val) + { + Val ^= Val >> 15; Val -= Val << 17; Val += Val << 7; Val += Val << 4; + Val ^= Val >> 5; Val += Val << 16; Val ^= Val >> 7; Val ^= Val >> 3; + Val ^= Val << 6; Val ^= Val >> 17; + + return (int)Val; + } + + static void UnquantizeTexelWeights( + int[][] OutputBuffer, + List Weights, + TexelWeightParams TexelParams, + int BlockWidth, + int BlockHeight) + { + int WeightIndices = 0; + int[][] Unquantized = new int[2][]; + Unquantized[0] = new int[144]; + Unquantized[1] = new int[144]; + + for (int i = 0; i < Weights.Count; i++) + { + Unquantized[0][WeightIndices] = UnquantizeTexelWeight(Weights[i]); + + if (TexelParams.DualPlane) + { + i++; + Unquantized[1][WeightIndices] = UnquantizeTexelWeight(Weights[i]); + + if (i == Weights.Count) + { + break; + } + } + + if (++WeightIndices >= (TexelParams.Width * TexelParams.Height)) break; + } + + // Do infill if necessary (Section C.2.18) ... + int Ds = (1024 + (BlockWidth / 2)) / (BlockWidth - 1); + int Dt = (1024 + (BlockHeight / 2)) / (BlockHeight - 1); + + int PlaneScale = TexelParams.DualPlane ? 2 : 1; + + for (int Plane = 0; Plane < PlaneScale; Plane++) + { + for (int t = 0; t < BlockHeight; t++) + { + for (int s = 0; s < BlockWidth; s++) + { + int cs = Ds * s; + int ct = Dt * t; + + int gs = (cs * (TexelParams.Width - 1) + 32) >> 6; + int gt = (ct * (TexelParams.Height - 1) + 32) >> 6; + + int js = gs >> 4; + int fs = gs & 0xF; + + int jt = gt >> 4; + int ft = gt & 0x0F; + + int w11 = (fs * ft + 8) >> 4; + int w10 = ft - w11; + int w01 = fs - w11; + int w00 = 16 - fs - ft + w11; + + int v0 = js + jt * TexelParams.Width; + + int p00 = 0; + int p01 = 0; + int p10 = 0; + int p11 = 0; + + if (v0 < (TexelParams.Width * TexelParams.Height)) + { + p00 = Unquantized[Plane][v0]; + } + + if (v0 + 1 < (TexelParams.Width * TexelParams.Height)) + { + p01 = Unquantized[Plane][v0 + 1]; + } + + if (v0 + TexelParams.Width < (TexelParams.Width * TexelParams.Height)) + { + p10 = Unquantized[Plane][v0 + TexelParams.Width]; + } + + if (v0 + TexelParams.Width + 1 < (TexelParams.Width * TexelParams.Height)) + { + p11 = Unquantized[Plane][v0 + TexelParams.Width + 1]; + } + + OutputBuffer[Plane][t * BlockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4; + } + } + } + } + + static int UnquantizeTexelWeight(IntegerEncoded IntEncoded) + { + int BitValue = IntEncoded.BitValue; + int BitLength = IntEncoded.NumberBits; + + int A = BitArrayStream.Replicate(BitValue & 1, 1, 7); + int B = 0, C = 0, D = 0; + + int Result = 0; + + switch (IntEncoded.GetEncoding()) + { + case IntegerEncoded.EIntegerEncoding.JustBits: + Result = BitArrayStream.Replicate(BitValue, BitLength, 6); + break; + + case IntegerEncoded.EIntegerEncoding.Trit: + { + D = IntEncoded.TritValue; + Debug.Assert(D < 3); + + switch (BitLength) + { + case 0: + { + int[] Results = { 0, 32, 63 }; + Result = Results[D]; + + break; + } + + case 1: + { + C = 50; + break; + } + + case 2: + { + C = 23; + int b = (BitValue >> 1) & 1; + B = (b << 6) | (b << 2) | b; + + break; + } + + case 3: + { + C = 11; + int cb = (BitValue >> 1) & 3; + B = (cb << 5) | cb; + + break; + } + + default: + throw new ASTCDecoderException("Invalid trit encoding for texel weight"); + } + + break; + } + + case IntegerEncoded.EIntegerEncoding.Quint: + { + D = IntEncoded.QuintValue; + Debug.Assert(D < 5); + + switch (BitLength) + { + case 0: + { + int[] Results = { 0, 16, 32, 47, 63 }; + Result = Results[D]; + + break; + } + + case 1: + { + C = 28; + + break; + } + + case 2: + { + C = 13; + int b = (BitValue >> 1) & 1; + B = (b << 6) | (b << 1); + + break; + } + + default: + throw new ASTCDecoderException("Invalid quint encoding for texel weight"); + } + + break; + } + } + + if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && BitLength > 0) + { + // Decode the value... + Result = D * C + B; + Result ^= A; + Result = (A & 0x20) | (Result >> 2); + } + + Debug.Assert(Result < 64); + + // Change from [0,63] to [0,64] + if (Result > 32) + { + Result += 1; + } + + return Result; + } + + static byte ReverseByte(byte b) + { + // Taken from http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64Bits + return (byte)((((b) * 0x80200802L) & 0x0884422110L) * 0x0101010101L >> 32); + } + + static uint[] ReadUintColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) + { + uint[] Ret = new uint[Number]; + + for (int i = 0; i < Number; i++) + { + Ret[i] = (uint)ColorValues[ColorValuesPosition++]; + } + + return Ret; + } + + static int[] ReadIntColorValues(int Number, int[] ColorValues, ref int ColorValuesPosition) + { + int[] Ret = new int[Number]; + + for (int i = 0; i < Number; i++) + { + Ret[i] = ColorValues[ColorValuesPosition++]; + } + + return Ret; + } + + static void ComputeEndpoints( + ASTCPixel[] EndPoints, + int[] ColorValues, + uint ColorEndpointMode, + ref int ColorValuesPosition) + { + switch (ColorEndpointMode) + { + case 0: + { + uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[0], (short)Val[0]); + EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[1], (short)Val[1]); + + break; + } + + + case 1: + { + uint[] Val = ReadUintColorValues(2, ColorValues, ref ColorValuesPosition); + int L0 = (int)((Val[0] >> 2) | (Val[1] & 0xC0)); + int L1 = (int)Math.Max(L0 + (Val[1] & 0x3F), 0xFFU); + + EndPoints[0] = new ASTCPixel(0xFF, (short)L0, (short)L0, (short)L0); + EndPoints[1] = new ASTCPixel(0xFF, (short)L1, (short)L1, (short)L1); + + break; + } + + case 4: + { + uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); + EndPoints[1] = new ASTCPixel((short)Val[3], (short)Val[1], (short)Val[1], (short)Val[1]); + + break; + } + + case 5: + { + int[] Val = ReadIntColorValues(4, ColorValues, ref ColorValuesPosition); + + BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); + BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); + + EndPoints[0] = new ASTCPixel((short)Val[2], (short)Val[0], (short)Val[0], (short)Val[0]); + EndPoints[1] = new ASTCPixel((short)(Val[2] + Val[3]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1]), (short)(Val[0] + Val[1])); + + EndPoints[0].ClampByte(); + EndPoints[1].ClampByte(); + + break; + } + + case 6: + { + uint[] Val = ReadUintColorValues(4, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel(0xFF, (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); + EndPoints[1] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[1], (short)Val[2]); + + break; + } + + case 8: + { + uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); + + if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) + { + EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract(0xFF, (short)Val[1], (short)Val[3], (short)Val[5]); + EndPoints[1] = ASTCPixel.BlueContract(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); + } + + break; + } + + case 9: + { + int[] Val = ReadIntColorValues(6, ColorValues, ref ColorValuesPosition); + + BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); + BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); + BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); + + if (Val[1] + Val[3] + Val[5] >= 0) + { + EndPoints[0] = new ASTCPixel(0xFF, (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel(0xFF, (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract(0xFF, Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); + EndPoints[1] = ASTCPixel.BlueContract(0xFF, Val[0], Val[2], Val[4]); + } + + EndPoints[0].ClampByte(); + EndPoints[1].ClampByte(); + + break; + } + + case 10: + { + uint[] Val = ReadUintColorValues(6, ColorValues, ref ColorValuesPosition); + + EndPoints[0] = new ASTCPixel((short)Val[4], (short)(Val[0] * Val[3] >> 8), (short)(Val[1] * Val[3] >> 8), (short)(Val[2] * Val[3] >> 8)); + EndPoints[1] = new ASTCPixel((short)Val[5], (short)Val[0], (short)Val[1], (short)Val[2]); + + break; + } + + case 12: + { + uint[] Val = ReadUintColorValues(8, ColorValues, ref ColorValuesPosition); + + if (Val[1] + Val[3] + Val[5] >= Val[0] + Val[2] + Val[4]) + { + EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract((short)Val[7], (short)Val[1], (short)Val[3], (short)Val[5]); + EndPoints[1] = ASTCPixel.BlueContract((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); + } + + break; + } + + case 13: + { + int[] Val = ReadIntColorValues(8, ColorValues, ref ColorValuesPosition); + + BitArrayStream.BitTransferSigned(ref Val[1], ref Val[0]); + BitArrayStream.BitTransferSigned(ref Val[3], ref Val[2]); + BitArrayStream.BitTransferSigned(ref Val[5], ref Val[4]); + BitArrayStream.BitTransferSigned(ref Val[7], ref Val[6]); + + if (Val[1] + Val[3] + Val[5] >= 0) + { + EndPoints[0] = new ASTCPixel((short)Val[6], (short)Val[0], (short)Val[2], (short)Val[4]); + EndPoints[1] = new ASTCPixel((short)(Val[7] + Val[6]), (short)(Val[0] + Val[1]), (short)(Val[2] + Val[3]), (short)(Val[4] + Val[5])); + } + else + { + EndPoints[0] = ASTCPixel.BlueContract(Val[6] + Val[7], Val[0] + Val[1], Val[2] + Val[3], Val[4] + Val[5]); + EndPoints[1] = ASTCPixel.BlueContract(Val[6], Val[0], Val[2], Val[4]); + } + + EndPoints[0].ClampByte(); + EndPoints[1].ClampByte(); + + break; + } + + default: + throw new ASTCDecoderException("Unsupported color endpoint mode (is it HDR?)"); + } + } + + static void DecodeColorValues( + int[] OutputValues, + byte[] InputData, + uint[] Modes, + int NumberPartitions, + int NumberBitsForColorData) + { + // First figure out how many color values we have + int NumberValues = 0; + + for (int i = 0; i < NumberPartitions; i++) + { + NumberValues += (int)((Modes[i] >> 2) + 1) << 1; + } + + // Then based on the number of values and the remaining number of bits, + // figure out the max value for each of them... + int Range = 256; + + while (--Range > 0) + { + IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(Range); + int BitLength = IntEncoded.GetBitLength(NumberValues); + + if (BitLength <= NumberBitsForColorData) + { + // Find the smallest possible range that matches the given encoding + while (--Range > 0) + { + IntegerEncoded NewIntEncoded = IntegerEncoded.CreateEncoding(Range); + if (!NewIntEncoded.MatchesEncoding(IntEncoded)) + { + break; + } + } + + // Return to last matching range. + Range++; + break; + } + } + + // We now have enough to decode our integer sequence. + List IntegerEncodedSequence = new List(); + BitArrayStream ColorBitStream = new BitArrayStream(new BitArray(InputData)); + + IntegerEncoded.DecodeIntegerSequence(IntegerEncodedSequence, ColorBitStream, Range, NumberValues); + + // Once we have the decoded values, we need to dequantize them to the 0-255 range + // This procedure is outlined in ASTC spec C.2.13 + int OutputIndices = 0; + + foreach (IntegerEncoded IntEncoded in IntegerEncodedSequence) + { + int BitLength = IntEncoded.NumberBits; + int BitValue = IntEncoded.BitValue; + + Debug.Assert(BitLength >= 1); + + int A = 0, B = 0, C = 0, D = 0; + // A is just the lsb replicated 9 times. + A = BitArrayStream.Replicate(BitValue & 1, 1, 9); + + switch (IntEncoded.GetEncoding()) + { + case IntegerEncoded.EIntegerEncoding.JustBits: + { + OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8); + + break; + } + + case IntegerEncoded.EIntegerEncoding.Trit: + { + D = IntEncoded.TritValue; + + switch (BitLength) + { + case 1: + { + C = 204; + + break; + } + + case 2: + { + C = 93; + // B = b000b0bb0 + int b = (BitValue >> 1) & 1; + B = (b << 8) | (b << 4) | (b << 2) | (b << 1); + + break; + } + + case 3: + { + C = 44; + // B = cb000cbcb + int cb = (BitValue >> 1) & 3; + B = (cb << 7) | (cb << 2) | cb; + + break; + } + + + case 4: + { + C = 22; + // B = dcb000dcb + int dcb = (BitValue >> 1) & 7; + B = (dcb << 6) | dcb; + + break; + } + + case 5: + { + C = 11; + // B = edcb000ed + int edcb = (BitValue >> 1) & 0xF; + B = (edcb << 5) | (edcb >> 2); + + break; + } + + case 6: + { + C = 5; + // B = fedcb000f + int fedcb = (BitValue >> 1) & 0x1F; + B = (fedcb << 4) | (fedcb >> 4); + + break; + } + + default: + throw new ASTCDecoderException("Unsupported trit encoding for color values!"); + } + + break; + } + + case IntegerEncoded.EIntegerEncoding.Quint: + { + D = IntEncoded.QuintValue; + + switch (BitLength) + { + case 1: + { + C = 113; + + break; + } + + case 2: + { + C = 54; + // B = b0000bb00 + int b = (BitValue >> 1) & 1; + B = (b << 8) | (b << 3) | (b << 2); + + break; + } + + case 3: + { + C = 26; + // B = cb0000cbc + int cb = (BitValue >> 1) & 3; + B = (cb << 7) | (cb << 1) | (cb >> 1); + + break; + } + + case 4: + { + C = 13; + // B = dcb0000dc + int dcb = (BitValue >> 1) & 7; + B = (dcb << 6) | (dcb >> 1); + + break; + } + + case 5: + { + C = 6; + // B = edcb0000e + int edcb = (BitValue >> 1) & 0xF; + B = (edcb << 5) | (edcb >> 3); + + break; + } + + default: + throw new ASTCDecoderException("Unsupported quint encoding for color values!"); + } + break; + } + } + + if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits) + { + int T = D * C + B; + T ^= A; + T = (A & 0x80) | (T >> 2); + + OutputValues[OutputIndices++] = T; + } + } + + // Make sure that each of our values is in the proper range... + for (int i = 0; i < NumberValues; i++) + { + Debug.Assert(OutputValues[i] <= 255); + } + } + + static void FillVoidExtentLDR(BitArrayStream BitStream, int[] OutputBuffer, int BlockWidth, int BlockHeight) + { + // Don't actually care about the void extent, just read the bits... + for (int i = 0; i < 4; ++i) + { + BitStream.ReadBits(13); + } + + // Decode the RGBA components and renormalize them to the range [0, 255] + ushort R = (ushort)BitStream.ReadBits(16); + ushort G = (ushort)BitStream.ReadBits(16); + ushort B = (ushort)BitStream.ReadBits(16); + ushort A = (ushort)BitStream.ReadBits(16); + + int RGBA = (R >> 8) | (G & 0xFF00) | ((B) & 0xFF00) << 8 | ((A) & 0xFF00) << 16; + + for (int j = 0; j < BlockHeight; j++) + { + for (int i = 0; i < BlockWidth; i++) + { + OutputBuffer[j * BlockWidth + i] = RGBA; + } + } + } + + static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream) + { + TexelWeightParams TexelParams = new TexelWeightParams(); + + // Read the entire block mode all at once + ushort ModeBits = (ushort)BitStream.ReadBits(11); + + // Does this match the void extent block mode? + if ((ModeBits & 0x01FF) == 0x1FC) + { + if ((ModeBits & 0x200) != 0) + { + TexelParams.VoidExtentHDR = true; + } + else + { + TexelParams.VoidExtentLDR = true; + } + + // Next two bits must be one. + if ((ModeBits & 0x400) == 0 || BitStream.ReadBits(1) == 0) + { + TexelParams.Error = true; + } + + return TexelParams; + } + + // First check if the last four bits are zero + if ((ModeBits & 0xF) == 0) + { + TexelParams.Error = true; + return TexelParams; + } + + // If the last two bits are zero, then if bits + // [6-8] are all ones, this is also reserved. + if ((ModeBits & 0x3) == 0 && (ModeBits & 0x1C0) == 0x1C0) + { + TexelParams.Error = true; + + return TexelParams; + } + + // Otherwise, there is no error... Figure out the layout + // of the block mode. Layout is determined by a number + // between 0 and 9 corresponding to table C.2.8 of the + // ASTC spec. + int Layout = 0; + + if ((ModeBits & 0x1) != 0 || (ModeBits & 0x2) != 0) + { + // layout is in [0-4] + if ((ModeBits & 0x8) != 0) + { + // layout is in [2-4] + if ((ModeBits & 0x4) != 0) + { + // layout is in [3-4] + if ((ModeBits & 0x100) != 0) + { + Layout = 4; + } + else + { + Layout = 3; + } + } + else + { + Layout = 2; + } + } + else + { + // layout is in [0-1] + if ((ModeBits & 0x4) != 0) + { + Layout = 1; + } + else + { + Layout = 0; + } + } + } + else + { + // layout is in [5-9] + if ((ModeBits & 0x100) != 0) + { + // layout is in [7-9] + if ((ModeBits & 0x80) != 0) + { + // layout is in [7-8] + Debug.Assert((ModeBits & 0x40) == 0); + + if ((ModeBits & 0x20) != 0) + { + Layout = 8; + } + else + { + Layout = 7; + } + } + else + { + Layout = 9; + } + } + else + { + // layout is in [5-6] + if ((ModeBits & 0x80) != 0) + { + Layout = 6; + } + else + { + Layout = 5; + } + } + } + + Debug.Assert(Layout < 10); + + // Determine R + int R = (ModeBits >> 4) & 1; + if (Layout < 5) + { + R |= (ModeBits & 0x3) << 1; + } + else + { + R |= (ModeBits & 0xC) >> 1; + } + + Debug.Assert(2 <= R && R <= 7); + + // Determine width & height + switch (Layout) + { + case 0: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x3; + + TexelParams.Width = B + 4; + TexelParams.Height = A + 2; + + break; + } + + case 1: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x3; + + TexelParams.Width = B + 8; + TexelParams.Height = A + 2; + + break; + } + + case 2: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x3; + + TexelParams.Width = A + 2; + TexelParams.Height = B + 8; + + break; + } + + case 3: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x1; + + TexelParams.Width = A + 2; + TexelParams.Height = B + 6; + + break; + } + + case 4: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 7) & 0x1; + + TexelParams.Width = B + 2; + TexelParams.Height = A + 2; + + break; + } + + case 5: + { + int A = (ModeBits >> 5) & 0x3; + + TexelParams.Width = 12; + TexelParams.Height = A + 2; + + break; + } + + case 6: + { + int A = (ModeBits >> 5) & 0x3; + + TexelParams.Width = A + 2; + TexelParams.Height = 12; + + break; + } + + case 7: + { + TexelParams.Width = 6; + TexelParams.Height = 10; + + break; + } + + case 8: + { + TexelParams.Width = 10; + TexelParams.Height = 6; + break; + } + + case 9: + { + int A = (ModeBits >> 5) & 0x3; + int B = (ModeBits >> 9) & 0x3; + + TexelParams.Width = A + 6; + TexelParams.Height = B + 6; + + break; + } + + default: + //Don't know this layout... + TexelParams.Error = true; + break; + } + + // Determine whether or not we're using dual planes + // and/or high precision layouts. + bool D = ((Layout != 9) && ((ModeBits & 0x400) != 0)); + bool H = (Layout != 9) && ((ModeBits & 0x200) != 0); + + if (H) + { + int[] MaxWeights = { 9, 11, 15, 19, 23, 31 }; + TexelParams.MaxWeight = MaxWeights[R - 2]; + } + else + { + int[] MaxWeights = { 1, 2, 3, 4, 5, 7 }; + TexelParams.MaxWeight = MaxWeights[R - 2]; + } + + TexelParams.DualPlane = D; + + return TexelParams; + } + } +} diff --git a/Ryujinx.Graphics/Texture/ASTCPixel.cs b/Ryujinx.Graphics/Texture/ASTCPixel.cs new file mode 100644 index 00000000..c43eaf93 --- /dev/null +++ b/Ryujinx.Graphics/Texture/ASTCPixel.cs @@ -0,0 +1,138 @@ +using System; +using System.Diagnostics; + +namespace Ryujinx.Graphics.Texture +{ + class ASTCPixel + { + public short R { get; set; } + public short G { get; set; } + public short B { get; set; } + public short A { get; set; } + + byte[] BitDepth = new byte[4]; + + public ASTCPixel(short _A, short _R, short _G, short _B) + { + A = _A; + R = _R; + G = _G; + B = _B; + + for (int i = 0; i < 4; i++) + BitDepth[i] = 8; + } + + public void ClampByte() + { + R = Math.Min(Math.Max(R, (short)0), (short)255); + G = Math.Min(Math.Max(G, (short)0), (short)255); + B = Math.Min(Math.Max(B, (short)0), (short)255); + A = Math.Min(Math.Max(A, (short)0), (short)255); + } + + public short GetComponent(int Index) + { + switch(Index) + { + case 0: return A; + case 1: return R; + case 2: return G; + case 3: return B; + } + + return 0; + } + + public void SetComponent(int Index, int Value) + { + switch (Index) + { + case 0: + A = (short)Value; + break; + case 1: + R = (short)Value; + break; + case 2: + G = (short)Value; + break; + case 3: + B = (short)Value; + break; + } + } + + public void ChangeBitDepth(byte[] Depth) + { + for(int i = 0; i< 4; i++) + { + int Value = ChangeBitDepth(GetComponent(i), BitDepth[i], Depth[i]); + + SetComponent(i, Value); + BitDepth[i] = Depth[i]; + } + } + + short ChangeBitDepth(short Value, byte OldDepth, byte NewDepth) + { + Debug.Assert(NewDepth <= 8); + Debug.Assert(OldDepth <= 8); + + if (OldDepth == NewDepth) + { + // Do nothing + return Value; + } + else if (OldDepth == 0 && NewDepth != 0) + { + return (short)((1 << NewDepth) - 1); + } + else if (NewDepth > OldDepth) + { + return (short)BitArrayStream.Replicate(Value, OldDepth, NewDepth); + } + else + { + // oldDepth > newDepth + if (NewDepth == 0) + { + return 0xFF; + } + else + { + byte BitsWasted = (byte)(OldDepth - NewDepth); + short TempValue = Value; + + TempValue = (short)((TempValue + (1 << (BitsWasted - 1))) >> BitsWasted); + TempValue = Math.Min(Math.Max((short)0, TempValue), (short)((1 << NewDepth) - 1)); + + return (byte)(TempValue); + } + } + } + + public int Pack() + { + ASTCPixel NewPixel = new ASTCPixel(A, R, G, B); + byte[] eightBitDepth = { 8, 8, 8, 8 }; + + NewPixel.ChangeBitDepth(eightBitDepth); + + return (byte)NewPixel.A << 24 | + (byte)NewPixel.B << 16 | + (byte)NewPixel.G << 8 | + (byte)NewPixel.R << 0; + } + + // Adds more precision to the blue channel as described + // in C.2.14 + public static ASTCPixel BlueContract(int a, int r, int g, int b) + { + return new ASTCPixel((short)(a), + (short)((r + b) >> 1), + (short)((g + b) >> 1), + (short)(b)); + } + } +} diff --git a/Ryujinx.Graphics/Texture/BitArrayStream.cs b/Ryujinx.Graphics/Texture/BitArrayStream.cs new file mode 100644 index 00000000..2a8ed091 --- /dev/null +++ b/Ryujinx.Graphics/Texture/BitArrayStream.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections; + +namespace Ryujinx.Graphics.Texture +{ + public class BitArrayStream + { + public BitArray BitsArray; + + public int Position { get; private set; } + + public BitArrayStream(BitArray BitArray) + { + BitsArray = BitArray; + Position = 0; + } + + public short ReadBits(int Length) + { + int RetValue = 0; + for (int i = Position; i < Position + Length; i++) + { + if (BitsArray[i]) + { + RetValue |= 1 << (i - Position); + } + } + + Position += Length; + return (short)RetValue; + } + + public int ReadBits(int Start, int End) + { + int RetValue = 0; + for (int i = Start; i <= End; i++) + { + if (BitsArray[i]) + { + RetValue |= 1 << (i - Start); + } + } + + return RetValue; + } + + public int ReadBit(int Index) + { + return Convert.ToInt32(BitsArray[Index]); + } + + public void WriteBits(int Value, int Length) + { + for (int i = Position; i < Position + Length; i++) + { + BitsArray[i] = ((Value >> (i - Position)) & 1) != 0; + } + + Position += Length; + } + + public byte[] ToByteArray() + { + byte[] RetArray = new byte[(BitsArray.Length + 7) / 8]; + BitsArray.CopyTo(RetArray, 0); + return RetArray; + } + + public static int Replicate(int Value, int NumberBits, int ToBit) + { + if (NumberBits == 0) return 0; + if (ToBit == 0) return 0; + + int TempValue = Value & ((1 << NumberBits) - 1); + int RetValue = TempValue; + int ResLength = NumberBits; + + while (ResLength < ToBit) + { + int Comp = 0; + if (NumberBits > ToBit - ResLength) + { + int NewShift = ToBit - ResLength; + Comp = NumberBits - NewShift; + NumberBits = NewShift; + } + RetValue <<= NumberBits; + RetValue |= TempValue >> Comp; + ResLength += NumberBits; + } + return RetValue; + } + + public static int PopCnt(int Number) + { + int Counter; + for (Counter = 0; Number != 0; Counter++) + { + Number &= Number - 1; + } + return Counter; + } + + public static void Swap(ref T lhs, ref T rhs) + { + T Temp = lhs; + lhs = rhs; + rhs = Temp; + } + + // Transfers a bit as described in C.2.14 + public static void BitTransferSigned(ref int a, ref int b) + { + b >>= 1; + b |= a & 0x80; + a >>= 1; + a &= 0x3F; + if ((a & 0x20) != 0) a -= 0x40; + } + } +} diff --git a/Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs b/Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs new file mode 100644 index 00000000..9451291e --- /dev/null +++ b/Ryujinx.Graphics/Texture/BlockLinearSwizzle.cs @@ -0,0 +1,59 @@ +using System; + +namespace Ryujinx.Graphics.Texture +{ + class BlockLinearSwizzle : ISwizzle + { + private int BhShift; + private int BppShift; + private int BhMask; + + private int XShift; + private int GobStride; + + public BlockLinearSwizzle(int Width, int Bpp, int BlockHeight = 16) + { + BhMask = (BlockHeight * 8) - 1; + + BhShift = CountLsbZeros(BlockHeight * 8); + BppShift = CountLsbZeros(Bpp); + + int WidthInGobs = (int)MathF.Ceiling(Width * Bpp / 64f); + + GobStride = 512 * BlockHeight * WidthInGobs; + + XShift = CountLsbZeros(512 * BlockHeight); + } + + private int CountLsbZeros(int Value) + { + int Count = 0; + + while (((Value >> Count) & 1) == 0) + { + Count++; + } + + return Count; + } + + public int GetSwizzleOffset(int X, int Y) + { + X <<= BppShift; + + int Position = (Y >> BhShift) * GobStride; + + Position += (X >> 6) << XShift; + + Position += ((Y & BhMask) >> 3) << 9; + + Position += ((X & 0x3f) >> 5) << 8; + Position += ((Y & 0x07) >> 1) << 6; + Position += ((X & 0x1f) >> 4) << 5; + Position += ((Y & 0x01) >> 0) << 4; + Position += ((X & 0x0f) >> 0) << 0; + + return Position; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ISwizzle.cs b/Ryujinx.Graphics/Texture/ISwizzle.cs new file mode 100644 index 00000000..583fc20c --- /dev/null +++ b/Ryujinx.Graphics/Texture/ISwizzle.cs @@ -0,0 +1,7 @@ +namespace Ryujinx.Graphics.Texture +{ + interface ISwizzle + { + int GetSwizzleOffset(int X, int Y); + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ImageConverter.cs b/Ryujinx.Graphics/Texture/ImageConverter.cs new file mode 100644 index 00000000..89529061 --- /dev/null +++ b/Ryujinx.Graphics/Texture/ImageConverter.cs @@ -0,0 +1,24 @@ +namespace Ryujinx.Graphics.Texture +{ + static class ImageConverter + { + public static byte[] G8R8ToR8G8( + byte[] Data, + int Width, + int Height, + int Depth) + { + int Texels = Width * Height * Depth; + + byte[] Output = new byte[Texels * 2]; + + for (int Texel = 0; Texel < Texels; Texel++) + { + Output[Texel * 2 + 0] = Data[Texel * 2 + 1]; + Output[Texel * 2 + 1] = Data[Texel * 2 + 0]; + } + + return Output; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/ImageUtils.cs b/Ryujinx.Graphics/Texture/ImageUtils.cs new file mode 100644 index 00000000..ccea43ec --- /dev/null +++ b/Ryujinx.Graphics/Texture/ImageUtils.cs @@ -0,0 +1,357 @@ +using Ryujinx.Graphics.Gal; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Texture +{ + static class ImageUtils + { + struct ImageDescriptor + { + public TextureReaderDelegate Reader; + + public bool HasColor; + public bool HasDepth; + public bool HasStencil; + + public bool Compressed; + + public ImageDescriptor( + TextureReaderDelegate Reader, + bool HasColor, + bool HasDepth, + bool HasStencil, + bool Compressed) + { + this.Reader = Reader; + this.HasColor = HasColor; + this.HasDepth = HasDepth; + this.HasStencil = HasStencil; + this.Compressed = Compressed; + } + } + + private const GalImageFormat Snorm = GalImageFormat.Snorm; + private const GalImageFormat Unorm = GalImageFormat.Unorm; + private const GalImageFormat Sint = GalImageFormat.Sint; + private const GalImageFormat Uint = GalImageFormat.Uint; + private const GalImageFormat Sfloat = GalImageFormat.Sfloat; + + private static readonly Dictionary s_TextureTable = + new Dictionary() + { + { GalTextureFormat.R32G32B32A32, GalImageFormat.R32G32B32A32 | Sint | Uint | Sfloat }, + { GalTextureFormat.R16G16B16A16, GalImageFormat.R16G16B16A16 | Snorm | Unorm | Sint | Uint | Sfloat }, + { GalTextureFormat.R32G32, GalImageFormat.R32G32 | Sint | Uint | Sfloat }, + { GalTextureFormat.A8B8G8R8, GalImageFormat.A8B8G8R8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.A2B10G10R10, GalImageFormat.A2B10G10R10 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.G8R8, GalImageFormat.G8R8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.R16, GalImageFormat.R16 | Snorm | Unorm | Sint | Uint | Sfloat }, + { GalTextureFormat.R8, GalImageFormat.R8 | Snorm | Unorm | Sint | Uint }, + { GalTextureFormat.R32, GalImageFormat.R32 | Sint | Uint | Sfloat }, + { GalTextureFormat.A4B4G4R4, GalImageFormat.A4B4G4R4 | Unorm }, + { GalTextureFormat.A1B5G5R5, GalImageFormat.A1R5G5B5 | Unorm }, + { GalTextureFormat.B5G6R5, GalImageFormat.B5G6R5 | Unorm }, + { GalTextureFormat.BF10GF11RF11, GalImageFormat.B10G11R11 | Sfloat }, + { GalTextureFormat.Z24S8, GalImageFormat.D24_S8 | Unorm }, + { GalTextureFormat.ZF32, GalImageFormat.D32 | Sfloat }, + { GalTextureFormat.ZF32_X24S8, GalImageFormat.D32_S8 | Unorm }, + + //Compressed formats + { GalTextureFormat.BC6H_SF16, GalImageFormat.BC6H_SF16 | Unorm }, + { GalTextureFormat.BC6H_UF16, GalImageFormat.BC6H_UF16 | Unorm }, + { GalTextureFormat.BC7U, GalImageFormat.BC7 | Unorm }, + { GalTextureFormat.BC1, GalImageFormat.BC1_RGBA | Unorm }, + { GalTextureFormat.BC2, GalImageFormat.BC2 | Unorm }, + { GalTextureFormat.BC3, GalImageFormat.BC3 | Unorm }, + { GalTextureFormat.BC4, GalImageFormat.BC4 | Unorm | Snorm }, + { GalTextureFormat.BC5, GalImageFormat.BC5 | Unorm | Snorm }, + { GalTextureFormat.Astc2D4x4, GalImageFormat.ASTC_4x4 | Unorm }, + { GalTextureFormat.Astc2D5x5, GalImageFormat.ASTC_5x5 | Unorm }, + { GalTextureFormat.Astc2D6x6, GalImageFormat.ASTC_6x6 | Unorm }, + { GalTextureFormat.Astc2D8x8, GalImageFormat.ASTC_8x8 | Unorm }, + { GalTextureFormat.Astc2D10x10, GalImageFormat.ASTC_10x10 | Unorm }, + { GalTextureFormat.Astc2D12x12, GalImageFormat.ASTC_12x12 | Unorm }, + { GalTextureFormat.Astc2D5x4, GalImageFormat.ASTC_5x4 | Unorm }, + { GalTextureFormat.Astc2D6x5, GalImageFormat.ASTC_6x5 | Unorm }, + { GalTextureFormat.Astc2D8x6, GalImageFormat.ASTC_8x6 | Unorm }, + { GalTextureFormat.Astc2D10x8, GalImageFormat.ASTC_10x8 | Unorm }, + { GalTextureFormat.Astc2D12x10, GalImageFormat.ASTC_12x10 | Unorm }, + { GalTextureFormat.Astc2D8x5, GalImageFormat.ASTC_8x5 | Unorm }, + { GalTextureFormat.Astc2D10x5, GalImageFormat.ASTC_10x5 | Unorm }, + { GalTextureFormat.Astc2D10x6, GalImageFormat.ASTC_10x6 | Unorm } + }; + + private static readonly Dictionary s_ImageTable = + new Dictionary() + { + { GalImageFormat.R32G32B32A32, new ImageDescriptor(TextureReader.Read16Bpp, true, false, false, false) }, + { GalImageFormat.R16G16B16A16, new ImageDescriptor(TextureReader.Read8Bpp, true, false, false, false) }, + { GalImageFormat.R32G32, new ImageDescriptor(TextureReader.Read8Bpp, true, false, false, false) }, + { GalImageFormat.A8B8G8R8, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.A2B10G10R10, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.R32, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.A4B4G4R4, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.BC6H_SF16, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.BC6H_UF16, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.A1R5G5B5, new ImageDescriptor(TextureReader.Read5551, true, false, false, false) }, + { GalImageFormat.B5G6R5, new ImageDescriptor(TextureReader.Read565, true, false, false, false) }, + { GalImageFormat.BC7, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.R16G16, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.R8G8, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.G8R8, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.R16, new ImageDescriptor(TextureReader.Read2Bpp, true, false, false, false) }, + { GalImageFormat.R8, new ImageDescriptor(TextureReader.Read1Bpp, true, false, false, false) }, + { GalImageFormat.B10G11R11, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.A8B8G8R8_SRGB, new ImageDescriptor(TextureReader.Read4Bpp, true, false, false, false) }, + { GalImageFormat.BC1_RGBA, new ImageDescriptor(TextureReader.Read8Bpt4x4, true, false, false, true) }, + { GalImageFormat.BC2, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.BC3, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.BC4, new ImageDescriptor(TextureReader.Read8Bpt4x4, true, false, false, true) }, + { GalImageFormat.BC5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.ASTC_4x4, new ImageDescriptor(TextureReader.Read16BptCompressedTexture4x4, true, false, false, true) }, + { GalImageFormat.ASTC_5x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture5x5, true, false, false, true) }, + { GalImageFormat.ASTC_6x6, new ImageDescriptor(TextureReader.Read16BptCompressedTexture6x6, true, false, false, true) }, + { GalImageFormat.ASTC_8x8, new ImageDescriptor(TextureReader.Read16BptCompressedTexture8x8, true, false, false, true) }, + { GalImageFormat.ASTC_10x10, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x10, true, false, false, true) }, + { GalImageFormat.ASTC_12x12, new ImageDescriptor(TextureReader.Read16BptCompressedTexture12x12, true, false, false, true) }, + { GalImageFormat.ASTC_5x4, new ImageDescriptor(TextureReader.Read16BptCompressedTexture5x4, true, false, false, true) }, + { GalImageFormat.ASTC_6x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture6x5, true, false, false, true) }, + { GalImageFormat.ASTC_8x6, new ImageDescriptor(TextureReader.Read16BptCompressedTexture8x6, true, false, false, true) }, + { GalImageFormat.ASTC_10x8, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x8, true, false, false, true) }, + { GalImageFormat.ASTC_12x10, new ImageDescriptor(TextureReader.Read16BptCompressedTexture12x10, true, false, false, true) }, + { GalImageFormat.ASTC_8x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture8x5, true, false, false, true) }, + { GalImageFormat.ASTC_10x5, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x5, true, false, false, true) }, + { GalImageFormat.ASTC_10x6, new ImageDescriptor(TextureReader.Read16BptCompressedTexture10x6, true, false, false, true) }, + + { GalImageFormat.D24_S8, new ImageDescriptor(TextureReader.Read4Bpp, false, true, true, false) }, + { GalImageFormat.D32, new ImageDescriptor(TextureReader.Read4Bpp, false, true, false, false) }, + { GalImageFormat.D16, new ImageDescriptor(TextureReader.Read2Bpp, false, true, false, false) }, + { GalImageFormat.D32_S8, new ImageDescriptor(TextureReader.Read8Bpp, false, true, true, false) }, + }; + + public static GalImageFormat ConvertTexture( + GalTextureFormat Format, + GalTextureType RType, + GalTextureType GType, + GalTextureType BType, + GalTextureType AType) + { + if (RType != GType || RType != BType || RType != AType) + { + throw new NotImplementedException("Per component types are not implemented"); + } + + if (!s_TextureTable.TryGetValue(Format, out GalImageFormat ImageFormat)) + { + throw new NotImplementedException("Texture with format " + ((int)Format).ToString("x2") + " not implemented"); + } + + GalTextureType Type = RType; + + GalImageFormat FormatType = GetFormatType(RType); + + if (ImageFormat.HasFlag(FormatType)) + { + return (ImageFormat & GalImageFormat.FormatMask) | FormatType; + } + else + { + throw new NotImplementedException("Texture with format " + Format + + " and component type " + Type + " is not implemented"); + } + } + + public static GalImageFormat ConvertSurface(GalSurfaceFormat Format) + { + switch (Format) + { + case GalSurfaceFormat.RGBA32Float: return GalImageFormat.R32G32B32A32 | Sfloat; + case GalSurfaceFormat.RGBA16Float: return GalImageFormat.R16G16B16A16 | Sfloat; + case GalSurfaceFormat.RG32Float: return GalImageFormat.R32G32 | Sfloat; + case GalSurfaceFormat.RG32Sint: return GalImageFormat.R32G32 | Sint; + case GalSurfaceFormat.RG32Uint: return GalImageFormat.R32G32 | Uint; + case GalSurfaceFormat.BGRA8Unorm: return GalImageFormat.R8G8B8A8 | Unorm; //Is this right? + case GalSurfaceFormat.BGRA8Srgb: return GalImageFormat.A8B8G8R8_SRGB; //This one might be wrong + case GalSurfaceFormat.RGB10A2Unorm: return GalImageFormat.A2B10G10R10 | Unorm; + case GalSurfaceFormat.RGBA8Unorm: return GalImageFormat.A8B8G8R8 | Unorm; + case GalSurfaceFormat.RGBA8Srgb: return GalImageFormat.A8B8G8R8_SRGB; + case GalSurfaceFormat.RGBA8Snorm: return GalImageFormat.A8B8G8R8 | Snorm; + case GalSurfaceFormat.RG16Snorm: return GalImageFormat.R16G16 | Snorm; + case GalSurfaceFormat.RG16Float: return GalImageFormat.R16G16 | Sfloat; + case GalSurfaceFormat.R11G11B10Float: return GalImageFormat.B10G11R11 | Sfloat; + case GalSurfaceFormat.R32Float: return GalImageFormat.R32 | Sfloat; + case GalSurfaceFormat.RG8Unorm: return GalImageFormat.R8G8 | Unorm; + case GalSurfaceFormat.RG8Snorm: return GalImageFormat.R8 | Snorm; + case GalSurfaceFormat.R16Float: return GalImageFormat.R16 | Sfloat; + case GalSurfaceFormat.R8Unorm: return GalImageFormat.R8 | Unorm; + } + + throw new NotImplementedException(Format.ToString()); + } + + public static GalImageFormat ConvertZeta(GalZetaFormat Format) + { + switch (Format) + { + case GalZetaFormat.Z32Float: return GalImageFormat.D32 | Sfloat; + case GalZetaFormat.S8Z24Unorm: return GalImageFormat.D24_S8 | Unorm; + case GalZetaFormat.Z16Unorm: return GalImageFormat.D16 | Unorm; + //This one might not be Uint, change when a texture uses this format + case GalZetaFormat.Z32S8X24Float: return GalImageFormat.D32_S8 | Uint; + } + + throw new NotImplementedException(Format.ToString()); + } + + public static TextureReaderDelegate GetReader(GalImageFormat Format) + { + return GetImageDescriptor(Format).Reader; + } + + public static int GetSize(GalImage Image) + { + switch (Image.Format & GalImageFormat.FormatMask) + { + case GalImageFormat.R32G32B32A32: + return Image.Width * Image.Height * 16; + + case GalImageFormat.R16G16B16A16: + case GalImageFormat.D32_S8: + case GalImageFormat.R32G32: + return Image.Width * Image.Height * 8; + + case GalImageFormat.A8B8G8R8: + case GalImageFormat.A8B8G8R8_SRGB: + case GalImageFormat.A2B10G10R10: + case GalImageFormat.R16G16: + case GalImageFormat.R32: + case GalImageFormat.D32: + case GalImageFormat.B10G11R11: + case GalImageFormat.D24_S8: + return Image.Width * Image.Height * 4; + + case GalImageFormat.B4G4R4A4: + case GalImageFormat.A1R5G5B5: + case GalImageFormat.B5G6R5: + case GalImageFormat.R8G8: + case GalImageFormat.R16: + case GalImageFormat.D16: + return Image.Width * Image.Height * 2; + + case GalImageFormat.R8: + return Image.Width * Image.Height; + + case GalImageFormat.BC1_RGBA: + case GalImageFormat.BC4: + { + return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 8); + } + + case GalImageFormat.BC6H_SF16: + case GalImageFormat.BC6H_UF16: + case GalImageFormat.BC7: + case GalImageFormat.BC2: + case GalImageFormat.BC3: + case GalImageFormat.BC5: + case GalImageFormat.ASTC_4x4: + return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 16); + + case GalImageFormat.ASTC_5x5: + return CompressedTextureSize(Image.Width, Image.Height, 5, 5, 16); + + case GalImageFormat.ASTC_6x6: + return CompressedTextureSize(Image.Width, Image.Height, 6, 6, 16); + + case GalImageFormat.ASTC_8x8: + return CompressedTextureSize(Image.Width, Image.Height, 8, 8, 16); + + case GalImageFormat.ASTC_10x10: + return CompressedTextureSize(Image.Width, Image.Height, 10, 10, 16); + + case GalImageFormat.ASTC_12x12: + return CompressedTextureSize(Image.Width, Image.Height, 12, 12, 16); + + case GalImageFormat.ASTC_5x4: + return CompressedTextureSize(Image.Width, Image.Height, 5, 4, 16); + + case GalImageFormat.ASTC_6x5: + return CompressedTextureSize(Image.Width, Image.Height, 6, 5, 16); + + case GalImageFormat.ASTC_8x6: + return CompressedTextureSize(Image.Width, Image.Height, 8, 6, 16); + + case GalImageFormat.ASTC_10x8: + return CompressedTextureSize(Image.Width, Image.Height, 10, 8, 16); + + case GalImageFormat.ASTC_12x10: + return CompressedTextureSize(Image.Width, Image.Height, 12, 10, 16); + + case GalImageFormat.ASTC_8x5: + return CompressedTextureSize(Image.Width, Image.Height, 8, 5, 16); + + case GalImageFormat.ASTC_10x5: + return CompressedTextureSize(Image.Width, Image.Height, 10, 5, 16); + + case GalImageFormat.ASTC_10x6: + return CompressedTextureSize(Image.Width, Image.Height, 10, 6, 16); + } + + throw new NotImplementedException((Image.Format & GalImageFormat.FormatMask).ToString()); + } + + public static bool HasColor(GalImageFormat Format) + { + return GetImageDescriptor(Format).HasColor; + } + + public static bool HasDepth(GalImageFormat Format) + { + return GetImageDescriptor(Format).HasDepth; + } + + public static bool HasStencil(GalImageFormat Format) + { + return GetImageDescriptor(Format).HasStencil; + } + + public static bool IsCompressed(GalImageFormat Format) + { + return GetImageDescriptor(Format).Compressed; + } + + private static ImageDescriptor GetImageDescriptor(GalImageFormat Format) + { + GalImageFormat TypeLess = (Format & GalImageFormat.FormatMask); + + if (s_ImageTable.TryGetValue(TypeLess, out ImageDescriptor Descriptor)) + { + return Descriptor; + } + + throw new NotImplementedException("Image with format " + TypeLess.ToString() + " not implemented"); + } + + private static GalImageFormat GetFormatType(GalTextureType Type) + { + switch (Type) + { + case GalTextureType.Snorm: return Snorm; + case GalTextureType.Unorm: return Unorm; + case GalTextureType.Sint: return Sint; + case GalTextureType.Uint: return Uint; + case GalTextureType.Float: return Sfloat; + + default: throw new NotImplementedException(((int)Type).ToString()); + } + } + + private static int CompressedTextureSize(int TextureWidth, int TextureHeight, int BlockWidth, int BlockHeight, int Bpb) + { + int W = (TextureWidth + (BlockWidth - 1)) / BlockWidth; + int H = (TextureHeight + (BlockHeight - 1)) / BlockHeight; + + return W * H * Bpb; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/IntegerEncoded.cs b/Ryujinx.Graphics/Texture/IntegerEncoded.cs new file mode 100644 index 00000000..683cb770 --- /dev/null +++ b/Ryujinx.Graphics/Texture/IntegerEncoded.cs @@ -0,0 +1,269 @@ +using System.Collections; +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Texture +{ + public struct IntegerEncoded + { + public enum EIntegerEncoding + { + JustBits, + Quint, + Trit + } + + EIntegerEncoding Encoding; + public int NumberBits { get; private set; } + public int BitValue { get; private set; } + public int TritValue { get; private set; } + public int QuintValue { get; private set; } + + public IntegerEncoded(EIntegerEncoding _Encoding, int NumBits) + { + Encoding = _Encoding; + NumberBits = NumBits; + BitValue = 0; + TritValue = 0; + QuintValue = 0; + } + + public bool MatchesEncoding(IntegerEncoded Other) + { + return Encoding == Other.Encoding && NumberBits == Other.NumberBits; + } + + public EIntegerEncoding GetEncoding() + { + return Encoding; + } + + public int GetBitLength(int NumberVals) + { + int TotalBits = NumberBits * NumberVals; + if (Encoding == EIntegerEncoding.Trit) + { + TotalBits += (NumberVals * 8 + 4) / 5; + } + else if (Encoding == EIntegerEncoding.Quint) + { + TotalBits += (NumberVals * 7 + 2) / 3; + } + return TotalBits; + } + + public static IntegerEncoded CreateEncoding(int MaxVal) + { + while (MaxVal > 0) + { + int Check = MaxVal + 1; + + // Is maxVal a power of two? + if ((Check & (Check - 1)) == 0) + { + return new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(MaxVal)); + } + + // Is maxVal of the type 3*2^n - 1? + if ((Check % 3 == 0) && ((Check / 3) & ((Check / 3) - 1)) == 0) + { + return new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(Check / 3 - 1)); + } + + // Is maxVal of the type 5*2^n - 1? + if ((Check % 5 == 0) && ((Check / 5) & ((Check / 5) - 1)) == 0) + { + return new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(Check / 5 - 1)); + } + + // Apparently it can't be represented with a bounded integer sequence... + // just iterate. + MaxVal--; + } + + return new IntegerEncoded(EIntegerEncoding.JustBits, 0); + } + + public static void DecodeTritBlock( + BitArrayStream BitStream, + List ListIntegerEncoded, + int NumberBitsPerValue) + { + // Implement the algorithm in section C.2.12 + int[] m = new int[5]; + int[] t = new int[5]; + int T; + + // Read the trit encoded block according to + // table C.2.14 + m[0] = BitStream.ReadBits(NumberBitsPerValue); + T = BitStream.ReadBits(2); + m[1] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(2) << 2; + m[2] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(1) << 4; + m[3] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(2) << 5; + m[4] = BitStream.ReadBits(NumberBitsPerValue); + T |= BitStream.ReadBits(1) << 7; + + int C = 0; + + BitArrayStream Tb = new BitArrayStream(new BitArray(new int[] { T })); + if (Tb.ReadBits(2, 4) == 7) + { + C = (Tb.ReadBits(5, 7) << 2) | Tb.ReadBits(0, 1); + t[4] = t[3] = 2; + } + else + { + C = Tb.ReadBits(0, 4); + if (Tb.ReadBits(5, 6) == 3) + { + t[4] = 2; + t[3] = Tb.ReadBit(7); + } + else + { + t[4] = Tb.ReadBit(7); + t[3] = Tb.ReadBits(5, 6); + } + } + + BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); + if (Cb.ReadBits(0, 1) == 3) + { + t[2] = 2; + t[1] = Cb.ReadBit(4); + t[0] = (Cb.ReadBit(3) << 1) | (Cb.ReadBit(2) & ~Cb.ReadBit(3)); + } + else if (Cb.ReadBits(2, 3) == 3) + { + t[2] = 2; + t[1] = 2; + t[0] = Cb.ReadBits(0, 1); + } + else + { + t[2] = Cb.ReadBit(4); + t[1] = Cb.ReadBits(2, 3); + t[0] = (Cb.ReadBit(1) << 1) | (Cb.ReadBit(0) & ~Cb.ReadBit(1)); + } + + for (int i = 0; i < 5; i++) + { + IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Trit, NumberBitsPerValue) + { + BitValue = m[i], + TritValue = t[i] + }; + ListIntegerEncoded.Add(IntEncoded); + } + } + + public static void DecodeQuintBlock( + BitArrayStream BitStream, + List ListIntegerEncoded, + int NumberBitsPerValue) + { + // Implement the algorithm in section C.2.12 + int[] m = new int[3]; + int[] q = new int[3]; + int Q; + + // Read the trit encoded block according to + // table C.2.15 + m[0] = BitStream.ReadBits(NumberBitsPerValue); + Q = BitStream.ReadBits(3); + m[1] = BitStream.ReadBits(NumberBitsPerValue); + Q |= BitStream.ReadBits(2) << 3; + m[2] = BitStream.ReadBits(NumberBitsPerValue); + Q |= BitStream.ReadBits(2) << 5; + + BitArrayStream Qb = new BitArrayStream(new BitArray(new int[] { Q })); + if (Qb.ReadBits(1, 2) == 3 && Qb.ReadBits(5, 6) == 0) + { + q[0] = q[1] = 4; + q[2] = (Qb.ReadBit(0) << 2) | ((Qb.ReadBit(4) & ~Qb.ReadBit(0)) << 1) | (Qb.ReadBit(3) & ~Qb.ReadBit(0)); + } + else + { + int C = 0; + if (Qb.ReadBits(1, 2) == 3) + { + q[2] = 4; + C = (Qb.ReadBits(3, 4) << 3) | ((~Qb.ReadBits(5, 6) & 3) << 1) | Qb.ReadBit(0); + } + else + { + q[2] = Qb.ReadBits(5, 6); + C = Qb.ReadBits(0, 4); + } + + BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C })); + if (Cb.ReadBits(0, 2) == 5) + { + q[1] = 4; + q[0] = Cb.ReadBits(3, 4); + } + else + { + q[1] = Cb.ReadBits(3, 4); + q[0] = Cb.ReadBits(0, 2); + } + } + + for (int i = 0; i < 3; i++) + { + IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Quint, NumberBitsPerValue) + { + BitValue = m[i], + QuintValue = q[i] + }; + ListIntegerEncoded.Add(IntEncoded); + } + } + + public static void DecodeIntegerSequence( + List DecodeIntegerSequence, + BitArrayStream BitStream, + int MaxRange, + int NumberValues) + { + // Determine encoding parameters + IntegerEncoded IntEncoded = CreateEncoding(MaxRange); + + // Start decoding + int NumberValuesDecoded = 0; + while (NumberValuesDecoded < NumberValues) + { + switch (IntEncoded.GetEncoding()) + { + case EIntegerEncoding.Quint: + { + DecodeQuintBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); + NumberValuesDecoded += 3; + + break; + } + + case EIntegerEncoding.Trit: + { + DecodeTritBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits); + NumberValuesDecoded += 5; + + break; + } + + case EIntegerEncoding.JustBits: + { + IntEncoded.BitValue = BitStream.ReadBits(IntEncoded.NumberBits); + DecodeIntegerSequence.Add(IntEncoded); + NumberValuesDecoded++; + + break; + } + } + } + } + } +} diff --git a/Ryujinx.Graphics/Texture/LinearSwizzle.cs b/Ryujinx.Graphics/Texture/LinearSwizzle.cs new file mode 100644 index 00000000..ef468e27 --- /dev/null +++ b/Ryujinx.Graphics/Texture/LinearSwizzle.cs @@ -0,0 +1,19 @@ +namespace Ryujinx.Graphics.Texture +{ + class LinearSwizzle : ISwizzle + { + private int Pitch; + private int Bpp; + + public LinearSwizzle(int Pitch, int Bpp) + { + this.Pitch = Pitch; + this.Bpp = Bpp; + } + + public int GetSwizzleOffset(int X, int Y) + { + return X * Bpp + Y * Pitch; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureFactory.cs b/Ryujinx.Graphics/Texture/TextureFactory.cs new file mode 100644 index 00000000..fa7a0f80 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureFactory.cs @@ -0,0 +1,132 @@ +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; +using System; + +namespace Ryujinx.Graphics.Texture +{ + static class TextureFactory + { + public static GalImage MakeTexture(NvGpuVmm Vmm, long TicPosition) + { + int[] Tic = ReadWords(Vmm, TicPosition, 8); + + GalImageFormat Format = GetImageFormat(Tic); + + GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7); + GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7); + GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7); + GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7); + + int Width = (Tic[4] & 0xffff) + 1; + int Height = (Tic[5] & 0xffff) + 1; + + return new GalImage( + Width, + Height, + Format, + XSource, + YSource, + ZSource, + WSource); + } + + public static byte[] GetTextureData(NvGpuVmm Vmm, long TicPosition) + { + int[] Tic = ReadWords(Vmm, TicPosition, 8); + + GalImageFormat Format = GetImageFormat(Tic); + + long TextureAddress = (uint)Tic[1]; + + TextureAddress |= (long)((ushort)Tic[2]) << 32; + + TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7); + + if (Swizzle == TextureSwizzle.BlockLinear || + Swizzle == TextureSwizzle.BlockLinearColorKey) + { + TextureAddress &= ~0x1ffL; + } + else if (Swizzle == TextureSwizzle.Pitch || + Swizzle == TextureSwizzle.PitchColorKey) + { + TextureAddress &= ~0x1fL; + } + + int Pitch = (Tic[3] & 0xffff) << 5; + + int BlockHeightLog2 = (Tic[3] >> 3) & 7; + int TileWidthLog2 = (Tic[3] >> 10) & 7; + + int BlockHeight = 1 << BlockHeightLog2; + int TileWidth = 1 << TileWidthLog2; + + int Width = (Tic[4] & 0xffff) + 1; + int Height = (Tic[5] & 0xffff) + 1; + + TextureInfo Texture = new TextureInfo( + TextureAddress, + Width, + Height, + Pitch, + BlockHeight, + TileWidth, + Swizzle, + Format); + + return TextureReader.Read(Vmm, Texture); + } + + public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition) + { + int[] Tsc = ReadWords(Vmm, TscPosition, 8); + + GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7); + GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7); + GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7); + + GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3); + GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3); + GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3); + + GalColorF BorderColor = new GalColorF( + BitConverter.Int32BitsToSingle(Tsc[4]), + BitConverter.Int32BitsToSingle(Tsc[5]), + BitConverter.Int32BitsToSingle(Tsc[6]), + BitConverter.Int32BitsToSingle(Tsc[7])); + + return new GalTextureSampler( + AddressU, + AddressV, + AddressP, + MinFilter, + MagFilter, + MipFilter, + BorderColor); + } + + private static GalImageFormat GetImageFormat(int[] Tic) + { + GalTextureType RType = (GalTextureType)((Tic[0] >> 7) & 7); + GalTextureType GType = (GalTextureType)((Tic[0] >> 10) & 7); + GalTextureType BType = (GalTextureType)((Tic[0] >> 13) & 7); + GalTextureType AType = (GalTextureType)((Tic[0] >> 16) & 7); + + GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); + + return ImageUtils.ConvertTexture(Format, RType, GType, BType, AType); + } + + private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count) + { + int[] Words = new int[Count]; + + for (int Index = 0; Index < Count; Index++, Position += 4) + { + Words[Index] = Vmm.ReadInt32(Position); + } + + return Words; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureHelper.cs b/Ryujinx.Graphics/Texture/TextureHelper.cs new file mode 100644 index 00000000..8130ab41 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureHelper.cs @@ -0,0 +1,45 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; +using System; + +namespace Ryujinx.Graphics.Texture +{ + static class TextureHelper + { + public static ISwizzle GetSwizzle(TextureInfo Texture, int BlockWidth, int Bpp) + { + int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth; + + int AlignMask = Texture.TileWidth * (64 / Bpp) - 1; + + Width = (Width + AlignMask) & ~AlignMask; + + switch (Texture.Swizzle) + { + case TextureSwizzle._1dBuffer: + case TextureSwizzle.Pitch: + case TextureSwizzle.PitchColorKey: + return new LinearSwizzle(Texture.Pitch, Bpp); + + case TextureSwizzle.BlockLinear: + case TextureSwizzle.BlockLinearColorKey: + return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight); + } + + throw new NotImplementedException(Texture.Swizzle.ToString()); + } + + public static (AMemory Memory, long Position) GetMemoryAndPosition( + IAMemory Memory, + long Position) + { + if (Memory is NvGpuVmm Vmm) + { + return (Vmm.Memory, Vmm.GetPhysicalAddress(Position)); + } + + return ((AMemory)Memory, Position); + } + } +} diff --git a/Ryujinx.Graphics/Texture/TextureInfo.cs b/Ryujinx.Graphics/Texture/TextureInfo.cs new file mode 100644 index 00000000..66445dcc --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureInfo.cs @@ -0,0 +1,60 @@ +using Ryujinx.Graphics.Gal; + +namespace Ryujinx.Graphics.Texture +{ + public struct TextureInfo + { + public long Position { get; private set; } + + public int Width { get; private set; } + public int Height { get; private set; } + public int Pitch { get; private set; } + + public int BlockHeight { get; private set; } + public int TileWidth { get; private set; } + + public TextureSwizzle Swizzle { get; private set; } + + public GalImageFormat Format { get; private set; } + + public TextureInfo( + long Position, + int Width, + int Height) + { + this.Position = Position; + this.Width = Width; + this.Height = Height; + + Pitch = 0; + + BlockHeight = 16; + + TileWidth = 1; + + Swizzle = TextureSwizzle.BlockLinear; + + Format = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm; + } + + public TextureInfo( + long Position, + int Width, + int Height, + int Pitch, + int BlockHeight, + int TileWidth, + TextureSwizzle Swizzle, + GalImageFormat Format) + { + this.Position = Position; + this.Width = Width; + this.Height = Height; + this.Pitch = Pitch; + this.BlockHeight = BlockHeight; + this.TileWidth = TileWidth; + this.Swizzle = Swizzle; + this.Format = Format; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureReader.cs b/Ryujinx.Graphics/Texture/TextureReader.cs new file mode 100644 index 00000000..dbaed1a8 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureReader.cs @@ -0,0 +1,398 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using System; + +namespace Ryujinx.Graphics.Texture +{ + delegate byte[] TextureReaderDelegate(IAMemory Memory, TextureInfo Texture); + + public static class TextureReader + { + public static byte[] Read(IAMemory Memory, TextureInfo Texture) + { + TextureReaderDelegate Reader = ImageUtils.GetReader(Texture.Format); + + return Reader(Memory, Texture); + } + + internal unsafe static byte[] Read1Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 1); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + byte Pixel = CpuMem.ReadByte(Position + Offset); + + *(BuffPtr + OutOffs) = Pixel; + + OutOffs++; + } + } + + return Output; + } + + internal unsafe static byte[] Read5551(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset); + + Pixel = (Pixel & 0x001f) << 11 | + (Pixel & 0x03e0) << 1 | + (Pixel & 0x7c00) >> 9 | + (Pixel & 0x8000) >> 15; + + *(short*)(BuffPtr + OutOffs) = (short)Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + internal unsafe static byte[] Read565(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset); + + Pixel = (Pixel & 0x001f) << 11 | + (Pixel & 0x07e0) | + (Pixel & 0xf800) >> 11; + + *(short*)(BuffPtr + OutOffs) = (short)Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + internal unsafe static byte[] Read2Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 2]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + short Pixel = CpuMem.ReadInt16(Position + Offset); + + *(short*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 2; + } + } + + return Output; + } + + internal unsafe static byte[] Read4Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 4]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + int Pixel = CpuMem.ReadInt32(Position + Offset); + + *(int*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 4; + } + } + + return Output; + } + + internal unsafe static byte[] Read8Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 8]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 8); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Pixel = CpuMem.ReadInt64(Position + Offset); + + *(long*)(BuffPtr + OutOffs) = Pixel; + + OutOffs += 8; + } + } + + return Output; + } + + internal unsafe static byte[] Read16Bpp(IAMemory Memory, TextureInfo Texture) + { + int Width = Texture.Width; + int Height = Texture.Height; + + byte[] Output = new byte[Width * Height * 16]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 16); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long PxLow = CpuMem.ReadInt64(Position + Offset + 0); + long PxHigh = CpuMem.ReadInt64(Position + Offset + 8); + + *(long*)(BuffPtr + OutOffs + 0) = PxLow; + *(long*)(BuffPtr + OutOffs + 8) = PxHigh; + + OutOffs += 16; + } + } + + return Output; + } + + internal unsafe static byte[] Read8Bpt4x4(IAMemory Memory, TextureInfo Texture) + { + int Width = (Texture.Width + 3) / 4; + int Height = (Texture.Height + 3) / 4; + + byte[] Output = new byte[Width * Height * 8]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 4, 8); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Tile = CpuMem.ReadInt64(Position + Offset); + + *(long*)(BuffPtr + OutOffs) = Tile; + + OutOffs += 8; + } + } + + return Output; + } + + internal unsafe static byte[] Read16BptCompressedTexture(IAMemory Memory, TextureInfo Texture, int BlockWidth, int BlockHeight) + { + int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth; + int Height = (Texture.Height + (BlockHeight - 1)) / BlockHeight; + + byte[] Output = new byte[Width * Height * 16]; + + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, BlockWidth, 16); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Output) + { + long OutOffs = 0; + + for (int Y = 0; Y < Height; Y++) + for (int X = 0; X < Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + long Tile0 = CpuMem.ReadInt64(Position + Offset + 0); + long Tile1 = CpuMem.ReadInt64(Position + Offset + 8); + + *(long*)(BuffPtr + OutOffs + 0) = Tile0; + *(long*)(BuffPtr + OutOffs + 8) = Tile1; + + OutOffs += 16; + } + } + + return Output; + } + + internal static byte[] Read16BptCompressedTexture4x4(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 4, 4); + } + + internal static byte[] Read16BptCompressedTexture5x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 5, 5); + } + + internal static byte[] Read16BptCompressedTexture6x6(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 6, 6); + } + + internal static byte[] Read16BptCompressedTexture8x8(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 8, 8); + } + + internal static byte[] Read16BptCompressedTexture10x10(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 10); + } + + internal static byte[] Read16BptCompressedTexture12x12(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 12, 12); + } + + internal static byte[] Read16BptCompressedTexture5x4(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 5, 4); + } + + internal static byte[] Read16BptCompressedTexture6x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 6, 5); + } + + internal static byte[] Read16BptCompressedTexture8x6(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 8, 6); + } + + internal static byte[] Read16BptCompressedTexture10x8(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 8); + } + + internal static byte[] Read16BptCompressedTexture12x10(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 12, 10); + } + + internal static byte[] Read16BptCompressedTexture8x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 5, 5); + } + + internal static byte[] Read16BptCompressedTexture10x5(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 5); + } + + internal static byte[] Read16BptCompressedTexture10x6(IAMemory Memory, TextureInfo Texture) + { + return Read16BptCompressedTexture(Memory, Texture, 10, 6); + } + } +} diff --git a/Ryujinx.Graphics/Texture/TextureSwizzle.cs b/Ryujinx.Graphics/Texture/TextureSwizzle.cs new file mode 100644 index 00000000..c67a5367 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureSwizzle.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.Graphics.Texture +{ + public enum TextureSwizzle + { + _1dBuffer = 0, + PitchColorKey = 1, + Pitch = 2, + BlockLinear = 3, + BlockLinearColorKey = 4 + } +} \ No newline at end of file diff --git a/Ryujinx.Graphics/Texture/TextureWriter.cs b/Ryujinx.Graphics/Texture/TextureWriter.cs new file mode 100644 index 00000000..16e78c56 --- /dev/null +++ b/Ryujinx.Graphics/Texture/TextureWriter.cs @@ -0,0 +1,35 @@ +using ChocolArm64.Memory; +using Ryujinx.Graphics.Gal; +using Ryujinx.Graphics.Memory; + +namespace Ryujinx.Graphics.Texture +{ + static class TextureWriter + { + public unsafe static void Write(IAMemory Memory, TextureInfo Texture, byte[] Data) + { + ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4); + + (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( + Memory, + Texture.Position); + + fixed (byte* BuffPtr = Data) + { + long InOffs = 0; + + for (int Y = 0; Y < Texture.Height; Y++) + for (int X = 0; X < Texture.Width; X++) + { + long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); + + int Pixel = *(int*)(BuffPtr + InOffs); + + CpuMem.WriteInt32(Position + Offset, Pixel); + + InOffs += 4; + } + } + } + } +} diff --git a/Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs b/Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs deleted file mode 100644 index 068878a9..00000000 --- a/Ryujinx.HLE/Gpu/Engines/INvGpuEngine.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Ryujinx.HLE.Gpu.Memory; - -namespace Ryujinx.HLE.Gpu.Engines -{ - interface INvGpuEngine - { - int[] Registers { get; } - - void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs b/Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs deleted file mode 100644 index 423e2021..00000000 --- a/Ryujinx.HLE/Gpu/Engines/MacroInterpreter.cs +++ /dev/null @@ -1,418 +0,0 @@ -using Ryujinx.HLE.Gpu.Memory; -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu.Engines -{ - class MacroInterpreter - { - private enum AssignmentOperation - { - IgnoreAndFetch = 0, - Move = 1, - MoveAndSetMaddr = 2, - FetchAndSend = 3, - MoveAndSend = 4, - FetchAndSetMaddr = 5, - MoveAndSetMaddrThenFetchAndSend = 6, - MoveAndSetMaddrThenSendHigh = 7 - } - - private enum AluOperation - { - AluReg = 0, - AddImmediate = 1, - BitfieldReplace = 2, - BitfieldExtractLslImm = 3, - BitfieldExtractLslReg = 4, - ReadImmediate = 5 - } - - private enum AluRegOperation - { - Add = 0, - AddWithCarry = 1, - Subtract = 2, - SubtractWithBorrow = 3, - BitwiseExclusiveOr = 8, - BitwiseOr = 9, - BitwiseAnd = 10, - BitwiseAndNot = 11, - BitwiseNotAnd = 12 - } - - private NvGpuFifo PFifo; - private INvGpuEngine Engine; - - public Queue Fifo { get; private set; } - - private int[] Gprs; - - private int MethAddr; - private int MethIncr; - - private bool Carry; - - private int OpCode; - - private int PipeOp; - - private int Pc; - - public MacroInterpreter(NvGpuFifo PFifo, INvGpuEngine Engine) - { - this.PFifo = PFifo; - this.Engine = Engine; - - Fifo = new Queue(); - - Gprs = new int[8]; - } - - public void Execute(NvGpuVmm Vmm, int[] Mme, int Position, int Param) - { - Reset(); - - Gprs[1] = Param; - - Pc = Position; - - FetchOpCode(Mme); - - while (Step(Vmm, Mme)); - - //Due to the delay slot, we still need to execute - //one more instruction before we actually exit. - Step(Vmm, Mme); - } - - private void Reset() - { - for (int Index = 0; Index < Gprs.Length; Index++) - { - Gprs[Index] = 0; - } - - MethAddr = 0; - MethIncr = 0; - - Carry = false; - } - - private bool Step(NvGpuVmm Vmm, int[] Mme) - { - int BaseAddr = Pc - 1; - - FetchOpCode(Mme); - - if ((OpCode & 7) < 7) - { - //Operation produces a value. - AssignmentOperation AsgOp = (AssignmentOperation)((OpCode >> 4) & 7); - - int Result = GetAluResult(); - - switch (AsgOp) - { - //Fetch parameter and ignore result. - case AssignmentOperation.IgnoreAndFetch: - { - SetDstGpr(FetchParam()); - - break; - } - - //Move result. - case AssignmentOperation.Move: - { - SetDstGpr(Result); - - break; - } - - //Move result and use as Method Address. - case AssignmentOperation.MoveAndSetMaddr: - { - SetDstGpr(Result); - - SetMethAddr(Result); - - break; - } - - //Fetch parameter and send result. - case AssignmentOperation.FetchAndSend: - { - SetDstGpr(FetchParam()); - - Send(Vmm, Result); - - break; - } - - //Move and send result. - case AssignmentOperation.MoveAndSend: - { - SetDstGpr(Result); - - Send(Vmm, Result); - - break; - } - - //Fetch parameter and use result as Method Address. - case AssignmentOperation.FetchAndSetMaddr: - { - SetDstGpr(FetchParam()); - - SetMethAddr(Result); - - break; - } - - //Move result and use as Method Address, then fetch and send paramter. - case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend: - { - SetDstGpr(Result); - - SetMethAddr(Result); - - Send(Vmm, FetchParam()); - - break; - } - - //Move result and use as Method Address, then send bits 17:12 of result. - case AssignmentOperation.MoveAndSetMaddrThenSendHigh: - { - SetDstGpr(Result); - - SetMethAddr(Result); - - Send(Vmm, (Result >> 12) & 0x3f); - - break; - } - } - } - else - { - //Branch. - bool OnNotZero = ((OpCode >> 4) & 1) != 0; - - bool Taken = OnNotZero - ? GetGprA() != 0 - : GetGprA() == 0; - - if (Taken) - { - Pc = BaseAddr + GetImm(); - - bool NoDelays = (OpCode & 0x20) != 0; - - if (NoDelays) - { - FetchOpCode(Mme); - } - - return true; - } - } - - bool Exit = (OpCode & 0x80) != 0; - - return !Exit; - } - - private void FetchOpCode(int[] Mme) - { - OpCode = PipeOp; - - PipeOp = Mme[Pc++]; - } - - private int GetAluResult() - { - AluOperation Op = (AluOperation)(OpCode & 7); - - switch (Op) - { - case AluOperation.AluReg: - { - AluRegOperation AluOp = (AluRegOperation)((OpCode >> 17) & 0x1f); - - return GetAluResult(AluOp, GetGprA(), GetGprB()); - } - - case AluOperation.AddImmediate: - { - return GetGprA() + GetImm(); - } - - case AluOperation.BitfieldReplace: - case AluOperation.BitfieldExtractLslImm: - case AluOperation.BitfieldExtractLslReg: - { - int BfSrcBit = (OpCode >> 17) & 0x1f; - int BfSize = (OpCode >> 22) & 0x1f; - int BfDstBit = (OpCode >> 27) & 0x1f; - - int BfMask = (1 << BfSize) - 1; - - int Dst = GetGprA(); - int Src = GetGprB(); - - switch (Op) - { - case AluOperation.BitfieldReplace: - { - Src = (int)((uint)Src >> BfSrcBit) & BfMask; - - Dst &= ~(BfMask << BfDstBit); - - Dst |= Src << BfDstBit; - - return Dst; - } - - case AluOperation.BitfieldExtractLslImm: - { - Src = (int)((uint)Src >> Dst) & BfMask; - - return Src << BfDstBit; - } - - case AluOperation.BitfieldExtractLslReg: - { - Src = (int)((uint)Src >> BfSrcBit) & BfMask; - - return Src << Dst; - } - } - - break; - } - - case AluOperation.ReadImmediate: - { - return Read(GetGprA() + GetImm()); - } - } - - throw new ArgumentException(nameof(OpCode)); - } - - private int GetAluResult(AluRegOperation AluOp, int A, int B) - { - switch (AluOp) - { - case AluRegOperation.Add: - { - ulong Result = (ulong)A + (ulong)B; - - Carry = Result > 0xffffffff; - - return (int)Result; - } - - case AluRegOperation.AddWithCarry: - { - ulong Result = (ulong)A + (ulong)B + (Carry ? 1UL : 0UL); - - Carry = Result > 0xffffffff; - - return (int)Result; - } - - case AluRegOperation.Subtract: - { - ulong Result = (ulong)A - (ulong)B; - - Carry = Result < 0x100000000; - - return (int)Result; - } - - case AluRegOperation.SubtractWithBorrow: - { - ulong Result = (ulong)A - (ulong)B - (Carry ? 0UL : 1UL); - - Carry = Result < 0x100000000; - - return (int)Result; - } - - case AluRegOperation.BitwiseExclusiveOr: return A ^ B; - case AluRegOperation.BitwiseOr: return A | B; - case AluRegOperation.BitwiseAnd: return A & B; - case AluRegOperation.BitwiseAndNot: return A & ~B; - case AluRegOperation.BitwiseNotAnd: return ~(A & B); - } - - throw new ArgumentOutOfRangeException(nameof(AluOp)); - } - - private int GetImm() - { - //Note: The immediate is signed, the sign-extension is intended here. - return OpCode >> 14; - } - - private void SetMethAddr(int Value) - { - MethAddr = (Value >> 0) & 0xfff; - MethIncr = (Value >> 12) & 0x3f; - } - - private void SetDstGpr(int Value) - { - Gprs[(OpCode >> 8) & 7] = Value; - } - - private int GetGprA() - { - return GetGprValue((OpCode >> 11) & 7); - } - - private int GetGprB() - { - return GetGprValue((OpCode >> 14) & 7); - } - - private int GetGprValue(int Index) - { - return Index != 0 ? Gprs[Index] : 0; - } - - private int FetchParam() - { - int Value; - - //If we don't have any parameters in the FIFO, - //keep running the PFIFO engine until it writes the parameters. - while (!Fifo.TryDequeue(out Value)) - { - if (!PFifo.Step()) - { - return 0; - } - } - - return Value; - } - - private int Read(int Reg) - { - return Engine.Registers[Reg]; - } - - private void Send(NvGpuVmm Vmm, int Value) - { - NvGpuPBEntry PBEntry = new NvGpuPBEntry(MethAddr, 0, Value); - - Engine.CallMethod(Vmm, PBEntry); - - MethAddr += MethIncr; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs deleted file mode 100644 index f9d6342c..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Engines -{ - enum NvGpuEngine - { - _2d = 0x902d, - _3d = 0xb197, - Compute = 0xb1c0, - Kepler = 0xa140, - Dma = 0xb0b5 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs deleted file mode 100644 index 7fb5ea8a..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2d.cs +++ /dev/null @@ -1,208 +0,0 @@ -using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu.Memory; -using Ryujinx.HLE.Gpu.Texture; -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu.Engines -{ - class NvGpuEngine2d : INvGpuEngine - { - private enum CopyOperation - { - SrcCopyAnd, - RopAnd, - Blend, - SrcCopy, - Rop, - SrcCopyPremult, - BlendPremult - } - - public int[] Registers { get; private set; } - - private NvGpu Gpu; - - private Dictionary Methods; - - public NvGpuEngine2d(NvGpu Gpu) - { - this.Gpu = Gpu; - - Registers = new int[0xe00]; - - Methods = new Dictionary(); - - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) - { - while (Count-- > 0) - { - Methods.Add(Meth, Method); - - Meth += Stride; - } - } - - AddMethod(0xb5, 1, 1, TextureCopy); - } - - public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) - { - Method(Vmm, PBEntry); - } - else - { - WriteRegister(PBEntry); - } - } - - private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation); - - bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0; - int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth); - int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight); - int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch); - int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions); - - bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0; - int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth); - int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight); - int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch); - int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions); - - TextureSwizzle SrcSwizzle = SrcLinear - ? TextureSwizzle.Pitch - : TextureSwizzle.BlockLinear; - - TextureSwizzle DstSwizzle = DstLinear - ? TextureSwizzle.Pitch - : TextureSwizzle.BlockLinear; - - int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); - int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); - - long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress); - long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress); - - long SrcKey = Vmm.GetPhysicalAddress(SrcAddress); - long DstKey = Vmm.GetPhysicalAddress(DstAddress); - - bool IsSrcFb = Gpu.Engine3d.IsFrameBufferPosition(SrcKey); - bool IsDstFb = Gpu.Engine3d.IsFrameBufferPosition(DstKey); - - TextureInfo SrcTexture() - { - return new TextureInfo( - SrcAddress, - SrcWidth, - SrcHeight, - SrcPitch, - SrcBlockHeight, 1, - SrcSwizzle, - GalTextureFormat.A8B8G8R8); - } - - TextureInfo DstTexture() - { - return new TextureInfo( - DstAddress, - DstWidth, - DstHeight, - DstPitch, - DstBlockHeight, 1, - DstSwizzle, - GalTextureFormat.A8B8G8R8); - } - - //TODO: fb -> fb copies, tex -> fb copies, formats other than RGBA8, - //make it throw for unimpl stuff (like the copy mode)... - if (IsSrcFb && IsDstFb) - { - //Frame Buffer -> Frame Buffer copy. - Gpu.Renderer.FrameBuffer.Copy( - SrcKey, - DstKey, - 0, - 0, - SrcWidth, - SrcHeight, - 0, - 0, - DstWidth, - DstHeight); - } - if (IsSrcFb) - { - //Frame Buffer -> Texture copy. - Gpu.Renderer.FrameBuffer.GetBufferData(SrcKey, (byte[] Buffer) => - { - TextureInfo Src = SrcTexture(); - TextureInfo Dst = DstTexture(); - - if (Src.Width != Dst.Width || - Src.Height != Dst.Height) - { - throw new NotImplementedException("Texture resizing is not supported"); - } - - TextureWriter.Write(Vmm, Dst, Buffer); - }); - } - else if (IsDstFb) - { - byte[] Buffer = TextureReader.Read(Vmm, SrcTexture()); - - Gpu.Renderer.FrameBuffer.SetBufferData( - DstKey, - DstWidth, - DstHeight, - Buffer); - } - else - { - //Texture -> Texture copy. - TextureInfo Src = SrcTexture(); - TextureInfo Dst = DstTexture(); - - if (Src.Width != Dst.Width || - Src.Height != Dst.Height) - { - throw new NotImplementedException("Texture resizing is not supported"); - } - - TextureWriter.Write(Vmm, Dst, TextureReader.Read(Vmm, Src)); - } - } - - private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg) - { - return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; - } - - private void WriteRegister(NvGpuPBEntry PBEntry) - { - int ArgsCount = PBEntry.Arguments.Count; - - if (ArgsCount > 0) - { - Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; - } - } - - private int ReadRegister(NvGpuEngine2dReg Reg) - { - return Registers[(int)Reg]; - } - - private void WriteRegister(NvGpuEngine2dReg Reg, int Value) - { - Registers[(int)Reg] = Value; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs deleted file mode 100644 index 29d66d46..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine2dReg.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Engines -{ - enum NvGpuEngine2dReg - { - DstFormat = 0x80, - DstLinear = 0x81, - DstBlockDimensions = 0x82, - DstDepth = 0x83, - DstLayer = 0x84, - DstPitch = 0x85, - DstWidth = 0x86, - DstHeight = 0x87, - DstAddress = 0x88, - SrcFormat = 0x8c, - SrcLinear = 0x8d, - SrcBlockDimensions = 0x8e, - SrcDepth = 0x8f, - SrcLayer = 0x90, - SrcPitch = 0x91, - SrcWidth = 0x92, - SrcHeight = 0x93, - SrcAddress = 0x94, - CopyOperation = 0xab - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs deleted file mode 100644 index 0d2f3bef..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs +++ /dev/null @@ -1,895 +0,0 @@ -using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu.Memory; -using Ryujinx.HLE.Gpu.Texture; -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu.Engines -{ - class NvGpuEngine3d : INvGpuEngine - { - public int[] Registers { get; private set; } - - private NvGpu Gpu; - - private Dictionary Methods; - - private struct ConstBuffer - { - public bool Enabled; - public long Position; - public int Size; - } - - private ConstBuffer[][] ConstBuffers; - - private HashSet FrameBuffers; - - private List[] UploadedKeys; - - private int CurrentInstance = 0; - - public NvGpuEngine3d(NvGpu Gpu) - { - this.Gpu = Gpu; - - Registers = new int[0xe00]; - - Methods = new Dictionary(); - - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) - { - while (Count-- > 0) - { - Methods.Add(Meth, Method); - - Meth += Stride; - } - } - - AddMethod(0x585, 1, 1, VertexEndGl); - AddMethod(0x674, 1, 1, ClearBuffers); - AddMethod(0x6c3, 1, 1, QueryControl); - AddMethod(0x8e4, 16, 1, CbData); - AddMethod(0x904, 5, 8, CbBind); - - ConstBuffers = new ConstBuffer[6][]; - - for (int Index = 0; Index < ConstBuffers.Length; Index++) - { - ConstBuffers[Index] = new ConstBuffer[18]; - } - - FrameBuffers = new HashSet(); - - UploadedKeys = new List[(int)NvGpuBufferType.Count]; - - for (int i = 0; i < UploadedKeys.Length; i++) - { - UploadedKeys[i] = new List(); - } - } - - public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) - { - Method(Vmm, PBEntry); - } - else - { - WriteRegister(PBEntry); - } - } - - public void ResetCache() - { - foreach (List Uploaded in UploadedKeys) - { - Uploaded.Clear(); - } - } - - private void VertexEndGl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - LockCaches(); - - GalPipelineState State = new GalPipelineState(); - - SetFlip(State); - SetFrontFace(State); - SetCullFace(State); - SetDepth(State); - SetStencil(State); - SetAlphaBlending(State); - SetPrimitiveRestart(State); - - for (int FbIndex = 0; FbIndex < 8; FbIndex++) - { - SetFrameBuffer(Vmm, 0); - } - - SetZeta(Vmm); - - SetRenderTargets(); - - long[] Keys = UploadShaders(Vmm); - - Gpu.Renderer.Shader.BindProgram(); - - UploadTextures(Vmm, State, Keys); - UploadConstBuffers(Vmm, State, Keys); - UploadVertexArrays(Vmm, State); - - DispatchRender(Vmm, State); - - UnlockCaches(); - } - - private void LockCaches() - { - Gpu.Renderer.Buffer.LockCache(); - Gpu.Renderer.Rasterizer.LockCaches(); - Gpu.Renderer.Texture.LockCache(); - } - - private void UnlockCaches() - { - Gpu.Renderer.Buffer.UnlockCache(); - Gpu.Renderer.Rasterizer.UnlockCaches(); - Gpu.Renderer.Texture.UnlockCache(); - } - - private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - int Arg0 = PBEntry.Arguments[0]; - - int FbIndex = (Arg0 >> 6) & 0xf; - - GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f); - - float Red = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 0); - float Green = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 1); - float Blue = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 2); - float Alpha = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 3); - - float Depth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth); - - int Stencil = ReadRegister(NvGpuEngine3dReg.ClearStencil); - - SetFrameBuffer(Vmm, FbIndex); - SetZeta(Vmm); - - Gpu.Renderer.Rasterizer.ClearBuffers( - Flags, - FbIndex, - Red, Green, Blue, Alpha, - Depth, - Stencil); - } - - private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex) - { - long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10); - - int Format = ReadRegister(NvGpuEngine3dReg.FrameBufferNFormat + FbIndex * 0x10); - - if (VA == 0 || Format == 0) - { - Gpu.Renderer.FrameBuffer.UnbindColor(FbIndex); - - return; - } - - long Key = Vmm.GetPhysicalAddress(VA); - - FrameBuffers.Add(Key); - - int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10); - int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10); - - float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 8); - float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 8); - - float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 8); - float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 8); - - int VpX = (int)MathF.Max(0, TX - MathF.Abs(SX)); - int VpY = (int)MathF.Max(0, TY - MathF.Abs(SY)); - - int VpW = (int)(TX + MathF.Abs(SX)) - VpX; - int VpH = (int)(TY + MathF.Abs(SY)) - VpY; - - GalImageFormat ImageFormat = ImageFormatConverter.ConvertFrameBuffer((GalFrameBufferFormat)Format); - - GalImage Image = new GalImage(Width, Height, ImageFormat); - - long Size = TextureHelper.GetTextureSize(Image); - - Gpu.Renderer.Texture.CreateFb(Key, Size, Image); - Gpu.Renderer.FrameBuffer.BindColor(Key, FbIndex); - - Gpu.Renderer.FrameBuffer.SetViewport(VpX, VpY, VpW, VpH); - } - - private void SetZeta(NvGpuVmm Vmm) - { - long ZA = MakeInt64From2xInt32(NvGpuEngine3dReg.ZetaAddress); - - int Format = ReadRegister(NvGpuEngine3dReg.ZetaFormat); - - bool ZetaEnable = (ReadRegister(NvGpuEngine3dReg.ZetaEnable) & 1) != 0; - - if (ZA == 0 || Format == 0 || !ZetaEnable) - { - Gpu.Renderer.FrameBuffer.UnbindZeta(); - - return; - } - - long Key = Vmm.GetPhysicalAddress(ZA); - - int Width = ReadRegister(NvGpuEngine3dReg.ZetaHoriz); - int Height = ReadRegister(NvGpuEngine3dReg.ZetaVert); - - GalImageFormat ImageFormat = ImageFormatConverter.ConvertZeta((GalZetaFormat)Format); - - GalImage Image = new GalImage(Width, Height, ImageFormat); - - long Size = TextureHelper.GetTextureSize(Image); - - Gpu.Renderer.Texture.CreateFb(Key, Size, Image); - Gpu.Renderer.FrameBuffer.BindZeta(Key); - } - - private long[] UploadShaders(NvGpuVmm Vmm) - { - long[] Keys = new long[5]; - - long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - - int Index = 1; - - int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl); - - bool VpAEnable = (VpAControl & 1) != 0; - - if (VpAEnable) - { - //Note: The maxwell supports 2 vertex programs, usually - //only VP B is used, but in some cases VP A is also used. - //In this case, it seems to function as an extra vertex - //shader stage. - //The graphics abstraction layer has a special overload for this - //case, which should merge the two shaders into one vertex shader. - int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset); - int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10); - - long VpAPos = BasePosition + (uint)VpAOffset; - long VpBPos = BasePosition + (uint)VpBOffset; - - Keys[(int)GalShaderType.Vertex] = VpBPos; - - Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex); - Gpu.Renderer.Shader.Bind(VpBPos); - - Index = 2; - } - - for (; Index < 6; Index++) - { - GalShaderType Type = GetTypeFromProgram(Index); - - int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10); - int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10); - - //Note: Vertex Program (B) is always enabled. - bool Enable = (Control & 1) != 0 || Index == 1; - - if (!Enable) - { - Gpu.Renderer.Shader.Unbind(Type); - - continue; - } - - long Key = BasePosition + (uint)Offset; - - Keys[(int)Type] = Key; - - Gpu.Renderer.Shader.Create(Vmm, Key, Type); - Gpu.Renderer.Shader.Bind(Key); - } - - return Keys; - } - - private static GalShaderType GetTypeFromProgram(int Program) - { - switch (Program) - { - case 0: - case 1: return GalShaderType.Vertex; - case 2: return GalShaderType.TessControl; - case 3: return GalShaderType.TessEvaluation; - case 4: return GalShaderType.Geometry; - case 5: return GalShaderType.Fragment; - } - - throw new ArgumentOutOfRangeException(nameof(Program)); - } - - private void SetFlip(GalPipelineState State) - { - State.FlipX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); - State.FlipY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); - } - - private void SetFrontFace(GalPipelineState State) - { - float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX); - float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY); - - GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace); - - //Flipping breaks facing. Flipping front facing too fixes it - if (SignX != SignY) - { - switch (FrontFace) - { - case GalFrontFace.CW: - FrontFace = GalFrontFace.CCW; - break; - - case GalFrontFace.CCW: - FrontFace = GalFrontFace.CW; - break; - } - } - - State.FrontFace = FrontFace; - } - - private void SetCullFace(GalPipelineState State) - { - State.CullFaceEnabled = (ReadRegister(NvGpuEngine3dReg.CullFaceEnable) & 1) != 0; - - if (State.CullFaceEnabled) - { - State.CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace); - } - } - - private void SetDepth(GalPipelineState State) - { - State.DepthTestEnabled = (ReadRegister(NvGpuEngine3dReg.DepthTestEnable) & 1) != 0; - - if (State.DepthTestEnabled) - { - State.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction); - } - } - - private void SetStencil(GalPipelineState State) - { - State.StencilTestEnabled = (ReadRegister(NvGpuEngine3dReg.StencilEnable) & 1) != 0; - - if (State.StencilTestEnabled) - { - State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc); - State.StencilBackFuncRef = ReadRegister(NvGpuEngine3dReg.StencilBackFuncRef); - State.StencilBackFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackFuncMask); - State.StencilBackOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpFail); - State.StencilBackOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZFail); - State.StencilBackOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZPass); - State.StencilBackMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackMask); - - State.StencilFrontFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncFunc); - State.StencilFrontFuncRef = ReadRegister(NvGpuEngine3dReg.StencilFrontFuncRef); - State.StencilFrontFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncMask); - State.StencilFrontOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpFail); - State.StencilFrontOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZFail); - State.StencilFrontOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZPass); - State.StencilFrontMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontMask); - } - } - - private void SetAlphaBlending(GalPipelineState State) - { - //TODO: Support independent blend properly. - State.BlendEnabled = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0; - - if (State.BlendEnabled) - { - State.BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0; - - State.BlendEquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb); - State.BlendFuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb); - State.BlendFuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb); - State.BlendEquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha); - State.BlendFuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha); - State.BlendFuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha); - } - } - - private void SetPrimitiveRestart(GalPipelineState State) - { - State.PrimitiveRestartEnabled = (ReadRegister(NvGpuEngine3dReg.PrimRestartEnable) & 1) != 0; - - if (State.PrimitiveRestartEnabled) - { - State.PrimitiveRestartIndex = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex); - } - } - - private void SetRenderTargets() - { - bool SeparateFragData = (ReadRegister(NvGpuEngine3dReg.RTSeparateFragData) & 1) != 0; - - if (SeparateFragData) - { - uint Control = (uint)(ReadRegister(NvGpuEngine3dReg.RTControl)); - - uint Count = Control & 0xf; - - int[] Map = new int[Count]; - - for (int i = 0; i < Count; i++) - { - int Shift = 4 + i * 3; - - Map[i] = (int)((Control >> Shift) & 7); - } - - Gpu.Renderer.FrameBuffer.SetMap(Map); - } - else - { - Gpu.Renderer.FrameBuffer.SetMap(null); - } - } - - private void UploadTextures(NvGpuVmm Vmm, GalPipelineState State, long[] Keys) - { - long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress); - - int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); - - int TexIndex = 0; - - for (int Index = 0; Index < Keys.Length; Index++) - { - foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index])) - { - long Position; - - if (DeclInfo.IsCb) - { - Position = ConstBuffers[Index][DeclInfo.Cbuf].Position; - } - else - { - Position = ConstBuffers[Index][TextureCbIndex].Position; - } - - int TextureHandle = Vmm.ReadInt32(Position + DeclInfo.Index * 4); - - UploadTexture(Vmm, TexIndex, TextureHandle); - - TexIndex++; - } - } - } - - private void UploadTexture(NvGpuVmm Vmm, int TexIndex, int TextureHandle) - { - if (TextureHandle == 0) - { - //TODO: Is this correct? - //Some games like puyo puyo will have 0 handles. - //It may be just normal behaviour or a bug caused by sync issues. - //The game does initialize the value properly after through. - return; - } - - int TicIndex = (TextureHandle >> 0) & 0xfffff; - int TscIndex = (TextureHandle >> 20) & 0xfff; - - long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset); - long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset); - - TicPosition += TicIndex * 0x20; - TscPosition += TscIndex * 0x20; - - GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition); - - long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff; - - Key = Vmm.GetPhysicalAddress(Key); - - if (Key == -1) - { - //FIXME: Should'nt ignore invalid addresses. - return; - } - - if (IsFrameBufferPosition(Key)) - { - //This texture is a frame buffer texture, - //we shouldn't read anything from memory and bind - //the frame buffer texture instead, since we're not - //really writing anything to memory. - Gpu.Renderer.FrameBuffer.BindTexture(Key, TexIndex); - } - else - { - GalImage NewImage = TextureFactory.MakeTexture(Vmm, TicPosition); - - long Size = (uint)TextureHelper.GetTextureSize(NewImage); - - bool HasCachedTexture = false; - - if (Gpu.Renderer.Texture.TryGetCachedTexture(Key, Size, out GalImage Image)) - { - if (NewImage.Equals(Image) && !QueryKeyUpload(Vmm, Key, Size, NvGpuBufferType.Texture)) - { - Gpu.Renderer.Texture.Bind(Key, TexIndex); - - HasCachedTexture = true; - } - } - - if (!HasCachedTexture) - { - byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition); - - Gpu.Renderer.Texture.Create(Key, Data, NewImage); - } - - Gpu.Renderer.Texture.Bind(Key, TexIndex); - } - - Gpu.Renderer.Texture.SetSampler(Sampler); - } - - private void UploadConstBuffers(NvGpuVmm Vmm, GalPipelineState State, long[] Keys) - { - for (int Stage = 0; Stage < Keys.Length; Stage++) - { - foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetConstBufferUsage(Keys[Stage])) - { - ConstBuffer Cb = ConstBuffers[Stage][DeclInfo.Cbuf]; - - if (!Cb.Enabled) - { - continue; - } - - long Key = Vmm.GetPhysicalAddress(Cb.Position); - - if (QueryKeyUpload(Vmm, Key, Cb.Size, NvGpuBufferType.ConstBuffer)) - { - IntPtr Source = Vmm.GetHostAddress(Cb.Position, Cb.Size); - - Gpu.Renderer.Buffer.SetData(Key, Cb.Size, Source); - } - - State.ConstBufferKeys[Stage][DeclInfo.Cbuf] = Key; - } - } - } - - private void UploadVertexArrays(NvGpuVmm Vmm, GalPipelineState State) - { - long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); - - long IboKey = Vmm.GetPhysicalAddress(IndexPosition); - - int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); - int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); - - GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt; - - int IndexEntrySize = 1 << IndexEntryFmt; - - if (IndexEntrySize > 4) - { - throw new InvalidOperationException(); - } - - if (IndexCount != 0) - { - int IbSize = IndexCount * IndexEntrySize; - - bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize); - - if (!IboCached || QueryKeyUpload(Vmm, IboKey, (uint)IbSize, NvGpuBufferType.Index)) - { - IntPtr DataAddress = Vmm.GetHostAddress(IndexPosition, IbSize); - - Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, DataAddress); - } - - Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat); - } - - List[] Attribs = new List[32]; - - for (int Attr = 0; Attr < 16; Attr++) - { - int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr); - - int ArrayIndex = Packed & 0x1f; - - if (Attribs[ArrayIndex] == null) - { - Attribs[ArrayIndex] = new List(); - } - - Attribs[ArrayIndex].Add(new GalVertexAttrib( - Attr, - ((Packed >> 6) & 0x1) != 0, - (Packed >> 7) & 0x3fff, - (GalVertexAttribSize)((Packed >> 21) & 0x3f), - (GalVertexAttribType)((Packed >> 27) & 0x7), - ((Packed >> 31) & 0x1) != 0)); - } - - State.VertexBindings = new GalVertexBinding[32]; - - for (int Index = 0; Index < 32; Index++) - { - if (Attribs[Index] == null) - { - continue; - } - - int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4); - - bool Enable = (Control & 0x1000) != 0; - - if (!Enable) - { - continue; - } - - long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4); - long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2); - - int VertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + Index * 4); - - bool Instanced = (ReadRegister(NvGpuEngine3dReg.VertexArrayNInstance + Index) & 1) != 0; - - int Stride = Control & 0xfff; - - if (Instanced && VertexDivisor != 0) - { - VertexPosition += Stride * (CurrentInstance / VertexDivisor); - } - - if (VertexPosition > VertexEndPos) - { - //Instance is invalid, ignore the draw call - continue; - } - - long VboKey = Vmm.GetPhysicalAddress(VertexPosition); - - long VbSize = (VertexEndPos - VertexPosition) + 1; - - bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, VbSize); - - if (!VboCached || QueryKeyUpload(Vmm, VboKey, VbSize, NvGpuBufferType.Vertex)) - { - IntPtr DataAddress = Vmm.GetHostAddress(VertexPosition, VbSize); - - Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, DataAddress); - } - - State.VertexBindings[Index].Enabled = true; - State.VertexBindings[Index].Stride = Stride; - State.VertexBindings[Index].VboKey = VboKey; - State.VertexBindings[Index].Instanced = Instanced; - State.VertexBindings[Index].Divisor = VertexDivisor; - State.VertexBindings[Index].Attribs = Attribs[Index].ToArray(); - } - } - - private void DispatchRender(NvGpuVmm Vmm, GalPipelineState State) - { - int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); - int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); - - GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); - - bool InstanceNext = ((PrimCtrl >> 26) & 1) != 0; - bool InstanceCont = ((PrimCtrl >> 27) & 1) != 0; - - if (InstanceNext && InstanceCont) - { - throw new InvalidOperationException("GPU tried to increase and reset instance count at the same time"); - } - - if (InstanceNext) - { - CurrentInstance++; - } - else if (!InstanceCont) - { - CurrentInstance = 0; - } - - State.Instance = CurrentInstance; - - Gpu.Renderer.Pipeline.Bind(State); - - if (IndexCount != 0) - { - int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); - int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); - int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase); - - long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); - - long IboKey = Vmm.GetPhysicalAddress(IndexPosition); - - Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType); - } - else - { - int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); - int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); - - Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType); - } - - //Is the GPU really clearing those registers after draw? - WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0); - WriteRegister(NvGpuEngine3dReg.IndexBatchCount, 0); - } - - private enum QueryMode - { - WriteSeq, - Sync, - WriteCounterAndTimestamp - } - - private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - WriteRegister(PBEntry); - - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress); - - int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence]; - int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl]; - - QueryMode Mode = (QueryMode)(Ctrl & 3); - - switch (Mode) - { - case QueryMode.WriteSeq: Vmm.WriteInt32(Position, Seq); break; - - case QueryMode.WriteCounterAndTimestamp: - { - //TODO: Implement counters. - long Counter = 1; - - long Timestamp = (uint)Environment.TickCount; - - Timestamp = (long)(Timestamp * 615384.615385); - - Vmm.WriteInt64(Position + 0, Counter); - Vmm.WriteInt64(Position + 8, Timestamp); - - break; - } - } - } - - private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); - - int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset); - - foreach (int Arg in PBEntry.Arguments) - { - Vmm.WriteInt32(Position + Offset, Arg); - - Offset += 4; - } - - WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset); - - UploadedKeys[(int)NvGpuBufferType.ConstBuffer].Clear(); - } - - private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - int Stage = (PBEntry.Method - 0x904) >> 3; - - int Index = PBEntry.Arguments[0]; - - bool Enabled = (Index & 1) != 0; - - Index = (Index >> 4) & 0x1f; - - long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress); - - long CbKey = Vmm.GetPhysicalAddress(Position); - - int Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize); - - if (!Gpu.Renderer.Buffer.IsCached(CbKey, Size)) - { - Gpu.Renderer.Buffer.Create(CbKey, Size); - } - - ConstBuffer Cb = ConstBuffers[Stage][Index]; - - if (Cb.Position != Position || Cb.Enabled != Enabled || Cb.Size != Size) - { - ConstBuffers[Stage][Index].Position = Position; - ConstBuffers[Stage][Index].Enabled = Enabled; - ConstBuffers[Stage][Index].Size = Size; - } - } - - private float GetFlipSign(NvGpuEngine3dReg Reg) - { - return MathF.Sign(ReadRegisterFloat(Reg)); - } - - private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg) - { - return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; - } - - private void WriteRegister(NvGpuPBEntry PBEntry) - { - int ArgsCount = PBEntry.Arguments.Count; - - if (ArgsCount > 0) - { - Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; - } - } - - private int ReadRegister(NvGpuEngine3dReg Reg) - { - return Registers[(int)Reg]; - } - - private float ReadRegisterFloat(NvGpuEngine3dReg Reg) - { - return BitConverter.Int32BitsToSingle(ReadRegister(Reg)); - } - - private void WriteRegister(NvGpuEngine3dReg Reg, int Value) - { - Registers[(int)Reg] = Value; - } - - public bool IsFrameBufferPosition(long Position) - { - return FrameBuffers.Contains(Position); - } - - private bool QueryKeyUpload(NvGpuVmm Vmm, long Key, long Size, NvGpuBufferType Type) - { - List Uploaded = UploadedKeys[(int)Type]; - - if (Uploaded.Contains(Key)) - { - return false; - } - - Uploaded.Add(Key); - - return Vmm.IsRegionModified(Key, Size, Type); - } - } -} diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs deleted file mode 100644 index ace324e9..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3dReg.cs +++ /dev/null @@ -1,101 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Engines -{ - enum NvGpuEngine3dReg - { - FrameBufferNAddress = 0x200, - FrameBufferNWidth = 0x202, - FrameBufferNHeight = 0x203, - FrameBufferNFormat = 0x204, - ViewportNScaleX = 0x280, - ViewportNScaleY = 0x281, - ViewportNScaleZ = 0x282, - ViewportNTranslateX = 0x283, - ViewportNTranslateY = 0x284, - ViewportNTranslateZ = 0x285, - ViewportNHoriz = 0x300, - ViewportNVert = 0x301, - VertexArrayFirst = 0x35d, - VertexArrayCount = 0x35e, - ClearNColor = 0x360, - ClearDepth = 0x364, - ClearStencil = 0x368, - StencilBackFuncRef = 0x3d5, - StencilBackMask = 0x3d6, - StencilBackFuncMask = 0x3d7, - RTSeparateFragData = 0x3eb, - ZetaAddress = 0x3f8, - ZetaFormat = 0x3fa, - ZetaBlockDimensions = 0x3fb, - ZetaLayerStride = 0x3fc, - VertexAttribNFormat = 0x458, - RTControl = 0x487, - ZetaHoriz = 0x48a, - ZetaVert = 0x48b, - ZetaArrayMode = 0x48c, - DepthTestEnable = 0x4b3, - IBlendEnable = 0x4b9, - DepthTestFunction = 0x4c3, - BlendSeparateAlpha = 0x4cf, - BlendEquationRgb = 0x4d0, - BlendFuncSrcRgb = 0x4d1, - BlendFuncDstRgb = 0x4d2, - BlendEquationAlpha = 0x4d3, - BlendFuncSrcAlpha = 0x4d4, - BlendFuncDstAlpha = 0x4d6, - BlendEnableMaster = 0x4d7, - IBlendNEnable = 0x4d8, - StencilEnable = 0x4e0, - StencilFrontOpFail = 0x4e1, - StencilFrontOpZFail = 0x4e2, - StencilFrontOpZPass = 0x4e3, - StencilFrontFuncFunc = 0x4e4, - StencilFrontFuncRef = 0x4e5, - StencilFrontFuncMask = 0x4e6, - StencilFrontMask = 0x4e7, - VertexArrayElemBase = 0x50d, - VertexArrayInstBase = 0x50e, - ZetaEnable = 0x54e, - TexHeaderPoolOffset = 0x55d, - TexSamplerPoolOffset = 0x557, - StencilTwoSideEnable = 0x565, - StencilBackOpFail = 0x566, - StencilBackOpZFail = 0x567, - StencilBackOpZPass = 0x568, - StencilBackFuncFunc = 0x569, - ShaderAddress = 0x582, - VertexBeginGl = 0x586, - PrimRestartEnable = 0x591, - PrimRestartIndex = 0x592, - IndexArrayAddress = 0x5f2, - IndexArrayEndAddr = 0x5f4, - IndexArrayFormat = 0x5f6, - IndexBatchFirst = 0x5f7, - IndexBatchCount = 0x5f8, - VertexArrayNInstance = 0x620, - CullFaceEnable = 0x646, - FrontFace = 0x647, - CullFace = 0x648, - QueryAddress = 0x6c0, - QuerySequence = 0x6c2, - QueryControl = 0x6c3, - VertexArrayNControl = 0x700, - VertexArrayNAddress = 0x701, - VertexArrayNDivisor = 0x703, - IBlendNSeparateAlpha = 0x780, - IBlendNEquationRgb = 0x781, - IBlendNFuncSrcRgb = 0x782, - IBlendNFuncDstRgb = 0x783, - IBlendNEquationAlpha = 0x784, - IBlendNFuncSrcAlpha = 0x785, - IBlendNFuncDstAlpha = 0x786, - VertexArrayNEndAddr = 0x7c0, - ShaderNControl = 0x800, - ShaderNOffset = 0x801, - ShaderNMaxGprs = 0x803, - ShaderNType = 0x804, - ConstBufferSize = 0x8e0, - ConstBufferAddress = 0x8e1, - ConstBufferOffset = 0x8e3, - TextureCbIndex = 0x982 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs deleted file mode 100644 index 7e355e8d..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDma.cs +++ /dev/null @@ -1,143 +0,0 @@ -using Ryujinx.HLE.Gpu.Memory; -using Ryujinx.HLE.Gpu.Texture; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu.Engines -{ - class NvGpuEngineDma : INvGpuEngine - { - public int[] Registers { get; private set; } - - private NvGpu Gpu; - - private Dictionary Methods; - - public NvGpuEngineDma(NvGpu Gpu) - { - this.Gpu = Gpu; - - Registers = new int[0x1d6]; - - Methods = new Dictionary(); - - void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method) - { - while (Count-- > 0) - { - Methods.Add(Meth, Method); - - Meth += Stride; - } - } - - AddMethod(0xc0, 1, 1, Execute); - } - - public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method)) - { - Method(Vmm, PBEntry); - } - else - { - WriteRegister(PBEntry); - } - } - - private void Execute(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - int Control = PBEntry.Arguments[0]; - - bool SrcLinear = ((Control >> 7) & 1) != 0; - bool DstLinear = ((Control >> 8) & 1) != 0; - - long SrcAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.SrcAddress); - long DstAddress = MakeInt64From2xInt32(NvGpuEngineDmaReg.DstAddress); - - int SrcPitch = ReadRegister(NvGpuEngineDmaReg.SrcPitch); - int DstPitch = ReadRegister(NvGpuEngineDmaReg.DstPitch); - - int DstBlkDim = ReadRegister(NvGpuEngineDmaReg.DstBlkDim); - int DstSizeX = ReadRegister(NvGpuEngineDmaReg.DstSizeX); - int DstSizeY = ReadRegister(NvGpuEngineDmaReg.DstSizeY); - int DstSizeZ = ReadRegister(NvGpuEngineDmaReg.DstSizeZ); - int DstPosXY = ReadRegister(NvGpuEngineDmaReg.DstPosXY); - int DstPosZ = ReadRegister(NvGpuEngineDmaReg.DstPosZ); - - int SrcBlkDim = ReadRegister(NvGpuEngineDmaReg.SrcBlkDim); - int SrcSizeX = ReadRegister(NvGpuEngineDmaReg.SrcSizeX); - int SrcSizeY = ReadRegister(NvGpuEngineDmaReg.SrcSizeY); - int SrcSizeZ = ReadRegister(NvGpuEngineDmaReg.SrcSizeZ); - int SrcPosXY = ReadRegister(NvGpuEngineDmaReg.SrcPosXY); - int SrcPosZ = ReadRegister(NvGpuEngineDmaReg.SrcPosZ); - - int DstPosX = (DstPosXY >> 0) & 0xffff; - int DstPosY = (DstPosXY >> 16) & 0xffff; - - int SrcPosX = (SrcPosXY >> 0) & 0xffff; - int SrcPosY = (SrcPosXY >> 16) & 0xffff; - - int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf); - int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf); - - ISwizzle SrcSwizzle; - - if (SrcLinear) - { - SrcSwizzle = new LinearSwizzle(SrcPitch, 1); - } - else - { - SrcSwizzle = new BlockLinearSwizzle(SrcSizeX, 1, SrcBlockHeight); - } - - ISwizzle DstSwizzle; - - if (DstLinear) - { - DstSwizzle = new LinearSwizzle(DstPitch, 1); - } - else - { - DstSwizzle = new BlockLinearSwizzle(DstSizeX, 1, DstBlockHeight); - } - - for (int Y = 0; Y < DstSizeY; Y++) - for (int X = 0; X < DstSizeX; X++) - { - long SrcOffset = SrcAddress + (uint)SrcSwizzle.GetSwizzleOffset(X, Y); - long DstOffset = DstAddress + (uint)DstSwizzle.GetSwizzleOffset(X, Y); - - Vmm.WriteByte(DstOffset, Vmm.ReadByte(SrcOffset)); - } - } - - private long MakeInt64From2xInt32(NvGpuEngineDmaReg Reg) - { - return - (long)Registers[(int)Reg + 0] << 32 | - (uint)Registers[(int)Reg + 1]; - } - - private void WriteRegister(NvGpuPBEntry PBEntry) - { - int ArgsCount = PBEntry.Arguments.Count; - - if (ArgsCount > 0) - { - Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1]; - } - } - - private int ReadRegister(NvGpuEngineDmaReg Reg) - { - return Registers[(int)Reg]; - } - - private void WriteRegister(NvGpuEngineDmaReg Reg, int Value) - { - Registers[(int)Reg] = Value; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs deleted file mode 100644 index 835a822d..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngineDmaReg.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Engines -{ - enum NvGpuEngineDmaReg - { - SrcAddress = 0x100, - DstAddress = 0x102, - SrcPitch = 0x104, - DstPitch = 0x105, - DstBlkDim = 0x1c3, - DstSizeX = 0x1c4, - DstSizeY = 0x1c5, - DstSizeZ = 0x1c6, - DstPosZ = 0x1c7, - DstPosXY = 0x1c8, - SrcBlkDim = 0x1ca, - SrcSizeX = 0x1cb, - SrcSizeY = 0x1cc, - SrcSizeZ = 0x1cd, - SrcPosZ = 0x1ce, - SrcPosXY = 0x1cf - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs deleted file mode 100644 index 0e626654..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuFifo.cs +++ /dev/null @@ -1,198 +0,0 @@ -using Ryujinx.HLE.Gpu.Memory; -using System.Collections.Concurrent; -using System.Threading; - -namespace Ryujinx.HLE.Gpu.Engines -{ - class NvGpuFifo - { - private const int MacrosCount = 0x80; - private const int MacroIndexMask = MacrosCount - 1; - - //Note: The size of the macro memory is unknown, we just make - //a guess here and use 256kb as the size. Increase if needed. - private const int MmeWords = 256 * 256; - - private NvGpu Gpu; - - private ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry[])> BufferQueue; - - private NvGpuEngine[] SubChannels; - - public AutoResetEvent Event { get; private set; } - - private struct CachedMacro - { - public int Position { get; private set; } - - private MacroInterpreter Interpreter; - - public CachedMacro(NvGpuFifo PFifo, INvGpuEngine Engine, int Position) - { - this.Position = Position; - - Interpreter = new MacroInterpreter(PFifo, Engine); - } - - public void PushParam(int Param) - { - Interpreter?.Fifo.Enqueue(Param); - } - - public void Execute(NvGpuVmm Vmm, int[] Mme, int Param) - { - Interpreter?.Execute(Vmm, Mme, Position, Param); - } - } - - private int CurrMacroPosition; - private int CurrMacroBindIndex; - - private CachedMacro[] Macros; - - private int[] Mme; - - public NvGpuFifo(NvGpu Gpu) - { - this.Gpu = Gpu; - - BufferQueue = new ConcurrentQueue<(NvGpuVmm, NvGpuPBEntry[])>(); - - SubChannels = new NvGpuEngine[8]; - - Macros = new CachedMacro[MacrosCount]; - - Mme = new int[MmeWords]; - - Event = new AutoResetEvent(false); - } - - public void PushBuffer(NvGpuVmm Vmm, NvGpuPBEntry[] Buffer) - { - BufferQueue.Enqueue((Vmm, Buffer)); - - Event.Set(); - } - - public void DispatchCalls() - { - while (Step()); - } - - private (NvGpuVmm Vmm, NvGpuPBEntry[] Pb) Curr; - - private int CurrPbEntryIndex; - - public bool Step() - { - while (Curr.Pb == null || Curr.Pb.Length <= CurrPbEntryIndex) - { - if (!BufferQueue.TryDequeue(out Curr)) - { - return false; - } - - Gpu.Engine3d.ResetCache(); - - CurrPbEntryIndex = 0; - } - - CallMethod(Curr.Vmm, Curr.Pb[CurrPbEntryIndex++]); - - return true; - } - - private void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (PBEntry.Method < 0x80) - { - switch ((NvGpuFifoMeth)PBEntry.Method) - { - case NvGpuFifoMeth.BindChannel: - { - NvGpuEngine Engine = (NvGpuEngine)PBEntry.Arguments[0]; - - SubChannels[PBEntry.SubChannel] = Engine; - - break; - } - - case NvGpuFifoMeth.SetMacroUploadAddress: - { - CurrMacroPosition = PBEntry.Arguments[0]; - - break; - } - - case NvGpuFifoMeth.SendMacroCodeData: - { - foreach (int Arg in PBEntry.Arguments) - { - Mme[CurrMacroPosition++] = Arg; - } - break; - } - - case NvGpuFifoMeth.SetMacroBindingIndex: - { - CurrMacroBindIndex = PBEntry.Arguments[0]; - - break; - } - - case NvGpuFifoMeth.BindMacro: - { - int Position = PBEntry.Arguments[0]; - - Macros[CurrMacroBindIndex] = new CachedMacro(this, Gpu.Engine3d, Position); - - break; - } - } - } - else - { - switch (SubChannels[PBEntry.SubChannel]) - { - case NvGpuEngine._2d: Call2dMethod (Vmm, PBEntry); break; - case NvGpuEngine._3d: Call3dMethod (Vmm, PBEntry); break; - case NvGpuEngine.Dma: CallDmaMethod(Vmm, PBEntry); break; - } - } - } - - private void Call2dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - Gpu.Engine2d.CallMethod(Vmm, PBEntry); - } - - private void Call3dMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - if (PBEntry.Method < 0xe00) - { - Gpu.Engine3d.CallMethod(Vmm, PBEntry); - } - else - { - int MacroIndex = (PBEntry.Method >> 1) & MacroIndexMask; - - if ((PBEntry.Method & 1) != 0) - { - foreach (int Arg in PBEntry.Arguments) - { - Macros[MacroIndex].PushParam(Arg); - } - } - else - { - Macros[MacroIndex].Execute(Vmm, Mme, PBEntry.Arguments[0]); - } - } - } - - private void CallDmaMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) - { - Gpu.EngineDma.CallMethod(Vmm, PBEntry); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs deleted file mode 100644 index ffd179f2..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuFifoMeth.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Engines -{ - enum NvGpuFifoMeth - { - BindChannel = 0, - SetMacroUploadAddress = 0x45, - SendMacroCodeData = 0x46, - SetMacroBindingIndex = 0x47, - BindMacro = 0x48 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs deleted file mode 100644 index 04c92f2a..00000000 --- a/Ryujinx.HLE/Gpu/Engines/NvGpuMethod.cs +++ /dev/null @@ -1,6 +0,0 @@ -using Ryujinx.HLE.Gpu.Memory; - -namespace Ryujinx.HLE.Gpu.Engines -{ - delegate void NvGpuMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry); -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs deleted file mode 100644 index a6c03f42..00000000 --- a/Ryujinx.HLE/Gpu/Memory/NvGpuBufferType.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Memory -{ - enum NvGpuBufferType - { - Index, - Vertex, - Texture, - ConstBuffer, - Count - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs deleted file mode 100644 index aba89e3c..00000000 --- a/Ryujinx.HLE/Gpu/Memory/NvGpuPBEntry.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.ObjectModel; - -namespace Ryujinx.HLE.Gpu.Memory -{ - struct NvGpuPBEntry - { - public int Method { get; private set; } - - public int SubChannel { get; private set; } - - private int[] m_Arguments; - - public ReadOnlyCollection Arguments => Array.AsReadOnly(m_Arguments); - - public NvGpuPBEntry(int Method, int SubChannel, params int[] Arguments) - { - this.Method = Method; - this.SubChannel = SubChannel; - this.m_Arguments = Arguments; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs deleted file mode 100644 index 6121b3e6..00000000 --- a/Ryujinx.HLE/Gpu/Memory/NvGpuPushBuffer.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System.Collections.Generic; -using System.IO; - -namespace Ryujinx.HLE.Gpu.Memory -{ - static class NvGpuPushBuffer - { - private enum SubmissionMode - { - Incrementing = 1, - NonIncrementing = 3, - Immediate = 4, - IncrementOnce = 5 - } - - public static NvGpuPBEntry[] Decode(byte[] Data) - { - using (MemoryStream MS = new MemoryStream(Data)) - { - BinaryReader Reader = new BinaryReader(MS); - - List PushBuffer = new List(); - - bool CanRead() => MS.Position + 4 <= MS.Length; - - while (CanRead()) - { - int Packed = Reader.ReadInt32(); - - int Meth = (Packed >> 0) & 0x1fff; - int SubC = (Packed >> 13) & 7; - int Args = (Packed >> 16) & 0x1fff; - int Mode = (Packed >> 29) & 7; - - switch ((SubmissionMode)Mode) - { - case SubmissionMode.Incrementing: - { - for (int Index = 0; Index < Args && CanRead(); Index++, Meth++) - { - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); - } - - break; - } - - case SubmissionMode.NonIncrementing: - { - int[] Arguments = new int[Args]; - - for (int Index = 0; Index < Arguments.Length; Index++) - { - if (!CanRead()) - { - break; - } - - Arguments[Index] = Reader.ReadInt32(); - } - - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Arguments)); - - break; - } - - case SubmissionMode.Immediate: - { - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Args)); - - break; - } - - case SubmissionMode.IncrementOnce: - { - if (CanRead()) - { - PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32())); - } - - if (CanRead() && Args > 1) - { - int[] Arguments = new int[Args - 1]; - - for (int Index = 0; Index < Arguments.Length && CanRead(); Index++) - { - Arguments[Index] = Reader.ReadInt32(); - } - - PushBuffer.Add(new NvGpuPBEntry(Meth + 1, SubC, Arguments)); - } - - break; - } - } - } - - return PushBuffer.ToArray(); - } - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs deleted file mode 100644 index e7e18064..00000000 --- a/Ryujinx.HLE/Gpu/Memory/NvGpuVmm.cs +++ /dev/null @@ -1,377 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using System; - -namespace Ryujinx.HLE.Gpu.Memory -{ - class NvGpuVmm : IAMemory, IGalMemory - { - public const long AddrSize = 1L << 40; - - private const int PTLvl0Bits = 14; - private const int PTLvl1Bits = 14; - private const int PTPageBits = 12; - - private const int PTLvl0Size = 1 << PTLvl0Bits; - private const int PTLvl1Size = 1 << PTLvl1Bits; - public const int PageSize = 1 << PTPageBits; - - private const int PTLvl0Mask = PTLvl0Size - 1; - private const int PTLvl1Mask = PTLvl1Size - 1; - public const int PageMask = PageSize - 1; - - private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; - private const int PTLvl1Bit = PTPageBits; - - public AMemory Memory { get; private set; } - - private NvGpuVmmCache Cache; - - private const long PteUnmapped = -1; - private const long PteReserved = -2; - - private long[][] PageTable; - - public NvGpuVmm(AMemory Memory) - { - this.Memory = Memory; - - Cache = new NvGpuVmmCache(); - - PageTable = new long[PTLvl0Size][]; - } - - public long Map(long PA, long VA, long Size) - { - lock (PageTable) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PA + Offset); - } - } - - return VA; - } - - public long Map(long PA, long Size) - { - lock (PageTable) - { - long VA = GetFreePosition(Size); - - if (VA != -1) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PA + Offset); - } - } - - return VA; - } - } - - public long ReserveFixed(long VA, long Size) - { - lock (PageTable) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - if (IsPageInUse(VA + Offset)) - { - return -1; - } - } - - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PteReserved); - } - } - - return VA; - } - - public long Reserve(long Size, long Align) - { - lock (PageTable) - { - long Position = GetFreePosition(Size, Align); - - if (Position != -1) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(Position + Offset, PteReserved); - } - } - - return Position; - } - } - - public void Free(long VA, long Size) - { - lock (PageTable) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - SetPte(VA + Offset, PteUnmapped); - } - } - } - - private long GetFreePosition(long Size, long Align = 1) - { - //Note: Address 0 is not considered valid by the driver, - //when 0 is returned it's considered a mapping error. - long Position = PageSize; - long FreeSize = 0; - - if (Align < 1) - { - Align = 1; - } - - Align = (Align + PageMask) & ~PageMask; - - while (Position + FreeSize < AddrSize) - { - if (!IsPageInUse(Position + FreeSize)) - { - FreeSize += PageSize; - - if (FreeSize >= Size) - { - return Position; - } - } - else - { - Position += FreeSize + PageSize; - FreeSize = 0; - - long Remainder = Position % Align; - - if (Remainder != 0) - { - Position = (Position - Remainder) + Align; - } - } - } - - return -1; - } - - public long GetPhysicalAddress(long VA) - { - long BasePos = GetPte(VA); - - if (BasePos < 0) - { - return -1; - } - - return BasePos + (VA & PageMask); - } - - public bool IsRegionFree(long VA, long Size) - { - for (long Offset = 0; Offset < Size; Offset += PageSize) - { - if (IsPageInUse(VA + Offset)) - { - return false; - } - } - - return true; - } - - private bool IsPageInUse(long VA) - { - if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0) - { - return false; - } - - long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask; - long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask; - - if (PageTable[L0] == null) - { - return false; - } - - return PageTable[L0][L1] != PteUnmapped; - } - - private long GetPte(long Position) - { - long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; - long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; - - if (PageTable[L0] == null) - { - return -1; - } - - return PageTable[L0][L1]; - } - - private void SetPte(long Position, long TgtAddr) - { - long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask; - long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask; - - if (PageTable[L0] == null) - { - PageTable[L0] = new long[PTLvl1Size]; - - for (int Index = 0; Index < PTLvl1Size; Index++) - { - PageTable[L0][Index] = PteUnmapped; - } - } - - PageTable[L0][L1] = TgtAddr; - } - - public bool IsRegionModified(long PA, long Size, NvGpuBufferType BufferType) - { - return Cache.IsRegionModified(Memory, BufferType, PA, Size); - } - - public IntPtr GetHostAddress(long Position, long Size) - { - return Memory.GetHostAddress(GetPhysicalAddress(Position), Size); - } - - public byte ReadByte(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadByte(Position); - } - - public ushort ReadUInt16(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadUInt16(Position); - } - - public uint ReadUInt32(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadUInt32(Position); - } - - public ulong ReadUInt64(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadUInt64(Position); - } - - public sbyte ReadSByte(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadSByte(Position); - } - - public short ReadInt16(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadInt16(Position); - } - - public int ReadInt32(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadInt32(Position); - } - - public long ReadInt64(long Position) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadInt64(Position); - } - - public byte[] ReadBytes(long Position, long Size) - { - Position = GetPhysicalAddress(Position); - - return Memory.ReadBytes(Position, Size); - } - - public void WriteByte(long Position, byte Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteByte(Position, Value); - } - - public void WriteUInt16(long Position, ushort Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteUInt16(Position, Value); - } - - public void WriteUInt32(long Position, uint Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteUInt32(Position, Value); - } - - public void WriteUInt64(long Position, ulong Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteUInt64(Position, Value); - } - - public void WriteSByte(long Position, sbyte Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteSByte(Position, Value); - } - - public void WriteInt16(long Position, short Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteInt16(Position, Value); - } - - public void WriteInt32(long Position, int Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteInt32(Position, Value); - } - - public void WriteInt64(long Position, long Value) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteInt64(Position, Value); - } - - public void WriteBytes(long Position, byte[] Data) - { - Position = GetPhysicalAddress(Position); - - Memory.WriteBytes(Position, Data); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs b/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs deleted file mode 100644 index b3f253b3..00000000 --- a/Ryujinx.HLE/Gpu/Memory/NvGpuVmmCache.cs +++ /dev/null @@ -1,308 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.HLE.Memory; -using System; -using System.Collections.Generic; - -namespace Ryujinx.HLE.Gpu.Memory -{ - class NvGpuVmmCache - { - private const int MaxCpCount = 10000; - private const int MaxCpTimeDelta = 60000; - - private class CachedPage - { - private struct Range - { - public long Start; - public long End; - - public Range(long Start, long End) - { - this.Start = Start; - this.End = End; - } - } - - private List[] Regions; - - private HashSet ResidencyKeys; - - public LinkedListNode Node { get; set; } - - public int Timestamp { get; private set; } - - public CachedPage() - { - Regions = new List[(int)NvGpuBufferType.Count]; - - for (int Index = 0; Index < Regions.Length; Index++) - { - Regions[Index] = new List(); - } - - ResidencyKeys = new HashSet(); - } - - public void AddResidency(long Key) - { - ResidencyKeys.Add(Key); - } - - public void RemoveResidency(HashSet[] Residency, long PageSize) - { - for (int i = 0; i < (int)NvGpuBufferType.Count; i++) - { - foreach (Range Region in Regions[i]) - { - foreach (long Key in ResidencyKeys) - { - Residency[Region.Start / PageSize].Remove(Key); - } - } - } - } - - public bool AddRange(long Start, long End, NvGpuBufferType BufferType) - { - List BtRegions = Regions[(int)BufferType]; - - for (int Index = 0; Index < BtRegions.Count; Index++) - { - Range Rg = BtRegions[Index]; - - if (Start >= Rg.Start && End <= Rg.End) - { - return false; - } - - if (Start <= Rg.End && Rg.Start <= End) - { - long MinStart = Math.Min(Rg.Start, Start); - long MaxEnd = Math.Max(Rg.End, End); - - BtRegions[Index] = new Range(MinStart, MaxEnd); - - Timestamp = Environment.TickCount; - - return true; - } - } - - BtRegions.Add(new Range(Start, End)); - - Timestamp = Environment.TickCount; - - return true; - } - - public int GetTotalCount() - { - int Count = 0; - - for (int Index = 0; Index < Regions.Length; Index++) - { - Count += Regions[Index].Count; - } - - return Count; - } - } - - private Dictionary Cache; - - private LinkedList SortedCache; - - private HashSet[] Residency; - - private long ResidencyPageSize; - - private int CpCount; - - public NvGpuVmmCache() - { - Cache = new Dictionary(); - - SortedCache = new LinkedList(); - } - - public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size) - { - (bool[] Modified, long ModifiedCount) = Memory.IsRegionModified(PA, Size); - - PA = Memory.GetPhysicalAddress(PA); - - ClearCachedPagesIfNeeded(); - - long PageSize = AMemory.PageSize; - - EnsureResidencyInitialized(PageSize); - - bool HasResidents = AddResidency(PA, Size); - - if (!HasResidents && ModifiedCount == 0) - { - return false; - } - - long Mask = PageSize - 1; - - long ResidencyKey = PA; - - long PAEnd = PA + Size; - - bool RegMod = false; - - int Index = 0; - - while (PA < PAEnd) - { - long Key = PA & ~AMemory.PageMask; - - long PAPgEnd = Math.Min((PA + AMemory.PageSize) & ~AMemory.PageMask, PAEnd); - - bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp); - - if (IsCached) - { - CpCount -= Cp.GetTotalCount(); - - SortedCache.Remove(Cp.Node); - } - else - { - Cp = new CachedPage(); - - Cache.Add(Key, Cp); - } - - if (Modified[Index++] && IsCached) - { - Cp = new CachedPage(); - - Cache[Key] = Cp; - } - - Cp.AddResidency(ResidencyKey); - - Cp.Node = SortedCache.AddLast(Key); - - RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType); - - CpCount += Cp.GetTotalCount(); - - PA = PAPgEnd; - } - - return RegMod; - } - - private bool AddResidency(long PA, long Size) - { - long PageSize = ResidencyPageSize; - - long Mask = PageSize - 1; - - long Key = PA; - - bool ResidentFound = false; - - for (long Cursor = PA & ~Mask; Cursor < ((PA + Size + PageSize - 1) & ~Mask); Cursor += PageSize) - { - long PageIndex = Cursor / PageSize; - - Residency[PageIndex].Add(Key); - - if (Residency[PageIndex].Count > 1) - { - ResidentFound = true; - } - } - - return ResidentFound; - } - - private void EnsureResidencyInitialized(long PageSize) - { - if (Residency == null) - { - Residency = new HashSet[DeviceMemory.RamSize / PageSize]; - - for (int i = 0; i < Residency.Length; i++) - { - Residency[i] = new HashSet(); - } - - ResidencyPageSize = PageSize; - } - else - { - if (ResidencyPageSize != PageSize) - { - throw new InvalidOperationException("Tried to change residency page size"); - } - } - } - - private void ClearCachedPagesIfNeeded() - { - if (CpCount <= MaxCpCount) - { - return; - } - - int Timestamp = Environment.TickCount; - - int TimeDelta; - - do - { - if (!TryPopOldestCachedPageKey(Timestamp, out long Key)) - { - break; - } - - CachedPage Cp = Cache[Key]; - - Cp.RemoveResidency(Residency, ResidencyPageSize); - - Cache.Remove(Key); - - CpCount -= Cp.GetTotalCount(); - - TimeDelta = RingDelta(Cp.Timestamp, Timestamp); - } - while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta); - } - - private bool TryPopOldestCachedPageKey(int Timestamp, out long Key) - { - LinkedListNode Node = SortedCache.First; - - if (Node == null) - { - Key = 0; - - return false; - } - - SortedCache.Remove(Node); - - Key = Node.Value; - - return true; - } - - private int RingDelta(int Old, int New) - { - if ((uint)New < (uint)Old) - { - return New + (~Old + 1); - } - else - { - return New - Old; - } - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/NvGpu.cs b/Ryujinx.HLE/Gpu/NvGpu.cs deleted file mode 100644 index 625cb727..00000000 --- a/Ryujinx.HLE/Gpu/NvGpu.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu.Engines; - -namespace Ryujinx.HLE.Gpu -{ - class NvGpu - { - public IGalRenderer Renderer { get; private set; } - - public NvGpuFifo Fifo { get; private set; } - - public NvGpuEngine2d Engine2d { get; private set; } - public NvGpuEngine3d Engine3d { get; private set; } - public NvGpuEngineDma EngineDma { get; private set; } - - public NvGpu(IGalRenderer Renderer) - { - this.Renderer = Renderer; - - Fifo = new NvGpuFifo(this); - - Engine2d = new NvGpuEngine2d(this); - Engine3d = new NvGpuEngine3d(this); - EngineDma = new NvGpuEngineDma(this); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs b/Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs deleted file mode 100644 index e66d7613..00000000 --- a/Ryujinx.HLE/Gpu/Texture/BlockLinearSwizzle.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; - -namespace Ryujinx.HLE.Gpu.Texture -{ - class BlockLinearSwizzle : ISwizzle - { - private int BhShift; - private int BppShift; - private int BhMask; - - private int XShift; - private int GobStride; - - public BlockLinearSwizzle(int Width, int Bpp, int BlockHeight = 16) - { - BhMask = (BlockHeight * 8) - 1; - - BhShift = CountLsbZeros(BlockHeight * 8); - BppShift = CountLsbZeros(Bpp); - - int WidthInGobs = (int)MathF.Ceiling(Width * Bpp / 64f); - - GobStride = 512 * BlockHeight * WidthInGobs; - - XShift = CountLsbZeros(512 * BlockHeight); - } - - private int CountLsbZeros(int Value) - { - int Count = 0; - - while (((Value >> Count) & 1) == 0) - { - Count++; - } - - return Count; - } - - public int GetSwizzleOffset(int X, int Y) - { - X <<= BppShift; - - int Position = (Y >> BhShift) * GobStride; - - Position += (X >> 6) << XShift; - - Position += ((Y & BhMask) >> 3) << 9; - - Position += ((X & 0x3f) >> 5) << 8; - Position += ((Y & 0x07) >> 1) << 6; - Position += ((X & 0x1f) >> 4) << 5; - Position += ((Y & 0x01) >> 0) << 4; - Position += ((X & 0x0f) >> 0) << 0; - - return Position; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/ISwizzle.cs b/Ryujinx.HLE/Gpu/Texture/ISwizzle.cs deleted file mode 100644 index 222aab16..00000000 --- a/Ryujinx.HLE/Gpu/Texture/ISwizzle.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Texture -{ - interface ISwizzle - { - int GetSwizzleOffset(int X, int Y); - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs b/Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs deleted file mode 100644 index 720f7832..00000000 --- a/Ryujinx.HLE/Gpu/Texture/LinearSwizzle.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Texture -{ - class LinearSwizzle : ISwizzle - { - private int Pitch; - private int Bpp; - - public LinearSwizzle(int Pitch, int Bpp) - { - this.Pitch = Pitch; - this.Bpp = Bpp; - } - - public int GetSwizzleOffset(int X, int Y) - { - return X * Bpp + Y * Pitch; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureFactory.cs b/Ryujinx.HLE/Gpu/Texture/TextureFactory.cs deleted file mode 100644 index 0ef33d3b..00000000 --- a/Ryujinx.HLE/Gpu/Texture/TextureFactory.cs +++ /dev/null @@ -1,125 +0,0 @@ -using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu.Memory; -using System; - -namespace Ryujinx.HLE.Gpu.Texture -{ - static class TextureFactory - { - public static GalImage MakeTexture(NvGpuVmm Vmm, long TicPosition) - { - int[] Tic = ReadWords(Vmm, TicPosition, 8); - - GalTextureType RType = (GalTextureType)((Tic[0] >> 7) & 7); - GalTextureType GType = (GalTextureType)((Tic[0] >> 10) & 7); - GalTextureType BType = (GalTextureType)((Tic[0] >> 13) & 7); - GalTextureType AType = (GalTextureType)((Tic[0] >> 16) & 7); - - GalImageFormat Format = ImageFormatConverter.ConvertTexture((GalTextureFormat)(Tic[0] & 0x7f), RType, GType, BType, AType); - - GalTextureSource XSource = (GalTextureSource)((Tic[0] >> 19) & 7); - GalTextureSource YSource = (GalTextureSource)((Tic[0] >> 22) & 7); - GalTextureSource ZSource = (GalTextureSource)((Tic[0] >> 25) & 7); - GalTextureSource WSource = (GalTextureSource)((Tic[0] >> 28) & 7); - - int Width = (Tic[4] & 0xffff) + 1; - int Height = (Tic[5] & 0xffff) + 1; - - return new GalImage( - Width, - Height, - Format, - XSource, - YSource, - ZSource, - WSource); - } - - public static byte[] GetTextureData(NvGpuVmm Vmm, long TicPosition) - { - int[] Tic = ReadWords(Vmm, TicPosition, 8); - - GalTextureFormat Format = (GalTextureFormat)(Tic[0] & 0x7f); - - long TextureAddress = (uint)Tic[1]; - - TextureAddress |= (long)((ushort)Tic[2]) << 32; - - TextureSwizzle Swizzle = (TextureSwizzle)((Tic[2] >> 21) & 7); - - if (Swizzle == TextureSwizzle.BlockLinear || - Swizzle == TextureSwizzle.BlockLinearColorKey) - { - TextureAddress &= ~0x1ffL; - } - else if (Swizzle == TextureSwizzle.Pitch || - Swizzle == TextureSwizzle.PitchColorKey) - { - TextureAddress &= ~0x1fL; - } - - int Pitch = (Tic[3] & 0xffff) << 5; - - int BlockHeightLog2 = (Tic[3] >> 3) & 7; - int TileWidthLog2 = (Tic[3] >> 10) & 7; - - int BlockHeight = 1 << BlockHeightLog2; - int TileWidth = 1 << TileWidthLog2; - - int Width = (Tic[4] & 0xffff) + 1; - int Height = (Tic[5] & 0xffff) + 1; - - TextureInfo Texture = new TextureInfo( - TextureAddress, - Width, - Height, - Pitch, - BlockHeight, - TileWidth, - Swizzle, - Format); - - return TextureReader.Read(Vmm, Texture); - } - - public static GalTextureSampler MakeSampler(NvGpu Gpu, NvGpuVmm Vmm, long TscPosition) - { - int[] Tsc = ReadWords(Vmm, TscPosition, 8); - - GalTextureWrap AddressU = (GalTextureWrap)((Tsc[0] >> 0) & 7); - GalTextureWrap AddressV = (GalTextureWrap)((Tsc[0] >> 3) & 7); - GalTextureWrap AddressP = (GalTextureWrap)((Tsc[0] >> 6) & 7); - - GalTextureFilter MagFilter = (GalTextureFilter) ((Tsc[1] >> 0) & 3); - GalTextureFilter MinFilter = (GalTextureFilter) ((Tsc[1] >> 4) & 3); - GalTextureMipFilter MipFilter = (GalTextureMipFilter)((Tsc[1] >> 6) & 3); - - GalColorF BorderColor = new GalColorF( - BitConverter.Int32BitsToSingle(Tsc[4]), - BitConverter.Int32BitsToSingle(Tsc[5]), - BitConverter.Int32BitsToSingle(Tsc[6]), - BitConverter.Int32BitsToSingle(Tsc[7])); - - return new GalTextureSampler( - AddressU, - AddressV, - AddressP, - MinFilter, - MagFilter, - MipFilter, - BorderColor); - } - - private static int[] ReadWords(NvGpuVmm Vmm, long Position, int Count) - { - int[] Words = new int[Count]; - - for (int Index = 0; Index < Count; Index++, Position += 4) - { - Words[Index] = Vmm.ReadInt32(Position); - } - - return Words; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs b/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs deleted file mode 100644 index 2683174d..00000000 --- a/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs +++ /dev/null @@ -1,204 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu.Memory; -using System; - -namespace Ryujinx.HLE.Gpu.Texture -{ - static class TextureHelper - { - public static ISwizzle GetSwizzle(TextureInfo Texture, int BlockWidth, int Bpp) - { - int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth; - - int AlignMask = Texture.TileWidth * (64 / Bpp) - 1; - - Width = (Width + AlignMask) & ~AlignMask; - - switch (Texture.Swizzle) - { - case TextureSwizzle._1dBuffer: - case TextureSwizzle.Pitch: - case TextureSwizzle.PitchColorKey: - return new LinearSwizzle(Texture.Pitch, Bpp); - - case TextureSwizzle.BlockLinear: - case TextureSwizzle.BlockLinearColorKey: - return new BlockLinearSwizzle(Width, Bpp, Texture.BlockHeight); - } - - throw new NotImplementedException(Texture.Swizzle.ToString()); - } - - public static int GetTextureSize(GalImage Image) - { - switch (Image.Format) - { - case GalImageFormat.R32G32B32A32_SFLOAT: - case GalImageFormat.R32G32B32A32_SINT: - case GalImageFormat.R32G32B32A32_UINT: - return Image.Width * Image.Height * 16; - - case GalImageFormat.R16G16B16A16_SFLOAT: - case GalImageFormat.R16G16B16A16_SINT: - case GalImageFormat.R16G16B16A16_SNORM: - case GalImageFormat.R16G16B16A16_UINT: - case GalImageFormat.R16G16B16A16_UNORM: - case GalImageFormat.D32_SFLOAT_S8_UINT: - case GalImageFormat.R32G32_SFLOAT: - case GalImageFormat.R32G32_SINT: - case GalImageFormat.R32G32_UINT: - return Image.Width * Image.Height * 8; - - case GalImageFormat.A8B8G8R8_SINT_PACK32: - case GalImageFormat.A8B8G8R8_SNORM_PACK32: - case GalImageFormat.A8B8G8R8_UINT_PACK32: - case GalImageFormat.A8B8G8R8_UNORM_PACK32: - case GalImageFormat.A8B8G8R8_SRGB_PACK32: - case GalImageFormat.A2B10G10R10_SINT_PACK32: - case GalImageFormat.A2B10G10R10_SNORM_PACK32: - case GalImageFormat.A2B10G10R10_UINT_PACK32: - case GalImageFormat.A2B10G10R10_UNORM_PACK32: - case GalImageFormat.R16G16_SFLOAT: - case GalImageFormat.R16G16_SINT: - case GalImageFormat.R16G16_SNORM: - case GalImageFormat.R16G16_UINT: - case GalImageFormat.R16G16_UNORM: - case GalImageFormat.R32_SFLOAT: - case GalImageFormat.R32_SINT: - case GalImageFormat.R32_UINT: - case GalImageFormat.D32_SFLOAT: - case GalImageFormat.B10G11R11_UFLOAT_PACK32: - case GalImageFormat.D24_UNORM_S8_UINT: - return Image.Width * Image.Height * 4; - - case GalImageFormat.B4G4R4A4_UNORM_PACK16: - case GalImageFormat.A1R5G5B5_UNORM_PACK16: - case GalImageFormat.B5G6R5_UNORM_PACK16: - case GalImageFormat.R8G8_SINT: - case GalImageFormat.R8G8_SNORM: - case GalImageFormat.R8G8_UINT: - case GalImageFormat.R8G8_UNORM: - case GalImageFormat.R16_SFLOAT: - case GalImageFormat.R16_SINT: - case GalImageFormat.R16_SNORM: - case GalImageFormat.R16_UINT: - case GalImageFormat.R16_UNORM: - case GalImageFormat.D16_UNORM: - return Image.Width * Image.Height * 2; - - case GalImageFormat.R8_SINT: - case GalImageFormat.R8_SNORM: - case GalImageFormat.R8_UINT: - case GalImageFormat.R8_UNORM: - return Image.Width * Image.Height; - - case GalImageFormat.BC1_RGBA_UNORM_BLOCK: - case GalImageFormat.BC4_SNORM_BLOCK: - case GalImageFormat.BC4_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 8); - } - - case GalImageFormat.BC6H_SFLOAT_BLOCK: - case GalImageFormat.BC6H_UFLOAT_BLOCK: - case GalImageFormat.BC7_UNORM_BLOCK: - case GalImageFormat.BC2_UNORM_BLOCK: - case GalImageFormat.BC3_UNORM_BLOCK: - case GalImageFormat.BC5_SNORM_BLOCK: - case GalImageFormat.BC5_UNORM_BLOCK: - case GalImageFormat.ASTC_4x4_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 4, 4, 16); - } - - case GalImageFormat.ASTC_5x5_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 5, 5, 16); - } - - case GalImageFormat.ASTC_6x6_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 6, 6, 16); - } - - case GalImageFormat.ASTC_8x8_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 8, 8, 16); - } - - case GalImageFormat.ASTC_10x10_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 10, 10, 16); - } - - case GalImageFormat.ASTC_12x12_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 12, 12, 16); - } - - case GalImageFormat.ASTC_5x4_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 5, 4, 16); - } - - case GalImageFormat.ASTC_6x5_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 6, 5, 16); - } - - case GalImageFormat.ASTC_8x6_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 8, 6, 16); - } - - case GalImageFormat.ASTC_10x8_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 10, 8, 16); - } - - case GalImageFormat.ASTC_12x10_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 12, 10, 16); - } - - case GalImageFormat.ASTC_8x5_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 8, 5, 16); - } - - case GalImageFormat.ASTC_10x5_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 10, 5, 16); - } - - case GalImageFormat.ASTC_10x6_UNORM_BLOCK: - { - return CompressedTextureSize(Image.Width, Image.Height, 10, 6, 16); - } - } - - throw new NotImplementedException(Image.Format.ToString()); - } - - public static int CompressedTextureSize(int TextureWidth, int TextureHeight, int BlockWidth, int BlockHeight, int Bpb) - { - int W = (TextureWidth + (BlockWidth - 1)) / BlockWidth; - int H = (TextureHeight + (BlockHeight - 1)) / BlockHeight; - - return W * H * Bpb; - } - - public static (AMemory Memory, long Position) GetMemoryAndPosition( - IAMemory Memory, - long Position) - { - if (Memory is NvGpuVmm Vmm) - { - return (Vmm.Memory, Vmm.GetPhysicalAddress(Position)); - } - - return ((AMemory)Memory, Position); - } - } -} diff --git a/Ryujinx.HLE/Gpu/Texture/TextureInfo.cs b/Ryujinx.HLE/Gpu/Texture/TextureInfo.cs deleted file mode 100644 index 2a98ce00..00000000 --- a/Ryujinx.HLE/Gpu/Texture/TextureInfo.cs +++ /dev/null @@ -1,60 +0,0 @@ -using Ryujinx.Graphics.Gal; - -namespace Ryujinx.HLE.Gpu.Texture -{ - struct TextureInfo - { - public long Position { get; private set; } - - public int Width { get; private set; } - public int Height { get; private set; } - public int Pitch { get; private set; } - - public int BlockHeight { get; private set; } - public int TileWidth { get; private set; } - - public TextureSwizzle Swizzle { get; private set; } - - public GalTextureFormat Format { get; private set; } - - public TextureInfo( - long Position, - int Width, - int Height) - { - this.Position = Position; - this.Width = Width; - this.Height = Height; - - Pitch = 0; - - BlockHeight = 16; - - TileWidth = 1; - - Swizzle = TextureSwizzle.BlockLinear; - - Format = GalTextureFormat.A8B8G8R8; - } - - public TextureInfo( - long Position, - int Width, - int Height, - int Pitch, - int BlockHeight, - int TileWidth, - TextureSwizzle Swizzle, - GalTextureFormat Format) - { - this.Position = Position; - this.Width = Width; - this.Height = Height; - this.Pitch = Pitch; - this.BlockHeight = BlockHeight; - this.TileWidth = TileWidth; - this.Swizzle = Swizzle; - this.Format = Format; - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureReader.cs b/Ryujinx.HLE/Gpu/Texture/TextureReader.cs deleted file mode 100644 index d293bf9f..00000000 --- a/Ryujinx.HLE/Gpu/Texture/TextureReader.cs +++ /dev/null @@ -1,366 +0,0 @@ -using ChocolArm64.Memory; -using Ryujinx.Graphics.Gal; -using System; - -namespace Ryujinx.HLE.Gpu.Texture -{ - static class TextureReader - { - public static byte[] Read(IAMemory Memory, TextureInfo Texture) - { - switch (Texture.Format) - { - case GalTextureFormat.R32G32B32A32: return Read16Bpp (Memory, Texture); - case GalTextureFormat.R16G16B16A16: return Read8Bpp (Memory, Texture); - case GalTextureFormat.R32G32: return Read8Bpp (Memory, Texture); - case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture); - case GalTextureFormat.A2B10G10R10: return Read4Bpp (Memory, Texture); - case GalTextureFormat.R32: return Read4Bpp (Memory, Texture); - case GalTextureFormat.BF10GF11RF11: return Read4Bpp (Memory, Texture); - case GalTextureFormat.Z24S8: return Read4Bpp (Memory, Texture); - case GalTextureFormat.A1B5G5R5: return Read5551 (Memory, Texture); - case GalTextureFormat.B5G6R5: return Read565 (Memory, Texture); - case GalTextureFormat.A4B4G4R4: return Read2Bpp (Memory, Texture); - case GalTextureFormat.G8R8: return Read2Bpp (Memory, Texture); - case GalTextureFormat.R16: return Read2Bpp (Memory, Texture); - case GalTextureFormat.R8: return Read1Bpp (Memory, Texture); - case GalTextureFormat.BC6H_SF16: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.BC6H_UF16: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.BC7U: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture); - case GalTextureFormat.BC2: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.BC3: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture); - case GalTextureFormat.BC5: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.ZF32: return Read4Bpp (Memory, Texture); - case GalTextureFormat.ZF32_X24S8: return Read8Bpp (Memory, Texture); - case GalTextureFormat.Astc2D4x4: return Read16BptCompressedTexture(Memory, Texture, 4, 4); - case GalTextureFormat.Astc2D5x5: return Read16BptCompressedTexture(Memory, Texture, 5, 5); - case GalTextureFormat.Astc2D6x6: return Read16BptCompressedTexture(Memory, Texture, 6, 6); - case GalTextureFormat.Astc2D8x8: return Read16BptCompressedTexture(Memory, Texture, 8, 8); - case GalTextureFormat.Astc2D10x10: return Read16BptCompressedTexture(Memory, Texture, 10, 10); - case GalTextureFormat.Astc2D12x12: return Read16BptCompressedTexture(Memory, Texture, 12, 12); - case GalTextureFormat.Astc2D5x4: return Read16BptCompressedTexture(Memory, Texture, 5, 4); - case GalTextureFormat.Astc2D6x5: return Read16BptCompressedTexture(Memory, Texture, 6, 5); - case GalTextureFormat.Astc2D8x6: return Read16BptCompressedTexture(Memory, Texture, 8, 6); - case GalTextureFormat.Astc2D10x8: return Read16BptCompressedTexture(Memory, Texture, 10, 8); - case GalTextureFormat.Astc2D12x10: return Read16BptCompressedTexture(Memory, Texture, 12, 10); - case GalTextureFormat.Astc2D8x5: return Read16BptCompressedTexture(Memory, Texture, 8, 5); - case GalTextureFormat.Astc2D10x5: return Read16BptCompressedTexture(Memory, Texture, 10, 5); - case GalTextureFormat.Astc2D10x6: return Read16BptCompressedTexture(Memory, Texture, 10, 6); - } - - throw new NotImplementedException("0x" + ((int)Texture.Format).ToString("x2")); - } - - private unsafe static byte[] Read1Bpp(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 1); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - byte Pixel = CpuMem.ReadByte(Position + Offset); - - *(BuffPtr + OutOffs) = Pixel; - - OutOffs++; - } - } - - return Output; - } - - private unsafe static byte[] Read5551(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 2]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset); - - Pixel = (Pixel & 0x001f) << 11 | - (Pixel & 0x03e0) << 1 | - (Pixel & 0x7c00) >> 9 | - (Pixel & 0x8000) >> 15; - - *(short*)(BuffPtr + OutOffs) = (short)Pixel; - - OutOffs += 2; - } - } - - return Output; - } - - private unsafe static byte[] Read565(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 2]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - uint Pixel = (uint)CpuMem.ReadInt16(Position + Offset); - - Pixel = (Pixel & 0x001f) << 11 | - (Pixel & 0x07e0) | - (Pixel & 0xf800) >> 11; - - *(short*)(BuffPtr + OutOffs) = (short)Pixel; - - OutOffs += 2; - } - } - - return Output; - } - - private unsafe static byte[] Read2Bpp(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 2]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 2); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - short Pixel = CpuMem.ReadInt16(Position + Offset); - - *(short*)(BuffPtr + OutOffs) = Pixel; - - OutOffs += 2; - } - } - - return Output; - } - - private unsafe static byte[] Read4Bpp(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 4]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - int Pixel = CpuMem.ReadInt32(Position + Offset); - - *(int*)(BuffPtr + OutOffs) = Pixel; - - OutOffs += 4; - } - } - - return Output; - } - - private unsafe static byte[] Read8Bpp(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 8]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 8); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long Pixel = CpuMem.ReadInt64(Position + Offset); - - *(long*)(BuffPtr + OutOffs) = Pixel; - - OutOffs += 8; - } - } - - return Output; - } - - private unsafe static byte[] Read16Bpp(IAMemory Memory, TextureInfo Texture) - { - int Width = Texture.Width; - int Height = Texture.Height; - - byte[] Output = new byte[Width * Height * 16]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 16); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long PxLow = CpuMem.ReadInt64(Position + Offset + 0); - long PxHigh = CpuMem.ReadInt64(Position + Offset + 8); - - *(long*)(BuffPtr + OutOffs + 0) = PxLow; - *(long*)(BuffPtr + OutOffs + 8) = PxHigh; - - OutOffs += 16; - } - } - - return Output; - } - - private unsafe static byte[] Read8Bpt4x4(IAMemory Memory, TextureInfo Texture) - { - int Width = (Texture.Width + 3) / 4; - int Height = (Texture.Height + 3) / 4; - - byte[] Output = new byte[Width * Height * 8]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 4, 8); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long Tile = CpuMem.ReadInt64(Position + Offset); - - *(long*)(BuffPtr + OutOffs) = Tile; - - OutOffs += 8; - } - } - - return Output; - } - - private unsafe static byte[] Read16BptCompressedTexture(IAMemory Memory, TextureInfo Texture, int BlockWidth, int BlockHeight) - { - int Width = (Texture.Width + (BlockWidth - 1)) / BlockWidth; - int Height = (Texture.Height + (BlockHeight - 1)) / BlockHeight; - - byte[] Output = new byte[Width * Height * 16]; - - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, BlockWidth, 16); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Output) - { - long OutOffs = 0; - - for (int Y = 0; Y < Height; Y++) - for (int X = 0; X < Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - long Tile0 = CpuMem.ReadInt64(Position + Offset + 0); - long Tile1 = CpuMem.ReadInt64(Position + Offset + 8); - - *(long*)(BuffPtr + OutOffs + 0) = Tile0; - *(long*)(BuffPtr + OutOffs + 8) = Tile1; - - OutOffs += 16; - } - } - - return Output; - } - } -} diff --git a/Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs b/Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs deleted file mode 100644 index 076df97a..00000000 --- a/Ryujinx.HLE/Gpu/Texture/TextureSwizzle.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ryujinx.HLE.Gpu.Texture -{ - enum TextureSwizzle - { - _1dBuffer = 0, - PitchColorKey = 1, - Pitch = 2, - BlockLinear = 3, - BlockLinearColorKey = 4 - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Gpu/Texture/TextureWriter.cs b/Ryujinx.HLE/Gpu/Texture/TextureWriter.cs deleted file mode 100644 index 6c3dda6b..00000000 --- a/Ryujinx.HLE/Gpu/Texture/TextureWriter.cs +++ /dev/null @@ -1,33 +0,0 @@ -using ChocolArm64.Memory; - -namespace Ryujinx.HLE.Gpu.Texture -{ - static class TextureWriter - { - public unsafe static void Write(IAMemory Memory, TextureInfo Texture, byte[] Data) - { - ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, 1, 4); - - (AMemory CpuMem, long Position) = TextureHelper.GetMemoryAndPosition( - Memory, - Texture.Position); - - fixed (byte* BuffPtr = Data) - { - long InOffs = 0; - - for (int Y = 0; Y < Texture.Height; Y++) - for (int X = 0; X < Texture.Width; X++) - { - long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y); - - int Pixel = *(int*)(BuffPtr + InOffs); - - CpuMem.WriteInt32(Position + Offset, Pixel); - - InOffs += 4; - } - } - } - } -} diff --git a/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASCtx.cs b/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASCtx.cs index 7b6a8676..70275b2a 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASCtx.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASCtx.cs @@ -1,4 +1,4 @@ -using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.Graphics.Memory; using System.Collections.Generic; namespace Ryujinx.HLE.HOS.Services.Nv.NvGpuAS diff --git a/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs b/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs index c9be18ea..95eb5b98 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs @@ -1,5 +1,5 @@ using ChocolArm64.Memory; -using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.Graphics.Memory; using Ryujinx.HLE.HOS.Services.Nv.NvMap; using Ryujinx.HLE.Logging; using System; diff --git a/Ryujinx.HLE/HOS/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs b/Ryujinx.HLE/HOS/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs index 842447f3..f4ed4821 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/NvHostChannel/NvHostChannelIoctl.cs @@ -1,5 +1,5 @@ using ChocolArm64.Memory; -using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.Graphics.Memory; using Ryujinx.HLE.HOS.Services.Nv.NvGpuAS; using Ryujinx.HLE.Logging; using System; diff --git a/Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs b/Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs index 782f7b80..38da2889 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs @@ -1,5 +1,5 @@ using ChocolArm64.Memory; -using Ryujinx.HLE.Gpu.Memory; +using Ryujinx.Graphics.Memory; using Ryujinx.HLE.Logging; using Ryujinx.HLE.Utilities; using System.Collections.Concurrent; diff --git a/Ryujinx.HLE/HOS/Services/Vi/NvFlinger.cs b/Ryujinx.HLE/HOS/Services/Vi/NvFlinger.cs index 12c17311..8f541fbf 100644 --- a/Ryujinx.HLE/HOS/Services/Vi/NvFlinger.cs +++ b/Ryujinx.HLE/HOS/Services/Vi/NvFlinger.cs @@ -1,5 +1,5 @@ using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu.Texture; +using Ryujinx.Graphics.Texture; using Ryujinx.HLE.HOS.Kernel; using Ryujinx.HLE.HOS.Services.Nv.NvMap; using Ryujinx.HLE.Logging; @@ -303,7 +303,7 @@ namespace Ryujinx.HLE.HOS.Services.Android int Right = Crop.Right; int Bottom = Crop.Bottom; - Renderer.QueueAction(() => Renderer.FrameBuffer.SetTransform(FlipX, FlipY, Top, Left, Right, Bottom)); + Renderer.QueueAction(() => Renderer.RenderTarget.SetTransform(FlipX, FlipY, Top, Left, Right, Bottom)); //TODO: Support double buffering here aswell, it is broken for GPU //frame buffers because it seems to be completely out of sync. @@ -311,7 +311,7 @@ namespace Ryujinx.HLE.HOS.Services.Android { //Frame buffer is rendered to by the GPU, we can just //bind the frame buffer texture, it's not necessary to read anything. - Renderer.QueueAction(() => Renderer.FrameBuffer.Set(FbAddr)); + Renderer.QueueAction(() => Renderer.RenderTarget.Set(FbAddr)); } else { @@ -321,7 +321,7 @@ namespace Ryujinx.HLE.HOS.Services.Android byte[] Data = TextureReader.Read(Context.Memory, Texture); - Renderer.QueueAction(() => Renderer.FrameBuffer.Set(Data, FbWidth, FbHeight)); + Renderer.QueueAction(() => Renderer.RenderTarget.Set(Data, FbWidth, FbHeight)); } Context.Device.Gpu.Renderer.QueueAction(() => ReleaseBuffer(Slot)); diff --git a/Ryujinx.HLE/Switch.cs b/Ryujinx.HLE/Switch.cs index 03dc44e8..32d5bef6 100644 --- a/Ryujinx.HLE/Switch.cs +++ b/Ryujinx.HLE/Switch.cs @@ -1,6 +1,6 @@ using Ryujinx.Audio; +using Ryujinx.Graphics; using Ryujinx.Graphics.Gal; -using Ryujinx.HLE.Gpu; using Ryujinx.HLE.HOS; using Ryujinx.HLE.Input; using Ryujinx.HLE.Logging; diff --git a/Ryujinx/Ui/GLScreen.cs b/Ryujinx/Ui/GLScreen.cs index 2c683f5e..e7eb2613 100644 --- a/Ryujinx/Ui/GLScreen.cs +++ b/Ryujinx/Ui/GLScreen.cs @@ -73,7 +73,7 @@ namespace Ryujinx { ResizeEvent = false; - Renderer.FrameBuffer.SetWindowSize(Width, Height); + Renderer.RenderTarget.SetWindowSize(Width, Height); } Ticks += Chrono.ElapsedTicks; @@ -96,7 +96,7 @@ namespace Ryujinx Visible = true; - Renderer.FrameBuffer.SetWindowSize(Width, Height); + Renderer.RenderTarget.SetWindowSize(Width, Height); Context.MakeCurrent(null); @@ -251,7 +251,7 @@ namespace Ryujinx private new void RenderFrame() { - Renderer.FrameBuffer.Render(); + Renderer.RenderTarget.Render(); Device.Statistics.RecordSystemFrameTime(); -- cgit v1.2.3