renderer_scene_cull.h 48 KB

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