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

129 lines
3.6 KiB
C++
Raw Normal View History

2023-10-26 18:37:29 +02:00
#include "Shader.h"
2023-11-01 23:12:30 +01:00
#include "Graphics/RenderPass/DepthPrepass.h"
#include "Graphics/RenderPass/BasePass.h"
2023-10-26 18:37:29 +02:00
using namespace Seele;
using namespace Seele::Gfx;
2023-11-05 10:36:01 +01:00
ShaderCompiler::ShaderCompiler(Gfx::PGraphics graphics)
: graphics(graphics)
2023-10-26 18:37:29 +02:00
{
}
2023-11-05 10:36:01 +01:00
ShaderCompiler::~ShaderCompiler()
2023-10-26 18:37:29 +02:00
{
}
2023-11-05 10:36:01 +01:00
const ShaderCollection* ShaderCompiler::findShaders(PermutationId id) const
2023-10-26 18:37:29 +02:00
{
2023-11-05 10:36:01 +01:00
return &shaders[id];
}
void ShaderCompiler::registerMaterial(PMaterial material)
{
materials[material->getName()] = material;
compile();
}
void ShaderCompiler::registerVertexData(VertexData* vd)
{
vertexData[vd->getTypeName()] = vd;
compile();
}
void ShaderCompiler::registerRenderPass(std::string name, std::string mainFile, bool useMaterials, bool hasFragmentShader, std::string fragmentFile, bool useMeshShading, bool hasTaskShader, std::string taskFile)
{
passes[name] = PassConfig{
.taskFile = taskFile,
.mainFile = mainFile,
.fragmentFile = fragmentFile,
.hasFragmentShader = hasFragmentShader,
.useMeshShading = useMeshShading,
.hasTaskShader = hasTaskShader,
.useMaterial = useMaterials,
};
compile();
}
void ShaderCompiler::compile()
{
ShaderPermutation permutation;
for (const auto& [name, pass] : passes)
2023-10-26 18:37:29 +02:00
{
2023-11-05 10:36:01 +01:00
std::memset(&permutation, 0, sizeof(ShaderPermutation));
permutation = {
.hasFragment = pass.hasFragmentShader,
.useMeshShading = pass.useMeshShading,
.hasTaskShader = pass.hasTaskShader,
};
std::memcpy(permutation.vertexMeshFile, pass.mainFile.c_str(), sizeof(permutation.vertexMeshFile));
if (pass.hasFragmentShader)
2023-10-26 18:37:29 +02:00
{
2023-11-05 10:36:01 +01:00
std::memcpy(permutation.fragmentFile, pass.fragmentFile.c_str(), sizeof(permutation.fragmentFile));
}
if (pass.hasTaskShader)
{
std::memcpy(permutation.taskFile, pass.taskFile.c_str(), sizeof(permutation.taskFile));
}
for (const auto& [vdName, vd] : vertexData)
{
std::memcpy(permutation.vertexDataName, vd->getTypeName().c_str(), sizeof(permutation.vertexDataName));
if (pass.useMaterial)
{
for (const auto& [matName, mat] : materials)
{
std::memcpy(permutation.materialName, matName.c_str(), sizeof(permutation.materialName));
createShaders(permutation);
}
}
else
{
std::memset(permutation.materialName, 0, sizeof(permutation.materialName));
createShaders(permutation);
}
2023-10-26 18:37:29 +02:00
}
}
}
2023-11-05 10:36:01 +01:00
ShaderCollection& ShaderCompiler::createShaders(ShaderPermutation permutation)
2023-10-26 18:37:29 +02:00
{
std::scoped_lock lock(shadersLock);
2023-11-05 10:36:01 +01:00
ShaderCollection collection;
2023-10-26 18:37:29 +02:00
ShaderCreateInfo createInfo;
2023-11-05 10:36:01 +01:00
createInfo.typeParameter = { permutation.materialName, permutation.vertexDataName };
createInfo.name = std::format("Material {0}", permutation.materialName);
createInfo.additionalModules.add(permutation.materialName);
createInfo.additionalModules.add(permutation.vertexDataName);
2023-10-26 18:37:29 +02:00
2023-11-05 10:36:01 +01:00
if (permutation.useMeshShading)
2023-10-26 18:37:29 +02:00
{
2023-11-05 10:36:01 +01:00
if (permutation.hasTaskShader)
{
createInfo.mainModule = permutation.taskFile;
createInfo.entryPoint = "taskMain";
collection.taskShader = graphics->createTaskShader(createInfo);
}
createInfo.mainModule = permutation.vertexMeshFile;
createInfo.entryPoint = "meshMain";
collection.meshShader = graphics->createMeshShader(createInfo);
}
else
{
createInfo.mainModule = permutation.vertexMeshFile;
createInfo.entryPoint = "vertexMain";
collection.vertexShader = graphics->createVertexShader(createInfo);
}
2023-10-26 18:37:29 +02:00
2023-11-05 10:36:01 +01:00
if (permutation.hasFragment)
{
createInfo.mainModule = permutation.fragmentFile;
createInfo.entryPoint = "fragmentMain";
2023-10-26 18:37:29 +02:00
collection.fragmentShader = graphics->createFragmentShader(createInfo);
}
2023-11-05 10:36:01 +01:00
PermutationId perm = PermutationId(permutation);
shaders[perm] = std::move(collection);
2023-10-26 18:37:29 +02:00
2023-11-05 10:36:01 +01:00
return shaders[perm];
2023-10-26 18:37:29 +02:00
}