2
0

visual_server_scene.h 16 KB

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