| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "tr_local.h" |
| 25 |
|
| 26 |
#include "tr_dsa.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
qboolean R_CheckFBO(const FBO_t * fbo) |
| 34 |
{ |
| 35 |
GLenum code = qglCheckNamedFramebufferStatusEXT(fbo->frameBuffer, GL_FRAMEBUFFER); |
| 36 |
|
| 37 |
if(code == GL_FRAMEBUFFER_COMPLETE) |
| 38 |
return qtrue; |
| 39 |
|
| 40 |
|
| 41 |
switch (code) |
| 42 |
{ |
| 43 |
case GL_FRAMEBUFFER_UNSUPPORTED: |
| 44 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Unsupported framebuffer format\n", fbo->name); |
| 45 |
break; |
| 46 |
|
| 47 |
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 48 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete attachment\n", fbo->name); |
| 49 |
break; |
| 50 |
|
| 51 |
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 52 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete, missing attachment\n", fbo->name); |
| 53 |
break; |
| 54 |
|
| 55 |
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: |
| 56 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete, missing draw buffer\n", fbo->name); |
| 57 |
break; |
| 58 |
|
| 59 |
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: |
| 60 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete, missing read buffer\n", fbo->name); |
| 61 |
break; |
| 62 |
|
| 63 |
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: |
| 64 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete multisample\n", fbo->name); |
| 65 |
break; |
| 66 |
|
| 67 |
default: |
| 68 |
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) unknown error 0x%X\n", fbo->name, code); |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
return qfalse; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
FBO_t *FBO_Create(const char *name, int width, int height) |
| 81 |
{ |
| 82 |
FBO_t *fbo; |
| 83 |
|
| 84 |
if(strlen(name) >= MAX_QPATH) |
| 85 |
{ |
| 86 |
ri.Error(ERR_DROP, "FBO_Create: \"%s\" is too long", name); |
| 87 |
} |
| 88 |
|
| 89 |
if(width <= 0 || width > glRefConfig.maxRenderbufferSize) |
| 90 |
{ |
| 91 |
ri.Error(ERR_DROP, "FBO_Create: bad width %i", width); |
| 92 |
} |
| 93 |
|
| 94 |
if(height <= 0 || height > glRefConfig.maxRenderbufferSize) |
| 95 |
{ |
| 96 |
ri.Error(ERR_DROP, "FBO_Create: bad height %i", height); |
| 97 |
} |
| 98 |
|
| 99 |
if(tr.numFBOs == MAX_FBOS) |
| 100 |
{ |
| 101 |
ri.Error(ERR_DROP, "FBO_Create: MAX_FBOS hit"); |
| 102 |
} |
| 103 |
|
| 104 |
fbo = tr.fbos[tr.numFBOs] = ri.Hunk_Alloc(sizeof(*fbo), h_low); |
| 105 |
Q_strncpyz(fbo->name, name, sizeof(fbo->name)); |
| 106 |
fbo->index = tr.numFBOs++; |
| 107 |
fbo->width = width; |
| 108 |
fbo->height = height; |
| 109 |
|
| 110 |
qglGenFramebuffers(1, &fbo->frameBuffer); |
| 111 |
|
| 112 |
return fbo; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
void FBO_CreateBuffer(FBO_t *fbo, int format, int index, int multisample) |
| 121 |
{ |
| 122 |
uint32_t *pRenderBuffer; |
| 123 |
GLenum attachment; |
| 124 |
qboolean absent; |
| 125 |
|
| 126 |
switch(format) |
| 127 |
{ |
| 128 |
case GL_RGB: |
| 129 |
case GL_RGBA: |
| 130 |
case GL_RGB8: |
| 131 |
case GL_RGBA8: |
| 132 |
case GL_RGB16F_ARB: |
| 133 |
case GL_RGBA16F_ARB: |
| 134 |
case GL_RGB32F_ARB: |
| 135 |
case GL_RGBA32F_ARB: |
| 136 |
fbo->colorFormat = format; |
| 137 |
pRenderBuffer = &fbo->colorBuffers[index]; |
| 138 |
attachment = GL_COLOR_ATTACHMENT0 + index; |
| 139 |
break; |
| 140 |
|
| 141 |
case GL_DEPTH_COMPONENT: |
| 142 |
case GL_DEPTH_COMPONENT16_ARB: |
| 143 |
case GL_DEPTH_COMPONENT24_ARB: |
| 144 |
case GL_DEPTH_COMPONENT32_ARB: |
| 145 |
fbo->depthFormat = format; |
| 146 |
pRenderBuffer = &fbo->depthBuffer; |
| 147 |
attachment = GL_DEPTH_ATTACHMENT; |
| 148 |
break; |
| 149 |
|
| 150 |
case GL_STENCIL_INDEX: |
| 151 |
case GL_STENCIL_INDEX1: |
| 152 |
case GL_STENCIL_INDEX4: |
| 153 |
case GL_STENCIL_INDEX8: |
| 154 |
case GL_STENCIL_INDEX16: |
| 155 |
fbo->stencilFormat = format; |
| 156 |
pRenderBuffer = &fbo->stencilBuffer; |
| 157 |
attachment = GL_STENCIL_ATTACHMENT; |
| 158 |
break; |
| 159 |
|
| 160 |
case GL_DEPTH_STENCIL: |
| 161 |
case GL_DEPTH24_STENCIL8: |
| 162 |
fbo->packedDepthStencilFormat = format; |
| 163 |
pRenderBuffer = &fbo->packedDepthStencilBuffer; |
| 164 |
attachment = 0; |
| 165 |
break; |
| 166 |
|
| 167 |
default: |
| 168 |
ri.Printf(PRINT_WARNING, "FBO_CreateBuffer: invalid format %d\n", format); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
absent = *pRenderBuffer == 0; |
| 173 |
if (absent) { |
| 174 |
qglGenRenderbuffers(1, pRenderBuffer); |
| 175 |
|
| 176 |
|
| 177 |
GL_BindRenderbuffer(*pRenderBuffer); |
| 178 |
} |
| 179 |
|
| 180 |
if (multisample && glRefConfig.framebufferMultisample) |
| 181 |
qglNamedRenderbufferStorageMultisampleEXT(*pRenderBuffer, multisample, format, fbo->width, fbo->height); |
| 182 |
else |
| 183 |
qglNamedRenderbufferStorageEXT(*pRenderBuffer, format, fbo->width, fbo->height); |
| 184 |
|
| 185 |
if(absent) |
| 186 |
{ |
| 187 |
if (attachment == 0) |
| 188 |
{ |
| 189 |
qglNamedFramebufferRenderbufferEXT(fbo->frameBuffer, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, *pRenderBuffer); |
| 190 |
qglNamedFramebufferRenderbufferEXT(fbo->frameBuffer, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *pRenderBuffer); |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
qglNamedFramebufferRenderbufferEXT(fbo->frameBuffer, attachment, GL_RENDERBUFFER, *pRenderBuffer); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void FBO_AttachImage(FBO_t *fbo, image_t *image, GLenum attachment, GLuint cubemapside) |
| 206 |
{ |
| 207 |
GLenum target = GL_TEXTURE_2D; |
| 208 |
int index; |
| 209 |
|
| 210 |
if (image->flags & IMGFLAG_CUBEMAP) |
| 211 |
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + cubemapside; |
| 212 |
|
| 213 |
qglNamedFramebufferTexture2DEXT(fbo->frameBuffer, attachment, target, image->texnum, 0); |
| 214 |
index = attachment - GL_COLOR_ATTACHMENT0; |
| 215 |
if (index >= 0 && index <= 15) |
| 216 |
fbo->colorImage[index] = image; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
void FBO_Bind(FBO_t * fbo) |
| 226 |
{ |
| 227 |
if (!glRefConfig.framebufferObject) |
| 228 |
{ |
| 229 |
ri.Printf(PRINT_WARNING, "FBO_Bind() called without framebuffers enabled!\n"); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if (glState.currentFBO == fbo) |
| 234 |
return; |
| 235 |
|
| 236 |
if (r_logFile->integer) |
| 237 |
{ |
| 238 |
|
| 239 |
GLimp_LogComment(va("--- FBO_Bind( %s ) ---\n", fbo ? fbo->name : "NULL")); |
| 240 |
} |
| 241 |
|
| 242 |
GL_BindFramebuffer(GL_FRAMEBUFFER, fbo ? fbo->frameBuffer : 0); |
| 243 |
glState.currentFBO = fbo; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
void FBO_Init(void) |
| 252 |
{ |
| 253 |
int i; |
| 254 |
int hdrFormat, multisample = 0; |
| 255 |
|
| 256 |
ri.Printf(PRINT_ALL, "------- FBO_Init -------\n"); |
| 257 |
|
| 258 |
if(!glRefConfig.framebufferObject) |
| 259 |
return; |
| 260 |
|
| 261 |
tr.numFBOs = 0; |
| 262 |
|
| 263 |
GL_CheckErrors(); |
| 264 |
|
| 265 |
R_IssuePendingRenderCommands(); |
| 266 |
|
| 267 |
hdrFormat = GL_RGBA8; |
| 268 |
if (r_hdr->integer && glRefConfig.textureFloat) |
| 269 |
hdrFormat = GL_RGBA16F_ARB; |
| 270 |
|
| 271 |
if (glRefConfig.framebufferMultisample) |
| 272 |
qglGetIntegerv(GL_MAX_SAMPLES, &multisample); |
| 273 |
|
| 274 |
if (r_ext_framebuffer_multisample->integer < multisample) |
| 275 |
multisample = r_ext_framebuffer_multisample->integer; |
| 276 |
|
| 277 |
if (multisample < 2 || !glRefConfig.framebufferBlit) |
| 278 |
multisample = 0; |
| 279 |
|
| 280 |
if (multisample != r_ext_framebuffer_multisample->integer) |
| 281 |
ri.Cvar_SetValue("r_ext_framebuffer_multisample", (float)multisample); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if (multisample && glRefConfig.framebufferMultisample) |
| 286 |
{ |
| 287 |
tr.renderFbo = FBO_Create("_render", tr.renderDepthImage->width, tr.renderDepthImage->height); |
| 288 |
FBO_CreateBuffer(tr.renderFbo, hdrFormat, 0, multisample); |
| 289 |
FBO_CreateBuffer(tr.renderFbo, GL_DEPTH_COMPONENT24, 0, multisample); |
| 290 |
R_CheckFBO(tr.renderFbo); |
| 291 |
|
| 292 |
tr.msaaResolveFbo = FBO_Create("_msaaResolve", tr.renderDepthImage->width, tr.renderDepthImage->height); |
| 293 |
FBO_AttachImage(tr.msaaResolveFbo, tr.renderImage, GL_COLOR_ATTACHMENT0, 0); |
| 294 |
FBO_AttachImage(tr.msaaResolveFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0); |
| 295 |
R_CheckFBO(tr.msaaResolveFbo); |
| 296 |
} |
| 297 |
else if (r_hdr->integer) |
| 298 |
{ |
| 299 |
tr.renderFbo = FBO_Create("_render", tr.renderDepthImage->width, tr.renderDepthImage->height); |
| 300 |
FBO_AttachImage(tr.renderFbo, tr.renderImage, GL_COLOR_ATTACHMENT0, 0); |
| 301 |
FBO_AttachImage(tr.renderFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0); |
| 302 |
R_CheckFBO(tr.renderFbo); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if (tr.renderFbo) |
| 308 |
{ |
| 309 |
GL_BindFramebuffer(GL_FRAMEBUFFER, tr.renderFbo->frameBuffer); |
| 310 |
qglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 311 |
} |
| 312 |
|
| 313 |
if (tr.screenScratchImage) |
| 314 |
{ |
| 315 |
tr.screenScratchFbo = FBO_Create("screenScratch", tr.screenScratchImage->width, tr.screenScratchImage->height); |
| 316 |
FBO_AttachImage(tr.screenScratchFbo, tr.screenScratchImage, GL_COLOR_ATTACHMENT0, 0); |
| 317 |
FBO_AttachImage(tr.screenScratchFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0); |
| 318 |
R_CheckFBO(tr.screenScratchFbo); |
| 319 |
} |
| 320 |
|
| 321 |
if (tr.sunRaysImage) |
| 322 |
{ |
| 323 |
tr.sunRaysFbo = FBO_Create("_sunRays", tr.renderDepthImage->width, tr.renderDepthImage->height); |
| 324 |
FBO_AttachImage(tr.sunRaysFbo, tr.sunRaysImage, GL_COLOR_ATTACHMENT0, 0); |
| 325 |
FBO_AttachImage(tr.sunRaysFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0); |
| 326 |
R_CheckFBO(tr.sunRaysFbo); |
| 327 |
} |
| 328 |
|
| 329 |
if (MAX_DRAWN_PSHADOWS && tr.pshadowMaps[0]) |
| 330 |
{ |
| 331 |
for( i = 0; i < MAX_DRAWN_PSHADOWS; i++) |
| 332 |
{ |
| 333 |
tr.pshadowFbos[i] = FBO_Create(va("_shadowmap%d", i), tr.pshadowMaps[i]->width, tr.pshadowMaps[i]->height); |
| 334 |
|
| 335 |
FBO_CreateBuffer(tr.pshadowFbos[i], GL_RGBA8, 0, 0); |
| 336 |
FBO_AttachImage(tr.pshadowFbos[i], tr.pshadowMaps[i], GL_DEPTH_ATTACHMENT, 0); |
| 337 |
R_CheckFBO(tr.pshadowFbos[i]); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
if (tr.sunShadowDepthImage[0]) |
| 342 |
{ |
| 343 |
for (i = 0; i < 4; i++) |
| 344 |
{ |
| 345 |
tr.sunShadowFbo[i] = FBO_Create("_sunshadowmap", tr.sunShadowDepthImage[i]->width, tr.sunShadowDepthImage[i]->height); |
| 346 |
|
| 347 |
|
| 348 |
FBO_CreateBuffer(tr.sunShadowFbo[i], GL_RGBA8, 0, 0); |
| 349 |
FBO_AttachImage(tr.sunShadowFbo[i], tr.sunShadowDepthImage[i], GL_DEPTH_ATTACHMENT, 0); |
| 350 |
R_CheckFBO(tr.sunShadowFbo[i]); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if (tr.screenShadowImage) |
| 355 |
{ |
| 356 |
tr.screenShadowFbo = FBO_Create("_screenshadow", tr.screenShadowImage->width, tr.screenShadowImage->height); |
| 357 |
FBO_AttachImage(tr.screenShadowFbo, tr.screenShadowImage, GL_COLOR_ATTACHMENT0, 0); |
| 358 |
R_CheckFBO(tr.screenShadowFbo); |
| 359 |
} |
| 360 |
|
| 361 |
if (tr.textureScratchImage[0]) |
| 362 |
{ |
| 363 |
for (i = 0; i < 2; i++) |
| 364 |
{ |
| 365 |
tr.textureScratchFbo[i] = FBO_Create(va("_texturescratch%d", i), tr.textureScratchImage[i]->width, tr.textureScratchImage[i]->height); |
| 366 |
FBO_AttachImage(tr.textureScratchFbo[i], tr.textureScratchImage[i], GL_COLOR_ATTACHMENT0, 0); |
| 367 |
R_CheckFBO(tr.textureScratchFbo[i]); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
if (tr.calcLevelsImage) |
| 372 |
{ |
| 373 |
tr.calcLevelsFbo = FBO_Create("_calclevels", tr.calcLevelsImage->width, tr.calcLevelsImage->height); |
| 374 |
FBO_AttachImage(tr.calcLevelsFbo, tr.calcLevelsImage, GL_COLOR_ATTACHMENT0, 0); |
| 375 |
R_CheckFBO(tr.calcLevelsFbo); |
| 376 |
} |
| 377 |
|
| 378 |
if (tr.targetLevelsImage) |
| 379 |
{ |
| 380 |
tr.targetLevelsFbo = FBO_Create("_targetlevels", tr.targetLevelsImage->width, tr.targetLevelsImage->height); |
| 381 |
FBO_AttachImage(tr.targetLevelsFbo, tr.targetLevelsImage, GL_COLOR_ATTACHMENT0, 0); |
| 382 |
R_CheckFBO(tr.targetLevelsFbo); |
| 383 |
} |
| 384 |
|
| 385 |
if (tr.quarterImage[0]) |
| 386 |
{ |
| 387 |
for (i = 0; i < 2; i++) |
| 388 |
{ |
| 389 |
tr.quarterFbo[i] = FBO_Create(va("_quarter%d", i), tr.quarterImage[i]->width, tr.quarterImage[i]->height); |
| 390 |
FBO_AttachImage(tr.quarterFbo[i], tr.quarterImage[i], GL_COLOR_ATTACHMENT0, 0); |
| 391 |
R_CheckFBO(tr.quarterFbo[i]); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (tr.hdrDepthImage) |
| 396 |
{ |
| 397 |
tr.hdrDepthFbo = FBO_Create("_hdrDepth", tr.hdrDepthImage->width, tr.hdrDepthImage->height); |
| 398 |
FBO_AttachImage(tr.hdrDepthFbo, tr.hdrDepthImage, GL_COLOR_ATTACHMENT0, 0); |
| 399 |
R_CheckFBO(tr.hdrDepthFbo); |
| 400 |
} |
| 401 |
|
| 402 |
if (tr.screenSsaoImage) |
| 403 |
{ |
| 404 |
tr.screenSsaoFbo = FBO_Create("_screenssao", tr.screenSsaoImage->width, tr.screenSsaoImage->height); |
| 405 |
FBO_AttachImage(tr.screenSsaoFbo, tr.screenSsaoImage, GL_COLOR_ATTACHMENT0, 0); |
| 406 |
R_CheckFBO(tr.screenSsaoFbo); |
| 407 |
} |
| 408 |
|
| 409 |
if (tr.renderCubeImage) |
| 410 |
{ |
| 411 |
tr.renderCubeFbo = FBO_Create("_renderCubeFbo", tr.renderCubeImage->width, tr.renderCubeImage->height); |
| 412 |
FBO_AttachImage(tr.renderCubeFbo, tr.renderCubeImage, GL_COLOR_ATTACHMENT0, 0); |
| 413 |
FBO_CreateBuffer(tr.renderCubeFbo, GL_DEPTH_COMPONENT24_ARB, 0, 0); |
| 414 |
R_CheckFBO(tr.renderCubeFbo); |
| 415 |
} |
| 416 |
|
| 417 |
GL_CheckErrors(); |
| 418 |
|
| 419 |
GL_BindFramebuffer(GL_FRAMEBUFFER, 0); |
| 420 |
glState.currentFBO = NULL; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
void FBO_Shutdown(void) |
| 429 |
{ |
| 430 |
int i, j; |
| 431 |
FBO_t *fbo; |
| 432 |
|
| 433 |
ri.Printf(PRINT_ALL, "------- FBO_Shutdown -------\n"); |
| 434 |
|
| 435 |
if(!glRefConfig.framebufferObject) |
| 436 |
return; |
| 437 |
|
| 438 |
FBO_Bind(NULL); |
| 439 |
|
| 440 |
for(i = 0; i < tr.numFBOs; i++) |
| 441 |
{ |
| 442 |
fbo = tr.fbos[i]; |
| 443 |
|
| 444 |
for(j = 0; j < glRefConfig.maxColorAttachments; j++) |
| 445 |
{ |
| 446 |
if(fbo->colorBuffers[j]) |
| 447 |
qglDeleteRenderbuffers(1, &fbo->colorBuffers[j]); |
| 448 |
} |
| 449 |
|
| 450 |
if(fbo->depthBuffer) |
| 451 |
qglDeleteRenderbuffers(1, &fbo->depthBuffer); |
| 452 |
|
| 453 |
if(fbo->stencilBuffer) |
| 454 |
qglDeleteRenderbuffers(1, &fbo->stencilBuffer); |
| 455 |
|
| 456 |
if(fbo->frameBuffer) |
| 457 |
qglDeleteFramebuffers(1, &fbo->frameBuffer); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
void R_FBOList_f(void) |
| 467 |
{ |
| 468 |
int i; |
| 469 |
FBO_t *fbo; |
| 470 |
|
| 471 |
if(!glRefConfig.framebufferObject) |
| 472 |
{ |
| 473 |
ri.Printf(PRINT_ALL, "GL_EXT_framebuffer_object is not available.\n"); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
ri.Printf(PRINT_ALL, " size name\n"); |
| 478 |
ri.Printf(PRINT_ALL, "----------------------------------------------------------\n"); |
| 479 |
|
| 480 |
for(i = 0; i < tr.numFBOs; i++) |
| 481 |
{ |
| 482 |
fbo = tr.fbos[i]; |
| 483 |
|
| 484 |
ri.Printf(PRINT_ALL, " %4i: %4i %4i %s\n", i, fbo->width, fbo->height, fbo->name); |
| 485 |
} |
| 486 |
|
| 487 |
ri.Printf(PRINT_ALL, " %i FBOs\n", tr.numFBOs); |
| 488 |
} |
| 489 |
|
| 490 |
void FBO_BlitFromTexture(struct image_s *src, vec4_t inSrcTexCorners, vec2_t inSrcTexScale, FBO_t *dst, ivec4_t inDstBox, struct shaderProgram_s *shaderProgram, vec4_t inColor, int blend) |
| 491 |
{ |
| 492 |
ivec4_t dstBox; |
| 493 |
vec4_t color; |
| 494 |
vec4_t quadVerts[4]; |
| 495 |
vec2_t texCoords[4]; |
| 496 |
vec2_t invTexRes; |
| 497 |
FBO_t *oldFbo = glState.currentFBO; |
| 498 |
mat4_t projection; |
| 499 |
int width, height; |
| 500 |
|
| 501 |
if (!src) |
| 502 |
{ |
| 503 |
ri.Printf(PRINT_WARNING, "Tried to blit from a NULL texture!\n"); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
width = dst ? dst->width : glConfig.vidWidth; |
| 508 |
height = dst ? dst->height : glConfig.vidHeight; |
| 509 |
|
| 510 |
if (inSrcTexCorners) |
| 511 |
{ |
| 512 |
VectorSet2(texCoords[0], inSrcTexCorners[0], inSrcTexCorners[1]); |
| 513 |
VectorSet2(texCoords[1], inSrcTexCorners[2], inSrcTexCorners[1]); |
| 514 |
VectorSet2(texCoords[2], inSrcTexCorners[2], inSrcTexCorners[3]); |
| 515 |
VectorSet2(texCoords[3], inSrcTexCorners[0], inSrcTexCorners[3]); |
| 516 |
} |
| 517 |
else |
| 518 |
{ |
| 519 |
VectorSet2(texCoords[0], 0.0f, 1.0f); |
| 520 |
VectorSet2(texCoords[1], 1.0f, 1.0f); |
| 521 |
VectorSet2(texCoords[2], 1.0f, 0.0f); |
| 522 |
VectorSet2(texCoords[3], 0.0f, 0.0f); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
if (inDstBox) |
| 527 |
{ |
| 528 |
dstBox[0] = inDstBox[0]; |
| 529 |
dstBox[1] = height - inDstBox[1] - inDstBox[3]; |
| 530 |
dstBox[2] = inDstBox[0] + inDstBox[2]; |
| 531 |
dstBox[3] = height - inDstBox[1]; |
| 532 |
} |
| 533 |
else |
| 534 |
{ |
| 535 |
VectorSet4(dstBox, 0, height, width, 0); |
| 536 |
} |
| 537 |
|
| 538 |
if (inSrcTexScale) |
| 539 |
{ |
| 540 |
VectorCopy2(inSrcTexScale, invTexRes); |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
VectorSet2(invTexRes, 1.0f, 1.0f); |
| 545 |
} |
| 546 |
|
| 547 |
if (inColor) |
| 548 |
{ |
| 549 |
VectorCopy4(inColor, color); |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
VectorCopy4(colorWhite, color); |
| 554 |
} |
| 555 |
|
| 556 |
if (!shaderProgram) |
| 557 |
{ |
| 558 |
shaderProgram = &tr.textureColorShader; |
| 559 |
} |
| 560 |
|
| 561 |
FBO_Bind(dst); |
| 562 |
|
| 563 |
qglViewport( 0, 0, width, height ); |
| 564 |
qglScissor( 0, 0, width, height ); |
| 565 |
|
| 566 |
Mat4Ortho(0, width, height, 0, 0, 1, projection); |
| 567 |
|
| 568 |
GL_Cull( CT_TWO_SIDED ); |
| 569 |
|
| 570 |
GL_BindToTMU(src, TB_COLORMAP); |
| 571 |
|
| 572 |
VectorSet4(quadVerts[0], dstBox[0], dstBox[1], 0.0f, 1.0f); |
| 573 |
VectorSet4(quadVerts[1], dstBox[2], dstBox[1], 0.0f, 1.0f); |
| 574 |
VectorSet4(quadVerts[2], dstBox[2], dstBox[3], 0.0f, 1.0f); |
| 575 |
VectorSet4(quadVerts[3], dstBox[0], dstBox[3], 0.0f, 1.0f); |
| 576 |
|
| 577 |
invTexRes[0] /= src->width; |
| 578 |
invTexRes[1] /= src->height; |
| 579 |
|
| 580 |
GL_State( blend ); |
| 581 |
|
| 582 |
GLSL_BindProgram(shaderProgram); |
| 583 |
|
| 584 |
GLSL_SetUniformMat4(shaderProgram, UNIFORM_MODELVIEWPROJECTIONMATRIX, projection); |
| 585 |
GLSL_SetUniformVec4(shaderProgram, UNIFORM_COLOR, color); |
| 586 |
GLSL_SetUniformVec2(shaderProgram, UNIFORM_INVTEXRES, invTexRes); |
| 587 |
GLSL_SetUniformVec2(shaderProgram, UNIFORM_AUTOEXPOSUREMINMAX, tr.refdef.autoExposureMinMax); |
| 588 |
GLSL_SetUniformVec3(shaderProgram, UNIFORM_TONEMINAVGMAXLINEAR, tr.refdef.toneMinAvgMaxLinear); |
| 589 |
|
| 590 |
RB_InstantQuad2(quadVerts, texCoords); |
| 591 |
|
| 592 |
FBO_Bind(oldFbo); |
| 593 |
} |
| 594 |
|
| 595 |
void FBO_Blit(const FBO_t *src, ivec4_t inSrcBox, vec2_t srcTexScale, FBO_t *dst, ivec4_t dstBox, struct shaderProgram_s *shaderProgram, vec4_t color, int blend) |
| 596 |
{ |
| 597 |
vec4_t srcTexCorners; |
| 598 |
|
| 599 |
if (!src) |
| 600 |
{ |
| 601 |
ri.Printf(PRINT_WARNING, "Tried to blit from a NULL FBO!\n"); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
if (inSrcBox) |
| 606 |
{ |
| 607 |
srcTexCorners[0] = inSrcBox[0] / (float)src->width; |
| 608 |
srcTexCorners[1] = (inSrcBox[1] + inSrcBox[3]) / (float)src->height; |
| 609 |
srcTexCorners[2] = (inSrcBox[0] + inSrcBox[2]) / (float)src->width; |
| 610 |
srcTexCorners[3] = inSrcBox[1] / (float)src->height; |
| 611 |
} |
| 612 |
else |
| 613 |
{ |
| 614 |
VectorSet4(srcTexCorners, 0.0f, 0.0f, 1.0f, 1.0f); |
| 615 |
} |
| 616 |
|
| 617 |
FBO_BlitFromTexture(src->colorImage[0], srcTexCorners, srcTexScale, dst, dstBox, shaderProgram, color, blend | GLS_DEPTHTEST_DISABLE); |
| 618 |
} |
| 619 |
|
| 620 |
void FBO_FastBlit(const FBO_t *src, ivec4_t srcBox, FBO_t *dst, ivec4_t dstBox, int buffers, int filter) |
| 621 |
{ |
| 622 |
ivec4_t srcBoxFinal, dstBoxFinal; |
| 623 |
GLuint srcFb, dstFb; |
| 624 |
|
| 625 |
if (!glRefConfig.framebufferBlit) |
| 626 |
{ |
| 627 |
FBO_Blit(src, srcBox, NULL, dst, dstBox, NULL, NULL, 0); |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
srcFb = src ? src->frameBuffer : 0; |
| 632 |
dstFb = dst ? dst->frameBuffer : 0; |
| 633 |
|
| 634 |
if (!srcBox) |
| 635 |
{ |
| 636 |
int width = src ? src->width : glConfig.vidWidth; |
| 637 |
int height = src ? src->height : glConfig.vidHeight; |
| 638 |
|
| 639 |
VectorSet4(srcBoxFinal, 0, 0, width, height); |
| 640 |
} |
| 641 |
else |
| 642 |
{ |
| 643 |
VectorSet4(srcBoxFinal, srcBox[0], srcBox[1], srcBox[0] + srcBox[2], srcBox[1] + srcBox[3]); |
| 644 |
} |
| 645 |
|
| 646 |
if (!dstBox) |
| 647 |
{ |
| 648 |
int width = dst ? dst->width : glConfig.vidWidth; |
| 649 |
int height = dst ? dst->height : glConfig.vidHeight; |
| 650 |
|
| 651 |
qglScissor(0, 0, width, height); |
| 652 |
|
| 653 |
VectorSet4(dstBoxFinal, 0, 0, width, height); |
| 654 |
} |
| 655 |
else |
| 656 |
{ |
| 657 |
ivec4_t scissorBox; |
| 658 |
|
| 659 |
Vector4Copy(dstBox, scissorBox); |
| 660 |
|
| 661 |
if (scissorBox[2] < 0) |
| 662 |
{ |
| 663 |
scissorBox[0] += scissorBox[2]; |
| 664 |
scissorBox[2] = abs(scissorBox[2]); |
| 665 |
} |
| 666 |
|
| 667 |
if (scissorBox[3] < 0) |
| 668 |
{ |
| 669 |
scissorBox[1] += scissorBox[3]; |
| 670 |
scissorBox[3] = abs(scissorBox[3]); |
| 671 |
} |
| 672 |
|
| 673 |
qglScissor(scissorBox[0], scissorBox[1], scissorBox[2], scissorBox[3]); |
| 674 |
|
| 675 |
VectorSet4(dstBoxFinal, dstBox[0], dstBox[1], dstBox[0] + dstBox[2], dstBox[1] + dstBox[3]); |
| 676 |
} |
| 677 |
|
| 678 |
GL_BindFramebuffer(GL_READ_FRAMEBUFFER, srcFb); |
| 679 |
GL_BindFramebuffer(GL_DRAW_FRAMEBUFFER, dstFb); |
| 680 |
qglBlitFramebuffer(srcBoxFinal[0], srcBoxFinal[1], srcBoxFinal[2], srcBoxFinal[3], |
| 681 |
dstBoxFinal[0], dstBoxFinal[1], dstBoxFinal[2], dstBoxFinal[3], |
| 682 |
buffers, filter); |
| 683 |
|
| 684 |
GL_BindFramebuffer(GL_FRAMEBUFFER, 0); |
| 685 |
glState.currentFBO = NULL; |
| 686 |
} |