Skip to content

Commit 0697e07

Browse files
committed
start working on command buffer
1 parent 07f9b2d commit 0697e07

File tree

6 files changed

+177
-28
lines changed

6 files changed

+177
-28
lines changed

Source/Metal/CommandAllocatorMTL.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// © 2021 NVIDIA Corporation
2+
3+
#pragma once
4+
5+
namespace nri {
6+
7+
struct DeviceMTL;
8+
struct CommandQueueMTL;
9+
10+
struct CommandAllocatorMTL {
11+
inline CommandAllocatorMTL(DeviceMTL& device)
12+
: m_Device(device) {
13+
}
14+
15+
inline DeviceMTL& GetDevice() const {
16+
return m_Device;
17+
}
18+
19+
~CommandAllocatorMTL();
20+
21+
22+
Result Create(const CommandQueue& commandQueue);
23+
24+
//================================================================================================================
25+
// NRI
26+
//================================================================================================================
27+
28+
Result CreateCommandBuffer(CommandBuffer*& commandBuffer);
29+
30+
private:
31+
struct CommandQueueMTL* m_CommandQueue;
32+
DeviceMTL& m_Device;
33+
};
34+
35+
}
36+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// © 2021 NVIDIA Corporation
2+
3+
#pragma region[ Core ]
4+
5+
static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) {
6+
//((CommandAllocatorVK&)commandAllocator).SetDebugName(name);
7+
}
8+
9+
static Result NRI_CALL CreateCommandBuffer(CommandAllocator& commandAllocator, CommandBuffer*& commandBuffer) {
10+
return ((CommandAllocatorMTL&)commandAllocator).CreateCommandBuffer(commandBuffer);
11+
}
12+
13+
static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) {
14+
//((CommandAllocatorVK&)commandAllocator).Reset();
15+
}
16+
17+
#pragma endregion
18+
19+
Define_Core_CommandAllocator_PartiallyFillFunctionTable(VK);
20+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "SharedMTL.h"
2+
3+
#include "CommandAllocatorMTL.h"
4+
5+
#include "CommandQueueMTL.h"
6+
7+
using namespace nri;
8+
9+
CommandAllocatorMTL::~CommandAllocatorMTL() {
10+
11+
}
12+
13+
Result CommandAllocatorMTL::Create(const CommandQueue& commandQueue) {
14+
m_CommandQueue = &(CommandQueueMTL&)commandQueue;
15+
return Result::SUCCESS;
16+
}
17+
18+
//================================================================================================================
19+
// NRI
20+
//================================================================================================================
21+
22+
Result CommandAllocatorMTL::CreateCommandBuffer(CommandBuffer*& commandBuffer) {
23+
24+
25+
return Result::SUCCESS;
26+
}
27+
28+
#include "CommandAllocatorMTL.hpp"

Source/Metal/CommandBufferMTL.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#pragma once
44

5+
#include <MacTypes.h>
56
#import <MetalKit/MetalKit.h>
67

78
namespace nri {
@@ -13,15 +14,18 @@ struct PipelineLayoutMTL;
1314
struct TextureMTL;
1415
struct DescriptorMTL;
1516

17+
18+
NriBits(CommandBufferDirtyBits, uint8_t,
19+
NONE = 0,
20+
CMD_DIRTY_STENCIL = NriBit(0)
21+
);
22+
1623
struct CommandBufferMTL {
24+
1725
inline CommandBufferMTL(DeviceMTL& device)
1826
: m_Device(device) {
1927
}
20-
21-
// inline operator VkCommandBuffer() const {
22-
// return m_Handle;
23-
// }
24-
28+
2529
inline DeviceMTL& GetDevice() const {
2630
return m_Device;
2731
}
@@ -77,20 +81,27 @@ struct CommandBufferMTL {
7781
void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride);
7882

7983
~CommandBufferMTL();
80-
84+
85+
void Create(id<MTLCommandBuffer> cmd);
86+
8187
struct CmdIndexBuffer {
8288
size_t m_Offset;
8389
MTLIndexType m_Type;
8490
struct BufferMTL* m_Buffer;
8591
};
92+
8693
private:
94+
95+
void updateCommandBufferState();
96+
8797
DeviceMTL& m_Device;
8898
struct PipelineMTL* m_CurrentPipeline = nullptr;
8999
struct CmdIndexBuffer m_CurrentIndexCmd;
90100
id<MTLCommandBuffer> m_Handle;
91-
id<MTLRenderCommandEncoder> m_encoder;
101+
id<MTLRenderCommandEncoder> m_RendererEncoder = nil;
102+
id<MTLComputeCommandEncoder> m_ComputeEncoder = nil;
92103

93-
//id<MTLRenderCommandEncoder> renderEncoder;
104+
CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE;
94105
};
95106
};
96107

Source/Metal/CommandBufferMTL.mm

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,26 @@
2626
}
2727

2828
Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) {
29-
29+
[m_Handle computeCommandEncoderWithDescriptor: NULL];
3030
}
3131
Result CommandBufferMTL::End() {
32+
m_ComputeEncoder = nil;
33+
m_RendererEncoder = nil;
3234

3335
}
3436
void CommandBufferMTL::SetPipeline(const Pipeline& pipeline) {
3537
if (m_CurrentPipeline == (PipelineMTL*)&pipeline)
3638
return;
3739
PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline;
3840
m_CurrentPipeline = &pipelineImpl;
39-
41+
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+
}
4049

