gi.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /**************************************************************************/
  2. /* gi.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 GI_RD_H
  31. #define GI_RD_H
  32. #include "core/templates/local_vector.h"
  33. #include "core/templates/rid_owner.h"
  34. #include "servers/rendering/environment/renderer_gi.h"
  35. #include "servers/rendering/renderer_compositor.h"
  36. #include "servers/rendering/renderer_rd/environment/sky.h"
  37. #include "servers/rendering/renderer_rd/shaders/environment/gi.glsl.gen.h"
  38. #include "servers/rendering/renderer_rd/shaders/environment/sdfgi_debug.glsl.gen.h"
  39. #include "servers/rendering/renderer_rd/shaders/environment/sdfgi_debug_probes.glsl.gen.h"
  40. #include "servers/rendering/renderer_rd/shaders/environment/sdfgi_direct_light.glsl.gen.h"
  41. #include "servers/rendering/renderer_rd/shaders/environment/sdfgi_integrate.glsl.gen.h"
  42. #include "servers/rendering/renderer_rd/shaders/environment/sdfgi_preprocess.glsl.gen.h"
  43. #include "servers/rendering/renderer_rd/shaders/environment/voxel_gi.glsl.gen.h"
  44. #include "servers/rendering/renderer_rd/shaders/environment/voxel_gi_debug.glsl.gen.h"
  45. #include "servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h"
  46. #include "servers/rendering/renderer_scene_render.h"
  47. #include "servers/rendering/rendering_device.h"
  48. #include "servers/rendering/storage/utilities.h"
  49. #define RB_SCOPE_GI SNAME("rbgi")
  50. #define RB_SCOPE_SDFGI SNAME("sdfgi")
  51. #define RB_TEX_AMBIENT SNAME("ambient")
  52. #define RB_TEX_REFLECTION SNAME("reflection")
  53. // Forward declare RenderDataRD and RendererSceneRenderRD so we can pass it into some of our methods, these classes are pretty tightly bound
  54. class RenderDataRD;
  55. class RendererSceneRenderRD;
  56. namespace RendererRD {
  57. class GI : public RendererGI {
  58. public:
  59. /* VOXEL GI STORAGE */
  60. struct VoxelGI {
  61. RID octree_buffer;
  62. RID data_buffer;
  63. RID sdf_texture;
  64. uint32_t octree_buffer_size = 0;
  65. uint32_t data_buffer_size = 0;
  66. Vector<int> level_counts;
  67. int cell_count = 0;
  68. Transform3D to_cell_xform;
  69. AABB bounds;
  70. Vector3i octree_size;
  71. float dynamic_range = 2.0;
  72. float energy = 1.0;
  73. float baked_exposure = 1.0;
  74. float bias = 1.4;
  75. float normal_bias = 0.0;
  76. float propagation = 0.5;
  77. bool interior = false;
  78. bool use_two_bounces = true;
  79. uint32_t version = 1;
  80. uint32_t data_version = 1;
  81. Dependency dependency;
  82. };
  83. /* VOXEL_GI INSTANCE */
  84. //@TODO VoxelGIInstance is still directly used in the render code, we'll address this when we refactor the render code itself.
  85. struct VoxelGIInstance {
  86. // access to our containers
  87. GI *gi = nullptr;
  88. RID probe;
  89. RID texture;
  90. RID write_buffer;
  91. struct Mipmap {
  92. RID texture;
  93. RID uniform_set;
  94. RID second_bounce_uniform_set;
  95. RID write_uniform_set;
  96. uint32_t level;
  97. uint32_t cell_offset;
  98. uint32_t cell_count;
  99. };
  100. Vector<Mipmap> mipmaps;
  101. struct DynamicMap {
  102. RID texture; //color normally, or emission on first pass
  103. RID fb_depth; //actual depth buffer for the first pass, float depth for later passes
  104. RID depth; //actual depth buffer for the first pass, float depth for later passes
  105. RID normal; //normal buffer for the first pass
  106. RID albedo; //emission buffer for the first pass
  107. RID orm; //orm buffer for the first pass
  108. RID fb; //used for rendering, only valid on first map
  109. RID uniform_set;
  110. uint32_t size;
  111. int mipmap; // mipmap to write to, -1 if no mipmap assigned
  112. };
  113. Vector<DynamicMap> dynamic_maps;
  114. int slot = -1;
  115. uint32_t last_probe_version = 0;
  116. uint32_t last_probe_data_version = 0;
  117. //uint64_t last_pass = 0;
  118. uint32_t render_index = 0;
  119. bool has_dynamic_object_data = false;
  120. Transform3D transform;
  121. void update(bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects);
  122. void debug(RD::DrawListID p_draw_list, RID p_framebuffer, const Projection &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha);
  123. void free_resources();
  124. };
  125. private:
  126. static GI *singleton;
  127. /* VOXEL GI STORAGE */
  128. mutable RID_Owner<VoxelGI, true> voxel_gi_owner;
  129. /* VOXEL_GI INSTANCE */
  130. mutable RID_Owner<VoxelGIInstance> voxel_gi_instance_owner;
  131. struct VoxelGILight {
  132. uint32_t type;
  133. float energy;
  134. float radius;
  135. float attenuation;
  136. float color[3];
  137. float cos_spot_angle;
  138. float position[3];
  139. float inv_spot_attenuation;
  140. float direction[3];
  141. uint32_t has_shadow;
  142. };
  143. struct VoxelGIPushConstant {
  144. int32_t limits[3];
  145. uint32_t stack_size;
  146. float emission_scale;
  147. float propagation;
  148. float dynamic_range;
  149. uint32_t light_count;
  150. uint32_t cell_offset;
  151. uint32_t cell_count;
  152. float aniso_strength;
  153. uint32_t pad;
  154. };
  155. struct VoxelGIDynamicPushConstant {
  156. int32_t limits[3];
  157. uint32_t light_count;
  158. int32_t x_dir[3];
  159. float z_base;
  160. int32_t y_dir[3];
  161. float z_sign;
  162. int32_t z_dir[3];
  163. float pos_multiplier;
  164. uint32_t rect_pos[2];
  165. uint32_t rect_size[2];
  166. uint32_t prev_rect_ofs[2];
  167. uint32_t prev_rect_size[2];
  168. uint32_t flip_x;
  169. uint32_t flip_y;
  170. float dynamic_range;
  171. uint32_t on_mipmap;
  172. float propagation;
  173. float pad[3];
  174. };
  175. VoxelGILight *voxel_gi_lights = nullptr;
  176. uint32_t voxel_gi_max_lights = 32;
  177. RID voxel_gi_lights_uniform;
  178. enum {
  179. VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT,
  180. VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE,
  181. VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP,
  182. VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE,
  183. VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING,
  184. VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE,
  185. VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT,
  186. VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT,
  187. VOXEL_GI_SHADER_VERSION_MAX
  188. };
  189. VoxelGiShaderRD voxel_gi_shader;
  190. RID voxel_gi_lighting_shader_version;
  191. RID voxel_gi_lighting_shader_version_shaders[VOXEL_GI_SHADER_VERSION_MAX];
  192. RID voxel_gi_lighting_shader_version_pipelines[VOXEL_GI_SHADER_VERSION_MAX];
  193. enum {
  194. VOXEL_GI_DEBUG_COLOR,
  195. VOXEL_GI_DEBUG_LIGHT,
  196. VOXEL_GI_DEBUG_EMISSION,
  197. VOXEL_GI_DEBUG_LIGHT_FULL,
  198. VOXEL_GI_DEBUG_MAX
  199. };
  200. struct VoxelGIDebugPushConstant {
  201. float projection[16];
  202. uint32_t cell_offset;
  203. float dynamic_range;
  204. float alpha;
  205. uint32_t level;
  206. int32_t bounds[3];
  207. uint32_t pad;
  208. };
  209. VoxelGiDebugShaderRD voxel_gi_debug_shader;
  210. RID voxel_gi_debug_shader_version;
  211. RID voxel_gi_debug_shader_version_shaders[VOXEL_GI_DEBUG_MAX];
  212. PipelineCacheRD voxel_gi_debug_shader_version_pipelines[VOXEL_GI_DEBUG_MAX];
  213. RID voxel_gi_debug_uniform_set;
  214. /* SDFGI */
  215. struct SDFGIShader {
  216. enum SDFGIPreprocessShaderVersion {
  217. PRE_PROCESS_SCROLL,
  218. PRE_PROCESS_SCROLL_OCCLUSION,
  219. PRE_PROCESS_JUMP_FLOOD_INITIALIZE,
  220. PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF,
  221. PRE_PROCESS_JUMP_FLOOD,
  222. PRE_PROCESS_JUMP_FLOOD_OPTIMIZED,
  223. PRE_PROCESS_JUMP_FLOOD_UPSCALE,
  224. PRE_PROCESS_OCCLUSION,
  225. PRE_PROCESS_STORE,
  226. PRE_PROCESS_MAX
  227. };
  228. struct PreprocessPushConstant {
  229. int32_t scroll[3];
  230. int32_t grid_size;
  231. int32_t probe_offset[3];
  232. int32_t step_size;
  233. int32_t half_size;
  234. uint32_t occlusion_index;
  235. int32_t cascade;
  236. uint32_t pad;
  237. };
  238. SdfgiPreprocessShaderRD preprocess;
  239. RID preprocess_shader;
  240. RID preprocess_pipeline[PRE_PROCESS_MAX];
  241. struct DebugPushConstant {
  242. float grid_size[3];
  243. uint32_t max_cascades;
  244. int32_t screen_size[2];
  245. float y_mult;
  246. float z_near;
  247. float inv_projection[3][4];
  248. float cam_basis[3][3];
  249. float cam_origin[3];
  250. };
  251. SdfgiDebugShaderRD debug;
  252. RID debug_shader;
  253. RID debug_shader_version;
  254. RID debug_pipeline;
  255. enum ProbeDebugMode {
  256. PROBE_DEBUG_PROBES,
  257. PROBE_DEBUG_PROBES_MULTIVIEW,
  258. PROBE_DEBUG_VISIBILITY,
  259. PROBE_DEBUG_VISIBILITY_MULTIVIEW,
  260. PROBE_DEBUG_MAX
  261. };
  262. struct DebugProbesSceneData {
  263. float projection[2][16];
  264. };
  265. struct DebugProbesPushConstant {
  266. uint32_t band_power;
  267. uint32_t sections_in_band;
  268. uint32_t band_mask;
  269. float section_arc;
  270. float grid_size[3];
  271. uint32_t cascade;
  272. uint32_t pad;
  273. float y_mult;
  274. int32_t probe_debug_index;
  275. int32_t probe_axis_size;
  276. };
  277. SdfgiDebugProbesShaderRD debug_probes;
  278. RID debug_probes_shader;
  279. RID debug_probes_shader_version;
  280. PipelineCacheRD debug_probes_pipeline[PROBE_DEBUG_MAX];
  281. struct Light {
  282. float color[3];
  283. float energy;
  284. float direction[3];
  285. uint32_t has_shadow;
  286. float position[3];
  287. float attenuation;
  288. uint32_t type;
  289. float cos_spot_angle;
  290. float inv_spot_attenuation;
  291. float radius;
  292. };
  293. struct DirectLightPushConstant {
  294. float grid_size[3];
  295. uint32_t max_cascades;
  296. uint32_t cascade;
  297. uint32_t light_count;
  298. uint32_t process_offset;
  299. uint32_t process_increment;
  300. int32_t probe_axis_size;
  301. float bounce_feedback;
  302. float y_mult;
  303. uint32_t use_occlusion;
  304. };
  305. enum {
  306. DIRECT_LIGHT_MODE_STATIC,
  307. DIRECT_LIGHT_MODE_DYNAMIC,
  308. DIRECT_LIGHT_MODE_MAX
  309. };
  310. SdfgiDirectLightShaderRD direct_light;
  311. RID direct_light_shader;
  312. RID direct_light_pipeline[DIRECT_LIGHT_MODE_MAX];
  313. enum {
  314. INTEGRATE_MODE_PROCESS,
  315. INTEGRATE_MODE_STORE,
  316. INTEGRATE_MODE_SCROLL,
  317. INTEGRATE_MODE_SCROLL_STORE,
  318. INTEGRATE_MODE_MAX
  319. };
  320. struct IntegratePushConstant {
  321. enum {
  322. SKY_FLAGS_MODE_COLOR = 0x01,
  323. SKY_FLAGS_MODE_SKY = 0x02,
  324. SKY_FLAGS_ORIENTATION_SIGN = 0x04,
  325. };
  326. float grid_size[3];
  327. uint32_t max_cascades;
  328. uint32_t probe_axis_size;
  329. uint32_t cascade;
  330. uint32_t history_index;
  331. uint32_t history_size;
  332. uint32_t ray_count;
  333. float ray_bias;
  334. int32_t image_size[2];
  335. int32_t world_offset[3];
  336. uint32_t sky_flags;
  337. int32_t scroll[3];
  338. float sky_energy;
  339. float sky_color_or_orientation[3];
  340. float y_mult;
  341. uint32_t store_ambient_texture;
  342. uint32_t pad[3];
  343. };
  344. SdfgiIntegrateShaderRD integrate;
  345. RID integrate_shader;
  346. RID integrate_pipeline[INTEGRATE_MODE_MAX];
  347. RID integrate_default_sky_uniform_set;
  348. } sdfgi_shader;
  349. public:
  350. static GI *get_singleton() { return singleton; }
  351. /* GI */
  352. enum {
  353. MAX_VOXEL_GI_INSTANCES = 8
  354. };
  355. // Struct for use in render buffer
  356. class RenderBuffersGI : public RenderBufferCustomDataRD {
  357. GDCLASS(RenderBuffersGI, RenderBufferCustomDataRD)
  358. private:
  359. RID voxel_gi_buffer;
  360. public:
  361. RID voxel_gi_textures[MAX_VOXEL_GI_INSTANCES];
  362. RID full_buffer;
  363. RID full_dispatch;
  364. RID full_mask;
  365. /* GI buffers */
  366. bool using_half_size_gi = false;
  367. RID uniform_set[RendererSceneRender::MAX_RENDER_VIEWS];
  368. RID scene_data_ubo;
  369. RID get_voxel_gi_buffer();
  370. virtual void configure(RenderSceneBuffersRD *p_render_buffers) override {}
  371. virtual void free_data() override;
  372. };
  373. /* VOXEL GI API */
  374. bool owns_voxel_gi(RID p_rid) { return voxel_gi_owner.owns(p_rid); }
  375. virtual RID voxel_gi_allocate() override;
  376. virtual void voxel_gi_free(RID p_voxel_gi) override;
  377. virtual void voxel_gi_initialize(RID p_voxel_gi) override;
  378. virtual void voxel_gi_allocate_data(RID p_voxel_gi, const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) override;
  379. virtual AABB voxel_gi_get_bounds(RID p_voxel_gi) const override;
  380. virtual Vector3i voxel_gi_get_octree_size(RID p_voxel_gi) const override;
  381. virtual Vector<uint8_t> voxel_gi_get_octree_cells(RID p_voxel_gi) const override;
  382. virtual Vector<uint8_t> voxel_gi_get_data_cells(RID p_voxel_gi) const override;
  383. virtual Vector<uint8_t> voxel_gi_get_distance_field(RID p_voxel_gi) const override;
  384. virtual Vector<int> voxel_gi_get_level_counts(RID p_voxel_gi) const override;
  385. virtual Transform3D voxel_gi_get_to_cell_xform(RID p_voxel_gi) const override;
  386. virtual void voxel_gi_set_dynamic_range(RID p_voxel_gi, float p_range) override;
  387. virtual float voxel_gi_get_dynamic_range(RID p_voxel_gi) const override;
  388. virtual void voxel_gi_set_propagation(RID p_voxel_gi, float p_range) override;
  389. virtual float voxel_gi_get_propagation(RID p_voxel_gi) const override;
  390. virtual void voxel_gi_set_energy(RID p_voxel_gi, float p_energy) override;
  391. virtual float voxel_gi_get_energy(RID p_voxel_gi) const override;
  392. virtual void voxel_gi_set_baked_exposure_normalization(RID p_voxel_gi, float p_baked_exposure) override;
  393. virtual float voxel_gi_get_baked_exposure_normalization(RID p_voxel_gi) const override;
  394. virtual void voxel_gi_set_bias(RID p_voxel_gi, float p_bias) override;
  395. virtual float voxel_gi_get_bias(RID p_voxel_gi) const override;
  396. virtual void voxel_gi_set_normal_bias(RID p_voxel_gi, float p_range) override;
  397. virtual float voxel_gi_get_normal_bias(RID p_voxel_gi) const override;
  398. virtual void voxel_gi_set_interior(RID p_voxel_gi, bool p_enable) override;
  399. virtual bool voxel_gi_is_interior(RID p_voxel_gi) const override;
  400. virtual void voxel_gi_set_use_two_bounces(RID p_voxel_gi, bool p_enable) override;
  401. virtual bool voxel_gi_is_using_two_bounces(RID p_voxel_gi) const override;
  402. virtual uint32_t voxel_gi_get_version(RID p_probe) const override;
  403. uint32_t voxel_gi_get_data_version(RID p_probe);
  404. RID voxel_gi_get_octree_buffer(RID p_voxel_gi) const;
  405. RID voxel_gi_get_data_buffer(RID p_voxel_gi) const;
  406. RID voxel_gi_get_sdf_texture(RID p_voxel_gi);
  407. Dependency *voxel_gi_get_dependency(RID p_voxel_gi) const;
  408. /* VOXEL_GI INSTANCE */
  409. _FORCE_INLINE_ RID voxel_gi_instance_get_texture(RID p_probe) {
  410. VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
  411. ERR_FAIL_NULL_V(voxel_gi, RID());
  412. return voxel_gi->texture;
  413. }
  414. _FORCE_INLINE_ void voxel_gi_instance_set_render_index(RID p_probe, uint32_t p_index) {
  415. VoxelGIInstance *voxel_gi = voxel_gi_instance_owner.get_or_null(p_probe);
  416. ERR_FAIL_NULL(voxel_gi);
  417. voxel_gi->render_index = p_index;
  418. }
  419. bool voxel_gi_instance_owns(RID p_rid) const {
  420. return voxel_gi_instance_owner.owns(p_rid);
  421. }
  422. void voxel_gi_instance_free(RID p_rid);
  423. RS::VoxelGIQuality voxel_gi_quality = RS::VOXEL_GI_QUALITY_LOW;
  424. /* SDFGI */
  425. class SDFGI : public RenderBufferCustomDataRD {
  426. GDCLASS(SDFGI, RenderBufferCustomDataRD)
  427. public:
  428. enum {
  429. MAX_CASCADES = 8,
  430. CASCADE_SIZE = 128,
  431. PROBE_DIVISOR = 16,
  432. ANISOTROPY_SIZE = 6,
  433. MAX_DYNAMIC_LIGHTS = 128,
  434. MAX_STATIC_LIGHTS = 1024,
  435. LIGHTPROBE_OCT_SIZE = 6,
  436. SH_SIZE = 16
  437. };
  438. struct Cascade {
  439. struct UBO {
  440. float offset[3];
  441. float to_cell;
  442. int32_t probe_offset[3];
  443. uint32_t pad;
  444. float pad2[4];
  445. };
  446. //cascade blocks are full-size for volume (128^3), half size for albedo/emission
  447. RID sdf_tex;
  448. RID light_tex;
  449. RID light_aniso_0_tex;
  450. RID light_aniso_1_tex;
  451. RID light_data;
  452. RID light_aniso_0_data;
  453. RID light_aniso_1_data;
  454. struct SolidCell { // this struct is unused, but remains as reference for size
  455. uint32_t position;
  456. uint32_t albedo;
  457. uint32_t static_light;
  458. uint32_t static_light_aniso;
  459. };
  460. // Buffers for indirect compute dispatch.
  461. RID solid_cell_dispatch_buffer_storage;
  462. RID solid_cell_dispatch_buffer_call;
  463. RID solid_cell_buffer;
  464. RID lightprobe_history_tex;
  465. RID lightprobe_average_tex;
  466. float cell_size;
  467. Vector3i position;
  468. static const Vector3i DIRTY_ALL;
  469. Vector3i dirty_regions; //(0,0,0 is not dirty, negative is refresh from the end, DIRTY_ALL is refresh all.
  470. RID sdf_store_uniform_set;
  471. RID sdf_direct_light_static_uniform_set;
  472. RID sdf_direct_light_dynamic_uniform_set;
  473. RID scroll_uniform_set;
  474. RID scroll_occlusion_uniform_set;
  475. RID integrate_uniform_set;
  476. RID lights_buffer;
  477. float baked_exposure_normalization = 1.0;
  478. bool all_dynamic_lights_dirty = true;
  479. };
  480. // access to our containers
  481. GI *gi = nullptr;
  482. // used for rendering (voxelization)
  483. RID render_albedo;
  484. RID render_emission;
  485. RID render_emission_aniso;
  486. RID render_occlusion[8];
  487. RID render_geom_facing;
  488. RID render_sdf[2];
  489. RID render_sdf_half[2];
  490. // used for ping pong processing in cascades
  491. RID sdf_initialize_uniform_set;
  492. RID sdf_initialize_half_uniform_set;
  493. RID jump_flood_uniform_set[2];
  494. RID jump_flood_half_uniform_set[2];
  495. RID sdf_upscale_uniform_set;
  496. int upscale_jfa_uniform_set_index;
  497. RID occlusion_uniform_set;
  498. uint32_t cascade_size = 128;
  499. LocalVector<Cascade> cascades;
  500. RID lightprobe_texture;
  501. RID lightprobe_data;
  502. RID occlusion_texture;
  503. RID occlusion_data;
  504. RID ambient_texture; //integrates with volumetric fog
  505. RID lightprobe_history_scroll; //used for scrolling lightprobes
  506. RID lightprobe_average_scroll; //used for scrolling lightprobes
  507. uint32_t history_size = 0;
  508. float solid_cell_ratio = 0;
  509. uint32_t solid_cell_count = 0;
  510. int num_cascades = 6;
  511. float min_cell_size = 0;
  512. uint32_t probe_axis_count = 0; //amount of probes per axis, this is an odd number because it encloses endpoints
  513. RID debug_uniform_set[RendererSceneRender::MAX_RENDER_VIEWS];
  514. RID debug_probes_scene_data_ubo;
  515. RID debug_probes_uniform_set;
  516. RID cascades_ubo;
  517. bool uses_occlusion = false;
  518. float bounce_feedback = 0.5;
  519. bool reads_sky = true;
  520. float energy = 1.0;
  521. float normal_bias = 1.1;
  522. float probe_bias = 1.1;
  523. RS::EnvironmentSDFGIYScale y_scale_mode = RS::ENV_SDFGI_Y_SCALE_75_PERCENT;
  524. float y_mult = 1.0;
  525. uint32_t version = 0;
  526. uint32_t render_pass = 0;
  527. int32_t cascade_dynamic_light_count[SDFGI::MAX_CASCADES]; //used dynamically
  528. RID integrate_sky_uniform_set;
  529. virtual void configure(RenderSceneBuffersRD *p_render_buffers) override {}
  530. virtual void free_data() override;
  531. ~SDFGI();
  532. void create(RID p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size, GI *p_gi);
  533. void update(RID p_env, const Vector3 &p_world_position);
  534. void update_light();
  535. void update_probes(RID p_env, RendererRD::SkyRD::Sky *p_sky);
  536. void store_probes();
  537. int get_pending_region_data(int p_region, Vector3i &r_local_offset, Vector3i &r_local_size, AABB &r_bounds) const;
  538. void update_cascades();
  539. void debug_draw(uint32_t p_view_count, const Projection *p_projections, const Transform3D &p_transform, int p_width, int p_height, RID p_render_target, RID p_texture, const Vector<RID> &p_texture_views);
  540. void debug_probes(RID p_framebuffer, const uint32_t p_view_count, const Projection *p_camera_with_transforms);
  541. void pre_process_gi(const Transform3D &p_transform, RenderDataRD *p_render_data);
  542. void render_region(Ref<RenderSceneBuffersRD> p_render_buffers, int p_region, const PagedArray<RenderGeometryInstance *> &p_instances, float p_exposure_normalization);
  543. void render_static_lights(RenderDataRD *p_render_data, Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_cascade_count, const uint32_t *p_cascade_indices, const PagedArray<RID> *p_positional_light_cull_result);
  544. };
  545. RS::EnvironmentSDFGIRayCount sdfgi_ray_count = RS::ENV_SDFGI_RAY_COUNT_16;
  546. RS::EnvironmentSDFGIFramesToConverge sdfgi_frames_to_converge = RS::ENV_SDFGI_CONVERGE_IN_30_FRAMES;
  547. RS::EnvironmentSDFGIFramesToUpdateLight sdfgi_frames_to_update_light = RS::ENV_SDFGI_UPDATE_LIGHT_IN_4_FRAMES;
  548. float sdfgi_solid_cell_ratio = 0.25;
  549. Vector3 sdfgi_debug_probe_pos;
  550. Vector3 sdfgi_debug_probe_dir;
  551. bool sdfgi_debug_probe_enabled = false;
  552. Vector3i sdfgi_debug_probe_index;
  553. uint32_t sdfgi_current_version = 0;
  554. /* SDFGI UPDATE */
  555. int sdfgi_get_lightprobe_octahedron_size() const { return SDFGI::LIGHTPROBE_OCT_SIZE; }
  556. virtual void sdfgi_reset() override;
  557. struct SDFGIData {
  558. float grid_size[3];
  559. uint32_t max_cascades;
  560. uint32_t use_occlusion;
  561. int32_t probe_axis_size;
  562. float probe_to_uvw;
  563. float normal_bias;
  564. float lightprobe_tex_pixel_size[3];
  565. float energy;
  566. float lightprobe_uv_offset[3];
  567. float y_mult;
  568. float occlusion_clamp[3];
  569. uint32_t pad3;
  570. float occlusion_renormalize[3];
  571. uint32_t pad4;
  572. float cascade_probe_size[3];
  573. uint32_t pad5;
  574. struct ProbeCascadeData {
  575. float position[3]; //offset of (0,0,0) in world coordinates
  576. float to_probe; // 1/bounds * grid_size
  577. int32_t probe_world_offset[3];
  578. float to_cell; // 1/bounds * grid_size
  579. float pad[3];
  580. float exposure_normalization;
  581. };
  582. ProbeCascadeData cascades[SDFGI::MAX_CASCADES];
  583. };
  584. struct VoxelGIData {
  585. float xform[16]; // 64 - 64
  586. float bounds[3]; // 12 - 76
  587. float dynamic_range; // 4 - 80
  588. float bias; // 4 - 84
  589. float normal_bias; // 4 - 88
  590. uint32_t blend_ambient; // 4 - 92
  591. uint32_t mipmaps; // 4 - 96
  592. float pad[3]; // 12 - 108
  593. float exposure_normalization; // 4 - 112
  594. };
  595. struct SceneData {
  596. float inv_projection[2][16];
  597. float cam_transform[16];
  598. float eye_offset[2][4];
  599. int32_t screen_size[2];
  600. float pad1;
  601. float pad2;
  602. };
  603. struct PushConstant {
  604. uint32_t max_voxel_gi_instances;
  605. uint32_t high_quality_vct;
  606. uint32_t orthogonal;
  607. uint32_t view_index;
  608. float proj_info[4];
  609. float z_near;
  610. float z_far;
  611. float pad2;
  612. float pad3;
  613. };
  614. RID sdfgi_ubo;
  615. enum Mode {
  616. MODE_VOXEL_GI,
  617. MODE_SDFGI,
  618. MODE_COMBINED,
  619. MODE_MAX
  620. };
  621. enum ShaderSpecializations {
  622. SHADER_SPECIALIZATION_HALF_RES = 1 << 0,
  623. SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX = 1 << 1,
  624. SHADER_SPECIALIZATION_USE_VRS = 1 << 2,
  625. SHADER_SPECIALIZATION_VARIATIONS = 8,
  626. };
  627. RID default_voxel_gi_buffer;
  628. bool half_resolution = false;
  629. GiShaderRD shader;
  630. RID shader_version;
  631. RID pipelines[SHADER_SPECIALIZATION_VARIATIONS][MODE_MAX];
  632. GI();
  633. ~GI();
  634. void init(RendererRD::SkyRD *p_sky);
  635. void free();
  636. Ref<SDFGI> create_sdfgi(RID p_env, const Vector3 &p_world_position, uint32_t p_requested_history_size);
  637. void setup_voxel_gi_instances(RenderDataRD *p_render_data, Ref<RenderSceneBuffersRD> p_render_buffers, const Transform3D &p_transform, const PagedArray<RID> &p_voxel_gi_instances, uint32_t &r_voxel_gi_instances_used);
  638. void process_gi(Ref<RenderSceneBuffersRD> p_render_buffers, const RID *p_normal_roughness_slices, RID p_voxel_gi_buffer, RID p_environment, uint32_t p_view_count, const Projection *p_projections, const Vector3 *p_eye_offsets, const Transform3D &p_cam_transform, const PagedArray<RID> &p_voxel_gi_instances);
  639. RID voxel_gi_instance_create(RID p_base);
  640. void voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform);
  641. bool voxel_gi_needs_update(RID p_probe) const;
  642. void voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects);
  643. void debug_voxel_gi(RID p_voxel_gi, RD::DrawListID p_draw_list, RID p_framebuffer, const Projection &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha);
  644. };
  645. } // namespace RendererRD
  646. #endif // GI_RD_H