renderer_scene_render_rd.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************/
  2. /* renderer_scene_render_rd.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 RENDERER_SCENE_RENDER_RD_H
  31. #define RENDERER_SCENE_RENDER_RD_H
  32. #include "core/templates/local_vector.h"
  33. #include "core/templates/rid_owner.h"
  34. #include "servers/rendering/renderer_compositor.h"
  35. #include "servers/rendering/renderer_rd/cluster_builder_rd.h"
  36. #include "servers/rendering/renderer_rd/effects/bokeh_dof.h"
  37. #include "servers/rendering/renderer_rd/effects/copy_effects.h"
  38. #include "servers/rendering/renderer_rd/effects/debug_effects.h"
  39. #include "servers/rendering/renderer_rd/effects/fsr.h"
  40. #include "servers/rendering/renderer_rd/effects/luminance.h"
  41. #include "servers/rendering/renderer_rd/effects/tone_mapper.h"
  42. #include "servers/rendering/renderer_rd/effects/vrs.h"
  43. #include "servers/rendering/renderer_rd/environment/fog.h"
  44. #include "servers/rendering/renderer_rd/environment/gi.h"
  45. #include "servers/rendering/renderer_rd/environment/sky.h"
  46. #include "servers/rendering/renderer_rd/framebuffer_cache_rd.h"
  47. #include "servers/rendering/renderer_rd/storage_rd/light_storage.h"
  48. #include "servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
  49. #include "servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.h"
  50. #include "servers/rendering/renderer_scene_render.h"
  51. #include "servers/rendering/rendering_device.h"
  52. #include "servers/rendering/rendering_method.h"
  53. // For RenderDataRD, possibly inherited from RefCounted and add proper getters for our implementation classes
  54. struct RenderDataRD {
  55. Ref<RenderSceneBuffersRD> render_buffers;
  56. RenderSceneDataRD *scene_data;
  57. const PagedArray<RenderGeometryInstance *> *instances = nullptr;
  58. const PagedArray<RID> *lights = nullptr;
  59. const PagedArray<RID> *reflection_probes = nullptr;
  60. const PagedArray<RID> *voxel_gi_instances = nullptr;
  61. const PagedArray<RID> *decals = nullptr;
  62. const PagedArray<RID> *lightmaps = nullptr;
  63. const PagedArray<RID> *fog_volumes = nullptr;
  64. RID environment;
  65. RID camera_attributes;
  66. RID shadow_atlas;
  67. RID occluder_debug_tex;
  68. RID reflection_atlas;
  69. RID reflection_probe;
  70. int reflection_probe_pass = 0;
  71. RID cluster_buffer;
  72. uint32_t cluster_size = 0;
  73. uint32_t cluster_max_elements = 0;
  74. uint32_t directional_light_count = 0;
  75. bool directional_light_soft_shadows = false;
  76. RenderingMethod::RenderInfo *render_info = nullptr;
  77. /* Shadow data */
  78. const RendererSceneRender::RenderShadowData *render_shadows = nullptr;
  79. int render_shadow_count = 0;
  80. LocalVector<int> cube_shadows;
  81. LocalVector<int> shadows;
  82. LocalVector<int> directional_shadows;
  83. /* GI info */
  84. const RendererSceneRender::RenderSDFGIData *render_sdfgi_regions = nullptr;
  85. int render_sdfgi_region_count = 0;
  86. const RendererSceneRender::RenderSDFGIUpdateData *sdfgi_update_data = nullptr;
  87. uint32_t voxel_gi_count = 0;
  88. };
  89. class RendererSceneRenderRD : public RendererSceneRender {
  90. friend RendererRD::SkyRD;
  91. friend RendererRD::GI;
  92. protected:
  93. RendererRD::ForwardIDStorage *forward_id_storage = nullptr;
  94. RendererRD::BokehDOF *bokeh_dof = nullptr;
  95. RendererRD::CopyEffects *copy_effects = nullptr;
  96. RendererRD::DebugEffects *debug_effects = nullptr;
  97. RendererRD::Luminance *luminance = nullptr;
  98. RendererRD::ToneMapper *tone_mapper = nullptr;
  99. RendererRD::FSR *fsr = nullptr;
  100. RendererRD::VRS *vrs = nullptr;
  101. double time = 0.0;
  102. double time_step = 0.0;
  103. /* ENVIRONMENT */
  104. bool glow_bicubic_upscale = false;
  105. bool use_physical_light_units = false;
  106. ////////////////////////////////
  107. virtual RendererRD::ForwardIDStorage *create_forward_id_storage() { return memnew(RendererRD::ForwardIDStorage); };
  108. void _update_vrs(Ref<RenderSceneBuffersRD> p_render_buffers);
  109. virtual void setup_render_buffer_data(Ref<RenderSceneBuffersRD> p_render_buffers) = 0;
  110. virtual void _render_scene(RenderDataRD *p_render_data, const Color &p_default_color) = 0;
  111. virtual void _render_buffers_debug_draw(const RenderDataRD *p_render_data);
  112. virtual void _render_material(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region, float p_exposure_normalization) = 0;
  113. virtual void _render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) = 0;
  114. virtual void _render_sdfgi(Ref<RenderSceneBuffersRD> p_render_buffers, const Vector3i &p_from, const Vector3i &p_size, const AABB &p_bounds, const PagedArray<RenderGeometryInstance *> &p_instances, const RID &p_albedo_texture, const RID &p_emission_texture, const RID &p_emission_aniso_texture, const RID &p_geom_facing_texture, float p_exposure_normalization) = 0;
  115. virtual void _render_particle_collider_heightfield(RID p_fb, const Transform3D &p_cam_transform, const Projection &p_cam_projection, const PagedArray<RenderGeometryInstance *> &p_instances) = 0;
  116. void _debug_sdfgi_probes(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_framebuffer, uint32_t p_view_count, const Projection *p_camera_with_transforms, bool p_will_continue_color, bool p_will_continue_depth);
  117. virtual RID _render_buffers_get_normal_texture(Ref<RenderSceneBuffersRD> p_render_buffers) = 0;
  118. virtual RID _render_buffers_get_velocity_texture(Ref<RenderSceneBuffersRD> p_render_buffers) = 0;
  119. bool _needs_post_prepass_render(RenderDataRD *p_render_data, bool p_use_gi);
  120. void _post_prepass_render(RenderDataRD *p_render_data, bool p_use_gi);
  121. void _pre_resolve_render(RenderDataRD *p_render_data, bool p_use_gi);
  122. void _render_buffers_copy_screen_texture(const RenderDataRD *p_render_data);
  123. void _render_buffers_copy_depth_texture(const RenderDataRD *p_render_data);
  124. void _render_buffers_post_process_and_tonemap(const RenderDataRD *p_render_data);
  125. void _post_process_subpass(RID p_source_texture, RID p_framebuffer, const RenderDataRD *p_render_data);
  126. void _disable_clear_request(const RenderDataRD *p_render_data);
  127. // needed for a single argument calls (material and uv2)
  128. PagedArrayPool<RenderGeometryInstance *> cull_argument_pool;
  129. PagedArray<RenderGeometryInstance *> cull_argument; //need this to exist
  130. RendererRD::SkyRD sky;
  131. RendererRD::GI gi;
  132. virtual void _update_shader_quality_settings() {}
  133. private:
  134. RS::ViewportDebugDraw debug_draw = RS::VIEWPORT_DEBUG_DRAW_DISABLED;
  135. static RendererSceneRenderRD *singleton;
  136. /* Shadow atlas */
  137. RS::ShadowQuality shadows_quality = RS::SHADOW_QUALITY_MAX; //So it always updates when first set
  138. RS::ShadowQuality directional_shadow_quality = RS::SHADOW_QUALITY_MAX;
  139. float shadows_quality_radius = 1.0;
  140. float directional_shadow_quality_radius = 1.0;
  141. float *directional_penumbra_shadow_kernel = nullptr;
  142. float *directional_soft_shadow_kernel = nullptr;
  143. float *penumbra_shadow_kernel = nullptr;
  144. float *soft_shadow_kernel = nullptr;
  145. int directional_penumbra_shadow_samples = 0;
  146. int directional_soft_shadow_samples = 0;
  147. int penumbra_shadow_samples = 0;
  148. int soft_shadow_samples = 0;
  149. RS::DecalFilter decals_filter = RS::DECAL_FILTER_LINEAR_MIPMAPS;
  150. RS::LightProjectorFilter light_projectors_filter = RS::LIGHT_PROJECTOR_FILTER_LINEAR_MIPMAPS;
  151. /* RENDER BUFFERS */
  152. /* GI */
  153. bool screen_space_roughness_limiter = false;
  154. float screen_space_roughness_limiter_amount = 0.25;
  155. float screen_space_roughness_limiter_limit = 0.18;
  156. /* Light data */
  157. uint64_t scene_pass = 0;
  158. uint32_t max_cluster_elements = 512;
  159. /* Volumetric Fog */
  160. uint32_t volumetric_fog_size = 128;
  161. uint32_t volumetric_fog_depth = 128;
  162. bool volumetric_fog_filter_active = true;
  163. public:
  164. static RendererSceneRenderRD *get_singleton() { return singleton; }
  165. /* LIGHTING */
  166. virtual void setup_added_reflection_probe(const Transform3D &p_transform, const Vector3 &p_half_size){};
  167. virtual void setup_added_light(const RS::LightType p_type, const Transform3D &p_transform, float p_radius, float p_spot_aperture){};
  168. virtual void setup_added_decal(const Transform3D &p_transform, const Vector3 &p_half_size){};
  169. /* GI */
  170. RendererRD::GI *get_gi() { return &gi; }
  171. /* SKY */
  172. RendererRD::SkyRD *get_sky() { return &sky; }
  173. /* SKY API */
  174. virtual RID sky_allocate() override;
  175. virtual void sky_initialize(RID p_rid) override;
  176. virtual void sky_set_radiance_size(RID p_sky, int p_radiance_size) override;
  177. virtual void sky_set_mode(RID p_sky, RS::SkyMode p_mode) override;
  178. virtual void sky_set_material(RID p_sky, RID p_material) override;
  179. virtual Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) override;
  180. /* ENVIRONMENT API */
  181. virtual void environment_glow_set_use_bicubic_upscale(bool p_enable) override;
  182. virtual void environment_set_volumetric_fog_volume_size(int p_size, int p_depth) override;
  183. virtual void environment_set_volumetric_fog_filter_active(bool p_enable) override;
  184. virtual void environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) override;
  185. virtual void environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) override;
  186. virtual void environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update) override;
  187. virtual Ref<Image> environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) override;
  188. _FORCE_INLINE_ bool is_using_physical_light_units() {
  189. return use_physical_light_units;
  190. }
  191. /* REFLECTION PROBE */
  192. virtual RID reflection_probe_create_framebuffer(RID p_color, RID p_depth);
  193. /* FOG VOLUMES */
  194. uint32_t get_volumetric_fog_size() const { return volumetric_fog_size; }
  195. uint32_t get_volumetric_fog_depth() const { return volumetric_fog_depth; }
  196. bool get_volumetric_fog_filter_active() const { return volumetric_fog_filter_active; }
  197. virtual RID fog_volume_instance_create(RID p_fog_volume) override;
  198. virtual void fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) override;
  199. virtual void fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) override;
  200. virtual RID fog_volume_instance_get_volume(RID p_fog_volume_instance) const override;
  201. virtual Vector3 fog_volume_instance_get_position(RID p_fog_volume_instance) const override;
  202. /* gi light probes */
  203. virtual RID voxel_gi_instance_create(RID p_base) override;
  204. virtual void voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) override;
  205. virtual bool voxel_gi_needs_update(RID p_probe) const override;
  206. virtual void voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) override;
  207. virtual void voxel_gi_set_quality(RS::VoxelGIQuality p_quality) override { gi.voxel_gi_quality = p_quality; }
  208. /* render buffers */
  209. virtual float _render_buffers_get_luminance_multiplier();
  210. virtual RD::DataFormat _render_buffers_get_color_format();
  211. virtual bool _render_buffers_can_be_storage();
  212. virtual Ref<RenderSceneBuffers> render_buffers_create() override;
  213. virtual void gi_set_use_half_resolution(bool p_enable) override;
  214. RID render_buffers_get_default_voxel_gi_buffer();
  215. virtual void base_uniforms_changed() = 0;
  216. virtual void render_scene(const Ref<RenderSceneBuffers> &p_render_buffers, const CameraData *p_camera_data, const CameraData *p_prev_camera_data, const PagedArray<RenderGeometryInstance *> &p_instances, const PagedArray<RID> &p_lights, const PagedArray<RID> &p_reflection_probes, const PagedArray<RID> &p_voxel_gi_instances, const PagedArray<RID> &p_decals, const PagedArray<RID> &p_lightmaps, const PagedArray<RID> &p_fog_volumes, RID p_environment, RID p_camera_attributes, RID p_shadow_atlas, RID p_occluder_debug_tex, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_mesh_lod_threshold, const RenderShadowData *p_render_shadows, int p_render_shadow_count, const RenderSDFGIData *p_render_sdfgi_regions, int p_render_sdfgi_region_count, const RenderSDFGIUpdateData *p_sdfgi_update_data = nullptr, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
  217. virtual void render_material(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) override;
  218. virtual void render_particle_collider_heightfield(RID p_collider, const Transform3D &p_transform, const PagedArray<RenderGeometryInstance *> &p_instances) override;
  219. virtual void set_scene_pass(uint64_t p_pass) override {
  220. scene_pass = p_pass;
  221. }
  222. _FORCE_INLINE_ uint64_t get_scene_pass() {
  223. return scene_pass;
  224. }
  225. virtual void screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_limit) override;
  226. virtual bool screen_space_roughness_limiter_is_active() const override;
  227. virtual float screen_space_roughness_limiter_get_amount() const;
  228. virtual float screen_space_roughness_limiter_get_limit() const;
  229. virtual void positional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) override;
  230. virtual void directional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) override;
  231. virtual void decals_set_filter(RS::DecalFilter p_filter) override;
  232. virtual void light_projectors_set_filter(RS::LightProjectorFilter p_filter) override;
  233. _FORCE_INLINE_ RS::ShadowQuality shadows_quality_get() const {
  234. return shadows_quality;
  235. }
  236. _FORCE_INLINE_ RS::ShadowQuality directional_shadow_quality_get() const {
  237. return directional_shadow_quality;
  238. }
  239. _FORCE_INLINE_ float shadows_quality_radius_get() const {
  240. return shadows_quality_radius;
  241. }
  242. _FORCE_INLINE_ float directional_shadow_quality_radius_get() const {
  243. return directional_shadow_quality_radius;
  244. }
  245. _FORCE_INLINE_ float *directional_penumbra_shadow_kernel_get() {
  246. return directional_penumbra_shadow_kernel;
  247. }
  248. _FORCE_INLINE_ float *directional_soft_shadow_kernel_get() {
  249. return directional_soft_shadow_kernel;
  250. }
  251. _FORCE_INLINE_ float *penumbra_shadow_kernel_get() {
  252. return penumbra_shadow_kernel;
  253. }
  254. _FORCE_INLINE_ float *soft_shadow_kernel_get() {
  255. return soft_shadow_kernel;
  256. }
  257. _FORCE_INLINE_ int directional_penumbra_shadow_samples_get() const {
  258. return directional_penumbra_shadow_samples;
  259. }
  260. _FORCE_INLINE_ int directional_soft_shadow_samples_get() const {
  261. return directional_soft_shadow_samples;
  262. }
  263. _FORCE_INLINE_ int penumbra_shadow_samples_get() const {
  264. return penumbra_shadow_samples;
  265. }
  266. _FORCE_INLINE_ int soft_shadow_samples_get() const {
  267. return soft_shadow_samples;
  268. }
  269. _FORCE_INLINE_ RS::LightProjectorFilter light_projectors_get_filter() const {
  270. return light_projectors_filter;
  271. }
  272. _FORCE_INLINE_ RS::DecalFilter decals_get_filter() const {
  273. return decals_filter;
  274. }
  275. int get_roughness_layers() const;
  276. bool is_using_radiance_cubemap_array() const;
  277. virtual TypedArray<Image> bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) override;
  278. virtual bool free(RID p_rid) override;
  279. virtual void update() override;
  280. virtual void set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) override;
  281. _FORCE_INLINE_ RS::ViewportDebugDraw get_debug_draw_mode() const {
  282. return debug_draw;
  283. }
  284. virtual void set_time(double p_time, double p_step) override;
  285. virtual void sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) override;
  286. virtual bool is_vrs_supported() const;
  287. virtual bool is_dynamic_gi_supported() const;
  288. virtual bool is_volumetric_supported() const;
  289. virtual uint32_t get_max_elements() const;
  290. void init();
  291. RendererSceneRenderRD();
  292. ~RendererSceneRenderRD();
  293. };
  294. #endif // RENDERER_SCENE_RENDER_RD_H