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 : GLuint shouldscale() const;
87 : void workinoq();
88 :
89 : /**
90 : * @brief Creates the geometry buffer for the scene
91 : *
92 : * Renders and copies a fbo (framebuffer object) to msfbo (multisample framebuffer object)
93 : * or gfbo (geometry buffer framebuffer object) depending on whether msaa is enabled
94 : *
95 : * @param bool depthclear toggles clearing the depth buffer
96 : * @param gamefxn pointer to a function for game-specific rendering
97 : */
98 : void rendergbuffer(bool depthclear = true, void (*gamefxn)() = dummyfxn);
99 : bool istransparentlayer() const;
100 : void rendermodelbatches();
101 : 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);
102 :
103 : /**
104 : * @brief Returns debug information about lighting.
105 : *
106 : * Types:
107 : * 0: light passes used
108 : * 1: lights visible
109 : * 2: lights occluded
110 : * 3: light batches used
111 : * 4: light batch rects used
112 : * 5: light batch stacks used
113 : *
114 : * @param type of value to query
115 : *
116 : * @return numeric value of appropriate type
117 : */
118 : int getlightdebuginfo(uint type) const;
119 : private:
120 : void bindmsdepth() const;
121 : void bindlighttexs(int msaapass, bool transparent) const; //only used in renderlights
122 : void cleanupscale();
123 : void cleanupmsbuffer();
124 : void preparegbuffer(bool depthclear = true);
125 : void rendercsmshadowmaps() const;
126 : void rendershadowmaps(int offset = 0) const;
127 : void rendersunpass(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask);
128 : void renderlightsnobatch(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2);
129 : void renderlightbatches(Shader &s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask);
130 : void rendergeom();
131 :
132 : void alphaparticles(float allsx1, float allsy1, float allsx2, float allsy2) const;
133 :
134 : void rendermaterialmask() const;
135 : void renderliquidmaterials() const;
136 : void packlights();
137 :
138 : struct MaterialInfo final
139 : {
140 : float matliquidsx1,
141 : matliquidsy1,
142 : matliquidsx2,
143 : matliquidsy2;
144 : float matsolidsx1,
145 : matsolidsy1,
146 : matsolidsx2,
147 : matsolidsy2;
148 : float matrefractsx1,
149 : matrefractsy1,
150 : matrefractsx2,
151 : matrefractsy2;
152 : std::array<uint, lighttilemaxheight> matliquidtiles,
153 : matsolidtiles;
154 : int hasmats;
155 : };
156 :
157 : struct AlphaInfo final
158 : {
159 : float alphafrontsx1, alphafrontsx2,
160 : alphafrontsy1, alphafrontsy2,
161 : alphabacksx1, alphabacksx2,
162 : alphabacksy1, alphabacksy2,
163 : alpharefractsx1, alpharefractsx2,
164 : alpharefractsy1, alpharefractsy2;
165 : int hasalphavas;
166 : };
167 :
168 : MaterialInfo findmaterials() const; //materials.cpp
169 : AlphaInfo findalphavas();
170 :
171 : struct TransparentModelInfo final
172 : {
173 : float mdlsx1, mdlsy1, mdlsx2, mdlsy2;
174 : std::array<uint, lighttilemaxheight> mdltiles;
175 :
176 1 : TransparentModelInfo() : mdlsx1(-1), mdlsy1(-1), mdlsx2(1), mdlsy2(1), mdltiles()
177 : {
178 1 : }
179 : };
180 : TransparentModelInfo tmodelinfo;
181 :
182 : std::array<uint, lighttilemaxheight> alphatiles;
183 :
184 : bool transparentlayer;
185 : bool inoq = false;
186 : bool gdepthinit;
187 : bool hdrfloat;
188 : bool msaadepthblit; //no way to change this outside constructor atm
189 : bool msaatonemapblit;
190 :
191 : int scalew,
192 : scaleh;
193 : //main g-buffers
194 : GLuint gfbo,
195 : gdepthtex,
196 : gcolortex,
197 : gnormaltex,
198 : gglowtex,
199 : gdepthrb,
200 : gstencilrb;
201 : //multisample antialiasing g-buffers
202 : GLuint msfbo,
203 : msdepthtex,
204 : mscolortex,
205 : msnormaltex,
206 : msglowtex,
207 : msdepthrb,
208 : msstencilrb,
209 : mshdrfbo,
210 : mshdrtex,
211 : msrefractfbo,
212 : msrefracttex;
213 : //refractive g-buffers
214 : GLuint refractfbo,
215 : refracttex;
216 : //rescaling g-buffers
217 : std::array<GLuint, 2> scalefbo,
218 : scaletex;
219 : GLenum stencilformat;
220 : matrix4 eyematrix,
221 : linearworldmatrix;
222 :
223 : int lightpassesused,
224 : lightsvisible,
225 : lightsoccluded,
226 : lightbatchesused,
227 : lightbatchrectsused,
228 : lightbatchstacksused;
229 :
230 : static float refractmargin,
231 : refractdepth;
232 : };
233 :
234 : extern GBuffer gbuf;
235 :
236 : struct shadowcacheval;
237 :
238 : struct ShadowMapInfo final
239 : {
240 : ushort x, y, size, sidemask;
241 : int light;
242 : const shadowcacheval *cached;
243 : };
244 :
245 : extern std::vector<ShadowMapInfo> shadowmaps;
246 : extern int smfilter;
247 :
248 : extern void addshadowmap(ushort x, ushort y, int size, int &idx, int light = -1, const shadowcacheval *cached = nullptr);
249 :
250 : constexpr int shadowatlassize = 4096;
251 :
252 : extern int smborder, smborder2;
253 :
254 : extern int gdepthstencil, gstencil, glineardepth, msaalineardepth, batchsunlight, smgather, tqaaresolvegather;
255 : extern int lighttilealignw, lighttilealignh, lighttilevieww, lighttileviewh, lighttilew, lighttileh;
256 : extern int spotlights; /// the number of spotlights in the world
257 : extern int volumetriclights; /// the number of volumetric lights in the world
258 : extern int nospeclights; /// number of non specular lights in the world
259 : extern int debugfullscreen; /// boolean-like, 1 to draw debug on whole screen
260 : extern int msaaedgedetect;
261 : extern int hdrclear;
262 : extern int msaatonemap;
263 : extern float ldrscale;
264 :
265 : /**
266 : * @brief Returns ldrscale, but scaled down by 255
267 : *
268 : * @return ldrscale divided by 255
269 : */
270 : extern float ldrscaleb(); //derived from ldrscale
271 :
272 : extern int vieww, viewh;
273 :
274 : enum
275 : {
276 : ShadowMap_None = 0,
277 : ShadowMap_Reflect,
278 : ShadowMap_CubeMap,
279 : ShadowMap_Cascade,
280 : ShadowMap_Spot,
281 : };
282 :
283 : extern int shadowmapping;
284 : extern int smcullside;
285 :
286 : extern matrix4 shadowmatrix;
287 :
288 : /**
289 : * @brief sets values for one of the bilateralshader[] elements
290 : *
291 : * bilateralshader[2] elements' referenced Shader objects have their radius
292 : * and depth parameters changed
293 : *
294 : * @param radius the bilateral filter radius to set
295 : * @param pass [0-1] the element of the bilateralshader() array to change
296 : * @param depth the depth of the bilateral filtering to set
297 : */
298 : extern void setbilateralshader(int radius, int pass, float depth);
299 :
300 : /**
301 : * @brief clears bilateralarray
302 : *
303 : * Makes bilateralshader[2]'s elements point to the null pointer..
304 : */
305 : void clearbilateralshaders();
306 :
307 : /**
308 : * @brief sets bilateralshader array using bilateralshader()
309 : *
310 : * Sets bilateralshader[2] elements to point to Shader objects representing the two passes
311 : */
312 : void loadbilateralshaders();
313 :
314 : extern void loaddeferredlightshaders();
315 : extern void cleardeferredlightshaders();
316 : extern void clearshadowcache();
317 :
318 :
319 : /**
320 : * @brief Cleans up shaders and textures associated with volumetric rendering.
321 : *
322 : * Cleans up the volumetric framebuffers, the volumetric textures, and the volumetric shaders.
323 : * Sets the volw/volh variables indicating size of the buffers to -1.
324 : */
325 : extern void cleanupvolumetric();
326 :
327 : extern void findshadowvas();
328 : extern void findshadowmms();
329 :
330 : extern int calcshadowinfo(const extentity &e, vec &origin, float &radius, vec &spotloc, int &spotangle, float &bias);
331 : extern void rendershadowmapworld();
332 : extern void batchshadowmapmodels(bool skipmesh = false);
333 : extern void renderrsmgeom(bool dyntex = false);
334 :
335 : extern int calcspheresidemask(const vec &p, float radius, float bias);
336 : extern int calcbbrsmsplits(const ivec &bbmin, const ivec &bbmax);
337 : extern int calcspherersmsplits(const vec ¢er, float radius);
338 :
339 : bool sphereinsidespot(const vec &dir, int spot, const vec ¢er, float radius);
340 : bool bbinsidespot(const vec &origin, const vec &dir, int spot, const ivec &bbmin, const ivec &bbmax);
341 :
342 : extern matrix4 worldmatrix, screenmatrix;
343 :
344 : extern int gw, gh, gdepthformat, ghasstencil;
345 : extern int msaasamples, msaalight;
346 : extern std::vector<vec2> msaapositions;
347 :
348 : extern int rhinoq;
349 :
350 : extern bool shouldworkinoq();
351 : extern void initgbuffer();
352 :
353 : /**
354 : * @brief Returns whether to pack normal maps into the alpha channel
355 : *
356 : * Packing normals into alpha channel will be done if `forcepacknorm` is enabled
357 : * of if `msaasamples` is nonzero or if the avatar mask is disabled
358 : *
359 : * @return true if normals are to be packed into the alpha channel
360 : */
361 : extern bool usepacknorm();
362 : extern void maskgbuffer(const char *mask);
363 : extern void shadegbuffer();
364 : extern void setuplights(GBuffer &buf);
365 :
366 : /**
367 : * @brief Draws any enabled debug buffers.
368 : *
369 : * Draws the following debug buffers in the following order:
370 : * - ao (debugao)
371 : * - shadowatlas (debushadowatlas)
372 : * - depth (debugdepth)
373 : * - stencil (debugstencil)
374 : * - refract (debugrefract)
375 : * - light scissor (debuglightscissor)
376 : * - rsm (debugrsm)
377 : * - rh (debugrh)
378 : * - aa (subpixelaa::Debug, debugtqaa)
379 : *
380 : * Later elements on this list will draw over the top of earlier ones. The showing
381 : * of these elements are controlled by their respective variables (in parentheses).
382 : *
383 : * @return true if any debug buffer was drawn, false otherwise.
384 : */
385 : extern bool debuglights();
386 : extern void cleanuplights();
387 :
388 : extern int avatarmask;
389 : extern void enableavatarmask();
390 : extern void disableavatarmask();
391 :
392 : template<class T>
393 0 : inline void calctilebounds(float sx1, float sy1, float sx2, float sy2, T &bx1, T &by1, T &bx2, T &by2)
394 : {
395 0 : int tx1 = std::max(static_cast<int>(std::floor(((sx1 + 1)*0.5f*vieww)/lighttilealignw)), 0),
396 0 : ty1 = std::max(static_cast<int>(std::floor(((sy1 + 1)*0.5f*viewh)/lighttilealignh)), 0),
397 0 : tx2 = std::min(static_cast<int>(std::ceil(((sx2 + 1)*0.5f*vieww)/lighttilealignw)), lighttilevieww),
398 0 : ty2 = std::min(static_cast<int>(std::ceil(((sy2 + 1)*0.5f*viewh)/lighttilealignh)), lighttileviewh);
399 0 : bx1 = T((tx1 * lighttilew) / lighttilevieww);
400 0 : by1 = T((ty1 * lighttileh) / lighttileviewh);
401 0 : bx2 = T((tx2 * lighttilew + lighttilevieww - 1) / lighttilevieww);
402 0 : by2 = T((ty2 * lighttileh + lighttileviewh - 1) / lighttileviewh);
403 0 : }
404 :
405 : #endif
|