Skip to content

Commit 8d455c0

Browse files
committed
tr_surface: parallel MD3 model loading
1 parent 7bb14c1 commit 8d455c0

File tree

1 file changed

+48
-58
lines changed

1 file changed

+48
-58
lines changed

src/engine/renderer/tr_surface.cpp

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -819,56 +819,47 @@ Defines ATTR_POSITION, ATTR_TEXCOORD, and maybe ATTR_QTANGENT
819819
*/
820820
static void Tess_SurfaceMDV( mdvSurface_t *srf )
821821
{
822-
int i, j;
823-
int numIndexes = 0;
824-
int numVertexes;
825-
mdvXyz_t *oldVert, *newVert;
826-
mdvNormal_t *oldNormal, *newNormal;
827-
mdvSt_t *st;
828-
srfTriangle_t *tri;
829-
float backlerp;
830-
float oldXyzScale, newXyzScale;
831-
832822
GLIMP_LOGCOMMENT( "--- Tess_SurfaceMDV ---" );
833823

834-
if ( backEnd.currentEntity->e.oldframe == backEnd.currentEntity->e.frame )
835-
{
836-
backlerp = 0;
837-
}
838-
else
824+
float backlerp = 0.0f;
825+
826+
if ( backEnd.currentEntity->e.oldframe != backEnd.currentEntity->e.frame )
839827
{
840828
backlerp = backEnd.currentEntity->e.backlerp;
841829
}
842830

843-
newXyzScale = ( 1.0f - backlerp );
844-
oldXyzScale = backlerp;
831+
float newXyzScale = ( 1.0f - backlerp );
832+
float oldXyzScale = backlerp;
845833

846834
Tess_CheckOverflow( srf->numVerts, srf->numTriangles * 3 );
847835

848-
numIndexes = srf->numTriangles * 3;
836+
int numIndexes = srf->numTriangles * 3;
849837

850-
for ( i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++ )
851838
{
852-
tess.indexes[ tess.numIndexes + i * 3 + 0 ] = tess.numVertexes + tri->indexes[ 0 ];
853-
tess.indexes[ tess.numIndexes + i * 3 + 1 ] = tess.numVertexes + tri->indexes[ 1 ];
854-
tess.indexes[ tess.numIndexes + i * 3 + 2 ] = tess.numVertexes + tri->indexes[ 2 ];
839+
srfTriangle_t *tri = srf->triangles;
840+
for ( size_t i = 0; i < srf->numTriangles; i++, tri++ )
841+
{
842+
tess.indexes[ tess.numIndexes + i * 3 + 0 ] = tess.numVertexes + tri->indexes[ 0 ];
843+
tess.indexes[ tess.numIndexes + i * 3 + 1 ] = tess.numVertexes + tri->indexes[ 1 ];
844+
tess.indexes[ tess.numIndexes + i * 3 + 2 ] = tess.numVertexes + tri->indexes[ 2 ];
845+
}
855846
}
856847

857-
newVert = srf->verts + ( backEnd.currentEntity->e.frame * srf->numVerts );
858-
oldVert = srf->verts + ( backEnd.currentEntity->e.oldframe * srf->numVerts );
859-
newNormal = srf->normals + ( backEnd.currentEntity->e.frame * srf->numVerts );
860-
oldNormal = srf->normals + ( backEnd.currentEntity->e.oldframe * srf->numVerts );
861-
st = srf->st;
848+
mdvXyz_t *newVert = srf->verts + ( backEnd.currentEntity->e.frame * srf->numVerts );
849+
mdvXyz_t *oldVert = srf->verts + ( backEnd.currentEntity->e.oldframe * srf->numVerts );
850+
mdvNormal_t *newNormal = srf->normals + ( backEnd.currentEntity->e.frame * srf->numVerts );
851+
mdvNormal_t *oldNormal = srf->normals + ( backEnd.currentEntity->e.oldframe * srf->numVerts );
852+
mdvSt_t *st = srf->st;
862853

863-
numVertexes = srf->numVerts;
854+
int numVertexes = srf->numVerts;
864855

865856
if (tess.skipTangents)
866857
{
867-
for (j = 0; j < numVertexes; j++, newVert++, oldVert++, st++)
858+
for ( size_t j = 0; j < numVertexes; j++, newVert++, oldVert++, st++ )
868859
{
869860
vec3_t tmpVert;
870861

871-
if (backlerp == 0)
862+
if ( backlerp == 0.0f )
872863
{
873864
// just copy
874865
VectorCopy(newVert->xyz, tmpVert);
@@ -891,25 +882,17 @@ static void Tess_SurfaceMDV( mdvSurface_t *srf )
891882
else
892883
{
893884
// calc tangent spaces
894-
float *v;
895-
const float *v0, *v1, *v2;
896-
const float *t0, *t1, *t2;
897-
vec3_t* xyz;
898-
vec3_t tangent, *tangents;
899-
vec3_t binormal, *binormals;
900-
vec3_t *normals;
885+
vec3_t *xyz = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof(vec3_t) );
886+
vec3_t *tangents = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
887+
vec3_t *binormals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
888+
vec3_t *normals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
901889

902-
xyz = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof(vec3_t) );
903-
tangents = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
904-
binormals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
905-
normals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
906-
907-
for ( i = 0; i < numVertexes; i++, newVert++, oldVert++, oldNormal++, newNormal++ )
890+
for ( size_t i = 0; i < numVertexes; i++, newVert++, oldVert++, oldNormal++, newNormal++ )
908891
{
909892
VectorClear( tangents[ i ] );
910893
VectorClear( binormals[ i ] );
911894

912-
if ( backlerp == 0 )
895+
if ( backlerp == 0.0f )
913896
{
914897
// just copy
915898
VectorCopy( newNormal->normal, normals[ i ] );
@@ -922,7 +905,7 @@ static void Tess_SurfaceMDV( mdvSurface_t *srf )
922905
VectorNormalizeFast( normals[ i ] );
923906
}
924907

925-
if ( backlerp == 0 )
908+
if ( backlerp == 0.0f )
926909
{
927910
// just copy
928911
VectorCopy(newVert->xyz, xyz[i]);
@@ -935,32 +918,37 @@ static void Tess_SurfaceMDV( mdvSurface_t *srf )
935918
}
936919
}
937920

938-
for (i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++)
921+
auto task0 = [&]( const size_t& i ) -> void
939922
{
940-
int* indices = tri->indexes;
941-
v0 = xyz[ indices[0] ];
942-
v1 = xyz[ indices[1] ];
943-
v2 = xyz[ indices[2] ];
923+
srfTriangle_t *tri = srf->triangles + i;
924+
int *indices = tri->indexes;
925+
const float *v0 = xyz[ indices[ 0 ] ];
926+
const float *v1 = xyz[ indices[ 1 ] ];
927+
const float *v2 = xyz[ indices[ 2 ] ];
944928

945-
t0 = st[ indices[ 0 ] ].st;
946-
t1 = st[ indices[ 1 ] ].st;
947-
t2 = st[ indices[ 2 ] ].st;
929+
const float *t0 = st[ indices[ 0 ] ].st;
930+
const float *t1 = st[ indices[ 1 ] ].st;
931+
const float *t2 = st[ indices[ 2 ] ].st;
948932

933+
vec3_t tangent, binormal;
949934
R_CalcTangents( tangent, binormal, v0, v1, v2, t0, t1, t2 );
950935

951-
for ( j = 0; j < 3; j++ )
936+
for ( size_t j = 0; j < 3; j++ )
952937
{
938+
float *v;
939+
953940
v = tangents[ indices[ j ] ];
954941
VectorAdd( v, tangent, v );
955942

956943
v = binormals[ indices[ j ] ];
957944
VectorAdd( v, binormal, v );
958945
}
959-
}
946+
};
960947

961-
for ( i = 0; i < numVertexes; i++ )
962-
{
948+
Omp::Tasker( task0, srf->numTriangles );
963949

950+
auto task1 = [&]( const size_t& i ) -> void
951+
{
964952
i16vec4_t qtangents;
965953

966954
R_TBNtoQtangents( tangents[ i ], binormals[ i ],
@@ -970,7 +958,9 @@ static void Tess_SurfaceMDV( mdvSurface_t *srf )
970958
Vector4Copy(qtangents, tess.verts[tess.numVertexes + i].qtangents);
971959
tess.verts[tess.numVertexes + i].texCoords[0] = st[i].st[0];
972960
tess.verts[tess.numVertexes + i].texCoords[1] = st[i].st[1];
973-
}
961+
};
962+
963+
Omp::Tasker( task1, numVertexes );
974964

975965
ri.Hunk_FreeTempMemory( normals );
976966
ri.Hunk_FreeTempMemory( binormals );

0 commit comments

Comments
 (0)