Skip to content

Commit 06662d0

Browse files
committed
feat: add interface
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent 36def81 commit 06662d0

File tree

13 files changed

+146
-78
lines changed

13 files changed

+146
-78
lines changed

Source/Metal/ConversionMTL.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ constexpr MTLColorWriteMask GetColorComponent(ColorWriteBits colorWriteMask) {
5555
return MTLColorWriteMask(colorWriteMask) & MTLColorWriteMaskAll;
5656
}
5757

58+
constexpr std::array<MTLDataType, (size_t)DescriptorType::MAX_NUM> DESCRIPTOR_TYPES = {
59+
MTLDataTypeSampler, // SAMPLER
60+
MTLDataTypeNone, // CONSTANT_BUFFER
61+
MTLDataTypeTexture, // TEXTURE
62+
MTLDataTypeNone, // STORAGE_TEXTURE
63+
MTLDataTypeStruct, // BUFFER
64+
MTLDataTypeStruct, // STORAGE_BUFFER
65+
MTLDataTypeArray, // STRUCTURED_BUFFER
66+
MTLDataTypeStruct, // STORAGE_STRUCTURED_BUFFER
67+
MTLDataTypePrimitiveAccelerationStructure // ACCELERATION_STRUCTURE
68+
};
69+
70+
constexpr MTLDataType GetDescriptorType(DescriptorType type) {
71+
return DESCRIPTOR_TYPES[(size_t)type];
72+
}
73+
5874
constexpr std::array<MTLCompareFunction, (size_t)CompareFunc::MAX_NUM> COMPARE_OP = {
5975
MTLCompareFunctionNever, // NONE
6076
MTLCompareFunctionAlways, // ALWAYS

Source/Metal/DescriptorMTL.h

Whitespace-only changes.

Source/Metal/DeviceMTL.hpp

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) {
1717
}
1818

1919
static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) {
20-
return ((const DeviceDesc&)device).GetFormatSupport(format);
20+
return (FormatSupportBits)0;
21+
//return ((const DeviceDesc&)device).GetFormatSupport(format);
2122
}
2223

2324
static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) {
@@ -45,18 +46,16 @@ static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue,
4546
}
4647

4748
static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) {
48-
//return ((DeviceMTL&)device).CreateImplementation<DescriptorPoolD3D12>(descriptorPool, descriptorPoolDesc);
49+
//return ((DeviceMTL&)device).CreateImplementation<DescriptorPoolMTL>(descriptorPool, descriptorPoolDesc);
4950
return Result::SUCCESS;
5051
}
5152

5253
static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) {
53-
//return ((DeviceMTL&)device).CreateImplementation<BufferD3D12>(buffer, bufferDesc);
54-
return Result::SUCCESS;
54+
return ((DeviceMTL&)device).CreateImplementation<BufferMTL>(buffer, bufferDesc);
5555
}
5656

5757
static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) {
58-
//return ((DeviceMTL&)device).CreateImplementation<TextureD3D12>(texture, textureDesc);
59-
return Result::SUCCESS;
58+
return ((DeviceMTL&)device).CreateImplementation<TextureMTL>(texture, textureDesc);
6059
}
6160

6261
static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) {
@@ -65,7 +64,12 @@ static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, De
6564
return Result::SUCCESS;
6665
}
6766

67+
6868
static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) {
69+
//DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice();
70+
//return device.CreateImplementation<DescriptorMTL>(textureView, textureViewDesc);
71+
72+
6973
//DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice();
7074
//return device.CreateImplementation<DescriptorD3D12>(textureView, textureViewDesc);
7175
return Result::SUCCESS;
@@ -150,7 +154,7 @@ static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) {
150154
}
151155

152156
static void NRI_CALL DestroyFence(Fence& fence) {
153-
//Destroy((FenceD3D12*)&fence);
157+
Destroy((FenceMTL*)&fence);
154158
}
155159

