visual_server_scene.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*************************************************************************/
  2. /* visual_server_scene.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 VISUALSERVERSCENE_H
  31. #define VISUALSERVERSCENE_H
  32. #include "servers/visual/rasterizer.h"
  33. #include "core/math/bvh.h"
  34. #include "core/math/geometry.h"
  35. #include "core/math/octree.h"
  36. #include "core/os/semaphore.h"
  37. #include "core/os/thread.h"
  38. #include "core/safe_refcount.h"
  39. #include "core/self_list.h"
  40. #include "portals/portal_renderer.h"
  41. #include "servers/arvr/arvr_interface.h"
  42. class VisualServerScene {
  43. public:
  44. enum {
  45. MAX_INSTANCE_CULL = 65536,
  46. MAX_LIGHTS_CULLED = 4096,
  47. MAX_REFLECTION_PROBES_CULLED = 4096,
  48. MAX_ROOM_CULL = 32,
  49. MAX_EXTERIOR_PORTALS = 128,
  50. };
  51. uint64_t render_pass;
  52. static VisualServerScene *singleton;
  53. /* CAMERA API */
  54. struct Camera : public RID_Data {
  55. enum Type {
  56. PERSPECTIVE,
  57. ORTHOGONAL,
  58. FRUSTUM
  59. };
  60. Type type;
  61. float fov;
  62. float znear, zfar;
  63. float size;
  64. Vector2 offset;
  65. uint32_t visible_layers;
  66. bool vaspect;
  67. RID env;
  68. Transform transform;
  69. int32_t previous_room_id_hint;
  70. Camera() {
  71. visible_layers = 0xFFFFFFFF;
  72. fov = 70;
  73. type = PERSPECTIVE;
  74. znear = 0.05;
  75. zfar = 100;
  76. size = 1.0;
  77. offset = Vector2();
  78. vaspect = false;
  79. previous_room_id_hint = -1;
  80. }
  81. };
  82. mutable RID_Owner<Camera> camera_owner;
  83. virtual RID camera_create();
  84. virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
  85. virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
  86. virtual void camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
  87. virtual void camera_set_transform(RID p_camera, const Transform &p_transform);
  88. virtual void camera_set_cull_mask(RID p_camera, uint32_t p_layers);
  89. virtual void camera_set_environment(RID p_camera, RID p_env);
  90. virtual void camera_set_use_vertical_aspect(RID p_camera, bool p_enable);
  91. /* SCENARIO API */
  92. struct Instance;
  93. // common interface for all spatial partitioning schemes
  94. // this is a bit excessive boilerplatewise but can be removed if we decide to stick with one method
  95. // note this is actually the BVH id +1, so that visual server can test against zero
  96. // for validity to maintain compatibility with octree (where 0 indicates invalid)
  97. typedef uint32_t SpatialPartitionID;
  98. class SpatialPartitioningScene {
  99. public:
  100. virtual SpatialPartitionID create(Instance *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1) = 0;
  101. virtual void erase(SpatialPartitionID p_handle) = 0;
  102. virtual void move(SpatialPartitionID p_handle, const AABB &p_aabb) = 0;
  103. virtual void activate(SpatialPartitionID p_handle, const AABB &p_aabb) {}
  104. virtual void deactivate(SpatialPartitionID p_handle) {}
  105. virtual void force_collision_check(SpatialPartitionID p_handle) {}
  106. virtual void update() {}
  107. virtual void update_collisions() {}
  108. virtual void set_pairable(SpatialPartitionID p_handle, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) = 0;
  109. virtual int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF) = 0;
  110. virtual int cull_aabb(const AABB &p_aabb, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF) = 0;
  111. virtual int cull_segment(const Vector3 &p_from, const Vector3 &p_to, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF) = 0;
  112. typedef void *(*PairCallback)(void *, uint32_t, Instance *, int, uint32_t, Instance *, int);
  113. typedef void (*UnpairCallback)(void *, uint32_t, Instance *, int, uint32_t, Instance *, int, void *);
  114. virtual void set_pair_callback(PairCallback p_callback, void *p_userdata) = 0;
  115. virtual void set_unpair_callback(UnpairCallback p_callback, void *p_userdata) = 0;
  116. // bvh specific
  117. virtual void params_set_node_expansion(real_t p_value) {}
  118. virtual void params_set_pairing_expansion(real_t p_value) {}
  119. // octree specific
  120. virtual void set_balance(float p_balance) {}
  121. virtual ~SpatialPartitioningScene() {}
  122. };
  123. class SpatialPartitioningScene_Octree : public SpatialPartitioningScene {
  124. Octree_CL<Instance, true> _octree;
  125. public:
  126. SpatialPartitionID create(Instance *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
  127. void erase(SpatialPartitionID p_handle);
  128. void move(SpatialPartitionID p_handle, const AABB &p_aabb);
  129. void set_pairable(SpatialPartitionID p_handle, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
  130. int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  131. int cull_aabb(const AABB &p_aabb, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  132. int cull_segment(const Vector3 &p_from, const Vector3 &p_to, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  133. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  134. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  135. void set_balance(float p_balance);
  136. };
  137. class SpatialPartitioningScene_BVH : public SpatialPartitioningScene {
  138. // Note that SpatialPartitionIDs are +1 based when stored in visual server, to enable 0 to indicate invalid ID.
  139. BVH_Manager<Instance, true, 256> _bvh;
  140. public:
  141. SpatialPartitioningScene_BVH();
  142. SpatialPartitionID create(Instance *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t p_pairable_mask = 1);
  143. void erase(SpatialPartitionID p_handle);
  144. void move(SpatialPartitionID p_handle, const AABB &p_aabb);
  145. void activate(SpatialPartitionID p_handle, const AABB &p_aabb);
  146. void deactivate(SpatialPartitionID p_handle);
  147. void force_collision_check(SpatialPartitionID p_handle);
  148. void update();
  149. void update_collisions();
  150. void set_pairable(SpatialPartitionID p_handle, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
  151. int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  152. int cull_aabb(const AABB &p_aabb, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  153. int cull_segment(const Vector3 &p_from, const Vector3 &p_to, Instance **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  154. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  155. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  156. void params_set_node_expansion(real_t p_value) { _bvh.params_set_node_expansion(p_value); }
  157. void params_set_pairing_expansion(real_t p_value) { _bvh.params_set_pairing_expansion(p_value); }
  158. };
  159. struct Scenario : RID_Data {
  160. VS::ScenarioDebugMode debug;
  161. RID self;
  162. SpatialPartitioningScene *sps;
  163. PortalRenderer _portal_renderer;
  164. List<Instance *> directional_lights;
  165. RID environment;
  166. RID fallback_environment;
  167. RID reflection_probe_shadow_atlas;
  168. RID reflection_atlas;
  169. SelfList<Instance>::List instances;
  170. Scenario();
  171. ~Scenario() { memdelete(sps); }
  172. };
  173. mutable RID_Owner<Scenario> scenario_owner;
  174. static void *_instance_pair(void *p_self, SpatialPartitionID, Instance *p_A, int, SpatialPartitionID, Instance *p_B, int);
  175. static void _instance_unpair(void *p_self, SpatialPartitionID, Instance *p_A, int, SpatialPartitionID, Instance *p_B, int, void *);
  176. virtual RID scenario_create();
  177. virtual void scenario_set_debug(RID p_scenario, VS::ScenarioDebugMode p_debug_mode);
  178. virtual void scenario_set_environment(RID p_scenario, RID p_environment);
  179. virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
  180. virtual void scenario_set_reflection_atlas_size(RID p_scenario, int p_size, int p_subdiv);
  181. /* INSTANCING API */
  182. struct InstanceBaseData {
  183. virtual ~InstanceBaseData() {}
  184. };
  185. struct Instance : RasterizerScene::InstanceBase {
  186. RID self;
  187. //scenario stuff
  188. SpatialPartitionID spatial_partition_id;
  189. // rooms & portals
  190. OcclusionHandle occlusion_handle; // handle of instance in occlusion system (or 0)
  191. VisualServer::InstancePortalMode portal_mode;
  192. Scenario *scenario;
  193. SelfList<Instance> scenario_item;
  194. //aabb stuff
  195. bool update_aabb;
  196. bool update_materials;
  197. SelfList<Instance> update_item;
  198. AABB aabb;
  199. AABB transformed_aabb;
  200. AABB *custom_aabb; // <Zylann> would using aabb directly with a bool be better?
  201. float extra_margin;
  202. uint32_t object_id;
  203. float lod_begin;
  204. float lod_end;
  205. float lod_begin_hysteresis;
  206. float lod_end_hysteresis;
  207. RID lod_instance;
  208. uint64_t last_render_pass;
  209. uint64_t last_frame_pass;
  210. uint64_t version; // changes to this, and changes to base increase version
  211. InstanceBaseData *base_data;
  212. virtual void base_removed() {
  213. singleton->instance_set_base(self, RID());
  214. }
  215. virtual void base_changed(bool p_aabb, bool p_materials) {
  216. singleton->_instance_queue_update(this, p_aabb, p_materials);
  217. }
  218. Instance() :
  219. scenario_item(this),
  220. update_item(this) {
  221. spatial_partition_id = 0;
  222. scenario = nullptr;
  223. update_aabb = false;
  224. update_materials = false;
  225. extra_margin = 0;
  226. object_id = 0;
  227. visible = true;
  228. occlusion_handle = 0;
  229. portal_mode = VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_STATIC;
  230. lod_begin = 0;
  231. lod_end = 0;
  232. lod_begin_hysteresis = 0;
  233. lod_end_hysteresis = 0;
  234. last_render_pass = 0;
  235. last_frame_pass = 0;
  236. version = 1;
  237. base_data = nullptr;
  238. custom_aabb = nullptr;
  239. }
  240. ~Instance() {
  241. if (base_data) {
  242. memdelete(base_data);
  243. }
  244. if (custom_aabb) {
  245. memdelete(custom_aabb);
  246. }
  247. }
  248. };
  249. SelfList<Instance>::List _instance_update_list;
  250. void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_materials = false);
  251. struct InstanceGeometryData : public InstanceBaseData {
  252. List<Instance *> lighting;
  253. bool lighting_dirty;
  254. bool can_cast_shadows;
  255. bool material_is_animated;
  256. List<Instance *> reflection_probes;
  257. bool reflection_dirty;
  258. List<Instance *> gi_probes;
  259. bool gi_probes_dirty;
  260. List<Instance *> lightmap_captures;
  261. InstanceGeometryData() {
  262. lighting_dirty = true;
  263. reflection_dirty = true;
  264. can_cast_shadows = true;
  265. material_is_animated = true;
  266. gi_probes_dirty = true;
  267. }
  268. };
  269. struct InstanceReflectionProbeData : public InstanceBaseData {
  270. Instance *owner;
  271. struct PairInfo {
  272. List<Instance *>::Element *L; //reflection iterator in geometry
  273. Instance *geometry;
  274. };
  275. List<PairInfo> geometries;
  276. RID instance;
  277. bool reflection_dirty;
  278. SelfList<InstanceReflectionProbeData> update_list;
  279. int render_step;
  280. int32_t previous_room_id_hint;
  281. InstanceReflectionProbeData() :
  282. update_list(this) {
  283. reflection_dirty = true;
  284. render_step = -1;
  285. previous_room_id_hint = -1;
  286. }
  287. };
  288. SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
  289. struct InstanceLightData : public InstanceBaseData {
  290. struct PairInfo {
  291. List<Instance *>::Element *L; //light iterator in geometry
  292. Instance *geometry;
  293. };
  294. RID instance;
  295. uint64_t last_version;
  296. List<Instance *>::Element *D; // directional light in scenario
  297. bool shadow_dirty;
  298. List<PairInfo> geometries;
  299. Instance *baked_light;
  300. int32_t previous_room_id_hint;
  301. InstanceLightData() {
  302. shadow_dirty = true;
  303. D = nullptr;
  304. last_version = 0;
  305. baked_light = nullptr;
  306. previous_room_id_hint = -1;
  307. }
  308. };
  309. struct InstanceGIProbeData : public InstanceBaseData {
  310. Instance *owner;
  311. struct PairInfo {
  312. List<Instance *>::Element *L; //gi probe iterator in geometry
  313. Instance *geometry;
  314. };
  315. List<PairInfo> geometries;
  316. Set<Instance *> lights;
  317. struct LightCache {
  318. VS::LightType type;
  319. Transform transform;
  320. Color color;
  321. float energy;
  322. float radius;
  323. float attenuation;
  324. float spot_angle;
  325. float spot_attenuation;
  326. bool visible;
  327. bool operator==(const LightCache &p_cache) {
  328. return (type == p_cache.type &&
  329. transform == p_cache.transform &&
  330. color == p_cache.color &&
  331. energy == p_cache.energy &&
  332. radius == p_cache.radius &&
  333. attenuation == p_cache.attenuation &&
  334. spot_angle == p_cache.spot_angle &&
  335. spot_attenuation == p_cache.spot_attenuation &&
  336. visible == p_cache.visible);
  337. }
  338. bool operator!=(const LightCache &p_cache) {
  339. return !operator==(p_cache);
  340. }
  341. LightCache() {
  342. type = VS::LIGHT_DIRECTIONAL;
  343. energy = 1.0;
  344. radius = 1.0;
  345. attenuation = 1.0;
  346. spot_angle = 1.0;
  347. spot_attenuation = 1.0;
  348. visible = true;
  349. }
  350. };
  351. struct LocalData {
  352. uint16_t pos[3];
  353. uint16_t energy[3]; //using 0..1024 for float range 0..1. integer is needed for deterministic add/remove of lights
  354. };
  355. struct CompBlockS3TC {
  356. uint32_t offset; //offset in mipmap
  357. uint32_t source_count; //sources
  358. uint32_t sources[16]; //id for each source
  359. uint8_t alpha[8]; //alpha block is pre-computed
  360. };
  361. struct Dynamic {
  362. Map<RID, LightCache> light_cache;
  363. Map<RID, LightCache> light_cache_changes;
  364. PoolVector<int> light_data;
  365. PoolVector<LocalData> local_data;
  366. Vector<Vector<uint32_t>> level_cell_lists;
  367. RID probe_data;
  368. bool enabled;
  369. int bake_dynamic_range;
  370. RasterizerStorage::GIProbeCompression compression;
  371. Vector<PoolVector<uint8_t>> mipmaps_3d;
  372. Vector<PoolVector<CompBlockS3TC>> mipmaps_s3tc; //for s3tc
  373. int updating_stage;
  374. float propagate;
  375. int grid_size[3];
  376. Transform light_to_cell_xform;
  377. } dynamic;
  378. RID probe_instance;
  379. bool invalid;
  380. uint32_t base_version;
  381. SelfList<InstanceGIProbeData> update_element;
  382. InstanceGIProbeData() :
  383. update_element(this) {
  384. invalid = true;
  385. base_version = 0;
  386. dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  387. }
  388. };
  389. SelfList<InstanceGIProbeData>::List gi_probe_update_list;
  390. struct InstanceLightmapCaptureData : public InstanceBaseData {
  391. struct PairInfo {
  392. List<Instance *>::Element *L; //iterator in geometry
  393. Instance *geometry;
  394. };
  395. List<PairInfo> geometries;
  396. Set<Instance *> users;
  397. InstanceLightmapCaptureData() {
  398. }
  399. };
  400. int instance_cull_count;
  401. Instance *instance_cull_result[MAX_INSTANCE_CULL];
  402. Instance *instance_shadow_cull_result[MAX_INSTANCE_CULL]; //used for generating shadowmaps
  403. Instance *light_cull_result[MAX_LIGHTS_CULLED];
  404. RID light_instance_cull_result[MAX_LIGHTS_CULLED];
  405. int light_cull_count;
  406. int directional_light_count;
  407. RID reflection_probe_instance_cull_result[MAX_REFLECTION_PROBES_CULLED];
  408. int reflection_probe_cull_count;
  409. RID_Owner<Instance> instance_owner;
  410. virtual RID instance_create();
  411. virtual void instance_set_base(RID p_instance, RID p_base);
  412. virtual void instance_set_scenario(RID p_instance, RID p_scenario);
  413. virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
  414. virtual void instance_set_transform(RID p_instance, const Transform &p_transform);
  415. virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_id);
  416. virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
  417. virtual void instance_set_surface_material(RID p_instance, int p_surface, RID p_material);
  418. virtual void instance_set_visible(RID p_instance, bool p_visible);
  419. virtual void instance_set_use_lightmap(RID p_instance, RID p_lightmap_instance, RID p_lightmap, int p_lightmap_slice, const Rect2 &p_lightmap_uv_rect);
  420. virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
  421. virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
  422. virtual void instance_set_exterior(RID p_instance, bool p_enabled);
  423. virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
  424. // Portals
  425. virtual void instance_set_portal_mode(RID p_instance, VisualServer::InstancePortalMode p_mode);
  426. bool _instance_get_transformed_aabb(RID p_instance, AABB &r_aabb);
  427. bool _instance_get_transformed_aabb_for_occlusion(VSInstance *p_instance, AABB &r_aabb) const {
  428. r_aabb = ((Instance *)p_instance)->transformed_aabb;
  429. return ((Instance *)p_instance)->portal_mode != VisualServer::INSTANCE_PORTAL_MODE_GLOBAL;
  430. }
  431. void *_instance_get_from_rid(RID p_instance);
  432. bool _instance_cull_check(VSInstance *p_instance, uint32_t p_cull_mask) const {
  433. uint32_t pairable_type = 1 << ((Instance *)p_instance)->base_type;
  434. return pairable_type & p_cull_mask;
  435. }
  436. ObjectID _instance_get_object_ID(VSInstance *p_instance) const {
  437. if (p_instance) {
  438. return ((Instance *)p_instance)->object_id;
  439. }
  440. return 0;
  441. }
  442. private:
  443. void _instance_create_occlusion_rep(Instance *p_instance);
  444. void _instance_destroy_occlusion_rep(Instance *p_instance);
  445. public:
  446. struct Ghost : RID_Data {
  447. // all interations with actual ghosts are indirect, as the ghost is part of the scenario
  448. Scenario *scenario = nullptr;
  449. uint32_t object_id = 0;
  450. RGhostHandle rghost_handle = 0; // handle in occlusion system (or 0)
  451. AABB aabb;
  452. virtual ~Ghost() {
  453. if (scenario) {
  454. if (rghost_handle) {
  455. scenario->_portal_renderer.rghost_destroy(rghost_handle);
  456. rghost_handle = 0;
  457. }
  458. scenario = nullptr;
  459. }
  460. }
  461. };
  462. RID_Owner<Ghost> ghost_owner;
  463. virtual RID ghost_create();
  464. virtual void ghost_set_scenario(RID p_ghost, RID p_scenario, ObjectID p_id, const AABB &p_aabb);
  465. virtual void ghost_update(RID p_ghost, const AABB &p_aabb);
  466. private:
  467. void _ghost_create_occlusion_rep(Ghost *p_ghost);
  468. void _ghost_destroy_occlusion_rep(Ghost *p_ghost);
  469. public:
  470. struct Portal : RID_Data {
  471. // all interations with actual portals are indirect, as the portal is part of the scenario
  472. uint32_t scenario_portal_id = 0;
  473. Scenario *scenario = nullptr;
  474. virtual ~Portal() {
  475. if (scenario) {
  476. scenario->_portal_renderer.portal_destroy(scenario_portal_id);
  477. scenario = nullptr;
  478. scenario_portal_id = 0;
  479. }
  480. }
  481. };
  482. RID_Owner<Portal> portal_owner;
  483. virtual RID portal_create();
  484. virtual void portal_set_scenario(RID p_portal, RID p_scenario);
  485. virtual void portal_set_geometry(RID p_portal, const Vector<Vector3> &p_points, real_t p_margin);
  486. virtual void portal_link(RID p_portal, RID p_room_from, RID p_room_to, bool p_two_way);
  487. virtual void portal_set_active(RID p_portal, bool p_active);
  488. // RoomGroups
  489. struct RoomGroup : RID_Data {
  490. // all interations with actual roomgroups are indirect, as the roomgroup is part of the scenario
  491. uint32_t scenario_roomgroup_id = 0;
  492. Scenario *scenario = nullptr;
  493. virtual ~RoomGroup() {
  494. if (scenario) {
  495. scenario->_portal_renderer.roomgroup_destroy(scenario_roomgroup_id);
  496. scenario = nullptr;
  497. scenario_roomgroup_id = 0;
  498. }
  499. }
  500. };
  501. RID_Owner<RoomGroup> roomgroup_owner;
  502. virtual RID roomgroup_create();
  503. virtual void roomgroup_prepare(RID p_roomgroup, ObjectID p_roomgroup_object_id);
  504. virtual void roomgroup_set_scenario(RID p_roomgroup, RID p_scenario);
  505. virtual void roomgroup_add_room(RID p_roomgroup, RID p_room);
  506. // Occluders
  507. struct Occluder : RID_Data {
  508. uint32_t scenario_occluder_id = 0;
  509. Scenario *scenario = nullptr;
  510. virtual ~Occluder() {
  511. if (scenario) {
  512. scenario->_portal_renderer.occluder_destroy(scenario_occluder_id);
  513. scenario = nullptr;
  514. scenario_occluder_id = 0;
  515. }
  516. }
  517. };
  518. RID_Owner<Occluder> occluder_owner;
  519. virtual RID occluder_create();
  520. virtual void occluder_set_scenario(RID p_occluder, RID p_scenario, VisualServer::OccluderType p_type);
  521. virtual void occluder_spheres_update(RID p_occluder, const Vector<Plane> &p_spheres);
  522. virtual void occluder_set_transform(RID p_occluder, const Transform &p_xform);
  523. virtual void occluder_set_active(RID p_occluder, bool p_active);
  524. virtual void set_use_occlusion_culling(bool p_enable);
  525. // Rooms
  526. struct Room : RID_Data {
  527. // all interations with actual rooms are indirect, as the room is part of the scenario
  528. uint32_t scenario_room_id = 0;
  529. Scenario *scenario = nullptr;
  530. virtual ~Room() {
  531. if (scenario) {
  532. scenario->_portal_renderer.room_destroy(scenario_room_id);
  533. scenario = nullptr;
  534. scenario_room_id = 0;
  535. }
  536. }
  537. };
  538. RID_Owner<Room> room_owner;
  539. virtual RID room_create();
  540. virtual void room_set_scenario(RID p_room, RID p_scenario);
  541. virtual void room_add_instance(RID p_room, RID p_instance, const AABB &p_aabb, const Vector<Vector3> &p_object_pts);
  542. virtual void room_add_ghost(RID p_room, ObjectID p_object_id, const AABB &p_aabb);
  543. virtual void room_set_bound(RID p_room, ObjectID p_room_object_id, const Vector<Plane> &p_convex, const AABB &p_aabb, const Vector<Vector3> &p_verts);
  544. virtual void room_prepare(RID p_room, int32_t p_priority);
  545. virtual void rooms_and_portals_clear(RID p_scenario);
  546. virtual void rooms_unload(RID p_scenario, String p_reason);
  547. virtual void rooms_finalize(RID p_scenario, bool p_generate_pvs, bool p_cull_using_pvs, bool p_use_secondary_pvs, bool p_use_signals, String p_pvs_filename, bool p_use_simple_pvs, bool p_log_pvs_generation);
  548. virtual void rooms_override_camera(RID p_scenario, bool p_override, const Vector3 &p_point, const Vector<Plane> *p_convex);
  549. virtual void rooms_set_active(RID p_scenario, bool p_active);
  550. virtual void rooms_set_params(RID p_scenario, int p_portal_depth_limit, real_t p_roaming_expansion_margin);
  551. virtual void rooms_set_debug_feature(RID p_scenario, VisualServer::RoomsDebugFeature p_feature, bool p_active);
  552. virtual void rooms_update_gameplay_monitor(RID p_scenario, const Vector<Vector3> &p_camera_positions);
  553. // don't use this in a game
  554. virtual bool rooms_is_loaded(RID p_scenario) const;
  555. virtual void callbacks_register(VisualServerCallbacks *p_callbacks);
  556. VisualServerCallbacks *get_callbacks() const { return _visual_server_callbacks; }
  557. // don't use these in a game!
  558. virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
  559. virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
  560. virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
  561. // internal (uses portals when available)
  562. int _cull_convex_from_point(Scenario *p_scenario, const Vector3 &p_point, const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, int32_t &r_previous_room_id_hint, uint32_t p_mask = 0xFFFFFFFF);
  563. void _rooms_instance_update(Instance *p_instance, const AABB &p_aabb);
  564. virtual void instance_geometry_set_flag(RID p_instance, VS::InstanceFlags p_flags, bool p_enabled);
  565. virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting);
  566. virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
  567. virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
  568. virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
  569. _FORCE_INLINE_ void _update_instance(Instance *p_instance);
  570. _FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
  571. _FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
  572. _FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
  573. _FORCE_INLINE_ bool _light_instance_update_shadow(Instance *p_instance, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_shadow_atlas, Scenario *p_scenario);
  574. void _prepare_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_force_environment, uint32_t p_visible_layers, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int32_t &r_previous_room_id_hint);
  575. void _render_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, const int p_eye, bool p_cam_orthogonal, RID p_force_environment, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass);
  576. void render_empty_scene(RID p_scenario, RID p_shadow_atlas);
  577. void render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  578. void render_camera(Ref<ARVRInterface> &p_interface, ARVRInterface::Eyes p_eye, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  579. void update_dirty_instances();
  580. //probes
  581. struct GIProbeDataHeader {
  582. uint32_t version;
  583. uint32_t cell_subdiv;
  584. uint32_t width;
  585. uint32_t height;
  586. uint32_t depth;
  587. uint32_t cell_count;
  588. uint32_t leaf_cell_count;
  589. };
  590. struct GIProbeDataCell {
  591. uint32_t children[8];
  592. uint32_t albedo;
  593. uint32_t emission;
  594. uint32_t normal;
  595. uint32_t level_alpha;
  596. };
  597. enum {
  598. GI_UPDATE_STAGE_CHECK,
  599. GI_UPDATE_STAGE_LIGHTING,
  600. GI_UPDATE_STAGE_UPLOADING,
  601. };
  602. void _gi_probe_bake_thread();
  603. static void _gi_probe_bake_threads(void *);
  604. bool probe_bake_thread_exit;
  605. Thread probe_bake_thread;
  606. Semaphore probe_bake_sem;
  607. Mutex probe_bake_mutex;
  608. List<Instance *> probe_bake_list;
  609. bool _render_reflection_probe_step(Instance *p_instance, int p_step);
  610. void _gi_probe_fill_local_data(int p_idx, int p_level, int p_x, int p_y, int p_z, const GIProbeDataCell *p_cell, const GIProbeDataHeader *p_header, InstanceGIProbeData::LocalData *p_local_data, Vector<uint32_t> *prev_cell);
  611. _FORCE_INLINE_ uint32_t _gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv);
  612. void _bake_gi_downscale_light(int p_idx, int p_level, const GIProbeDataCell *p_cells, const GIProbeDataHeader *p_header, InstanceGIProbeData::LocalData *p_local_data, float p_propagate);
  613. void _bake_gi_probe_light(const GIProbeDataHeader *header, const GIProbeDataCell *cells, InstanceGIProbeData::LocalData *local_data, const uint32_t *leaves, int p_leaf_count, const InstanceGIProbeData::LightCache &light_cache, int p_sign);
  614. void _bake_gi_probe(Instance *p_gi_probe);
  615. bool _check_gi_probe(Instance *p_gi_probe);
  616. void _setup_gi_probe(Instance *p_instance);
  617. void render_probes();
  618. bool free(RID p_rid);
  619. private:
  620. bool _use_bvh;
  621. VisualServerCallbacks *_visual_server_callbacks;
  622. public:
  623. VisualServerScene();
  624. virtual ~VisualServerScene();
  625. };
  626. #endif // VISUALSERVERSCENE_H