Permalink
Browse files

Optimise VBO creation by eliminating memcpy() use.

  • Loading branch information...
1 parent 0cf06f6 commit 9b0fe6c7039463307738f4382c6014ad4e4649bd Darren Salt committed May 3, 2012
Showing with 25 additions and 117 deletions.
  1. +25 −117 src/engine/rendererGL/tr_vbo.c
@@ -94,8 +94,6 @@ VBO_t *R_CreateVBO( const char *name, byte *vertexes, int vertexesSize,
/*
============
R_CreateVBO2
-
-RB: OPTIMIZE rewrite to not use memcpy
============
*/
VBO_t *R_CreateVBO2( const char *name, int numVertexes, srfVert_t *verts, unsigned int stateBits, vboUsage_t usage )
@@ -107,7 +105,6 @@ VBO_t *R_CreateVBO2( const char *name, int numVertexes, srfVert_t *vert
int dataSize;
int dataOfs;
- vec4_t tmp;
int glUsage;
switch ( usage )
@@ -165,132 +162,65 @@ VBO_t *R_CreateVBO2( const char *name, int numVertexes, srfVert_t *vert
data = ri.Hunk_AllocateTempMemory( dataSize );
dataOfs = 0;
- // set up xyz array
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 3; j++ )
- {
- tmp[ j ] = verts[ i ].xyz[ j ];
- }
-
- tmp[ 3 ] = 1;
+ // since this is all float, point tmp directly into data
+ // 2-entry -> { memb[0], memb[1], 0, 1 }
+ // 3-entry -> { memb[0], memb[1], memb[2], 1 }
+#define VERTEXSIZE(memb) ( sizeof( verts->memb ) / sizeof( verts->memb[ 0 ] ) )
+#define VERTEXCOPY(memb) \
+ do { \
+ vec_t *tmp = (vec_t *) ( data + dataOfs ); \
+ for ( i = 0; i < numVertexes; i++ ) \
+ { \
+ for ( j = 0; j < VERTEXSIZE( memb ); j++ ) { *tmp++ = verts[ i ].memb[ j ]; } \
+ if ( VERTEXSIZE( memb ) < 3 ) { *tmp++ = 0; } \
+ if ( VERTEXSIZE( memb ) < 4 ) { *tmp++ = 1; } \
+ } \
+ dataOfs += i * sizeof( vec4_t ); \
+ } while ( 0 )
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ // set up xyz array
+ VERTEXCOPY( xyz );
// feed vertex texcoords
if ( stateBits & ATTR_TEXCOORD )
{
vbo->ofsTexCoords = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 2; j++ )
- {
- tmp[ j ] = verts[ i ].st[ j ];
- }
-
- tmp[ 2 ] = 0;
- tmp[ 3 ] = 1;
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( st );
}
// feed vertex lightmap texcoords
if ( stateBits & ATTR_LIGHTCOORD )
{
vbo->ofsLightCoords = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 2; j++ )
- {
- tmp[ j ] = verts[ i ].lightmap[ j ];
- }
-
- tmp[ 2 ] = 0;
- tmp[ 3 ] = 1;
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( lightmap );
}
// feed vertex tangents
if ( stateBits & ATTR_TANGENT )
{
vbo->ofsTangents = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 3; j++ )
- {
- tmp[ j ] = verts[ i ].tangent[ j ];
- }
-
- tmp[ 3 ] = 1;
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( tangent );
}
// feed vertex binormals
if ( stateBits & ATTR_BINORMAL )
{
vbo->ofsBinormals = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 3; j++ )
- {
- tmp[ j ] = verts[ i ].binormal[ j ];
- }
-
- tmp[ 3 ] = 1;
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( binormal );
}
// feed vertex normals
if ( stateBits & ATTR_NORMAL )
{
vbo->ofsNormals = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 3; j++ )
- {
- tmp[ j ] = verts[ i ].normal[ j ];
- }
-
- tmp[ 3 ] = 1;
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( normal );
}
// feed vertex colors
if ( stateBits & ATTR_COLOR )
{
vbo->ofsColors = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 4; j++ )
- {
- tmp[ j ] = verts[ i ].lightColor[ j ];
- }
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( lightColor );
}
#if !defined( COMPAT_Q3A ) && !defined( COMPAT_ET )
@@ -299,36 +229,14 @@ VBO_t *R_CreateVBO2( const char *name, int numVertexes, srfVert_t *vert
if ( stateBits & ATTR_PAINTCOLOR )
{
vbo->ofsPaintColors = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 4; j++ )
- {
- tmp[ j ] = verts[ i ].paintColor[ j ];
- }
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( paintColor );
}
// feed vertex light directions
if ( stateBits & ATTR_LIGHTDIRECTION )
{
vbo->ofsLightDirections = dataOfs;
-
- for ( i = 0; i < numVertexes; i++ )
- {
- for ( j = 0; j < 3; j++ )
- {
- tmp[ j ] = verts[ i ].lightDirection[ j ];
- }
-
- tmp[ 3 ] = 1;
-
- memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) );
- dataOfs += sizeof( vec4_t );
- }
+ VERTEXCOPY( lightDirection );
}
#endif

0 comments on commit 9b0fe6c

Please sign in to comment.