Skip to content

Commit

Permalink
Merge pull request #1 from briancullinan2/polybuffers
Browse files Browse the repository at this point in the history
Polybuffers
  • Loading branch information
briancullinan2 authored Jun 12, 2024
2 parents edbea83 + ad1f4da commit 1422e6f
Show file tree
Hide file tree
Showing 45 changed files with 5,609 additions and 849 deletions.
9 changes: 9 additions & 0 deletions code/cgame/cg_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,16 @@ typedef enum {
CG_STOPCAMERA,
CG_GETCAMERAINFO,

CG_R_ADDPOLYBUFFERTOSCENE,


CG_MEMSET = 100,
CG_MEMCPY,
CG_STRNCPY,
CG_SIN,
CG_COS,
CG_ATAN2,
CG_SQRT,
CG_FLOOR = 107,
CG_CEIL,
CG_TESTPRINTINT,
Expand Down
3 changes: 3 additions & 0 deletions code/client/cl_cgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ static intptr_t CL_CgameSystemCalls( intptr_t *args ) {
case CG_R_ADDPOLYSTOSCENE:
re.AddPolyToScene( args[1], args[2], VMA(3), args[4] );
return 0;
case CG_R_ADDPOLYBUFFERTOSCENE:
re.AddPolyBufferToScene( VMA( 1 ) );
break;
case CG_R_LIGHTFORPOINT:
return re.LightForPoint( VMA(1), VMA(2), VMA(3), VMA(4) );
case CG_R_ADDLIGHTTOSCENE:
Expand Down
2 changes: 1 addition & 1 deletion code/qcommon/qfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

// surface geometry should not exceed these limits
#define SHADER_MAX_VERTEXES 1000
#define SHADER_MAX_VERTEXES 2000
#define SHADER_MAX_INDEXES (6*SHADER_MAX_VERTEXES)


Expand Down
4 changes: 2 additions & 2 deletions code/renderer/tr_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ Stretches a raw 32 bit power of 2 bitmap image over the given screen rectangle.
Used for cinematics.
=============
*/
void RE_StretchRaw( int x, int y, int w, int h, int cols, int rows, byte *data, int client, qboolean dirty ) {
void RE_StretchRaw( int x, int y, int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty ) {
int i, j;
int start, end;

Expand Down Expand Up @@ -1021,7 +1021,7 @@ void RE_StretchRaw( int x, int y, int w, int h, int cols, int rows, byte *data,
}


void RE_UploadCinematic( int w, int h, int cols, int rows, byte *data, int client, qboolean dirty ) {
void RE_UploadCinematic( int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty ) {

image_t *image;

Expand Down
9 changes: 8 additions & 1 deletion code/renderer/tr_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ cvar_t *r_screenshotJpegQuality;

static cvar_t *r_maxpolys;
static cvar_t* r_maxpolyverts;
static cvar_t *r_maxpolybuffers;
int max_polys;
int max_polyverts;
int max_polybuffers;

static char gl_extensions[ 32768 ];

Expand Down Expand Up @@ -1530,6 +1532,7 @@ static void R_Register( void )
ri.Cvar_SetDescription( r_maxpolys, "Maximum number of polygons to draw in a scene." );
r_maxpolyverts = ri.Cvar_Get( "r_maxpolyverts", XSTRING( MAX_POLYVERTS ), CVAR_LATCH );
ri.Cvar_SetDescription( r_maxpolyverts, "Maximum number of polygon vertices to draw in a scene." );
r_maxpolybuffers = ri.Cvar_Get( "r_maxpolybuffers", va("%i", MAX_POLYBUFFERS), CVAR_LATCH);

//
// archived variables that can change at any time
Expand Down Expand Up @@ -1862,11 +1865,13 @@ void R_Init( void ) {

max_polys = r_maxpolys->integer;
max_polyverts = r_maxpolyverts->integer;
max_polybuffers = r_maxpolybuffers->integer;

ptr = ri.Hunk_Alloc( sizeof( *backEndData ) + sizeof(srfPoly_t) * max_polys + sizeof(polyVert_t) * max_polyverts, h_low);
ptr = ri.Hunk_Alloc( sizeof( *backEndData ) + sizeof(srfPoly_t) * max_polys + sizeof(polyVert_t) * max_polyverts + sizeof(srfPolyBuffer_t) * max_polybuffers, h_low);
backEndData = (backEndData_t *) ptr;
backEndData->polys = (srfPoly_t *) ((char *) ptr + sizeof( *backEndData ));
backEndData->polyVerts = (polyVert_t *) ((char *) ptr + sizeof( *backEndData ) + sizeof(srfPoly_t) * max_polys);
backEndData->polybuffers = (srfPolyBuffer_t *) ((char *) ptr + sizeof( *backEndData ) + sizeof(srfPoly_t) * max_polys + sizeof(polyVert_t) * max_polyverts);

R_InitNextFrame();

Expand Down Expand Up @@ -2033,5 +2038,7 @@ refexport_t *GetRefAPI ( int apiVersion, refimport_t *rimp ) {
re.VertexLighting = RE_VertexLighting;
re.SyncRender = RE_SyncRender;

re.AddPolyBufferToScene = RE_AddPolyBufferToScene;

return &re;
}
21 changes: 18 additions & 3 deletions code/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ typedef struct {
int numPolys;
struct srfPoly_s *polys;

int numPolyBuffers;
struct srfPolyBuffer_s *polybuffers;

int numDrawSurfs;
struct drawSurf_s *drawSurfs;
#ifdef USE_PMLIGHT
Expand Down Expand Up @@ -603,6 +606,7 @@ typedef enum {
SF_IQM,
SF_FLARE,
SF_ENTITY, // beams, rails, lightning, etc that can be determined by entity
SF_POLYBUFFER,

SF_NUM_SURFACE_TYPES,
SF_MAX = 0x7fffffff // ensures that sizeof( surfaceType_t ) == sizeof( int )
Expand Down Expand Up @@ -637,6 +641,12 @@ typedef struct srfPoly_s {
} srfPoly_t;


typedef struct srfPolyBuffer_s {
surfaceType_t surfaceType;
int fogIndex;
polyBuffer_t* pPolyBuffer;
} srfPolyBuffer_t;

typedef struct srfFlare_s {
surfaceType_t surfaceType;
vec3_t origin;
Expand Down Expand Up @@ -1332,7 +1342,7 @@ void R_AddNullModelSurfaces( trRefEntity_t *e );
void R_AddBeamSurfaces( trRefEntity_t *e );
void R_AddRailSurfaces( trRefEntity_t *e, qboolean isUnderwater );
void R_AddLightningBoltSurfaces( trRefEntity_t *e );

void R_AddPolygonBufferSurfaces( void );
void R_AddPolygonSurfaces( void );

void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader,
Expand Down Expand Up @@ -1416,8 +1426,8 @@ void GL_Cull( cullType_t cullType );
#define CLS_TEXCOORD_ARRAY 0x00000002
#define CLS_NORMAL_ARRAY 0x00000004

void RE_StretchRaw( int x, int y, int w, int h, int cols, int rows, byte *data, int client, qboolean dirty );
void RE_UploadCinematic( int w, int h, int cols, int rows, byte *data, int client, qboolean dirty );
void RE_StretchRaw( int x, int y, int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty );
void RE_UploadCinematic( int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty );

void RE_BeginFrame( stereoFrame_t stereoFrame );
void RE_BeginRegistration( glconfig_t *glconfig );
Expand Down Expand Up @@ -1689,6 +1699,7 @@ void R_InitNextFrame( void );
void RE_ClearScene( void );
void RE_AddRefEntityToScene( const refEntity_t *ent, qboolean intShaderTime );
void RE_AddPolyToScene( qhandle_t hShader , int numVerts, const polyVert_t *verts, int num );
void RE_AddPolyBufferToScene( polyBuffer_t* pPolyBuffer );
void RE_AddLightToScene( const vec3_t org, float intensity, float r, float g, float b );
void RE_AddAdditiveLightToScene( const vec3_t org, float intensity, float r, float g, float b );
void RE_AddLinearLightToScene( const vec3_t start, const vec3_t end, float intensity, float r, float g, float b );
Expand Down Expand Up @@ -1872,6 +1883,7 @@ typedef enum {
// the main view, all the 3D icons, etc
#define MAX_POLYS 8192
#define MAX_POLYVERTS 32768
#define MAX_POLYBUFFERS 256

// all of the information needed by the back end must be
// contained in a backEndData_t
Expand All @@ -1887,11 +1899,14 @@ typedef struct {
trRefEntity_t entities[MAX_REFENTITIES];
srfPoly_t *polys;//[MAX_POLYS];
polyVert_t *polyVerts;//[MAX_POLYVERTS];
srfPolyBuffer_t *polybuffers; //[MAX_POLYBUFFERS];
int *indexes;//[MAX_POLYVERTS];
renderCommandList_t commands;
} backEndData_t;

extern int max_polys;
extern int max_polyverts;
extern int max_polybuffers;

extern backEndData_t *backEndData;

Expand Down
2 changes: 2 additions & 0 deletions code/renderer/tr_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,8 @@ static void R_GenerateDrawSurfs( void ) {

R_AddPolygonSurfaces();

R_AddPolygonBufferSurfaces();

// set the projection matrix with the minimum zfar
// now that we have the world bounded
// this needs to be done before entities are
Expand Down
5 changes: 5 additions & 0 deletions code/renderer/tr_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,11 @@ void R_ModelBounds( qhandle_t handle, vec3_t mins, vec3_t maxs ) {

model = R_GetModelByHandle( handle );

if(handle == 0) {
VectorCopy( tr.world->bmodels[0].bounds[0], mins );
VectorCopy( tr.world->bmodels[0].bounds[1], maxs );
return;
} else
if(model->type == MOD_BRUSH) {
VectorCopy( model->bmodel->bounds[0], mins );
VectorCopy( model->bmodel->bounds[1], maxs );
Expand Down
113 changes: 113 additions & 0 deletions code/renderer/tr_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ static int r_numpolys;
static int r_firstScenePoly;

static int r_numpolyverts;
static int r_numindexes;

static int r_firstScenePolybuffer;
static int r_numpolybuffers;

/*
====================
Expand All @@ -64,6 +67,10 @@ void R_InitNextFrame( void ) {
r_firstScenePoly = 0;

r_numpolyverts = 0;
r_numindexes = 0;

r_numpolybuffers = 0;
r_firstScenePolybuffer = 0;
}


Expand All @@ -77,6 +84,7 @@ void RE_ClearScene( void ) {
r_firstSceneDlight = r_numdlights;
r_firstSceneEntity = r_numentities;
r_firstScenePoly = r_numpolys;
r_firstScenePolybuffer = r_numpolybuffers;
}

/*
Expand Down Expand Up @@ -453,6 +461,9 @@ void RE_RenderScene( const refdef_t *fd ) {
tr.refdef.numPolys = r_numpolys - r_firstScenePoly;
tr.refdef.polys = &backEndData->polys[r_firstScenePoly];

tr.refdef.numPolyBuffers = r_numpolybuffers - r_firstScenePolybuffer;
tr.refdef.polybuffers = &backEndData->polybuffers[r_firstScenePolybuffer];

// turn off dynamic lighting globally by clearing all the
// dlights if it needs to be disabled
if ( r_dynamiclight->integer == 0 || glConfig.hardwareType == GLHW_PERMEDIA2 ) {
Expand Down Expand Up @@ -514,6 +525,108 @@ void RE_RenderScene( const refdef_t *fd ) {
r_firstSceneEntity = r_numentities;
r_firstSceneDlight = r_numdlights;
r_firstScenePoly = r_numpolys;
r_firstScenePolybuffer = r_numpolybuffers;

tr.frontEndMsec += ri.Milliseconds() - startTime;
}


/*
=====================
R_AddPolygonBufferSurfaces
Adds all the scene's polys into this view's drawsurf list
=====================
*/
void R_AddPolygonBufferSurfaces( void ) {
int i;
shader_t *sh;
srfPolyBuffer_t *polybuffer;

tr.currentEntityNum = REFENTITYNUM_WORLD;
tr.shiftedEntityNum = tr.currentEntityNum << QSORT_REFENTITYNUM_SHIFT;

#ifdef USE_UNLOCKED_CVARS
int startList = floor(tr.refdef.firstPolyBuffer / MAX_POLYBUFFERS_DIVISOR);
int numLists = floor((tr.refdef.numPolyBuffers - tr.refdef.firstPolyBuffer) / MAX_POLYBUFFERS_DIVISOR);
int startIndex = tr.refdef.firstPolyBuffer % MAX_POLYBUFFERS_DIVISOR;
for( int j = startList; j <= startList + numLists; j++ ) {
for ( i = j == startList ? startIndex : 0,
polybuffer = j == startList ? &backEndData->polybuffers[j][startIndex] : &backEndData->polybuffers[j][0];
(j * MAX_POLYBUFFERS_DIVISOR) + i < tr.refdef.numPolyBuffers ; i++, polybuffer++
) {
sh = R_GetShaderByHandle( polybuffer->pPolyBuffer->shader );
R_AddDrawSurf( ( void * )polybuffer, sh, polybuffer->fogIndex, 0 );
}
}
#else
for ( i = 0, polybuffer = tr.refdef.polybuffers; i < tr.refdef.numPolyBuffers ; i++, polybuffer++ ) {
sh = R_GetShaderByHandle( polybuffer->pPolyBuffer->shader );

R_AddDrawSurf( ( void * )polybuffer, sh, polybuffer->fogIndex, 0 );
}
#endif
}


/*
=====================
RE_AddPolyBufferToScene
=====================
*/
void RE_AddPolyBufferToScene( polyBuffer_t* pPolyBuffer ) {
srfPolyBuffer_t* pPolySurf;
int fogIndex;
fog_t* fog;
vec3_t bounds[2];
int i;

if ( r_numpolybuffers >= max_polybuffers
|| r_numpolyverts + pPolyBuffer->numVerts >= max_polyverts
) {
ri.Printf( PRINT_DEVELOPER, "WARNING: RE_AddPolyBufferToScene: r_numpolybuffers or r_maxpolyverts reached\n");
return;
}

#ifdef USE_UNLOCKED_CVARS
int buffersUsed = r_numpolybuffers % MAX_POLYBUFFERS_DIVISOR;
int buffersList = (r_numpolybuffers - buffersUsed) / MAX_POLYBUFFERS_DIVISOR;
if(buffersUsed + 1 >= MAX_POLYBUFFERS_DIVISOR) {
Com_Printf("Expanding the polybuffers list one time.\n");
backEndData->polyVerts[buffersList + 1] = ri.Hunk_Alloc(sizeof(polyBuffer_t) * MAX_POLYBUFFERS_DIVISOR, h_low);
r_numpolyverts = (buffersList + 1) * MAX_POLYBUFFERS_DIVISOR;
pPolySurf = &backEndData->polybuffers[buffersList + 1][0];
} else {
pPolySurf = &backEndData->polybuffers[buffersList][buffersUsed];
}
#else
pPolySurf = &backEndData->polybuffers[r_numpolybuffers];
#endif
r_numpolybuffers++;

pPolySurf->surfaceType = SF_POLYBUFFER;
pPolySurf->pPolyBuffer = pPolyBuffer;

VectorCopy( pPolyBuffer->xyz[0], bounds[0] );
VectorCopy( pPolyBuffer->xyz[0], bounds[1] );
for ( i = 1 ; i < pPolyBuffer->numVerts ; i++ ) {
AddPointToBounds( pPolyBuffer->xyz[i], bounds[0], bounds[1] );
}
for ( fogIndex = 1 ; fogIndex < tr.world->numfogs ; fogIndex++ ) {
fog = &tr.world->fogs[fogIndex];
if ( bounds[1][0] >= fog->bounds[0][0]
&& bounds[1][1] >= fog->bounds[0][1]
&& bounds[1][2] >= fog->bounds[0][2]
&& bounds[0][0] <= fog->bounds[1][0]
&& bounds[0][1] <= fog->bounds[1][1]
&& bounds[0][2] <= fog->bounds[1][2] ) {
break;
}
}
if ( fogIndex == tr.world->numfogs ) {
fogIndex = 0;
}

pPolySurf->fogIndex = fogIndex;
}
2 changes: 1 addition & 1 deletion code/renderer/tr_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ static qboolean ParseShader( const char **text )
shader.noPicMip = 1;
continue;
}
else if ( !Q_stricmp( token, "novlcollapse" ) && s_extendedShader )
else if ( !Q_stricmp( token, "novlcollapse" ) /* && s_extendedShader */ )
{
shader.noVLcollapse = 1;
continue;
Expand Down
29 changes: 28 additions & 1 deletion code/renderer/tr_surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,32 @@ static void RB_SurfaceGrid( srfGridMesh_t *cv ) {
}


static void RB_SurfacePolyBuffer( srfPolyBuffer_t *surf ) {
int i;
int numv;

VBO_Flush();

RB_CHECKOVERFLOW( surf->pPolyBuffer->numVerts, surf->pPolyBuffer->numIndicies );

tess.surfType = SF_POLYBUFFER;

numv = tess.numVertexes;
for ( i = 0; i < surf->pPolyBuffer->numVerts; i++ ) {
VectorCopy( surf->pPolyBuffer->xyz[i], tess.xyz[numv] );
tess.texCoords[0][numv][0] = surf->pPolyBuffer->st[i][0];
tess.texCoords[0][numv][1] = surf->pPolyBuffer->st[i][1];
*(int *)&tess.vertexColors[numv] = *(int *)surf->pPolyBuffer->color[i];

numv++;
}

for ( i = 0; i < surf->pPolyBuffer->numIndicies; i++ ) {
tess.indexes[tess.numIndexes++] = tess.numVertexes + surf->pPolyBuffer->indicies[i];
}

tess.numVertexes = numv;
}
/*
===========================================================================
Expand Down Expand Up @@ -1373,5 +1399,6 @@ void (*rb_surfaceTable[SF_NUM_SURFACE_TYPES])( void *) = {
(void(*)(void*))RB_MDRSurfaceAnim, // SF_MDR,
(void(*)(void*))RB_IQMSurfaceAnim, // SF_IQM,
(void(*)(void*))RB_SurfaceFlare, // SF_FLARE,
(void(*)(void*))RB_SurfaceEntity // SF_ENTITY
(void(*)(void*))RB_SurfaceEntity, // SF_ENTITY
(void(*)(void*))RB_SurfacePolyBuffer // SF_POLYBUFFER,
};
Loading

0 comments on commit 1422e6f

Please sign in to comment.