light_storage.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. #ifndef LIGHT_STORAGE_RD_H
  31. #define LIGHT_STORAGE_RD_H
  32. #include "core/templates/local_vector.h"
  33. #include "core/templates/paged_array.h"
  34. #include "core/templates/rid_owner.h"
  35. #include "core/templates/self_list.h"
  36. #include "servers/rendering/renderer_rd/cluster_builder_rd.h"
  37. #include "servers/rendering/renderer_rd/environment/sky.h"
  38. #include "servers/rendering/renderer_rd/storage_rd/forward_id_storage.h"
  39. #include "servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
  40. #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
  41. #include "servers/rendering/storage/light_storage.h"
  42. #include "servers/rendering/storage/utilities.h"
  43. class RenderDataRD;
  44. namespace RendererRD {
  45. class LightStorage : public RendererLightStorage {
  46. public:
  47. enum ShadowAtlastQuadrant : uint32_t {
  48. QUADRANT_SHIFT = 27,
  49. OMNI_LIGHT_FLAG = 1 << 26,
  50. SHADOW_INDEX_MASK = OMNI_LIGHT_FLAG - 1,
  51. SHADOW_INVALID = 0xFFFFFFFF
  52. };
  53. private:
  54. static LightStorage *singleton;
  55. uint32_t max_cluster_elements = 512;
  56. /* LIGHT */
  57. struct Light {
  58. RS::LightType type;
  59. float param[RS::LIGHT_PARAM_MAX];
  60. Color color = Color(1, 1, 1, 1);
  61. RID projector;
  62. bool shadow = false;
  63. bool negative = false;
  64. bool reverse_cull = false;
  65. RS::LightBakeMode bake_mode = RS::LIGHT_BAKE_DYNAMIC;
  66. uint32_t max_sdfgi_cascade = 2;
  67. uint32_t cull_mask = 0xFFFFFFFF;
  68. uint32_t shadow_caster_mask = 0xFFFFFFFF;
  69. bool distance_fade = false;
  70. real_t distance_fade_begin = 40.0;
  71. real_t distance_fade_shadow = 50.0;
  72. real_t distance_fade_length = 10.0;
  73. RS::LightOmniShadowMode omni_shadow_mode = RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  74. RS::LightDirectionalShadowMode directional_shadow_mode = RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  75. bool directional_blend_splits = false;
  76. RS::LightDirectionalSkyMode directional_sky_mode = RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY;
  77. uint64_t version = 0;
  78. Dependency dependency;
  79. };
  80. mutable RID_Owner<Light, true> light_owner;
  81. /* LIGHT INSTANCE */
  82. struct LightInstance {
  83. struct ShadowTransform {
  84. Projection camera;
  85. Transform3D transform;
  86. float farplane = 0.0;
  87. float split = 0.0;
  88. float bias_scale = 0.0;
  89. float shadow_texel_size = 0.0;
  90. float range_begin = 0.0;
  91. Rect2 atlas_rect;
  92. Vector2 uv_scale;
  93. };
  94. RS::LightType light_type = RS::LIGHT_DIRECTIONAL;
  95. ShadowTransform shadow_transform[6];
  96. AABB aabb;
  97. RID self;
  98. RID light;
  99. Transform3D transform;
  100. Vector3 light_vector;
  101. Vector3 spot_vector;
  102. float linear_att = 0.0;
  103. uint64_t shadow_pass = 0;
  104. uint64_t last_scene_pass = 0;
  105. uint64_t last_scene_shadow_pass = 0;
  106. uint64_t last_pass = 0;
  107. uint32_t cull_mask = 0;
  108. uint32_t light_directional_index = 0;
  109. Rect2 directional_rect;
  110. HashSet<RID> shadow_atlases; //shadow atlases where this light is registered
  111. ForwardID forward_id = -1;
  112. LightInstance() {}
  113. };
  114. mutable RID_Owner<LightInstance> light_instance_owner;
  115. /* OMNI/SPOT LIGHT DATA */
  116. struct LightData {
  117. float position[3];
  118. float inv_radius;
  119. float direction[3]; // in omni, x and y are used for dual paraboloid offset
  120. float size;
  121. float color[3];
  122. float attenuation;
  123. float inv_spot_attenuation;
  124. float cos_spot_angle;
  125. float specular_amount;
  126. float shadow_opacity;
  127. float atlas_rect[4]; // in omni, used for atlas uv, in spot, used for projector uv
  128. float shadow_matrix[16];
  129. float shadow_bias;
  130. float shadow_normal_bias;
  131. float transmittance_bias;
  132. float soft_shadow_size;
  133. float soft_shadow_scale;
  134. uint32_t mask;
  135. float volumetric_fog_energy;
  136. uint32_t bake_mode;
  137. float projector_rect[4];
  138. };
  139. struct LightInstanceDepthSort {
  140. float depth;
  141. LightInstance *light_instance;
  142. Light *light;
  143. bool operator<(const LightInstanceDepthSort &p_sort) const {
  144. return depth < p_sort.depth;
  145. }
  146. };
  147. uint32_t max_lights;
  148. uint32_t omni_light_count = 0;
  149. uint32_t spot_light_count = 0;
  150. LightData *omni_lights = nullptr;
  151. LightData *spot_lights = nullptr;
  152. LightInstanceDepthSort *omni_light_sort = nullptr;
  153. LightInstanceDepthSort *spot_light_sort = nullptr;
  154. RID omni_light_buffer;
  155. RID spot_light_buffer;
  156. /* DIRECTIONAL LIGHT DATA */
  157. struct DirectionalLightData {
  158. float direction[3];
  159. float energy;
  160. float color[3];
  161. float size;
  162. float specular;
  163. uint32_t mask;
  164. float softshadow_angle;
  165. float soft_shadow_scale;
  166. uint32_t blend_splits;
  167. float shadow_opacity;
  168. float fade_from;
  169. float fade_to;
  170. uint32_t pad[2];
  171. uint32_t bake_mode;
  172. float volumetric_fog_energy;
  173. float shadow_bias[4];
  174. float shadow_normal_bias[4];
  175. float shadow_transmittance_bias[4];
  176. float shadow_z_range[4];
  177. float shadow_range_begin[4];
  178. float shadow_split_offsets[4];
  179. float shadow_matrices[4][16];
  180. float uv_scale1[2];
  181. float uv_scale2[2];
  182. float uv_scale3[2];
  183. float uv_scale4[2];
  184. };
  185. uint32_t max_directional_lights;
  186. DirectionalLightData *directional_lights = nullptr;
  187. RID directional_light_buffer;
  188. /* REFLECTION PROBE */
  189. struct ReflectionProbe {
  190. RS::ReflectionProbeUpdateMode update_mode = RS::REFLECTION_PROBE_UPDATE_ONCE;
  191. int resolution = 256;
  192. float intensity = 1.0;
  193. float blend_distance = 1.0;
  194. RS::ReflectionProbeAmbientMode ambient_mode = RS::REFLECTION_PROBE_AMBIENT_ENVIRONMENT;
  195. Color ambient_color;
  196. float ambient_color_energy = 1.0;
  197. float max_distance = 0;
  198. Vector3 size = Vector3(20, 20, 20);
  199. Vector3 origin_offset;
  200. bool interior = false;
  201. bool box_projection = false;
  202. bool enable_shadows = false;
  203. uint32_t cull_mask = (1 << 20) - 1;
  204. uint32_t reflection_mask = (1 << 20) - 1;
  205. float mesh_lod_threshold = 0.01;
  206. float baked_exposure = 1.0;
  207. Dependency dependency;
  208. };
  209. mutable RID_Owner<ReflectionProbe, true> reflection_probe_owner;
  210. /* REFLECTION ATLAS */
  211. struct ReflectionAtlas {
  212. int count = 0;
  213. int size = 0;
  214. RID reflection;
  215. RID depth_buffer;
  216. RID depth_fb;
  217. struct Reflection {
  218. RID owner;
  219. RendererRD::SkyRD::ReflectionData data;
  220. RID fbs[6];
  221. };
  222. Vector<Reflection> reflections;
  223. Ref<RenderSceneBuffersRD> render_buffers; // Further render buffers used.
  224. ClusterBuilderRD *cluster_builder = nullptr; // only used if cluster builder is supported by the renderer.
  225. };
  226. mutable RID_Owner<ReflectionAtlas> reflection_atlas_owner;
  227. /* REFLECTION PROBE INSTANCE */
  228. struct ReflectionProbeInstance {
  229. RID probe;
  230. int atlas_index = -1;
  231. RID atlas;
  232. bool dirty = true;
  233. bool rendering = false;
  234. int processing_layer = 1;
  235. int processing_side = 0;
  236. uint64_t last_pass = 0;
  237. uint32_t cull_mask = 0;
  238. RendererRD::ForwardID forward_id = -1;
  239. Transform3D transform;
  240. };
  241. mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner;
  242. /* REFLECTION DATA */
  243. enum {
  244. REFLECTION_AMBIENT_DISABLED = 0,
  245. REFLECTION_AMBIENT_ENVIRONMENT = 1,
  246. REFLECTION_AMBIENT_COLOR = 2,
  247. };
  248. struct ReflectionData {
  249. float box_extents[3];
  250. float index;
  251. float box_offset[3];
  252. uint32_t mask;
  253. float ambient[3]; // ambient color,
  254. float intensity;
  255. float blend_distance;
  256. uint32_t exterior;
  257. uint32_t box_project;
  258. uint32_t ambient_mode;
  259. float exposure_normalization;
  260. uint32_t pad0;
  261. uint32_t pad1;
  262. uint32_t pad2;
  263. float local_matrix[16]; // up to here for spot and omni, rest is for directional
  264. };
  265. struct ReflectionProbeInstanceSort {
  266. float size;
  267. ReflectionProbeInstance *probe_instance;
  268. bool operator<(const ReflectionProbeInstanceSort &p_sort) const {
  269. return size < p_sort.size;
  270. }
  271. };
  272. uint32_t max_reflections;
  273. uint32_t reflection_count = 0;
  274. // uint32_t max_reflection_probes_per_instance = 0; // seems unused
  275. ReflectionData *reflections = nullptr;
  276. ReflectionProbeInstanceSort *reflection_sort = nullptr;
  277. RID reflection_buffer;
  278. /* LIGHTMAP */
  279. struct Lightmap {
  280. RID light_texture;
  281. RID shadow_texture;
  282. RS::ShadowmaskMode shadowmask_mode = RS::SHADOWMASK_MODE_NONE;
  283. bool uses_spherical_harmonics = false;
  284. bool interior = false;
  285. AABB bounds = AABB(Vector3(), Vector3(1, 1, 1));
  286. float baked_exposure = 1.0;
  287. Vector2i light_texture_size;
  288. int32_t array_index = -1; //unassigned
  289. PackedVector3Array points;
  290. PackedColorArray point_sh;
  291. PackedInt32Array tetrahedra;
  292. PackedInt32Array bsp_tree;
  293. struct BSP {
  294. static const int32_t EMPTY_LEAF = INT32_MIN;
  295. float plane[4];
  296. int32_t over = EMPTY_LEAF, under = EMPTY_LEAF;
  297. };
  298. Dependency dependency;
  299. };
  300. bool using_lightmap_array;
  301. Vector<RID> lightmap_textures;
  302. uint64_t lightmap_array_version = 0;
  303. float lightmap_probe_capture_update_speed = 4;
  304. mutable RID_Owner<Lightmap, true> lightmap_owner;
  305. Vector<RID> shadowmask_textures;
  306. /* LIGHTMAP INSTANCE */
  307. struct LightmapInstance {
  308. RID lightmap;
  309. Transform3D transform;
  310. };
  311. mutable RID_Owner<LightmapInstance> lightmap_instance_owner;
  312. /* SHADOW ATLAS */
  313. uint64_t shadow_atlas_realloc_tolerance_msec = 500;
  314. struct ShadowShrinkStage {
  315. RID texture;
  316. RID filter_texture;
  317. uint32_t size = 0;
  318. };
  319. struct ShadowAtlas {
  320. struct Quadrant {
  321. uint32_t subdivision = 0;
  322. struct Shadow {
  323. RID owner;
  324. uint64_t version = 0;
  325. uint64_t fog_version = 0; // used for fog
  326. uint64_t alloc_tick = 0;
  327. Shadow() {}
  328. };
  329. Vector<Shadow> shadows;
  330. Quadrant() {}
  331. } quadrants[4];
  332. int size_order[4] = { 0, 1, 2, 3 };
  333. uint32_t smallest_subdiv = 0;
  334. int size = 0;
  335. bool use_16_bits = true;
  336. RID depth;
  337. RID fb; //for copying
  338. HashMap<RID, uint32_t> shadow_owners;
  339. };
  340. RID_Owner<ShadowAtlas> shadow_atlas_owner;
  341. void _update_shadow_atlas(ShadowAtlas *shadow_atlas);
  342. void _shadow_atlas_invalidate_shadow(ShadowAtlas::Quadrant::Shadow *p_shadow, RID p_atlas, ShadowAtlas *p_shadow_atlas, uint32_t p_quadrant, uint32_t p_shadow_idx);
  343. bool _shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow);
  344. bool _shadow_atlas_find_omni_shadows(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow);
  345. /* DIRECTIONAL SHADOW */
  346. struct DirectionalShadow {
  347. RID depth;
  348. RID fb; //when renderign direct
  349. int light_count = 0;
  350. int size = 0;
  351. bool use_16_bits = true;
  352. int current_light = 0;
  353. } directional_shadow;
  354. /* SHADOW CUBEMAPS */
  355. struct ShadowCubemap {
  356. RID cubemap;
  357. RID side_fb[6];
  358. };
  359. HashMap<int, ShadowCubemap> shadow_cubemaps;
  360. ShadowCubemap *_get_shadow_cubemap(int p_size);
  361. /* PIPELINE HINTS */
  362. bool shadow_cubemaps_used = false;
  363. bool shadow_dual_paraboloid_used = false;
  364. public:
  365. static LightStorage *get_singleton();
  366. LightStorage();
  367. virtual ~LightStorage();
  368. bool free(RID p_rid);
  369. /* Settings */
  370. void set_max_cluster_elements(const uint32_t p_max_cluster_elements) {
  371. max_cluster_elements = p_max_cluster_elements;
  372. set_max_reflection_probes(p_max_cluster_elements);
  373. set_max_lights(p_max_cluster_elements);
  374. }
  375. uint32_t get_max_cluster_elements() const { return max_cluster_elements; }
  376. /* LIGHT */
  377. bool owns_light(RID p_rid) { return light_owner.owns(p_rid); }
  378. void _light_initialize(RID p_rid, RS::LightType p_type);
  379. virtual RID directional_light_allocate() override;
  380. virtual void directional_light_initialize(RID p_light) override;
  381. virtual RID omni_light_allocate() override;
  382. virtual void omni_light_initialize(RID p_light) override;
  383. virtual RID spot_light_allocate() override;
  384. virtual void spot_light_initialize(RID p_light) override;
  385. virtual void light_free(RID p_rid) override;
  386. virtual void light_set_color(RID p_light, const Color &p_color) override;
  387. virtual void light_set_param(RID p_light, RS::LightParam p_param, float p_value) override;
  388. virtual void light_set_shadow(RID p_light, bool p_enabled) override;
  389. virtual void light_set_projector(RID p_light, RID p_texture) override;
  390. virtual void light_set_negative(RID p_light, bool p_enable) override;
  391. virtual void light_set_cull_mask(RID p_light, uint32_t p_mask) override;
  392. virtual void light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) override;
  393. virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) override;
  394. virtual void light_set_shadow_caster_mask(RID p_light, uint32_t p_caster_mask) override;
  395. virtual uint32_t light_get_shadow_caster_mask(RID p_light) const override;
  396. virtual void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) override;
  397. virtual void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) override;
  398. virtual void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) override;
  399. virtual void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) override;
  400. virtual void light_directional_set_blend_splits(RID p_light, bool p_enable) override;
  401. virtual bool light_directional_get_blend_splits(RID p_light) const override;
  402. virtual void light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) override;
  403. virtual RS::LightDirectionalSkyMode light_directional_get_sky_mode(RID p_light) const override;
  404. virtual RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) override;
  405. virtual RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) override;
  406. virtual RS::LightType light_get_type(RID p_light) const override {
  407. const Light *light = light_owner.get_or_null(p_light);
  408. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
  409. return light->type;
  410. }
  411. virtual AABB light_get_aabb(RID p_light) const override;
  412. virtual float light_get_param(RID p_light, RS::LightParam p_param) override {
  413. const Light *light = light_owner.get_or_null(p_light);
  414. ERR_FAIL_NULL_V(light, 0);
  415. return light->param[p_param];
  416. }
  417. _FORCE_INLINE_ RID light_get_projector(RID p_light) {
  418. const Light *light = light_owner.get_or_null(p_light);
  419. ERR_FAIL_NULL_V(light, RID());
  420. return light->projector;
  421. }
  422. virtual Color light_get_color(RID p_light) override {
  423. const Light *light = light_owner.get_or_null(p_light);
  424. ERR_FAIL_NULL_V(light, Color());
  425. return light->color;
  426. }
  427. _FORCE_INLINE_ bool light_is_distance_fade_enabled(RID p_light) {
  428. const Light *light = light_owner.get_or_null(p_light);
  429. return light->distance_fade;
  430. }
  431. _FORCE_INLINE_ float light_get_distance_fade_begin(RID p_light) {
  432. const Light *light = light_owner.get_or_null(p_light);
  433. return light->distance_fade_begin;
  434. }
  435. _FORCE_INLINE_ float light_get_distance_fade_shadow(RID p_light) {
  436. const Light *light = light_owner.get_or_null(p_light);
  437. return light->distance_fade_shadow;
  438. }
  439. _FORCE_INLINE_ float light_get_distance_fade_length(RID p_light) {
  440. const Light *light = light_owner.get_or_null(p_light);
  441. return light->distance_fade_length;
  442. }
  443. virtual bool light_has_shadow(RID p_light) const override {
  444. const Light *light = light_owner.get_or_null(p_light);
  445. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
  446. return light->shadow;
  447. }
  448. virtual bool light_has_projector(RID p_light) const override {
  449. const Light *light = light_owner.get_or_null(p_light);
  450. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
  451. return TextureStorage::get_singleton()->owns_texture(light->projector);
  452. }
  453. _FORCE_INLINE_ bool light_is_negative(RID p_light) const {
  454. const Light *light = light_owner.get_or_null(p_light);
  455. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL);
  456. return light->negative;
  457. }
  458. _FORCE_INLINE_ float light_get_transmittance_bias(RID p_light) const {
  459. const Light *light = light_owner.get_or_null(p_light);
  460. ERR_FAIL_NULL_V(light, 0.0);
  461. return light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
  462. }
  463. virtual bool light_get_reverse_cull_face_mode(RID p_light) const override {
  464. const Light *light = light_owner.get_or_null(p_light);
  465. ERR_FAIL_NULL_V(light, false);
  466. return light->reverse_cull;
  467. }
  468. virtual RS::LightBakeMode light_get_bake_mode(RID p_light) override;
  469. virtual uint32_t light_get_max_sdfgi_cascade(RID p_light) override;
  470. virtual uint64_t light_get_version(RID p_light) const override;
  471. virtual uint32_t light_get_cull_mask(RID p_light) const override;
  472. Dependency *light_get_dependency(RID p_light) const;
  473. /* LIGHT INSTANCE API */
  474. bool owns_light_instance(RID p_rid) { return light_instance_owner.owns(p_rid); }
  475. virtual RID light_instance_create(RID p_light) override;
  476. virtual void light_instance_free(RID p_light) override;
  477. virtual void light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) override;
  478. virtual void light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) override;
  479. 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;
  480. virtual void light_instance_mark_visible(RID p_light_instance) override;
  481. virtual bool light_instance_is_shadow_visible_at_position(RID p_light_instance, const Vector3 &p_position) const override {
  482. const LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  483. ERR_FAIL_NULL_V(light_instance, false);
  484. const Light *light = light_owner.get_or_null(light_instance->light);
  485. ERR_FAIL_NULL_V(light, false);
  486. if (!light->shadow) {
  487. return false;
  488. }
  489. if (!light->distance_fade) {
  490. return true;
  491. }
  492. real_t distance = p_position.distance_to(light_instance->transform.origin);
  493. if (distance > light->distance_fade_shadow + light->distance_fade_length) {
  494. return false;
  495. }
  496. return true;
  497. }
  498. _FORCE_INLINE_ RID light_instance_get_base_light(RID p_light_instance) {
  499. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  500. return li->light;
  501. }
  502. _FORCE_INLINE_ Transform3D light_instance_get_base_transform(RID p_light_instance) {
  503. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  504. return li->transform;
  505. }
  506. _FORCE_INLINE_ AABB light_instance_get_base_aabb(RID p_light_instance) {
  507. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  508. return li->aabb;
  509. }
  510. _FORCE_INLINE_ void light_instance_set_cull_mask(RID p_light_instance, uint32_t p_cull_mask) {
  511. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  512. li->cull_mask = p_cull_mask;
  513. }
  514. _FORCE_INLINE_ uint32_t light_instance_get_cull_mask(RID p_light_instance) {
  515. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  516. return li->cull_mask;
  517. }
  518. _FORCE_INLINE_ Rect2 light_instance_get_shadow_atlas_rect(RID p_light_instance, RID p_shadow_atlas, Vector2i &r_omni_offset) {
  519. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_shadow_atlas);
  520. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  521. uint32_t key = shadow_atlas->shadow_owners[li->self];
  522. uint32_t quadrant = (key >> QUADRANT_SHIFT) & 0x3;
  523. uint32_t shadow = key & SHADOW_INDEX_MASK;
  524. ERR_FAIL_COND_V(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size(), Rect2());
  525. uint32_t atlas_size = shadow_atlas->size;
  526. uint32_t quadrant_size = atlas_size >> 1;
  527. uint32_t x = (quadrant & 1) * quadrant_size;
  528. uint32_t y = (quadrant >> 1) * quadrant_size;
  529. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  530. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  531. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  532. if (key & OMNI_LIGHT_FLAG) {
  533. if (((shadow + 1) % shadow_atlas->quadrants[quadrant].subdivision) == 0) {
  534. r_omni_offset.x = 1 - int(shadow_atlas->quadrants[quadrant].subdivision);
  535. r_omni_offset.y = 1;
  536. } else {
  537. r_omni_offset.x = 1;
  538. r_omni_offset.y = 0;
  539. }
  540. }
  541. uint32_t width = shadow_size;
  542. uint32_t height = shadow_size;
  543. return Rect2(x / float(shadow_atlas->size), y / float(shadow_atlas->size), width / float(shadow_atlas->size), height / float(shadow_atlas->size));
  544. }
  545. _FORCE_INLINE_ float light_instance_get_shadow_texel_size(RID p_light_instance, RID p_shadow_atlas) {
  546. #ifdef DEBUG_ENABLED
  547. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  548. ERR_FAIL_COND_V(!li->shadow_atlases.has(p_shadow_atlas), 0);
  549. #endif
  550. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_shadow_atlas);
  551. ERR_FAIL_NULL_V(shadow_atlas, 0);
  552. #ifdef DEBUG_ENABLED
  553. ERR_FAIL_COND_V(!shadow_atlas->shadow_owners.has(p_light_instance), 0);
  554. #endif
  555. uint32_t key = shadow_atlas->shadow_owners[p_light_instance];
  556. uint32_t quadrant = (key >> QUADRANT_SHIFT) & 0x3;
  557. uint32_t quadrant_size = shadow_atlas->size >> 1;
  558. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  559. return float(1.0) / shadow_size;
  560. }
  561. _FORCE_INLINE_ Projection light_instance_get_shadow_camera(RID p_light_instance, int p_index) {
  562. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  563. return li->shadow_transform[p_index].camera;
  564. }
  565. _FORCE_INLINE_ Transform3D light_instance_get_shadow_transform(RID p_light_instance, int p_index) {
  566. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  567. return li->shadow_transform[p_index].transform;
  568. }
  569. _FORCE_INLINE_ float light_instance_get_shadow_bias_scale(RID p_light_instance, int p_index) {
  570. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  571. return li->shadow_transform[p_index].bias_scale;
  572. }
  573. _FORCE_INLINE_ float light_instance_get_shadow_range(RID p_light_instance, int p_index) {
  574. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  575. return li->shadow_transform[p_index].farplane;
  576. }
  577. _FORCE_INLINE_ float light_instance_get_shadow_range_begin(RID p_light_instance, int p_index) {
  578. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  579. return li->shadow_transform[p_index].range_begin;
  580. }
  581. _FORCE_INLINE_ Vector2 light_instance_get_shadow_uv_scale(RID p_light_instance, int p_index) {
  582. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  583. return li->shadow_transform[p_index].uv_scale;
  584. }
  585. _FORCE_INLINE_ void light_instance_set_directional_shadow_atlas_rect(RID p_light_instance, int p_index, const Rect2 p_atlas_rect) {
  586. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  587. li->shadow_transform[p_index].atlas_rect = p_atlas_rect;
  588. }
  589. _FORCE_INLINE_ Rect2 light_instance_get_directional_shadow_atlas_rect(RID p_light_instance, int p_index) {
  590. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  591. return li->shadow_transform[p_index].atlas_rect;
  592. }
  593. _FORCE_INLINE_ float light_instance_get_directional_shadow_split(RID p_light_instance, int p_index) {
  594. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  595. return li->shadow_transform[p_index].split;
  596. }
  597. _FORCE_INLINE_ float light_instance_get_directional_shadow_texel_size(RID p_light_instance, int p_index) {
  598. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  599. return li->shadow_transform[p_index].shadow_texel_size;
  600. }
  601. _FORCE_INLINE_ void light_instance_set_render_pass(RID p_light_instance, uint64_t p_pass) {
  602. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  603. li->last_pass = p_pass;
  604. }
  605. _FORCE_INLINE_ uint64_t light_instance_get_render_pass(RID p_light_instance) {
  606. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  607. return li->last_pass;
  608. }
  609. _FORCE_INLINE_ void light_instance_set_shadow_pass(RID p_light_instance, uint64_t p_pass) {
  610. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  611. li->last_scene_shadow_pass = p_pass;
  612. }
  613. _FORCE_INLINE_ uint64_t light_instance_get_shadow_pass(RID p_light_instance) {
  614. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  615. return li->last_scene_shadow_pass;
  616. }
  617. _FORCE_INLINE_ ForwardID light_instance_get_forward_id(RID p_light_instance) {
  618. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  619. return li->forward_id;
  620. }
  621. _FORCE_INLINE_ RS::LightType light_instance_get_type(RID p_light_instance) {
  622. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  623. return li->light_type;
  624. }
  625. _FORCE_INLINE_ void light_instance_set_directional_rect(RID p_light_instance, const Rect2 &p_directional_rect) {
  626. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  627. li->directional_rect = p_directional_rect;
  628. }
  629. _FORCE_INLINE_ Rect2 light_instance_get_directional_rect(RID p_light_instance) {
  630. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  631. return li->directional_rect;
  632. }
  633. /* LIGHT DATA */
  634. void free_light_data();
  635. void set_max_lights(const uint32_t p_max_lights);
  636. RID get_omni_light_buffer() { return omni_light_buffer; }
  637. RID get_spot_light_buffer() { return spot_light_buffer; }
  638. RID get_directional_light_buffer() { return directional_light_buffer; }
  639. uint32_t get_max_directional_lights() { return max_directional_lights; }
  640. uint32_t get_directional_light_blend_splits(uint32_t p_directional_light_count) const {
  641. uint32_t blend_splits = 0;
  642. for (uint32_t i = 0; i < p_directional_light_count; i++) {
  643. if (directional_lights[i].blend_splits) {
  644. blend_splits |= 1U << i;
  645. }
  646. }
  647. return blend_splits;
  648. }
  649. bool has_directional_shadows(const uint32_t p_directional_light_count) {
  650. for (uint32_t i = 0; i < p_directional_light_count; i++) {
  651. if (directional_lights[i].shadow_opacity > 0.001) {
  652. return true;
  653. }
  654. }
  655. return false;
  656. }
  657. void update_light_buffers(RenderDataRD *p_render_data, const PagedArray<RID> &p_lights, const Transform3D &p_camera_transform, RID p_shadow_atlas, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_positional_light_count, bool &r_directional_light_soft_shadows);
  658. /* REFLECTION PROBE */
  659. bool owns_reflection_probe(RID p_rid) { return reflection_probe_owner.owns(p_rid); }
  660. virtual RID reflection_probe_allocate() override;
  661. virtual void reflection_probe_initialize(RID p_reflection_probe) override;
  662. virtual void reflection_probe_free(RID p_rid) override;
  663. virtual void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) override;
  664. virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity) override;
  665. virtual void reflection_probe_set_blend_distance(RID p_probe, float p_blend_distance) override;
  666. virtual void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) override;
  667. virtual void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) override;
  668. virtual void reflection_probe_set_ambient_energy(RID p_probe, float p_energy) override;
  669. virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance) override;
  670. virtual void reflection_probe_set_size(RID p_probe, const Vector3 &p_size) override;
  671. virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) override;
  672. virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable) override;
  673. virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) override;
  674. virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) override;
  675. virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) override;
  676. virtual void reflection_probe_set_reflection_mask(RID p_probe, uint32_t p_layers) override;
  677. virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution) override;
  678. virtual void reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) override;
  679. void reflection_probe_set_baked_exposure(RID p_probe, float p_exposure);
  680. virtual AABB reflection_probe_get_aabb(RID p_probe) const override;
  681. virtual RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const override;
  682. virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const override;
  683. virtual uint32_t reflection_probe_get_reflection_mask(RID p_probe) const override;
  684. virtual Vector3 reflection_probe_get_size(RID p_probe) const override;
  685. virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const override;
  686. virtual float reflection_probe_get_origin_max_distance(RID p_probe) const override;
  687. virtual float reflection_probe_get_mesh_lod_threshold(RID p_probe) const override;
  688. int reflection_probe_get_resolution(RID p_probe) const;
  689. float reflection_probe_get_baked_exposure(RID p_probe) const;
  690. virtual bool reflection_probe_renders_shadows(RID p_probe) const override;
  691. float reflection_probe_get_intensity(RID p_probe) const;
  692. float reflection_probe_get_blend_distance(RID p_probe) const;
  693. bool reflection_probe_is_interior(RID p_probe) const;
  694. bool reflection_probe_is_box_projection(RID p_probe) const;
  695. RS::ReflectionProbeAmbientMode reflection_probe_get_ambient_mode(RID p_probe) const;
  696. Color reflection_probe_get_ambient_color(RID p_probe) const;
  697. float reflection_probe_get_ambient_color_energy(RID p_probe) const;
  698. Dependency *reflection_probe_get_dependency(RID p_probe) const;
  699. /* REFLECTION ATLAS */
  700. bool owns_reflection_atlas(RID p_rid) { return reflection_atlas_owner.owns(p_rid); }
  701. virtual RID reflection_atlas_create() override;
  702. virtual void reflection_atlas_free(RID p_ref_atlas) override;
  703. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) override;
  704. virtual int reflection_atlas_get_size(RID p_ref_atlas) const override;
  705. _FORCE_INLINE_ RID reflection_atlas_get_texture(RID p_ref_atlas) {
  706. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_ref_atlas);
  707. ERR_FAIL_NULL_V(atlas, RID());
  708. return atlas->reflection;
  709. }
  710. /* REFLECTION PROBE INSTANCE */
  711. bool owns_reflection_probe_instance(RID p_rid) { return reflection_probe_instance_owner.owns(p_rid); }
  712. virtual RID reflection_probe_instance_create(RID p_probe) override;
  713. virtual void reflection_probe_instance_free(RID p_instance) override;
  714. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) override;
  715. virtual bool reflection_probe_has_atlas_index(RID p_instance) override;
  716. virtual void reflection_probe_release_atlas_index(RID p_instance) override;
  717. virtual bool reflection_probe_instance_needs_redraw(RID p_instance) override;
  718. virtual bool reflection_probe_instance_has_reflection(RID p_instance) override;
  719. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) override;
  720. virtual Ref<RenderSceneBuffers> reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) override;
  721. virtual bool reflection_probe_instance_postprocess_step(RID p_instance) override;
  722. uint32_t reflection_probe_instance_get_resolution(RID p_instance);
  723. RID reflection_probe_instance_get_framebuffer(RID p_instance, int p_index);
  724. RID reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index);
  725. _FORCE_INLINE_ RID reflection_probe_instance_get_probe(RID p_instance) {
  726. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  727. ERR_FAIL_NULL_V(rpi, RID());
  728. return rpi->probe;
  729. }
  730. _FORCE_INLINE_ RendererRD::ForwardID reflection_probe_instance_get_forward_id(RID p_instance) {
  731. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  732. ERR_FAIL_NULL_V(rpi, 0);
  733. return rpi->forward_id;
  734. }
  735. _FORCE_INLINE_ void reflection_probe_instance_set_cull_mask(RID p_instance, uint32_t p_render_pass) {
  736. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  737. ERR_FAIL_NULL(rpi);
  738. rpi->cull_mask = p_render_pass;
  739. }
  740. _FORCE_INLINE_ void reflection_probe_instance_set_render_pass(RID p_instance, uint32_t p_render_pass) {
  741. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  742. ERR_FAIL_NULL(rpi);
  743. rpi->last_pass = p_render_pass;
  744. }
  745. _FORCE_INLINE_ uint32_t reflection_probe_instance_get_render_pass(RID p_instance) {
  746. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  747. ERR_FAIL_NULL_V(rpi, 0);
  748. return rpi->last_pass;
  749. }
  750. _FORCE_INLINE_ Transform3D reflection_probe_instance_get_transform(RID p_instance) {
  751. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  752. ERR_FAIL_NULL_V(rpi, Transform3D());
  753. return rpi->transform;
  754. }
  755. _FORCE_INLINE_ int reflection_probe_instance_get_atlas_index(RID p_instance) {
  756. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  757. ERR_FAIL_NULL_V(rpi, -1);
  758. return rpi->atlas_index;
  759. }
  760. ClusterBuilderRD *reflection_probe_instance_get_cluster_builder(RID p_instance, ClusterBuilderSharedDataRD *p_cluster_builder_shared);
  761. /* REFLECTION DATA */
  762. void free_reflection_data();
  763. void set_max_reflection_probes(const uint32_t p_max_reflection_probes);
  764. RID get_reflection_probe_buffer() { return reflection_buffer; }
  765. void update_reflection_probe_buffer(RenderDataRD *p_render_data, const PagedArray<RID> &p_reflections, const Transform3D &p_camera_inverse_transform, RID p_environment);
  766. static RD::DataFormat get_reflection_probe_color_format();
  767. static uint32_t get_reflection_probe_color_usage_bits();
  768. static RD::DataFormat get_reflection_probe_depth_format();
  769. static uint32_t get_reflection_probe_depth_usage_bits();
  770. /* LIGHTMAP */
  771. bool owns_lightmap(RID p_rid) { return lightmap_owner.owns(p_rid); }
  772. virtual RID lightmap_allocate() override;
  773. virtual void lightmap_initialize(RID p_lightmap) override;
  774. virtual void lightmap_free(RID p_rid) override;
  775. virtual void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) override;
  776. virtual void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) override;
  777. virtual void lightmap_set_probe_interior(RID p_lightmap, bool p_interior) override;
  778. 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;
  779. virtual void lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) override;
  780. virtual PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const override;
  781. virtual PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const override;
  782. virtual PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const override;
  783. virtual PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const override;
  784. virtual AABB lightmap_get_aabb(RID p_lightmap) const override;
  785. virtual bool lightmap_is_interior(RID p_lightmap) const override;
  786. virtual void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) override;
  787. virtual void lightmap_set_probe_capture_update_speed(float p_speed) override;
  788. Dependency *lightmap_get_dependency(RID p_lightmap) const;
  789. virtual void lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) override;
  790. virtual RS::ShadowmaskMode lightmap_get_shadowmask_mode(RID p_lightmap) override;
  791. virtual void lightmap_set_shadowmask_mode(RID p_lightmap, RS::ShadowmaskMode p_mode) override;
  792. virtual float lightmap_get_probe_capture_update_speed() const override {
  793. return lightmap_probe_capture_update_speed;
  794. }
  795. _FORCE_INLINE_ RID lightmap_get_texture(RID p_lightmap) const {
  796. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  797. ERR_FAIL_NULL_V(lm, RID());
  798. return lm->light_texture;
  799. }
  800. _FORCE_INLINE_ float lightmap_get_baked_exposure_normalization(RID p_lightmap) const {
  801. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  802. ERR_FAIL_NULL_V(lm, 1.0);
  803. return lm->baked_exposure;
  804. }
  805. _FORCE_INLINE_ int32_t lightmap_get_array_index(RID p_lightmap) const {
  806. ERR_FAIL_COND_V(!using_lightmap_array, -1); //only for arrays
  807. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  808. return lm->array_index;
  809. }
  810. _FORCE_INLINE_ bool lightmap_uses_spherical_harmonics(RID p_lightmap) const {
  811. ERR_FAIL_COND_V(!using_lightmap_array, false); //only for arrays
  812. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  813. return lm->uses_spherical_harmonics;
  814. }
  815. _FORCE_INLINE_ Vector2i lightmap_get_light_texture_size(RID p_lightmap) const {
  816. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  817. return lm->light_texture_size;
  818. }
  819. _FORCE_INLINE_ uint64_t lightmap_array_get_version() const {
  820. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  821. return lightmap_array_version;
  822. }
  823. _FORCE_INLINE_ int lightmap_array_get_size() const {
  824. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  825. return lightmap_textures.size();
  826. }
  827. _FORCE_INLINE_ const Vector<RID> &lightmap_array_get_textures() const {
  828. ERR_FAIL_COND_V(!using_lightmap_array, lightmap_textures); //only for arrays
  829. return lightmap_textures;
  830. }
  831. _FORCE_INLINE_ RID shadowmask_get_texture(RID p_lightmap) const {
  832. const Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  833. ERR_FAIL_NULL_V(lm, RID());
  834. return lm->shadow_texture;
  835. }
  836. /* LIGHTMAP INSTANCE */
  837. bool owns_lightmap_instance(RID p_rid) { return lightmap_instance_owner.owns(p_rid); }
  838. virtual RID lightmap_instance_create(RID p_lightmap) override;
  839. virtual void lightmap_instance_free(RID p_lightmap) override;
  840. virtual void lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) override;
  841. _FORCE_INLINE_ bool lightmap_instance_is_valid(RID p_lightmap_instance) {
  842. return lightmap_instance_owner.get_or_null(p_lightmap_instance) != nullptr;
  843. }
  844. _FORCE_INLINE_ RID lightmap_instance_get_lightmap(RID p_lightmap_instance) {
  845. LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap_instance);
  846. return li->lightmap;
  847. }
  848. _FORCE_INLINE_ Transform3D lightmap_instance_get_transform(RID p_lightmap_instance) {
  849. LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap_instance);
  850. return li->transform;
  851. }
  852. /* SHADOW ATLAS API */
  853. bool owns_shadow_atlas(RID p_rid) { return shadow_atlas_owner.owns(p_rid); }
  854. virtual RID shadow_atlas_create() override;
  855. virtual void shadow_atlas_free(RID p_atlas) override;
  856. virtual void shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits = true) override;
  857. virtual void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) override;
  858. virtual bool shadow_atlas_update_light(RID p_atlas, RID p_light_instance, float p_coverage, uint64_t p_light_version) override;
  859. _FORCE_INLINE_ bool shadow_atlas_owns_light_instance(RID p_atlas, RID p_light_instance) {
  860. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  861. ERR_FAIL_NULL_V(atlas, false);
  862. return atlas->shadow_owners.has(p_light_instance);
  863. }
  864. _FORCE_INLINE_ uint32_t shadow_atlas_get_light_instance_key(RID p_atlas, RID p_light_instance) {
  865. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  866. ERR_FAIL_NULL_V(atlas, -1);
  867. return atlas->shadow_owners[p_light_instance];
  868. }
  869. _FORCE_INLINE_ RID shadow_atlas_get_texture(RID p_atlas) {
  870. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  871. ERR_FAIL_NULL_V(atlas, RID());
  872. return atlas->depth;
  873. }
  874. _FORCE_INLINE_ int shadow_atlas_get_size(RID p_atlas) {
  875. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  876. ERR_FAIL_NULL_V(atlas, 0);
  877. return atlas->size;
  878. }
  879. _FORCE_INLINE_ int shadow_atlas_get_quadrant_shadow_size(RID p_atlas, uint32_t p_quadrant) {
  880. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  881. ERR_FAIL_NULL_V(atlas, 0);
  882. ERR_FAIL_UNSIGNED_INDEX_V(p_quadrant, 4, 0);
  883. return atlas->quadrants[p_quadrant].shadows.size();
  884. }
  885. _FORCE_INLINE_ uint32_t shadow_atlas_get_quadrant_subdivision(RID p_atlas, uint32_t p_quadrant) {
  886. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  887. ERR_FAIL_NULL_V(atlas, 0);
  888. ERR_FAIL_UNSIGNED_INDEX_V(p_quadrant, 4, 0);
  889. return atlas->quadrants[p_quadrant].subdivision;
  890. }
  891. _FORCE_INLINE_ RID shadow_atlas_get_fb(RID p_atlas) {
  892. ShadowAtlas *atlas = shadow_atlas_owner.get_or_null(p_atlas);
  893. ERR_FAIL_NULL_V(atlas, RID());
  894. return atlas->fb;
  895. }
  896. virtual void shadow_atlas_update(RID p_atlas) override;
  897. static RD::DataFormat get_shadow_atlas_depth_format(bool p_16_bits);
  898. static uint32_t get_shadow_atlas_depth_usage_bits();
  899. /* DIRECTIONAL SHADOW */
  900. virtual void directional_shadow_atlas_set_size(int p_size, bool p_16_bits = true) override;
  901. virtual int get_directional_light_shadow_size(RID p_light_instance) override;
  902. virtual void set_directional_shadow_count(int p_count) override;
  903. Rect2i get_directional_shadow_rect();
  904. void update_directional_shadow_atlas();
  905. _FORCE_INLINE_ RID directional_shadow_get_texture() {
  906. return directional_shadow.depth;
  907. }
  908. _FORCE_INLINE_ int directional_shadow_get_size() {
  909. return directional_shadow.size;
  910. }
  911. _FORCE_INLINE_ RID direction_shadow_get_fb() {
  912. return directional_shadow.fb;
  913. }
  914. _FORCE_INLINE_ void directional_shadow_increase_current_light() {
  915. directional_shadow.current_light++;
  916. }
  917. /* SHADOW CUBEMAPS */
  918. RID get_cubemap(int p_size);
  919. RID get_cubemap_fb(int p_size, int p_pass);
  920. static RD::DataFormat get_cubemap_depth_format();
  921. static uint32_t get_cubemap_depth_usage_bits();
  922. /* PIPELINE HINTS */
  923. bool get_shadow_cubemaps_used() const;
  924. bool get_shadow_dual_paraboloid_used() const;
  925. };
  926. } // namespace RendererRD
  927. #endif // LIGHT_STORAGE_RD_H