Optimise vertex data to cut down on unnecessary copying

This commit is contained in:
Ian Curtis 2017-08-30 21:45:25 +00:00
parent 165926aa06
commit 266c911133
3 changed files with 55 additions and 55 deletions

View file

@ -21,21 +21,24 @@ struct ClipPoly
int count = 0;
};
struct Vertex
struct Vertex // half vertex
{
float pos[4];
float normal[3];
float texcoords[2];
UINT8 color[4];
float faceNormal[3];
float fixedShade;
};
struct Poly // our polys are always 3 triangles, unlike the real h/w
struct FVertex : Vertex // full vertex including face attributes
{
Vertex p1;
Vertex p2;
Vertex p3;
float faceNormal[3];
UINT8 faceColour[4];
FVertex& operator=(const Vertex& vertex)
{
memcpy(this, &vertex, sizeof(Vertex));
return *this;
}
};
struct R3DPoly
@ -46,6 +49,42 @@ struct R3DPoly
int number = 4;
};
struct Poly // our polys are always 3 triangles, unlike the real h/w
{
Poly() {}; // default
Poly(bool firstTriangle, const R3DPoly& r3dPoly) {
if (firstTriangle) {
p1 = r3dPoly.v[0];
p2 = r3dPoly.v[1];
p3 = r3dPoly.v[2];
}
else {
p1 = r3dPoly.v[0];
p2 = r3dPoly.v[2];
p3 = r3dPoly.v[3];
}
// copy face attributes
for (int i = 0; i < 4; i++) {
p1.faceColour[i] = r3dPoly.faceColour[i];
p2.faceColour[i] = r3dPoly.faceColour[i];
p3.faceColour[i] = r3dPoly.faceColour[i];
}
for (int i = 0; i < 3; i++) {
p1.faceNormal[i] = r3dPoly.faceNormal[i];
p2.faceNormal[i] = r3dPoly.faceNormal[i];
p3.faceNormal[i] = r3dPoly.faceNormal[i];
}
}
FVertex p1;
FVertex p2;
FVertex p3;
};
struct Mesh
{
//helper funcs

View file

@ -289,12 +289,12 @@ void CNew3D::RenderFrame(void)
glEnableVertexAttribArray(5);
// before draw, specify vertex and index arrays with their offsets, offsetof is maybe evil ..
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inVertex"), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inNormal"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inTexCoord"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoords));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inColour"), 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inFaceNormal"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, faceNormal));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inFixedShade"), 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, fixedShade));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inVertex"), 4, GL_FLOAT, GL_FALSE, sizeof(FVertex), 0);
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inNormal"), 3, GL_FLOAT, GL_FALSE, sizeof(FVertex), (void*)offsetof(FVertex, normal));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inTexCoord"), 2, GL_FLOAT, GL_FALSE, sizeof(FVertex), (void*)offsetof(FVertex, texcoords));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inColour"), 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(FVertex), (void*)offsetof(FVertex, faceColour));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inFaceNormal"), 3, GL_FLOAT, GL_FALSE, sizeof(FVertex), (void*)offsetof(FVertex, faceNormal));
glVertexAttribPointer(m_r3dShader.GetVertexAttribPos("inFixedShade"), 1, GL_FLOAT, GL_FALSE, sizeof(FVertex), (void*)offsetof(FVertex, fixedShade));
m_r3dShader.SetShader(true);
@ -897,49 +897,10 @@ void CNew3D::RenderViewport(UINT32 addr)
void CNew3D::CopyVertexData(const R3DPoly& r3dPoly, std::vector<Poly>& polyArray)
{
Poly p;
p.p1 = r3dPoly.v[0];
p.p2 = r3dPoly.v[1];
p.p3 = r3dPoly.v[2];
// Copy face colour to vertices
for (int i = 0; i < 4; i++) {
p.p1.color[i] = r3dPoly.faceColour[i];
p.p2.color[i] = r3dPoly.faceColour[i];
p.p3.color[i] = r3dPoly.faceColour[i];
}
// Copy face normal
for (int i = 0; i < 3; i++) {
p.p1.faceNormal[i] = r3dPoly.faceNormal[i];
p.p2.faceNormal[i] = r3dPoly.faceNormal[i];
p.p3.faceNormal[i] = r3dPoly.faceNormal[i];
}
polyArray.emplace_back(p);
polyArray.emplace_back(Poly(true,r3dPoly)); // create object directly in array without temporary copy
if (r3dPoly.number == 4) {
p.p1 = r3dPoly.v[0];
p.p2 = r3dPoly.v[2];
p.p3 = r3dPoly.v[3];
// Copy face colour to vertices
for (int i = 0; i < 4; i++) {
p.p1.color[i] = r3dPoly.faceColour[i];
p.p2.color[i] = r3dPoly.faceColour[i];
p.p3.color[i] = r3dPoly.faceColour[i];
}
// Copy face normal
for (int i = 0; i < 3; i++) {
p.p1.faceNormal[i] = r3dPoly.faceNormal[i];
p.p2.faceNormal[i] = r3dPoly.faceNormal[i];
p.p3.faceNormal[i] = r3dPoly.faceNormal[i];
}
polyArray.emplace_back(p);
polyArray.emplace_back(Poly(false, r3dPoly)); // copy second triangle
}
}

View file

@ -142,7 +142,7 @@ void main()
vec4 finalData;
vec4 fogData;
if(fsDiscard>0) {
if(fsDiscard>=0) {
discard; //emulate back face culling here
}