Skip to content

Commit 063be04

Browse files
committed
Added Overdraw and Vertex Fetch optimizations
1 parent 6704462 commit 063be04

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

src/AssImpMesh.cpp

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,55 @@ void AssImpMesh::render()
104104
// Initializes all the buffer objects/arrays
105105
void AssImpMesh::setupMesh()
106106
{
107+
// ============================================
108+
// MESH OPTIMIZATION (before splitting arrays)
109+
// ============================================
110+
if (_indices.size() > 300 && _vertices.size() > 100)
111+
{
112+
size_t vertexCount = _vertices.size();
113+
114+
// Extract positions temporarily for overdraw optimization
115+
std::vector<float> tempPositions(vertexCount * 3);
116+
for (size_t i = 0; i < vertexCount; i++)
117+
{
118+
tempPositions[i * 3 + 0] = _vertices[i].Position.x;
119+
tempPositions[i * 3 + 1] = _vertices[i].Position.y;
120+
tempPositions[i * 3 + 2] = _vertices[i].Position.z;
121+
}
122+
123+
// Step 1: Vertex Cache Optimization
124+
meshopt_optimizeVertexCache(
125+
_indices.data(),
126+
_indices.data(),
127+
_indices.size(),
128+
vertexCount
129+
);
130+
131+
// Step 2: Overdraw Optimization
132+
meshopt_optimizeOverdraw(
133+
_indices.data(),
134+
_indices.data(),
135+
_indices.size(),
136+
tempPositions.data(),
137+
vertexCount,
138+
sizeof(float) * 3,
139+
1.05f
140+
);
141+
142+
// Step 3: Vertex Fetch Optimization
143+
meshopt_optimizeVertexFetch(
144+
_vertices.data(),
145+
_indices.data(),
146+
_indices.size(),
147+
_vertices.data(),
148+
vertexCount,
149+
sizeof(Vertex)
150+
);
151+
}
152+
153+
// ============================================
154+
// Extract to separate arrays
155+
// ============================================
107156
std::vector<float> points;
108157
std::vector<float> normals;
109158
std::vector<float> texCoords;
@@ -126,11 +175,15 @@ void AssImpMesh::setupMesh()
126175
tangents.push_back(v.Tangent.x);
127176
tangents.push_back(v.Tangent.y);
128177
tangents.push_back(v.Tangent.z);
178+
129179
bitangents.push_back(v.Bitangent.x);
130180
bitangents.push_back(v.Bitangent.y);
131181
bitangents.push_back(v.Bitangent.z);
132182
}
133183

184+
// ============================================
185+
// Texture flags
186+
// ============================================
134187
_hasTexture = false;
135188

136189
for (unsigned int i = 0; i < _textures.size(); i++)
@@ -166,11 +219,11 @@ void AssImpMesh::setupMesh()
166219
{
167220
_hasOpacityADSMap = true;
168221
_hasOpacityPBRMap = true;
169-
}
222+
}
170223

171224
// PBR from model
172225
if (name == "albedoMap")
173-
{
226+
{
174227
_hasAlbedoPBRMap = true;
175228
}
176229
if (name == "metallicMap")
@@ -195,15 +248,14 @@ void AssImpMesh::setupMesh()
195248
{
196249
_hasAOPBRMap = true;
197250
}
198-
if( name == "heightMap")
251+
if (name == "heightMap")
199252
{
200253
_hasHeightPBRMap = true;
201254
}
202255
if (name == "opacityMap")
203256
{
204257
_hasOpacityPBRMap = true;
205258
_hasOpacityADSMap = true;
206-
//_opacityPBRMapInverted = _textures[i].invertOpacity;
207259
}
208260
if (name == "transmissionMap")
209261
{
@@ -231,20 +283,10 @@ void AssImpMesh::setupMesh()
231283
}
232284
if (name == "clearcoatNormalMap")
233285
{
234-
_hasClearcoatNormalPBRMap = true;
286+
_hasClearcoatNormalPBRMap = true;
235287
}
236288
}
237289

238-
if (_indices.size() > 300) // Skip tiny meshes (100 triangles)
239-
{
240-
meshopt_optimizeVertexCache(
241-
_indices.data(),
242-
_indices.data(),
243-
_indices.size(),
244-
_vertices.size()
245-
);
246-
}
247-
248290
initBuffers(&_indices, &points, &normals, &texCoords, &tangents, &bitangents);
249291
computeBounds();
250292
}

0 commit comments

Comments
 (0)