LCOV - code coverage report
Current view: top level - engine/render - renderlights.h (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 78.3 % 46 36
Test Date: 2026-07-09 06:56:18 Functions: 50.0 % 4 2

            Line data    Source code
       1              : #ifndef RENDERLIGHTS_H_
       2              : #define RENDERLIGHTS_H_
       3              : 
       4              : const int lighttilemaxwidth  = 16;
       5              : const int lighttilemaxheight = 16;
       6              : 
       7              : /** @brief Singleton object used to store the graphics buffers
       8              :  *
       9              :  * Stores the handles for the graphics buffers nd the functions which act upon
      10              :  * the g-buffers.
      11              :  */
      12              : class GBuffer final
      13              : {
      14              :     public:
      15            1 :         GBuffer()
      16            1 :         {
      17              :             //set all of the textures to 0/null
      18            1 :             scalew       = 0;
      19            1 :             scaleh       = 0;
      20            1 :             gfbo         = 0;
      21            1 :             gdepthtex    = 0;
      22            1 :             gcolortex    = 0;
      23            1 :             gnormaltex   = 0;
      24            1 :             gglowtex     = 0;
      25            1 :             gdepthrb     = 0;
      26            1 :             gstencilrb   = 0;
      27            1 :             msfbo        = 0;
      28            1 :             msdepthtex   = 0;
      29            1 :             mscolortex   = 0;
      30            1 :             msnormaltex  = 0;
      31            1 :             msglowtex    = 0;
      32            1 :             msdepthrb    = 0;
      33            1 :             msstencilrb  = 0;
      34            1 :             mshdrfbo     = 0;
      35            1 :             mshdrtex     = 0;
      36            1 :             msrefractfbo = 0;
      37            1 :             msrefracttex = 0;
      38            1 :             refractfbo   = 0;
      39            1 :             refracttex   = 0;
      40            1 :             stencilformat= 0;
      41            1 :             scalefbo.fill(0);
      42            1 :             scaletex.fill(0);
      43            1 :             gdepthinit   = false;
      44            1 :             hdrfloat     = false;
      45            1 :             msaadepthblit= false;
      46            1 :             msaatonemapblit = false;
      47            1 :             inoq = false;
      48            1 :             transparentlayer = 0;
      49              : 
      50            1 :         }
      51              :         static void dummyfxn();
      52              :         //main g-buffers
      53              :         void cleanupgbuffer();
      54              :         void renderao() const;                              //ao.cpp
      55              :         void renderradiancehints() const;                   //radiancehints.cpp
      56              :         void rendertransparent();                           //renderalpha.cpp
      57              :         void resolvemsaadepth(int w, int h) const;
      58              :         void setupgbuffer();
      59              :         void bindgdepth() const;
      60              :         void renderparticles(int layer = 0) const;                //renderparticles.cpp
      61              :         void rendervolumetric();
      62              :         void renderwaterfog(int mat, float surface);        //water.cpp
      63              :         void setaavelocityparams(GLenum tmu = GL_TEXTURE0); //aa.cpp
      64              :         void shademodelpreview(int x, int y, int w, int h, bool background = true, bool scissor = false);
      65              :         void viewdepth() const;
      66              :         void rendershadowatlas();
      67              :         //multisample antialiasing specific buffers
      68              :         void setupmsbuffer(int w, int h);
      69              :         void resolvemsaacolor(int w, int h);
      70              :         void shademinimap(const vec &color = vec(-1, -1, -1));
      71              :         void shadesky() const;
      72              :         //refractive
      73              :         void processhdr(GLuint outfbo, int aa);
      74              : 
      75              :         /**
      76              :          * @brief Draws the debug refraction buffer.
      77              :          *
      78              :          * Draws the buffer containing the refraction information to the screen.
      79              :          * The size of the debug draw is dependent on the debugfullscreen variable;
      80              :          * if 1, the debug buffer spans the whole screen, if not, the debug buffer
      81              :          * is drawn to the bottom right corner of the screen.
      82              :          */
      83              :         void viewrefract();
      84              :         void doscale(GLuint outfbo = 0) const;
      85              :         void setupscale(int sw, int sh, int w, int h);
      86              : 
      87              :         /**
      88              :          * @brief Returns the scale fbo if available.
      89              :          *
      90              :          * Otherwise, returns 0.
      91              :          *
      92              :          * @return scale buffer or 0
      93              :          */
      94              :         GLuint shouldscale() const;
      95              :         void workinoq();
      96              : 
      97              :         /**
      98              :          * @brief Creates the geometry buffer for the scene
      99              :          *
     100              :          * Renders and copies a fbo (framebuffer object) to msfbo (multisample framebuffer object)
     101              :          * or gfbo (geometry buffer framebuffer object) depending on whether msaa is enabled
     102              :          *
     103              :          * @param bool depthclear toggles clearing the depth buffer
     104              :          * @param gamefxn pointer to a function for game-specific rendering
     105              :          */
     106              :         void rendergbuffer(bool depthclear = true, void (*gamefxn)() = dummyfxn);
     107              : 
     108              :         /**
     109              :          * @brief Returns whether a transparency layer is being rendered.
     110              :          *
     111              :          * This will only be true within GBuffer::rendertransparent(), which sets
     112              :          * this value > 0 while rendering transparency and then value = 0 when
     113              :          * finished.
     114              :          *
     115              :          * @return whether rendertransparent() is rendering transparency
     116              :          */
     117              :         bool istransparentlayer() const;
     118              :         void rendermodelbatches();
     119              :         void renderlights(float bsx1 = -1, float bsy1 = -1, float bsx2 = 1, float bsy2 = 1, const uint *tilemask = nullptr, int stencilmask = 0, int msaapass = 0, bool transparent = false);
     120              : 
     121              :         /**
     122              :          * @brief Returns debug information about lighting.
     123              :          *
     124              :          * Types:
     125              :          * 0: light passes used
     126              :          * 1: lights visible
     127              :          * 2: lights occluded
     128              :          * 3: light batches used
     129              :          * 4: light batch rects used
     130              :          * 5: light batch stacks used
     131              :          *
     132              :          * @param type of value to query
     133              :          *
     134              :          * @return numeric value of appropriate type
     135              :          */
     136              :         int getlightdebuginfo(uint type) const;
     137              :     private:
     138              :         void bindmsdepth() const;
     139              :         void bindlighttexs(int msaapass, bool transparent) const; //only used in renderlights
     140              :         void cleanupscale();
     141              :         void cleanupmsbuffer();
     142              :         void preparegbuffer(bool depthclear = true);
     143              :         void rendercsmshadowmaps() const;
     144              :         void rendershadowmaps(int offset = 0) const;
     145              :         void rendersunpass(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask);
     146              :         void renderlightsnobatch(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2);
     147              :         void renderlightbatches(Shader &s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask);
     148              :         void rendergeom();
     149              : 
     150              :         /**
     151              :          * @brief Renders alpha particles.
     152              :          *
     153              :          * @param allsx1 bottom left scissor corner x coordinate
     154              :          * @param allsy1 bottom left scissor corner y coordinate
     155              :          * @param allsx2 top right scissor corner y coordinate
     156              :          * @param allsy2 top right scissor corner y coordinate
     157              :          */
     158              :         void alphaparticles(float allsx1, float allsy1, float allsx2, float allsy2) const;
     159              : 
     160              :         void rendermaterialmask() const;
     161              :         void renderliquidmaterials() const;
     162              :         void packlights();
     163              : 
     164              :         struct MaterialInfo final
     165              :         {
     166              :             float matliquidsx1,
     167              :                   matliquidsy1,
     168              :                   matliquidsx2,
     169              :                   matliquidsy2;
     170              :             float matsolidsx1,
     171              :                   matsolidsy1,
     172              :                   matsolidsx2,
     173              :                   matsolidsy2;
     174              :             float matrefractsx1,
     175              :                   matrefractsy1,
     176              :                   matrefractsx2,
     177              :                   matrefractsy2;
     178              :             std::array<uint, lighttilemaxheight> matliquidtiles,
     179              :                                                  matsolidtiles;
     180              :             int hasmats;
     181              :         };
     182              : 
     183              :         struct AlphaInfo final
     184              :         {
     185              :             float alphafrontsx1, alphafrontsx2,
     186              :                   alphafrontsy1, alphafrontsy2,
     187              :                   alphabacksx1, alphabacksx2,
     188              :                   alphabacksy1, alphabacksy2,
     189              :                   alpharefractsx1, alpharefractsx2,
     190              :                   alpharefractsy1, alpharefractsy2;
     191              :             int hasalphavas;
     192              :         };
     193              : 
     194              :         MaterialInfo findmaterials() const; //materials.cpp
     195              :         AlphaInfo findalphavas();
     196              : 
     197              :         struct TransparentModelInfo final
     198              :         {
     199              :             float mdlsx1, mdlsy1, mdlsx2, mdlsy2;
     200              :             std::array<uint, lighttilemaxheight> mdltiles;
     201              : 
     202            1 :             TransparentModelInfo() : mdlsx1(-1), mdlsy1(-1), mdlsx2(1), mdlsy2(1), mdltiles()
     203              :             {
     204            1 :             }
     205              :         };
     206              :         TransparentModelInfo tmodelinfo;
     207              : 
     208              :         std::array<uint, lighttilemaxheight> alphatiles;
     209              : 
     210              :         bool transparentlayer;
     211              :         bool inoq = false;
     212              :         bool gdepthinit;
     213              :         bool hdrfloat;
     214              :         bool msaadepthblit; //no way to change this outside constructor atm
     215              :         bool msaatonemapblit;
     216              : 
     217              :         int scalew,
     218              :             scaleh;
     219              :         //main g-buffers
     220              :         GLuint gfbo,
     221              :                gdepthtex,
     222              :                gcolortex,
     223              :                gnormaltex,
     224              :                gglowtex,
     225              :                gdepthrb,
     226              :                gstencilrb;
     227              :         //multisample antialiasing g-buffers
     228              :         GLuint msfbo,
     229              :                msdepthtex,
     230              :                mscolortex,
     231              :                msnormaltex,
     232              :                msglowtex,
     233              :                msdepthrb,
     234              :                msstencilrb,
     235              :                mshdrfbo,
     236              :                mshdrtex,
     237              :                msrefractfbo,
     238              :                msrefracttex;
     239              :         //refractive g-buffers
     240              :         GLuint refractfbo,
     241              :                refracttex;
     242              :         //rescaling g-buffers
     243              :         std::array<GLuint, 2> scalefbo,
     244              :                               scaletex;
     245              :         GLenum stencilformat;
     246              :         matrix4 eyematrix,
     247              :                 linearworldmatrix;
     248              : 
     249              :     int lightpassesused,
     250              :         lightsvisible,
     251              :         lightsoccluded,
     252              :         lightbatchesused,
     253              :         lightbatchrectsused,
     254              :         lightbatchstacksused;
     255              : 
     256              :     static float refractmargin,
     257              :                  refractdepth;
     258              : };
     259              : 
     260              : extern GBuffer gbuf;
     261              : 
     262              : struct shadowcacheval;
     263              : 
     264              : struct ShadowMapInfo final
     265              : {
     266              :     ushort x, y, size, sidemask;
     267              :     int light;
     268              :     const shadowcacheval *cached;
     269              : };
     270              : 
     271              : extern std::vector<ShadowMapInfo> shadowmaps;
     272              : extern int smfilter;
     273              : 
     274              : extern void addshadowmap(ushort x, ushort y, int size, int &idx, int light = -1, const shadowcacheval *cached = nullptr);
     275              : 
     276              : constexpr int shadowatlassize = 4096;
     277              : 
     278              : extern int smborder, smborder2;
     279              : 
     280              : extern int gdepthstencil, gstencil, glineardepth, msaalineardepth, batchsunlight, smgather;
     281              : extern int lighttilealignw, lighttilealignh, lighttilevieww, lighttileviewh, lighttilew, lighttileh;
     282              : extern int spotlights;       /// the number of spotlights in the world
     283              : extern int volumetriclights; /// the number of volumetric lights in the world
     284              : extern int nospeclights;     /// number of non specular lights in the world
     285              : extern int debugfullscreen;  /// boolean-like, 1 to draw debug on whole screen
     286              : extern int msaaedgedetect;
     287              : extern int hdrclear;
     288              : extern int msaatonemap;
     289              : extern float ldrscale;
     290              : 
     291              : /**
     292              :  * @brief Returns ldrscale, but scaled down  by 255
     293              :  *
     294              :  * @return ldrscale divided by 255
     295              :  */
     296              : extern float ldrscaleb(); //derived from ldrscale
     297              : 
     298              : extern int vieww, viewh;
     299              : 
     300              : enum
     301              : {
     302              :     ShadowMap_None = 0,
     303              :     ShadowMap_Reflect,
     304              :     ShadowMap_CubeMap,
     305              :     ShadowMap_Cascade,
     306              :     ShadowMap_Spot,
     307              : };
     308              : 
     309              : extern int shadowmapping;
     310              : extern int smcullside;
     311              : 
     312              : extern matrix4 shadowmatrix;
     313              : 
     314              : /**
     315              :  * @brief sets values for one of the bilateralshader[] elements
     316              :  *
     317              :  * bilateralshader[2] elements' referenced Shader objects have their radius
     318              :  * and depth parameters changed
     319              :  *
     320              :  * @param radius the bilateral filter radius to set
     321              :  * @param pass [0-1] the element of the bilateralshader() array to change
     322              :  * @param depth the depth of the bilateral filtering to set
     323              :  */
     324              : extern void setbilateralshader(int radius, int pass, float depth);
     325              : 
     326              : /**
     327              :  * @brief clears bilateralarray
     328              :  *
     329              :  * Makes bilateralshader[2]'s elements point to the null pointer..
     330              :  */
     331              : void clearbilateralshaders();
     332              : 
     333              : /**
     334              :  * @brief sets bilateralshader array using bilateralshader()
     335              :  *
     336              :  * Sets bilateralshader[2] elements to point to Shader objects representing the two passes
     337              :  */
     338              : void loadbilateralshaders();
     339              : 
     340              : extern void loaddeferredlightshaders();
     341              : extern void cleardeferredlightshaders();
     342              : extern void clearshadowcache();
     343              : 
     344              : 
     345              : /**
     346              :  * @brief Cleans up shaders and textures associated with volumetric rendering.
     347              :  *
     348              :  * Cleans up the volumetric framebuffers, the volumetric textures, and the volumetric shaders.
     349              :  * Sets the volw/volh variables indicating size of the buffers to -1.
     350              :  */
     351              : extern void cleanupvolumetric();
     352              : 
     353              : extern void findshadowvas();
     354              : extern void findshadowmms();
     355              : 
     356              : extern int calcshadowinfo(const extentity &e, vec &origin, float &radius, vec &spotloc, int &spotangle, float &bias);
     357              : extern void rendershadowmapworld();
     358              : extern void batchshadowmapmodels(bool skipmesh = false);
     359              : extern void renderrsmgeom(bool dyntex = false);
     360              : 
     361              : extern int calcspheresidemask(const vec &p, float radius, float bias);
     362              : extern int calcbbrsmsplits(const ivec &bbmin, const ivec &bbmax);
     363              : extern int calcspherersmsplits(const vec &center, float radius);
     364              : 
     365              : bool sphereinsidespot(const vec &dir, int spot, const vec &center, float radius);
     366              : bool bbinsidespot(const vec &origin, const vec &dir, int spot, const ivec &bbmin, const ivec &bbmax);
     367              : 
     368              : extern matrix4 worldmatrix, screenmatrix;
     369              : 
     370              : extern int gw, gh, gdepthformat, ghasstencil;
     371              : extern int msaasamples, msaalight;
     372              : extern std::vector<vec2> msaapositions;
     373              : 
     374              : extern int rhinoq;
     375              : 
     376              : extern bool shouldworkinoq();
     377              : extern void initgbuffer();
     378              : 
     379              : /**
     380              :  * @brief Returns whether to pack normal maps into the alpha channel
     381              :  *
     382              :  * Packing normals into alpha channel will be done if `forcepacknorm` is enabled
     383              :  * of if `msaasamples` is nonzero or if the avatar mask is disabled
     384              :  *
     385              :  * @return true if normals are to be packed into the alpha channel
     386              :  */
     387              : extern bool usepacknorm();
     388              : 
     389              : /**
     390              :  * @brief Calls gldrawBuffers with the buffers specified.
     391              :  *
     392              :  * The value of the mask intput determines which of the buffers are activated.
     393              :  * The buffers 0-3 are set by the chars 'c' 'n' 'g' 'd' respectively. The depth
     394              :  * buffer 'd' will only be activated if gdepthformat is enabled.
     395              :  *
     396              :  * For example, maskgbuffer("cnd") will enable the buffers GL_COLOR_ATTACHMENT0,1,3.
     397              :  *
     398              :  * @param mask string determining which buffers to use
     399              :  */
     400              : extern void maskgbuffer(const char *mask);
     401              : extern void shadegbuffer();
     402              : extern void setuplights(GBuffer &buf);
     403              : 
     404              : /**
     405              :  * @brief Draws any enabled debug buffers.
     406              :  *
     407              :  * Draws the following debug buffers in the following order:
     408              :  *  - ao (debugao)
     409              :  *  - shadowatlas (debushadowatlas)
     410              :  *  - depth (debugdepth)
     411              :  *  - stencil (debugstencil)
     412              :  *  - refract (debugrefract)
     413              :  *  - light scissor (debuglightscissor)
     414              :  *  - rsm (debugrsm)
     415              :  *  - rh (debugrh)
     416              :  *  - aa (subpixelaa::Debug, debugtqaa)
     417              :  *
     418              :  * Later elements on this list will draw over the top of earlier ones. The showing
     419              :  * of these elements are controlled by their respective variables (in parentheses).
     420              :  *
     421              :  * @return true if any debug buffer was drawn, false otherwise.
     422              :  */
     423              : extern bool debuglights();
     424              : extern void cleanuplights();
     425              : 
     426              : extern int avatarmask;
     427              : extern void enableavatarmask();
     428              : extern void disableavatarmask();
     429              : 
     430              : template<class T>
     431            0 : inline void calctilebounds(float sx1, float sy1, float sx2, float sy2, T &bx1, T &by1, T &bx2, T &by2)
     432              : {
     433            0 :     int tx1 = std::max(static_cast<int>(std::floor(((sx1 + 1)*0.5f*vieww)/lighttilealignw)), 0),
     434            0 :         ty1 = std::max(static_cast<int>(std::floor(((sy1 + 1)*0.5f*viewh)/lighttilealignh)), 0),
     435            0 :         tx2 = std::min(static_cast<int>(std::ceil(((sx2 + 1)*0.5f*vieww)/lighttilealignw)), lighttilevieww),
     436            0 :         ty2 = std::min(static_cast<int>(std::ceil(((sy2 + 1)*0.5f*viewh)/lighttilealignh)), lighttileviewh);
     437            0 :     bx1 = T((tx1 * lighttilew) / lighttilevieww);
     438            0 :     by1 = T((ty1 * lighttileh) / lighttileviewh);
     439            0 :     bx2 = T((tx2 * lighttilew + lighttilevieww - 1) / lighttilevieww);
     440            0 :     by2 = T((ty2 * lighttileh + lighttileviewh - 1) / lighttileviewh);
     441            0 : }
     442              : 
     443              : #endif
        

Generated by: LCOV version 2.0-1