renderer_scene_render_rd.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 RENDERING_SERVER_SCENE_RENDER_RD_H
  31. #define RENDERING_SERVER_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/renderer_scene_environment_rd.h"
  37. #include "servers/rendering/renderer_rd/renderer_scene_gi_rd.h"
  38. #include "servers/rendering/renderer_rd/renderer_scene_sky_rd.h"
  39. #include "servers/rendering/renderer_rd/renderer_storage_rd.h"
  40. #include "servers/rendering/renderer_rd/shaders/volumetric_fog.glsl.gen.h"
  41. #include "servers/rendering/renderer_scene_render.h"
  42. #include "servers/rendering/rendering_device.h"
  43. class RendererSceneRenderRD : public RendererSceneRender {
  44. friend RendererSceneSkyRD;
  45. friend RendererSceneGIRD;
  46. protected:
  47. RendererStorageRD *storage;
  48. double time;
  49. double time_step = 0;
  50. struct RenderBufferData {
  51. virtual void configure(RID p_color_buffer, RID p_depth_buffer, int p_width, int p_height, RS::ViewportMSAA p_msaa) = 0;
  52. virtual ~RenderBufferData() {}
  53. };
  54. virtual RenderBufferData *_create_render_buffer_data() = 0;
  55. void _setup_lights(const PagedArray<RID> &p_lights, const Transform &p_camera_transform, RID p_shadow_atlas, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_positional_light_count);
  56. void _setup_decals(const PagedArray<RID> &p_decals, const Transform &p_camera_inverse_xform);
  57. void _setup_reflections(const PagedArray<RID> &p_reflections, const Transform &p_camera_inverse_transform, RID p_environment);
  58. virtual void _render_scene(RID p_render_buffer, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, const PagedArray<GeometryInstance *> &p_instances, const PagedArray<RID> &p_gi_probes, const PagedArray<RID> &p_lightmaps, RID p_environment, RID p_cluster_buffer, uint32_t p_cluster_size, uint32_t p_cluster_max_elements, RID p_camera_effects, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass, const Color &p_default_color, float p_screen_lod_threshold) = 0;
  59. virtual void _render_shadow_begin() = 0;
  60. virtual void _render_shadow_append(RID p_framebuffer, const PagedArray<GeometryInstance *> &p_instances, const CameraMatrix &p_projection, const Transform &p_transform, float p_zfar, float p_bias, float p_normal_bias, bool p_use_dp, bool p_use_dp_flip, bool p_use_pancake, const Plane &p_camera_plane = Plane(), float p_lod_distance_multiplier = 0.0, float p_screen_lod_threshold = 0.0, const Rect2i &p_rect = Rect2i(), bool p_flip_y = false, bool p_clear_region = true, bool p_begin = true, bool p_end = true) = 0;
  61. virtual void _render_shadow_process() = 0;
  62. virtual void _render_shadow_end(uint32_t p_barrier = RD::BARRIER_MASK_ALL) = 0;
  63. virtual void _render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, const PagedArray<GeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) = 0;
  64. virtual void _render_uv2(const PagedArray<GeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) = 0;
  65. virtual void _render_sdfgi(RID p_render_buffers, const Vector3i &p_from, const Vector3i &p_size, const AABB &p_bounds, const PagedArray<GeometryInstance *> &p_instances, const RID &p_albedo_texture, const RID &p_emission_texture, const RID &p_emission_aniso_texture, const RID &p_geom_facing_texture) = 0;
  66. virtual void _render_particle_collider_heightfield(RID p_fb, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, const PagedArray<GeometryInstance *> &p_instances) = 0;
  67. void _debug_sdfgi_probes(RID p_render_buffers, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform);
  68. RenderBufferData *render_buffers_get_data(RID p_render_buffers);
  69. virtual void _base_uniforms_changed() = 0;
  70. virtual RID _render_buffers_get_normal_texture(RID p_render_buffers) = 0;
  71. void _process_ssao(RID p_render_buffers, RID p_environment, RID p_normal_buffer, const CameraMatrix &p_projection);
  72. void _process_ssr(RID p_render_buffers, RID p_dest_framebuffer, RID p_normal_buffer, RID p_specular_buffer, RID p_metallic, const Color &p_metallic_mask, RID p_environment, const CameraMatrix &p_projection, bool p_use_additive);
  73. void _process_sss(RID p_render_buffers, const CameraMatrix &p_camera);
  74. bool _needs_post_prepass_render(bool p_use_gi);
  75. void _post_prepass_render(bool p_use_gi);
  76. void _pre_resolve_render(bool p_use_gi);
  77. void _pre_opaque_render(bool p_use_ssao, bool p_use_gi, RID p_normal_roughness_buffer, RID p_gi_probe_buffer);
  78. uint32_t _get_render_state_directional_light_count() const;
  79. // needed for a single argument calls (material and uv2)
  80. PagedArrayPool<GeometryInstance *> cull_argument_pool;
  81. PagedArray<GeometryInstance *> cull_argument; //need this to exist
  82. RendererSceneGIRD gi;
  83. RendererSceneSkyRD sky;
  84. RendererSceneEnvironmentRD *get_environment(RID p_environment) {
  85. if (p_environment.is_valid()) {
  86. return environment_owner.getornull(p_environment);
  87. } else {
  88. return nullptr;
  89. }
  90. }
  91. private:
  92. RS::ViewportDebugDraw debug_draw = RS::VIEWPORT_DEBUG_DRAW_DISABLED;
  93. static RendererSceneRenderRD *singleton;
  94. /* REFLECTION ATLAS */
  95. struct ReflectionAtlas {
  96. int count = 0;
  97. int size = 0;
  98. RID reflection;
  99. RID depth_buffer;
  100. RID depth_fb;
  101. struct Reflection {
  102. RID owner;
  103. RendererSceneSkyRD::ReflectionData data;
  104. RID fbs[6];
  105. };
  106. Vector<Reflection> reflections;
  107. ClusterBuilderRD *cluster_builder = nullptr;
  108. };
  109. mutable RID_Owner<ReflectionAtlas> reflection_atlas_owner;
  110. /* REFLECTION PROBE INSTANCE */
  111. struct ReflectionProbeInstance {
  112. RID probe;
  113. int atlas_index = -1;
  114. RID atlas;
  115. bool dirty = true;
  116. bool rendering = false;
  117. int processing_layer = 1;
  118. int processing_side = 0;
  119. uint32_t render_step = 0;
  120. uint64_t last_pass = 0;
  121. uint32_t render_index = 0;
  122. uint32_t cull_mask = 0;
  123. Transform transform;
  124. };
  125. mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner;
  126. /* DECAL INSTANCE */
  127. struct DecalInstance {
  128. RID decal;
  129. Transform transform;
  130. uint32_t render_index;
  131. uint32_t cull_mask;
  132. };
  133. mutable RID_Owner<DecalInstance> decal_instance_owner;
  134. /* LIGHTMAP INSTANCE */
  135. struct LightmapInstance {
  136. RID lightmap;
  137. Transform transform;
  138. };
  139. mutable RID_Owner<LightmapInstance> lightmap_instance_owner;
  140. /* SHADOW ATLAS */
  141. struct ShadowShrinkStage {
  142. RID texture;
  143. RID filter_texture;
  144. uint32_t size;
  145. };
  146. struct ShadowAtlas {
  147. enum {
  148. QUADRANT_SHIFT = 27,
  149. SHADOW_INDEX_MASK = (1 << QUADRANT_SHIFT) - 1,
  150. SHADOW_INVALID = 0xFFFFFFFF
  151. };
  152. struct Quadrant {
  153. uint32_t subdivision;
  154. struct Shadow {
  155. RID owner;
  156. uint64_t version;
  157. uint64_t fog_version; // used for fog
  158. uint64_t alloc_tick;
  159. Shadow() {
  160. version = 0;
  161. fog_version = 0;
  162. alloc_tick = 0;
  163. }
  164. };
  165. Vector<Shadow> shadows;
  166. Quadrant() {
  167. subdivision = 0; //not in use
  168. }
  169. } quadrants[4];
  170. int size_order[4] = { 0, 1, 2, 3 };
  171. uint32_t smallest_subdiv = 0;
  172. int size = 0;
  173. bool use_16_bits = false;
  174. RID depth;
  175. RID fb; //for copying
  176. Map<RID, uint32_t> shadow_owners;
  177. };
  178. RID_Owner<ShadowAtlas> shadow_atlas_owner;
  179. void _update_shadow_atlas(ShadowAtlas *shadow_atlas);
  180. bool _shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow);
  181. RS::ShadowQuality shadows_quality = RS::SHADOW_QUALITY_MAX; //So it always updates when first set
  182. RS::ShadowQuality directional_shadow_quality = RS::SHADOW_QUALITY_MAX;
  183. float shadows_quality_radius = 1.0;
  184. float directional_shadow_quality_radius = 1.0;
  185. float *directional_penumbra_shadow_kernel;
  186. float *directional_soft_shadow_kernel;
  187. float *penumbra_shadow_kernel;
  188. float *soft_shadow_kernel;
  189. int directional_penumbra_shadow_samples = 0;
  190. int directional_soft_shadow_samples = 0;
  191. int penumbra_shadow_samples = 0;
  192. int soft_shadow_samples = 0;
  193. /* DIRECTIONAL SHADOW */
  194. struct DirectionalShadow {
  195. RID depth;
  196. RID fb; //when renderign direct
  197. int light_count = 0;
  198. int size = 0;
  199. bool use_16_bits = false;
  200. int current_light = 0;
  201. } directional_shadow;
  202. void _update_directional_shadow_atlas();
  203. /* SHADOW CUBEMAPS */
  204. struct ShadowCubemap {
  205. RID cubemap;
  206. RID side_fb[6];
  207. };
  208. Map<int, ShadowCubemap> shadow_cubemaps;
  209. ShadowCubemap *_get_shadow_cubemap(int p_size);
  210. void _create_shadow_cubemaps();
  211. /* LIGHT INSTANCE */
  212. struct LightInstance {
  213. struct ShadowTransform {
  214. CameraMatrix camera;
  215. Transform transform;
  216. float farplane;
  217. float split;
  218. float bias_scale;
  219. float shadow_texel_size;
  220. float range_begin;
  221. Rect2 atlas_rect;
  222. Vector2 uv_scale;
  223. };
  224. RS::LightType light_type = RS::LIGHT_DIRECTIONAL;
  225. ShadowTransform shadow_transform[6];
  226. AABB aabb;
  227. RID self;
  228. RID light;
  229. Transform transform;
  230. Vector3 light_vector;
  231. Vector3 spot_vector;
  232. float linear_att = 0.0;
  233. uint64_t shadow_pass = 0;
  234. uint64_t last_scene_pass = 0;
  235. uint64_t last_scene_shadow_pass = 0;
  236. uint64_t last_pass = 0;
  237. uint32_t light_index = 0;
  238. uint32_t cull_mask = 0;
  239. uint32_t light_directional_index = 0;
  240. uint32_t current_shadow_atlas_key = 0;
  241. Vector2 dp;
  242. Rect2 directional_rect;
  243. Set<RID> shadow_atlases; //shadow atlases where this light is registered
  244. LightInstance() {}
  245. };
  246. mutable RID_Owner<LightInstance> light_instance_owner;
  247. /* ENVIRONMENT */
  248. RS::EnvironmentSSAOQuality ssao_quality = RS::ENV_SSAO_QUALITY_MEDIUM;
  249. bool ssao_half_size = false;
  250. bool ssao_using_half_size = false;
  251. float ssao_adaptive_target = 0.5;
  252. int ssao_blur_passes = 2;
  253. float ssao_fadeout_from = 50.0;
  254. float ssao_fadeout_to = 300.0;
  255. bool glow_bicubic_upscale = false;
  256. bool glow_high_quality = false;
  257. RS::EnvironmentSSRRoughnessQuality ssr_roughness_quality = RS::ENV_SSR_ROUGNESS_QUALITY_LOW;
  258. mutable RID_Owner<RendererSceneEnvironmentRD, true> environment_owner;
  259. /* CAMERA EFFECTS */
  260. struct CameraEffects {
  261. bool dof_blur_far_enabled = false;
  262. float dof_blur_far_distance = 10;
  263. float dof_blur_far_transition = 5;
  264. bool dof_blur_near_enabled = false;
  265. float dof_blur_near_distance = 2;
  266. float dof_blur_near_transition = 1;
  267. float dof_blur_amount = 0.1;
  268. bool override_exposure_enabled = false;
  269. float override_exposure = 1;
  270. };
  271. RS::DOFBlurQuality dof_blur_quality = RS::DOF_BLUR_QUALITY_MEDIUM;
  272. RS::DOFBokehShape dof_blur_bokeh_shape = RS::DOF_BOKEH_HEXAGON;
  273. bool dof_blur_use_jitter = false;
  274. RS::SubSurfaceScatteringQuality sss_quality = RS::SUB_SURFACE_SCATTERING_QUALITY_MEDIUM;
  275. float sss_scale = 0.05;
  276. float sss_depth_scale = 0.01;
  277. mutable RID_Owner<CameraEffects, true> camera_effects_owner;
  278. /* RENDER BUFFERS */
  279. ClusterBuilderSharedDataRD cluster_builder_shared;
  280. ClusterBuilderRD *current_cluster_builder = nullptr;
  281. struct VolumetricFog;
  282. struct RenderBuffers {
  283. RenderBufferData *data = nullptr;
  284. int width = 0, height = 0;
  285. RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED;
  286. RS::ViewportScreenSpaceAA screen_space_aa = RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED;
  287. bool use_debanding = false;
  288. RID render_target;
  289. uint64_t auto_exposure_version = 1;
  290. RID texture; //main texture for rendering to, must be filled after done rendering
  291. RID depth_texture; //main depth texture
  292. RendererSceneGIRD::SDFGI *sdfgi = nullptr;
  293. VolumetricFog *volumetric_fog = nullptr;
  294. RendererSceneGIRD::RenderBuffersGI gi;
  295. ClusterBuilderRD *cluster_builder = nullptr;
  296. //built-in textures used for ping pong image processing and blurring
  297. struct Blur {
  298. RID texture;
  299. struct Mipmap {
  300. RID texture;
  301. int width;
  302. int height;
  303. };
  304. Vector<Mipmap> mipmaps;
  305. };
  306. Blur blur[2]; //the second one starts from the first mipmap
  307. struct Luminance {
  308. Vector<RID> reduce;
  309. RID current;
  310. } luminance;
  311. struct SSAO {
  312. RID depth;
  313. Vector<RID> depth_slices;
  314. RID ao_deinterleaved;
  315. Vector<RID> ao_deinterleaved_slices;
  316. RID ao_pong;
  317. Vector<RID> ao_pong_slices;
  318. RID ao_final;
  319. RID importance_map[2];
  320. } ssao;
  321. struct SSR {
  322. RID normal_scaled;
  323. RID depth_scaled;
  324. RID blur_radius[2];
  325. } ssr;
  326. RID ambient_buffer;
  327. RID reflection_buffer;
  328. };
  329. /* GI */
  330. bool screen_space_roughness_limiter = false;
  331. float screen_space_roughness_limiter_amount = 0.25;
  332. float screen_space_roughness_limiter_limit = 0.18;
  333. mutable RID_Owner<RenderBuffers> render_buffers_owner;
  334. void _free_render_buffer_data(RenderBuffers *rb);
  335. void _allocate_blur_textures(RenderBuffers *rb);
  336. void _allocate_luminance_textures(RenderBuffers *rb);
  337. void _render_buffers_debug_draw(RID p_render_buffers, RID p_shadow_atlas, RID p_occlusion_buffer);
  338. void _render_buffers_post_process_and_tonemap(RID p_render_buffers, RID p_environment, RID p_camera_effects, const CameraMatrix &p_projection);
  339. /* Cluster */
  340. struct Cluster {
  341. /* Scene State UBO */
  342. // !BAS! Most data here is not just used by our clustering logic but also by other lighting implementations. Maybe rename this struct to something more appropriate
  343. enum {
  344. REFLECTION_AMBIENT_DISABLED = 0,
  345. REFLECTION_AMBIENT_ENVIRONMENT = 1,
  346. REFLECTION_AMBIENT_COLOR = 2,
  347. };
  348. struct ReflectionData {
  349. float box_extents[3];
  350. float index;
  351. float box_offset[3];
  352. uint32_t mask;
  353. float ambient[3]; // ambient color,
  354. float intensity;
  355. uint32_t exterior;
  356. uint32_t box_project;
  357. uint32_t ambient_mode;
  358. uint32_t pad;
  359. float local_matrix[16]; // up to here for spot and omni, rest is for directional
  360. };
  361. struct LightData {
  362. float position[3];
  363. float inv_radius;
  364. float direction[3];
  365. float size;
  366. float color[3];
  367. float attenuation;
  368. float inv_spot_attenuation;
  369. float cos_spot_angle;
  370. float specular_amount;
  371. uint32_t shadow_enabled;
  372. float atlas_rect[4]; // in omni, used for atlas uv, in spot, used for projector uv
  373. float shadow_matrix[16];
  374. float shadow_bias;
  375. float shadow_normal_bias;
  376. float transmittance_bias;
  377. float soft_shadow_size;
  378. float soft_shadow_scale;
  379. uint32_t mask;
  380. float shadow_volumetric_fog_fade;
  381. uint32_t pad;
  382. float projector_rect[4];
  383. };
  384. struct DirectionalLightData {
  385. float direction[3];
  386. float energy;
  387. float color[3];
  388. float size;
  389. float specular;
  390. uint32_t mask;
  391. float softshadow_angle;
  392. float soft_shadow_scale;
  393. uint32_t blend_splits;
  394. uint32_t shadow_enabled;
  395. float fade_from;
  396. float fade_to;
  397. uint32_t pad[3];
  398. float shadow_volumetric_fog_fade;
  399. float shadow_bias[4];
  400. float shadow_normal_bias[4];
  401. float shadow_transmittance_bias[4];
  402. float shadow_z_range[4];
  403. float shadow_range_begin[4];
  404. float shadow_split_offsets[4];
  405. float shadow_matrices[4][16];
  406. float shadow_color1[4];
  407. float shadow_color2[4];
  408. float shadow_color3[4];
  409. float shadow_color4[4];
  410. float uv_scale1[2];
  411. float uv_scale2[2];
  412. float uv_scale3[2];
  413. float uv_scale4[2];
  414. };
  415. struct DecalData {
  416. float xform[16];
  417. float inv_extents[3];
  418. float albedo_mix;
  419. float albedo_rect[4];
  420. float normal_rect[4];
  421. float orm_rect[4];
  422. float emission_rect[4];
  423. float modulate[4];
  424. float emission_energy;
  425. uint32_t mask;
  426. float upper_fade;
  427. float lower_fade;
  428. float normal_xform[12];
  429. float normal[3];
  430. float normal_fade;
  431. };
  432. template <class T>
  433. struct InstanceSort {
  434. float depth;
  435. T *instance;
  436. bool operator<(const InstanceSort &p_sort) const {
  437. return depth < p_sort.depth;
  438. }
  439. };
  440. ReflectionData *reflections;
  441. InstanceSort<ReflectionProbeInstance> *reflection_sort;
  442. uint32_t max_reflections;
  443. RID reflection_buffer;
  444. uint32_t max_reflection_probes_per_instance;
  445. uint32_t reflection_count = 0;
  446. DecalData *decals;
  447. InstanceSort<DecalInstance> *decal_sort;
  448. uint32_t max_decals;
  449. RID decal_buffer;
  450. uint32_t decal_count;
  451. LightData *omni_lights;
  452. LightData *spot_lights;
  453. InstanceSort<LightInstance> *omni_light_sort;
  454. InstanceSort<LightInstance> *spot_light_sort;
  455. uint32_t max_lights;
  456. RID omni_light_buffer;
  457. RID spot_light_buffer;
  458. uint32_t omni_light_count = 0;
  459. uint32_t spot_light_count = 0;
  460. DirectionalLightData *directional_lights;
  461. uint32_t max_directional_lights;
  462. RID directional_light_buffer;
  463. } cluster;
  464. struct RenderState {
  465. RID render_buffers;
  466. Transform cam_transform;
  467. CameraMatrix cam_projection;
  468. bool cam_ortogonal = false;
  469. const PagedArray<GeometryInstance *> *instances = nullptr;
  470. const PagedArray<RID> *lights = nullptr;
  471. const PagedArray<RID> *reflection_probes = nullptr;
  472. const PagedArray<RID> *gi_probes = nullptr;
  473. const PagedArray<RID> *decals = nullptr;
  474. const PagedArray<RID> *lightmaps = nullptr;
  475. RID environment;
  476. RID camera_effects;
  477. RID shadow_atlas;
  478. RID reflection_atlas;
  479. RID reflection_probe;
  480. int reflection_probe_pass = 0;
  481. float screen_lod_threshold = 0.0;
  482. const RenderShadowData *render_shadows = nullptr;
  483. int render_shadow_count = 0;
  484. const RenderSDFGIData *render_sdfgi_regions = nullptr;
  485. int render_sdfgi_region_count = 0;
  486. const RenderSDFGIUpdateData *sdfgi_update_data = nullptr;
  487. uint32_t directional_light_count = 0;
  488. uint32_t gi_probe_count = 0;
  489. LocalVector<int> cube_shadows;
  490. LocalVector<int> shadows;
  491. LocalVector<int> directional_shadows;
  492. bool depth_prepass_used;
  493. } render_state;
  494. struct VolumetricFog {
  495. enum {
  496. MAX_TEMPORAL_FRAMES = 16
  497. };
  498. uint32_t width = 0;
  499. uint32_t height = 0;
  500. uint32_t depth = 0;
  501. float length;
  502. float spread;
  503. RID light_density_map;
  504. RID prev_light_density_map;
  505. RID fog_map;
  506. RID uniform_set;
  507. RID uniform_set2;
  508. RID sdfgi_uniform_set;
  509. RID sky_uniform_set;
  510. int last_shadow_filter = -1;
  511. Transform prev_cam_transform;
  512. };
  513. enum {
  514. VOLUMETRIC_FOG_SHADER_DENSITY,
  515. VOLUMETRIC_FOG_SHADER_DENSITY_WITH_SDFGI,
  516. VOLUMETRIC_FOG_SHADER_FILTER,
  517. VOLUMETRIC_FOG_SHADER_FOG,
  518. VOLUMETRIC_FOG_SHADER_MAX,
  519. };
  520. struct VolumetricFogShader {
  521. struct ParamsUBO {
  522. float fog_frustum_size_begin[2];
  523. float fog_frustum_size_end[2];
  524. float fog_frustum_end;
  525. float z_near;
  526. float z_far;
  527. uint32_t filter_axis;
  528. int32_t fog_volume_size[3];
  529. uint32_t directional_light_count;
  530. float light_energy[3];
  531. float base_density;
  532. float detail_spread;
  533. float gi_inject;
  534. uint32_t max_gi_probes;
  535. uint32_t cluster_type_size;
  536. float screen_size[2];
  537. uint32_t cluster_shift;
  538. uint32_t cluster_width;
  539. uint32_t max_cluster_element_count_div_32;
  540. uint32_t use_temporal_reprojection;
  541. uint32_t temporal_frame;
  542. float temporal_blend;
  543. float cam_rotation[12];
  544. float to_prev_view[16];
  545. };
  546. VolumetricFogShaderRD shader;
  547. RID params_ubo;
  548. RID shader_version;
  549. RID pipelines[VOLUMETRIC_FOG_SHADER_MAX];
  550. } volumetric_fog;
  551. uint32_t volumetric_fog_depth = 128;
  552. uint32_t volumetric_fog_size = 128;
  553. bool volumetric_fog_filter_active = true;
  554. void _volumetric_fog_erase(RenderBuffers *rb);
  555. void _update_volumetric_fog(RID p_render_buffers, RID p_environment, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, RID p_shadow_atlas, int p_directional_light_count, bool p_use_directional_shadows, int p_positional_light_count, int p_gi_probe_count);
  556. RID shadow_sampler;
  557. uint64_t scene_pass = 0;
  558. uint64_t shadow_atlas_realloc_tolerance_msec = 500;
  559. /* !BAS! is this used anywhere?
  560. struct SDFGICosineNeighbour {
  561. uint32_t neighbour;
  562. float weight;
  563. };
  564. */
  565. uint32_t max_cluster_elements = 512;
  566. void _render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<GeometryInstance *> &p_instances, const Plane &p_camera_plane = Plane(), float p_lod_distance_multiplier = 0, float p_screen_lod_threshold = 0.0, bool p_open_pass = true, bool p_close_pass = true, bool p_clear_region = true);
  567. public:
  568. virtual Transform geometry_instance_get_transform(GeometryInstance *p_instance) = 0;
  569. virtual AABB geometry_instance_get_aabb(GeometryInstance *p_instance) = 0;
  570. /* SHADOW ATLAS API */
  571. RID shadow_atlas_create();
  572. void shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits = false);
  573. void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision);
  574. bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version);
  575. _FORCE_INLINE_ bool shadow_atlas_owns_light_instance(RID p_atlas, RID p_light_intance) {
  576. ShadowAtlas *atlas = shadow_atlas_owner.getornull(p_atlas);
  577. ERR_FAIL_COND_V(!atlas, false);
  578. return atlas->shadow_owners.has(p_light_intance);
  579. }
  580. _FORCE_INLINE_ RID shadow_atlas_get_texture(RID p_atlas) {
  581. ShadowAtlas *atlas = shadow_atlas_owner.getornull(p_atlas);
  582. ERR_FAIL_COND_V(!atlas, RID());
  583. return atlas->depth;
  584. }
  585. _FORCE_INLINE_ Size2i shadow_atlas_get_size(RID p_atlas) {
  586. ShadowAtlas *atlas = shadow_atlas_owner.getornull(p_atlas);
  587. ERR_FAIL_COND_V(!atlas, Size2i());
  588. return Size2(atlas->size, atlas->size);
  589. }
  590. void directional_shadow_atlas_set_size(int p_size, bool p_16_bits = false);
  591. int get_directional_light_shadow_size(RID p_light_intance);
  592. void set_directional_shadow_count(int p_count);
  593. _FORCE_INLINE_ RID directional_shadow_get_texture() {
  594. return directional_shadow.depth;
  595. }
  596. _FORCE_INLINE_ Size2i directional_shadow_get_size() {
  597. return Size2i(directional_shadow.size, directional_shadow.size);
  598. }
  599. /* SDFGI UPDATE */
  600. virtual void sdfgi_update(RID p_render_buffers, RID p_environment, const Vector3 &p_world_position);
  601. virtual int sdfgi_get_pending_region_count(RID p_render_buffers) const;
  602. virtual AABB sdfgi_get_pending_region_bounds(RID p_render_buffers, int p_region) const;
  603. virtual uint32_t sdfgi_get_pending_region_cascade(RID p_render_buffers, int p_region) const;
  604. RID sdfgi_get_ubo() const { return gi.sdfgi_ubo; }
  605. /* SKY API */
  606. virtual RID sky_allocate();
  607. virtual void sky_initialize(RID p_rid);
  608. void sky_set_radiance_size(RID p_sky, int p_radiance_size);
  609. void sky_set_mode(RID p_sky, RS::SkyMode p_mode);
  610. void sky_set_material(RID p_sky, RID p_material);
  611. Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size);
  612. /* ENVIRONMENT API */
  613. virtual RID environment_allocate();
  614. virtual void environment_initialize(RID p_rid);
  615. void environment_set_background(RID p_env, RS::EnvironmentBG p_bg);
  616. void environment_set_sky(RID p_env, RID p_sky);
  617. void environment_set_sky_custom_fov(RID p_env, float p_scale);
  618. void environment_set_sky_orientation(RID p_env, const Basis &p_orientation);
  619. void environment_set_bg_color(RID p_env, const Color &p_color);
  620. void environment_set_bg_energy(RID p_env, float p_energy);
  621. void environment_set_canvas_max_layer(RID p_env, int p_max_layer);
  622. void environment_set_ambient_light(RID p_env, const Color &p_color, RS::EnvironmentAmbientSource p_ambient = RS::ENV_AMBIENT_SOURCE_BG, float p_energy = 1.0, float p_sky_contribution = 0.0, RS::EnvironmentReflectionSource p_reflection_source = RS::ENV_REFLECTION_SOURCE_BG, const Color &p_ao_color = Color());
  623. RS::EnvironmentBG environment_get_background(RID p_env) const;
  624. RID environment_get_sky(RID p_env) const;
  625. float environment_get_sky_custom_fov(RID p_env) const;
  626. Basis environment_get_sky_orientation(RID p_env) const;
  627. Color environment_get_bg_color(RID p_env) const;
  628. float environment_get_bg_energy(RID p_env) const;
  629. int environment_get_canvas_max_layer(RID p_env) const;
  630. Color environment_get_ambient_light_color(RID p_env) const;
  631. RS::EnvironmentAmbientSource environment_get_ambient_source(RID p_env) const;
  632. float environment_get_ambient_light_energy(RID p_env) const;
  633. float environment_get_ambient_sky_contribution(RID p_env) const;
  634. RS::EnvironmentReflectionSource environment_get_reflection_source(RID p_env) const;
  635. Color environment_get_ao_color(RID p_env) const;
  636. bool is_environment(RID p_env) const;
  637. void environment_set_glow(RID p_env, bool p_enable, Vector<float> p_levels, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, RS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap);
  638. void environment_glow_set_use_bicubic_upscale(bool p_enable);
  639. void environment_glow_set_use_high_quality(bool p_enable);
  640. void environment_set_fog(RID p_env, bool p_enable, const Color &p_light_color, float p_light_energy, float p_sun_scatter, float p_density, float p_height, float p_height_density, float p_aerial_perspective);
  641. bool environment_is_fog_enabled(RID p_env) const;
  642. Color environment_get_fog_light_color(RID p_env) const;
  643. float environment_get_fog_light_energy(RID p_env) const;
  644. float environment_get_fog_sun_scatter(RID p_env) const;
  645. float environment_get_fog_density(RID p_env) const;
  646. float environment_get_fog_height(RID p_env) const;
  647. float environment_get_fog_height_density(RID p_env) const;
  648. float environment_get_fog_aerial_perspective(RID p_env) const;
  649. void environment_set_volumetric_fog(RID p_env, bool p_enable, float p_density, const Color &p_light, float p_light_energy, float p_length, float p_detail_spread, float p_gi_inject, bool p_temporal_reprojection, float p_temporal_reprojection_amount);
  650. virtual void environment_set_volumetric_fog_volume_size(int p_size, int p_depth);
  651. virtual void environment_set_volumetric_fog_filter_active(bool p_enable);
  652. void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance);
  653. void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_power, float p_detail, float p_horizon, float p_sharpness, float p_light_affect, float p_ao_channel_affect);
  654. void environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to);
  655. bool environment_is_ssao_enabled(RID p_env) const;
  656. float environment_get_ssao_ao_affect(RID p_env) const;
  657. float environment_get_ssao_light_affect(RID p_env) const;
  658. bool environment_is_ssr_enabled(RID p_env) const;
  659. bool environment_is_sdfgi_enabled(RID p_env) const;
  660. virtual void environment_set_sdfgi(RID p_env, bool p_enable, RS::EnvironmentSDFGICascades p_cascades, float p_min_cell_size, RS::EnvironmentSDFGIYScale p_y_scale, bool p_use_occlusion, float p_bounce_feedback, bool p_read_sky, float p_energy, float p_normal_bias, float p_probe_bias);
  661. virtual void environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count);
  662. virtual void environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames);
  663. virtual void environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update);
  664. void environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality);
  665. RS::EnvironmentSSRRoughnessQuality environment_get_ssr_roughness_quality() const;
  666. void environment_set_tonemap(RID p_env, RS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale);
  667. void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, bool p_use_1d_color_correction, RID p_color_correction);
  668. virtual Ref<Image> environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size);
  669. virtual RID camera_effects_allocate();
  670. virtual void camera_effects_initialize(RID p_rid);
  671. virtual void camera_effects_set_dof_blur_quality(RS::DOFBlurQuality p_quality, bool p_use_jitter);
  672. virtual void camera_effects_set_dof_blur_bokeh_shape(RS::DOFBokehShape p_shape);
  673. virtual void camera_effects_set_dof_blur(RID p_camera_effects, bool p_far_enable, float p_far_distance, float p_far_transition, bool p_near_enable, float p_near_distance, float p_near_transition, float p_amount);
  674. virtual void camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure);
  675. RID light_instance_create(RID p_light);
  676. void light_instance_set_transform(RID p_light_instance, const Transform &p_transform);
  677. void light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb);
  678. void light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale = 1.0, float p_range_begin = 0, const Vector2 &p_uv_scale = Vector2());
  679. void light_instance_mark_visible(RID p_light_instance);
  680. _FORCE_INLINE_ RID light_instance_get_base_light(RID p_light_instance) {
  681. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  682. return li->light;
  683. }
  684. _FORCE_INLINE_ Transform light_instance_get_base_transform(RID p_light_instance) {
  685. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  686. return li->transform;
  687. }
  688. _FORCE_INLINE_ Rect2 light_instance_get_shadow_atlas_rect(RID p_light_instance, RID p_shadow_atlas) {
  689. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  690. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  691. uint32_t key = shadow_atlas->shadow_owners[li->self];
  692. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  693. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  694. ERR_FAIL_COND_V(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size(), Rect2());
  695. uint32_t atlas_size = shadow_atlas->size;
  696. uint32_t quadrant_size = atlas_size >> 1;
  697. uint32_t x = (quadrant & 1) * quadrant_size;
  698. uint32_t y = (quadrant >> 1) * quadrant_size;
  699. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  700. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  701. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  702. uint32_t width = shadow_size;
  703. uint32_t height = shadow_size;
  704. return Rect2(x / float(shadow_atlas->size), y / float(shadow_atlas->size), width / float(shadow_atlas->size), height / float(shadow_atlas->size));
  705. }
  706. _FORCE_INLINE_ CameraMatrix light_instance_get_shadow_camera(RID p_light_instance, int p_index) {
  707. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  708. return li->shadow_transform[p_index].camera;
  709. }
  710. _FORCE_INLINE_ float light_instance_get_shadow_texel_size(RID p_light_instance, RID p_shadow_atlas) {
  711. #ifdef DEBUG_ENABLED
  712. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  713. ERR_FAIL_COND_V(!li->shadow_atlases.has(p_shadow_atlas), 0);
  714. #endif
  715. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  716. ERR_FAIL_COND_V(!shadow_atlas, 0);
  717. #ifdef DEBUG_ENABLED
  718. ERR_FAIL_COND_V(!shadow_atlas->shadow_owners.has(p_light_instance), 0);
  719. #endif
  720. uint32_t key = shadow_atlas->shadow_owners[p_light_instance];
  721. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  722. uint32_t quadrant_size = shadow_atlas->size >> 1;
  723. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  724. return float(1.0) / shadow_size;
  725. }
  726. _FORCE_INLINE_ Transform
  727. light_instance_get_shadow_transform(RID p_light_instance, int p_index) {
  728. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  729. return li->shadow_transform[p_index].transform;
  730. }
  731. _FORCE_INLINE_ float light_instance_get_shadow_bias_scale(RID p_light_instance, int p_index) {
  732. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  733. return li->shadow_transform[p_index].bias_scale;
  734. }
  735. _FORCE_INLINE_ float light_instance_get_shadow_range(RID p_light_instance, int p_index) {
  736. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  737. return li->shadow_transform[p_index].farplane;
  738. }
  739. _FORCE_INLINE_ float light_instance_get_shadow_range_begin(RID p_light_instance, int p_index) {
  740. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  741. return li->shadow_transform[p_index].range_begin;
  742. }
  743. _FORCE_INLINE_ Vector2 light_instance_get_shadow_uv_scale(RID p_light_instance, int p_index) {
  744. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  745. return li->shadow_transform[p_index].uv_scale;
  746. }
  747. _FORCE_INLINE_ Rect2 light_instance_get_directional_shadow_atlas_rect(RID p_light_instance, int p_index) {
  748. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  749. return li->shadow_transform[p_index].atlas_rect;
  750. }
  751. _FORCE_INLINE_ float light_instance_get_directional_shadow_split(RID p_light_instance, int p_index) {
  752. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  753. return li->shadow_transform[p_index].split;
  754. }
  755. _FORCE_INLINE_ float light_instance_get_directional_shadow_texel_size(RID p_light_instance, int p_index) {
  756. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  757. return li->shadow_transform[p_index].shadow_texel_size;
  758. }
  759. _FORCE_INLINE_ void light_instance_set_render_pass(RID p_light_instance, uint64_t p_pass) {
  760. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  761. li->last_pass = p_pass;
  762. }
  763. _FORCE_INLINE_ uint64_t light_instance_get_render_pass(RID p_light_instance) {
  764. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  765. return li->last_pass;
  766. }
  767. _FORCE_INLINE_ void light_instance_set_index(RID p_light_instance, uint32_t p_index) {
  768. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  769. li->light_index = p_index;
  770. }
  771. _FORCE_INLINE_ uint32_t light_instance_get_index(RID p_light_instance) {
  772. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  773. return li->light_index;
  774. }
  775. _FORCE_INLINE_ RS::LightType light_instance_get_type(RID p_light_instance) {
  776. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  777. return li->light_type;
  778. }
  779. virtual RID reflection_atlas_create();
  780. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count);
  781. virtual int reflection_atlas_get_size(RID p_ref_atlas) const;
  782. _FORCE_INLINE_ RID reflection_atlas_get_texture(RID p_ref_atlas) {
  783. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(p_ref_atlas);
  784. ERR_FAIL_COND_V(!atlas, RID());
  785. return atlas->reflection;
  786. }
  787. virtual RID reflection_probe_instance_create(RID p_probe);
  788. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform);
  789. virtual void reflection_probe_release_atlas_index(RID p_instance);
  790. virtual bool reflection_probe_instance_needs_redraw(RID p_instance);
  791. virtual bool reflection_probe_instance_has_reflection(RID p_instance);
  792. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas);
  793. virtual bool reflection_probe_instance_postprocess_step(RID p_instance);
  794. uint32_t reflection_probe_instance_get_resolution(RID p_instance);
  795. RID reflection_probe_instance_get_framebuffer(RID p_instance, int p_index);
  796. RID reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index);
  797. _FORCE_INLINE_ RID reflection_probe_instance_get_probe(RID p_instance) {
  798. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  799. ERR_FAIL_COND_V(!rpi, RID());
  800. return rpi->probe;
  801. }
  802. _FORCE_INLINE_ void reflection_probe_instance_set_render_index(RID p_instance, uint32_t p_render_index) {
  803. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  804. ERR_FAIL_COND(!rpi);
  805. rpi->render_index = p_render_index;
  806. }
  807. _FORCE_INLINE_ uint32_t reflection_probe_instance_get_render_index(RID p_instance) {
  808. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  809. ERR_FAIL_COND_V(!rpi, 0);
  810. return rpi->render_index;
  811. }
  812. _FORCE_INLINE_ void reflection_probe_instance_set_render_pass(RID p_instance, uint32_t p_render_pass) {
  813. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  814. ERR_FAIL_COND(!rpi);
  815. rpi->last_pass = p_render_pass;
  816. }
  817. _FORCE_INLINE_ uint32_t reflection_probe_instance_get_render_pass(RID p_instance) {
  818. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  819. ERR_FAIL_COND_V(!rpi, 0);
  820. return rpi->last_pass;
  821. }
  822. _FORCE_INLINE_ Transform reflection_probe_instance_get_transform(RID p_instance) {
  823. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  824. ERR_FAIL_COND_V(!rpi, Transform());
  825. return rpi->transform;
  826. }
  827. _FORCE_INLINE_ int reflection_probe_instance_get_atlas_index(RID p_instance) {
  828. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  829. ERR_FAIL_COND_V(!rpi, -1);
  830. return rpi->atlas_index;
  831. }
  832. virtual RID decal_instance_create(RID p_decal);
  833. virtual void decal_instance_set_transform(RID p_decal, const Transform &p_transform);
  834. _FORCE_INLINE_ RID decal_instance_get_base(RID p_decal) const {
  835. DecalInstance *decal = decal_instance_owner.getornull(p_decal);
  836. return decal->decal;
  837. }
  838. _FORCE_INLINE_ Transform decal_instance_get_transform(RID p_decal) const {
  839. DecalInstance *decal = decal_instance_owner.getornull(p_decal);
  840. return decal->transform;
  841. }
  842. virtual RID lightmap_instance_create(RID p_lightmap);
  843. virtual void lightmap_instance_set_transform(RID p_lightmap, const Transform &p_transform);
  844. _FORCE_INLINE_ bool lightmap_instance_is_valid(RID p_lightmap_instance) {
  845. return lightmap_instance_owner.getornull(p_lightmap_instance) != nullptr;
  846. }
  847. _FORCE_INLINE_ RID lightmap_instance_get_lightmap(RID p_lightmap_instance) {
  848. LightmapInstance *li = lightmap_instance_owner.getornull(p_lightmap_instance);
  849. return li->lightmap;
  850. }
  851. _FORCE_INLINE_ Transform lightmap_instance_get_transform(RID p_lightmap_instance) {
  852. LightmapInstance *li = lightmap_instance_owner.getornull(p_lightmap_instance);
  853. return li->transform;
  854. }
  855. void _fill_instance_indices(const RID *p_omni_light_instances, uint32_t p_omni_light_instance_count, uint32_t *p_omni_light_indices, const RID *p_spot_light_instances, uint32_t p_spot_light_instance_count, uint32_t *p_spot_light_indices, const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count, uint32_t *p_reflection_probe_indices, const RID *p_decal_instances, uint32_t p_decal_instance_count, uint32_t *p_decal_instance_indices, uint32_t p_layer_mask, uint32_t p_max_dst_words = 2);
  856. /* gi light probes */
  857. RID gi_probe_instance_create(RID p_base);
  858. void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform);
  859. bool gi_probe_needs_update(RID p_probe) const;
  860. void gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RendererSceneRender::GeometryInstance *> &p_dynamic_objects);
  861. void gi_probe_set_quality(RS::GIProbeQuality p_quality) { gi.gi_probe_quality = p_quality; }
  862. /* render buffers */
  863. RID render_buffers_create();
  864. void render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, RS::ViewportMSAA p_msaa, RS::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_debanding);
  865. void gi_set_use_half_resolution(bool p_enable);
  866. RID render_buffers_get_ao_texture(RID p_render_buffers);
  867. RID render_buffers_get_back_buffer_texture(RID p_render_buffers);
  868. RID render_buffers_get_gi_probe_buffer(RID p_render_buffers);
  869. RID render_buffers_get_default_gi_probe_buffer();
  870. RID render_buffers_get_gi_ambient_texture(RID p_render_buffers);
  871. RID render_buffers_get_gi_reflection_texture(RID p_render_buffers);
  872. uint32_t render_buffers_get_sdfgi_cascade_count(RID p_render_buffers) const;
  873. bool render_buffers_is_sdfgi_enabled(RID p_render_buffers) const;
  874. RID render_buffers_get_sdfgi_irradiance_probes(RID p_render_buffers) const;
  875. Vector3 render_buffers_get_sdfgi_cascade_offset(RID p_render_buffers, uint32_t p_cascade) const;
  876. Vector3i render_buffers_get_sdfgi_cascade_probe_offset(RID p_render_buffers, uint32_t p_cascade) const;
  877. float render_buffers_get_sdfgi_cascade_probe_size(RID p_render_buffers, uint32_t p_cascade) const;
  878. float render_buffers_get_sdfgi_normal_bias(RID p_render_buffers) const;
  879. uint32_t render_buffers_get_sdfgi_cascade_probe_count(RID p_render_buffers) const;
  880. uint32_t render_buffers_get_sdfgi_cascade_size(RID p_render_buffers) const;
  881. bool render_buffers_is_sdfgi_using_occlusion(RID p_render_buffers) const;
  882. float render_buffers_get_sdfgi_energy(RID p_render_buffers) const;
  883. RID render_buffers_get_sdfgi_occlusion_texture(RID p_render_buffers) const;
  884. bool render_buffers_has_volumetric_fog(RID p_render_buffers) const;
  885. RID render_buffers_get_volumetric_fog_texture(RID p_render_buffers);
  886. RID render_buffers_get_volumetric_fog_sky_uniform_set(RID p_render_buffers);
  887. float render_buffers_get_volumetric_fog_end(RID p_render_buffers);
  888. float render_buffers_get_volumetric_fog_detail_spread(RID p_render_buffers);
  889. void render_scene(RID p_render_buffers, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, const PagedArray<GeometryInstance *> &p_instances, const PagedArray<RID> &p_lights, const PagedArray<RID> &p_reflection_probes, const PagedArray<RID> &p_gi_probes, const PagedArray<RID> &p_decals, const PagedArray<RID> &p_lightmaps, RID p_environment, RID p_camera_effects, 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_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);
  890. void render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, const PagedArray<GeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region);
  891. void render_particle_collider_heightfield(RID p_collider, const Transform &p_transform, const PagedArray<GeometryInstance *> &p_instances);
  892. virtual void set_scene_pass(uint64_t p_pass) {
  893. scene_pass = p_pass;
  894. }
  895. _FORCE_INLINE_ uint64_t get_scene_pass() {
  896. return scene_pass;
  897. }
  898. virtual void screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_limit);
  899. virtual bool screen_space_roughness_limiter_is_active() const;
  900. virtual float screen_space_roughness_limiter_get_amount() const;
  901. virtual float screen_space_roughness_limiter_get_limit() const;
  902. virtual void sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality);
  903. RS::SubSurfaceScatteringQuality sub_surface_scattering_get_quality() const;
  904. virtual void sub_surface_scattering_set_scale(float p_scale, float p_depth_scale);
  905. virtual void shadows_quality_set(RS::ShadowQuality p_quality);
  906. virtual void directional_shadow_quality_set(RS::ShadowQuality p_quality);
  907. _FORCE_INLINE_ RS::ShadowQuality shadows_quality_get() const { return shadows_quality; }
  908. _FORCE_INLINE_ RS::ShadowQuality directional_shadow_quality_get() const { return directional_shadow_quality; }
  909. _FORCE_INLINE_ float shadows_quality_radius_get() const { return shadows_quality_radius; }
  910. _FORCE_INLINE_ float directional_shadow_quality_radius_get() const { return directional_shadow_quality_radius; }
  911. _FORCE_INLINE_ float *directional_penumbra_shadow_kernel_get() { return directional_penumbra_shadow_kernel; }
  912. _FORCE_INLINE_ float *directional_soft_shadow_kernel_get() { return directional_soft_shadow_kernel; }
  913. _FORCE_INLINE_ float *penumbra_shadow_kernel_get() { return penumbra_shadow_kernel; }
  914. _FORCE_INLINE_ float *soft_shadow_kernel_get() { return soft_shadow_kernel; }
  915. _FORCE_INLINE_ int directional_penumbra_shadow_samples_get() const { return directional_penumbra_shadow_samples; }
  916. _FORCE_INLINE_ int directional_soft_shadow_samples_get() const { return directional_soft_shadow_samples; }
  917. _FORCE_INLINE_ int penumbra_shadow_samples_get() const { return penumbra_shadow_samples; }
  918. _FORCE_INLINE_ int soft_shadow_samples_get() const { return soft_shadow_samples; }
  919. int get_roughness_layers() const;
  920. bool is_using_radiance_cubemap_array() const;
  921. virtual TypedArray<Image> bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size);
  922. virtual bool free(RID p_rid);
  923. virtual void update();
  924. virtual void set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw);
  925. _FORCE_INLINE_ RS::ViewportDebugDraw get_debug_draw_mode() const {
  926. return debug_draw;
  927. }
  928. void set_time(double p_time, double p_step);
  929. RID get_reflection_probe_buffer();
  930. RID get_omni_light_buffer();
  931. RID get_spot_light_buffer();
  932. RID get_directional_light_buffer();
  933. RID get_decal_buffer();
  934. int get_max_directional_lights() const;
  935. void sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir);
  936. virtual bool is_dynamic_gi_supported() const;
  937. virtual bool is_clustered_enabled() const;
  938. virtual bool is_volumetric_supported() const;
  939. virtual uint32_t get_max_elements() const;
  940. RendererSceneRenderRD(RendererStorageRD *p_storage);
  941. ~RendererSceneRenderRD();
  942. };
  943. #endif // RASTERIZER_SCENE_RD_H