rasterizer_scene_gles2.h 24 KB

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