156160
static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) {
@@ -191,12 +195,12 @@ static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) {
191195
static void* NRI_CALL GetDeviceNativeObject(const Device& device) {
192196
if (!(&device))
193197
return nullptr;
194-
return (MTLDevice*)((DeviceMTL&)device);
198+
return (DeviceMTL*)&((DeviceMTL&)device);
195199

196200
//return ((DeviceMetal&)device).GetNativeObject();
197201
}
198202

199-
Result DeviceMetal::FillFunctionTable(CoreInterface& table) const {
203+
Result DeviceMTL::FillFunctionTable(CoreInterface& table) const {
200204
table = {};
201205
Core_Device_PartiallyFillFunctionTableMTL(table);
202206
Core_Buffer_PartiallyFillFunctionTableMTL(table);
@@ -214,44 +218,44 @@ Result DeviceMetal::FillFunctionTable(CoreInterface& table) const {
214218

215219
#pragma endregion
216220

217-
Result DeviceMetal::FillFunctionTable(HelperInterface& table) const {
221+
Result DeviceMTL::FillFunctionTable(HelperInterface& table) const {
218222
table = {};
219-
return ResVult::UNSUPPORTED;
223+
return Result::UNSUPPORTED;
220224
}
221225

222-
Result DeviceMetal::FillFunctionTable(LowLatencyInterface& table) const {
226+
Result DeviceMTL::FillFunctionTable(LowLatencyInterface& table) const {
223227
table = {};
224-
return ResVult::UNSUPPORTED;
228+
return Result::UNSUPPORTED;
225229
}
226230

227-
Result DeviceMetal::FillFunctionTable(MeshShaderInterface& table) const {
228-
table = {};
229-
return ResVult::UNSUPPORTED;
230-
}
231+
//Result DeviceMTL::FillFunctionTable(MeshShaderInterface& table) const {
232+
// table = {};
233+
// return Result::UNSUPPORTED;
234+
//}
231235

232-
Result DeviceMetal::FillFunctionTable(RayTracingInterface& table) const {
236+
Result DeviceMTL::FillFunctionTable(RayTracingInterface& table) const {
233237
table = {};
234-
return ResVult::UNSUPPORTED;
238+
return Result::UNSUPPORTED;
235239
}
236240

237-
Result DeviceMetal::FillFunctionTable(StreamerInterface& table) const {
241+
Result DeviceMTL::FillFunctionTable(StreamerInterface& table) const {
238242
table = {};
239-
return ResVult::UNSUPPORTED;
243+
return Result::UNSUPPORTED;
240244
}
241245

242-
Result DeviceMetal::FillFunctionTable(SwapChainInterface& table) const {
246+
Result DeviceMTL::FillFunctionTable(SwapChainInterface& table) const {
243247
table = {};
244-
return ResVult::UNSUPPORTED;
248+
return Result::UNSUPPORTED;
245249
}
246250

247-
Result DeviceMetal::FillFunctionTable(ResourceAllocatorInterface& table) const {
251+
Result DeviceMTL::FillFunctionTable(ResourceAllocatorInterface& table) const {
248252
table = {};
249-
return ResVult::UNSUPPORTED;
253+
return Result::UNSUPPORTED;
250254
}
251255

252256
Define_Core_Device_PartiallyFillFunctionTable(MTL);
253-
Define_Helper_Device_PartiallyFillFunctionTable(MTL);
254-
Define_RayTracing_Device_PartiallyFillFunctionTable(MTL);
255-
Define_Streamer_Device_PartiallyFillFunctionTable(MTL);
256-
Define_SwapChain_Device_PartiallyFillFunctionTable(MTL);
257-
Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL);
257+
//Define_Helper_Device_PartiallyFillFunctionTable(MTL);
258+
//Define_RayTracing_Device_PartiallyFillFunctionTable(MTL);
259+
//Define_Streamer_Device_PartiallyFillFunctionTable(MTL);
260+
//Define_SwapChain_Device_PartiallyFillFunctionTable(MTL);
261+
//Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL);

Source/Metal/DeviceMTL.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
#include "DeviceMTL.h"
66

7+
//#include "AccelerationStructureVK.h"
8+
#include "BufferMTL.h"
9+
//#include "CommandAllocatorVK.h"
10+
//#include "CommandBufferMTL.h"
11+
#include "CommandQueueMTL.h"
12+
//#include "DescriptorPoolMTL.h"
13+
#include "DescriptorSetMTL.h"
14+
#include "TextureMTL.h"
15+
#include "FenceMTL.h"
16+
#include "MemoryMTL.h"
17+
#include "PipelineLayoutMTL.h"
18+
#include "PipelineMTL.h"
19+
20+
721
using namespace nri;
822

923
static bool FindMTLGpuFamily(id<MTLDevice> device,
@@ -130,6 +144,7 @@ static bool FindMTLGpuFamily(id<MTLDevice> device,
130144
m_Desc.adapterDesc.vendor = nri::Vendor::APPLE;
131145
}
132146

147+
133148
const uint64_t regID = [m_Device registryID];
134149
if (regID)
135150
{
@@ -166,3 +181,5 @@ static bool FindMTLGpuFamily(id<MTLDevice> device,
166181

167182
}
168183

184+
#include "DeviceMTL.hpp"
185+

Source/Metal/PipelineLayoutMTL.h

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,22 @@ struct PipelineLayoutMTL {
1111
: m_Device(device) {
1212
}
1313

14-
// inline operator VkPipelineLayout() const {
15-
// return m_Handle;
16-
// }
17-
//
18-
// inline DeviceVK& GetDevice() const {
19-
// return m_Device;
20-
// }
21-
//
22-
// inline const RuntimeBindingInfo& GetRuntimeBindingInfo() const {
23-
// return m_RuntimeBindingInfo;
24-
// }
25-
//
26-
// inline VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t index) const {
27-
// return m_DescriptorSetLayouts[index];
28-
// }
29-
//
30-
// inline VkPipelineBindPoint GetPipelineBindPoint() const {
31-
// return m_PipelineBindPoint;
32-
// }
33-
//
34-
// inline uint32_t GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const {
35-
36-
// }
37-
//
3814
~PipelineLayoutMTL();
3915
//
40-
// Result Create(const PipelineLayoutDesc& pipelineLayoutDesc);
41-
//
42-
// //================================================================================================================
43-
// // NRI
44-
// //================================================================================================================
45-
//
46-
// void SetDebugName(const char* name);
16+
inline Result Create(const PipelineLayoutDesc& pipelineLayoutDesc) {
17+
return CreateDesc(pipelineLayoutDesc);
18+
}
19+
4720

21+
struct PipelineDescriptorSet {
22+
NSMutableArray<MTLArgumentDescriptor*>* m_ArgumentDescriptors;
23+
};
4824

4925
private:
26+
Result CreateDesc(const PipelineLayoutDesc& pipelineLayoutDesc);
5027
DeviceMTL& m_Device;
51-
//Vector<MTLArgumentDescriptor*> m_DescriptorSetLayouts;
52-
28+
29+
std::vector<PipelineDescriptorSet> m_DescriptorSets;
5330
};
5431

5532
}

Source/Metal/PipelineLayoutMTL.mm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,30 @@
88

99
}
1010

11+
Result PipelineLayoutMTL::CreateDesc(const PipelineLayoutDesc& pipelineLayoutDesc) {
12+
m_DescriptorSets.resize(pipelineLayoutDesc.descriptorSetNum);
13+
for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) {
14+
const DescriptorSetDesc& descriptorSetDesc = pipelineLayoutDesc.descriptorSets[i];
15+
16+
NSMutableArray<MTLArgumentDescriptor*>* argumentDescriptors = [[NSMutableArray alloc] init];
17+
MTLArgumentDescriptor* argDescriptor = [MTLArgumentDescriptor argumentDescriptor];
18+
19+
for(size_t r = 0; r < descriptorSetDesc.rangeNum; r++) {
20+
21+
}
22+
23+
24+
//argDescriptor.access = memberDescriptor.mAccessType;
25+
//argDescriptor.arrayLength = memberDescriptor.mArrayLength;
26+
//argDescriptor.constantBlockAlignment = memberDescriptor.mAlignment;
27+
//argDescriptor.dataType = memberDescriptor.mDataType;
28+
//argDescriptor.index = memberDescriptor.mArgumentIndex;
29+
//argDescriptor.textureType = memberDescriptor.mTextureType;
30+
31+
[argumentDescriptors addObject:argDescriptor];
32+
m_DescriptorSets[i].m_ArgumentDescriptors = argumentDescriptors;
33+
34+
}
35+
return Result::SUCCESS;
36+
}
1137

Source/Metal/PipelineMTL.mm

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init];
1313
for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) {
1414
}
15+
// Depth-stencil
16+
const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth;
17+
const StencilAttachmentDesc& sa = graphicsPipelineDesc.outputMerger.stencil;
1518

19+
const PipelineLayout *pl = graphicsPipelineDesc.pipelineLayout;
1620
const VertexInputDesc *vi = graphicsPipelineDesc.vertexInput;
1721
if (vi) {
22+
1823
// VkVertexInputBindingDescription* streams const_cast<VkVertexInputBindingDescription*>(vertexInputState.pVertexBindingDescriptions);
1924
//for (uint32_t i = 0; i < vi->streamNum; i++) {
2025
// const VertexStreamDesc &stream = vi->streams[i];
@@ -30,28 +35,25 @@
3035
// TODO: multisampling
3136
}
3237

33-
3438
// Blending
35-
const OutputMergerDesc& om = graphicsPipelineDesc.outputMerger;
3639

3740
// assign render target pixel format for all attachments
41+
const OutputMergerDesc& om = graphicsPipelineDesc.outputMerger;
3842
for (uint32_t i = 0; i < om.colorNum; i++) {
3943

40-
// MTLRenderPipelineColorAttachmentDescriptor& colorAtachment = &renderPipelineDesc.colorAttachments[i];
4144
const ColorAttachmentDesc& attachmentDesc = om.color[i];
42-
43-
//colorAtachment.pixelFormat = GetFormatMTL(attachmentDesc.format, false);
45+
renderPipelineDesc.colorAttachments[i].pixelFormat = GetFormatMTL(attachmentDesc.format, false);
4446

45-
//colorAtachment.blendingEnabled = attachmentDesc.blendEnabled;
46-
//colorAtachment.rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func);
47-
//colorAtachment.alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func);
47+
renderPipelineDesc.colorAttachments[i].blendingEnabled = attachmentDesc.blendEnabled;
48+
renderPipelineDesc.colorAttachments[i].rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func);
49+
renderPipelineDesc.colorAttachments[i].alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func);
4850

49-
//colorAtachment.sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor);
50-
//colorAtachment.destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor);
51-
//colorAtachment.sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor);
52-
//colorAtachment.destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor);
51+
renderPipelineDesc.colorAttachments[i].sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor);
52+
renderPipelineDesc.colorAttachments[i].destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor);
53+
renderPipelineDesc.colorAttachments[i].sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor);
54+
renderPipelineDesc.colorAttachments[i].destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor);
5355

54-
//colorAtachment.writeMask = GetColorComponent(pDesc->mColorWriteMasks[blendDescIndex]);
56+
renderPipelineDesc.colorAttachments[i].writeMask = GetColorComponent(attachmentDesc.colorWriteMask);
5557

5658
}
5759

Source/Metal/QueryMTL.h

Whitespace-only changes.

Source/Metal/QueryMTL.mm

Whitespace-only changes.

Source/Metal/SwapChainMTL.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// © 2021 NVIDIA Corporation
2+
3+
#pragma once
4+
5+
namespace nri {
6+
7+
// Let's keep things simple and hide it under the hood
8+
constexpr uint32_t MAX_NUMBER_OF_FRAMES_IN_FLIGHT = 8;
9+
10+
struct SwapChainMTL: public DisplayDescHelper {
11+
SwapChainVK(DeviceMTL& device);
12+
~SwapChainVK();
13+
14+
Result Create(const SwapChainDesc& swapChainDesc);
15+
16+
private:
17+
DeviceMTL& m_Device;
18+
uint64_t m_PresentId = 0;
19+
uint32_t m_TextureIndex = 0;
20+
uint8_t m_FrameIndex = 0; // in flight, not global
21+
};
22+
23+
};

0 commit comments

Comments
 (0)