Skip to content

Commit e3e93c8

Browse files
Implemented Index and Constant Buffers (#55)
* removed hash - window.yml * Implemented Index and Constant Buffers * Update Buffer.h * Update METALPipelineHandler.h * removed opengl test for mac
1 parent 53de695 commit e3e93c8

File tree

12 files changed

+130
-26
lines changed

12 files changed

+130
-26
lines changed

.github/workflows/macosx.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
fail-fast: false
2424
matrix:
2525
prj: [ BlankApp, HelloTriangle, SpinningCube, ModelLoading ]
26-
rhi: [ metal, opengl ] # TODO: Add vulkan
26+
rhi: [ metal ] # TODO: Add vulkan
2727

2828
steps:
2929
- name: Get current date as package key

.github/workflows/windows.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,12 @@ jobs:
4747
- name: Update xmake repository
4848
run: xmake repo --update
4949

50-
- name: Retrieve dependencies hash
51-
id: dep_hash
52-
run: echo "hash=$(xmake l utils.ci.packageskey)" >> $GITHUB_ENV
53-
shell: bash
54-
5550
- name: Restore cached xmake dependencies
5651
id: restore-depcache
5752
uses: actions/cache/restore@v4
5853
with:
5954
path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages
60-
key: ${{ matrix.rhi }}-${{ matrix.prj }}-${{ env.hash }}-W${{ env.key }}
55+
key: ${{ matrix.rhi }}-${{ matrix.prj }}-W${{ env.key }}
6156

6257
- name: Check xmake packages directory exists
6358
shell: bash

Core/Graphics/Buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <Core/Graphics/Types.h>
44
#include <Core/Types.h>
5+
#include <typeindex>
56

67
#if defined(CGL_RHI_DX11)
78
#include <Core/Graphics/RHI/D3D11/D3D11VertexBuffer.h>

Core/Graphics/RHI/Metal/METALIndexBuffer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
#include "Foundation/NSTypes.hpp"
44
#include "Metal/MTLBuffer.hpp"
5+
#include "Metal/MTLStageInputOutputDescriptor.hpp"
56

67
namespace CGL::Graphics
78
{
89
struct METALIndexBuffer
910
{
1011
MTL::Buffer* Buffer;
12+
MTL::IndexType Format;
1113

12-
NS::UInteger Offset;
13-
NS::UInteger Index;
14+
NS::UInteger IndicesCount;
1415
};
1516
}

Core/Graphics/RHI/Metal/METALPipelineHandler.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,27 @@ namespace CGL::Graphics {
2323

2424
m_rpState = gpu_device->newRenderPipelineState(m_rpDescriptor, &ns_error);
2525

26-
if(!m_rpState)
27-
{
26+
if(!m_rpState)
27+
{
2828
CGL_LOG(METALPipelineHandler, Error, ns_error->localizedDescription()->utf8String());
2929
return;
3030
}
3131

3232
CGL_LOG(METALPipelineHandler, Info, "RenderPipelineState Created");
33+
34+
MTL::DepthStencilDescriptor* depthStencilDescriptor = MTL::DepthStencilDescriptor::alloc()->init();
35+
36+
depthStencilDescriptor->setDepthWriteEnabled(true);
37+
depthStencilDescriptor->setDepthCompareFunction(MTL::CompareFunction::CompareFunctionAlways);
38+
39+
this->m_depthStencilState = gpu_device->newDepthStencilState(depthStencilDescriptor);
40+
41+
CGL_LOG(METALPipelineHandler, Info, "DepthStencilState Created");
42+
3343
m_rpDescriptor->release();
3444
m_rpDescriptor = nullptr;
45+
46+
depthStencilDescriptor->release();
47+
depthStencilDescriptor = nullptr;
3548
}
3649
}

Core/Graphics/RHI/Metal/METALPipelineHandler.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "Metal/MTLDevice.hpp"
44
#include "Metal/MTLRenderPipeline.hpp"
55

6-
#include "Core/Logging/Log.h"
6+
#include <Core/Graphics/Buffer.h>
7+
#include <Core/Logging/Log.h>
78

89
namespace CGL::Graphics
910
{
@@ -17,10 +18,18 @@ namespace CGL::Graphics
1718
inline MTL::RenderPipelineDescriptor* GetRenderPipelineDescriptor() const { return m_rpDescriptor; }
1819
inline MTL::RenderPipelineState* GetRenderPipelineState() const { return m_rpState; }
1920

20-
void CreateRenderPipelineState(MTL::Device* gpu_device);
21+
inline MTL::DepthStencilState* GetDepthStencilState() const { return m_depthStencilState; }
22+
23+
inline void SetIndexBuffer(IndexBuffer* indexBuffer) { m_indexBuffer = indexBuffer; }
24+
inline METALIndexBuffer* GetIndexBuffer() const { return m_indexBuffer; }
2125

26+
void CreateRenderPipelineState(MTL::Device* gpu_device);
2227
private:
2328
MTL::RenderPipelineDescriptor* m_rpDescriptor;
2429
MTL::RenderPipelineState* m_rpState;
30+
31+
MTL::DepthStencilState* m_depthStencilState;
32+
33+
METALIndexBuffer* m_indexBuffer;
2534
};
2635
}

