rasterizer_storage_gles2.h 42 KB

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