Skip to content

Commit

Permalink
feat: begin shader load logic
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <mpollind@gmail.com>
  • Loading branch information
pollend committed Sep 8, 2024
1 parent da03259 commit b2f4953
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
67 changes: 55 additions & 12 deletions Source/Metal/CommandBufferMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,59 @@
PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline;
m_CurrentPipeline = &pipelineImpl;

switch(m_CurrentPipeline->m_pipelineType) {
case nri::PipelineMTL::Compute:
m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL];
break;
default:
break;
}
// if(m_CurrentPipeline->m_pipelineType == nri::PipelineMTL::Compute) {
// m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL];
// }

}
void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {

}
void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) {

}
void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) {
//if (pDesc->mUsedStages & SHADER_STAGE_VERT)
//{
// [m_RendererEncoder setVertexBytes:data length:size atIndex:pushConstantIndex];
//}

//if (pDesc->mUsedStages & SHADER_STAGE_FRAG)
//{
// [m_RendererEncoder setFragmentBytes:data length:size atIndex:pushConstantIndex];
//}

//if (pDesc->mUsedStages & SHADER_STAGE_COMP)
//{
// [m_RendererEncoder setBytes:data length:size atIndex:pushConstantIndex];
//}

}
void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {}
void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) {}
void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) {}
void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {}
void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {}
void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {
//if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_BUFFERS)
{
[m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers
afterStages:MTLRenderStageFragment
beforeStages:MTLRenderStageVertex];
}

//if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_TEXTURES)
{
[m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeTextures
afterStages:MTLRenderStageFragment
beforeStages:MTLRenderStageVertex];
}

//if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_RENDERTARGETS)
{
[m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeRenderTargets
afterStages:MTLRenderStageFragment
beforeStages:MTLRenderStageVertex];
}


}
void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) {
m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL];
}
Expand All @@ -61,7 +100,11 @@
m_RendererEncoder = nil;
m_ComputeEncoder = nil;
}
void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {}
void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {
MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum);

// [m_RendererEncoder setViewports:<#(const MTLViewport * _Nonnull)#> count:<#(NSUInteger)#>
}
void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {
NSCAssert(m_RendererEncoder, @"encoder set");
MTLScissorRect rect;
Expand Down
10 changes: 10 additions & 0 deletions Source/Metal/CommandQueueMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace nri {

struct DeviceMTL;

NriBits(QueueBarrierBits, uint8_t,
NONE = 0,
BARRIER_FLAG_BUFFERS = NriBit(0),
BARRIER_FLAG_TEXTURES = NriBit(1),
BARRIER_FLAG_RENDERTARGETS = NriBit(2),
BARRIER_FLAG_FENCE = NriBit(3));

struct CommandQueueMTL {

inline CommandQueueMTL(DeviceMTL& device)
Expand Down Expand Up @@ -35,11 +42,14 @@ struct CommandQueueMTL {

void SetDebugName(const char* name);
Result Create(CommandQueueType type);
QueueBarrierBits m_BarrierBits = QueueBarrierBits::NONE;

private:
DeviceMTL& m_Device;
CommandQueueType m_Type = CommandQueueType(-1);
id<MTLCommandQueue> m_Handle;
Lock m_Lock;

};

}
Expand Down
12 changes: 7 additions & 5 deletions Source/Metal/PipelineMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace nri {
struct DeviceMTL;
struct PipelineLayoutMTL;

NriEnum(PipelineType, uint8_t,
Compute,
Graphics,
Raytracing
);

struct PipelineMTL {
enum PipelineType {
Compute,
Graphics,
Raytracing
};

inline PipelineMTL(DeviceMTL& device)
: m_Device(device) {
Expand All @@ -26,6 +27,7 @@ struct PipelineMTL {
PipelineType m_pipelineType;
MTLPrimitiveTopologyClass m_topologyClass;
MTLPrimitiveType m_primitiveType;
StageBits m_usedBits;
private:
DeviceMTL& m_Device;
};
Expand Down
14 changes: 14 additions & 0 deletions Source/Metal/PipelineMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
}

for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) {
const ShaderDesc& shader = graphicsPipelineDesc.shaders[i];
m_usedBits |= shader.stage;

dispatch_data_t byteCode =
dispatch_data_create(shader.bytecode, shader.size, nil,
DISPATCH_DATA_DESTRUCTOR_DEFAULT);
id<MTLLibrary> lib = [m_Device newLibraryWithData:byteCode error:nil];

// Create a MTLFunction from the loaded MTLLibrary.
NSString *entryPointNStr = [lib functionNames][0];
if (shader.entryPointName) {
entryPointNStr =
[[NSString alloc] initWithUTF8String:shader.entryPointName];
}
}
// Depth-stencil
const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth;
Expand Down

0 comments on commit b2f4953

Please sign in to comment.