rasterizer_scene_gles3.h 28 KB

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