THERE IS SOMETHING ON SCREEN

This commit is contained in:
Dynamitos
2023-11-11 22:39:17 +01:00
parent 4c05886d38
commit a9ac3d14a7
18 changed files with 85 additions and 36 deletions
+14 -7
View File
@@ -50,10 +50,17 @@ void ShaderCompiler::registerRenderPass(std::string name, std::string mainFile,
void ShaderCompiler::compile()
{
ShaderPermutation permutation;
for (const auto& [name, pass] : passes)
{
std::strncpy(permutation.vertexMeshFile, pass.mainFile.c_str(), sizeof(permutation.vertexMeshFile));
ShaderPermutation permutation;
if (pass.useMeshShading)
{
permutation.setMeshFile(pass.mainFile);
}
else
{
permutation.setVertexFile(pass.mainFile);
}
if (pass.hasFragmentShader)
{
permutation.setFragmentFile(pass.fragmentFile);
@@ -81,19 +88,22 @@ void ShaderCompiler::compile()
}
}
ShaderCollection& ShaderCompiler::createShaders(ShaderPermutation permutation)
void ShaderCompiler::createShaders(ShaderPermutation permutation)
{
std::scoped_lock lock(shadersLock);
PermutationId perm = PermutationId(permutation);
if (shaders.contains(perm))
return;
ShaderCollection collection;
ShaderCreateInfo createInfo;
createInfo.typeParameter = { Pair<const char*, const char*>("IVertexData", permutation.vertexDataName) };
createInfo.name = std::format("Material {0}", permutation.materialName);
if (std::strlen(permutation.materialName) > 0)
{
createInfo.additionalModules.add(permutation.materialName);
createInfo.typeParameter.add(Pair<const char*, const char*>("IMaterial", permutation.materialName));
}
createInfo.typeParameter.add({ Pair<const char*, const char*>("IVertexData", permutation.vertexDataName) });
createInfo.additionalModules.add(permutation.vertexDataName);
createInfo.additionalModules.add(permutation.vertexMeshFile);
if (permutation.hasFragment)
@@ -131,8 +141,5 @@ ShaderCollection& ShaderCompiler::createShaders(ShaderPermutation permutation)
collection.fragmentShader = graphics->createFragmentShader(createInfo);
}
collection.vertexDeclaration = graphics->createVertexDeclaration(Array<VertexElement>());
PermutationId perm = PermutationId(permutation);
shaders[perm] = std::move(collection);
return shaders[perm];
}
+1 -1
View File
@@ -148,7 +148,7 @@ public:
std::string taskFile = "");
private:
void compile();
ShaderCollection& createShaders(ShaderPermutation permutation);
void createShaders(ShaderPermutation permutation);
std::mutex shadersLock;
Map<PermutationId, ShaderCollection> shaders;
Map<std::string, PMaterial> materials;
@@ -61,6 +61,14 @@ void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data)
dirty = true;
}
void Seele::StaticMeshVertexData::loadColors(MeshId id, const Array<Vector>& data)
{
uint64 offset = meshOffsets[id];
assert(offset + data.size() <= head);
std::memcpy(colorData.data() + offset, data.data(), data.size() * sizeof(Vector));
dirty = true;
}
void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer)
{
uint64 offset = meshOffsets[id];
@@ -69,16 +77,19 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB
Array<Vector> nor(numVertices);
Array<Vector> tan(numVertices);
Array<Vector> bit(numVertices);
Array<Vector> col(numVertices);
std::copy(positionData.begin() + offset, positionData.begin() + offset + numVertices, pos.begin());
std::copy(texCoordsData.begin() + offset, texCoordsData.begin() + offset + numVertices, tex.begin());
std::copy(normalData.begin() + offset, normalData.begin() + offset + numVertices, nor.begin());
std::copy(tangentData.begin() + offset, tangentData.begin() + offset + numVertices, tan.begin());
std::copy(biTangentData.begin() + offset, biTangentData.begin() + offset + numVertices, bit.begin());
std::copy(colorData.begin() + offset, colorData.begin() + offset + numVertices, col.begin());
Serialization::save(buffer, pos);
Serialization::save(buffer, tex);
Serialization::save(buffer, nor);
Serialization::save(buffer, tan);
Serialization::save(buffer, bit);
Serialization::save(buffer, col);
}
void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer)
@@ -88,16 +99,19 @@ void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer)
Array<Vector> nor;
Array<Vector> tan;
Array<Vector> bit;
Array<Vector> col;
Serialization::load(buffer, pos);
Serialization::load(buffer, tex);
Serialization::load(buffer, nor);
Serialization::load(buffer, tan);
Serialization::load(buffer, bit);
Serialization::load(buffer, col);
loadPositions(id, pos);
loadTexCoords(id, tex);
loadNormals(id, nor);
loadTangents(id, tan);
loadBiTangents(id, bit);
loadBiTangents(id, col);
}
void StaticMeshVertexData::init(Gfx::PGraphics graphics)
@@ -109,6 +123,7 @@ void StaticMeshVertexData::init(Gfx::PGraphics graphics)
descriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(3, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(4, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(5, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->create();
descriptorSet = descriptorLayout->allocateDescriptorSet();
}
@@ -141,6 +156,7 @@ void StaticMeshVertexData::resizeBuffers()
normals = graphics->createShaderBuffer(createInfo);
tangents = graphics->createShaderBuffer(createInfo);
biTangents = graphics->createShaderBuffer(createInfo);
colors = graphics->createShaderBuffer(createInfo);
createInfo.sourceData.size = verticesAllocated * sizeof(Vector2);
createInfo.stride = sizeof(Vector2);
texCoords = graphics->createShaderBuffer(createInfo);
@@ -150,6 +166,7 @@ void StaticMeshVertexData::resizeBuffers()
normalData.resize(verticesAllocated);
tangentData.resize(verticesAllocated);
biTangentData.resize(verticesAllocated);
colorData.resize(verticesAllocated);
}
void StaticMeshVertexData::updateBuffers()
@@ -174,6 +191,10 @@ void StaticMeshVertexData::updateBuffers()
.size = biTangentData.size() * sizeof(Vector),
.data = (uint8*)biTangentData.data(),
});
colors->updateContents(DataSource{
.size = colorData.size() * sizeof(Vector),
.data = (uint8*)colorData.data()
});
descriptorLayout->reset();
descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer(0, positions);
@@ -181,5 +202,6 @@ void StaticMeshVertexData::updateBuffers()
descriptorSet->updateBuffer(2, normals);
descriptorSet->updateBuffer(3, tangents);
descriptorSet->updateBuffer(4, biTangents);
descriptorSet->updateBuffer(5, colors);
descriptorSet->writeChanges();
}
@@ -16,6 +16,7 @@ public:
void loadNormals(MeshId id, const Array<Vector>& data);
void loadTangents(MeshId id, const Array<Vector>& data);
void loadBiTangents(MeshId id, const Array<Vector>& data);
void loadColors(MeshId id, const Array<Vector>& data);
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) override;
virtual void init(Gfx::PGraphics graphics) override;
@@ -36,6 +37,8 @@ private:
Array<Vector> tangentData;
Gfx::OShaderBuffer biTangents;
Array<Vector> biTangentData;
Gfx::OShaderBuffer colors;
Array<Vector> colorData;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::PDescriptorSet descriptorSet;
};
+2
View File
@@ -33,6 +33,8 @@ void VertexData::updateMesh(const Component::Transform& transform, PMesh mesh)
},
.indexBuffer = mesh->indexBuffer,
});
matInstanceData.materialInstance = mesh->referencedMaterial->getHandle();
matInstanceData.materialInstance->updateDescriptor();
matInstanceData.numMeshes += meshData[mesh->id].size();
}
+4 -3
View File
@@ -14,7 +14,7 @@ Material::Material(Gfx::PGraphics graphics,
uint32 uniformDataSize,
uint32 uniformBinding,
std::string materialName,
Map<std::string, OShaderExpression> expressions,
Array<OShaderExpression> expressions,
Array<std::string> parameter,
MaterialNode brdf)
: graphics(graphics)
@@ -112,14 +112,15 @@ void Material::compile()
codeStream << "struct " << materialName << " : IMaterial {\n";
for(const auto& parameter : parameters)
{
PShaderParameter handle = PShaderExpression(codeExpressions[parameter]);
PShaderParameter handle = PShaderExpression(*codeExpressions.find([&parameter](const OShaderExpression& exp) {return exp->key == parameter; }));
handle->generateDeclaration(codeStream);
}
codeStream << "\ttypedef " << brdf.profile << " BRDF;\n";
codeStream << "\t" << brdf.profile << " prepare(MaterialParameter input) {\n";
codeStream << "\t\t" << brdf.profile << " result;\n";
Map<std::string, std::string> varState;
for(const auto& [_, expr] :codeExpressions)
// initialize variable state
for(const auto& expr :codeExpressions)
{
codeStream << expr->evaluate(varState);
}
+2 -2
View File
@@ -14,7 +14,7 @@ public:
uint32 uniformDataSize,
uint32 uniformBinding,
std::string materialName,
Map<std::string, OShaderExpression> expressions,
Array<OShaderExpression> expressions,
Array<std::string> parameter,
MaterialNode brdf);
~Material();
@@ -34,7 +34,7 @@ private:
uint64 instanceId;
Gfx::ODescriptorLayout layout;
std::string materialName;
Map<std::string, OShaderExpression> codeExpressions;
Array<OShaderExpression> codeExpressions;
Array<std::string> parameters;
MaterialNode brdf;
};
+3 -2
View File
@@ -10,7 +10,7 @@ MaterialInstance::MaterialInstance()
MaterialInstance::MaterialInstance(uint64 id,
Gfx::PGraphics graphics,
Map<std::string, OShaderExpression>& expressions,
Array<OShaderExpression>& expressions,
Array<std::string> params,
uint32 uniformBinding,
uint32 uniformSize)
@@ -33,7 +33,8 @@ MaterialInstance::MaterialInstance(uint64 id,
parameters.reserve(params.size());
for (size_t i = 0; i < params.size(); ++i)
{
Serialization::save(buffer, expressions[params[i]]);
const std::string& name = params[i];
Serialization::save(buffer, *expressions.find([&name](const OShaderExpression& p) {return p->key == name; }));
buffer.rewind();
OShaderParameter param;
Serialization::load(buffer, param);
+1 -1
View File
@@ -11,7 +11,7 @@ public:
MaterialInstance();
MaterialInstance(uint64 id,
Gfx::PGraphics graphics,
Map<std::string, OShaderExpression>& expressions,
Array<OShaderExpression>& expressions,
Array<std::string> params,
uint32 uniformBinding,
uint32 uniformSize);
+1 -1
View File
@@ -109,7 +109,7 @@ DECLARE_NAME_REF(Gfx, SamplerState)
struct SamplerParameter : public ShaderParameter
{
static constexpr uint64 IDENTIFIER = 0x08;
Gfx::PSamplerState data;
Gfx::OSamplerState data;
SamplerParameter() {}
SamplerParameter(std::string name, uint32 byteOffset, uint32 binding);
virtual ~SamplerParameter();