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