Skip to content

Commit

Permalink
gl2shim: use IBO for quads
Browse files Browse the repository at this point in the history
  • Loading branch information
mittorn committed Oct 11, 2023
1 parent 92ac769 commit e9fcfc3
Showing 1 changed file with 53 additions and 33 deletions.
86 changes: 53 additions & 33 deletions ref/gl/gl2_shim/gl2_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ GNU General Public License for more details.
// increase this when adding more attributes
#define MAX_PROGS 32
#define MAX_BEGINEND_VERTS 8192
void* APIENTRY (*_pglMapBufferRange)(GLenum target, GLsizei offset, GLsizei length, GLbitfield access);
void* APIENTRY (*_pglFlushMappedBufferRange)(GLenum target, GLsizei offset, GLsizei length);
void (*_pglBufferStorage)( GLenum target,
void* (APIENTRY* _pglMapBufferRange)(GLenum target, GLsizei offset, GLsizei length, GLbitfield access);
void* (APIENTRY* _pglFlushMappedBufferRange)(GLenum target, GLsizei offset, GLsizei length);
void* (APIENTRY* _pglDrawRangeElementsBaseVertex)( GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, GLuint vertex );
void (APIENTRY*_pglBufferStorage)( GLenum target,
GLsizei size,
const GLvoid * data,
GLbitfield flags);
Expand Down Expand Up @@ -106,6 +107,7 @@ static struct
gl2wrap_prog_t progs[MAX_PROGS];
gl2wrap_prog_t *cur_prog;
GLboolean uchanged;
GLuint triquads_ibo[4];
} gl2wrap;

static struct
Expand Down Expand Up @@ -168,12 +170,12 @@ static GLboolean alpha_test_state;
static GLboolean fogging;


static void APIENTRY (*rpglEnable)(GLenum e);
static void APIENTRY (*rpglDisable)(GLenum e);
static void APIENTRY (*rpglDrawElements )( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices );
static void APIENTRY (*rpglDrawArrays )(GLenum mode, GLint first, GLsizei count);
static void APIENTRY (*rpglDrawRangeElements )( GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices );
static void APIENTRY (*rpglBindBufferARB)( GLenum buf, GLuint obj);
static void (APIENTRY*rpglEnable)(GLenum e);
static void (APIENTRY*rpglDisable)(GLenum e);
static void (APIENTRY*rpglDrawElements )( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices );
static void (APIENTRY*rpglDrawArrays )(GLenum mode, GLint first, GLsizei count);
static void (APIENTRY*rpglDrawRangeElements )( GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices );
static void (APIENTRY*rpglBindBufferARB)( GLenum buf, GLuint obj);
#ifdef QUAD_BATCH
void GL2_FlushPrims( void );
static void APIENTRY (*rpglBindTexture)( GLenum tex, GLuint obj);
Expand Down Expand Up @@ -421,12 +423,8 @@ static gl2wrap_prog_t *GL2_SetProg( const GLuint flags )
gl2wrap.cur_prog = prog;
return prog;
}
#if 1 //def QUAD_BATCH

#define TRIQUADS_SIZE GL2_MAX_VERTS / 4 * 6
#else
#define TRIQUADS_SIZE 256
#endif
unsigned short triquads_array[4][ TRIQUADS_SIZE * 6 ];

int GL2_ShimInit( void )
{
Expand Down Expand Up @@ -464,19 +462,26 @@ int GL2_ShimInit( void )
gl2wrap_config.cycle_buffers = 1;

memset( &gl2wrap, 0, sizeof( gl2wrap ) );
GL2_ShimInstall();

/// TODO: use IBO
for( i = 0; i < 4; i++ )
for( i = 0; i < (!!_pglDrawRangeElementsBaseVertex?1:4); i++ )
{
int j;
GLushort triquads_array[TRIQUADS_SIZE * 6];

for( j = 0; j < TRIQUADS_SIZE; j++ )
{
triquads_array[i][j * 6] = j * 4 + i;
triquads_array[i][j * 6 + 1] = j * 4 + 1 + i;
triquads_array[i][j * 6 + 2] = j * 4 + 2 + i;
triquads_array[i][j * 6 + 3] = j * 4 + i;
triquads_array[i][j * 6 + 4] = j * 4 + 2 + i;
triquads_array[i][j * 6 + 5] = j * 4 + 3 + i;
triquads_array[j * 6] = j * 4 + i;
triquads_array[j * 6 + 1] = j * 4 + 1 + i;
triquads_array[j * 6 + 2] = j * 4 + 2 + i;
triquads_array[j * 6 + 3] = j * 4 + i;
triquads_array[j * 6 + 4] = j * 4 + 2 + i;
triquads_array[j * 6 + 5] = j * 4 + 3 + i;
}
pglGenBuffersARB( 1, &gl2wrap.triquads_ibo[i] );
rpglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, gl2wrap.triquads_ibo[i] );
pglBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(triquads_array), triquads_array, GL_STATIC_DRAW_ARB );
}

gl2wrap.color[0] = 1.f;
Expand All @@ -485,7 +490,6 @@ int GL2_ShimInit( void )
gl2wrap.color[3] = 1.f;
gl2wrap.uchanged = GL_TRUE;

GL2_ShimInstall();
total = 0;

for ( i = 0; i < GL2_ATTR_MAX; ++i )
Expand Down Expand Up @@ -555,6 +559,8 @@ int GL2_ShimInit( void )
}
if( gl2wrap_config.vao_mandatory )
pglBindVertexArray(0);
rpglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
rpglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );



