Skip to content

Commit 67ed90a

Browse files
committed
tr_surface: parallel MD5 model CPU runtime computation
1 parent 291d6a7 commit 67ed90a

File tree

1 file changed

+93
-29
lines changed

1 file changed

+93
-29
lines changed

src/engine/renderer/tr_surface.cpp

Lines changed: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,14 @@ static void Tess_SurfaceMD5( md5Surface_t *srf )
10581058
shaderVertex_t *tessVertex = tess.verts + tess.numVertexes;
10591059
shaderVertex_t *lastVertex = tessVertex + srf->numVerts;
10601060

1061+
size_t num_chunks = Omp::GetThreads();
1062+
1063+
size_t chunk_size = srf->numVerts / num_chunks;
1064+
10611065
// Deform the vertices by the lerped bones.
10621066
if ( tess.skipTangents )
10631067
{
1068+
if ( !r_parallelNotbntoq.Get() ) {
10641069
for ( ; tessVertex < lastVertex; tessVertex++,
10651070
surfaceVertex++ )
10661071
{
@@ -1084,49 +1089,108 @@ static void Tess_SurfaceMD5( md5Surface_t *srf )
10841089

10851090
Vector2Copy( surfaceVertex->texCoords, tessVertex->texCoords );
10861091
}
1092+
} else {
1093+
auto task = [&]( size_t chunk_index ) -> void
1094+
{
1095+
size_t chunk_offset = chunk_index * chunk_size;
1096+
1097+
shaderVertex_t *chunk_tessVertex = tessVertex + chunk_offset;
1098+
md5Vertex_t *chunk_surfaceVertex = surfaceVertex + chunk_offset;
1099+
1100+
shaderVertex_t *chunk_lastVertex =
1101+
chunk_index == num_chunks - 1
1102+
? lastVertex
1103+
: chunk_tessVertex + chunk_size;
1104+
1105+
for ( ; chunk_tessVertex < chunk_lastVertex;
1106+
chunk_tessVertex++,
1107+
chunk_surfaceVertex++ )
1108+
{
1109+
vec3_t position = {};
1110+
1111+
float *chunk_boneWeight = chunk_surfaceVertex->boneWeights;
1112+
float *chunk_lastWeight = chunk_boneWeight + chunk_surfaceVertex->numWeights;
1113+
uint32_t *chunk_boneIndex = chunk_surfaceVertex->boneIndexes;
1114+
vec4_t *chunk_surfacePosition = &chunk_surfaceVertex->position;
1115+
1116+
for ( ; chunk_boneWeight < chunk_lastWeight;
1117+
chunk_boneWeight++,
1118+
chunk_boneIndex++ )
1119+
{
1120+
vec3_t tmp;
1121+
1122+
TransformPoint( &bones[ *chunk_boneIndex ], *chunk_surfacePosition, tmp );
1123+
VectorMA( position, *chunk_boneWeight, tmp, position );
1124+
}
1125+
1126+
VectorCopy( position, chunk_tessVertex->xyz );
1127+
1128+
Vector2Copy( chunk_surfaceVertex->texCoords, chunk_tessVertex->texCoords );
1129+
}
1130+
};
1131+
1132+
Omp::Tasker( task, num_chunks );
1133+
}
10871134
}
10881135
else
10891136
{
1090-
for ( ; tessVertex < lastVertex; tessVertex++,
1091-
surfaceVertex++ )
1137+
auto task = [&]( size_t chunk_index ) -> void
10921138
{
1093-
vec3_t tangent = {}, binormal = {}, normal = {}, position = {};
1139+
size_t chunk_offset = chunk_index * chunk_size;
10941140

1095-
float *boneWeight = surfaceVertex->boneWeights;
1096-
float *lastWeight = boneWeight + surfaceVertex->numWeights;
1097-
uint32_t *boneIndex = surfaceVertex->boneIndexes;
1098-
vec4_t *surfacePosition = &surfaceVertex->position;
1099-
vec4_t *surfaceNormal = &surfaceVertex->normal;
1100-
vec4_t *surfaceTangent = &surfaceVertex->tangent;
1101-
vec4_t *surfaceBinormal = &surfaceVertex->binormal;
1141+
shaderVertex_t *chunk_tessVertex = tessVertex + chunk_offset;
1142+
md5Vertex_t *chunk_surfaceVertex = surfaceVertex + chunk_offset;
11021143

1103-
for ( ; boneWeight < lastWeight; boneWeight++,
1104-
boneIndex++ )
1144+
shaderVertex_t *chunk_lastVertex =
1145+
chunk_index == num_chunks - 1
1146+
? lastVertex
1147+
: chunk_tessVertex + chunk_size;
1148+
1149+
for ( ; chunk_tessVertex < chunk_lastVertex;
1150+
chunk_tessVertex++,
1151+
chunk_surfaceVertex++ )
11051152
{
1106-
vec3_t tmp;
1153+
vec3_t tangent = {}, binormal = {}, normal = {}, position = {};
1154+
1155+
float *chunk_boneWeight = chunk_surfaceVertex->boneWeights;
1156+
float *chunk_lastWeight = chunk_boneWeight + chunk_surfaceVertex->numWeights;
1157+
uint32_t *chunk_boneIndex = chunk_surfaceVertex->boneIndexes;
1158+
vec4_t *chunk_surfacePosition = &chunk_surfaceVertex->position;
1159+
vec4_t *chunk_surfaceNormal = &chunk_surfaceVertex->normal;
1160+
vec4_t *chunk_surfaceTangent = &chunk_surfaceVertex->tangent;
1161+
vec4_t *chunk_surfaceBinormal = &chunk_surfaceVertex->binormal;
1162+
1163+
for ( ; chunk_boneWeight < chunk_lastWeight;
1164+
chunk_boneWeight++,
1165+
chunk_boneIndex++ )
1166+
{
1167+
vec3_t tmp;
11071168

1108-
TransformPoint( &bones[ *boneIndex ], *surfacePosition, tmp );
1109-
VectorMA( position, *boneWeight, tmp, position );
1169+
TransformPoint( &bones[ *chunk_boneIndex ], *chunk_surfacePosition, tmp );
1170+
VectorMA( position, *chunk_boneWeight, tmp, position );
11101171

1111-
TransformNormalVector( &bones[ *boneIndex ], *surfaceNormal, tmp );
1112-
VectorMA( normal, *boneWeight, tmp, normal );
1172+
TransformNormalVector( &bones[ *chunk_boneIndex ], *chunk_surfaceNormal, tmp );
1173+
VectorMA( normal, *chunk_boneWeight, tmp, normal );
11131174

1114-
TransformNormalVector( &bones[ *boneIndex ], *surfaceTangent, tmp );
1115-
VectorMA( tangent, *boneWeight, tmp, tangent );
1175+
TransformNormalVector( &bones[ *chunk_boneIndex ], *chunk_surfaceTangent, tmp );
1176+
VectorMA( tangent, *chunk_boneWeight, tmp, tangent );
11161177

1117-
TransformNormalVector( &bones[ *boneIndex ], *surfaceBinormal, tmp );
1118-
VectorMA( binormal, *boneWeight, tmp, binormal );
1119-
}
1178+
TransformNormalVector( &bones[ *chunk_boneIndex ], *chunk_surfaceBinormal, tmp );
1179+
VectorMA( binormal, *chunk_boneWeight, tmp, binormal );
1180+
}
11201181

1121-
VectorNormalizeFast( normal );
1122-
VectorNormalizeFast( tangent );
1123-
VectorNormalizeFast( binormal );
1124-
VectorCopy( position, tessVertex->xyz );
1182+
VectorNormalizeFast( normal );
1183+
VectorNormalizeFast( tangent );
1184+
VectorNormalizeFast( binormal );
1185+
VectorCopy( position, chunk_tessVertex->xyz );
11251186

1126-
R_TBNtoQtangentsFast( tangent, binormal, normal, tessVertex->qtangents );
1187+
R_TBNtoQtangentsFast( tangent, binormal, normal, chunk_tessVertex->qtangents );
11271188

1128-
Vector2Copy( surfaceVertex->texCoords, tessVertex->texCoords );
1129-
}
1189+
Vector2Copy( chunk_surfaceVertex->texCoords, chunk_tessVertex->texCoords );
1190+
}
1191+
};
1192+
1193+
Omp::Tasker( task, num_chunks );
11301194
}
11311195

11321196
tess.numIndexes += numIndexes;

0 commit comments

Comments
 (0)