Skip to content

Commit 5e748cd

Browse files
committed
support surface copy parameters
1 parent 8496fc1 commit 5e748cd

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu
9696
// TODO: use compute/void vertex function instead
9797
size_t newStride = Align(stride, 4);
9898
size_t newSize = vertexBufferRange.size / stride * newStride;
99+
// TODO: use one big buffer for all restrided buffers
99100
restrideInfo.buffer = m_mtlr->GetDevice()->newBuffer(newSize, MTL::StorageModeShared);
100101

101102
uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset;

src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ MetalRenderer::MetalRenderer()
6060
}
6161

6262
// Hybrid pipelines
63-
m_copyDepthToColorPipeline = new MetalHybridComputePipeline(this, m_utilityLibrary, "vertexCopyDepthToColor", "kernelCopyDepthToColor");
64-
m_copyColorToDepthPipeline = new MetalHybridComputePipeline(this, m_utilityLibrary, "vertexCopyColorToDepth", "kernelCopyColorToDepth");
63+
m_copyTextureToTexturePipeline = new MetalHybridComputePipeline(this, m_utilityLibrary, "vertexCopyTextureToTexture", "kernelCopyTextureToTexture");
6564
}
6665

6766
MetalRenderer::~MetalRenderer()
6867
{
69-
delete m_copyDepthToColorPipeline;
70-
delete m_copyColorToDepthPipeline;
68+
delete m_copyTextureToTexturePipeline;
7169

7270
m_presentPipeline->release();
7371

@@ -476,26 +474,28 @@ void MetalRenderer::surfaceCopy_copySurfaceWithFormatConversion(LatteTexture* so
476474
return;
477475
}
478476

479-
MetalHybridComputePipeline* copyPipeline;
480-
if (srcTextureMtl->IsDepth())
481-
copyPipeline = m_copyDepthToColorPipeline;
482-
else
483-
copyPipeline = m_copyColorToDepthPipeline;
484-
485477
MTL::Texture* textures[] = {srcTextureMtl->GetTexture(), dstTextureMtl->GetTexture()};
486478

479+
struct CopyParams
480+
{
481+
uint32 width;
482+
uint32 srcMip;
483+
uint32 srcSlice;
484+
uint32 dstMip;
485+
uint32 dstSlice;
486+
} params{(uint32)effectiveCopyWidth, (uint32)texSrcMip, (uint32)texSrcSlice, (uint32)texDstMip, (uint32)texDstSlice};
487+
487488
if (m_encoderType == MetalEncoderType::Render)
488489
{
489490
auto renderCommandEncoder = static_cast<MTL::RenderCommandEncoder*>(m_commandEncoder);
490491

491-
renderCommandEncoder->setRenderPipelineState(copyPipeline->GetRenderPipelineState());
492+
renderCommandEncoder->setRenderPipelineState(m_copyTextureToTexturePipeline->GetRenderPipelineState());
492493

493494
renderCommandEncoder->setViewport(MTL::Viewport{0.0, 0.0, (double)effectiveCopyWidth, (double)effectiveCopyHeight, 0.0, 1.0});
494495
renderCommandEncoder->setScissorRect(MTL::ScissorRect{0, 0, (uint32)effectiveCopyWidth, (uint32)effectiveCopyHeight});
495496

496497
renderCommandEncoder->setVertexTextures(textures, NS::Range(0, 2));
497-
renderCommandEncoder->setVertexBytes(&effectiveCopyWidth, sizeof(uint32), 0);
498-
// TODO: set slices and mips
498+
renderCommandEncoder->setVertexBytes(&params, sizeof(params), 0);
499499

500500
renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangle, NS::UInteger(0), NS::UInteger(3));
501501
}

src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ class MetalRenderer : public Renderer
261261
MTL::RenderPipelineState* m_presentPipeline;
262262

263263
// Hybrid pipelines
264-
class MetalHybridComputePipeline* m_copyDepthToColorPipeline;
265-
class MetalHybridComputePipeline* m_copyColorToDepthPipeline;
264+
class MetalHybridComputePipeline* m_copyTextureToTexturePipeline;
266265

267266
// Basic
268267
MTL::SamplerState* m_nearestSampler;

src/Cafe/HW/Latte/Renderer/Metal/UtilityShaderSource.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ inline const char* utilityShaderSource = \
2222
" return tex.sample(samplr, in.texCoord);\n" \
2323
"}\n" \
2424
"\n" \
25-
"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" \
26-
" uint2 coord = uint2(vid % copyWidth, vid / copyWidth);\n" \
27-
" return dst.write(float4(src.read(coord), 0.0, 0.0, 0.0), coord);\n" \
28-
"}\n" \
25+
"struct CopyParams {\n" \
26+
" uint width;\n" \
27+
" uint srcMip;\n" \
28+
" uint srcSlice;\n" \
29+
" uint dstMip;\n" \
30+
" uint dstSlice;\n" \
31+
"};\n" \
2932
"\n" \
30-
"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" \
31-
" uint2 coord = uint2(vid % copyWidth, vid / copyWidth);\n" \
32-
" return dst.write(float4(src.read(coord).r), coord);\n" \
33+
"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" \
34+
" uint2 coord = uint2(vid % params.width, vid / params.width);\n" \
35+
" return dst.write(float4(src.read(coord, params.srcSlice, params.srcMip).r, 0.0, 0.0, 0.0), coord, params.dstSlice, params.dstMip);\n" \
3336
"}\n" \
3437
"\n";

0 commit comments

Comments
 (0)