Files
Seele/src/Engine/Graphics/StaticMeshVertexData.cpp
T

244 lines
9.1 KiB
C++
Raw Normal View History

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"
#include "Mesh.h"
2023-10-07 19:29:53 +02:00
using namespace Seele;
2023-10-26 18:37:29 +02:00
extern List<VertexData*> vertexDataList;
2024-06-09 12:20:04 +02:00
StaticMeshVertexData::StaticMeshVertexData() { vertexDataList.add(this); }
2023-10-07 19:29:53 +02:00
2024-06-09 12:20:04 +02:00
StaticMeshVertexData::~StaticMeshVertexData() {}
2023-10-07 19:29:53 +02:00
2024-06-09 12:20:04 +02:00
StaticMeshVertexData* StaticMeshVertexData::getInstance() {
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
2024-06-25 08:59:09 +02:00
void StaticMeshVertexData::loadPositions(uint64 offset, const Array<Vector4>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-06-13 22:47:51 +02:00
std::memcpy(positionData.data() + offset, data.data(), data.size() * sizeof(Vector4));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-06-25 08:59:09 +02:00
void StaticMeshVertexData::loadTexCoords(uint64 offset, uint64 index, const Array<Vector2>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-05-06 18:36:16 +02:00
std::memcpy(texCoordsData[index].data() + offset, data.data(), data.size() * sizeof(Vector2));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-06-25 08:59:09 +02:00
void StaticMeshVertexData::loadNormals(uint64 offset, const Array<Vector4>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-06-13 22:47:51 +02:00
std::memcpy(normalData.data() + offset, data.data(), data.size() * sizeof(Vector4));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-06-25 08:59:09 +02:00
void StaticMeshVertexData::loadTangents(uint64 offset, const Array<Vector4>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-06-13 22:47:51 +02:00
std::memcpy(tangentData.data() + offset, data.data(), data.size() * sizeof(Vector4));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-06-25 08:59:09 +02:00
void StaticMeshVertexData::loadBiTangents(uint64 offset, const Array<Vector4>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-06-13 22:47:51 +02:00
std::memcpy(biTangentData.data() + offset, data.data(), data.size() * sizeof(Vector4));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-06-25 08:59:09 +02:00
void Seele::StaticMeshVertexData::loadColors(uint64 offset, const Array<Vector4>& data) {
2023-11-11 22:39:17 +01:00
assert(offset + data.size() <= head);
2024-06-13 22:47:51 +02:00
std::memcpy(colorData.data() + offset, data.data(), data.size() * sizeof(Vector4));
2023-11-11 22:39:17 +01:00
dirty = true;
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) {
2024-05-01 14:10:52 +02:00
uint64 offset;
{
2024-06-25 08:59:09 +02:00
std::unique_lock l(vertexDataLock);
2024-05-01 14:10:52 +02:00
offset = meshOffsets[id];
}
2024-06-13 22:47:51 +02:00
Array<Vector4> pos(numVertices);
2024-05-06 18:36:16 +02:00
Array<Vector2> tex[MAX_TEXCOORDS];
2024-06-09 12:20:04 +02:00
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-05-06 18:36:16 +02:00
tex[i].resize(numVertices);
std::memcpy(tex[i].data(), texCoordsData[i].data() + offset, numVertices * sizeof(Vector2));
Serialization::save(buffer, tex[i]);
}
2024-06-13 22:47:51 +02:00
Array<Vector4> nor(numVertices);
Array<Vector4> tan(numVertices);
Array<Vector4> bit(numVertices);
Array<Vector4> col(numVertices);
std::memcpy(pos.data(), positionData.data() + offset, numVertices * sizeof(Vector4));
std::memcpy(nor.data(), normalData.data() + offset, numVertices * sizeof(Vector4));
std::memcpy(tan.data(), tangentData.data() + offset, numVertices * sizeof(Vector4));
std::memcpy(bit.data(), biTangentData.data() + offset, numVertices * sizeof(Vector4));
std::memcpy(col.data(), colorData.data() + offset, numVertices * sizeof(Vector4));
2023-11-01 13:38:49 +01:00
Serialization::save(buffer, pos);
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
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
2024-06-25 08:59:09 +02:00
uint64 offset;
{
std::unique_lock l(vertexDataLock);
offset = meshOffsets[id];
}
2024-06-13 22:47:51 +02:00
Array<Vector4> pos;
2024-05-06 18:36:16 +02:00
Array<Vector2> tex[MAX_TEXCOORDS];
2024-06-09 12:20:04 +02:00
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-05-06 18:36:16 +02:00
Serialization::load(buffer, tex[i]);
2024-06-25 08:59:09 +02:00
loadTexCoords(offset, i, tex[i]);
2024-05-06 18:36:16 +02:00
}
2024-06-13 22:47:51 +02:00
Array<Vector4> nor;
Array<Vector4> tan;
Array<Vector4> bit;
Array<Vector4> col;
2023-11-01 13:38:49 +01:00
Serialization::load(buffer, pos);
Serialization::load(buffer, nor);
Serialization::load(buffer, tan);
Serialization::load(buffer, bit);
2023-11-11 22:39:17 +01:00
Serialization::load(buffer, col);
2024-06-25 08:59:09 +02:00
loadPositions(offset, pos);
loadNormals(offset, nor);
loadTangents(offset, tan);
loadBiTangents(offset, bit);
loadColors(offset, col);
2023-11-01 13:38:49 +01:00
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::init(Gfx::PGraphics _graphics) {
2023-12-22 19:46:07 +01:00
VertexData::init(_graphics);
2024-04-19 18:23:36 +02:00
descriptorLayout = _graphics->createDescriptorLayout("pVertexData");
2024-06-09 12:20:04 +02:00
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER});
descriptorLayout->addDescriptorBinding(
2024-06-13 22:47:51 +02:00
Gfx::DescriptorBinding{.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = MAX_TEXCOORDS,});
2023-10-31 17:55:30 +01:00
descriptorLayout->create();
descriptorSet = descriptorLayout->allocateDescriptorSet();
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::destroy() {
2023-12-12 11:37:00 +01:00
VertexData::destroy();
positions = nullptr;
2024-06-09 12:20:04 +02:00
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-05-06 18:36:16 +02:00
texCoords[i] = nullptr;
}
2023-12-12 11:37:00 +01:00
normals = nullptr;
tangents = nullptr;
biTangents = nullptr;
colors = nullptr;
descriptorLayout = nullptr;
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::bindBuffers(Gfx::PRenderCommand) {
2023-10-31 17:55:30 +01:00
// TODO: for legacy vertex buffer binding
}
2024-06-09 12:20:04 +02:00
Gfx::PDescriptorLayout StaticMeshVertexData::getVertexDataLayout() { return descriptorLayout; }
2023-10-31 17:55:30 +01:00
2024-06-09 12:20:04 +02:00
Gfx::PDescriptorSet StaticMeshVertexData::getVertexDataSet() { return descriptorSet; }
2023-10-31 17:55:30 +01:00
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::resizeBuffers() {
2023-10-31 16:16:23 +01:00
ShaderBufferCreateInfo createInfo = {
2024-06-09 12:20:04 +02:00
.sourceData =
{
2024-06-13 22:47:51 +02:00
.size = verticesAllocated * sizeof(Vector4),
2024-06-09 12:20:04 +02:00
},
2024-06-13 22:47:51 +02:00
.numElements = verticesAllocated,
2024-05-15 15:27:13 +02:00
.dynamic = false,
2024-06-09 10:44:24 +02:00
.vertexBuffer = 1,
2024-04-20 21:35:43 +02:00
.name = "Positions",
2023-10-31 16:16:23 +01:00
};
positions = graphics->createShaderBuffer(createInfo);
2024-06-09 10:44:24 +02:00
createInfo.vertexBuffer = false;
2024-04-20 21:35:43 +02:00
createInfo.name = "Normals";
2023-10-31 16:16:23 +01:00
normals = graphics->createShaderBuffer(createInfo);
2024-04-20 21:35:43 +02:00
createInfo.name = "Tangents";
2023-10-31 16:16:23 +01:00
tangents = graphics->createShaderBuffer(createInfo);
2024-04-20 21:35:43 +02:00
createInfo.name = "BiTangents";
2023-10-31 16:16:23 +01:00
biTangents = graphics->createShaderBuffer(createInfo);
2024-04-20 21:35:43 +02:00
createInfo.name = "Colors";
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);
2024-04-20 21:35:43 +02:00
createInfo.name = "TexCoords";
2024-06-09 12:20:04 +02:00
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-05-06 18:36:16 +02:00
texCoords[i] = graphics->createShaderBuffer(createInfo);
texCoordsData[i].resize(verticesAllocated);
}
2024-05-09 08:41:46 +02:00
2023-10-31 17:55:30 +01:00
positionData.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
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::updateBuffers() {
2024-05-15 15:27:13 +02:00
positions->updateContents(ShaderBufferCreateInfo{
.sourceData{
2024-06-13 22:47:51 +02:00
.size = positionData.size() * sizeof(Vector4),
2024-05-15 15:27:13 +02:00
.data = (uint8*)positionData.data(),
},
.numElements = positionData.size(),
2024-06-09 12:20:04 +02:00
});
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
texCoords[i]->updateContents(ShaderBufferCreateInfo{
.sourceData =
{
.size = texCoordsData[i].size() * sizeof(Vector2),
.data = (uint8*)texCoordsData[i].data(),
},
2024-05-15 15:27:13 +02:00
.numElements = texCoordsData[i].size(),
2024-06-09 12:20:04 +02:00
});
2024-05-06 18:36:16 +02:00
}
2024-06-09 12:20:04 +02:00
normals->updateContents(ShaderBufferCreateInfo{
.sourceData =
{
2024-06-13 22:47:51 +02:00
.size = normalData.size() * sizeof(Vector4),
2024-06-09 12:20:04 +02:00
.data = (uint8*)normalData.data(),
},
2024-05-15 15:27:13 +02:00
.numElements = normalData.size(),
2024-06-09 12:20:04 +02:00
});
2024-06-13 22:47:51 +02:00
tangents->updateContents(ShaderBufferCreateInfo{
.sourceData =
{
.size = tangentData.size() * sizeof(Vector4),
.data = (uint8*)tangentData.data(),
},
.numElements = tangentData.size(),
});
2024-06-09 12:20:04 +02:00
biTangents->updateContents(ShaderBufferCreateInfo{
.sourceData =
{
2024-06-13 22:47:51 +02:00
.size = biTangentData.size() * sizeof(Vector4),
2024-06-09 12:20:04 +02:00
.data = (uint8*)biTangentData.data(),
},
2024-05-15 15:27:13 +02:00
.numElements = biTangentData.size(),
2024-06-09 12:20:04 +02:00
});
colors->updateContents(ShaderBufferCreateInfo{
2024-06-13 22:47:51 +02:00
.sourceData =
{
.size = colorData.size() * sizeof(Vector4),
.data = (uint8*)colorData.data(),
},
2024-05-15 15:27:13 +02:00
.numElements = colorData.size(),
2024-06-09 12:20:04 +02:00
});
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);
2024-05-06 18:36:16 +02:00
descriptorSet->updateBuffer(1, normals);
descriptorSet->updateBuffer(2, tangents);
descriptorSet->updateBuffer(3, biTangents);
descriptorSet->updateBuffer(4, colors);
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
descriptorSet->updateBuffer(5, i, texCoords[i]);
}
2023-10-31 17:55:30 +01:00
descriptorSet->writeChanges();
2023-10-31 16:16:23 +01:00
}