| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "tr_local.h" |
| 24 |
|
| 25 |
void RB_ToneMap(FBO_t *hdrFbo, ivec4_t hdrBox, FBO_t *ldrFbo, ivec4_t ldrBox, int autoExposure) |
| 26 |
{ |
| 27 |
ivec4_t srcBox, dstBox; |
| 28 |
vec4_t color; |
| 29 |
static int lastFrameCount = 0; |
| 30 |
|
| 31 |
if (autoExposure) |
| 32 |
{ |
| 33 |
if (lastFrameCount == 0 || tr.frameCount < lastFrameCount || tr.frameCount - lastFrameCount > 5) |
| 34 |
{ |
| 35 |
|
| 36 |
FBO_t *srcFbo, *dstFbo, *tmp; |
| 37 |
int size = 256; |
| 38 |
|
| 39 |
lastFrameCount = tr.frameCount; |
| 40 |
|
| 41 |
VectorSet4(dstBox, 0, 0, size, size); |
| 42 |
|
| 43 |
FBO_Blit(hdrFbo, hdrBox, NULL, tr.textureScratchFbo[0], dstBox, &tr.calclevels4xShader[0], NULL, 0); |
| 44 |
|
| 45 |
srcFbo = tr.textureScratchFbo[0]; |
| 46 |
dstFbo = tr.textureScratchFbo[1]; |
| 47 |
|
| 48 |
|
| 49 |
while (size > 1) |
| 50 |
{ |
| 51 |
VectorSet4(srcBox, 0, 0, size, size); |
| 52 |
|
| 53 |
size >>= 1; |
| 54 |
VectorSet4(dstBox, 0, 0, size, size); |
| 55 |
|
| 56 |
if (size == 1) |
| 57 |
dstFbo = tr.targetLevelsFbo; |
| 58 |
|
| 59 |
|
| 60 |
FBO_FastBlit(srcFbo, srcBox, dstFbo, dstBox, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 61 |
|
| 62 |
tmp = srcFbo; |
| 63 |
srcFbo = dstFbo; |
| 64 |
dstFbo = tmp; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
VectorSet4(srcBox, 0, 0, 0, 0); |
| 70 |
|
| 71 |
color[0] = |
| 72 |
color[1] = |
| 73 |
color[2] = 1.0f; |
| 74 |
if (glRefConfig.textureFloat) |
| 75 |
color[3] = 0.03f; |
| 76 |
else |
| 77 |
color[3] = 0.1f; |
| 78 |
|
| 79 |
FBO_Blit(tr.targetLevelsFbo, srcBox, NULL, tr.calcLevelsFbo, NULL, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
color[0] = |
| 84 |
color[1] = |
| 85 |
color[2] = pow(2, r_cameraExposure->value - autoExposure); |
| 86 |
color[3] = 1.0f; |
| 87 |
|
| 88 |
if (autoExposure) |
| 89 |
GL_BindToTMU(tr.calcLevelsImage, TB_LEVELSMAP); |
| 90 |
else |
| 91 |
GL_BindToTMU(tr.fixedLevelsImage, TB_LEVELSMAP); |
| 92 |
|
| 93 |
FBO_Blit(hdrFbo, hdrBox, NULL, ldrFbo, ldrBox, &tr.tonemapShader, color, 0); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void RB_BokehBlur(FBO_t *src, ivec4_t srcBox, FBO_t *dst, ivec4_t dstBox, float blur) |
| 107 |
{ |
| 108 |
|
| 109 |
vec4_t color; |
| 110 |
|
| 111 |
blur *= 10.0f; |
| 112 |
|
| 113 |
if (blur < 0.004f) |
| 114 |
return; |
| 115 |
|
| 116 |
if (glRefConfig.framebufferObject) |
| 117 |
{ |
| 118 |
|
| 119 |
if (blur > 0.0f) |
| 120 |
{ |
| 121 |
ivec4_t quarterBox; |
| 122 |
|
| 123 |
quarterBox[0] = 0; |
| 124 |
quarterBox[1] = tr.quarterFbo[0]->height; |
| 125 |
quarterBox[2] = tr.quarterFbo[0]->width; |
| 126 |
quarterBox[3] = -tr.quarterFbo[0]->height; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
FBO_FastBlit(src, srcBox, tr.quarterFbo[0], quarterBox, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 131 |
} |
| 132 |
|
| 133 |
#ifndef HQ_BLUR |
| 134 |
if (blur > 1.0f) |
| 135 |
{ |
| 136 |
|
| 137 |
|
| 138 |
FBO_FastBlit(tr.quarterFbo[0], NULL, tr.textureScratchFbo[0], NULL, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 139 |
} |
| 140 |
#endif |
| 141 |
|
| 142 |
if (blur > 0.0f && blur <= 1.0f) |
| 143 |
{ |
| 144 |
|
| 145 |
VectorSet4(color, 1, 1, 1, blur); |
| 146 |
|
| 147 |
FBO_Blit(tr.quarterFbo[0], NULL, NULL, dst, dstBox, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA); |
| 148 |
} |
| 149 |
#ifndef HQ_BLUR |
| 150 |
|
| 151 |
else if (blur > 1.0f && blur <= 2.0f) |
| 152 |
{ |
| 153 |
|
| 154 |
FBO_Blit(tr.quarterFbo[0], NULL, NULL, dst, dstBox, NULL, NULL, 0); |
| 155 |
|
| 156 |
VectorSet4(color, 1, 1, 1, blur - 1.0f); |
| 157 |
|
| 158 |
FBO_Blit(tr.textureScratchFbo[0], NULL, NULL, dst, dstBox, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA); |
| 159 |
} |
| 160 |
else if (blur > 2.0f) |
| 161 |
{ |
| 162 |
|
| 163 |
int i; |
| 164 |
|
| 165 |
for (i = 0; i < 2; i++) |
| 166 |
{ |
| 167 |
vec2_t blurTexScale; |
| 168 |
float subblur; |
| 169 |
|
| 170 |
subblur = ((blur - 2.0f) / 2.0f) / 3.0f * (float)(i + 1); |
| 171 |
|
| 172 |
blurTexScale[0] = |
| 173 |
blurTexScale[1] = subblur; |
| 174 |
|
| 175 |
color[0] = |
| 176 |
color[1] = |
| 177 |
color[2] = 0.5f; |
| 178 |
color[3] = 1.0f; |
| 179 |
|
| 180 |
if (i != 0) |
| 181 |
FBO_Blit(tr.textureScratchFbo[0], NULL, blurTexScale, tr.textureScratchFbo[1], NULL, &tr.bokehShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE); |
| 182 |
else |
| 183 |
FBO_Blit(tr.textureScratchFbo[0], NULL, blurTexScale, tr.textureScratchFbo[1], NULL, &tr.bokehShader, color, 0); |
| 184 |
} |
| 185 |
|
| 186 |
FBO_Blit(tr.textureScratchFbo[1], NULL, NULL, dst, dstBox, NULL, NULL, 0); |
| 187 |
} |
| 188 |
#else |
| 189 |
else if (blur > 1.0f) |
| 190 |
{ |
| 191 |
|
| 192 |
int i; |
| 193 |
|
| 194 |
src = tr.quarterFbo[0]; |
| 195 |
dst = tr.quarterFbo[1]; |
| 196 |
|
| 197 |
VectorSet4(color, 0.5f, 0.5f, 0.5f, 1); |
| 198 |
|
| 199 |
for (i = 0; i < 2; i++) |
| 200 |
{ |
| 201 |
vec2_t blurTexScale; |
| 202 |
float subblur; |
| 203 |
|
| 204 |
subblur = (blur - 1.0f) / 2.0f * (float)(i + 1); |
| 205 |
|
| 206 |
blurTexScale[0] = |
| 207 |
blurTexScale[1] = subblur; |
| 208 |
|
| 209 |
color[0] = |
| 210 |
color[1] = |
| 211 |
color[2] = 1.0f; |
| 212 |
if (i != 0) |
| 213 |
color[3] = 1.0f; |
| 214 |
else |
| 215 |
color[3] = 0.5f; |
| 216 |
|
| 217 |
FBO_Blit(tr.quarterFbo[0], NULL, blurTexScale, tr.quarterFbo[1], NULL, &tr.bokehShader, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA); |
| 218 |
} |
| 219 |
|
| 220 |
FBO_Blit(tr.quarterFbo[1], NULL, NULL, dst, dstBox, NULL, NULL, 0); |
| 221 |
} |
| 222 |
#endif |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
static void RB_RadialBlur(FBO_t *srcFbo, FBO_t *dstFbo, int passes, float stretch, float x, float y, float w, float h, float xcenter, float ycenter, float alpha) |
| 228 |
{ |
| 229 |
ivec4_t srcBox, dstBox; |
| 230 |
int srcWidth, srcHeight; |
| 231 |
vec4_t color; |
| 232 |
const float inc = 1.f / passes; |
| 233 |
const float mul = powf(stretch, inc); |
| 234 |
float scale; |
| 235 |
|
| 236 |
alpha *= inc; |
| 237 |
VectorSet4(color, alpha, alpha, alpha, 1.0f); |
| 238 |
|
| 239 |
srcWidth = srcFbo ? srcFbo->width : glConfig.vidWidth; |
| 240 |
srcHeight = srcFbo ? srcFbo->height : glConfig.vidHeight; |
| 241 |
|
| 242 |
VectorSet4(srcBox, 0, 0, srcWidth, srcHeight); |
| 243 |
|
| 244 |
VectorSet4(dstBox, x, y, w, h); |
| 245 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, 0); |
| 246 |
|
| 247 |
--passes; |
| 248 |
scale = mul; |
| 249 |
while (passes > 0) |
| 250 |
{ |
| 251 |
float iscale = 1.f / scale; |
| 252 |
float s0 = xcenter * (1.f - iscale); |
| 253 |
float t0 = (1.0f - ycenter) * (1.f - iscale); |
| 254 |
|
| 255 |
srcBox[0] = s0 * srcWidth; |
| 256 |
srcBox[1] = t0 * srcHeight; |
| 257 |
srcBox[2] = iscale * srcWidth; |
| 258 |
srcBox[3] = iscale * srcHeight; |
| 259 |
|
| 260 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE ); |
| 261 |
|
| 262 |
scale *= mul; |
| 263 |
--passes; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
static qboolean RB_UpdateSunFlareVis(void) |
| 269 |
{ |
| 270 |
GLuint sampleCount = 0; |
| 271 |
if (!glRefConfig.occlusionQuery) |
| 272 |
return qtrue; |
| 273 |
|
| 274 |
tr.sunFlareQueryIndex ^= 1; |
| 275 |
if (!tr.sunFlareQueryActive[tr.sunFlareQueryIndex]) |
| 276 |
return qtrue; |
| 277 |
|
| 278 |
|
| 279 |
if (0) |
| 280 |
{ |
| 281 |
int iter; |
| 282 |
for (iter=0 ; ; ++iter) |
| 283 |
{ |
| 284 |
GLint available = 0; |
| 285 |
qglGetQueryObjectiv(tr.sunFlareQuery[tr.sunFlareQueryIndex], GL_QUERY_RESULT_AVAILABLE, &available); |
| 286 |
if (available) |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
ri.Printf(PRINT_DEVELOPER, "Waited %d iterations\n", iter); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
qglGetQueryObjectuiv(tr.sunFlareQuery[tr.sunFlareQueryIndex], GL_QUERY_RESULT, &sampleCount); |
| 296 |
return sampleCount > 0; |
| 297 |
} |
| 298 |
|
| 299 |
void RB_SunRays(FBO_t *srcFbo, ivec4_t srcBox, FBO_t *dstFbo, ivec4_t dstBox) |
| 300 |
{ |
| 301 |
vec4_t color; |
| 302 |
float dot; |
| 303 |
const float cutoff = 0.25f; |
| 304 |
qboolean colorize = qtrue; |
| 305 |
|
| 306 |
|
| 307 |
mat4_t mvp; |
| 308 |
vec4_t pos, hpos; |
| 309 |
|
| 310 |
dot = DotProduct(tr.sunDirection, backEnd.viewParms.or.axis[0]); |
| 311 |
if (dot < cutoff) |
| 312 |
return; |
| 313 |
|
| 314 |
if (!RB_UpdateSunFlareVis()) |
| 315 |
return; |
| 316 |
|
| 317 |
|
| 318 |
{ |
| 319 |
float dist; |
| 320 |
mat4_t trans, model; |
| 321 |
|
| 322 |
Mat4Translation( backEnd.viewParms.or.origin, trans ); |
| 323 |
Mat4Multiply( backEnd.viewParms.world.modelMatrix, trans, model ); |
| 324 |
Mat4Multiply(backEnd.viewParms.projectionMatrix, model, mvp); |
| 325 |
|
| 326 |
dist = backEnd.viewParms.zFar / 1.75; |
| 327 |
|
| 328 |
VectorScale( tr.sunDirection, dist, pos ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
Mat4Transform(mvp, pos, hpos); |
| 334 |
|
| 335 |
|
| 336 |
hpos[3] = 0.5f / hpos[3]; |
| 337 |
|
| 338 |
pos[0] = 0.5f + hpos[0] * hpos[3]; |
| 339 |
pos[1] = 0.5f + hpos[1] * hpos[3]; |
| 340 |
|
| 341 |
|
| 342 |
{ |
| 343 |
float mul = 1.f; |
| 344 |
ivec4_t rayBox, quarterBox; |
| 345 |
int srcWidth = srcFbo ? srcFbo->width : glConfig.vidWidth; |
| 346 |
int srcHeight = srcFbo ? srcFbo->height : glConfig.vidHeight; |
| 347 |
|
| 348 |
VectorSet4(color, mul, mul, mul, 1); |
| 349 |
|
| 350 |
rayBox[0] = srcBox[0] * tr.sunRaysFbo->width / srcWidth; |
| 351 |
rayBox[1] = srcBox[1] * tr.sunRaysFbo->height / srcHeight; |
| 352 |
rayBox[2] = srcBox[2] * tr.sunRaysFbo->width / srcWidth; |
| 353 |
rayBox[3] = srcBox[3] * tr.sunRaysFbo->height / srcHeight; |
| 354 |
|
| 355 |
quarterBox[0] = 0; |
| 356 |
quarterBox[1] = tr.quarterFbo[0]->height; |
| 357 |
quarterBox[2] = tr.quarterFbo[0]->width; |
| 358 |
quarterBox[3] = -tr.quarterFbo[0]->height; |
| 359 |
|
| 360 |
|
| 361 |
if (colorize) |
| 362 |
{ |
| 363 |
FBO_FastBlit(srcFbo, srcBox, tr.quarterFbo[0], quarterBox, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 364 |
FBO_Blit(tr.sunRaysFbo, rayBox, NULL, tr.quarterFbo[0], quarterBox, NULL, color, GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO); |
| 365 |
} |
| 366 |
else |
| 367 |
{ |
| 368 |
FBO_FastBlit(tr.sunRaysFbo, rayBox, tr.quarterFbo[0], quarterBox, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
{ |
| 374 |
const float stretch_add = 2.f/3.f; |
| 375 |
float stretch = 1.f + stretch_add; |
| 376 |
int i; |
| 377 |
for (i=0; i<2; ++i) |
| 378 |
{ |
| 379 |
RB_RadialBlur(tr.quarterFbo[i&1], tr.quarterFbo[(~i) & 1], 5, stretch, 0.f, 0.f, tr.quarterFbo[0]->width, tr.quarterFbo[0]->height, pos[0], pos[1], 1.125f); |
| 380 |
stretch += stretch_add; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
{ |
| 386 |
float mul = 1.f; |
| 387 |
|
| 388 |
VectorSet4(color, mul, mul, mul, 1); |
| 389 |
|
| 390 |
FBO_Blit(tr.quarterFbo[0], NULL, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static void RB_BlurAxis(FBO_t *srcFbo, FBO_t *dstFbo, float strength, qboolean horizontal) |
| 395 |
{ |
| 396 |
float dx, dy; |
| 397 |
float xmul, ymul; |
| 398 |
float weights[3] = { |
| 399 |
0.227027027f, |
| 400 |
0.316216216f, |
| 401 |
0.070270270f, |
| 402 |
}; |
| 403 |
float offsets[3] = { |
| 404 |
0.f, |
| 405 |
1.3846153846f, |
| 406 |
3.2307692308f, |
| 407 |
}; |
| 408 |
|
| 409 |
xmul = horizontal; |
| 410 |
ymul = 1.f - xmul; |
| 411 |
|
| 412 |
xmul *= strength; |
| 413 |
ymul *= strength; |
| 414 |
|
| 415 |
{ |
| 416 |
ivec4_t srcBox, dstBox; |
| 417 |
vec4_t color; |
| 418 |
|
| 419 |
VectorSet4(color, weights[0], weights[0], weights[0], 1.0f); |
| 420 |
VectorSet4(srcBox, 0, 0, srcFbo->width, srcFbo->height); |
| 421 |
VectorSet4(dstBox, 0, 0, dstFbo->width, dstFbo->height); |
| 422 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, 0); |
| 423 |
|
| 424 |
VectorSet4(color, weights[1], weights[1], weights[1], 1.0f); |
| 425 |
dx = offsets[1] * xmul; |
| 426 |
dy = offsets[1] * ymul; |
| 427 |
VectorSet4(srcBox, dx, dy, srcFbo->width, srcFbo->height); |
| 428 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE); |
| 429 |
VectorSet4(srcBox, -dx, -dy, srcFbo->width, srcFbo->height); |
| 430 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE); |
| 431 |
|
| 432 |
VectorSet4(color, weights[2], weights[2], weights[2], 1.0f); |
| 433 |
dx = offsets[2] * xmul; |
| 434 |
dy = offsets[2] * ymul; |
| 435 |
VectorSet4(srcBox, dx, dy, srcFbo->width, srcFbo->height); |
| 436 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE); |
| 437 |
VectorSet4(srcBox, -dx, -dy, srcFbo->width, srcFbo->height); |
| 438 |
FBO_Blit(srcFbo, srcBox, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
static void RB_HBlur(FBO_t *srcFbo, FBO_t *dstFbo, float strength) |
| 443 |
{ |
| 444 |
RB_BlurAxis(srcFbo, dstFbo, strength, qtrue); |
| 445 |
} |
| 446 |
|
| 447 |
static void RB_VBlur(FBO_t *srcFbo, FBO_t *dstFbo, float strength) |
| 448 |
{ |
| 449 |
RB_BlurAxis(srcFbo, dstFbo, strength, qfalse); |
| 450 |
} |
| 451 |
|
| 452 |
void RB_GaussianBlur(FBO_t *srcFbo, FBO_t *dstFbo, float blur) |
| 453 |
{ |
| 454 |
|
| 455 |
float factor = Com_Clamp(0.f, 1.f, blur); |
| 456 |
|
| 457 |
if (factor <= 0.f) |
| 458 |
return; |
| 459 |
|
| 460 |
{ |
| 461 |
ivec4_t srcBox, dstBox; |
| 462 |
vec4_t color; |
| 463 |
|
| 464 |
VectorSet4(color, 1, 1, 1, 1); |
| 465 |
|
| 466 |
|
| 467 |
FBO_FastBlit(srcFbo, NULL, tr.quarterFbo[0], NULL, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 468 |
FBO_FastBlit(tr.quarterFbo[0], NULL, tr.textureScratchFbo[0], NULL, GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 469 |
|
| 470 |
|
| 471 |
qglColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); |
| 472 |
FBO_BlitFromTexture(tr.whiteImage, NULL, NULL, tr.textureScratchFbo[0], NULL, NULL, color, GLS_DEPTHTEST_DISABLE); |
| 473 |
qglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 474 |
|
| 475 |
|
| 476 |
RB_HBlur(tr.textureScratchFbo[0], tr.textureScratchFbo[1], factor); |
| 477 |
RB_VBlur(tr.textureScratchFbo[1], tr.textureScratchFbo[0], factor); |
| 478 |
|
| 479 |
|
| 480 |
VectorSet4(srcBox, 0, 0, tr.textureScratchFbo[0]->width, tr.textureScratchFbo[0]->height); |
| 481 |
VectorSet4(dstBox, 0, 0, glConfig.vidWidth, glConfig.vidHeight); |
| 482 |
color[3] = factor; |
| 483 |
FBO_Blit(tr.textureScratchFbo[0], srcBox, NULL, dstFbo, dstBox, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA); |
| 484 |
} |
| 485 |
} |