From 42fc2bb490694e019cd9c63065674ac810feef25 Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sat, 6 Jul 2024 09:58:20 +0200 Subject: [PATCH] do the copy on the GPU --- include/renderer_mtl/renderer_mtl.hpp | 3 +- src/core/renderer_mtl/renderer_mtl.cpp | 52 +++++++++++++------ .../metal_copy_to_lut_texture.metal | 2 +- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/include/renderer_mtl/renderer_mtl.hpp b/include/renderer_mtl/renderer_mtl.hpp index e21582bb..3ec77ace 100644 --- a/include/renderer_mtl/renderer_mtl.hpp +++ b/include/renderer_mtl/renderer_mtl.hpp @@ -51,6 +51,7 @@ class RendererMTL final : public Renderer { MTL::SamplerState* nearestSampler; MTL::SamplerState* linearSampler; MTL::Texture* lightLUTTextureArray; + MTL::DepthStencilState* defaultDepthStencilState; // Pipelines MTL::RenderPipelineState* displayPipeline; @@ -104,5 +105,5 @@ class RendererMTL final : public Renderer { Metal::Texture& getTexture(Metal::Texture& tex); void setupTextureEnvState(MTL::RenderCommandEncoder* encoder); void bindTexturesToSlots(MTL::RenderCommandEncoder* encoder); - void updateLightingLUT(); + void updateLightingLUT(MTL::RenderCommandEncoder* encoder); }; diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index 4c1a7ef6..6b9e5fcb 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -11,7 +11,7 @@ using namespace PICA; CMRC_DECLARE(RendererMTL); -#define LIGHT_LUT_TEXTURE_WIDTH 256 +const u16 LIGHT_LUT_TEXTURE_WIDTH = 256; // HACK: redefinition... PICA::ColorFmt ToColorFormat(u32 format) { @@ -117,8 +117,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { textureDescriptor->setPixelFormat(MTL::PixelFormatR16Uint); textureDescriptor->setWidth(LIGHT_LUT_TEXTURE_WIDTH); textureDescriptor->setArrayLength(Lights::LUT_Count); - textureDescriptor->setUsage(MTL::TextureUsageShaderRead); - textureDescriptor->setStorageMode(MTL::StorageModeShared); + textureDescriptor->setUsage(MTL::TextureUsageShaderRead | MTL::TextureUsageShaderWrite); + textureDescriptor->setStorageMode(MTL::StorageModePrivate); lightLUTTextureArray = device->newTexture(textureDescriptor); textureDescriptor->release(); @@ -224,12 +224,20 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { drawPipelineCache.set(device, library, vertexDrawFunction, vertexDescriptor); // Copy to LUT texture - MTL::Function* vertexCopyToLutTextureFunction = copyToLutTextureLibrary->newFunction(NS::String::string("vertexCopyToLutTexture", NS::ASCIIStringEncoding)); + MTL::FunctionConstantValues* constants = MTL::FunctionConstantValues::alloc()->init(); + constants->setConstantValue(&LIGHT_LUT_TEXTURE_WIDTH, MTL::DataTypeUShort, NS::UInteger(0)); + + error = nullptr; + MTL::Function* vertexCopyToLutTextureFunction = copyToLutTextureLibrary->newFunction(NS::String::string("vertexCopyToLutTexture", NS::ASCIIStringEncoding), constants, &error); + if (error) { + Helpers::panic("Error creating copy_to_lut_texture vertex function: %s", error->description()->cString(NS::ASCIIStringEncoding)); + } + constants->release(); MTL::RenderPipelineDescriptor* copyToLutTexturePipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init(); - copyToLutTexturePipelineDescriptor->setVertexFunction(vertexDisplayFunction); - auto* copyToLutTextureColorAttachment = copyToLutTexturePipelineDescriptor->colorAttachments()->object(0); - copyToLutTextureColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm); + copyToLutTexturePipelineDescriptor->setVertexFunction(vertexCopyToLutTextureFunction); + // Disable rasterization + copyToLutTexturePipelineDescriptor->setRasterizationEnabled(false); error = nullptr; copyToLutTexturePipeline = device->newRenderPipelineState(copyToLutTexturePipelineDescriptor, &error); @@ -242,6 +250,10 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { // Vertex buffer cache vertexBufferCache.set(device); + + // -------- Depth stencil state -------- + MTL::DepthStencilDescriptor* depthStencilDescriptor = MTL::DepthStencilDescriptor::alloc()->init(); + defaultDepthStencilState = device->newDepthStencilState(depthStencilDescriptor); } void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { @@ -450,6 +462,12 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::spantexture, (depthStencilRenderTarget ? depthStencilRenderTarget->texture : nullptr)); + + // Update the LUT texture if necessary + if (gpu.lightingLUTDirty) { + updateLightingLUT(renderCommandEncoder); + } + renderCommandEncoder->setRenderPipelineState(pipeline); renderCommandEncoder->setDepthStencilState(depthStencilState); // If size is < 4KB, use inline vertex data, otherwise use a buffer @@ -460,11 +478,6 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::spansetVertexBuffer(buffer.buffer, buffer.offset, VERTEX_BUFFER_BINDING_INDEX); } - // Update the LUT texture if necessary - if (gpu.lightingLUTDirty) { - updateLightingLUT(); - } - // Bind resources setupTextureEnvState(renderCommandEncoder); bindTexturesToSlots(renderCommandEncoder); @@ -600,7 +613,7 @@ void RendererMTL::bindTexturesToSlots(MTL::RenderCommandEncoder* encoder) { encoder->setFragmentSamplerState(linearSampler, 3); } -void RendererMTL::updateLightingLUT() { +void RendererMTL::updateLightingLUT(MTL::RenderCommandEncoder* encoder) { gpu.lightingLUTDirty = false; std::array u16_lightinglut; @@ -609,7 +622,14 @@ void RendererMTL::updateLightingLUT() { u16_lightinglut[i] = value * 65535 / 4095; } - for (int i = 0; i < Lights::LUT_Count; i++) { - lightLUTTextureArray->replaceRegion(MTL::Region(0, 0, LIGHT_LUT_TEXTURE_WIDTH, 1), 0, i, u16_lightinglut.data() + LIGHT_LUT_TEXTURE_WIDTH * i, 0, 0); - } + //for (int i = 0; i < Lights::LUT_Count; i++) { + // lightLUTTextureArray->replaceRegion(MTL::Region(0, 0, LIGHT_LUT_TEXTURE_WIDTH, 1), 0, i, u16_lightinglut.data() + LIGHT_LUT_TEXTURE_WIDTH * i, 0, 0); + //} + + renderCommandEncoder->setRenderPipelineState(copyToLutTexturePipeline); + renderCommandEncoder->setDepthStencilState(defaultDepthStencilState); + renderCommandEncoder->setVertexTexture(lightLUTTextureArray, 0); + renderCommandEncoder->setVertexBytes(u16_lightinglut.data(), sizeof(u16_lightinglut), 0); + + renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangle, NS::UInteger(0), GPU::LightingLutSize); } diff --git a/src/host_shaders/metal_copy_to_lut_texture.metal b/src/host_shaders/metal_copy_to_lut_texture.metal index b069f6c5..1c996516 100644 --- a/src/host_shaders/metal_copy_to_lut_texture.metal +++ b/src/host_shaders/metal_copy_to_lut_texture.metal @@ -3,6 +3,6 @@ using namespace metal; constant ushort lutTextureWidth [[function_constant(0)]]; -vertex void vertexCopyToLutTexture(uint vid [[vertex_id]], constant ushort* data [[buffer(0)]], texture1d_array out [[texture(0)]]) { +vertex void vertexCopyToLutTexture(uint vid [[vertex_id]], texture1d_array out [[texture(0)]], constant ushort* data [[buffer(0)]]) { out.write(data[vid], vid % lutTextureWidth, vid / lutTextureWidth); }