Debian Codegraph @

code/renderergl2/tr_postprocess.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 2011 Andrei Drexler, Richard Allen, James Canete
4
5 This file is part of Reaction source code.
6
7 Reaction source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Reaction source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Reaction source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // determine average log luminance
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 // downscale to 1x1 texture
49 while (size > 1)
50 {
51 VectorSet4(srcBox, 0, 0, size, size);
52 //size >>= 2;
53 size >>= 1;
54 VectorSet4(dstBox, 0, 0, size, size);
55
56 if (size == 1)
57 dstFbo = tr.targetLevelsFbo;
58
59 //FBO_Blit(targetFbo, srcBox, NULL, tr.textureScratchFbo[nextScratch], dstBox, &tr.calclevels4xShader[1], NULL, 0);
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 // blend with old log luminance for gradual change
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 // tonemap
83 color[0] =
84 color[1] =
85 color[2] = pow(2, r_cameraExposure->value - autoExposure); //exp2(r_cameraExposure->value);
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 RB_BokehBlur
99
100
101 Blurs a part of one framebuffer to another.
102
103 Framebuffers can be identical.
104 =============
105 */
106 void RB_BokehBlur(FBO_t *src, ivec4_t srcBox, FBO_t *dst, ivec4_t dstBox, float blur)
107 {
108 // ivec4_t srcBox, dstBox;
109 vec4_t color;
110
111 blur *= 10.0f;
112
113 if (blur < 0.004f)
114 return;
115
116 if (glRefConfig.framebufferObject)
117 {
118 // bokeh blur
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 // create a quarter texture
129 //FBO_Blit(NULL, NULL, NULL, tr.quarterFbo[0], NULL, NULL, NULL, 0);
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 // create a 1/16th texture
137 //FBO_Blit(tr.quarterFbo[0], NULL, NULL, tr.textureScratchFbo[0], NULL, NULL, NULL, 0);
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 // Crossfade original with quarter texture
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 // ok blur, but can see some pixelization
151 else if (blur > 1.0f && blur <= 2.0f)
152 {
153 // crossfade quarter texture with 1/16th texture
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 // blur 1/16th texture then replace
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 // higher quality blur, but slower
189 else if (blur > 1.0f)
190 {
191 // blur quarter texture then replace
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 /* debug code */
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 // Note: On desktop OpenGL this is a sample count (glRefConfig.occlusionQueryTarget == GL_SAMPLES_PASSED)
294 // but on OpenGL ES this is a boolean (glRefConfig.occlusionQueryTarget == GL_ANY_SAMPLES_PASSED)
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 // float w, h, w2, h2;
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 // From RB_DrawSun()
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; // div sqrt(3)
327
328 VectorScale( tr.sunDirection, dist, pos );
329 }
330
331 // project sun point
332 //Mat4Multiply(backEnd.viewParms.projectionMatrix, backEnd.viewParms.world.modelMatrix, mvp);
333 Mat4Transform(mvp, pos, hpos);
334
335 // transform to UV coords
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 // initialize quarter buffers
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 // first, downsample the framebuffer
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 // radial blur passes, ping-ponging between the two quarter-size buffers
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 // add result back on top of the main buffer
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 //float mul = 1.f;
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 // first, downsample the framebuffer
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 // set the alpha channel
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 // blur the tiny buffer horizontally and vertically
476 RB_HBlur(tr.textureScratchFbo[0], tr.textureScratchFbo[1], factor);
477 RB_VBlur(tr.textureScratchFbo[1], tr.textureScratchFbo[0], factor);
478
479 // finally, merge back to framebuffer
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 }