forked from DaemonEngine/Daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtr_fbo.cpp
More file actions
397 lines (320 loc) · 10.3 KB
/
tr_fbo.cpp
File metadata and controls
397 lines (320 loc) · 10.3 KB
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
/*
===========================================================================
Copyright (C) 2006 Kirk Barnes
Copyright (C) 2006-2008 Robert Beckebans <trebor_7@users.sourceforge.net>
This file is part of Daemon source code.
Daemon source code 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.
Daemon source code 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 Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_fbo.c
#include "tr_local.h"
#include "GLUtils.h"
/*
=============
R_CheckFBO
=============
*/
bool R_CheckFBO( const FBO_t *fbo )
{
int code;
int id;
glGetIntegerv( GL_FRAMEBUFFER_BINDING, &id );
GL_fboShim.glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo->frameBuffer );
code = GL_fboShim.glCheckFramebufferStatus( GL_DRAW_FRAMEBUFFER );
if ( code == GL_FRAMEBUFFER_COMPLETE )
{
GL_fboShim.glBindFramebuffer( GL_DRAW_FRAMEBUFFER, id );
return true;
}
// an error occurred
switch ( code )
{
case GL_FRAMEBUFFER_UNSUPPORTED:
Log::Warn("R_CheckFBO: (%s) Unsupported framebuffer format", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
Log::Warn("R_CheckFBO: (%s) Framebuffer incomplete attachment", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
Log::Warn("R_CheckFBO: (%s) Framebuffer incomplete, missing attachment", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
Log::Warn("R_CheckFBO: (%s) Framebuffer incomplete, missing draw buffer", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
Log::Warn("R_CheckFBO: (%s) Framebuffer incomplete, missing read buffer", fbo->name );
break;
/* Errors specific to EXT_framebuffer_object. Some headers may also define
the names without _EXT suffix but that's for GLES so those names may only
be available when GLES is available. The GL/glext.h header for desktop
OpenGL uses _EXT suffixed names. */
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
Log::Warn("R_CheckFBO: (%s) Framebuffer incomplete, attached images must have same dimensions", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
Log::Warn("R_CheckFBO: (%s) Framebuffer incomplete, attached images must have same format", fbo->name );
break;
default:
Log::Warn("R_CheckFBO: (%s) unknown error 0x%X", fbo->name, code );
break;
}
GL_fboShim.glBindFramebuffer( GL_DRAW_FRAMEBUFFER, id );
return false;
}
/*
============
R_CreateFBO
============
*/
FBO_t *R_CreateFBO( const char *name, int width, int height )
{
FBO_t *fbo;
if ( strlen( name ) >= MAX_QPATH )
{
Sys::Drop( "R_CreateFBO: \"%s\" is too long", name );
}
if ( width <= 0 )
{
Sys::Drop( "R_CreateFBO: bad width %i", width );
}
if ( height <= 0 )
{
Sys::Drop( "R_CreateFBO: bad height %i", height );
}
if ( tr.numFBOs == MAX_FBOS )
{
Sys::Drop( "R_CreateFBO: MAX_FBOS hit" );
}
fbo = tr.fbos[ tr.numFBOs++ ] = (FBO_t*) ri.Hunk_Alloc( sizeof( *fbo ), ha_pref::h_low );
Q_strncpyz( fbo->name, name, sizeof( fbo->name ) );
fbo->width = width;
fbo->height = height;
GL_fboShim.glGenFramebuffers( 1, &fbo->frameBuffer );
return fbo;
}
/*
=================
R_AttachFBOTexture1D
=================
*/
void R_AttachFBOTexture1D( int texId, int index )
{
if ( index < 0 || index >= glConfig.maxColorAttachments )
{
Log::Warn("R_AttachFBOTexture1D: invalid attachment index %i", index );
return;
}
GL_fboShim.glFramebufferTexture1D( GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_1D, texId, 0 );
}
/*
=================
R_AttachFBOTexture2D
=================
*/
void R_AttachFBOTexture2D( int target, int texId, int index )
{
if ( target != GL_TEXTURE_2D && target != GL_TEXTURE_2D_MULTISAMPLE
&& ( target < GL_TEXTURE_CUBE_MAP_POSITIVE_X || target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ) )
{
Log::Warn("R_AttachFBOTexture2D: invalid target %i", target );
return;
}
if ( index < 0 || index >= glConfig.maxColorAttachments )
{
Log::Warn("R_AttachFBOTexture2D: invalid attachment index %i", index );
return;
}
GL_fboShim.glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, target, texId, 0 );
}
/*
=================
R_AttachFBOTexture3D
=================
*/
void R_AttachFBOTexture3D( int texId, int index, int zOffset )
{
if ( index < 0 || index >= glConfig.maxColorAttachments )
{
Log::Warn("R_AttachFBOTexture3D: invalid attachment index %i", index );
return;
}
GL_fboShim.glFramebufferTexture3D( GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_3D, texId, 0, zOffset );
}
/*
=================
R_AttachFBOTexturePackedDepthStencil
=================
*/
void R_AttachFBOTexturePackedDepthStencil( int texId )
{
GL_fboShim.glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texId, 0 );
GL_fboShim.glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texId, 0 );
}
void R_AttachFBOTexturePackedDepthStencilMSAA( int texId ) {
GL_fboShim.glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, texId, 0 );
GL_fboShim.glFramebufferTexture2D( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, texId, 0 );
}
/*
============
R_BindFBO
============
*/
void R_BindFBO( FBO_t *fbo )
{
R_BindFBO( GL_DRAW_FRAMEBUFFER, fbo );
}
void R_BindFBO( GLenum target, FBO_t *fbo )
{
GLuint handle = fbo == nullptr ? 0 : fbo->frameBuffer;
if ( target != GL_DRAW_FRAMEBUFFER )
{
GL_fboShim.glBindFramebuffer( target, handle );
}
else if ( glState.currentFBO != fbo )
{
GL_fboShim.glBindFramebuffer( target, handle );
glState.currentFBO = fbo;
}
}
/*
============
R_BindNullFBO
============
*/
void R_BindNullFBO()
{
R_BindFBO( nullptr );
}
/*
============
R_InitFBOs
============
*/
void R_InitFBOs()
{
int i;
int width, height;
Log::Debug("------- R_InitFBOs -------");
tr.numFBOs = 0;
GL_CheckErrors();
// make sure the render thread is stopped
R_SyncRenderThread();
width = windowConfig.vidWidth;
height = windowConfig.vidHeight;
tr.mainFBO[0] = R_CreateFBO( "_main[0]", width, height );
R_BindFBO( tr.mainFBO[0] );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.currentRenderImage[0]->texnum, 0 );
R_AttachFBOTexturePackedDepthStencil( tr.currentDepthImage->texnum );
R_CheckFBO( tr.mainFBO[0] );
tr.mainFBO[1] = R_CreateFBO( "_main[1]", width, height );
R_BindFBO( tr.mainFBO[1] );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.currentRenderImage[1]->texnum, 0 );
R_AttachFBOTexturePackedDepthStencil( tr.currentDepthImage->texnum );
R_CheckFBO( tr.mainFBO[1] );
if ( glConfig.usingReadonlyDepth )
{
tr.readonlyDepthFBO = R_CreateFBO( "_depthReadonly", width, height );
R_BindFBO( tr.readonlyDepthFBO );
R_AttachFBOTexturePackedDepthStencil( tr.depthSamplerImage->texnum );
glConfig.usingReadonlyDepth = R_CheckFBO( tr.readonlyDepthFBO );
}
if ( glConfig.MSAA ) {
tr.msaaFBO = R_CreateFBO( "msaa", width, height );
R_BindFBO( tr.msaaFBO );
R_AttachFBOTexture2D( GL_TEXTURE_2D_MULTISAMPLE, tr.currentRenderImageMSAA->texnum, 0 );
R_AttachFBOTexturePackedDepthStencilMSAA( tr.currentDepthImageMSAA->texnum );
R_CheckFBO( tr.msaaFBO );
}
if ( glConfig.realtimeLighting )
{
/* It's only required to create frame buffers only used by the
tiled dynamic lighting renderer when this feature is enabled. */
tr.depthtile1FBO = R_CreateFBO( "_depthtile1", tr.depthtile1RenderImage->width, tr.depthtile1RenderImage->height );
R_BindFBO( tr.depthtile1FBO );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.depthtile1RenderImage->texnum, 0 );
R_CheckFBO( tr.depthtile1FBO );
tr.depthtile2FBO = R_CreateFBO( "_depthtile2", tr.depthtile2RenderImage->width, tr.depthtile2RenderImage->height );
R_BindFBO( tr.depthtile2FBO );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.depthtile2RenderImage->texnum, 0 );
R_CheckFBO( tr.depthtile2FBO );
tr.lighttileFBO = R_CreateFBO( "_lighttile", tr.lighttileRenderImage->width, tr.lighttileRenderImage->height );
R_BindFBO( tr.lighttileFBO );
R_AttachFBOTexture3D( tr.lighttileRenderImage->texnum, 0, 0 );
R_CheckFBO( tr.lighttileFBO );
}
if ( r_liquidMapping->integer )
{
width = windowConfig.vidWidth;
height = windowConfig.vidHeight;
tr.portalRenderFBO = R_CreateFBO( "_portalRender", width, height );
R_BindFBO( tr.portalRenderFBO );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.portalRenderImage->texnum, 0 );
R_CheckFBO( tr.portalRenderFBO );
}
if ( glConfig.bloom )
{
width = windowConfig.vidWidth * 0.25f;
height = windowConfig.vidHeight * 0.25f;
tr.contrastRenderFBO = R_CreateFBO( "_contrastRender", width, height );
R_BindFBO( tr.contrastRenderFBO );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.contrastRenderFBOImage->texnum, 0 );
R_CheckFBO( tr.contrastRenderFBO );
for ( i = 0; i < 2; i++ )
{
tr.bloomRenderFBO[ i ] = R_CreateFBO( va( "_bloomRender%d", i ), width, height );
R_BindFBO( tr.bloomRenderFBO[ i ] );
R_AttachFBOTexture2D( GL_TEXTURE_2D, tr.bloomRenderFBOImage[ i ]->texnum, 0 );
R_CheckFBO( tr.bloomRenderFBO[ i ] );
}
}
GL_CheckErrors();
R_BindNullFBO();
}
/*
============
R_ShutdownFBOs
============
*/
void R_ShutdownFBOs()
{
FBO_t *fbo;
Log::Debug("------- R_ShutdownFBOs -------" );
R_BindNullFBO();
for ( int i = 0; i < tr.numFBOs; i++ )
{
fbo = tr.fbos[ i ];
if ( fbo->frameBuffer )
{
GL_fboShim.glDeleteFramebuffers( 1, &fbo->frameBuffer );
}
}
}
class ListFBOsCmd : public Cmd::StaticCmd
{
public:
ListFBOsCmd() : StaticCmd("listFBOs", Cmd::RENDERER, "list renderer's OpenGL framebuffer objects") {}
void Run( const Cmd::Args & ) const override
{
int i;
FBO_t *fbo;
Print(" size name" );
Print("----------------------------------------------------------" );
for ( i = 0; i < tr.numFBOs; i++ )
{
fbo = tr.fbos[ i ];
Print(" %4i: %4i %4i %s", i, fbo->width, fbo->height, fbo->name );
}
Print(" %i FBOs", tr.numFBOs );
}
};
static ListFBOsCmd listFBOsCmdRegistration;