Trying to fix invalid descriptorlayout
This commit is contained in:
@@ -8,4 +8,5 @@ target_sources(SeeleEngine
|
||||
MaterialAsset.cpp
|
||||
MaterialInstance.h
|
||||
MaterialInstance.cpp
|
||||
ShaderExpression.h)
|
||||
ShaderExpression.h
|
||||
ShaderExpression.cpp)
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Asset/AssetRegistry.h"
|
||||
#include "Graphics/VertexShaderInput.h"
|
||||
#include "BRDF.h"
|
||||
#include "Graphics/WindowManager.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
@@ -32,21 +33,23 @@ Material::~Material()
|
||||
|
||||
void Material::save()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Material::load()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Material::compile()
|
||||
{
|
||||
layout = WindowManager::getGraphics()->createDescriptorLayout();
|
||||
auto& stream = getReadStream();
|
||||
json j;
|
||||
stream >> j;
|
||||
materialName = j["name"].get<std::string>();
|
||||
std::cout << "Compiling material " << materialName << std::endl;
|
||||
//Shader file needs to conform to the slang standard, which prohibits _
|
||||
materialName.erase(std::remove(materialName.begin(), materialName.end(), '_'), materialName.end());
|
||||
std::ofstream codeStream("./shaders/generated/"+materialName+".slang");
|
||||
std::string profile = j["profile"].get<std::string>();
|
||||
|
||||
@@ -57,45 +60,55 @@ void Material::compile()
|
||||
codeStream << "import MaterialParameter;" << std::endl;
|
||||
|
||||
codeStream << "struct " << materialName << ": IMaterial {" << std::endl;
|
||||
uint32 uniformBufferOffset = 0;
|
||||
uint32 bindingCounter = 1; // Uniform buffers are always binding 0
|
||||
layout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
||||
for(auto param : j["params"].items())
|
||||
{
|
||||
std::string type = param.value()["type"].get<std::string>();
|
||||
|
||||
auto default = param.value().find("default");
|
||||
if(type.compare("float") == 0)
|
||||
{
|
||||
PFloatParameter p = new FloatParameter();
|
||||
p->name = param.key();
|
||||
PFloatParameter p = new FloatParameter(param.key(), uniformBufferOffset, 0);
|
||||
codeStream << "layout(offset = " << uniformBufferOffset << ")";
|
||||
uniformBufferOffset += 4;
|
||||
if(default != param.value().end())
|
||||
{
|
||||
p->defaultValue = std::stof(default.value().get<std::string>());
|
||||
p->data = std::stof(default.value().get<std::string>());
|
||||
}
|
||||
parameters.add(p);
|
||||
}
|
||||
else if(type.compare("float3") == 0)
|
||||
{
|
||||
PVectorParameter p = new VectorParameter();
|
||||
p->name = param.key();
|
||||
PVectorParameter p = new VectorParameter(param.key(), uniformBufferOffset, 0);
|
||||
codeStream << "layout(offset = " << uniformBufferOffset << ")";
|
||||
uniformBufferOffset += 12;
|
||||
if(default != param.value().end())
|
||||
{
|
||||
p->defaultValue = parseVector(default.value().get<std::string>().c_str());
|
||||
p->data = parseVector(default.value().get<std::string>().c_str());
|
||||
}
|
||||
parameters.add(p);
|
||||
}
|
||||
else if(type.compare("Texture2D") == 0)
|
||||
{
|
||||
PTextureParameter p = new TextureParameter();
|
||||
p->name = param.key();
|
||||
PTextureParameter p = new TextureParameter(param.key(), 0, bindingCounter);
|
||||
layout->addDescriptorBinding(bindingCounter++, Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
|
||||
if(default != param.value().end())
|
||||
{
|
||||
|
||||
p->data = AssetRegistry::findTexture(default.value().get<std::string>());
|
||||
}
|
||||
else
|
||||
{
|
||||
p->data = AssetRegistry::findTexture(""); // this will return placeholder texture
|
||||
}
|
||||
parameters.add(p);
|
||||
}
|
||||
else if(type.compare("SamplerState") == 0)
|
||||
{
|
||||
PSamplerParameter p = new SamplerParameter();
|
||||
p->name = param.key();
|
||||
PSamplerParameter p = new SamplerParameter(param.key(), 0, bindingCounter);
|
||||
layout->addDescriptorBinding(bindingCounter++, Gfx::SE_DESCRIPTOR_TYPE_SAMPLER);
|
||||
SamplerCreateInfo createInfo;
|
||||
p->data = WindowManager::getGraphics()->createSamplerState(createInfo);
|
||||
parameters.add(p);
|
||||
}
|
||||
else
|
||||
@@ -104,6 +117,18 @@ void Material::compile()
|
||||
}
|
||||
codeStream << type << " " << param.key() << ";\n";
|
||||
}
|
||||
uniformDataSize = uniformBufferOffset;
|
||||
if(uniformDataSize != 0)
|
||||
{
|
||||
uniformData = new uint8[uniformDataSize];
|
||||
BulkResourceData resourceData;
|
||||
resourceData.data = uniformData;
|
||||
resourceData.size = uniformDataSize;
|
||||
uniformBuffer = WindowManager::getGraphics()->createUniformBuffer(resourceData);
|
||||
}
|
||||
layout->create();
|
||||
descriptorSet = layout->allocatedDescriptorSet();
|
||||
updateDescriptorData();
|
||||
BRDF* brdf = BRDF::getBRDFByName(profile);
|
||||
brdf->generateMaterialCode(codeStream, j["code"]);
|
||||
codeStream << "};";
|
||||
@@ -113,10 +138,11 @@ void Material::compile()
|
||||
const Gfx::ShaderCollection* Material::getShaders(Gfx::RenderPassType renderPass, VertexInputType* vertexInput) const
|
||||
{
|
||||
Gfx::ShaderPermutation permutation;
|
||||
permutation.passType = renderPass;
|
||||
std::string materialName = getFileName();
|
||||
std::string vertexInputName = vertexInput->getName();
|
||||
std::memcpy(permutation.materialName, materialName.c_str(), sizeof(permutation.materialName));
|
||||
std::memcpy(permutation.materialName, vertexInputName.c_str(), sizeof(permutation.materialName));
|
||||
std::memcpy(permutation.vertexInputName, vertexInputName.c_str(), sizeof(permutation.vertexInputName));
|
||||
return shaderMap.findShaders(Gfx::PermutationId(permutation));
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,9 @@ public:
|
||||
~Material();
|
||||
virtual void save() override;
|
||||
virtual void load() override;
|
||||
virtual PMaterial getRenderMaterial() { return this; }
|
||||
virtual const Material* getRenderMaterial() const { return this; }
|
||||
Gfx::PDescriptorLayout getDescriptorLayout() const { return layout; }
|
||||
// The name of the generated material shader, opposed to the name of the .asset file
|
||||
const std::string& getName() {return materialName;}
|
||||
|
||||
const Gfx::ShaderCollection* getShaders(Gfx::RenderPassType renderPass, VertexInputType* vertexInput) const;
|
||||
@@ -25,7 +27,9 @@ private:
|
||||
static std::mutex shaderMapLock;
|
||||
|
||||
std::string materialName;
|
||||
Gfx::PDescriptorLayout layout;
|
||||
friend class MaterialLoader;
|
||||
friend class MaterialInstance;
|
||||
};
|
||||
DEFINE_REF(Material);
|
||||
} // namespace Seele
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "MaterialAsset.h"
|
||||
#include "Graphics/WindowManager.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
@@ -19,3 +20,35 @@ MaterialAsset::MaterialAsset(const std::filesystem::path& fullPath)
|
||||
MaterialAsset::~MaterialAsset()
|
||||
{
|
||||
}
|
||||
|
||||
void MaterialAsset::beginFrame()
|
||||
{
|
||||
descriptorSet->beginFrame();
|
||||
}
|
||||
|
||||
void MaterialAsset::endFrame()
|
||||
{
|
||||
descriptorSet->endFrame();
|
||||
}
|
||||
|
||||
void MaterialAsset::updateDescriptorData()
|
||||
{
|
||||
BulkResourceData uniformUpdate;
|
||||
uniformUpdate.size = uniformDataSize;
|
||||
uniformUpdate.data = uniformData;
|
||||
for(auto param : parameters)
|
||||
{
|
||||
param->updateDescriptorSet(descriptorSet, uniformData);
|
||||
}
|
||||
if(uniformUpdate.size != 0)
|
||||
{
|
||||
uniformBuffer->updateContents(uniformUpdate);
|
||||
descriptorSet->updateBuffer(0, uniformBuffer);
|
||||
}
|
||||
descriptorSet->writeChanges();
|
||||
}
|
||||
|
||||
Gfx::PDescriptorSet MaterialAsset::getDescriptor() const
|
||||
{
|
||||
return descriptorSet;
|
||||
}
|
||||
@@ -14,15 +14,24 @@ public:
|
||||
MaterialAsset(const std::string &directory, const std::string &name);
|
||||
MaterialAsset(const std::filesystem::path &fullPath);
|
||||
~MaterialAsset();
|
||||
virtual void beginFrame();
|
||||
virtual void endFrame();
|
||||
virtual void save() = 0;
|
||||
virtual void load() = 0;
|
||||
virtual PMaterial getRenderMaterial() = 0;
|
||||
virtual const Material* getRenderMaterial() const = 0;
|
||||
Gfx::SeBlendOp getBlendMode() const {return Gfx::SE_BLEND_OP_END_RANGE;}
|
||||
Gfx::MaterialShadingModel getShadingModel() const {return Gfx::MaterialShadingModel::DefaultLit;}
|
||||
|
||||
// This needs to be called while the descriptorset is unused
|
||||
void updateDescriptorData();
|
||||
Gfx::PDescriptorSet getDescriptor() const;
|
||||
protected:
|
||||
//For now its simply the collection of parameters, since there is no point for expressions
|
||||
Array<PShaderParameter> parameters;
|
||||
Gfx::PDescriptorSet descriptorSet;
|
||||
Gfx::PUniformBuffer uniformBuffer;
|
||||
uint32 uniformDataSize;
|
||||
uint8* uniformData;
|
||||
};
|
||||
DEFINE_REF(MaterialAsset);
|
||||
} // namespace Seele
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "MaterialInstance.h"
|
||||
#include "Material.h"
|
||||
#include "Graphics/WindowManager.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
@@ -28,20 +29,15 @@ void MaterialInstance::save()
|
||||
|
||||
void MaterialInstance::load()
|
||||
{
|
||||
|
||||
baseMaterial = nullptr; // TODO: actually load the file
|
||||
BulkResourceData resourceData;
|
||||
resourceData.size = baseMaterial->uniformDataSize;
|
||||
resourceData.data = nullptr;
|
||||
uniformBuffer = WindowManager::getGraphics()->createUniformBuffer(resourceData);
|
||||
descriptorSet = baseMaterial->layout->allocatedDescriptorSet();
|
||||
}
|
||||
|
||||
PMaterial MaterialInstance::getRenderMaterial()
|
||||
const Material* MaterialInstance::getRenderMaterial() const
|
||||
{
|
||||
return baseMaterial;
|
||||
}
|
||||
|
||||
PMaterial MaterialInstance::getBaseMaterial() const
|
||||
{
|
||||
return baseMaterial;
|
||||
}
|
||||
|
||||
Gfx::PDescriptorSet MaterialInstance::getDescriptor()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
@@ -14,11 +14,9 @@ public:
|
||||
~MaterialInstance();
|
||||
virtual void save() override;
|
||||
virtual void load() override;
|
||||
virtual PMaterial getRenderMaterial();
|
||||
PMaterial getBaseMaterial() const;
|
||||
Gfx::PDescriptorSet getDescriptor();
|
||||
virtual const Material* getRenderMaterial() const;
|
||||
private:
|
||||
PMaterial baseMaterial;
|
||||
Material* baseMaterial;
|
||||
};
|
||||
DEFINE_REF(MaterialInstance);
|
||||
} // namespace Seele
|
||||
@@ -14,65 +14,50 @@ struct ExpressionOutput
|
||||
struct ShaderExpression
|
||||
{
|
||||
};
|
||||
DECLARE_NAME_REF(Gfx, DescriptorSet);
|
||||
struct ShaderParameter : public ShaderExpression
|
||||
{
|
||||
std::string name;
|
||||
uint32 byteOffset;
|
||||
uint32 binding;
|
||||
ShaderParameter(std::string name, uint32 byteOffset, uint32 binding);
|
||||
virtual ~ShaderParameter();
|
||||
// update a descriptorset, in case of a uniform buffer, copy the data to the dst + byteOffset
|
||||
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) = 0;
|
||||
};
|
||||
DEFINE_REF(ShaderParameter);
|
||||
struct FloatParameter : public ShaderParameter
|
||||
{
|
||||
float defaultValue;
|
||||
bool setParameter(const std::string& paramName, float newDefault)
|
||||
{
|
||||
if(name.compare(paramName) == 0)
|
||||
{
|
||||
defaultValue = newDefault;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
float data;
|
||||
FloatParameter(std::string name, uint32 byteOffset, uint32 binding);
|
||||
virtual ~FloatParameter();
|
||||
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
|
||||
};
|
||||
DEFINE_REF(FloatParameter);
|
||||
struct VectorParameter : public ShaderParameter
|
||||
{
|
||||
Vector defaultValue;
|
||||
bool setParameter(const std::string& paramName, Vector newDefault)
|
||||
{
|
||||
if(name.compare(paramName) == 0)
|
||||
{
|
||||
defaultValue = newDefault;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Vector data;
|
||||
VectorParameter(std::string name, uint32 byteOffset, uint32 binding);
|
||||
virtual ~VectorParameter();
|
||||
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
|
||||
};
|
||||
DEFINE_REF(VectorParameter);
|
||||
DECLARE_NAME_REF(Gfx, Texture2D);
|
||||
DECLARE_REF(TextureAsset);
|
||||
struct TextureParameter : public ShaderParameter
|
||||
{
|
||||
Gfx::PTexture2D defaultValue;
|
||||
bool setParameter(const std::string& paramName, Gfx::PTexture2D newDefault)
|
||||
{
|
||||
if(name.compare(paramName) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
PTextureAsset data;
|
||||
TextureParameter(std::string name, uint32 byteOffset, uint32 binding);
|
||||
virtual ~TextureParameter();
|
||||
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
|
||||
};
|
||||
DEFINE_REF(TextureParameter);
|
||||
DECLARE_NAME_REF(Gfx, SamplerState);
|
||||
struct SamplerParameter : public ShaderParameter
|
||||
{
|
||||
Gfx::PSamplerState defaultValue;
|
||||
bool setParameter(const std::string& paramName, Gfx::PSamplerState newDefault)
|
||||
{
|
||||
if(name.compare(paramName) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Gfx::PSamplerState data;
|
||||
SamplerParameter(std::string name, uint32 byteOffset, uint32 binding);
|
||||
virtual ~SamplerParameter();
|
||||
virtual void updateDescriptorSet(Gfx::PDescriptorSet descriptorSet, uint8* dst) override;
|
||||
};
|
||||
DEFINE_REF(SamplerParameter);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user