Skip to content

Commit 89637d6

Browse files
committed
feat: add more methods to implMTL
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent fdac99d commit 89637d6

File tree

6 files changed

+179
-67
lines changed

6 files changed

+179
-67
lines changed

Source/Metal/CommandBufferMTL.mm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@
323323
}
324324

325325
void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) {
326+
MTLRenderPassDescriptor* renderPassDesc = [MTLRenderPassDescriptor alloc];
327+
id<MTLRenderCommandEncoder> rendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDesc];
328+
329+
330+
[rendererEncoder setCullMode: MTLCullModeNone];
331+
[rendererEncoder setTriangleFillMode: MTLTriangleFillModeFill];
332+
[rendererEncoder setDepthBias: 0 slopeScale: 0 clamp: 0];
333+
326334
}
327335

328336
void CommandBufferMTL::EndCurrentEncoders() {
@@ -525,7 +533,7 @@
525533
(srcRegionDesc->depth == WHOLE_SIZE) ? src.GetSize(2, srcRegionDesc->mipOffset) : srcRegionDesc->depth
526534
);
527535

528-
// Copy to the texture's final subresource.
536+
// Copy to the texture's final subresourc e.
529537
[m_BlitEncoder copyFromTexture: src.GetHandle()
530538
sourceSlice: srcRegionDesc->layerOffset
531539
sourceLevel: srcRegionDesc->mipOffset

Source/Metal/CommandQueueMTL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct CommandQueueMTL {
3434
void SetDebugName(const char* name);
3535
void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain);
3636
Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum);
37-
Result WaitForIdle();
37+
Result WaitForIdle();
3838

3939
Result Create(CommandQueueType type);
4040

Source/Metal/DescriptorPoolMTL.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
}
4141
void DescriptorPoolMTL::Reset() {
42+
m_ArgumentOffset = 0;
43+
m_AllocatedSets.clear();
4244

4345
}
4446

Source/Metal/DeviceMTL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct DeviceMTL final : public DeviceBase {
5353
}
5454

5555

56-
id<MTLRenderPipelineState> GetClearPipeline();
56+
id<MTLRenderPipelineState> GetClearPipeline(ClearDesc* desc, size_t numFormat);
5757

5858
void Destruct() override;
5959
Result FillFunctionTable(CoreInterface& table) const override;

Source/Metal/DeviceMTL.mm

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,9 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property
4848

4949
DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator)
5050
: DeviceBase(callbacks, stdAllocator) {
51-
52-
5351
m_Desc.graphicsAPI = GraphicsAPI::MTL;
5452
m_Desc.nriVersionMajor = NRI_VERSION_MAJOR;
5553
m_Desc.nriVersionMinor = NRI_VERSION_MINOR;
56-
57-
// NSString* clearVert = [NSString stringWithCString: " \
58-
// #include <metal_stdlib> \
59-
// using namespace metal; \
60-
// typedef struct { \
61-
// float4 a_position [[attribute(0)]]; \
62-
// } AttributesPos; \
63-
// typedef struct { \
64-
// float4 colors[9]; \
65-
// } ClearColorsIn; \
66-
// typedef struct { \
67-
// float4 v_position [[position]]; \
68-
// uint layer [[render_target_array_index]]; \
69-
// } VaryingsPos; \
70-
// vertex VaryingsPos vertClear(AttributesPos attributes [[stage_in]], constant ClearColorsIn& ccIn [[buffer(0)]]) { \
71-
// VaryingsPos varyings; \
72-
// varyings.v_position = float4(attributes.a_position.x, -attributes.a_position.y, ccIn.colors[4].r, 1.0); \
73-
// varyings.layer = uint(attributes.a_position.w); \
74-
// return varyings; \
75-
// } \
76-
// " encoding: NSASCIIStringEncoding];
77-
7854
}
7955

8056

@@ -100,8 +76,106 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property
10076
//}
10177

10278

