Line data Source code
1 : /* renderlights.cpp: render lights to deferred buffers
2 : *
3 : * light entities and sunlight in the game is rendered to deferred buffers
4 : * "g-buffers" which are used to compose a scene
5 : * lights are cached using a shadow map to allow rendering less than once per
6 : * frame, improving performance and light count allowed
7 : */
8 : #include "../libprimis-headers/cube.h"
9 : #include "../../shared/geomexts.h"
10 : #include "../../shared/glemu.h"
11 : #include "../../shared/glexts.h"
12 :
13 : #include "aa.h"
14 : #include "ao.h"
15 : #include "csm.h"
16 : #include "hdr.h"
17 : #include "lightsphere.h"
18 : #include "octarender.h"
19 : #include "postfx.h"
20 : #include "radiancehints.h"
21 : #include "rendergl.h"
22 : #include "renderlights.h"
23 : #include "rendermodel.h"
24 : #include "rendersky.h"
25 : #include "rendertimers.h"
26 : #include "renderva.h"
27 : #include "renderwindow.h"
28 : #include "shader.h"
29 : #include "shaderparam.h"
30 : #include "stain.h"
31 : #include "texture.h"
32 :
33 : #include "interface/control.h"
34 : #include "interface/console.h"
35 :
36 : #include "world/dynlight.h"
37 : #include "world/light.h"
38 : #include "world/material.h"
39 : #include "world/octaedit.h"
40 : #include "world/octaworld.h"
41 : #include "world/world.h"
42 :
43 : int vieww = -1,
44 : viewh = -1;
45 :
46 : int gw = -1,
47 : gh = -1;
48 :
49 : GBuffer gbuf;
50 :
51 : int hdrclear = 0;
52 :
53 : int spotlights = 0,
54 : volumetriclights = 0,
55 : nospeclights = 0;
56 : std::vector<vec2> msaapositions;
57 :
58 : //`g`-buffer `scale`
59 0 : static VARFP(gscale, 25, 100, 100, gbuf.cleanupgbuffer()); //size of g buffer, approximately correlates to g buffer linear dimensions
60 0 : static VARFP(gscalecubic, 0, 0, 1, gbuf.cleanupgbuffer()); //g-buffer scale cubic: use cubic interpolation for g buffer upscaling to screen output
61 0 : static VARFP(gscalenearest, 0, 0, 1, gbuf.cleanupgbuffer()); //g buffer nearest neighbor interpolation
62 :
63 : matrix4 worldmatrix, screenmatrix;
64 :
65 : static std::array<Shader *, 2> bilateralshader = { nullptr, nullptr };
66 :
67 0 : static Shader *loadbilateralshader(int pass)
68 : {
69 0 : if(!aobilateral)
70 : {
71 0 : return nullshader;
72 : }
73 0 : std::string opts;
74 0 : bool linear = aoreducedepth && (aoreduce || aoreducedepth > 1),
75 0 : upscale = aoreduce && aobilateralupscale,
76 0 : reduce = aoreduce && (upscale || (!linear && !aopackdepth));
77 0 : if(reduce)
78 : {
79 0 : opts.push_back('r');
80 0 : opts.push_back('0' + aoreduce);
81 : }
82 0 : if(upscale)
83 : {
84 0 : opts.push_back('u');
85 : }
86 0 : else if(linear)
87 : {
88 0 : opts.push_back('l');
89 :
90 : }
91 0 : if(aopackdepth)
92 : {
93 0 : opts.push_back('p');
94 : }
95 :
96 0 : DEF_FORMAT_STRING(name, "bilateral%c%s%d", 'x' + pass, opts.c_str(), aobilateral);
97 0 : return generateshader(name, "bilateralshader \"%s\" %d %d", opts.c_str(), aobilateral, reduce ? aoreduce : 0);
98 0 : }
99 :
100 0 : void loadbilateralshaders()
101 : {
102 0 : for(int k = 0; k < 2; ++k)
103 : {
104 0 : bilateralshader[k] = loadbilateralshader(k);
105 : }
106 0 : }
107 :
108 : /**
109 : * @brief clears bilateralarray
110 : *
111 : * Makes bilateralshader[2]'s elements point to the null pointer..
112 : */
113 0 : void clearbilateralshaders()
114 : {
115 0 : bilateralshader.fill(nullptr);
116 0 : }
117 :
118 0 : static void setbilateralparams(int radius, float depth)
119 : {
120 0 : float sigma = blursigma*2*radius;
121 0 : LOCALPARAMF(bilateralparams, 1.0f/(M_LN2*2*sigma*sigma), 1.0f/(M_LN2*depth*depth));
122 0 : }
123 :
124 : /**
125 : * @brief sets values for one of the bilateralshader[] elements
126 : *
127 : * bilateralshader[2] elements' referenced Shader objects have their radius
128 : * and depth parameters changed
129 : *
130 : * @param radius the bilateral filter radius to set
131 : * @param pass [0-1] the element of the bilateralshader() array to change
132 : * @param depth the depth of the bilateral filtering to set
133 : */
134 0 : void setbilateralshader(int radius, int pass, float depth)
135 : {
136 0 : bilateralshader[pass]->set();
137 0 : setbilateralparams(radius, depth);
138 0 : }
139 :
140 : //debug commands
141 : //for individual debug commands, see respective functions lower in the file
142 : VAR(debugfullscreen, 0, 0, 1); //used in header
143 :
144 0 : void GBuffer::cleanupscale()
145 : {
146 0 : for(GLuint &i : scalefbo)
147 : {
148 0 : if(i)
149 : {
150 0 : glDeleteFramebuffers(1, &i);
151 0 : i = 0;
152 : }
153 : }
154 0 : for(GLuint &i : scaletex)
155 : {
156 0 : if(i)
157 : {
158 0 : glDeleteTextures(1, &i);
159 0 : i = 0;
160 : }
161 : }
162 0 : scalew = scaleh = -1;
163 0 : }
164 :
165 0 : void GBuffer::setupscale(int sw, int sh, int w, int h)
166 : {
167 0 : scalew = w;
168 0 : scaleh = h;
169 :
170 0 : for(int i = 0; i < (gscalecubic ? 2 : 1); ++i)
171 : {
172 0 : if(!scaletex[i])
173 : {
174 0 : glGenTextures(1, &scaletex[i]);
175 : }
176 0 : if(!scalefbo[i])
177 : {
178 0 : glGenFramebuffers(1, &scalefbo[i]);
179 : }
180 0 : glBindFramebuffer(GL_FRAMEBUFFER, scalefbo[i]);
181 :
182 0 : createtexture(scaletex[i], sw, i ? h : sh, nullptr, 3, gscalecubic || !gscalenearest ? 1 : 0, GL_RGB, GL_TEXTURE_2D);
183 :
184 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, scaletex[i], 0);
185 0 : if(!i)
186 : {
187 0 : bindgdepth();
188 : }
189 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
190 : {
191 0 : fatal("failed allocating scale buffer!");
192 : }
193 : }
194 :
195 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
196 :
197 0 : if(gscalecubic)
198 : {
199 0 : useshaderbyname("scalecubicx");
200 0 : useshaderbyname("scalecubicy");
201 : }
202 0 : }
203 :
204 0 : GLuint GBuffer::shouldscale() const
205 : {
206 0 : return scalefbo[0];
207 : }
208 :
209 0 : void GBuffer::doscale(GLuint outfbo) const
210 : {
211 0 : if(!scaletex[0])
212 : {
213 0 : return;
214 : }
215 0 : timer *scaletimer = begintimer("scaling");
216 0 : if(gscalecubic)
217 : {
218 0 : glBindFramebuffer(GL_FRAMEBUFFER, scalefbo[1]);
219 0 : glViewport(0, 0, gw, hudh());
220 0 : glBindTexture(GL_TEXTURE_2D, scaletex[0]);
221 0 : SETSHADER(scalecubicy);
222 0 : screenquad(1, 1);
223 0 : glBindFramebuffer(GL_FRAMEBUFFER, outfbo);
224 0 : glViewport(0, 0, hudw(), hudh());
225 0 : glBindTexture(GL_TEXTURE_2D, scaletex[1]);
226 0 : SETSHADER(scalecubicx);
227 0 : screenquad(1, 1);
228 : }
229 : else
230 : {
231 0 : glBindFramebuffer(GL_FRAMEBUFFER, outfbo);
232 0 : glViewport(0, 0, hudw(), hudh());
233 0 : glBindTexture(GL_TEXTURE_2D, scaletex[0]);
234 0 : SETSHADER(scalelinear);
235 0 : screenquad(1, 1);
236 : }
237 :
238 0 : endtimer(scaletimer);
239 : }
240 :
241 0 : VARFP(glineardepth, 0, 0, 3, initwarning("g-buffer setup", Init_Load, Change_Shaders)); // g-buffer linear depth buffer
242 : VAR(gdepthformat, 1, 0, 0); // g-buffer depth buffer format
243 0 : VARF(gstencil, 0, 0, 1, initwarning("g-buffer setup", Init_Load, Change_Shaders)); // g-buffer stenciling
244 0 : VARF(gdepthstencil, 0, 2, 2, initwarning("g-buffer setup", Init_Load, Change_Shaders)); // g-buffer depth buffer stenciling
245 : VAR(ghasstencil, 1, 0, 0); // g buffer has stencil
246 0 : static VARFP(msaa, 0, 0, 16, initwarning("MSAA setup", Init_Load, Change_Shaders)); // multi-sample antialiasing
247 0 : static VARF(msaadepthstencil, 0, 2, 2, initwarning("MSAA setup", Init_Load, Change_Shaders)); // multi-sample antialiasing depth buffer stenciling
248 0 : static VARF(msaastencil, 0, 0, 1, initwarning("MSAA setup", Init_Load, Change_Shaders)); // multi-sample antialiasing stenciling
249 0 : VARF(msaaedgedetect, 0, 1, 1, gbuf.cleanupgbuffer()); // multi-sample antialiasing edge detection
250 0 : VARFP(msaalineardepth, -1, -1, 3, initwarning("MSAA setup", Init_Load, Change_Shaders)); // multi-sample antialiasing linear depth
251 0 : VARFP(msaatonemap, 0, 0, 1, gbuf.cleanupgbuffer()); // multi-sample antialiasing tone mapping
252 : static VAR(msaamaxsamples, 1, 0, 0); // multi-sample antialiasing maximum samples
253 : static VAR(msaamaxdepthtexsamples, 1, 0, 0); // multi-sample antialiasing maximum depth buffer texture sample count
254 : static VAR(msaamaxcolortexsamples, 1, 0, 0); // multi-sample antialiasing maximum color buffer texture sample count
255 : static VAR(msaaminsamples, 1, 0, 0); // multi-sample antialiasing minimum sample count
256 : VAR(msaasamples, 1, 0, 0); // multi-sample antialiasing sampling
257 : VAR(msaalight, 1, 0, 0); // multi-sample antialias lights
258 0 : static VARF(msaapreserve, -1, 0, 1, initwarning("MSAA setup", Init_Load, Change_Shaders)); // preserve multi-sample antialiasing
259 :
260 0 : static void checkmsaasamples()
261 : {
262 : GLuint tex;
263 0 : glGenTextures(1, &tex);
264 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, tex);
265 :
266 : GLint samples;
267 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaaminsamples, GL_RGBA8, 1, 1, GL_TRUE);
268 0 : glGetTexLevelParameteriv(GL_TEXTURE_2D_MULTISAMPLE, 0, GL_TEXTURE_SAMPLES, &samples);
269 0 : msaasamples = samples;
270 :
271 0 : glDeleteTextures(1, &tex);
272 0 : }
273 :
274 0 : void initgbuffer()
275 : {
276 0 : msaamaxsamples = msaamaxdepthtexsamples = msaamaxcolortexsamples = msaaminsamples = msaasamples = msaalight = 0;
277 0 : msaapositions.clear();
278 :
279 : GLint val;
280 0 : glGetIntegerv(GL_MAX_SAMPLES, &val);
281 0 : msaamaxsamples = val;
282 0 : glGetIntegerv(GL_MAX_DEPTH_TEXTURE_SAMPLES, &val);
283 0 : msaamaxdepthtexsamples = val;
284 0 : glGetIntegerv(GL_MAX_COLOR_TEXTURE_SAMPLES, &val);
285 0 : msaamaxcolortexsamples = val;
286 :
287 0 : int maxsamples = std::min(msaamaxsamples, msaamaxcolortexsamples),
288 0 : reqsamples = std::min(msaa, maxsamples);
289 0 : if(reqsamples >= 2)
290 : {
291 0 : msaaminsamples = 2;
292 0 : while(msaaminsamples*2 <= reqsamples)
293 : {
294 0 : msaaminsamples *= 2;
295 : }
296 : }
297 :
298 0 : int lineardepth = glineardepth;
299 0 : if(msaaminsamples)
300 : {
301 0 : if(msaamaxdepthtexsamples < msaaminsamples)
302 : {
303 0 : if(msaalineardepth > 0)
304 : {
305 0 : lineardepth = msaalineardepth;
306 : }
307 0 : else if(!lineardepth)
308 : {
309 0 : lineardepth = 1;
310 : }
311 : }
312 0 : else if(msaalineardepth >= 0)
313 : {
314 0 : lineardepth = msaalineardepth;
315 : }
316 : }
317 0 : gdepthformat = lineardepth;
318 0 : if(msaaminsamples)
319 : {
320 0 : ghasstencil = (msaadepthstencil > 1 || (msaadepthstencil && gdepthformat)) ? 2 : (msaastencil ? 1 : 0);
321 0 : checkmsaasamples();
322 0 : if(msaapreserve >= 0)
323 : {
324 0 : msaalight = 3;
325 : }
326 : }
327 : else
328 : {
329 0 : ghasstencil = (gdepthstencil > 1 || (gdepthstencil && gdepthformat)) ? 2 : (gstencil ? 1 : 0);
330 : }
331 0 : initao();
332 0 : }
333 :
334 0 : static VARF(forcepacknorm, 0, 0, 1, initwarning("g-buffer setup", Init_Load, Change_Shaders));
335 :
336 : static bool useavatarmask();
337 :
338 1 : bool usepacknorm()
339 : {
340 1 : return forcepacknorm || msaasamples || !useavatarmask();
341 : }
342 :
343 0 : void maskgbuffer(const char *mask)
344 : {
345 : GLenum drawbufs[4];
346 0 : int numbufs = 0;
347 0 : while(*mask)
348 : {
349 0 : switch(*mask++)
350 : {
351 0 : case 'c':
352 : {
353 0 : drawbufs[numbufs++] = GL_COLOR_ATTACHMENT0;
354 0 : break;
355 : }
356 0 : case 'n':
357 : {
358 0 : drawbufs[numbufs++] = GL_COLOR_ATTACHMENT1;
359 0 : break;
360 : }
361 0 : case 'd':
362 : {
363 0 : if(gdepthformat)
364 : {
365 0 : drawbufs[numbufs++] = GL_COLOR_ATTACHMENT3;
366 : }
367 0 : break;
368 : }
369 0 : case 'g':
370 : {
371 0 : drawbufs[numbufs++] = GL_COLOR_ATTACHMENT2;
372 0 : break;
373 : }
374 : }
375 : }
376 0 : glDrawBuffers(numbufs, drawbufs);
377 0 : }
378 :
379 0 : void GBuffer::cleanupmsbuffer()
380 : {
381 0 : if(msfbo) { glDeleteFramebuffers(1, &msfbo); msfbo = 0; }
382 0 : if(msdepthtex) { glDeleteTextures(1, &msdepthtex); msdepthtex = 0; }
383 0 : if(mscolortex) { glDeleteTextures(1, &mscolortex); mscolortex = 0; }
384 0 : if(msnormaltex) { glDeleteTextures(1, &msnormaltex); msnormaltex = 0; }
385 0 : if(msglowtex) { glDeleteTextures(1, &msglowtex); msglowtex = 0; }
386 0 : if(msstencilrb) { glDeleteRenderbuffers(1, &msstencilrb); msstencilrb = 0; }
387 0 : if(msdepthrb) { glDeleteRenderbuffers(1, &msdepthrb); msdepthrb = 0; }
388 0 : if(mshdrfbo) { glDeleteFramebuffers(1, &mshdrfbo); mshdrfbo = 0; }
389 0 : if(mshdrtex) { glDeleteTextures(1, &mshdrtex); mshdrtex = 0; }
390 0 : if(msrefractfbo) { glDeleteFramebuffers(1, &msrefractfbo); msrefractfbo = 0; }
391 0 : if(msrefracttex) { glDeleteTextures(1, &msrefracttex); msrefracttex = 0; }
392 0 : }
393 :
394 0 : void GBuffer::bindmsdepth() const
395 : {
396 0 : if(gdepthformat)
397 : {
398 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, msdepthrb);
399 0 : if(ghasstencil > 1)
400 : {
401 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, msdepthrb);
402 : }
403 0 : else if(msaalight && ghasstencil)
404 : {
405 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, msstencilrb);
406 : }
407 : }
408 : else
409 : {
410 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, msdepthtex, 0);
411 0 : if(ghasstencil > 1)
412 : {
413 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, msdepthtex, 0);
414 : }
415 0 : else if(msaalight && ghasstencil)
416 : {
417 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, msstencilrb);
418 : }
419 : }
420 0 : }
421 :
422 0 : void GBuffer::setupmsbuffer(int w, int h)
423 : {
424 0 : if(!msfbo)
425 : {
426 0 : glGenFramebuffers(1, &msfbo);
427 : }
428 :
429 0 : glBindFramebuffer(GL_FRAMEBUFFER, msfbo);
430 :
431 0 : stencilformat = ghasstencil > 1 ? GL_DEPTH24_STENCIL8 : (ghasstencil ? GL_STENCIL_INDEX8 : 0);
432 :
433 0 : if(gdepthformat)
434 : {
435 0 : if(!msdepthrb)
436 : {
437 0 : glGenRenderbuffers(1, &msdepthrb);
438 : }
439 0 : glBindRenderbuffer(GL_RENDERBUFFER, msdepthrb);
440 0 : glRenderbufferStorageMultisample_(GL_RENDERBUFFER, msaasamples, ghasstencil > 1 ? stencilformat : GL_DEPTH_COMPONENT24, w, h);
441 0 : glBindRenderbuffer(GL_RENDERBUFFER, 0);
442 : }
443 0 : if(msaalight && ghasstencil == 1)
444 : {
445 0 : if(!msstencilrb)
446 : {
447 0 : glGenRenderbuffers(1, &msstencilrb);
448 : }
449 0 : glBindRenderbuffer(GL_RENDERBUFFER, msstencilrb);
450 0 : glRenderbufferStorageMultisample_(GL_RENDERBUFFER, msaasamples, GL_STENCIL_INDEX8, w, h);
451 0 : glBindRenderbuffer(GL_RENDERBUFFER, 0);
452 : }
453 :
454 0 : if(!msdepthtex)
455 : {
456 0 : glGenTextures(1, &msdepthtex);
457 : }
458 0 : if(!mscolortex)
459 : {
460 0 : glGenTextures(1, &mscolortex);
461 : }
462 0 : if(!msnormaltex)
463 : {
464 0 : glGenTextures(1, &msnormaltex);
465 : }
466 :
467 0 : maskgbuffer(msaalight ? "cndg" : "cnd");
468 :
469 : static const GLenum depthformats[] = { GL_RGBA8, GL_R16F, GL_R32F };
470 0 : GLenum depthformat = gdepthformat ? depthformats[gdepthformat-1] : (ghasstencil > 1 ? stencilformat : GL_DEPTH_COMPONENT24);
471 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
472 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, depthformat, w, h, GL_TRUE);
473 :
474 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mscolortex);
475 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, GL_RGBA8, w, h, GL_TRUE);
476 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msnormaltex);
477 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, GL_RGBA8, w, h, GL_TRUE);
478 0 : if(msaalight)
479 : {
480 0 : if(!msglowtex)
481 : {
482 0 : glGenTextures(1, &msglowtex);
483 : }
484 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msglowtex);
485 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, hdrformat, w, h, GL_TRUE);
486 : }
487 :
488 0 : bindmsdepth();
489 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, mscolortex, 0);
490 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D_MULTISAMPLE, msnormaltex, 0);
491 0 : if(msaalight)
492 : {
493 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D_MULTISAMPLE, msglowtex, 0);
494 : }
495 0 : if(gdepthformat)
496 : {
497 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D_MULTISAMPLE, msdepthtex, 0);
498 : }
499 :
500 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
501 : {
502 0 : if(msaalight)
503 : {
504 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msglowtex);
505 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, GL_RGBA8, w, h, GL_TRUE);
506 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D_MULTISAMPLE, msglowtex, 0);
507 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
508 : {
509 0 : fatal("failed allocating MSAA g-buffer!");
510 : }
511 : }
512 : else
513 : {
514 0 : fatal("failed allocating MSAA g-buffer!");
515 : }
516 : }
517 :
518 0 : glClearColor(0, 0, 0, 0);
519 0 : glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (ghasstencil ? GL_STENCIL_BUFFER_BIT : 0));
520 :
521 0 : msaapositions.clear();
522 0 : for(int i = 0; i < msaasamples; ++i)
523 : {
524 : GLfloat vals[2];
525 0 : glGetMultisamplefv(GL_SAMPLE_POSITION, i, vals);
526 0 : msaapositions.emplace_back(vec2(vals[0], vals[1]));
527 : }
528 :
529 0 : if(msaalight)
530 : {
531 0 : if(!mshdrtex)
532 : {
533 0 : glGenTextures(1, &mshdrtex);
534 : }
535 0 : if(!mshdrfbo)
536 : {
537 0 : glGenFramebuffers(1, &mshdrfbo);
538 : }
539 0 : glBindFramebuffer(GL_FRAMEBUFFER, mshdrfbo);
540 0 : bindmsdepth();
541 0 : hdrformat = 0;
542 0 : for(int prec = hdrprec; prec >= 0; prec--)
543 : {
544 0 : GLenum format = gethdrformat(prec);
545 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mshdrtex);
546 0 : glGetError();
547 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, format, w, h, GL_TRUE);
548 0 : if(glGetError() == GL_NO_ERROR)
549 : {
550 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, mshdrtex, 0);
551 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
552 : {
553 0 : hdrformat = format;
554 0 : break;
555 : }
556 : }
557 : }
558 :
559 0 : if(!hdrformat || glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
560 : {
561 0 : fatal("failed allocating MSAA HDR buffer!");
562 : }
563 0 : if(!msrefracttex)
564 : {
565 0 : glGenTextures(1, &msrefracttex);
566 : }
567 0 : if(!msrefractfbo)
568 : {
569 0 : glGenFramebuffers(1, &msrefractfbo);
570 : }
571 0 : glBindFramebuffer(GL_FRAMEBUFFER, msrefractfbo);
572 :
573 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msrefracttex);
574 0 : glTexImage2DMultisample_(GL_TEXTURE_2D_MULTISAMPLE, msaasamples, GL_RGB, w, h, GL_TRUE);
575 :
576 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, msrefracttex, 0);
577 0 : bindmsdepth();
578 :
579 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
580 : {
581 0 : fatal("failed allocating MSAA refraction buffer!");
582 : }
583 : }
584 :
585 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
586 :
587 0 : useshaderbyname("msaaedgedetect");
588 0 : useshaderbyname("msaaresolve");
589 0 : useshaderbyname("msaareducew");
590 0 : useshaderbyname("msaareduce");
591 0 : if(!msaalight)
592 : {
593 0 : useshaderbyname("msaaresolvedepth");
594 : }
595 0 : if(msaalight > 1 && msaatonemap)
596 : {
597 0 : useshaderbyname("msaatonemap");
598 0 : if(msaalight > 2)
599 : {
600 0 : useshaderbyname("msaatonemapsample");
601 : }
602 : }
603 0 : }
604 :
605 0 : void GBuffer::bindgdepth() const
606 : {
607 0 : if(gdepthformat || msaalight)
608 : {
609 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gdepthrb);
610 0 : if(ghasstencil > 1)
611 : {
612 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gdepthrb);
613 : }
614 0 : else if(!msaalight || ghasstencil)
615 : {
616 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gstencilrb);
617 : }
618 : }
619 : else
620 : {
621 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_RECTANGLE, gdepthtex, 0);
622 0 : if(ghasstencil > 1)
623 : {
624 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_RECTANGLE, gdepthtex, 0);
625 : }
626 0 : else if(ghasstencil)
627 : {
628 0 : glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gstencilrb);
629 : }
630 : }
631 0 : }
632 :
633 0 : void GBuffer::setupgbuffer()
634 : {
635 : //start with screen resolution
636 0 : int sw = renderw(),
637 0 : sh = renderh();
638 : //scale sw and sh if gscale (g-buffer scale) is not 100%
639 0 : if(gscale != 100)
640 : {
641 0 : sw = std::max((renderw()*gscale + 99)/100, 1);
642 0 : sh = std::max((renderh()*gscale + 99)/100, 1);
643 : }
644 :
645 0 : if(gw == sw && gh == sh && ((sw >= hudw() && sh >= hudh() && !scalefbo[0]) || (scalew == hudw() && scaleh == hudh())))
646 : {
647 0 : return;
648 : }
649 : //clean up various buffers & info with them
650 0 : cleanupscale();
651 0 : cleanupbloom();
652 0 : cleanupao();
653 0 : cleanupvolumetric();
654 0 : cleanupaa();
655 0 : cleanuppostfx();
656 :
657 0 : gw = sw;
658 0 : gh = sh;
659 :
660 0 : hdrformat = gethdrformat(hdrprec);
661 0 : stencilformat = ghasstencil > 1 ? GL_DEPTH24_STENCIL8 : (ghasstencil ? GL_STENCIL_INDEX8 : 0);
662 :
663 0 : if(msaasamples)
664 : {
665 0 : setupmsbuffer(gw, gh);
666 : }
667 0 : hdrfloat = floatformat(hdrformat);
668 0 : hdrclear = 3;
669 0 : gdepthinit = false;
670 :
671 0 : if(gdepthformat || msaalight)
672 : {
673 0 : if(!gdepthrb)
674 : {
675 0 : glGenRenderbuffers(1, &gdepthrb);
676 : }
677 0 : glBindRenderbuffer(GL_RENDERBUFFER, gdepthrb);
678 0 : glRenderbufferStorage(GL_RENDERBUFFER, ghasstencil > 1 ? stencilformat : GL_DEPTH_COMPONENT24, gw, gh);
679 0 : glBindRenderbuffer(GL_RENDERBUFFER, 0);
680 : }
681 0 : if(!msaalight && ghasstencil == 1)
682 : {
683 0 : if(!gstencilrb)
684 : {
685 0 : glGenRenderbuffers(1, &gstencilrb);
686 : }
687 0 : glBindRenderbuffer(GL_RENDERBUFFER, gstencilrb);
688 0 : glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, gw, gh);
689 0 : glBindRenderbuffer(GL_RENDERBUFFER, 0);
690 : }
691 :
692 0 : if(!msaalight)
693 : {
694 0 : if(!gdepthtex)
695 : {
696 0 : glGenTextures(1, &gdepthtex);
697 : }
698 0 : if(!gcolortex)
699 : {
700 0 : glGenTextures(1, &gcolortex);
701 : }
702 0 : if(!gnormaltex)
703 : {
704 0 : glGenTextures(1, &gnormaltex);
705 : }
706 0 : if(!gglowtex)
707 : {
708 0 : glGenTextures(1, &gglowtex);
709 : }
710 0 : if(!gfbo)
711 : {
712 0 : glGenFramebuffers(1, &gfbo);
713 : }
714 :
715 0 : glBindFramebuffer(GL_FRAMEBUFFER, gfbo);
716 :
717 0 : maskgbuffer("cndg");
718 :
719 : static const GLenum depthformats[] = { GL_RGBA8, GL_R16F, GL_R32F };
720 0 : GLenum depthformat = gdepthformat ? depthformats[gdepthformat-1] : (ghasstencil > 1 ? stencilformat : GL_DEPTH_COMPONENT24);
721 0 : createtexture(gdepthtex, gw, gh, nullptr, 3, 0, depthformat, GL_TEXTURE_RECTANGLE);
722 :
723 0 : createtexture(gcolortex, gw, gh, nullptr, 3, 0, GL_RGBA8, GL_TEXTURE_RECTANGLE);
724 0 : createtexture(gnormaltex, gw, gh, nullptr, 3, 0, GL_RGBA8, GL_TEXTURE_RECTANGLE);
725 0 : createtexture(gglowtex, gw, gh, nullptr, 3, 0, hdrformat, GL_TEXTURE_RECTANGLE);
726 :
727 0 : bindgdepth();
728 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, gcolortex, 0);
729 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_RECTANGLE, gnormaltex, 0);
730 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_RECTANGLE, gglowtex, 0);
731 0 : if(gdepthformat)
732 : {
733 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_RECTANGLE, gdepthtex, 0);
734 : }
735 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
736 : {
737 0 : createtexture(gglowtex, gw, gh, nullptr, 3, 0, GL_RGBA8, GL_TEXTURE_RECTANGLE);
738 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_RECTANGLE, gglowtex, 0);
739 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
740 : {
741 0 : fatal("failed allocating g-buffer!");
742 : }
743 : }
744 :
745 0 : glClearColor(0, 0, 0, 0);
746 0 : glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (ghasstencil ? GL_STENCIL_BUFFER_BIT : 0));
747 : }
748 :
749 0 : if(!hdrtex)
750 : {
751 0 : glGenTextures(1, &hdrtex);
752 : }
753 0 : if(!hdrfbo)
754 : {
755 0 : glGenFramebuffers(1, &hdrfbo);
756 : }
757 :
758 0 : glBindFramebuffer(GL_FRAMEBUFFER, hdrfbo);
759 :
760 0 : createtexture(hdrtex, gw, gh, nullptr, 3, 1, hdrformat, GL_TEXTURE_RECTANGLE);
761 :
762 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, hdrtex, 0);
763 0 : bindgdepth();
764 :
765 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
766 : {
767 0 : fatal("failed allocating HDR buffer!");
768 : }
769 :
770 0 : if(!msaalight || (msaalight > 2 && msaatonemap && msaatonemapblit))
771 : {
772 0 : if(!refracttex)
773 : {
774 0 : glGenTextures(1, &refracttex);
775 : }
776 0 : if(!refractfbo)
777 : {
778 0 : glGenFramebuffers(1, &refractfbo);
779 : }
780 0 : glBindFramebuffer(GL_FRAMEBUFFER, refractfbo);
781 0 : createtexture(refracttex, gw, gh, nullptr, 3, 0, GL_RGB, GL_TEXTURE_RECTANGLE);
782 :
783 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, refracttex, 0);
784 0 : bindgdepth();
785 :
786 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
787 : {
788 0 : fatal("failed allocating refraction buffer!");
789 : }
790 : }
791 :
792 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
793 :
794 0 : if(gw < hudw() || gh < hudh())
795 : {
796 0 : setupscale(gw, gh, hudw(), hudh());
797 : }
798 : }
799 :
800 0 : void GBuffer::cleanupgbuffer()
801 : {
802 0 : if(gfbo) { glDeleteFramebuffers(1, &gfbo); gfbo = 0; }
803 0 : if(gdepthtex) { glDeleteTextures(1, &gdepthtex); gdepthtex = 0; }
804 0 : if(gcolortex) { glDeleteTextures(1, &gcolortex); gcolortex = 0; }
805 0 : if(gnormaltex) { glDeleteTextures(1, &gnormaltex); gnormaltex = 0; }
806 0 : if(gglowtex) { glDeleteTextures(1, &gglowtex); gglowtex = 0; }
807 0 : if(gstencilrb) { glDeleteRenderbuffers(1, &gstencilrb); gstencilrb = 0; }
808 0 : if(gdepthrb) { glDeleteRenderbuffers(1, &gdepthrb); gdepthrb = 0; }
809 0 : if(hdrfbo) { glDeleteFramebuffers(1, &hdrfbo); hdrfbo = 0; }
810 0 : if(hdrtex) { glDeleteTextures(1, &hdrtex); hdrtex = 0; }
811 0 : if(refractfbo) { glDeleteFramebuffers(1, &refractfbo); refractfbo = 0; }
812 0 : if(refracttex) { glDeleteTextures(1, &refracttex); refracttex = 0; }
813 0 : gw = gh = -1;
814 0 : cleanupscale();
815 0 : cleanupmsbuffer();
816 0 : cleardeferredlightshaders();
817 0 : }
818 :
819 0 : void GBuffer::resolvemsaadepth(int w, int h) const
820 : {
821 0 : if(!msaasamples || msaalight)
822 : {
823 0 : return;
824 : }
825 :
826 0 : timer *resolvetimer = drawtex ? nullptr : begintimer("msaa depth resolve");
827 :
828 0 : if(msaadepthblit)
829 : {
830 0 : glBindFramebuffer(GL_READ_FRAMEBUFFER, msfbo);
831 0 : glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gfbo);
832 0 : if(ghasstencil)
833 : {
834 0 : glClear(GL_STENCIL_BUFFER_BIT);
835 : }
836 0 : glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
837 : }
838 0 : if(!msaadepthblit || gdepthformat)
839 : {
840 0 : glBindFramebuffer(GL_FRAMEBUFFER, gfbo);
841 0 : glViewport(0, 0, w, h);
842 0 : maskgbuffer("d");
843 0 : if(!msaadepthblit)
844 : {
845 0 : if(ghasstencil)
846 : {
847 0 : glStencilFunc(GL_ALWAYS, 0, ~0);
848 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
849 0 : glEnable(GL_STENCIL_TEST);
850 : }
851 0 : glDepthFunc(GL_ALWAYS);
852 0 : SETSHADER(msaaresolvedepth);
853 : }
854 : else
855 : {
856 0 : glDisable(GL_DEPTH_TEST);
857 0 : SETSHADER(msaaresolve);
858 : }
859 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
860 0 : screenquad();
861 0 : maskgbuffer("cnd");
862 0 : if(!msaadepthblit)
863 : {
864 0 : if(ghasstencil)
865 : {
866 0 : glDisable(GL_STENCIL_TEST);
867 : }
868 0 : glDepthFunc(GL_LESS);
869 : }
870 : else
871 : {
872 0 : glEnable(GL_DEPTH_TEST);
873 : }
874 : }
875 :
876 0 : endtimer(resolvetimer);
877 : }
878 :
879 0 : void GBuffer::resolvemsaacolor(int w, int h)
880 : {
881 0 : if(!msaalight)
882 : {
883 0 : return;
884 : }
885 0 : timer *resolvetimer = drawtex ? nullptr : begintimer("msaa resolve");
886 :
887 0 : glBindFramebuffer(GL_READ_FRAMEBUFFER, mshdrfbo);
888 0 : glBindFramebuffer(GL_DRAW_FRAMEBUFFER, hdrfbo);
889 0 : glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
890 :
891 0 : glBindFramebuffer(GL_FRAMEBUFFER, hdrfbo);
892 :
893 0 : endtimer(resolvetimer);
894 : }
895 :
896 : float ldrscale = 1.0f;
897 :
898 0 : float ldrscaleb()
899 : {
900 0 : return ldrscale/255;
901 : }
902 :
903 : static VAR(debugdepth, 0, 0, 1); //toggles showing depth buffer onscreen
904 :
905 0 : void GBuffer::viewdepth() const
906 : {
907 0 : int w = (debugfullscreen) ? hudw() : std::min(hudw(), hudh())/2, //if debugfullscreen, set to hudw/hudh size; if not, do small size
908 0 : h = (debugfullscreen) ? hudh() : (w*hudh())/hudw();
909 0 : SETSHADER(hudrect);
910 0 : gle::colorf(1, 1, 1);
911 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
912 0 : debugquad(0, 0, w, h, 0, 0, gw, gh);
913 0 : }
914 :
915 : static VAR(debugstencil, 0, 0, 0xFF);
916 :
917 0 : void viewstencil()
918 : {
919 0 : if(!ghasstencil || !hdrfbo)
920 : {
921 0 : return;
922 : }
923 0 : glBindFramebuffer(GL_FRAMEBUFFER, hdrfbo);
924 0 : glViewport(0, 0, gw, gh);
925 :
926 0 : glClearColor(0, 0, 0, 0);
927 0 : glClear(GL_COLOR_BUFFER_BIT);
928 :
929 0 : glStencilFunc(GL_NOTEQUAL, 0, debugstencil);
930 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
931 0 : glEnable(GL_STENCIL_TEST);
932 0 : SETSHADER(hudnotexture);
933 0 : gle::colorf(1, 1, 1);
934 0 : debugquad(0, 0, hudw(), hudh(), 0, 0, gw, gh);
935 0 : glDisable(GL_STENCIL_TEST);
936 :
937 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
938 0 : glViewport(0, 0, hudw(), hudh());
939 :
940 0 : int w = debugfullscreen ? hudw() : std::min(hudw(), hudh())/2, //if debugfullscreen, set to hudw/hudh size; if not, do small size
941 0 : h = debugfullscreen ? hudh() : (w*hudh())/hudw();
942 0 : SETSHADER(hudrect);
943 0 : gle::colorf(1, 1, 1);
944 0 : glBindTexture(GL_TEXTURE_RECTANGLE, hdrtex);
945 0 : debugquad(0, 0, w, h, 0, 0, gw, gh);
946 : }
947 :
948 : static VAR(debugrefract, 0, 0, 1);
949 :
950 0 : void GBuffer::viewrefract()
951 : {
952 0 : int w = debugfullscreen ? hudw() : std::min(hudw(), hudh())/2, //if debugfullscreen, set to hudw/hudh size; if not, do small size
953 0 : h = debugfullscreen ? hudh() : (w*hudh())/hudw();
954 0 : SETSHADER(hudrect);
955 0 : gle::colorf(1, 1, 1);
956 0 : glBindTexture(GL_TEXTURE_RECTANGLE, refracttex);
957 0 : debugquad(0, 0, w, h, 0, 0, gw, gh);
958 0 : }
959 :
960 : PackNode shadowatlaspacker(0, 0, shadowatlassize, shadowatlassize);
961 :
962 : VAR(smminradius, 0, 16, 10000);
963 :
964 : class lightinfo final
965 : {
966 : public:
967 : int ent, shadowmap, flags;
968 : vec o, color;
969 : float radius, dist;
970 : vec dir, spotx, spoty;
971 : int spot;
972 : float sx1, sy1, sx2, sy2, sz1, sz2;
973 : occludequery *query;
974 :
975 : lightinfo() : query(nullptr)
976 : {
977 : }
978 :
979 0 : lightinfo(const vec &o, const vec &color, float radius, int flags = 0, const vec &dir = vec(0, 0, 0), int spot = 0)
980 0 : : ent(-1), shadowmap(-1), flags(flags),
981 0 : o(o), color(color), radius(radius), dist(camera1->o.dist(o)),
982 0 : dir(dir), spot(spot), query(nullptr)
983 : {
984 0 : if(spot > 0)
985 : {
986 0 : calcspot();
987 : }
988 0 : calcscissor();
989 0 : }
990 :
991 0 : lightinfo(int i, const extentity &e)
992 0 : : ent(i), shadowmap(-1), flags(e.attr5),
993 0 : o(e.o), color(vec(e.attr2, e.attr3, e.attr4).max(0)), radius(e.attr1), dist(camera1->o.dist(e.o)),
994 0 : dir(0, 0, 0), spot(0), query(nullptr)
995 : {
996 0 : if(e.attached && e.attached->type == EngineEnt_Spotlight)
997 : {
998 0 : dir = vec(e.attached->o).sub(e.o).normalize();
999 0 : spot = std::clamp(static_cast<int>(e.attached->attr1), 1, 89);
1000 0 : calcspot();
1001 : }
1002 0 : calcscissor();
1003 0 : }
1004 :
1005 0 : bool noshadow() const
1006 : {
1007 0 : return flags&LightEnt_NoShadow || radius <= smminradius;
1008 : }
1009 :
1010 0 : bool nospec() const
1011 : {
1012 0 : return (flags&LightEnt_NoSpecular) != 0;
1013 : }
1014 :
1015 0 : bool volumetric() const
1016 : {
1017 0 : return (flags&LightEnt_Volumetric) != 0;
1018 : }
1019 :
1020 0 : void addscissor(float &dx1, float &dy1, float &dx2, float &dy2) const
1021 : {
1022 0 : dx1 = std::min(dx1, sx1);
1023 0 : dy1 = std::min(dy1, sy1);
1024 0 : dx2 = std::max(dx2, sx2);
1025 0 : dy2 = std::max(dy2, sy2);
1026 0 : }
1027 :
1028 0 : void addscissor(float &dx1, float &dy1, float &dx2, float &dy2, float &dz1, float &dz2) const
1029 : {
1030 0 : addscissor(dx1, dy1, dx2, dy2);
1031 0 : dz1 = std::min(dz1, sz1);
1032 0 : dz2 = std::max(dz2, sz2);
1033 0 : }
1034 :
1035 0 : bool validscissor() const
1036 : {
1037 0 : return sx1 < sx2 && sy1 < sy2 && sz1 < sz2;
1038 : }
1039 :
1040 0 : bool checkquery() const
1041 : {
1042 0 : return query && query->owner == this && occlusionengine.checkquery(query);
1043 : }
1044 :
1045 0 : void calcbb(vec &bbmin, vec &bbmax) const
1046 : {
1047 0 : if(spot > 0)
1048 : {
1049 0 : float spotscale = radius * tan360(spot);
1050 0 : vec up = vec(spotx).mul(spotscale).abs(),
1051 0 : right = vec(spoty).mul(spotscale).abs(),
1052 0 : center = vec(dir).mul(radius).add(o);
1053 0 : bbmin = bbmax = center;
1054 0 : bbmin.sub(up).sub(right);
1055 0 : bbmax.add(up).add(right);
1056 0 : bbmin.min(o);
1057 0 : bbmax.max(o);
1058 : }
1059 : else
1060 : {
1061 0 : bbmin = vec(o).sub(radius);
1062 0 : bbmax = vec(o).add(radius);
1063 : }
1064 0 : }
1065 :
1066 : private:
1067 0 : void calcspot()
1068 : {
1069 0 : quat orient(dir, vec(0, 0, dir.z < 0 ? -1 : 1));
1070 0 : spotx = orient.invertedrotate(vec(1, 0, 0));
1071 0 : spoty = orient.invertedrotate(vec(0, 1, 0));
1072 0 : }
1073 :
1074 0 : void calcscissor()
1075 : {
1076 0 : sx1 = sy1 = sz1 = -1;
1077 0 : sx2 = sy2 = sz2 = 1;
1078 0 : if(spot > 0)
1079 : {
1080 0 : calcspotscissor(o, radius, dir, spot, spotx, spoty, sx1, sy1, sx2, sy2, sz1, sz2);
1081 : }
1082 : else
1083 : {
1084 0 : calcspherescissor(o, radius, sx1, sy1, sx2, sy2, sz1, sz2);
1085 : }
1086 0 : }
1087 : };
1088 :
1089 : struct ShadowCacheKey final
1090 : {
1091 : vec o;
1092 : float radius;
1093 : vec dir;
1094 : int spot;
1095 :
1096 0 : bool operator==(const ShadowCacheKey &y) const
1097 : {
1098 0 : return o == y.o && radius == y.radius && dir == y.dir && spot == y.spot;
1099 : }
1100 :
1101 : ShadowCacheKey() {}
1102 0 : ShadowCacheKey(const lightinfo &l) : o(l.o), radius(l.radius), dir(l.dir), spot(l.spot) {}
1103 : };
1104 :
1105 : template <>
1106 : struct std::hash<ShadowCacheKey>
1107 : {
1108 0 : size_t operator()(const ShadowCacheKey &k) const
1109 : {
1110 : auto vechash = std::hash<vec>();
1111 0 : return vechash(k.o);
1112 : }
1113 : };
1114 :
1115 : struct shadowcacheval final
1116 : {
1117 : ushort x, y, size, sidemask;
1118 :
1119 :
1120 : static inline bool htcmp(const ShadowCacheKey &x, const ShadowCacheKey &y)
1121 : {
1122 : return x.o == y.o && x.radius == y.radius && x.dir == y.dir && x.spot == y.spot;
1123 : }
1124 :
1125 0 : shadowcacheval() {}
1126 0 : shadowcacheval(const shadowmapinfo &sm) : x(sm.x), y(sm.y), size(sm.size), sidemask(sm.sidemask) {}
1127 : };
1128 :
1129 : class ShadowAtlas final
1130 : {
1131 : public:
1132 : GLuint fbo = 0;
1133 : std::unordered_map<ShadowCacheKey, shadowcacheval> cache;
1134 : bool full = false;
1135 :
1136 : void cleanup();
1137 : void view();
1138 : void setup();
1139 : void setcomparemode(); //will call one of setsm(non)comparemode()
1140 : void bind();
1141 :
1142 : private:
1143 : GLuint tex = 0;
1144 : GLenum target = GL_NONE;
1145 :
1146 : void setsmnoncomparemode();
1147 : void setsmcomparemode();
1148 : bool usesmcomparemode();
1149 :
1150 : };
1151 :
1152 0 : void ShadowAtlas::cleanup()
1153 : {
1154 0 : if(tex)
1155 : {
1156 0 : glDeleteTextures(1, &tex);
1157 0 : tex = 0;
1158 : }
1159 0 : if(fbo)
1160 : {
1161 0 : glDeleteFramebuffers(1, &fbo);
1162 0 : fbo = 0;
1163 : }
1164 0 : clearshadowcache();
1165 0 : }
1166 :
1167 0 : void ShadowAtlas::bind()
1168 : {
1169 0 : glBindTexture(target, tex);
1170 0 : }
1171 :
1172 : ShadowAtlas shadowatlas;
1173 :
1174 : //`s`hadow `m`ap vars
1175 : static FVAR(smpolyfactor, -1e3f, 1, 1e3f);
1176 : static FVAR(smpolyoffset, -1e3f, 0, 1e3f);
1177 : static FVAR(smbias, -1e6f, 0.01f, 1e6f);
1178 : static FVAR(smpolyfactor2, -1e3f, 1.5f, 1e3f);
1179 : static FVAR(smpolyoffset2, -1e3f, 0, 1e3f);
1180 : static FVAR(smbias2, -1e6f, 0.02f, 1e6f);
1181 : static FVAR(smprec, 1e-3f, 1, 1e3f);
1182 : static FVAR(smcubeprec, 1e-3f, 1, 1e3f);
1183 : static FVAR(smspotprec, 1e-3f, 1, 1e3f);
1184 :
1185 0 : VARFP(smsize, 10, 12, 14, shadowatlas.cleanup()); //size of shadow map: 2^size = x,y dimensions (1024x1024 at 10, 16384x16384 at 14)
1186 0 : VARFP(smdepthprec, 0, 0, 2, shadowatlas.cleanup()); //bit depth of sm depth map: 16bpp, 24bpp, or 32bpp respectively
1187 : VAR(smsidecull, 0, 1, 1); //`s`hadow `m`ap `side` `cull`: toggles culling lights outside the view frustum (outside the fov)
1188 : VAR(smviscull, 0, 1, 1); //`s`hadow `m`ap `vis`ibility `cull`ing: toggles visibility culling based of distance
1189 : VAR(smborder, 0, 3, 16); //`s`hadow `m`ap border
1190 : VAR(smborder2, 0, 4, 16); //smborder if smfilter > 2, for filter bleed reasons
1191 : VAR(smminsize, 1, 96, 1024); //min size for individual sm, not whole buffer
1192 : VAR(smmaxsize, 1, 384, 1024); //max size for individual sm, not whole buffer
1193 : //VAR(smmaxsize, 1, 4096, 4096);
1194 : VAR(smused, 1, 0, 0); //read only: shadow map area used
1195 : VAR(smquery, 0, 1, 1); // `s`hadow `m`ap `query1: whether to occlusion query lights
1196 0 : VARF(smcullside, 0, 1, 1, shadowatlas.cleanup());
1197 0 : VARF(smcache, 0, 1, 2, shadowatlas.cleanup());
1198 0 : VARFP(smfilter, 0, 2, 3, { cleardeferredlightshaders(); shadowatlas.cleanup(); cleanupvolumetric(); });
1199 0 : VARFP(smgather, 0, 0, 1, { cleardeferredlightshaders(); shadowatlas.cleanup(); cleanupvolumetric(); });
1200 : VAR(smnoshadow, 0, 0, 1);
1201 : VAR(smdynshadow, 0, 1, 1); //`s`hadow `m`ap `dyn`amic `shadow`
1202 :
1203 0 : void ShadowAtlas::setsmnoncomparemode() // use texture gather
1204 : {
1205 0 : glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
1206 0 : glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1207 0 : glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1208 0 : }
1209 :
1210 0 : void ShadowAtlas::setsmcomparemode() // use embedded shadow cmp
1211 : {
1212 0 : glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
1213 0 : glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1214 0 : glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1215 0 : }
1216 :
1217 0 : void ShadowAtlas::setcomparemode()
1218 : {
1219 0 : if(usesmcomparemode())
1220 : {
1221 0 : setsmcomparemode();
1222 : }
1223 : else
1224 : {
1225 0 : setsmnoncomparemode();
1226 : }
1227 0 : }
1228 :
1229 0 : static bool usegatherforsm()
1230 : {
1231 0 : return smfilter > 1 && smgather && usetexgather;
1232 : }
1233 :
1234 0 : bool ShadowAtlas::usesmcomparemode()
1235 : {
1236 0 : return !usegatherforsm() || (usetexgather > 1);
1237 : }
1238 :
1239 0 : void ShadowAtlas::view()
1240 : {
1241 0 : int w = std::min(hudw(), hudh())/2,
1242 0 : h = (w*hudh())/hudw(),
1243 0 : x = hudw()-w,
1244 0 : y = hudh()-h;
1245 0 : float tw = 1,
1246 0 : th = 1;
1247 0 : if(target == GL_TEXTURE_RECTANGLE)
1248 : {
1249 0 : vec2 sasize = shadowatlaspacker.dimensions();
1250 0 : tw = sasize.x;
1251 0 : th = sasize.y;
1252 0 : SETSHADER(hudrect);
1253 : }
1254 : else
1255 : {
1256 0 : hudshader->set();
1257 : }
1258 0 : gle::colorf(1, 1, 1);
1259 0 : glBindTexture(target, tex);
1260 0 : if(usesmcomparemode())
1261 : {
1262 0 : setsmnoncomparemode();
1263 : }
1264 0 : debugquad(x, y, w, h, 0, 0, tw, th);
1265 0 : if(usesmcomparemode())
1266 : {
1267 0 : setsmcomparemode();
1268 : }
1269 0 : }
1270 :
1271 : static VAR(debugshadowatlas, 0, 0, 1);
1272 :
1273 0 : void ShadowAtlas::setup()
1274 : {
1275 0 : int size = std::min((1<<smsize), hwtexsize);
1276 0 : shadowatlaspacker.resize(size, size);
1277 :
1278 0 : if(!tex)
1279 : {
1280 0 : glGenTextures(1, &tex);
1281 : }
1282 0 : vec2 sasize = shadowatlaspacker.dimensions();
1283 0 : target = usegatherforsm() ? GL_TEXTURE_2D : GL_TEXTURE_RECTANGLE;
1284 0 : createtexture(tex, sasize.x, sasize.y, nullptr, 3, 1, smdepthprec > 1 ? GL_DEPTH_COMPONENT32 : (smdepthprec ? GL_DEPTH_COMPONENT24 : GL_DEPTH_COMPONENT16), target);
1285 0 : glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
1286 0 : glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
1287 :
1288 0 : if(!fbo)
1289 : {
1290 0 : glGenFramebuffers(1, &fbo);
1291 : }
1292 0 : glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1293 0 : glDrawBuffer(GL_NONE);
1294 0 : glReadBuffer(GL_NONE);
1295 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, target, tex, 0);
1296 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
1297 : {
1298 0 : fatal("failed allocating shadow atlas!");
1299 : }
1300 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
1301 0 : }
1302 :
1303 : const matrix4 cubeshadowviewmatrix[6] =
1304 : {
1305 : // sign-preserving cubemap projections
1306 : matrix4(vec(0, 0, 1), vec(0, 1, 0), vec(-1, 0, 0)), // +X
1307 : matrix4(vec(0, 0, 1), vec(0, 1, 0), vec( 1, 0, 0)), // -X
1308 : matrix4(vec(1, 0, 0), vec(0, 0, 1), vec(0, -1, 0)), // +Y
1309 : matrix4(vec(1, 0, 0), vec(0, 0, 1), vec(0, 1, 0)), // -Y
1310 : matrix4(vec(1, 0, 0), vec(0, 1, 0), vec(0, 0, -1)), // +Z
1311 : matrix4(vec(1, 0, 0), vec(0, 1, 0), vec(0, 0, 1)) // -Z
1312 : };
1313 :
1314 : static constexpr int LightTile_MaxBatch = 8; //also used in lightbatchkey below
1315 :
1316 0 : static VARF(lighttilebatch, 0, LightTile_MaxBatch, LightTile_MaxBatch, cleardeferredlightshaders());
1317 0 : VARF(batchsunlight, 0, 2, 2, cleardeferredlightshaders());
1318 :
1319 : int shadowmapping = 0;
1320 :
1321 : //not final: batchstack/batchrect derived
1322 : class lightrect
1323 : {
1324 : public:
1325 : uchar x1, y1, x2, y2;
1326 :
1327 : lightrect() {}
1328 0 : lightrect(const lightinfo &l)
1329 0 : {
1330 0 : calctilebounds(l.sx1, l.sy1, l.sx2, l.sy2, x1, y1, x2, y2);
1331 0 : }
1332 :
1333 0 : bool outside(const lightrect &o) const
1334 : {
1335 0 : return x1 >= o.x2 || x2 <= o.x1 || y1 >= o.y2 || y2 <= o.y1;
1336 : }
1337 :
1338 0 : bool inside(const lightrect &o) const
1339 : {
1340 0 : return x1 >= o.x1 && x2 <= o.x2 && y1 >= o.y1 && y2 <= o.y2;
1341 : }
1342 :
1343 0 : void intersect(const lightrect &o)
1344 : {
1345 0 : x1 = std::max(x1, o.x1);
1346 0 : y1 = std::max(y1, o.y1);
1347 0 : x2 = std::min(x2, o.x2);
1348 0 : y2 = std::min(y2, o.y2);
1349 0 : }
1350 :
1351 0 : bool overlaps(int tx1, int ty1, int tx2, int ty2, const uint *tilemask) const
1352 : {
1353 0 : if(static_cast<int>(x2) <= tx1 || static_cast<int>(x1) >= tx2 || static_cast<int>(y2) <= ty1 || static_cast<int>(y1) >= ty2)
1354 : {
1355 0 : return false;
1356 : }
1357 0 : if(!tilemask)
1358 : {
1359 0 : return true;
1360 : }
1361 0 : uint xmask = (1<<x2) - (1<<x1);
1362 0 : for(int y = std::max(static_cast<int>(y1), ty1), end = std::min(static_cast<int>(y2), ty2); y < end; y++)
1363 : {
1364 0 : if(tilemask[y] & xmask)
1365 : {
1366 0 : return true;
1367 : }
1368 : }
1369 0 : return false;
1370 : }
1371 : protected:
1372 : //only called by child batchstack object
1373 0 : lightrect(uchar x1, uchar y1, uchar x2, uchar y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
1374 : };
1375 :
1376 : //batchflag enum is local to this file
1377 : enum
1378 : {
1379 : BatchFlag_Spotlight = 1<<0,
1380 : BatchFlag_NoShadow = 1<<1,
1381 : BatchFlag_NoSun = 1<<2
1382 : };
1383 :
1384 : struct lightbatch
1385 : {
1386 : uchar flags, numlights;
1387 : ushort lights[LightTile_MaxBatch];
1388 :
1389 : std::vector<lightrect> rects;
1390 :
1391 : void reset()
1392 : {
1393 : rects.clear();
1394 : }
1395 :
1396 0 : bool overlaps(int tx1, int ty1, int tx2, int ty2, const uint *tilemask) const
1397 : {
1398 0 : if(!tx1 && !ty1 && tx2 >= lighttilew && ty2 >= lighttileh && !tilemask)
1399 : {
1400 0 : return true;
1401 : }
1402 0 : for(size_t i = 0; i < rects.size(); i++)
1403 : {
1404 0 : if(rects[i].overlaps(tx1, ty1, tx2, ty2, tilemask))
1405 : {
1406 0 : return true;
1407 : }
1408 : }
1409 0 : return false;
1410 : }
1411 : };
1412 :
1413 : static std::vector<lightinfo> lights;
1414 : static std::vector<int> lightorder;
1415 : static std::vector<lightbatch> lightbatches;
1416 : std::vector<shadowmapinfo> shadowmaps;
1417 :
1418 1 : void clearshadowcache()
1419 : {
1420 1 : shadowmaps.clear();
1421 :
1422 1 : clearradiancehintscache();
1423 1 : clearshadowmeshes();
1424 1 : }
1425 :
1426 0 : void addshadowmap(ushort x, ushort y, int size, int &idx, int light, const shadowcacheval *cached)
1427 : {
1428 0 : idx = shadowmaps.size();
1429 : shadowmapinfo sm;
1430 0 : sm.x = x;
1431 0 : sm.y = y;
1432 0 : sm.size = size;
1433 0 : sm.light = light;
1434 0 : sm.sidemask = 0;
1435 0 : sm.cached = cached;
1436 0 : shadowmaps.push_back(sm);
1437 0 : }
1438 :
1439 : //calculate bouunding box reflective shadow map splits
1440 0 : int calcbbrsmsplits(const ivec &bbmin, const ivec &bbmax)
1441 : {
1442 0 : if(!rsmcull)
1443 : {
1444 0 : return 1;
1445 : }
1446 0 : for(int k = 0; k < 4; ++k)
1447 : {
1448 0 : const plane &p = rsm.cull[k];
1449 0 : ivec omin, omax;
1450 0 : if(p.x > 0)
1451 : {
1452 0 : omin.x = bbmin.x;
1453 0 : omax.x = bbmax.x;
1454 : }
1455 : else
1456 : {
1457 0 : omin.x = bbmax.x;
1458 0 : omax.x = bbmin.x;
1459 : }
1460 0 : if(p.y > 0)
1461 : {
1462 0 : omin.y = bbmin.y;
1463 0 : omax.y = bbmax.y;
1464 : }
1465 : else
1466 : {
1467 0 : omin.y = bbmax.y;
1468 0 : omax.y = bbmin.y;
1469 : }
1470 0 : if(p.z > 0)
1471 : {
1472 0 : omin.z = bbmin.z;
1473 0 : omax.z = bbmax.z;
1474 : }
1475 : else
1476 : {
1477 0 : omin.z = bbmax.z;
1478 0 : omax.z = bbmin.z;
1479 : }
1480 0 : if(omax.dist(p) < 0)
1481 : {
1482 0 : return 0;
1483 : }
1484 0 : if(omin.dist(p) < 0)
1485 : {
1486 0 : while(++k < 4)
1487 : {
1488 0 : const plane &p = rsm.cull[k];
1489 0 : ivec omax(p.x > 0 ? bbmax.x : bbmin.x, p.y > 0 ? bbmax.y : bbmin.y, p.z > 0 ? bbmax.z : bbmin.z);
1490 0 : if(omax.dist(p) < 0)
1491 : {
1492 0 : return 0;
1493 : }
1494 : }
1495 : }
1496 : }
1497 0 : return 1;
1498 : }
1499 :
1500 0 : int calcspherersmsplits(const vec ¢er, float radius)
1501 : {
1502 0 : if(!rsmcull)
1503 : {
1504 0 : return 1;
1505 : }
1506 0 : for(int k = 0; k < 4; ++k)
1507 : {
1508 0 : const plane &p = rsm.cull[k];
1509 0 : float dist = p.dist(center);
1510 0 : if(dist < -radius)
1511 : {
1512 0 : return 0;
1513 : }
1514 0 : if(dist < radius)
1515 : {
1516 0 : while(++k < 4)
1517 : {
1518 0 : const plane &p = rsm.cull[k];
1519 0 : if(p.dist(center) < -radius)
1520 : {
1521 0 : return 0;
1522 : }
1523 : }
1524 : }
1525 : }
1526 0 : return 1;
1527 : }
1528 :
1529 : static FVAR(avatarshadowdist, 0, 12, 100);
1530 : static FVAR(avatarshadowbias, 0, 8, 100);
1531 0 : static VARF(avatarshadowstencil, 0, 1, 2, initwarning("g-buffer setup", Init_Load, Change_Shaders));
1532 :
1533 : int avatarmask = 0;
1534 :
1535 1 : static bool useavatarmask()
1536 : {
1537 1 : return avatarshadowstencil && ghasstencil && (!msaasamples || (msaalight && avatarshadowstencil > 1));
1538 : }
1539 :
1540 0 : void enableavatarmask()
1541 : {
1542 0 : if(useavatarmask())
1543 : {
1544 0 : avatarmask = 0x40;
1545 0 : glStencilFunc(GL_ALWAYS, avatarmask, ~0);
1546 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
1547 0 : glEnable(GL_STENCIL_TEST);
1548 : }
1549 0 : }
1550 :
1551 0 : void disableavatarmask()
1552 : {
1553 0 : if(avatarmask)
1554 : {
1555 0 : avatarmask = 0;
1556 0 : glDisable(GL_STENCIL_TEST);
1557 : }
1558 0 : }
1559 :
1560 : VAR(forcespotlights, 1, 0, 0);
1561 :
1562 : static Shader *volumetricshader = nullptr;
1563 : std::array<Shader *, 2> volumetricbilateralshader = { nullptr, nullptr };
1564 :
1565 0 : void clearvolumetricshaders()
1566 : {
1567 0 : volumetricshader = nullptr;
1568 0 : volumetricbilateralshader.fill(nullptr);
1569 0 : }
1570 :
1571 0 : VARFP(volumetric, 0, 1, 1, cleanupvolumetric()); //toggles displaying volumetric lights
1572 0 : VARFP(volreduce, 0, 1, 2, cleanupvolumetric()); //size reduction factor for volumetric tex: 1 is 1/4, 2 is 1/16
1573 0 : VARFP(volbilateral, 0, 1, 3, cleanupvolumetric()); //toggles bilateral filtering
1574 : FVAR(volbilateraldepth, 0, 4, 1e3f); //bilateral filtering depth
1575 0 : VARFP(volblur, 0, 1, 3, cleanupvolumetric());
1576 0 : VARFP(volsteps, 1, 32, 128, cleanupvolumetric()); //iterations to run for volumetric algorithm
1577 : FVAR(volminstep, 0, 0.0625f, 1e3f);
1578 : FVAR(volprefilter, 0, 0.1, 1e3f);
1579 : FVAR(voldistclamp, 0, 0.99f, 2);
1580 0 : CVAR1R(volcolor, 0x808080);
1581 : FVARR(volscale, 0, 1, 16);
1582 :
1583 0 : Shader *loadvolumetricshader()
1584 : {
1585 0 : std::string common, shadow;
1586 :
1587 0 : if(usegatherforsm())
1588 : {
1589 0 : common.push_back(smfilter > 2 ? 'G' : 'g');
1590 : }
1591 0 : else if(smfilter)
1592 : {
1593 0 : common.push_back(smfilter > 2 ? 'E' : (smfilter > 1 ? 'F' : 'f'));
1594 : }
1595 0 : if(spotlights || forcespotlights)
1596 : {
1597 0 : common.push_back('s');
1598 : }
1599 :
1600 0 : shadow.push_back('p');
1601 :
1602 0 : DEF_FORMAT_STRING(name, "volumetric%s%s%d", common.c_str(), shadow.c_str(), volsteps);
1603 0 : return generateshader(name, "volumetricshader \"%s\" \"%s\" %d", common.c_str(), shadow.c_str(), volsteps);
1604 0 : }
1605 :
1606 0 : static void loadvolumetricshaders()
1607 : {
1608 0 : volumetricshader = loadvolumetricshader();
1609 :
1610 0 : if(volbilateral)
1611 : {
1612 0 : for(int i = 0; i < 2; ++i)
1613 : {
1614 0 : DEF_FORMAT_STRING(name, "volumetricbilateral%c%d%d", 'x' + i, volbilateral, volreduce);
1615 0 : volumetricbilateralshader[i] = generateshader(name, "volumetricbilateralshader %d %d", volbilateral, volreduce);
1616 : }
1617 : }
1618 0 : }
1619 :
1620 : static int volw = -1,
1621 : volh = -1;
1622 : static std::array<GLuint, 2> volfbo = { 0, 0 },
1623 : voltex = { 0, 0 };
1624 :
1625 0 : static void setupvolumetric(int w, int h)
1626 : {
1627 0 : volw = w>>volreduce;
1628 0 : volh = h>>volreduce;
1629 :
1630 0 : for(int i = 0; i < (volbilateral || volblur ? 2 : 1); ++i)
1631 : {
1632 0 : if(!voltex[i])
1633 : {
1634 0 : glGenTextures(1, &voltex[i]);
1635 : }
1636 0 : if(!volfbo[i])
1637 : {
1638 0 : glGenFramebuffers(1, &volfbo[i]);
1639 : }
1640 :
1641 0 : glBindFramebuffer(GL_FRAMEBUFFER, volfbo[i]);
1642 :
1643 0 : createtexture(voltex[i], volw, volh, nullptr, 3, 1, hdrformat, GL_TEXTURE_RECTANGLE);
1644 :
1645 0 : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, voltex[i], 0);
1646 :
1647 0 : if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
1648 : {
1649 0 : fatal("failed allocating volumetric buffer!");
1650 : }
1651 : }
1652 :
1653 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
1654 :
1655 0 : loadvolumetricshaders();
1656 0 : }
1657 :
1658 0 : void cleanupvolumetric()
1659 : {
1660 0 : for(GLuint &i : volfbo)
1661 : {
1662 0 : if(i)
1663 : {
1664 0 : glDeleteFramebuffers(1, &i);
1665 0 : i = 0;
1666 : }
1667 : }
1668 0 : for(GLuint &i : voltex)
1669 : {
1670 0 : if(i)
1671 : {
1672 0 : glDeleteTextures(1, &i);
1673 0 : i = 0;
1674 : }
1675 : }
1676 0 : volw = volh = -1;
1677 :
1678 0 : clearvolumetricshaders();
1679 0 : }
1680 :
1681 : static Shader *deferredlightshader = nullptr,
1682 : *deferredminimapshader = nullptr,
1683 : *deferredmsaapixelshader = nullptr,
1684 : *deferredmsaasampleshader = nullptr;
1685 :
1686 0 : void cleardeferredlightshaders()
1687 : {
1688 0 : deferredlightshader = nullptr;
1689 0 : deferredminimapshader = nullptr;
1690 0 : deferredmsaapixelshader = nullptr;
1691 0 : deferredmsaasampleshader = nullptr;
1692 0 : }
1693 :
1694 0 : Shader *loaddeferredlightshader(const char *type = nullptr)
1695 : {
1696 : string common, shadow, sun;
1697 0 : int commonlen = 0,
1698 0 : shadowlen = 0,
1699 0 : sunlen = 0;
1700 :
1701 0 : bool minimap = false,
1702 0 : multisample = false,
1703 0 : avatar = true;
1704 0 : if(type)
1705 : {
1706 0 : if(std::strchr(type, 'm'))
1707 : {
1708 0 : minimap = true;
1709 : }
1710 0 : if(std::strchr(type, 'M'))
1711 : {
1712 0 : multisample = true;
1713 : }
1714 0 : if(std::strchr(type, 'D'))
1715 : {
1716 0 : avatar = false;
1717 : }
1718 0 : copystring(common, type);
1719 0 : commonlen = std::strlen(common);
1720 : }
1721 0 : if(!minimap)
1722 : {
1723 0 : if(!multisample || msaalight)
1724 : {
1725 0 : common[commonlen++] = 't';
1726 : }
1727 0 : if(avatar && useavatarmask())
1728 : {
1729 0 : common[commonlen++] = 'd';
1730 : }
1731 0 : if(lighttilebatch)
1732 : {
1733 0 : common[commonlen++] = 'n';
1734 0 : common[commonlen++] = '0' + lighttilebatch;
1735 : }
1736 : }
1737 0 : if(usegatherforsm())
1738 : {
1739 0 : common[commonlen++] = smfilter > 2 ? 'G' : 'g';
1740 : }
1741 0 : else if(smfilter)
1742 : {
1743 0 : common[commonlen++] = smfilter > 2 ? 'E' : (smfilter > 1 ? 'F' : 'f');
1744 : }
1745 0 : if(spotlights || forcespotlights)
1746 : {
1747 0 : common[commonlen++] = 's';
1748 : }
1749 0 : if(nospeclights)
1750 : {
1751 0 : common[commonlen++] = 'z';
1752 : }
1753 0 : common[commonlen] = '\0';
1754 :
1755 0 : shadow[shadowlen++] = 'p';
1756 0 : shadow[shadowlen] = '\0';
1757 :
1758 0 : int usecsm = 0,
1759 0 : userh = 0;
1760 0 : if(!sunlight.iszero() && csm.getcsmproperty(cascadedshadowmap::ShadowMap))
1761 : {
1762 0 : usecsm = csm.getcsmproperty(cascadedshadowmap::Splits);
1763 0 : sun[sunlen++] = 'c';
1764 0 : sun[sunlen++] = '0' + usecsm;
1765 0 : if(!minimap)
1766 : {
1767 0 : if(avatar && ao && aosun)
1768 : {
1769 0 : sun[sunlen++] = 'A';
1770 : }
1771 0 : if(gi && giscale && gidist)
1772 : {
1773 0 : userh = rhsplits;
1774 0 : sun[sunlen++] = 'r';
1775 0 : sun[sunlen++] = '0' + rhsplits;
1776 : }
1777 : }
1778 : }
1779 0 : if(!minimap)
1780 : {
1781 0 : if(avatar && ao)
1782 : {
1783 0 : sun[sunlen++] = 'a';
1784 : }
1785 0 : if(lighttilebatch && (!usecsm || batchsunlight > (userh ? 1 : 0)))
1786 : {
1787 0 : sun[sunlen++] = 'b';
1788 : }
1789 : }
1790 0 : sun[sunlen] = '\0';
1791 :
1792 0 : DEF_FORMAT_STRING(name, "deferredlight%s%s%s", common, shadow, sun);
1793 0 : return generateshader(name, "deferredlightshader \"%s\" \"%s\" \"%s\" %d %d %d", common, shadow, sun, usecsm, userh, !minimap ? lighttilebatch : 0);
1794 : }
1795 :
1796 0 : void loaddeferredlightshaders()
1797 : {
1798 0 : if(msaasamples)
1799 : {
1800 : string opts;
1801 0 : if(msaalight > 2)
1802 : {
1803 0 : copystring(opts, "MS");
1804 : }
1805 0 : else if(msaalight==2)
1806 : {
1807 0 : copystring(opts, ghasstencil || !msaaedgedetect ? "MO" : "MOT");
1808 : }
1809 : else
1810 : {
1811 0 : formatstring(opts, ghasstencil || !msaaedgedetect ? "MR%d" : "MRT%d", msaasamples);
1812 : }
1813 0 : deferredmsaasampleshader = loaddeferredlightshader(opts);
1814 0 : deferredmsaapixelshader = loaddeferredlightshader("M");
1815 0 : deferredlightshader = msaalight ? deferredmsaapixelshader : loaddeferredlightshader("D");
1816 : }
1817 : else
1818 : {
1819 0 : deferredlightshader = loaddeferredlightshader();
1820 : }
1821 0 : }
1822 :
1823 0 : static bool sortlights(int x, int y)
1824 : {
1825 0 : const lightinfo &xl = lights[x],
1826 0 : &yl = lights[y];
1827 0 : if(!xl.spot)
1828 : {
1829 0 : if(yl.spot)
1830 : {
1831 0 : return true;
1832 : }
1833 : }
1834 0 : else if(!yl.spot)
1835 : {
1836 0 : return false;
1837 : }
1838 0 : if(!xl.noshadow())
1839 : {
1840 0 : if(yl.noshadow())
1841 : {
1842 0 : return true;
1843 : }
1844 : }
1845 0 : else if(!yl.noshadow())
1846 : {
1847 0 : return false;
1848 : }
1849 0 : if(xl.sz1 < yl.sz1)
1850 : {
1851 0 : return true;
1852 : }
1853 0 : else if(xl.sz1 > yl.sz1)
1854 : {
1855 0 : return false;
1856 : }
1857 0 : return xl.dist - xl.radius < yl.dist - yl.radius;
1858 : }
1859 :
1860 : VAR(lighttilealignw, 1, 16, 256); // x tiling size for lights inside the shadow cache (pixel grid size to snap to)
1861 : VAR(lighttilealignh, 1, 16, 256); // y tiling size for lights
1862 : int lighttilemaxw = variable("lighttilew", 1, 10, lighttilemaxwidth, &lighttilemaxw, nullptr, 0);
1863 : int lighttilemaxh = variable("lighttileh", 1, 10, lighttilemaxheight, &lighttilemaxh, nullptr, 0);
1864 :
1865 : int lighttilew = 0,
1866 : lighttileh = 0,
1867 : lighttilevieww = 0,
1868 : lighttileviewh = 0;
1869 :
1870 0 : void calctilesize()
1871 : {
1872 0 : lighttilevieww = (vieww + lighttilealignw - 1)/lighttilealignw;
1873 0 : lighttileviewh = (viewh + lighttilealignh - 1)/lighttilealignh;
1874 0 : lighttilew = std::min(lighttilevieww, lighttilemaxw);
1875 0 : lighttileh = std::min(lighttileviewh, lighttilemaxh);
1876 0 : }
1877 :
1878 0 : void resetlights()
1879 : {
1880 : static constexpr int shadowcacheevict = 2;
1881 : static int evictshadowcache = 0;
1882 0 : shadowatlas.cache.clear();
1883 0 : if(smcache)
1884 : {
1885 0 : vec2 sasize = shadowatlaspacker.dimensions();
1886 0 : int evictx = ((evictshadowcache%shadowcacheevict)*sasize.x)/shadowcacheevict,
1887 0 : evicty = ((evictshadowcache/shadowcacheevict)*sasize.y)/shadowcacheevict,
1888 0 : evictx2 = (((evictshadowcache%shadowcacheevict)+1)*sasize.x)/shadowcacheevict,
1889 0 : evicty2 = (((evictshadowcache/shadowcacheevict)+1)*sasize.y)/shadowcacheevict;
1890 0 : for(const shadowmapinfo &sm : shadowmaps)
1891 : {
1892 0 : if(sm.light < 0)
1893 : {
1894 0 : continue;
1895 : }
1896 0 : lightinfo &l = lights[sm.light];
1897 0 : if(sm.cached && shadowatlas.full)
1898 : {
1899 0 : int w = l.spot ? sm.size : sm.size*3,
1900 0 : h = l.spot ? sm.size : sm.size*2;
1901 0 : if(sm.x < evictx2 && sm.x + w > evictx && sm.y < evicty2 && sm.y + h > evicty)
1902 : {
1903 0 : continue;
1904 : }
1905 : }
1906 0 : shadowatlas.cache[l] = sm;
1907 : }
1908 0 : if(shadowatlas.full)
1909 : {
1910 0 : evictshadowcache = (evictshadowcache + 1)%(shadowcacheevict*shadowcacheevict);
1911 0 : shadowatlas.full = false;
1912 : }
1913 : }
1914 :
1915 0 : lights.clear();
1916 0 : lightorder.clear();
1917 :
1918 0 : shadowmaps.clear();
1919 0 : shadowatlaspacker.reset();
1920 :
1921 0 : calctilesize();
1922 0 : }
1923 :
1924 : static VAR(depthtestlights, 0, 2, 2);
1925 : static FVAR(depthtestlightsclamp, 0, 0.999995f, 1); //z margin for light depth testing at depthtestlights = 2
1926 : static VAR(depthfaillights, 0, 1, 1);
1927 :
1928 0 : static void lightquads(float z, const vec2 &s1, const vec2 &s2)
1929 : {
1930 0 : gle::attribf(s1.x, s1.y, z);
1931 0 : gle::attribf(s1.x, s2.y, z);
1932 0 : gle::attribf(s2.x, s2.y, z);
1933 0 : gle::attribf(s1.x, s1.y, z);
1934 0 : gle::attribf(s2.x, s2.y, z);
1935 0 : gle::attribf(s2.x, s1.y, z);
1936 :
1937 0 : }
1938 :
1939 0 : static void lightquads(float z, const vec2 &s1, const vec2 &s2, const ivec2 &t1, const ivec2 &t2)
1940 : {
1941 0 : int vx1 = std::max(static_cast<int>(std::floor((s1.x*0.5f+0.5f)*vieww)), ((t1.x()*lighttilevieww)/lighttilew)*lighttilealignw),
1942 0 : vy1 = std::max(static_cast<int>(std::floor((s1.y*0.5f+0.5f)*viewh)), ((t1.y()*lighttileviewh)/lighttileh)*lighttilealignh),
1943 0 : vx2 = std::min(static_cast<int>(std::ceil((s2.x*0.5f+0.5f)*vieww)), std::min(((t2.x()*lighttilevieww)/lighttilew)*lighttilealignw, vieww)),
1944 0 : vy2 = std::min(static_cast<int>(std::ceil((s2.y*0.5f+0.5f)*viewh)), std::min(((t2.y()*lighttileviewh)/lighttileh)*lighttilealignh, viewh));
1945 0 : lightquads(z, {(vx1*2.0f)/vieww-1.0f, (vy1*2.0f)/viewh-1.0f}, {(vx2*2.0f)/vieww-1.0f, (vy2*2.0f)/viewh-1.0f});
1946 0 : }
1947 :
1948 0 : static void lightquads(float z, vec2 s1, vec2 s2, int x1, int y1, int x2, int y2, const uint *tilemask)
1949 : {
1950 0 : if(!tilemask)
1951 : {
1952 0 : lightquads(z, s1, s2, {x1, y1}, {x2, y2});
1953 : }
1954 : else
1955 : {
1956 0 : for(int y = y1; y < y2;)
1957 : {
1958 0 : int starty = y;
1959 0 : uint xmask = (1<<x2) - (1<<x1),
1960 0 : startmask = tilemask[y] & xmask;
1961 : do
1962 : {
1963 0 : ++y;
1964 0 : } while(y < y2 && (tilemask[y]&xmask) == startmask);
1965 0 : for(int x = x1; x < x2;)
1966 : {
1967 0 : while(x < x2 && !(startmask&(1<<x)))
1968 : {
1969 0 : ++x;
1970 : }
1971 0 : if(x >= x2)
1972 : {
1973 0 : break;
1974 : }
1975 0 : int startx = x;
1976 : do
1977 : {
1978 0 : ++x;
1979 0 : } while(x < x2 && startmask&(1<<x));
1980 0 : lightquads(z, s1, s2, {startx, starty}, {x, y});
1981 : }
1982 : }
1983 : }
1984 0 : }
1985 :
1986 0 : static void lightquad(float sz1, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask)
1987 : {
1988 : int btx1, bty1, btx2, bty2;
1989 0 : calctilebounds(bsx1, bsy1, bsx2, bsy2, btx1, bty1, btx2, bty2);
1990 :
1991 0 : gle::begin(GL_TRIANGLES);
1992 0 : lightquads(sz1, {bsx1, bsy1}, {bsx2, bsy2}, btx1, bty1, btx2, bty2, tilemask);
1993 0 : gle::end();
1994 0 : }
1995 :
1996 0 : void GBuffer::bindlighttexs(int msaapass, bool transparent) const
1997 : {
1998 0 : if(msaapass)
1999 : {
2000 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mscolortex);
2001 : }
2002 : else
2003 : {
2004 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gcolortex);
2005 : }
2006 0 : glActiveTexture(GL_TEXTURE1);
2007 0 : if(msaapass)
2008 : {
2009 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msnormaltex);
2010 : }
2011 : else
2012 : {
2013 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gnormaltex);
2014 : }
2015 0 : if(transparent)
2016 : {
2017 0 : glActiveTexture(GL_TEXTURE2);
2018 0 : if(msaapass)
2019 : {
2020 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msglowtex);
2021 : }
2022 : else
2023 : {
2024 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gglowtex);
2025 : }
2026 : }
2027 0 : glActiveTexture(GL_TEXTURE3);
2028 0 : if(msaapass)
2029 : {
2030 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
2031 : }
2032 : else
2033 : {
2034 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
2035 : }
2036 0 : glActiveTexture(GL_TEXTURE4);
2037 0 : shadowatlas.bind();
2038 0 : shadowatlas.setcomparemode();
2039 0 : if(ao)
2040 : {
2041 0 : glActiveTexture(GL_TEXTURE5);
2042 0 : glBindTexture(GL_TEXTURE_RECTANGLE, aotex[2] ? aotex[2] : aotex[0]);
2043 : }
2044 0 : if(useradiancehints())
2045 : {
2046 0 : for(int i = 0; i < 4; ++i)
2047 : {
2048 0 : glActiveTexture(GL_TEXTURE6 + i);
2049 0 : glBindTexture(GL_TEXTURE_3D, rhtex[i]);
2050 : }
2051 : }
2052 0 : glActiveTexture(GL_TEXTURE0);
2053 0 : }
2054 :
2055 0 : static void setlightglobals(bool transparent = false)
2056 : {
2057 0 : vec2 sasize = shadowatlaspacker.dimensions();
2058 0 : GLOBALPARAMF(shadowatlasscale, 1.0f/sasize.x, 1.0f/sasize.y);
2059 0 : if(ao)
2060 : {
2061 0 : if(transparent || drawtex || (editmode && fullbright))
2062 : {
2063 0 : GLOBALPARAMF(aoscale, 0.0f, 0.0f);
2064 0 : GLOBALPARAMF(aoparams, 1.0f, 0.0f, 1.0f, 0.0f);
2065 0 : }
2066 : else
2067 : {
2068 0 : GLOBALPARAM(aoscale, aotex[2] ? vec2(1, 1) : vec2(static_cast<float>(aow)/vieww, static_cast<float>(aoh)/viewh));
2069 0 : GLOBALPARAMF(aoparams, aomin, 1.0f-aomin, aosunmin, 1.0f-aosunmin);
2070 : }
2071 : }
2072 0 : float lightscale = 2.0f*ldrscaleb();
2073 0 : if(!drawtex && editmode && fullbright)
2074 : {
2075 0 : GLOBALPARAMF(lightscale, fullbrightlevel*lightscale, fullbrightlevel*lightscale, fullbrightlevel*lightscale, 255*lightscale);
2076 0 : }
2077 : else
2078 : {
2079 0 : GLOBALPARAMF(lightscale, ambient.x*lightscale*ambientscale, ambient.y*lightscale*ambientscale, ambient.z*lightscale*ambientscale, 255*lightscale);
2080 : }
2081 0 : if(!sunlight.iszero() && csm.getcsmproperty(cascadedshadowmap::ShadowMap))
2082 : {
2083 0 : csm.bindparams();
2084 0 : rh.bindparams();
2085 0 : if(!drawtex && editmode && fullbright)
2086 : {
2087 0 : GLOBALPARAMF(sunlightdir, 0, 0, 0);
2088 0 : GLOBALPARAMF(sunlightcolor, 0, 0, 0);
2089 0 : GLOBALPARAMF(giscale, 0);
2090 0 : GLOBALPARAMF(skylightcolor, 0, 0, 0);
2091 0 : }
2092 : else
2093 : {
2094 0 : GLOBALPARAM(sunlightdir, sunlightdir);
2095 0 : GLOBALPARAMF(sunlightcolor, sunlight.x*lightscale*sunlightscale, sunlight.y*lightscale*sunlightscale, sunlight.z*lightscale*sunlightscale);
2096 0 : GLOBALPARAMF(giscale, 2*giscale);
2097 0 : GLOBALPARAMF(skylightcolor, 2*giaoscale*skylight.x*lightscale*skylightscale, 2*giaoscale*skylight.y*lightscale*skylightscale, 2*giaoscale*skylight.z*lightscale*skylightscale);
2098 : }
2099 : }
2100 :
2101 0 : matrix4 lightmatrix;
2102 0 : lightmatrix.identity();
2103 0 : GLOBALPARAM(lightmatrix, lightmatrix);
2104 0 : }
2105 :
2106 : //values only for interaction between setlightparams() and setlightshader()
2107 : struct LightParamInfo
2108 : {
2109 : std::array<vec4<float>, 8> lightposv, lightcolorv, spotparamsv, shadowparamsv;
2110 : std::array<vec2, 8> shadowoffsetv;
2111 : };
2112 :
2113 : //sets the ith element of lightposv, lightcolorv, spotparamsv, shadowparamsv, shadowoffsetv
2114 : //UB if i > 7
2115 : //
2116 0 : static void setlightparams(int i, const lightinfo &l, LightParamInfo &li)
2117 : {
2118 0 : li.lightposv[i] = vec4<float>(l.o, 1).div(l.radius);
2119 0 : li.lightcolorv[i] = vec4<float>(vec(l.color).mul(2*ldrscaleb()), l.nospec() ? 0 : 1);
2120 0 : if(l.spot > 0)
2121 : {
2122 0 : li.spotparamsv[i] = vec4<float>(vec(l.dir).neg(), 1/(1 - cos360(l.spot)));
2123 : }
2124 0 : if(l.shadowmap >= 0)
2125 : {
2126 0 : const shadowmapinfo &sm = shadowmaps[l.shadowmap];
2127 0 : float smnearclip = SQRT3 / l.radius, smfarclip = SQRT3,
2128 0 : bias = (smfilter > 2 || shadowatlaspacker.dimensions().x > shadowatlassize ? smbias2 : smbias) * (smcullside ? 1 : -1) * smnearclip * (1024.0f / sm.size);
2129 0 : int border = smfilter > 2 ? smborder2 : smborder;
2130 0 : if(l.spot > 0)
2131 : {
2132 0 : li.shadowparamsv[i] = vec4<float>(
2133 0 : -0.5f * sm.size * cotan360(l.spot),
2134 0 : (-smnearclip * smfarclip / (smfarclip - smnearclip) - 0.5f*bias),
2135 0 : 1 / (1 + std::fabs(l.dir.z)),
2136 0 : 0.5f + 0.5f * (smfarclip + smnearclip) / (smfarclip - smnearclip));
2137 : }
2138 : else
2139 : {
2140 0 : li.shadowparamsv[i] = vec4<float>(
2141 0 : -0.5f * (sm.size - border),
2142 0 : -smnearclip * smfarclip / (smfarclip - smnearclip) - 0.5f*bias,
2143 0 : sm.size,
2144 0 : 0.5f + 0.5f * (smfarclip + smnearclip) / (smfarclip - smnearclip));
2145 : }
2146 0 : li.shadowoffsetv[i] = vec2(sm.x + 0.5f*sm.size, sm.y + 0.5f*sm.size);
2147 : }
2148 0 : }
2149 :
2150 0 : static void setlightshader(Shader *s, const LightParamInfo &li, int n, bool baselight, bool shadowmap, bool spotlight, bool transparent = false, bool avatar = false)
2151 : {
2152 0 : static const LocalShaderParam lightpos("lightpos"),
2153 0 : lightcolor("lightcolor"),
2154 0 : spotparams("spotparams"),
2155 0 : shadowparams("shadowparams"),
2156 0 : shadowoffset("shadowoffset");
2157 0 : s->setvariant(n-1, (shadowmap ? 1 : 0) + (baselight ? 0 : 2) + (spotlight ? 4 : 0) + (transparent ? 8 : 0) + (avatar ? 24 : 0));
2158 0 : lightpos.setv(li.lightposv.data(), n);
2159 0 : lightcolor.setv(li.lightcolorv.data(), n);
2160 0 : if(spotlight)
2161 : {
2162 0 : spotparams.setv(li.spotparamsv.data(), n);
2163 : }
2164 0 : if(shadowmap)
2165 : {
2166 0 : shadowparams.setv(li.shadowparamsv.data(), n);
2167 0 : shadowoffset.setv(li.shadowoffsetv.data(), n);
2168 : }
2169 0 : }
2170 :
2171 0 : static void setavatarstencil(int stencilref, bool on)
2172 : {
2173 0 : glStencilFunc(GL_EQUAL, (on ? 0x40 : 0) | stencilref, !(stencilref&0x08) && msaalight==2 ? 0x47 : 0x4F);
2174 0 : }
2175 :
2176 0 : void GBuffer::rendersunpass(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask)
2177 : {
2178 0 : if(hasDBT && depthtestlights > 1)
2179 : {
2180 0 : glDepthBounds_(0, depthtestlightsclamp);
2181 : }
2182 0 : int tx1 = std::max(static_cast<int>(std::floor((bsx1*0.5f+0.5f)*vieww)), 0),
2183 0 : ty1 = std::max(static_cast<int>(std::floor((bsy1*0.5f+0.5f)*viewh)), 0),
2184 0 : tx2 = std::min(static_cast<int>(std::ceil((bsx2*0.5f+0.5f)*vieww)), vieww),
2185 0 : ty2 = std::min(static_cast<int>(std::ceil((bsy2*0.5f+0.5f)*viewh)), viewh);
2186 0 : s->setvariant(transparent ? 0 : -1, 16);
2187 0 : lightquad(-1, (tx1*2.0f)/vieww-1.0f, (ty1*2.0f)/viewh-1.0f, (tx2*2.0f)/vieww-1.0f, (ty2*2.0f)/viewh-1.0f, tilemask);
2188 0 : lightpassesused++;
2189 :
2190 0 : if(stencilref >= 0)
2191 : {
2192 0 : setavatarstencil(stencilref, true);
2193 :
2194 0 : s->setvariant(0, 17);
2195 0 : lightquad(-1, (tx1*2.0f)/vieww-1.0f, (ty1*2.0f)/viewh-1.0f, (tx2*2.0f)/vieww-1.0f, (ty2*2.0f)/viewh-1.0f, tilemask);
2196 0 : lightpassesused++;
2197 :
2198 0 : setavatarstencil(stencilref, false);
2199 : }
2200 0 : }
2201 :
2202 0 : void GBuffer::renderlightsnobatch(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2)
2203 : {
2204 0 : lightsphere::enable();
2205 :
2206 0 : glEnable(GL_SCISSOR_TEST);
2207 :
2208 0 : bool outside = true;
2209 0 : static LightParamInfo li;
2210 0 : for(int avatarpass = 0; avatarpass < (stencilref >= 0 ? 2 : 1); ++avatarpass)
2211 : {
2212 0 : if(avatarpass)
2213 : {
2214 0 : setavatarstencil(stencilref, true);
2215 : }
2216 :
2217 0 : for(size_t i = 0; i < lightorder.size(); i++)
2218 : {
2219 0 : const lightinfo &l = lights[lightorder[i]];
2220 0 : float sx1 = std::max(bsx1, l.sx1),
2221 0 : sy1 = std::max(bsy1, l.sy1),
2222 0 : sx2 = std::min(bsx2, l.sx2),
2223 0 : sy2 = std::min(bsy2, l.sy2);
2224 0 : if(sx1 >= sx2 || sy1 >= sy2 || l.sz1 >= l.sz2 || (avatarpass && l.dist - l.radius > avatarshadowdist))
2225 : {
2226 0 : continue;
2227 : }
2228 0 : matrix4 lightmatrix = camprojmatrix;
2229 0 : lightmatrix.translate(l.o);
2230 0 : lightmatrix.scale(l.radius);
2231 0 : GLOBALPARAM(lightmatrix, lightmatrix);
2232 :
2233 0 : setlightparams(0, l, li);
2234 0 : setlightshader(s, li, 1, false, l.shadowmap >= 0, l.spot > 0, transparent, avatarpass > 0);
2235 :
2236 0 : int tx1 = static_cast<int>(std::floor((sx1*0.5f+0.5f)*vieww)),
2237 0 : ty1 = static_cast<int>(std::floor((sy1*0.5f+0.5f)*viewh)),
2238 0 : tx2 = static_cast<int>(std::ceil((sx2*0.5f+0.5f)*vieww)),
2239 0 : ty2 = static_cast<int>(std::ceil((sy2*0.5f+0.5f)*viewh));
2240 0 : glScissor(tx1, ty1, tx2-tx1, ty2-ty1);
2241 :
2242 0 : if(hasDBT && depthtestlights > 1)
2243 : {
2244 0 : glDepthBounds_(l.sz1*0.5f + 0.5f, std::min(l.sz2*0.5f + 0.5f, depthtestlightsclamp));
2245 : }
2246 :
2247 0 : if(camera1->o.dist(l.o) <= l.radius + nearplane + 1 && depthfaillights)
2248 : {
2249 0 : if(outside)
2250 : {
2251 0 : outside = false;
2252 0 : glDepthFunc(GL_GEQUAL);
2253 0 : glCullFace(GL_FRONT);
2254 : }
2255 : }
2256 0 : else if(!outside)
2257 : {
2258 0 : outside = true;
2259 0 : glDepthFunc(GL_LESS);
2260 0 : glCullFace(GL_BACK);
2261 : }
2262 :
2263 0 : lightsphere::draw();
2264 :
2265 0 : lightpassesused++;
2266 : }
2267 :
2268 0 : if(avatarpass)
2269 : {
2270 0 : setavatarstencil(stencilref, false);
2271 : }
2272 : }
2273 :
2274 0 : if(!outside)
2275 : {
2276 0 : outside = true;
2277 0 : glDepthFunc(GL_LESS);
2278 0 : glCullFace(GL_BACK);
2279 : }
2280 :
2281 0 : glDisable(GL_SCISSOR_TEST);
2282 :
2283 0 : lightsphere::disable();
2284 0 : }
2285 :
2286 0 : void GBuffer::renderlightbatches(Shader &s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask)
2287 : {
2288 0 : bool sunpass = !sunlight.iszero() && csm.getcsmproperty(cascadedshadowmap::ShadowMap) && batchsunlight <= (gi && giscale && gidist ? 1 : 0);
2289 : int btx1, bty1, btx2, bty2;
2290 0 : calctilebounds(bsx1, bsy1, bsx2, bsy2, btx1, bty1, btx2, bty2);
2291 0 : static LightParamInfo li;
2292 0 : for(size_t i = 0; i < lightbatches.size(); i++)
2293 : {
2294 0 : const lightbatch &batch = lightbatches[i];
2295 0 : if(!batch.overlaps(btx1, bty1, btx2, bty2, tilemask))
2296 : {
2297 0 : continue;
2298 : }
2299 :
2300 0 : int n = batch.numlights;
2301 0 : float sx1 = 1,
2302 0 : sy1 = 1,
2303 0 : sx2 = -1,
2304 0 : sy2 = -1,
2305 0 : sz1 = 1,
2306 0 : sz2 = -1;
2307 0 : for(int j = 0; j < n; ++j)
2308 : {
2309 0 : const lightinfo &l = lights[batch.lights[j]];
2310 0 : setlightparams(j, l, li); //set 0...batch.numlights
2311 0 : l.addscissor(sx1, sy1, sx2, sy2, sz1, sz2);
2312 : }
2313 :
2314 0 : bool baselight = !(batch.flags & BatchFlag_NoSun) && !sunpass;
2315 0 : if(baselight)
2316 : {
2317 0 : sx1 = bsx1;
2318 0 : sy1 = bsy1;
2319 0 : sx2 = bsx2;
2320 0 : sy2 = bsy2;
2321 0 : sz1 = -1;
2322 0 : sz2 = 1;
2323 : }
2324 : else
2325 : {
2326 0 : sx1 = std::max(sx1, bsx1);
2327 0 : sy1 = std::max(sy1, bsy1);
2328 0 : sx2 = std::min(sx2, bsx2);
2329 0 : sy2 = std::min(sy2, bsy2);
2330 0 : if(sx1 >= sx2 || sy1 >= sy2 || sz1 >= sz2)
2331 : {
2332 0 : continue;
2333 : }
2334 : }
2335 :
2336 0 : if(n)
2337 : {
2338 0 : bool shadowmap = !(batch.flags & BatchFlag_NoShadow),
2339 0 : spotlight = (batch.flags & BatchFlag_Spotlight) != 0;
2340 0 : setlightshader(&s, li, n, baselight, shadowmap, spotlight, transparent);
2341 : }
2342 : else
2343 : {
2344 0 : s.setvariant(transparent ? 0 : -1, 16);
2345 : }
2346 :
2347 0 : lightpassesused++;
2348 :
2349 0 : if(hasDBT && depthtestlights > 1)
2350 : {
2351 0 : glDepthBounds_(sz1*0.5f + 0.5f, std::min(sz2*0.5f + 0.5f, depthtestlightsclamp));
2352 : }
2353 0 : gle::begin(GL_TRIANGLES);
2354 0 : for(size_t j = 0; j < batch.rects.size(); j++)
2355 : {
2356 0 : const lightrect &r = batch.rects[j];
2357 0 : int x1 = std::max(static_cast<int>(r.x1), btx1),
2358 0 : y1 = std::max(static_cast<int>(r.y1), bty1),
2359 0 : x2 = std::min(static_cast<int>(r.x2), btx2),
2360 0 : y2 = std::min(static_cast<int>(r.y2), bty2);
2361 0 : if(x1 < x2 && y1 < y2)
2362 : {
2363 0 : lightquads(sz1, {sx1, sy1}, {sx2, sy2}, x1, y1, x2, y2, tilemask);
2364 : }
2365 : }
2366 0 : gle::end();
2367 : }
2368 :
2369 0 : if(stencilref >= 0)
2370 : {
2371 0 : setavatarstencil(stencilref, true);
2372 :
2373 0 : bool baselight = !sunpass;
2374 0 : for(int offset = 0; baselight || offset < static_cast<int>(lightorder.size()); baselight = false)
2375 : {
2376 0 : int n = 0;
2377 0 : bool shadowmap = false,
2378 0 : spotlight = false;
2379 0 : float sx1 = 1,
2380 0 : sy1 = 1,
2381 0 : sx2 = -1,
2382 0 : sy2 = -1,
2383 0 : sz1 = 1,
2384 0 : sz2 = -1;
2385 0 : for(; offset < static_cast<int>(lightorder.size()); offset++)
2386 : {
2387 0 : const lightinfo &l = lights[lightorder[offset]];
2388 0 : if(l.dist - l.radius > avatarshadowdist)
2389 : {
2390 0 : continue;
2391 : }
2392 0 : if(!n)
2393 : {
2394 0 : shadowmap = l.shadowmap >= 0;
2395 0 : spotlight = l.spot > 0;
2396 : }
2397 0 : else if(n >= lighttilebatch || (l.shadowmap >= 0) != shadowmap || (l.spot > 0) != spotlight)
2398 : {
2399 : break;
2400 : }
2401 0 : setlightparams(n++, l, li);
2402 0 : l.addscissor(sx1, sy1, sx2, sy2, sz1, sz2);
2403 : }
2404 0 : if(baselight)
2405 : {
2406 0 : sx1 = bsx1;
2407 0 : sy1 = bsy1;
2408 0 : sx2 = bsx2;
2409 0 : sy2 = bsy2;
2410 0 : sz1 = -1;
2411 0 : sz2 = 1;
2412 : }
2413 : else
2414 : {
2415 0 : if(!n)
2416 : {
2417 0 : break;
2418 : }
2419 0 : sx1 = std::max(sx1, bsx1);
2420 0 : sy1 = std::max(sy1, bsy1);
2421 0 : sx2 = std::min(sx2, bsx2);
2422 0 : sy2 = std::min(sy2, bsy2);
2423 0 : if(sx1 >= sx2 || sy1 >= sy2 || sz1 >= sz2)
2424 : {
2425 0 : continue;
2426 : }
2427 : }
2428 :
2429 0 : if(n)
2430 : {
2431 0 : setlightshader(&s, li, n, baselight, shadowmap, spotlight, false, true);
2432 : }
2433 : else
2434 : {
2435 0 : s.setvariant(0, 17);
2436 : }
2437 0 : if(hasDBT && depthtestlights > 1)
2438 : {
2439 0 : glDepthBounds_(sz1*0.5f + 0.5f, std::min(sz2*0.5f + 0.5f, depthtestlightsclamp));
2440 : }
2441 0 : lightquad(sz1, sx1, sy1, sx2, sy2, tilemask);
2442 0 : lightpassesused++;
2443 : }
2444 :
2445 0 : setavatarstencil(stencilref, false);
2446 : }
2447 0 : }
2448 :
2449 0 : void GBuffer::renderlights(float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask, int stencilmask, int msaapass, bool transparent)
2450 : {
2451 0 : Shader *s = drawtex == Draw_TexMinimap ? deferredminimapshader : (msaapass <= 0 ? deferredlightshader : (msaapass > 1 ? deferredmsaasampleshader : deferredmsaapixelshader));
2452 0 : if(!s || s == nullshader)
2453 : {
2454 0 : return;
2455 : }
2456 :
2457 0 : bool depth = true;
2458 0 : if(!depthtestlights)
2459 : {
2460 0 : glDisable(GL_DEPTH_TEST);
2461 0 : depth = false;
2462 : }
2463 : else
2464 : {
2465 0 : glDepthMask(GL_FALSE);
2466 : }
2467 :
2468 0 : bindlighttexs(msaapass, transparent);
2469 0 : setlightglobals(transparent);
2470 :
2471 0 : gle::defvertex(3);
2472 :
2473 0 : bool avatar = useavatarmask() && !transparent && !drawtex;
2474 0 : int stencilref = -1;
2475 0 : if(msaapass == 1 && ghasstencil)
2476 : {
2477 0 : int tx1 = std::max(static_cast<int>(std::floor((bsx1*0.5f+0.5f)*vieww)), 0),
2478 0 : ty1 = std::max(static_cast<int>(std::floor((bsy1*0.5f+0.5f)*viewh)), 0),
2479 0 : tx2 = std::min(static_cast<int>(std::ceil((bsx2*0.5f+0.5f)*vieww)), vieww),
2480 0 : ty2 = std::min(static_cast<int>(std::ceil((bsy2*0.5f+0.5f)*viewh)), viewh);
2481 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2482 0 : if(stencilmask)
2483 : {
2484 0 : glStencilFunc(GL_EQUAL, stencilmask|0x08, 0x07);
2485 : }
2486 : else
2487 : {
2488 0 : glStencilFunc(GL_ALWAYS, 0x08, ~0);
2489 0 : glEnable(GL_STENCIL_TEST);
2490 : }
2491 0 : if(avatar)
2492 : {
2493 0 : glStencilMask(~0x40);
2494 : }
2495 0 : if(depthtestlights && depth)
2496 : {
2497 0 : glDisable(GL_DEPTH_TEST);
2498 0 : depth = false;
2499 : }
2500 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2501 0 : SETSHADER(msaaedgedetect);
2502 0 : lightquad(-1, (tx1*2.0f)/vieww-1.0f, (ty1*2.0f)/viewh-1.0f, (tx2*2.0f)/vieww-1.0f, (ty2*2.0f)/viewh-1.0f, tilemask);
2503 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2504 0 : glStencilFunc(GL_EQUAL, stencilref = stencilmask, (avatar ? 0x40 : 0) | (msaalight==2 ? 0x07 : 0x0F));
2505 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2506 0 : if(avatar)
2507 : {
2508 0 : glStencilMask(~0);
2509 : }
2510 0 : else if(msaalight==2 && !stencilmask)
2511 : {
2512 0 : glDisable(GL_STENCIL_TEST);
2513 : }
2514 0 : }
2515 0 : else if(msaapass == 2)
2516 : {
2517 0 : if(ghasstencil)
2518 : {
2519 0 : glStencilFunc(GL_EQUAL, stencilref = stencilmask|0x08, avatar ? 0x4F : 0x0F);
2520 : }
2521 0 : if(msaalight==2)
2522 : {
2523 0 : glSampleMaski(0, 2); glEnable(GL_SAMPLE_MASK);
2524 : }
2525 : }
2526 0 : else if(ghasstencil && (stencilmask || avatar))
2527 : {
2528 0 : if(!stencilmask)
2529 : {
2530 0 : glEnable(GL_STENCIL_TEST);
2531 : }
2532 0 : glStencilFunc(GL_EQUAL, stencilref = stencilmask, avatar ? 0x4F : 0x0F);
2533 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2534 : }
2535 :
2536 0 : if(!avatar)
2537 : {
2538 0 : stencilref = -1;
2539 : }
2540 :
2541 0 : glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2542 0 : glEnable(GL_BLEND);
2543 :
2544 0 : if(hasDBT && depthtestlights > 1)
2545 : {
2546 0 : glEnable(GL_DEPTH_BOUNDS_TEST_EXT);
2547 : }
2548 :
2549 0 : bool sunpass = !lighttilebatch || drawtex == Draw_TexMinimap || (!sunlight.iszero() && csm.getcsmproperty(cascadedshadowmap::ShadowMap) && batchsunlight <= (gi && giscale && gidist ? 1 : 0));
2550 0 : if(sunpass)
2551 : {
2552 0 : if(depthtestlights && depth)
2553 : {
2554 0 : glDisable(GL_DEPTH_TEST);
2555 0 : depth = false;
2556 : }
2557 0 : rendersunpass(s, stencilref, transparent, bsx1, bsy1, bsx2, bsy2, tilemask);
2558 : }
2559 :
2560 0 : if(depthtestlights && !depth)
2561 : {
2562 0 : glEnable(GL_DEPTH_TEST);
2563 0 : depth = true;
2564 : }
2565 :
2566 0 : if(!lighttilebatch || drawtex == Draw_TexMinimap)
2567 : {
2568 0 : renderlightsnobatch(s, stencilref, transparent, bsx1, bsy1, bsx2, bsy2);
2569 : }
2570 : else
2571 : {
2572 0 : renderlightbatches(*s, stencilref, transparent, bsx1, bsy1, bsx2, bsy2, tilemask);
2573 : }
2574 :
2575 0 : if(msaapass == 1 && ghasstencil)
2576 : {
2577 0 : if(msaalight==2 && !stencilmask && !avatar)
2578 : {
2579 0 : glEnable(GL_STENCIL_TEST);
2580 : }
2581 : }
2582 0 : else if(msaapass == 2)
2583 : {
2584 0 : if(ghasstencil && !stencilmask)
2585 : {
2586 0 : glDisable(GL_STENCIL_TEST);
2587 : }
2588 0 : if(msaalight==2)
2589 : {
2590 0 : glDisable(GL_SAMPLE_MASK);
2591 : }
2592 : }
2593 0 : else if(avatar && !stencilmask)
2594 : {
2595 0 : glDisable(GL_STENCIL_TEST);
2596 : }
2597 :
2598 0 : glDisable(GL_BLEND);
2599 :
2600 0 : if(!depthtestlights)
2601 : {
2602 0 : glEnable(GL_DEPTH_TEST);
2603 : }
2604 : else
2605 : {
2606 0 : glDepthMask(GL_TRUE);
2607 0 : if(hasDBT && depthtestlights > 1)
2608 : {
2609 0 : glDisable(GL_DEPTH_BOUNDS_TEST_EXT);
2610 : }
2611 : }
2612 : }
2613 :
2614 0 : void GBuffer::rendervolumetric()
2615 : {
2616 0 : if(!volumetric || !volumetriclights || !volscale)
2617 : {
2618 0 : return;
2619 : }
2620 0 : float bsx1 = 1,
2621 0 : bsy1 = 1,
2622 0 : bsx2 = -1,
2623 0 : bsy2 = -1;
2624 0 : for(size_t i = 0; i < lightorder.size(); i++)
2625 : {
2626 0 : const lightinfo &l = lights[lightorder[i]];
2627 0 : if(!l.volumetric() || l.checkquery())
2628 : {
2629 0 : continue;
2630 : }
2631 :
2632 0 : l.addscissor(bsx1, bsy1, bsx2, bsy2);
2633 : }
2634 0 : if(bsx1 >= bsx2 || bsy1 >= bsy2)
2635 : {
2636 0 : return;
2637 : }
2638 :
2639 0 : timer *voltimer = begintimer("volumetric lights");
2640 :
2641 0 : glBindFramebuffer(GL_FRAMEBUFFER, volfbo[0]);
2642 0 : glViewport(0, 0, volw, volh);
2643 :
2644 0 : glClearColor(0, 0, 0, 0);
2645 0 : glClear(GL_COLOR_BUFFER_BIT);
2646 :
2647 0 : glActiveTexture(GL_TEXTURE3);
2648 0 : if(msaalight)
2649 : {
2650 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
2651 : }
2652 : else
2653 : {
2654 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
2655 : }
2656 0 : glActiveTexture(GL_TEXTURE4);
2657 0 : shadowatlas.bind();
2658 0 : shadowatlas.setcomparemode();
2659 0 : glActiveTexture(GL_TEXTURE0);
2660 0 : vec2 sasize = shadowatlaspacker.dimensions();
2661 0 : GLOBALPARAMF(shadowatlasscale, 1.0f/sasize.x, 1.0f/sasize.y);
2662 0 : GLOBALPARAMF(volscale, static_cast<float>(vieww)/volw, static_cast<float>(viewh)/volh, static_cast<float>(volw)/vieww, static_cast<float>(volh)/viewh);
2663 0 : GLOBALPARAMF(volminstep, volminstep);
2664 0 : GLOBALPARAMF(volprefilter, volprefilter);
2665 0 : GLOBALPARAMF(voldistclamp, farplane*voldistclamp);
2666 :
2667 0 : glBlendFunc(GL_ONE, GL_ONE);
2668 0 : glEnable(GL_BLEND);
2669 :
2670 0 : if(!depthtestlights)
2671 : {
2672 0 : glDisable(GL_DEPTH_TEST);
2673 : }
2674 : else
2675 : {
2676 0 : glDepthMask(GL_FALSE);
2677 : }
2678 :
2679 0 : lightsphere::enable();
2680 :
2681 0 : glEnable(GL_SCISSOR_TEST);
2682 :
2683 0 : bool outside = true;
2684 0 : for(size_t i = 0; i < lightorder.size(); i++)
2685 : {
2686 0 : const lightinfo &l = lights[lightorder[i]];
2687 0 : if(!l.volumetric() || l.checkquery())
2688 : {
2689 0 : continue;
2690 : }
2691 :
2692 0 : matrix4 lightmatrix = camprojmatrix;
2693 0 : lightmatrix.translate(l.o);
2694 0 : lightmatrix.scale(l.radius);
2695 0 : GLOBALPARAM(lightmatrix, lightmatrix);
2696 :
2697 0 : if(l.spot > 0)
2698 : {
2699 0 : volumetricshader->setvariant(0, l.shadowmap >= 0 ? 2 : 1);
2700 0 : LOCALPARAM(spotparams, vec4<float>(l.dir, 1/(1 - cos360(l.spot))));
2701 : }
2702 0 : else if(l.shadowmap >= 0)
2703 : {
2704 0 : volumetricshader->setvariant(0, 0);
2705 : }
2706 : else
2707 : {
2708 0 : volumetricshader->set();
2709 : }
2710 :
2711 0 : LOCALPARAM(lightpos, vec4<float>(l.o, 1).div(l.radius));
2712 0 : vec color = vec(l.color).mul(ldrscaleb()).mul(volcolor.tocolor().mul(volscale));
2713 0 : LOCALPARAM(lightcolor, color);
2714 :
2715 0 : if(l.shadowmap >= 0)
2716 : {
2717 0 : shadowmapinfo &sm = shadowmaps[l.shadowmap];
2718 0 : float smnearclip = SQRT3 / l.radius,
2719 0 : smfarclip = SQRT3,
2720 0 : bias = (smfilter > 2 ? smbias2 : smbias) * (smcullside ? 1 : -1) * smnearclip * (1024.0f / sm.size);
2721 0 : int border = smfilter > 2 ? smborder2 : smborder;
2722 0 : if(l.spot > 0)
2723 : {
2724 0 : LOCALPARAMF(shadowparams,
2725 : 0.5f * sm.size * cotan360(l.spot),
2726 : (-smnearclip * smfarclip / (smfarclip - smnearclip) - 0.5f*bias),
2727 : 1 / (1 + std::fabs(l.dir.z)),
2728 : 0.5f + 0.5f * (smfarclip + smnearclip) / (smfarclip - smnearclip));
2729 : }
2730 : else
2731 : {
2732 0 : LOCALPARAMF(shadowparams,
2733 : 0.5f * (sm.size - border),
2734 : -smnearclip * smfarclip / (smfarclip - smnearclip) - 0.5f*bias,
2735 : sm.size,
2736 : 0.5f + 0.5f * (smfarclip + smnearclip) / (smfarclip - smnearclip));
2737 : }
2738 0 : LOCALPARAMF(shadowoffset, sm.x + 0.5f*sm.size, sm.y + 0.5f*sm.size);
2739 : }
2740 :
2741 0 : int tx1 = static_cast<int>(std::floor((l.sx1*0.5f+0.5f)*volw)),
2742 0 : ty1 = static_cast<int>(std::floor((l.sy1*0.5f+0.5f)*volh)),
2743 0 : tx2 = static_cast<int>(std::ceil((l.sx2*0.5f+0.5f)*volw)),
2744 0 : ty2 = static_cast<int>(std::ceil((l.sy2*0.5f+0.5f)*volh));
2745 0 : glScissor(tx1, ty1, tx2-tx1, ty2-ty1);
2746 :
2747 0 : if(camera1->o.dist(l.o) <= l.radius + nearplane + 1 && depthfaillights)
2748 : {
2749 0 : if(outside)
2750 : {
2751 0 : outside = false;
2752 0 : if(depthtestlights)
2753 : {
2754 0 : glDisable(GL_DEPTH_TEST);
2755 : }
2756 0 : glCullFace(GL_FRONT);
2757 : }
2758 : }
2759 0 : else if(!outside)
2760 : {
2761 0 : outside = true;
2762 0 : if(depthtestlights)
2763 : {
2764 0 : glEnable(GL_DEPTH_TEST);
2765 : }
2766 0 : glCullFace(GL_BACK);
2767 : }
2768 :
2769 0 : lightsphere::draw();
2770 : }
2771 :
2772 0 : if(!outside)
2773 : {
2774 0 : outside = true;
2775 0 : glCullFace(GL_BACK);
2776 : }
2777 :
2778 0 : lightsphere::disable();
2779 :
2780 0 : if(depthtestlights)
2781 : {
2782 0 : glDepthMask(GL_TRUE);
2783 :
2784 0 : glDisable(GL_DEPTH_TEST);
2785 : }
2786 :
2787 0 : int cx1 = static_cast<int>(std::floor((bsx1*0.5f+0.5f)*volw))&~1,
2788 0 : cy1 = static_cast<int>(std::floor((bsy1*0.5f+0.5f)*volh))&~1,
2789 0 : cx2 = (static_cast<int>(std::ceil((bsx2*0.5f+0.5f)*volw))&~1) + 2,
2790 0 : cy2 = (static_cast<int>(std::ceil((bsy2*0.5f+0.5f)*volh))&~1) + 2;
2791 0 : if(volbilateral || volblur)
2792 : {
2793 0 : int radius = (volbilateral ? volbilateral : volblur)*2;
2794 0 : cx1 = std::max(cx1 - radius, 0);
2795 0 : cy1 = std::max(cy1 - radius, 0);
2796 0 : cx2 = std::min(cx2 + radius, volw);
2797 0 : cy2 = std::min(cy2 + radius, volh);
2798 0 : glScissor(cx1, cy1, cx2-cx1, cy2-cy1);
2799 :
2800 0 : glDisable(GL_BLEND);
2801 :
2802 0 : if(volbilateral)
2803 : {
2804 0 : for(int i = 0; i < 2; ++i)
2805 : {
2806 0 : glBindFramebuffer(GL_FRAMEBUFFER, volfbo[(i+1)%2]);
2807 0 : glViewport(0, 0, volw, volh);
2808 0 : volumetricbilateralshader[i]->set();
2809 0 : setbilateralparams(volbilateral, volbilateraldepth);
2810 0 : glBindTexture(GL_TEXTURE_RECTANGLE, voltex[i%2]);
2811 0 : screenquadoffset(0.25f, 0.25f, vieww, viewh);
2812 : }
2813 : }
2814 : else
2815 : {
2816 : std::array<float, maxblurradius+1> blurweights,
2817 : bluroffsets;
2818 0 : setupblurkernel(volblur, blurweights.data(), bluroffsets.data());
2819 0 : for(int i = 0; i < 2; ++i)
2820 : {
2821 0 : glBindFramebuffer(GL_FRAMEBUFFER, volfbo[(i+1)%2]);
2822 0 : glViewport(0, 0, volw, volh);
2823 0 : setblurshader(i%2, 1, volblur, blurweights.data(), bluroffsets.data(), GL_TEXTURE_RECTANGLE);
2824 0 : glBindTexture(GL_TEXTURE_RECTANGLE, voltex[i%2]);
2825 0 : screenquad(volw, volh);
2826 : }
2827 : }
2828 :
2829 0 : glEnable(GL_BLEND);
2830 : }
2831 :
2832 0 : glBindFramebuffer(GL_FRAMEBUFFER, msaalight ? mshdrfbo : hdrfbo);
2833 0 : glViewport(0, 0, vieww, viewh);
2834 :
2835 0 : int margin = (1<<volreduce) - 1;
2836 0 : cx1 = std::max((cx1 * vieww) / volw - margin, 0);
2837 0 : cy1 = std::max((cy1 * viewh) / volh - margin, 0);
2838 0 : cx2 = std::min((cx2 * vieww + margin + volw - 1) / volw, vieww);
2839 0 : cy2 = std::min((cy2 * viewh + margin + volh - 1) / volh, viewh);
2840 0 : glScissor(cx1, cy1, cx2-cx1, cy2-cy1);
2841 :
2842 0 : bool avatar = useavatarmask();
2843 0 : if(avatar)
2844 : {
2845 0 : glStencilFunc(GL_EQUAL, 0, 0x40);
2846 0 : glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2847 0 : glEnable(GL_STENCIL_TEST);
2848 : }
2849 :
2850 0 : SETSHADER(scalelinear);
2851 0 : glBindTexture(GL_TEXTURE_RECTANGLE, voltex[0]);
2852 0 : screenquad(volw, volh);
2853 :
2854 0 : if(volbilateral || volblur)
2855 : {
2856 0 : std::swap(volfbo[0], volfbo[1]);
2857 0 : std::swap(voltex[0], voltex[1]);
2858 : }
2859 :
2860 0 : if(avatar)
2861 : {
2862 0 : glDisable(GL_STENCIL_TEST);
2863 : }
2864 :
2865 0 : glDisable(GL_SCISSOR_TEST);
2866 :
2867 0 : glEnable(GL_DEPTH_TEST);
2868 :
2869 0 : glDisable(GL_BLEND);
2870 :
2871 0 : endtimer(voltimer);
2872 : }
2873 :
2874 : static VAR(oqvol, 0, 1, 1); //`o`cclusion `q`uery `vol`umetrics: toggles occlusion queries of volumetric lights
2875 : static VAR(oqlights, 0, 1, 1); //`o`cclusion `q`uery `lights: toggles occlusion queries of lights behind geometry
2876 : static VAR(debuglightscissor, 0, 0, 1); //displays the light scissor map in the corner of the screen
2877 :
2878 0 : static void viewlightscissor()
2879 : {
2880 0 : std::vector<extentity *> &ents = entities::getents();
2881 0 : gle::defvertex(2);
2882 0 : for(size_t i = 0; i < entgroup.size(); i++)
2883 : {
2884 0 : int idx = entgroup[i];
2885 0 : if((static_cast<long>(ents.size()) > idx) && ents[idx]->type == EngineEnt_Light)
2886 : {
2887 0 : extentity &e = *ents[idx];
2888 0 : for(size_t j = 0; j < lights.size(); j++)
2889 : {
2890 0 : if(lights[j].o == e.o)
2891 : {
2892 0 : lightinfo &l = lights[j];
2893 0 : if(!l.validscissor())
2894 : {
2895 0 : break;
2896 : }
2897 0 : gle::colorf(l.color.x/255, l.color.y/255, l.color.z/255);
2898 0 : float x1 = (l.sx1+1)/2*hudw(),
2899 0 : x2 = (l.sx2+1)/2*hudw(),
2900 0 : y1 = (1-l.sy1)/2*hudh(),
2901 0 : y2 = (1-l.sy2)/2*hudh();
2902 0 : gle::begin(GL_TRIANGLE_STRIP);
2903 0 : gle::attribf(x1, y1);
2904 0 : gle::attribf(x2, y1);
2905 0 : gle::attribf(x1, y2);
2906 0 : gle::attribf(x2, y2);
2907 0 : gle::end();
2908 : }
2909 : }
2910 : }
2911 : }
2912 0 : }
2913 :
2914 0 : void collectlights()
2915 : {
2916 0 : if(lights.size())
2917 : {
2918 0 : return;
2919 : }
2920 :
2921 : // point lights processed here
2922 0 : const std::vector<extentity *> &ents = entities::getents();
2923 0 : if(!editmode || !fullbright)
2924 : {
2925 0 : for(size_t i = 0; i < ents.size(); i++)
2926 : {
2927 0 : const extentity *e = ents[i];
2928 0 : if(e->type != EngineEnt_Light || e->attr1 <= 0)
2929 : {
2930 0 : continue;
2931 : }
2932 0 : if(smviscull && view.isfoggedsphere(e->attr1, e->o))
2933 : {
2934 0 : continue;
2935 : }
2936 0 : lightinfo l = lightinfo(i, *e);
2937 0 : lights.push_back(l);
2938 0 : if(l.validscissor())
2939 : {
2940 0 : lightorder.emplace_back(lights.size()-1);
2941 : }
2942 : }
2943 : }
2944 :
2945 0 : size_t numdynlights = 0;
2946 0 : if(!drawtex)
2947 : {
2948 0 : updatedynlights();
2949 0 : numdynlights = finddynlights();
2950 : }
2951 0 : for(size_t i = 0; i < numdynlights; ++i)
2952 : {
2953 0 : vec o, color, dir;
2954 : float radius;
2955 : int spot, flags;
2956 0 : if(!getdynlight(i, o, radius, color, dir, spot, flags))
2957 : {
2958 0 : continue;
2959 : }
2960 0 : const lightinfo &l = lights.emplace_back(lightinfo(o, vec(color).mul(255).max(0), radius, flags, dir, spot));
2961 0 : if(l.validscissor())
2962 : {
2963 0 : lightorder.emplace_back(lights.size()-1);
2964 : }
2965 : }
2966 :
2967 0 : std::sort(lightorder.begin(), lightorder.end(), sortlights);
2968 :
2969 0 : bool queried = false;
2970 0 : if(!drawtex && smquery && oqfrags && oqlights)
2971 : {
2972 0 : for(size_t i = 0; i < lightorder.size(); i++)
2973 : {
2974 0 : int idx = lightorder[i];
2975 0 : lightinfo &l = lights[idx];
2976 0 : if((l.noshadow() && (!oqvol || !l.volumetric())) || l.radius >= rootworld.mapsize())
2977 : {
2978 0 : continue;
2979 : }
2980 0 : vec bbmin, bbmax;
2981 0 : l.calcbb(bbmin, bbmax);
2982 0 : if(!camera1->o.insidebb(bbmin, bbmax, 2))
2983 : {
2984 0 : l.query = occlusionengine.newquery(&l);
2985 0 : if(l.query)
2986 : {
2987 0 : if(!queried)
2988 : {
2989 0 : startbb(false);
2990 0 : queried = true;
2991 : }
2992 0 : l.query->startquery();
2993 0 : ivec bo(bbmin),
2994 0 : br = ivec(bbmax).sub(bo).add(1);
2995 0 : drawbb(bo, br);
2996 0 : occlusionengine.endquery();
2997 : }
2998 : }
2999 : }
3000 : }
3001 0 : if(queried)
3002 : {
3003 0 : endbb(false);
3004 0 : glFlush();
3005 : }
3006 :
3007 0 : smused = 0;
3008 :
3009 0 : if(smcache && !smnoshadow && shadowatlas.cache.size())
3010 : {
3011 0 : for(int mismatched = 0; mismatched < 2; ++mismatched)
3012 : {
3013 0 : for(size_t i = 0; i < lightorder.size(); i++)
3014 : {
3015 0 : int idx = lightorder[i];
3016 0 : lightinfo &l = lights[idx];
3017 0 : if(l.noshadow())
3018 : {
3019 0 : continue;
3020 : }
3021 0 : auto itr = shadowatlas.cache.find(l);
3022 0 : if(itr == shadowatlas.cache.end())
3023 : {
3024 0 : continue;
3025 : }
3026 0 : float prec = smprec,
3027 : lod;
3028 : int w, h;
3029 0 : if(l.spot)
3030 : {
3031 0 : w = 1;
3032 0 : h = 1;
3033 0 : prec *= tan360(l.spot);
3034 0 : lod = smspotprec;
3035 : }
3036 : else
3037 : {
3038 0 : w = 3;
3039 0 : h = 2;
3040 0 : lod = smcubeprec;
3041 : }
3042 0 : lod *= std::clamp(l.radius * prec / sqrtf(std::max(1.0f, l.dist/l.radius)), static_cast<float>(smminsize), static_cast<float>(smmaxsize));
3043 0 : const float sasizex = shadowatlaspacker.dimensions().x;
3044 0 : int size = std::clamp(static_cast<int>(std::ceil((lod * sasizex) / shadowatlassize)), 1, static_cast<int>(sasizex) / w);
3045 0 : w *= size;
3046 0 : h *= size;
3047 0 : const shadowcacheval &cached = (*itr).second;
3048 0 : if(mismatched)
3049 : {
3050 0 : if(cached.size == size)
3051 : {
3052 0 : continue;
3053 : }
3054 0 : ushort x = USHRT_MAX,
3055 0 : y = USHRT_MAX;
3056 0 : if(!shadowatlaspacker.insert(x, y, w, h))
3057 : {
3058 0 : continue;
3059 : }
3060 0 : addshadowmap(x, y, size, l.shadowmap, idx);
3061 : }
3062 : else
3063 : {
3064 0 : if(cached.size != size)
3065 : {
3066 0 : continue;
3067 : }
3068 0 : ushort x = cached.x,
3069 0 : y = cached.y;
3070 0 : shadowatlaspacker.reserve(x, y, w, h);
3071 0 : addshadowmap(x, y, size, l.shadowmap, idx, &cached);
3072 : }
3073 0 : smused += w*h;
3074 : }
3075 : }
3076 : }
3077 : }
3078 :
3079 : static VAR(csminoq, 0, 1, 1); //cascaded shadow maps in occlusion queries
3080 : static VAR(sminoq, 0, 1, 1); //shadow maps in occlusion queries
3081 : VAR(rhinoq, 0, 1, 1); //radiance hints in occlusion queries
3082 :
3083 0 : bool shouldworkinoq()
3084 : {
3085 0 : return !drawtex && oqfrags && (!wireframe || !editmode);
3086 : }
3087 :
3088 : struct BatchRect final : lightrect
3089 : {
3090 : uchar group;
3091 : ushort idx;
3092 :
3093 : BatchRect() {}
3094 0 : BatchRect(const lightinfo &l, ushort idx)
3095 0 : : lightrect(l),
3096 0 : group((l.shadowmap < 0 ? BatchFlag_NoShadow : 0) | (l.spot > 0 ? BatchFlag_Spotlight : 0)),
3097 0 : idx(idx)
3098 0 : {}
3099 : };
3100 :
3101 : struct batchstack final : lightrect
3102 : {
3103 : ushort offset, numrects;
3104 : uchar flags;
3105 :
3106 : batchstack() {}
3107 0 : batchstack(uchar x1, uchar y1, uchar x2, uchar y2, ushort offset, ushort numrects, uchar flags = 0) : lightrect(x1, y1, x2, y2), offset(offset), numrects(numrects), flags(flags) {}
3108 : };
3109 :
3110 0 : static void batchlights(const batchstack &initstack, std::vector<BatchRect> &batchrects, int &lightbatchstacksused, int &lightbatchrectsused)
3111 : {
3112 0 : constexpr size_t stacksize = 32;
3113 0 : std::stack<batchstack> stack;
3114 0 : stack.push(initstack);
3115 :
3116 0 : while(stack.size() > 0)
3117 : {
3118 0 : const batchstack s = stack.top();
3119 0 : stack.pop();
3120 0 : if(stack.size() + 5 > stacksize)
3121 : {
3122 0 : batchlights(s, batchrects, lightbatchstacksused, lightbatchrectsused);
3123 0 : continue;
3124 : }
3125 0 : ++lightbatchstacksused;
3126 0 : int groups[BatchFlag_NoSun] = { 0 };
3127 0 : lightrect split(s);
3128 0 : ushort splitidx = USHRT_MAX;
3129 0 : int outside = s.offset,
3130 0 : inside = s.offset + s.numrects;
3131 0 : for(int i = outside; i < inside; ++i)
3132 : {
3133 0 : const BatchRect &r = batchrects[i];
3134 0 : if(r.outside(s))
3135 : {
3136 0 : if(i != outside)
3137 : {
3138 0 : std::swap(batchrects[i], batchrects[outside]);
3139 : }
3140 0 : ++outside;
3141 : }
3142 0 : else if(s.inside(r))
3143 : {
3144 0 : ++groups[r.group];
3145 0 : std::swap(batchrects[i--], batchrects[--inside]);
3146 : }
3147 0 : else if(r.idx < splitidx)
3148 : {
3149 0 : split = r;
3150 0 : splitidx = r.idx;
3151 : }
3152 : }
3153 :
3154 0 : uchar flags = s.flags;
3155 0 : int batched = s.offset + s.numrects;
3156 0 : for(int g = 0; g < BatchFlag_NoShadow; ++g)
3157 : {
3158 0 : while(groups[g] >= lighttilebatch || (inside == outside && (groups[g] || !(flags & BatchFlag_NoSun))))
3159 : {
3160 0 : lightbatch key;
3161 0 : key.flags = flags | g;
3162 0 : flags |= BatchFlag_NoSun;
3163 :
3164 0 : int n = std::min(groups[g], lighttilebatch);
3165 0 : groups[g] -= n;
3166 0 : key.numlights = n;
3167 0 : for(int i = 0; i < n; ++i)
3168 : {
3169 0 : int best = -1;
3170 0 : ushort bestidx = USHRT_MAX;
3171 0 : for(int j = inside; j < batched; ++j)
3172 : {
3173 0 : const BatchRect &r = batchrects[j];
3174 : {
3175 0 : if(r.group == g && r.idx < bestidx)
3176 : {
3177 0 : best = j;
3178 0 : bestidx = r.idx;
3179 : }
3180 : }
3181 : }
3182 0 : key.lights[i] = lightorder[bestidx];
3183 0 : std::swap(batchrects[best], batchrects[--batched]);
3184 : }
3185 :
3186 0 : key.rects.push_back(s);
3187 0 : lightbatches.push_back(std::move(key));
3188 0 : ++lightbatchrectsused;
3189 0 : }
3190 : }
3191 0 : if(splitidx != USHRT_MAX)
3192 : {
3193 0 : int numoverlap = batched - outside;
3194 0 : split.intersect(s);
3195 :
3196 0 : if(split.y1 > s.y1)
3197 : {
3198 0 : stack.push(batchstack(s.x1, s.y1, s.x2, split.y1, outside, numoverlap, flags));
3199 : }
3200 0 : if(split.x1 > s.x1)
3201 : {
3202 0 : stack.push(batchstack(s.x1, split.y1, split.x1, split.y2, outside, numoverlap, flags));
3203 : }
3204 0 : stack.push(batchstack(split.x1, split.y1, split.x2, split.y2, outside, numoverlap, flags));
3205 0 : if(split.x2 < s.x2)
3206 : {
3207 0 : stack.push(batchstack(split.x2, split.y1, s.x2, split.y2, outside, numoverlap, flags));
3208 : }
3209 0 : if(split.y2 < s.y2)
3210 : {
3211 0 : stack.push(batchstack(s.x1, split.y2, s.x2, s.y2, outside, numoverlap, flags));
3212 : }
3213 : }
3214 : }
3215 0 : }
3216 :
3217 0 : static bool sortlightbatches(const lightbatch &x, const lightbatch &y)
3218 : {
3219 0 : if(x.flags < y.flags)
3220 : {
3221 0 : return true;
3222 : }
3223 0 : if(x.flags > y.flags)
3224 : {
3225 0 : return false;
3226 : }
3227 0 : return x.numlights > y.numlights;
3228 : }
3229 :
3230 0 : static void batchlights(std::vector<BatchRect> &batchrects, int &lightbatchstacksused, int &lightbatchrectsused, int &lightbatchesused)
3231 : {
3232 0 : lightbatches.clear();
3233 0 : lightbatchstacksused = 0;
3234 0 : lightbatchrectsused = 0;
3235 :
3236 0 : if(lighttilebatch && drawtex != Draw_TexMinimap)
3237 : {
3238 0 : batchlights(batchstack(0, 0, lighttilew, lighttileh, 0, batchrects.size()), batchrects, lightbatchstacksused, lightbatchrectsused);
3239 0 : std::sort(lightbatches.begin(), lightbatches.end(), sortlightbatches);
3240 : }
3241 :
3242 0 : lightbatchesused = lightbatches.size();
3243 0 : }
3244 :
3245 0 : void GBuffer::packlights()
3246 : {
3247 0 : lightsvisible = lightsoccluded = 0;
3248 0 : lightpassesused = 0;
3249 0 : std::vector<BatchRect> batchrects;
3250 :
3251 0 : for(size_t i = 0; i < lightorder.size(); i++)
3252 : {
3253 0 : int idx = lightorder[i];
3254 0 : lightinfo &l = lights[idx];
3255 0 : if(l.checkquery())
3256 : {
3257 0 : if(l.shadowmap >= 0)
3258 : {
3259 0 : shadowmaps[l.shadowmap].light = -1;
3260 0 : l.shadowmap = -1;
3261 : }
3262 0 : lightsoccluded++;
3263 0 : continue;
3264 : }
3265 :
3266 0 : if(!l.noshadow() && !smnoshadow && l.shadowmap < 0)
3267 : {
3268 0 : float prec = smprec,
3269 : lod;
3270 : int w, h;
3271 0 : if(l.spot)
3272 : {
3273 0 : w = 1;
3274 0 : h = 1;
3275 0 : prec *= tan360(l.spot);
3276 0 : lod = smspotprec;
3277 : }
3278 : else
3279 : {
3280 0 : w = 3;
3281 0 : h = 2;
3282 0 : lod = smcubeprec;
3283 : }
3284 0 : lod *= std::clamp(l.radius * prec / sqrtf(std::max(1.0f, l.dist/l.radius)), static_cast<float>(smminsize), static_cast<float>(smmaxsize));
3285 0 : const float sasizex = shadowatlaspacker.dimensions().x;
3286 0 : int size = std::clamp(static_cast<int>(std::ceil((lod * sasizex) / shadowatlassize)), 1, static_cast<int>(sasizex) / w);
3287 0 : w *= size;
3288 0 : h *= size;
3289 0 : ushort x = USHRT_MAX,
3290 0 : y = USHRT_MAX;
3291 0 : if(shadowatlaspacker.insert(x, y, w, h))
3292 : {
3293 0 : addshadowmap(x, y, size, l.shadowmap, idx);
3294 0 : smused += w*h;
3295 : }
3296 0 : else if(smcache)
3297 : {
3298 0 : shadowatlas.full = true;
3299 : }
3300 : }
3301 0 : batchrects.emplace_back(l, i);
3302 : }
3303 :
3304 0 : lightsvisible = lightorder.size() - lightsoccluded;
3305 :
3306 0 : batchlights(batchrects, lightbatchstacksused, lightbatchrectsused, lightbatchesused);
3307 0 : }
3308 :
3309 0 : void GBuffer::rendercsmshadowmaps() const
3310 : {
3311 0 : if(sunlight.iszero() || !csm.getcsmproperty(cascadedshadowmap::ShadowMap))
3312 : {
3313 0 : return;
3314 : }
3315 0 : if(inoq)
3316 : {
3317 0 : glBindFramebuffer(GL_FRAMEBUFFER, shadowatlas.fbo);
3318 0 : glDepthMask(GL_TRUE);
3319 : }
3320 0 : csm.setup();
3321 0 : shadowmapping = ShadowMap_Cascade;
3322 0 : shadoworigin = vec(0, 0, 0);
3323 0 : shadowdir = csm.lightview;
3324 0 : shadowbias = csm.lightview.project_bb(worldmin, worldmax);
3325 0 : shadowradius = std::fabs(csm.lightview.project_bb(worldmax, worldmin));
3326 :
3327 0 : float polyfactor = csm.getcsmproperty(cascadedshadowmap::PolyFactor),
3328 0 : polyoffset = csm.getcsmproperty(cascadedshadowmap::PolyOffset);
3329 0 : if(smfilter > 2)
3330 : {
3331 0 : csm.setcsmproperty(cascadedshadowmap::PolyFactor, csm.getcsmproperty(cascadedshadowmap::PolyFactor2));
3332 0 : csm.setcsmproperty(cascadedshadowmap::PolyOffset, csm.getcsmproperty(cascadedshadowmap::PolyOffset2));
3333 : }
3334 0 : if(polyfactor || polyoffset)
3335 : {
3336 0 : glPolygonOffset(polyfactor, polyoffset);
3337 0 : glEnable(GL_POLYGON_OFFSET_FILL);
3338 : }
3339 0 : glEnable(GL_SCISSOR_TEST);
3340 :
3341 0 : findshadowvas();
3342 0 : findshadowmms();
3343 :
3344 0 : batching::shadowmaskbatchedmodels(smdynshadow!=0);
3345 0 : batchshadowmapmodels();
3346 :
3347 0 : for(int i = 0; i < csm.getcsmproperty(cascadedshadowmap::Splits); ++i)
3348 : {
3349 0 : if(csm.splits[i].idx >= 0)
3350 : {
3351 0 : const shadowmapinfo &sm = shadowmaps[csm.splits[i].idx];
3352 :
3353 0 : shadowmatrix.mul(csm.splits[i].proj, csm.model);
3354 0 : GLOBALPARAM(shadowmatrix, shadowmatrix);
3355 :
3356 0 : glViewport(sm.x, sm.y, sm.size, sm.size);
3357 0 : glScissor(sm.x, sm.y, sm.size, sm.size);
3358 0 : glClear(GL_DEPTH_BUFFER_BIT);
3359 :
3360 0 : shadowside = i;
3361 :
3362 0 : rendershadowmapworld();
3363 0 : batching::rendershadowmodelbatches();
3364 : }
3365 : }
3366 :
3367 0 : batching::clearbatchedmapmodels();
3368 :
3369 0 : glDisable(GL_SCISSOR_TEST);
3370 :
3371 0 : if(polyfactor || polyoffset)
3372 : {
3373 0 : glDisable(GL_POLYGON_OFFSET_FILL);
3374 : }
3375 0 : shadowmapping = 0;
3376 :
3377 0 : if(inoq)
3378 : {
3379 0 : glBindFramebuffer(GL_FRAMEBUFFER, msaasamples ? msfbo : gfbo);
3380 0 : glViewport(0, 0, vieww, viewh);
3381 :
3382 0 : glFlush();
3383 : }
3384 : }
3385 :
3386 0 : int calcshadowinfo(const extentity &e, vec &origin, float &radius, vec &spotloc, int &spotangle, float &bias)
3387 : {
3388 0 : if(e.attr5&LightEnt_NoShadow || e.attr1 <= smminradius)
3389 : {
3390 0 : return ShadowMap_None;
3391 : }
3392 0 : origin = e.o;
3393 0 : radius = e.attr1;
3394 : int type, w, border;
3395 : float lod;
3396 0 : if(e.attached && e.attached->type == EngineEnt_Spotlight)
3397 : {
3398 0 : type = ShadowMap_Spot;
3399 0 : w = 1;
3400 0 : border = 0;
3401 0 : lod = smspotprec;
3402 0 : spotloc = e.attached->o;
3403 0 : spotangle = std::clamp(static_cast<int>(e.attached->attr1), 1, 89);
3404 : }
3405 : else
3406 : {
3407 0 : type = ShadowMap_CubeMap;
3408 0 : w = 3;
3409 0 : lod = smcubeprec;
3410 0 : border = smfilter > 2 ? smborder2 : smborder;
3411 0 : spotloc = e.o;
3412 0 : spotangle = 0;
3413 : }
3414 :
3415 0 : lod *= smminsize;
3416 0 : const float sasizex = shadowatlaspacker.dimensions().x;
3417 0 : int size = std::clamp(static_cast<int>(std::ceil((lod * sasizex) / shadowatlassize)), 1, static_cast<int>(sasizex) / w);
3418 0 : bias = border / static_cast<float>(size - border);
3419 :
3420 0 : return type;
3421 : }
3422 :
3423 : matrix4 shadowmatrix;
3424 :
3425 0 : void GBuffer::rendershadowmaps(int offset) const
3426 : {
3427 0 : if(!(sminoq && !debugshadowatlas && !inoq && shouldworkinoq()))
3428 : {
3429 0 : offset = 0;
3430 : }
3431 :
3432 0 : for(; offset < static_cast<int>(shadowmaps.size()); offset++)
3433 : {
3434 0 : if(shadowmaps[offset].light >= 0)
3435 : {
3436 0 : break;
3437 : }
3438 : }
3439 :
3440 0 : if(offset >= static_cast<int>(shadowmaps.size()))
3441 : {
3442 0 : return;
3443 : }
3444 :
3445 0 : if(inoq)
3446 : {
3447 0 : glBindFramebuffer(GL_FRAMEBUFFER, shadowatlas.fbo);
3448 0 : glDepthMask(GL_TRUE);
3449 : }
3450 :
3451 0 : float polyfactor = smpolyfactor,
3452 0 : polyoffset = smpolyoffset;
3453 0 : if(smfilter > 2)
3454 : {
3455 0 : polyfactor = smpolyfactor2;
3456 0 : polyoffset = smpolyoffset2;
3457 : }
3458 0 : if(polyfactor || polyoffset)
3459 : {
3460 0 : glPolygonOffset(polyfactor, polyoffset);
3461 0 : glEnable(GL_POLYGON_OFFSET_FILL);
3462 : }
3463 :
3464 0 : glEnable(GL_SCISSOR_TEST);
3465 :
3466 0 : const std::vector<extentity *> &ents = entities::getents();
3467 0 : for(size_t i = offset; i < shadowmaps.size(); i++)
3468 : {
3469 0 : shadowmapinfo &sm = shadowmaps[i];
3470 0 : if(sm.light < 0)
3471 : {
3472 0 : continue;
3473 : }
3474 0 : const lightinfo &l = lights[sm.light];
3475 0 : const extentity *e = l.ent >= 0 ? ents[l.ent] : nullptr;
3476 : int border, sidemask;
3477 0 : if(l.spot)
3478 : {
3479 0 : shadowmapping = ShadowMap_Spot;
3480 0 : border = 0;
3481 0 : sidemask = 1;
3482 : }
3483 : else
3484 : {
3485 0 : shadowmapping = ShadowMap_CubeMap;
3486 0 : border = smfilter > 2 ? smborder2 : smborder;
3487 0 : sidemask = drawtex == Draw_TexMinimap ? 0x2F : (smsidecull ? view.cullfrustumsides(l.o, l.radius, sm.size, border) : 0x3F);
3488 : }
3489 :
3490 0 : sm.sidemask = sidemask;
3491 :
3492 0 : shadoworigin = l.o;
3493 0 : shadowradius = l.radius;
3494 0 : shadowbias = border / static_cast<float>(sm.size - border);
3495 0 : shadowdir = l.dir;
3496 0 : shadowspot = l.spot;
3497 :
3498 0 : const shadowmesh *mesh = e ? findshadowmesh(l.ent, *e) : nullptr;
3499 :
3500 0 : findshadowvas();
3501 0 : findshadowmms();
3502 :
3503 0 : batching::shadowmaskbatchedmodels(!(l.flags&LightEnt_Static) && smdynshadow);
3504 0 : batchshadowmapmodels(mesh != nullptr);
3505 :
3506 0 : const shadowcacheval *cached = nullptr;
3507 0 : int cachemask = 0;
3508 0 : if(smcache)
3509 : {
3510 0 : int dynmask = smcache <= 1 ? batching::batcheddynamicmodels() : 0;
3511 0 : cached = sm.cached;
3512 0 : if(cached)
3513 : {
3514 0 : if(!debugshadowatlas)
3515 : {
3516 0 : cachemask = cached->sidemask & ~dynmask;
3517 : }
3518 0 : sm.sidemask |= cachemask;
3519 : }
3520 0 : sm.sidemask &= ~dynmask;
3521 :
3522 0 : sidemask &= ~cachemask;
3523 0 : if(!sidemask)
3524 : {
3525 0 : batching::clearbatchedmapmodels();
3526 0 : continue;
3527 : }
3528 : }
3529 :
3530 0 : float smnearclip = SQRT3 / l.radius,
3531 0 : smfarclip = SQRT3;
3532 0 : matrix4 smprojmatrix(vec4<float>(static_cast<float>(sm.size - border) / sm.size, 0, 0, 0),
3533 0 : vec4<float>(0, static_cast<float>(sm.size - border) / sm.size, 0, 0),
3534 0 : vec4<float>(0, 0, -(smfarclip + smnearclip) / (smfarclip - smnearclip), -1),
3535 0 : vec4<float>(0, 0, -2*smnearclip*smfarclip / (smfarclip - smnearclip), 0));
3536 :
3537 0 : if(shadowmapping == ShadowMap_Spot)
3538 : {
3539 0 : glViewport(sm.x, sm.y, sm.size, sm.size);
3540 0 : glScissor(sm.x, sm.y, sm.size, sm.size);
3541 0 : glClear(GL_DEPTH_BUFFER_BIT);
3542 :
3543 0 : float invradius = 1.0f / l.radius,
3544 0 : spotscale = invradius * cotan360(l.spot);
3545 0 : matrix4 spotmatrix(vec(l.spotx).mul(spotscale), vec(l.spoty).mul(spotscale), vec(l.dir).mul(-invradius));
3546 0 : spotmatrix.translate(vec(l.o).neg());
3547 0 : shadowmatrix.mul(smprojmatrix, spotmatrix);
3548 0 : GLOBALPARAM(shadowmatrix, shadowmatrix);
3549 :
3550 0 : glCullFace((l.dir.z >= 0) == (smcullside != 0) ? GL_BACK : GL_FRONT);
3551 :
3552 0 : shadowside = 0;
3553 :
3554 0 : if(mesh)
3555 : {
3556 0 : rendershadowmesh(mesh);
3557 : }
3558 : else
3559 : {
3560 0 : rendershadowmapworld();
3561 : }
3562 0 : batching::rendershadowmodelbatches();
3563 : }
3564 : else
3565 : {
3566 0 : if(!cachemask)
3567 : {
3568 0 : int cx1 = sidemask & 0x03 ? 0 : (sidemask & 0xC ? sm.size : 2 * sm.size),
3569 0 : cx2 = sidemask & 0x30 ? 3 * sm.size : (sidemask & 0xC ? 2 * sm.size : sm.size),
3570 0 : cy1 = sidemask & 0x15 ? 0 : sm.size,
3571 0 : cy2 = sidemask & 0x2A ? 2 * sm.size : sm.size;
3572 0 : glScissor(sm.x + cx1, sm.y + cy1, cx2 - cx1, cy2 - cy1);
3573 0 : glClear(GL_DEPTH_BUFFER_BIT);
3574 : }
3575 0 : for(int side = 0; side < 6; ++side)
3576 : {
3577 0 : if(sidemask&(1<<side))
3578 : {
3579 0 : int sidex = (side>>1)*sm.size,
3580 0 : sidey = (side&1)*sm.size;
3581 0 : glViewport(sm.x + sidex, sm.y + sidey, sm.size, sm.size);
3582 0 : glScissor(sm.x + sidex, sm.y + sidey, sm.size, sm.size);
3583 0 : if(cachemask)
3584 : {
3585 0 : glClear(GL_DEPTH_BUFFER_BIT);
3586 : }
3587 0 : matrix4 cubematrix(cubeshadowviewmatrix[side]);
3588 0 : cubematrix.scale(1.0f/l.radius);
3589 0 : cubematrix.translate(vec(l.o).neg());
3590 0 : shadowmatrix.mul(smprojmatrix, cubematrix);
3591 0 : GLOBALPARAM(shadowmatrix, shadowmatrix);
3592 :
3593 0 : glCullFace((side & 1) ^ (side >> 2) ^ smcullside ? GL_FRONT : GL_BACK);
3594 :
3595 0 : shadowside = side;
3596 :
3597 0 : if(mesh)
3598 : {
3599 0 : rendershadowmesh(mesh);
3600 : }
3601 : else
3602 : {
3603 0 : rendershadowmapworld();
3604 : }
3605 0 : batching::rendershadowmodelbatches();
3606 : }
3607 : }
3608 : }
3609 :
3610 0 : batching::clearbatchedmapmodels();
3611 : }
3612 :
3613 0 : glCullFace(GL_BACK);
3614 0 : glDisable(GL_SCISSOR_TEST);
3615 :
3616 0 : if(polyfactor || polyoffset)
3617 : {
3618 0 : glDisable(GL_POLYGON_OFFSET_FILL);
3619 : }
3620 0 : shadowmapping = 0;
3621 0 : if(inoq)
3622 : {
3623 0 : glBindFramebuffer(GL_FRAMEBUFFER, msaasamples ? msfbo : gfbo);
3624 0 : glViewport(0, 0, vieww, viewh);
3625 :
3626 0 : glFlush();
3627 : }
3628 : }
3629 :
3630 0 : void GBuffer::rendershadowatlas()
3631 : {
3632 0 : timer *smcputimer = begintimer("shadow map", false),
3633 0 : *smtimer = begintimer("shadow map");
3634 :
3635 0 : glBindFramebuffer(GL_FRAMEBUFFER, shadowatlas.fbo);
3636 0 : glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
3637 :
3638 0 : if(debugshadowatlas)
3639 : {
3640 0 : glClearDepth(0);
3641 0 : glClear(GL_DEPTH_BUFFER_BIT);
3642 0 : glClearDepth(1);
3643 : }
3644 :
3645 : // sun light
3646 0 : if(!csminoq || debugshadowatlas || inoq || !shouldworkinoq())
3647 : {
3648 0 : rendercsmshadowmaps();
3649 : }
3650 :
3651 0 : const int smoffset = shadowmaps.size();
3652 :
3653 0 : packlights();
3654 :
3655 : // point lights
3656 0 : rendershadowmaps(smoffset);
3657 :
3658 0 : glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3659 :
3660 0 : endtimer(smtimer);
3661 0 : endtimer(smcputimer);
3662 0 : }
3663 :
3664 0 : void GBuffer::workinoq()
3665 : {
3666 0 : collectlights();
3667 :
3668 0 : if(drawtex)
3669 : {
3670 0 : return;
3671 : }
3672 :
3673 0 : if(shouldworkinoq())
3674 : {
3675 0 : inoq = true;
3676 :
3677 0 : if(csminoq && !debugshadowatlas)
3678 : {
3679 0 : rendercsmshadowmaps();
3680 : }
3681 0 : if(sminoq && !debugshadowatlas)
3682 : {
3683 0 : rendershadowmaps();
3684 : }
3685 0 : if(rhinoq)
3686 : {
3687 0 : renderradiancehints();
3688 : }
3689 :
3690 0 : inoq = false;
3691 : }
3692 : }
3693 :
3694 : static VAR(gdepthclear, 0, 1, 1); //toggles whether to clear the g depth buffer to 0000/1111 (black or white depending on gdepthformat) upon creation
3695 : static VAR(gcolorclear, 0, 1, 1); //toggles whether to clear the g buffer to 0,0,0,0 (black) upon creation
3696 :
3697 0 : void GBuffer::preparegbuffer(bool depthclear)
3698 : {
3699 0 : glBindFramebuffer(GL_FRAMEBUFFER, msaasamples && (msaalight || !drawtex) ? msfbo : gfbo);
3700 0 : glViewport(0, 0, vieww, viewh);
3701 :
3702 0 : if(drawtex && gdepthinit)
3703 : {
3704 0 : glEnable(GL_SCISSOR_TEST);
3705 0 : glScissor(0, 0, vieww, viewh);
3706 : }
3707 0 : if(gdepthformat && gdepthclear)
3708 : {
3709 0 : maskgbuffer("d");
3710 0 : if(gdepthformat == 1)
3711 : {
3712 0 : glClearColor(1, 1, 1, 1);
3713 : }
3714 : else
3715 : {
3716 0 : glClearColor(-farplane, 0, 0, 0);
3717 : }
3718 0 : glClear(GL_COLOR_BUFFER_BIT);
3719 0 : maskgbuffer("cn");
3720 : }
3721 : else
3722 : {
3723 0 : maskgbuffer("cnd");
3724 : }
3725 0 : if(gcolorclear)
3726 : {
3727 0 : glClearColor(0, 0, 0, 0);
3728 : }
3729 0 : glClear((depthclear ? GL_DEPTH_BUFFER_BIT : 0)|(gcolorclear ? GL_COLOR_BUFFER_BIT : 0)|(depthclear && ghasstencil && (!msaasamples || msaalight || ghasstencil > 1) ? GL_STENCIL_BUFFER_BIT : 0));
3730 0 : if(gdepthformat && gdepthclear)
3731 : {
3732 0 : maskgbuffer("cnd");
3733 : }
3734 0 : if(drawtex && gdepthinit)
3735 : {
3736 0 : glDisable(GL_SCISSOR_TEST);
3737 : }
3738 0 : gdepthinit = true;
3739 :
3740 0 : matrix4 invscreenmatrix,
3741 0 : invcammatrix,
3742 0 : invcamprojmatrix;
3743 0 : invcammatrix.invert(cammatrix);
3744 0 : invcamprojmatrix.invert(camprojmatrix);
3745 0 : invscreenmatrix.identity();
3746 0 : invscreenmatrix.settranslation(-1.0f, -1.0f, -1.0f);
3747 0 : invscreenmatrix.setscale(2.0f/vieww, 2.0f/viewh, 2.0f);
3748 :
3749 0 : eyematrix.muld(projmatrix.inverse(), invscreenmatrix);
3750 0 : if(drawtex == Draw_TexMinimap)
3751 : {
3752 0 : linearworldmatrix.muld(invcamprojmatrix, invscreenmatrix);
3753 0 : if(!gdepthformat)
3754 : {
3755 0 : worldmatrix = linearworldmatrix;
3756 : }
3757 0 : linearworldmatrix.a.z = invcammatrix.a.z;
3758 0 : linearworldmatrix.b.z = invcammatrix.b.z;
3759 0 : linearworldmatrix.c.z = invcammatrix.c.z;
3760 0 : linearworldmatrix.d.z = invcammatrix.d.z;
3761 0 : if(gdepthformat)
3762 : {
3763 0 : worldmatrix = linearworldmatrix;
3764 : }
3765 0 : GLOBALPARAMF(radialfogscale, 0, 0, 0, 0);
3766 : }
3767 : else
3768 : {
3769 0 : float xscale = eyematrix.a.x,
3770 0 : yscale = eyematrix.b.y,
3771 0 : xoffset = eyematrix.d.x,
3772 0 : yoffset = eyematrix.d.y,
3773 0 : zscale = eyematrix.d.z;
3774 0 : matrix4 depthmatrix(vec(xscale/zscale, 0, xoffset/zscale), vec(0, yscale/zscale, yoffset/zscale));
3775 0 : linearworldmatrix.muld(invcammatrix, depthmatrix);
3776 0 : if(gdepthformat)
3777 : {
3778 0 : worldmatrix = linearworldmatrix;
3779 : }
3780 : else
3781 : {
3782 0 : worldmatrix.muld(invcamprojmatrix, invscreenmatrix);
3783 : }
3784 :
3785 0 : GLOBALPARAMF(radialfogscale, xscale/zscale, yscale/zscale, xoffset/zscale, yoffset/zscale);
3786 : }
3787 :
3788 0 : screenmatrix.identity();
3789 0 : screenmatrix.settranslation(0.5f*vieww, 0.5f*viewh, 0.5f);
3790 0 : screenmatrix.setscale(0.5f*vieww, 0.5f*viewh, 0.5f);
3791 0 : screenmatrix.muld(camprojmatrix);
3792 :
3793 0 : GLOBALPARAMF(viewsize, vieww, viewh, 1.0f/vieww, 1.0f/viewh);
3794 0 : GLOBALPARAMF(gdepthscale, eyematrix.d.z, eyematrix.c.w, eyematrix.d.w);
3795 0 : GLOBALPARAMF(gdepthinvscale, eyematrix.d.z / eyematrix.c.w, eyematrix.d.w / eyematrix.c.w);
3796 0 : GLOBALPARAMF(gdepthpackparams, -1.0f/farplane, -255.0f/farplane, -(255.0f*255.0f)/farplane);
3797 0 : GLOBALPARAMF(gdepthunpackparams, -farplane, -farplane/255.0f, -farplane/(255.0f*255.0f));
3798 0 : GLOBALPARAM(worldmatrix, worldmatrix);
3799 :
3800 0 : GLOBALPARAMF(ldrscale, ldrscale);
3801 0 : GLOBALPARAMF(hdrgamma, hdrgamma, 1.0f/hdrgamma);
3802 0 : GLOBALPARAM(camera, camera1->o);
3803 0 : GLOBALPARAMF(millis, lastmillis/1000.0f);
3804 :
3805 0 : glerror();
3806 :
3807 0 : if(depthclear)
3808 : {
3809 0 : resetlights();
3810 : }
3811 0 : batching::resetmodelbatches();
3812 0 : }
3813 :
3814 :
3815 : //allows passing nothing to internal uses of rendergbuffer
3816 : //(the parameter is for taking a game function to be rendered onscreen)
3817 0 : void GBuffer::dummyfxn()
3818 : {
3819 0 : return;
3820 : }
3821 :
3822 0 : void GBuffer::rendergbuffer(bool depthclear, void (*gamefxn)())
3823 : {
3824 0 : timer *gcputimer = drawtex ? nullptr : begintimer("g-buffer", false),
3825 0 : *gtimer = drawtex ? nullptr : begintimer("g-buffer");
3826 :
3827 0 : preparegbuffer(depthclear);
3828 :
3829 0 : if(limitsky())
3830 : {
3831 0 : renderexplicitsky();
3832 0 : glerror();
3833 : }
3834 0 : rendergeom();
3835 0 : glerror();
3836 0 : renderdecals();
3837 0 : glerror();
3838 0 : rendermapmodels();
3839 0 : glerror();
3840 0 : gamefxn();
3841 0 : if(drawtex == Draw_TexMinimap)
3842 : {
3843 0 : if(depthclear)
3844 : {
3845 0 : findmaterials();
3846 : }
3847 0 : renderminimapmaterials();
3848 0 : glerror();
3849 : }
3850 0 : else if(!drawtex)
3851 : {
3852 0 : rendermodelbatches();
3853 0 : glerror();
3854 0 : renderstains(StainBuffer_Opaque, true);
3855 0 : renderstains(StainBuffer_Mapmodel, true);
3856 0 : glerror();
3857 : }
3858 :
3859 0 : endtimer(gtimer);
3860 0 : endtimer(gcputimer);
3861 0 : }
3862 :
3863 0 : void GBuffer::shademinimap(const vec &color)
3864 : {
3865 0 : glerror();
3866 :
3867 0 : glBindFramebuffer(GL_FRAMEBUFFER, msaalight ? mshdrfbo : hdrfbo);
3868 0 : glViewport(0, 0, vieww, viewh);
3869 :
3870 0 : if(color.x >= 0)
3871 : {
3872 0 : glClearColor(color.x, color.y, color.z, 0);
3873 0 : glClear(GL_COLOR_BUFFER_BIT);
3874 : }
3875 :
3876 0 : renderlights(-1, -1, 1, 1, nullptr, 0, msaalight ? -1 : 0);
3877 0 : glerror();
3878 0 : }
3879 :
3880 0 : void GBuffer::shademodelpreview(int x, int y, int w, int h, bool background, bool scissor)
3881 : {
3882 0 : glerror();
3883 :
3884 0 : glBindFramebuffer(GL_FRAMEBUFFER, 0);
3885 0 : glViewport(0, 0, hudw(), hudh());
3886 :
3887 0 : if(msaalight)
3888 : {
3889 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mscolortex);
3890 : }
3891 : else
3892 : {
3893 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gcolortex);
3894 : }
3895 0 : glActiveTexture(GL_TEXTURE1);
3896 0 : if(msaalight)
3897 : {
3898 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msnormaltex);
3899 : }
3900 : else
3901 : {
3902 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gnormaltex);
3903 : }
3904 0 : glActiveTexture(GL_TEXTURE3);
3905 0 : if(msaalight)
3906 : {
3907 0 : glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
3908 : }
3909 : else
3910 : {
3911 0 : glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
3912 : }
3913 0 : glActiveTexture(GL_TEXTURE0);
3914 :
3915 0 : float lightscale = 2.0f*ldrscale;
3916 0 : GLOBALPARAMF(lightscale, 0.1f*lightscale, 0.1f*lightscale, 0.1f*lightscale, lightscale);
3917 0 : GLOBALPARAM(sunlightdir, vec(0, -1, 2).normalize());
3918 0 : GLOBALPARAMF(sunlightcolor, 0.6f*lightscale, 0.6f*lightscale, 0.6f*lightscale);
3919 :
3920 0 : SETSHADER(modelpreview);
3921 :
3922 0 : LOCALPARAMF(cutout, background ? -1 : 0);
3923 :
3924 0 : if(scissor)
3925 : {
3926 0 : glEnable(GL_SCISSOR_TEST);
3927 : }
3928 :
3929 0 : int sx = std::clamp(x, 0, hudw()),
3930 0 : sy = std::clamp(y, 0, hudh()),
3931 0 : sw = std::clamp(x + w, 0, hudw()) - sx,
3932 0 : sh = std::clamp(y + h, 0, hudh()) - sy;
3933 0 : float sxk = 2.0f/hudw(),
3934 0 : syk = 2.0f/hudh(),
3935 0 : txk = vieww/static_cast<float>(w),
3936 0 : tyk = viewh/static_cast<float>(h);
3937 0 : hudquad(sx*sxk - 1, sy*syk - 1, sw*sxk, sh*syk, (sx-x)*txk, (sy-y)*tyk, sw*txk, sh*tyk);
3938 :
3939 0 : if(scissor)
3940 : {
3941 0 : glDisable(GL_SCISSOR_TEST);
3942 : }
3943 :
3944 0 : glerror();
3945 0 : }
3946 :
3947 0 : void GBuffer::shadesky() const
3948 : {
3949 0 : glBindFramebuffer(GL_FRAMEBUFFER, msaalight ? mshdrfbo : hdrfbo);
3950 0 : glViewport(0, 0, vieww, viewh);
3951 :
3952 0 : drawskybox((hdrclear > 0 ? hdrclear-- : msaalight) > 0);
3953 0 : }
3954 :
3955 0 : bool GBuffer::istransparentlayer() const
3956 : {
3957 0 : return transparentlayer;
3958 : }
3959 :
3960 0 : void shadegbuffer()
3961 : {
3962 0 : if(msaasamples && !msaalight && !drawtex)
3963 : {
3964 0 : gbuf.resolvemsaadepth(vieww, viewh);
3965 : }
3966 0 : glerror();
3967 :
3968 0 : timer *shcputimer = begintimer("deferred shading", false),
3969 0 : *shtimer = begintimer("deferred shading");
3970 :
3971 0 : gbuf.shadesky();
3972 :
3973 0 : if(msaasamples && (msaalight || !drawtex))
3974 : {
3975 0 : if((ghasstencil && msaaedgedetect) || msaalight==2)
3976 : {
3977 0 : for(int i = 0; i < 2; ++i)
3978 : {
3979 0 : gbuf.renderlights(-1, -1, 1, 1, nullptr, 0, i+1);
3980 : }
3981 0 : }
3982 : else
3983 : {
3984 0 : gbuf.renderlights(-1, -1, 1, 1, nullptr, 0, drawtex ? -1 : 3);
3985 : }
3986 0 : }
3987 : else
3988 : {
3989 0 : gbuf.renderlights();
3990 : }
3991 0 : glerror();
3992 :
3993 0 : if(!drawtex)
3994 : {
3995 0 : renderstains(StainBuffer_Opaque, false);
3996 0 : renderstains(StainBuffer_Mapmodel, false);
3997 : }
3998 :
3999 0 : endtimer(shtimer);
4000 0 : endtimer(shcputimer);
4001 0 : }
4002 :
4003 0 : void setuplights(GBuffer &buf)
4004 : {
4005 0 : glerror();
4006 0 : buf.setupgbuffer();
4007 0 : if(bloomw < 0 || bloomh < 0)
4008 : {
4009 0 : setupbloom(gw, gh);
4010 : }
4011 0 : if(ao && (aow < 0 || aoh < 0))
4012 : {
4013 0 : setupao(gw, gh);
4014 : }
4015 0 : if(volumetriclights && volumetric && (volw < 0 || volh < 0))
4016 : {
4017 0 : setupvolumetric(gw, gh);
4018 : }
4019 0 : if(!shadowatlas.fbo)
4020 : {
4021 0 : shadowatlas.setup();
4022 : }
4023 0 : if(useradiancehints() && !rhfbo)
4024 : {
4025 0 : setupradiancehints();
4026 : }
4027 0 : if(!deferredlightshader)
4028 : {
4029 0 : loaddeferredlightshaders();
4030 : }
4031 0 : if(drawtex == Draw_TexMinimap && !deferredminimapshader)
4032 : {
4033 0 : deferredminimapshader = loaddeferredlightshader(msaalight ? "mM" : "m");
4034 : }
4035 0 : setupaa(buf, gw, gh);
4036 0 : glerror();
4037 0 : }
4038 :
4039 0 : bool debuglights()
4040 : {
4041 0 : viewao(); //this fxn checks for the appropriate debug var
4042 0 : if(debugshadowatlas)
4043 : {
4044 0 : shadowatlas.view();
4045 : }
4046 0 : else if(debugdepth)
4047 : {
4048 0 : gbuf.viewdepth();
4049 : }
4050 0 : else if(debugstencil)
4051 : {
4052 0 : viewstencil();
4053 : }
4054 0 : else if(debugrefract)
4055 : {
4056 0 : gbuf.viewrefract();
4057 : }
4058 0 : else if(debuglightscissor)
4059 : {
4060 0 : viewlightscissor();
4061 : }
4062 0 : else if(debugrsm)
4063 : {
4064 0 : viewrsm();
4065 : }
4066 0 : else if(debugrh)
4067 : {
4068 0 : viewrh();
4069 : }
4070 0 : else if(!debugaa())
4071 : {
4072 0 : return false;
4073 : }
4074 0 : return true;
4075 : }
4076 :
4077 0 : void cleanuplights()
4078 : {
4079 0 : gbuf.cleanupgbuffer();
4080 0 : cleanupbloom();
4081 0 : cleanupao();
4082 0 : cleanupvolumetric();
4083 0 : shadowatlas.cleanup();
4084 0 : cleanupradiancehints();
4085 0 : lightsphere::cleanup();
4086 0 : cleanupaa();
4087 0 : }
4088 :
4089 1 : int GBuffer::getlightdebuginfo(uint type) const
4090 : {
4091 1 : switch(type)
4092 : {
4093 1 : case 0:
4094 : {
4095 1 : return lightpassesused;
4096 : }
4097 0 : case 1:
4098 : {
4099 0 : return lightsvisible;
4100 : }
4101 0 : case 2:
4102 : {
4103 0 : return lightsoccluded;
4104 : }
4105 0 : case 3:
4106 : {
4107 0 : return lightbatchesused;
4108 : }
4109 0 : case 4:
4110 : {
4111 0 : return lightbatchrectsused;
4112 : }
4113 0 : case 5:
4114 : {
4115 0 : return lightbatchstacksused;
4116 : }
4117 0 : default:
4118 : {
4119 0 : return -1;
4120 : }
4121 : }
4122 : }
4123 :
4124 1 : void initrenderlightscmds()
4125 : {
4126 2 : addcommand("usepacknorm", reinterpret_cast<identfun>(+[](){intret(usepacknorm() ? 1 : 0);}), "", Id_Command);
4127 2 : addcommand("lightdebuginfo", reinterpret_cast<identfun>(+[] (int * index) {intret(gbuf.getlightdebuginfo(static_cast<uint>(*index)));} ), "i", Id_Command);
4128 2 : addcommand("getcsmproperty", reinterpret_cast<identfun>(+[] (int * index) {floatret(csm.getcsmproperty(*index));} ), "i", Id_Command);
4129 2 : addcommand("setcsmproperty", reinterpret_cast<identfun>(+[] (int * index, float * value) {intret(csm.setcsmproperty(*index, *value));} ), "if", Id_Command);
4130 1 : }
|