rasterizer_scene_gles3.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /**************************************************************************/
  2. /* rasterizer_scene_gles3.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 RASTERIZER_SCENE_GLES3_H
  31. #define RASTERIZER_SCENE_GLES3_H
  32. #ifdef GLES3_ENABLED
  33. #include "core/math/projection.h"
  34. #include "core/templates/paged_allocator.h"
  35. #include "core/templates/rid_owner.h"
  36. #include "core/templates/self_list.h"
  37. #include "scene/resources/mesh.h"
  38. #include "servers/rendering/renderer_compositor.h"
  39. #include "servers/rendering/renderer_scene_render.h"
  40. #include "servers/rendering_server.h"
  41. #include "shader_gles3.h"
  42. #include "shaders/cubemap_filter.glsl.gen.h"
  43. #include "shaders/sky.glsl.gen.h"
  44. #include "storage/light_storage.h"
  45. #include "storage/material_storage.h"
  46. #include "storage/render_scene_buffers_gles3.h"
  47. #include "storage/utilities.h"
  48. enum RenderListType {
  49. RENDER_LIST_OPAQUE, //used for opaque objects
  50. RENDER_LIST_ALPHA, //used for transparent objects
  51. RENDER_LIST_SECONDARY, //used for shadows and other objects
  52. RENDER_LIST_MAX
  53. };
  54. enum PassMode {
  55. PASS_MODE_COLOR,
  56. PASS_MODE_COLOR_TRANSPARENT,
  57. PASS_MODE_COLOR_ADDITIVE,
  58. PASS_MODE_SHADOW,
  59. PASS_MODE_DEPTH,
  60. };
  61. // These should share as much as possible with SkyUniform Location
  62. enum SceneUniformLocation {
  63. SCENE_TONEMAP_UNIFORM_LOCATION,
  64. SCENE_GLOBALS_UNIFORM_LOCATION,
  65. SCENE_DATA_UNIFORM_LOCATION,
  66. SCENE_MATERIAL_UNIFORM_LOCATION,
  67. SCENE_EMPTY, // Unused, put here to avoid conflicts with SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION.
  68. SCENE_OMNILIGHT_UNIFORM_LOCATION,
  69. SCENE_SPOTLIGHT_UNIFORM_LOCATION,
  70. SCENE_DIRECTIONAL_LIGHT_UNIFORM_LOCATION,
  71. SCENE_MULTIVIEW_UNIFORM_LOCATION,
  72. };
  73. enum SkyUniformLocation {
  74. SKY_TONEMAP_UNIFORM_LOCATION,
  75. SKY_GLOBALS_UNIFORM_LOCATION,
  76. SKY_EMPTY, // Unused, put here to avoid conflicts with SCENE_DATA_UNIFORM_LOCATION.
  77. SKY_MATERIAL_UNIFORM_LOCATION,
  78. SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION,
  79. SKY_MULTIVIEW_UNIFORM_LOCATION,
  80. };
  81. struct RenderDataGLES3 {
  82. Ref<RenderSceneBuffersGLES3> render_buffers;
  83. bool transparent_bg = false;
  84. Transform3D cam_transform;
  85. Transform3D inv_cam_transform;
  86. Projection cam_projection;
  87. bool cam_orthogonal = false;
  88. uint32_t camera_visible_layers = 0xFFFFFFFF;
  89. // For stereo rendering
  90. uint32_t view_count = 1;
  91. Vector3 view_eye_offset[RendererSceneRender::MAX_RENDER_VIEWS];
  92. Projection view_projection[RendererSceneRender::MAX_RENDER_VIEWS];
  93. float z_near = 0.0;
  94. float z_far = 0.0;
  95. const PagedArray<RenderGeometryInstance *> *instances = nullptr;
  96. const PagedArray<RID> *lights = nullptr;
  97. const PagedArray<RID> *reflection_probes = nullptr;
  98. RID environment;
  99. RID camera_attributes;
  100. RID reflection_probe;
  101. int reflection_probe_pass = 0;
  102. float lod_distance_multiplier = 0.0;
  103. float screen_mesh_lod_threshold = 0.0;
  104. uint32_t directional_light_count = 0;
  105. uint32_t spot_light_count = 0;
  106. uint32_t omni_light_count = 0;
  107. RenderingMethod::RenderInfo *render_info = nullptr;
  108. };
  109. class RasterizerCanvasGLES3;
  110. class RasterizerSceneGLES3 : public RendererSceneRender {
  111. private:
  112. static RasterizerSceneGLES3 *singleton;
  113. RS::ViewportDebugDraw debug_draw = RS::VIEWPORT_DEBUG_DRAW_DISABLED;
  114. uint64_t scene_pass = 0;
  115. template <class T>
  116. struct InstanceSort {
  117. float depth;
  118. T *instance = nullptr;
  119. bool operator<(const InstanceSort &p_sort) const {
  120. return depth < p_sort.depth;
  121. }
  122. };
  123. struct SceneGlobals {
  124. RID shader_default_version;
  125. RID default_material;
  126. RID default_shader;
  127. RID cubemap_filter_shader_version;
  128. } scene_globals;
  129. /* LIGHT INSTANCE */
  130. struct LightData {
  131. float position[3];
  132. float inv_radius;
  133. float direction[3]; // Only used by SpotLight
  134. float size;
  135. float color[3];
  136. float attenuation;
  137. float inv_spot_attenuation;
  138. float cos_spot_angle;
  139. float specular_amount;
  140. float shadow_opacity;
  141. };
  142. static_assert(sizeof(LightData) % 16 == 0, "LightData size must be a multiple of 16 bytes");
  143. struct DirectionalLightData {
  144. float direction[3];
  145. float energy;
  146. float color[3];
  147. float size;
  148. uint32_t enabled; // For use by SkyShaders
  149. float pad[2];
  150. float specular;
  151. };
  152. static_assert(sizeof(DirectionalLightData) % 16 == 0, "DirectionalLightData size must be a multiple of 16 bytes");
  153. class GeometryInstanceGLES3;
  154. // Cached data for drawing surfaces
  155. struct GeometryInstanceSurface {
  156. enum {
  157. FLAG_PASS_DEPTH = 1,
  158. FLAG_PASS_OPAQUE = 2,
  159. FLAG_PASS_ALPHA = 4,
  160. FLAG_PASS_SHADOW = 8,
  161. FLAG_USES_SHARED_SHADOW_MATERIAL = 128,
  162. FLAG_USES_SCREEN_TEXTURE = 2048,
  163. FLAG_USES_DEPTH_TEXTURE = 4096,
  164. FLAG_USES_NORMAL_TEXTURE = 8192,
  165. FLAG_USES_DOUBLE_SIDED_SHADOWS = 16384,
  166. };
  167. union {
  168. struct {
  169. uint64_t lod_index : 8;
  170. uint64_t surface_index : 8;
  171. uint64_t geometry_id : 32;
  172. uint64_t material_id_low : 16;
  173. uint64_t material_id_hi : 16;
  174. uint64_t shader_id : 32;
  175. uint64_t uses_softshadow : 1;
  176. uint64_t uses_projector : 1;
  177. uint64_t uses_forward_gi : 1;
  178. uint64_t uses_lightmap : 1;
  179. uint64_t depth_layer : 4;
  180. uint64_t priority : 8;
  181. };
  182. struct {
  183. uint64_t sort_key1;
  184. uint64_t sort_key2;
  185. };
  186. } sort;
  187. RS::PrimitiveType primitive = RS::PRIMITIVE_MAX;
  188. uint32_t flags = 0;
  189. uint32_t surface_index = 0;
  190. uint32_t lod_index = 0;
  191. void *surface = nullptr;
  192. GLES3::SceneShaderData *shader = nullptr;
  193. GLES3::SceneMaterialData *material = nullptr;
  194. void *surface_shadow = nullptr;
  195. GLES3::SceneShaderData *shader_shadow = nullptr;
  196. GLES3::SceneMaterialData *material_shadow = nullptr;
  197. GeometryInstanceSurface *next = nullptr;
  198. GeometryInstanceGLES3 *owner = nullptr;
  199. };
  200. class GeometryInstanceGLES3 : public RenderGeometryInstanceBase {
  201. public:
  202. //used during rendering
  203. bool store_transform_cache = true;
  204. int32_t instance_count = 0;
  205. bool can_sdfgi = false;
  206. bool using_projectors = false;
  207. bool using_softshadows = false;
  208. uint32_t omni_light_count = 0;
  209. LocalVector<RID> omni_lights;
  210. uint32_t spot_light_count = 0;
  211. LocalVector<RID> spot_lights;
  212. LocalVector<uint32_t> omni_light_gl_cache;
  213. LocalVector<uint32_t> spot_light_gl_cache;
  214. //used during setup
  215. GeometryInstanceSurface *surface_caches = nullptr;
  216. SelfList<GeometryInstanceGLES3> dirty_list_element;
  217. GeometryInstanceGLES3() :
  218. dirty_list_element(this) {}
  219. virtual void _mark_dirty() override;
  220. virtual void set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) override;
  221. virtual void set_lightmap_capture(const Color *p_sh9) override;
  222. virtual void pair_light_instances(const RID *p_light_instances, uint32_t p_light_instance_count) override;
  223. virtual void pair_reflection_probe_instances(const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count) override {}
  224. virtual void pair_decal_instances(const RID *p_decal_instances, uint32_t p_decal_instance_count) override {}
  225. virtual void pair_voxel_gi_instances(const RID *p_voxel_gi_instances, uint32_t p_voxel_gi_instance_count) override {}
  226. virtual void set_softshadow_projector_pairing(bool p_softshadow, bool p_projector) override {}
  227. };
  228. enum {
  229. INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE = 1 << 4,
  230. INSTANCE_DATA_FLAG_USE_GI_BUFFERS = 1 << 5,
  231. INSTANCE_DATA_FLAG_USE_LIGHTMAP_CAPTURE = 1 << 7,
  232. INSTANCE_DATA_FLAG_USE_LIGHTMAP = 1 << 8,
  233. INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP = 1 << 9,
  234. INSTANCE_DATA_FLAG_USE_VOXEL_GI = 1 << 10,
  235. INSTANCE_DATA_FLAG_PARTICLES = 1 << 11,
  236. INSTANCE_DATA_FLAG_MULTIMESH = 1 << 12,
  237. INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D = 1 << 13,
  238. INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR = 1 << 14,
  239. INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA = 1 << 15,
  240. };
  241. static void _geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker);
  242. static void _geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker);
  243. SelfList<GeometryInstanceGLES3>::List geometry_instance_dirty_list;
  244. // Use PagedAllocator instead of RID to maximize performance
  245. PagedAllocator<GeometryInstanceGLES3> geometry_instance_alloc;
  246. PagedAllocator<GeometryInstanceSurface> geometry_instance_surface_alloc;
  247. void _geometry_instance_add_surface_with_material(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material, uint32_t p_material_id, uint32_t p_shader_id, RID p_mesh);
  248. void _geometry_instance_add_surface_with_material_chain(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material, RID p_mat_src, RID p_mesh);
  249. void _geometry_instance_add_surface(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, RID p_material, RID p_mesh);
  250. void _geometry_instance_update(RenderGeometryInstance *p_geometry_instance);
  251. void _update_dirty_geometry_instances();
  252. struct SceneState {
  253. struct UBO {
  254. float projection_matrix[16];
  255. float inv_projection_matrix[16];
  256. float inv_view_matrix[16];
  257. float view_matrix[16];
  258. float viewport_size[2];
  259. float screen_pixel_size[2];
  260. float ambient_light_color_energy[4];
  261. float ambient_color_sky_mix;
  262. uint32_t material_uv2_mode;
  263. float emissive_exposure_normalization;
  264. uint32_t use_ambient_light = 0;
  265. uint32_t use_ambient_cubemap = 0;
  266. uint32_t use_reflection_cubemap = 0;
  267. float fog_aerial_perspective;
  268. float time;
  269. float radiance_inverse_xform[12];
  270. uint32_t directional_light_count;
  271. float z_far;
  272. float z_near;
  273. float IBL_exposure_normalization;
  274. uint32_t fog_enabled;
  275. float fog_density;
  276. float fog_height;
  277. float fog_height_density;
  278. float fog_light_color[3];
  279. float fog_sun_scatter;
  280. uint32_t camera_visible_layers;
  281. uint32_t pad1;
  282. uint32_t pad2;
  283. uint32_t pad3;
  284. };
  285. static_assert(sizeof(UBO) % 16 == 0, "Scene UBO size must be a multiple of 16 bytes");
  286. struct MultiviewUBO {
  287. float projection_matrix_view[RendererSceneRender::MAX_RENDER_VIEWS][16];
  288. float inv_projection_matrix_view[RendererSceneRender::MAX_RENDER_VIEWS][16];
  289. float eye_offset[RendererSceneRender::MAX_RENDER_VIEWS][4];
  290. };
  291. static_assert(sizeof(MultiviewUBO) % 16 == 0, "Multiview UBO size must be a multiple of 16 bytes");
  292. struct TonemapUBO {
  293. float exposure = 1.0;
  294. float white = 1.0;
  295. int32_t tonemapper = 0;
  296. int32_t pad = 0;
  297. };
  298. static_assert(sizeof(TonemapUBO) % 16 == 0, "Tonemap UBO size must be a multiple of 16 bytes");
  299. UBO ubo;
  300. GLuint ubo_buffer = 0;
  301. MultiviewUBO multiview_ubo;
  302. GLuint multiview_buffer = 0;
  303. GLuint tonemap_buffer = 0;
  304. bool used_depth_prepass = false;
  305. GLES3::SceneShaderData::BlendMode current_blend_mode = GLES3::SceneShaderData::BLEND_MODE_MIX;
  306. GLES3::SceneShaderData::DepthDraw current_depth_draw = GLES3::SceneShaderData::DEPTH_DRAW_OPAQUE;
  307. GLES3::SceneShaderData::DepthTest current_depth_test = GLES3::SceneShaderData::DEPTH_TEST_DISABLED;
  308. GLES3::SceneShaderData::Cull cull_mode = GLES3::SceneShaderData::CULL_BACK;
  309. bool texscreen_copied = false;
  310. bool used_screen_texture = false;
  311. bool used_normal_texture = false;
  312. bool used_depth_texture = false;
  313. LightData *omni_lights = nullptr;
  314. LightData *spot_lights = nullptr;
  315. InstanceSort<GLES3::LightInstance> *omni_light_sort;
  316. InstanceSort<GLES3::LightInstance> *spot_light_sort;
  317. GLuint omni_light_buffer = 0;
  318. GLuint spot_light_buffer = 0;
  319. uint32_t omni_light_count = 0;
  320. uint32_t spot_light_count = 0;
  321. DirectionalLightData *directional_lights = nullptr;
  322. GLuint directional_light_buffer = 0;
  323. } scene_state;
  324. struct RenderListParameters {
  325. GeometryInstanceSurface **elements = nullptr;
  326. int element_count = 0;
  327. bool reverse_cull = false;
  328. uint64_t spec_constant_base_flags = 0;
  329. bool force_wireframe = false;
  330. RenderListParameters(GeometryInstanceSurface **p_elements, int p_element_count, bool p_reverse_cull, uint64_t p_spec_constant_base_flags, bool p_force_wireframe = false) {
  331. elements = p_elements;
  332. element_count = p_element_count;
  333. reverse_cull = p_reverse_cull;
  334. spec_constant_base_flags = p_spec_constant_base_flags;
  335. force_wireframe = p_force_wireframe;
  336. }
  337. };
  338. struct RenderList {
  339. LocalVector<GeometryInstanceSurface *> elements;
  340. void clear() {
  341. elements.clear();
  342. }
  343. //should eventually be replaced by radix
  344. struct SortByKey {
  345. _FORCE_INLINE_ bool operator()(const GeometryInstanceSurface *A, const GeometryInstanceSurface *B) const {
  346. return (A->sort.sort_key2 == B->sort.sort_key2) ? (A->sort.sort_key1 < B->sort.sort_key1) : (A->sort.sort_key2 < B->sort.sort_key2);
  347. }
  348. };
  349. void sort_by_key() {
  350. SortArray<GeometryInstanceSurface *, SortByKey> sorter;
  351. sorter.sort(elements.ptr(), elements.size());
  352. }
  353. void sort_by_key_range(uint32_t p_from, uint32_t p_size) {
  354. SortArray<GeometryInstanceSurface *, SortByKey> sorter;
  355. sorter.sort(elements.ptr() + p_from, p_size);
  356. }
  357. struct SortByDepth {
  358. _FORCE_INLINE_ bool operator()(const GeometryInstanceSurface *A, const GeometryInstanceSurface *B) const {
  359. return (A->owner->depth < B->owner->depth);
  360. }
  361. };
  362. void sort_by_depth() { //used for shadows
  363. SortArray<GeometryInstanceSurface *, SortByDepth> sorter;
  364. sorter.sort(elements.ptr(), elements.size());
  365. }
  366. struct SortByReverseDepthAndPriority {
  367. _FORCE_INLINE_ bool operator()(const GeometryInstanceSurface *A, const GeometryInstanceSurface *B) const {
  368. return (A->sort.priority == B->sort.priority) ? (A->owner->depth > B->owner->depth) : (A->sort.priority < B->sort.priority);
  369. }
  370. };
  371. void sort_by_reverse_depth_and_priority() { //used for alpha
  372. SortArray<GeometryInstanceSurface *, SortByReverseDepthAndPriority> sorter;
  373. sorter.sort(elements.ptr(), elements.size());
  374. }
  375. _FORCE_INLINE_ void add_element(GeometryInstanceSurface *p_element) {
  376. elements.push_back(p_element);
  377. }
  378. };
  379. RenderList render_list[RENDER_LIST_MAX];
  380. void _setup_lights(const RenderDataGLES3 *p_render_data, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_omni_light_count, uint32_t &r_spot_light_count);
  381. void _setup_environment(const RenderDataGLES3 *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_pancake_shadows);
  382. void _fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append = false);
  383. template <PassMode p_pass_mode>
  384. _FORCE_INLINE_ void _render_list_template(RenderListParameters *p_params, const RenderDataGLES3 *p_render_data, uint32_t p_from_element, uint32_t p_to_element, bool p_alpha_pass = false);
  385. protected:
  386. double time;
  387. double time_step = 0;
  388. bool screen_space_roughness_limiter = false;
  389. float screen_space_roughness_limiter_amount = 0.25;
  390. float screen_space_roughness_limiter_limit = 0.18;
  391. void _render_buffers_debug_draw(Ref<RenderSceneBuffersGLES3> p_render_buffers, RID p_shadow_atlas, RID p_occlusion_buffer);
  392. /* Camera Attributes */
  393. struct CameraAttributes {
  394. float exposure_multiplier = 1.0;
  395. float exposure_normalization = 1.0;
  396. };
  397. bool use_physical_light_units = false;
  398. mutable RID_Owner<CameraAttributes, true> camera_attributes_owner;
  399. /* Environment */
  400. RS::EnvironmentSSAOQuality ssao_quality = RS::ENV_SSAO_QUALITY_MEDIUM;
  401. bool ssao_half_size = false;
  402. float ssao_adaptive_target = 0.5;
  403. int ssao_blur_passes = 2;
  404. float ssao_fadeout_from = 50.0;
  405. float ssao_fadeout_to = 300.0;
  406. bool glow_bicubic_upscale = false;
  407. RS::EnvironmentSSRRoughnessQuality ssr_roughness_quality = RS::ENV_SSR_ROUGHNESS_QUALITY_LOW;
  408. /* Sky */
  409. struct SkyGlobals {
  410. float fog_aerial_perspective = 0.0;
  411. Color fog_light_color;
  412. float fog_sun_scatter = 0.0;
  413. bool fog_enabled = false;
  414. float fog_density = 0.0;
  415. float z_far = 0.0;
  416. uint32_t directional_light_count = 0;
  417. DirectionalLightData *directional_lights = nullptr;
  418. DirectionalLightData *last_frame_directional_lights = nullptr;
  419. uint32_t last_frame_directional_light_count = 0;
  420. GLuint directional_light_buffer = 0;
  421. RID shader_default_version;
  422. RID default_material;
  423. RID default_shader;
  424. RID fog_material;
  425. RID fog_shader;
  426. GLuint screen_triangle = 0;
  427. GLuint screen_triangle_array = 0;
  428. GLuint radical_inverse_vdc_cache_tex = 0;
  429. uint32_t max_directional_lights = 4;
  430. uint32_t roughness_layers = 8;
  431. uint32_t ggx_samples = 128;
  432. } sky_globals;
  433. struct Sky {
  434. // Screen Buffers
  435. GLuint half_res_pass = 0;
  436. GLuint half_res_framebuffer = 0;
  437. GLuint quarter_res_pass = 0;
  438. GLuint quarter_res_framebuffer = 0;
  439. Size2i screen_size = Size2i(0, 0);
  440. // Radiance Cubemap
  441. GLuint radiance = 0;
  442. GLuint radiance_framebuffer = 0;
  443. GLuint raw_radiance = 0;
  444. RID material;
  445. GLuint uniform_buffer;
  446. int radiance_size = 256;
  447. int mipmap_count = 1;
  448. RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
  449. //ReflectionData reflection;
  450. bool reflection_dirty = false;
  451. bool dirty = false;
  452. int processing_layer = 0;
  453. Sky *dirty_list = nullptr;
  454. float baked_exposure = 1.0;
  455. //State to track when radiance cubemap needs updating
  456. GLES3::SkyMaterialData *prev_material;
  457. Vector3 prev_position = Vector3(0.0, 0.0, 0.0);
  458. float prev_time = 0.0f;
  459. };
  460. Sky *dirty_sky_list = nullptr;
  461. mutable RID_Owner<Sky, true> sky_owner;
  462. void _setup_sky(const RenderDataGLES3 *p_render_data, const PagedArray<RID> &p_lights, const Projection &p_projection, const Transform3D &p_transform, const Size2i p_screen_size);
  463. void _invalidate_sky(Sky *p_sky);
  464. void _update_dirty_skys();
  465. void _update_sky_radiance(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_luminance_multiplier);
  466. void _filter_sky_radiance(Sky *p_sky, int p_base_layer);
  467. void _draw_sky(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_luminance_multiplier, bool p_use_multiview, bool p_flip_y);
  468. void _free_sky_data(Sky *p_sky);
  469. public:
  470. static RasterizerSceneGLES3 *get_singleton() { return singleton; }
  471. RasterizerCanvasGLES3 *canvas = nullptr;
  472. RenderGeometryInstance *geometry_instance_create(RID p_base) override;
  473. void geometry_instance_free(RenderGeometryInstance *p_geometry_instance) override;
  474. uint32_t geometry_instance_get_pair_mask() override;
  475. /* SDFGI UPDATE */
  476. void sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) override {}
  477. int sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const override {
  478. return 0;
  479. }
  480. AABB sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override {
  481. return AABB();
  482. }
  483. uint32_t sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override {
  484. return 0;
  485. }
  486. /* SKY API */
  487. RID sky_allocate() override;
  488. void sky_initialize(RID p_rid) override;
  489. void sky_set_radiance_size(RID p_sky, int p_radiance_size) override;
  490. void sky_set_mode(RID p_sky, RS::SkyMode p_mode) override;
  491. void sky_set_material(RID p_sky, RID p_material) override;
  492. Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) override;
  493. float sky_get_baked_exposure(RID p_sky) const;
  494. /* ENVIRONMENT API */
  495. void environment_glow_set_use_bicubic_upscale(bool p_enable) override;
  496. void environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) override;
  497. 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) override;
  498. void environment_set_ssil_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) override;
  499. void environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) override;
  500. void environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) override;
  501. void environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update) override;
  502. void environment_set_volumetric_fog_volume_size(int p_size, int p_depth) override;
  503. void environment_set_volumetric_fog_filter_active(bool p_enable) override;
  504. Ref<Image> environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) override;
  505. _FORCE_INLINE_ bool is_using_physical_light_units() {
  506. return use_physical_light_units;
  507. }
  508. void positional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) override;
  509. void directional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) override;
  510. RID fog_volume_instance_create(RID p_fog_volume) override;
  511. void fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) override;
  512. void fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) override;
  513. RID fog_volume_instance_get_volume(RID p_fog_volume_instance) const override;
  514. Vector3 fog_volume_instance_get_position(RID p_fog_volume_instance) const override;
  515. RID voxel_gi_instance_create(RID p_voxel_gi) override;
  516. void voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) override;
  517. bool voxel_gi_needs_update(RID p_probe) const override;
  518. void voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) override;
  519. void voxel_gi_set_quality(RS::VoxelGIQuality) override;
  520. void render_scene(const Ref<RenderSceneBuffers> &p_render_buffers, const CameraData *p_camera_data, const CameraData *p_prev_camera_data, const PagedArray<RenderGeometryInstance *> &p_instances, const PagedArray<RID> &p_lights, const PagedArray<RID> &p_reflection_probes, const PagedArray<RID> &p_voxel_gi_instances, const PagedArray<RID> &p_decals, const PagedArray<RID> &p_lightmaps, const PagedArray<RID> &p_fog_volumes, RID p_environment, RID p_camera_attributes, RID p_shadow_atlas, RID p_occluder_debug_tex, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_mesh_lod_threshold, const RenderShadowData *p_render_shadows, int p_render_shadow_count, const RenderSDFGIData *p_render_sdfgi_regions, int p_render_sdfgi_region_count, const RenderSDFGIUpdateData *p_sdfgi_update_data = nullptr, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
  521. void render_material(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) override;
  522. void render_particle_collider_heightfield(RID p_collider, const Transform3D &p_transform, const PagedArray<RenderGeometryInstance *> &p_instances) override;
  523. void set_scene_pass(uint64_t p_pass) override {
  524. scene_pass = p_pass;
  525. }
  526. _FORCE_INLINE_ uint64_t get_scene_pass() {
  527. return scene_pass;
  528. }
  529. void set_time(double p_time, double p_step) override;
  530. void set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) override;
  531. _FORCE_INLINE_ RS::ViewportDebugDraw get_debug_draw_mode() const {
  532. return debug_draw;
  533. }
  534. Ref<RenderSceneBuffers> render_buffers_create() override;
  535. void gi_set_use_half_resolution(bool p_enable) override;
  536. void screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_curve) override;
  537. bool screen_space_roughness_limiter_is_active() const override;
  538. void sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) override;
  539. void sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) override;
  540. TypedArray<Image> bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) override;
  541. bool free(RID p_rid) override;
  542. void update() override;
  543. void sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) override;
  544. void decals_set_filter(RS::DecalFilter p_filter) override;
  545. void light_projectors_set_filter(RS::LightProjectorFilter p_filter) override;
  546. RasterizerSceneGLES3();
  547. ~RasterizerSceneGLES3();
  548. };
  549. #endif // GLES3_ENABLED
  550. #endif // RASTERIZER_SCENE_GLES3_H