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

251 lines
8.9 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"
2024-09-16 13:00:53 +02:00
#include "Graphics/VertexData.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-12-27 17:06:43 +01:00
void StaticMeshVertexData::loadPositions(uint64 offset, const Array<PositionType>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-12-27 17:06:43 +01:00
std::memcpy(posData.data() + offset, data.data(), data.size() * sizeof(PositionType));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-12-27 17:06:43 +01:00
void StaticMeshVertexData::loadTexCoords(uint64 offset, uint64 index, const Array<TexCoordType>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-12-27 17:06:43 +01:00
std::memcpy(texData[index].data() + offset, data.data(), data.size() * sizeof(TexCoordType));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-12-27 17:06:43 +01:00
void StaticMeshVertexData::loadNormals(uint64 offset, const Array<NormalType>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-12-27 17:06:43 +01:00
std::memcpy(norData.data() + offset, data.data(), data.size() * sizeof(NormalType));
2023-10-31 16:16:23 +01:00
dirty = true;
}
2024-12-27 17:06:43 +01:00
void StaticMeshVertexData::loadTangents(uint64 offset, const Array<TangentType>& data) {
2023-11-05 11:47:22 +01:00
assert(offset + data.size() <= head);
2024-12-27 17:06:43 +01:00
std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType));
dirty = true;
}
void StaticMeshVertexData::loadBitangents(uint64 offset, const Array<BiTangentType>& data) {
assert(offset + data.size() <= head);
std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType));
dirty = true;
}
void StaticMeshVertexData::loadColors(uint64 offset, const Array<ColorType>& data) {
assert(offset + data.size() <= head);
std::memcpy(colData.data() + offset, data.data(), data.size() * sizeof(ColorType));
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-12-27 17:06:43 +01:00
Array<TexCoordType> 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-12-27 17:06:43 +01:00
std::memcpy(tex[i].data(), texData[i].data() + offset, numVertices * sizeof(TexCoordType));
2024-05-06 18:36:16 +02:00
Serialization::save(buffer, tex[i]);
}
2024-12-27 17:06:43 +01:00
Array<PositionType> pos(numVertices);
Array<NormalType> nor(numVertices);
Array<TangentType> tan(numVertices);
Array<BiTangentType> bit(numVertices);
Array<ColorType> col(numVertices);
std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(PositionType));
std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(NormalType));
std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType));
std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType));
std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(ColorType));
2023-11-01 13:38:49 +01:00
Serialization::save(buffer, pos);
Serialization::save(buffer, nor);
2024-12-27 17:06:43 +01:00
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-12-27 17:06:43 +01:00
Array<TexCoordType> 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-12-27 17:06:43 +01:00
result += tex[i].size() * sizeof(TexCoordType);
2024-05-06 18:36:16 +02:00
}
2024-12-27 17:06:43 +01:00
Array<PositionType> pos;
Array<NormalType> nor;
Array<TangentType> tan;
Array<BiTangentType> bit;
Array<ColorType> col;
2023-11-01 13:38:49 +01:00
Serialization::load(buffer, pos);
Serialization::load(buffer, nor);
2024-12-27 17:06:43 +01:00
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);
2024-12-27 17:06:43 +01:00
loadTangents(offset, tan);
loadBitangents(offset, bit);
2024-06-25 08:59:09 +02:00
loadColors(offset, col);
2024-12-27 17:06:43 +01:00
result += pos.size() * sizeof(PositionType);
result += nor.size() * sizeof(NormalType);
result += tan.size() * sizeof(TangentType);
result += bit.size() * sizeof(BiTangentType);
result += col.size() * sizeof(ColorType);
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);
descriptorLayout = _graphics->createDescriptorLayout("pVertexData");
2024-12-27 17:06:43 +01: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,
});
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,
2024-12-27 17:06:43 +01:00
});
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,
2024-09-27 15:57:37 +02:00
.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;
2024-12-27 17:06:43 +01:00
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);
2024-12-27 17:06:43 +01:00
tanData.resize(verticesAllocated);
bitData.resize(verticesAllocated);
2024-08-08 22:53:49 +02:00
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-10-01 11:15:38 +02:00
positions = graphics->createShaderBuffer(ShaderBufferCreateInfo{
2024-08-08 22:53:49 +02:00
.sourceData =
{
2024-12-27 17:06:43 +01:00
.size = verticesAllocated * sizeof(PositionType),
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-27 17:06:43 +01:00
.size = verticesAllocated * sizeof(NormalType),
2024-10-01 11:15:38 +02:00
.data = (uint8*)norData.data(),
},
.name = "Normals",
});
2024-12-27 17:06:43 +01:00
tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = verticesAllocated * sizeof(TangentType),
.data = (uint8*)tanData.data(),
},
.name = "Tangents",
});
biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = verticesAllocated * sizeof(BiTangentType),
.data = (uint8*)bitData.data(),
},
.name = "BiTangents",
});
2024-10-01 11:15:38 +02:00
colors = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
2024-12-27 17:06:43 +01:00
.size = verticesAllocated * sizeof(ColorType),
2024-10-01 11:15:38 +02:00
.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 =
{
2024-12-27 17:06:43 +01:00
.size = verticesAllocated * sizeof(TexCoordType),
2024-10-01 11:15:38 +02:00
.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-12-27 17:06:43 +01:00
descriptorSet->updateBuffer(2, 0, tangents);
descriptorSet->updateBuffer(3, 0, biTangents);
descriptorSet->updateBuffer(4, 0, colors);
2024-05-06 18:36:16 +02:00
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
2024-12-27 17:06:43 +01:00
descriptorSet->updateBuffer(5, 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
}