Core/Graphics/RHI/Metal/METALRenderer.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ namespace CGL::Graphics
5858
void Renderer::SetRenderPipeline_METAL()
5959
{
6060
GetImpl()->GetRenderPipelineHandler()->CreateRenderPipelineState(GetImpl()->GetDevice());
61-
GetImpl()->GetRenderCommandEncoder()->setRenderPipelineState(GetImpl()->GetRenderPipelineHandler()->GetRenderPipelineState());
61+
62+
const auto& rCommandEncoder = GetImpl()->GetRenderCommandEncoder();
63+
64+
rCommandEncoder->setFrontFacingWinding(MTL::WindingClockwise);
65+
rCommandEncoder->setCullMode(MTL::CullModeBack);
66+
rCommandEncoder->setTriangleFillMode(MTL::TriangleFillMode::TriangleFillModeFill);
67+
rCommandEncoder->setRenderPipelineState(GetImpl()->GetRenderPipelineHandler()->GetRenderPipelineState());
68+
rCommandEncoder->setDepthStencilState(GetImpl()->GetRenderPipelineHandler()->GetDepthStencilState());
6269
}
6370

6471
void Renderer::SetPrimitiveTopology_METAL(PrimitiveType topology)
@@ -99,9 +106,19 @@ namespace CGL::Graphics
99106

100107
void Renderer::SetIndexBuffer_METAL(const IndexBuffer& buffer)
101108
{
102-
// TODO: implement index buffer
109+
GetImpl()->GetRenderPipelineHandler()->SetIndexBuffer(&const_cast<IndexBuffer&>(buffer));
103110
}
104111

112+
void Renderer::SetConstantBufferData_METAL(const MTL::Buffer* buffer, const void* data, size_t size)
113+
{
114+
memcpy(const_cast<MTL::Buffer*>(buffer)->contents(), data, size);
115+
}
116+
117+
void Renderer::SetConstantBuffer_METAL([[maybe_unused]] ShaderType type, [[maybe_unused]] u32 startSlot, const MTL::Buffer* buffer)
118+
{
119+
GetImpl()->GetRenderCommandEncoder()->setVertexBuffer(buffer, 0, 1);
120+
}
121+
105122
ShaderCompileResult Renderer::CompileVertexShader_METAL(const ShaderSource& source, VertexShader* outShader)
106123
{
107124
assert(GetImpl() && GetImpl()->GetDevice() && outShader);
@@ -168,7 +185,20 @@ namespace CGL::Graphics
168185

169186
IndexBuffer Renderer::CreateIndexBuffer_METAL(const BufferSource& source)
170187
{
171-
// TODO: implement Index Buffer
188+
assert(GetImpl() && GetImpl()->GetDevice());
189+
190+
const u32 sourceSZ = source.TypeSize * source.Count;
191+
192+
return IndexBuffer {
193+
.Buffer = GetImpl()->GetDevice()->newBuffer(source.Data, sourceSZ, MTL::ResourceStorageModeShared),
194+
.Format = (source.TypeSize == sizeof(u16) ? MTL::IndexTypeUInt16 : MTL::IndexTypeUInt32),
195+
.IndicesCount = source.Count
196+
};
197+
}
198+
199+
void Renderer::CreateConstantBuffer_METAL(const BufferSource& source, MTL::Buffer** outBuffer)
200+
{
201+
*outBuffer = GetImpl()->GetDevice()->newBuffer(source.TypeSize, MTL::ResourceStorageModeShared);
172202
}
173203

174204
void Renderer::Draw_METAL(u32 vertexCount, u32 startVertex)
@@ -178,8 +208,12 @@ namespace CGL::Graphics
178208
);
179209
}
180210

181-
void Renderer::DrawIndexed_METAL(u32 indexCount, u32 startIndex, u32 baseVertex)
211+
void Renderer::DrawIndexed_METAL(u32 indexCount, [[maybe_unused]] u32 startIndex, [[maybe_unused]] u32 baseVertex)
182212
{
183-
// TODO: implement indexed drawcall
213+
const METALIndexBuffer* IndexBuffer = GetImpl()->GetRenderPipelineHandler()->GetIndexBuffer();
214+
215+
GetImpl()->GetRenderCommandEncoder()->drawIndexedPrimitives(
216+
GetImpl()->GetPrimitiveType(), indexCount, IndexBuffer->Format, IndexBuffer->Buffer, 0
217+
);
184218
}
185219
}

