LCOV - code coverage report
Current view: top level - engine/render - renderlights.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 0.9 % 2173 19
Test Date: 2025-10-13 07:05:02 Functions: 6.0 % 150 9

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

Generated by: LCOV version 2.0-1