Skip to content

Commit

Permalink
Implemented ShaderCompiler for Metal (#39)
Browse files Browse the repository at this point in the history
* Implemented ShaderCompiler for Metal

* changes for GLSL & HLSL shaders
  • Loading branch information
Thirulogeswaren authored Aug 22, 2024
1 parent 3f9d317 commit 88a4e28
Show file tree
Hide file tree
Showing 26 changed files with 525 additions and 100 deletions.
5 changes: 2 additions & 3 deletions Core/Application/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Application.h"
#include <SDL_scancode.h>
#include <chrono>
#include <SDL2/SDL.h>

Expand All @@ -10,8 +9,8 @@ namespace CGL::Core
bool g_isTestMode{ false };

Application::Application(std::string_view name, i32 argc, char** argv)
: m_name(name)
, m_isRunning(true)
: m_isRunning(true)
, m_name(name)
, m_window(nullptr)
{
// Parse command line arguments
Expand Down
10 changes: 10 additions & 0 deletions Core/Common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#if defined (CGL_RHI_METAL)
#define NS_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION

#define EXCLUDE_STDHEADERS
#include <Core/Common.h>

#endif
9 changes: 8 additions & 1 deletion Core/Common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#ifndef EXCLUDE_STDHEADERS
#include <cassert>
#include <string>
#include <string_view>
Expand All @@ -12,4 +13,10 @@
#include <utility>

#include <Core/Types.h>
#include <Core/Math/Math.h>
#endif

#if defined (CGL_RHI_METAL)
#include "Foundation/Foundation.hpp"
#include "QuartzCore/QuartzCore.hpp"
#include "Metal/Metal.hpp"
#endif
6 changes: 4 additions & 2 deletions Core/Graphics/Buffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Core/Graphics/Buffer.h>
#include <Core/Graphics/Types.h>
#include <Core/Types.h>

#if defined(CGL_RHI_DX11)
Expand All @@ -23,6 +23,7 @@
#if defined(CGL_RHI_METAL)
#include <Core/Graphics/RHI/Metal/METALVertexBuffer.h>
#include <Core/Graphics/RHI/Metal/METALIndexBuffer.h>
#include <Core/Graphics/RHI/Metal/METALConstantBuffer.h>
#endif

#if defined(CGL_RHI_VULKAN)
Expand Down Expand Up @@ -55,10 +56,11 @@ namespace CGL::Graphics
#elif defined(CGL_RHI_METAL)
using VertexBuffer = METALVertexBuffer;
using IndexBuffer = METALIndexBuffer;
template <typename T> using ConstantBuffer = METALConstantBuffer<T>;
#elif defined(CGL_RHI_VULKAN)
using VertexBuffer = VULKANVertexBuffer;
using IndexBuffer = VULKANIndexBuffer;
#else
#error Unsupported buffer types for RHI
#endif
}
}
11 changes: 11 additions & 0 deletions Core/Graphics/RHI/METALCommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "Metal/MTLLibrary.hpp"
#include <Metal/Metal.hpp>
#include <memory>

struct METALCompileObjects
{
std::unique_ptr<MTL::Library*>& library;
MTL::Device* device;
};
19 changes: 19 additions & 0 deletions Core/Graphics/RHI/Metal/METALConstantBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "Metal/MTLBuffer.hpp"

namespace CGL::Graphics
{
template <class T>
struct METALConstantBuffer
{
using value_type = T;

METALConstantBuffer()
{
static_assert((sizeof(T) % 16) == 0, "Constant buffer size must be 16-byte aligned.");
}

MTL::Buffer* Buffer;
};
}
15 changes: 15 additions & 0 deletions Core/Graphics/RHI/Metal/METALIndexBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

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

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

NS::UInteger Offset;
NS::UInteger Index;
};
}
36 changes: 36 additions & 0 deletions Core/Graphics/RHI/Metal/METALPipelineHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "METALPipelineHandler.h"
#include "Foundation/NSError.hpp"
#include "Foundation/NSString.hpp"

namespace CGL::Graphics {

CGL_DEFINE_LOG_CATEGORY(METALPipelineHandler);

METALPipelineHandler::METALPipelineHandler() : m_rpDescriptor { nullptr }, m_rpState { nullptr }
{
m_rpDescriptor = MTL::RenderPipelineDescriptor::alloc()->init();

m_rpDescriptor->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatRGBA8Unorm_sRGB);

CGL_LOG(METALPipelineHandler, Info, "RenderPipelineDescriptor Initialized");
}

void METALPipelineHandler::CreateRenderPipelineState(MTL::Device* gpu_device)
{
if(!m_rpDescriptor) return;

NS::Error* ns_error{};

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

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

CGL_LOG(METALPipelineHandler, Info, "RenderPipelineState Created");
m_rpDescriptor->release();
m_rpDescriptor = nullptr;
}
}
26 changes: 26 additions & 0 deletions Core/Graphics/RHI/Metal/METALPipelineHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "Metal/MTLDevice.hpp"
#include "Metal/MTLRenderPipeline.hpp"

#include "Core/Logging/Log.h"

namespace CGL::Graphics
{
CGL_DECLARE_LOG_CATEGORY(METALPipelineHandler);

class METALPipelineHandler
{
public:
METALPipelineHandler();

inline MTL::RenderPipelineDescriptor* GetRenderPipelineDescriptor() const { return m_rpDescriptor; }
inline MTL::RenderPipelineState* GetRenderPipelineState() const { return m_rpState; }

void CreateRenderPipelineState(MTL::Device* gpu_device);

private:
MTL::RenderPipelineDescriptor* m_rpDescriptor;
MTL::RenderPipelineState* m_rpState;
};
}
12 changes: 12 additions & 0 deletions Core/Graphics/RHI/Metal/METALPixelShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "Metal/MTLLibrary.hpp"

namespace CGL::Graphics
{
struct METALPixelShader
{
std::unique_ptr<MTL::Library*> SourceContent;
MTL::Function* Shader;
};
}
Loading

0 comments on commit 88a4e28

Please sign in to comment.