Basic shader compilation

This commit is contained in:
Dynamitos
2024-04-11 12:38:42 +02:00
parent c3be0e23e7
commit 77dc9ef3fd
24 changed files with 254 additions and 148 deletions
+2 -1
View File
@@ -7,7 +7,8 @@ namespace Metal {
DECLARE_REF(Graphics)
class Buffer
{
public:
private:
};
DEFINE_REF(Buffer)
class VertexBuffer : public Gfx::VertexBuffer, public Buffer
+2
View File
@@ -17,6 +17,8 @@ target_sources(Engine
RenderPass.mm
Resources.h
Resources.mm
Shader.h
Shader.mm
Texture.h
Texture.mm
Window.h
+1 -6
View File
@@ -1,19 +1,14 @@
#pragma once
#include "Graphics/Command.h"
#include "Metal/MTLComputeCommandEncoder.hpp"
#include "Metal/MTLDrawable.hpp"
#include "Metal/MTLIOCommandQueue.hpp"
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "MinimalEngine.h"
#include "RenderPass.h"
#include "Resources.h"
#include "Graphics.h"
namespace Seele {
namespace Metal {
DECLARE_REF(CommandQueue)
DECLARE_REF(ComputeCommand)
DECLARE_REF(RenderCommand)
DECLARE_REF(Graphics)
class Command
{
public:
+1 -1
View File
@@ -1,5 +1,4 @@
#pragma once
#include "Graphics/Metal/Command.h"
#include "Metal/Metal.hpp"
#include "Graphics/Graphics.h"
@@ -8,6 +7,7 @@ namespace Seele
namespace Metal
{
DECLARE_REF(CommandQueue)
DECLARE_REF(IOCommandQueue)
class Graphics : public Gfx::Graphics
{
public:
+1 -2
View File
@@ -1,8 +1,7 @@
#pragma once
#include "Graphics/Initializer.h"
#include "Graphics/Pipeline.h"
#include "Metal/MTLComputePipeline.hpp"
#include "Metal/MTLRenderPipeline.hpp"
#include "Resources.h"
#include "MinimalEngine.h"
namespace Seele {
+2 -2
View File
@@ -1,12 +1,12 @@
#pragma once
#include "Graphics/RenderTarget.h"
#include "Graphics.h"
#include "Metal/MTLRenderPass.hpp"
#include "Resources.h"
namespace Seele
{
namespace Metal
{
DECLARE_REF(Graphics)
class RenderPass : public Gfx::RenderPass
{
public:
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include "Graphics/Shader.h"
#include "Resources.h"
namespace Seele {
namespace Metal {
class Shader {
public:
Shader(PGraphics graphics);
virtual ~Shader();
void create(const ShaderCreateInfo &createInfo);
constexpr MTL::Function *getFunction() const { return function; }
constexpr const char *getEntryPointName() const {
// SLang renames all entry points to main, so we dont need that
return "main"; // entryPointName.c_str();
}
uint32 getShaderHash() const;
private:
PGraphics graphics;
MTL::Library* library;
MTL::Function *function;
uint32 hash;
};
DEFINE_REF(Shader)
template <typename Base> class ShaderBase : public Base, public Shader {
public:
ShaderBase(PGraphics graphics) : Shader(graphics) {}
virtual ~ShaderBase() {}
};
using VertexShader = ShaderBase<Gfx::VertexShader>;
using FragmentShader = ShaderBase<Gfx::FragmentShader>;
using ComputeShader = ShaderBase<Gfx::ComputeShader>;
using TaskShader = ShaderBase<Gfx::TaskShader>;
using MeshShader = ShaderBase<Gfx::MeshShader>;
DEFINE_REF(VertexShader)
DEFINE_REF(FragmentShader)
DEFINE_REF(ComputeShader)
DEFINE_REF(TaskShader)
DEFINE_REF(MeshShader)
} // namespace Metal
} // namespace Seele
+59
View File
@@ -0,0 +1,59 @@
#include "Shader.h"
#include "Graphics.h"
#include "Graphics/slang-compile.h"
#include "Metal/MTLLibrary.hpp"
#include <slang.h>
using namespace Seele;
using namespace Seele::Metal;
Shader::Shader(PGraphics graphics) : graphics(graphics) {}
Shader::~Shader() {
if (function) {
function->release();
library->release();
}
}
void Shader::create(const ShaderCreateInfo &createInfo) {
Slang::ComPtr<slang::IBlob> kernelBlob = generateShader(createInfo, SLANG_DXIL);
thread_local IRCompiler* pCompiler = nullptr;
if(pCompiler == nullptr)
{
pCompiler = IRCompilerCreate();
}
IRCompilerSetEntryPointName(pCompiler, "main");
IRObject* pDXIL = IRObjectCreateFromDXIL(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), IRBytecodeOwnershipNone);
// Compile DXIL to Metal IR:
IRError* pError = nullptr;
IRObject* pOutIR = IRCompilerAllocCompileAndLink(pCompiler, NULL, pDXIL, &pError);
if (!pOutIR)
{
// Inspect pError to determine cause.
IRErrorDestroy( pError );
}
// Retrieve Metallib:
MetaLibBinary* pMetallib = IRMetalLibBinaryCreate();
IRObjectGetMetalLibBinary(pOutIR, stage, pMetallib);
size_t metallibSize = IRMetalLibGetBytecodeSize(pMetallib);
uint8_t* metallib = new uint8_t[metallibSize];
IRMetalLibGetBytecode(pMetallib, metallib);
// Store the metallib to custom format or disk, or use to create a MTLLibrary.
NS::Error* __autoreleasing error = nil;
dispatch_data_t data =
dispatch_data_create(metallib, metallibSize, dispatch_get_main_queue(), NULL);
library = graphics->getDevice()->newLibrary(data, &error);
function = library->newFunction(NS::String::string("main", NS::ASCIIStringEncoding));
delete [] metallib;
IRMetalLibBinaryDestroy(pMetallib);
IRObjectDestroy(pDXIL);
IRObjectDestroy(pOutIR);
IRCompilerDestroy(pCompiler);
}
-1
View File
@@ -1,7 +1,6 @@
#pragma once
#include "Graphics/Texture.h"
#include "Graphics.h"
#include "Metal/MTLTexture.hpp"
namespace Seele
{
+1 -4
View File
@@ -61,10 +61,7 @@ TextureBase::~TextureBase() {
void TextureBase::executePipelineBarrier(Gfx::SeAccessFlags,
Gfx::SePipelineStageFlags,
Gfx::SeAccessFlags,
Gfx::SePipelineStageFlags) {
}
Gfx::SePipelineStageFlags) {}
void TextureBase::changeLayout(Gfx::SeImageLayout, Gfx::SeAccessFlags,
Gfx::SePipelineStageFlags, Gfx::SeAccessFlags,
-1
View File
@@ -1,7 +1,6 @@
#pragma once
#include "Graphics.h"
#include "Graphics/Window.h"
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "Resources.h"
#include "Texture.h"