Skip to content

Commit

Permalink
Implemented ShaderCompiler for Metal
Browse files Browse the repository at this point in the history
  • Loading branch information
Thirulogeswaren committed Aug 14, 2024
1 parent 9d4e7ae commit 985b184
Show file tree
Hide file tree
Showing 28 changed files with 617 additions and 160 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
4 changes: 2 additions & 2 deletions Core/Application/Application.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <SDL2/SDL_events.h>
#include <Core/Common.h>
#include <Core/Graphics/Renderer.h>
#include <SDL2/SDL_events.h>

struct SDL_Window;

Expand Down Expand Up @@ -37,4 +37,4 @@ namespace CGL::Core
SDL_Window* m_window;
std::unique_ptr<Graphics::Renderer> m_renderer;
};
}
}
11 changes: 11 additions & 0 deletions Core/Common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#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 <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
#endif
5 changes: 4 additions & 1 deletion Core/External/SimpleMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,15 @@ namespace DirectX
x(ix), y(iy), width(iw), height(ih), minDepth(iminz), maxDepth(imaxz)
{
}
#if defined (CGL_PLATFORM_WINDOWS)
explicit Viewport(const RECT& rct) noexcept :
x(float(rct.left)), y(float(rct.top)),
width(float(rct.right - rct.left)),
height(float(rct.bottom - rct.top)),
minDepth(0.f), maxDepth(1.f)
{
}
#endif

#if defined(__d3d11_h__) || defined(__d3d11_x_h__)
// Direct3D 11 interop
Expand Down Expand Up @@ -984,9 +986,10 @@ namespace DirectX
bool operator != (const Viewport& vp) const noexcept;
#endif

#if defined (CGL_PLATFORM_WINDOWS)
// Assignment operators
Viewport& operator= (const RECT& rct) noexcept;

#endif
// Viewport operations
float AspectRatio() const noexcept;

Expand Down
2 changes: 2 additions & 0 deletions Core/External/SimpleMath.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3746,6 +3746,7 @@ inline bool Viewport::operator != (const Viewport& vp) const noexcept
// Assignment operators
//------------------------------------------------------------------------------

#if defined (CGL_PLATFORM_WINDOWS)
inline Viewport& Viewport::operator= (const RECT& rct) noexcept
{
x = float(rct.left); y = float(rct.top);
Expand All @@ -3754,6 +3755,7 @@ inline Viewport& Viewport::operator= (const RECT& rct) noexcept
minDepth = 0.f; maxDepth = 1.f;
return *this;
}
#endif

#if defined(__d3d11_h__) || defined(__d3d11_x_h__)
inline Viewport& Viewport::operator= (const D3D11_VIEWPORT& vp) noexcept
Expand Down
11 changes: 7 additions & 4 deletions Core/Graphics/Buffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

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

#if defined(CGL_RHI_DX11)
#include <Core/Graphics/RHI/D3D11/D3D11VertexBuffer.h>
Expand All @@ -19,8 +20,9 @@
#endif

#if defined(CGL_RHI_METAL)
#include <Core/Graphics/RHI/Metal/METALVertexBuffer.h>
#include <Core/Graphics/RHI/Metal/METALConstantBuffer.h>
#include <Core/Graphics/RHI/Metal/METALIndexBuffer.h>
#include <Core/Graphics/RHI/Metal/METALVertexBuffer.h>
#endif

#if defined(CGL_RHI_VULKAN)
Expand Down Expand Up @@ -50,12 +52,13 @@ namespace CGL::Graphics
using VertexBuffer = OPENGLVertexBuffer;
using IndexBuffer = OPENGLIndexBuffer;
#elif defined(CGL_RHI_METAL)
using VertexBuffer = METALVertexBuffer;
using IndexBuffer = METALIndexBuffer;
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
}
}
9 changes: 9 additions & 0 deletions Core/Graphics/RHI/METALCommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <Metal/Metal.hpp>

struct METALCompileObjects
{
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* cBuffer;
};
}
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* inxBuffer;

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

namespace CGL::Graphics {

CGL_DEFINE_LOG_CATEGORY(METALPipelineHandler);

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

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

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

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

NS::Error* ns_error{};

rpState = gpu_device->newRenderPipelineState(rpDescriptor, &ns_error);

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

CGL_LOG(METALPipelineHandler, Info, "RenderPipelineState Created");
rpDescriptor->release();
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 auto GetRenderPipelineDescriptor() const { return rpDescriptor; }
inline auto GetRenderPipelineState() const { return rpState; }

void CreateRenderPipelineState(MTL::Device* gpu_device);

private:
MTL::RenderPipelineDescriptor* rpDescriptor;
MTL::RenderPipelineState* 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
{
MTL::Library* cSourceContent;
MTL::Function* pShader;
};
}
Loading

0 comments on commit 985b184

Please sign in to comment.