-
Notifications
You must be signed in to change notification settings - Fork 17
/
helper_fbo.h
349 lines (334 loc) · 13.9 KB
/
helper_fbo.h
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
/*
* Copyright (c) 2016-2023, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __HELPERFBO__
#define __HELPERFBO__
#include <nvgl/extensions_gl.hpp>
#include <nvh/nvprint.hpp>
namespace fbo {
inline bool CheckStatus()
{
GLenum status;
status = (GLenum)glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE:
return true;
case GL_FRAMEBUFFER_UNSUPPORTED:
LOGE("Unsupported framebuffer format\n");
assert(!"Unsupported framebuffer format");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
LOGE("Framebuffer incomplete, missing attachment\n");
assert(!"Framebuffer incomplete, missing attachment");
break;
//case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
// PRINTF(("Framebuffer incomplete, attached images must have same dimensions\n"));
// assert(!"Framebuffer incomplete, attached images must have same dimensions");
// break;
//case GL_FRAMEBUFFER_INCOMPLETE_FORMATS:
// PRINTF(("Framebuffer incomplete, attached images must have same format\n"));
// assert(!"Framebuffer incomplete, attached images must have same format");
// break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
LOGE("Framebuffer incomplete, missing draw buffer\n");
assert(!"Framebuffer incomplete, missing draw buffer");
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
LOGE("Framebuffer incomplete, missing read buffer\n");
assert(!"Framebuffer incomplete, missing read buffer");
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
LOGE("Framebuffer incomplete attachment\n");
assert(!"Framebuffer incomplete attachment");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
LOGE("Framebuffer incomplete multisample\n");
assert(!"Framebuffer incomplete multisample");
break;
default:
LOGE("Error %x\n", status);
assert(!"unknown FBO Error");
break;
}
return false;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline GLuint create()
{
GLuint fb;
glGenFramebuffers(1, &fb);
return fb;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline void bind(GLuint framebuffer)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool attachTexture2DTarget(GLuint framebuffer, GLuint textureID, int colorAttachment, GLenum target = GL_TEXTURE_2D)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment, target, textureID, 0);
return true; //CheckStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool attachTexture2D(GLuint framebuffer, GLuint textureID, int colorAttachment, int samples)
{
return attachTexture2DTarget(framebuffer, textureID, colorAttachment, samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool detachColorTexture(GLuint framebuffer, int colorAttachment, int samples)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment,
samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D, 0, 0);
return true; //CheckStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
#ifdef USE_RENDERBUFFERS
inline bool attachRenderbuffer(GLuint framebuffer, GLuint rb, int colorAttachment)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment, GL_RENDERBUFFER, rb);
return true; //CheckStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool attachDSTRenderbuffer(GLuint framebuffer, GLuint dstrb)
{
bool bRes;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dstrb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, dstrb);
return true; //CheckStatus() ;
}
#endif
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool attachDSTTexture2DTarget(GLuint framebuffer, GLuint textureDepthID, GLenum target = GL_TEXTURE_2D)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, target, textureDepthID, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, target, textureDepthID, 0);
return true; //CheckStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool attachDSTTexture2D(GLuint framebuffer, GLuint textureDepthID, int msaaRaster)
{
return attachDSTTexture2DTarget(framebuffer, textureDepthID, (msaaRaster > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline bool detachDSTTexture(GLuint framebuffer, int msaaRaster)
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
GLenum target = (msaaRaster > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, target, 0, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, target, 0, 0);
return true; //CheckStatus();
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline void deleteFBO(GLuint fbo)
{
glDeleteFramebuffers(1, &fbo);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline void blitFBO(GLuint srcFBO, GLuint dstFBO, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLenum filtering)
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, srcFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dstFBO);
// GL_NEAREST is needed when Stencil/depth are involved
glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, filtering);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline void blitFBONearest(GLuint srcFBO, GLuint dstFBO, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
{
blitFBO(srcFBO, dstFBO, srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, GL_NEAREST);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline void blitFBOLinear(GLuint srcFBO, GLuint dstFBO, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1)
{
blitFBO(srcFBO, dstFBO, srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, GL_LINEAR);
}
} // namespace fbo
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
namespace texture {
inline GLuint create(int w, int h, int samples, int coverageSamples, GLenum intfmt, GLenum fmt, GLuint textureID = 0)
{
if(samples <= 1)
{
if(textureID == 0)
glCreateTextures(GL_TEXTURE_2D, 1, &textureID);
glTextureStorage2D(textureID, 1, intfmt, w, h);
glTextureParameterf(textureID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameterf(textureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameterf(textureID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameterf(textureID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
else
{
if(textureID == 0)
glCreateTextures(GL_TEXTURE_2D_MULTISAMPLE, 1, &textureID);
// Note: fixed-samples set to GL_TRUE, otherwise it could fail when attaching to FBO having render-buffer !!
if(coverageSamples > 1)
{
glTextureImage2DMultisampleCoverageNV(textureID, GL_TEXTURE_2D_MULTISAMPLE, coverageSamples, samples, intfmt, w, h, GL_TRUE);
}
else
{
glTextureStorage2DMultisample(textureID, samples, intfmt, w, h, GL_TRUE);
}
}
return textureID;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline GLuint createRGBA8(int w, int h, int samples, int coverageSamples = 0, GLuint textureID = 0)
{
return create(w, h, samples, coverageSamples, GL_RGBA8, GL_RGBA, textureID);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline GLuint createDST(int w, int h, int samples, int coverageSamples = 0, GLuint textureID = 0)
{
return create(w, h, samples, coverageSamples, GL_DEPTH24_STENCIL8, GL_DEPTH24_STENCIL8, textureID);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline void deleteTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}
} // namespace texture
//------------------------------------------------------------------------------
// Render-buffers should be forgotten. Thing of the past
//------------------------------------------------------------------------------
#ifdef USE_RENDERBUFFERS
namespace renderbuffer {
inline GLuint createRenderBuffer(int w, int h, int samples, int coverageSamples, GLenum fmt)
{
int query;
GLuint rb;
glGenRenderbuffers(1, &rb);
glBindRenderbuffer(GL_RENDERBUFFER, rb);
if(coverageSamples)
{
glRenderbufferStorageMultisampleCoverageNV(GL_RENDERBUFFER, coverageSamples, samples, fmt, w, h);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_COVERAGE_SAMPLES_NV, &query);
if(query < coverageSamples)
rb = 0;
else if(query > coverageSamples)
{
// report back the actual number
coverageSamples = query;
LOGW("Warning: coverage samples is now %d\n", coverageSamples);
}
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_COLOR_SAMPLES_NV, &query);
if(query < samples)
rb = 0;
else if(query > samples)
{
// report back the actual number
samples = query;
LOGW("Warning: depth-samples is now %d\n", samples);
}
}
else
{
// create a regular MSAA color buffer
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, fmt, w, h);
// check the number of samples
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &query);
if(query < samples)
rb = 0;
else if(query > samples)
{
samples = query;
LOGW("Warning: depth-samples is now %d\n", samples);
}
}
glBindRenderbuffer(GL_RENDERBUFFER, 0);
return rb;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline GLuint createRenderBufferRGBA8(int w, int h, int samples, int coverageSamples)
{
return createRenderBuffer(w, h, samples, coverageSamples, GL_RGBA8);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline GLuint createRenderBufferD24S8(int w, int h, int samples, int coverageSamples)
{
return createRenderBuffer(w, h, samples, coverageSamples, GL_DEPTH24_STENCIL8);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
inline GLuint createRenderBufferS8(int w, int h, int samples, int coverageSamples)
{
return createRenderBuffer(w, h, samples, coverageSamples, GL_STENCIL_INDEX8);
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
#ifdef USE_RENDERBUFFERS
inline void deleteRenderBuffer(GLuint rb)
{
glDeleteRenderbuffers(1, &rb);
}
#endif
} // namespace renderbuffer
#endif
#endif //#ifndef __HELPERFBO__