rendering_server_scene.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*************************************************************************/
  2. /* rendering_server_scene.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/rendering/rasterizer.h"
  33. #include "core/local_vector.h"
  34. #include "core/math/geometry_3d.h"
  35. #include "core/math/octree.h"
  36. #include "core/os/semaphore.h"
  37. #include "core/os/thread.h"
  38. #include "core/rid_owner.h"
  39. #include "core/self_list.h"
  40. #include "servers/xr/xr_interface.h"
  41. class RenderingServerScene {
  42. public:
  43. enum {
  44. MAX_INSTANCE_CULL = 65536,
  45. MAX_LIGHTS_CULLED = 4096,
  46. MAX_REFLECTION_PROBES_CULLED = 4096,
  47. MAX_DECALS_CULLED = 4096,
  48. MAX_GI_PROBES_CULLED = 4096,
  49. MAX_ROOM_CULL = 32,
  50. MAX_LIGHTMAPS_CULLED = 4096,
  51. MAX_EXTERIOR_PORTALS = 128,
  52. };
  53. uint64_t render_pass;
  54. static RenderingServerScene *singleton;
  55. /* CAMERA API */
  56. struct Camera {
  57. enum Type {
  58. PERSPECTIVE,
  59. ORTHOGONAL,
  60. FRUSTUM
  61. };
  62. Type type;
  63. float fov;
  64. float znear, zfar;
  65. float size;
  66. Vector2 offset;
  67. uint32_t visible_layers;
  68. bool vaspect;
  69. RID env;
  70. RID effects;
  71. Transform transform;
  72. Camera() {
  73. visible_layers = 0xFFFFFFFF;
  74. fov = 75;
  75. type = PERSPECTIVE;
  76. znear = 0.05;
  77. zfar = 100;
  78. size = 1.0;
  79. offset = Vector2();
  80. vaspect = false;
  81. }
  82. };
  83. mutable RID_PtrOwner<Camera> camera_owner;
  84. virtual RID camera_create();
  85. virtual void camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far);
  86. virtual void camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far);
  87. virtual void camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
  88. virtual void camera_set_transform(RID p_camera, const Transform &p_transform);
  89. virtual void camera_set_cull_mask(RID p_camera, uint32_t p_layers);
  90. virtual void camera_set_environment(RID p_camera, RID p_env);
  91. virtual void camera_set_camera_effects(RID p_camera, RID p_fx);
  92. virtual void camera_set_use_vertical_aspect(RID p_camera, bool p_enable);
  93. /* SCENARIO API */
  94. struct Instance;
  95. struct Scenario {
  96. RS::ScenarioDebugMode debug;
  97. RID self;
  98. Octree<Instance, true> octree;
  99. List<Instance *> directional_lights;
  100. RID environment;
  101. RID fallback_environment;
  102. RID camera_effects;
  103. RID reflection_probe_shadow_atlas;
  104. RID reflection_atlas;
  105. SelfList<Instance>::List instances;
  106. LocalVector<RID> dynamic_lights;
  107. Scenario() { debug = RS::SCENARIO_DEBUG_DISABLED; }
  108. };
  109. mutable RID_PtrOwner<Scenario> scenario_owner;
  110. static void *_instance_pair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int);
  111. static void _instance_unpair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int, void *);
  112. virtual RID scenario_create();
  113. virtual void scenario_set_debug(RID p_scenario, RS::ScenarioDebugMode p_debug_mode);
  114. virtual void scenario_set_environment(RID p_scenario, RID p_environment);
  115. virtual void scenario_set_camera_effects(RID p_scenario, RID p_fx);
  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_reflection_size, int p_reflection_count);
  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_dependencies;
  131. SelfList<Instance> update_item;
  132. AABB *custom_aabb; // <Zylann> would using aabb directly with a bool be better?
  133. float extra_margin;
  134. ObjectID 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. Vector<Color> lightmap_target_sh; //target is used for incrementally changing the SH over time, this avoids pops in some corner cases and when going interior <-> exterior
  141. uint64_t last_render_pass;
  142. uint64_t last_frame_pass;
  143. uint64_t version; // changes to this, and changes to base increase version
  144. InstanceBaseData *base_data;
  145. virtual void dependency_deleted(RID p_dependency) {
  146. if (p_dependency == base) {
  147. singleton->instance_set_base(self, RID());
  148. } else if (p_dependency == skeleton) {
  149. singleton->instance_attach_skeleton(self, RID());
  150. } else {
  151. singleton->_instance_queue_update(this, false, true);
  152. }
  153. }
  154. virtual void dependency_changed(bool p_aabb, bool p_dependencies) {
  155. singleton->_instance_queue_update(this, p_aabb, p_dependencies);
  156. }
  157. Instance() :
  158. scenario_item(this),
  159. update_item(this) {
  160. octree_id = 0;
  161. scenario = nullptr;
  162. update_aabb = false;
  163. update_dependencies = false;
  164. extra_margin = 0;
  165. visible = true;
  166. lod_begin = 0;
  167. lod_end = 0;
  168. lod_begin_hysteresis = 0;
  169. lod_end_hysteresis = 0;
  170. last_render_pass = 0;
  171. last_frame_pass = 0;
  172. version = 1;
  173. base_data = nullptr;
  174. custom_aabb = nullptr;
  175. }
  176. ~Instance() {
  177. if (base_data) {
  178. memdelete(base_data);
  179. }
  180. if (custom_aabb) {
  181. memdelete(custom_aabb);
  182. }
  183. }
  184. };
  185. SelfList<Instance>::List _instance_update_list;
  186. void _instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies = false);
  187. struct InstanceGeometryData : public InstanceBaseData {
  188. List<Instance *> lighting;
  189. bool lighting_dirty;
  190. bool can_cast_shadows;
  191. bool material_is_animated;
  192. List<Instance *> decals;
  193. bool decal_dirty;
  194. List<Instance *> reflection_probes;
  195. bool reflection_dirty;
  196. List<Instance *> gi_probes;
  197. bool gi_probes_dirty;
  198. List<Instance *> lightmap_captures;
  199. InstanceGeometryData() {
  200. lighting_dirty = false;
  201. reflection_dirty = true;
  202. can_cast_shadows = true;
  203. material_is_animated = true;
  204. gi_probes_dirty = true;
  205. decal_dirty = true;
  206. }
  207. };
  208. struct InstanceReflectionProbeData : public InstanceBaseData {
  209. Instance *owner;
  210. struct PairInfo {
  211. List<Instance *>::Element *L; //reflection iterator in geometry
  212. Instance *geometry;
  213. };
  214. List<PairInfo> geometries;
  215. RID instance;
  216. bool reflection_dirty;
  217. SelfList<InstanceReflectionProbeData> update_list;
  218. int render_step;
  219. InstanceReflectionProbeData() :
  220. update_list(this) {
  221. reflection_dirty = true;
  222. render_step = -1;
  223. }
  224. };
  225. struct InstanceDecalData : public InstanceBaseData {
  226. Instance *owner;
  227. RID instance;
  228. struct PairInfo {
  229. List<Instance *>::Element *L; //reflection iterator in geometry
  230. Instance *geometry;
  231. };
  232. List<PairInfo> geometries;
  233. InstanceDecalData() {
  234. }
  235. };
  236. SelfList<InstanceReflectionProbeData>::List reflection_probe_render_list;
  237. struct InstanceLightData : public InstanceBaseData {
  238. struct PairInfo {
  239. List<Instance *>::Element *L; //light iterator in geometry
  240. Instance *geometry;
  241. };
  242. RID instance;
  243. uint64_t last_version;
  244. List<Instance *>::Element *D; // directional light in scenario
  245. bool shadow_dirty;
  246. List<PairInfo> geometries;
  247. Instance *baked_light;
  248. RS::LightBakeMode bake_mode;
  249. uint32_t max_sdfgi_cascade = 2;
  250. uint64_t sdfgi_cascade_light_pass = 0;
  251. InstanceLightData() {
  252. bake_mode = RS::LIGHT_BAKE_DISABLED;
  253. shadow_dirty = true;
  254. D = nullptr;
  255. last_version = 0;
  256. baked_light = nullptr;
  257. }
  258. };
  259. struct InstanceGIProbeData : public InstanceBaseData {
  260. Instance *owner;
  261. struct PairInfo {
  262. List<Instance *>::Element *L; //gi probe iterator in geometry
  263. Instance *geometry;
  264. };
  265. List<PairInfo> geometries;
  266. List<PairInfo> dynamic_geometries;
  267. Set<Instance *> lights;
  268. struct LightCache {
  269. RS::LightType type;
  270. Transform transform;
  271. Color color;
  272. float energy;
  273. float bake_energy;
  274. float radius;
  275. float attenuation;
  276. float spot_angle;
  277. float spot_attenuation;
  278. bool has_shadow;
  279. };
  280. Vector<LightCache> light_cache;
  281. Vector<RID> light_instances;
  282. RID probe_instance;
  283. bool invalid;
  284. uint32_t base_version;
  285. SelfList<InstanceGIProbeData> update_element;
  286. InstanceGIProbeData() :
  287. update_element(this) {
  288. invalid = true;
  289. base_version = 0;
  290. }
  291. };
  292. SelfList<InstanceGIProbeData>::List gi_probe_update_list;
  293. struct InstanceLightmapData : public InstanceBaseData {
  294. struct PairInfo {
  295. List<Instance *>::Element *L; //iterator in geometry
  296. Instance *geometry;
  297. };
  298. List<PairInfo> geometries;
  299. Set<Instance *> users;
  300. InstanceLightmapData() {
  301. }
  302. };
  303. Set<Instance *> heightfield_particle_colliders_update_list;
  304. int instance_cull_count;
  305. Instance *instance_cull_result[MAX_INSTANCE_CULL];
  306. Instance *instance_shadow_cull_result[MAX_INSTANCE_CULL]; //used for generating shadowmaps
  307. Instance *light_cull_result[MAX_LIGHTS_CULLED];
  308. RID sdfgi_light_cull_result[MAX_LIGHTS_CULLED];
  309. RID light_instance_cull_result[MAX_LIGHTS_CULLED];
  310. uint64_t sdfgi_light_cull_pass = 0;
  311. int light_cull_count;
  312. int directional_light_count;
  313. RID reflection_probe_instance_cull_result[MAX_REFLECTION_PROBES_CULLED];
  314. RID decal_instance_cull_result[MAX_DECALS_CULLED];
  315. int reflection_probe_cull_count;
  316. int decal_cull_count;
  317. RID gi_probe_instance_cull_result[MAX_GI_PROBES_CULLED];
  318. int gi_probe_cull_count;
  319. Instance *lightmap_cull_result[MAX_LIGHTS_CULLED];
  320. int lightmap_cull_count;
  321. RID_PtrOwner<Instance> instance_owner;
  322. virtual RID instance_create();
  323. virtual void instance_set_base(RID p_instance, RID p_base);
  324. virtual void instance_set_scenario(RID p_instance, RID p_scenario);
  325. virtual void instance_set_layer_mask(RID p_instance, uint32_t p_mask);
  326. virtual void instance_set_transform(RID p_instance, const Transform &p_transform);
  327. virtual void instance_attach_object_instance_id(RID p_instance, ObjectID p_id);
  328. virtual void instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight);
  329. virtual void instance_set_surface_material(RID p_instance, int p_surface, RID p_material);
  330. virtual void instance_set_visible(RID p_instance, bool p_visible);
  331. virtual void instance_set_custom_aabb(RID p_instance, AABB p_aabb);
  332. virtual void instance_attach_skeleton(RID p_instance, RID p_skeleton);
  333. virtual void instance_set_exterior(RID p_instance, bool p_enabled);
  334. virtual void instance_set_extra_visibility_margin(RID p_instance, real_t p_margin);
  335. // don't use these in a game!
  336. virtual Vector<ObjectID> instances_cull_aabb(const AABB &p_aabb, RID p_scenario = RID()) const;
  337. virtual Vector<ObjectID> instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario = RID()) const;
  338. virtual Vector<ObjectID> instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario = RID()) const;
  339. virtual void instance_geometry_set_flag(RID p_instance, RS::InstanceFlags p_flags, bool p_enabled);
  340. virtual void instance_geometry_set_cast_shadows_setting(RID p_instance, RS::ShadowCastingSetting p_shadow_casting_setting);
  341. virtual void instance_geometry_set_material_override(RID p_instance, RID p_material);
  342. virtual void instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin);
  343. virtual void instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance);
  344. virtual void instance_geometry_set_lightmap(RID p_instance, RID p_lightmap, const Rect2 &p_lightmap_uv_scale, int p_slice_index);
  345. void _update_instance_shader_parameters_from_material(Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter> &isparams, const Map<StringName, RasterizerScene::InstanceBase::InstanceShaderParameter> &existing_isparams, RID p_material);
  346. virtual void instance_geometry_set_shader_parameter(RID p_instance, const StringName &p_parameter, const Variant &p_value);
  347. virtual void instance_geometry_get_shader_parameter_list(RID p_instance, List<PropertyInfo> *p_parameters) const;
  348. virtual Variant instance_geometry_get_shader_parameter(RID p_instance, const StringName &p_parameter) const;
  349. virtual Variant instance_geometry_get_shader_parameter_default_value(RID p_instance, const StringName &p_parameter) const;
  350. _FORCE_INLINE_ void _update_instance(Instance *p_instance);
  351. _FORCE_INLINE_ void _update_instance_aabb(Instance *p_instance);
  352. _FORCE_INLINE_ void _update_dirty_instance(Instance *p_instance);
  353. _FORCE_INLINE_ void _update_instance_lightmap_captures(Instance *p_instance);
  354. _FORCE_INLINE_ bool _light_instance_update_shadow(Instance *p_instance, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_shadow_atlas, Scenario *p_scenario);
  355. RID _render_get_environment(RID p_camera, RID p_scenario);
  356. bool _render_reflection_probe_step(Instance *p_instance, int p_step);
  357. void _prepare_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_render_buffers, RID p_environment, uint32_t p_visible_layers, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, bool p_using_shadows = true);
  358. void _render_scene(RID p_render_buffers, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_environment, RID p_force_camera_effects, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass);
  359. void render_empty_scene(RID p_render_buffers, RID p_scenario, RID p_shadow_atlas);
  360. void render_camera(RID p_render_buffers, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  361. void render_camera(RID p_render_buffers, Ref<XRInterface> &p_interface, XRInterface::Eyes p_eye, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas);
  362. void update_dirty_instances();
  363. void render_particle_colliders();
  364. void render_probes();
  365. TypedArray<Image> bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size);
  366. bool free(RID p_rid);
  367. RenderingServerScene();
  368. virtual ~RenderingServerScene();
  369. };
  370. #endif // VISUALSERVERSCENE_H