4150
}
4251
void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {}
@@ -45,19 +54,46 @@
4554
void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {}
4655
void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {}
4756
void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) {
48-
m_encoder = [m_Handle renderCommandEncoderWithDescriptor: NULL];
57+
m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL];
4958
}
5059
void CommandBufferMTL::EndRendering() {
51-
[m_encoder endEncoding];
60+
[m_RendererEncoder endEncoding];
61+
m_RendererEncoder = nil;
62+
m_ComputeEncoder = nil;
5263
}
5364
void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {}
54-
void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {}
55-
void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) {}
56-
void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) {}
57-
void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) {}
58-
void CommandBufferMTL::SetBlendConstants(const Color32f& color) {}
59-
void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) {}
60-
void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) {}
65+
void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {
66+
NSCAssert(m_RendererEncoder, @"encoder set");
67+
MTLScissorRect rect;
68+
rect.x = rects[rectNum].x;
69+
rect.y = rects[rectNum].y;
70+
rect.width = rects[rectNum].width;
71+
rect.height = rects[rectNum].height;
72+
[m_RendererEncoder setScissorRect:rect];
73+
}
74+
void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) {
75+
}
76+
void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) {
77+
[m_RendererEncoder setStencilFrontReferenceValue: frontRef backReferenceValue:backRef];
78+
}
79+
void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) {
80+
81+
}
82+
void CommandBufferMTL::SetBlendConstants(const Color32f& color) {
83+
[m_RendererEncoder
84+
setBlendColorRed:color.x
85+
green:color.y
86+
blue:color.z
87+
alpha:color.w
88+
];
89+
}
90+
void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) {
91+
92+
}
93+
94+
void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) {
95+
}
96+
6197
void CommandBufferMTL::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) {
6298
m_CurrentIndexCmd.m_Buffer = &(BufferMTL&)buffer;
6399
switch(indexType) {
@@ -71,28 +107,38 @@
71107
}
72108
m_CurrentIndexCmd.m_Offset = offset;
73109
}
110+
74111
void CommandBufferMTL::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) {
112+
for(size_t i = 0; i < bufferNum; i++) {
113+
BufferMTL* mtlBuffer = (BufferMTL*)buffers[i];
114+
[m_RendererEncoder setVertexBuffer: mtlBuffer->GetHandle()
115+
offset: offsets[i]
116+
atIndex: i + baseSlot];
117+
}
75118

76119
}
120+
77121
void CommandBufferMTL::Draw(const DrawDesc& drawDesc) {
78-
[m_encoder drawPrimitives: m_CurrentPipeline->m_primitiveType
122+
[m_RendererEncoder drawPrimitives: m_CurrentPipeline->m_primitiveType
79123
vertexStart:drawDesc.baseVertex
80124
vertexCount:drawDesc.vertexNum
81125
instanceCount:drawDesc.instanceNum
82126
baseInstance:0];
83127
}
128+
84129
void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) {
85130
id<MTLBuffer> indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle();
86-
[m_encoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType
131+
[m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType
87132
indexCount:drawIndexedDesc.indexNum
88133
indexType: m_CurrentIndexCmd.m_Type
89134
indexBuffer: indexBuffer
90135
indexBufferOffset: m_CurrentIndexCmd.m_Offset];
91136
}
92137

93138
void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {
139+
// TODO: implement count Buffer
94140
NSCAssert(!countBuffer, @"count buffer not supported");
95-
[m_encoder
141+
[m_RendererEncoder
96142
drawPrimitives: m_CurrentPipeline->m_primitiveType
97143
indirectBuffer:((BufferMTL&)buffer).GetHandle()
98144
indirectBufferOffset: offset];
@@ -101,7 +147,9 @@
101147
void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {
102148

103149
}
104-
void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) {}
150+
void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) {
151+
152+
}
105153
void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) {}
106154
void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) {}
107155
void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) {}
@@ -124,7 +172,13 @@
124172
void CommandBufferMTL::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) {}
125173
void CommandBufferMTL::DispatchRaysIndirect(const Buffer& buffer, uint64_t offset) {}
126174
void CommandBufferMTL::DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc) {}
127-
void CommandBufferMTL::DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) {}
175+
void CommandBufferMTL::DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) {
176+
177+
}
178+
179+
void Create(id<MTLCommandBuffer> cmd);
180+
181+
128182

129183
#include "CommandBufferMTL.hpp"
130184

Source/Metal/PipelineMTL.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ struct DeviceMTL;
88
struct PipelineLayoutMTL;
99

1010
struct PipelineMTL {
11-
enum PiplineType {
12-
None,
13-
Graphics,
11+
enum PipelineType {
1412
Compute,
13+
Graphics,
1514
Raytracing
1615
};
1716

@@ -23,7 +22,8 @@ struct PipelineMTL {
2322
Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc);
2423
Result Create(const ComputePipelineDesc& computePipelineDesc);
2524
Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc);
26-
25+
26+
PipelineType m_pipelineType;
2727
MTLPrimitiveTopologyClass m_topologyClass;
2828
MTLPrimitiveType m_primitiveType;
2929
private:

0 commit comments

Comments
 (0)