Files
Seele/src/Engine/Graphics/Vulkan/Shader.cpp
T

147 lines
5.0 KiB
C++
Raw Normal View History

2023-11-01 23:12:30 +01:00
#include "Shader.h"
#include "Graphics.h"
2020-06-02 11:46:18 +02:00
#include "slang.h"
#include "slang-com-ptr.h"
2021-10-15 23:12:29 +02:00
#include "stdlib.h"
2023-11-12 16:11:27 +01:00
#include <format>
2020-05-05 01:51:13 +02:00
using namespace Seele;
2020-06-02 11:46:18 +02:00
using namespace Seele::Vulkan;
Shader::Shader(PGraphics graphics, ShaderType shaderType, VkShaderStageFlags stage)
: graphics(graphics)
, type(shaderType)
, stage(stage)
{
}
Shader::~Shader()
{
if(module != VK_NULL_HANDLE)
{
vkDestroyShaderModule(graphics->getDevice(), module, nullptr);
}
}
uint32 Seele::Vulkan::Shader::getShaderHash() const
{
return hash;
}
2023-11-12 14:46:22 +01:00
#define CHECK_RESULT(x) {SlangResult r = x; if(r != 0) {throw std::runtime_error(std::format("Error: {0}", r));}}
#define CHECK_DIAGNOSTICS() {if(diagnostics) {std::cout << (const char*)diagnostics->getBufferPointer() << std::endl; assert(false);}}
2020-06-02 11:46:18 +02:00
void Shader::create(const ShaderCreateInfo& createInfo)
{
entryPointName = createInfo.entryPoint;
thread_local Slang::ComPtr<slang::IGlobalSession> globalSession;
if(!globalSession)
{
slang::createGlobalSession(globalSession.writeRef());
}
slang::SessionDesc sessionDesc;
sessionDesc.flags = 0;
sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
Array<slang::PreprocessorMacroDesc> macros;
2023-11-08 23:27:21 +01:00
for(const auto& [key, val] : createInfo.defines)
2020-09-19 14:36:50 +02:00
{
macros.add(slang::PreprocessorMacroDesc{
2023-11-08 23:27:21 +01:00
.name = key,
.value = val,
});
2020-09-19 14:36:50 +02:00
}
sessionDesc.preprocessorMacroCount = macros.size();
sessionDesc.preprocessorMacros = macros.data();
slang::TargetDesc vulkan;
2023-11-10 19:18:09 +01:00
vulkan.profile = globalSession->findProfile("sm_6_6");
vulkan.format = SLANG_SPIRV;
2023-12-02 10:55:00 +01:00
sessionDesc.targetCount = 1;
sessionDesc.targets = &vulkan;
StaticArray<const char*, 3> searchPaths = {"shaders/", "shaders/lib/", "shaders/generated/"};
sessionDesc.searchPaths = searchPaths.data();
sessionDesc.searchPathCount = searchPaths.size();
2020-06-02 11:46:18 +02:00
Slang::ComPtr<slang::ISession> session;
2023-11-12 14:46:22 +01:00
CHECK_RESULT(globalSession->createSession(sessionDesc, session.writeRef()));
Slang::ComPtr<slang::IBlob> diagnostics;
Array<slang::IComponentType*> modules;
Slang::ComPtr<slang::IEntryPoint> entrypoint;
2023-11-09 22:15:51 +01:00
slang::IModule* mainModule = nullptr;
for (const auto& moduleName : createInfo.additionalModules)
2020-06-02 11:46:18 +02:00
{
modules.add(session->loadModule(moduleName.c_str(), diagnostics.writeRef()));
2023-11-09 22:15:51 +01:00
if (moduleName == createInfo.mainModule)
{
mainModule = (slang::IModule*)modules.back();
}
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
2020-06-02 11:46:18 +02:00
}
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
mainModule->findEntryPointByName(createInfo.entryPoint.c_str(), entrypoint.writeRef());
modules.add(entrypoint);
2023-11-10 19:18:09 +01:00
Slang::ComPtr<slang::IComponentType> moduleComposition;
session->createCompositeComponentType(modules.data(), modules.size(), moduleComposition.writeRef(), diagnostics.writeRef());
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
Slang::ComPtr<slang::IComponentType> linkedProgram;
moduleComposition->link(linkedProgram.writeRef(), diagnostics.writeRef());
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
slang::ProgramLayout* reflection = linkedProgram->getLayout(0, diagnostics.writeRef());
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
Array<slang::SpecializationArg> specialization;
2023-11-10 19:18:09 +01:00
for(const auto& [key, value] : createInfo.typeParameter)
{
2023-11-10 19:18:09 +01:00
specialization.add(slang::SpecializationArg::fromType(reflection->findTypeByName(value)));
}
Slang::ComPtr<slang::IComponentType> specializedComponent;
linkedProgram->specialize(specialization.data(), specialization.size(), specializedComponent.writeRef(), diagnostics.writeRef());
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
Slang::ComPtr<slang::IBlob> kernelBlob;
specializedComponent->getEntryPointCode(
0,
0,
kernelBlob.writeRef(),
diagnostics.writeRef()
);
2023-11-12 14:46:22 +01:00
CHECK_DIAGNOSTICS();
2023-11-10 19:18:09 +01:00
for (uint32 i = 0; i < reflection->getParameterCount(); ++i)
{
slang::VariableLayoutReflection* varLayout = reflection->getParameterByIndex(i);
2023-11-12 14:46:22 +01:00
//std::cout << varLayout->getName() << " in space " << varLayout->getBindingSpace() << " index " << varLayout->getBindingIndex() << std::endl;
2023-11-10 19:18:09 +01:00
}
VkShaderModuleCreateInfo moduleInfo =
{
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.codeSize = kernelBlob->getBufferSize(),
.pCode = (uint32_t*)kernelBlob->getBufferPointer(),
};
2023-11-09 22:15:51 +01:00
VK_CHECK(vkCreateShaderModule(graphics->getDevice(), &moduleInfo, nullptr, &module));
2022-11-22 22:11:37 +01:00
hash = CRC::Calculate(entryPointName.data(), entryPointName.size(), CRC::CRC_32());
hash = CRC::Calculate(kernelBlob->getBufferPointer(), kernelBlob->getBufferSize(), CRC::CRC_32(), hash);
2023-12-02 10:55:00 +01:00
/*
2023-11-27 21:08:27 +01:00
specializedComponent->getEntryPointCode(
0,
1,
kernelBlob.writeRef(),
diagnostics.writeRef()
);
CHECK_DIAGNOSTICS();
std::ofstream shaderStream(createInfo.name + createInfo.entryPoint + ".glsl");
shaderStream << (char*)kernelBlob->getBufferPointer();
shaderStream.close();
2023-12-02 10:55:00 +01:00
*/
2020-06-02 11:46:18 +02:00
}