support surface copy parameters

This commit is contained in:
Samuliak 2024-08-12 11:14:55 +02:00
parent 8496fc1574
commit 5e748cdb48
4 changed files with 25 additions and 22 deletions

View File

@ -96,6 +96,7 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
// TODO: use compute/void vertex function instead // TODO: use compute/void vertex function instead
size_t newStride = Align(stride, 4); size_t newStride = Align(stride, 4);
size_t newSize = vertexBufferRange.size / stride * newStride; size_t newSize = vertexBufferRange.size / stride * newStride;
// TODO: use one big buffer for all restrided buffers
restrideInfo.buffer = m_mtlr->GetDevice()->newBuffer(newSize, MTL::StorageModeShared); restrideInfo.buffer = m_mtlr->GetDevice()->newBuffer(newSize, MTL::StorageModeShared);
uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset; uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset;

View File

@ -60,14 +60,12 @@ MetalRenderer::MetalRenderer()
} }
// Hybrid pipelines // Hybrid pipelines
m_copyDepthToColorPipeline = new MetalHybridComputePipeline(this, m_utilityLibrary, "vertexCopyDepthToColor", "kernelCopyDepthToColor"); m_copyTextureToTexturePipeline = new MetalHybridComputePipeline(this, m_utilityLibrary, "vertexCopyTextureToTexture", "kernelCopyTextureToTexture");
m_copyColorToDepthPipeline = new MetalHybridComputePipeline(this, m_utilityLibrary, "vertexCopyColorToDepth", "kernelCopyColorToDepth");
} }
MetalRenderer::~MetalRenderer() MetalRenderer::~MetalRenderer()
{ {
delete m_copyDepthToColorPipeline; delete m_copyTextureToTexturePipeline;
delete m_copyColorToDepthPipeline;
m_presentPipeline->release(); m_presentPipeline->release();
@ -476,26 +474,28 @@ void MetalRenderer::surfaceCopy_copySurfaceWithFormatConversion(LatteTexture* so
return; return;
} }
MetalHybridComputePipeline* copyPipeline;
if (srcTextureMtl->IsDepth())
copyPipeline = m_copyDepthToColorPipeline;
else
copyPipeline = m_copyColorToDepthPipeline;
MTL::Texture* textures[] = {srcTextureMtl->GetTexture(), dstTextureMtl->GetTexture()}; MTL::Texture* textures[] = {srcTextureMtl->GetTexture(), dstTextureMtl->GetTexture()};
struct CopyParams
{
uint32 width;
uint32 srcMip;
uint32 srcSlice;
uint32 dstMip;
uint32 dstSlice;
} params{(uint32)effectiveCopyWidth, (uint32)texSrcMip, (uint32)texSrcSlice, (uint32)texDstMip, (uint32)texDstSlice};
if (m_encoderType == MetalEncoderType::Render) if (m_encoderType == MetalEncoderType::Render)
{ {
auto renderCommandEncoder = static_cast<MTL::RenderCommandEncoder*>(m_commandEncoder); auto renderCommandEncoder = static_cast<MTL::RenderCommandEncoder*>(m_commandEncoder);
renderCommandEncoder->setRenderPipelineState(copyPipeline->GetRenderPipelineState()); renderCommandEncoder->setRenderPipelineState(m_copyTextureToTexturePipeline->GetRenderPipelineState());
renderCommandEncoder->setViewport(MTL::Viewport{0.0, 0.0, (double)effectiveCopyWidth, (double)effectiveCopyHeight, 0.0, 1.0}); renderCommandEncoder->setViewport(MTL::Viewport{0.0, 0.0, (double)effectiveCopyWidth, (double)effectiveCopyHeight, 0.0, 1.0});
renderCommandEncoder->setScissorRect(MTL::ScissorRect{0, 0, (uint32)effectiveCopyWidth, (uint32)effectiveCopyHeight}); renderCommandEncoder->setScissorRect(MTL::ScissorRect{0, 0, (uint32)effectiveCopyWidth, (uint32)effectiveCopyHeight});
renderCommandEncoder->setVertexTextures(textures, NS::Range(0, 2)); renderCommandEncoder->setVertexTextures(textures, NS::Range(0, 2));
renderCommandEncoder->setVertexBytes(&effectiveCopyWidth, sizeof(uint32), 0); renderCommandEncoder->setVertexBytes(&params, sizeof(params), 0);
// TODO: set slices and mips
renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangle, NS::UInteger(0), NS::UInteger(3)); renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangle, NS::UInteger(0), NS::UInteger(3));
} }

View File

@ -261,8 +261,7 @@ private:
MTL::RenderPipelineState* m_presentPipeline; MTL::RenderPipelineState* m_presentPipeline;
// Hybrid pipelines // Hybrid pipelines
class MetalHybridComputePipeline* m_copyDepthToColorPipeline; class MetalHybridComputePipeline* m_copyTextureToTexturePipeline;
class MetalHybridComputePipeline* m_copyColorToDepthPipeline;
// Basic // Basic
MTL::SamplerState* m_nearestSampler; MTL::SamplerState* m_nearestSampler;

View File

@ -22,13 +22,16 @@ inline const char* utilityShaderSource = \
" return tex.sample(samplr, in.texCoord);\n" \ " return tex.sample(samplr, in.texCoord);\n" \
"}\n" \ "}\n" \
"\n" \ "\n" \
"vertex void vertexCopyDepthToColor(uint vid [[vertex_id]], depth2d<float, access::read> src [[texture(0)]], texture2d<float, access::write> dst [[texture(1)]], constant uint& copyWidth) {\n" \ "struct CopyParams {\n" \
" uint2 coord = uint2(vid % copyWidth, vid / copyWidth);\n" \ " uint width;\n" \
" return dst.write(float4(src.read(coord), 0.0, 0.0, 0.0), coord);\n" \ " uint srcMip;\n" \
"}\n" \ " uint srcSlice;\n" \
" uint dstMip;\n" \
" uint dstSlice;\n" \
"};\n" \
"\n" \ "\n" \
"vertex void vertexCopyColorToDepth(uint vid [[vertex_id]], texture2d<float, access::read> src [[texture(0)]], texture2d<float, access::write> dst [[texture(1)]], constant uint& copyWidth) {\n" \ "vertex void vertexCopyTextureToTexture(uint vid [[vertex_id]], texture2d_array<float, access::read> src [[texture(0)]], texture2d_array<float, access::write> dst [[texture(1)]], constant CopyParams& params) {\n" \
" uint2 coord = uint2(vid % copyWidth, vid / copyWidth);\n" \ " uint2 coord = uint2(vid % params.width, vid / params.width);\n" \
" return dst.write(float4(src.read(coord).r), coord);\n" \ " return dst.write(float4(src.read(coord, params.srcSlice, params.srcMip).r, 0.0, 0.0, 0.0), coord, params.dstSlice, params.dstMip);\n" \
"}\n" \ "}\n" \
"\n"; "\n";