103-
id<MTLRenderPipelineState> DeviceMTL::GetClearPipeline() {
79+
static id<MTLFunction> initShaderFromSource(id<MTLDevice> dev, NSString* src, NSString* method) {
80+
id<MTLFunction> mtlFunc = nil;
81+
NSError* err = nil;
82+
83+
MTLCompileOptions* options = [MTLCompileOptions alloc];
84+
id<MTLLibrary> mtlLib = [dev newLibraryWithSource: src
85+
options: options
86+
error: &err]; // temp retain
87+
NSCAssert(err, @"Failed to load Metal shader library %@", err );
88+
89+
return [mtlLib newFunctionWithName: method];
90+
}
91+
92+
93+
id<MTLRenderPipelineState> DeviceMTL::GetClearPipeline(ClearDesc* desc, size_t numFormats) {
94+
95+
NSString* clearVert = [NSString stringWithCString: "\
96+
#include <metal_stdlib> \n\
97+
using namespace metal; \n\
98+
typedef struct { \n\
99+
float4 a_position [[attribute(0)]]; \n\
100+
} AttributesPos; \n\
101+
typedef struct { \n\
102+
float4 colors[16]; \n\
103+
} ClearColorsIn; \n\
104+
typedef struct { \n\
105+
float4 v_position [[position]]; \n\
106+
uint layer [[render_target_array_index]]; \n\
107+
} VaryingsPos; \n\
108+
vertex VaryingsPos vertClear(AttributesPos attributes [[stage_in]], constant ClearColorsIn& ccIn [[buffer(0)]]) { \n\
109+
VaryingsPos varyings; \n \
110+
varyings.v_position = float4(attributes.a_position.x, -attributes.a_position.y, ccIn.colors[4].r, 1.0); \n\
111+
varyings.layer = uint(attributes.a_position.w); \n\
112+
return varyings; \n\
113+
}" encoding: NSASCIIStringEncoding];
114+
115+
NSMutableString* clearFrag = [NSMutableString alloc];
116+
[clearFrag appendString: @"\
117+
#include <metal_stdlib> \n\
118+
using namespace metal; \n\
119+
typedef struct { \n\
120+
float4 v_position [[position]]; \n\
121+
} VaryingsPos; \n\
122+
typedef struct { \n\
123+
float4 colors[16]; \n\
124+
} ClearColorsIn; \n\
125+
typedef struct { \n\
126+
"];
127+
for(uint32_t i = 0; i < numFormats; i++) {
128+
[clearFrag appendFormat: @"float4 color%u [[color(%u)]];", i, desc[i].colorAttachmentIndex];
129+
}
130+
[clearFrag appendString: @"\
131+
} ClearColorsOut; \n\
132+
fragment ClearColorsOut fragClear(VaryingsPos varyings [[stage_in]], constant ClearColorsIn& ccIn [[buffer(0)]]) { \n\
133+
ClearColorsOut ccOut; \n"];
134+
for(uint32_t i = 0; i < numFormats; i++) {
135+
[clearFrag appendFormat: @"ccOut.color%u = float4(ccIn.colors[%u]);", i, i];
136+
}
137+
[clearFrag appendString: @"\
138+
return ccOut;\n\
139+
}"];
140+
141+
id<MTLFunction> vtxFunc = initShaderFromSource(m_Device, clearVert, @"vertClear");
142+
id<MTLFunction> fragFunc = initShaderFromSource(m_Device, clearFrag, @"fragClear");
143+
MTLRenderPipelineDescriptor* renderPipelineDesc = [MTLRenderPipelineDescriptor new]; // temp retain
144+
// owner->setMetalObjectLabel(plDesc, @"ClearRenderAttachments");
145+
renderPipelineDesc.vertexFunction = vtxFunc;
146+
renderPipelineDesc.fragmentFunction = fragFunc;
147+
// plDesc.sampleCount = 1;
148+
renderPipelineDesc.inputPrimitiveTopology = MTLPrimitiveTopologyClassTriangle;
149+
150+
MTLVertexDescriptor* vtxDesc = renderPipelineDesc.vertexDescriptor;
151+
152+
MTLVertexAttributeDescriptorArray* vaDescArray = vtxDesc.attributes;
153+
MTLVertexAttributeDescriptor* vaDesc;
154+
NSUInteger vtxBuffIdx = 0;
155+
NSUInteger vtxStride = 0;
156+
157+
// Vertex location
158+
vaDesc = vaDescArray[0];
159+
vaDesc.format = MTLVertexFormatFloat4;
160+
vaDesc.bufferIndex = vtxBuffIdx;
161+
vaDesc.offset = vtxStride;
162+
vtxStride += sizeof(simd::float4);
163+
164+
// Vertex attribute buffer.
165+
MTLVertexBufferLayoutDescriptorArray* vbDescArray = vtxDesc.layouts;
166+
MTLVertexBufferLayoutDescriptor* vbDesc = vbDescArray[vtxBuffIdx];
167+
vbDesc.stepFunction = MTLVertexStepFunctionPerVertex;
168+
vbDesc.stepRate = 1;
169+
vbDesc.stride = vtxStride;
170+
171+
NSError* error = nil;
172+
id <MTLRenderPipelineState> rps = [m_Device newRenderPipelineStateWithDescriptor:renderPipelineDesc error: &error];
173+
174+
[vtxFunc release]; // temp release
175+
[fragFunc release]; // temp release
176+
[renderPipelineDesc release]; // temp release
104177

178+
return rps;
105179
}
106180

107181

Source/Metal/ImplMTL.mm

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "HelperDeviceMemoryAllocator.h"
1818
#include "CommandQueueMTL.h"
1919
#include "SwapChainMTL.h"
20+
#include "FenceMTL.h"
21+
#include "DescriptorPoolMTL.h"
2022

2123

2224
Result CreateDeviceMTL(const DeviceCreationDesc& desc, DeviceBase*& device) {
@@ -70,9 +72,13 @@ static Result NRI_CALL CreateCommandBuffer(CommandAllocator& commandAllocator, C
7072
return ((CommandAllocatorMTL&)commandAllocator).CreateCommandBuffer(commandBuffer);
7173
}
7274

75+
76+
static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) {
77+
return ((DeviceMTL&)device).CreateImplementation<DescriptorPoolMTL>(descriptorPool, descriptorPoolDesc);
78+
}
79+
7380
static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) {
74-
// ((CommandAllocatorVK&)commandAllocator).Reset();
75-
81+
((CommandAllocatorMTL&)commandAllocator).Reset();
7682
}
7783

7884
static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) {
@@ -219,6 +225,11 @@ static void NRI_CALL CmdResolveTexture(CommandBuffer& commandBuffer, Texture& ds
219225
//((CommandBufferMTL&)commandBuffer).ResolveTexture(dstTexture, dstRegionDesc, srcTexture, srcRegionDesc);
220226
}
221227

228+
229+
static void NRI_CALL Wait(Fence& fence, uint64_t value) {
230+
((FenceMTL&)fence).Wait(value);
231+
}
232+
222233
static void NRI_CALL CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) {
223234
((CommandBufferMTL&)commandBuffer).CopyBuffer(dstBuffer, dstOffset, srcBuffer, srcOffset, size);
224235
}
@@ -265,31 +276,46 @@ static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureD
265276
return ((DeviceMTL&)device).CreateImplementation<TextureMTL>(texture, textureDesc);
266277
}
267278

279+
static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) {
280+
DeviceMTL& device = ((CommandQueueMTL&)commandQueue).GetDevice();
281+
return device.CreateImplementation<CommandAllocatorMTL>(commandAllocator, commandQueue);
282+
}
268283

269-
//static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) {
270-
// DeviceMTL& device = ((const BufferMTL*)bufferViewDesc.buffer)->GetDevice();
271-
// return device.CreateImplementation<DescriptorMTL>(bufferView, bufferViewDesc);
272-
//}
273-
//
274-
//static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) {
275-
// DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
276-
// return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
277-
//}
278-
//
279-
//static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) {
280-
// DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
281-
// return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
282-
//}
283-
//
284-
//static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) {
285-
// DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
286-
// return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
287-
//}
284+
static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) {
285+
DeviceMTL& device = ((const BufferMTL*)bufferViewDesc.buffer)->GetDevice();
286+
return device.CreateImplementation<DescriptorMTL>(bufferView, bufferViewDesc);
287+
}
288288

