Files
Seele/src/Engine/Graphics/Metal/Shader.mm
T

72 lines
2.4 KiB
Plaintext
Raw Normal View History

2024-04-11 12:38:42 +02:00
#include "Shader.h"
#include "Graphics.h"
2024-04-11 18:51:47 +02:00
#include "Graphics/Enums.h"
2024-04-11 12:38:42 +02:00
#include "Graphics/slang-compile.h"
#include "Metal/MTLLibrary.hpp"
2024-04-11 18:51:47 +02:00
#include "metal_irconverter/metal_irconverter.h"
2024-04-11 12:38:42 +02:00
#include <slang.h>
using namespace Seele;
using namespace Seele::Metal;
2024-04-11 18:51:47 +02:00
Shader::Shader(PGraphics graphics, Gfx::SeShaderStageFlags stage)
: stage(stage), graphics(graphics) {}
2024-04-11 12:38:42 +02:00
Shader::~Shader() {
if (function) {
function->release();
library->release();
}
}
void Shader::create(const ShaderCreateInfo &createInfo) {
2024-04-11 18:51:47 +02:00
Slang::ComPtr<slang::IBlob> kernelBlob =
generateShader(createInfo, SLANG_DXIL);
thread_local IRCompiler *pCompiler = nullptr;
if (pCompiler == nullptr) {
2024-04-11 12:38:42 +02:00
pCompiler = IRCompilerCreate();
}
IRCompilerSetEntryPointName(pCompiler, "main");
2024-04-11 18:51:47 +02:00
IRObject *pDXIL = IRObjectCreateFromDXIL(
(const uint8 *)kernelBlob->getBufferPointer(),
kernelBlob->getBufferSize(), IRBytecodeOwnershipNone);
2024-04-11 12:38:42 +02:00
// Compile DXIL to Metal IR:
2024-04-11 18:51:47 +02:00
IRError *pError = nullptr;
IRObject *pOutIR =
IRCompilerAllocCompileAndLink(pCompiler, NULL, pDXIL, &pError);
2024-04-11 12:38:42 +02:00
2024-04-11 18:51:47 +02:00
if (!pOutIR) {
2024-04-11 12:38:42 +02:00
// Inspect pError to determine cause.
2024-04-11 18:51:47 +02:00
IRErrorDestroy(pError);
}
IRShaderStage irStage;
switch (stage) {
case Gfx::SE_SHADER_STAGE_VERTEX_BIT: irStage = IRShaderStageVertex;
case Gfx::SE_SHADER_STAGE_FRAGMENT_BIT: irStage = IRShaderStageFragment;
case Gfx::SE_SHADER_STAGE_COMPUTE_BIT: irStage = IRShaderStageCompute;
case Gfx::SE_SHADER_STAGE_TASK_BIT_NV: irStage = IRShaderStageAmplification;
case Gfx::SE_SHADER_STAGE_MESH_BIT_NV: irStage = IRShaderStageMesh;
2024-04-11 12:38:42 +02:00
}
// Retrieve Metallib:
2024-04-11 18:51:47 +02:00
IRMetalLibBinary *pMetallib = IRMetalLibBinaryCreate();
IRObjectGetMetalLibBinary(pOutIR, irStage, pMetallib);
2024-04-11 12:38:42 +02:00
size_t metallibSize = IRMetalLibGetBytecodeSize(pMetallib);
2024-04-11 18:51:47 +02:00
uint8_t *metallib = new uint8_t[metallibSize];
2024-04-11 12:38:42 +02:00
IRMetalLibGetBytecode(pMetallib, metallib);
// Store the metallib to custom format or disk, or use to create a MTLLibrary.
2024-04-11 18:51:47 +02:00
NS::Error *__autoreleasing error = nil;
dispatch_data_t data = dispatch_data_create(metallib, metallibSize,
dispatch_get_main_queue(), NULL);
2024-04-11 12:38:42 +02:00
library = graphics->getDevice()->newLibrary(data, &error);
2024-04-11 18:51:47 +02:00
function =
library->newFunction(NS::String::string("main", NS::ASCIIStringEncoding));
delete[] metallib;
2024-04-11 12:38:42 +02:00
IRMetalLibBinaryDestroy(pMetallib);
IRObjectDestroy(pDXIL);
IRObjectDestroy(pOutIR);
IRCompilerDestroy(pCompiler);
}