Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Index and Constant Buffers #55

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,12 @@ jobs:
- name: Update xmake repository
run: xmake repo --update

- name: Retrieve dependencies hash
id: dep_hash
run: echo "hash=$(xmake l utils.ci.packageskey)" >> $GITHUB_ENV
shell: bash

- name: Restore cached xmake dependencies
id: restore-depcache
uses: actions/cache/restore@v4
with:
path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages
key: ${{ matrix.rhi }}-${{ matrix.prj }}-${{ env.hash }}-W${{ env.key }}
key: ${{ matrix.rhi }}-${{ matrix.prj }}-W${{ env.key }}

- name: Check xmake packages directory exists
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions Core/Graphics/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <Core/Graphics/Types.h>
#include <Core/Types.h>

#include <typeindex>
ArnavMehta3000 marked this conversation as resolved.
Show resolved Hide resolved

#if defined(CGL_RHI_DX11)
#include <Core/Graphics/RHI/D3D11/D3D11VertexBuffer.h>
#include <Core/Graphics/RHI/D3D11/D3D11IndexBuffer.h>
Expand Down
5 changes: 3 additions & 2 deletions Core/Graphics/RHI/Metal/METALIndexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include "Foundation/NSTypes.hpp"
#include "Metal/MTLBuffer.hpp"
#include "Metal/MTLStageInputOutputDescriptor.hpp"

namespace CGL::Graphics
{
struct METALIndexBuffer
{
MTL::Buffer* Buffer;
MTL::IndexType Format;

NS::UInteger Offset;
NS::UInteger Index;
NS::UInteger IndicesCount;
};
}
17 changes: 15 additions & 2 deletions Core/Graphics/RHI/Metal/METALPipelineHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,27 @@ namespace CGL::Graphics {

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

if(!m_rpState)
{
if(!m_rpState)
{
CGL_LOG(METALPipelineHandler, Error, ns_error->localizedDescription()->utf8String());
return;
}

CGL_LOG(METALPipelineHandler, Info, "RenderPipelineState Created");

MTL::DepthStencilDescriptor* depthStencilDescriptor = MTL::DepthStencilDescriptor::alloc()->init();

depthStencilDescriptor->setDepthWriteEnabled(true);
depthStencilDescriptor->setDepthCompareFunction(MTL::CompareFunction::CompareFunctionAlways);

this->m_depthStencilState = gpu_device->newDepthStencilState(depthStencilDescriptor);

CGL_LOG(METALPipelineHandler, Info, "DepthStencilState Created");

m_rpDescriptor->release();
m_rpDescriptor = nullptr;

depthStencilDescriptor->release();
depthStencilDescriptor = nullptr;
}
}
11 changes: 10 additions & 1 deletion Core/Graphics/RHI/Metal/METALPipelineHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Metal/MTLDevice.hpp"
#include "Metal/MTLRenderPipeline.hpp"

#include "Core/Graphics/Buffer.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use <> for Core includes

#include "Core/Logging/Log.h"

namespace CGL::Graphics
Expand All @@ -17,10 +18,18 @@ namespace CGL::Graphics
inline MTL::RenderPipelineDescriptor* GetRenderPipelineDescriptor() const { return m_rpDescriptor; }
inline MTL::RenderPipelineState* GetRenderPipelineState() const { return m_rpState; }

void CreateRenderPipelineState(MTL::Device* gpu_device);
inline MTL::DepthStencilState* GetDepthStencilState() const { return m_depthStencilState; }

inline void SetIndexBuffer(IndexBuffer* indexBuffer) { m_indexBuffer = indexBuffer; }
inline METALIndexBuffer* GetIndexBuffer() const { return m_indexBuffer; }

void CreateRenderPipelineState(MTL::Device* gpu_device);
private:
MTL::RenderPipelineDescriptor* m_rpDescriptor;
MTL::RenderPipelineState* m_rpState;

MTL::DepthStencilState* m_depthStencilState;

