LCOV - code coverage report
Current view: top level - engine/render - ao.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 0.0 % 174 0
Test Date: 2025-10-13 07:05:02 Functions: 0.0 % 19 0

            Line data    Source code
       1              : /* ao.cpp: screenspace ambient occlusion
       2              :  *
       3              :  * Screenspace ambient occlusion is a way to simulate darkening of corners which
       4              :  * do not recieve as much diffuse light as other areas. SSAO relies on the depth
       5              :  * buffer of the scene to determine areas which appear to be creases and
       6              :  * darkens those areas. Various settings allow for more or less artifact-free
       7              :  * rendition of this darkening effect.
       8              :  */
       9              : #include "../libprimis-headers/cube.h"
      10              : #include "../../shared/geomexts.h"
      11              : #include "../../shared/glemu.h"
      12              : #include "../../shared/glexts.h"
      13              : 
      14              : #include <format>
      15              : 
      16              : #include "ao.h"
      17              : #include "rendergl.h"
      18              : #include "renderlights.h"
      19              : #include "rendertimers.h"
      20              : #include "renderwindow.h"
      21              : #include "shader.h"
      22              : #include "shaderparam.h"
      23              : #include "texture.h"
      24              : 
      25              : #include "interface/control.h"
      26              : 
      27              : int aow  = -1,
      28              :     aoh  = -1;
      29              : static std::array<GLuint, 4> aofbo = { 0, 0, 0, 0 };
      30              : std::array<GLuint, 4> aotex = { 0, 0, 0, 0 };
      31              : GLuint aonoisetex = 0;
      32              : 
      33            0 : VARFP(ao, 0, 1, 1, { cleanupao(); cleardeferredlightshaders(); }); //toggles ao use in general
      34              : static FVARR(aoradius, 0, 5, 256);
      35              : static FVAR(aocutoff, 0, 2.0f, 1e3f);
      36              : static FVARR(aodark, 1e-3f, 11.0f, 1e3f);
      37              : static FVARR(aosharp, 1e-3f, 1, 1e3f);
      38              : static FVAR(aoprefilterdepth, 0, 1, 1e3f);
      39              : FVARR(aomin, 0, 0.25f, 1);
      40            0 : VARFR(aosun, 0, 1, 1, cleardeferredlightshaders()); //toggles ambient occlusion for sunlight
      41              : FVARR(aosunmin, 0, 0.5f, 1);
      42              : static VARP(aoblur, 0, 4, 7);
      43              : static VARP(aoiter, 0, 0, 4); //number of times to run ao shader (higher is smoother)
      44            0 : VARFP(aoreduce, 0, 1, 2, cleanupao());
      45            0 : VARF(aoreducedepth, 0, 1, 2, cleanupao());
      46            0 : static VARFP(aofloatdepth, 0, 1, 2, initwarning("AO setup", Init_Load, Change_Shaders));
      47            0 : static VARFP(aoprec, 0, 1, 1, cleanupao()); //toggles between r8 and rgba8 buffer format
      48              : static VAR(aodepthformat, 1, 0, 0);
      49            0 : static VARF(aonoise, 0, 5, 8, cleanupao()); //power or two scale factor for ao noise effect
      50            0 : VARFP(aobilateral, 0, 3, 10, cleanupao());
      51              : static FVARP(aobilateraldepth, 0, 4, 1e3f);
      52            0 : VARFP(aobilateralupscale, 0, 0, 1, cleanupao());
      53            0 : VARF(aopackdepth, 0, 1, 1, cleanupao());
      54            0 : static VARFP(aotaps, 1, 12, 12, cleanupao());
      55              : static VAR(debugao, 0, 0, 4);
      56              : 
      57              : static Shader *ambientobscuranceshader = nullptr;
      58              : 
      59              : /**
      60              :  * @brief Creates a new ambient obscurance object.
      61              :  *
      62              :  * Creates a new ambient obscurance (ambient occlusion) object with values based
      63              :  * on current settings. This object envelops the AO shader and its accompanying
      64              :  * values.
      65              :  *
      66              :  * @return a new Shader object that applies an AO effect.
      67              :  */
      68            0 : static Shader *loadambientobscuranceshader()
      69              : {
      70              :     string opts;
      71            0 :     int optslen = 0;
      72              : 
      73            0 :     bool linear = aoreducedepth && (aoreduce || aoreducedepth > 1);
      74            0 :     if(linear)
      75              :     {
      76            0 :         opts[optslen++] = 'l';
      77              :     }
      78            0 :     if(aobilateral && aopackdepth)
      79              :     {
      80            0 :         opts[optslen++] = 'p';
      81              :     }
      82            0 :     opts[optslen] = '\0';
      83            0 :     std::string name = std::format("ambientobscurance{}{}", opts, aotaps);
      84            0 :     return generateshader(name, "ambientobscuranceshader \"%s\" %d", opts, aotaps);
      85            0 : }
      86              : 
      87              : /**
      88              :  * @brief Sets the ambientobscuranceshader gvar to the value created by above fxn.
      89              :  *
      90              :  * The loadambientobscuranceshader() function is called to generate a Shader object
      91              :  * and is assigned to the ambient obscurance shader global variable. This global
      92              :  * is local to this file.
      93              :  */
      94            0 : static void loadaoshaders()
      95              : {
      96            0 :     ambientobscuranceshader = loadambientobscuranceshader();
      97            0 : }
      98              : 
      99              : /**
     100              :  * @brief un-sets the ambientobscuranceshader gvar defined by loadaoshaders
     101              :  *
     102              :  * Sets the ambientobscuranceshader variable set in loadaoshaders() to nullptr.
     103              :  * Does not free the object.
     104              :  */
     105            0 : static void clearaoshaders()
     106              : {
     107            0 :     ambientobscuranceshader = nullptr;
     108            0 : }
     109              : 
     110            0 : void setupao(int w, int h)
     111              : {
     112            0 :     int sw = w>>aoreduce,
     113            0 :         sh = h>>aoreduce;
     114              : 
     115            0 :     if(sw == aow && sh == aoh)
     116              :     {
     117            0 :         return;
     118              :     }
     119            0 :     aow = sw;
     120            0 :     aoh = sh;
     121            0 :     if(!aonoisetex)
     122              :     {
     123            0 :         glGenTextures(1, &aonoisetex);
     124              :     }
     125            0 :     bvec *noise = new bvec[(1<<aonoise)*(1<<aonoise)];
     126            0 :     for(int k = 0; k < (1<<aonoise)*(1<<aonoise); ++k)
     127              :     {
     128            0 :         noise[k] = bvec(vec(randomfloat(2)-1, randomfloat(2)-1, 0).normalize());
     129              :     }
     130            0 :     createtexture(aonoisetex, 1<<aonoise, 1<<aonoise, noise, 0, 0, GL_RGB, GL_TEXTURE_2D);
     131            0 :     delete[] noise;
     132              : 
     133            0 :     bool upscale = aoreduce && aobilateral && aobilateralupscale;
     134            0 :     GLenum format = aoprec ? GL_R8 : GL_RGBA8,
     135            0 :            packformat = aobilateral && aopackdepth ? (aodepthformat ? GL_RG16F : GL_RGBA8) : format;
     136            0 :     int packfilter = upscale && aopackdepth && !aodepthformat ? 0 : 1;
     137            0 :     for(int i = 0; i < (upscale ? 3 : 2); ++i)
     138              :     {
     139              :         //create framebuffer
     140            0 :         if(!aotex[i])
     141              :         {
     142            0 :             glGenTextures(1, &aotex[i]);
     143              :         }
     144            0 :         if(!aofbo[i])
     145              :         {
     146            0 :             glGenFramebuffers(1, &aofbo[i]);
     147              :         }
     148            0 :         createtexture(aotex[i], upscale && i ? w : aow, upscale && i >= 2 ? h : aoh, nullptr, 3, i < 2 ? packfilter : 1, i < 2 ? packformat : format, GL_TEXTURE_RECTANGLE);
     149            0 :         glBindFramebuffer(GL_FRAMEBUFFER, aofbo[i]);
     150            0 :         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, aotex[i], 0);
     151              :         //make sure we have a framebuffer
     152            0 :         if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
     153              :         {
     154            0 :             fatal("failed allocating AO buffer!");
     155              :         }
     156            0 :         if(!upscale && packformat == GL_RG16F)
     157              :         {
     158            0 :             glClearColor(0, 0, 0, 0);
     159            0 :             glClear(GL_COLOR_BUFFER_BIT);
     160              :         }
     161              :     }
     162            0 :     if(aoreducedepth && (aoreduce || aoreducedepth > 1))
     163              :     {
     164              :         //create framebuffer
     165            0 :         if(!aotex[3])
     166              :         {
     167            0 :             glGenTextures(1, &aotex[3]);
     168              :         }
     169            0 :         if(!aofbo[3])
     170              :         {
     171            0 :             glGenFramebuffers(1, &aofbo[3]);
     172              :         }
     173            0 :         createtexture(aotex[3], aow, aoh, nullptr, 3, 0, aodepthformat > 1 ? GL_R32F : (aodepthformat ? GL_R16F : GL_RGBA8), GL_TEXTURE_RECTANGLE);
     174            0 :         glBindFramebuffer(GL_FRAMEBUFFER, aofbo[3]);
     175            0 :         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, aotex[3], 0);
     176              :         //make sure we have a framebuffer
     177            0 :         if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
     178              :         {
     179            0 :             fatal("failed allocating AO buffer!");
     180              :         }
     181              :     }
     182              : 
     183            0 :     glBindFramebuffer(GL_FRAMEBUFFER, 0);
     184              : 
     185            0 :     loadaoshaders();
     186            0 :     loadbilateralshaders();
     187              : }
     188              : 
     189            0 : void cleanupao()
     190              : {
     191            0 :     for(int i = 0; i < 4; ++i)
     192              :     {
     193            0 :         if(aofbo[i])
     194              :         {
     195            0 :             glDeleteFramebuffers(1, &aofbo[i]);
     196            0 :             aofbo[i] = 0;
     197              :         }
     198              :     }
     199            0 :     for(int i = 0; i < 4; ++i)
     200              :     {
     201            0 :         if(aotex[i])
     202              :         {
     203            0 :             glDeleteTextures(1, &aotex[i]);
     204            0 :             aotex[i] = 0;
     205              :         }
     206              :     }
     207            0 :     if(aonoisetex)
     208              :     {
     209            0 :         glDeleteTextures(1, &aonoisetex);
     210            0 :         aonoisetex = 0;
     211              :     }
     212            0 :     aow = aoh = -1;
     213              : 
     214            0 :     clearaoshaders();
     215            0 :     clearbilateralshaders();
     216            0 : }
     217              : 
     218            0 : void initao()
     219              : {
     220            0 :     aodepthformat = aofloatdepth ? aofloatdepth : 0;
     221            0 : }
     222              : 
     223            0 : void viewao()
     224              : {
     225            0 :     if(!ao || !debugao)
     226              :     {
     227            0 :         return;
     228              :     }
     229            0 :     int w = debugfullscreen ? hudw() : std::min(hudw(), hudh())/2, //if debugfullscreen, set to hudw/hudh size; if not, do small size
     230            0 :         h = debugfullscreen ? hudh() : (w*hudh())/hudw();
     231            0 :     SETSHADER(hudrect);
     232            0 :     gle::colorf(1, 1, 1);
     233            0 :     glBindTexture(GL_TEXTURE_RECTANGLE, aotex[debugao - 1]);
     234            0 :     int tw = aotex[2] ? gw : aow,
     235            0 :         th = aotex[2] ? gh : aoh;
     236            0 :     debugquad(0, 0, w, h, 0, 0, tw, th);
     237              : }
     238              : 
     239            0 : void GBuffer::renderao() const
     240              : {
     241            0 :     if(!ao)
     242              :     {
     243            0 :         return;
     244              :     }
     245            0 :     timer *aotimer = begintimer("ambient obscurance");
     246              : 
     247            0 :     if(msaasamples)
     248              :     {
     249            0 :         glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
     250              :     }
     251              :     else
     252              :     {
     253            0 :         glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
     254              :     }
     255            0 :     bool linear = aoreducedepth && (aoreduce || aoreducedepth > 1);
     256            0 :     float xscale = eyematrix.a.x,
     257            0 :           yscale = eyematrix.b.y;
     258            0 :     if(linear)
     259              :     {
     260            0 :         glBindFramebuffer(GL_FRAMEBUFFER, aofbo[3]);
     261            0 :         glViewport(0, 0, aow, aoh);
     262            0 :         SETSHADER(linearizedepth);
     263            0 :         screenquad(vieww, viewh);
     264              : 
     265            0 :         xscale *= static_cast<float>(vieww)/aow;
     266            0 :         yscale *= static_cast<float>(viewh)/aoh;
     267              : 
     268            0 :         glBindTexture(GL_TEXTURE_RECTANGLE, aotex[3]);
     269              :     }
     270              : 
     271            0 :     ambientobscuranceshader->set();
     272              : 
     273            0 :     glBindFramebuffer(GL_FRAMEBUFFER, aofbo[0]);
     274            0 :     glViewport(0, 0, aow, aoh);
     275            0 :     glActiveTexture(GL_TEXTURE1);
     276              : 
     277            0 :     if(msaasamples)
     278              :     {
     279            0 :         glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msnormaltex);
     280              :     }
     281              :     else
     282              :     {
     283            0 :         glBindTexture(GL_TEXTURE_RECTANGLE, gnormaltex);
     284              :     }
     285              : 
     286            0 :     LOCALPARAM(normalmatrix, matrix3(cammatrix));
     287            0 :     glActiveTexture(GL_TEXTURE2);
     288            0 :     glBindTexture(GL_TEXTURE_2D, aonoisetex);
     289            0 :     glActiveTexture(GL_TEXTURE0);
     290              : 
     291            0 :     LOCALPARAMF(tapparams, aoradius*eyematrix.d.z/xscale, aoradius*eyematrix.d.z/yscale, aoradius*aoradius*aocutoff*aocutoff);
     292            0 :     LOCALPARAMF(contrastparams, (2.0f*aodark)/aotaps, aosharp);
     293            0 :     LOCALPARAMF(offsetscale, xscale/eyematrix.d.z, yscale/eyematrix.d.z, eyematrix.d.x/eyematrix.d.z, eyematrix.d.y/eyematrix.d.z);
     294            0 :     LOCALPARAMF(prefilterdepth, aoprefilterdepth);
     295            0 :     screenquad(vieww, viewh, aow/static_cast<float>(1<<aonoise), aoh/static_cast<float>(1<<aonoise));
     296              : 
     297            0 :     if(aobilateral)
     298              :     {
     299            0 :         if(aoreduce && aobilateralupscale)
     300              :         {
     301            0 :             for(int i = 0; i < 2; ++i)
     302              :             {
     303            0 :                 setbilateralshader(aobilateral, i, aobilateraldepth);
     304            0 :                 glBindFramebuffer(GL_FRAMEBUFFER, aofbo[i+1]);
     305            0 :                 glViewport(0, 0, vieww, i ? viewh : aoh);
     306            0 :                 glBindTexture(GL_TEXTURE_RECTANGLE, aotex[i]);
     307            0 :                 glActiveTexture(GL_TEXTURE1);
     308            0 :                 if(msaasamples)
     309              :                 {
     310            0 :                     glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
     311              :                 }
     312              :                 else
     313              :                 {
     314            0 :                     glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
     315              :                 }
     316            0 :                 glActiveTexture(GL_TEXTURE0);
     317            0 :                 screenquad(vieww, viewh, i ? vieww : aow, aoh);
     318              :             }
     319            0 :         }
     320              :         else
     321              :         {
     322            0 :             for(int i = 0; i < 2 + 2*aoiter; ++i)
     323              :             {
     324            0 :                 setbilateralshader(aobilateral, i%2, aobilateraldepth);
     325            0 :                 glBindFramebuffer(GL_FRAMEBUFFER, aofbo[(i+1)%2]);
     326            0 :                 glViewport(0, 0, aow, aoh);
     327            0 :                 glBindTexture(GL_TEXTURE_RECTANGLE, aotex[i%2]);
     328            0 :                 glActiveTexture(GL_TEXTURE1);
     329            0 :                 if(linear)
     330              :                 {
     331            0 :                     glBindTexture(GL_TEXTURE_RECTANGLE, aotex[3]);
     332              :                 }
     333            0 :                 else if(msaasamples)
     334              :                 {
     335            0 :                     glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msdepthtex);
     336              :                 }
     337              :                 else
     338              :                 {
     339            0 :                     glBindTexture(GL_TEXTURE_RECTANGLE, gdepthtex);
     340              :                 }
     341            0 :                 glActiveTexture(GL_TEXTURE0);
     342            0 :                 screenquad(vieww, viewh);
     343              :             }
     344              :         }
     345              :     }
     346            0 :     else if(aoblur)
     347              :     {
     348              :         std::array<float, maxblurradius+1> blurweights,
     349              :                                            bluroffsets;
     350            0 :         setupblurkernel(aoblur, blurweights.data(), bluroffsets.data());
     351            0 :         for(int i = 0; i < 2+2*aoiter; ++i)
     352              :         {
     353            0 :             glBindFramebuffer(GL_FRAMEBUFFER, aofbo[(i+1)%2]);
     354            0 :             glViewport(0, 0, aow, aoh);
     355            0 :             setblurshader(i%2, 1, aoblur, blurweights.data(), bluroffsets.data(), GL_TEXTURE_RECTANGLE);
     356            0 :             glBindTexture(GL_TEXTURE_RECTANGLE, aotex[i%2]);
     357            0 :             screenquad(aow, aoh);
     358              :         }
     359              :     }
     360              : 
     361            0 :     glBindFramebuffer(GL_FRAMEBUFFER, msaasamples ? msfbo : gfbo);
     362            0 :     glViewport(0, 0, vieww, viewh);
     363              : 
     364            0 :     endtimer(aotimer);
     365              : }
        

Generated by: LCOV version 2.0-1