renderer_scene_cull.h 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /**************************************************************************/
  2. /* renderer_scene_cull.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. #pragma once
  31. #include "core/math/dynamic_bvh.h"
  32. #include "core/math/transform_interpolator.h"
  33. #include "core/templates/bin_sorted_array.h"
  34. #include "core/templates/local_vector.h"
  35. #include "core/templates/paged_allocator.h"
  36. #include "core/templates/paged_array.h"
  37. #include "core/templates/pass_func.h"
  38. #include "core/templates/rid_owner.h"
  39. #include "core/templates/self_list.h"
  40. #include "servers/rendering/instance_uniforms.h"
  41. #include "servers/rendering/renderer_scene_occlusion_cull.h"
  42. #include "servers/rendering/renderer_scene_render.h"
  43. #include "servers/rendering/rendering_method.h"
  44. #include "servers/rendering/rendering_server_globals.h"
  45. #include "servers/rendering/storage/utilities.h"
  46. class RenderingLightCuller;
  47. class RendererSceneCull : public RenderingMethod {
  48. public:
  49. RendererSceneRender *scene_render = nullptr;
  50. enum {
  51. SDFGI_MAX_CASCADES = 8,
  52. SDFGI_MAX_REGIONS_PER_CASCADE = 3,
  53. MAX_INSTANCE_PAIRS = 32,
  54. MAX_UPDATE_SHADOWS = 512
  55. };
  56. uint64_t render_pass;
  57. static RendererSceneCull *singleton;
  58. /* EVENT QUEUING */
  59. void tick();
  60. void pre_draw(bool p_will_draw);
  61. /* CAMERA API */
  62. struct Camera {
  63. enum Type {
  64. PERSPECTIVE,
  65. ORTHOGONAL,
  66. FRUSTUM
  67. };
  68. Type type;
  69. float fov;
  70. float znear, zfar;
  71. float size;
  72. Vector2 offset;
  73. uint32_t visible_layers;
  74. bool vaspect;
  75. RID env;
  76. RID attributes;
  77. RID compositor;
  78. Transform3D transform;
  79. Camera() {
  80. visible_layers = 0xFFFFFFFF;
  81. fov = 75;
  82. type = PERSPECTIVE;
  83. znear = 0.05;
  84. zfar = 4000;
  85. size = 1.0;
  86. offset = Vector2();
  87. vaspect = false;
  88. }
  89. };
  90. mutable RID_Owner<Camera, true> camera_owner;
  91. virtual RID camera_allocate();
  92. virtual void camera_initialize(RID p_rid);
  93. virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
  94. virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
  95. virtual void camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
  96. virtual void camera_set_transform(RID p_camera, const Transform3D &p_transform);
  97. virtual void camera_set_cull_mask(RID p_camera, uint32_t p_layers);
  98. virtual void camera_set_environment(RID p_camera, RID p_env);
  99. virtual void camera_set_camera_attributes(RID p_camera, RID p_attributes);
  100. virtual void camera_set_compositor(RID p_camera, RID p_compositor);
  101. virtual void camera_set_use_vertical_aspect(RID p_camera, bool p_enable);
  102. virtual bool is_camera(RID p_camera) const;
  103. /* OCCLUDER API */
  104. virtual RID occluder_allocate();
  105. virtual void occluder_initialize(RID p_occluder);
  106. virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices);
  107. /* VISIBILITY NOTIFIER API */
  108. RendererSceneOcclusionCull *dummy_occlusion_culling = nullptr;
  109. /* SCENARIO API */
  110. struct Instance;
  111. struct PlaneSign {
  112. _ALWAYS_INLINE_ PlaneSign() {}
  113. _ALWAYS_INLINE_ PlaneSign(const Plane &p_plane) {
  114. if (p_plane.normal.x > 0) {
  115. signs[0] = 0;
  116. } else {
  117. signs[0] = 3;
  118. }
  119. if (p_plane.normal.y > 0) {
  120. signs[1] = 1;
  121. } else {
  122. signs[1] = 4;
  123. }
  124. if (p_plane.normal.z > 0) {
  125. signs[2] = 2;
  126. } else {
  127. signs[2] = 5;
  128. }
  129. }
  130. uint32_t signs[3];
  131. };
  132. struct Frustum {
  133. Vector<Plane> planes;
  134. Vector<PlaneSign> plane_signs;
  135. const Plane *planes_ptr;
  136. const PlaneSign *plane_signs_ptr;
  137. uint32_t plane_count;
  138. _ALWAYS_INLINE_ Frustum() {}
  139. _ALWAYS_INLINE_ Frustum(const Frustum &p_frustum) {
  140. planes = p_frustum.planes;
  141. plane_signs = p_frustum.plane_signs;
  142. planes_ptr = planes.ptr();
  143. plane_signs_ptr = plane_signs.ptr();
  144. plane_count = p_frustum.plane_count;
  145. }
  146. _ALWAYS_INLINE_ void operator=(const Frustum &p_frustum) {
  147. planes = p_frustum.planes;
  148. plane_signs = p_frustum.plane_signs;
  149. planes_ptr = planes.ptr();
  150. plane_signs_ptr = plane_signs.ptr();
  151. plane_count = p_frustum.plane_count;
  152. }
  153. _ALWAYS_INLINE_ Frustum(const Vector<Plane> &p_planes) {
  154. planes = p_planes;
  155. planes_ptr = planes.ptrw();
  156. plane_count = planes.size();
  157. for (int i = 0; i < planes.size(); i++) {
  158. PlaneSign ps(p_planes[i]);
  159. plane_signs.push_back(ps);
  160. }
  161. plane_signs_ptr = plane_signs.ptr();
  162. }
  163. };
  164. struct InstanceBounds {
  165. // Efficiently store instance bounds.
  166. // Because bounds checking is performed first,
  167. // keep it separated from data.
  168. real_t bounds[6];
  169. _ALWAYS_INLINE_ InstanceBounds() {}
  170. _ALWAYS_INLINE_ InstanceBounds(const AABB &p_aabb) {
  171. bounds[0] = p_aabb.position.x;
  172. bounds[1] = p_aabb.position.y;
  173. bounds[2] = p_aabb.position.z;
  174. bounds[3] = p_aabb.position.x + p_aabb.size.x;
  175. bounds[4] = p_aabb.position.y + p_aabb.size.y;
  176. bounds[5] = p_aabb.position.z + p_aabb.size.z;
  177. }
  178. _ALWAYS_INLINE_ bool in_frustum(const Frustum &p_frustum) const {
  179. // This is not a full SAT check and the possibility of false positives exist,
  180. // but the tradeoff vs performance is still very good.
  181. for (uint32_t i = 0; i < p_frustum.plane_count; i++) {
  182. Vector3 min(
  183. bounds[p_frustum.plane_signs_ptr[i].signs[0]],
  184. bounds[p_frustum.plane_signs_ptr[i].signs[1]],
  185. bounds[p_frustum.plane_signs_ptr[i].signs[2]]);
  186. if (p_frustum.planes_ptr[i].distance_to(min) >= 0.0) {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. _ALWAYS_INLINE_ bool in_aabb(const AABB &p_aabb) const {
  193. Vector3 end = p_aabb.position + p_aabb.size;
  194. if (bounds[0] >= end.x) {
  195. return false;
  196. }
  197. if (bounds[3] <= p_aabb.position.x) {
  198. return false;
  199. }
  200. if (bounds[1] >= end.y) {
  201. return false;
  202. }
  203. if (bounds[4] <= p_aabb.position.y) {
  204. return false;
  205. }
  206. if (bounds[2] >= end.z) {
  207. return false;
  208. }
  209. if (bounds[5] <= p_aabb.position.z) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. };
  215. struct InstanceVisibilityNotifierData;
  216. struct InstanceData {
  217. // Store instance pointer as well as common instance processing information,
  218. // to make processing more cache friendly.
  219. enum Flags : uint32_t {
  220. FLAG_BASE_TYPE_MASK = 0xFF,
  221. FLAG_CAST_SHADOWS = (1 << 8),
  222. FLAG_CAST_SHADOWS_ONLY = (1 << 9),
  223. FLAG_REDRAW_IF_VISIBLE = (1 << 10),
  224. FLAG_GEOM_LIGHTING_DIRTY = (1 << 11),
  225. FLAG_GEOM_REFLECTION_DIRTY = (1 << 12),
  226. FLAG_GEOM_DECAL_DIRTY = (1 << 13),
  227. FLAG_GEOM_VOXEL_GI_DIRTY = (1 << 14),
  228. FLAG_LIGHTMAP_CAPTURE = (1 << 15),
  229. FLAG_USES_BAKED_LIGHT = (1 << 16),
  230. FLAG_USES_MESH_INSTANCE = (1 << 17),
  231. FLAG_REFLECTION_PROBE_DIRTY = (1 << 18),
  232. FLAG_IGNORE_OCCLUSION_CULLING = (1 << 19),
  233. FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK = (3 << 20), // 2 bits, overlaps with the other vis. dependency flags
  234. FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE = (1 << 20),
  235. FLAG_VISIBILITY_DEPENDENCY_HIDDEN = (1 << 21),
  236. FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN = (1 << 22),
  237. FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY = (1 << 23),
  238. FLAG_IGNORE_ALL_CULLING = (1 << 24),
  239. };
  240. uint32_t flags = 0;
  241. uint32_t layer_mask = 0; //for fast layer-mask discard
  242. RID base_rid;
  243. union {
  244. uint64_t instance_data_rid;
  245. RenderGeometryInstance *instance_geometry;
  246. InstanceVisibilityNotifierData *visibility_notifier = nullptr;
  247. };
  248. Instance *instance = nullptr;
  249. int32_t parent_array_index = -1;
  250. int32_t visibility_index = -1;
  251. // Each time occlusion culling determines an instance is visible,
  252. // set this to occlusion_frame plus some delay.
  253. // Once the timeout is reached, allow the instance to be occlusion culled.
  254. // This creates a delay for occlusion culling, which prevents flickering
  255. // when jittering the raster occlusion projection.
  256. uint64_t occlusion_timeout = 0;
  257. };
  258. struct InstanceVisibilityData {
  259. uint64_t viewport_state = 0;
  260. int32_t array_index = -1;
  261. RS::VisibilityRangeFadeMode fade_mode = RS::VISIBILITY_RANGE_FADE_DISABLED;
  262. Vector3 position;
  263. Instance *instance = nullptr;
  264. float range_begin = 0.0f;
  265. float range_end = 0.0f;
  266. float range_begin_margin = 0.0f;
  267. float range_end_margin = 0.0f;
  268. float children_fade_alpha = 1.0f;
  269. };
  270. class VisibilityArray : public BinSortedArray<InstanceVisibilityData> {
  271. _FORCE_INLINE_ virtual void _update_idx(InstanceVisibilityData &r_element, uint64_t p_idx) {
  272. r_element.instance->visibility_index = p_idx;
  273. if (r_element.instance->scenario && r_element.instance->array_index != -1) {
  274. r_element.instance->scenario->instance_data[r_element.instance->array_index].visibility_index = p_idx;
  275. }
  276. }
  277. };
  278. PagedArrayPool<InstanceBounds> instance_aabb_page_pool;
  279. PagedArrayPool<InstanceData> instance_data_page_pool;
  280. PagedArrayPool<InstanceVisibilityData> instance_visibility_data_page_pool;
  281. struct Scenario {
  282. enum IndexerType {
  283. INDEXER_GEOMETRY, //for geometry
  284. INDEXER_VOLUMES, //for everything else
  285. INDEXER_MAX
  286. };
  287. DynamicBVH indexers[INDEXER_MAX];
  288. RID self;
  289. List<Instance *> directional_lights;
  290. RID environment;
  291. RID fallback_environment;
  292. RID camera_attributes;
  293. RID compositor;
  294. RID reflection_probe_shadow_atlas;
  295. RID reflection_atlas;
  296. uint64_t used_viewport_visibility_bits;
  297. HashMap<RID, uint64_t> viewport_visibility_masks;
  298. SelfList<Instance>::List instances;
  299. LocalVector<RID> dynamic_lights;
  300. PagedArray<InstanceBounds> instance_aabbs;
  301. PagedArray<InstanceData> instance_data;
  302. VisibilityArray instance_visibility;
  303. Scenario() {
  304. indexers[INDEXER_GEOMETRY].set_index(INDEXER_GEOMETRY);
  305. indexers[INDEXER_VOLUMES].set_index(INDEXER_VOLUMES);
  306. used_viewport_visibility_bits = 0;
  307. }
  308. };
  309. int indexer_update_iterations = 0;
  310. mutable RID_Owner<Scenario, true> scenario_owner;
  311. static void _instance_pair(Instance *p_A, Instance *p_B);
  312. static void _instance_unpair(Instance *p_A, Instance *p_B);
  313. void _instance_update_mesh_instance(Instance *p_instance) const;
  314. virtual RID scenario_allocate();
  315. virtual void scenario_initialize(RID p_rid);
  316. virtual void scenario_set_environment(RID p_scenario, RID p_environment);
  317. virtual void scenario_set_camera_attributes(RID p_scenario, RID p_attributes);
  318. virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
  319. virtual void scenario_set_compositor(RID p_scenario, RID p_compositor);
  320. virtual void scenario_set_reflection_atlas_size(RID p_scenario, int p_reflection_size, int p_reflection_count);
  321. virtual bool is_scenario(RID p_scenario) const;
  322. virtual RID scenario_get_environment(RID p_scenario);
  323. virtual void scenario_add_viewport_visibility_mask(RID p_scenario, RID p_viewport);
  324. virtual void scenario_remove_viewport_visibility_mask(RID p_scenario, RID p_viewport);
  325. /* INSTANCING API */
  326. struct InstancePair {
  327. Instance *a = nullptr;
  328. Instance *b = nullptr;
  329. SelfList<InstancePair> list_a;
  330. SelfList<InstancePair> list_b;
  331. InstancePair() :
  332. list_a(this), list_b(this) {}
  333. };
  334. mutable PagedAllocator<InstancePair> pair_allocator;
  335. struct InstanceBaseData {
  336. virtual ~InstanceBaseData() {}
  337. };
  338. struct Instance {
  339. RS::InstanceType base_type;
  340. RID base;
  341. RID skeleton;
  342. RID material_override;
  343. RID material_overlay;
  344. RID mesh_instance; //only used for meshes and when skeleton/blendshapes exist
  345. Transform3D transform;
  346. bool teleported = false;
  347. float lod_bias;
  348. bool ignore_occlusion_culling;
  349. bool ignore_all_culling;
  350. Vector<RID> materials;
  351. RS::ShadowCastingSetting cast_shadows;
  352. uint32_t layer_mask;
  353. // Fit in 32 bits.
  354. bool mirror : 1;
  355. bool receive_shadows : 1;
  356. bool visible : 1;
  357. bool baked_light : 1; // This flag is only to know if it actually did use baked light.
  358. bool dynamic_gi : 1; // Same as above for dynamic objects.
  359. bool redraw_if_visible : 1;
  360. Instance *lightmap = nullptr;
  361. Rect2 lightmap_uv_scale;
  362. int lightmap_slice_index;
  363. uint32_t lightmap_cull_index;
  364. Vector<Color> lightmap_sh; //spherical harmonic
  365. AABB aabb;
  366. AABB transformed_aabb;
  367. AABB prev_transformed_aabb;
  368. InstanceUniforms instance_uniforms;
  369. //
  370. RID self;
  371. //scenario stuff
  372. DynamicBVH::ID indexer_id;
  373. int32_t array_index = -1;
  374. int32_t visibility_index = -1;
  375. float visibility_range_begin = 0.0f;
  376. float visibility_range_end = 0.0f;
  377. float visibility_range_begin_margin = 0.0f;
  378. float visibility_range_end_margin = 0.0f;
  379. RS::VisibilityRangeFadeMode visibility_range_fade_mode = RS::VISIBILITY_RANGE_FADE_DISABLED;
  380. Instance *visibility_parent = nullptr;
  381. HashSet<Instance *> visibility_dependencies;
  382. uint32_t visibility_dependencies_depth = 0;
  383. float transparency = 0.0f;
  384. Scenario *scenario = nullptr;
  385. SelfList<Instance> scenario_item;
  386. //aabb stuff
  387. bool update_aabb;
  388. bool update_dependencies;
  389. SelfList<Instance> update_item;
  390. AABB *custom_aabb = nullptr; // <Zylann> would using aabb directly with a bool be better?
  391. float extra_margin;
  392. ObjectID object_id;
  393. // sorting
  394. float sorting_offset = 0.0;
  395. bool use_aabb_center = true;
  396. Vector<Color> lightmap_target_sh; //target is used for incrementally changing the SH over time, this avoids pops in some corner cases and when going interior <-> exterior
  397. uint64_t last_frame_pass;
  398. uint64_t version; // changes to this, and changes to base increase version
  399. InstanceBaseData *base_data = nullptr;
  400. SelfList<InstancePair>::List pairs;
  401. uint64_t pair_check;
  402. DependencyTracker dependency_tracker;
  403. static void dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *tracker) {
  404. Instance *instance = (Instance *)tracker->userdata;
  405. switch (p_notification) {
  406. case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA:
  407. case Dependency::DEPENDENCY_CHANGED_SKELETON_BONES:
  408. case Dependency::DEPENDENCY_CHANGED_AABB: {
  409. singleton->_instance_queue_update(instance, true, false);
  410. } break;
  411. case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES:
  412. case Dependency::DEPENDENCY_CHANGED_MATERIAL: {
  413. singleton->_instance_queue_update(instance, false, true);
  414. } break;
  415. case Dependency::DEPENDENCY_CHANGED_MESH:
  416. case Dependency::DEPENDENCY_CHANGED_PARTICLES:
  417. case Dependency::DEPENDENCY_CHANGED_MULTIMESH:
  418. case Dependency::DEPENDENCY_CHANGED_DECAL:
  419. case Dependency::DEPENDENCY_CHANGED_LIGHT:
  420. case Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE: {
  421. singleton->_instance_queue_update(instance, true, true);
  422. } break;
  423. case Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR:
  424. case Dependency::DEPENDENCY_CHANGED_CULL_MASK: {
  425. //requires repairing
  426. if (instance->indexer_id.is_valid()) {
  427. singleton->_unpair_instance(instance);
  428. singleton->_instance_queue_update(instance, true, true);
  429. }
  430. } break;
  431. default: {
  432. // Ignored notifications.
  433. } break;
  434. }
  435. }
  436. static void dependency_deleted(const RID &p_dependency, DependencyTracker *tracker) {
  437. Instance *instance = (Instance *)tracker->userdata;
  438. if (p_dependency == instance->base) {
  439. singleton->instance_set_base(instance->self, RID());
  440. } else if (p_dependency == instance->skeleton) {
  441. singleton->instance_attach_skeleton(instance->self, RID());
  442. } else {
  443. // It's possible the same material is used in multiple slots,
  444. // so we check whether we need to clear them all.
  445. if (p_dependency == instance->material_override) {
  446. singleton->instance_geometry_set_material_override(instance->self, RID());
  447. }
  448. if (p_dependency == instance->material_overlay) {
  449. singleton->instance_geometry_set_material_overlay(instance->self, RID());
  450. }
  451. for (int i = 0; i < instance->materials.size(); i++) {
  452. if (p_dependency == instance->materials[i]) {
  453. singleton->instance_set_surface_override_material(instance->self, i, RID());
  454. }
  455. }
  456. if (instance->base_type == RS::INSTANCE_PARTICLES) {
  457. RID particle_material = RSG::particles_storage->particles_get_process_material(instance->base);
  458. if (p_dependency == particle_material) {
  459. RSG::particles_storage->particles_set_process_material(instance->base, RID());
  460. }
  461. }
  462. // Even if no change is made we still need to call `_instance_queue_update`.
  463. // This dependency could also be a result of the freed material being used
  464. // by the mesh this mesh instance uses.
  465. singleton->_instance_queue_update(instance, false, true);
  466. }
  467. }
  468. Instance() :
  469. scenario_item(this),
  470. update_item(this) {
  471. base_type = RS::INSTANCE_NONE;
  472. cast_shadows = RS::SHADOW_CASTING_SETTING_ON;
  473. receive_shadows = true;
  474. visible = true;
  475. layer_mask = 1;
  476. baked_light = true;
  477. dynamic_gi = false;
  478. redraw_if_visible = false;
  479. lightmap_slice_index = 0;
  480. lightmap = nullptr;
  481. lightmap_cull_index = 0;
  482. lod_bias = 1.0;
  483. ignore_occlusion_culling = false;
  484. ignore_all_culling = false;
  485. scenario = nullptr;
  486. update_aabb = false;
  487. update_dependencies = false;
  488. extra_margin = 0;
  489. visible = true;
  490. visibility_range_begin = 0;
  491. visibility_range_end = 0;
  492. visibility_range_begin_margin = 0;
  493. visibility_range_end_margin = 0;
  494. last_frame_pass = 0;
  495. version = 1;
  496. base_data = nullptr;
  497. custom_aabb = nullptr;
  498. pair_check = 0;
  499. array_index = -1;
  500. dependency_tracker.userdata = this;
  501. dependency_tracker.changed_callback = dependency_changed;
  502. dependency_tracker.deleted_callback = dependency_deleted;
  503. }
  504. ~Instance() {
  505. if (base_data) {
  506. memdelete(base_data);
  507. }
  508. if (custom_aabb) {
  509. memdelete(custom_aabb);
  510. }
  511. }
  512. };
  513. mutable SelfList<Instance>::List _instance_update_list;
  514. void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies = false) const;
  515. struct InstanceGeometryData : public InstanceBaseData {
  516. RenderGeometryInstance *geometry_instance = nullptr;
  517. HashSet<Instance *> lights;
  518. bool can_cast_shadows;
  519. bool material_is_animated;
  520. uint32_t projector_count = 0;
  521. uint32_t softshadow_count = 0;
  522. HashSet<Instance *> decals;
  523. HashSet<Instance *> reflection_probes;
  524. HashSet<Instance *> voxel_gi_instances;
  525. HashSet<Instance *> lightmap_captures;
  526. InstanceGeometryData() {
  527. can_cast_shadows = true;
  528. material_is_animated = true;
  529. }
  530. };
  531. struct InstanceReflectionProbeData : public InstanceBaseData {
  532. Instance *owner = nullptr;
  533. HashSet<Instance *> geometries;
  534. RID instance;
  535. SelfList<InstanceReflectionProbeData> update_list;
  536. int render_step;
  537. InstanceReflectionProbeData() :
  538. update_list(this) {
  539. render_step = -1;
  540. }
  541. };
  542. struct InstanceDecalData : public InstanceBaseData {
  543. Instance *owner = nullptr;
  544. RID instance;
  545. uint32_t cull_mask = 0xFFFFFFFF;
  546. HashSet<Instance *> geometries;
  547. InstanceDecalData() {
  548. }
  549. };
  550. SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
  551. struct InstanceParticlesCollisionData : public InstanceBaseData {
  552. RID instance;
  553. uint32_t cull_mask = 0xFFFFFFFF;
  554. };
  555. struct InstanceFogVolumeData : public InstanceBaseData {
  556. RID instance;
  557. bool is_global;
  558. };
  559. struct InstanceVisibilityNotifierData : public InstanceBaseData {
  560. bool just_visible = false;
  561. uint64_t visible_in_frame = 0;
  562. RID base;
  563. SelfList<InstanceVisibilityNotifierData> list_element;
  564. InstanceVisibilityNotifierData() :
  565. list_element(this) {}
  566. };
  567. SpinLock visible_notifier_list_lock;
  568. SelfList<InstanceVisibilityNotifierData>::List visible_notifier_list;
  569. struct InstanceLightData : public InstanceBaseData {
  570. RID instance;
  571. uint64_t last_version;
  572. List<Instance *>::Element *D; // directional light in scenario
  573. bool uses_projector = false;
  574. bool uses_softshadow = false;
  575. HashSet<Instance *> geometries;
  576. Instance *baked_light = nullptr;
  577. RS::LightBakeMode bake_mode;
  578. uint32_t max_sdfgi_cascade = 2;
  579. uint32_t cull_mask = 0xFFFFFFFF;
  580. private:
  581. // Instead of a single dirty flag, we maintain a count
  582. // so that we can detect lights that are being made dirty
  583. // each frame, and switch on tighter caster culling.
  584. int32_t shadow_dirty_count;
  585. uint32_t light_update_frame_id;
  586. bool light_intersects_multiple_cameras;
  587. uint32_t light_intersects_multiple_cameras_timeout_frame_id;
  588. public:
  589. bool is_shadow_dirty() const { return shadow_dirty_count != 0; }
  590. void make_shadow_dirty() { shadow_dirty_count = light_intersects_multiple_cameras ? 1 : 2; }
  591. void detect_light_intersects_multiple_cameras(uint32_t p_frame_id) {
  592. // We need to detect the case where shadow updates are occurring
  593. // more than once per frame. In this case, we need to turn off
  594. // tighter caster culling, so situation reverts to one full shadow update
  595. // per frame (light_intersects_multiple_cameras is set).
  596. if (p_frame_id == light_update_frame_id) {
  597. light_intersects_multiple_cameras = true;
  598. light_intersects_multiple_cameras_timeout_frame_id = p_frame_id + 60;
  599. } else {
  600. // When shadow_volume_intersects_multiple_cameras is set, we
  601. // want to detect the situation this is no longer the case, via a timeout.
  602. // The system can go back to tighter caster culling in this situation.
  603. // Having a long-ish timeout prevents rapid cycling.
  604. if (light_intersects_multiple_cameras && (p_frame_id >= light_intersects_multiple_cameras_timeout_frame_id)) {
  605. light_intersects_multiple_cameras = false;
  606. light_intersects_multiple_cameras_timeout_frame_id = UINT32_MAX;
  607. }
  608. }
  609. light_update_frame_id = p_frame_id;
  610. }
  611. void decrement_shadow_dirty() {
  612. shadow_dirty_count--;
  613. DEV_ASSERT(shadow_dirty_count >= 0);
  614. }
  615. // Shadow updates can either full (everything in the shadow volume)
  616. // or closely culled to the camera frustum.
  617. bool is_shadow_update_full() const { return shadow_dirty_count == 0; }
  618. InstanceLightData() {
  619. bake_mode = RS::LIGHT_BAKE_DISABLED;
  620. D = nullptr;
  621. last_version = 0;
  622. baked_light = nullptr;
  623. shadow_dirty_count = 1;
  624. light_update_frame_id = UINT32_MAX;
  625. light_intersects_multiple_cameras_timeout_frame_id = UINT32_MAX;
  626. light_intersects_multiple_cameras = false;
  627. }
  628. };
  629. struct InstanceVoxelGIData : public InstanceBaseData {
  630. Instance *owner = nullptr;
  631. HashSet<Instance *> geometries;
  632. HashSet<Instance *> dynamic_geometries;
  633. HashSet<Instance *> lights;
  634. struct LightCache {
  635. RS::LightType type;
  636. Transform3D transform;
  637. Color color;
  638. float energy;
  639. float intensity;
  640. float bake_energy;
  641. float radius;
  642. float attenuation;
  643. float spot_angle;
  644. float spot_attenuation;
  645. bool has_shadow;
  646. RS::LightDirectionalSkyMode sky_mode;
  647. };
  648. Vector<LightCache> light_cache;
  649. Vector<RID> light_instances;
  650. RID probe_instance;
  651. bool invalid;
  652. uint32_t base_version;
  653. SelfList<InstanceVoxelGIData> update_element;
  654. InstanceVoxelGIData() :
  655. update_element(this) {
  656. invalid = true;
  657. base_version = 0;
  658. }
  659. };
  660. SelfList<InstanceVoxelGIData>::List voxel_gi_update_list;
  661. struct InstanceLightmapData : public InstanceBaseData {
  662. RID instance;
  663. HashSet<Instance *> geometries;
  664. HashSet<Instance *> users;
  665. InstanceLightmapData() {
  666. }
  667. };
  668. mutable uint64_t pair_pass = 1;
  669. struct PairInstances {
  670. Instance *instance = nullptr;
  671. PagedAllocator<InstancePair> *pair_allocator = nullptr;
  672. SelfList<InstancePair>::List pairs_found;
  673. DynamicBVH *bvh = nullptr;
  674. DynamicBVH *bvh2 = nullptr; //some may need to cull in two
  675. uint32_t pair_mask;
  676. uint64_t pair_pass;
  677. _FORCE_INLINE_ bool operator()(void *p_data) {
  678. Instance *p_instance = (Instance *)p_data;
  679. if (instance != p_instance && instance->transformed_aabb.intersects(p_instance->transformed_aabb) && (pair_mask & (1 << p_instance->base_type))) {
  680. //test is more coarse in indexer
  681. p_instance->pair_check = pair_pass;
  682. InstancePair *pair = pair_allocator->alloc();
  683. pair->a = instance;
  684. pair->b = p_instance;
  685. pairs_found.add(&pair->list_a);
  686. }
  687. return false;
  688. }
  689. void pair() {
  690. if (bvh) {
  691. bvh->aabb_query(instance->transformed_aabb, *this);
  692. }
  693. if (bvh2) {
  694. bvh2->aabb_query(instance->transformed_aabb, *this);
  695. }
  696. while (instance->pairs.first()) {
  697. InstancePair *pair = instance->pairs.first()->self();
  698. Instance *other_instance = instance == pair->a ? pair->b : pair->a;
  699. if (other_instance->pair_check != pair_pass) {
  700. //unpaired
  701. _instance_unpair(instance, other_instance);
  702. } else {
  703. //kept
  704. other_instance->pair_check = 0; // if kept, then put pair check to zero, so we can distinguish with the newly added ones
  705. }
  706. pair_allocator->free(pair);
  707. }
  708. while (pairs_found.first()) {
  709. InstancePair *pair = pairs_found.first()->self();
  710. pairs_found.remove(pairs_found.first());
  711. if (pair->b->pair_check == pair_pass) {
  712. //paired
  713. _instance_pair(instance, pair->b);
  714. }
  715. pair->a->pairs.add(&pair->list_a);
  716. pair->b->pairs.add(&pair->list_b);
  717. }
  718. }
  719. };
  720. mutable HashSet<Instance *> heightfield_particle_colliders_update_list;
  721. PagedArrayPool<Instance *> instance_cull_page_pool;
  722. PagedArrayPool<RenderGeometryInstance *> geometry_instance_cull_page_pool;
  723. PagedArrayPool<RID> rid_cull_page_pool;
  724. PagedArray<Instance *> instance_cull_result;
  725. PagedArray<Instance *> instance_shadow_cull_result;
  726. struct InstanceCullResult {
  727. PagedArray<RenderGeometryInstance *> geometry_instances;
  728. PagedArray<Instance *> lights;
  729. PagedArray<RID> light_instances;
  730. PagedArray<RID> lightmaps;
  731. PagedArray<RID> reflections;
  732. PagedArray<RID> decals;
  733. PagedArray<RID> voxel_gi_instances;
  734. PagedArray<RID> mesh_instances;
  735. PagedArray<RID> fog_volumes;
  736. struct DirectionalShadow {
  737. PagedArray<RenderGeometryInstance *> cascade_geometry_instances[RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES];
  738. } directional_shadows[RendererSceneRender::MAX_DIRECTIONAL_LIGHTS];
  739. PagedArray<RenderGeometryInstance *> sdfgi_region_geometry_instances[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE];
  740. PagedArray<RID> sdfgi_cascade_lights[SDFGI_MAX_CASCADES];
  741. void clear() {
  742. geometry_instances.clear();
  743. lights.clear();
  744. light_instances.clear();
  745. lightmaps.clear();
  746. reflections.clear();
  747. decals.clear();
  748. voxel_gi_instances.clear();
  749. mesh_instances.clear();
  750. fog_volumes.clear();
  751. for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
  752. for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
  753. directional_shadows[i].cascade_geometry_instances[j].clear();
  754. }
  755. }
  756. for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
  757. sdfgi_region_geometry_instances[i].clear();
  758. }
  759. for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
  760. sdfgi_cascade_lights[i].clear();
  761. }
  762. }
  763. void reset() {
  764. geometry_instances.reset();
  765. lights.reset();
  766. light_instances.reset();
  767. lightmaps.reset();
  768. reflections.reset();
  769. decals.reset();
  770. voxel_gi_instances.reset();
  771. mesh_instances.reset();
  772. fog_volumes.reset();
  773. for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
  774. for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
  775. directional_shadows[i].cascade_geometry_instances[j].reset();
  776. }
  777. }
  778. for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
  779. sdfgi_region_geometry_instances[i].reset();
  780. }
  781. for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
  782. sdfgi_cascade_lights[i].reset();
  783. }
  784. }
  785. void append_from(InstanceCullResult &p_cull_result) {
  786. geometry_instances.merge_unordered(p_cull_result.geometry_instances);
  787. lights.merge_unordered(p_cull_result.lights);
  788. light_instances.merge_unordered(p_cull_result.light_instances);
  789. lightmaps.merge_unordered(p_cull_result.lightmaps);
  790. reflections.merge_unordered(p_cull_result.reflections);
  791. decals.merge_unordered(p_cull_result.decals);
  792. voxel_gi_instances.merge_unordered(p_cull_result.voxel_gi_instances);
  793. mesh_instances.merge_unordered(p_cull_result.mesh_instances);
  794. fog_volumes.merge_unordered(p_cull_result.fog_volumes);
  795. for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
  796. for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
  797. directional_shadows[i].cascade_geometry_instances[j].merge_unordered(p_cull_result.directional_shadows[i].cascade_geometry_instances[j]);
  798. }
  799. }
  800. for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
  801. sdfgi_region_geometry_instances[i].merge_unordered(p_cull_result.sdfgi_region_geometry_instances[i]);
  802. }
  803. for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
  804. sdfgi_cascade_lights[i].merge_unordered(p_cull_result.sdfgi_cascade_lights[i]);
  805. }
  806. }
  807. void init(PagedArrayPool<RID> *p_rid_pool, PagedArrayPool<RenderGeometryInstance *> *p_geometry_instance_pool, PagedArrayPool<Instance *> *p_instance_pool) {
  808. geometry_instances.set_page_pool(p_geometry_instance_pool);
  809. light_instances.set_page_pool(p_rid_pool);
  810. lights.set_page_pool(p_instance_pool);
  811. lightmaps.set_page_pool(p_rid_pool);
  812. reflections.set_page_pool(p_rid_pool);
  813. decals.set_page_pool(p_rid_pool);
  814. voxel_gi_instances.set_page_pool(p_rid_pool);
  815. mesh_instances.set_page_pool(p_rid_pool);
  816. fog_volumes.set_page_pool(p_rid_pool);
  817. for (int i = 0; i < RendererSceneRender::MAX_DIRECTIONAL_LIGHTS; i++) {
  818. for (int j = 0; j < RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES; j++) {
  819. directional_shadows[i].cascade_geometry_instances[j].set_page_pool(p_geometry_instance_pool);
  820. }
  821. }
  822. for (int i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
  823. sdfgi_region_geometry_instances[i].set_page_pool(p_geometry_instance_pool);
  824. }
  825. for (int i = 0; i < SDFGI_MAX_CASCADES; i++) {
  826. sdfgi_cascade_lights[i].set_page_pool(p_rid_pool);
  827. }
  828. }
  829. };
  830. InstanceCullResult scene_cull_result;
  831. LocalVector<InstanceCullResult> scene_cull_result_threads;
  832. RendererSceneRender::RenderShadowData render_shadow_data[MAX_UPDATE_SHADOWS];
  833. uint32_t max_shadows_used = 0;
  834. RendererSceneRender::RenderSDFGIData render_sdfgi_data[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE];
  835. RendererSceneRender::RenderSDFGIUpdateData sdfgi_update_data;
  836. uint32_t thread_cull_threshold = 200;
  837. mutable RID_Owner<Instance, true> instance_owner{ 65536, 4194304 };
  838. uint32_t geometry_instance_pair_mask = 0; // used in traditional forward, unnecessary on clustered
  839. LocalVector<Vector2> camera_jitter_array;
  840. RenderingLightCuller *light_culler = nullptr;
  841. virtual RID instance_allocate();
  842. virtual void instance_initialize(RID p_rid);
  843. virtual void instance_set_base(RID p_instance, RID p_base);
  844. virtual void instance_set_scenario(RID p_instance, RID p_scenario);
  845. virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
  846. virtual void instance_set_pivot_data(RID p_instance, float p_sorting_offset, bool p_use_aabb_center);
  847. virtual void instance_set_transform(RID p_instance, const Transform3D &p_transform);
  848. virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_id);
  849. virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
  850. virtual void instance_set_surface_override_material(RID p_instance, int p_surface, RID p_material);
  851. virtual void instance_set_visible(RID p_instance, bool p_visible);
  852. virtual void instance_geometry_set_transparency(RID p_instance, float p_transparency);
  853. virtual void instance_teleport(RID p_instance);
  854. virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
  855. virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
  856. virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
  857. virtual void instance_set_visibility_parent(RID p_instance, RID p_parent_instance);
  858. virtual void instance_set_ignore_culling(RID p_instance, bool p_enabled);
  859. bool _update_instance_visibility_depth(Instance *p_instance);
  860. void _update_instance_visibility_dependencies(Instance *p_instance) const;
  861. // don't use these in a game!
  862. virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
  863. virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
  864. virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
  865. virtual void instance_geometry_set_flag(RID p_instance, RS::InstanceFlags p_flags, bool p_enabled);
  866. virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, RS::ShadowCastingSetting p_shadow_casting_setting);
  867. virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
  868. virtual void instance_geometry_set_material_overlay(RID p_instance, RID p_material);
  869. virtual void instance_geometry_set_visibility_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin, RS::VisibilityRangeFadeMode p_fade_mode);
  870. virtual void instance_geometry_set_lightmap(RID p_instance, RID p_lightmap, const Rect2 &p_lightmap_uv_scale, int p_slice_index);
  871. virtual void instance_geometry_set_lod_bias(RID p_instance, float p_lod_bias);
  872. virtual void instance_geometry_set_shader_parameter(RID p_instance, const StringName &p_parameter, const Variant &p_value);
  873. virtual void instance_geometry_get_shader_parameter_list(RID p_instance, List<PropertyInfo> *p_parameters) const;
  874. virtual Variant instance_geometry_get_shader_parameter(RID p_instance, const StringName &p_parameter) const;
  875. virtual Variant instance_geometry_get_shader_parameter_default_value(RID p_instance, const StringName &p_parameter) const;
  876. virtual void mesh_generate_pipelines(RID p_mesh, bool p_background_compilation);
  877. virtual uint32_t get_pipeline_compilations(RS::PipelineSource p_source);
  878. _FORCE_INLINE_ void _update_instance(Instance *p_instance) const;
  879. _FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance) const;
  880. _FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance) const;
  881. _FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance) const;
  882. void _unpair_instance(Instance *p_instance);
  883. void _light_instance_setup_directional_shadow(int p_shadow_index, Instance *p_instance, const Transform3D p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect);
  884. _FORCE_INLINE_ bool _light_instance_update_shadow(Instance *p_instance, const Transform3D p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_shadow_atlas, Scenario *p_scenario, float p_screen_mesh_lod_threshold, uint32_t p_visible_layers = 0xFFFFFF);
  885. RID _render_get_environment(RID p_camera, RID p_scenario);
  886. RID _render_get_compositor(RID p_camera, RID p_scenario);
  887. struct Cull {
  888. struct Shadow {
  889. RID light_instance;
  890. uint32_t caster_mask;
  891. struct Cascade {
  892. Frustum frustum;
  893. Projection projection;
  894. Transform3D transform;
  895. real_t zfar;
  896. real_t split;
  897. real_t shadow_texel_size;
  898. real_t bias_scale;
  899. real_t range_begin;
  900. Vector2 uv_scale;
  901. } cascades[RendererSceneRender::MAX_DIRECTIONAL_LIGHT_CASCADES]; //max 4 cascades
  902. uint32_t cascade_count;
  903. } shadows[RendererSceneRender::MAX_DIRECTIONAL_LIGHTS];
  904. uint32_t shadow_count;
  905. struct SDFGI {
  906. //have arrays here because SDFGI functions expects this, plus regions can have areas
  907. AABB region_aabb[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE]; //max 3 regions per cascade
  908. uint32_t region_cascade[SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE]; //max 3 regions per cascade
  909. uint32_t region_count = 0;
  910. uint32_t cascade_light_index[SDFGI_MAX_CASCADES];
  911. uint32_t cascade_light_count = 0;
  912. } sdfgi;
  913. SpinLock lock;
  914. Frustum frustum;
  915. } cull;
  916. struct VisibilityCullData {
  917. uint64_t viewport_mask;
  918. Scenario *scenario = nullptr;
  919. Vector3 camera_position;
  920. uint32_t cull_offset;
  921. uint32_t cull_count;
  922. };
  923. void _visibility_cull_threaded(uint32_t p_thread, VisibilityCullData *cull_data);
  924. void _visibility_cull(const VisibilityCullData &cull_data, uint64_t p_from, uint64_t p_to);
  925. template <bool p_fade_check>
  926. _FORCE_INLINE_ int _visibility_range_check(InstanceVisibilityData &r_vis_data, const Vector3 &p_camera_pos, uint64_t p_viewport_mask);
  927. struct CullData {
  928. Cull *cull = nullptr;
  929. Scenario *scenario = nullptr;
  930. RID shadow_atlas;
  931. Transform3D cam_transform;
  932. uint32_t visible_layers;
  933. Instance *render_reflection_probe = nullptr;
  934. const RendererSceneOcclusionCull::HZBuffer *occlusion_buffer;
  935. const Projection *camera_matrix;
  936. uint64_t visibility_viewport_mask;
  937. };
  938. void _scene_cull_threaded(uint32_t p_thread, CullData *cull_data);
  939. void _scene_cull(CullData &cull_data, InstanceCullResult &cull_result, uint64_t p_from, uint64_t p_to);
  940. static void _scene_particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis);
  941. _FORCE_INLINE_ bool _visibility_parent_check(const CullData &p_cull_data, const InstanceData &p_instance_data);
  942. bool _render_reflection_probe_step(Instance *p_instance, int p_step);
  943. void _render_scene(const RendererSceneRender::CameraData *p_camera_data, const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, RID p_force_camera_attributes, RID p_compositor, uint32_t p_visible_layers, RID p_scenario, RID p_viewport, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_mesh_lod_threshold, bool p_using_shadows = true, RenderInfo *r_render_info = nullptr);
  944. void render_empty_scene(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_scenario, RID p_shadow_atlas);
  945. void render_camera(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_camera, RID p_scenario, RID p_viewport, Size2 p_viewport_size, uint32_t p_jitter_phase_count, float p_screen_mesh_lod_threshold, RID p_shadow_atlas, Ref<XRInterface> &p_xr_interface, RenderingMethod::RenderInfo *r_render_info = nullptr);
  946. void update_dirty_instances() const;
  947. void render_particle_colliders();
  948. virtual void render_probes();
  949. TypedArray<Image> bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size);
  950. //pass to scene render
  951. /* ENVIRONMENT API */
  952. #ifdef PASSBASE
  953. #undef PASSBASE
  954. #endif
  955. #define PASSBASE scene_render
  956. PASS1(voxel_gi_set_quality, RS::VoxelGIQuality)
  957. /* SKY API */
  958. PASS0R(RID, sky_allocate)
  959. PASS1(sky_initialize, RID)
  960. PASS2(sky_set_radiance_size, RID, int)
  961. PASS2(sky_set_mode, RID, RS::SkyMode)
  962. PASS2(sky_set_material, RID, RID)
  963. PASS4R(Ref<Image>, sky_bake_panorama, RID, float, bool, const Size2i &)
  964. // Compositor effect
  965. PASS0R(RID, compositor_effect_allocate)
  966. PASS1(compositor_effect_initialize, RID)
  967. PASS1RC(bool, is_compositor_effect, RID)
  968. PASS2(compositor_effect_set_enabled, RID, bool)
  969. PASS3(compositor_effect_set_callback, RID, RS::CompositorEffectCallbackType, const Callable &)
  970. PASS3(compositor_effect_set_flag, RID, RS::CompositorEffectFlags, bool)
  971. // Compositor
  972. PASS0R(RID, compositor_allocate)
  973. PASS1(compositor_initialize, RID)
  974. PASS1RC(bool, is_compositor, RID)
  975. PASS2(compositor_set_compositor_effects, RID, const TypedArray<RID> &)
  976. // Environment
  977. PASS0R(RID, environment_allocate)
  978. PASS1(environment_initialize, RID)
  979. PASS1RC(bool, is_environment, RID)
  980. // Background
  981. PASS2(environment_set_background, RID, RS::EnvironmentBG)
  982. PASS2(environment_set_sky, RID, RID)
  983. PASS2(environment_set_sky_custom_fov, RID, float)
  984. PASS2(environment_set_sky_orientation, RID, const Basis &)
  985. PASS2(environment_set_bg_color, RID, const Color &)
  986. PASS3(environment_set_bg_energy, RID, float, float)
  987. PASS2(environment_set_canvas_max_layer, RID, int)
  988. PASS6(environment_set_ambient_light, RID, const Color &, RS::EnvironmentAmbientSource, float, float, RS::EnvironmentReflectionSource)
  989. PASS2(environment_set_camera_feed_id, RID, int)
  990. PASS1RC(RS::EnvironmentBG, environment_get_background, RID)
  991. PASS1RC(RID, environment_get_sky, RID)
  992. PASS1RC(float, environment_get_sky_custom_fov, RID)
  993. PASS1RC(Basis, environment_get_sky_orientation, RID)
  994. PASS1RC(Color, environment_get_bg_color, RID)
  995. PASS1RC(float, environment_get_bg_energy_multiplier, RID)
  996. PASS1RC(float, environment_get_bg_intensity, RID)
  997. PASS1RC(int, environment_get_canvas_max_layer, RID)
  998. PASS1RC(RS::EnvironmentAmbientSource, environment_get_ambient_source, RID)
  999. PASS1RC(Color, environment_get_ambient_light, RID)
  1000. PASS1RC(float, environment_get_ambient_light_energy, RID)
  1001. PASS1RC(float, environment_get_ambient_sky_contribution, RID)
  1002. PASS1RC(RS::EnvironmentReflectionSource, environment_get_reflection_source, RID)
  1003. // Tonemap
  1004. PASS4(environment_set_tonemap, RID, RS::EnvironmentToneMapper, float, float)
  1005. PASS1RC(RS::EnvironmentToneMapper, environment_get_tone_mapper, RID)
  1006. PASS1RC(float, environment_get_exposure, RID)
  1007. PASS1RC(float, environment_get_white, RID)
  1008. // Fog
  1009. PASS11(environment_set_fog, RID, bool, const Color &, float, float, float, float, float, float, float, RS::EnvironmentFogMode)
  1010. PASS1RC(bool, environment_get_fog_enabled, RID)
  1011. PASS1RC(Color, environment_get_fog_light_color, RID)
  1012. PASS1RC(float, environment_get_fog_light_energy, RID)
  1013. PASS1RC(float, environment_get_fog_sun_scatter, RID)
  1014. PASS1RC(float, environment_get_fog_density, RID)
  1015. PASS1RC(float, environment_get_fog_sky_affect, RID)
  1016. PASS1RC(float, environment_get_fog_height, RID)
  1017. PASS1RC(float, environment_get_fog_height_density, RID)
  1018. PASS1RC(float, environment_get_fog_aerial_perspective, RID)
  1019. PASS1RC(RS::EnvironmentFogMode, environment_get_fog_mode, RID)
  1020. PASS2(environment_set_volumetric_fog_volume_size, int, int)
  1021. PASS1(environment_set_volumetric_fog_filter_active, bool)
  1022. // Depth Fog
  1023. PASS4(environment_set_fog_depth, RID, float, float, float)
  1024. PASS1RC(float, environment_get_fog_depth_curve, RID)
  1025. PASS1RC(float, environment_get_fog_depth_begin, RID)
  1026. PASS1RC(float, environment_get_fog_depth_end, RID)
  1027. // Volumentric Fog
  1028. PASS14(environment_set_volumetric_fog, RID, bool, float, const Color &, const Color &, float, float, float, float, float, bool, float, float, float)
  1029. PASS1RC(bool, environment_get_volumetric_fog_enabled, RID)
  1030. PASS1RC(float, environment_get_volumetric_fog_density, RID)
  1031. PASS1RC(Color, environment_get_volumetric_fog_scattering, RID)
  1032. PASS1RC(Color, environment_get_volumetric_fog_emission, RID)
  1033. PASS1RC(float, environment_get_volumetric_fog_emission_energy, RID)
  1034. PASS1RC(float, environment_get_volumetric_fog_anisotropy, RID)
  1035. PASS1RC(float, environment_get_volumetric_fog_length, RID)
  1036. PASS1RC(float, environment_get_volumetric_fog_detail_spread, RID)
  1037. PASS1RC(float, environment_get_volumetric_fog_gi_inject, RID)
  1038. PASS1RC(float, environment_get_volumetric_fog_sky_affect, RID)
  1039. PASS1RC(bool, environment_get_volumetric_fog_temporal_reprojection, RID)
  1040. PASS1RC(float, environment_get_volumetric_fog_temporal_reprojection_amount, RID)
  1041. PASS1RC(float, environment_get_volumetric_fog_ambient_inject, RID)
  1042. // Glow
  1043. PASS13(environment_set_glow, RID, bool, Vector<float>, float, float, float, float, RS::EnvironmentGlowBlendMode, float, float, float, float, RID)
  1044. PASS1RC(bool, environment_get_glow_enabled, RID)
  1045. PASS1RC(Vector<float>, environment_get_glow_levels, RID)
  1046. PASS1RC(float, environment_get_glow_intensity, RID)
  1047. PASS1RC(float, environment_get_glow_strength, RID)
  1048. PASS1RC(float, environment_get_glow_bloom, RID)
  1049. PASS1RC(float, environment_get_glow_mix, RID)
  1050. PASS1RC(RS::EnvironmentGlowBlendMode, environment_get_glow_blend_mode, RID)
  1051. PASS1RC(float, environment_get_glow_hdr_bleed_threshold, RID)
  1052. PASS1RC(float, environment_get_glow_hdr_luminance_cap, RID)
  1053. PASS1RC(float, environment_get_glow_hdr_bleed_scale, RID)
  1054. PASS1RC(float, environment_get_glow_map_strength, RID)
  1055. PASS1RC(RID, environment_get_glow_map, RID)
  1056. PASS1(environment_glow_set_use_bicubic_upscale, bool)
  1057. // SSR
  1058. PASS6(environment_set_ssr, RID, bool, int, float, float, float)
  1059. PASS1RC(bool, environment_get_ssr_enabled, RID)
  1060. PASS1RC(int, environment_get_ssr_max_steps, RID)
  1061. PASS1RC(float, environment_get_ssr_fade_in, RID)
  1062. PASS1RC(float, environment_get_ssr_fade_out, RID)
  1063. PASS1RC(float, environment_get_ssr_depth_tolerance, RID)
  1064. PASS1(environment_set_ssr_roughness_quality, RS::EnvironmentSSRRoughnessQuality)
  1065. // SSAO
  1066. PASS10(environment_set_ssao, RID, bool, float, float, float, float, float, float, float, float)
  1067. PASS1RC(bool, environment_get_ssao_enabled, RID)
  1068. PASS1RC(float, environment_get_ssao_radius, RID)
  1069. PASS1RC(float, environment_get_ssao_intensity, RID)
  1070. PASS1RC(float, environment_get_ssao_power, RID)
  1071. PASS1RC(float, environment_get_ssao_detail, RID)
  1072. PASS1RC(float, environment_get_ssao_horizon, RID)
  1073. PASS1RC(float, environment_get_ssao_sharpness, RID)
  1074. PASS1RC(float, environment_get_ssao_direct_light_affect, RID)
  1075. PASS1RC(float, environment_get_ssao_ao_channel_affect, RID)
  1076. PASS6(environment_set_ssao_quality, RS::EnvironmentSSAOQuality, bool, float, int, float, float)
  1077. // SSIL
  1078. PASS6(environment_set_ssil, RID, bool, float, float, float, float)
  1079. PASS1RC(bool, environment_get_ssil_enabled, RID)
  1080. PASS1RC(float, environment_get_ssil_radius, RID)
  1081. PASS1RC(float, environment_get_ssil_intensity, RID)
  1082. PASS1RC(float, environment_get_ssil_sharpness, RID)
  1083. PASS1RC(float, environment_get_ssil_normal_rejection, RID)
  1084. PASS6(environment_set_ssil_quality, RS::EnvironmentSSILQuality, bool, float, int, float, float)
  1085. // SDFGI
  1086. PASS11(environment_set_sdfgi, RID, bool, int, float, RS::EnvironmentSDFGIYScale, bool, float, bool, float, float, float)
  1087. PASS1RC(bool, environment_get_sdfgi_enabled, RID)
  1088. PASS1RC(int, environment_get_sdfgi_cascades, RID)
  1089. PASS1RC(float, environment_get_sdfgi_min_cell_size, RID)
  1090. PASS1RC(bool, environment_get_sdfgi_use_occlusion, RID)
  1091. PASS1RC(float, environment_get_sdfgi_bounce_feedback, RID)
  1092. PASS1RC(bool, environment_get_sdfgi_read_sky_light, RID)
  1093. PASS1RC(float, environment_get_sdfgi_energy, RID)
  1094. PASS1RC(float, environment_get_sdfgi_normal_bias, RID)
  1095. PASS1RC(float, environment_get_sdfgi_probe_bias, RID)
  1096. PASS1RC(RS::EnvironmentSDFGIYScale, environment_get_sdfgi_y_scale, RID)
  1097. PASS1(environment_set_sdfgi_ray_count, RS::EnvironmentSDFGIRayCount)
  1098. PASS1(environment_set_sdfgi_frames_to_converge, RS::EnvironmentSDFGIFramesToConverge)
  1099. PASS1(environment_set_sdfgi_frames_to_update_light, RS::EnvironmentSDFGIFramesToUpdateLight)
  1100. // Adjustment
  1101. PASS7(environment_set_adjustment, RID, bool, float, float, float, bool, RID)
  1102. PASS1RC(bool, environment_get_adjustments_enabled, RID)
  1103. PASS1RC(float, environment_get_adjustments_brightness, RID)
  1104. PASS1RC(float, environment_get_adjustments_contrast, RID)
  1105. PASS1RC(float, environment_get_adjustments_saturation, RID)
  1106. PASS1RC(bool, environment_get_use_1d_color_correction, RID)
  1107. PASS1RC(RID, environment_get_color_correction, RID)
  1108. PASS3R(Ref<Image>, environment_bake_panorama, RID, bool, const Size2i &)
  1109. PASS3(screen_space_roughness_limiter_set_active, bool, float, float)
  1110. PASS1(sub_surface_scattering_set_quality, RS::SubSurfaceScatteringQuality)
  1111. PASS2(sub_surface_scattering_set_scale, float, float)
  1112. PASS1(positional_soft_shadow_filter_set_quality, RS::ShadowQuality)
  1113. PASS1(directional_soft_shadow_filter_set_quality, RS::ShadowQuality)
  1114. PASS2(sdfgi_set_debug_probe_select, const Vector3 &, const Vector3 &)
  1115. /* Render Buffers */
  1116. PASS0R(Ref<RenderSceneBuffers>, render_buffers_create)
  1117. PASS1(gi_set_use_half_resolution, bool)
  1118. /* Misc */
  1119. PASS1(set_debug_draw_mode, RS::ViewportDebugDraw)
  1120. PASS1(decals_set_filter, RS::DecalFilter)
  1121. PASS1(light_projectors_set_filter, RS::LightProjectorFilter)
  1122. PASS1(lightmaps_set_bicubic_filter, bool)
  1123. virtual void update();
  1124. bool free(RID p_rid);
  1125. void set_scene_render(RendererSceneRender *p_scene_render);
  1126. virtual void update_visibility_notifiers();
  1127. /* INTERPOLATION */
  1128. void update_interpolation_tick(bool p_process = true);
  1129. void update_interpolation_frame(bool p_process = true);
  1130. virtual void set_physics_interpolation_enabled(bool p_enabled);
  1131. struct InterpolationData {
  1132. bool interpolation_enabled = false;
  1133. } _interpolation_data;
  1134. RendererSceneCull();
  1135. virtual ~RendererSceneCull();
  1136. };