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

230 lines
9.0 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"
2024-08-07 21:19:33 +02:00
#include <fstream>
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-08-08 22:53:49 +02:00
std::memcpy(posData.data() + offset, data.data(), data.size() * sizeof(Vector4));
//positions->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
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-08-08 22:53:49 +02:00
std::memcpy(texData[index].data() + offset, data.data(), data.size() * sizeof(Vector2));
//texCoords[index]->updateContents(offset * sizeof(Vector2), data.size() * sizeof(Vector2), data.data());
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-08-08 22:53:49 +02:00
std::memcpy(norData.data() + offset, data.data(), data.size() * sizeof(Vector4));
// normals->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
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-08-08 22:53:49 +02:00
std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(Vector4));
//tangents->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
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-08-08 22:53:49 +02:00
std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(Vector4));
// biTangents->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
2023-10-31 16:16:23 +01:00
dirty = true;
}
void StaticMeshVertexData::loadColors(uint64 offset, const Array<Vector4>& data) {
2023-11-11 22:39:17 +01:00
assert(offset + data.size() <= head);
2024-08-08 22:53:49 +02:00
std::memcpy(colData.data() + offset, data.data(), data.size() * sizeof(Vector4));
//colors->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
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-07-18 11:45:56 +02:00
VertexData::serializeMesh(id, numVertices, 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);
texCoords[i]->readContents(offset * sizeof(Vector2), numVertices * sizeof(Vector2), tex[i].data());
2024-05-06 18:36:16 +02:00
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);
positions->readContents(offset * sizeof(Vector4), numVertices * sizeof(Vector4), pos.data());
normals->readContents(offset * sizeof(Vector4), numVertices * sizeof(Vector4), nor.data());
tangents->readContents(offset * sizeof(Vector4), numVertices * sizeof(Vector4), tan.data());
biTangents->readContents(offset * sizeof(Vector4), numVertices * sizeof(Vector4), bit.data());
colors->readContents(offset * sizeof(Vector4), numVertices * sizeof(Vector4), col.data());
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-08-07 21:19:33 +02:00
uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
uint64 result = VertexData::deserializeMesh(id, buffer);
2024-06-25 08:59:09 +02:00
uint64 offset;
{
std::unique_lock l(vertexDataLock);
offset = meshOffsets[id];
}
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-08-07 21:19:33 +02:00
result += tex[i].size() * sizeof(Vector2);
2024-05-06 18:36:16 +02:00
}
Array<Vector4> pos;
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);
2024-08-07 21:19:33 +02:00
result += pos.size() * sizeof(Vector4);
result += nor.size() * sizeof(Vector4);
result += tan.size() * sizeof(Vector4);
result += bit.size() * sizeof(Vector4);
result += col.size() * sizeof(Vector4);
return result;
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);
descriptorLayout = _graphics->createDescriptorLayout("pVertexData");
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(Gfx::DescriptorBinding{
.binding = 5,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = MAX_TEXCOORDS,
});
descriptorLayout->create();
descriptorSet = descriptorLayout->allocateDescriptorSet();
}
2024-05-09 08:41:46 +02:00
void StaticMeshVertexData::destroy() {
VertexData::destroy();
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
texCoords[i] = nullptr;
}
positions = nullptr;
normals = nullptr;
tangents = nullptr;
biTangents = nullptr;
colors = nullptr;
descriptorSet = nullptr;
descriptorLayout = nullptr;
}
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() {
2024-08-08 22:53:49 +02:00
posData.resize(verticesAllocated);
norData.resize(verticesAllocated);
tanData.resize(verticesAllocated);
bitData.resize(verticesAllocated);
colData.resize(verticesAllocated);
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-08-08 22:53:49 +02:00
texData[i].resize(verticesAllocated);
}
2023-10-31 16:16:23 +01:00
}
2024-06-09 12:20:04 +02:00
void StaticMeshVertexData::updateBuffers() {
2024-08-08 22:53:49 +02:00
ShaderBufferCreateInfo createInfo = {
.sourceData =
{
.size = verticesAllocated * sizeof(Vector4),
.data = (uint8*)posData.data(),
},
.numElements = verticesAllocated,
.dynamic = true,
.vertexBuffer = true,
.name = "Positions",
};
positions = graphics->createShaderBuffer(createInfo);
createInfo.vertexBuffer = false;
createInfo.name = "Normals";
createInfo.sourceData.data = (uint8*)norData.data();
normals = graphics->createShaderBuffer(createInfo);
createInfo.name = "Tangents";
createInfo.sourceData.data = (uint8*)tanData.data();
tangents = graphics->createShaderBuffer(createInfo);
createInfo.name = "BiTangents";
createInfo.sourceData.data = (uint8*)bitData.data();
biTangents = graphics->createShaderBuffer(createInfo);
createInfo.name = "Colors";
createInfo.sourceData.data = (uint8*)colData.data();
colors = graphics->createShaderBuffer(createInfo);
createInfo.sourceData.size = verticesAllocated * sizeof(Vector2);
createInfo.name = "TexCoords";
2024-06-09 12:20:04 +02:00
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-08-08 22:53:49 +02:00
createInfo.sourceData.data = (uint8*)texData[i].data();
texCoords[i] = graphics->createShaderBuffer(createInfo);
2024-05-06 18:36:16 +02:00
}
2024-08-08 22:53:49 +02:00
posData.clear();
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
texData[i].clear();
}
norData.clear();
tanData.clear();
bitData.clear();
colData.clear();
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
}