CVertexBuffer: Make use of std::array

Same behavior, but without hardcoded array sizes.
This commit is contained in:
Lioncash 2020-06-16 16:13:19 -04:00
parent 09f5163184
commit d081dd87cf
2 changed files with 94 additions and 64 deletions

View File

@ -21,32 +21,45 @@ CVertexBuffer::~CVertexBuffer()
CVertexArrayManager::DeleteAllArraysForVBO(this); CVertexArrayManager::DeleteAllArraysForVBO(this);
if (mBuffered) if (mBuffered)
glDeleteBuffers(14, mAttribBuffers); glDeleteBuffers(static_cast<GLsizei>(mAttribBuffers.size()), mAttribBuffers.data());
} }
uint16 CVertexBuffer::AddVertex(const CVertex& rkVtx) uint16 CVertexBuffer::AddVertex(const CVertex& rkVtx)
{ {
if (mPositions.size() == 0xFFFF) throw std::overflow_error("VBO contains too many vertices"); if (mPositions.size() == 0xFFFF)
throw std::overflow_error("VBO contains too many vertices");
if (mVtxDesc & EVertexAttribute::Position) mPositions.push_back(rkVtx.Position); if (mVtxDesc & EVertexAttribute::Position)
if (mVtxDesc & EVertexAttribute::Normal) mNormals.push_back(rkVtx.Normal); mPositions.push_back(rkVtx.Position);
if (mVtxDesc & EVertexAttribute::Color0) mColors[0].push_back(rkVtx.Color[0]); if (mVtxDesc & EVertexAttribute::Normal)
if (mVtxDesc & EVertexAttribute::Color1) mColors[1].push_back(rkVtx.Color[1]); mNormals.push_back(rkVtx.Normal);
if (mVtxDesc & EVertexAttribute::Color0)
mColors[0].push_back(rkVtx.Color[0]);
if (mVtxDesc & EVertexAttribute::Color1)
mColors[1].push_back(rkVtx.Color[1]);
for (uint32 iTex = 0; iTex < 8; iTex++) for (size_t iTex = 0; iTex < mTexCoords.size(); iTex++)
if (mVtxDesc & (EVertexAttribute::Tex0 << iTex)) mTexCoords[iTex].push_back(rkVtx.Tex[iTex]); {
if (mVtxDesc & (EVertexAttribute::Tex0 << iTex))
mTexCoords[iTex].push_back(rkVtx.Tex[iTex]);
}
for (uint32 iMtx = 0; iMtx < 8; iMtx++) for (size_t iMtx = 0; iMtx < mTexCoords.size(); iMtx++)
if (mVtxDesc & (EVertexAttribute::PosMtx << iMtx)) mTexCoords[iMtx].push_back(rkVtx.MatrixIndices[iMtx]); {
if (mVtxDesc & (EVertexAttribute::PosMtx << iMtx))
mTexCoords[iMtx].push_back(rkVtx.MatrixIndices[iMtx]);
}
if (mVtxDesc.HasAnyFlags(EVertexAttribute::BoneIndices | EVertexAttribute::BoneWeights) && mpSkin) if (mVtxDesc.HasAnyFlags(EVertexAttribute::BoneIndices | EVertexAttribute::BoneWeights) && mpSkin)
{ {
const SVertexWeights& rkWeights = mpSkin->WeightsForVertex(rkVtx.ArrayPosition); const SVertexWeights& rkWeights = mpSkin->WeightsForVertex(rkVtx.ArrayPosition);
if (mVtxDesc & EVertexAttribute::BoneIndices) mBoneIndices.push_back(rkWeights.Indices); if (mVtxDesc & EVertexAttribute::BoneIndices)
if (mVtxDesc & EVertexAttribute::BoneWeights) mBoneWeights.push_back(rkWeights.Weights); mBoneIndices.push_back(rkWeights.Indices);
if (mVtxDesc & EVertexAttribute::BoneWeights)
mBoneWeights.push_back(rkWeights.Weights);
} }
return (mPositions.size() - 1); return mPositions.size() - 1;
} }
uint16 CVertexBuffer::AddIfUnique(const CVertex& rkVtx, uint16 Start) uint16 CVertexBuffer::AddIfUnique(const CVertex& rkVtx, uint16 Start)
@ -59,25 +72,43 @@ uint16 CVertexBuffer::AddIfUnique(const CVertex& rkVtx, uint16 Start)
bool Unique = false; bool Unique = false;
if (mVtxDesc & EVertexAttribute::Position) if (mVtxDesc & EVertexAttribute::Position)
if (rkVtx.Position != mPositions[iVert]) Unique = true; {
if (rkVtx.Position != mPositions[iVert])
Unique = true;
}
if (!Unique && (mVtxDesc & EVertexAttribute::Normal)) if (!Unique && (mVtxDesc & EVertexAttribute::Normal))
if (rkVtx.Normal != mNormals[iVert]) Unique = true; {
if (rkVtx.Normal != mNormals[iVert])
Unique = true;
}
if (!Unique && (mVtxDesc & EVertexAttribute::Color0)) if (!Unique && (mVtxDesc & EVertexAttribute::Color0))
if (rkVtx.Color[0] != mColors[0][iVert]) Unique = true; {
if (rkVtx.Color[0] != mColors[0][iVert])
Unique = true;
}
if (!Unique && (mVtxDesc & EVertexAttribute::Color1)) if (!Unique && (mVtxDesc & EVertexAttribute::Color1))
if (rkVtx.Color[1] != mColors[1][iVert]) Unique = true; {
if (rkVtx.Color[1] != mColors[1][iVert])
Unique = true;
}
if (!Unique) if (!Unique)
for (uint32 iTex = 0; iTex < 8; iTex++) {
if ((mVtxDesc & (EVertexAttribute::Tex0 << iTex))) for (size_t iTex = 0; iTex < mTexCoords.size(); iTex++)
{
if (mVtxDesc & (EVertexAttribute::Tex0 << iTex))
{
if (rkVtx.Tex[iTex] != mTexCoords[iTex][iVert]) if (rkVtx.Tex[iTex] != mTexCoords[iTex][iVert])
{ {
Unique = true; Unique = true;
break; break;
} }
}
}
}
if (!Unique && mpSkin && (mVtxDesc.HasAnyFlags(EVertexAttribute::BoneIndices | EVertexAttribute::BoneWeights))) if (!Unique && mpSkin && (mVtxDesc.HasAnyFlags(EVertexAttribute::BoneIndices | EVertexAttribute::BoneWeights)))
{ {
@ -85,8 +116,8 @@ uint16 CVertexBuffer::AddIfUnique(const CVertex& rkVtx, uint16 Start)
for (uint32 iWgt = 0; iWgt < 4; iWgt++) for (uint32 iWgt = 0; iWgt < 4; iWgt++)
{ {
if ( ((mVtxDesc & EVertexAttribute::BoneIndices) && (rkWeights.Indices[iWgt] != mBoneIndices[iVert][iWgt])) || if (((mVtxDesc & EVertexAttribute::BoneIndices) && (rkWeights.Indices[iWgt] != mBoneIndices[iVert][iWgt])) ||
((mVtxDesc & EVertexAttribute::BoneWeights) && (rkWeights.Weights[iWgt] != mBoneWeights[iVert][iWgt])) ) ((mVtxDesc & EVertexAttribute::BoneWeights) && (rkWeights.Weights[iWgt] != mBoneWeights[iVert][iWgt])))
{ {
Unique = true; Unique = true;
break; break;
@ -94,7 +125,8 @@ uint16 CVertexBuffer::AddIfUnique(const CVertex& rkVtx, uint16 Start)
} }
} }
if (!Unique) return iVert; if (!Unique)
return iVert;
} }
} }
@ -103,7 +135,7 @@ uint16 CVertexBuffer::AddIfUnique(const CVertex& rkVtx, uint16 Start)
void CVertexBuffer::Reserve(uint16 Size) void CVertexBuffer::Reserve(uint16 Size)
{ {
uint32 ReserveSize = mPositions.size() + Size; const size_t ReserveSize = mPositions.size() + Size;
if (mVtxDesc & EVertexAttribute::Position) if (mVtxDesc & EVertexAttribute::Position)
mPositions.reserve(ReserveSize); mPositions.reserve(ReserveSize);
@ -117,9 +149,11 @@ void CVertexBuffer::Reserve(uint16 Size)
if (mVtxDesc & EVertexAttribute::Color1) if (mVtxDesc & EVertexAttribute::Color1)
mColors[1].reserve(ReserveSize); mColors[1].reserve(ReserveSize);
for (uint32 iTex = 0; iTex < 8; iTex++) for (size_t iTex = 0; iTex < mTexCoords.size(); iTex++)
{
if (mVtxDesc & (EVertexAttribute::Tex0 << iTex)) if (mVtxDesc & (EVertexAttribute::Tex0 << iTex))
mTexCoords[iTex].reserve(ReserveSize); mTexCoords[iTex].reserve(ReserveSize);
}
if (mVtxDesc & EVertexAttribute::BoneIndices) if (mVtxDesc & EVertexAttribute::BoneIndices)
mBoneIndices.reserve(ReserveSize); mBoneIndices.reserve(ReserveSize);
@ -131,7 +165,7 @@ void CVertexBuffer::Reserve(uint16 Size)
void CVertexBuffer::Clear() void CVertexBuffer::Clear()
{ {
if (mBuffered) if (mBuffered)
glDeleteBuffers(14, mAttribBuffers); glDeleteBuffers(static_cast<GLsizei>(mAttribBuffers.size()), mAttribBuffers.data());
mBuffered = false; mBuffered = false;
mPositions.clear(); mPositions.clear();
@ -139,8 +173,8 @@ void CVertexBuffer::Clear()
mColors[0].clear(); mColors[0].clear();
mColors[1].clear(); mColors[1].clear();
for (uint32 iTex = 0; iTex < 8; iTex++) for (auto& coord : mTexCoords)
mTexCoords[iTex].clear(); coord.clear();
mBoneIndices.clear(); mBoneIndices.clear();
mBoneWeights.clear(); mBoneWeights.clear();
@ -151,49 +185,46 @@ void CVertexBuffer::Buffer()
// Make sure we don't end up with two buffers for the same data... // Make sure we don't end up with two buffers for the same data...
if (mBuffered) if (mBuffered)
{ {
glDeleteBuffers(14, mAttribBuffers); glDeleteBuffers(static_cast<GLsizei>(mAttribBuffers.size()), mAttribBuffers.data());
mBuffered = false; mBuffered = false;
} }
// Generate buffers // Generate buffers
glGenBuffers(14, mAttribBuffers); glGenBuffers(static_cast<GLsizei>(mAttribBuffers.size()), mAttribBuffers.data());
for (uint32 iAttrib = 0; iAttrib < 14; iAttrib++) for (size_t iAttrib = 0; iAttrib < mAttribBuffers.size(); iAttrib++)
{ {
int Attrib = (EVertexAttribute::Position << iAttrib); const auto Attrib = static_cast<int>(EVertexAttribute::Position << iAttrib);
bool HasAttrib = ((mVtxDesc & Attrib) != 0); const bool HasAttrib = (mVtxDesc & Attrib) != 0;
if (!HasAttrib) continue; if (!HasAttrib)
continue;
if (iAttrib < 2) if (iAttrib < 2)
{ {
std::vector<CVector3f> *pBuffer = (iAttrib == 0) ? &mPositions : &mNormals; const std::vector<CVector3f>& pBuffer = iAttrib == 0 ? mPositions : mNormals;
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glBufferData(GL_ARRAY_BUFFER, pBuffer->size() * sizeof(CVector3f), pBuffer->data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, pBuffer.size() * sizeof(CVector3f), pBuffer.data(), GL_STATIC_DRAW);
} }
else if (iAttrib < 4) else if (iAttrib < 4)
{ {
uint8 Index = (uint8) (iAttrib - 2); const auto Index = static_cast<uint8>(iAttrib - 2);
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glBufferData(GL_ARRAY_BUFFER, mColors[Index].size() * sizeof(CColor), mColors[Index].data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mColors[Index].size() * sizeof(CColor), mColors[Index].data(), GL_STATIC_DRAW);
} }
else if (iAttrib < 12) else if (iAttrib < 12)
{ {
uint8 Index = (uint8) (iAttrib - 4); const auto Index = static_cast<uint8>(iAttrib - 4);
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glBufferData(GL_ARRAY_BUFFER, mTexCoords[Index].size() * sizeof(CVector2f), mTexCoords[Index].data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mTexCoords[Index].size() * sizeof(CVector2f), mTexCoords[Index].data(), GL_STATIC_DRAW);
} }
else if (iAttrib == 12) else if (iAttrib == 12)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glBufferData(GL_ARRAY_BUFFER, mBoneIndices.size() * sizeof(TBoneIndices), mBoneIndices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mBoneIndices.size() * sizeof(TBoneIndices), mBoneIndices.data(), GL_STATIC_DRAW);
} }
else if (iAttrib == 13) else if (iAttrib == 13)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
@ -206,7 +237,9 @@ void CVertexBuffer::Buffer()
void CVertexBuffer::Bind() void CVertexBuffer::Bind()
{ {
if (!mBuffered) Buffer(); if (!mBuffered)
Buffer();
CVertexArrayManager::Current()->BindVAO(this); CVertexArrayManager::Current()->BindVAO(this);
} }
@ -248,44 +281,40 @@ GLuint CVertexBuffer::CreateVAO()
glGenVertexArrays(1, &VertexArray); glGenVertexArrays(1, &VertexArray);
glBindVertexArray(VertexArray); glBindVertexArray(VertexArray);
for (uint32 iAttrib = 0; iAttrib < 14; iAttrib++) for (size_t iAttrib = 0; iAttrib < mAttribBuffers.size(); iAttrib++)
{ {
int Attrib = (EVertexAttribute::Position << iAttrib); const auto Attrib = static_cast<int>(EVertexAttribute::Position << iAttrib);
bool HasAttrib = ((mVtxDesc & Attrib) != 0); const bool HasAttrib = (mVtxDesc & Attrib) != 0;
if (!HasAttrib) continue; if (!HasAttrib) continue;
if (iAttrib < 2) if (iAttrib < 2)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glVertexAttribPointer(iAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(CVector3f), (void*) 0); glVertexAttribPointer(iAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(CVector3f), nullptr);
glEnableVertexAttribArray(iAttrib); glEnableVertexAttribArray(iAttrib);
} }
else if (iAttrib < 4) else if (iAttrib < 4)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glVertexAttribPointer(iAttrib, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(CColor), (void*) 0); glVertexAttribPointer(iAttrib, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(CColor), nullptr);
glEnableVertexAttribArray(iAttrib); glEnableVertexAttribArray(iAttrib);
} }
else if (iAttrib < 12) else if (iAttrib < 12)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glVertexAttribPointer(iAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(CVector2f), (void*) 0); glVertexAttribPointer(iAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(CVector2f), nullptr);
glEnableVertexAttribArray(iAttrib); glEnableVertexAttribArray(iAttrib);
} }
else if (iAttrib == 12) else if (iAttrib == 12)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glVertexAttribIPointer(iAttrib, 1, GL_UNSIGNED_INT, sizeof(TBoneIndices), (void*) 0); glVertexAttribIPointer(iAttrib, 1, GL_UNSIGNED_INT, sizeof(TBoneIndices), nullptr);
glEnableVertexAttribArray(iAttrib); glEnableVertexAttribArray(iAttrib);
} }
else if (iAttrib == 13) else if (iAttrib == 13)
{ {
glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]); glBindBuffer(GL_ARRAY_BUFFER, mAttribBuffers[iAttrib]);
glVertexAttribPointer(iAttrib, 4, GL_FLOAT, GL_FALSE, sizeof(TBoneWeights), (void*) 0); glVertexAttribPointer(iAttrib, 4, GL_FLOAT, GL_FALSE, sizeof(TBoneWeights), nullptr);
glEnableVertexAttribArray(iAttrib); glEnableVertexAttribArray(iAttrib);
} }
} }

