renderer_scene_cull.h 45 KB

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