Debian Codegraph @

code/renderergl2/tr_cmds.c

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