rasterizer_scene_gles3.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*************************************************************************/
  2. /* rasterizer_scene_gles3.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 RASTERIZERSCENEGLES3_H
  31. #define RASTERIZERSCENEGLES3_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. // make sure this struct is padded to be a multiple of 16 bytes for webgl
  137. float pad[2];
  138. } ubo_data;
  139. GLuint scene_ubo;
  140. struct EnvironmentRadianceUBO {
  141. float transform[16];
  142. float ambient_contribution;
  143. uint8_t padding[12];
  144. } env_radiance_data;
  145. GLuint env_radiance_ubo;
  146. GLuint sky_verts;
  147. GLuint sky_array;
  148. GLuint directional_ubo;
  149. GLuint spot_array_ubo;
  150. GLuint omni_array_ubo;
  151. GLuint reflection_array_ubo;
  152. GLuint immediate_buffer;
  153. GLuint immediate_array;
  154. uint32_t ubo_light_size;
  155. uint8_t *spot_array_tmp;
  156. uint8_t *omni_array_tmp;
  157. uint8_t *reflection_array_tmp;
  158. int max_ubo_lights;
  159. int max_forward_lights_per_object;
  160. int max_ubo_reflections;
  161. int max_skeleton_bones;
  162. bool used_contact_shadows;
  163. int spot_light_count;
  164. int omni_light_count;
  165. int directional_light_count;
  166. int reflection_probe_count;
  167. bool cull_front;
  168. bool cull_disabled;
  169. bool used_sss;
  170. bool used_screen_texture;
  171. bool used_depth_prepass;
  172. bool used_depth_texture;
  173. bool prepared_depth_texture;
  174. bool bound_depth_texture;
  175. VS::ViewportDebugDraw debug_draw;
  176. } state;
  177. /* SHADOW ATLAS API */
  178. struct ShadowAtlas : public RID_Data {
  179. enum {
  180. QUADRANT_SHIFT = 27,
  181. SHADOW_INDEX_MASK = (1 << QUADRANT_SHIFT) - 1,
  182. SHADOW_INVALID = 0xFFFFFFFF
  183. };
  184. struct Quadrant {
  185. uint32_t subdivision;
  186. struct Shadow {
  187. RID owner;
  188. uint64_t version;
  189. uint64_t alloc_tick;
  190. Shadow() {
  191. version = 0;
  192. alloc_tick = 0;
  193. }
  194. };
  195. Vector<Shadow> shadows;
  196. Quadrant() {
  197. subdivision = 0; //not in use
  198. }
  199. } quadrants[4];
  200. int size_order[4];
  201. uint32_t smallest_subdiv;
  202. int size;
  203. GLuint fbo;
  204. GLuint depth;
  205. Map<RID, uint32_t> shadow_owners;
  206. };
  207. struct ShadowCubeMap {
  208. GLuint fbo_id[6];
  209. GLuint cubemap;
  210. uint32_t size;
  211. };
  212. Vector<ShadowCubeMap> shadow_cubemaps;
  213. RID_Owner<ShadowAtlas> shadow_atlas_owner;
  214. RID shadow_atlas_create();
  215. void shadow_atlas_set_size(RID p_atlas, int p_size);
  216. void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision);
  217. 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);
  218. bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version);
  219. struct DirectionalShadow {
  220. GLuint fbo;
  221. GLuint depth;
  222. int light_count;
  223. int size;
  224. int current_light;
  225. } directional_shadow;
  226. virtual int get_directional_light_shadow_size(RID p_light_intance);
  227. virtual void set_directional_shadow_count(int p_count);
  228. /* REFLECTION PROBE ATLAS API */
  229. struct ReflectionAtlas : public RID_Data {
  230. int subdiv;
  231. int size;
  232. struct Reflection {
  233. RID owner;
  234. uint64_t last_frame;
  235. };
  236. GLuint fbo[6];
  237. GLuint color;
  238. Vector<Reflection> reflections;
  239. };
  240. mutable RID_Owner<ReflectionAtlas> reflection_atlas_owner;
  241. virtual RID reflection_atlas_create();
  242. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_size);
  243. virtual void reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv);
  244. /* REFLECTION CUBEMAPS */
  245. struct ReflectionCubeMap {
  246. GLuint fbo_id[6];
  247. GLuint cubemap;
  248. GLuint depth;
  249. int size;
  250. };
  251. Vector<ReflectionCubeMap> reflection_cubemaps;
  252. /* REFLECTION PROBE INSTANCE */
  253. struct ReflectionProbeInstance : public RID_Data {
  254. RasterizerStorageGLES3::ReflectionProbe *probe_ptr;
  255. RID probe;
  256. RID self;
  257. RID atlas;
  258. int reflection_atlas_index;
  259. int render_step;
  260. uint64_t last_pass;
  261. int reflection_index;
  262. Transform transform;
  263. };
  264. struct ReflectionProbeDataUBO {
  265. float box_extents[4];
  266. float box_ofs[4];
  267. float params[4]; // intensity, 0, 0, boxproject
  268. float ambient[4]; //color, probe contrib
  269. float atlas_clamp[4];
  270. float local_matrix[16]; //up to here for spot and omni, rest is for directional
  271. //notes: for ambientblend, use distance to edge to blend between already existing global environment
  272. };
  273. mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner;
  274. virtual RID reflection_probe_instance_create(RID p_probe);
  275. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform);
  276. virtual void reflection_probe_release_atlas_index(RID p_instance);
  277. virtual bool reflection_probe_instance_needs_redraw(RID p_instance);
  278. virtual bool reflection_probe_instance_has_reflection(RID p_instance);
  279. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas);
  280. virtual bool reflection_probe_instance_postprocess_step(RID p_instance);
  281. /* ENVIRONMENT API */
  282. struct Environment : public RID_Data {
  283. VS::EnvironmentBG bg_mode;
  284. RID sky;
  285. float sky_custom_fov;
  286. Basis sky_orientation;
  287. Color bg_color;
  288. float bg_energy;
  289. float sky_ambient;
  290. int camera_feed_id;
  291. Color ambient_color;
  292. float ambient_energy;
  293. float ambient_sky_contribution;
  294. int canvas_max_layer;
  295. bool ssr_enabled;
  296. int ssr_max_steps;
  297. float ssr_fade_in;
  298. float ssr_fade_out;
  299. float ssr_depth_tolerance;
  300. bool ssr_roughness;
  301. bool ssao_enabled;
  302. float ssao_intensity;
  303. float ssao_radius;
  304. float ssao_intensity2;
  305. float ssao_radius2;
  306. float ssao_bias;
  307. float ssao_light_affect;
  308. float ssao_ao_channel_affect;
  309. Color ssao_color;
  310. VS::EnvironmentSSAOQuality ssao_quality;
  311. float ssao_bilateral_sharpness;
  312. VS::EnvironmentSSAOBlur ssao_filter;
  313. bool glow_enabled;
  314. int glow_levels;
  315. float glow_intensity;
  316. float glow_strength;
  317. float glow_bloom;
  318. VS::EnvironmentGlowBlendMode glow_blend_mode;
  319. float glow_hdr_bleed_threshold;
  320. float glow_hdr_bleed_scale;
  321. float glow_hdr_luminance_cap;
  322. bool glow_bicubic_upscale;
  323. VS::EnvironmentToneMapper tone_mapper;
  324. float tone_mapper_exposure;
  325. float tone_mapper_exposure_white;
  326. bool auto_exposure;
  327. float auto_exposure_speed;
  328. float auto_exposure_min;
  329. float auto_exposure_max;
  330. float auto_exposure_grey;
  331. bool dof_blur_far_enabled;
  332. float dof_blur_far_distance;
  333. float dof_blur_far_transition;
  334. float dof_blur_far_amount;
  335. VS::EnvironmentDOFBlurQuality dof_blur_far_quality;
  336. bool dof_blur_near_enabled;
  337. float dof_blur_near_distance;
  338. float dof_blur_near_transition;
  339. float dof_blur_near_amount;
  340. VS::EnvironmentDOFBlurQuality dof_blur_near_quality;
  341. bool adjustments_enabled;
  342. float adjustments_brightness;
  343. float adjustments_contrast;
  344. float adjustments_saturation;
  345. RID color_correction;
  346. bool fog_enabled;
  347. Color fog_color;
  348. Color fog_sun_color;
  349. float fog_sun_amount;
  350. bool fog_depth_enabled;
  351. float fog_depth_begin;
  352. float fog_depth_end;
  353. float fog_depth_curve;
  354. bool fog_transmit_enabled;
  355. float fog_transmit_curve;
  356. bool fog_height_enabled;
  357. float fog_height_min;
  358. float fog_height_max;
  359. float fog_height_curve;
  360. Environment() :
  361. bg_mode(VS::ENV_BG_CLEAR_COLOR),
  362. sky_custom_fov(0.0),
  363. bg_energy(1.0),
  364. sky_ambient(0),
  365. camera_feed_id(0),
  366. ambient_energy(1.0),
  367. ambient_sky_contribution(0.0),
  368. canvas_max_layer(0),
  369. ssr_enabled(false),
  370. ssr_max_steps(64),
  371. ssr_fade_in(0.15),
  372. ssr_fade_out(2.0),
  373. ssr_depth_tolerance(0.2),
  374. ssr_roughness(true),
  375. ssao_enabled(false),
  376. ssao_intensity(1.0),
  377. ssao_radius(1.0),
  378. ssao_intensity2(1.0),
  379. ssao_radius2(0.0),
  380. ssao_bias(0.01),
  381. ssao_light_affect(0),
  382. ssao_ao_channel_affect(0),
  383. ssao_quality(VS::ENV_SSAO_QUALITY_LOW),
  384. ssao_bilateral_sharpness(4),
  385. ssao_filter(VS::ENV_SSAO_BLUR_3x3),
  386. glow_enabled(false),
  387. glow_levels((1 << 2) | (1 << 4)),
  388. glow_intensity(0.8),
  389. glow_strength(1.0),
  390. glow_bloom(0.0),
  391. glow_blend_mode(VS::GLOW_BLEND_MODE_SOFTLIGHT),
  392. glow_hdr_bleed_threshold(1.0),
  393. glow_hdr_bleed_scale(2.0),
  394. glow_hdr_luminance_cap(12.0),
  395. glow_bicubic_upscale(false),
  396. tone_mapper(VS::ENV_TONE_MAPPER_LINEAR),
  397. tone_mapper_exposure(1.0),
  398. tone_mapper_exposure_white(1.0),
  399. auto_exposure(false),
  400. auto_exposure_speed(0.5),
  401. auto_exposure_min(0.05),
  402. auto_exposure_max(8),
  403. auto_exposure_grey(0.4),
  404. dof_blur_far_enabled(false),
  405. dof_blur_far_distance(10),
  406. dof_blur_far_transition(5),
  407. dof_blur_far_amount(0.1),
  408. dof_blur_far_quality(VS::ENV_DOF_BLUR_QUALITY_MEDIUM),
  409. dof_blur_near_enabled(false),
  410. dof_blur_near_distance(2),
  411. dof_blur_near_transition(1),
  412. dof_blur_near_amount(0.1),
  413. dof_blur_near_quality(VS::ENV_DOF_BLUR_QUALITY_MEDIUM),
  414. adjustments_enabled(false),
  415. adjustments_brightness(1.0),
  416. adjustments_contrast(1.0),
  417. adjustments_saturation(1.0),
  418. fog_enabled(false),
  419. fog_color(Color(0.5, 0.5, 0.5)),
  420. fog_sun_color(Color(0.8, 0.8, 0.0)),
  421. fog_sun_amount(0),
  422. fog_depth_enabled(true),
  423. fog_depth_begin(10),
  424. fog_depth_end(0),
  425. fog_depth_curve(1),
  426. fog_transmit_enabled(true),
  427. fog_transmit_curve(1),
  428. fog_height_enabled(false),
  429. fog_height_min(10),
  430. fog_height_max(0),
  431. fog_height_curve(1) {
  432. }
  433. };
  434. RID_Owner<Environment> environment_owner;
  435. virtual RID environment_create();
  436. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg);
  437. virtual void environment_set_sky(RID p_env, RID p_sky);
  438. virtual void environment_set_sky_custom_fov(RID p_env, float p_scale);
  439. virtual void environment_set_sky_orientation(RID p_env, const Basis &p_orientation);
  440. virtual void environment_set_bg_color(RID p_env, const Color &p_color);
  441. virtual void environment_set_bg_energy(RID p_env, float p_energy);
  442. virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer);
  443. virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0);
  444. virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id);
  445. 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);
  446. 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);
  447. 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);
  448. virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture);
  449. 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);
  450. 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);
  451. 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);
  452. virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp);
  453. virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount);
  454. 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);
  455. virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve);
  456. virtual bool is_environment(RID p_env);
  457. virtual VS::EnvironmentBG environment_get_background(RID p_env);
  458. virtual int environment_get_canvas_max_layer(RID p_env);
  459. /* LIGHT INSTANCE */
  460. struct LightDataUBO {
  461. float light_pos_inv_radius[4];
  462. float light_direction_attenuation[4];
  463. float light_color_energy[4];
  464. float light_params[4]; //spot attenuation, spot angle, specular, shadow enabled
  465. float light_clamp[4];
  466. float light_shadow_color_contact[4];
  467. union {
  468. struct {
  469. float matrix1[16]; //up to here for spot and omni, rest is for directional
  470. float matrix2[16];
  471. float matrix3[16];
  472. float matrix4[16];
  473. };
  474. float matrix[4 * 16];
  475. } shadow;
  476. float shadow_split_offsets[4];
  477. };
  478. struct LightInstance : public RID_Data {
  479. struct ShadowTransform {
  480. CameraMatrix camera;
  481. Transform transform;
  482. float farplane;
  483. float split;
  484. float bias_scale;
  485. };
  486. ShadowTransform shadow_transform[4];
  487. RID self;
  488. RID light;
  489. RasterizerStorageGLES3::Light *light_ptr;
  490. Transform transform;
  491. Vector3 light_vector;
  492. Vector3 spot_vector;
  493. float linear_att;
  494. uint64_t shadow_pass;
  495. uint64_t last_scene_pass;
  496. uint64_t last_scene_shadow_pass;
  497. uint64_t last_pass;
  498. uint16_t light_index;
  499. uint16_t light_directional_index;
  500. uint32_t current_shadow_atlas_key;
  501. Vector2 dp;
  502. Rect2 directional_rect;
  503. Set<RID> shadow_atlases; //shadow atlases where this light is registered
  504. LightInstance() {}
  505. };
  506. mutable RID_Owner<LightInstance> light_instance_owner;
  507. virtual RID light_instance_create(RID p_light);
  508. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform);
  509. 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);
  510. virtual void light_instance_mark_visible(RID p_light_instance);
  511. /* REFLECTION INSTANCE */
  512. struct GIProbeInstance : public RID_Data {
  513. RID data;
  514. RasterizerStorageGLES3::GIProbe *probe;
  515. GLuint tex_cache;
  516. Vector3 cell_size_cache;
  517. Vector3 bounds;
  518. Transform transform_to_data;
  519. GIProbeInstance() :
  520. probe(NULL),
  521. tex_cache(0) {
  522. }
  523. };
  524. mutable RID_Owner<GIProbeInstance> gi_probe_instance_owner;
  525. virtual RID gi_probe_instance_create();
  526. virtual void gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data);
  527. virtual void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform);
  528. virtual void gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds);
  529. /* RENDER LIST */
  530. struct RenderList {
  531. enum {
  532. DEFAULT_MAX_ELEMENTS = 65536,
  533. SORT_FLAG_SKELETON = 1,
  534. SORT_FLAG_INSTANCING = 2,
  535. MAX_DIRECTIONAL_LIGHTS = 16,
  536. DEFAULT_MAX_LIGHTS = 4096,
  537. DEFAULT_MAX_REFLECTIONS = 1024,
  538. DEFAULT_MAX_LIGHTS_PER_OBJECT = 32,
  539. SORT_KEY_PRIORITY_SHIFT = 56,
  540. SORT_KEY_PRIORITY_MASK = 0xFF,
  541. //depth layer for opaque (56-52)
  542. SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT = 52,
  543. SORT_KEY_OPAQUE_DEPTH_LAYER_MASK = 0xF,
  544. //64 bits unsupported in MSVC
  545. #define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 50)
  546. #define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 49)
  547. #define SORT_KEY_LIGHTMAP_CAPTURE_FLAG (uint64_t(1) << 48)
  548. #define SORT_KEY_LIGHTMAP_LAYERED_FLAG (uint64_t(1) << 47)
  549. #define SORT_KEY_LIGHTMAP_FLAG (uint64_t(1) << 46)
  550. #define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 45)
  551. #define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 44)
  552. SORT_KEY_SHADING_SHIFT = 44,
  553. SORT_KEY_SHADING_MASK = 127,
  554. //44-28 material index
  555. SORT_KEY_MATERIAL_INDEX_SHIFT = 28,
  556. //28-8 geometry index
  557. SORT_KEY_GEOMETRY_INDEX_SHIFT = 8,
  558. //bits 5-7 geometry type
  559. SORT_KEY_GEOMETRY_TYPE_SHIFT = 5,
  560. //bits 0-5 for flags
  561. SORT_KEY_OPAQUE_PRE_PASS = 8,
  562. SORT_KEY_CULL_DISABLED_FLAG = 4,
  563. SORT_KEY_SKELETON_FLAG = 2,
  564. SORT_KEY_MIRROR_FLAG = 1
  565. };
  566. int max_elements;
  567. int max_lights;
  568. int max_reflections;
  569. int max_lights_per_object;
  570. struct Element {
  571. RasterizerScene::InstanceBase *instance;
  572. RasterizerStorageGLES3::Geometry *geometry;
  573. RasterizerStorageGLES3::Material *material;
  574. RasterizerStorageGLES3::GeometryOwner *owner;
  575. uint64_t sort_key;
  576. };
  577. Element *base_elements;
  578. Element **elements;
  579. int element_count;
  580. int alpha_element_count;
  581. void clear() {
  582. element_count = 0;
  583. alpha_element_count = 0;
  584. }
  585. //should eventually be replaced by radix
  586. struct SortByKey {
  587. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  588. return A->sort_key < B->sort_key;
  589. }
  590. };
  591. void sort_by_key(bool p_alpha) {
  592. SortArray<Element *, SortByKey> sorter;
  593. if (p_alpha) {
  594. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  595. } else {
  596. sorter.sort(elements, element_count);
  597. }
  598. }
  599. struct SortByDepth {
  600. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  601. return A->instance->depth < B->instance->depth;
  602. }
  603. };
  604. void sort_by_depth(bool p_alpha) { //used for shadows
  605. SortArray<Element *, SortByDepth> sorter;
  606. if (p_alpha) {
  607. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  608. } else {
  609. sorter.sort(elements, element_count);
  610. }
  611. }
  612. struct SortByReverseDepthAndPriority {
  613. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  614. uint32_t layer_A = uint32_t(A->sort_key >> SORT_KEY_PRIORITY_SHIFT);
  615. uint32_t layer_B = uint32_t(B->sort_key >> SORT_KEY_PRIORITY_SHIFT);
  616. if (layer_A == layer_B) {
  617. return A->instance->depth > B->instance->depth;
  618. } else {
  619. return layer_A < layer_B;
  620. }
  621. }
  622. };
  623. void sort_by_reverse_depth_and_priority(bool p_alpha) { //used for alpha
  624. SortArray<Element *, SortByReverseDepthAndPriority> sorter;
  625. if (p_alpha) {
  626. sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
  627. } else {
  628. sorter.sort(elements, element_count);
  629. }
  630. }
  631. _FORCE_INLINE_ Element *add_element() {
  632. if (element_count + alpha_element_count >= max_elements)
  633. return NULL;
  634. elements[element_count] = &base_elements[element_count];
  635. return elements[element_count++];
  636. }
  637. _FORCE_INLINE_ Element *add_alpha_element() {
  638. if (element_count + alpha_element_count >= max_elements)
  639. return NULL;
  640. int idx = max_elements - alpha_element_count - 1;
  641. elements[idx] = &base_elements[idx];
  642. alpha_element_count++;
  643. return elements[idx];
  644. }
  645. void init() {
  646. element_count = 0;
  647. alpha_element_count = 0;
  648. elements = memnew_arr(Element *, max_elements);
  649. base_elements = memnew_arr(Element, max_elements);
  650. for (int i = 0; i < max_elements; i++)
  651. elements[i] = &base_elements[i]; // assign elements
  652. }
  653. RenderList() {
  654. max_elements = DEFAULT_MAX_ELEMENTS;
  655. max_lights = DEFAULT_MAX_LIGHTS;
  656. max_reflections = DEFAULT_MAX_REFLECTIONS;
  657. }
  658. ~RenderList() {
  659. memdelete_arr(elements);
  660. memdelete_arr(base_elements);
  661. }
  662. };
  663. LightInstance *directional_light;
  664. LightInstance *directional_lights[RenderList::MAX_DIRECTIONAL_LIGHTS];
  665. RID first_directional_light;
  666. RenderList render_list;
  667. _FORCE_INLINE_ void _set_cull(bool p_front, bool p_disabled, bool p_reverse_cull);
  668. _FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES3::Material *p_material, bool p_depth_pass, bool p_alpha_pass);
  669. _FORCE_INLINE_ void _setup_geometry(RenderList::Element *e, const Transform &p_view_transform);
  670. _FORCE_INLINE_ void _render_geometry(RenderList::Element *e);
  671. void _setup_light(RenderList::Element *e, const Transform &p_view_transform);
  672. 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);
  673. _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);
  674. _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);
  675. 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);
  676. void _setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, bool p_no_fog = false);
  677. void _setup_directional_light(int p_index, const Transform &p_camera_inverse_transform, bool p_use_shadows);
  678. 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);
  679. 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);
  680. void _copy_screen(bool p_invalidate_color = false, bool p_invalidate_depth = false);
  681. void _copy_texture_to_front_buffer(GLuint p_texture); //used for debug
  682. void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass);
  683. void _blur_effect_buffer();
  684. void _render_mrts(Environment *env, const CameraMatrix &p_cam_projection);
  685. void _post_process(Environment *env, const CameraMatrix &p_cam_projection);
  686. void _prepare_depth_texture();
  687. void _bind_depth_texture();
  688. 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);
  689. virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count);
  690. virtual bool free(RID p_rid);
  691. virtual void set_scene_pass(uint64_t p_pass);
  692. virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw);
  693. void iteration();
  694. void initialize();
  695. void finalize();
  696. RasterizerSceneGLES3();
  697. ~RasterizerSceneGLES3();
  698. };
  699. #endif // RASTERIZERSCENEGLES3_H