View File

@ -5,18 +5,19 @@
#include "Core/Resource/Animation/CSkin.h" #include "Core/Resource/Animation/CSkin.h"
#include "Core/Resource/Model/CVertex.h" #include "Core/Resource/Model/CVertex.h"
#include "Core/Resource/Model/EVertexAttribute.h" #include "Core/Resource/Model/EVertexAttribute.h"
#include <array>
#include <vector> #include <vector>
#include <GL/glew.h> #include <GL/glew.h>
class CVertexBuffer class CVertexBuffer
{ {
FVertexDescription mVtxDesc; // Flags that indicate what vertex attributes are enabled on this vertex buffer FVertexDescription mVtxDesc; // Flags that indicate what vertex attributes are enabled on this vertex buffer
GLuint mAttribBuffers[14]; // Separate GL buffer for each attribute to allow not tracking unused attribs. No support for matrix indices currently. std::array<GLuint, 14> mAttribBuffers{}; // Separate GL buffer for each attribute to allow not tracking unused attribs. No support for matrix indices currently.
TResPtr<CSkin> mpSkin; // Skin for skinned models. Null on unskinned models; TResPtr<CSkin> mpSkin; // Skin for skinned models. Null on unskinned models;
std::vector<CVector3f> mPositions; // Vector of vertex positions std::vector<CVector3f> mPositions; // Vector of vertex positions
std::vector<CVector3f> mNormals; // Vector of vertex normals std::vector<CVector3f> mNormals; // Vector of vertex normals
std::vector<CColor> mColors[2]; // Vectors of vertex colors std::array<std::vector<CColor>, 2> mColors; // Vectors of vertex colors
std::vector<CVector2f> mTexCoords[8]; // Vectors of texture coordinates std::array<std::vector<CVector2f>, 8> mTexCoords; // Vectors of texture coordinates
std::vector<TBoneIndices> mBoneIndices; // Vectors of bone indices std::vector<TBoneIndices> mBoneIndices; // Vectors of bone indices
std::vector<TBoneWeights> mBoneWeights; // Vectors of bone weights std::vector<TBoneWeights> mBoneWeights; // Vectors of bone weights
bool mBuffered = false; // Bool value that indicates whether the attributes have been buffered. bool mBuffered = false; // Bool value that indicates whether the attributes have been buffered.