289+
static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) {
290+
return ((DeviceMTL&)device).CreateImplementation<BufferMTL>(buffer, bufferDesc);
291+
}
289292

290-
//static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& workSubmissionDesc) {
291-
// ((CommandQueueMTL&)commandQueue).Submit(workSubmissionDesc, nullptr);
292-
//}
293+
static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) {
294+
DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
295+
return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
296+
}
297+
298+
static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) {
299+
DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
300+
return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
301+
}
302+
303+
static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) {
304+
DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
305+
return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
306+
}
307+
308+
static Result NRI_CALL CreateSampler(Device& device, const SamplerDesc& samplerDesc, Descriptor*& sampler) {
309+
return ((DeviceMTL&)device).CreateImplementation<DescriptorMTL>(sampler, samplerDesc);
310+
}
311+
312+
static Result NRI_CALL CreatePipelineLayout(Device& device, const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) {
313+
return ((DeviceMTL&)device).CreateImplementation<PipelineLayoutMTL>(pipelineLayout, pipelineLayoutDesc);
314+
}
315+
316+
static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& workSubmissionDesc) {
317+
((CommandQueueMTL&)commandQueue).Submit(workSubmissionDesc, nullptr);
318+
}
293319

294320

295321
static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) {
@@ -389,8 +415,11 @@ static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipeli
389415

390416

391417
static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum) {
392-
return Result::SUCCESS;
393-
//return ((DescriptorPoolMTL&)descriptorPool).AllocateDescriptorSets(pipelineLayout, setIndex, descriptorSets, instanceNum, variableDescriptorNum);
418+
return ((DescriptorPoolMTL&)descriptorPool).AllocateDescriptorSets(pipelineLayout, setIndex, descriptorSets, instanceNum, variableDescriptorNum);
419+
}
420+
421+
static void NRI_CALL ResetDescriptorPool(DescriptorPool& descriptorPool) {
422+
((DescriptorPoolMTL&)descriptorPool).Reset();
394423
}
395424

