1/**************************************************************************/
2/* light_storage.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef LIGHT_STORAGE_GLES3_H
32#define LIGHT_STORAGE_GLES3_H
33
34#ifdef GLES3_ENABLED
35
36#include "core/templates/local_vector.h"
37#include "core/templates/rid_owner.h"
38#include "core/templates/self_list.h"
39#include "drivers/gles3/storage/texture_storage.h"
40#include "servers/rendering/renderer_compositor.h"
41#include "servers/rendering/storage/light_storage.h"
42#include "servers/rendering/storage/utilities.h"
43
44#include "platform_config.h"
45#ifndef OPENGL_INCLUDE_H
46#include <GLES3/gl3.h>
47#else
48#include OPENGL_INCLUDE_H
49#endif
50
51namespace GLES3 {
52
53/* LIGHT */
54
55struct Light {
56 RS::LightType type;
57 float param[RS::LIGHT_PARAM_MAX];
58 Color color = Color(1, 1, 1, 1);
59 RID projector;
60 bool shadow = false;
61 bool negative = false;
62 bool reverse_cull = false;
63 RS::LightBakeMode bake_mode = RS::LIGHT_BAKE_DYNAMIC;
64 uint32_t max_sdfgi_cascade = 2;
65 uint32_t cull_mask = 0xFFFFFFFF;
66 bool distance_fade = false;
67 real_t distance_fade_begin = 40.0;
68 real_t distance_fade_shadow = 50.0;
69 real_t distance_fade_length = 10.0;
70 RS::LightOmniShadowMode omni_shadow_mode = RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
71 RS::LightDirectionalShadowMode directional_shadow_mode = RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
72 bool directional_blend_splits = false;
73 RS::LightDirectionalSkyMode directional_sky_mode = RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY;
74 uint64_t version = 0;
75
76 Dependency dependency;
77};
78
79/* Light instance */
80struct LightInstance {
81 RS::LightType light_type = RS::LIGHT_DIRECTIONAL;
82
83 AABB aabb;
84 RID self;
85 RID light;
86 Transform3D transform;
87
88 Vector3 light_vector;
89 Vector3 spot_vector;
90 float linear_att = 0.0;
91
92 uint64_t shadow_pass = 0;
93 uint64_t last_scene_pass = 0;
94 uint64_t last_scene_shadow_pass = 0;
95 uint64_t last_pass = 0;
96 uint32_t cull_mask = 0;
97 uint32_t light_directional_index = 0;
98
99 Rect2 directional_rect;
100
101 uint32_t gl_id = -1;
102
103 LightInstance() {}
104};
105
106/* REFLECTION PROBE */
107
108struct ReflectionProbe {
109 RS::ReflectionProbeUpdateMode update_mode = RS::REFLECTION_PROBE_UPDATE_ONCE;
110 int resolution = 256;
111 float intensity = 1.0;
112 RS::ReflectionProbeAmbientMode ambient_mode = RS::REFLECTION_PROBE_AMBIENT_ENVIRONMENT;
113 Color ambient_color;
114 float ambient_color_energy = 1.0;
115 float max_distance = 0;
116 Vector3 size = Vector3(20, 20, 20);
117 Vector3 origin_offset;
118 bool interior = false;
119 bool box_projection = false;
120 bool enable_shadows = false;
121 uint32_t cull_mask = (1 << 20) - 1;
122 float mesh_lod_threshold = 0.01;
123 float baked_exposure = 1.0;
124
125 Dependency dependency;
126};
127
128/* LIGHTMAP */
129
130struct Lightmap {
131 RID light_texture;
132 bool uses_spherical_harmonics = false;
133 bool interior = false;
134 AABB bounds = AABB(Vector3(), Vector3(1, 1, 1));
135 float baked_exposure = 1.0;
136 int32_t array_index = -1; //unassigned
137 PackedVector3Array points;
138 PackedColorArray point_sh;
139 PackedInt32Array tetrahedra;
140 PackedInt32Array bsp_tree;
141
142 struct BSP {
143 static const int32_t EMPTY_LEAF = INT32_MIN;
144 float plane[4];
145 int32_t over = EMPTY_LEAF, under = EMPTY_LEAF;
146 };
147
148 Dependency dependency;
149};
150
151class LightStorage : public RendererLightStorage {
152private:
153 static LightStorage *singleton;
154
155 /* LIGHT */
156 mutable RID_Owner<Light, true> light_owner;
157
158 /* Light instance */
159 mutable RID_Owner<LightInstance> light_instance_owner;
160
161 /* REFLECTION PROBE */
162 mutable RID_Owner<ReflectionProbe, true> reflection_probe_owner;
163
164 /* LIGHTMAP */
165
166 Vector<RID> lightmap_textures;
167
168 mutable RID_Owner<Lightmap, true> lightmap_owner;
169
170public:
171 static LightStorage *get_singleton();
172
173 LightStorage();
174 virtual ~LightStorage();
175
176 /* Light API */
177
178 Light *get_light(RID p_rid) { return light_owner.get_or_null(p_rid); };
179 bool owns_light(RID p_rid) { return light_owner.owns(p_rid); };
180
181 void _light_initialize(RID p_rid, RS::LightType p_type);
182
183 virtual RID directional_light_allocate() override;
184 virtual void directional_light_initialize(RID p_rid) override;
185 virtual RID omni_light_allocate() override;
186 virtual void omni_light_initialize(RID p_rid) override;
187 virtual RID spot_light_allocate() override;
188 virtual void spot_light_initialize(RID p_rid) override;
189
190 virtual void light_free(RID p_rid) override;
191
192 virtual void light_set_color(RID p_light, const Color &p_color) override;
193 virtual void light_set_param(RID p_light, RS::LightParam p_param, float p_value) override;
194 virtual void light_set_shadow(RID p_light, bool p_enabled) override;
195 virtual void light_set_projector(RID p_light, RID p_texture) override;
196 virtual void light_set_negative(RID p_light, bool p_enable) override;
197 virtual void light_set_cull_mask(RID p_light, uint32_t p_mask) override;
198 virtual void light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) override;
199 virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) override;
200 virtual void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) override;
201 virtual void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) override {}
202
203 virtual void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) override;
204
205 virtual void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) override;
206 virtual void light_directional_set_blend_splits(RID p_light, bool p_enable) override;
207 virtual bool light_directional_get_blend_splits(RID p_light) const override;
208 virtual void light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) override;
209 virtual RS::LightDirectionalSkyMode light_directional_get_sky_mode(RID p_light) const override;
210
211 virtual RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) override;
212 virtual RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) override;
213 virtual RS::LightType light_get_type(RID p_light) const override {
214 const Light *light = light_owner.get_or_null(p_light);
215 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
216
217 return light->type;
218 }
219 virtual AABB light_get_aabb(RID p_light) const override;
220
221 virtual float light_get_param(RID p_light, RS::LightParam p_param) override {
222 const Light *light = light_owner.get_or_null(p_light);
223 ERR_FAIL_NULL_V(light, 0);
224
225 return light->param[p_param];
226 }
227
228 _FORCE_INLINE_ RID light_get_projector(RID p_light) {
229 const Light *light = light_owner.get_or_null(p_light);
230 ERR_FAIL_NULL_V(light, RID());
231
232 return light->projector;
233 }
234
235 virtual Color light_get_color(RID p_light) override {
236 const Light *light = light_owner.get_or_null(p_light);
237 ERR_FAIL_NULL_V(light, Color());
238
239 return light->color;
240 }
241
242 _FORCE_INLINE_ bool light_is_distance_fade_enabled(RID p_light) {
243 const Light *light = light_owner.get_or_null(p_light);
244 return light->distance_fade;
245 }
246
247 _FORCE_INLINE_ float light_get_distance_fade_begin(RID p_light) {
248 const Light *light = light_owner.get_or_null(p_light);
249 return light->distance_fade_begin;
250 }
251
252 _FORCE_INLINE_ float light_get_distance_fade_shadow(RID p_light) {
253 const Light *light = light_owner.get_or_null(p_light);
254 return light->distance_fade_shadow;
255 }
256
257 _FORCE_INLINE_ float light_get_distance_fade_length(RID p_light) {
258 const Light *light = light_owner.get_or_null(p_light);
259 return light->distance_fade_length;
260 }
261
262 virtual bool light_has_shadow(RID p_light) const override {
263 const Light *light = light_owner.get_or_null(p_light);
264 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
265
266 return light->shadow;
267 }
268
269 virtual bool light_has_projector(RID p_light) const override {
270 const Light *light = light_owner.get_or_null(p_light);
271 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
272
273 return TextureStorage::get_singleton()->owns_texture(light->projector);
274 }
275
276 _FORCE_INLINE_ bool light_is_negative(RID p_light) const {
277 const Light *light = light_owner.get_or_null(p_light);
278 ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
279
280 return light->negative;
281 }
282
283 _FORCE_INLINE_ float light_get_transmittance_bias(RID p_light) const {
284 const Light *light = light_owner.get_or_null(p_light);
285 ERR_FAIL_NULL_V(light, 0.0);
286
287 return light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
288 }
289
290 virtual bool light_get_reverse_cull_face_mode(RID p_light) const override {
291 const Light *light = light_owner.get_or_null(p_light);
292 ERR_FAIL_NULL_V(light, false);
293
294 return light->reverse_cull;
295 }
296
297 virtual RS::LightBakeMode light_get_bake_mode(RID p_light) override;
298 virtual uint32_t light_get_max_sdfgi_cascade(RID p_light) override { return 0; }
299 virtual uint64_t light_get_version(RID p_light) const override;
300 virtual uint32_t light_get_cull_mask(RID p_light) const override;
301
302 /* LIGHT INSTANCE API */
303
304 LightInstance *get_light_instance(RID p_rid) { return light_instance_owner.get_or_null(p_rid); };
305 bool owns_light_instance(RID p_rid) { return light_instance_owner.owns(p_rid); };
306
307 virtual RID light_instance_create(RID p_light) override;
308 virtual void light_instance_free(RID p_light_instance) override;
309
310 virtual void light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) override;
311 virtual void light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) override;
312 virtual void light_instance_set_shadow_transform(RID p_light_instance, const Projection &p_projection, const Transform3D &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale = 1.0, float p_range_begin = 0, const Vector2 &p_uv_scale = Vector2()) override;
313 virtual void light_instance_mark_visible(RID p_light_instance) override;
314
315 _FORCE_INLINE_ RS::LightType light_instance_get_type(RID p_light_instance) {
316 LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
317 return li->light_type;
318 }
319 _FORCE_INLINE_ uint32_t light_instance_get_gl_id(RID p_light_instance) {
320 LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
321 return li->gl_id;
322 }
323
324 /* PROBE API */
325
326 virtual RID reflection_probe_allocate() override;
327 virtual void reflection_probe_initialize(RID p_rid) override;
328 virtual void reflection_probe_free(RID p_rid) override;
329
330 virtual void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) override;
331 virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity) override;
332 virtual void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) override;
333 virtual void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) override;
334 virtual void reflection_probe_set_ambient_energy(RID p_probe, float p_energy) override;
335 virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance) override;
336 virtual void reflection_probe_set_size(RID p_probe, const Vector3 &p_size) override;
337 virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) override;
338 virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable) override;
339 virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) override;
340 virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) override;
341 virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) override;
342 virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution) override;
343 virtual void reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) override;
344 virtual float reflection_probe_get_mesh_lod_threshold(RID p_probe) const override;
345
346 virtual AABB reflection_probe_get_aabb(RID p_probe) const override;
347 virtual RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const override;
348 virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const override;
349 virtual Vector3 reflection_probe_get_size(RID p_probe) const override;
350 virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const override;
351 virtual float reflection_probe_get_origin_max_distance(RID p_probe) const override;
352 virtual bool reflection_probe_renders_shadows(RID p_probe) const override;
353
354 /* REFLECTION ATLAS */
355
356 virtual RID reflection_atlas_create() override;
357 virtual void reflection_atlas_free(RID p_ref_atlas) override;
358 virtual int reflection_atlas_get_size(RID p_ref_atlas) const override;
359 virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) override;
360
361 /* REFLECTION PROBE INSTANCE */
362
363 virtual RID reflection_probe_instance_create(RID p_probe) override;
364 virtual void reflection_probe_instance_free(RID p_instance) override;
365 virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) override;
366 virtual void reflection_probe_release_atlas_index(RID p_instance) override;
367 virtual bool reflection_probe_instance_needs_redraw(RID p_instance) override;
368 virtual bool reflection_probe_instance_has_reflection(RID p_instance) override;
369 virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) override;
370 virtual Ref<RenderSceneBuffers> reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) override;
371 virtual bool reflection_probe_instance_postprocess_step(RID p_instance) override;
372
373 /* LIGHTMAP CAPTURE */
374
375 Lightmap *get_lightmap(RID p_rid) { return lightmap_owner.get_or_null(p_rid); };
376 bool owns_lightmap(RID p_rid) { return lightmap_owner.owns(p_rid); };
377
378 virtual RID lightmap_allocate() override;
379 virtual void lightmap_initialize(RID p_rid) override;
380 virtual void lightmap_free(RID p_rid) override;
381
382 virtual void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) override;
383 virtual void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) override;
384 virtual void lightmap_set_probe_interior(RID p_lightmap, bool p_interior) override;
385 virtual void lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) override;
386 virtual void lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) override;
387 virtual PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const override;
388 virtual PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const override;
389 virtual PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const override;
390 virtual PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const override;
391 virtual AABB lightmap_get_aabb(RID p_lightmap) const override;
392 virtual void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) override;
393 virtual bool lightmap_is_interior(RID p_lightmap) const override;
394 virtual void lightmap_set_probe_capture_update_speed(float p_speed) override;
395 virtual float lightmap_get_probe_capture_update_speed() const override;
396
397 /* LIGHT SHADOW MAPPING */
398 /*
399 struct CanvasOccluder {
400 RID self;
401
402 GLuint vertex_id; // 0 means, unconfigured
403 GLuint index_id; // 0 means, unconfigured
404 LocalVector<Vector2> lines;
405 int len;
406 };
407
408 RID_Owner<CanvasOccluder> canvas_occluder_owner;
409
410 RID canvas_light_occluder_create();
411 void canvas_light_occluder_set_polylines(RID p_occluder, const LocalVector<Vector2> &p_lines);
412 */
413
414 /* LIGHTMAP INSTANCE */
415
416 virtual RID lightmap_instance_create(RID p_lightmap) override;
417 virtual void lightmap_instance_free(RID p_lightmap) override;
418 virtual void lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) override;
419
420 /* SHADOW ATLAS API */
421
422 virtual RID shadow_atlas_create() override;
423 virtual void shadow_atlas_free(RID p_atlas) override;
424 virtual void shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits = true) override;
425 virtual void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) override;
426 virtual bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) override;
427
428 virtual void shadow_atlas_update(RID p_atlas) override;
429
430 virtual void directional_shadow_atlas_set_size(int p_size, bool p_16_bits = true) override;
431 virtual int get_directional_light_shadow_size(RID p_light_intance) override;
432 virtual void set_directional_shadow_count(int p_count) override;
433};
434
435} // namespace GLES3
436
437#endif // GLES3_ENABLED
438
439#endif // LIGHT_STORAGE_GLES3_H
440