rasterizer_scene_rd.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*************************************************************************/
  2. /* rasterizer_scene_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RASTERIZER_SCENE_RD_H
  31. #define RASTERIZER_SCENE_RD_H
  32. #include "core/rid_owner.h"
  33. #include "servers/visual/rasterizer.h"
  34. #include "servers/visual/rasterizer_rd/rasterizer_storage_rd.h"
  35. #include "servers/visual/rasterizer_rd/shaders/giprobe.glsl.gen.h"
  36. #include "servers/visual/rasterizer_rd/shaders/giprobe_debug.glsl.gen.h"
  37. #include "servers/visual/rendering_device.h"
  38. class RasterizerSceneRD : public RasterizerScene {
  39. public:
  40. enum GIProbeQuality {
  41. GIPROBE_QUALITY_ULTRA_LOW,
  42. GIPROBE_QUALITY_MEDIUM,
  43. GIPROBE_QUALITY_HIGH,
  44. };
  45. protected:
  46. struct RenderBufferData {
  47. virtual void configure(RID p_color_buffer, RID p_depth_buffer, int p_width, int p_height, VS::ViewportMSAA p_msaa) = 0;
  48. virtual ~RenderBufferData() {}
  49. };
  50. virtual RenderBufferData *_create_render_buffer_data() = 0;
  51. virtual void _render_scene(RID p_render_buffer, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, RID p_environment, 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) = 0;
  52. virtual void _render_shadow(RID p_framebuffer, InstanceBase **p_cull_result, int p_cull_count, const CameraMatrix &p_projection, const Transform &p_transform, float p_zfar, float p_bias, float p_normal_bias, bool p_use_dp, bool use_dp_flip) = 0;
  53. virtual void _render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) = 0;
  54. virtual void _debug_giprobe(RID p_gi_probe, RenderingDevice::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha);
  55. RenderBufferData *render_buffers_get_data(RID p_render_buffers);
  56. virtual void _base_uniforms_changed() = 0;
  57. virtual void _render_buffers_uniform_set_changed(RID p_render_buffers) = 0;
  58. virtual RID _render_buffers_get_roughness_texture(RID p_render_buffers) = 0;
  59. virtual RID _render_buffers_get_normal_texture(RID p_render_buffers) = 0;
  60. void _process_ssao(RID p_render_buffers, RID p_environment, RID p_normal_buffer, const CameraMatrix &p_projection);
  61. private:
  62. VS::ViewportDebugDraw debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED;
  63. double time_step = 0;
  64. int roughness_layers;
  65. RasterizerStorageRD *storage;
  66. struct ReflectionData {
  67. struct Layer {
  68. struct Mipmap {
  69. RID framebuffers[6];
  70. RID views[6];
  71. Size2i size;
  72. };
  73. Vector<Mipmap> mipmaps;
  74. };
  75. RID radiance_base_cubemap; //cubemap for first layer, first cubemap
  76. Vector<Layer> layers;
  77. };
  78. void _clear_reflection_data(ReflectionData &rd);
  79. void _update_reflection_data(ReflectionData &rd, int p_size, int p_mipmaps, bool p_use_array, RID p_base_cube, int p_base_layer);
  80. void _create_reflection_from_panorama(ReflectionData &rd, RID p_panorama, bool p_quality);
  81. void _create_reflection_from_base_mipmap(ReflectionData &rd, bool p_use_arrays, bool p_quality, int p_cube_side);
  82. void _update_reflection_mipmaps(ReflectionData &rd, bool p_quality);
  83. /* SKY */
  84. struct Sky {
  85. RID radiance;
  86. RID uniform_set;
  87. int radiance_size = 256;
  88. VS::SkyMode mode = VS::SKY_MODE_QUALITY;
  89. RID panorama;
  90. ReflectionData reflection;
  91. bool dirty = false;
  92. Sky *dirty_list = nullptr;
  93. };
  94. Sky *dirty_sky_list = nullptr;
  95. void _sky_invalidate(Sky *p_sky);
  96. void _update_dirty_skys();
  97. uint32_t sky_ggx_samples_quality;
  98. uint32_t sky_ggx_samples_realtime;
  99. bool sky_use_cubemap_array;
  100. mutable RID_Owner<Sky> sky_owner;
  101. /* REFLECTION ATLAS */
  102. struct ReflectionAtlas {
  103. int count = 0;
  104. int size = 0;
  105. RID reflection;
  106. RID depth_buffer;
  107. RID depth_fb;
  108. struct Reflection {
  109. RID owner;
  110. ReflectionData data;
  111. RID fbs[6];
  112. };
  113. Vector<Reflection> reflections;
  114. };
  115. RID_Owner<ReflectionAtlas> reflection_atlas_owner;
  116. /* REFLECTION PROBE INSTANCE */
  117. struct ReflectionProbeInstance {
  118. RID probe;
  119. int atlas_index = -1;
  120. RID atlas;
  121. bool dirty = true;
  122. bool rendering = false;
  123. int processing_side = 0;
  124. uint32_t render_step = 0;
  125. uint64_t last_pass = 0;
  126. uint32_t render_index = 0;
  127. Transform transform;
  128. };
  129. mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner;
  130. /* GIPROBE INSTANCE */
  131. struct GIProbeLight {
  132. uint32_t type;
  133. float energy;
  134. float radius;
  135. float attenuation;
  136. float color[3];
  137. float spot_angle_radians;
  138. float position[3];
  139. float spot_attenuation;
  140. float direction[3];
  141. uint32_t has_shadow;
  142. };
  143. struct GIProbePushConstant {
  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 GIProbeDynamicPushConstant {
  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. struct GIProbeInstance {
  176. RID probe;
  177. RID texture;
  178. RID anisotropy[2]; //only if anisotropy is used
  179. RID anisotropy_r16[2]; //only if anisotropy is used
  180. RID write_buffer;
  181. struct Mipmap {
  182. RID texture;
  183. RID anisotropy[2]; //only if anisotropy is used
  184. RID uniform_set;
  185. RID second_bounce_uniform_set;
  186. RID write_uniform_set;
  187. uint32_t level;
  188. uint32_t cell_offset;
  189. uint32_t cell_count;
  190. };
  191. Vector<Mipmap> mipmaps;
  192. struct DynamicMap {
  193. RID texture; //color normally, or emission on first pass
  194. RID fb_depth; //actual depth buffer for the first pass, float depth for later passes
  195. RID depth; //actual depth buffer for the first pass, float depth for later passes
  196. RID normal; //normal buffer for the first pass
  197. RID albedo; //emission buffer for the first pass
  198. RID orm; //orm buffer for the first pass
  199. RID fb; //used for rendering, only valid on first map
  200. RID uniform_set;
  201. uint32_t size;
  202. int mipmap; // mipmap to write to, -1 if no mipmap assigned
  203. };
  204. Vector<DynamicMap> dynamic_maps;
  205. int slot = -1;
  206. uint32_t last_probe_version = 0;
  207. uint32_t last_probe_data_version = 0;
  208. uint64_t last_pass = 0;
  209. uint32_t render_index = 0;
  210. bool has_dynamic_object_data = false;
  211. Transform transform;
  212. };
  213. GIProbeLight *gi_probe_lights;
  214. uint32_t gi_probe_max_lights;
  215. RID gi_probe_lights_uniform;
  216. bool gi_probe_use_anisotropy = false;
  217. GIProbeQuality gi_probe_quality = GIPROBE_QUALITY_MEDIUM;
  218. Vector<RID> gi_probe_slots;
  219. enum {
  220. GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT,
  221. GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE,
  222. GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP,
  223. GI_PROBE_SHADER_VERSION_WRITE_TEXTURE,
  224. GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING,
  225. GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE,
  226. GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT,
  227. GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT,
  228. GI_PROBE_SHADER_VERSION_MAX
  229. };
  230. GiprobeShaderRD giprobe_shader;
  231. RID giprobe_lighting_shader_version;
  232. RID giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_MAX];
  233. RID giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_MAX];
  234. mutable RID_Owner<GIProbeInstance> gi_probe_instance_owner;
  235. enum {
  236. GI_PROBE_DEBUG_COLOR,
  237. GI_PROBE_DEBUG_LIGHT,
  238. GI_PROBE_DEBUG_EMISSION,
  239. GI_PROBE_DEBUG_LIGHT_FULL,
  240. GI_PROBE_DEBUG_MAX
  241. };
  242. struct GIProbeDebugPushConstant {
  243. float projection[16];
  244. uint32_t cell_offset;
  245. float dynamic_range;
  246. float alpha;
  247. uint32_t level;
  248. int32_t bounds[3];
  249. uint32_t pad;
  250. };
  251. GiprobeDebugShaderRD giprobe_debug_shader;
  252. RID giprobe_debug_shader_version;
  253. RID giprobe_debug_shader_version_shaders[GI_PROBE_DEBUG_MAX];
  254. RenderPipelineVertexFormatCacheRD giprobe_debug_shader_version_pipelines[GI_PROBE_DEBUG_MAX];
  255. RID giprobe_debug_uniform_set;
  256. /* SHADOW ATLAS */
  257. struct ShadowAtlas {
  258. enum {
  259. QUADRANT_SHIFT = 27,
  260. SHADOW_INDEX_MASK = (1 << QUADRANT_SHIFT) - 1,
  261. SHADOW_INVALID = 0xFFFFFFFF
  262. };
  263. struct Quadrant {
  264. uint32_t subdivision;
  265. struct Shadow {
  266. RID owner;
  267. uint64_t version;
  268. uint64_t alloc_tick;
  269. Shadow() {
  270. version = 0;
  271. alloc_tick = 0;
  272. }
  273. };
  274. Vector<Shadow> shadows;
  275. Quadrant() {
  276. subdivision = 0; //not in use
  277. }
  278. } quadrants[4];
  279. int size_order[4] = { 0, 1, 2, 3 };
  280. uint32_t smallest_subdiv = 0;
  281. int size = 0;
  282. RID depth;
  283. RID fb; //for copying
  284. Map<RID, uint32_t> shadow_owners;
  285. };
  286. RID_Owner<ShadowAtlas> shadow_atlas_owner;
  287. 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);
  288. /* DIRECTIONAL SHADOW */
  289. struct DirectionalShadow {
  290. RID depth;
  291. RID fb; //for copying
  292. int light_count = 0;
  293. int size = 0;
  294. int current_light = 0;
  295. } directional_shadow;
  296. /* SHADOW CUBEMAPS */
  297. struct ShadowCubemap {
  298. RID cubemap;
  299. RID side_fb[6];
  300. };
  301. Map<int, ShadowCubemap> shadow_cubemaps;
  302. ShadowCubemap *_get_shadow_cubemap(int p_size);
  303. struct ShadowMap {
  304. RID depth;
  305. RID fb;
  306. };
  307. Map<Vector2i, ShadowMap> shadow_maps;
  308. ShadowMap *_get_shadow_map(const Size2i &p_size);
  309. void _create_shadow_cubemaps();
  310. /* LIGHT INSTANCE */
  311. struct LightInstance {
  312. struct ShadowTransform {
  313. CameraMatrix camera;
  314. Transform transform;
  315. float farplane;
  316. float split;
  317. float bias_scale;
  318. Rect2 atlas_rect;
  319. };
  320. VS::LightType light_type;
  321. ShadowTransform shadow_transform[4];
  322. RID self;
  323. RID light;
  324. Transform transform;
  325. Vector3 light_vector;
  326. Vector3 spot_vector;
  327. float linear_att;
  328. uint64_t shadow_pass = 0;
  329. uint64_t last_scene_pass = 0;
  330. uint64_t last_scene_shadow_pass = 0;
  331. uint64_t last_pass = 0;
  332. uint32_t light_index = 0;
  333. uint32_t light_directional_index = 0;
  334. uint32_t current_shadow_atlas_key;
  335. Vector2 dp;
  336. Rect2 directional_rect;
  337. Set<RID> shadow_atlases; //shadow atlases where this light is registered
  338. LightInstance() {}
  339. };
  340. mutable RID_Owner<LightInstance> light_instance_owner;
  341. /* ENVIRONMENT */
  342. struct Environent {
  343. // BG
  344. VS::EnvironmentBG background = VS::ENV_BG_CLEAR_COLOR;
  345. RID sky;
  346. float sky_custom_fov = 0.0;
  347. Basis sky_orientation;
  348. Color bg_color;
  349. float bg_energy = 1.0;
  350. int canvas_max_layer = 0;
  351. VS::EnvironmentAmbientSource ambient_source = VS::ENV_AMBIENT_SOURCE_BG;
  352. Color ambient_light;
  353. float ambient_light_energy = 1.0;
  354. float ambient_sky_contribution = 1.0;
  355. VS::EnvironmentReflectionSource reflection_source = VS::ENV_REFLECTION_SOURCE_BG;
  356. Color ao_color;
  357. /// Tonemap
  358. VS::EnvironmentToneMapper tone_mapper;
  359. float exposure = 1.0;
  360. float white = 1.0;
  361. bool auto_exposure = false;
  362. float min_luminance = 0.2;
  363. float max_luminance = 8.0;
  364. float auto_exp_speed = 0.2;
  365. float auto_exp_scale = 0.5;
  366. uint64_t auto_exposure_version = 0;
  367. /// Glow
  368. bool glow_enabled = false;
  369. int glow_levels = (1 << 2) | (1 << 4);
  370. float glow_intensity = 0.8;
  371. float glow_strength = 1.0;
  372. float glow_bloom = 0.0;
  373. float glow_mix = 0.01;
  374. VS::EnvironmentGlowBlendMode glow_blend_mode = VS::GLOW_BLEND_MODE_SOFTLIGHT;
  375. float glow_hdr_bleed_threshold = 1.0;
  376. float glow_hdr_luminance_cap = 12.0;
  377. float glow_hdr_bleed_scale = 2.0;
  378. bool glow_bicubic_upscale = false;
  379. /// SSAO
  380. bool ssao_enabled = false;
  381. float ssao_radius = 1;
  382. float ssao_intensity = 1;
  383. float ssao_bias = 0.01;
  384. float ssao_direct_light_affect = 0.0;
  385. float ssao_ao_channel_affect = 0.0;
  386. float ssao_blur_edge_sharpness = 4.0;
  387. VS::EnvironmentSSAOBlur ssao_blur = VS::ENV_SSAO_BLUR_3x3;
  388. };
  389. VS::EnvironmentSSAOQuality ssao_quality = VS::ENV_SSAO_QUALITY_MEDIUM;
  390. bool ssao_half_size = false;
  391. static uint64_t auto_exposure_counter;
  392. mutable RID_Owner<Environent> environment_owner;
  393. /* CAMERA EFFECTS */
  394. struct CameraEffects {
  395. bool dof_blur_far_enabled = false;
  396. float dof_blur_far_distance = 10;
  397. float dof_blur_far_transition = 5;
  398. bool dof_blur_near_enabled = false;
  399. float dof_blur_near_distance = 2;
  400. float dof_blur_near_transition = 1;
  401. float dof_blur_amount = 0.1;
  402. bool override_exposure_enabled = false;
  403. float override_exposure = 1;
  404. };
  405. VS::DOFBlurQuality dof_blur_quality = VS::DOF_BLUR_QUALITY_MEDIUM;
  406. VS::DOFBokehShape dof_blur_bokeh_shape = VS::DOF_BOKEH_HEXAGON;
  407. bool dof_blur_use_jitter = false;
  408. mutable RID_Owner<CameraEffects> camera_effects_owner;
  409. /* RENDER BUFFERS */
  410. struct RenderBuffers {
  411. RenderBufferData *data = nullptr;
  412. int width = 0, height = 0;
  413. VS::ViewportMSAA msaa = VS::VIEWPORT_MSAA_DISABLED;
  414. RID render_target;
  415. uint64_t auto_exposure_version = 1;
  416. RID texture; //main texture for rendering to, must be filled after done rendering
  417. RID depth_texture; //main depth texture
  418. //built-in textures used for ping pong image processing and blurring
  419. struct Blur {
  420. RID texture;
  421. struct Mipmap {
  422. RID texture;
  423. RID framebuffer;
  424. int width;
  425. int height;
  426. };
  427. Vector<Mipmap> mipmaps;
  428. };
  429. Blur blur[2]; //the second one starts from the first mipmap
  430. struct Luminance {
  431. Vector<RID> reduce;
  432. RID current;
  433. } luminance;
  434. struct SSAO {
  435. RID depth;
  436. Vector<RID> depth_slices;
  437. RID ao[2];
  438. RID ao_full; //when using half-size
  439. } ssao;
  440. };
  441. bool screen_space_roughness_limiter = false;
  442. float screen_space_roughness_limiter_curve = 1.0;
  443. mutable RID_Owner<RenderBuffers> render_buffers_owner;
  444. void _free_render_buffer_data(RenderBuffers *rb);
  445. void _allocate_blur_textures(RenderBuffers *rb);
  446. void _allocate_luminance_textures(RenderBuffers *rb);
  447. void _render_buffers_debug_draw(RID p_render_buffers, RID p_shadow_atlas);
  448. void _render_buffers_post_process_and_tonemap(RID p_render_buffers, RID p_environment, RID p_camera_effects, const CameraMatrix &p_projection);
  449. uint64_t scene_pass = 0;
  450. uint64_t shadow_atlas_realloc_tolerance_msec = 500;
  451. public:
  452. /* SHADOW ATLAS API */
  453. RID shadow_atlas_create();
  454. void shadow_atlas_set_size(RID p_atlas, int p_size);
  455. void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision);
  456. bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version);
  457. _FORCE_INLINE_ bool shadow_atlas_owns_light_instance(RID p_atlas, RID p_light_intance) {
  458. ShadowAtlas *atlas = shadow_atlas_owner.getornull(p_atlas);
  459. ERR_FAIL_COND_V(!atlas, false);
  460. return atlas->shadow_owners.has(p_light_intance);
  461. }
  462. _FORCE_INLINE_ RID shadow_atlas_get_texture(RID p_atlas) {
  463. ShadowAtlas *atlas = shadow_atlas_owner.getornull(p_atlas);
  464. ERR_FAIL_COND_V(!atlas, RID());
  465. return atlas->depth;
  466. }
  467. _FORCE_INLINE_ Size2i shadow_atlas_get_size(RID p_atlas) {
  468. ShadowAtlas *atlas = shadow_atlas_owner.getornull(p_atlas);
  469. ERR_FAIL_COND_V(!atlas, Size2i());
  470. return Size2(atlas->size, atlas->size);
  471. }
  472. void directional_shadow_atlas_set_size(int p_size);
  473. int get_directional_light_shadow_size(RID p_light_intance);
  474. void set_directional_shadow_count(int p_count);
  475. _FORCE_INLINE_ RID directional_shadow_get_texture() {
  476. return directional_shadow.depth;
  477. }
  478. _FORCE_INLINE_ Size2i directional_shadow_get_size() {
  479. return Size2i(directional_shadow.size, directional_shadow.size);
  480. }
  481. /* SKY API */
  482. RID sky_create();
  483. void sky_set_radiance_size(RID p_sky, int p_radiance_size);
  484. void sky_set_mode(RID p_sky, VS::SkyMode p_mode);
  485. void sky_set_texture(RID p_sky, RID p_panorama);
  486. RID sky_get_panorama_texture_rd(RID p_sky) const;
  487. RID sky_get_radiance_texture_rd(RID p_sky) const;
  488. RID sky_get_radiance_uniform_set_rd(RID p_sky, RID p_shader, int p_set) const;
  489. /* ENVIRONMENT API */
  490. RID environment_create();
  491. void environment_set_background(RID p_env, VS::EnvironmentBG p_bg);
  492. void environment_set_sky(RID p_env, RID p_sky);
  493. void environment_set_sky_custom_fov(RID p_env, float p_scale);
  494. void environment_set_sky_orientation(RID p_env, const Basis &p_orientation);
  495. void environment_set_bg_color(RID p_env, const Color &p_color);
  496. void environment_set_bg_energy(RID p_env, float p_energy);
  497. void environment_set_canvas_max_layer(RID p_env, int p_max_layer);
  498. void environment_set_ambient_light(RID p_env, const Color &p_color, VS::EnvironmentAmbientSource p_ambient = VS::ENV_AMBIENT_SOURCE_BG, float p_energy = 1.0, float p_sky_contribution = 0.0, VS::EnvironmentReflectionSource p_reflection_source = VS::ENV_REFLECTION_SOURCE_BG, const Color &p_ao_color = Color());
  499. VS::EnvironmentBG environment_get_background(RID p_env) const;
  500. RID environment_get_sky(RID p_env) const;
  501. float environment_get_sky_custom_fov(RID p_env) const;
  502. Basis environment_get_sky_orientation(RID p_env) const;
  503. Color environment_get_bg_color(RID p_env) const;
  504. float environment_get_bg_energy(RID p_env) const;
  505. int environment_get_canvas_max_layer(RID p_env) const;
  506. Color environment_get_ambient_light_color(RID p_env) const;
  507. VS::EnvironmentAmbientSource environment_get_ambient_light_ambient_source(RID p_env) const;
  508. float environment_get_ambient_light_ambient_energy(RID p_env) const;
  509. float environment_get_ambient_sky_contribution(RID p_env) const;
  510. VS::EnvironmentReflectionSource environment_get_reflection_source(RID p_env) const;
  511. Color environment_get_ao_color(RID p_env) const;
  512. bool is_environment(RID p_env) const;
  513. void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale);
  514. void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) {}
  515. 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, bool p_roughness) {}
  516. void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_bias, float p_light_affect, float p_ao_channel_affect, VS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness);
  517. void environment_set_ssao_quality(VS::EnvironmentSSAOQuality p_quality, bool p_half_size);
  518. bool environment_is_ssao_enabled(RID p_env) const;
  519. float environment_get_ssao_ao_affect(RID p_env) const;
  520. float environment_get_ssao_light_affect(RID p_env) const;
  521. bool environment_is_ssr_enabled(RID p_env) const;
  522. void environment_set_tonemap(RID p_env, VS::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);
  523. void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) {}
  524. void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) {}
  525. void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) {}
  526. void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) {}
  527. virtual RID camera_effects_create();
  528. virtual void camera_effects_set_dof_blur_quality(VS::DOFBlurQuality p_quality, bool p_use_jitter);
  529. virtual void camera_effects_set_dof_blur_bokeh_shape(VS::DOFBokehShape p_shape);
  530. 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);
  531. virtual void camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure);
  532. RID light_instance_create(RID p_light);
  533. void light_instance_set_transform(RID p_light_instance, const Transform &p_transform);
  534. 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_bias_scale = 1.0);
  535. void light_instance_mark_visible(RID p_light_instance);
  536. _FORCE_INLINE_ RID light_instance_get_base_light(RID p_light_instance) {
  537. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  538. return li->light;
  539. }
  540. _FORCE_INLINE_ Transform light_instance_get_base_transform(RID p_light_instance) {
  541. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  542. return li->transform;
  543. }
  544. _FORCE_INLINE_ Rect2 light_instance_get_shadow_atlas_rect(RID p_light_instance, RID p_shadow_atlas) {
  545. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  546. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  547. uint32_t key = shadow_atlas->shadow_owners[li->self];
  548. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  549. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  550. ERR_FAIL_COND_V(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size(), Rect2());
  551. uint32_t atlas_size = shadow_atlas->size;
  552. uint32_t quadrant_size = atlas_size >> 1;
  553. uint32_t x = (quadrant & 1) * quadrant_size;
  554. uint32_t y = (quadrant >> 1) * quadrant_size;
  555. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  556. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  557. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  558. uint32_t width = shadow_size;
  559. uint32_t height = shadow_size;
  560. return Rect2(x / float(shadow_atlas->size), y / float(shadow_atlas->size), width / float(shadow_atlas->size), height / float(shadow_atlas->size));
  561. }
  562. _FORCE_INLINE_ CameraMatrix light_instance_get_shadow_camera(RID p_light_instance, int p_index) {
  563. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  564. return li->shadow_transform[p_index].camera;
  565. }
  566. _FORCE_INLINE_ Transform light_instance_get_shadow_transform(RID p_light_instance, int p_index) {
  567. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  568. return li->shadow_transform[p_index].transform;
  569. }
  570. _FORCE_INLINE_ Rect2 light_instance_get_directional_shadow_atlas_rect(RID p_light_instance, int p_index) {
  571. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  572. return li->shadow_transform[p_index].atlas_rect;
  573. }
  574. _FORCE_INLINE_ float light_instance_get_directional_shadow_split(RID p_light_instance, int p_index) {
  575. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  576. return li->shadow_transform[p_index].split;
  577. }
  578. _FORCE_INLINE_ void light_instance_set_render_pass(RID p_light_instance, uint64_t p_pass) {
  579. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  580. li->last_pass = p_pass;
  581. }
  582. _FORCE_INLINE_ uint64_t light_instance_get_render_pass(RID p_light_instance) {
  583. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  584. return li->last_pass;
  585. }
  586. _FORCE_INLINE_ void light_instance_set_index(RID p_light_instance, uint32_t p_index) {
  587. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  588. li->light_index = p_index;
  589. }
  590. _FORCE_INLINE_ uint32_t light_instance_get_index(RID p_light_instance) {
  591. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  592. return li->light_index;
  593. }
  594. _FORCE_INLINE_ VS::LightType light_instance_get_type(RID p_light_instance) {
  595. LightInstance *li = light_instance_owner.getornull(p_light_instance);
  596. return li->light_type;
  597. }
  598. virtual RID reflection_atlas_create();
  599. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count);
  600. _FORCE_INLINE_ RID reflection_atlas_get_texture(RID p_ref_atlas) {
  601. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(p_ref_atlas);
  602. ERR_FAIL_COND_V(!atlas, RID());
  603. return atlas->reflection;
  604. }
  605. virtual RID reflection_probe_instance_create(RID p_probe);
  606. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform);
  607. virtual void reflection_probe_release_atlas_index(RID p_instance);
  608. virtual bool reflection_probe_instance_needs_redraw(RID p_instance);
  609. virtual bool reflection_probe_instance_has_reflection(RID p_instance);
  610. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas);
  611. virtual bool reflection_probe_instance_postprocess_step(RID p_instance);
  612. uint32_t reflection_probe_instance_get_resolution(RID p_instance);
  613. RID reflection_probe_instance_get_framebuffer(RID p_instance, int p_index);
  614. RID reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index);
  615. _FORCE_INLINE_ RID reflection_probe_instance_get_probe(RID p_instance) {
  616. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  617. ERR_FAIL_COND_V(!rpi, RID());
  618. return rpi->probe;
  619. }
  620. _FORCE_INLINE_ void reflection_probe_instance_set_render_index(RID p_instance, uint32_t p_render_index) {
  621. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  622. ERR_FAIL_COND(!rpi);
  623. rpi->render_index = p_render_index;
  624. }
  625. _FORCE_INLINE_ uint32_t reflection_probe_instance_get_render_index(RID p_instance) {
  626. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  627. ERR_FAIL_COND_V(!rpi, 0);
  628. return rpi->render_index;
  629. }
  630. _FORCE_INLINE_ void reflection_probe_instance_set_render_pass(RID p_instance, uint32_t p_render_pass) {
  631. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  632. ERR_FAIL_COND(!rpi);
  633. rpi->last_pass = p_render_pass;
  634. }
  635. _FORCE_INLINE_ uint32_t reflection_probe_instance_get_render_pass(RID p_instance) {
  636. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  637. ERR_FAIL_COND_V(!rpi, 0);
  638. return rpi->last_pass;
  639. }
  640. _FORCE_INLINE_ Transform reflection_probe_instance_get_transform(RID p_instance) {
  641. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  642. ERR_FAIL_COND_V(!rpi, Transform());
  643. return rpi->transform;
  644. }
  645. _FORCE_INLINE_ int reflection_probe_instance_get_atlas_index(RID p_instance) {
  646. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  647. ERR_FAIL_COND_V(!rpi, -1);
  648. return rpi->atlas_index;
  649. }
  650. RID gi_probe_instance_create(RID p_base);
  651. void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform);
  652. bool gi_probe_needs_update(RID p_probe) const;
  653. void gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, int p_dynamic_object_count, InstanceBase **p_dynamic_objects);
  654. _FORCE_INLINE_ uint32_t gi_probe_instance_get_slot(RID p_probe) {
  655. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  656. return gi_probe->slot;
  657. }
  658. _FORCE_INLINE_ RID gi_probe_instance_get_base_probe(RID p_probe) {
  659. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  660. return gi_probe->probe;
  661. }
  662. _FORCE_INLINE_ Transform gi_probe_instance_get_transform_to_cell(RID p_probe) {
  663. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  664. return storage->gi_probe_get_to_cell_xform(gi_probe->probe) * gi_probe->transform.affine_inverse();
  665. }
  666. _FORCE_INLINE_ RID gi_probe_instance_get_texture(RID p_probe) {
  667. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  668. return gi_probe->texture;
  669. }
  670. _FORCE_INLINE_ RID gi_probe_instance_get_aniso_texture(RID p_probe, int p_index) {
  671. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  672. return gi_probe->anisotropy[p_index];
  673. }
  674. _FORCE_INLINE_ void gi_probe_instance_set_render_index(RID p_instance, uint32_t p_render_index) {
  675. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_instance);
  676. ERR_FAIL_COND(!gi_probe);
  677. gi_probe->render_index = p_render_index;
  678. }
  679. _FORCE_INLINE_ uint32_t gi_probe_instance_get_render_index(RID p_instance) {
  680. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_instance);
  681. ERR_FAIL_COND_V(!gi_probe, 0);
  682. return gi_probe->render_index;
  683. }
  684. _FORCE_INLINE_ void gi_probe_instance_set_render_pass(RID p_instance, uint32_t p_render_pass) {
  685. GIProbeInstance *g_probe = gi_probe_instance_owner.getornull(p_instance);
  686. ERR_FAIL_COND(!g_probe);
  687. g_probe->last_pass = p_render_pass;
  688. }
  689. _FORCE_INLINE_ uint32_t gi_probe_instance_get_render_pass(RID p_instance) {
  690. GIProbeInstance *g_probe = gi_probe_instance_owner.getornull(p_instance);
  691. ERR_FAIL_COND_V(!g_probe, 0);
  692. return g_probe->last_pass;
  693. }
  694. const Vector<RID> &gi_probe_get_slots() const;
  695. _FORCE_INLINE_ bool gi_probe_is_anisotropic() const {
  696. return gi_probe_use_anisotropy;
  697. }
  698. GIProbeQuality gi_probe_get_quality() const;
  699. RID render_buffers_create();
  700. void render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, VS::ViewportMSAA p_msaa);
  701. RID render_buffers_get_ao_texture(RID p_render_buffers);
  702. RID render_buffers_get_back_buffer_texture(RID p_render_buffers);
  703. void render_scene(RID p_render_buffers, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_camera_effects, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass);
  704. void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count);
  705. void render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region);
  706. virtual void set_scene_pass(uint64_t p_pass) { scene_pass = p_pass; }
  707. _FORCE_INLINE_ uint64_t get_scene_pass() { return scene_pass; }
  708. virtual void screen_space_roughness_limiter_set_active(bool p_enable, float p_curve);
  709. virtual bool screen_space_roughness_limiter_is_active() const;
  710. virtual float screen_space_roughness_limiter_get_curve() const;
  711. int get_roughness_layers() const;
  712. bool is_using_radiance_cubemap_array() const;
  713. virtual bool free(RID p_rid);
  714. virtual void update();
  715. virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw);
  716. _FORCE_INLINE_ VS::ViewportDebugDraw get_debug_draw_mode() const { return debug_draw; }
  717. virtual void set_time(double p_time, double p_step);
  718. RasterizerSceneRD(RasterizerStorageRD *p_storage);
  719. ~RasterizerSceneRD();
  720. };
  721. #endif // RASTERIZER_SCENE_RD_H