2023-10-07 19:29:53 +02:00
|
|
|
#include "StaticMeshVertexData.h"
|
2023-10-31 16:16:23 +01:00
|
|
|
#include "Graphics.h"
|
2023-10-31 17:55:30 +01:00
|
|
|
#include "Graphics/Enums.h"
|
2023-10-07 19:29:53 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2023-10-26 18:37:29 +02:00
|
|
|
extern List<VertexData*> vertexDataList;
|
|
|
|
|
|
2023-10-31 16:16:23 +01:00
|
|
|
|
|
|
|
|
StaticMeshVertexData::StaticMeshVertexData()
|
2023-10-26 18:37:29 +02:00
|
|
|
{
|
|
|
|
|
vertexDataList.add(this);
|
|
|
|
|
}
|
2023-10-07 19:29:53 +02:00
|
|
|
|
|
|
|
|
StaticMeshVertexData::~StaticMeshVertexData()
|
|
|
|
|
{}
|
|
|
|
|
|
2023-10-31 17:55:30 +01:00
|
|
|
StaticMeshVertexData* StaticMeshVertexData::getInstance()
|
2023-10-31 16:16:23 +01:00
|
|
|
{
|
2023-10-31 17:55:30 +01:00
|
|
|
static StaticMeshVertexData instance;
|
|
|
|
|
return &instance;
|
2023-10-29 09:20:23 +01:00
|
|
|
}
|
2023-10-31 16:16:23 +01:00
|
|
|
|
|
|
|
|
void StaticMeshVertexData::loadPositions(MeshId id, const Array<Vector>& data)
|
|
|
|
|
{
|
|
|
|
|
uint64 offset = meshOffsets[id];
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2023-10-31 16:16:23 +01:00
|
|
|
std::memcpy(positionData.data() + offset, data.data(), data.size() * sizeof(Vector));
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::loadTexCoords(MeshId id, const Array<Vector2>& data)
|
|
|
|
|
{
|
|
|
|
|
uint64 offset = meshOffsets[id];
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2023-10-31 16:16:23 +01:00
|
|
|
std::memcpy(texCoordsData.data() + offset, data.data(), data.size() * sizeof(Vector2));
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::loadNormals(MeshId id, const Array<Vector>& data)
|
|
|
|
|
{
|
|
|
|
|
uint64 offset = meshOffsets[id];
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2023-10-31 16:16:23 +01:00
|
|
|
std::memcpy(normalData.data() + offset, data.data(), data.size() * sizeof(Vector));
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::loadTangents(MeshId id, const Array<Vector>& data)
|
|
|
|
|
{
|
|
|
|
|
uint64 offset = meshOffsets[id];
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2023-10-31 16:16:23 +01:00
|
|
|
std::memcpy(tangentData.data() + offset, data.data(), data.size() * sizeof(Vector));
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data)
|
|
|
|
|
{
|
|
|
|
|
uint64 offset = meshOffsets[id];
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2023-10-31 16:16:23 +01:00
|
|
|
std::memcpy(biTangentData.data() + offset, data.data(), data.size() * sizeof(Vector));
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 22:39:17 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 13:38:49 +01:00
|
|
|
void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer)
|
|
|
|
|
{
|
|
|
|
|
uint64 offset = meshOffsets[id];
|
|
|
|
|
Array<Vector> pos(numVertices);
|
|
|
|
|
Array<Vector2> tex(numVertices);
|
|
|
|
|
Array<Vector> nor(numVertices);
|
|
|
|
|
Array<Vector> tan(numVertices);
|
|
|
|
|
Array<Vector> bit(numVertices);
|
2023-11-11 22:39:17 +01:00
|
|
|
Array<Vector> col(numVertices);
|
2023-11-01 13:38:49 +01:00
|
|
|
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());
|
2023-11-11 22:39:17 +01:00
|
|
|
std::copy(colorData.begin() + offset, colorData.begin() + offset + numVertices, col.begin());
|
2023-11-01 13:38:49 +01:00
|
|
|
Serialization::save(buffer, pos);
|
|
|
|
|
Serialization::save(buffer, tex);
|
|
|
|
|
Serialization::save(buffer, nor);
|
|
|
|
|
Serialization::save(buffer, tan);
|
|
|
|
|
Serialization::save(buffer, bit);
|
2023-11-11 22:39:17 +01:00
|
|
|
Serialization::save(buffer, col);
|
2023-11-01 13:38:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer)
|
|
|
|
|
{
|
|
|
|
|
Array<Vector> pos;
|
|
|
|
|
Array<Vector2> tex;
|
|
|
|
|
Array<Vector> nor;
|
|
|
|
|
Array<Vector> tan;
|
|
|
|
|
Array<Vector> bit;
|
2023-11-11 22:39:17 +01:00
|
|
|
Array<Vector> col;
|
2023-11-01 13:38:49 +01:00
|
|
|
Serialization::load(buffer, pos);
|
|
|
|
|
Serialization::load(buffer, tex);
|
|
|
|
|
Serialization::load(buffer, nor);
|
|
|
|
|
Serialization::load(buffer, tan);
|
|
|
|
|
Serialization::load(buffer, bit);
|
2023-11-11 22:39:17 +01:00
|
|
|
Serialization::load(buffer, col);
|
2023-11-01 13:38:49 +01:00
|
|
|
loadPositions(id, pos);
|
|
|
|
|
loadTexCoords(id, tex);
|
|
|
|
|
loadNormals(id, nor);
|
|
|
|
|
loadTangents(id, tan);
|
|
|
|
|
loadBiTangents(id, bit);
|
2023-11-22 13:18:54 +01:00
|
|
|
loadColors(id, col);
|
2023-11-01 13:38:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 16:16:23 +01:00
|
|
|
void StaticMeshVertexData::init(Gfx::PGraphics graphics)
|
|
|
|
|
{
|
|
|
|
|
VertexData::init(graphics);
|
2023-10-31 17:55:30 +01:00
|
|
|
descriptorLayout = graphics->createDescriptorLayout("StaticMeshDescriptorLayout");
|
|
|
|
|
descriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
|
|
|
|
descriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
|
|
|
|
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);
|
2023-11-11 22:39:17 +01:00
|
|
|
descriptorLayout->addDescriptorBinding(5, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
2023-10-31 17:55:30 +01:00
|
|
|
descriptorLayout->create();
|
|
|
|
|
descriptorSet = descriptorLayout->allocateDescriptorSet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::bindBuffers(Gfx::PRenderCommand)
|
|
|
|
|
{
|
|
|
|
|
// TODO: for legacy vertex buffer binding
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::PDescriptorLayout StaticMeshVertexData::getVertexDataLayout()
|
|
|
|
|
{
|
|
|
|
|
return descriptorLayout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::PDescriptorSet StaticMeshVertexData::getVertexDataSet()
|
|
|
|
|
{
|
|
|
|
|
return descriptorSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::resizeBuffers()
|
|
|
|
|
{
|
2023-10-31 16:16:23 +01:00
|
|
|
ShaderBufferCreateInfo createInfo = {
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2023-10-31 17:55:30 +01:00
|
|
|
.size = verticesAllocated * sizeof(Vector),
|
2023-10-31 16:16:23 +01:00
|
|
|
},
|
2023-11-15 17:42:57 +01:00
|
|
|
.numElements = verticesAllocated * 3,
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = true,
|
2023-10-31 16:16:23 +01:00
|
|
|
};
|
|
|
|
|
positions = graphics->createShaderBuffer(createInfo);
|
|
|
|
|
normals = graphics->createShaderBuffer(createInfo);
|
|
|
|
|
tangents = graphics->createShaderBuffer(createInfo);
|
|
|
|
|
biTangents = graphics->createShaderBuffer(createInfo);
|
2023-11-11 22:39:17 +01:00
|
|
|
colors = graphics->createShaderBuffer(createInfo);
|
2023-11-01 23:12:30 +01:00
|
|
|
createInfo.sourceData.size = verticesAllocated * sizeof(Vector2);
|
2023-11-15 17:42:57 +01:00
|
|
|
createInfo.numElements = verticesAllocated * 2;
|
2023-10-31 16:16:23 +01:00
|
|
|
texCoords = graphics->createShaderBuffer(createInfo);
|
2023-10-31 17:55:30 +01:00
|
|
|
|
|
|
|
|
positionData.resize(verticesAllocated);
|
|
|
|
|
texCoordsData.resize(verticesAllocated);
|
|
|
|
|
normalData.resize(verticesAllocated);
|
|
|
|
|
tangentData.resize(verticesAllocated);
|
|
|
|
|
biTangentData.resize(verticesAllocated);
|
2023-11-11 22:39:17 +01:00
|
|
|
colorData.resize(verticesAllocated);
|
2023-10-31 16:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StaticMeshVertexData::updateBuffers()
|
|
|
|
|
{
|
2023-11-01 23:12:30 +01:00
|
|
|
positions->updateContents(DataSource{
|
2023-10-31 16:16:23 +01:00
|
|
|
.size = positionData.size() * sizeof(Vector),
|
|
|
|
|
.data = (uint8*)positionData.data(),
|
|
|
|
|
});
|
2023-11-01 23:12:30 +01:00
|
|
|
texCoords->updateContents(DataSource{
|
2023-10-31 16:16:23 +01:00
|
|
|
.size = texCoordsData.size() * sizeof(Vector2),
|
|
|
|
|
.data = (uint8*)texCoordsData.data(),
|
|
|
|
|
});
|
2023-11-01 23:12:30 +01:00
|
|
|
normals->updateContents(DataSource{
|
2023-10-31 16:16:23 +01:00
|
|
|
.size = normalData.size() * sizeof(Vector),
|
|
|
|
|
.data = (uint8*)normalData.data(),
|
|
|
|
|
});
|
2023-11-01 23:12:30 +01:00
|
|
|
tangents->updateContents(DataSource{
|
2023-10-31 16:16:23 +01:00
|
|
|
.size = tangentData.size() * sizeof(Vector),
|
|
|
|
|
.data = (uint8*)tangentData.data(),
|
|
|
|
|
});
|
2023-11-01 23:12:30 +01:00
|
|
|
biTangents->updateContents(DataSource{
|
2023-10-31 16:16:23 +01:00
|
|
|
.size = biTangentData.size() * sizeof(Vector),
|
|
|
|
|
.data = (uint8*)biTangentData.data(),
|
|
|
|
|
});
|
2023-11-11 22:39:17 +01:00
|
|
|
colors->updateContents(DataSource{
|
|
|
|
|
.size = colorData.size() * sizeof(Vector),
|
|
|
|
|
.data = (uint8*)colorData.data()
|
|
|
|
|
});
|
2023-11-05 10:36:01 +01:00
|
|
|
descriptorLayout->reset();
|
2023-10-31 17:55:30 +01:00
|
|
|
descriptorSet = descriptorLayout->allocateDescriptorSet();
|
|
|
|
|
descriptorSet->updateBuffer(0, positions);
|
|
|
|
|
descriptorSet->updateBuffer(1, texCoords);
|
|
|
|
|
descriptorSet->updateBuffer(2, normals);
|
|
|
|
|
descriptorSet->updateBuffer(3, tangents);
|
|
|
|
|
descriptorSet->updateBuffer(4, biTangents);
|
2023-11-11 22:39:17 +01:00
|
|
|
descriptorSet->updateBuffer(5, colors);
|
2023-10-31 17:55:30 +01:00
|
|
|
descriptorSet->writeChanges();
|
2023-10-31 16:16:23 +01:00
|
|
|
}
|