Skip to content

Commit 8868d96

Browse files
committed
feat: begin shader load logic
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent 0697e07 commit 8868d96

File tree

4 files changed

+86
-17
lines changed

4 files changed

+86
-17
lines changed

Source/Metal/CommandBufferMTL.mm

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,59 @@
3939
PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline;
4040
m_CurrentPipeline = &pipelineImpl;
4141

42-
switch(m_CurrentPipeline->m_pipelineType) {
43-
case nri::PipelineMTL::Compute:
44-
m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL];
45-
break;
46-
default:
47-
break;
48-
}
42+
// if(m_CurrentPipeline->m_pipelineType == nri::PipelineMTL::Compute) {
43+
// m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL];
44+
// }
45+
46+
}
47+
void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {
48+
49+
}
50+
void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) {
51+
52+
}
53+
void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) {
54+
//if (pDesc->mUsedStages & SHADER_STAGE_VERT)
55+
//{
56+
// [m_RendererEncoder setVertexBytes:data length:size atIndex:pushConstantIndex];
57+
//}
58+
59+
//if (pDesc->mUsedStages & SHADER_STAGE_FRAG)
60+
//{
61+
// [m_RendererEncoder setFragmentBytes:data length:size atIndex:pushConstantIndex];
62+
//}
63+
64+
//if (pDesc->mUsedStages & SHADER_STAGE_COMP)
65+
//{
66+
// [m_RendererEncoder setBytes:data length:size atIndex:pushConstantIndex];
67+
//}
4968

5069
}
51-
void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {}
52-
void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) {}
53-
void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) {}
5470
void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {}
55-
void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {}
71+
void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {
72+
//if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_BUFFERS)
73+
{
74+
[m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers
75+
afterStages:MTLRenderStageFragment
76+
beforeStages:MTLRenderStageVertex];
77+
}
78+
79+
//if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_TEXTURES)
80+
{
81+
[m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeTextures
82+
afterStages:MTLRenderStageFragment
83+
beforeStages:MTLRenderStageVertex];
84+
}
85+
86+
//if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_RENDERTARGETS)
87+
{
88+
[m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeRenderTargets
89+
afterStages:MTLRenderStageFragment
90+
beforeStages:MTLRenderStageVertex];
91+
}
92+
93+
94+
}
5695
void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) {
5796
m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL];
5897
}
@@ -61,7 +100,11 @@
61100
m_RendererEncoder = nil;
62101
m_ComputeEncoder = nil;
63102
}
64-
void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {}
103+
void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {
104+
MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum);
105+
106+
// [m_RendererEncoder setViewports:<#(const MTLViewport * _Nonnull)#> count:<#(NSUInteger)#>
107+
}
65108
void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {
66109
NSCAssert(m_RendererEncoder, @"encoder set");
67110
MTLScissorRect rect;

Source/Metal/CommandQueueMTL.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ namespace nri {
77

88
struct DeviceMTL;
99

10+
NriBits(QueueBarrierBits, uint8_t,
11+
NONE = 0,
12+
BARRIER_FLAG_BUFFERS = NriBit(0),
13+
BARRIER_FLAG_TEXTURES = NriBit(1),
14+
BARRIER_FLAG_RENDERTARGETS = NriBit(2),
15+
BARRIER_FLAG_FENCE = NriBit(3));
16+
1017
struct CommandQueueMTL {
1118

1219
inline CommandQueueMTL(DeviceMTL& device)
@@ -35,11 +42,14 @@ struct CommandQueueMTL {
3542

3643
void SetDebugName(const char* name);
3744
Result Create(CommandQueueType type);
45+
QueueBarrierBits m_BarrierBits = QueueBarrierBits::NONE;
46+
3847
private:
3948
DeviceMTL& m_Device;
4049
CommandQueueType m_Type = CommandQueueType(-1);
4150
id<MTLCommandQueue> m_Handle;
4251
Lock m_Lock;
52+
4353
};
4454

4555
}

Source/Metal/PipelineMTL.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ namespace nri {
77
struct DeviceMTL;
88
struct PipelineLayoutMTL;
99

10+
NriEnum(PipelineType, uint8_t,
11+
Compute,
12+
Graphics,
13+
Raytracing
14+
);
15+
1016
struct PipelineMTL {
11-
enum PipelineType {
12-
Compute,
13-
Graphics,
14-
Raytracing
15-
};
1617

1718
inline PipelineMTL(DeviceMTL& device)
1819
: m_Device(device) {
@@ -26,6 +27,7 @@ struct PipelineMTL {
2627
PipelineType m_pipelineType;
2728
MTLPrimitiveTopologyClass m_topologyClass;
2829
MTLPrimitiveType m_primitiveType;
30+
StageBits m_usedBits;
2931
private:
3032
DeviceMTL& m_Device;
3133
};

Source/Metal/PipelineMTL.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
}
4444

4545
for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) {
46+
const ShaderDesc& shader = graphicsPipelineDesc.shaders[i];
47+
m_usedBits |= shader.stage;
48+
49+
dispatch_data_t byteCode =
50+
dispatch_data_create(shader.bytecode, shader.size, nil,
51+
DISPATCH_DATA_DESTRUCTOR_DEFAULT);
52+
id<MTLLibrary> lib = [m_Device newLibraryWithData:byteCode error:nil];
53+
54+
// Create a MTLFunction from the loaded MTLLibrary.
55+
NSString *entryPointNStr = [lib functionNames][0];
56+
if (shader.entryPointName) {
57+
entryPointNStr =
58+
[[NSString alloc] initWithUTF8String:shader.entryPointName];
59+
}
4660
}
4761
// Depth-stencil
4862
const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth;

0 commit comments

Comments
 (0)