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

147 lines
5.6 KiB
C++
Raw Normal View History

2023-10-26 18:37:29 +02:00
#include "Shader.h"
2024-06-19 10:33:19 +02:00
#include "Graphics/Graphics.h"
#include "Graphics/Initializer.h"
2024-06-11 14:15:29 +02:00
#include "Material/Material.h"
2024-06-09 12:20:04 +02:00
#include "ThreadPool.h"
2024-01-16 19:24:49 +01:00
#include <fmt/core.h>
2023-10-26 18:37:29 +02:00
using namespace Seele;
using namespace Seele::Gfx;
2024-06-09 12:20:04 +02:00
ShaderCompiler::ShaderCompiler(Gfx::PGraphics graphics) : graphics(graphics) {}
2023-10-26 18:37:29 +02:00
2024-06-09 12:20:04 +02:00
ShaderCompiler::~ShaderCompiler() {}
2023-10-26 18:37:29 +02:00
2024-06-09 12:20:04 +02:00
const ShaderCollection* ShaderCompiler::findShaders(PermutationId id) const { return &shaders[id]; }
2023-11-05 10:36:01 +01:00
2024-06-09 12:20:04 +02:00
void ShaderCompiler::registerMaterial(PMaterial material) {
2024-05-31 14:21:32 +02:00
materials[material->getName()] = material;
compile();
2023-11-05 10:36:01 +01:00
}
2024-06-09 12:20:04 +02:00
void ShaderCompiler::registerVertexData(VertexData* vd) {
2024-05-31 14:21:32 +02:00
vertexData[vd->getTypeName()] = vd;
compile();
2023-11-05 10:36:01 +01:00
}
2024-06-09 12:20:04 +02:00
void ShaderCompiler::registerRenderPass(std::string name, PassConfig config) {
2024-06-07 09:19:47 +02:00
passes[name] = std::move(config);
2024-05-31 14:21:32 +02:00
compile();
2023-11-05 10:36:01 +01:00
}
2024-06-09 12:20:04 +02:00
ShaderPermutation ShaderCompiler::getTemplate(std::string name) {
2024-06-07 09:19:47 +02:00
std::scoped_lock lock(shadersLock);
2024-05-31 14:21:32 +02:00
ShaderPermutation permutation;
2024-06-09 12:20:04 +02:00
PassConfig& pass = passes[name];
if (pass.useMeshShading) {
2024-05-31 14:21:32 +02:00
permutation.setMeshFile(pass.mainFile);
2024-06-09 12:20:04 +02:00
} else {
2024-05-31 14:21:32 +02:00
permutation.setVertexFile(pass.mainFile);
}
2024-06-09 12:20:04 +02:00
if (pass.hasFragmentShader) {
2024-05-31 14:21:32 +02:00
permutation.setFragmentFile(pass.fragmentFile);
}
2024-06-09 12:20:04 +02:00
if (pass.hasTaskShader) {
2024-05-31 14:21:32 +02:00
permutation.setTaskFile(pass.taskFile);
}
permutation.setVisibilityPass(pass.useVisibility);
return permutation;
}
2023-11-05 10:36:01 +01:00
2024-06-09 12:20:04 +02:00
void ShaderCompiler::compile() {
2024-05-31 14:21:32 +02:00
List<std::function<void()>> work;
2024-06-09 12:20:04 +02:00
for (const auto& [name, pass] : passes) {
for (const auto& [vdName, vd] : vertexData) {
if (pass.useMaterial) {
for (const auto& [matName, mat] : materials) {
2024-06-19 10:33:19 +02:00
for (int y = 0; y < 2; y++) {
work.add([=]() {
ShaderPermutation permutation = getTemplate(name);
permutation.setPositionOnly(false);
permutation.setDepthCulling(y);
permutation.setVertexData(vd->getTypeName());
OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout);
layout->addDescriptorLayout(vd->getVertexDataLayout());
layout->addDescriptorLayout(vd->getInstanceDataLayout());
permutation.setMaterial(mat->getName());
createShaders(permutation, std::move(layout));
});
2024-05-31 14:21:32 +02:00
}
}
2024-06-09 12:20:04 +02:00
} else {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
work.add([=]() {
ShaderPermutation permutation = getTemplate(name);
permutation.setPositionOnly(x);
2024-06-11 16:55:20 +02:00
permutation.setDepthCulling(y);
2024-06-09 12:20:04 +02:00
permutation.setVertexData(vd->getTypeName());
OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout);
layout->addDescriptorLayout(vd->getVertexDataLayout());
layout->addDescriptorLayout(vd->getInstanceDataLayout());
createShaders(permutation, std::move(layout));
});
2024-05-31 14:21:32 +02:00
}
}
}
}
}
getThreadPool().runAndWait(std::move(work));
2023-10-26 18:37:29 +02:00
}
2023-11-05 10:36:01 +01:00
2024-06-09 12:20:04 +02:00
void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipelineLayout layout) {
2024-05-31 14:21:32 +02:00
PermutationId perm = PermutationId(permutation);
{
std::scoped_lock lock(shadersLock);
if (shaders.contains(perm))
return;
}
ShaderCollection collection;
2024-04-19 18:23:36 +02:00
collection.pipelineLayout = std::move(layout);
2023-10-26 18:37:29 +02:00
2024-05-31 14:21:32 +02:00
ShaderCreateInfo createInfo;
createInfo.name = fmt::format("Material {0}", permutation.materialName);
2024-04-19 18:23:36 +02:00
createInfo.rootSignature = collection.pipelineLayout;
2024-06-09 12:20:04 +02:00
if (std::strlen(permutation.materialName) > 0) {
2024-04-19 18:23:36 +02:00
createInfo.additionalModules.add(permutation.materialName);
2024-05-31 14:21:32 +02:00
createInfo.defines["MATERIAL_FILE_NAME"] = permutation.materialName;
2024-04-19 18:23:36 +02:00
}
2024-06-09 12:20:04 +02:00
if (permutation.positionOnly) {
2024-05-31 14:21:32 +02:00
createInfo.defines["POS_ONLY"] = "1";
}
2024-06-11 16:55:20 +02:00
if (permutation.depthCulling) {
createInfo.defines["DEPTH_CULLING"] = "1";
2024-05-31 14:21:32 +02:00
}
2024-06-09 12:20:04 +02:00
if (permutation.visibilityPass) {
2024-05-31 14:21:32 +02:00
createInfo.defines["VISIBILITY"] = "1";
}
2024-06-09 12:20:04 +02:00
createInfo.typeParameter.add({Pair<const char*, const char*>("IVertexData", permutation.vertexDataName)});
2024-05-31 14:21:32 +02:00
createInfo.additionalModules.add(permutation.vertexDataName);
2023-10-26 18:37:29 +02:00
2024-06-09 12:20:04 +02:00
if (permutation.useMeshShading) {
if (permutation.hasTaskShader) {
2024-05-31 14:21:32 +02:00
createInfo.mainModule = permutation.taskFile;
createInfo.entryPoint = "taskMain";
collection.taskShader = graphics->createTaskShader(createInfo);
}
createInfo.mainModule = permutation.vertexMeshFile;
createInfo.entryPoint = "meshMain";
collection.meshShader = graphics->createMeshShader(createInfo);
2024-06-09 12:20:04 +02:00
} else {
2024-05-31 14:21:32 +02:00
createInfo.mainModule = permutation.vertexMeshFile;
createInfo.entryPoint = "vertexMain";
collection.vertexShader = graphics->createVertexShader(createInfo);
}
2023-10-26 18:37:29 +02:00
2024-06-09 12:20:04 +02:00
if (permutation.hasFragment) {
2024-05-31 14:21:32 +02:00
createInfo.mainModule = permutation.fragmentFile;
createInfo.entryPoint = "fragmentMain";
collection.fragmentShader = graphics->createFragmentShader(createInfo);
}
collection.pipelineLayout->create();
{
std::scoped_lock lock(shadersLock);
shaders[perm] = std::move(collection);
}
2024-04-19 18:23:36 +02:00
}