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"
|
2024-09-16 13:00:53 +02:00
|
|
|
#include "Graphics/VertexData.h"
|
2024-05-08 10:51:59 +02:00
|
|
|
#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-10-01 11:15:38 +02:00
|
|
|
void StaticMeshVertexData::loadPositions(uint64 offset, const Array<Vector>& data) {
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2024-10-01 11:15:38 +02:00
|
|
|
std::memcpy(posData.data() + offset, data.data(), data.size() * sizeof(Vector));
|
|
|
|
|
// positions->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
|
2023-10-31 16:16:23 +01:00
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 11:15:38 +02:00
|
|
|
void StaticMeshVertexData::loadTexCoords(uint64 offset, uint64 index, const Array<U16Vector2>& data) {
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2024-10-01 11:15:38 +02:00
|
|
|
std::memcpy(texData[index].data() + offset, data.data(), data.size() * sizeof(U16Vector2));
|
|
|
|
|
// texCoords[index]->updateContents(offset * sizeof(Vector2), data.size() * sizeof(Vector2), data.data());
|
2023-10-31 16:16:23 +01:00
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 20:47:57 +01:00
|
|
|
void StaticMeshVertexData::loadNormals(uint64 offset, const Array<uint32>& data) {
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2024-12-21 20:47:57 +01:00
|
|
|
std::memcpy(norData.data() + offset, data.data(), data.size() * sizeof(uint32));
|
2024-08-08 22:14:25 +02:00
|
|
|
// normals->updateContents(offset * sizeof(Vector4), data.size() * sizeof(Vector4), data.data());
|
2023-10-31 16:16:23 +01:00
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 11:15:38 +02:00
|
|
|
void StaticMeshVertexData::loadColors(uint64 offset, const Array<U16Vector>& data) {
|
2023-11-05 11:47:22 +01:00
|
|
|
assert(offset + data.size() <= head);
|
2024-10-01 11:15:38 +02:00
|
|
|
std::memcpy(colData.data() + offset, data.data(), data.size() * sizeof(U16Vector));
|
|
|
|
|
// 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-10-01 11:15:38 +02:00
|
|
|
Array<U16Vector2> 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);
|
2024-10-01 11:15:38 +02:00
|
|
|
std::memcpy(tex[i].data(), texData[i].data() + offset, numVertices * sizeof(U16Vector2));
|
2024-05-06 18:36:16 +02:00
|
|
|
Serialization::save(buffer, tex[i]);
|
|
|
|
|
}
|
2024-10-01 11:15:38 +02:00
|
|
|
Array<Vector> pos(numVertices);
|
2024-12-21 20:47:57 +01:00
|
|
|
Array<uint32> nor(numVertices);
|
2024-10-01 11:15:38 +02:00
|
|
|
Array<U16Vector> col(numVertices);
|
|
|
|
|
std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(Vector));
|
2024-12-21 20:47:57 +01:00
|
|
|
std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(uint32));
|
2024-10-01 11:15:38 +02:00
|
|
|
std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(U16Vector));
|
2023-11-01 13:38:49 +01:00
|
|
|
Serialization::save(buffer, pos);
|
|
|
|
|
Serialization::save(buffer, nor);
|
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-10-01 11:15:38 +02:00
|
|
|
Array<U16Vector2> 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-10-01 11:15:38 +02:00
|
|
|
result += tex[i].size() * sizeof(U16Vector2);
|
2024-05-06 18:36:16 +02:00
|
|
|
}
|
2024-10-01 11:15:38 +02:00
|
|
|
Array<Vector> pos;
|
2024-12-21 20:47:57 +01:00
|
|
|
Array<uint32> nor;
|
2024-10-01 11:15:38 +02:00
|
|
|
Array<U16Vector> col;
|
2023-11-01 13:38:49 +01:00
|
|
|
Serialization::load(buffer, pos);
|
|
|
|
|
Serialization::load(buffer, nor);
|
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);
|
|
|
|
|
loadColors(offset, col);
|
2024-10-01 11:15:38 +02:00
|
|
|
result += pos.size() * sizeof(Vector);
|
2024-12-21 20:47:57 +01:00
|
|
|
result += nor.size() * sizeof(uint32);
|
2024-10-01 11:15:38 +02:00
|
|
|
result += col.size() * sizeof(U16Vector);
|
2024-08-07 21:19:33 +02:00
|
|
|
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);
|
2024-08-08 22:14:25 +02:00
|
|
|
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});
|
2024-09-27 15:57:37 +02:00
|
|
|
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2024-10-01 16:56:04 +02:00
|
|
|
.binding = 3,
|
2024-09-27 15:57:37 +02:00
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
|
|
|
.descriptorCount = MAX_TEXCOORDS,
|
|
|
|
|
});
|
2024-08-08 22:14:25 +02:00
|
|
|
descriptorLayout->create();
|
|
|
|
|
descriptorSet = descriptorLayout->allocateDescriptorSet();
|
|
|
|
|
}
|
2024-05-09 08:41:46 +02:00
|
|
|
|
2024-08-08 22:14:25 +02:00
|
|
|
void StaticMeshVertexData::destroy() {
|
|
|
|
|
VertexData::destroy();
|
|
|
|
|
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
|
|
|
|
|
texCoords[i] = nullptr;
|
|
|
|
|
}
|
|
|
|
|
positions = nullptr;
|
|
|
|
|
normals = 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);
|
|
|
|
|
colData.resize(verticesAllocated);
|
2024-08-08 22:14:25 +02:00
|
|
|
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
|
2024-08-08 22:53:49 +02:00
|
|
|
texData[i].resize(verticesAllocated);
|
2024-08-08 22:14:25 +02:00
|
|
|
}
|
2023-10-31 16:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void StaticMeshVertexData::updateBuffers() {
|
2024-10-01 11:15:38 +02:00
|
|
|
positions = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
2024-08-08 22:53:49 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
2024-10-01 11:15:38 +02:00
|
|
|
.size = verticesAllocated * sizeof(Vector),
|
2024-08-08 22:53:49 +02:00
|
|
|
.data = (uint8*)posData.data(),
|
|
|
|
|
},
|
2024-12-25 14:59:08 +01:00
|
|
|
.usage = Gfx::SE_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR,
|
2024-08-08 22:53:49 +02:00
|
|
|
.name = "Positions",
|
2024-10-01 11:15:38 +02:00
|
|
|
});
|
|
|
|
|
normals = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
|
|
|
|
.sourceData =
|
|
|
|
|
{
|
2024-12-21 20:47:57 +01:00
|
|
|
.size = verticesAllocated * sizeof(uint32),
|
2024-10-01 11:15:38 +02:00
|
|
|
.data = (uint8*)norData.data(),
|
|
|
|
|
},
|
|
|
|
|
.name = "Normals",
|
|
|
|
|
});
|
|
|
|
|
colors = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
|
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = verticesAllocated * sizeof(U16Vector),
|
|
|
|
|
.data = (uint8*)colData.data(),
|
|
|
|
|
},
|
|
|
|
|
.name = "Colors",
|
|
|
|
|
});
|
2024-06-09 12:20:04 +02:00
|
|
|
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
|
2024-10-01 11:15:38 +02:00
|
|
|
texCoords[i] = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
|
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = verticesAllocated * sizeof(U16Vector2),
|
|
|
|
|
.data = (uint8*)texData[i].data(),
|
|
|
|
|
},
|
|
|
|
|
.name = "TexCoords",
|
|
|
|
|
});
|
2024-05-06 18:36:16 +02:00
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
descriptorLayout->reset();
|
2023-10-31 17:55:30 +01:00
|
|
|
descriptorSet = descriptorLayout->allocateDescriptorSet();
|
2024-09-27 15:57:37 +02:00
|
|
|
descriptorSet->updateBuffer(0, 0, positions);
|
|
|
|
|
descriptorSet->updateBuffer(1, 0, normals);
|
2024-10-01 11:15:38 +02:00
|
|
|
descriptorSet->updateBuffer(2, 0, colors);
|
2024-05-06 18:36:16 +02:00
|
|
|
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
|
2024-10-01 11:15:38 +02:00
|
|
|
descriptorSet->updateBuffer(3, i, texCoords[i]);
|
2024-05-06 18:36:16 +02:00
|
|
|
}
|
2023-10-31 17:55:30 +01:00
|
|
|
descriptorSet->writeChanges();
|
2023-10-31 16:16:23 +01:00
|
|
|
}
|