METALIndexBuffer* m_indexBuffer;
};
}
44 changes: 39 additions & 5 deletions Core/Graphics/RHI/Metal/METALRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ namespace CGL::Graphics
void Renderer::SetRenderPipeline_METAL()
{
GetImpl()->GetRenderPipelineHandler()->CreateRenderPipelineState(GetImpl()->GetDevice());
GetImpl()->GetRenderCommandEncoder()->setRenderPipelineState(GetImpl()->GetRenderPipelineHandler()->GetRenderPipelineState());

const auto& rCommandEncoder = GetImpl()->GetRenderCommandEncoder();

rCommandEncoder->setFrontFacingWinding(MTL::WindingClockwise);
rCommandEncoder->setCullMode(MTL::CullModeBack);
rCommandEncoder->setTriangleFillMode(MTL::TriangleFillMode::TriangleFillModeFill);
rCommandEncoder->setRenderPipelineState(GetImpl()->GetRenderPipelineHandler()->GetRenderPipelineState());
rCommandEncoder->setDepthStencilState(GetImpl()->GetRenderPipelineHandler()->GetDepthStencilState());
}

void Renderer::SetPrimitiveTopology_METAL(PrimitiveType topology)
Expand Down Expand Up @@ -99,9 +106,19 @@ namespace CGL::Graphics

void Renderer::SetIndexBuffer_METAL(const IndexBuffer& buffer)
{
// TODO: implement index buffer
GetImpl()->GetRenderPipelineHandler()->SetIndexBuffer(&const_cast<IndexBuffer&>(buffer));
}

void Renderer::SetConstantBufferData_METAL(const MTL::Buffer* buffer, const void* data, size_t size)
{
memcpy(const_cast<MTL::Buffer*>(buffer)->contents(), data, size);
}

void Renderer::SetConstantBuffer_METAL([[maybe_unused]] ShaderType type, [[maybe_unused]] u32 startSlot, const MTL::Buffer* buffer)
{
GetImpl()->GetRenderCommandEncoder()->setVertexBuffer(buffer, 0, 1);
}

ShaderCompileResult Renderer::CompileVertexShader_METAL(const ShaderSource& source, VertexShader* outShader)
{
assert(GetImpl() && GetImpl()->GetDevice() && outShader);
Expand Down Expand Up @@ -168,7 +185,20 @@ namespace CGL::Graphics

IndexBuffer Renderer::CreateIndexBuffer_METAL(const BufferSource& source)
{
// TODO: implement Index Buffer
assert(GetImpl() && GetImpl()->GetDevice());

const u32 sourceSZ = source.TypeSize * source.Count;

return IndexBuffer {
.Buffer = GetImpl()->GetDevice()->newBuffer(source.Data, sourceSZ, MTL::ResourceStorageModeShared),
.Format = (source.TypeSize == sizeof(u16) ? MTL::IndexTypeUInt16 : MTL::IndexTypeUInt32),
.IndicesCount = source.Count
};
}

void Renderer::CreateConstantBuffer_METAL(const BufferSource& source, MTL::Buffer** outBuffer)
{
*outBuffer = GetImpl()->GetDevice()->newBuffer(source.TypeSize, MTL::ResourceStorageModeShared);
}

void Renderer::Draw_METAL(u32 vertexCount, u32 startVertex)
Expand All @@ -178,8 +208,12 @@ namespace CGL::Graphics
);
}

void Renderer::DrawIndexed_METAL(u32 indexCount, u32 startIndex, u32 baseVertex)
void Renderer::DrawIndexed_METAL(u32 indexCount, [[maybe_unused]] u32 startIndex, [[maybe_unused]] u32 baseVertex)
{
// TODO: implement indexed drawcall
const METALIndexBuffer* IndexBuffer = GetImpl()->GetRenderPipelineHandler()->GetIndexBuffer();

GetImpl()->GetRenderCommandEncoder()->drawIndexedPrimitives(
GetImpl()->GetPrimitiveType(), indexCount, IndexBuffer->Format, IndexBuffer->Buffer, 0
);
}
}
3 changes: 3 additions & 0 deletions Core/Graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ namespace CGL::Graphics
ShaderCompileResult CompilePixelShader_METAL(const ShaderSource& source, PixelShader* outShader);
VertexBuffer CreateVertexBuffer_METAL(const BufferSource& source);
IndexBuffer CreateIndexBuffer_METAL(const BufferSource& source);
void CreateConstantBuffer_METAL(const BufferSource& source, MTL::Buffer** outBuffer);
void SetConstantBufferData_METAL(const MTL::Buffer* buffer, const void* data, size_t size);
void SetConstantBuffer_METAL(ShaderType type, u32 startSlot, const MTL::Buffer* buffer);
void Draw_METAL(u32 vertexCount, u32 startVertex = 0);
void DrawIndexed_METAL(u32 indexCount, u32 startIndex = 0, u32 baseVertex = 0);
METALRendererImpl* GetImpl() const;
Expand Down
20 changes: 13 additions & 7 deletions Core/Graphics/Renderer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@