Core/Graphics/Renderer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ namespace CGL::Graphics
121121
ShaderCompileResult CompilePixelShader_METAL(const ShaderSource& source, PixelShader* outShader);
122122
VertexBuffer CreateVertexBuffer_METAL(const BufferSource& source);
123123
IndexBuffer CreateIndexBuffer_METAL(const BufferSource& source);
124+
void CreateConstantBuffer_METAL(const BufferSource& source, MTL::Buffer** outBuffer);
125+
void SetConstantBufferData_METAL(const MTL::Buffer* buffer, const void* data, size_t size);
126+
void SetConstantBuffer_METAL(ShaderType type, u32 startSlot, const MTL::Buffer* buffer);
124127
void Draw_METAL(u32 vertexCount, u32 startVertex = 0);
125128
void DrawIndexed_METAL(u32 indexCount, u32 startIndex = 0, u32 baseVertex = 0);
126129
METALRendererImpl* GetImpl() const;

Core/Graphics/Renderer.inl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@
22

33
namespace CGL::Graphics
44
{
5-
template <typename T>
5+
template <typename T>
66
void Renderer::CreateContantBuffer(const BufferSource& source, ConstantBuffer<T>& outBuffer)
77
{
88
#if defined(CGL_RHI_DX11)
99
CreateContantBuffer_D3D11(source, outBuffer.Buffer);
1010
#elif defined(CGL_RHI_OPENGL)
1111
CreateConstantBuffer_OPENGL(source, outBuffer.Buffer);
12+
#elif defined(CGL_RHI_METAL)
13+
CreateConstantBuffer_METAL(source, &outBuffer.Buffer);
1214
#endif
1315
}
1416

15-
template <typename T>
17+
template <typename T>
1618
void Renderer::SetConstantBufferData(const ConstantBuffer<T>& buffer, const T& data)
1719
{
1820
#if defined(CGL_RHI_DX11)
1921
SetConstantBufferData_D3D11(buffer.Buffer.Get(), static_cast<const void*>(&data), sizeof(T));
2022
#elif defined(CGL_RHI_OPENGL)
21-
SetConstantBufferData_OPENGL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
22-
#endif
23+
SetConstantBufferData_OPENGL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
24+
#elif defined(CGL_RHI_METAL)
25+
SetConstantBufferData_METAL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
26+
#endif
2327
}
24-
template <typename T>
28+
template <typename T>
2529
void Renderer::SetConstantBuffer(ShaderType shaderType, u32 startSlot, const ConstantBuffer<T>& buffer)
2630
{
2731
#if defined(CGL_RHI_DX11)
2832
SetContantBuffer_D3D11(shaderType, startSlot, buffer.Buffer);
2933
#elif defined(CGL_RHI_OPENGL)
3034
SetConstantBuffer_OPENGL(shaderType, startSlot, buffer.Buffer);
31-
#endif
35+
#elif defined(CGL_RHI_METAL)
36+
SetConstantBuffer_METAL(shaderType, startSlot, buffer.Buffer);
37+
#endif
3238
}
33-
}
39+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <metal_stdlib>
2+
using namespace metal;
3+
4+
struct VertexOut {
5+
float4 position [[position]];
6+
float4 color;
7+
};
8+
9+
fragment float4 SpinningCubePS(VertexOut in [[stage_in]])
10+
{
11+
return in.color;
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <metal_stdlib>
2+
using namespace metal;
3+
4+
struct TransformationData
5+
{
6+
float4x4 modelMatrix;
7+
float4x4 viewMatrix;
8+
float4x4 perspectiveMatrix;
9+
};
10+
11+
struct VertexOut
12+
{
13+
float4 position [[position]];
14+
float4 color;
15+
};
16+
17+
vertex VertexOut SpinningCubeVS(uint vertexID [[vertex_id]], constant VertexOut* vertexData, constant TransformationData* transformationData)
18+
{
19+
VertexOut out;
20+
21+
float4 vertexPosition = vertexData[vertexID].position;
22+
23+
float4x4 ModelView = transformationData->viewMatrix * transformationData->modelMatrix;
24+
float4x4 ModelViewProjection = transformationData->perspectiveMatrix * ModelView;
25+
26+
out.position = ModelViewProjection * vertexPosition;
27+
out.color = vertexData[vertexID].color;
28+
29+
return out;
30+
}

Samples/SpinningCube/SpinningCube.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "SpinningCube.h"
22
#include <Core/Application/AssetFinder.h>
33

4-
#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN)
4+
#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN) || defined(CGL_RHI_METAL)
55
#define CGL_UPLOAD_MATRIX(mat) mat
66
#else
77
#define CGL_UPLOAD_MATRIX(mat) mat.Transpose()

0 commit comments

Comments
 (0)