2023-10-24 15:01:09 +02:00
|
|
|
#include "VertexData.h"
|
2023-10-31 23:44:46 +01:00
|
|
|
#include "Graphics/Enums.h"
|
|
|
|
|
#include "Graphics/Initializer.h"
|
2023-10-24 15:01:09 +02:00
|
|
|
#include "Material/Material.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
2023-10-26 18:37:29 +02:00
|
|
|
#include "Graphics/Descriptor.h"
|
|
|
|
|
#include "Component/Mesh.h"
|
2023-11-15 17:42:57 +01:00
|
|
|
#include "Graphics/Shader.h"
|
2024-01-24 23:10:33 +01:00
|
|
|
#include "Graphics/Mesh.h"
|
2023-12-22 19:46:07 +01:00
|
|
|
#include "Containers/Set.h"
|
2023-10-24 15:01:09 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2023-11-30 11:51:53 +01:00
|
|
|
constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024 * 1024;
|
2023-10-31 17:55:30 +01:00
|
|
|
|
2023-10-24 15:01:09 +02:00
|
|
|
void VertexData::resetMeshData()
|
|
|
|
|
{
|
2023-12-17 16:16:46 +01:00
|
|
|
std::unique_lock l(materialDataLock);
|
2024-05-09 08:41:46 +02:00
|
|
|
for (auto& mat : materialData)
|
2023-11-17 18:46:37 +01:00
|
|
|
{
|
2024-05-16 19:47:35 +02:00
|
|
|
for (auto inst : mat.instances)
|
|
|
|
|
{
|
|
|
|
|
inst.instanceData.clear();
|
|
|
|
|
inst.instanceMeshData.clear();
|
|
|
|
|
}
|
2024-05-12 19:36:32 +02:00
|
|
|
if (mat.material != nullptr)
|
|
|
|
|
{
|
|
|
|
|
mat.material->getDescriptorLayout()->reset();
|
|
|
|
|
}
|
2023-11-17 18:46:37 +01:00
|
|
|
}
|
2023-10-31 16:16:23 +01:00
|
|
|
if (dirty)
|
|
|
|
|
{
|
|
|
|
|
updateBuffers();
|
2023-11-08 23:27:21 +01:00
|
|
|
dirty = false;
|
2023-10-31 16:16:23 +01:00
|
|
|
}
|
2023-10-24 15:01:09 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-26 09:40:48 +01:00
|
|
|
void VertexData::updateMesh(PMesh mesh, Component::Transform& transform)
|
2023-10-24 15:01:09 +02:00
|
|
|
{
|
2023-12-17 16:16:46 +01:00
|
|
|
std::unique_lock l(materialDataLock);
|
2024-01-18 18:39:44 +01:00
|
|
|
PMaterialInstance referencedInstance = mesh->referencedMaterial->getHandle();
|
|
|
|
|
PMaterial mat = referencedInstance->getBaseMaterial();
|
2024-05-09 08:41:46 +02:00
|
|
|
if (materialData.size() <= mat->getId())
|
|
|
|
|
{
|
2024-05-12 19:36:32 +02:00
|
|
|
materialData.resize(mat->getId() + 1);
|
2024-05-09 08:41:46 +02:00
|
|
|
}
|
|
|
|
|
MaterialData& matData = materialData[mat->getId()];
|
2023-11-08 23:27:21 +01:00
|
|
|
matData.material = mat;
|
2024-05-12 19:36:32 +02:00
|
|
|
if (matData.instances.size() <= referencedInstance->getId())
|
2023-11-26 11:27:39 +01:00
|
|
|
{
|
2024-05-12 19:36:32 +02:00
|
|
|
matData.instances.resize(referencedInstance->getId() + 1);
|
2023-11-26 11:27:39 +01:00
|
|
|
}
|
2024-05-12 19:36:32 +02:00
|
|
|
BatchedDrawCall& matInstanceData = matData.instances[referencedInstance->getId()];
|
|
|
|
|
const auto& data = meshData[mesh->id];
|
|
|
|
|
|
|
|
|
|
Matrix4 transformMatrix = transform.toMatrix() * mesh->transform;
|
|
|
|
|
matInstanceData.instanceData.add(InstanceData{
|
|
|
|
|
.transformMatrix = transformMatrix,
|
|
|
|
|
.inverseTransformMatrix = glm::inverse(transformMatrix),
|
|
|
|
|
});
|
|
|
|
|
matInstanceData.instanceMeshData.add(data);
|
|
|
|
|
matInstanceData.numMeshes++;
|
|
|
|
|
for (size_t i = 0; i < 0; ++i)
|
|
|
|
|
{
|
|
|
|
|
auto bounding = meshlets[data.meshletOffset + i].bounding;
|
|
|
|
|
StaticArray<Vector, 8> corners;
|
|
|
|
|
Vector min = bounding.min;//bounding.center - bounding.radius * Vector(1, 1, 1);
|
|
|
|
|
Vector max = bounding.max;//bounding.center + bounding.radius * Vector(1, 1, 1);
|
|
|
|
|
corners[0] = transformMatrix * Vector4(min.x, min.y, min.z, 1);
|
|
|
|
|
corners[1] = transformMatrix * Vector4(min.x, min.y, max.z, 1);
|
|
|
|
|
corners[2] = transformMatrix * Vector4(min.x, max.y, min.z, 1);
|
|
|
|
|
corners[3] = transformMatrix * Vector4(min.x, max.y, max.z, 1);
|
|
|
|
|
corners[4] = transformMatrix * Vector4(max.x, min.y, min.z, 1);
|
|
|
|
|
corners[5] = transformMatrix * Vector4(max.x, min.y, max.z, 1);
|
|
|
|
|
corners[6] = transformMatrix * Vector4(max.x, max.y, min.z, 1);
|
|
|
|
|
corners[7] = transformMatrix * Vector4(max.x, max.y, max.z, 1);
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color });
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 18:39:44 +01:00
|
|
|
matInstanceData.materialInstance = referencedInstance;
|
|
|
|
|
referencedInstance->updateDescriptor();
|
2023-10-24 15:01:09 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-26 09:40:48 +01:00
|
|
|
void VertexData::createDescriptors()
|
2023-11-22 13:18:54 +01:00
|
|
|
{
|
2023-12-17 16:16:46 +01:00
|
|
|
std::unique_lock l(materialDataLock);
|
2024-05-12 19:36:32 +02:00
|
|
|
|
|
|
|
|
instanceData.clear();
|
|
|
|
|
instanceMeshData.clear();
|
|
|
|
|
|
|
|
|
|
uint32 numMeshlets = 0;
|
|
|
|
|
Array<uint32> cullingOffsets;
|
|
|
|
|
for (auto& mat : materialData)
|
|
|
|
|
{
|
|
|
|
|
for (auto& instance : mat.instances)
|
|
|
|
|
{
|
|
|
|
|
instance.offsets.instanceOffset = instanceData.size();
|
|
|
|
|
instance.offsets.cullingCounterOffset = cullingOffsets.size();
|
|
|
|
|
instance.numMeshes = instance.instanceData.size();
|
|
|
|
|
instance.numMeshlets = 0;
|
|
|
|
|
for (size_t i = 0; i < instance.instanceData.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
instanceData.add(instance.instanceData[i]);
|
|
|
|
|
instanceMeshData.add(instance.instanceMeshData[i]);
|
|
|
|
|
instance.numMeshlets += instance.instanceMeshData[i].numMeshlets;
|
|
|
|
|
cullingOffsets.add(numMeshlets);
|
|
|
|
|
numMeshlets += instance.numMeshlets;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-15 15:27:13 +02:00
|
|
|
cullingOffsetBuffer->rotateBuffer(cullingOffsets.size() * sizeof(uint32));
|
|
|
|
|
cullingOffsetBuffer->updateContents(ShaderBufferCreateInfo{
|
2024-05-12 19:36:32 +02:00
|
|
|
.sourceData = {
|
|
|
|
|
.size = cullingOffsets.size() * sizeof(uint32),
|
|
|
|
|
.data = (uint8*)cullingOffsets.data(),
|
|
|
|
|
},
|
|
|
|
|
.numElements = cullingOffsets.size(),
|
|
|
|
|
.name = "MeshletOffset"
|
|
|
|
|
});
|
2024-05-15 15:27:13 +02:00
|
|
|
cullingOffsetBuffer->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_MEMORY_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT
|
|
|
|
|
);
|
|
|
|
|
cullingBuffer->rotateBuffer(numMeshlets * sizeof(uint32));
|
|
|
|
|
cullingBuffer->updateContents(ShaderBufferCreateInfo{
|
2024-05-12 19:36:32 +02:00
|
|
|
.sourceData = {
|
|
|
|
|
.size = numMeshlets * sizeof(uint32),
|
|
|
|
|
},
|
|
|
|
|
.numElements = numMeshlets,
|
|
|
|
|
.name = "MeshletCulling"
|
|
|
|
|
});
|
2024-05-15 15:27:13 +02:00
|
|
|
cullingBuffer->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_MEMORY_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_MESH_SHADER_BIT_EXT
|
|
|
|
|
);
|
|
|
|
|
instanceBuffer->rotateBuffer(instanceData.size() * sizeof(InstanceData));
|
|
|
|
|
instanceBuffer->updateContents(ShaderBufferCreateInfo{
|
2024-05-08 10:51:59 +02:00
|
|
|
.sourceData = {
|
2024-05-15 15:27:13 +02:00
|
|
|
.size = instanceData.size() * sizeof(InstanceData),
|
2024-05-08 10:51:59 +02:00
|
|
|
.data = (uint8*)instanceData.data(),
|
|
|
|
|
},
|
|
|
|
|
.numElements = instanceData.size(),
|
|
|
|
|
.name = "InstanceBuffer"
|
|
|
|
|
});
|
|
|
|
|
instanceBuffer->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_MEMORY_READ_BIT,
|
2024-05-15 15:27:13 +02:00
|
|
|
Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT
|
2024-05-08 10:51:59 +02:00
|
|
|
);
|
|
|
|
|
|
2024-05-15 15:27:13 +02:00
|
|
|
instanceMeshDataBuffer->rotateBuffer(sizeof(MeshData) * instanceMeshData.size());
|
|
|
|
|
instanceMeshDataBuffer->updateContents(ShaderBufferCreateInfo{
|
2024-05-08 10:51:59 +02:00
|
|
|
.sourceData = {
|
|
|
|
|
.size = sizeof(MeshData) * instanceMeshData.size(),
|
|
|
|
|
.data = (uint8*)instanceMeshData.data(),
|
|
|
|
|
},
|
|
|
|
|
.numElements = instanceMeshData.size(),
|
|
|
|
|
.name = "MeshDataBuffer"
|
|
|
|
|
});
|
|
|
|
|
instanceMeshDataBuffer->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_MEMORY_READ_BIT,
|
2024-05-15 15:27:13 +02:00
|
|
|
Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT
|
2024-05-08 10:51:59 +02:00
|
|
|
);
|
2024-05-15 15:27:13 +02:00
|
|
|
instanceDataLayout->reset();
|
|
|
|
|
descriptorSet = instanceDataLayout->allocateDescriptorSet();
|
2024-05-08 10:51:59 +02:00
|
|
|
descriptorSet->updateBuffer(0, instanceBuffer);
|
|
|
|
|
descriptorSet->updateBuffer(1, instanceMeshDataBuffer);
|
|
|
|
|
descriptorSet->updateBuffer(2, meshletBuffer);
|
|
|
|
|
descriptorSet->updateBuffer(3, primitiveIndicesBuffer);
|
|
|
|
|
descriptorSet->updateBuffer(4, vertexIndicesBuffer);
|
2024-05-12 19:36:32 +02:00
|
|
|
descriptorSet->updateBuffer(5, cullingOffsetBuffer);
|
|
|
|
|
descriptorSet->updateBuffer(6, cullingBuffer);
|
2024-05-08 10:51:59 +02:00
|
|
|
|
|
|
|
|
descriptorSet->writeChanges();
|
2023-11-10 19:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:33:32 +02:00
|
|
|
void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet> loadedMeshlets)
|
2023-10-31 16:16:23 +01:00
|
|
|
{
|
2024-05-01 14:10:52 +02:00
|
|
|
std::unique_lock l(vertexDataLock);
|
2023-11-22 13:18:54 +01:00
|
|
|
meshlets.reserve(meshlets.size() + loadedMeshlets.size());
|
|
|
|
|
vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet);
|
2023-11-26 11:27:39 +01:00
|
|
|
primitiveIndices.reserve(primitiveIndices.size() + loadedMeshlets.size() * Gfx::numPrimitivesPerMeshlet * 3);
|
2024-05-12 19:36:32 +02:00
|
|
|
uint32 meshletOffset = meshlets.size();
|
|
|
|
|
AABB meshAABB;
|
|
|
|
|
for (uint32 i = 0; i < loadedMeshlets.size(); ++i)
|
2023-10-31 16:16:23 +01:00
|
|
|
{
|
2024-05-12 19:36:32 +02:00
|
|
|
Meshlet& m = loadedMeshlets[i];
|
|
|
|
|
meshAABB = meshAABB.combine(m.boundingBox);
|
|
|
|
|
uint32 vertexOffset = vertexIndices.size();
|
|
|
|
|
vertexIndices.resize(vertexOffset + m.numVertices);
|
|
|
|
|
std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, m.numVertices * sizeof(uint32));
|
|
|
|
|
uint32 primitiveOffset = primitiveIndices.size();
|
|
|
|
|
primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3));
|
|
|
|
|
std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8));
|
|
|
|
|
meshlets.add(MeshletDescription{
|
|
|
|
|
.bounding = m.boundingBox,//.toSphere(),
|
|
|
|
|
.vertexCount = m.numVertices,
|
|
|
|
|
.primitiveCount = m.numPrimitives,
|
|
|
|
|
.vertexOffset = vertexOffset,
|
|
|
|
|
.primitiveOffset = primitiveOffset,
|
|
|
|
|
.color = Vector((float)rand() / RAND_MAX,(float)rand() / RAND_MAX,(float)rand() / RAND_MAX),
|
|
|
|
|
.indicesOffset = (uint32)meshOffsets[id],
|
2023-11-10 19:18:09 +01:00
|
|
|
});
|
2023-10-31 16:16:23 +01:00
|
|
|
}
|
2024-05-12 19:36:32 +02:00
|
|
|
meshData[id] = MeshData{
|
|
|
|
|
.bounding = meshAABB,//.toSphere(),
|
|
|
|
|
.numMeshlets = (uint32)loadedMeshlets.size(),
|
|
|
|
|
.meshletOffset = meshletOffset,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
meshData[id].firstIndex = indices.size();
|
|
|
|
|
meshData[id].numIndices = loadedIndices.size();
|
2024-05-04 09:25:13 +02:00
|
|
|
if (!graphics->supportMeshShading())
|
|
|
|
|
{
|
|
|
|
|
indices.resize(indices.size() + loadedIndices.size());
|
2024-05-12 19:36:32 +02:00
|
|
|
std::memcpy(indices.data() + meshData[id].firstIndex, loadedIndices.data(), loadedIndices.size() * sizeof(uint32));
|
2024-05-04 09:25:13 +02:00
|
|
|
indexBuffer = graphics->createIndexBuffer(IndexBufferCreateInfo{
|
|
|
|
|
.sourceData = {
|
|
|
|
|
.size = sizeof(uint32) * indices.size(),
|
|
|
|
|
.data = (uint8*)indices.data(),
|
|
|
|
|
},
|
|
|
|
|
.indexType = Gfx::SE_INDEX_TYPE_UINT32,
|
|
|
|
|
.name = "IndexBuffer",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
2023-11-10 19:18:09 +01:00
|
|
|
meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2023-10-31 23:44:46 +01:00
|
|
|
.size = sizeof(MeshletDescription) * meshlets.size(),
|
|
|
|
|
.data = (uint8*)meshlets.data()
|
|
|
|
|
},
|
2023-11-15 17:42:57 +01:00
|
|
|
.numElements = meshlets.size(),
|
2023-11-26 11:27:39 +01:00
|
|
|
.dynamic = false,
|
2024-04-20 21:35:43 +02:00
|
|
|
.name = "MeshletBuffer"
|
2024-05-12 19:36:32 +02:00
|
|
|
});
|
2023-11-10 19:18:09 +01:00
|
|
|
vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2024-04-07 16:33:32 +02:00
|
|
|
.size = sizeof(uint32) * vertexIndices.size(),
|
2023-10-31 23:44:46 +01:00
|
|
|
.data = (uint8*)vertexIndices.data(),
|
|
|
|
|
},
|
2023-11-15 17:42:57 +01:00
|
|
|
.numElements = vertexIndices.size(),
|
2023-11-26 11:27:39 +01:00
|
|
|
.dynamic = false,
|
2024-04-20 21:35:43 +02:00
|
|
|
.name = "VertexIndicesBuffer"
|
2024-05-12 19:36:32 +02:00
|
|
|
});
|
2023-11-10 19:18:09 +01:00
|
|
|
primitiveIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2024-04-24 23:25:34 +02:00
|
|
|
.size = sizeof(uint8) * primitiveIndices.size(),
|
2023-10-31 23:44:46 +01:00
|
|
|
.data = (uint8*)primitiveIndices.data(),
|
|
|
|
|
},
|
2023-11-15 17:42:57 +01:00
|
|
|
.numElements = primitiveIndices.size(),
|
2023-11-26 11:27:39 +01:00
|
|
|
.dynamic = false,
|
2024-04-20 21:35:43 +02:00
|
|
|
.name = "PrimitiveIndicesBuffer",
|
2024-05-12 19:36:32 +02:00
|
|
|
});
|
2023-10-24 15:01:09 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 17:55:30 +01:00
|
|
|
MeshId VertexData::allocateVertexData(uint64 numVertices)
|
|
|
|
|
{
|
2024-05-01 14:10:52 +02:00
|
|
|
std::unique_lock l(vertexDataLock);
|
2023-10-31 17:55:30 +01:00
|
|
|
MeshId res{ idCounter++ };
|
|
|
|
|
meshOffsets[res] = head;
|
2023-11-09 22:15:51 +01:00
|
|
|
meshVertexCounts[res] = numVertices;
|
2023-10-31 17:55:30 +01:00
|
|
|
head += numVertices;
|
|
|
|
|
if (head > verticesAllocated)
|
|
|
|
|
{
|
2023-11-30 11:51:53 +01:00
|
|
|
verticesAllocated = std::max(head, verticesAllocated + NUM_DEFAULT_ELEMENTS);
|
2023-10-31 17:55:30 +01:00
|
|
|
resizeBuffers();
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
uint64 VertexData::getMeshOffset(MeshId id)
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
|
|
|
|
return meshOffsets[id];
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
uint64 VertexData::getMeshVertexCount(MeshId id)
|
|
|
|
|
{
|
|
|
|
|
return meshVertexCounts[id];
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 15:01:09 +02:00
|
|
|
List<VertexData*> vertexDataList;
|
|
|
|
|
|
|
|
|
|
List<VertexData*> VertexData::getList()
|
|
|
|
|
{
|
|
|
|
|
return vertexDataList;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 10:51:59 +02:00
|
|
|
VertexData* VertexData::findByTypeName(std::string name)
|
2023-11-01 13:38:49 +01:00
|
|
|
{
|
|
|
|
|
for (auto vd : vertexDataList)
|
|
|
|
|
{
|
|
|
|
|
if (vd->getTypeName() == name)
|
|
|
|
|
{
|
|
|
|
|
return vd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 10:51:59 +02:00
|
|
|
void VertexData::init(Gfx::PGraphics _graphics)
|
2023-10-24 15:01:09 +02:00
|
|
|
{
|
2023-12-10 22:27:59 +01:00
|
|
|
graphics = _graphics;
|
2023-10-31 17:55:30 +01:00
|
|
|
verticesAllocated = NUM_DEFAULT_ELEMENTS;
|
2024-04-19 18:23:36 +02:00
|
|
|
instanceDataLayout = graphics->createDescriptorLayout("pScene");
|
2023-11-26 09:40:48 +01:00
|
|
|
|
2024-05-08 10:51:59 +02:00
|
|
|
// instanceData
|
2024-05-12 19:36:32 +02:00
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, });
|
2023-11-26 09:40:48 +01:00
|
|
|
// meshData
|
2024-05-12 19:36:32 +02:00
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, });
|
2023-11-26 09:40:48 +01:00
|
|
|
// meshletData
|
2024-05-12 19:36:32 +02:00
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
|
2023-11-26 09:40:48 +01:00
|
|
|
// primitiveIndices
|
2024-05-12 19:36:32 +02:00
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
|
|
|
|
|
// vertexIndices
|
|
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
|
|
|
|
|
// cullingList
|
|
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
|
|
|
|
|
// cullingOffset
|
|
|
|
|
instanceDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 6, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER });
|
2024-05-15 15:27:13 +02:00
|
|
|
cullingOffsetBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .dynamic = true });
|
|
|
|
|
cullingBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .dynamic = true });
|
|
|
|
|
instanceBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .dynamic = true });
|
|
|
|
|
instanceMeshDataBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .dynamic = true });
|
2023-10-24 15:01:09 +02:00
|
|
|
instanceDataLayout->create();
|
2023-10-31 17:55:30 +01:00
|
|
|
resizeBuffers();
|
2023-11-05 10:36:01 +01:00
|
|
|
graphics->getShaderCompiler()->registerVertexData(this);
|
2023-10-29 09:20:23 +01:00
|
|
|
}
|
2023-10-31 16:16:23 +01:00
|
|
|
|
2023-12-12 11:37:00 +01:00
|
|
|
void VertexData::destroy()
|
|
|
|
|
{
|
2024-05-08 10:51:59 +02:00
|
|
|
instanceBuffer = nullptr;
|
|
|
|
|
instanceMeshDataBuffer = nullptr;
|
2023-12-12 11:37:00 +01:00
|
|
|
instanceDataLayout = nullptr;
|
|
|
|
|
meshletBuffer = nullptr;
|
|
|
|
|
vertexIndicesBuffer = nullptr;
|
|
|
|
|
primitiveIndicesBuffer = nullptr;
|
|
|
|
|
indexBuffer = nullptr;
|
|
|
|
|
meshData.clear();
|
|
|
|
|
materialData.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 16:16:23 +01:00
|
|
|
VertexData::VertexData()
|
|
|
|
|
: idCounter(0)
|
2023-11-05 10:36:01 +01:00
|
|
|
, head(0)
|
|
|
|
|
, verticesAllocated(0)
|
2023-11-06 14:47:21 +01:00
|
|
|
, dirty(false)
|
2023-10-31 16:16:23 +01:00
|
|
|
{
|
|
|
|
|
}
|