namespace CGL::Graphics
{
template <typename T>
template <typename T>
void Renderer::CreateContantBuffer(const BufferSource& source, ConstantBuffer<T>& outBuffer)
{
#if defined(CGL_RHI_DX11)
CreateContantBuffer_D3D11(source, outBuffer.Buffer);
#elif defined(CGL_RHI_OPENGL)
CreateConstantBuffer_OPENGL(source, outBuffer.Buffer);
#elif defined(CGL_RHI_METAL)
CreateConstantBuffer_METAL(source, &outBuffer.Buffer);
#endif
}

template <typename T>
template <typename T>
void Renderer::SetConstantBufferData(const ConstantBuffer<T>& buffer, const T& data)
{
#if defined(CGL_RHI_DX11)
SetConstantBufferData_D3D11(buffer.Buffer.Get(), static_cast<const void*>(&data), sizeof(T));
#elif defined(CGL_RHI_OPENGL)
SetConstantBufferData_OPENGL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
#endif
SetConstantBufferData_OPENGL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
#elif defined(CGL_RHI_METAL)
SetConstantBufferData_METAL(buffer.Buffer, static_cast<const void*>(&data), sizeof(T));
#endif
}
template <typename T>
template <typename T>
void Renderer::SetConstantBuffer(ShaderType shaderType, u32 startSlot, const ConstantBuffer<T>& buffer)
{
#if defined(CGL_RHI_DX11)
SetContantBuffer_D3D11(shaderType, startSlot, buffer.Buffer);
#elif defined(CGL_RHI_OPENGL)
SetConstantBuffer_OPENGL(shaderType, startSlot, buffer.Buffer);
#endif
#elif defined(CGL_RHI_METAL)
SetConstantBuffer_METAL(shaderType, startSlot, buffer.Buffer);
#endif
}
}
}
12 changes: 12 additions & 0 deletions Samples/SpinningCube/Assets/Metal/SpinningCubePS.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <metal_stdlib>
using namespace metal;

struct VertexOut {
float4 position [[position]];
float4 color;
};

fragment float4 SpinningCubePS(VertexOut in [[stage_in]])
{
return in.color;
}
30 changes: 30 additions & 0 deletions Samples/SpinningCube/Assets/Metal/SpinningCubeVS.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <metal_stdlib>
using namespace metal;

struct TransformationData
{
float4x4 modelMatrix;
float4x4 viewMatrix;
float4x4 perspectiveMatrix;
};

struct VertexOut
{
float4 position [[position]];
float4 color;
};

vertex VertexOut SpinningCubeVS(uint vertexID [[vertex_id]], constant VertexOut* vertexData, constant TransformationData* transformationData)
{
VertexOut out;

float4 vertexPosition = vertexData[vertexID].position;

float4x4 ModelView = transformationData->viewMatrix * transformationData->modelMatrix;
float4x4 ModelViewProjection = transformationData->perspectiveMatrix * ModelView;

out.position = ModelViewProjection * vertexPosition;
out.color = vertexData[vertexID].color;

return out;
}
2 changes: 1 addition & 1 deletion Samples/SpinningCube/SpinningCube.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "SpinningCube.h"
#include <Core/Application/AssetFinder.h>

#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN)
#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN) || defined(CGL_RHI_METAL)
#define CGL_UPLOAD_MATRIX(mat) mat
#else
#define CGL_UPLOAD_MATRIX(mat) mat.Transpose()
Expand Down
Loading