2024-04-11 12:38:42 +02:00
|
|
|
#include "Shader.h"
|
2024-04-13 23:51:38 +02:00
|
|
|
#include "Foundation/NSError.hpp"
|
|
|
|
|
#include "Foundation/NSString.hpp"
|
2024-04-11 12:38:42 +02:00
|
|
|
#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"
|
2024-04-13 23:51:38 +02:00
|
|
|
#include "Metal/MTLDevice.hpp"
|
|
|
|
|
#include "Metal/MTLLibrary.hpp"
|
2024-04-19 18:23:36 +02:00
|
|
|
#include "Descriptor.h"
|
2024-04-15 13:48:34 +02:00
|
|
|
#include <fstream>
|
2024-04-12 09:27:30 +02:00
|
|
|
#include <iostream>
|
2024-04-17 14:33:06 +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-14 11:35:37 +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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 11:35:37 +02:00
|
|
|
void Shader::create(const ShaderCreateInfo& createInfo) {
|
2024-04-19 18:23:36 +02:00
|
|
|
Map<std::string, uint32> test;
|
|
|
|
|
Slang::ComPtr<slang::IBlob> kernelBlob = generateShader(createInfo, SLANG_DXIL, test);
|
2024-04-17 14:33:06 +02:00
|
|
|
hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32());
|
|
|
|
|
IRCompiler* pCompiler = IRCompilerCreate();
|
2024-04-19 18:23:36 +02:00
|
|
|
IRCompilerSetMinimumGPUFamily(pCompiler, IRGPUFamilyMetal3);
|
|
|
|
|
IRCompilerIgnoreRootSignature(pCompiler, true);
|
2024-04-17 14:33:06 +02:00
|
|
|
IRCompilerSetEntryPointName(pCompiler, "main");
|
|
|
|
|
|
|
|
|
|
IRObject* pDXIL = IRObjectCreateFromDXIL((const uint8*)kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(),
|
|
|
|
|
IRBytecodeOwnershipNone);
|
2024-04-19 18:23:36 +02:00
|
|
|
IRVersionedRootSignatureDescriptor descriptor;
|
|
|
|
|
descriptor.version = IRRootSignatureVersion_1_1;
|
|
|
|
|
Map<std::string, Gfx::PDescriptorLayout> layouts =createInfo.rootSignature->getLayouts();
|
|
|
|
|
descriptor.desc_1_1.NumParameters = layouts.size();
|
|
|
|
|
descriptor.desc_1_1.NumStaticSamplers = 0;
|
|
|
|
|
descriptor.desc_1_1.pStaticSamplers = nullptr;
|
|
|
|
|
Array<IRRootParameter1> parameters;
|
|
|
|
|
// each layout has an array for its bindings
|
|
|
|
|
Array<Array<IRDescriptorRange1>> ranges;
|
|
|
|
|
for(const auto& [name, layout] : layouts)
|
|
|
|
|
{
|
|
|
|
|
uint32 textureReg = 0; // SRV
|
|
|
|
|
uint32 samplerReg = 0; // Sampler
|
|
|
|
|
uint32 uavReg = 0; // UAV
|
|
|
|
|
uint32 constantReg = 0; // CBV
|
|
|
|
|
auto& bindingRanges = ranges.add();
|
|
|
|
|
for(auto binding : layout->getBindings())
|
|
|
|
|
{
|
|
|
|
|
IRDescriptorRangeType type;
|
|
|
|
|
uint32 reg = 0;
|
|
|
|
|
switch(binding.descriptorType)
|
|
|
|
|
{
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER:
|
|
|
|
|
type = IRDescriptorRangeTypeSRV;
|
|
|
|
|
reg = textureReg++;
|
|
|
|
|
break;
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
|
|
|
|
|
type = IRDescriptorRangeTypeCBV;
|
|
|
|
|
reg = constantReg++;
|
|
|
|
|
break;
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
|
|
|
|
|
case Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE:
|
|
|
|
|
type = IRDescriptorRangeTypeUAV;
|
|
|
|
|
reg = uavReg++;
|
|
|
|
|
break;
|
|
|
|
|
default: throw std::logic_error("Not implemented");
|
|
|
|
|
};
|
|
|
|
|
bindingRanges.add() = IRDescriptorRange1{
|
|
|
|
|
.BaseShaderRegister = reg,
|
|
|
|
|
.NumDescriptors = binding.descriptorCount,
|
|
|
|
|
.RangeType = type,
|
|
|
|
|
.RegisterSpace = createInfo.rootSignature->findParameter(name),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
parameters.add() = IRRootParameter1{
|
|
|
|
|
.ParameterType = IRRootParameterTypeDescriptorTable,
|
|
|
|
|
.DescriptorTable = IRRootDescriptorTable1{ .NumDescriptorRanges = (uint32)(bindingRanges.size()), bindingRanges.data() },
|
|
|
|
|
.ShaderVisibility = IRShaderVisibilityAll,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
IRError* signatureError = nullptr;
|
|
|
|
|
descriptor.desc_1_1.NumParameters = parameters.size();
|
|
|
|
|
descriptor.desc_1_1.pParameters = parameters.data();
|
|
|
|
|
IRRootSignature* rootSignature = IRRootSignatureCreateFromDescriptor(&descriptor, &signatureError);
|
|
|
|
|
assert(rootSignature);
|
|
|
|
|
IRCompilerSetGlobalRootSignature(pCompiler, rootSignature);
|
|
|
|
|
// Compile DXIL to Metal IR:
|
2024-04-17 14:33:06 +02:00
|
|
|
IRError* pError = nullptr;
|
|
|
|
|
IRObject* pOutIR = IRCompilerAllocCompileAndLink(pCompiler, NULL, pDXIL, &pError);
|
|
|
|
|
|
|
|
|
|
if (!pOutIR) {
|
|
|
|
|
// Inspect pError to determine cause.
|
|
|
|
|
IRErrorDestroy(pError);
|
2024-04-11 12:38:42 +02:00
|
|
|
}
|
2024-04-17 14:33:06 +02:00
|
|
|
IRShaderStage irStage;
|
|
|
|
|
switch (stage) {
|
|
|
|
|
case Gfx::SE_SHADER_STAGE_VERTEX_BIT:
|
|
|
|
|
irStage = IRShaderStageVertex;
|
|
|
|
|
break;
|
|
|
|
|
case Gfx::SE_SHADER_STAGE_FRAGMENT_BIT:
|
|
|
|
|
irStage = IRShaderStageFragment;
|
|
|
|
|
break;
|
|
|
|
|
case Gfx::SE_SHADER_STAGE_COMPUTE_BIT:
|
|
|
|
|
irStage = IRShaderStageCompute;
|
|
|
|
|
break;
|
|
|
|
|
case Gfx::SE_SHADER_STAGE_TASK_BIT_EXT:
|
|
|
|
|
irStage = IRShaderStageAmplification;
|
|
|
|
|
break;
|
|
|
|
|
case Gfx::SE_SHADER_STAGE_MESH_BIT_EXT:
|
|
|
|
|
irStage = IRShaderStageMesh;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Retrieve Metallib:
|
|
|
|
|
IRMetalLibBinary* pMetallib = IRMetalLibBinaryCreate();
|
|
|
|
|
IRObjectGetMetalLibBinary(pOutIR, irStage, pMetallib);
|
|
|
|
|
dispatch_data_t data = IRMetalLibGetBytecodeData(pMetallib);
|
2024-04-19 18:23:36 +02:00
|
|
|
|
|
|
|
|
IRShaderReflection* reflection = IRShaderReflectionCreate();
|
|
|
|
|
IRObjectGetReflection(pOutIR, irStage, reflection);
|
|
|
|
|
std::cout << " NumRes: " << IRShaderReflectionGetResourceCount(reflection) << std::endl;
|
|
|
|
|
std::cout << IRShaderReflectionAllocStringAndSerialize(reflection) << std::endl;
|
|
|
|
|
IRShaderReflectionDestroy(reflection);
|
|
|
|
|
|
2024-04-17 14:33:06 +02:00
|
|
|
// Store the metallib to custom format or disk, or use to create a MTLLibrary.
|
|
|
|
|
NS::Error* error;
|
|
|
|
|
library = graphics->getDevice()->newLibrary(data, &error);
|
|
|
|
|
function = library->newFunction(NS::String::string("main", NS::ASCIIStringEncoding));
|
2024-04-15 13:48:34 +02:00
|
|
|
if(!function)
|
|
|
|
|
{
|
2024-04-17 14:33:06 +02:00
|
|
|
assert(false);
|
2024-04-15 13:48:34 +02:00
|
|
|
}
|
2024-04-17 14:33:06 +02:00
|
|
|
IRMetalLibBinaryDestroy(pMetallib);
|
|
|
|
|
IRObjectDestroy(pDXIL);
|
|
|
|
|
IRObjectDestroy(pOutIR);
|
|
|
|
|
IRCompilerDestroy(pCompiler);
|
2024-04-14 11:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-19 18:23:36 +02:00
|
|
|
uint32 Shader::getShaderHash() const { return hash; }
|