use depth stencil render target

This commit is contained in:
Samuliak 2024-07-03 10:21:49 +02:00
parent 9241306d4d
commit d977f7ef85
4 changed files with 224 additions and 94 deletions

View file

@ -0,0 +1,55 @@
#pragma once
#include "pica_to_mtl.hpp"
using namespace PICA;
namespace Metal {
struct DepthStencilHash {
bool depthWrite;
u8 depthFunc;
};
class DepthStencilCache {
public:
DepthStencilCache() = default;
~DepthStencilCache() {
clear();
}
void set(MTL::Device* dev) {
device = dev;
}
MTL::DepthStencilState* get(DepthStencilHash hash) {
u8 intHash = hash.depthWrite | (hash.depthFunc << 1);
auto& depthStencilState = depthStencilCache[intHash];
if (!depthStencilState) {
MTL::DepthStencilDescriptor* desc = MTL::DepthStencilDescriptor::alloc()->init();
desc->setDepthWriteEnabled(hash.depthWrite);
desc->setDepthCompareFunction(toMTLCompareFunc(hash.depthFunc));
depthStencilState = device->newDepthStencilState(desc);
desc->release();
}
return depthStencilState;
}
void clear() {
for (auto& pair : depthStencilCache) {
pair.second->release();
}
depthStencilCache.clear();
}
private:
std::unordered_map<u8, MTL::DepthStencilState*> depthStencilCache;
MTL::Device* device;
};
} // namespace Metal

View file

@ -20,8 +20,25 @@ inline MTL::PixelFormat toMTLPixelFormatDepth(DepthFmt 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;
// Apple sillicon doesn't support 24-bit depth buffers, so we use 32-bit instead
case DepthFmt::Depth24Stencil8: return MTL::PixelFormatDepth32Float_Stencil8;
}
}
inline MTL::CompareFunction toMTLCompareFunc(u8 func) {
switch (func) {
case 0: return MTL::CompareFunctionNever;
case 1: return MTL::CompareFunctionAlways;
case 2: return MTL::CompareFunctionEqual;
case 3: return MTL::CompareFunctionNotEqual;
case 4: return MTL::CompareFunctionLess;
case 5: return MTL::CompareFunctionLessEqual;
case 6: return MTL::CompareFunctionGreater;
case 7: return MTL::CompareFunctionGreaterEqual;
default: panic("Unknown compare function %u", func);
}
return MTL::CompareFunctionAlways;
}
} // namespace PICA

View file

@ -5,6 +5,7 @@
#include "texture.hpp"
#include "render_target.hpp"
#include "mtl_pipeline_cache.hpp"
#include "mtl_depth_stencil_cache.hpp"
// HACK: use the OpenGL cache
#include "../renderer_gl/surface_cache.hpp"
@ -41,6 +42,7 @@ class RendererMTL final : public Renderer {
SurfaceCache<Metal::Texture, 256, true> textureCache;
Metal::PipelineCache blitPipelineCache;
Metal::PipelineCache drawPipelineCache;
Metal::DepthStencilCache depthStencilCache;
// Helpers
MTL::SamplerState* basicSampler;
@ -58,6 +60,7 @@ class RendererMTL final : public Renderer {
}
std::optional<Metal::ColorRenderTarget> getColorRenderTarget(u32 addr, PICA::ColorFmt format, u32 width, u32 height, bool createIfnotFound = true);
Metal::DepthStencilRenderTarget& getDepthRenderTarget();
MTL::Texture* getTexture(Metal::Texture& tex);
void setupTextureEnvState(MTL::RenderCommandEncoder* encoder);
void bindTexturesToSlots(MTL::RenderCommandEncoder* encoder);