Debian Codegraph @

code/renderergl1/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 shaders/surfs %i leafs %i verts %i/%i tris %.2f mtex %.2f dc\n",
39 backEnd.pc.c_shaders, 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
68 Com_Memset( &tr.pc, 0, sizeof( tr.pc ) );
69 Com_Memset( &backEnd.pc, 0, sizeof( backEnd.pc ) );
70 }
71
72
73 /*
74 ====================
75 R_IssueRenderCommands
76 ====================
77 */
78 void R_IssueRenderCommands( qboolean runPerformanceCounters ) {
79 renderCommandList_t *cmdList;
80
81 cmdList = &backEndData->commands;
82 assert(cmdList);
83 // add an end-of-list command
84 *(int *)(cmdList->cmds + cmdList->used) = RC_END_OF_LIST;
85
86 // clear it out, in case this is a sync and not a buffer flip
87 cmdList->used = 0;
88
89 if ( runPerformanceCounters ) {
90 R_PerformanceCounters();
91 }
92
93 // actually start the commands going
94 if ( !r_skipBackEnd->integer ) {
95 // let it start on the new batch
96 RB_ExecuteRenderCommands( cmdList->cmds );
97 }
98 }
99
100
101 /*
102 ====================
103 R_IssuePendingRenderCommands
104
105 Issue any pending commands and wait for them to complete.
106 ====================
107 */
108 void R_IssuePendingRenderCommands( void ) {
109 if ( !tr.registered ) {
110 return;
111 }
112 R_IssueRenderCommands( qfalse );
113 }
114
115 /*
116 ============
117 R_GetCommandBufferReserved
118
119 make sure there is enough command space
120 ============
121 */
122 void *R_GetCommandBufferReserved( int bytes, int reservedBytes ) {
123 renderCommandList_t *cmdList;
124
125 cmdList = &backEndData->commands;
126 bytes = PAD(bytes, sizeof(void *));
127
128 // always leave room for the end of list command
129 if ( cmdList->used + bytes + sizeof( int ) + reservedBytes > MAX_RENDER_COMMANDS ) {
130 if ( bytes > MAX_RENDER_COMMANDS - sizeof( int ) ) {
131 ri.Error( ERR_FATAL, "R_GetCommandBuffer: bad size %i", bytes );
132 }
133 // if we run out of room, just start dropping commands
134 ri.Printf( PRINT_WARNING, "Failed to allocate render command of size %d\n", bytes );
135 return NULL;
136 }
137
138 cmdList->used += bytes;
139
140 return cmdList->cmds + cmdList->used - bytes;
141 }
142
143 /*
144 =============
145 R_GetCommandBuffer
146
147 returns NULL if there is not enough space for important commands
148 =============
149 */
150 void *R_GetCommandBuffer( int bytes ) {
151 return R_GetCommandBufferReserved( bytes, PAD( sizeof( swapBuffersCommand_t ), sizeof(void *) ) );
152 }
153
154
155 /*
156 =============
157 R_AddDrawSurfCmd
158
159 =============
160 */
161 void R_AddDrawSurfCmd( drawSurf_t *drawSurfs, int numDrawSurfs ) {
162 drawSurfsCommand_t *cmd;
163
164 cmd = R_GetCommandBuffer( sizeof( *cmd ) );
165 if ( !cmd ) {
166 return;
167 }
168 cmd->commandId = RC_DRAW_SURFS;
169
170 cmd->drawSurfs = drawSurfs;
171 cmd->numDrawSurfs = numDrawSurfs;
172
173 cmd->refdef = tr.refdef;
174 cmd->viewParms = tr.viewParms;
175 }
176
177
178 /*
179 =============
180 RE_SetColor
181
182 Passing NULL will set the color to white
183 =============
184 */
185 void RE_SetColor( const float *rgba ) {
186 setColorCommand_t *cmd;
187
188 if ( !tr.registered ) {
189 return;
190 }
191 cmd = R_GetCommandBuffer( sizeof( *cmd ) );
192 if ( !cmd ) {
193 return;
194 }
195 cmd->commandId = RC_SET_COLOR;
196 if ( !rgba ) {
197 static float white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
198
199 rgba = white;
200 }
201
202 cmd->color[0] = rgba[0];
203 cmd->color[1] = rgba[1];
204 cmd->color[2] = rgba[2];
205 cmd->color[3] = rgba[3];
206 }
207
208
209 /*
210 =============
211 RE_StretchPic
212 =============
213 */
214 void RE_StretchPic ( float x, float y, float w, float h,
215 float s1, float t1, float s2, float t2, qhandle_t hShader ) {
216 stretchPicCommand_t *cmd;
217
218 if (!tr.registered) {
219 return;
220 }
221 cmd = R_GetCommandBuffer( sizeof( *cmd ) );
222 if ( !cmd ) {
223 return;
224 }
225 cmd->commandId = RC_STRETCH_PIC;
226 cmd->shader = R_GetShaderByHandle( hShader );
227 cmd->x = x;
228 cmd->y = y;
229 cmd->w = w;
230 cmd->h = h;
231 cmd->s1 = s1;
232 cmd->t1 = t1;
233 cmd->s2 = s2;
234 cmd->t2 = t2;
235 }
236
237 #define MODE_RED_CYAN 1
238 #define MODE_RED_BLUE 2
239 #define MODE_RED_GREEN 3
240 #define MODE_GREEN_MAGENTA 4
241 #define MODE_MAX MODE_GREEN_MAGENTA
242
243 void R_SetColorMode(GLboolean *rgba, stereoFrame_t stereoFrame, int colormode)
244 {
245 rgba[0] = rgba[1] = rgba[2] = rgba[3] = GL_TRUE;
246
247 if(colormode > MODE_MAX)
248 {
249 if(stereoFrame == STEREO_LEFT)
250 stereoFrame = STEREO_RIGHT;
251 else if(stereoFrame == STEREO_RIGHT)
252 stereoFrame = STEREO_LEFT;
253
254 colormode -= MODE_MAX;
255 }
256
257 if(colormode == MODE_GREEN_MAGENTA)
258 {
259 if(stereoFrame == STEREO_LEFT)
260 rgba[0] = rgba[2] = GL_FALSE;
261 else if(stereoFrame == STEREO_RIGHT)
262 rgba[1] = GL_FALSE;
263 }
264 else
265 {
266 if(stereoFrame == STEREO_LEFT)
267 rgba[1] = rgba[2] = GL_FALSE;
268 else if(stereoFrame == STEREO_RIGHT)
269 {
270 rgba[0] = GL_FALSE;
271
272 if(colormode == MODE_RED_BLUE)
273 rgba[1] = GL_FALSE;
274 else if(colormode == MODE_RED_GREEN)
275 rgba[2] = GL_FALSE;
276 }
277 }
278 }
279
280
281 /*
282 ====================
283 RE_BeginFrame
284
285 If running in stereo, RE_BeginFrame will be called twice
286 for each RE_EndFrame
287 ====================
288 */
289 void RE_BeginFrame( stereoFrame_t stereoFrame ) {
290 drawBufferCommand_t *cmd = NULL;
291 colorMaskCommand_t *colcmd = NULL;
292
293 if ( !tr.registered ) {
294 return;
295 }
296 glState.finishCalled = qfalse;
297
298 tr.frameCount++;
299 tr.frameSceneNum = 0;
300
301 //
302 // do overdraw measurement
303 //
304 if ( r_measureOverdraw->integer )
305 {
306 if ( glConfig.stencilBits < 4 )
307 {
308 ri.Printf( PRINT_ALL, "Warning: not enough stencil bits to measure overdraw: %d\n", glConfig.stencilBits );
309 ri.Cvar_Set( "r_measureOverdraw", "0" );
310 r_measureOverdraw->modified = qfalse;
311 }
312 else if ( r_shadows->integer == 2 )
313 {
314 ri.Printf( PRINT_ALL, "Warning: stencil shadows and overdraw measurement are mutually exclusive\n" );
315 ri.Cvar_Set( "r_measureOverdraw", "0" );
316 r_measureOverdraw->modified = qfalse;
317 }
318 else
319 {
320 R_IssuePendingRenderCommands();
321 qglEnable( GL_STENCIL_TEST );
322 qglStencilMask( ~0U );
323 qglClearStencil( 0U );
324 qglStencilFunc( GL_ALWAYS, 0U, ~0U );
325 qglStencilOp( GL_KEEP, GL_INCR, GL_INCR );
326 }
327 r_measureOverdraw->modified = qfalse;
328 }
329 else
330 {
331 // this is only reached if it was on and is now off
332 if ( r_measureOverdraw->modified ) {
333 R_IssuePendingRenderCommands();
334 qglDisable( GL_STENCIL_TEST );
335 }
336 r_measureOverdraw->modified = qfalse;
337 }
338
339 //
340 // texturemode stuff
341 //
342 if ( r_textureMode->modified ) {
343 R_IssuePendingRenderCommands();
344 GL_TextureMode( r_textureMode->string );
345 r_textureMode->modified = qfalse;
346 }
347
348 //
349 // gamma stuff
350 //
351 if ( r_gamma->modified ) {
352 r_gamma->modified = qfalse;
353
354 R_IssuePendingRenderCommands();
355 R_SetColorMappings();
356 }
357
358 // check for errors
359 if ( !r_ignoreGLErrors->integer )
360 {
361 int err;
362
363 R_IssuePendingRenderCommands();
364 if ((err = qglGetError()) != GL_NO_ERROR)
365 ri.Error(ERR_FATAL, "RE_BeginFrame() - glGetError() failed (0x%x)!", err);
366 }
367
368 if (glConfig.stereoEnabled) {
369 if( !(cmd = R_GetCommandBuffer(sizeof(*cmd))) )
370 return;
371
372 cmd->commandId = RC_DRAW_BUFFER;
373
374 if ( stereoFrame == STEREO_LEFT ) {
375 cmd->buffer = (int)GL_BACK_LEFT;
376 } else if ( stereoFrame == STEREO_RIGHT ) {
377 cmd->buffer = (int)GL_BACK_RIGHT;
378 } else {
379 ri.Error( ERR_FATAL, "RE_BeginFrame: Stereo is enabled, but stereoFrame was %i", stereoFrame );
380 }
381 }
382 else
383 {
384 if(r_anaglyphMode->integer)
385 {
386 if(r_anaglyphMode->modified)
387 {
388 // clear both, front and backbuffer.
389 qglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
390 qglClearColor(0.0f, 0.0f, 0.0f, 1.0f);
391
392 qglDrawBuffer(GL_FRONT);
393 qglClear(GL_COLOR_BUFFER_BIT);
394 qglDrawBuffer(GL_BACK);
395 qglClear(GL_COLOR_BUFFER_BIT);
396
397 r_anaglyphMode->modified = qfalse;
398 }
399
400 if(stereoFrame == STEREO_LEFT)
401 {
402 if( !(cmd = R_GetCommandBuffer(sizeof(*cmd))) )
403 return;
404
405 if( !(colcmd = R_GetCommandBuffer(sizeof(*colcmd))) )
406 return;
407 }
408 else if(stereoFrame == STEREO_RIGHT)
409 {
410 clearDepthCommand_t *cldcmd;
411
412 if( !(cldcmd = R_GetCommandBuffer(sizeof(*cldcmd))) )
413 return;
414
415 cldcmd->commandId = RC_CLEARDEPTH;
416
417 if( !(colcmd = R_GetCommandBuffer(sizeof(*colcmd))) )
418 return;
419 }
420 else
421 ri.Error( ERR_FATAL, "RE_BeginFrame: Stereo is enabled, but stereoFrame was %i", stereoFrame );
422
423 R_SetColorMode(colcmd->rgba, stereoFrame, r_anaglyphMode->integer);
424 colcmd->commandId = RC_COLORMASK;
425 }
426 else
427 {
428 if(stereoFrame != STEREO_CENTER)
429 ri.Error( ERR_FATAL, "RE_BeginFrame: Stereo is disabled, but stereoFrame was %i", stereoFrame );
430
431 if( !(cmd = R_GetCommandBuffer(sizeof(*cmd))) )
432 return;
433 }
434
435 if(cmd)
436 {
437 cmd->commandId = RC_DRAW_BUFFER;
438
439 if(r_anaglyphMode->modified)
440 {
441 qglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
442 r_anaglyphMode->modified = qfalse;
443 }
444
445 if (!Q_stricmp(r_drawBuffer->string, "GL_FRONT"))
446 cmd->buffer = (int)GL_FRONT;
447 else
448 cmd->buffer = (int)GL_BACK;
449 }
450 }
451
452 tr.refdef.stereoFrame = stereoFrame;
453 }
454
455
456 /*
457 =============
458 RE_EndFrame
459
460 Returns the number of msec spent in the back end
461 =============
462 */
463 void RE_EndFrame( int *frontEndMsec, int *backEndMsec ) {
464 swapBuffersCommand_t *cmd;
465
466 if ( !tr.registered ) {
467 return;
468 }
469 cmd = R_GetCommandBufferReserved( sizeof( *cmd ), 0 );
470 if ( !cmd ) {
471 return;
472 }
473 cmd->commandId = RC_SWAP_BUFFERS;
474
475 R_IssueRenderCommands( qtrue );
476
477 R_InitNextFrame();
478
479 if ( frontEndMsec ) {
480 *frontEndMsec = tr.frontEndMsec;
481 }
482 tr.frontEndMsec = 0;
483 if ( backEndMsec ) {
484 *backEndMsec = backEnd.pc.msec;
485 }
486 backEnd.pc.msec = 0;
487 }
488
489 /*
490 =============
491 RE_TakeVideoFrame
492 =============
493 */
494 void RE_TakeVideoFrame( int width, int height,
495 byte *captureBuffer, byte *encodeBuffer, qboolean motionJpeg )
496 {
497 videoFrameCommand_t *cmd;
498
499 if( !tr.registered ) {
500 return;
501 }
502
503 cmd = R_GetCommandBuffer( sizeof( *cmd ) );
504 if( !cmd ) {
505 return;
506 }
507
508 cmd->commandId = RC_VIDEOFRAME;
509
510 cmd->width = width;
511 cmd->height = height;
512 cmd->captureBuffer = captureBuffer;
513 cmd->encodeBuffer = encodeBuffer;
514 cmd->motionJpeg = motionJpeg;
515 }