396425

@@ -408,20 +437,19 @@ static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char*
408437
table.GetBufferMemoryDesc = ::GetBufferMemoryDesc;
409438
table.GetTextureMemoryDesc = ::GetTextureMemoryDesc;
410439
table.GetCommandQueue = ::GetCommandQueue;
411-
//table.CreateCommandAllocator = ::CreateCommandAllocator;
440+
table.CreateCommandAllocator = ::CreateCommandAllocator;
412441
table.CreateCommandBuffer = ::CreateCommandBuffer;
413-
//table.CreateDescriptorPool = ::CreateDescriptorPool;
414-
//table.CreateBuffer = ::CreateBuffer;
442+
table.CreateDescriptorPool = ::CreateDescriptorPool;
443+
table.CreateBuffer = ::CreateBuffer;
415444
table.CreateTexture = ::CreateTexture;
416-
//table.CreateBufferView = ::CreateBufferView;
417-
//table.CreateTexture1DView = ::CreateTexture1DView;
418-
//table.CreateTexture2DView = ::CreateTexture2DView;
419-
//table.CreateTexture3DView = ::CreateTexture3DView;
420-
//table.CreateSampler = ::CreateSampler;
421-
//table.CreatePipelineLayout = ::CreatePipelineLayout;
445+
table.CreateBufferView = ::CreateBufferView;
446+
table.CreateTexture1DView = ::CreateTexture1DView;
447+
table.CreateTexture2DView = ::CreateTexture2DView;
448+
table.CreateTexture3DView = ::CreateTexture3DView;
449+
table.CreateSampler = ::CreateSampler;
450+
table.CreatePipelineLayout = ::CreatePipelineLayout;
422451
table.CreateGraphicsPipeline = ::CreateGraphicsPipeline;
423452
table.CreateComputePipeline = ::CreateComputePipeline;
424-
425453
// table.CreateQueryPool = ::CreateQueryPool;
426454
// table.CreateFence = ::CreateFence;
427455
table.DestroyCommandAllocator = ::DestroyCommandAllocator;
@@ -479,14 +507,14 @@ static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char*
479507
table.CmdBeginAnnotation = ::CmdBeginAnnotation;
480508
table.CmdEndAnnotation = ::CmdEndAnnotation;
481509
table.EndCommandBuffer = ::EndCommandBuffer;
482-
// table.QueueSubmit = ::QueueSubmit;
483-
// table.Wait = ::Wait;
510+
table.QueueSubmit = ::QueueSubmit;
511+
table.Wait = ::Wait;
484512
// table.GetFenceValue = ::GetFenceValue;
485513
// table.UpdateDescriptorRanges = ::UpdateDescriptorRanges;
486514
// table.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers;
487515
// table.CopyDescriptorSet = ::CopyDescriptorSet;
488516
table.AllocateDescriptorSets = ::AllocateDescriptorSets;
489-
// table.ResetDescriptorPool = ::ResetDescriptorPool;
517+
table.ResetDescriptorPool = ::ResetDescriptorPool;
490518
table.ResetCommandAllocator = ::ResetCommandAllocator;
491519
table.MapBuffer = ::MapBuffer;
492520
table.UnmapBuffer = ::UnmapBuffer;

0 commit comments

Comments
 (0)