visual_server_scene.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/allocators.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/self_list.h"
  39. #include "servers/arvr/arvr_interface.h"
  40. class VisualServerScene {
  41. public:
  42. enum {
  43. MAX_INSTANCE_CULL = 65536,
  44. MAX_LIGHTS_CULLED = 4096,
  45. MAX_REFLECTION_PROBES_CULLED = 4096,
  46. MAX_ROOM_CULL = 32,
  47. MAX_EXTERIOR_PORTALS = 128,
  48. };
  49. uint64_t render_pass;
  50. static VisualServerScene *singleton;
  51. // FIXME: Kept as reference for future implementation
  52. #if 0
  53. struct Portal {
  54. bool enabled;
  55. float disable_distance;
  56. Color disable_color;
  57. float connect_range;
  58. Vector<Point2> shape;
  59. Rect2 bounds;
  60. Portal() { enabled=true; disable_distance=50; disable_color=Color(); connect_range=0.8; }
  61. };
  62. #endif
  63. /* CAMERA API */
  64. struct Camera : public RID_Data {
  65. enum Type {
  66. PERSPECTIVE,
  67. ORTHOGONAL
  68. };
  69. Type type;
  70. float fov;
  71. float znear, zfar;
  72. float size;
  73. uint32_t visible_layers;
  74. bool vaspect;
  75. RID env;
  76. Transform transform;
  77. Camera() {
  78. visible_layers = 0xFFFFFFFF;
  79. fov = 70;
  80. type = PERSPECTIVE;
  81. znear = 0.05;
  82. zfar = 100;
  83. size = 1.0;
  84. vaspect = false;
  85. }
  86. };
  87. mutable RID_Owner<Camera> camera_owner;
  88. virtual RID camera_create();
  89. virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
  90. virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
  91. virtual void camera_set_transform(RID p_camera, const Transform &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_use_vertical_aspect(RID p_camera, bool p_enable);
  95. /* SCENARIO API */
  96. struct Instance;
  97. struct Scenario : RID_Data {
  98. VS::ScenarioDebugMode debug;
  99. RID self;
  100. // well wtf, balloon allocator is slower?
  101. Octree<Instance, true> octree;
  102. List<Instance *> directional_lights;
  103. RID environment;
  104. RID fallback_environment;
  105. RID reflection_probe_shadow_atlas;
  106. RID reflection_atlas;
  107. SelfList<Instance>::List instances;
  108. Scenario() { debug = VS::SCENARIO_DEBUG_DISABLED; }
  109. };
  110. mutable RID_Owner<Scenario> scenario_owner;
  111. static void *_instance_pair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int);
  112. static void _instance_unpair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int, void *);
  113. virtual RID scenario_create();
  114. virtual void scenario_set_debug(RID p_scenario, VS::ScenarioDebugMode p_debug_mode);
  115. virtual void scenario_set_environment(RID p_scenario, RID p_environment);
  116. virtual void scenario_set_fallback_environment(RID p_scenario, RID p_environment);
  117. virtual void scenario_set_reflection_atlas_size(RID p_scenario, int p_size, int p_subdiv);
  118. /* INSTANCING API */
  119. struct InstanceBaseData {
  120. virtual ~InstanceBaseData() {}
  121. };
  122. struct Instance : RasterizerScene::InstanceBase {
  123. RID self;
  124. //scenario stuff
  125. OctreeElementID octree_id;
  126. Scenario *scenario;
  127. SelfList<Instance> scenario_item;
  128. //aabb stuff
  129. bool update_aabb;
  130. bool update_materials;
  131. SelfList<Instance> update_item;
  132. AABB aabb;
  133. AABB transformed_aabb;
  134. AABB *custom_aabb; // <Zylann> would using aabb directly with a bool be better?
  135. float extra_margin;
  136. uint32_t object_ID;
  137. float lod_begin;
  138. float lod_end;
  139. float lod_begin_hysteresis;
  140. float lod_end_hysteresis;
  141. RID lod_instance;
  142. uint64_t last_render_pass;
  143. uint64_t last_frame_pass;
  144. uint64_t version; // changes to this, and changes to base increase version
  145. InstanceBaseData *base_data;
  146. virtual void base_removed() {
  147. singleton->instance_set_base(self, RID());
  148. }
  149. virtual void base_changed(bool p_aabb, bool p_materials) {
  150. singleton->_instance_queue_update(this, p_aabb, p_materials);
  151. }
  152. Instance() :
  153. scenario_item(this),
  154. update_item(this) {
  155. octree_id = 0;
  156. scenario = NULL;
  157. update_aabb = false;
  158. update_materials = false;
  159. extra_margin = 0;
  160. object_ID = 0;
  161. visible = true;
  162. lod_begin = 0;
  163. lod_end = 0;
  164. lod_begin_hysteresis = 0;
  165. lod_end_hysteresis = 0;
  166. last_render_pass = 0;
  167. last_frame_pass = 0;
  168. version = 1;
  169. base_data = NULL;
  170. custom_aabb = NULL;
  171. }
  172. ~Instance() {
  173. if (base_data)
  174. memdelete(base_data);
  175. if (custom_aabb)
  176. memdelete(custom_aabb);
  177. }
  178. };
  179. SelfList<Instance>::List _instance_update_list;
  180. void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_materials = false);
  181. struct InstanceGeometryData : public InstanceBaseData {
  182. List<Instance *> lighting;
  183. bool lighting_dirty;
  184. bool can_cast_shadows;
  185. bool material_is_animated;
  186. List<Instance *> reflection_probes;
  187. bool reflection_dirty;
  188. List<Instance *> gi_probes;
  189. bool gi_probes_dirty;
  190. List<Instance *> lightmap_captures;
  191. InstanceGeometryData() {
  192. lighting_dirty = false;
  193. reflection_dirty = true;
  194. can_cast_shadows = true;
  195. material_is_animated = true;
  196. gi_probes_dirty = true;
  197. }
  198. };
  199. struct InstanceReflectionProbeData : public InstanceBaseData {
  200. Instance *owner;
  201. struct PairInfo {
  202. List<Instance *>::Element *L; //reflection iterator in geometry
  203. Instance *geometry;
  204. };
  205. List<PairInfo> geometries;
  206. RID instance;
  207. bool reflection_dirty;
  208. SelfList<InstanceReflectionProbeData> update_list;
  209. int render_step;
  210. InstanceReflectionProbeData() :
  211. update_list(this) {
  212. reflection_dirty = true;
  213. render_step = -1;
  214. }
  215. };
  216. SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
  217. struct InstanceLightData : public InstanceBaseData {
  218. struct PairInfo {
  219. List<Instance *>::Element *L; //light iterator in geometry
  220. Instance *geometry;
  221. };
  222. RID instance;
  223. uint64_t last_version;
  224. List<Instance *>::Element *D; // directional light in scenario
  225. bool shadow_dirty;
  226. List<PairInfo> geometries;
  227. Instance *baked_light;
  228. InstanceLightData() {
  229. shadow_dirty = true;
  230. D = NULL;
  231. last_version = 0;
  232. baked_light = NULL;
  233. }
  234. };
  235. struct InstanceGIProbeData : public InstanceBaseData {
  236. Instance *owner;
  237. struct PairInfo {
  238. List<Instance *>::Element *L; //gi probe iterator in geometry
  239. Instance *geometry;
  240. };
  241. List<PairInfo> geometries;
  242. Set<Instance *> lights;
  243. struct LightCache {
  244. VS::LightType type;
  245. Transform transform;
  246. Color color;
  247. float energy;
  248. float radius;
  249. float attenuation;
  250. float spot_angle;
  251. float spot_attenuation;
  252. bool visible;
  253. bool operator==(const LightCache &p_cache) {
  254. return (type == p_cache.type &&
  255. transform == p_cache.transform &&
  256. color == p_cache.color &&
  257. energy == p_cache.energy &&
  258. radius == p_cache.radius &&
  259. attenuation == p_cache.attenuation &&
  260. spot_angle == p_cache.spot_angle &&
  261. spot_attenuation == p_cache.spot_attenuation &&
  262. visible == p_cache.visible);
  263. }
  264. bool operator!=(const LightCache &p_cache) {
  265. return !operator==(p_cache);
  266. }
  267. LightCache() {
  268. type = VS::LIGHT_DIRECTIONAL;
  269. energy = 1.0;
  270. radius = 1.0;
  271. attenuation = 1.0;
  272. spot_angle = 1.0;
  273. spot_attenuation = 1.0;
  274. visible = true;
  275. }
  276. };
  277. struct LocalData {
  278. uint16_t pos[3];
  279. uint16_t energy[3]; //using 0..1024 for float range 0..1. integer is needed for deterministic add/remove of lights
  280. };
  281. struct CompBlockS3TC {
  282. uint32_t offset; //offset in mipmap
  283. uint32_t source_count; //sources
  284. uint32_t sources[16]; //id for each source
  285. uint8_t alpha[8]; //alpha block is pre-computed
  286. };
  287. struct Dynamic {
  288. Map<RID, LightCache> light_cache;
  289. Map<RID, LightCache> light_cache_changes;
  290. PoolVector<int> light_data;
  291. PoolVector<LocalData> local_data;
  292. Vector<Vector<uint32_t> > level_cell_lists;
  293. RID probe_data;
  294. bool enabled;
  295. int bake_dynamic_range;
  296. RasterizerStorage::GIProbeCompression compression;
  297. Vector<PoolVector<uint8_t> > mipmaps_3d;
  298. Vector<PoolVector<CompBlockS3TC> > mipmaps_s3tc; //for s3tc
  299. int updating_stage;
  300. float propagate;
  301. int grid_size[3];
  302. Transform light_to_cell_xform;
  303. } dynamic;
  304. RID probe_instance;
  305. bool invalid;
  306. uint32_t base_version;
  307. SelfList<InstanceGIProbeData> update_element;
  308. InstanceGIProbeData() :
  309. update_element(this) {
  310. invalid = true;
  311. base_version = 0;
  312. dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  313. }
  314. };
  315. SelfList<InstanceGIProbeData>::List gi_probe_update_list;
  316. struct InstanceLightmapCaptureData : public InstanceBaseData {
  317. struct PairInfo {
  318. List<Instance *>::Element *L; //iterator in geometry
  319. Instance *geometry;
  320. };
  321. List<PairInfo> geometries;
  322. Set<Instance *> users;
  323. InstanceLightmapCaptureData() {
  324. }
  325. };
  326. int instance_cull_count;
  327. Instance *instance_cull_result[MAX_INSTANCE_CULL];
  328. Instance *instance_shadow_cull_result[MAX_INSTANCE_CULL]; //used for generating shadowmaps
  329. Instance *light_cull_result[MAX_LIGHTS_CULLED];
  330. RID light_instance_cull_result[MAX_LIGHTS_CULLED];
  331. int light_cull_count;
  332. int directional_light_count;
  333. RID reflection_probe_instance_cull_result[MAX_REFLECTION_PROBES_CULLED];
  334. int reflection_probe_cull_count;
  335. RID_Owner<Instance> instance_owner;
  336. // from can be mesh, light, area and portal so far.
  337. virtual RID instance_create(); // from can be mesh, light, poly, area and portal so far.
  338. virtual void instance_set_base(RID p_instance, RID p_base); // from can be mesh, light, poly, area and portal so far.
  339. virtual void instance_set_scenario(RID p_instance, RID p_scenario); // from can be mesh, light, poly, area and portal so far.
  340. virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
  341. virtual void instance_set_transform(RID p_instance, const Transform &p_transform);
  342. virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_ID);
  343. virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
  344. virtual void instance_set_surface_material(RID p_instance, int p_surface, RID p_material);
  345. virtual void instance_set_visible(RID p_instance, bool p_visible);
  346. virtual void instance_set_use_lightmap(RID p_instance, RID p_lightmap_instance, RID p_lightmap);
  347. virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
  348. virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
  349. virtual void instance_set_exterior(RID p_instance, bool p_enabled);
  350. virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
  351. // don't use these in a game!
  352. virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
  353. virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
  354. virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
  355. virtual void instance_geometry_set_flag(RID p_instance, VS::InstanceFlags p_flags, bool p_enabled);
  356. virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting);
  357. virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
  358. virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
  359. virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
  360. _FORCE_INLINE_ void _update_instance(Instance *p_instance);
  361. _FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
  362. _FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
  363. _FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
  364. _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);
  365. 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);
  366. void _render_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_force_environment, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass);
  367. void render_empty_scene(RID p_scenario, RID p_shadow_atlas);
  368. void render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  369. 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);
  370. void update_dirty_instances();
  371. //probes
  372. struct GIProbeDataHeader {
  373. uint32_t version;
  374. uint32_t cell_subdiv;
  375. uint32_t width;
  376. uint32_t height;
  377. uint32_t depth;
  378. uint32_t cell_count;
  379. uint32_t leaf_cell_count;
  380. };
  381. struct GIProbeDataCell {
  382. uint32_t children[8];
  383. uint32_t albedo;
  384. uint32_t emission;
  385. uint32_t normal;
  386. uint32_t level_alpha;
  387. };
  388. enum {
  389. GI_UPDATE_STAGE_CHECK,
  390. GI_UPDATE_STAGE_LIGHTING,
  391. GI_UPDATE_STAGE_UPLOADING,
  392. };
  393. void _gi_probe_bake_thread();
  394. static void _gi_probe_bake_threads(void *);
  395. volatile bool probe_bake_thread_exit;
  396. Thread *probe_bake_thread;
  397. Semaphore *probe_bake_sem;
  398. Mutex *probe_bake_mutex;
  399. List<Instance *> probe_bake_list;
  400. bool _render_reflection_probe_step(Instance *p_instance, int p_step);
  401. 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);
  402. _FORCE_INLINE_ uint32_t _gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv);
  403. 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);
  404. 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);
  405. void _bake_gi_probe(Instance *p_gi_probe);
  406. bool _check_gi_probe(Instance *p_gi_probe);
  407. void _setup_gi_probe(Instance *p_instance);
  408. void render_probes();
  409. bool free(RID p_rid);
  410. VisualServerScene();
  411. virtual ~VisualServerScene();
  412. };
  413. #endif // VISUALSERVERSCENE_H