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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*
===========================================================================
Copyright (C) 2011 Andrei Drexler, Richard Allen, James Canete
This file is part of Reaction source code.
Reaction 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.
Reaction 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 Reaction source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "tr_local.h"
void RB_ToneMap(FBO_t *hdrFbo, ivec4_t hdrBox, FBO_t *ldrFbo, ivec4_t ldrBox, int autoExposure)
{
ivec4_t srcBox, dstBox;
vec4_t color;
static int lastFrameCount = 0;
if (autoExposure)
{
if (lastFrameCount == 0 || tr.frameCount < lastFrameCount || tr.frameCount - lastFrameCount > 5)
{
// determine average log luminance
FBO_t *srcFbo, *dstFbo, *tmp;
int size = 256;
lastFrameCount = tr.frameCount;
VectorSet4(dstBox, 0, 0, size, size);
FBO_Blit(hdrFbo, hdrBox, NULL, tr.textureScratchFbo[0], dstBox, &tr.calclevels4xShader[0], NULL, 0);
srcFbo = tr.textureScratchFbo[0];
dstFbo = tr.textureScratchFbo[1];
// downscale to 1x1 texture
while (size > 1)
{
VectorSet4(srcBox, 0, 0, size, size);
//size >>= 2;
size >>= 1;
VectorSet4(dstBox, 0, 0, size, size);
if (size == 1)
dstFbo = tr.targetLevelsFbo;
//FBO_Blit(targetFbo, srcBox, NULL, tr.textureScratchFbo[nextScratch], dstBox, &tr.calclevels4xShader[1], NULL, 0);
FBO_FastBlit(srcFbo, srcBox, dstFbo, dstBox, GL_COLOR_BUFFER_BIT, GL_LINEAR);
tmp = srcFbo;
srcFbo = dstFbo;
dstFbo = tmp;
}
}
// blend with old log luminance for gradual change
VectorSet4(srcBox, 0, 0, 0, 0);
color[0] =
color[1] =
color[2] = 1.0f;
if (glRefConfig.textureFloat)
color[3] = 0.03f;
else
color[3] = 0.1f;
FBO_Blit(tr.targetLevelsFbo, srcBox, NULL, tr.calcLevelsFbo, NULL, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA);
}
// tonemap
color[0] =
color[1] =
color[2] = pow(2, r_cameraExposure->value); //exp2(r_cameraExposure->value);
color[3] = 1.0f;
if (autoExposure)
GL_BindToTMU(tr.calcLevelsImage, TB_LEVELSMAP);
else
GL_BindToTMU(tr.fixedLevelsImage, TB_LEVELSMAP);
FBO_Blit(hdrFbo, hdrBox, NULL, ldrFbo, ldrBox, &tr.tonemapShader, color, 0);
}
/*
=============
RB_BokehBlur
Blurs a part of one framebuffer to another.
Framebuffers can be identical.
=============
*/
void RB_BokehBlur(FBO_t *src, ivec4_t srcBox, FBO_t *dst, ivec4_t dstBox, float blur)
{
// ivec4_t srcBox, dstBox;
vec4_t color;
blur *= 10.0f;
if (blur < 0.004f)
return;
if (glRefConfig.framebufferObject)
{
// bokeh blur
if (blur > 0.0f)
{
ivec4_t quarterBox;
quarterBox[0] = 0;
quarterBox[1] = tr.quarterFbo[0]->height;
quarterBox[2] = tr.quarterFbo[0]->width;
quarterBox[3] = -tr.quarterFbo[0]->height;
// create a quarter texture
//FBO_Blit(NULL, NULL, NULL, tr.quarterFbo[0], NULL, NULL, NULL, 0);
FBO_FastBlit(src, srcBox, tr.quarterFbo[0], quarterBox, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
#ifndef HQ_BLUR
if (blur > 1.0f)
{
// create a 1/16th texture
//FBO_Blit(tr.quarterFbo[0], NULL, NULL, tr.textureScratchFbo[0], NULL, NULL, NULL, 0);
FBO_FastBlit(tr.quarterFbo[0], NULL, tr.textureScratchFbo[0], NULL, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
#endif
if (blur > 0.0f && blur <= 1.0f)
{
// Crossfade original with quarter texture
VectorSet4(color, 1, 1, 1, blur);
FBO_Blit(tr.quarterFbo[0], NULL, NULL, dst, dstBox, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA);
}
#ifndef HQ_BLUR
// ok blur, but can see some pixelization
else if (blur > 1.0f && blur <= 2.0f)
{
// crossfade quarter texture with 1/16th texture
FBO_Blit(tr.quarterFbo[0], NULL, NULL, dst, dstBox, NULL, NULL, 0);
VectorSet4(color, 1, 1, 1, blur - 1.0f);
FBO_Blit(tr.textureScratchFbo[0], NULL, NULL, dst, dstBox, NULL, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA);
}
else if (blur > 2.0f)
{
// blur 1/16th texture then replace
int i;
for (i = 0; i < 2; i++)
{
vec2_t blurTexScale;
float subblur;
subblur = ((blur - 2.0f) / 2.0f) / 3.0f * (float)(i + 1);
blurTexScale[0] =
blurTexScale[1] = subblur;
color[0] =
color[1] =
color[2] = 0.5f;
color[3] = 1.0f;
if (i != 0)
FBO_Blit(tr.textureScratchFbo[0], NULL, blurTexScale, tr.textureScratchFbo[1], NULL, &tr.bokehShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE);
else
FBO_Blit(tr.textureScratchFbo[0], NULL, blurTexScale, tr.textureScratchFbo[1], NULL, &tr.bokehShader, color, 0);
}
FBO_Blit(tr.textureScratchFbo[1], NULL, NULL, dst, dstBox, &tr.textureColorShader, NULL, 0);
}
#else // higher quality blur, but slower
else if (blur > 1.0f)
{
// blur quarter texture then replace
int i;
src = tr.quarterFbo[0];
dst = tr.quarterFbo[1];
VectorSet4(color, 0.5f, 0.5f, 0.5f, 1);
for (i = 0; i < 2; i++)
{
vec2_t blurTexScale;
float subblur;
subblur = (blur - 1.0f) / 2.0f * (float)(i + 1);
blurTexScale[0] =
blurTexScale[1] = subblur;
color[0] =
color[1] =
color[2] = 1.0f;
if (i != 0)
color[3] = 1.0f;
else
color[3] = 0.5f;
FBO_Blit(tr.quarterFbo[0], NULL, blurTexScale, tr.quarterFbo[1], NULL, &tr.bokehShader, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA);
}
FBO_Blit(tr.quarterFbo[1], NULL, NULL, dst, dstBox, &tr.textureColorShader, NULL, 0);
}
#endif
}
}
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)
{
ivec4_t srcBox, dstBox;
vec4_t color;
const float inc = 1.f / passes;
const float mul = powf(stretch, inc);
float scale;
{
vec2_t texScale;
texScale[0] =
texScale[1] = 1.0f;
alpha *= inc;
VectorSet4(color, alpha, alpha, alpha, 1.0f);
VectorSet4(srcBox, 0, 0, srcFbo->width, srcFbo->height);
VectorSet4(dstBox, x, y, w, h);
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, 0);
--passes;
scale = mul;
while (passes > 0)
{
float iscale = 1.f / scale;
float s0 = xcenter * (1.f - iscale);
float t0 = (1.0f - ycenter) * (1.f - iscale);
float s1 = iscale + s0;
float t1 = iscale + t0;
if (srcFbo)
{
srcBox[0] = s0 * srcFbo->width;
srcBox[1] = t0 * srcFbo->height;
srcBox[2] = (s1 - s0) * srcFbo->width;
srcBox[3] = (t1 - t0) * srcFbo->height;
}
else
{
srcBox[0] = s0 * glConfig.vidWidth;
srcBox[1] = t0 * glConfig.vidHeight;
srcBox[2] = (s1 - s0) * glConfig.vidWidth;
srcBox[3] = (t1 - t0) * glConfig.vidHeight;
}
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
scale *= mul;
--passes;
}
}
}
static qboolean RB_UpdateSunFlareVis(void)
{
GLuint sampleCount = 0;
if (!glRefConfig.occlusionQuery)
return qtrue;
tr.sunFlareQueryIndex ^= 1;
if (!tr.sunFlareQueryActive[tr.sunFlareQueryIndex])
return qtrue;
/* debug code */
if (0)
{
int iter;
for (iter=0 ; ; ++iter)
{
GLint available = 0;
qglGetQueryObjectivARB(tr.sunFlareQuery[tr.sunFlareQueryIndex], GL_QUERY_RESULT_AVAILABLE_ARB, &available);
if (available)
break;
}
ri.Printf(PRINT_DEVELOPER, "Waited %d iterations\n", iter);
}
qglGetQueryObjectuivARB(tr.sunFlareQuery[tr.sunFlareQueryIndex], GL_QUERY_RESULT_ARB, &sampleCount);
return sampleCount > 0;
}
void RB_SunRays(FBO_t *srcFbo, ivec4_t srcBox, FBO_t *dstFbo, ivec4_t dstBox)
{
vec4_t color;
float dot;
const float cutoff = 0.25f;
qboolean colorize = qtrue;
// float w, h, w2, h2;
mat4_t mvp;
vec4_t pos, hpos;
dot = DotProduct(tr.sunDirection, backEnd.viewParms.or.axis[0]);
if (dot < cutoff)
return;
if (!RB_UpdateSunFlareVis())
return;
// From RB_DrawSun()
{
float dist;
mat4_t trans, model, mvp;
Mat4Translation( backEnd.viewParms.or.origin, trans );
Mat4Multiply( backEnd.viewParms.world.modelMatrix, trans, model );
Mat4Multiply(backEnd.viewParms.projectionMatrix, model, mvp);
dist = backEnd.viewParms.zFar / 1.75; // div sqrt(3)
VectorScale( tr.sunDirection, dist, pos );
}
// project sun point
//Mat4Multiply(backEnd.viewParms.projectionMatrix, backEnd.viewParms.world.modelMatrix, mvp);
Mat4Transform(mvp, pos, hpos);
// transform to UV coords
hpos[3] = 0.5f / hpos[3];
pos[0] = 0.5f + hpos[0] * hpos[3];
pos[1] = 0.5f + hpos[1] * hpos[3];
// initialize quarter buffers
{
float mul = 1.f;
vec2_t texScale;
ivec4_t rayBox, quarterBox;
texScale[0] =
texScale[1] = 1.0f;
VectorSet4(color, mul, mul, mul, 1);
if (srcFbo)
{
rayBox[0] = srcBox[0] * tr.sunRaysFbo->width / srcFbo->width;
rayBox[1] = srcBox[1] * tr.sunRaysFbo->height / srcFbo->height;
rayBox[2] = srcBox[2] * tr.sunRaysFbo->width / srcFbo->width;
rayBox[3] = srcBox[3] * tr.sunRaysFbo->height / srcFbo->height;
}
else
{
rayBox[0] = srcBox[0] * tr.sunRaysFbo->width / glConfig.vidWidth;
rayBox[1] = srcBox[1] * tr.sunRaysFbo->height / glConfig.vidHeight;
rayBox[2] = srcBox[2] * tr.sunRaysFbo->width / glConfig.vidWidth;
rayBox[3] = srcBox[3] * tr.sunRaysFbo->height / glConfig.vidHeight;
}
quarterBox[0] = 0;
quarterBox[1] = tr.quarterFbo[0]->height;
quarterBox[2] = tr.quarterFbo[0]->width;
quarterBox[3] = -tr.quarterFbo[0]->height;
// first, downsample the framebuffer
if (colorize)
{
FBO_FastBlit(srcFbo, srcBox, tr.quarterFbo[0], quarterBox, GL_COLOR_BUFFER_BIT, GL_LINEAR);
FBO_Blit(tr.sunRaysFbo, rayBox, NULL, tr.quarterFbo[0], quarterBox, NULL, color, GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO);
}
else
{
FBO_FastBlit(tr.sunRaysFbo, rayBox, tr.quarterFbo[0], quarterBox, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
}
// radial blur passes, ping-ponging between the two quarter-size buffers
{
const float stretch_add = 2.f/3.f;
float stretch = 1.f + stretch_add;
int i;
for (i=0; i<2; ++i)
{
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);
stretch += stretch_add;
}
}
// add result back on top of the main buffer
{
float mul = 1.f;
vec2_t texScale;
texScale[0] =
texScale[1] = 1.0f;
VectorSet4(color, mul, mul, mul, 1);
FBO_Blit(tr.quarterFbo[0], NULL, texScale, dstFbo, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE);
}
}
static void RB_BlurAxis(FBO_t *srcFbo, FBO_t *dstFbo, float strength, qboolean horizontal)
{
float dx, dy;
float xmul, ymul;
float weights[3] = {
0.227027027f,
0.316216216f,
0.070270270f,
};
float offsets[3] = {
0.f,
1.3846153846f,
3.2307692308f,
};
xmul = horizontal;
ymul = 1.f - xmul;
xmul *= strength;
ymul *= strength;
{
ivec4_t srcBox, dstBox;
vec4_t color;
vec2_t texScale;
texScale[0] =
texScale[1] = 1.0f;
VectorSet4(color, weights[0], weights[0], weights[0], 1.0f);
VectorSet4(srcBox, 0, 0, srcFbo->width, srcFbo->height);
VectorSet4(dstBox, 0, 0, dstFbo->width, dstFbo->height);
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, 0 );
VectorSet4(color, weights[1], weights[1], weights[1], 1.0f);
dx = offsets[1] * xmul;
dy = offsets[1] * ymul;
VectorSet4(srcBox, dx, dy, srcFbo->width, srcFbo->height);
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
VectorSet4(srcBox, -dx, -dy, srcFbo->width, srcFbo->height);
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
VectorSet4(color, weights[2], weights[2], weights[2], 1.0f);
dx = offsets[2] * xmul;
dy = offsets[2] * ymul;
VectorSet4(srcBox, dx, dy, srcFbo->width, srcFbo->height);
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
VectorSet4(srcBox, -dx, -dy, srcFbo->width, srcFbo->height);
FBO_Blit(srcFbo, srcBox, texScale, dstFbo, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
}
}
static void RB_HBlur(FBO_t *srcFbo, FBO_t *dstFbo, float strength)
{
RB_BlurAxis(srcFbo, dstFbo, strength, qtrue);
}
static void RB_VBlur(FBO_t *srcFbo, FBO_t *dstFbo, float strength)
{
RB_BlurAxis(srcFbo, dstFbo, strength, qfalse);
}
void RB_GaussianBlur(float blur)
{
//float mul = 1.f;
float factor = Com_Clamp(0.f, 1.f, blur);
if (factor <= 0.f)
return;
{
ivec4_t srcBox, dstBox;
vec4_t color;
vec2_t texScale;
texScale[0] =
texScale[1] = 1.0f;
VectorSet4(color, 1, 1, 1, 1);
// first, downsample the framebuffer
FBO_FastBlit(NULL, NULL, tr.quarterFbo[0], NULL, GL_COLOR_BUFFER_BIT, GL_LINEAR);
FBO_FastBlit(tr.quarterFbo[0], NULL, tr.textureScratchFbo[0], NULL, GL_COLOR_BUFFER_BIT, GL_LINEAR);
// set the alpha channel
VectorSet4(srcBox, 0, 0, tr.whiteImage->width, tr.whiteImage->height);
VectorSet4(dstBox, 0, 0, tr.textureScratchFbo[0]->width, tr.textureScratchFbo[0]->height);
qglColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
FBO_BlitFromTexture(tr.whiteImage, srcBox, texScale, tr.textureScratchFbo[0], dstBox, &tr.textureColorShader, color, GLS_DEPTHTEST_DISABLE);
qglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// blur the tiny buffer horizontally and vertically
RB_HBlur(tr.textureScratchFbo[0], tr.textureScratchFbo[1], factor);
RB_VBlur(tr.textureScratchFbo[1], tr.textureScratchFbo[0], factor);
// finally, merge back to framebuffer
VectorSet4(srcBox, 0, 0, tr.textureScratchFbo[0]->width, tr.textureScratchFbo[0]->height);
VectorSet4(dstBox, 0, 0, glConfig.vidWidth, glConfig.vidHeight);
color[3] = factor;
FBO_Blit(tr.textureScratchFbo[0], srcBox, texScale, NULL, dstBox, &tr.textureColorShader, color, GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA);
}
}
|