visual_server_scene.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 "allocators.h"
  34. #include "geometry.h"
  35. #include "octree.h"
  36. #include "os/semaphore.h"
  37. #include "os/thread.h"
  38. #include "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() {
  150. singleton->_instance_queue_update(this, true, true);
  151. }
  152. virtual void base_material_changed() {
  153. singleton->_instance_queue_update(this, false, true);
  154. }
  155. Instance() :
  156. scenario_item(this),
  157. update_item(this) {
  158. octree_id = 0;
  159. scenario = NULL;
  160. update_aabb = false;
  161. update_materials = false;
  162. extra_margin = 0;
  163. object_ID = 0;
  164. visible = true;
  165. lod_begin = 0;
  166. lod_end = 0;
  167. lod_begin_hysteresis = 0;
  168. lod_end_hysteresis = 0;
  169. last_render_pass = 0;
  170. last_frame_pass = 0;
  171. version = 1;
  172. base_data = NULL;
  173. custom_aabb = NULL;
  174. }
  175. ~Instance() {
  176. if (base_data)
  177. memdelete(base_data);
  178. if (custom_aabb)
  179. memdelete(custom_aabb);
  180. }
  181. };
  182. SelfList<Instance>::List _instance_update_list;
  183. void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_materials = false);
  184. struct InstanceGeometryData : public InstanceBaseData {
  185. List<Instance *> lighting;
  186. bool lighting_dirty;
  187. bool can_cast_shadows;
  188. List<Instance *> reflection_probes;
  189. bool reflection_dirty;
  190. List<Instance *> gi_probes;
  191. bool gi_probes_dirty;
  192. List<Instance *> lightmap_captures;
  193. InstanceGeometryData() {
  194. lighting_dirty = false;
  195. reflection_dirty = true;
  196. can_cast_shadows = true;
  197. gi_probes_dirty = true;
  198. }
  199. };
  200. struct InstanceReflectionProbeData : public InstanceBaseData {
  201. Instance *owner;
  202. struct PairInfo {
  203. List<Instance *>::Element *L; //reflection iterator in geometry
  204. Instance *geometry;
  205. };
  206. List<PairInfo> geometries;
  207. RID instance;
  208. bool reflection_dirty;
  209. SelfList<InstanceReflectionProbeData> update_list;
  210. int render_step;
  211. InstanceReflectionProbeData() :
  212. update_list(this) {
  213. reflection_dirty = true;
  214. render_step = -1;
  215. }
  216. };
  217. SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
  218. struct InstanceLightData : public InstanceBaseData {
  219. struct PairInfo {
  220. List<Instance *>::Element *L; //light iterator in geometry
  221. Instance *geometry;
  222. };
  223. RID instance;
  224. uint64_t last_version;
  225. List<Instance *>::Element *D; // directional light in scenario
  226. bool shadow_dirty;
  227. List<PairInfo> geometries;
  228. Instance *baked_light;
  229. InstanceLightData() {
  230. shadow_dirty = true;
  231. D = NULL;
  232. last_version = 0;
  233. baked_light = NULL;
  234. }
  235. };
  236. struct InstanceGIProbeData : public InstanceBaseData {
  237. Instance *owner;
  238. struct PairInfo {
  239. List<Instance *>::Element *L; //gi probe iterator in geometry
  240. Instance *geometry;
  241. };
  242. List<PairInfo> geometries;
  243. Set<Instance *> lights;
  244. struct LightCache {
  245. VS::LightType type;
  246. Transform transform;
  247. Color color;
  248. float energy;
  249. float radius;
  250. float attenuation;
  251. float spot_angle;
  252. float spot_attenuation;
  253. bool visible;
  254. bool operator==(const LightCache &p_cache) {
  255. return (type == p_cache.type &&
  256. transform == p_cache.transform &&
  257. color == p_cache.color &&
  258. energy == p_cache.energy &&
  259. radius == p_cache.radius &&
  260. attenuation == p_cache.attenuation &&
  261. spot_angle == p_cache.spot_angle &&
  262. spot_attenuation == p_cache.spot_attenuation &&
  263. visible == p_cache.visible);
  264. }
  265. LightCache() {
  266. type = VS::LIGHT_DIRECTIONAL;
  267. energy = 1.0;
  268. radius = 1.0;
  269. attenuation = 1.0;
  270. spot_angle = 1.0;
  271. spot_attenuation = 1.0;
  272. visible = true;
  273. }
  274. };
  275. struct LocalData {
  276. uint16_t pos[3];
  277. uint16_t energy[3]; //using 0..1024 for float range 0..1. integer is needed for deterministic add/remove of lights
  278. };
  279. struct CompBlockS3TC {
  280. uint32_t offset; //offset in mipmap
  281. uint32_t source_count; //sources
  282. uint32_t sources[16]; //id for each source
  283. uint8_t alpha[8]; //alpha block is pre-computed
  284. };
  285. struct Dynamic {
  286. Map<RID, LightCache> light_cache;
  287. Map<RID, LightCache> light_cache_changes;
  288. PoolVector<int> light_data;
  289. PoolVector<LocalData> local_data;
  290. Vector<Vector<uint32_t> > level_cell_lists;
  291. RID probe_data;
  292. bool enabled;
  293. int bake_dynamic_range;
  294. RasterizerStorage::GIProbeCompression compression;
  295. Vector<PoolVector<uint8_t> > mipmaps_3d;
  296. Vector<PoolVector<CompBlockS3TC> > mipmaps_s3tc; //for s3tc
  297. int updating_stage;
  298. float propagate;
  299. int grid_size[3];
  300. Transform light_to_cell_xform;
  301. } dynamic;
  302. RID probe_instance;
  303. bool invalid;
  304. uint32_t base_version;
  305. SelfList<InstanceGIProbeData> update_element;
  306. InstanceGIProbeData() :
  307. update_element(this) {
  308. invalid = true;
  309. base_version = 0;
  310. dynamic.updating_stage = GI_UPDATE_STAGE_CHECK;
  311. }
  312. };
  313. SelfList<InstanceGIProbeData>::List gi_probe_update_list;
  314. struct InstanceLightmapCaptureData : public InstanceBaseData {
  315. struct PairInfo {
  316. List<Instance *>::Element *L; //iterator in geometry
  317. Instance *geometry;
  318. };
  319. List<PairInfo> geometries;
  320. Set<Instance *> users;
  321. InstanceLightmapCaptureData() {
  322. }
  323. };
  324. Instance *instance_cull_result[MAX_INSTANCE_CULL];
  325. Instance *instance_shadow_cull_result[MAX_INSTANCE_CULL]; //used for generating shadowmaps
  326. Instance *light_cull_result[MAX_LIGHTS_CULLED];
  327. RID light_instance_cull_result[MAX_LIGHTS_CULLED];
  328. int light_cull_count;
  329. RID reflection_probe_instance_cull_result[MAX_REFLECTION_PROBES_CULLED];
  330. int reflection_probe_cull_count;
  331. RID_Owner<Instance> instance_owner;
  332. // from can be mesh, light, area and portal so far.
  333. virtual RID instance_create(); // from can be mesh, light, poly, area and portal so far.
  334. virtual void instance_set_base(RID p_instance, RID p_base); // from can be mesh, light, poly, area and portal so far.
  335. virtual void instance_set_scenario(RID p_instance, RID p_scenario); // from can be mesh, light, poly, area and portal so far.
  336. virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
  337. virtual void instance_set_transform(RID p_instance, const Transform &p_transform);
  338. virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_ID);
  339. virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
  340. virtual void instance_set_surface_material(RID p_instance, int p_surface, RID p_material);
  341. virtual void instance_set_visible(RID p_instance, bool p_visible);
  342. virtual void instance_set_use_lightmap(RID p_instance, RID p_lightmap_instance, RID p_lightmap);
  343. virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
  344. virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
  345. virtual void instance_set_exterior(RID p_instance, bool p_enabled);
  346. virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
  347. // don't use these in a game!
  348. virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
  349. virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
  350. virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
  351. virtual void instance_geometry_set_flag(RID p_instance, VS::InstanceFlags p_flags, bool p_enabled);
  352. virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting);
  353. virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
  354. virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
  355. virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
  356. _FORCE_INLINE_ void _update_instance(Instance *p_instance);
  357. _FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
  358. _FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
  359. _FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
  360. _FORCE_INLINE_ void _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);
  361. void _render_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, int p_reflection_probe_pass);
  362. void render_empty_scene(RID p_scenario, RID p_shadow_atlas);
  363. void render_camera(RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  364. 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);
  365. void update_dirty_instances();
  366. //probes
  367. struct GIProbeDataHeader {
  368. uint32_t version;
  369. uint32_t cell_subdiv;
  370. uint32_t width;
  371. uint32_t height;
  372. uint32_t depth;
  373. uint32_t cell_count;
  374. uint32_t leaf_cell_count;
  375. };
  376. struct GIProbeDataCell {
  377. uint32_t children[8];
  378. uint32_t albedo;
  379. uint32_t emission;
  380. uint32_t normal;
  381. uint32_t level_alpha;
  382. };
  383. enum {
  384. GI_UPDATE_STAGE_CHECK,
  385. GI_UPDATE_STAGE_LIGHTING,
  386. GI_UPDATE_STAGE_UPLOADING,
  387. };
  388. void _gi_probe_bake_thread();
  389. static void _gi_probe_bake_threads(void *);
  390. volatile bool probe_bake_thread_exit;
  391. Thread *probe_bake_thread;
  392. Semaphore *probe_bake_sem;
  393. Mutex *probe_bake_mutex;
  394. List<Instance *> probe_bake_list;
  395. bool _render_reflection_probe_step(Instance *p_instance, int p_step);
  396. 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);
  397. _FORCE_INLINE_ uint32_t _gi_bake_find_cell(const GIProbeDataCell *cells, int x, int y, int z, int p_cell_subdiv);
  398. 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);
  399. 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);
  400. void _bake_gi_probe(Instance *p_gi_probe);
  401. bool _check_gi_probe(Instance *p_gi_probe);
  402. void _setup_gi_probe(Instance *p_instance);
  403. void render_probes();
  404. bool free(RID p_rid);
  405. VisualServerScene();
  406. ~VisualServerScene();
  407. };
  408. #endif // VISUALSERVERSCENE_H