rasterizer_storage_gles2.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /**************************************************************************/
  2. /* rasterizer_storage_gles2.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_STORAGE_GLES2_H
  31. #define RASTERIZER_STORAGE_GLES2_H
  32. #include "core/bitfield_dynamic.h"
  33. #include "core/pool_vector.h"
  34. #include "core/self_list.h"
  35. #include "drivers/gles_common/rasterizer_asserts.h"
  36. #include "servers/visual/rasterizer.h"
  37. #include "servers/visual/shader_language.h"
  38. #include "shader_compiler_gles2.h"
  39. #include "shader_gles2.h"
  40. #include "shaders/copy.glsl.gen.h"
  41. #include "shaders/cubemap_filter.glsl.gen.h"
  42. class RasterizerCanvasGLES2;
  43. class RasterizerSceneGLES2;
  44. #define WRAPPED_GL_ACTIVE_TEXTURE storage->gl_wrapper.gl_active_texture
  45. class RasterizerStorageGLES2 : public RasterizerStorage {
  46. public:
  47. RasterizerCanvasGLES2 *canvas;
  48. RasterizerSceneGLES2 *scene;
  49. static GLuint system_fbo;
  50. struct Config {
  51. bool shrink_textures_x2;
  52. bool use_fast_texture_filter;
  53. bool use_anisotropic_filter;
  54. bool use_skeleton_software;
  55. bool use_lightmap_filter_bicubic;
  56. bool use_physical_light_attenuation;
  57. int max_vertex_texture_image_units;
  58. int max_texture_image_units;
  59. static const int32_t max_desired_texture_image_units = 64;
  60. int max_texture_size;
  61. int max_cubemap_texture_size;
  62. int max_viewport_dimensions[2];
  63. // TODO implement wireframe in GLES2
  64. // bool generate_wireframes;
  65. Set<String> extensions;
  66. bool float_texture_supported;
  67. bool s3tc_supported;
  68. bool etc1_supported;
  69. bool pvrtc_supported;
  70. bool rgtc_supported;
  71. bool bptc_supported;
  72. bool keep_original_textures;
  73. bool force_vertex_shading;
  74. bool use_rgba_2d_shadows;
  75. bool use_rgba_3d_shadows;
  76. float anisotropic_level;
  77. bool support_32_bits_indices;
  78. bool support_write_depth;
  79. bool support_half_float_vertices;
  80. bool support_npot_repeat_mipmap;
  81. bool support_depth_texture;
  82. bool support_depth_cubemaps;
  83. bool support_shadow_cubemaps;
  84. bool multisample_supported;
  85. bool render_to_mipmap_supported;
  86. GLuint depth_internalformat;
  87. GLuint depth_type;
  88. GLuint depth_buffer_internalformat;
  89. // in some cases the legacy render didn't orphan. We will mark these
  90. // so the user can switch orphaning off for them.
  91. bool should_orphan;
  92. } config;
  93. struct Resources {
  94. GLuint white_tex;
  95. GLuint black_tex;
  96. GLuint transparent_tex;
  97. GLuint normal_tex;
  98. GLuint aniso_tex;
  99. GLuint mipmap_blur_fbo;
  100. GLuint mipmap_blur_color;
  101. GLuint radical_inverse_vdc_cache_tex;
  102. bool use_rgba_2d_shadows;
  103. GLuint quadie;
  104. size_t skeleton_transform_buffer_size;
  105. GLuint skeleton_transform_buffer;
  106. PoolVector<float> skeleton_transform_cpu_buffer;
  107. size_t blend_shape_transform_cpu_buffer_size;
  108. PoolVector<float> blend_shape_transform_cpu_buffer;
  109. } resources;
  110. mutable struct Shaders {
  111. ShaderCompilerGLES2 compiler;
  112. CopyShaderGLES2 copy;
  113. CubemapFilterShaderGLES2 cubemap_filter;
  114. ShaderCompilerGLES2::IdentifierActions actions_canvas;
  115. ShaderCompilerGLES2::IdentifierActions actions_scene;
  116. ShaderCompilerGLES2::IdentifierActions actions_particles;
  117. } shaders;
  118. struct Info {
  119. uint64_t texture_mem;
  120. uint64_t vertex_mem;
  121. struct Render {
  122. uint32_t object_count;
  123. uint32_t draw_call_count;
  124. uint32_t material_switch_count;
  125. uint32_t surface_switch_count;
  126. uint32_t shader_rebind_count;
  127. uint32_t vertices_count;
  128. uint32_t _2d_item_count;
  129. uint32_t _2d_draw_call_count;
  130. void reset() {
  131. object_count = 0;
  132. draw_call_count = 0;
  133. material_switch_count = 0;
  134. surface_switch_count = 0;
  135. shader_rebind_count = 0;
  136. vertices_count = 0;
  137. _2d_item_count = 0;
  138. _2d_draw_call_count = 0;
  139. }
  140. } render, render_final, snap;
  141. Info() :
  142. texture_mem(0),
  143. vertex_mem(0) {
  144. render.reset();
  145. render_final.reset();
  146. }
  147. } info;
  148. void bind_quad_array() const;
  149. /////////////////////////////////////////////////////////////////////////////////////////
  150. //////////////////////////////////DATA///////////////////////////////////////////////////
  151. /////////////////////////////////////////////////////////////////////////////////////////
  152. struct Instantiable : public RID_Data {
  153. SelfList<RasterizerScene::InstanceBase>::List instance_list;
  154. _FORCE_INLINE_ void instance_change_notify(bool p_aabb, bool p_materials) {
  155. SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first();
  156. while (instances) {
  157. instances->self()->base_changed(p_aabb, p_materials);
  158. instances = instances->next();
  159. }
  160. }
  161. _FORCE_INLINE_ void instance_remove_deps() {
  162. SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first();
  163. while (instances) {
  164. instances->self()->base_removed();
  165. instances = instances->next();
  166. }
  167. }
  168. Instantiable() {}
  169. virtual ~Instantiable() {}
  170. };
  171. struct GeometryOwner : public Instantiable {
  172. };
  173. struct Geometry : public Instantiable {
  174. enum Type {
  175. GEOMETRY_INVALID,
  176. GEOMETRY_SURFACE,
  177. GEOMETRY_IMMEDIATE,
  178. GEOMETRY_MULTISURFACE
  179. };
  180. Type type;
  181. RID material;
  182. uint64_t last_pass;
  183. uint32_t index;
  184. virtual void material_changed_notify() {}
  185. Geometry() {
  186. last_pass = 0;
  187. index = 0;
  188. }
  189. };
  190. /////////////////////////////////////////////////////////////////////////////////////////
  191. //////////////////////////////////API////////////////////////////////////////////////////
  192. /////////////////////////////////////////////////////////////////////////////////////////
  193. /* TEXTURE API */
  194. struct RenderTarget;
  195. struct Texture : RID_Data {
  196. Texture *proxy;
  197. Set<Texture *> proxy_owners;
  198. String path;
  199. uint32_t flags;
  200. int width, height, depth;
  201. int alloc_width, alloc_height;
  202. Image::Format format;
  203. VS::TextureType type;
  204. GLenum target;
  205. GLenum gl_format_cache;
  206. GLenum gl_internal_format_cache;
  207. GLenum gl_type_cache;
  208. int data_size;
  209. int total_data_size;
  210. bool ignore_mipmaps;
  211. bool compressed;
  212. bool srgb;
  213. int mipmaps;
  214. bool resize_to_po2;
  215. bool active;
  216. GLenum tex_id;
  217. uint16_t stored_cube_sides;
  218. RenderTarget *render_target;
  219. Vector<Ref<Image>> images;
  220. bool redraw_if_visible;
  221. VisualServer::TextureDetectCallback detect_3d;
  222. void *detect_3d_ud;
  223. VisualServer::TextureDetectCallback detect_srgb;
  224. void *detect_srgb_ud;
  225. VisualServer::TextureDetectCallback detect_normal;
  226. void *detect_normal_ud;
  227. Texture() :
  228. proxy(nullptr),
  229. flags(0),
  230. width(0),
  231. height(0),
  232. alloc_width(0),
  233. alloc_height(0),
  234. format(Image::FORMAT_L8),
  235. type(VS::TEXTURE_TYPE_2D),
  236. target(0),
  237. data_size(0),
  238. total_data_size(0),
  239. ignore_mipmaps(false),
  240. compressed(false),
  241. mipmaps(0),
  242. resize_to_po2(false),
  243. active(false),
  244. tex_id(0),
  245. stored_cube_sides(0),
  246. render_target(nullptr),
  247. redraw_if_visible(false),
  248. detect_3d(nullptr),
  249. detect_3d_ud(nullptr),
  250. detect_srgb(nullptr),
  251. detect_srgb_ud(nullptr),
  252. detect_normal(nullptr),
  253. detect_normal_ud(nullptr) {
  254. }
  255. _ALWAYS_INLINE_ Texture *get_ptr() {
  256. if (proxy) {
  257. return proxy; //->get_ptr(); only one level of indirection, else not inlining possible.
  258. } else {
  259. return this;
  260. }
  261. }
  262. ~Texture() {
  263. if (tex_id != 0) {
  264. glDeleteTextures(1, &tex_id);
  265. }
  266. for (Set<Texture *>::Element *E = proxy_owners.front(); E; E = E->next()) {
  267. E->get()->proxy = nullptr;
  268. }
  269. if (proxy) {
  270. proxy->proxy_owners.erase(this);
  271. }
  272. }
  273. };
  274. mutable RID_Owner<Texture> texture_owner;
  275. Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const;
  276. virtual RID texture_create();
  277. virtual void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VS::TextureType p_type, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT);
  278. virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  279. virtual void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer = 0);
  280. virtual Ref<Image> texture_get_data(RID p_texture, int p_layer = 0) const;
  281. virtual void texture_set_flags(RID p_texture, uint32_t p_flags);
  282. virtual uint32_t texture_get_flags(RID p_texture) const;
  283. virtual Image::Format texture_get_format(RID p_texture) const;
  284. virtual VS::TextureType texture_get_type(RID p_texture) const;
  285. virtual uint32_t texture_get_texid(RID p_texture) const;
  286. virtual uint32_t texture_get_width(RID p_texture) const;
  287. virtual uint32_t texture_get_height(RID p_texture) const;
  288. virtual uint32_t texture_get_depth(RID p_texture) const;
  289. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth);
  290. virtual void texture_bind(RID p_texture, uint32_t p_texture_no);
  291. virtual void texture_set_path(RID p_texture, const String &p_path);
  292. virtual String texture_get_path(RID p_texture) const;
  293. virtual void texture_set_shrink_all_x2_on_set_data(bool p_enable);
  294. virtual void texture_debug_usage(List<VS::TextureInfo> *r_info);
  295. virtual RID texture_create_radiance_cubemap(RID p_source, int p_resolution = -1) const;
  296. virtual void textures_keep_original(bool p_enable);
  297. virtual void texture_set_proxy(RID p_texture, RID p_proxy);
  298. virtual Size2 texture_size_with_proxy(RID p_texture) const;
  299. virtual void texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata);
  300. virtual void texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata);
  301. virtual void texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata);
  302. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable);
  303. /* SKY API */
  304. struct Sky : public RID_Data {
  305. RID panorama;
  306. GLuint radiance;
  307. int radiance_size;
  308. };
  309. mutable RID_Owner<Sky> sky_owner;
  310. virtual RID sky_create();
  311. virtual void sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size);
  312. /* SHADER API */
  313. struct Material;
  314. struct Shader : public RID_Data {
  315. RID self;
  316. VS::ShaderMode mode;
  317. ShaderGLES2 *shader;
  318. String code;
  319. SelfList<Material>::List materials;
  320. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  321. uint32_t texture_count;
  322. uint32_t custom_code_id;
  323. uint32_t version;
  324. SelfList<Shader> dirty_list;
  325. Map<StringName, RID> default_textures;
  326. Vector<ShaderLanguage::ShaderNode::Uniform::Hint> texture_hints;
  327. bool valid;
  328. String path;
  329. uint32_t index;
  330. uint64_t last_pass;
  331. struct CanvasItem {
  332. enum BlendMode {
  333. BLEND_MODE_MIX,
  334. BLEND_MODE_ADD,
  335. BLEND_MODE_SUB,
  336. BLEND_MODE_MUL,
  337. BLEND_MODE_PMALPHA,
  338. };
  339. int blend_mode;
  340. enum LightMode {
  341. LIGHT_MODE_NORMAL,
  342. LIGHT_MODE_UNSHADED,
  343. LIGHT_MODE_LIGHT_ONLY
  344. };
  345. int light_mode;
  346. // these flags are specifically for batching
  347. // some of the logic is thus in rasterizer_storage.cpp
  348. // we could alternatively set bitflags for each 'uses' and test on the fly
  349. // defined in RasterizerStorageCommon::BatchFlags
  350. unsigned int batch_flags;
  351. bool uses_screen_texture;
  352. bool uses_screen_uv;
  353. bool uses_time;
  354. bool uses_modulate;
  355. bool uses_color;
  356. bool uses_vertex;
  357. // all these should disable item joining if used in a custom shader
  358. bool uses_world_matrix;
  359. bool uses_extra_matrix;
  360. bool uses_projection_matrix;
  361. bool uses_instance_custom;
  362. } canvas_item;
  363. struct Spatial {
  364. enum BlendMode {
  365. BLEND_MODE_MIX,
  366. BLEND_MODE_ADD,
  367. BLEND_MODE_SUB,
  368. BLEND_MODE_MUL,
  369. };
  370. int blend_mode;
  371. enum DepthDrawMode {
  372. DEPTH_DRAW_OPAQUE,
  373. DEPTH_DRAW_ALWAYS,
  374. DEPTH_DRAW_NEVER,
  375. DEPTH_DRAW_ALPHA_PREPASS,
  376. };
  377. int depth_draw_mode;
  378. enum CullMode {
  379. CULL_MODE_FRONT,
  380. CULL_MODE_BACK,
  381. CULL_MODE_DISABLED,
  382. };
  383. int cull_mode;
  384. bool uses_alpha;
  385. bool uses_alpha_scissor;
  386. bool unshaded;
  387. bool no_depth_test;
  388. bool uses_vertex;
  389. bool uses_discard;
  390. bool uses_sss;
  391. bool uses_screen_texture;
  392. bool uses_depth_texture;
  393. bool uses_time;
  394. bool uses_tangent;
  395. bool uses_ensure_correct_normals;
  396. bool writes_modelview_or_projection;
  397. bool uses_vertex_lighting;
  398. bool uses_world_coordinates;
  399. } spatial;
  400. struct Particles {
  401. } particles;
  402. bool uses_vertex_time;
  403. bool uses_fragment_time;
  404. Shader() :
  405. dirty_list(this) {
  406. shader = nullptr;
  407. valid = false;
  408. custom_code_id = 0;
  409. version = 1;
  410. last_pass = 0;
  411. }
  412. };
  413. mutable RID_Owner<Shader> shader_owner;
  414. mutable SelfList<Shader>::List _shader_dirty_list;
  415. void _shader_make_dirty(Shader *p_shader);
  416. virtual RID shader_create();
  417. virtual void shader_set_code(RID p_shader, const String &p_code);
  418. virtual String shader_get_code(RID p_shader) const;
  419. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const;
  420. virtual void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture);
  421. virtual RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const;
  422. virtual void shader_add_custom_define(RID p_shader, const String &p_define);
  423. virtual void shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const;
  424. virtual void shader_remove_custom_define(RID p_shader, const String &p_define);
  425. void set_shader_async_hidden_forbidden(bool p_forbidden) {}
  426. bool is_shader_async_hidden_forbidden() { return false; }
  427. void _update_shader(Shader *p_shader) const;
  428. void update_dirty_shaders();
  429. /* COMMON MATERIAL API */
  430. struct Material : public RID_Data {
  431. Shader *shader;
  432. Map<StringName, Variant> params;
  433. SelfList<Material> list;
  434. SelfList<Material> dirty_list;
  435. Vector<Pair<StringName, RID>> textures;
  436. float line_width;
  437. int render_priority;
  438. RID next_pass;
  439. uint32_t index;
  440. uint64_t last_pass;
  441. Map<Geometry *, int> geometry_owners;
  442. Map<RasterizerScene::InstanceBase *, int> instance_owners;
  443. bool can_cast_shadow_cache;
  444. bool is_animated_cache;
  445. Material() :
  446. list(this),
  447. dirty_list(this) {
  448. can_cast_shadow_cache = false;
  449. is_animated_cache = false;
  450. shader = nullptr;
  451. line_width = 1.0;
  452. last_pass = 0;
  453. render_priority = 0;
  454. }
  455. };
  456. mutable SelfList<Material>::List _material_dirty_list;
  457. void _material_make_dirty(Material *p_material) const;
  458. void _material_add_geometry(RID p_material, Geometry *p_geometry);
  459. void _material_remove_geometry(RID p_material, Geometry *p_geometry);
  460. void _update_material(Material *p_material);
  461. mutable RID_Owner<Material> material_owner;
  462. virtual RID material_create();
  463. virtual void material_set_shader(RID p_material, RID p_shader);
  464. virtual RID material_get_shader(RID p_material) const;
  465. virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value);
  466. virtual Variant material_get_param(RID p_material, const StringName &p_param) const;
  467. virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const;
  468. virtual void material_set_line_width(RID p_material, float p_width);
  469. virtual void material_set_next_pass(RID p_material, RID p_next_material);
  470. virtual bool material_is_animated(RID p_material);
  471. virtual bool material_casts_shadows(RID p_material);
  472. virtual bool material_uses_tangents(RID p_material);
  473. virtual bool material_uses_ensure_correct_normals(RID p_material);
  474. virtual void material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance);
  475. virtual void material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance);
  476. virtual void material_set_render_priority(RID p_material, int priority);
  477. void update_dirty_materials();
  478. /* MESH API */
  479. struct Mesh;
  480. struct Surface : public Geometry {
  481. struct Attrib {
  482. bool enabled;
  483. bool integer;
  484. GLuint index;
  485. GLint size;
  486. GLenum type;
  487. GLboolean normalized;
  488. GLsizei stride;
  489. uint32_t offset;
  490. };
  491. Attrib attribs[VS::ARRAY_MAX];
  492. Mesh *mesh;
  493. uint32_t format;
  494. GLuint vertex_id;
  495. GLuint index_id;
  496. AABB aabb;
  497. int array_len;
  498. int index_array_len;
  499. int max_bone;
  500. int array_byte_size;
  501. int index_array_byte_size;
  502. VS::PrimitiveType primitive;
  503. Vector<AABB> skeleton_bone_aabb;
  504. Vector<bool> skeleton_bone_used;
  505. bool active;
  506. PoolVector<uint8_t> data;
  507. PoolVector<uint8_t> index_data;
  508. Vector<PoolVector<uint8_t>> blend_shape_data;
  509. GLuint blend_shape_buffer_id;
  510. size_t blend_shape_buffer_size;
  511. int total_data_size;
  512. Surface() :
  513. mesh(nullptr),
  514. array_len(0),
  515. index_array_len(0),
  516. array_byte_size(0),
  517. index_array_byte_size(0),
  518. primitive(VS::PRIMITIVE_POINTS),
  519. active(false),
  520. total_data_size(0) {
  521. }
  522. };
  523. struct MultiMesh;
  524. struct Mesh : public GeometryOwner {
  525. bool active;
  526. Vector<Surface *> surfaces;
  527. int blend_shape_count;
  528. VS::BlendShapeMode blend_shape_mode;
  529. PoolRealArray blend_shape_values;
  530. SelfList<Mesh> update_list;
  531. AABB custom_aabb;
  532. mutable uint64_t last_pass;
  533. SelfList<MultiMesh>::List multimeshes;
  534. _FORCE_INLINE_ void update_multimeshes() {
  535. SelfList<MultiMesh> *mm = multimeshes.first();
  536. while (mm) {
  537. mm->self()->instance_change_notify(false, true);
  538. mm = mm->next();
  539. }
  540. }
  541. Mesh() :
  542. blend_shape_count(0),
  543. blend_shape_mode(VS::BLEND_SHAPE_MODE_NORMALIZED),
  544. blend_shape_values(PoolRealArray()),
  545. update_list(this) {
  546. }
  547. };
  548. mutable RID_Owner<Mesh> mesh_owner;
  549. SelfList<Mesh>::List blend_shapes_update_list;
  550. virtual RID mesh_create();
  551. virtual void mesh_add_surface(RID p_mesh, uint32_t p_format, VS::PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t>> &p_blend_shapes = Vector<PoolVector<uint8_t>>(), const Vector<AABB> &p_bone_aabbs = Vector<AABB>());
  552. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_amount);
  553. virtual int mesh_get_blend_shape_count(RID p_mesh) const;
  554. virtual void mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode);
  555. virtual VS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const;
  556. virtual void mesh_set_blend_shape_values(RID p_mesh, PoolVector<float> p_values);
  557. virtual PoolVector<float> mesh_get_blend_shape_values(RID p_mesh) const;
  558. virtual void mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data);
  559. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material);
  560. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const;
  561. virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const;
  562. virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const;
  563. virtual PoolVector<uint8_t> mesh_surface_get_array(RID p_mesh, int p_surface) const;
  564. virtual PoolVector<uint8_t> mesh_surface_get_index_array(RID p_mesh, int p_surface) const;
  565. virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const;
  566. virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const;
  567. virtual AABB mesh_surface_get_aabb(RID p_mesh, int p_surface) const;
  568. virtual Vector<PoolVector<uint8_t>> mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const;
  569. virtual Vector<AABB> mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const;
  570. virtual void mesh_remove_surface(RID p_mesh, int p_surface);
  571. virtual int mesh_get_surface_count(RID p_mesh) const;
  572. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb);
  573. virtual AABB mesh_get_custom_aabb(RID p_mesh) const;
  574. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton) const;
  575. virtual void mesh_clear(RID p_mesh);
  576. void update_dirty_blend_shapes();
  577. /* MULTIMESH API */
  578. struct MultiMesh : public GeometryOwner {
  579. RID mesh;
  580. int size;
  581. VS::MultimeshTransformFormat transform_format;
  582. VS::MultimeshColorFormat color_format;
  583. VS::MultimeshCustomDataFormat custom_data_format;
  584. Vector<float> data;
  585. AABB aabb;
  586. SelfList<MultiMesh> update_list;
  587. SelfList<MultiMesh> mesh_list;
  588. int visible_instances;
  589. int xform_floats;
  590. int color_floats;
  591. int custom_data_floats;
  592. bool dirty_aabb;
  593. bool dirty_data;
  594. MMInterpolator interpolator;
  595. LocalVector<RID> linked_canvas_items;
  596. MultiMesh() :
  597. size(0),
  598. transform_format(VS::MULTIMESH_TRANSFORM_2D),
  599. color_format(VS::MULTIMESH_COLOR_NONE),
  600. custom_data_format(VS::MULTIMESH_CUSTOM_DATA_NONE),
  601. update_list(this),
  602. mesh_list(this),
  603. visible_instances(-1),
  604. xform_floats(0),
  605. color_floats(0),
  606. custom_data_floats(0),
  607. dirty_aabb(true),
  608. dirty_data(true) {
  609. }
  610. };
  611. mutable RID_Owner<MultiMesh> multimesh_owner;
  612. SelfList<MultiMesh>::List multimesh_update_list;
  613. virtual RID _multimesh_create();
  614. virtual void _multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data = VS::MULTIMESH_CUSTOM_DATA_NONE);
  615. virtual int _multimesh_get_instance_count(RID p_multimesh) const;
  616. virtual void _multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  617. virtual void _multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform);
  618. virtual void _multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform);
  619. virtual void _multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  620. virtual void _multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_custom_data);
  621. virtual RID _multimesh_get_mesh(RID p_multimesh) const;
  622. virtual Transform _multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  623. virtual Transform2D _multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const;
  624. virtual Color _multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  625. virtual Color _multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const;
  626. virtual void _multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array);
  627. virtual void _multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  628. virtual int _multimesh_get_visible_instances(RID p_multimesh) const;
  629. virtual AABB _multimesh_get_aabb(RID p_multimesh) const;
  630. virtual MMInterpolator *_multimesh_get_interpolator(RID p_multimesh) const;
  631. virtual void multimesh_attach_canvas_item(RID p_multimesh, RID p_canvas_item, bool p_attach);
  632. void update_dirty_multimeshes();
  633. /* IMMEDIATE API */
  634. struct Immediate : public Geometry {
  635. struct Chunk {
  636. RID texture;
  637. VS::PrimitiveType primitive;
  638. Vector<Vector3> vertices;
  639. Vector<Vector3> normals;
  640. Vector<Plane> tangents;
  641. Vector<Color> colors;
  642. Vector<Vector2> uvs;
  643. Vector<Vector2> uv2s;
  644. };
  645. List<Chunk> chunks;
  646. bool building;
  647. int mask;
  648. AABB aabb;
  649. Immediate() {
  650. type = GEOMETRY_IMMEDIATE;
  651. building = false;
  652. }
  653. };
  654. Vector3 chunk_normal;
  655. Plane chunk_tangent;
  656. Color chunk_color;
  657. Vector2 chunk_uv;
  658. Vector2 chunk_uv2;
  659. mutable RID_Owner<Immediate> immediate_owner;
  660. virtual RID immediate_create();
  661. virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture = RID());
  662. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex);
  663. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal);
  664. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent);
  665. virtual void immediate_color(RID p_immediate, const Color &p_color);
  666. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv);
  667. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv);
  668. virtual void immediate_end(RID p_immediate);
  669. virtual void immediate_clear(RID p_immediate);
  670. virtual void immediate_set_material(RID p_immediate, RID p_material);
  671. virtual RID immediate_get_material(RID p_immediate) const;
  672. virtual AABB immediate_get_aabb(RID p_immediate) const;
  673. /* SKELETON API */
  674. struct Skeleton : RID_Data {
  675. bool use_2d;
  676. int size;
  677. uint32_t revision;
  678. // TODO use float textures for storage
  679. Vector<float> bone_data;
  680. GLuint tex_id;
  681. SelfList<Skeleton> update_list;
  682. Set<RasterizerScene::InstanceBase *> instances;
  683. Transform2D base_transform_2d;
  684. LocalVector<RID> linked_canvas_items;
  685. Skeleton() :
  686. use_2d(false),
  687. size(0),
  688. revision(1),
  689. tex_id(0),
  690. update_list(this) {
  691. }
  692. };
  693. mutable RID_Owner<Skeleton> skeleton_owner;
  694. SelfList<Skeleton>::List skeleton_update_list;
  695. void update_dirty_skeletons();
  696. virtual RID skeleton_create();
  697. virtual void skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton = false);
  698. virtual int skeleton_get_bone_count(RID p_skeleton) const;
  699. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform);
  700. virtual Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) const;
  701. virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform);
  702. virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const;
  703. virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform);
  704. virtual uint32_t skeleton_get_revision(RID p_skeleton) const;
  705. virtual void skeleton_attach_canvas_item(RID p_skeleton, RID p_canvas_item, bool p_attach);
  706. void _update_skeleton_transform_buffer(const PoolVector<float> &p_data, size_t p_size);
  707. /* Light API */
  708. struct Light : Instantiable {
  709. VS::LightType type;
  710. float param[VS::LIGHT_PARAM_MAX];
  711. Color color;
  712. Color shadow_color;
  713. RID projector;
  714. bool shadow;
  715. bool negative;
  716. bool reverse_cull;
  717. uint32_t cull_mask;
  718. VS::LightBakeMode bake_mode;
  719. VS::LightOmniShadowMode omni_shadow_mode;
  720. VS::LightOmniShadowDetail omni_shadow_detail;
  721. VS::LightDirectionalShadowMode directional_shadow_mode;
  722. VS::LightDirectionalShadowDepthRangeMode directional_range_mode;
  723. bool directional_blend_splits;
  724. uint64_t version;
  725. };
  726. mutable RID_Owner<Light> light_owner;
  727. virtual RID light_create(VS::LightType p_type);
  728. virtual void light_set_color(RID p_light, const Color &p_color);
  729. virtual void light_set_param(RID p_light, VS::LightParam p_param, float p_value);
  730. virtual void light_set_shadow(RID p_light, bool p_enabled);
  731. virtual void light_set_shadow_color(RID p_light, const Color &p_color);
  732. virtual void light_set_projector(RID p_light, RID p_texture);
  733. virtual void light_set_negative(RID p_light, bool p_enable);
  734. virtual void light_set_cull_mask(RID p_light, uint32_t p_mask);
  735. virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled);
  736. virtual void light_set_use_gi(RID p_light, bool p_enabled);
  737. virtual void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode);
  738. virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode);
  739. virtual void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail);
  740. virtual void light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode);
  741. virtual void light_directional_set_blend_splits(RID p_light, bool p_enable);
  742. virtual bool light_directional_get_blend_splits(RID p_light) const;
  743. virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light);
  744. virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light);
  745. virtual void light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode);
  746. virtual VS::LightDirectionalShadowDepthRangeMode light_directional_get_shadow_depth_range_mode(RID p_light) const;
  747. virtual bool light_has_shadow(RID p_light) const;
  748. virtual VS::LightType light_get_type(RID p_light) const;
  749. virtual float light_get_param(RID p_light, VS::LightParam p_param);
  750. virtual Color light_get_color(RID p_light);
  751. virtual bool light_get_use_gi(RID p_light);
  752. virtual VS::LightBakeMode light_get_bake_mode(RID p_light);
  753. virtual AABB light_get_aabb(RID p_light) const;
  754. virtual uint64_t light_get_version(RID p_light) const;
  755. /* PROBE API */
  756. struct ReflectionProbe : Instantiable {
  757. VS::ReflectionProbeUpdateMode update_mode;
  758. float intensity;
  759. Color interior_ambient;
  760. float interior_ambient_energy;
  761. float interior_ambient_probe_contrib;
  762. float max_distance;
  763. Vector3 extents;
  764. Vector3 origin_offset;
  765. bool interior;
  766. bool box_projection;
  767. bool enable_shadows;
  768. uint32_t cull_mask;
  769. int resolution;
  770. };
  771. mutable RID_Owner<ReflectionProbe> reflection_probe_owner;
  772. virtual RID reflection_probe_create();
  773. virtual void reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode);
  774. virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity);
  775. virtual void reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient);
  776. virtual void reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy);
  777. virtual void reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib);
  778. virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance);
  779. virtual void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents);
  780. virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset);
  781. virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable);
  782. virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable);
  783. virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable);
  784. virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers);
  785. virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution);
  786. virtual AABB reflection_probe_get_aabb(RID p_probe) const;
  787. virtual VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const;
  788. virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const;
  789. virtual int reflection_probe_get_resolution(RID p_probe) const;
  790. virtual Vector3 reflection_probe_get_extents(RID p_probe) const;
  791. virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const;
  792. virtual float reflection_probe_get_origin_max_distance(RID p_probe) const;
  793. virtual bool reflection_probe_renders_shadows(RID p_probe) const;
  794. /* GI PROBE API */
  795. virtual RID gi_probe_create();
  796. virtual void gi_probe_set_bounds(RID p_probe, const AABB &p_bounds);
  797. virtual AABB gi_probe_get_bounds(RID p_probe) const;
  798. virtual void gi_probe_set_cell_size(RID p_probe, float p_size);
  799. virtual float gi_probe_get_cell_size(RID p_probe) const;
  800. virtual void gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform);
  801. virtual Transform gi_probe_get_to_cell_xform(RID p_probe) const;
  802. virtual void gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data);
  803. virtual PoolVector<int> gi_probe_get_dynamic_data(RID p_probe) const;
  804. virtual void gi_probe_set_dynamic_range(RID p_probe, int p_range);
  805. virtual int gi_probe_get_dynamic_range(RID p_probe) const;
  806. virtual void gi_probe_set_energy(RID p_probe, float p_range);
  807. virtual float gi_probe_get_energy(RID p_probe) const;
  808. virtual void gi_probe_set_bias(RID p_probe, float p_range);
  809. virtual float gi_probe_get_bias(RID p_probe) const;
  810. virtual void gi_probe_set_normal_bias(RID p_probe, float p_range);
  811. virtual float gi_probe_get_normal_bias(RID p_probe) const;
  812. virtual void gi_probe_set_propagation(RID p_probe, float p_range);
  813. virtual float gi_probe_get_propagation(RID p_probe) const;
  814. virtual void gi_probe_set_interior(RID p_probe, bool p_enable);
  815. virtual bool gi_probe_is_interior(RID p_probe) const;
  816. virtual void gi_probe_set_compress(RID p_probe, bool p_enable);
  817. virtual bool gi_probe_is_compressed(RID p_probe) const;
  818. virtual uint32_t gi_probe_get_version(RID p_probe);
  819. virtual RID gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression);
  820. virtual void gi_probe_dynamic_data_update(RID p_gi_probe_data, int p_depth_slice, int p_slice_count, int p_mipmap, const void *p_data);
  821. /* LIGHTMAP */
  822. struct LightmapCapture : public Instantiable {
  823. PoolVector<LightmapCaptureOctree> octree;
  824. AABB bounds;
  825. Transform cell_xform;
  826. int cell_subdiv;
  827. float energy;
  828. bool interior;
  829. SelfList<LightmapCapture> update_list;
  830. LightmapCapture() :
  831. update_list(this) {
  832. energy = 1.0;
  833. cell_subdiv = 1;
  834. interior = false;
  835. }
  836. };
  837. SelfList<LightmapCapture>::List capture_update_list;
  838. void update_dirty_captures();
  839. mutable RID_Owner<LightmapCapture> lightmap_capture_data_owner;
  840. virtual RID lightmap_capture_create();
  841. virtual void lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds);
  842. virtual AABB lightmap_capture_get_bounds(RID p_capture) const;
  843. virtual void lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree);
  844. virtual PoolVector<uint8_t> lightmap_capture_get_octree(RID p_capture) const;
  845. virtual void lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform);
  846. virtual Transform lightmap_capture_get_octree_cell_transform(RID p_capture) const;
  847. virtual void lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv);
  848. virtual int lightmap_capture_get_octree_cell_subdiv(RID p_capture) const;
  849. virtual void lightmap_capture_set_energy(RID p_capture, float p_energy);
  850. virtual float lightmap_capture_get_energy(RID p_capture) const;
  851. virtual void lightmap_capture_set_interior(RID p_capture, bool p_interior);
  852. virtual bool lightmap_capture_is_interior(RID p_capture) const;
  853. virtual const PoolVector<LightmapCaptureOctree> *lightmap_capture_get_octree_ptr(RID p_capture) const;
  854. /* PARTICLES */
  855. void update_particles();
  856. virtual RID particles_create();
  857. virtual void particles_set_emitting(RID p_particles, bool p_emitting);
  858. virtual bool particles_get_emitting(RID p_particles);
  859. virtual void particles_set_amount(RID p_particles, int p_amount);
  860. virtual void particles_set_lifetime(RID p_particles, float p_lifetime);
  861. virtual void particles_set_one_shot(RID p_particles, bool p_one_shot);
  862. virtual void particles_set_pre_process_time(RID p_particles, float p_time);
  863. virtual void particles_set_explosiveness_ratio(RID p_particles, float p_ratio);
  864. virtual void particles_set_randomness_ratio(RID p_particles, float p_ratio);
  865. virtual void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb);
  866. virtual void particles_set_speed_scale(RID p_particles, float p_scale);
  867. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable);
  868. virtual void particles_set_process_material(RID p_particles, RID p_material);
  869. virtual void particles_set_fixed_fps(RID p_particles, int p_fps);
  870. virtual void particles_set_fractional_delta(RID p_particles, bool p_enable);
  871. virtual void particles_restart(RID p_particles);
  872. virtual void particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order);
  873. virtual void particles_set_draw_passes(RID p_particles, int p_passes);
  874. virtual void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh);
  875. virtual void particles_request_process(RID p_particles);
  876. virtual AABB particles_get_current_aabb(RID p_particles);
  877. virtual AABB particles_get_aabb(RID p_particles) const;
  878. virtual void particles_set_emission_transform(RID p_particles, const Transform &p_transform);
  879. virtual int particles_get_draw_passes(RID p_particles) const;
  880. virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const;
  881. virtual bool particles_is_inactive(RID p_particles) const;
  882. /* INSTANCE */
  883. virtual void instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance);
  884. virtual void instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance);
  885. virtual void instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance);
  886. virtual void instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance);
  887. /* RENDER TARGET */
  888. struct RenderTarget : public RID_Data {
  889. GLuint fbo;
  890. GLuint color;
  891. GLuint depth;
  892. GLuint multisample_fbo;
  893. GLuint multisample_color;
  894. GLuint multisample_depth;
  895. bool multisample_active;
  896. struct Effect {
  897. GLuint fbo;
  898. int width;
  899. int height;
  900. GLuint color;
  901. Effect() :
  902. fbo(0),
  903. width(0),
  904. height(0),
  905. color(0) {
  906. }
  907. };
  908. Effect copy_screen_effect;
  909. struct MipMaps {
  910. struct Size {
  911. GLuint fbo = 0;
  912. GLuint color = 0;
  913. int width = 0;
  914. int height = 0;
  915. };
  916. Vector<Size> sizes;
  917. GLuint color;
  918. int levels;
  919. MipMaps() :
  920. color(0),
  921. levels(0) {
  922. }
  923. };
  924. MipMaps mip_maps[2];
  925. struct External {
  926. GLuint fbo;
  927. GLuint color;
  928. GLuint depth;
  929. bool depth_owned;
  930. External() :
  931. fbo(0),
  932. color(0),
  933. depth(0),
  934. depth_owned(false) {
  935. }
  936. } external;
  937. int x, y, width, height;
  938. bool flags[RENDER_TARGET_FLAG_MAX];
  939. bool used_in_frame;
  940. VS::ViewportMSAA msaa;
  941. bool use_fxaa;
  942. bool use_debanding;
  943. float sharpen_intensity;
  944. RID texture;
  945. bool used_dof_blur_near;
  946. bool mip_maps_allocated;
  947. RenderTarget() :
  948. fbo(0),
  949. color(0),
  950. depth(0),
  951. multisample_fbo(0),
  952. multisample_color(0),
  953. multisample_depth(0),
  954. multisample_active(false),
  955. x(0),
  956. y(0),
  957. width(0),
  958. height(0),
  959. used_in_frame(false),
  960. msaa(VS::VIEWPORT_MSAA_DISABLED),
  961. use_fxaa(false),
  962. use_debanding(false),
  963. sharpen_intensity(0.0),
  964. used_dof_blur_near(false),
  965. mip_maps_allocated(false) {
  966. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; ++i) {
  967. flags[i] = false;
  968. }
  969. external.fbo = 0;
  970. }
  971. };
  972. mutable RID_Owner<RenderTarget> render_target_owner;
  973. void _render_target_clear(RenderTarget *rt);
  974. void _render_target_allocate(RenderTarget *rt);
  975. virtual RID render_target_create();
  976. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y);
  977. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height);
  978. virtual RID render_target_get_texture(RID p_render_target) const;
  979. virtual uint32_t render_target_get_depth_texture_id(RID p_render_target) const;
  980. virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id, unsigned int p_depth_id);
  981. virtual void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value);
  982. virtual bool render_target_was_used(RID p_render_target);
  983. virtual void render_target_clear_used(RID p_render_target);
  984. virtual void render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa);
  985. virtual void render_target_set_use_fxaa(RID p_render_target, bool p_fxaa);
  986. virtual void render_target_set_use_debanding(RID p_render_target, bool p_debanding);
  987. virtual void render_target_set_sharpen_intensity(RID p_render_target, float p_intensity);
  988. /* CANVAS SHADOW */
  989. struct CanvasLightShadow : public RID_Data {
  990. int size;
  991. int height;
  992. GLuint fbo;
  993. GLuint depth;
  994. GLuint distance; //for older devices
  995. };
  996. RID_Owner<CanvasLightShadow> canvas_light_shadow_owner;
  997. virtual RID canvas_light_shadow_buffer_create(int p_width);
  998. /* LIGHT SHADOW MAPPING */
  999. struct CanvasOccluder : public RID_Data {
  1000. GLuint vertex_id; // 0 means, unconfigured
  1001. GLuint index_id; // 0 means, unconfigured
  1002. PoolVector<Vector2> lines;
  1003. int len;
  1004. };
  1005. RID_Owner<CanvasOccluder> canvas_occluder_owner;
  1006. virtual RID canvas_light_occluder_create();
  1007. virtual void canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines);
  1008. virtual VS::InstanceType get_base_type(RID p_rid) const;
  1009. virtual bool free(RID p_rid);
  1010. struct Frame {
  1011. RenderTarget *current_rt;
  1012. bool clear_request;
  1013. Color clear_request_color;
  1014. float time[4];
  1015. float delta;
  1016. uint64_t count;
  1017. } frame;
  1018. struct GLWrapper {
  1019. mutable BitFieldDynamic texture_unit_table;
  1020. mutable LocalVector<uint32_t> texture_units_bound;
  1021. void gl_active_texture(GLenum p_texture) const {
  1022. ::glActiveTexture(p_texture);
  1023. p_texture -= GL_TEXTURE0;
  1024. // Check for below zero and above max in one check.
  1025. ERR_FAIL_COND((unsigned int)p_texture >= texture_unit_table.get_num_bits());
  1026. // Set if the first occurrence in the table.
  1027. if (texture_unit_table.check_and_set(p_texture)) {
  1028. texture_units_bound.push_back(p_texture);
  1029. }
  1030. }
  1031. void initialize(int p_max_texture_image_units);
  1032. void reset();
  1033. } gl_wrapper;
  1034. void initialize();
  1035. void finalize();
  1036. void _copy_screen();
  1037. virtual bool has_os_feature(const String &p_feature) const;
  1038. virtual void update_dirty_resources();
  1039. virtual void set_debug_generate_wireframes(bool p_generate);
  1040. virtual void render_info_begin_capture();
  1041. virtual void render_info_end_capture();
  1042. virtual int get_captured_render_info(VS::RenderInfo p_info);
  1043. virtual uint64_t get_render_info(VS::RenderInfo p_info);
  1044. virtual String get_video_adapter_name() const;
  1045. virtual String get_video_adapter_vendor() const;
  1046. static int32_t safe_gl_get_integer(unsigned int p_gl_param_name, int32_t p_max_accepted = INT32_MAX);
  1047. // NOTE : THESE SIZES ARE IN BYTES. BUFFER SIZES MAY NOT BE SPECIFIED IN BYTES SO REMEMBER TO CONVERT THEM WHEN CALLING.
  1048. void buffer_orphan_and_upload(unsigned int p_buffer_size_bytes, unsigned int p_offset_bytes, unsigned int p_data_size_bytes, const void *p_data, GLenum p_target = GL_ARRAY_BUFFER, GLenum p_usage = GL_DYNAMIC_DRAW, bool p_optional_orphan = false) const;
  1049. bool safe_buffer_sub_data(unsigned int p_total_buffer_size, GLenum p_target, unsigned int p_offset, unsigned int p_data_size, const void *p_data, unsigned int &r_offset_after) const;
  1050. RasterizerStorageGLES2();
  1051. };
  1052. inline bool RasterizerStorageGLES2::safe_buffer_sub_data(unsigned int p_total_buffer_size, GLenum p_target, unsigned int p_offset, unsigned int p_data_size, const void *p_data, unsigned int &r_offset_after) const {
  1053. r_offset_after = p_offset + p_data_size;
  1054. #ifdef DEBUG_ENABLED
  1055. // we are trying to write across the edge of the buffer
  1056. if (r_offset_after > p_total_buffer_size) {
  1057. return false;
  1058. }
  1059. #endif
  1060. glBufferSubData(p_target, p_offset, p_data_size, p_data);
  1061. return true;
  1062. }
  1063. // standardize the orphan / upload in one place so it can be changed per platform as necessary, and avoid future
  1064. // bugs causing pipeline stalls
  1065. // NOTE : THESE SIZES ARE IN BYTES. BUFFER SIZES MAY NOT BE SPECIFIED IN BYTES SO REMEMBER TO CONVERT THEM WHEN CALLING.
  1066. inline void RasterizerStorageGLES2::buffer_orphan_and_upload(unsigned int p_buffer_size_bytes, unsigned int p_offset_bytes, unsigned int p_data_size_bytes, const void *p_data, GLenum p_target, GLenum p_usage, bool p_optional_orphan) const {
  1067. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  1068. // Was previously #ifndef GLES_OVER_GL however this causes stalls on desktop mac also (and possibly other)
  1069. if (!p_optional_orphan || (config.should_orphan)) {
  1070. glBufferData(p_target, p_buffer_size_bytes, nullptr, p_usage);
  1071. #ifdef RASTERIZER_EXTRA_CHECKS
  1072. // fill with garbage off the end of the array
  1073. if (p_buffer_size_bytes) {
  1074. unsigned int start = p_offset_bytes + p_data_size_bytes;
  1075. unsigned int end = start + 1024;
  1076. if (end < p_buffer_size_bytes) {
  1077. uint8_t *garbage = (uint8_t *)alloca(1024);
  1078. for (int n = 0; n < 1024; n++) {
  1079. garbage[n] = Math::random(0, 255);
  1080. }
  1081. glBufferSubData(p_target, start, 1024, garbage);
  1082. }
  1083. }
  1084. #endif
  1085. }
  1086. ERR_FAIL_COND((p_offset_bytes + p_data_size_bytes) > p_buffer_size_bytes);
  1087. glBufferSubData(p_target, p_offset_bytes, p_data_size_bytes, p_data);
  1088. }
  1089. #endif // RASTERIZER_STORAGE_GLES2_H