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

Generated by: LCOV version 2.0-1