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"
|
2022-11-15 12:19:11 +01:00
|
|
|
#include "slang-com-ptr.h"
|
2021-10-15 23:12:29 +02:00
|
|
|
#include "stdlib.h"
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 18:45:35 +01:00
|
|
|
uint32 Seele::Vulkan::Shader::getShaderHash() const
|
|
|
|
|
{
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
void Shader::create(const ShaderCreateInfo& createInfo)
|
|
|
|
|
{
|
|
|
|
|
entryPointName = createInfo.entryPoint;
|
2022-11-15 12:19:11 +01:00
|
|
|
thread_local Slang::ComPtr<slang::IGlobalSession> globalSession;
|
|
|
|
|
if(!globalSession)
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
slang::createGlobalSession(globalSession.writeRef());
|
2020-08-06 00:54:43 +02:00
|
|
|
}
|
2022-11-15 12:19:11 +01:00
|
|
|
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
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
macros.add(slang::PreprocessorMacroDesc{
|
2023-11-08 23:27:21 +01:00
|
|
|
.name = key,
|
|
|
|
|
.value = val,
|
2022-11-15 12:19:11 +01:00
|
|
|
});
|
2020-09-19 14:36:50 +02:00
|
|
|
}
|
2022-11-15 12:19:11 +01:00
|
|
|
sessionDesc.preprocessorMacroCount = macros.size();
|
|
|
|
|
sessionDesc.preprocessorMacros = macros.data();
|
|
|
|
|
slang::TargetDesc vulkan;
|
|
|
|
|
vulkan.profile = globalSession->findProfile("glsl_vk");
|
|
|
|
|
vulkan.format = SLANG_SPIRV;
|
|
|
|
|
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
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
Slang::ComPtr<slang::ISession> session;
|
|
|
|
|
globalSession->createSession(sessionDesc, session.writeRef());
|
|
|
|
|
|
|
|
|
|
Slang::ComPtr<slang::IBlob> diagnostics;
|
|
|
|
|
Array<slang::IComponentType*> modules;
|
|
|
|
|
Slang::ComPtr<slang::IEntryPoint> entrypoint;
|
2020-06-02 11:46:18 +02:00
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
for (auto moduleName : createInfo.additionalModules)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
modules.add(session->loadModule(moduleName.c_str(), diagnostics.writeRef()));
|
|
|
|
|
if(diagnostics)
|
2021-10-16 12:59:11 +02:00
|
|
|
{
|
2022-11-15 12:19:11 +01:00
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
2021-10-16 12:59:11 +02:00
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2022-11-15 12:19:11 +01:00
|
|
|
slang::IModule* mainModule = session->loadModule(createInfo.mainModule.c_str(), diagnostics.writeRef());
|
|
|
|
|
modules.add(mainModule);
|
|
|
|
|
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mainModule->findEntryPointByName(createInfo.entryPoint.c_str(), entrypoint.writeRef());
|
|
|
|
|
modules.add(entrypoint);
|
|
|
|
|
|
|
|
|
|
slang::IComponentType* moduleComposition;
|
|
|
|
|
session->createCompositeComponentType(modules.data(), modules.size(), &moduleComposition, diagnostics.writeRef());
|
|
|
|
|
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 23:27:21 +01:00
|
|
|
/*slang::ProgramLayout* layout = moduleComposition->getLayout();
|
|
|
|
|
for(auto typeParam : createInfo.typeParameter)
|
2022-11-15 12:19:11 +01:00
|
|
|
{
|
|
|
|
|
Slang::ComPtr<slang::ITypeConformance> typeConformance;
|
2023-11-08 23:27:21 +01:00
|
|
|
session->createTypeConformanceComponentType(layout->findTypeByName(typeParam), layout->findTypeByName("IMaterial"), typeConformance.writeRef(), -1, diagnostics.writeRef());
|
2022-11-15 12:19:11 +01:00
|
|
|
modules.add(typeConformance);
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Slang::ComPtr<slang::IComponentType> conformingModule;
|
|
|
|
|
session->createCompositeComponentType(modules.data(), modules.size(), conformingModule.writeRef(), diagnostics.writeRef());
|
2023-11-08 23:27:21 +01:00
|
|
|
*/
|
2022-11-15 12:19:11 +01:00
|
|
|
Slang::ComPtr<slang::IComponentType> linkedProgram;
|
|
|
|
|
moduleComposition->link(linkedProgram.writeRef(), diagnostics.writeRef());
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slang::ProgramLayout* reflection = linkedProgram->getLayout(0, diagnostics.writeRef());
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array<slang::SpecializationArg> specialization;
|
|
|
|
|
for(auto typeArg : createInfo.typeParameter)
|
|
|
|
|
{
|
|
|
|
|
specialization.add(slang::SpecializationArg::fromType(reflection->findTypeByName(typeArg)));
|
|
|
|
|
}
|
|
|
|
|
Slang::ComPtr<slang::IComponentType> specializedComponent;
|
|
|
|
|
linkedProgram->specialize(specialization.data(), specialization.size(), specializedComponent.writeRef(), diagnostics.writeRef());
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
Slang::ComPtr<slang::IBlob> kernelBlob;
|
|
|
|
|
specializedComponent->getEntryPointCode(
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
kernelBlob.writeRef(),
|
|
|
|
|
diagnostics.writeRef()
|
|
|
|
|
);
|
|
|
|
|
if(diagnostics)
|
|
|
|
|
{
|
|
|
|
|
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
|
|
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
|
|
|
|
|
VkShaderModuleCreateInfo moduleInfo;
|
|
|
|
|
moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
|
|
|
moduleInfo.pNext = nullptr;
|
|
|
|
|
moduleInfo.flags = 0;
|
2022-11-15 12:19:11 +01:00
|
|
|
moduleInfo.codeSize = kernelBlob->getBufferSize();
|
|
|
|
|
moduleInfo.pCode = (uint32_t*)kernelBlob->getBufferPointer();
|
2020-06-02 11:46:18 +02:00
|
|
|
VK_CHECK(vkCreateShaderModule(graphics->getDevice(), &moduleInfo, nullptr, &module));
|
2020-10-30 18:45:35 +01:00
|
|
|
|
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);
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|