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 : /* gbuffer: a singleton object used to store the graphics buffers
8 : * (as OpenGL uints) and the functions which act upon the g-buffers.
9 : */
10 : class GBuffer final
11 : {
12 : public:
13 1 : GBuffer()
14 1 : {
15 : //set all of the textures to 0/null
16 1 : scalew = 0;
17 1 : scaleh = 0;
18 1 : gfbo = 0;
19 1 : gdepthtex = 0;
20 1 : gcolortex = 0;
21 1 : gnormaltex = 0;
22 1 : gglowtex = 0;
23 1 : gdepthrb = 0;
24 1 : gstencilrb = 0;
25 1 : msfbo = 0;
26 1 : msdepthtex = 0;
27 1 : mscolortex = 0;
28 1 : msnormaltex = 0;
29 1 : msglowtex = 0;
30 1 : msdepthrb = 0;
31 1 : msstencilrb = 0;
32 1 : mshdrfbo = 0;
33 1 : mshdrtex = 0;
34 1 : msrefractfbo = 0;
35 1 : msrefracttex = 0;
36 1 : refractfbo = 0;
37 1 : refracttex = 0;
38 1 : stencilformat= 0;
39 1 : scalefbo.fill(0);
40 1 : scaletex.fill(0);
41 1 : gdepthinit = false;
42 1 : hdrfloat = false;
43 1 : msaadepthblit= false;
44 1 : msaatonemapblit = false;
45 1 : inoq = false;
46 1 : transparentlayer = 0;
47 :
48 1 : }
49 : static void dummyfxn();
50 : //main g-buffers
51 : void cleanupgbuffer();
52 : void renderao() const; //ao.cpp
53 : void renderradiancehints() const; //radiancehints.cpp
54 : void rendertransparent(); //renderalpha.cpp
55 : void resolvemsaadepth(int w, int h) const;
56 : void setupgbuffer();
57 : void bindgdepth() const;
58 : void renderparticles(int layer = 0) const; //renderparticles.cpp
59 : void rendervolumetric();
60 : void renderwaterfog(int mat, float surface); //water.cpp
61 : void setaavelocityparams(GLenum tmu = GL_TEXTURE0); //aa.cpp
62 : void shademodelpreview(int x, int y, int w, int h, bool background = true, bool scissor = false);
63 : void viewdepth() const;
64 : void rendershadowatlas();
65 : //multisample antialiasing specific buffers
66 : void setupmsbuffer(int w, int h);
67 : void resolvemsaacolor(int w, int h);
68 : void shademinimap(const vec &color = vec(-1, -1, -1));
69 : void shadesky() const;
70 : //refractive
71 : void processhdr(GLuint outfbo, int aa);
72 : void viewrefract();
73 : void doscale(GLuint outfbo = 0) const;
74 : void setupscale(int sw, int sh, int w, int h);
75 : GLuint shouldscale() const;
76 : void workinoq();
77 :
78 : /**
79 : * @brief Creates the geometry buffer for the scene
80 : *
81 : * Renders and copies a fbo (framebuffer object) to msfbo (multisample framebuffer object)
82 : * or gfbo (geometry buffer framebuffer object) depending on whether msaa is enabled
83 : *
84 : * @param bool depthclear toggles clearing the depth buffer
85 : * @param gamefxn pointer to a function for game-specific rendering
86 : */
87 : void rendergbuffer(bool depthclear = true, void (*gamefxn)() = dummyfxn);
88 : bool istransparentlayer() const;
89 : void rendermodelbatches();
90 : 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);
91 :
92 : /**
93 : * @brief Returns debug information about lighting.
94 : *
95 : * Types:
96 : * 0: light passes used
97 : * 1: lights visible
98 : * 2: lights occluded
99 : * 3: light batches used
100 : * 4: light batch rects used
101 : * 5: light batch stacks used
102 : *
103 : * @param type of value to query
104 : *
105 : * @return numeric value of appropriate type
106 : */
107 : int getlightdebuginfo(uint type) const;
108 : private:
109 : void bindmsdepth() const;
110 : void bindlighttexs(int msaapass, bool transparent) const; //only used in renderlights
111 : void cleanupscale();
112 : void cleanupmsbuffer();
113 : void preparegbuffer(bool depthclear = true);
114 : void rendercsmshadowmaps() const;
115 : void rendershadowmaps(int offset = 0) const;
116 : void rendersunpass(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask);
117 : void renderlightsnobatch(Shader *s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2);
118 : void renderlightbatches(Shader &s, int stencilref, bool transparent, float bsx1, float bsy1, float bsx2, float bsy2, const uint *tilemask);
119 : void rendergeom();
120 :
121 : void alphaparticles(float allsx1, float allsy1, float allsx2, float allsy2) const;
122 :
123 : void rendermaterialmask() const;
124 : void renderliquidmaterials() const;
125 : void packlights();
126 :
127 : struct MaterialInfo final
128 : {
129 : float matliquidsx1,
130 : matliquidsy1,
131 : matliquidsx2,
132 : matliquidsy2;
133 : float matsolidsx1,
134 : matsolidsy1,
135 : matsolidsx2,
136 : matsolidsy2;
137 : float matrefractsx1,
138 : matrefractsy1,
139 : matrefractsx2,
140 : matrefractsy2;
141 : uint matliquidtiles[lighttilemaxheight],
142 : matsolidtiles[lighttilemaxheight];
143 : int hasmats;
144 : };
145 :
146 : struct AlphaInfo final
147 : {
148 : float alphafrontsx1, alphafrontsx2,
149 : alphafrontsy1, alphafrontsy2,
150 : alphabacksx1, alphabacksx2,
151 : alphabacksy1, alphabacksy2,
152 : alpharefractsx1, alpharefractsx2,
153 : alpharefractsy1, alpharefractsy2;
154 : int hasalphavas;
155 : };
156 :
157 : MaterialInfo findmaterials() const; //materials.cpp
158 : AlphaInfo findalphavas();
159 :
160 : struct transparentmodelinfo final
161 : {
162 : float mdlsx1, mdlsy1, mdlsx2, mdlsy2;
163 : std::array<uint, lighttilemaxheight> mdltiles;
164 :
165 1 : transparentmodelinfo() : mdlsx1(-1), mdlsy1(-1), mdlsx2(1), mdlsy2(1), mdltiles()
166 : {
167 1 : }
168 : };
169 : transparentmodelinfo tmodelinfo;
170 :
171 : uint alphatiles[lighttilemaxheight];
172 :
173 : bool transparentlayer;
174 : bool inoq = false;
175 : bool gdepthinit;
176 : bool hdrfloat;
177 : bool msaadepthblit; //no way to change this outside constructor atm
178 : bool msaatonemapblit;
179 :
180 : int scalew,
181 : scaleh;
182 : //main g-buffers
183 : GLuint gfbo,
184 : gdepthtex,
185 : gcolortex,
186 : gnormaltex,
187 : gglowtex,
188 : gdepthrb,
189 : gstencilrb;
190 : //multisample antialiasing g-buffers
191 : GLuint msfbo,
192 : msdepthtex,
193 : mscolortex,
194 : msnormaltex,
195 : msglowtex,
196 : msdepthrb,
197 : msstencilrb,
198 : mshdrfbo,
199 : mshdrtex,
200 : msrefractfbo,
201 : msrefracttex;
202 : //refractive g-buffers
203 : GLuint refractfbo,
204 : refracttex;
205 : //rescaling g-buffers
206 : std::array<GLuint, 2> scalefbo,
207 : scaletex;
208 : GLenum stencilformat;
209 : matrix4 eyematrix,
210 : linearworldmatrix;
211 :
212 : int lightpassesused,
213 : lightsvisible,
214 : lightsoccluded,
215 : lightbatchesused,
216 : lightbatchrectsused,
217 : lightbatchstacksused;
218 :
219 : static float refractmargin,
220 : refractdepth;
221 : };
222 :
223 : extern GBuffer gbuf;
224 :
225 : class PackNode
226 : {
227 : public:
228 14 : PackNode(ushort x, ushort y, ushort w, ushort h) : w(w), h(h), child1(0), child2(0), x(x), y(y), available(std::min(w, h)) {}
229 :
230 1 : void reset()
231 : {
232 1 : discardchildren();
233 1 : available = std::min(w, h);
234 1 : }
235 :
236 1 : bool resize(int nw, int nh)
237 : {
238 1 : if(w == nw && h == nw)
239 : {
240 0 : return false;
241 : }
242 1 : discardchildren();
243 1 : w = nw;
244 1 : h = nh;
245 1 : available = std::min(w, h);
246 1 : return true;
247 : }
248 :
249 13 : ~PackNode()
250 : {
251 13 : discardchildren();
252 13 : }
253 :
254 : bool insert(ushort &tx, ushort &ty, ushort tw, ushort th);
255 : void reserve(ushort tx, ushort ty, ushort tw, ushort th);
256 :
257 3 : int availablespace() const
258 : {
259 3 : return available;
260 : }
261 :
262 2 : vec2 dimensions() const
263 : {
264 2 : return {static_cast<float>(w), static_cast<float>(h)};
265 : }
266 :
267 : //debugging printouts, not used in program logic
268 :
269 : //i: recursion depth
270 7 : void printchildren(int i = 0) const
271 : {
272 7 : print(i);
273 :
274 7 : if(child1)
275 : {
276 3 : child1->printchildren(i+1);
277 : }
278 7 : if(child2)
279 : {
280 3 : child2->printchildren(i+1);
281 : }
282 7 : }
283 :
284 : //i: depth to print out
285 7 : void print(int i) const
286 : {
287 7 : std::printf("%d: %d %d\n", i, w, h);
288 7 : }
289 :
290 : private:
291 : ushort w, h;
292 : PackNode *child1, *child2;
293 : ushort x, y;
294 : int available;
295 :
296 18 : void discardchildren()
297 : {
298 18 : if(child1)
299 : {
300 6 : delete child1;
301 6 : child1 = nullptr;
302 : }
303 18 : if(child2)
304 : {
305 6 : delete child2;
306 6 : child2 = nullptr;
307 : }
308 18 : }
309 :
310 : void forceempty()
311 : {
312 : discardchildren();
313 : available = 0;
314 : }
315 : };
316 :
317 : extern PackNode shadowatlaspacker;
318 :
319 : struct shadowcacheval;
320 :
321 : struct shadowmapinfo
322 : {
323 : ushort x, y, size, sidemask;
324 : int light;
325 : const shadowcacheval *cached;
326 : };
327 :
328 : extern std::vector<shadowmapinfo> shadowmaps;
329 : extern int smfilter;
330 :
331 : extern void addshadowmap(ushort x, ushort y, int size, int &idx, int light = -1, const shadowcacheval *cached = nullptr);
332 :
333 : constexpr int shadowatlassize = 4096;
334 :
335 : extern int smborder, smborder2;
336 :
337 : extern int gdepthstencil, gstencil, glineardepth, msaalineardepth, batchsunlight, smgather, tqaaresolvegather;
338 : extern int lighttilealignw, lighttilealignh, lighttilevieww, lighttileviewh, lighttilew, lighttileh;
339 : extern int spotlights;
340 : extern int volumetriclights;
341 : extern int nospeclights;
342 : extern int debugfullscreen;
343 : extern int msaaedgedetect;
344 : extern int hdrclear;
345 : extern int msaatonemap;
346 : extern float ldrscale;
347 : extern float ldrscaleb(); //derived from ldrscale
348 :
349 : extern int vieww, viewh;
350 :
351 : enum
352 : {
353 : ShadowMap_None = 0,
354 : ShadowMap_Reflect,
355 : ShadowMap_CubeMap,
356 : ShadowMap_Cascade,
357 : ShadowMap_Spot,
358 : };
359 :
360 : extern int shadowmapping;
361 : extern int smcullside;
362 :
363 : extern matrix4 shadowmatrix;
364 :
365 : extern void setbilateralshader(int radius, int pass, float depth);
366 : void clearbilateralshaders();
367 : void loadbilateralshaders();
368 :
369 : extern void loaddeferredlightshaders();
370 : extern void cleardeferredlightshaders();
371 : extern void clearshadowcache();
372 :
373 : extern void cleanupvolumetric();
374 :
375 : extern void findshadowvas();
376 : extern void findshadowmms();
377 :
378 : extern int calcshadowinfo(const extentity &e, vec &origin, float &radius, vec &spotloc, int &spotangle, float &bias);
379 : extern void rendershadowmapworld();
380 : extern void batchshadowmapmodels(bool skipmesh = false);
381 : extern void renderrsmgeom(bool dyntex = false);
382 :
383 : extern int calcspheresidemask(const vec &p, float radius, float bias);
384 : extern int calcbbrsmsplits(const ivec &bbmin, const ivec &bbmax);
385 : extern int calcspherersmsplits(const vec ¢er, float radius);
386 :
387 0 : inline bool sphereinsidespot(const vec &dir, int spot, const vec ¢er, float radius)
388 : {
389 0 : const vec2 &sc = sincos360[spot];
390 0 : float cdist = dir.dot(center),
391 0 : cradius = radius + sc.y*cdist;
392 0 : return sc.x*sc.x*(center.dot(center) - cdist*cdist) <= cradius*cradius;
393 : }
394 0 : inline bool bbinsidespot(const vec &origin, const vec &dir, int spot, const ivec &bbmin, const ivec &bbmax)
395 : {
396 0 : vec radius = vec(ivec(bbmax).sub(bbmin)).mul(0.5f),
397 0 : center = vec(bbmin).add(radius);
398 0 : return sphereinsidespot(dir, spot, center.sub(origin), radius.magnitude());
399 : }
400 :
401 : extern matrix4 worldmatrix, screenmatrix;
402 :
403 : extern int gw, gh, gdepthformat, ghasstencil;
404 : extern int msaasamples, msaalight;
405 : extern std::vector<vec2> msaapositions;
406 :
407 : extern int rhinoq;
408 : extern int rsmcull;
409 : extern GLuint rhfbo;
410 :
411 : extern bool shouldworkinoq();
412 : extern void initgbuffer();
413 : extern bool usepacknorm();
414 : extern void maskgbuffer(const char *mask);
415 : extern void shadegbuffer();
416 : extern void setuplights(GBuffer &buf);
417 : extern bool debuglights();
418 : extern void cleanuplights();
419 :
420 : extern int avatarmask;
421 : extern void enableavatarmask();
422 : extern void disableavatarmask();
423 :
424 : template<class T>
425 0 : inline void calctilebounds(float sx1, float sy1, float sx2, float sy2, T &bx1, T &by1, T &bx2, T &by2)
426 : {
427 0 : int tx1 = std::max(static_cast<int>(std::floor(((sx1 + 1)*0.5f*vieww)/lighttilealignw)), 0),
428 0 : ty1 = std::max(static_cast<int>(std::floor(((sy1 + 1)*0.5f*viewh)/lighttilealignh)), 0),
429 0 : tx2 = std::min(static_cast<int>(std::ceil(((sx2 + 1)*0.5f*vieww)/lighttilealignw)), lighttilevieww),
430 0 : ty2 = std::min(static_cast<int>(std::ceil(((sy2 + 1)*0.5f*viewh)/lighttilealignh)), lighttileviewh);
431 0 : bx1 = T((tx1 * lighttilew) / lighttilevieww);
432 0 : by1 = T((ty1 * lighttileh) / lighttileviewh);
433 0 : bx2 = T((tx2 * lighttilew + lighttilevieww - 1) / lighttilevieww);
434 0 : by2 = T((ty2 * lighttileh + lighttileviewh - 1) / lighttileviewh);
435 0 : }
436 :
437 : #endif
|