Expand Down Expand Up @@ -831,11 +837,24 @@ void GL2_FlushPrims( void )
{
if(count == 4)
rpglDrawArrays( GL_TRIANGLE_FAN, startindex, count );
/// TODO: use DrawRangeElementsBaseVertex when supported
else if(_pglDrawRangeElementsBaseVertex)
{
pglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, gl2wrap.triquads_ibo[0] );
_pglDrawRangeElementsBaseVertex( GL_TRIANGLES, startindex, startindex + count, Q_min(count / 4 * 6,TRIQUADS_SIZE * 6 - startindex), GL_UNSIGNED_SHORT, (void*)(size_t)(startindex / 4 * 6 * 2), startindex % 4 );
pglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
else if(rpglDrawRangeElements)
rpglDrawRangeElements( GL_TRIANGLES, startindex, startindex + count, Q_min(count / 4 * 6,sizeof(triquads_array)/2 - startindex), GL_UNSIGNED_SHORT, &triquads_array[startindex % 4][0] + (startindex / 4 * 6) );
{
pglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, gl2wrap.triquads_ibo[startindex % 4] );
rpglDrawRangeElements( GL_TRIANGLES, startindex, startindex + count, Q_min(count / 4 * 6,TRIQUADS_SIZE * 6 - startindex), GL_UNSIGNED_SHORT, (void*)(size_t)(startindex / 4 * 6 * 2) );
pglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
}
else
rpglDrawElements( GL_TRIANGLES, Q_min(count / 4 * 6,sizeof(triquads_array)/2), GL_UNSIGNED_SHORT, &triquads_array[startindex % 4][0] + (startindex / 4 * 6) );
{
pglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, gl2wrap.triquads_ibo[startindex % 4] );
rpglDrawElements( GL_TRIANGLES, Q_min(count / 4 * 6,TRIQUADS_SIZE * 6 - startindex), GL_UNSIGNED_SHORT, (void*)(size_t)(startindex / 4 * 6 * 2) );
pglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
}
}
else if( gl2wrap.prim == GL_POLYGON )
rpglDrawArrays( GL_TRIANGLE_FAN, startindex, count );
Expand Down Expand Up @@ -876,7 +895,7 @@ static void APIENTRY GL2_End( void )
GL2_FlushPrims();
}

static void (* APIENTRY rpglTexImage2D)( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels );
static void (APIENTRY* rpglTexImage2D)( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels );
static void APIENTRY GL2_TexImage2D( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels )
{
void *data = (void*)pixels;
Expand Down Expand Up @@ -906,7 +925,7 @@ static void APIENTRY GL2_TexImage2D( GLenum target, GLint level, GLint internalf
if( data != pixels )
free(data);
}
static void (* APIENTRY rpglTexParameteri)( GLenum target, GLenum pname, GLint param );
static void (APIENTRY* rpglTexParameteri)( GLenum target, GLenum pname, GLint param );
static void APIENTRY GL2_TexParameteri( GLenum target, GLenum pname, GLint param )
{
if ( pname == GL_TEXTURE_BORDER_COLOR )
Expand All @@ -924,7 +943,7 @@ static void APIENTRY GL2_TexParameteri( GLenum target, GLenum pname, GLint param
}


GLboolean (* APIENTRY rpglIsEnabled)(GLenum e);
GLboolean (APIENTRY* rpglIsEnabled)(GLenum e);
static GLboolean APIENTRY GL2_IsEnabled(GLenum e)
{
if(e == GL_FOG)
Expand Down Expand Up @@ -1247,17 +1266,17 @@ static void GL2_SetPointer( int idx, GLint size, GLenum type, GLsizei stride, co
// gl2wrap_arrays.vbo_flags &= ~(1 << idx);
}

void GL2_VertexPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
static void APIENTRY GL2_VertexPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
{
GL2_SetPointer( GL2_ATTR_POS, size, type, stride, pointer );
}

void GL2_ColorPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
static void APIENTRY GL2_ColorPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
{
GL2_SetPointer( GL2_ATTR_COLOR, size, type, stride, pointer );
}

void GL2_TexCoordPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
static void APIENTRY GL2_TexCoordPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *pointer )
{
GL2_SetPointer( GL2_ATTR_TEXCOORD0 + gl2wrap_arrays.texture, size, type, stride, pointer );
}
Expand All @@ -1277,13 +1296,13 @@ static unsigned int GL2_GetArrIdx( GLenum array )
}
}

void GL2_EnableClientState( GLenum array )
static void APIENTRY GL2_EnableClientState( GLenum array )
{
int idx = GL2_GetArrIdx(array);
gl2wrap_arrays.flags |= 1 << idx;
}

void GL2_DisableClientState( GLenum array )
static void APIENTRY GL2_DisableClientState( GLenum array )
{
unsigned int idx = GL2_GetArrIdx(array);
gl2wrap_arrays.flags &= ~(1 << idx);
Expand Down Expand Up @@ -1587,6 +1606,7 @@ void GL2_ShimInstall( void )
_pglWaitSync = gEngfuncs.GL_GetProcAddress("glWaitSync");
_pglClientWaitSync = gEngfuncs.GL_GetProcAddress("glClientWaitSync");
_pglDeleteSync = gEngfuncs.GL_GetProcAddress("glDeleteSync");
_pglDrawRangeElementsBaseVertex = gEngfuncs.GL_GetProcAddress("glDrawRangeElementsBaseVertex");

#ifdef QUAD_BATCH
GL2_OVERRIDE_PTR_B( BindTexture )
Expand Down

0 comments on commit e9fcfc3

Please sign in to comment.