rasterizer_scene_gles3.h 27 KB

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