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

Generated by: LCOV version 2.0-1