Skip to content

Commit

Permalink
gi: improve mesh processing code
Browse files Browse the repository at this point in the history
  • Loading branch information
pablode committed Dec 8, 2024
1 parent 64db0df commit f0782e1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
23 changes: 13 additions & 10 deletions src/gi/impl/Gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace gtl
std::vector<glm::mat3x4> instanceTransforms;
const GiMaterial* material = nullptr;
GiScene* scene;
GiMeshDataCompressed cpuData;
GiMeshData cpuData;
std::optional<GiMeshGpuData> gpuData;
bool visible = true;
std::string name;
Expand Down Expand Up @@ -691,22 +691,25 @@ namespace gtl
// Build mesh BLAS & buffers if they don't exist yet
if (!mesh->gpuData.has_value())
{
const auto& data = giDecompressMeshData(mesh->cpuData);
std::vector<GiFace> meshFaces;
std::vector<GiVertex> meshVertices;
std::vector<GiPrimvarData> meshPrimvars;
giDecompressMeshData(mesh->cpuData, meshFaces, meshVertices, meshPrimvars);

if (data.faces.empty())
if (meshFaces.empty())
{
continue;
}

// Collect vertices
std::vector<rp::FVertex> vertexData;
std::vector<CgpuVertex> positionData;
vertexData.resize(data.vertices.size());
positionData.resize(data.vertices.size());
vertexData.resize(meshVertices.size());
positionData.resize(meshVertices.size());

for (uint32_t i = 0; i < positionData.size(); i++)
{
const GiVertex& cpuVert = data.vertices[i];
const GiVertex& cpuVert = meshVertices[i];
uint32_t encodedNormal = _EncodeDirection(glm::make_vec3(cpuVert.norm));
uint32_t encodedTangent = _EncodeDirection(glm::make_vec3(cpuVert.tangent));

Expand All @@ -722,11 +725,11 @@ namespace gtl

// Collect indices
std::vector<uint32_t> indexData;
indexData.reserve(data.faces.size() * 3);
indexData.reserve(meshFaces.size() * 3);

for (uint32_t i = 0; i < data.faces.size(); i++)
for (uint32_t i = 0; i < meshFaces.size(); i++)
{
const auto* face = &data.faces[i];
const auto* face = &meshFaces[i];
indexData.push_back(face->v_i[0]);
indexData.push_back(face->v_i[1]);
indexData.push_back(face->v_i[2]);
Expand All @@ -742,7 +745,7 @@ namespace gtl
{
const GiPrimvarData* primvar = nullptr;

for (const GiPrimvarData& p : data.primvars)
for (const GiPrimvarData& p : meshPrimvars)
{
// FIXME: we should check if scene data and primvar types match to prevent crashes
if (p.name == sceneDataName && !p.data.empty())
Expand Down
29 changes: 15 additions & 14 deletions src/gi/impl/MeshProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ namespace
return std::move(data);
}

GiMeshDataCompressed _CompressData(const std::vector<GiFace>& faces,
const std::vector<GiVertex>& vertices,
const std::vector<GiPrimvarData>& primvars)
GiMeshData _CompressData(const std::vector<GiFace>& faces,
const std::vector<GiVertex>& vertices,
const std::vector<GiPrimvarData>& primvars)
{
auto logBufferCompression = [](const std::string_view name, const GiMeshBuffer& buf)
{
Expand All @@ -109,7 +109,7 @@ namespace
buf.data.size(), float(buf.uncompressedSize) / float(buf.data.size()));
};

GiMeshDataCompressed m;
GiMeshData m;
m.faces = _CompressMeshBuffer(faces);
m.vertices = _CompressMeshBuffer(vertices);

Expand Down Expand Up @@ -138,9 +138,9 @@ namespace

namespace gtl
{
GiMeshDataCompressed giProcessMeshData(const std::vector<GiFace>& faces,
const std::vector<GiVertex>& vertices,
const std::vector<GiPrimvarData>& primvars)
GiMeshData giProcessMeshData(const std::vector<GiFace>& faces,
const std::vector<GiVertex>& vertices,
const std::vector<GiPrimvarData>& primvars)
{
// Remap vertices & compress data.

Expand Down Expand Up @@ -207,15 +207,18 @@ namespace gtl
return _CompressData(newFaces, newVertices, newPrimvars);
}

GiMeshData giDecompressMeshData(const GiMeshDataCompressed& m)
void giDecompressMeshData(const GiMeshData& cmd,
std::vector<GiFace>& faces,
std::vector<GiVertex>& vertices,
std::vector<GiPrimvarData>& primvars)
{
std::vector<GiFace> faces = _DecompressMeshBuffer<GiFace>(m.faces);
std::vector<GiVertex> vertices = _DecompressMeshBuffer<GiVertex>(m.vertices);
faces = _DecompressMeshBuffer<GiFace>(cmd.faces);
vertices = _DecompressMeshBuffer<GiVertex>(cmd.vertices);

std::vector<GiPrimvarData> primvars(m.primvars.size());
primvars.resize(cmd.primvars.size());
for (size_t i = 0; i < primvars.size(); i++)
{
const auto& p = m.primvars[i];
const auto& p = cmd.primvars[i];

primvars[i] = GiPrimvarData {
.name = p.name,
Expand All @@ -224,7 +227,5 @@ namespace gtl
.data = _DecompressMeshBuffer<uint8_t>(p.buffer)
};
}

return GiMeshData { faces, primvars, vertices };
}
}
22 changes: 8 additions & 14 deletions src/gi/impl/MeshProcessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@

namespace gtl
{
struct GiMeshDataCompressed;

struct GiMeshData
{
std::vector<GiFace> faces;
std::vector<GiPrimvarData> primvars;
std::vector<GiVertex> vertices;
};

struct GiMeshBuffer
{
bool isCompressed;
Expand All @@ -47,7 +38,7 @@ namespace gtl
GiMeshBuffer buffer;
};

struct GiMeshDataCompressed
struct GiMeshData
{
GiMeshBuffer faces;
GiMeshBuffer vertices;
Expand All @@ -56,9 +47,12 @@ namespace gtl
uint32_t vertexCount;
};

GiMeshDataCompressed giProcessMeshData(const std::vector<GiFace>& faces,
const std::vector<GiVertex>& vertices,
const std::vector<GiPrimvarData>& primvars);
GiMeshData giProcessMeshData(const std::vector<GiFace>& faces,
const std::vector<GiVertex>& vertices,
const std::vector<GiPrimvarData>& primvars);

GiMeshData giDecompressMeshData(const GiMeshDataCompressed& data);
void giDecompressMeshData(const GiMeshData& cmd,
std::vector<GiFace>& faces,
std::vector<GiVertex>& vertices,
std::vector<GiPrimvarData>& primvars);
}

0 comments on commit f0782e1

Please sign in to comment.