rasterizer_scene_gles3.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. /* Must come before shaders or the Windows build fails... */
  33. #include "rasterizer_storage_gles3.h"
  34. #include "drivers/gles3/shaders/cube_to_dp.glsl.gen.h"
  35. #include "drivers/gles3/shaders/effect_blur.glsl.gen.h"
  36. #include "drivers/gles3/shaders/exposure.glsl.gen.h"
  37. #include "drivers/gles3/shaders/resolve.glsl.gen.h"
  38. #include "drivers/gles3/shaders/scene.glsl.gen.h"
  39. #include "drivers/gles3/shaders/screen_space_reflection.glsl.gen.h"
  40. #include "drivers/gles3/shaders/ssao.glsl.gen.h"
  41. #include "drivers/gles3/shaders/ssao_blur.glsl.gen.h"
  42. #include "drivers/gles3/shaders/ssao_minify.glsl.gen.h"
  43. #include "drivers/gles3/shaders/subsurf_scattering.glsl.gen.h"
  44. #include "drivers/gles3/shaders/tonemap.glsl.gen.h"
  45. class RasterizerSceneGLES3 : public RasterizerScene {
  46. public:
  47. enum ShadowFilterMode {
  48. SHADOW_FILTER_NEAREST,
  49. SHADOW_FILTER_PCF5,
  50. SHADOW_FILTER_PCF13,
  51. };
  52. ShadowFilterMode shadow_filter_mode;
  53. uint64_t shadow_atlas_realloc_tolerance_msec;
  54. enum SubSurfaceScatterQuality {
  55. SSS_QUALITY_LOW,
  56. SSS_QUALITY_MEDIUM,
  57. SSS_QUALITY_HIGH,
  58. };
  59. SubSurfaceScatterQuality subsurface_scatter_quality;
  60. float subsurface_scatter_size;
  61. bool subsurface_scatter_follow_surface;
  62. bool subsurface_scatter_weight_samples;
  63. uint64_t render_pass;
  64. uint64_t scene_pass;
  65. uint32_t current_material_index;
  66. uint32_t current_geometry_index;
  67. RID default_material;
  68. RID default_material_twosided;
  69. RID default_shader;
  70. RID default_shader_twosided;
  71. RID default_worldcoord_material;
  72. RID default_worldcoord_material_twosided;
  73. RID default_worldcoord_shader;
  74. RID default_worldcoord_shader_twosided;
  75. RID default_overdraw_material;
  76. RID default_overdraw_shader;
  77. RasterizerStorageGLES3 *storage;
  78. Vector<RasterizerStorageGLES3::RenderTarget::Exposure> exposure_shrink;
  79. int exposure_shrink_size;
  80. struct State {
  81. bool texscreen_copied;
  82. int current_blend_mode;
  83. float current_line_width;
  84. int current_depth_draw;
  85. bool current_depth_test;
  86. GLuint current_main_tex;
  87. SceneShaderGLES3 scene_shader;
  88. CubeToDpShaderGLES3 cube_to_dp_shader;
  89. ResolveShaderGLES3 resolve_shader;
  90. ScreenSpaceReflectionShaderGLES3 ssr_shader;
  91. EffectBlurShaderGLES3 effect_blur_shader;
  92. SubsurfScatteringShaderGLES3 sss_shader;
  93. SsaoMinifyShaderGLES3 ssao_minify_shader;
  94. SsaoShaderGLES3 ssao_shader;
  95. SsaoBlurShaderGLES3 ssao_blur_shader;
  96. ExposureShaderGLES3 exposure_shader;
  97. TonemapShaderGLES3 tonemap_shader;
  98. struct SceneDataUBO {
  99. //this is a std140 compatible struct. Please read the OpenGL 3.3 Specification spec before doing any changes
  100. float projection_matrix[16];
  101. float inv_projection_matrix[16];
  102. float camera_inverse_matrix[16];
  103. float camera_matrix[16];
  104. float ambient_light_color[4];
  105. float bg_color[4];
  106. float fog_color_enabled[4];
  107. float fog_sun_color_amount[4];
  108. float ambient_energy;
  109. float bg_energy;
  110. float z_offset;
  111. float z_slope_scale;
  112. float shadow_dual_paraboloid_render_zfar;
  113. float shadow_dual_paraboloid_render_side;
  114. float viewport_size[2];
  115. float screen_pixel_size[2];
  116. float shadow_atlas_pixel_size[2];
  117. float shadow_directional_pixel_size[2];
  118. float time;
  119. float z_far;
  120. float reflection_multiplier;
  121. float subsurface_scatter_width;
  122. float ambient_occlusion_affect_light;
  123. float ambient_occlusion_affect_ssao;
  124. float opaque_prepass_threshold;
  125. uint32_t fog_depth_enabled;
  126. float fog_depth_begin;
  127. float fog_depth_end;
  128. float fog_density;
  129. float fog_depth_curve;
  130. uint32_t fog_transmit_enabled;
  131. float fog_transmit_curve;
  132. uint32_t fog_height_enabled;
  133. float fog_height_min;
  134. float fog_height_max;
  135. float fog_height_curve;
  136. uint32_t view_index;
  137. // make sure this struct is padded to be a multiple of 16 bytes for webgl
  138. float pad[3];
  139. } ubo_data;
  140. static_assert(sizeof(SceneDataUBO) % 16 == 0, "SceneDataUBO size must be a multiple of 16 bytes");
  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_verts;
  149. GLuint sky_array;
  150. GLuint directional_ubo;
  151. GLuint spot_array_ubo;
  152. GLuint omni_array_ubo;
  153. GLuint reflection_array_ubo;
  154. GLuint immediate_buffer;
  155. GLuint immediate_array;
  156. uint32_t ubo_light_size;
  157. uint8_t *spot_array_tmp;
  158. uint8_t *omni_array_tmp;
  159. uint8_t *reflection_array_tmp;
  160. int max_ubo_lights;
  161. int max_forward_lights_per_object;
  162. int max_ubo_reflections;
  163. int max_skeleton_bones;
  164. bool used_contact_shadows;
  165. int spot_light_count;
  166. int omni_light_count;
  167. int directional_light_count;
  168. int reflection_probe_count;
  169. bool cull_front;
  170. bool cull_disabled;
  171. bool used_sss;
  172. bool used_screen_texture;
  173. bool used_depth_prepass;
  174. bool used_depth_texture;
  175. bool prepared_depth_texture;
  176. bool bound_depth_texture;
  177. VS::ViewportDebugDraw debug_draw;
  178. } state;
  179. /* SHADOW ATLAS API */
  180. struct ShadowAtlas : public RID_Data {
  181. enum {
  182. QUADRANT_SHIFT = 27,
  183. SHADOW_INDEX_MASK = (1 << QUADRANT_SHIFT) - 1,
  184. SHADOW_INVALID = 0xFFFFFFFF
  185. };
  186. struct Quadrant {
  187. uint32_t subdivision;
  188. struct Shadow {
  189. RID owner;
  190. uint64_t version;
  191. uint64_t alloc_tick;
  192. Shadow() {
  193. version = 0;
  194. alloc_tick = 0;
  195. }
  196. };
  197. Vector<Shadow> shadows;
  198. Quadrant() {
  199. subdivision = 0; //not in use
  200. }
  201. } quadrants[4];
  202. int size_order[4];
  203. uint32_t smallest_subdiv;
  204. int size;
  205. GLuint fbo;
  206. GLuint depth;
  207. Map<RID, uint32_t> shadow_owners;
  208. };
  209. struct ShadowCubeMap {
  210. GLuint fbo_id[6];
  211. GLuint cubemap;
  212. uint32_t size;
  213. };
  214. Vector<ShadowCubeMap> shadow_cubemaps;
  215. RID_Owner<ShadowAtlas> shadow_atlas_owner;
  216. int directional_shadow_size;
  217. void directional_shadow_create();
  218. RID shadow_atlas_create();
  219. void shadow_atlas_set_size(RID p_atlas, int p_size);
  220. void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision);
  221. 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);
  222. bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version);
  223. struct DirectionalShadow {
  224. GLuint fbo = 0;
  225. GLuint depth = 0;
  226. int light_count = 0;
  227. int size = 0;
  228. int current_light = 0;
  229. } directional_shadow;
  230. virtual int get_directional_light_shadow_size(RID p_light_intance);
  231. virtual void set_directional_shadow_count(int p_count);
  232. /* REFLECTION PROBE ATLAS API */
  233. struct ReflectionAtlas : public RID_Data {
  234. int subdiv;
  235. int size;
  236. struct Reflection {
  237. RID owner;
  238. uint64_t last_frame;
  239. };
  240. GLuint fbo[6];
  241. GLuint color;
  242. Vector<Reflection> reflections;
  243. };
  244. mutable RID_Owner<ReflectionAtlas> reflection_atlas_owner;
  245. virtual RID reflection_atlas_create();
  246. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_size);
  247. virtual void reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv);
  248. /* REFLECTION CUBEMAPS */
  249. struct ReflectionCubeMap {
  250. GLuint fbo_id[6];
  251. GLuint cubemap;
  252. GLuint depth;
  253. int size;
  254. };
  255. Vector<ReflectionCubeMap> reflection_cubemaps;
  256. /* REFLECTION PROBE INSTANCE */
  257. struct ReflectionProbeInstance : public RID_Data {
  258. RasterizerStorageGLES3::ReflectionProbe *probe_ptr;
  259. RID probe;
  260. RID self;
  261. RID atlas;
  262. int reflection_atlas_index;
  263. int render_step;
  264. uint64_t last_pass;
  265. int reflection_index;
  266. Transform transform;
  267. };
  268. struct ReflectionProbeDataUBO {
  269. float box_extents[4];
  270. float box_ofs[4];
  271. float params[4]; // intensity, 0, 0, boxproject
  272. float ambient[4]; //color, probe contrib
  273. float atlas_clamp[4];
  274. float local_matrix[16]; //up to here for spot and omni, rest is for directional
  275. //notes: for ambientblend, use distance to edge to blend between already existing global environment
  276. };
  277. mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner;
  278. virtual RID reflection_probe_instance_create(RID p_probe);
  279. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform);
  280. virtual void reflection_probe_release_atlas_index(RID p_instance);
  281. virtual bool reflection_probe_instance_needs_redraw(RID p_instance);
  282. virtual bool reflection_probe_instance_has_reflection(RID p_instance);
  283. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas);
  284. virtual bool reflection_probe_instance_postprocess_step(RID p_instance);
  285. /* ENVIRONMENT API */
  286. struct Environment : public RID_Data {
  287. VS::EnvironmentBG bg_mode;
  288. RID sky;
  289. float sky_custom_fov;
  290. Basis sky_orientation;
  291. Color bg_color;
  292. float bg_energy;
  293. float sky_ambient;
  294. int camera_feed_id;
  295. Color ambient_color;
  296. float ambient_energy;
  297. float ambient_sky_contribution;
  298. int canvas_max_layer;
  299. bool ssr_enabled;
  300. int ssr_max_steps;
  301. float ssr_fade_in;
  302. float ssr_fade_out;
  303. float ssr_depth_tolerance;
  304. bool ssr_roughness;
  305. bool ssao_enabled;
  306. float ssao_intensity;
  307. float ssao_radius;
  308. float ssao_intensity2;
  309. float ssao_radius2;
  310. float ssao_bias;
  311. float ssao_light_affect;
  312. float ssao_ao_channel_affect;
  313. Color ssao_color;
  314. VS::EnvironmentSSAOQuality ssao_quality;
  315. float ssao_bilateral_sharpness;
  316. VS::EnvironmentSSAOBlur ssao_filter;
  317. bool glow_enabled;
  318. int glow_levels;
  319. float glow_intensity;
  320. float glow_strength;
  321. float glow_bloom;
  322. VS::EnvironmentGlowBlendMode glow_blend_mode;
  323. float glow_hdr_bleed_threshold;
  324. float glow_hdr_bleed_scale;
  325. float glow_hdr_luminance_cap;
  326. bool glow_bicubic_upscale;
  327. bool glow_high_quality;
  328. float glow_map_strength;
  329. RID glow_map;
  330. VS::EnvironmentToneMapper tone_mapper;
  331. float tone_mapper_exposure;
  332. float tone_mapper_exposure_white;
  333. bool auto_exposure;
  334. float auto_exposure_speed;
  335. float auto_exposure_min;
  336. float auto_exposure_max;
  337. float auto_exposure_grey;
  338. bool dof_blur_far_enabled;
  339. float dof_blur_far_distance;
  340. float dof_blur_far_transition;
  341. float dof_blur_far_amount;
  342. VS::EnvironmentDOFBlurQuality dof_blur_far_quality;
  343. bool dof_blur_near_enabled;
  344. float dof_blur_near_distance;
  345. float dof_blur_near_transition;
  346. float dof_blur_near_amount;
  347. VS::EnvironmentDOFBlurQuality dof_blur_near_quality;
  348. bool adjustments_enabled;
  349. float adjustments_brightness;
  350. float adjustments_contrast;
  351. float adjustments_saturation;
  352. RID color_correction;
  353. bool fog_enabled;
  354. Color fog_color;
  355. Color fog_sun_color;
  356. float fog_sun_amount;
  357. bool fog_depth_enabled;
  358. float fog_depth_begin;
  359. float fog_depth_end;
  360. float fog_depth_curve;
  361. bool fog_transmit_enabled;
  362. float fog_transmit_curve;
  363. bool fog_height_enabled;
  364. float fog_height_min;
  365. float fog_height_max;
  366. float fog_height_curve;
  367. Environment() :
  368. bg_mode(VS::ENV_BG_CLEAR_COLOR),
  369. sky_custom_fov(0.0),
  370. bg_energy(1.0),
  371. sky_ambient(0),
  372. camera_feed_id(0),
  373. ambient_energy(1.0),
  374. ambient_sky_contribution(0.0),
  375. canvas_max_layer(0),
  376. ssr_enabled(false),
  377. ssr_max_steps(64),
  378. ssr_fade_in(0.15),
  379. ssr_fade_out(2.0),
  380. ssr_depth_tolerance(0.2),
  381. ssr_roughness(true),
  382. ssao_enabled(false),
  383. ssao_intensity(1.0),
  384. ssao_radius(1.0),
  385. ssao_intensity2(1.0),
  386. ssao_radius2(0.0),
  387. ssao_bias(0.01),
  388. ssao_light_affect(0),
  389. ssao_ao_channel_affect(0),
  390. ssao_quality(VS::ENV_SSAO_QUALITY_LOW),
  391. ssao_bilateral_sharpness(4),
  392. ssao_filter(VS::ENV_SSAO_BLUR_3x3),
  393. glow_enabled(false),
  394. glow_levels((1 << 2) | (1 << 4)),
  395. glow_intensity(0.8),
  396. glow_strength(1.0),
  397. glow_bloom(0.0),
  398. glow_blend_mode(VS::GLOW_BLEND_MODE_SOFTLIGHT),
  399. glow_hdr_bleed_threshold(1.0),
  400. glow_hdr_bleed_scale(2.0),
  401. glow_hdr_luminance_cap(12.0),
  402. glow_bicubic_upscale(false),
  403. glow_high_quality(false),
  404. tone_mapper(VS::ENV_TONE_MAPPER_LINEAR),
  405. tone_mapper_exposure(1.0),
  406. tone_mapper_exposure_white(1.0),
  407. auto_exposure(false),
  408. auto_exposure_speed(0.5),
  409. auto_exposure_min(0.05),
  410. auto_exposure_max(8),
  411. auto_exposure_grey(0.4),
  412. dof_blur_far_enabled(false),
  413. dof_blur_far_distance(10),
  414. dof_blur_far_transition(5),
  415. dof_blur_far_amount(0.1),
  416. dof_blur_far_quality(VS::ENV_DOF_BLUR_QUALITY_MEDIUM),
  417. dof_blur_near_enabled(false),
  418. dof_blur_near_distance(2),
  419. dof_blur_near_transition(1),
  420. dof_blur_near_amount(0.1),
  421. dof_blur_near_quality(VS::ENV_DOF_BLUR_QUALITY_MEDIUM),
  422. adjustments_enabled(false),
  423. adjustments_brightness(1.0),
  424. adjustments_contrast(1.0),
  425. adjustments_saturation(1.0),
  426. fog_enabled(false),
  427. fog_color(Color(0.5, 0.5, 0.5)),
  428. fog_sun_color(Color(0.8, 0.8, 0.0)),
  429. fog_sun_amount(0),
  430. fog_depth_enabled(true),
  431. fog_depth_begin(10),
  432. fog_depth_end(0),
  433. fog_depth_curve(1),
  434. fog_transmit_enabled(true),
  435. fog_transmit_curve(1),
  436. fog_height_enabled(false),
  437. fog_height_min(10),
  438. fog_height_max(0),
  439. fog_height_curve(1) {
  440. }
  441. };
  442. RID_Owner<Environment> environment_owner;
  443. virtual RID environment_create();
  444. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg);
  445. virtual void environment_set_sky(RID p_env, RID p_sky);
  446. virtual void environment_set_sky_custom_fov(RID p_env, float p_scale);
  447. virtual void environment_set_sky_orientation(RID p_env, const Basis &p_orientation);
  448. virtual void environment_set_bg_color(RID p_env, const Color &p_color);
  449. virtual void environment_set_bg_energy(RID p_env, float p_energy);
  450. virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer);
  451. virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0);
  452. virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id);
  453. 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);
  454. 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);
  455. 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, bool p_high_quality);
  456. virtual void environment_set_glow_map(RID p_env, float p_glow_map_strength, RID p_glow_map);
  457. virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture);
  458. 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);
  459. 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);
  460. 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);
  461. virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp);
  462. virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount);
  463. 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);
  464. virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve);
  465. virtual bool is_environment(RID p_env);
  466. virtual VS::EnvironmentBG environment_get_background(RID p_env);
  467. virtual int environment_get_canvas_max_layer(RID p_env);
  468. /* LIGHT INSTANCE */
  469. struct LightDataUBO {
  470. float light_pos_inv_radius[4];
  471. float light_direction_attenuation[4];
  472. float light_color_energy[4];
  473. float light_params[4]; //spot attenuation, spot angle, specular, shadow enabled
  474. float light_clamp[4];
  475. float light_shadow_color_contact[4];
  476. union {
  477. struct {
  478. float matrix1[16]; //up to here for spot and omni, rest is for directional
  479. float matrix2[16];
  480. float matrix3[16];
  481. float matrix4[16];
  482. };
  483. float matrix[4 * 16];
  484. } shadow;
  485. float shadow_split_offsets[4];
  486. float fade_from;
  487. float pad[3];
  488. };
  489. struct LightInstance : public RID_Data {
  490. struct ShadowTransform {
  491. CameraMatrix camera;
  492. Transform transform;
  493. float farplane;
  494. float split;
  495. float bias_scale;
  496. };
  497. ShadowTransform shadow_transform[4];
  498. RID self;
  499. RID light;
  500. RasterizerStorageGLES3::Light *light_ptr;
  501. Transform transform;
  502. Vector3 light_vector;
  503. Vector3 spot_vector;
  504. float linear_att;
  505. uint64_t shadow_pass;
  506. uint64_t last_scene_pass;
  507. uint64_t last_scene_shadow_pass;
  508. uint64_t last_pass;
  509. uint16_t light_index;
  510. uint16_t light_directional_index;
  511. uint32_t current_shadow_atlas_key;
  512. Vector2 dp;
  513. Rect2 directional_rect;
  514. Set<RID> shadow_atlases; //shadow atlases where this light is registered
  515. LightInstance() {}
  516. };
  517. mutable RID_Owner<LightInstance> light_instance_owner;
  518. virtual RID light_instance_create(RID p_light);
  519. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform);
  520. 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);
  521. virtual void light_instance_mark_visible(RID p_light_instance);
  522. /* REFLECTION INSTANCE */
  523. struct GIProbeInstance : public RID_Data {
  524. RID data;
  525. RasterizerStorageGLES3::GIProbe *probe;
  526. GLuint tex_cache;
  527. Vector3 cell_size_cache;
  528. Vector3 bounds;
  529. Transform transform_to_data;
  530. GIProbeInstance() :
  531. probe(nullptr),
  532. tex_cache(0) {
  533. }
  534. };
  535. mutable RID_Owner<GIProbeInstance> gi_probe_instance_owner;
  536. virtual RID gi_probe_instance_create();
  537. virtual void gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data);
  538. virtual void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform);
  539. virtual void gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds);
  540. /* RENDER LIST */
  541. struct RenderList {
  542. enum {
  543. DEFAULT_MAX_ELEMENTS = 65536,
  544. SORT_FLAG_SKELETON = 1,
  545. SORT_FLAG_INSTANCING = 2,
  546. MAX_DIRECTIONAL_LIGHTS = 16,
  547. DEFAULT_MAX_LIGHTS = 4096,
  548. DEFAULT_MAX_REFLECTIONS = 1024,
  549. DEFAULT_MAX_LIGHTS_PER_OBJECT = 32,
  550. SORT_KEY_PRIORITY_SHIFT = 56,
  551. SORT_KEY_PRIORITY_MASK = 0xFF,
  552. //depth layer for opaque (56-52)
  553. SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT = 52,
  554. SORT_KEY_OPAQUE_DEPTH_LAYER_MASK = 0xF,
  555. //64 bits unsupported in MSVC
  556. #define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 50)
  557. #define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 49)
  558. #define SORT_KEY_LIGHTMAP_CAPTURE_FLAG (uint64_t(1) << 48)
  559. #define SORT_KEY_LIGHTMAP_LAYERED_FLAG (uint64_t(1) << 47)
  560. #define SORT_KEY_LIGHTMAP_FLAG (uint64_t(1) << 46)
  561. #define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 45)
  562. #define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 44)
  563. SORT_KEY_SHADING_SHIFT = 44,
  564. SORT_KEY_SHADING_MASK = 127,
  565. //44-28 material index
  566. SORT_KEY_MATERIAL_INDEX_SHIFT = 28,
  567. //28-8 geometry index
  568. SORT_KEY_GEOMETRY_INDEX_SHIFT = 8,
  569. //bits 5-7 geometry type
  570. SORT_KEY_GEOMETRY_TYPE_SHIFT = 5,
  571. //bits 0-5 for flags
  572. SORT_KEY_OPAQUE_PRE_PASS = 8,
  573. SORT_KEY_CULL_DISABLED_FLAG = 4,
  574. SORT_KEY_SKELETON_FLAG = 2,
  575. SORT_KEY_MIRROR_FLAG = 1
  576. };
  577. int max_elements;
  578. int max_lights;
  579. int max_reflections;
  580. int max_lights_per_object;
  581. struct Element {
  582. RasterizerScene::InstanceBase *instance;
  583. RasterizerStorageGLES3::Geometry *geometry;
  584. RasterizerStorageGLES3::Material *material;
  585. RasterizerStorageGLES3::GeometryOwner *owner;
  586. uint64_t sort_key;
  587. };
  588. Element *base_elements;
  589. Element **elements;
  590. int element_count;
  591. int alpha_element_count;
  592. void clear() {
  593. element_count = 0;
  594. alpha_element_count = 0;
  595. }
  596. //should eventually be replaced by radix
  597. struct SortByKey {
  598. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  599. return A->sort_key < B->sort_key;
  600. }
  601. };
  602. void sort_by_key(bool p_alpha) {
  603. SortArray<Element *, SortByKey> sorter;
  604. if (p_alpha) {
  605. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  606. } else {
  607. sorter.sort(elements, element_count);
  608. }
  609. }
  610. struct SortByDepth {
  611. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  612. return A->instance->depth < B->instance->depth;
  613. }
  614. };
  615. void sort_by_depth(bool p_alpha) { //used for shadows
  616. SortArray<Element *, SortByDepth> sorter;
  617. if (p_alpha) {
  618. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  619. } else {
  620. sorter.sort(elements, element_count);
  621. }
  622. }
  623. struct SortByReverseDepthAndPriority {
  624. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  625. uint32_t layer_A = uint32_t(A->sort_key >> SORT_KEY_PRIORITY_SHIFT);
  626. uint32_t layer_B = uint32_t(B->sort_key >> SORT_KEY_PRIORITY_SHIFT);
  627. if (layer_A == layer_B) {
  628. return A->instance->depth > B->instance->depth;
  629. } else {
  630. return layer_A < layer_B;
  631. }
  632. }
  633. };
  634. void sort_by_reverse_depth_and_priority(bool p_alpha) { //used for alpha
  635. SortArray<Element *, SortByReverseDepthAndPriority> sorter;
  636. if (p_alpha) {
  637. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  638. } else {
  639. sorter.sort(elements, element_count);
  640. }
  641. }
  642. _FORCE_INLINE_ Element *add_element() {
  643. if (element_count + alpha_element_count >= max_elements) {
  644. return nullptr;
  645. }
  646. elements[element_count] = &base_elements[element_count];
  647. return elements[element_count++];
  648. }
  649. _FORCE_INLINE_ Element *add_alpha_element() {
  650. if (element_count + alpha_element_count >= max_elements) {
  651. return nullptr;
  652. }
  653. int idx = max_elements - alpha_element_count - 1;
  654. elements[idx] = &base_elements[idx];
  655. alpha_element_count++;
  656. return elements[idx];
  657. }
  658. void init() {
  659. element_count = 0;
  660. alpha_element_count = 0;
  661. elements = memnew_arr(Element *, max_elements);
  662. base_elements = memnew_arr(Element, max_elements);
  663. for (int i = 0; i < max_elements; i++) {
  664. elements[i] = &base_elements[i]; // assign elements
  665. }
  666. }
  667. RenderList() {
  668. max_elements = DEFAULT_MAX_ELEMENTS;
  669. max_lights = DEFAULT_MAX_LIGHTS;
  670. max_reflections = DEFAULT_MAX_REFLECTIONS;
  671. }
  672. ~RenderList() {
  673. memdelete_arr(elements);
  674. memdelete_arr(base_elements);
  675. }
  676. };
  677. LightInstance *directional_light;
  678. LightInstance *directional_lights[RenderList::MAX_DIRECTIONAL_LIGHTS];
  679. RID first_directional_light;
  680. RenderList render_list;
  681. _FORCE_INLINE_ void _set_cull(bool p_front, bool p_disabled, bool p_reverse_cull);
  682. _FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES3::Material *p_material, bool p_depth_pass, bool p_alpha_pass);
  683. _FORCE_INLINE_ void _setup_geometry(RenderList::Element *e, const Transform &p_view_transform);
  684. _FORCE_INLINE_ void _render_geometry(RenderList::Element *e);
  685. void _setup_light(RenderList::Element *e, const Transform &p_view_transform);
  686. void _render_list(RenderList::Element **p_elements, int p_element_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RasterizerStorageGLES3::Sky *p_sky, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add, bool p_directional_shadows);
  687. _FORCE_INLINE_ void _add_geometry(RasterizerStorageGLES3::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner, int p_material, bool p_depth_pass, bool p_shadow_pass);
  688. _FORCE_INLINE_ void _add_geometry_with_material(RasterizerStorageGLES3::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner, RasterizerStorageGLES3::Material *p_material, bool p_depth_pass, bool p_shadow_pass);
  689. void _draw_sky(RasterizerStorageGLES3::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);
  690. void _setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, const int p_eye = 0, bool p_no_fog = false);
  691. void _setup_directional_light(int p_index, const Transform &p_camera_inverse_transform, bool p_use_shadows);
  692. void _setup_lights(RID *p_light_cull_result, int p_light_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix &p_camera_projection, RID p_shadow_atlas);
  693. void _setup_reflections(RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix &p_camera_projection, RID p_reflection_atlas, Environment *p_env);
  694. void _copy_screen(bool p_invalidate_color = false, bool p_invalidate_depth = false);
  695. void _copy_texture_to_front_buffer(GLuint p_texture); //used for debug
  696. void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass);
  697. void _blur_effect_buffer();
  698. void _render_mrts(Environment *env, const CameraMatrix &p_cam_projection);
  699. void _post_process(Environment *env, const CameraMatrix &p_cam_projection);
  700. void _prepare_depth_texture();
  701. void _bind_depth_texture();
  702. bool _element_needs_directional_add(RenderList::Element *e);
  703. virtual void render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, const int p_eye, 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);
  704. virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count);
  705. virtual bool free(RID p_rid);
  706. virtual void set_scene_pass(uint64_t p_pass);
  707. virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw);
  708. private:
  709. _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array);
  710. _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array);
  711. static const GLenum gl_primitive[];
  712. static const GLenum _cube_side_enum[6];
  713. public:
  714. void iteration();
  715. void initialize();
  716. void finalize();
  717. RasterizerSceneGLES3();
  718. ~RasterizerSceneGLES3();
  719. };
  720. #endif // RASTERIZER_SCENE_GLES3_H