1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2000-2009 Darklegion Development
This file is part of Tremulous.
Tremulous is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Tremulous is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "tr_local.h"
volatile renderCommandList_t *renderCommandList;
volatile qboolean renderThreadActive;
/*
=====================
R_PerformanceCounters
=====================
*/
void R_PerformanceCounters( void ) {
if ( !r_speeds->integer ) {
// clear the counters even if we aren't printing
Com_Memset( &tr.pc, 0, sizeof( tr.pc ) );
Com_Memset( &backEnd.pc, 0, sizeof( backEnd.pc ) );
return;
}
if (r_speeds->integer == 1) {
ri.Printf (PRINT_ALL, "%i/%i shaders/surfs %i leafs %i verts %i/%i tris %.2f mtex %.2f dc\n",
backEnd.pc.c_shaders, backEnd.pc.c_surfaces, tr.pc.c_leafs, backEnd.pc.c_vertexes,
backEnd.pc.c_indexes/3, backEnd.pc.c_totalIndexes/3,
R_SumOfUsedImages()/(1000000.0f), backEnd.pc.c_overDraw / (float)(glConfig.vidWidth * glConfig.vidHeight) );
} else if (r_speeds->integer == 2) {
ri.Printf (PRINT_ALL, "(patch) %i sin %i sclip %i sout %i bin %i bclip %i bout\n",
tr.pc.c_sphere_cull_patch_in, tr.pc.c_sphere_cull_patch_clip, tr.pc.c_sphere_cull_patch_out,
tr.pc.c_box_cull_patch_in, tr.pc.c_box_cull_patch_clip, tr.pc.c_box_cull_patch_out );
ri.Printf (PRINT_ALL, "(md3) %i sin %i sclip %i sout %i bin %i bclip %i bout\n",
tr.pc.c_sphere_cull_md3_in, tr.pc.c_sphere_cull_md3_clip, tr.pc.c_sphere_cull_md3_out,
tr.pc.c_box_cull_md3_in, tr.pc.c_box_cull_md3_clip, tr.pc.c_box_cull_md3_out );
} else if (r_speeds->integer == 3) {
ri.Printf (PRINT_ALL, "viewcluster: %i\n", tr.viewCluster );
} else if (r_speeds->integer == 4) {
if ( backEnd.pc.c_dlightVertexes ) {
ri.Printf (PRINT_ALL, "dlight srf:%i culled:%i verts:%i tris:%i\n",
tr.pc.c_dlightSurfaces, tr.pc.c_dlightSurfacesCulled,
backEnd.pc.c_dlightVertexes, backEnd.pc.c_dlightIndexes / 3 );
}
}
else if (r_speeds->integer == 5 )
{
ri.Printf( PRINT_ALL, "zFar: %.0f\n", tr.viewParms.zFar );
}
else if (r_speeds->integer == 6 )
{
ri.Printf( PRINT_ALL, "flare adds:%i tests:%i renders:%i\n",
backEnd.pc.c_flareAdds, backEnd.pc.c_flareTests, backEnd.pc.c_flareRenders );
}
Com_Memset( &tr.pc, 0, sizeof( tr.pc ) );
Com_Memset( &backEnd.pc, 0, sizeof( backEnd.pc ) );
}
/*
====================
R_InitCommandBuffers
====================
*/
void R_InitCommandBuffers( void ) {
glConfig.smpActive = qfalse;
if ( r_smp->integer ) {
ri.Printf( PRINT_ALL, "Trying SMP acceleration...\n" );
if ( GLimp_SpawnRenderThread( RB_RenderThread ) ) {
ri.Printf( PRINT_ALL, "...succeeded.\n" );
glConfig.smpActive = qtrue;
} else {
ri.Printf( PRINT_ALL, "...failed.\n" );
}
}
}
/*
====================
R_ShutdownCommandBuffers
====================
*/
void R_ShutdownCommandBuffers( void ) {
// kill the rendering thread
if ( glConfig.smpActive ) {
GLimp_WakeRenderer( NULL );
glConfig.smpActive = qfalse;
}
}
/*
====================
R_IssueRenderCommands
====================
*/
int c_blockedOnRender;
int c_blockedOnMain;
void R_IssueRenderCommands( qboolean runPerformanceCounters ) {
renderCommandList_t *cmdList;
cmdList = &backEndData[tr.smpFrame]->commands;
assert(cmdList);
// add an end-of-list command
*(int *)(cmdList->cmds + cmdList->used) = RC_END_OF_LIST;
// clear it out, in case this is a sync and not a buffer flip
cmdList->used = 0;
if ( glConfig.smpActive ) {
// if the render thread is not idle, wait for it
if ( renderThreadActive ) {
c_blockedOnRender++;
if ( r_showSmp->integer ) {
ri.Printf( PRINT_ALL, "R" );
}
} else {
c_blockedOnMain++;
if ( r_showSmp->integer ) {
ri.Printf( PRINT_ALL, "." );
}
}
// sleep until the renderer has completed
GLimp_FrontEndSleep();
}
// at this point, the back end thread is idle, so it is ok
// to look at it's performance counters
if ( runPerformanceCounters ) {
R_PerformanceCounters();
}
// actually start the commands going
if ( !r_skipBackEnd->integer ) {
// let it start on the new batch
if ( !glConfig.smpActive ) {
RB_ExecuteRenderCommands( cmdList->cmds );
} else {
GLimp_WakeRenderer( cmdList );
}
}
}
/*
====================
R_SyncRenderThread
Issue any pending commands and wait for them to complete.
After exiting, the render thread will have completed its work
and will remain idle and the main thread is free to issue
OpenGL calls until R_IssueRenderCommands is called.
====================
*/
void R_SyncRenderThread( void ) {
if ( !tr.registered ) {
return;
}
R_IssueRenderCommands( qfalse );
if ( !glConfig.smpActive ) {
return;
}
GLimp_FrontEndSleep();
}
/*
============
R_GetCommandBuffer
make sure there is enough command space, waiting on the
render thread if needed.
============
*/
void *R_GetCommandBuffer( int bytes ) {
renderCommandList_t *cmdList;
cmdList = &backEndData[tr.smpFrame]->commands;
// always leave room for the end of list command
if ( cmdList->used + bytes + 4 > MAX_RENDER_COMMANDS ) {
if ( bytes > MAX_RENDER_COMMANDS - 4 ) {
ri.Error( ERR_FATAL, "R_GetCommandBuffer: bad size %i", bytes );
}
// if we run out of room, just start dropping commands
return NULL;
}
cmdList->used += bytes;
return cmdList->cmds + cmdList->used - bytes;
}
/*
=============
R_AddDrawSurfCmd
=============
*/
void R_AddDrawSurfCmd( drawSurf_t *drawSurfs, int numDrawSurfs ) {
drawSurfsCommand_t *cmd;
cmd = R_GetCommandBuffer( sizeof( *cmd ) );
if ( !cmd ) {
return;
}
cmd->commandId = RC_DRAW_SURFS;
cmd->drawSurfs = drawSurfs;
cmd->numDrawSurfs = numDrawSurfs;
cmd->refdef = tr.refdef;
cmd->viewParms = tr.viewParms;
}
/*
=============
RE_SetColor
Passing NULL will set the color to white
=============
*/
void RE_SetColor( const float *rgba ) {
setColorCommand_t *cmd;
if ( !tr.registered ) {
return;
}
cmd = R_GetCommandBuffer( sizeof( *cmd ) );
if ( !cmd ) {
return;
}
cmd->commandId = RC_SET_COLOR;
if ( !rgba ) {
static float colorWhite[4] = { 1, 1, 1, 1 };
rgba = colorWhite;
}
cmd->color[0] = rgba[0];
cmd->color[1] = rgba[1];
cmd->color[2] = rgba[2];
cmd->color[3] = rgba[3];
}
/*
=============
R_ClipRegion
=============
*/
static qboolean R_ClipRegion ( float *x, float *y, float *w, float *h,
float *s1, float *t1, float *s2, float *t2 ) {
float left, top, right, bottom;
float _s1, _t1, _s2, _t2;
float clipLeft, clipTop, clipRight, clipBottom;
if (tr.clipRegion[2] <= tr.clipRegion[0] ||
tr.clipRegion[3] <= tr.clipRegion[1] ) {
return qfalse;
}
left = *x;
top = *y;
right = *x + *w;
bottom = *y + *h;
_s1 = *s1;
_t1 = *t1;
_s2 = *s2;
_t2 = *t2;
clipLeft = tr.clipRegion[0];
clipTop = tr.clipRegion[1];
clipRight = tr.clipRegion[2];
clipBottom = tr.clipRegion[3];
// Completely clipped away
if ( right <= clipLeft || left >= clipRight ||
bottom <= clipTop || top >= clipBottom ) {
return qtrue;
}
// Clip left edge
if ( left < clipLeft ) {
float f = ( clipLeft - left ) / ( right - left );
*s1 = ( f * ( _s2 - _s1 ) ) + _s1;
*x = clipLeft;
*w -= ( clipLeft - left );
}
// Clip right edge
if ( right > clipRight ) {
float f = ( clipRight - right ) / ( left - right );
*s2 = ( f * ( _s1 - _s2 ) ) + _s2;
*w = clipRight - *x;
}
// Clip top edge
if ( top < clipTop ) {
float f = ( clipTop - top ) / ( bottom - top );
*t1 = ( f * ( _t2 - _t1 ) ) + _t1;
*y = clipTop;
*h -= ( clipTop - top );
}
// Clip bottom edge
if ( bottom > clipBottom ) {
float f = ( clipBottom - bottom ) / ( top - bottom );
*t2 = ( f * ( _t1 - _t2 ) ) + _t2;
*h = clipBottom - *y;
}
return qfalse;
}
/*
=============
RE_SetClipRegion
=============
*/
void RE_SetClipRegion( const float *region ) {
if ( region == NULL ) {
Com_Memset( tr.clipRegion, 0, sizeof( vec4_t ) );
} else {
Vector4Copy( region, tr.clipRegion );
}
}
/*
=============
RE_StretchPic
=============
*/
void RE_StretchPic ( float x, float y, float w, float h,
float s1, float t1, float s2, float t2, qhandle_t hShader ) {
stretchPicCommand_t *cmd;
if (!tr.registered) {
return;
}
if (R_ClipRegion(&x, &y, &w, &h, &s1, &t1, &s2, &t2)) {
return;
}
cmd = R_GetCommandBuffer( sizeof( *cmd ) );
if ( !cmd ) {
return;
}
cmd->commandId = RC_STRETCH_PIC;
cmd->shader = R_GetShaderByHandle( hShader );
cmd->x = x;
cmd->y = y;
cmd->w = w;
cmd->h = h;
cmd->s1 = s1;
cmd->t1 = t1;
cmd->s2 = s2;
cmd->t2 = t2;
}
#define MODE_RED_CYAN 1
#define MODE_RED_BLUE 2
#define MODE_RED_GREEN 3
#define MODE_MAX MODE_RED_GREEN
void R_SetColorMode(GLboolean *rgba, stereoFrame_t stereoFrame, int colormode)
{
rgba[0] = rgba[1] = rgba[2] = rgba[3] = GL_TRUE;
if(colormode > MODE_MAX)
{
if(stereoFrame == STEREO_LEFT)
stereoFrame = STEREO_RIGHT;
else if(stereoFrame == STEREO_RIGHT)
stereoFrame = STEREO_LEFT;
colormode -= MODE_MAX;
}
if(stereoFrame == STEREO_LEFT)
rgba[1] = rgba[2] = GL_FALSE;
else if(stereoFrame == STEREO_RIGHT)
{
rgba[0] = GL_FALSE;
if(colormode == MODE_RED_BLUE)
rgba[1] = GL_FALSE;
else if(colormode == MODE_RED_GREEN)
rgba[2] = GL_FALSE;
}
}
/*
====================
RE_BeginFrame
If running in stereo, RE_BeginFrame will be called twice
for each RE_EndFrame
====================
*/
void RE_BeginFrame( stereoFrame_t stereoFrame ) {
drawBufferCommand_t *cmd = NULL;
colorMaskCommand_t *colcmd = NULL;
if ( !tr.registered ) {
return;
}
glState.finishCalled = qfalse;
tr.frameCount++;
tr.frameSceneNum = 0;
//
// do overdraw measurement
//
if ( r_measureOverdraw->integer )
{
if ( glConfig.stencilBits < 4 )
{
ri.Printf( PRINT_ALL, "Warning: not enough stencil bits to measure overdraw: %d\n", glConfig.stencilBits );
ri.Cvar_Set( "r_measureOverdraw", "0" );
r_measureOverdraw->modified = qfalse;
}
else if ( r_shadows->integer == 2 )
{
ri.Printf( PRINT_ALL, "Warning: stencil shadows and overdraw measurement are mutually exclusive\n" );
ri.Cvar_Set( "r_measureOverdraw", "0" );
r_measureOverdraw->modified = qfalse;
}
else
{
R_SyncRenderThread();
qglEnable( GL_STENCIL_TEST );
qglStencilMask( ~0U );
qglClearStencil( 0U );
qglStencilFunc( GL_ALWAYS, 0U, ~0U );
qglStencilOp( GL_KEEP, GL_INCR, GL_INCR );
}
r_measureOverdraw->modified = qfalse;
}
else
{
// this is only reached if it was on and is now off
if ( r_measureOverdraw->modified ) {
R_SyncRenderThread();
qglDisable( GL_STENCIL_TEST );
}
r_measureOverdraw->modified = qfalse;
}
//
// texturemode stuff
//
if ( r_textureMode->modified ) {
R_SyncRenderThread();
GL_TextureMode( r_textureMode->string );
r_textureMode->modified = qfalse;
}
//
// gamma stuff
//
if ( r_gamma->modified ) {
r_gamma->modified = qfalse;
R_SyncRenderThread();
R_SetColorMappings();
}
// check for errors
if ( !r_ignoreGLErrors->integer )
{
int err;
R_SyncRenderThread();
if ((err = qglGetError()) != GL_NO_ERROR)
ri.Error(ERR_FATAL, "RE_BeginFrame() - glGetError() failed (0x%x)!\n", err);
}
if (glConfig.stereoEnabled) {
if( !(cmd = R_GetCommandBuffer(sizeof(*cmd))) )
return;
cmd->commandId = RC_DRAW_BUFFER;
if ( stereoFrame == STEREO_LEFT ) {
cmd->buffer = (int)GL_BACK_LEFT;
} else if ( stereoFrame == STEREO_RIGHT ) {
cmd->buffer = (int)GL_BACK_RIGHT;
} else {
ri.Error( ERR_FATAL, "RE_BeginFrame: Stereo is enabled, but stereoFrame was %i", stereoFrame );
}
}
else
{
if(r_anaglyphMode->integer)
{
if(r_anaglyphMode->modified)
{
// clear both, front and backbuffer.
qglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
qglClearColor(0.0f, 0.0f, 0.0f, 1.0f);
qglDrawBuffer(GL_FRONT);
qglClear(GL_COLOR_BUFFER_BIT);
qglDrawBuffer(GL_BACK);
qglClear(GL_COLOR_BUFFER_BIT);
r_anaglyphMode->modified = qfalse;
}
if(stereoFrame == STEREO_LEFT)
{
if( !(cmd = R_GetCommandBuffer(sizeof(*cmd))) )
return;
if( !(colcmd = R_GetCommandBuffer(sizeof(*colcmd))) )
return;
}
else if(stereoFrame == STEREO_RIGHT)
{
clearDepthCommand_t *cldcmd;
if( !(cldcmd = R_GetCommandBuffer(sizeof(*cldcmd))) )
return;
cldcmd->commandId = RC_CLEARDEPTH;
if( !(colcmd = R_GetCommandBuffer(sizeof(*colcmd))) )
return;
}
else
ri.Error( ERR_FATAL, "RE_BeginFrame: Stereo is enabled, but stereoFrame was %i", stereoFrame );
R_SetColorMode(colcmd->rgba, stereoFrame, r_anaglyphMode->integer);
colcmd->commandId = RC_COLORMASK;
}
else
{
if(stereoFrame != STEREO_CENTER)
ri.Error( ERR_FATAL, "RE_BeginFrame: Stereo is disabled, but stereoFrame was %i", stereoFrame );
if( !(cmd = R_GetCommandBuffer(sizeof(*cmd))) )
return;
}
if(cmd)
{
cmd->commandId = RC_DRAW_BUFFER;
if(r_anaglyphMode->modified)
{
qglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
r_anaglyphMode->modified = qfalse;
}
if (!Q_stricmp(r_drawBuffer->string, "GL_FRONT"))
cmd->buffer = (int)GL_FRONT;
else
cmd->buffer = (int)GL_BACK;
}
}
tr.refdef.stereoFrame = stereoFrame;
}
/*
=============
RE_EndFrame
Returns the number of msec spent in the back end
=============
*/
void RE_EndFrame( int *frontEndMsec, int *backEndMsec ) {
swapBuffersCommand_t *cmd;
if ( !tr.registered ) {
return;
}
cmd = R_GetCommandBuffer( sizeof( *cmd ) );
if ( !cmd ) {
return;
}
cmd->commandId = RC_SWAP_BUFFERS;
R_IssueRenderCommands( qtrue );
// use the other buffers next frame, because another CPU
// may still be rendering into the current ones
R_ToggleSmpFrame();
if ( frontEndMsec ) {
*frontEndMsec = tr.frontEndMsec;
}
tr.frontEndMsec = 0;
if ( backEndMsec ) {
*backEndMsec = backEnd.pc.msec;
}
backEnd.pc.msec = 0;
}
/*
=============
RE_TakeVideoFrame
=============
*/
void RE_TakeVideoFrame( int width, int height,
byte *captureBuffer, byte *encodeBuffer, qboolean motionJpeg )
{
videoFrameCommand_t *cmd;
if( !tr.registered ) {
return;
}
cmd = R_GetCommandBuffer( sizeof( *cmd ) );
if( !cmd ) {
return;
}
cmd->commandId = RC_VIDEOFRAME;
cmd->width = width;
cmd->height = height;
cmd->captureBuffer = captureBuffer;
cmd->encodeBuffer = encodeBuffer;
cmd->motionJpeg = motionJpeg;
}
|