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