2023-01-21 18:43:21 +01:00
|
|
|
#include "Material.h"
|
|
|
|
|
#include "Window/WindowManager.h"
|
|
|
|
|
#include "MaterialInstance.h"
|
2023-01-29 18:58:59 +01:00
|
|
|
#include "Graphics/VertexShaderInput.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
2023-01-21 18:43:21 +01:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
|
|
|
|
Gfx::ShaderMap Material::shaderMap;
|
|
|
|
|
std::mutex Material::shaderMapLock;
|
|
|
|
|
|
2023-02-24 22:09:07 +01:00
|
|
|
Material::Material(Gfx::PGraphics graphics,
|
|
|
|
|
Array<PShaderParameter> parameter,
|
|
|
|
|
Gfx::PDescriptorLayout layout,
|
|
|
|
|
uint32 uniformDataSize,
|
|
|
|
|
uint32 uniformBinding,
|
|
|
|
|
std::string materialName,
|
|
|
|
|
Array<PShaderExpression> expressions,
|
|
|
|
|
MaterialNode brdf)
|
2023-01-29 18:58:59 +01:00
|
|
|
: MaterialInterface(graphics, parameter, uniformDataSize, uniformBinding)
|
2023-01-21 18:43:21 +01:00
|
|
|
, layout(layout)
|
|
|
|
|
, materialName(materialName)
|
2023-02-24 22:09:07 +01:00
|
|
|
, codeExpressions(expressions)
|
|
|
|
|
, brdf(brdf)
|
2023-01-21 18:43:21 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material::~Material()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::PDescriptorSet Material::createDescriptorSet()
|
|
|
|
|
{
|
|
|
|
|
Gfx::PDescriptorSet descriptorSet = layout->allocateDescriptorSet();
|
|
|
|
|
BulkResourceData uniformUpdate = {
|
|
|
|
|
.size = uniformDataSize,
|
|
|
|
|
.data = (uint8*)uniformData.data(),
|
|
|
|
|
};
|
|
|
|
|
for(auto param : parameters)
|
|
|
|
|
{
|
|
|
|
|
param->updateDescriptorSet(descriptorSet, uniformData.data());
|
|
|
|
|
}
|
|
|
|
|
if(uniformUpdate.size != 0)
|
|
|
|
|
{
|
|
|
|
|
uniformBuffer->updateContents(uniformUpdate);
|
|
|
|
|
descriptorSet->updateBuffer(uniformBinding, uniformBuffer);
|
|
|
|
|
}
|
|
|
|
|
descriptorSet->writeChanges();
|
|
|
|
|
return descriptorSet;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 14:56:13 +01:00
|
|
|
void Material::save(ArchiveBuffer& buffer) const
|
2023-01-21 18:43:21 +01:00
|
|
|
{
|
2023-02-13 14:56:13 +01:00
|
|
|
MaterialInterface::save(buffer);
|
2023-04-09 16:39:53 +02:00
|
|
|
Serialization::save(buffer, materialName);
|
2023-02-13 14:56:13 +01:00
|
|
|
Serialization::save(buffer, layout->getSetIndex());
|
2023-07-13 21:01:20 +02:00
|
|
|
Serialization::save(buffer, codeExpressions);
|
|
|
|
|
Serialization::save(buffer, brdf);
|
2023-02-13 14:56:13 +01:00
|
|
|
const auto& bindings = layout->getBindings();
|
|
|
|
|
Serialization::save(buffer, bindings.size());
|
|
|
|
|
for (const auto& binding : bindings)
|
|
|
|
|
{
|
|
|
|
|
Serialization::save(buffer, binding.binding);
|
|
|
|
|
Serialization::save(buffer, binding.bindingFlags);
|
|
|
|
|
Serialization::save(buffer, binding.descriptorCount);
|
|
|
|
|
Serialization::save(buffer, binding.descriptorType);
|
|
|
|
|
Serialization::save(buffer, binding.shaderStages);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::load(ArchiveBuffer& buffer)
|
|
|
|
|
{
|
|
|
|
|
graphics = buffer.getGraphics();
|
|
|
|
|
MaterialInterface::load(buffer);
|
|
|
|
|
Serialization::load(buffer, materialName);
|
|
|
|
|
uint32 setIndex;
|
|
|
|
|
Serialization::load(buffer, setIndex);
|
2023-07-13 21:01:20 +02:00
|
|
|
Serialization::load(buffer, codeExpressions);
|
|
|
|
|
Serialization::load(buffer, brdf);
|
2023-02-13 14:56:13 +01:00
|
|
|
uint64 numBindings;
|
|
|
|
|
Serialization::load(buffer, numBindings);
|
|
|
|
|
layout = graphics->createDescriptorLayout();
|
|
|
|
|
for (uint64 i = 0; i < numBindings; ++i)
|
|
|
|
|
{
|
|
|
|
|
uint32 binding;
|
|
|
|
|
Serialization::load(buffer, binding);
|
|
|
|
|
|
|
|
|
|
Gfx::SeDescriptorBindingFlags bindingFlags;
|
|
|
|
|
Serialization::load(buffer, bindingFlags);
|
|
|
|
|
|
|
|
|
|
uint32 descriptorCount;
|
|
|
|
|
Serialization::load(buffer, descriptorCount);
|
|
|
|
|
|
|
|
|
|
Gfx::SeDescriptorType descriptorType;
|
|
|
|
|
Serialization::load(buffer, descriptorType);
|
|
|
|
|
|
|
|
|
|
Gfx::SeShaderStageFlags shaderStages;
|
|
|
|
|
Serialization::load(buffer, shaderStages);
|
|
|
|
|
|
|
|
|
|
layout->addDescriptorBinding(binding, descriptorType, descriptorCount, bindingFlags, shaderStages);
|
|
|
|
|
}
|
|
|
|
|
layout->create();
|
2023-01-21 18:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-24 22:09:07 +01:00
|
|
|
void Material::compile()
|
|
|
|
|
{
|
|
|
|
|
std::ofstream codeStream("./shaders/generated/"+materialName+".slang");
|
|
|
|
|
codeStream << "import Material;\n";
|
|
|
|
|
codeStream << "import BRDF;\n";
|
|
|
|
|
codeStream << "import MaterialParameter;\n";
|
|
|
|
|
codeStream << "struct " << materialName << " : IMaterial {\n";
|
|
|
|
|
for(const auto& parameter : parameters)
|
|
|
|
|
{
|
|
|
|
|
parameter->generateDeclaration(codeStream);
|
|
|
|
|
}
|
|
|
|
|
codeStream << "\ttypedef " << brdf.profile << " BRDF;\n";
|
|
|
|
|
codeStream << "\t" << brdf.profile << " prepare(MaterialFragmentParameter input) {\n";
|
|
|
|
|
codeStream << "\t\t" << brdf.profile << " result;\n";
|
|
|
|
|
Map<int32, std::string> varState;
|
|
|
|
|
for(const auto& expr : codeExpressions)
|
|
|
|
|
{
|
|
|
|
|
codeStream << expr->evaluate(varState);
|
|
|
|
|
}
|
|
|
|
|
for(const auto& [name, exp] : brdf.variables)
|
|
|
|
|
{
|
|
|
|
|
codeStream << "\t\tresult." << name << " = " << varState[exp->key] << ";";
|
|
|
|
|
}
|
|
|
|
|
codeStream << "\t\treturn result;\n";
|
|
|
|
|
codeStream << "\t}\n";
|
|
|
|
|
codeStream << "};\n";
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
const Gfx::ShaderCollection* Material::getShaders(Gfx::RenderPassType renderPass, VertexInputType* vertexInput) const
|
|
|
|
|
{
|
|
|
|
|
Gfx::ShaderPermutation permutation;
|
|
|
|
|
permutation.passType = renderPass;
|
|
|
|
|
std::string vertexInputName = vertexInput->getName();
|
2023-02-13 14:56:13 +01:00
|
|
|
std::strncpy(permutation.materialName, materialName.c_str(), sizeof(permutation.materialName));
|
|
|
|
|
std::strncpy(permutation.vertexInputName, vertexInputName.c_str(), sizeof(permutation.vertexInputName));
|
2023-01-21 18:43:21 +01:00
|
|
|
return shaderMap.findShaders(Gfx::PermutationId(permutation));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::ShaderCollection& Material::createShaders(Gfx::PGraphics graphics, Gfx::RenderPassType renderPass, VertexInputType* vertexInput)
|
|
|
|
|
{
|
|
|
|
|
std::scoped_lock l(shaderMapLock);
|
|
|
|
|
return shaderMap.createShaders(graphics, renderPass, this, vertexInput, false);
|
|
|
|
|
}
|