rasterizer_scene_gles2.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*************************************************************************/
  2. /* rasterizer_scene_gles2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 RASTERIZERSCENEGLES2_H
  31. #define RASTERIZERSCENEGLES2_H
  32. /* Must come before shaders or the Windows build fails... */
  33. #include "rasterizer_storage_gles2.h"
  34. #include "shaders/cube_to_dp.glsl.gen.h"
  35. #include "shaders/scene.glsl.gen.h"
  36. /*
  37. #include "drivers/gles3/shaders/effect_blur.glsl.gen.h"
  38. #include "drivers/gles3/shaders/exposure.glsl.gen.h"
  39. #include "drivers/gles3/shaders/resolve.glsl.gen.h"
  40. #include "drivers/gles3/shaders/scene.glsl.gen.h"
  41. #include "drivers/gles3/shaders/screen_space_reflection.glsl.gen.h"
  42. #include "drivers/gles3/shaders/ssao.glsl.gen.h"
  43. #include "drivers/gles3/shaders/ssao_blur.glsl.gen.h"
  44. #include "drivers/gles3/shaders/ssao_minify.glsl.gen.h"
  45. #include "drivers/gles3/shaders/subsurf_scattering.glsl.gen.h"
  46. #include "drivers/gles3/shaders/tonemap.glsl.gen.h"
  47. */
  48. class RasterizerSceneGLES2 : public RasterizerScene {
  49. public:
  50. enum ShadowFilterMode {
  51. SHADOW_FILTER_NEAREST,
  52. SHADOW_FILTER_PCF5,
  53. SHADOW_FILTER_PCF13,
  54. };
  55. enum {
  56. INSTANCE_ATTRIB_BASE = 8,
  57. INSTANCE_BONE_BASE = 13,
  58. };
  59. ShadowFilterMode shadow_filter_mode;
  60. RID default_material;
  61. RID default_material_twosided;
  62. RID default_shader;
  63. RID default_shader_twosided;
  64. RID default_worldcoord_material;
  65. RID default_worldcoord_material_twosided;
  66. RID default_worldcoord_shader;
  67. RID default_worldcoord_shader_twosided;
  68. RID default_overdraw_material;
  69. RID default_overdraw_shader;
  70. uint64_t render_pass;
  71. uint64_t scene_pass;
  72. uint32_t current_material_index;
  73. uint32_t current_geometry_index;
  74. uint32_t current_light_index;
  75. uint32_t current_refprobe_index;
  76. uint32_t current_shader_index;
  77. RasterizerStorageGLES2 *storage;
  78. struct State {
  79. bool texscreen_copied;
  80. int current_blend_mode;
  81. float current_line_width;
  82. int current_depth_draw;
  83. bool current_depth_test;
  84. GLuint current_main_tex;
  85. SceneShaderGLES2 scene_shader;
  86. CubeToDpShaderGLES2 cube_to_dp_shader;
  87. GLuint sky_verts;
  88. GLuint immediate_buffer;
  89. Color default_ambient;
  90. // ResolveShaderGLES3 resolve_shader;
  91. // ScreenSpaceReflectionShaderGLES3 ssr_shader;
  92. // EffectBlurShaderGLES3 effect_blur_shader;
  93. // SubsurfScatteringShaderGLES3 sss_shader;
  94. // SsaoMinifyShaderGLES3 ssao_minify_shader;
  95. // SsaoShaderGLES3 ssao_shader;
  96. // SsaoBlurShaderGLES3 ssao_blur_shader;
  97. // ExposureShaderGLES3 exposure_shader;
  98. // TonemapShaderGLES3 tonemap_shader;
  99. /*
  100. struct SceneDataUBO {
  101. //this is a std140 compatible struct. Please read the OpenGL 3.3 Specificaiton spec before doing any changes
  102. float projection_matrix[16];
  103. float inv_projection_matrix[16];
  104. float camera_inverse_matrix[16];
  105. float camera_matrix[16];
  106. float ambient_light_color[4];
  107. float bg_color[4];
  108. float fog_color_enabled[4];
  109. float fog_sun_color_amount[4];
  110. float ambient_energy;
  111. float bg_energy;
  112. float z_offset;
  113. float z_slope_scale;
  114. float shadow_dual_paraboloid_render_zfar;
  115. float shadow_dual_paraboloid_render_side;
  116. float viewport_size[2];
  117. float screen_pixel_size[2];
  118. float shadow_atlas_pixel_size[2];
  119. float shadow_directional_pixel_size[2];
  120. float time;
  121. float z_far;
  122. float reflection_multiplier;
  123. float subsurface_scatter_width;
  124. float ambient_occlusion_affect_light;
  125. uint32_t fog_depth_enabled;
  126. float fog_depth_begin;
  127. float fog_depth_curve;
  128. uint32_t fog_transmit_enabled;
  129. float fog_transmit_curve;
  130. uint32_t fog_height_enabled;
  131. float fog_height_min;
  132. float fog_height_max;
  133. float fog_height_curve;
  134. // make sure this struct is padded to be a multiple of 16 bytes for webgl
  135. } ubo_data;
  136. GLuint scene_ubo;
  137. struct EnvironmentRadianceUBO {
  138. float transform[16];
  139. float ambient_contribution;
  140. uint8_t padding[12];
  141. } env_radiance_data;
  142. GLuint env_radiance_ubo;
  143. GLuint sky_array;
  144. GLuint directional_ubo;
  145. GLuint spot_array_ubo;
  146. GLuint omni_array_ubo;
  147. GLuint reflection_array_ubo;
  148. GLuint immediate_buffer;
  149. GLuint immediate_array;
  150. uint32_t ubo_light_size;
  151. uint8_t *spot_array_tmp;
  152. uint8_t *omni_array_tmp;
  153. uint8_t *reflection_array_tmp;
  154. int max_ubo_lights;
  155. int max_forward_lights_per_object;
  156. int max_ubo_reflections;
  157. int max_skeleton_bones;
  158. bool used_contact_shadows;
  159. int spot_light_count;
  160. int omni_light_count;
  161. int directional_light_count;
  162. int reflection_probe_count;
  163. bool cull_front;
  164. bool cull_disabled;
  165. bool used_sss;
  166. bool using_contact_shadows;
  167. VS::ViewportDebugDraw debug_draw;
  168. */
  169. bool used_screen_texture;
  170. bool shadow_is_dual_parabolloid;
  171. float dual_parbolloid_direction;
  172. float dual_parbolloid_zfar;
  173. bool render_no_shadows;
  174. Vector2 viewport_size;
  175. Vector2 screen_pixel_size;
  176. } state;
  177. /* SHADOW ATLAS API */
  178. uint64_t shadow_atlas_realloc_tolerance_msec;
  179. struct ShadowAtlas : public RID_Data {
  180. enum {
  181. QUADRANT_SHIFT = 27,
  182. SHADOW_INDEX_MASK = (1 << QUADRANT_SHIFT) - 1,
  183. SHADOW_INVALID = 0xFFFFFFFF,
  184. };
  185. struct Quadrant {
  186. uint32_t subdivision;
  187. struct Shadow {
  188. RID owner;
  189. uint64_t version;
  190. uint64_t alloc_tick;
  191. Shadow() {
  192. version = 0;
  193. alloc_tick = 0;
  194. }
  195. };
  196. Vector<Shadow> shadows;
  197. Quadrant() {
  198. subdivision = 0;
  199. }
  200. } quadrants[4];
  201. int size_order[4];
  202. uint32_t smallest_subdiv;
  203. int size;
  204. GLuint fbo;
  205. GLuint depth;
  206. GLuint color;
  207. Map<RID, uint32_t> shadow_owners;
  208. };
  209. struct ShadowCubeMap {
  210. GLuint fbo[6];
  211. GLuint cubemap;
  212. uint32_t size;
  213. };
  214. Vector<ShadowCubeMap> shadow_cubemaps;
  215. RID_Owner<ShadowAtlas> shadow_atlas_owner;
  216. RID shadow_atlas_create();
  217. void shadow_atlas_set_size(RID p_atlas, int p_size);
  218. void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision);
  219. 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);
  220. bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version);
  221. struct DirectionalShadow {
  222. GLuint fbo;
  223. GLuint depth;
  224. GLuint color;
  225. int light_count;
  226. int size;
  227. int current_light;
  228. } directional_shadow;
  229. virtual int get_directional_light_shadow_size(RID p_light_intance);
  230. virtual void set_directional_shadow_count(int p_count);
  231. /* REFLECTION PROBE ATLAS API */
  232. virtual RID reflection_atlas_create();
  233. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_size);
  234. virtual void reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv);
  235. /* REFLECTION CUBEMAPS */
  236. /* REFLECTION PROBE INSTANCE */
  237. struct ReflectionProbeInstance : public RID_Data {
  238. RasterizerStorageGLES2::ReflectionProbe *probe_ptr;
  239. RID probe;
  240. RID self;
  241. RID atlas;
  242. int reflection_atlas_index;
  243. int render_step;
  244. int reflection_index;
  245. GLuint fbo[6];
  246. GLuint color[6];
  247. GLuint depth;
  248. GLuint cubemap;
  249. int current_resolution;
  250. mutable bool dirty;
  251. uint64_t last_pass;
  252. uint32_t index;
  253. Transform transform;
  254. };
  255. mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner;
  256. ReflectionProbeInstance **reflection_probe_instances;
  257. int reflection_probe_count;
  258. virtual RID reflection_probe_instance_create(RID p_probe);
  259. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform);
  260. virtual void reflection_probe_release_atlas_index(RID p_instance);
  261. virtual bool reflection_probe_instance_needs_redraw(RID p_instance);
  262. virtual bool reflection_probe_instance_has_reflection(RID p_instance);
  263. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas);
  264. virtual bool reflection_probe_instance_postprocess_step(RID p_instance);
  265. /* ENVIRONMENT API */
  266. struct Environment : public RID_Data {
  267. VS::EnvironmentBG bg_mode;
  268. RID sky;
  269. float sky_custom_fov;
  270. Basis sky_orientation;
  271. Color bg_color;
  272. float bg_energy;
  273. float sky_ambient;
  274. Color ambient_color;
  275. float ambient_energy;
  276. float ambient_sky_contribution;
  277. int canvas_max_layer;
  278. bool fog_enabled;
  279. Color fog_color;
  280. Color fog_sun_color;
  281. float fog_sun_amount;
  282. bool fog_depth_enabled;
  283. float fog_depth_begin;
  284. float fog_depth_end;
  285. float fog_depth_curve;
  286. bool fog_transmit_enabled;
  287. float fog_transmit_curve;
  288. bool fog_height_enabled;
  289. float fog_height_min;
  290. float fog_height_max;
  291. float fog_height_curve;
  292. Environment() :
  293. bg_mode(VS::ENV_BG_CLEAR_COLOR),
  294. sky_custom_fov(0.0),
  295. bg_energy(1.0),
  296. sky_ambient(0),
  297. ambient_energy(1.0),
  298. ambient_sky_contribution(0.0),
  299. canvas_max_layer(0),
  300. fog_enabled(false),
  301. fog_color(Color(0.5, 0.5, 0.5)),
  302. fog_sun_color(Color(0.8, 0.8, 0.0)),
  303. fog_sun_amount(0),
  304. fog_depth_enabled(true),
  305. fog_depth_begin(10),
  306. fog_depth_end(0),
  307. fog_depth_curve(1),
  308. fog_transmit_enabled(true),
  309. fog_transmit_curve(1),
  310. fog_height_enabled(false),
  311. fog_height_min(0),
  312. fog_height_max(100),
  313. fog_height_curve(1) {
  314. }
  315. };
  316. mutable RID_Owner<Environment> environment_owner;
  317. virtual RID environment_create();
  318. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg);
  319. virtual void environment_set_sky(RID p_env, RID p_sky);
  320. virtual void environment_set_sky_custom_fov(RID p_env, float p_scale);
  321. virtual void environment_set_sky_orientation(RID p_env, const Basis &p_orientation);
  322. virtual void environment_set_bg_color(RID p_env, const Color &p_color);
  323. virtual void environment_set_bg_energy(RID p_env, float p_energy);
  324. virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer);
  325. virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0);
  326. virtual void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality);
  327. virtual void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality);
  328. virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, 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);
  329. virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture);
  330. virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_in, float p_fade_out, float p_depth_tolerance, bool p_roughness);
  331. virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, float p_ao_channel_affect, const Color &p_color, VS::EnvironmentSSAOQuality p_quality, VS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness);
  332. virtual 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);
  333. virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp);
  334. virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount);
  335. virtual 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);
  336. virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve);
  337. virtual bool is_environment(RID p_env);
  338. virtual VS::EnvironmentBG environment_get_background(RID p_env);
  339. virtual int environment_get_canvas_max_layer(RID p_env);
  340. /* LIGHT INSTANCE */
  341. struct LightInstance : public RID_Data {
  342. struct ShadowTransform {
  343. CameraMatrix camera;
  344. Transform transform;
  345. float farplane;
  346. float split;
  347. float bias_scale;
  348. };
  349. ShadowTransform shadow_transform[4];
  350. RID self;
  351. RID light;
  352. RasterizerStorageGLES2::Light *light_ptr;
  353. Transform transform;
  354. Vector3 light_vector;
  355. Vector3 spot_vector;
  356. float linear_att;
  357. // TODO passes and all that stuff ?
  358. uint64_t last_scene_pass;
  359. uint64_t last_scene_shadow_pass;
  360. uint16_t light_index;
  361. uint16_t light_directional_index;
  362. Rect2 directional_rect;
  363. Set<RID> shadow_atlases; // atlases where this light is registered
  364. };
  365. mutable RID_Owner<LightInstance> light_instance_owner;
  366. virtual RID light_instance_create(RID p_light);
  367. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform);
  368. virtual 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);
  369. virtual void light_instance_mark_visible(RID p_light_instance);
  370. virtual bool light_instances_can_render_shadow_cube() const { return storage->config.support_shadow_cubemaps; }
  371. LightInstance **render_light_instances;
  372. int render_directional_lights;
  373. int render_light_instance_count;
  374. /* REFLECTION INSTANCE */
  375. virtual RID gi_probe_instance_create();
  376. virtual void gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data);
  377. virtual void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform);
  378. virtual void gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds);
  379. /* RENDER LIST */
  380. enum LightMode {
  381. LIGHTMODE_NORMAL,
  382. LIGHTMODE_UNSHADED,
  383. LIGHTMODE_LIGHTMAP,
  384. LIGHTMODE_LIGHTMAP_CAPTURE,
  385. };
  386. struct RenderList {
  387. enum {
  388. MAX_LIGHTS = 255,
  389. MAX_REFLECTION_PROBES = 255,
  390. DEFAULT_MAX_ELEMENTS = 65536,
  391. SORT_KEY_PRIORITY_SHIFT = 56
  392. };
  393. int max_elements;
  394. struct Element {
  395. RasterizerScene::InstanceBase *instance;
  396. RasterizerStorageGLES2::Geometry *geometry;
  397. RasterizerStorageGLES2::Material *material;
  398. RasterizerStorageGLES2::GeometryOwner *owner;
  399. bool use_accum; //is this an add pass for multipass
  400. bool *use_accum_ptr;
  401. union {
  402. //TODO: should be endian swapped on big endian
  403. struct {
  404. int32_t depth_layer : 16;
  405. int32_t priority : 16;
  406. };
  407. uint32_t depth_key;
  408. };
  409. union {
  410. struct {
  411. //from least significant to most significant in sort, TODO: should be endian swapped on big endian
  412. uint64_t geometry_index : 14;
  413. uint64_t instancing : 1;
  414. uint64_t skeleton : 1;
  415. uint64_t shader_index : 10;
  416. uint64_t material_index : 10;
  417. uint64_t light_index : 8;
  418. uint64_t light_type2 : 1; // if 1==0 : nolight/directional, else omni/spot
  419. uint64_t refprobe_1_index : 8;
  420. uint64_t refprobe_0_index : 8;
  421. uint64_t light_type1 : 1; //no light, directional is 0, omni spot is 1
  422. uint64_t light_mode : 2; // LightMode enum
  423. };
  424. uint64_t sort_key;
  425. };
  426. };
  427. Element *base_elements;
  428. Element **elements;
  429. int element_count;
  430. int alpha_element_count;
  431. void clear() {
  432. element_count = 0;
  433. alpha_element_count = 0;
  434. }
  435. // sorts
  436. struct SortByKey {
  437. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  438. if (A->depth_key == B->depth_key) {
  439. return A->sort_key < B->sort_key;
  440. } else {
  441. return A->depth_key < B->depth_key;
  442. }
  443. }
  444. };
  445. void sort_by_key(bool p_alpha) {
  446. SortArray<Element *, SortByKey> sorter;
  447. if (p_alpha) {
  448. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  449. } else {
  450. sorter.sort(elements, element_count);
  451. }
  452. }
  453. struct SortByDepth {
  454. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  455. return A->instance->depth < B->instance->depth;
  456. }
  457. };
  458. void sort_by_depth(bool p_alpha) { //used for shadows
  459. SortArray<Element *, SortByDepth> sorter;
  460. if (p_alpha) {
  461. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  462. } else {
  463. sorter.sort(elements, element_count);
  464. }
  465. }
  466. struct SortByReverseDepthAndPriority {
  467. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  468. if (A->priority == B->priority) {
  469. return A->instance->depth > B->instance->depth;
  470. } else {
  471. return A->priority < B->priority;
  472. }
  473. }
  474. };
  475. void sort_by_reverse_depth_and_priority(bool p_alpha) { //used for alpha
  476. SortArray<Element *, SortByReverseDepthAndPriority> sorter;
  477. if (p_alpha) {
  478. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  479. } else {
  480. sorter.sort(elements, element_count);
  481. }
  482. }
  483. // element adding and stuff
  484. _FORCE_INLINE_ Element *add_element() {
  485. if (element_count + alpha_element_count >= max_elements)
  486. return NULL;
  487. elements[element_count] = &base_elements[element_count];
  488. return elements[element_count++];
  489. }
  490. _FORCE_INLINE_ Element *add_alpha_element() {
  491. if (element_count + alpha_element_count >= max_elements) {
  492. return NULL;
  493. }
  494. int idx = max_elements - alpha_element_count - 1;
  495. elements[idx] = &base_elements[idx];
  496. alpha_element_count++;
  497. return elements[idx];
  498. }
  499. void init() {
  500. element_count = 0;
  501. alpha_element_count = 0;
  502. elements = memnew_arr(Element *, max_elements);
  503. base_elements = memnew_arr(Element, max_elements);
  504. for (int i = 0; i < max_elements; i++) {
  505. elements[i] = &base_elements[i];
  506. }
  507. }
  508. RenderList() {
  509. max_elements = DEFAULT_MAX_ELEMENTS;
  510. }
  511. ~RenderList() {
  512. memdelete_arr(elements);
  513. memdelete_arr(base_elements);
  514. }
  515. };
  516. RenderList render_list;
  517. void _add_geometry(RasterizerStorageGLES2::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES2::GeometryOwner *p_owner, int p_material, bool p_depth_pass, bool p_shadow_pass);
  518. void _add_geometry_with_material(RasterizerStorageGLES2::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES2::GeometryOwner *p_owner, RasterizerStorageGLES2::Material *p_material, bool p_depth_pass, bool p_shadow_pass);
  519. void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass);
  520. void _render_render_list(RenderList::Element **p_elements, int p_element_count,
  521. const Transform &p_view_transform,
  522. const CameraMatrix &p_projection,
  523. RID p_shadow_atlas,
  524. Environment *p_env,
  525. GLuint p_base_env,
  526. float p_shadow_bias,
  527. float p_shadow_normal_bias,
  528. bool p_reverse_cull,
  529. bool p_alpha_pass,
  530. bool p_shadow);
  531. void _draw_sky(RasterizerStorageGLES2::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy, const Basis &p_sky_orientation);
  532. _FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, bool p_alpha_pass, Size2i p_skeleton_tex_size = Size2i(0, 0));
  533. _FORCE_INLINE_ void _setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton);
  534. _FORCE_INLINE_ void _setup_light_type(LightInstance *p_light, ShadowAtlas *shadow_atlas);
  535. _FORCE_INLINE_ void _setup_light(LightInstance *p_light, ShadowAtlas *shadow_atlas, const Transform &p_view_transform, bool accum_pass);
  536. _FORCE_INLINE_ void _setup_refprobes(ReflectionProbeInstance *p_refprobe1, ReflectionProbeInstance *p_refprobe2, const Transform &p_view_transform, Environment *p_env);
  537. _FORCE_INLINE_ void _render_geometry(RenderList::Element *p_element);
  538. virtual void render_scene(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_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass);
  539. virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count);
  540. virtual bool free(RID p_rid);
  541. virtual void set_scene_pass(uint64_t p_pass);
  542. virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw);
  543. void iteration();
  544. void initialize();
  545. void finalize();
  546. RasterizerSceneGLES2();
  547. };
  548. #endif // RASTERIZERSCENEGLES2_H