visual_server_scene.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /**************************************************************************/
  2. /* visual_server_scene.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 VISUAL_SERVER_SCENE_H
  31. #define VISUAL_SERVER_SCENE_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. /* EVENT QUEUING */
  54. void tick();
  55. void pre_draw(bool p_will_draw);
  56. /* CAMERA API */
  57. struct Scenario;
  58. struct Camera : public RID_Data {
  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. RID env;
  71. // transform_prev is only used when using fixed timestep interpolation
  72. Transform transform;
  73. Transform transform_prev;
  74. bool interpolated : 1;
  75. bool on_interpolate_transform_list : 1;
  76. bool vaspect : 1;
  77. TransformInterpolator::Method interpolation_method : 3;
  78. int32_t previous_room_id_hint;
  79. Transform get_transform_interpolated() const;
  80. Camera() {
  81. visible_layers = 0xFFFFFFFF;
  82. fov = 70;
  83. type = PERSPECTIVE;
  84. znear = 0.05;
  85. zfar = 100;
  86. size = 1.0;
  87. offset = Vector2();
  88. vaspect = false;
  89. previous_room_id_hint = -1;
  90. interpolated = true;
  91. on_interpolate_transform_list = false;
  92. interpolation_method = TransformInterpolator::INTERP_LERP;
  93. }
  94. };
  95. mutable RID_Owner<Camera> camera_owner;
  96. virtual RID camera_create();
  97. virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
  98. virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
  99. virtual void camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
  100. virtual void camera_set_transform(RID p_camera, const Transform &p_transform);
  101. virtual void camera_set_interpolated(RID p_camera, bool p_interpolated);
  102. virtual void camera_reset_physics_interpolation(RID p_camera);
  103. virtual void camera_set_cull_mask(RID p_camera, uint32_t p_layers);
  104. virtual void camera_set_environment(RID p_camera, RID p_env);
  105. virtual void camera_set_use_vertical_aspect(RID p_camera, bool p_enable);
  106. /* SCENARIO API */
  107. struct Instance;
  108. // common interface for all spatial partitioning schemes
  109. // this is a bit excessive boilerplatewise but can be removed if we decide to stick with one method
  110. // note this is actually the BVH id +1, so that visual server can test against zero
  111. // for validity to maintain compatibility with octree (where 0 indicates invalid)
  112. typedef uint32_t SpatialPartitionID;
  113. class SpatialPartitioningScene {
  114. public:
  115. 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;
  116. virtual void erase(SpatialPartitionID p_handle) = 0;
  117. virtual void move(SpatialPartitionID p_handle, const AABB &p_aabb) = 0;
  118. virtual void activate(SpatialPartitionID p_handle, const AABB &p_aabb) {}
  119. virtual void deactivate(SpatialPartitionID p_handle) {}
  120. virtual void force_collision_check(SpatialPartitionID p_handle) {}
  121. virtual void update() {}
  122. virtual void update_collisions() {}
  123. virtual void set_pairable(Instance *p_instance, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) = 0;
  124. virtual int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF) = 0;
  125. 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;
  126. 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;
  127. typedef void *(*PairCallback)(void *, uint32_t, Instance *, int, uint32_t, Instance *, int);
  128. typedef void (*UnpairCallback)(void *, uint32_t, Instance *, int, uint32_t, Instance *, int, void *);
  129. virtual void set_pair_callback(PairCallback p_callback, void *p_userdata) = 0;
  130. virtual void set_unpair_callback(UnpairCallback p_callback, void *p_userdata) = 0;
  131. // bvh specific
  132. virtual void params_set_node_expansion(real_t p_value) {}
  133. virtual void params_set_pairing_expansion(real_t p_value) {}
  134. // octree specific
  135. virtual void set_balance(float p_balance) {}
  136. virtual ~SpatialPartitioningScene() {}
  137. };
  138. class SpatialPartitioningScene_Octree : public SpatialPartitioningScene {
  139. Octree_CL<Instance, true> _octree;
  140. public:
  141. 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);
  142. void erase(SpatialPartitionID p_handle);
  143. void move(SpatialPartitionID p_handle, const AABB &p_aabb);
  144. void set_pairable(Instance *p_instance, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
  145. int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  146. 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);
  147. 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);
  148. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  149. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  150. void set_balance(float p_balance);
  151. };
  152. class SpatialPartitioningScene_BVH : public SpatialPartitioningScene {
  153. template <class T>
  154. class UserPairTestFunction {
  155. public:
  156. static bool user_pair_check(const T *p_a, const T *p_b) {
  157. // return false if no collision, decided by masks etc
  158. return true;
  159. }
  160. };
  161. template <class T>
  162. class UserCullTestFunction {
  163. // write this logic once for use in all routines
  164. // double check this as a possible source of bugs in future.
  165. static bool _cull_pairing_mask_test_hit(uint32_t p_maskA, uint32_t p_typeA, uint32_t p_maskB, uint32_t p_typeB) {
  166. // double check this as a possible source of bugs in future.
  167. bool A_match_B = p_maskA & p_typeB;
  168. if (!A_match_B) {
  169. bool B_match_A = p_maskB & p_typeA;
  170. if (!B_match_A) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. public:
  177. static bool user_cull_check(const T *p_a, const T *p_b) {
  178. DEV_ASSERT(p_a);
  179. DEV_ASSERT(p_b);
  180. uint32_t a_mask = p_a->bvh_pairable_mask;
  181. uint32_t a_type = p_a->bvh_pairable_type;
  182. uint32_t b_mask = p_b->bvh_pairable_mask;
  183. uint32_t b_type = p_b->bvh_pairable_type;
  184. if (!_cull_pairing_mask_test_hit(a_mask, a_type, b_mask, b_type)) {
  185. return false;
  186. }
  187. return true;
  188. }
  189. };
  190. private:
  191. // Note that SpatialPartitionIDs are +1 based when stored in visual server, to enable 0 to indicate invalid ID.
  192. BVH_Manager<Instance, 2, true, 256, UserPairTestFunction<Instance>, UserCullTestFunction<Instance>> _bvh;
  193. Instance *_dummy_cull_object;
  194. public:
  195. SpatialPartitioningScene_BVH();
  196. ~SpatialPartitioningScene_BVH();
  197. 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);
  198. void erase(SpatialPartitionID p_handle);
  199. void move(SpatialPartitionID p_handle, const AABB &p_aabb);
  200. void activate(SpatialPartitionID p_handle, const AABB &p_aabb);
  201. void deactivate(SpatialPartitionID p_handle);
  202. void force_collision_check(SpatialPartitionID p_handle);
  203. void update();
  204. void update_collisions();
  205. void set_pairable(Instance *p_instance, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask);
  206. int cull_convex(const Vector<Plane> &p_convex, Instance **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  207. 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);
  208. 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);
  209. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  210. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  211. void params_set_node_expansion(real_t p_value) { _bvh.params_set_node_expansion(p_value); }
  212. void params_set_pairing_expansion(real_t p_value) { _bvh.params_set_pairing_expansion(p_value); }
  213. };
  214. struct Scenario : RID_Data {
  215. VS::ScenarioDebugMode debug;
  216. RID self;
  217. SpatialPartitioningScene *sps;
  218. PortalRenderer _portal_renderer;
  219. List<Instance *> directional_lights;
  220. RID environment;
  221. RID fallback_environment;
  222. RID reflection_probe_shadow_atlas;
  223. RID reflection_atlas;
  224. SelfList<Instance>::List instances;
  225. Scenario();
  226. ~Scenario() { memdelete(sps); }
  227. };
  228. mutable RID_Owner<Scenario> scenario_owner;
  229. static void *_instance_pair(void *p_self, SpatialPartitionID, Instance *p_A, int, SpatialPartitionID, Instance *p_B, int);
  230. static void _instance_unpair(void *p_self, SpatialPartitionID, Instance *p_A, int, SpatialPartitionID, Instance *p_B, int, void *);
  231. virtual RID scenario_create();
  232. virtual void scenario_set_debug(RID p_scenario, VS::ScenarioDebugMode p_debug_mode);
  233. virtual void scenario_set_environment(RID p_scenario, RID p_environment);
  234. virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
  235. virtual void scenario_set_reflection_atlas_size(RID p_scenario, int p_size, int p_subdiv);
  236. /* INSTANCING API */
  237. struct InstanceBaseData {
  238. virtual ~InstanceBaseData() {}
  239. };
  240. struct Instance : RasterizerScene::InstanceBase {
  241. RID self;
  242. //scenario stuff
  243. SpatialPartitionID spatial_partition_id;
  244. // rooms & portals
  245. OcclusionHandle occlusion_handle; // handle of instance in occlusion system (or 0)
  246. VisualServer::InstancePortalMode portal_mode;
  247. Scenario *scenario;
  248. SelfList<Instance> scenario_item;
  249. //aabb stuff
  250. bool update_aabb;
  251. bool update_materials;
  252. SelfList<Instance> update_item;
  253. AABB aabb;
  254. AABB transformed_aabb;
  255. AABB *custom_aabb; // <Zylann> would using aabb directly with a bool be better?
  256. float sorting_offset;
  257. bool use_aabb_center;
  258. float extra_margin;
  259. uint32_t object_id;
  260. float lod_begin;
  261. float lod_end;
  262. float lod_begin_hysteresis;
  263. float lod_end_hysteresis;
  264. RID lod_instance;
  265. // These are used for the user cull testing function
  266. // in the BVH, this is precached rather than recalculated each time.
  267. uint32_t bvh_pairable_mask;
  268. uint32_t bvh_pairable_type;
  269. uint64_t last_render_pass;
  270. uint64_t last_frame_pass;
  271. uint64_t version; // changes to this, and changes to base increase version
  272. InstanceBaseData *base_data;
  273. virtual void base_removed() {
  274. singleton->instance_set_base(self, RID());
  275. }
  276. virtual void base_changed(bool p_aabb, bool p_materials) {
  277. singleton->_instance_queue_update(this, p_aabb, p_materials);
  278. }
  279. Instance() :
  280. scenario_item(this),
  281. update_item(this) {
  282. spatial_partition_id = 0;
  283. scenario = nullptr;
  284. update_aabb = false;
  285. update_materials = false;
  286. extra_margin = 0;
  287. object_id = 0;
  288. visible = true;
  289. occlusion_handle = 0;
  290. portal_mode = VisualServer::InstancePortalMode::INSTANCE_PORTAL_MODE_STATIC;
  291. lod_begin = 0;
  292. lod_end = 0;
  293. lod_begin_hysteresis = 0;
  294. lod_end_hysteresis = 0;
  295. bvh_pairable_mask = 0;
  296. bvh_pairable_type = 0;
  297. last_render_pass = 0;
  298. last_frame_pass = 0;
  299. version = 1;
  300. base_data = nullptr;
  301. custom_aabb = nullptr;
  302. sorting_offset = 0.0f;
  303. use_aabb_center = false;
  304. }
  305. ~Instance() {
  306. if (base_data) {
  307. memdelete(base_data);
  308. }
  309. if (custom_aabb) {
  310. memdelete(custom_aabb);
  311. }
  312. }
  313. };
  314. SelfList<Instance>::List _instance_update_list;
  315. // fixed timestep interpolation
  316. virtual void set_physics_interpolation_enabled(bool p_enabled);
  317. struct InterpolationData {
  318. void notify_free_camera(RID p_rid, Camera &r_camera);
  319. void notify_free_instance(RID p_rid, Instance &r_instance);
  320. LocalVector<RID> instance_interpolate_update_list;
  321. LocalVector<RID> instance_transform_update_lists[2];
  322. LocalVector<RID> *instance_transform_update_list_curr = &instance_transform_update_lists[0];
  323. LocalVector<RID> *instance_transform_update_list_prev = &instance_transform_update_lists[1];
  324. LocalVector<RID> instance_teleport_list;
  325. LocalVector<RID> camera_transform_update_lists[2];
  326. LocalVector<RID> *camera_transform_update_list_curr = &camera_transform_update_lists[0];
  327. LocalVector<RID> *camera_transform_update_list_prev = &camera_transform_update_lists[1];
  328. LocalVector<RID> camera_teleport_list;
  329. bool interpolation_enabled = false;
  330. } _interpolation_data;
  331. void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_materials = false);
  332. struct InstanceGeometryData : public InstanceBaseData {
  333. List<Instance *> lighting;
  334. bool lighting_dirty;
  335. bool can_cast_shadows;
  336. bool material_is_animated;
  337. List<Instance *> reflection_probes;
  338. bool reflection_dirty;
  339. List<Instance *> gi_probes;
  340. bool gi_probes_dirty;
  341. List<Instance *> lightmap_captures;
  342. InstanceGeometryData() {
  343. lighting_dirty = true;
  344. reflection_dirty = true;
  345. can_cast_shadows = true;
  346. material_is_animated = true;
  347. gi_probes_dirty = true;
  348. }
  349. };
  350. struct InstanceReflectionProbeData : public InstanceBaseData {
  351. Instance *owner;
  352. struct PairInfo {
  353. List<Instance *>::Element *L; //reflection iterator in geometry
  354. Instance *geometry;
  355. };
  356. List<PairInfo> geometries;
  357. RID instance;
  358. bool reflection_dirty;
  359. SelfList<InstanceReflectionProbeData> update_list;
  360. int render_step;
  361. int32_t previous_room_id_hint;
  362. InstanceReflectionProbeData() :
  363. update_list(this) {
  364. reflection_dirty = true;
  365. render_step = -1;
  366. previous_room_id_hint = -1;
  367. }
  368. };
  369. SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
  370. struct InstanceLightData : public InstanceBaseData {
  371. struct PairInfo {
  372. List<Instance *>::Element *L; //light iterator in geometry
  373. Instance *geometry;
  374. };
  375. RID instance;
  376. uint64_t last_version;
  377. List<Instance *>::Element *D; // directional light in scenario
  378. bool shadow_dirty;
  379. List<PairInfo> geometries;
  380. Instance *baked_light;
  381. int32_t previous_room_id_hint;
  382. InstanceLightData() {
  383. shadow_dirty = true;
  384. D = nullptr;
  385. last_version = 0;
  386. baked_light = nullptr;
  387. previous_room_id_hint = -1;
  388. }
  389. };
  390. struct InstanceGIProbeData : public InstanceBaseData {
  391. Instance *owner;
  392. struct PairInfo {
  393. List<Instance *>::Element *L; //gi probe iterator in geometry
  394. Instance *geometry;
  395. };
  396. List<PairInfo> geometries;
  397. Set<Instance *> lights;
  398. struct LightCache {
  399. VS::LightType type;
  400. Transform transform;
  401. Color color;
  402. float energy;
  403. float radius;
  404. float attenuation;
  405. float spot_angle;
  406. float spot_attenuation;
  407. bool visible;
  408. bool operator==(const LightCache &p_cache) {
  409. return (type == p_cache.type &&
  410. transform == p_cache.transform &&
  411. color == p_cache.color &&
  412. energy == p_cache.energy &&
  413. radius == p_cache.radius &&
  414. attenuation == p_cache.attenuation &&
  415. spot_angle == p_cache.spot_angle &&
  416. spot_attenuation == p_cache.spot_attenuation &&
  417. visible == p_cache.visible);
  418. }
  419. bool operator!=(const LightCache &p_cache) {
  420. return !operator==(p_cache);
  421. }
  422. LightCache() {
  423. type = VS::LIGHT_DIRECTIONAL;
  424. energy = 1.0;
  425. radius = 1.0;
  426. attenuation = 1.0;
  427. spot_angle = 1.0;
  428. spot_attenuation = 1.0;
  429. visible = true;
  430. }
  431. };
  432. struct LocalData {
  433. uint16_t pos[3];
  434. uint16_t energy[3]; //using 0..1024 for float range 0..1. integer is needed for deterministic add/remove of lights
  435. };
  436. struct CompBlockS3TC {
  437. uint32_t offset; //offset in mipmap
  438. uint32_t source_count; //sources
  439. uint32_t sources[16]; //id for each source
  440. uint8_t alpha[8]; //alpha block is pre-computed
  441. };
  442. struct Dynamic {
  443. Map<RID, LightCache> light_cache;
  444. Map<RID, LightCache> light_cache_changes;
  445. PoolVector<int> light_data;
  446. PoolVector<LocalData> local_data;
  447. Vector<Vector<uint32_t>> level_cell_lists;
  448. RID probe_data;
  449. bool enabled;
  450. int bake_dynamic_range;
  451. RasterizerStorage::GIProbeCompression compression;
  452. Vector<PoolVector<uint8_t>> mipmaps_3d;
  453. Vector<PoolVector<CompBlockS3TC>> mipmaps_s3tc; //for s3tc
  454. int updating_stage;
  455. float propagate;
  456. int grid_size[3];
  457. Transform light_to_cell_xform;
  458. } dynamic;
  459. RID probe_instance;
  460. bool invalid;
  461. uint32_t base_version;
  462. SelfList<InstanceGIProbeData> update_element;
  463. InstanceGIProbeData() :
  464. update_element(this) {
  465. invalid = true;
  466. base_version = 0;
  467. dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  468. }
  469. };
  470. SelfList<InstanceGIProbeData>::List gi_probe_update_list;
  471. struct InstanceLightmapCaptureData : public InstanceBaseData {
  472. struct PairInfo {
  473. List<Instance *>::Element *L; //iterator in geometry
  474. Instance *geometry;
  475. };
  476. List<PairInfo> geometries;
  477. Set<Instance *> users;
  478. InstanceLightmapCaptureData() {
  479. }
  480. };
  481. int instance_cull_count;
  482. Instance *instance_cull_result[MAX_INSTANCE_CULL];
  483. Instance *instance_shadow_cull_result[MAX_INSTANCE_CULL]; //used for generating shadowmaps
  484. Instance *light_cull_result[MAX_LIGHTS_CULLED];
  485. RID light_instance_cull_result[MAX_LIGHTS_CULLED];
  486. int light_cull_count;
  487. int directional_light_count;
  488. RID reflection_probe_instance_cull_result[MAX_REFLECTION_PROBES_CULLED];
  489. int reflection_probe_cull_count;
  490. RID_Owner<Instance> instance_owner;
  491. virtual RID instance_create();
  492. virtual void instance_set_base(RID p_instance, RID p_base);
  493. virtual void instance_set_scenario(RID p_instance, RID p_scenario);
  494. virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
  495. virtual void instance_set_pivot_data(RID p_instance, float p_sorting_offset, bool p_use_aabb_center);
  496. virtual void instance_set_transform(RID p_instance, const Transform &p_transform);
  497. virtual void instance_set_interpolated(RID p_instance, bool p_interpolated);
  498. virtual void instance_reset_physics_interpolation(RID p_instance);
  499. virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_id);
  500. virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
  501. virtual void instance_set_surface_material(RID p_instance, int p_surface, RID p_material);
  502. virtual void instance_set_visible(RID p_instance, bool p_visible);
  503. 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);
  504. virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
  505. virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
  506. virtual void instance_set_exterior(RID p_instance, bool p_enabled);
  507. virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
  508. // Portals
  509. virtual void instance_set_portal_mode(RID p_instance, VisualServer::InstancePortalMode p_mode);
  510. bool _instance_get_transformed_aabb(RID p_instance, AABB &r_aabb);
  511. bool _instance_get_transformed_aabb_for_occlusion(VSInstance *p_instance, AABB &r_aabb) const {
  512. r_aabb = ((Instance *)p_instance)->transformed_aabb;
  513. return ((Instance *)p_instance)->portal_mode != VisualServer::INSTANCE_PORTAL_MODE_GLOBAL;
  514. }
  515. void *_instance_get_from_rid(RID p_instance);
  516. bool _instance_cull_check(VSInstance *p_instance, uint32_t p_cull_mask) const {
  517. uint32_t pairable_type = 1 << ((Instance *)p_instance)->base_type;
  518. return pairable_type & p_cull_mask;
  519. }
  520. ObjectID _instance_get_object_ID(VSInstance *p_instance) const {
  521. if (p_instance) {
  522. return ((Instance *)p_instance)->object_id;
  523. }
  524. return 0;
  525. }
  526. private:
  527. void _instance_create_occlusion_rep(Instance *p_instance);
  528. void _instance_destroy_occlusion_rep(Instance *p_instance);
  529. public:
  530. struct Ghost : RID_Data {
  531. // all interactions with actual ghosts are indirect, as the ghost is part of the scenario
  532. Scenario *scenario = nullptr;
  533. uint32_t object_id = 0;
  534. RGhostHandle rghost_handle = 0; // handle in occlusion system (or 0)
  535. AABB aabb;
  536. virtual ~Ghost() {
  537. if (scenario) {
  538. if (rghost_handle) {
  539. scenario->_portal_renderer.rghost_destroy(rghost_handle);
  540. rghost_handle = 0;
  541. }
  542. scenario = nullptr;
  543. }
  544. }
  545. };
  546. RID_Owner<Ghost> ghost_owner;
  547. virtual RID ghost_create();
  548. virtual void ghost_set_scenario(RID p_ghost, RID p_scenario, ObjectID p_id, const AABB &p_aabb);
  549. virtual void ghost_update(RID p_ghost, const AABB &p_aabb);
  550. private:
  551. void _ghost_create_occlusion_rep(Ghost *p_ghost);
  552. void _ghost_destroy_occlusion_rep(Ghost *p_ghost);
  553. public:
  554. /* PORTALS API */
  555. struct Portal : RID_Data {
  556. // all interactions with actual portals are indirect, as the portal is part of the scenario
  557. uint32_t scenario_portal_id = 0;
  558. Scenario *scenario = nullptr;
  559. virtual ~Portal() {
  560. if (scenario) {
  561. scenario->_portal_renderer.portal_destroy(scenario_portal_id);
  562. scenario = nullptr;
  563. scenario_portal_id = 0;
  564. }
  565. }
  566. };
  567. RID_Owner<Portal> portal_owner;
  568. virtual RID portal_create();
  569. virtual void portal_set_scenario(RID p_portal, RID p_scenario);
  570. virtual void portal_set_geometry(RID p_portal, const Vector<Vector3> &p_points, real_t p_margin);
  571. virtual void portal_link(RID p_portal, RID p_room_from, RID p_room_to, bool p_two_way);
  572. virtual void portal_set_active(RID p_portal, bool p_active);
  573. /* ROOMGROUPS API */
  574. struct RoomGroup : RID_Data {
  575. // all interactions with actual roomgroups are indirect, as the roomgroup is part of the scenario
  576. uint32_t scenario_roomgroup_id = 0;
  577. Scenario *scenario = nullptr;
  578. virtual ~RoomGroup() {
  579. if (scenario) {
  580. scenario->_portal_renderer.roomgroup_destroy(scenario_roomgroup_id);
  581. scenario = nullptr;
  582. scenario_roomgroup_id = 0;
  583. }
  584. }
  585. };
  586. RID_Owner<RoomGroup> roomgroup_owner;
  587. virtual RID roomgroup_create();
  588. virtual void roomgroup_prepare(RID p_roomgroup, ObjectID p_roomgroup_object_id);
  589. virtual void roomgroup_set_scenario(RID p_roomgroup, RID p_scenario);
  590. virtual void roomgroup_add_room(RID p_roomgroup, RID p_room);
  591. /* OCCLUDERS API */
  592. struct OccluderInstance : RID_Data {
  593. uint32_t scenario_occluder_id = 0;
  594. Scenario *scenario = nullptr;
  595. virtual ~OccluderInstance() {
  596. if (scenario) {
  597. scenario->_portal_renderer.occluder_instance_destroy(scenario_occluder_id);
  598. scenario = nullptr;
  599. scenario_occluder_id = 0;
  600. }
  601. }
  602. };
  603. RID_Owner<OccluderInstance> occluder_instance_owner;
  604. struct OccluderResource : RID_Data {
  605. uint32_t occluder_resource_id = 0;
  606. void destroy(PortalResources &r_portal_resources) {
  607. r_portal_resources.occluder_resource_destroy(occluder_resource_id);
  608. occluder_resource_id = 0;
  609. }
  610. virtual ~OccluderResource() {
  611. DEV_ASSERT(occluder_resource_id == 0);
  612. }
  613. };
  614. RID_Owner<OccluderResource> occluder_resource_owner;
  615. virtual RID occluder_instance_create();
  616. virtual void occluder_instance_set_scenario(RID p_occluder_instance, RID p_scenario);
  617. virtual void occluder_instance_link_resource(RID p_occluder_instance, RID p_occluder_resource);
  618. virtual void occluder_instance_set_transform(RID p_occluder_instance, const Transform &p_xform);
  619. virtual void occluder_instance_set_active(RID p_occluder_instance, bool p_active);
  620. virtual RID occluder_resource_create();
  621. virtual void occluder_resource_prepare(RID p_occluder_resource, VisualServer::OccluderType p_type);
  622. virtual void occluder_resource_spheres_update(RID p_occluder_resource, const Vector<Plane> &p_spheres);
  623. virtual void occluder_resource_mesh_update(RID p_occluder_resource, const Geometry::OccluderMeshData &p_mesh_data);
  624. virtual void set_use_occlusion_culling(bool p_enable);
  625. // editor only .. slow
  626. virtual Geometry::MeshData occlusion_debug_get_current_polys(RID p_scenario) const;
  627. const PortalResources &get_portal_resources() const {
  628. return _portal_resources;
  629. }
  630. PortalResources &get_portal_resources() {
  631. return _portal_resources;
  632. }
  633. /* ROOMS API */
  634. struct Room : RID_Data {
  635. // all interactions with actual rooms are indirect, as the room is part of the scenario
  636. uint32_t scenario_room_id = 0;
  637. Scenario *scenario = nullptr;
  638. virtual ~Room() {
  639. if (scenario) {
  640. scenario->_portal_renderer.room_destroy(scenario_room_id);
  641. scenario = nullptr;
  642. scenario_room_id = 0;
  643. }
  644. }
  645. };
  646. RID_Owner<Room> room_owner;
  647. virtual RID room_create();
  648. virtual void room_set_scenario(RID p_room, RID p_scenario);
  649. virtual void room_add_instance(RID p_room, RID p_instance, const AABB &p_aabb, const Vector<Vector3> &p_object_pts);
  650. virtual void room_add_ghost(RID p_room, ObjectID p_object_id, const AABB &p_aabb);
  651. 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);
  652. virtual void room_prepare(RID p_room, int32_t p_priority);
  653. virtual void rooms_and_portals_clear(RID p_scenario);
  654. virtual void rooms_unload(RID p_scenario, String p_reason);
  655. 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);
  656. virtual void rooms_override_camera(RID p_scenario, bool p_override, const Vector3 &p_point, const Vector<Plane> *p_convex);
  657. virtual void rooms_set_active(RID p_scenario, bool p_active);
  658. virtual void rooms_set_params(RID p_scenario, int p_portal_depth_limit, real_t p_roaming_expansion_margin);
  659. virtual void rooms_set_debug_feature(RID p_scenario, VisualServer::RoomsDebugFeature p_feature, bool p_active);
  660. virtual void rooms_update_gameplay_monitor(RID p_scenario, const Vector<Vector3> &p_camera_positions);
  661. // don't use this in a game
  662. virtual bool rooms_is_loaded(RID p_scenario) const;
  663. virtual void callbacks_register(VisualServerCallbacks *p_callbacks);
  664. VisualServerCallbacks *get_callbacks() const {
  665. return _visual_server_callbacks;
  666. }
  667. // don't use these in a game!
  668. virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
  669. virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
  670. virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
  671. // internal (uses portals when available)
  672. int _cull_convex_from_point(Scenario *p_scenario, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, 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);
  673. void _rooms_instance_update(Instance *p_instance, const AABB &p_aabb);
  674. virtual void instance_geometry_set_flag(RID p_instance, VS::InstanceFlags p_flags, bool p_enabled);
  675. virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting);
  676. virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
  677. virtual void instance_geometry_set_material_overlay(RID p_instance, RID p_material);
  678. virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
  679. virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
  680. _FORCE_INLINE_ void _update_instance(Instance *p_instance);
  681. _FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
  682. _FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
  683. _FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
  684. _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);
  685. 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);
  686. 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);
  687. void render_empty_scene(RID p_scenario, RID p_shadow_atlas);
  688. void render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  689. 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);
  690. void update_dirty_instances();
  691. // interpolation
  692. void update_interpolation_tick(bool p_process = true);
  693. void update_interpolation_frame(bool p_process = true);
  694. //probes
  695. struct GIProbeDataHeader {
  696. uint32_t version;
  697. uint32_t cell_subdiv;
  698. uint32_t width;
  699. uint32_t height;
  700. uint32_t depth;
  701. uint32_t cell_count;
  702. uint32_t leaf_cell_count;
  703. };
  704. struct GIProbeDataCell {
  705. uint32_t children[8];
  706. uint32_t albedo;
  707. uint32_t emission;
  708. uint32_t normal;
  709. uint32_t level_alpha;
  710. };
  711. enum {
  712. GI_UPDATE_STAGE_CHECK,
  713. GI_UPDATE_STAGE_LIGHTING,
  714. GI_UPDATE_STAGE_UPLOADING,
  715. };
  716. void _gi_probe_bake_thread();
  717. static void _gi_probe_bake_threads(void *);
  718. bool probe_bake_thread_exit;
  719. Thread probe_bake_thread;
  720. Semaphore probe_bake_sem;
  721. Mutex probe_bake_mutex;
  722. List<Instance *> probe_bake_list;
  723. bool _render_reflection_probe_step(Instance *p_instance, int p_step);
  724. 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);
  725. _FORCE_INLINE_ uint32_t _gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv);
  726. 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);
  727. 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);
  728. void _bake_gi_probe(Instance *p_gi_probe);
  729. bool _check_gi_probe(Instance *p_gi_probe);
  730. void _setup_gi_probe(Instance *p_instance);
  731. void render_probes();
  732. bool free(RID p_rid);
  733. private:
  734. bool _use_bvh;
  735. VisualServerCallbacks *_visual_server_callbacks;
  736. PortalResources _portal_resources;
  737. public:
  738. VisualServerScene();
  739. virtual ~VisualServerScene();
  740. };
  741. #endif // VISUAL_SERVER_SCENE_H