From 1ba54b44fbfe4ca7a91b15ba199cf4eed565b18d Mon Sep 17 00:00:00 2001 From: Samuliak Date: Wed, 3 Jul 2024 07:40:32 +0200 Subject: [PATCH] support depth render targets --- include/renderer_mtl/pica_to_mtl.hpp | 30 ++++++++++++++++++++++++++ include/renderer_mtl/render_target.hpp | 22 ++++++++++++++----- include/renderer_mtl/renderer_mtl.hpp | 6 +++--- src/core/renderer_mtl/renderer_mtl.cpp | 4 ++-- 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 include/renderer_mtl/pica_to_mtl.hpp diff --git a/include/renderer_mtl/pica_to_mtl.hpp b/include/renderer_mtl/pica_to_mtl.hpp new file mode 100644 index 00000000..eefcd293 --- /dev/null +++ b/include/renderer_mtl/pica_to_mtl.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include "PICA/regs.hpp" + +// HACK: both functions return a hardcoded format for now, since render pipeline needs to know the format at creation time +namespace PICA { + +inline MTL::PixelFormat toMTLPixelFormatColor(ColorFmt format) { + return MTL::PixelFormatRGBA8Unorm; + //switch (format) { + //case ColorFmt::RGBA8: return MTL::PixelFormatRGBA8Unorm; + //case ColorFmt::RGB8: return MTL::PixelFormatRGBA8Unorm; // TODO: return the correct format + //case ColorFmt::RGBA5551: return MTL::PixelFormatBGR5A1Unorm; + //case ColorFmt::RGB565: return MTL::PixelFormatB5G6R5Unorm; // TODO: check if this is correct + //case ColorFmt::RGBA4: return MTL::PixelFormatABGR4Unorm; // TODO: check if this is correct + //} +} + +inline MTL::PixelFormat toMTLPixelFormatDepth(DepthFmt format) { + return MTL::PixelFormatDepth24Unorm_Stencil8; + //switch (format) { + //case DepthFmt::Depth16: return MTL::PixelFormatDepth16Unorm; + //case DepthFmt::Unknown1: return MTL::PixelFormatInvalid; + //case DepthFmt::Depth24: return MTL::PixelFormatDepth32Float; // TODO: is this okay? + //case DepthFmt::Depth24Stencil8: return MTL::PixelFormatDepth24Unorm_Stencil8; + //} +} + +} // namespace PICA diff --git a/include/renderer_mtl/render_target.hpp b/include/renderer_mtl/render_target.hpp index ad98e384..a35ff89e 100644 --- a/include/renderer_mtl/render_target.hpp +++ b/include/renderer_mtl/render_target.hpp @@ -2,22 +2,23 @@ #include #include #include -#include "PICA/regs.hpp" #include "boost/icl/interval.hpp" #include "helpers.hpp" #include "math_util.hpp" #include "opengl.hpp" +#include "pica_to_mtl.hpp" template using Interval = boost::icl::right_open_interval; namespace Metal { +template struct RenderTarget { MTL::Device* device; u32 location; - PICA::ColorFmt format; + Format_t format; OpenGL::uvec2 size; bool valid; @@ -28,9 +29,8 @@ struct RenderTarget { RenderTarget() : valid(false) {} - RenderTarget(MTL::Device* dev, u32 loc, PICA::ColorFmt format, u32 x, u32 y, bool valid = true) + RenderTarget(MTL::Device* dev, u32 loc, Format_t format, u32 x, u32 y, bool valid = true) : device(dev), location(loc), format(format), size({x, y}), valid(valid) { - u64 endLoc = (u64)loc + sizeInBytes(); // Check if start and end are valid here range = Interval(loc, (u32)endLoc); @@ -51,9 +51,18 @@ struct RenderTarget { } void allocate() { + MTL::PixelFormat pixelFormat = MTL::PixelFormatInvalid; + if (std::is_same::value) { + pixelFormat = PICA::toMTLPixelFormatColor((PICA::ColorFmt)format); + } else if (std::is_same::value) { + pixelFormat = PICA::toMTLPixelFormatDepth((PICA::DepthFmt)format); + } else { + panic("Invalid format type"); + } + MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::alloc()->init(); descriptor->setTextureType(MTL::TextureType2D); - descriptor->setPixelFormat(MTL::PixelFormatRGBA8Unorm); + descriptor->setPixelFormat(pixelFormat); descriptor->setWidth(size.u()); descriptor->setHeight(size.v()); descriptor->setUsage(MTL::TextureUsageRenderTarget | MTL::TextureUsageShaderRead); @@ -74,4 +83,7 @@ struct RenderTarget { } }; +typedef RenderTarget ColorRenderTarget; +typedef RenderTarget DepthStencilRenderTarget; + } // namespace Metal diff --git a/include/renderer_mtl/renderer_mtl.hpp b/include/renderer_mtl/renderer_mtl.hpp index e73d2dc9..43f71b3a 100644 --- a/include/renderer_mtl/renderer_mtl.hpp +++ b/include/renderer_mtl/renderer_mtl.hpp @@ -35,8 +35,8 @@ class RendererMTL final : public Renderer { MTL::CommandQueue* commandQueue; // Caches - SurfaceCache colorRenderTargetCache; - SurfaceCache depthStencilRenderTargetCache; + SurfaceCache colorRenderTargetCache; + SurfaceCache depthStencilRenderTargetCache; SurfaceCache textureCache; // Helpers @@ -56,7 +56,7 @@ class RendererMTL final : public Renderer { } } - std::optional getColorRenderTarget(u32 addr, PICA::ColorFmt format, u32 width, u32 height, bool createIfnotFound = true); + std::optional getColorRenderTarget(u32 addr, PICA::ColorFmt format, u32 width, u32 height, bool createIfnotFound = true); MTL::Texture* getTexture(Metal::Texture& tex); void setupTextureEnvState(MTL::RenderCommandEncoder* encoder); void bindTexturesToSlots(MTL::RenderCommandEncoder* encoder); diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index e363322c..2debf592 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -359,7 +359,7 @@ void RendererMTL::deinitGraphicsContext() { Helpers::warn("RendererMTL::deinitGraphicsContext not implemented"); } -std::optional RendererMTL::getColorRenderTarget(u32 addr, PICA::ColorFmt format, u32 width, u32 height, bool createIfnotFound) { +std::optional RendererMTL::getColorRenderTarget(u32 addr, PICA::ColorFmt format, u32 width, u32 height, bool createIfnotFound) { // Try to find an already existing buffer that contains the provided address // This is a more relaxed check compared to getColourFBO as display transfer/texcopy may refer to // subrect of a surface and in case of texcopy we don't know the format of the surface. @@ -373,7 +373,7 @@ std::optional RendererMTL::getColorRenderTarget(u32 addr, P } // Otherwise create and cache a new buffer. - Metal::RenderTarget sampleBuffer(device, addr, format, width, height); + Metal::ColorRenderTarget sampleBuffer(device, addr, format, width, height); return colorRenderTargetCache.add(sampleBuffer); }