Skip to content

Commit f5ab5fa

Browse files
committed
Fix not swapping buffers because out of cmd buffer space
Reserve space for end of list and swap buffer commands. These are absolutely required and cannot be dropped. Dropping swap buffer command causes screen to not update and possible crash from drawsurf buffer overflow if not enough cmd buffer space for many continous frames. Fix reserved size for swap buffer command being too small on x86_64 Replace 4 with sizeof( int ) in R_GetCommandBufferReserved ioquake/ioq3@8531162 ioquake/ioq3@81e2b6c ioquake/ioq3@41f83ac Also make sure all renderer commands do not function when the renderer is not loaded (tr.registered) Simplify the setColor(NULL) command Fix incorrect null pointer check on an array Remove unused variables in SP tr_cmds Doubled the size of renderer command buffers to match other engines. Fixes and replaces/supersedes #1109
1 parent 5ef6711 commit f5ab5fa

File tree

6 files changed

+83
-43
lines changed

6 files changed

+83
-43
lines changed

code/rd-vanilla/tr_cmds.cpp

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ void R_PerformanceCounters( void ) {
8989
R_IssueRenderCommands
9090
====================
9191
*/
92-
int c_blockedOnRender;
93-
int c_blockedOnMain;
94-
9592
void R_IssueRenderCommands( qboolean runPerformanceCounters ) {
9693
renderCommandList_t *cmdList;
9794

@@ -134,23 +131,21 @@ void R_IssuePendingRenderCommands( void ) {
134131

135132
/*
136133
============
137-
R_GetCommandBuffer
134+
R_GetCommandBufferReserved
138135
139136
make sure there is enough command space
140137
============
141138
*/
142-
void *R_GetCommandBuffer( int bytes ) {
139+
static void *R_GetCommandBufferReserved( int bytes, int reservedBytes ) {
143140
renderCommandList_t *cmdList;
144141

145142
cmdList = &backEndData->commands;
146-
bytes = PAD(bytes, sizeof (void *));
147-
148-
assert(cmdList);
143+
bytes = PAD(bytes, sizeof(void *));
149144

150145
// always leave room for the end of list command
151-
if ( cmdList->used + bytes + 4 > MAX_RENDER_COMMANDS ) {
152-
if ( bytes > MAX_RENDER_COMMANDS - 4 ) {
153-
Com_Error( ERR_FATAL, "R_GetCommandBuffer: bad size %i", bytes );
146+
if ( cmdList->used + bytes + sizeof( int ) + reservedBytes > MAX_RENDER_COMMANDS ) {
147+
if ( bytes > MAX_RENDER_COMMANDS - (int)sizeof( int ) ) {
148+
ri.Error( ERR_FATAL, "R_GetCommandBuffer: bad size %i", bytes );
154149
}
155150
// if we run out of room, just start dropping commands
156151
return NULL;
@@ -161,6 +156,17 @@ void *R_GetCommandBuffer( int bytes ) {
161156
return cmdList->cmds + cmdList->used - bytes;
162157
}
163158

159+
/*
160+
============
161+
R_GetCommandBuffer
162+
163+
make sure there is enough command space
164+
============
165+
*/
166+
static void *R_GetCommandBuffer( int bytes ) {
167+
return R_GetCommandBufferReserved( bytes, PAD( sizeof( swapBuffersCommand_t ), sizeof(void *) ) );
168+
}
169+
164170

165171
/*
166172
=============
@@ -203,18 +209,14 @@ void RE_SetColor( const float *rgba ) {
203209
return;
204210
}
205211
cmd->commandId = RC_SET_COLOR;
206-
if ( rgba ) {
207-
cmd->color[0] = rgba[0];
208-
cmd->color[1] = rgba[1];
209-
cmd->color[2] = rgba[2];
210-
cmd->color[3] = rgba[3];
211-
return;
212+
if ( !rgba ) {
213+
rgba = colorWhite;
212214
}
215+
cmd->color[0] = rgba[0];
216+
cmd->color[1] = rgba[1];
217+
cmd->color[2] = rgba[2];
218+
cmd->color[3] = rgba[3];
213219

214-
cmd->color[0] = 1;
215-
cmd->color[1] = 1;
216-
cmd->color[2] = 1;
217-
cmd->color[3] = 1;
218220
}
219221

220222

@@ -227,6 +229,9 @@ void RE_StretchPic ( float x, float y, float w, float h,
227229
float s1, float t1, float s2, float t2, qhandle_t hShader ) {
228230
stretchPicCommand_t *cmd;
229231

232+
if ( !tr.registered ) {
233+
return;
234+
}
230235
cmd = (stretchPicCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
231236
if ( !cmd ) {
232237
return;
@@ -252,6 +257,9 @@ void RE_RotatePic ( float x, float y, float w, float h,
252257
float s1, float t1, float s2, float t2,float a, qhandle_t hShader ) {
253258
rotatePicCommand_t *cmd;
254259

260+
if (!tr.registered) {
261+
return;
262+
}
255263
cmd = (rotatePicCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
256264
if ( !cmd ) {
257265
return;
@@ -278,6 +286,10 @@ void RE_RotatePic2 ( float x, float y, float w, float h,
278286
float s1, float t1, float s2, float t2,float a, qhandle_t hShader ) {
279287
rotatePicCommand_t *cmd;
280288

289+
if (!tr.registered) {
290+
return;
291+
}
292+
281293
cmd = (rotatePicCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
282294
if ( !cmd ) {
283295
return;
@@ -314,6 +326,10 @@ void RE_RenderWorldEffects(void)
314326
{
315327
setModeCommand_t *cmd;
316328

329+
if (!tr.registered) {
330+
return;
331+
}
332+
317333
cmd = (setModeCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
318334
if ( !cmd ) {
319335
return;
@@ -330,6 +346,10 @@ void RE_Scissor ( float x, float y, float w, float h)
330346
{
331347
scissorCommand_t *cmd;
332348

349+
if (!tr.registered) {
350+
return;
351+
}
352+
333353
cmd = (scissorCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
334354
if ( !cmd ) {
335355
return;
@@ -350,7 +370,7 @@ for each RE_EndFrame
350370
====================
351371
*/
352372
void RE_BeginFrame( stereoFrame_t stereoFrame ) {
353-
drawBufferCommand_t *cmd;
373+
drawBufferCommand_t *cmd = NULL;
354374

355375
if ( !tr.registered ) {
356376
return;
@@ -472,7 +492,7 @@ void RE_EndFrame( int *frontEndMsec, int *backEndMsec ) {
472492
if ( !tr.registered ) {
473493
return;
474494
}
475-
cmd = (swapBuffersCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
495+
cmd = (swapBuffersCommand_t *) R_GetCommandBufferReserved( sizeof( *cmd ), 0 );
476496
if ( !cmd ) {
477497
return;
478498
}

code/rd-vanilla/tr_local.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ RENDERER BACK END COMMAND QUEUE
16941694
=============================================================
16951695
*/
16961696

1697-
#define MAX_RENDER_COMMANDS 0x40000
1697+
#define MAX_RENDER_COMMANDS 0x80000
16981698

16991699
typedef struct {
17001700
byte cmds[MAX_RENDER_COMMANDS];
@@ -1800,7 +1800,6 @@ typedef struct {
18001800

18011801
extern backEndData_t *backEndData;
18021802

1803-
void *R_GetCommandBuffer( int bytes );
18041803
void RB_ExecuteRenderCommands( const void *data );
18051804

18061805
void R_IssuePendingRenderCommands( void );

code/rd-vanilla/tr_shader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3372,7 +3372,7 @@ static inline const int *R_FindLightmap( const int *lightmapIndex )
33723372
return lightmapIndex;
33733373

33743374
// bail if no world dir
3375-
if( tr.worldDir == NULL || !*tr.worldDir )
3375+
if( tr.worldDir[0] == '\0' )
33763376
{
33773377
return lightmapsVertex;
33783378
}

codemp/rd-dedicated/tr_local.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,6 @@ extern int max_polyverts;
18091809
extern backEndData_t *backEndData;
18101810

18111811

1812-
void *R_GetCommandBuffer( int bytes );
18131812
void RB_ExecuteRenderCommands( const void *data );
18141813

18151814
void R_IssuePendingRenderCommands( void );

codemp/rd-vanilla/tr_cmds.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void R_IssueRenderCommands( qboolean runPerformanceCounters ) {
9090
renderCommandList_t *cmdList;
9191

9292
cmdList = &backEndData->commands;
93-
assert(cmdList);
93+
9494
// add an end-of-list command
9595
byteAlias_t *ba = (byteAlias_t *)&cmdList->cmds[cmdList->used];
9696
ba->ui = RC_END_OF_LIST;
@@ -128,23 +128,21 @@ void R_IssuePendingRenderCommands( void ) {
128128

129129
/*
130130
============
131-
R_GetCommandBuffer
131+
R_GetCommandBufferReserved
132132
133133
make sure there is enough command space
134134
============
135135
*/
136-
void *R_GetCommandBuffer( int bytes ) {
136+
static void *R_GetCommandBufferReserved( int bytes, int reservedBytes ) {
137137
renderCommandList_t *cmdList;
138138

139139
cmdList = &backEndData->commands;
140-
bytes = PAD(bytes, sizeof (void *));
141-
142-
assert(cmdList);
140+
bytes = PAD(bytes, sizeof(void *));
143141

144142
// always leave room for the end of list command
145-
if ( cmdList->used + bytes + 4 > MAX_RENDER_COMMANDS ) {
146-
if ( bytes > MAX_RENDER_COMMANDS - 4 ) {
147-
Com_Error( ERR_FATAL, "R_GetCommandBuffer: bad size %i", bytes );
143+
if ( cmdList->used + bytes + sizeof( int ) + reservedBytes > MAX_RENDER_COMMANDS ) {
144+
if ( bytes > MAX_RENDER_COMMANDS - (int)sizeof( int ) ) {
145+
ri.Error( ERR_FATAL, "R_GetCommandBuffer: bad size %i", bytes );
148146
}
149147
// if we run out of room, just start dropping commands
150148
return NULL;
@@ -155,6 +153,17 @@ void *R_GetCommandBuffer( int bytes ) {
155153
return cmdList->cmds + cmdList->used - bytes;
156154
}
157155

156+
/*
157+
============
158+
R_GetCommandBuffer
159+
160+
make sure there is enough command space
161+
============
162+
*/
163+
static void *R_GetCommandBuffer( int bytes ) {
164+
return R_GetCommandBufferReserved( bytes, PAD( sizeof( swapBuffersCommand_t ), sizeof(void *) ) );
165+
}
166+
158167

159168
/*
160169
=============
@@ -198,15 +207,13 @@ void RE_SetColor( const float *rgba ) {
198207
}
199208
cmd->commandId = RC_SET_COLOR;
200209
if ( !rgba ) {
201-
static float colorWhite[4] = { 1, 1, 1, 1 };
202-
203210
rgba = colorWhite;
204211
}
205-
206212
cmd->color[0] = rgba[0];
207213
cmd->color[1] = rgba[1];
208214
cmd->color[2] = rgba[2];
209215
cmd->color[3] = rgba[3];
216+
210217
}
211218

212219

@@ -219,6 +226,9 @@ void RE_StretchPic ( float x, float y, float w, float h,
219226
float s1, float t1, float s2, float t2, qhandle_t hShader ) {
220227
stretchPicCommand_t *cmd;
221228

229+
if ( !tr.registered ) {
230+
return;
231+
}
222232
cmd = (stretchPicCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
223233
if ( !cmd ) {
224234
return;
@@ -244,6 +254,9 @@ void RE_RotatePic ( float x, float y, float w, float h,
244254
float s1, float t1, float s2, float t2,float a, qhandle_t hShader ) {
245255
rotatePicCommand_t *cmd;
246256

257+
if (!tr.registered) {
258+
return;
259+
}
247260
cmd = (rotatePicCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
248261
if ( !cmd ) {
249262
return;
@@ -270,6 +283,10 @@ void RE_RotatePic2 ( float x, float y, float w, float h,
270283
float s1, float t1, float s2, float t2,float a, qhandle_t hShader ) {
271284
rotatePicCommand_t *cmd;
272285

286+
if (!tr.registered) {
287+
return;
288+
}
289+
273290
cmd = (rotatePicCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
274291
if ( !cmd ) {
275292
return;
@@ -291,6 +308,9 @@ void RE_RenderWorldEffects(void)
291308
{
292309
drawBufferCommand_t *cmd;
293310

311+
if (!tr.registered) {
312+
return;
313+
}
294314
cmd = (drawBufferCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
295315
if ( !cmd ) {
296316
return;
@@ -302,6 +322,9 @@ void RE_RenderAutoMap(void)
302322
{
303323
drawBufferCommand_t *cmd;
304324

325+
if (!tr.registered) {
326+
return;
327+
}
305328
cmd = (drawBufferCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
306329
if ( !cmd )
307330
{
@@ -319,7 +342,7 @@ for each RE_EndFrame
319342
====================
320343
*/
321344
void RE_BeginFrame( stereoFrame_t stereoFrame ) {
322-
drawBufferCommand_t *cmd;
345+
drawBufferCommand_t *cmd = NULL;
323346

324347
if ( !tr.registered ) {
325348
return;
@@ -442,7 +465,7 @@ void RE_EndFrame( int *frontEndMsec, int *backEndMsec ) {
442465
if ( !tr.registered ) {
443466
return;
444467
}
445-
cmd = (swapBuffersCommand_t *) R_GetCommandBuffer( sizeof( *cmd ) );
468+
cmd = (swapBuffersCommand_t *) R_GetCommandBufferReserved( sizeof( *cmd ), 0 );
446469
if ( !cmd ) {
447470
return;
448471
}

codemp/rd-vanilla/tr_local.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ RENDERER BACK END COMMAND QUEUE
17301730
=============================================================
17311731
*/
17321732

1733-
#define MAX_RENDER_COMMANDS 0x40000
1733+
#define MAX_RENDER_COMMANDS 0x80000
17341734

17351735
typedef struct renderCommandList_s {
17361736
byte cmds[MAX_RENDER_COMMANDS];
@@ -1833,7 +1833,6 @@ extern int max_polyverts;
18331833
extern backEndData_t *backEndData;
18341834

18351835

1836-
void *R_GetCommandBuffer( int bytes );
18371836
void RB_ExecuteRenderCommands( const void *data );
18381837

18391838
void R_IssuePendingRenderCommands( void );

0 commit comments

Comments
 (0)