rasterizer_gles2.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  1. /*************************************************************************/
  2. /* rasterizer_gles2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 RASTERIZER_GLES2_H
  31. #define RASTERIZER_GLES2_H
  32. #include "servers/visual/rasterizer.h"
  33. #define MAX_POLYGON_VERTICES 4096 //used for WebGL canvas_draw_polygon call.
  34. #ifdef GLES2_ENABLED
  35. #include "camera_matrix.h"
  36. #include "image.h"
  37. #include "list.h"
  38. #include "map.h"
  39. #include "rid.h"
  40. #include "self_list.h"
  41. #include "servers/visual_server.h"
  42. #include "sort.h"
  43. #include "platform_config.h"
  44. #ifndef GLES2_INCLUDE_H
  45. #include <GLES2/gl2.h>
  46. #else
  47. #include GLES2_INCLUDE_H
  48. #endif
  49. #include "drivers/gles2/shader_compiler_gles2.h"
  50. #include "drivers/gles2/shaders/blur.glsl.gen.h"
  51. #include "drivers/gles2/shaders/canvas.glsl.gen.h"
  52. #include "drivers/gles2/shaders/canvas_shadow.glsl.gen.h"
  53. #include "drivers/gles2/shaders/copy.glsl.gen.h"
  54. #include "drivers/gles2/shaders/material.glsl.gen.h"
  55. #include "servers/visual/particle_system_sw.h"
  56. /**
  57. @author Juan Linietsky <reduzio@gmail.com>
  58. */
  59. class RasterizerGLES2 : public Rasterizer {
  60. enum {
  61. MAX_SCENE_LIGHTS = 2048,
  62. LIGHT_SPOT_BIT = 0x80,
  63. DEFAULT_SKINNED_BUFFER_SIZE = 2048, // 10k vertices
  64. MAX_HW_LIGHTS = 1,
  65. };
  66. uint8_t *skinned_buffer;
  67. int skinned_buffer_size;
  68. bool pvr_supported;
  69. bool pvr_srgb_supported;
  70. bool s3tc_supported;
  71. bool s3tc_srgb_supported;
  72. bool latc_supported;
  73. bool etc_supported;
  74. bool atitc_supported;
  75. bool npo2_textures_available;
  76. bool read_depth_supported;
  77. bool use_framebuffers;
  78. bool full_float_fb_supported;
  79. bool use_shadow_mapping;
  80. bool use_fp16_fb;
  81. bool srgb_supported;
  82. bool float_supported;
  83. bool float_linear_supported;
  84. bool use_16bits_fbo;
  85. ShadowFilterTechnique shadow_filter;
  86. bool use_shadow_esm;
  87. bool use_shadow_pcf;
  88. bool use_hw_skeleton_xform;
  89. bool use_depth24;
  90. bool use_texture_instancing;
  91. bool use_attribute_instancing;
  92. bool use_rgba_shadowmaps;
  93. bool use_anisotropic_filter;
  94. float anisotropic_level;
  95. bool use_half_float;
  96. bool low_memory_2d;
  97. bool shrink_textures_x2;
  98. Vector<float> skel_default;
  99. Image _get_gl_image_and_format(const Image &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, int &r_gl_components, bool &r_has_alpha_cache, bool &r_compressed);
  100. struct RenderTarget;
  101. struct Texture {
  102. String path;
  103. uint32_t flags;
  104. int width, height;
  105. int alloc_width, alloc_height;
  106. Image::Format format;
  107. GLenum target;
  108. GLenum gl_format_cache;
  109. GLenum gl_internal_format_cache;
  110. int gl_components_cache;
  111. int data_size; //original data size, useful for retrieving back
  112. bool has_alpha;
  113. bool format_has_alpha;
  114. bool compressed;
  115. bool disallow_mipmaps;
  116. int total_data_size;
  117. bool ignore_mipmaps;
  118. ObjectID reloader;
  119. StringName reloader_func;
  120. Image image[6];
  121. int mipmaps;
  122. bool active;
  123. GLuint tex_id;
  124. RenderTarget *render_target;
  125. Texture() {
  126. ignore_mipmaps = false;
  127. render_target = NULL;
  128. flags = width = height = 0;
  129. tex_id = 0;
  130. data_size = 0;
  131. format = Image::FORMAT_GRAYSCALE;
  132. gl_components_cache = 0;
  133. format_has_alpha = false;
  134. has_alpha = false;
  135. active = false;
  136. disallow_mipmaps = false;
  137. compressed = false;
  138. total_data_size = 0;
  139. target = GL_TEXTURE_2D;
  140. mipmaps = 0;
  141. reloader = 0;
  142. }
  143. ~Texture() {
  144. if (tex_id != 0) {
  145. glDeleteTextures(1, &tex_id);
  146. }
  147. }
  148. };
  149. mutable RID_Owner<Texture> texture_owner;
  150. struct Shader {
  151. String vertex_code;
  152. String fragment_code;
  153. String light_code;
  154. int vertex_line;
  155. int fragment_line;
  156. int light_line;
  157. VS::ShaderMode mode;
  158. uint32_t custom_code_id;
  159. uint32_t version;
  160. bool valid;
  161. bool has_alpha;
  162. bool can_zpass;
  163. bool has_texscreen;
  164. bool has_screen_uv;
  165. bool writes_vertex;
  166. bool uses_discard;
  167. bool uses_time;
  168. bool uses_normal;
  169. bool uses_texpixel_size;
  170. Map<StringName, ShaderLanguage::Uniform> uniforms;
  171. StringName first_texture;
  172. Map<StringName, RID> default_textures;
  173. SelfList<Shader> dirty_list;
  174. Shader() :
  175. dirty_list(this) {
  176. valid = false;
  177. custom_code_id = 0;
  178. has_alpha = false;
  179. version = 1;
  180. vertex_line = 0;
  181. fragment_line = 0;
  182. light_line = 0;
  183. can_zpass = true;
  184. has_texscreen = false;
  185. has_screen_uv = false;
  186. writes_vertex = false;
  187. uses_discard = false;
  188. uses_time = false;
  189. uses_normal = false;
  190. }
  191. };
  192. mutable RID_Owner<Shader> shader_owner;
  193. mutable SelfList<Shader>::List _shader_dirty_list;
  194. _FORCE_INLINE_ void _shader_make_dirty(Shader *p_shader);
  195. void _update_shader(Shader *p_shader) const;
  196. struct Material {
  197. bool flags[VS::MATERIAL_FLAG_MAX];
  198. VS::MaterialBlendMode blend_mode;
  199. VS::MaterialDepthDrawMode depth_draw_mode;
  200. float line_width;
  201. bool has_alpha;
  202. mutable uint32_t shader_version;
  203. RID shader; // shader material
  204. Shader *shader_cache;
  205. struct UniformData {
  206. bool inuse;
  207. bool istexture;
  208. Variant value;
  209. int index;
  210. };
  211. mutable Map<StringName, UniformData> shader_params;
  212. uint64_t last_pass;
  213. Material() {
  214. for (int i = 0; i < VS::MATERIAL_FLAG_MAX; i++)
  215. flags[i] = false;
  216. flags[VS::MATERIAL_FLAG_VISIBLE] = true;
  217. line_width = 1;
  218. has_alpha = false;
  219. depth_draw_mode = VS::MATERIAL_DEPTH_DRAW_OPAQUE_ONLY;
  220. blend_mode = VS::MATERIAL_BLEND_MODE_MIX;
  221. last_pass = 0;
  222. shader_version = 0;
  223. shader_cache = NULL;
  224. }
  225. };
  226. _FORCE_INLINE_ void _update_material_shader_params(Material *p_material) const;
  227. mutable RID_Owner<Material> material_owner;
  228. struct Geometry {
  229. enum Type {
  230. GEOMETRY_INVALID,
  231. GEOMETRY_SURFACE,
  232. GEOMETRY_IMMEDIATE,
  233. GEOMETRY_PARTICLES,
  234. GEOMETRY_MULTISURFACE,
  235. };
  236. Type type;
  237. RID material;
  238. bool has_alpha;
  239. bool material_owned;
  240. Geometry() {
  241. has_alpha = false;
  242. material_owned = false;
  243. }
  244. virtual ~Geometry(){};
  245. };
  246. struct GeometryOwner {
  247. virtual ~GeometryOwner() {}
  248. };
  249. struct Mesh;
  250. struct Surface : public Geometry {
  251. struct ArrayData {
  252. uint32_t ofs, size, datatype, count;
  253. bool normalize;
  254. bool bind;
  255. ArrayData() {
  256. ofs = 0;
  257. size = 0;
  258. count = 0;
  259. datatype = 0;
  260. normalize = 0;
  261. bind = false;
  262. }
  263. };
  264. Mesh *mesh;
  265. Array data;
  266. Array morph_data;
  267. ArrayData array[VS::ARRAY_MAX];
  268. // support for vertex array objects
  269. GLuint array_object_id;
  270. // support for vertex buffer object
  271. GLuint vertex_id; // 0 means, unconfigured
  272. GLuint index_id; // 0 means, unconfigured
  273. // no support for the above, array in localmem.
  274. uint8_t *array_local;
  275. uint8_t *index_array_local;
  276. Vector<AABB> skeleton_bone_aabb;
  277. Vector<bool> skeleton_bone_used;
  278. //bool packed;
  279. struct MorphTarget {
  280. uint32_t configured_format;
  281. uint8_t *array;
  282. };
  283. MorphTarget *morph_targets_local;
  284. int morph_target_count;
  285. AABB aabb;
  286. int array_len;
  287. int index_array_len;
  288. int max_bone;
  289. float vertex_scale;
  290. float uv_scale;
  291. float uv2_scale;
  292. bool alpha_sort;
  293. VS::PrimitiveType primitive;
  294. uint32_t format;
  295. uint32_t configured_format;
  296. int stride;
  297. int local_stride;
  298. uint32_t morph_format;
  299. bool active;
  300. Point2 uv_min;
  301. Point2 uv_max;
  302. Surface() {
  303. array_len = 0;
  304. local_stride = 0;
  305. morph_format = 0;
  306. type = GEOMETRY_SURFACE;
  307. primitive = VS::PRIMITIVE_POINTS;
  308. index_array_len = 0;
  309. vertex_scale = 1.0;
  310. uv_scale = 1.0;
  311. uv2_scale = 1.0;
  312. alpha_sort = false;
  313. format = 0;
  314. stride = 0;
  315. morph_targets_local = 0;
  316. morph_target_count = 0;
  317. array_local = index_array_local = 0;
  318. vertex_id = index_id = 0;
  319. active = false;
  320. //packed=false;
  321. }
  322. ~Surface() {
  323. }
  324. };
  325. struct Mesh {
  326. bool active;
  327. Vector<Surface *> surfaces;
  328. int morph_target_count;
  329. VS::MorphTargetMode morph_target_mode;
  330. AABB custom_aabb;
  331. mutable uint64_t last_pass;
  332. Mesh() {
  333. morph_target_mode = VS::MORPH_MODE_NORMALIZED;
  334. morph_target_count = 0;
  335. last_pass = 0;
  336. active = false;
  337. }
  338. };
  339. mutable RID_Owner<Mesh> mesh_owner;
  340. Error _surface_set_arrays(Surface *p_surface, uint8_t *p_mem, uint8_t *p_index_mem, const Array &p_arrays, bool p_main);
  341. struct MultiMesh;
  342. struct MultiMeshSurface : public Geometry {
  343. Surface *surface;
  344. MultiMeshSurface() { type = GEOMETRY_MULTISURFACE; }
  345. };
  346. struct MultiMesh : public GeometryOwner {
  347. struct Element {
  348. float matrix[16];
  349. uint8_t color[4];
  350. Element() {
  351. matrix[0] = 1;
  352. matrix[1] = 0;
  353. matrix[2] = 0;
  354. matrix[3] = 0;
  355. matrix[4] = 0;
  356. matrix[5] = 1;
  357. matrix[6] = 0;
  358. matrix[7] = 0;
  359. matrix[8] = 0;
  360. matrix[9] = 0;
  361. matrix[10] = 1;
  362. matrix[11] = 0;
  363. matrix[12] = 0;
  364. matrix[13] = 0;
  365. matrix[14] = 0;
  366. matrix[15] = 1;
  367. };
  368. };
  369. AABB aabb;
  370. RID mesh;
  371. int visible;
  372. //IDirect3DVertexBuffer9* instance_buffer;
  373. Vector<Element> elements;
  374. Vector<MultiMeshSurface> cache_surfaces;
  375. mutable uint64_t last_pass;
  376. GLuint tex_id;
  377. int tw;
  378. int th;
  379. SelfList<MultiMesh> dirty_list;
  380. MultiMesh() :
  381. dirty_list(this) {
  382. tw = 1;
  383. th = 1;
  384. tex_id = 0;
  385. last_pass = 0;
  386. visible = -1;
  387. }
  388. };
  389. mutable RID_Owner<MultiMesh> multimesh_owner;
  390. mutable SelfList<MultiMesh>::List _multimesh_dirty_list;
  391. struct Immediate : public Geometry {
  392. struct Chunk {
  393. RID texture;
  394. VS::PrimitiveType primitive;
  395. Vector<Vector3> vertices;
  396. Vector<Vector3> normals;
  397. Vector<Plane> tangents;
  398. Vector<Color> colors;
  399. Vector<Vector2> uvs;
  400. Vector<Vector2> uvs2;
  401. };
  402. List<Chunk> chunks;
  403. bool building;
  404. int mask;
  405. AABB aabb;
  406. Immediate() {
  407. type = GEOMETRY_IMMEDIATE;
  408. building = false;
  409. }
  410. };
  411. mutable RID_Owner<Immediate> immediate_owner;
  412. struct Particles : public Geometry {
  413. ParticleSystemSW data; // software particle system
  414. Particles() {
  415. type = GEOMETRY_PARTICLES;
  416. }
  417. };
  418. mutable RID_Owner<Particles> particles_owner;
  419. struct ParticlesInstance : public GeometryOwner {
  420. RID particles;
  421. ParticleSystemProcessSW particles_process;
  422. Transform transform;
  423. ParticlesInstance() {}
  424. };
  425. mutable RID_Owner<ParticlesInstance> particles_instance_owner;
  426. ParticleSystemDrawInfoSW particle_draw_info;
  427. struct Skeleton {
  428. struct Bone {
  429. float mtx[4][4]; //used
  430. Bone() {
  431. for (int i = 0; i < 4; i++) {
  432. for (int j = 0; j < 4; j++) {
  433. mtx[i][j] = (i == j) ? 1 : 0;
  434. }
  435. }
  436. }
  437. _ALWAYS_INLINE_ void transform_add_mul3(const float *p_src, float *r_dst, float p_weight) const {
  438. r_dst[0] += ((mtx[0][0] * p_src[0]) + (mtx[1][0] * p_src[1]) + (mtx[2][0] * p_src[2]) + mtx[3][0]) * p_weight;
  439. r_dst[1] += ((mtx[0][1] * p_src[0]) + (mtx[1][1] * p_src[1]) + (mtx[2][1] * p_src[2]) + mtx[3][1]) * p_weight;
  440. r_dst[2] += ((mtx[0][2] * p_src[0]) + (mtx[1][2] * p_src[1]) + (mtx[2][2] * p_src[2]) + mtx[3][2]) * p_weight;
  441. }
  442. _ALWAYS_INLINE_ void transform3_add_mul3(const float *p_src, float *r_dst, float p_weight) const {
  443. r_dst[0] += ((mtx[0][0] * p_src[0]) + (mtx[1][0] * p_src[1]) + (mtx[2][0] * p_src[2])) * p_weight;
  444. r_dst[1] += ((mtx[0][1] * p_src[0]) + (mtx[1][1] * p_src[1]) + (mtx[2][1] * p_src[2])) * p_weight;
  445. r_dst[2] += ((mtx[0][2] * p_src[0]) + (mtx[1][2] * p_src[1]) + (mtx[2][2] * p_src[2])) * p_weight;
  446. }
  447. _ALWAYS_INLINE_ AABB transform_aabb(const AABB &p_aabb) const {
  448. float vertices[8][3] = {
  449. { p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z },
  450. { p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z },
  451. { p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z },
  452. { p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z },
  453. { p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z },
  454. { p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z },
  455. { p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z },
  456. { p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z }
  457. };
  458. AABB ret;
  459. for (int i = 0; i < 8; i++) {
  460. Vector3 xv(
  461. ((mtx[0][0] * vertices[i][0]) + (mtx[1][0] * vertices[i][1]) + (mtx[2][0] * vertices[i][2]) + mtx[3][0]),
  462. ((mtx[0][1] * vertices[i][0]) + (mtx[1][1] * vertices[i][1]) + (mtx[2][1] * vertices[i][2]) + mtx[3][1]),
  463. ((mtx[0][2] * vertices[i][0]) + (mtx[1][2] * vertices[i][1]) + (mtx[2][2] * vertices[i][2]) + mtx[3][2]));
  464. if (i == 0)
  465. ret.pos = xv;
  466. else
  467. ret.expand_to(xv);
  468. }
  469. return ret;
  470. }
  471. };
  472. GLuint tex_id;
  473. float pixel_size; //for texture
  474. Vector<Bone> bones;
  475. SelfList<Skeleton> dirty_list;
  476. Skeleton() :
  477. dirty_list(this) {
  478. tex_id = 0;
  479. pixel_size = 1.0;
  480. }
  481. };
  482. mutable RID_Owner<Skeleton> skeleton_owner;
  483. mutable SelfList<Skeleton>::List _skeleton_dirty_list;
  484. template <bool USE_NORMAL, bool USE_TANGENT, bool INPLACE>
  485. void _skeleton_xform(const uint8_t *p_src_array, int p_src_stride, uint8_t *p_dst_array, int p_dst_stride, int p_elements, const uint8_t *p_src_bones, const uint8_t *p_src_weights, const Skeleton::Bone *p_bone_xforms);
  486. struct Light {
  487. VS::LightType type;
  488. float vars[VS::LIGHT_PARAM_MAX];
  489. Color colors[3];
  490. bool shadow_enabled;
  491. RID projector;
  492. bool volumetric_enabled;
  493. Color volumetric_color;
  494. VS::LightOmniShadowMode omni_shadow_mode;
  495. VS::LightDirectionalShadowMode directional_shadow_mode;
  496. float directional_shadow_param[3];
  497. Light() {
  498. vars[VS::LIGHT_PARAM_SPOT_ATTENUATION] = 1;
  499. vars[VS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  500. vars[VS::LIGHT_PARAM_ATTENUATION] = 1.0;
  501. vars[VS::LIGHT_PARAM_ENERGY] = 1.0;
  502. vars[VS::LIGHT_PARAM_RADIUS] = 1.0;
  503. vars[VS::LIGHT_PARAM_SHADOW_DARKENING] = 0.0;
  504. vars[VS::LIGHT_PARAM_SHADOW_Z_OFFSET] = 0.2;
  505. vars[VS::LIGHT_PARAM_SHADOW_Z_SLOPE_SCALE] = 1.4;
  506. vars[VS::LIGHT_PARAM_SHADOW_ESM_MULTIPLIER] = 60.0;
  507. vars[VS::LIGHT_PARAM_SHADOW_BLUR_PASSES] = 1;
  508. colors[VS::LIGHT_COLOR_DIFFUSE] = Color(1, 1, 1);
  509. colors[VS::LIGHT_COLOR_SPECULAR] = Color(1, 1, 1);
  510. shadow_enabled = false;
  511. volumetric_enabled = false;
  512. directional_shadow_param[VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_SPLIT_WEIGHT] = 0.5;
  513. directional_shadow_param[VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_MAX_DISTANCE] = 0;
  514. directional_shadow_param[VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_ZOFFSET_SCALE] = 2.0;
  515. omni_shadow_mode = VS::LIGHT_OMNI_SHADOW_DEFAULT;
  516. directional_shadow_mode = VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  517. }
  518. };
  519. struct Environment {
  520. VS::EnvironmentBG bg_mode;
  521. Variant bg_param[VS::ENV_BG_PARAM_MAX];
  522. bool fx_enabled[VS::ENV_FX_MAX];
  523. Variant fx_param[VS::ENV_FX_PARAM_MAX];
  524. Environment() {
  525. bg_mode = VS::ENV_BG_DEFAULT_COLOR;
  526. bg_param[VS::ENV_BG_PARAM_COLOR] = Color(0, 0, 0);
  527. bg_param[VS::ENV_BG_PARAM_TEXTURE] = RID();
  528. bg_param[VS::ENV_BG_PARAM_CUBEMAP] = RID();
  529. bg_param[VS::ENV_BG_PARAM_ENERGY] = 1.0;
  530. bg_param[VS::ENV_BG_PARAM_SCALE] = 1.0;
  531. bg_param[VS::ENV_BG_PARAM_GLOW] = 0.0;
  532. bg_param[VS::ENV_BG_PARAM_CANVAS_MAX_LAYER] = 0;
  533. for (int i = 0; i < VS::ENV_FX_MAX; i++)
  534. fx_enabled[i] = false;
  535. fx_param[VS::ENV_FX_PARAM_GLOW_BLUR_PASSES] = 1;
  536. fx_param[VS::ENV_FX_PARAM_GLOW_BLUR_SCALE] = 1.0;
  537. fx_param[VS::ENV_FX_PARAM_GLOW_BLUR_STRENGTH] = 1.0;
  538. fx_param[VS::ENV_FX_PARAM_GLOW_BLUR_BLEND_MODE] = 0;
  539. fx_param[VS::ENV_FX_PARAM_GLOW_BLOOM] = 0.0;
  540. fx_param[VS::ENV_FX_PARAM_GLOW_BLOOM_TRESHOLD] = 0.5;
  541. fx_param[VS::ENV_FX_PARAM_DOF_BLUR_PASSES] = 1;
  542. fx_param[VS::ENV_FX_PARAM_DOF_BLUR_BEGIN] = 100.0;
  543. fx_param[VS::ENV_FX_PARAM_DOF_BLUR_RANGE] = 10.0;
  544. fx_param[VS::ENV_FX_PARAM_HDR_TONEMAPPER] = VS::ENV_FX_HDR_TONE_MAPPER_LINEAR;
  545. fx_param[VS::ENV_FX_PARAM_HDR_EXPOSURE] = 0.4;
  546. fx_param[VS::ENV_FX_PARAM_HDR_WHITE] = 1.0;
  547. fx_param[VS::ENV_FX_PARAM_HDR_GLOW_TRESHOLD] = 0.95;
  548. fx_param[VS::ENV_FX_PARAM_HDR_GLOW_SCALE] = 0.2;
  549. fx_param[VS::ENV_FX_PARAM_HDR_MIN_LUMINANCE] = 0.4;
  550. fx_param[VS::ENV_FX_PARAM_HDR_MAX_LUMINANCE] = 8.0;
  551. fx_param[VS::ENV_FX_PARAM_HDR_EXPOSURE_ADJUST_SPEED] = 0.5;
  552. fx_param[VS::ENV_FX_PARAM_FOG_BEGIN] = 100.0;
  553. fx_param[VS::ENV_FX_PARAM_FOG_ATTENUATION] = 1.0;
  554. fx_param[VS::ENV_FX_PARAM_FOG_BEGIN_COLOR] = Color(0, 0, 0);
  555. fx_param[VS::ENV_FX_PARAM_FOG_END_COLOR] = Color(0, 0, 0);
  556. fx_param[VS::ENV_FX_PARAM_FOG_BG] = true;
  557. fx_param[VS::ENV_FX_PARAM_BCS_BRIGHTNESS] = 1.0;
  558. fx_param[VS::ENV_FX_PARAM_BCS_CONTRAST] = 1.0;
  559. fx_param[VS::ENV_FX_PARAM_BCS_SATURATION] = 1.0;
  560. }
  561. };
  562. mutable RID_Owner<Environment> environment_owner;
  563. struct SampledLight {
  564. int w, h;
  565. GLuint texture;
  566. float multiplier;
  567. bool is_float;
  568. };
  569. mutable RID_Owner<SampledLight> sampled_light_owner;
  570. struct ViewportData {
  571. //1x1 fbo+texture for storing previous HDR value
  572. GLuint lum_fbo;
  573. GLuint lum_color;
  574. ViewportData() {
  575. lum_fbo = 0;
  576. lum_color = 0;
  577. }
  578. };
  579. mutable RID_Owner<ViewportData> viewport_data_owner;
  580. struct RenderTarget {
  581. Texture *texture_ptr;
  582. RID texture;
  583. GLuint fbo;
  584. GLuint color;
  585. GLuint depth;
  586. int width, height;
  587. uint64_t last_pass;
  588. };
  589. mutable RID_Owner<RenderTarget> render_target_owner;
  590. struct ShadowBuffer;
  591. struct LightInstance {
  592. struct SplitInfo {
  593. CameraMatrix camera;
  594. Transform transform;
  595. float near;
  596. float far;
  597. };
  598. RID light;
  599. Light *base;
  600. Transform transform;
  601. CameraMatrix projection;
  602. Transform custom_transform[4];
  603. CameraMatrix custom_projection[4];
  604. Vector3 light_vector;
  605. Vector3 spot_vector;
  606. float linear_att;
  607. uint64_t shadow_pass;
  608. uint64_t last_pass;
  609. uint16_t sort_key;
  610. Vector2 dp;
  611. CameraMatrix shadow_projection[4];
  612. float shadow_split[4];
  613. ShadowBuffer *near_shadow_buffer;
  614. void clear_shadow_buffers() {
  615. clear_near_shadow_buffers();
  616. }
  617. void clear_near_shadow_buffers() {
  618. if (near_shadow_buffer) {
  619. near_shadow_buffer->owner = NULL;
  620. near_shadow_buffer = NULL;
  621. }
  622. }
  623. LightInstance() {
  624. shadow_pass = 0;
  625. last_pass = 0;
  626. sort_key = 0;
  627. near_shadow_buffer = NULL;
  628. }
  629. };
  630. mutable RID_Owner<Light> light_owner;
  631. mutable RID_Owner<LightInstance> light_instance_owner;
  632. LightInstance *light_instances[MAX_SCENE_LIGHTS];
  633. LightInstance *directional_lights[4];
  634. int light_instance_count;
  635. int directional_light_count;
  636. int last_light_id;
  637. bool current_depth_test;
  638. bool current_depth_mask;
  639. VS::MaterialBlendMode current_blend_mode;
  640. bool use_fast_texture_filter;
  641. int max_texture_size;
  642. bool fragment_lighting;
  643. RID shadow_material;
  644. RID shadow_material_double_sided;
  645. Material *shadow_mat_ptr;
  646. Material *shadow_mat_double_sided_ptr;
  647. int max_texture_units;
  648. GLuint base_framebuffer;
  649. GLuint gui_quad_buffer;
  650. GLuint indices_buffer;
  651. struct RenderList {
  652. enum {
  653. DEFAULT_MAX_ELEMENTS = 4096,
  654. MAX_LIGHTS = 4,
  655. SORT_FLAG_SKELETON = 1,
  656. SORT_FLAG_INSTANCING = 2,
  657. };
  658. static int max_elements;
  659. struct Element {
  660. float depth;
  661. const InstanceData *instance;
  662. const Skeleton *skeleton;
  663. const Geometry *geometry;
  664. const Geometry *geometry_cmp;
  665. const Material *material;
  666. const GeometryOwner *owner;
  667. bool *additive_ptr;
  668. bool additive;
  669. bool mirror;
  670. union {
  671. #ifdef BIG_ENDIAN_ENABLED
  672. struct {
  673. uint8_t sort_flags;
  674. uint8_t light_type;
  675. uint16_t light;
  676. };
  677. #else
  678. struct {
  679. uint16_t light;
  680. uint8_t light_type;
  681. uint8_t sort_flags;
  682. };
  683. #endif
  684. uint32_t sort_key;
  685. };
  686. };
  687. Element *_elements;
  688. Element **elements;
  689. int element_count;
  690. void clear() {
  691. element_count = 0;
  692. }
  693. struct SortZ {
  694. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  695. return A->depth > B->depth;
  696. }
  697. };
  698. void sort_z() {
  699. SortArray<Element *, SortZ> sorter;
  700. sorter.sort(elements, element_count);
  701. }
  702. struct SortMatGeom {
  703. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  704. // TODO move to a single uint64 (one comparison)
  705. if (A->material->shader_cache == B->material->shader_cache) {
  706. if (A->material == B->material) {
  707. return A->geometry_cmp < B->geometry_cmp;
  708. } else {
  709. return (A->material < B->material);
  710. }
  711. } else {
  712. return A->material->shader_cache < B->material->shader_cache;
  713. }
  714. }
  715. };
  716. void sort_mat_geom() {
  717. SortArray<Element *, SortMatGeom> sorter;
  718. sorter.sort(elements, element_count);
  719. }
  720. struct SortMatLight {
  721. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  722. if (A->geometry_cmp == B->geometry_cmp) {
  723. if (A->material == B->material) {
  724. return A->light < B->light;
  725. } else {
  726. return (A->material < B->material);
  727. }
  728. } else {
  729. return (A->geometry_cmp < B->geometry_cmp);
  730. }
  731. }
  732. };
  733. void sort_mat_light() {
  734. SortArray<Element *, SortMatLight> sorter;
  735. sorter.sort(elements, element_count);
  736. }
  737. struct SortMatLightType {
  738. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  739. if (A->light_type == B->light_type) {
  740. if (A->material->shader_cache == B->material->shader_cache) {
  741. if (A->material == B->material) {
  742. return (A->geometry_cmp < B->geometry_cmp);
  743. } else {
  744. return (A->material < B->material);
  745. }
  746. } else {
  747. return (A->material->shader_cache < B->material->shader_cache);
  748. }
  749. } else {
  750. return A->light_type < B->light_type;
  751. }
  752. }
  753. };
  754. void sort_mat_light_type() {
  755. SortArray<Element *, SortMatLightType> sorter;
  756. sorter.sort(elements, element_count);
  757. }
  758. struct SortMatLightTypeFlags {
  759. _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
  760. if (A->sort_key == B->sort_key) {
  761. if (A->material->shader_cache == B->material->shader_cache) {
  762. if (A->material == B->material) {
  763. return (A->geometry_cmp < B->geometry_cmp);
  764. } else {
  765. return (A->material < B->material);
  766. }
  767. } else {
  768. return (A->material->shader_cache < B->material->shader_cache);
  769. }
  770. } else {
  771. return A->sort_key < B->sort_key; //one is null and one is not
  772. }
  773. }
  774. };
  775. void sort_mat_light_type_flags() {
  776. SortArray<Element *, SortMatLightTypeFlags> sorter;
  777. sorter.sort(elements, element_count);
  778. }
  779. _FORCE_INLINE_ Element *add_element() {
  780. if (element_count >= max_elements)
  781. return NULL;
  782. elements[element_count] = &_elements[element_count];
  783. return elements[element_count++];
  784. }
  785. void init() {
  786. element_count = 0;
  787. elements = memnew_arr(Element *, max_elements);
  788. _elements = memnew_arr(Element, max_elements);
  789. for (int i = 0; i < max_elements; i++)
  790. elements[i] = &_elements[i]; // assign elements
  791. }
  792. RenderList() {
  793. }
  794. ~RenderList() {
  795. memdelete_arr(elements);
  796. memdelete_arr(_elements);
  797. }
  798. };
  799. RenderList opaque_render_list;
  800. RenderList alpha_render_list;
  801. RID default_material;
  802. CameraMatrix camera_projection;
  803. Transform camera_transform;
  804. Transform camera_transform_inverse;
  805. float camera_z_near;
  806. float camera_z_far;
  807. Size2 camera_vp_size;
  808. bool camera_ortho;
  809. Set<String> extensions;
  810. bool texscreen_copied;
  811. bool texscreen_used;
  812. Plane camera_plane;
  813. void _add_geometry(const Geometry *p_geometry, const InstanceData *p_instance, const Geometry *p_geometry_cmp, const GeometryOwner *p_owner, int p_material = -1);
  814. void _render_list_forward(RenderList *p_render_list, const Transform &p_view_transform, const Transform &p_view_transform_inverse, const CameraMatrix &p_projection, bool p_reverse_cull = false, bool p_fragment_light = false, bool p_alpha_pass = false);
  815. //void _setup_light(LightInstance* p_instance, int p_idx);
  816. void _setup_light(uint16_t p_light);
  817. _FORCE_INLINE_ void _setup_shader_params(const Material *p_material);
  818. bool _setup_material(const Geometry *p_geometry, const Material *p_material, bool p_no_const_light, bool p_opaque_pass);
  819. void _setup_skeleton(const Skeleton *p_skeleton);
  820. Error _setup_geometry(const Geometry *p_geometry, const Material *p_material, const Skeleton *p_skeleton, const float *p_morphs);
  821. void _render(const Geometry *p_geometry, const Material *p_material, const Skeleton *p_skeleton, const GeometryOwner *p_owner, const Transform &p_xform);
  822. /***********/
  823. /* SHADOWS */
  824. /***********/
  825. struct ShadowBuffer {
  826. int size;
  827. GLuint fbo;
  828. GLuint rbo;
  829. GLuint depth;
  830. GLuint rgba; //for older devices
  831. #if 0
  832. GLuint fbo_blur;
  833. GLuint rbo_blur;
  834. GLuint blur;
  835. #endif
  836. LightInstance *owner;
  837. bool init(int p_size, bool p_use_depth);
  838. ShadowBuffer() {
  839. size = 0;
  840. depth = 0;
  841. owner = NULL;
  842. }
  843. };
  844. Vector<ShadowBuffer> near_shadow_buffers;
  845. ShadowBuffer blur_shadow_buffer;
  846. Vector<ShadowBuffer> far_shadow_buffers;
  847. LightInstance *shadow;
  848. int shadow_pass;
  849. float shadow_near_far_split_size_ratio;
  850. bool _allocate_shadow_buffers(LightInstance *p_instance, Vector<ShadowBuffer> &p_buffers);
  851. void _debug_draw_shadow(GLuint tex, const Rect2 &p_rect);
  852. void _debug_draw_shadows_type(Vector<ShadowBuffer> &p_shadows, Point2 &ofs);
  853. void _debug_shadows();
  854. void _debug_luminances();
  855. void _debug_samplers();
  856. /***********/
  857. /* FBOs */
  858. /***********/
  859. struct FrameBuffer {
  860. GLuint fbo;
  861. GLuint color;
  862. GLuint depth;
  863. int width, height;
  864. int scale;
  865. bool active;
  866. int blur_size;
  867. struct Blur {
  868. GLuint fbo;
  869. GLuint color;
  870. Blur() {
  871. fbo = 0;
  872. color = 0;
  873. }
  874. } blur[3];
  875. struct Luminance {
  876. int size;
  877. GLuint fbo;
  878. GLuint color;
  879. Luminance() {
  880. fbo = 0;
  881. color = 0;
  882. size = 0;
  883. }
  884. };
  885. Vector<Luminance> luminance;
  886. GLuint sample_fbo;
  887. GLuint sample_color;
  888. FrameBuffer() {
  889. blur_size = 0;
  890. }
  891. } framebuffer;
  892. void _update_framebuffer();
  893. void _process_glow_and_bloom();
  894. //void _update_blur_buffer();
  895. /*********/
  896. /* FRAME */
  897. /*********/
  898. struct _Rinfo {
  899. int texture_mem;
  900. int vertex_count;
  901. int object_count;
  902. int mat_change_count;
  903. int surface_count;
  904. int shader_change_count;
  905. int ci_draw_commands;
  906. int draw_calls;
  907. } _rinfo;
  908. /*******************/
  909. /* CANVAS OCCLUDER */
  910. /*******************/
  911. struct CanvasOccluder {
  912. GLuint vertex_id; // 0 means, unconfigured
  913. GLuint index_id; // 0 means, unconfigured
  914. DVector<Vector2> lines;
  915. int len;
  916. };
  917. RID_Owner<CanvasOccluder> canvas_occluder_owner;
  918. /***********************/
  919. /* CANVAS LIGHT SHADOW */
  920. /***********************/
  921. struct CanvasLightShadow {
  922. int size;
  923. int height;
  924. GLuint fbo;
  925. GLuint rbo;
  926. GLuint depth;
  927. GLuint rgba; //for older devices
  928. GLuint blur;
  929. };
  930. RID_Owner<CanvasLightShadow> canvas_light_shadow_owner;
  931. RID canvas_shadow_blur;
  932. /* ETC */
  933. RenderTarget *current_rt;
  934. bool current_rt_transparent;
  935. bool current_rt_vflip;
  936. ViewportData *current_vd;
  937. GLuint white_tex;
  938. RID canvas_tex;
  939. float canvas_opacity;
  940. Color canvas_modulate;
  941. bool canvas_use_modulate;
  942. bool uses_texpixel_size;
  943. bool rebind_texpixel_size;
  944. Transform canvas_transform;
  945. CanvasItemMaterial *canvas_last_material;
  946. bool canvas_texscreen_used;
  947. Vector2 normal_flip;
  948. _FORCE_INLINE_ void _canvas_normal_set_flip(const Vector2 &p_flip);
  949. _FORCE_INLINE_ Texture *_bind_canvas_texture(const RID &p_texture);
  950. VS::MaterialBlendMode canvas_blend_mode;
  951. int _setup_geometry_vinfo;
  952. bool pack_arrays;
  953. bool keep_copies;
  954. bool use_reload_hooks;
  955. bool cull_front;
  956. bool lights_use_shadow;
  957. _FORCE_INLINE_ void _set_cull(bool p_front, bool p_reverse_cull = false);
  958. _FORCE_INLINE_ Color _convert_color(const Color &p_color);
  959. void _process_glow_bloom();
  960. void _process_hdr();
  961. void _draw_tex_bg();
  962. bool using_canvas_bg;
  963. Size2 window_size;
  964. VS::ViewportRect viewport;
  965. double last_time;
  966. double scaled_time;
  967. double time_delta;
  968. uint64_t frame;
  969. uint64_t scene_pass;
  970. bool draw_next_frame;
  971. Environment *current_env;
  972. VS::ScenarioDebugMode current_debug;
  973. RID overdraw_material;
  974. float shader_time_rollback;
  975. float time_scale;
  976. mutable MaterialShaderGLES2 material_shader;
  977. mutable CanvasShaderGLES2 canvas_shader;
  978. BlurShaderGLES2 blur_shader;
  979. CopyShaderGLES2 copy_shader;
  980. mutable CanvasShadowShaderGLES2 canvas_shadow_shader;
  981. mutable ShaderCompilerGLES2 shader_precompiler;
  982. void _draw_primitive(int p_points, const Vector3 *p_vertices, const Vector3 *p_normals, const Color *p_colors, const Vector3 *p_uvs, const Plane *p_tangents = NULL, int p_instanced = 1);
  983. _FORCE_INLINE_ void _draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs);
  984. _FORCE_INLINE_ void _draw_gui_primitive2(int p_points, const Vector2 *p_vertices, const Color *p_colors, const Vector2 *p_uvs, const Vector2 *p_uvs2);
  985. void _draw_textured_quad(const Rect2 &p_rect, const Rect2 &p_src_region, const Size2 &p_tex_size, bool p_h_flip = false, bool p_v_flip = false, bool p_transpose = false);
  986. void _draw_quad(const Rect2 &p_rect);
  987. void _copy_screen_quad();
  988. void _copy_to_texscreen();
  989. bool _test_depth_shadow_buffer();
  990. Vector3 chunk_vertex;
  991. Vector3 chunk_normal;
  992. Plane chunk_tangent;
  993. Color chunk_color;
  994. Vector2 chunk_uv;
  995. Vector2 chunk_uv2;
  996. GLuint tc0_id_cache;
  997. GLuint tc0_idx;
  998. template <bool use_normalmap>
  999. _FORCE_INLINE_ void _canvas_item_render_commands(CanvasItem *p_item, CanvasItem *current_clip, bool &reclip);
  1000. _FORCE_INLINE_ void _canvas_item_setup_shader_params(CanvasItemMaterial *material, Shader *p_shader);
  1001. _FORCE_INLINE_ void _canvas_item_setup_shader_uniforms(CanvasItemMaterial *material, Shader *p_shader);
  1002. public:
  1003. /* TEXTURE API */
  1004. virtual RID texture_create();
  1005. virtual void texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT);
  1006. virtual void texture_set_data(RID p_texture, const Image &p_image, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT);
  1007. virtual Image texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) const;
  1008. virtual void texture_set_flags(RID p_texture, uint32_t p_flags);
  1009. virtual uint32_t texture_get_flags(RID p_texture) const;
  1010. virtual Image::Format texture_get_format(RID p_texture) const;
  1011. virtual uint32_t texture_get_width(RID p_texture) const;
  1012. virtual uint32_t texture_get_height(RID p_texture) const;
  1013. virtual bool texture_has_alpha(RID p_texture) const;
  1014. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height);
  1015. virtual void texture_set_reload_hook(RID p_texture, ObjectID p_owner, const StringName &p_function) const;
  1016. virtual void texture_set_path(RID p_texture, const String &p_path);
  1017. virtual String texture_get_path(RID p_texture) const;
  1018. virtual void texture_debug_usage(List<VS::TextureInfo> *r_info);
  1019. virtual void texture_set_shrink_all_x2_on_set_data(bool p_enable);
  1020. GLuint _texture_get_name(RID p_tex);
  1021. /* SHADER API */
  1022. virtual RID shader_create(VS::ShaderMode p_mode = VS::SHADER_MATERIAL);
  1023. virtual void shader_set_mode(RID p_shader, VS::ShaderMode p_mode);
  1024. virtual VS::ShaderMode shader_get_mode(RID p_shader) const;
  1025. virtual void shader_set_code(RID p_shader, const String &p_vertex, const String &p_fragment, const String &p_light, int p_vertex_ofs = 0, int p_fragment_ofs = 0, int p_light_ofs = 0);
  1026. virtual String shader_get_fragment_code(RID p_shader) const;
  1027. virtual String shader_get_vertex_code(RID p_shader) const;
  1028. virtual String shader_get_light_code(RID p_shader) const;
  1029. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const;
  1030. virtual void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture);
  1031. virtual RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const;
  1032. virtual Variant shader_get_default_param(RID p_shader, const StringName &p_name);
  1033. /* COMMON MATERIAL API */
  1034. virtual RID material_create();
  1035. virtual void material_set_shader(RID p_shader_material, RID p_shader);
  1036. virtual RID material_get_shader(RID p_shader_material) const;
  1037. virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value);
  1038. virtual Variant material_get_param(RID p_material, const StringName &p_param) const;
  1039. virtual void material_set_flag(RID p_material, VS::MaterialFlag p_flag, bool p_enabled);
  1040. virtual bool material_get_flag(RID p_material, VS::MaterialFlag p_flag) const;
  1041. virtual void material_set_depth_draw_mode(RID p_material, VS::MaterialDepthDrawMode p_mode);
  1042. virtual VS::MaterialDepthDrawMode material_get_depth_draw_mode(RID p_material) const;
  1043. virtual void material_set_blend_mode(RID p_material, VS::MaterialBlendMode p_mode);
  1044. virtual VS::MaterialBlendMode material_get_blend_mode(RID p_material) const;
  1045. virtual void material_set_line_width(RID p_material, float p_line_width);
  1046. virtual float material_get_line_width(RID p_material) const;
  1047. /* MESH API */
  1048. virtual RID mesh_create();
  1049. virtual void mesh_add_surface(RID p_mesh, VS::PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), bool p_alpha_sort = false);
  1050. virtual Array mesh_get_surface_arrays(RID p_mesh, int p_surface) const;
  1051. virtual Array mesh_get_surface_morph_arrays(RID p_mesh, int p_surface) const;
  1052. virtual void mesh_add_custom_surface(RID p_mesh, const Variant &p_dat);
  1053. virtual void mesh_set_morph_target_count(RID p_mesh, int p_amount);
  1054. virtual int mesh_get_morph_target_count(RID p_mesh) const;
  1055. virtual void mesh_set_morph_target_mode(RID p_mesh, VS::MorphTargetMode p_mode);
  1056. virtual VS::MorphTargetMode mesh_get_morph_target_mode(RID p_mesh) const;
  1057. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material, bool p_owned = false);
  1058. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const;
  1059. virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const;
  1060. virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const;
  1061. virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const;
  1062. virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const;
  1063. virtual void mesh_remove_surface(RID p_mesh, int p_index);
  1064. virtual int mesh_get_surface_count(RID p_mesh) const;
  1065. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) const;
  1066. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb);
  1067. virtual AABB mesh_get_custom_aabb(RID p_mesh) const;
  1068. /* MULTIMESH API */
  1069. virtual RID multimesh_create();
  1070. virtual void multimesh_set_instance_count(RID p_multimesh, int p_count);
  1071. virtual int multimesh_get_instance_count(RID p_multimesh) const;
  1072. virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  1073. virtual void multimesh_set_aabb(RID p_multimesh, const AABB &p_aabb);
  1074. virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform);
  1075. virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  1076. virtual RID multimesh_get_mesh(RID p_multimesh) const;
  1077. virtual AABB multimesh_get_aabb(RID p_multimesh) const;
  1078. virtual Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  1079. virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  1080. virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  1081. virtual int multimesh_get_visible_instances(RID p_multimesh) const;
  1082. /* IMMEDIATE API */
  1083. virtual RID immediate_create();
  1084. virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture = RID());
  1085. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex);
  1086. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal);
  1087. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent);
  1088. virtual void immediate_color(RID p_immediate, const Color &p_color);
  1089. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv);
  1090. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv);
  1091. virtual void immediate_end(RID p_immediate);
  1092. virtual void immediate_clear(RID p_immediate);
  1093. virtual AABB immediate_get_aabb(RID p_immediate) const;
  1094. virtual void immediate_set_material(RID p_immediate, RID p_material);
  1095. virtual RID immediate_get_material(RID p_immediate) const;
  1096. /* PARTICLES API */
  1097. virtual RID particles_create();
  1098. virtual void particles_set_amount(RID p_particles, int p_amount);
  1099. virtual int particles_get_amount(RID p_particles) const;
  1100. virtual void particles_set_emitting(RID p_particles, bool p_emitting);
  1101. virtual bool particles_is_emitting(RID p_particles) const;
  1102. virtual void particles_set_visibility_aabb(RID p_particles, const AABB &p_visibility);
  1103. virtual AABB particles_get_visibility_aabb(RID p_particles) const;
  1104. virtual void particles_set_emission_half_extents(RID p_particles, const Vector3 &p_half_extents);
  1105. virtual Vector3 particles_get_emission_half_extents(RID p_particles) const;
  1106. virtual void particles_set_emission_base_velocity(RID p_particles, const Vector3 &p_base_velocity);
  1107. virtual Vector3 particles_get_emission_base_velocity(RID p_particles) const;
  1108. virtual void particles_set_emission_points(RID p_particles, const DVector<Vector3> &p_points);
  1109. virtual DVector<Vector3> particles_get_emission_points(RID p_particles) const;
  1110. virtual void particles_set_gravity_normal(RID p_particles, const Vector3 &p_normal);
  1111. virtual Vector3 particles_get_gravity_normal(RID p_particles) const;
  1112. virtual void particles_set_variable(RID p_particles, VS::ParticleVariable p_variable, float p_value);
  1113. virtual float particles_get_variable(RID p_particles, VS::ParticleVariable p_variable) const;
  1114. virtual void particles_set_randomness(RID p_particles, VS::ParticleVariable p_variable, float p_randomness);
  1115. virtual float particles_get_randomness(RID p_particles, VS::ParticleVariable p_variable) const;
  1116. virtual void particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos);
  1117. virtual float particles_get_color_phase_pos(RID p_particles, int p_phase) const;
  1118. virtual void particles_set_color_phases(RID p_particles, int p_phases);
  1119. virtual int particles_get_color_phases(RID p_particles) const;
  1120. virtual void particles_set_color_phase_color(RID p_particles, int p_phase, const Color &p_color);
  1121. virtual Color particles_get_color_phase_color(RID p_particles, int p_phase) const;
  1122. virtual void particles_set_attractors(RID p_particles, int p_attractors);
  1123. virtual int particles_get_attractors(RID p_particles) const;
  1124. virtual void particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3 &p_pos);
  1125. virtual Vector3 particles_get_attractor_pos(RID p_particles, int p_attractor) const;
  1126. virtual void particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force);
  1127. virtual float particles_get_attractor_strength(RID p_particles, int p_attractor) const;
  1128. virtual void particles_set_material(RID p_particles, RID p_material, bool p_owned = false);
  1129. virtual RID particles_get_material(RID p_particles) const;
  1130. virtual AABB particles_get_aabb(RID p_particles) const;
  1131. virtual void particles_set_height_from_velocity(RID p_particles, bool p_enable);
  1132. virtual bool particles_has_height_from_velocity(RID p_particles) const;
  1133. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable);
  1134. virtual bool particles_is_using_local_coordinates(RID p_particles) const;
  1135. /* SKELETON API */
  1136. virtual RID skeleton_create();
  1137. virtual void skeleton_resize(RID p_skeleton, int p_bones);
  1138. virtual int skeleton_get_bone_count(RID p_skeleton) const;
  1139. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform);
  1140. virtual Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone);
  1141. /* LIGHT API */
  1142. virtual RID light_create(VS::LightType p_type);
  1143. virtual VS::LightType light_get_type(RID p_light) const;
  1144. virtual void light_set_color(RID p_light, VS::LightColor p_type, const Color &p_color);
  1145. virtual Color light_get_color(RID p_light, VS::LightColor p_type) const;
  1146. virtual void light_set_shadow(RID p_light, bool p_enabled);
  1147. virtual bool light_has_shadow(RID p_light) const;
  1148. virtual void light_set_volumetric(RID p_light, bool p_enabled);
  1149. virtual bool light_is_volumetric(RID p_light) const;
  1150. virtual void light_set_projector(RID p_light, RID p_texture);
  1151. virtual RID light_get_projector(RID p_light) const;
  1152. virtual void light_set_var(RID p_light, VS::LightParam p_var, float p_value);
  1153. virtual float light_get_var(RID p_light, VS::LightParam p_var) const;
  1154. virtual void light_set_operator(RID p_light, VS::LightOp p_op);
  1155. virtual VS::LightOp light_get_operator(RID p_light) const;
  1156. virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode);
  1157. virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) const;
  1158. virtual void light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode);
  1159. virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) const;
  1160. virtual void light_directional_set_shadow_param(RID p_light, VS::LightDirectionalShadowParam p_param, float p_value);
  1161. virtual float light_directional_get_shadow_param(RID p_light, VS::LightDirectionalShadowParam p_param) const;
  1162. virtual AABB light_get_aabb(RID p_poly) const;
  1163. virtual RID light_instance_create(RID p_light);
  1164. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform);
  1165. virtual ShadowType light_instance_get_shadow_type(RID p_light_instance, bool p_far = false) const;
  1166. virtual int light_instance_get_shadow_passes(RID p_light_instance) const;
  1167. virtual bool light_instance_get_pssm_shadow_overlap(RID p_light_instance) const;
  1168. virtual void light_instance_set_shadow_transform(RID p_light_instance, int p_index, const CameraMatrix &p_camera, const Transform &p_transform, float p_split_near = 0, float p_split_far = 0);
  1169. virtual int light_instance_get_shadow_size(RID p_light_instance, int p_index = 0) const;
  1170. virtual void shadow_clear_near();
  1171. virtual bool shadow_allocate_near(RID p_light);
  1172. virtual bool shadow_allocate_far(RID p_light);
  1173. /* SHADOW */
  1174. virtual RID particles_instance_create(RID p_particles);
  1175. virtual void particles_instance_set_transform(RID p_particles_instance, const Transform &p_transform);
  1176. /* VIEWPORT */
  1177. virtual RID viewport_data_create();
  1178. virtual RID render_target_create();
  1179. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height);
  1180. virtual RID render_target_get_texture(RID p_render_target) const;
  1181. virtual bool render_target_renedered_in_frame(RID p_render_target);
  1182. /* RENDER API */
  1183. /* all calls (inside begin/end shadow) are always warranted to be in the following order: */
  1184. virtual void begin_frame();
  1185. virtual void set_viewport(const VS::ViewportRect &p_viewport);
  1186. virtual void set_render_target(RID p_render_target, bool p_transparent_bg = false, bool p_vflip = false);
  1187. virtual void clear_viewport(const Color &p_color);
  1188. virtual void capture_viewport(Image *r_capture);
  1189. virtual void begin_scene(RID p_viewport_data, RID p_env, VS::ScenarioDebugMode p_debug);
  1190. virtual void begin_shadow_map(RID p_light_instance, int p_shadow_pass);
  1191. virtual void set_camera(const Transform &p_world, const CameraMatrix &p_projection, bool p_ortho_hint);
  1192. virtual void add_light(RID p_light_instance); ///< all "add_light" calls happen before add_geometry calls
  1193. typedef Map<StringName, Variant> ParamOverrideMap;
  1194. virtual void add_mesh(const RID &p_mesh, const InstanceData *p_data);
  1195. virtual void add_multimesh(const RID &p_multimesh, const InstanceData *p_data);
  1196. virtual void add_immediate(const RID &p_immediate, const InstanceData *p_data);
  1197. virtual void add_particles(const RID &p_particle_instance, const InstanceData *p_data);
  1198. virtual void end_scene();
  1199. virtual void end_shadow_map();
  1200. virtual void end_frame();
  1201. /* CANVAS API */
  1202. virtual void begin_canvas_bg();
  1203. virtual void canvas_begin();
  1204. virtual void canvas_disable_blending();
  1205. virtual void canvas_set_opacity(float p_opacity);
  1206. virtual void canvas_set_blend_mode(VS::MaterialBlendMode p_mode);
  1207. virtual void canvas_begin_rect(const Matrix32 &p_transform);
  1208. virtual void canvas_set_clip(bool p_clip, const Rect2 &p_rect);
  1209. virtual void canvas_end_rect();
  1210. virtual void canvas_draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width);
  1211. virtual void canvas_draw_rect(const Rect2 &p_rect, int p_flags, const Rect2 &p_source, RID p_texture, const Color &p_modulate);
  1212. virtual void canvas_draw_style_box(const Rect2 &p_rect, const Rect2 &p_src_region, RID p_texture, const float *p_margins, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1));
  1213. virtual void canvas_draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width);
  1214. virtual void canvas_draw_polygon(int p_vertex_count, const int *p_indices, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, const RID &p_texture, bool p_singlecolor);
  1215. virtual void canvas_set_transform(const Matrix32 &p_transform);
  1216. virtual void canvas_render_items(CanvasItem *p_item_list, int p_z, const Color &p_modulate, CanvasLight *p_light);
  1217. virtual void canvas_debug_viewport_shadows(CanvasLight *p_lights_with_shadow);
  1218. /* CANVAS LIGHT SHADOW */
  1219. //buffer
  1220. virtual RID canvas_light_shadow_buffer_create(int p_width);
  1221. virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Matrix32 &p_light_xform, int p_light_mask, float p_near, float p_far, CanvasLightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache);
  1222. //occluder
  1223. virtual RID canvas_light_occluder_create();
  1224. virtual void canvas_light_occluder_set_polylines(RID p_occluder, const DVector<Vector2> &p_lines);
  1225. /* ENVIRONMENT */
  1226. virtual RID environment_create();
  1227. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg);
  1228. virtual VS::EnvironmentBG environment_get_background(RID p_env) const;
  1229. virtual void environment_set_background_param(RID p_env, VS::EnvironmentBGParam p_param, const Variant &p_value);
  1230. virtual Variant environment_get_background_param(RID p_env, VS::EnvironmentBGParam p_param) const;
  1231. virtual void environment_set_enable_fx(RID p_env, VS::EnvironmentFx p_effect, bool p_enabled);
  1232. virtual bool environment_is_fx_enabled(RID p_env, VS::EnvironmentFx p_effect) const;
  1233. virtual void environment_fx_set_param(RID p_env, VS::EnvironmentFxParam p_param, const Variant &p_value);
  1234. virtual Variant environment_fx_get_param(RID p_env, VS::EnvironmentFxParam p_param) const;
  1235. /* SAMPLED LIGHT */
  1236. virtual RID sampled_light_dp_create(int p_width, int p_height);
  1237. virtual void sampled_light_dp_update(RID p_sampled_light, const Color *p_data, float p_multiplier);
  1238. /*MISC*/
  1239. virtual bool is_texture(const RID &p_rid) const;
  1240. virtual bool is_material(const RID &p_rid) const;
  1241. virtual bool is_mesh(const RID &p_rid) const;
  1242. virtual bool is_immediate(const RID &p_rid) const;
  1243. virtual bool is_multimesh(const RID &p_rid) const;
  1244. virtual bool is_particles(const RID &p_beam) const;
  1245. virtual bool is_light(const RID &p_rid) const;
  1246. virtual bool is_light_instance(const RID &p_rid) const;
  1247. virtual bool is_particles_instance(const RID &p_rid) const;
  1248. virtual bool is_skeleton(const RID &p_rid) const;
  1249. virtual bool is_environment(const RID &p_rid) const;
  1250. virtual bool is_shader(const RID &p_rid) const;
  1251. virtual bool is_canvas_light_occluder(const RID &p_rid) const;
  1252. virtual void set_time_scale(float p_scale);
  1253. virtual void free(const RID &p_rid);
  1254. virtual void init();
  1255. virtual void finish();
  1256. virtual int get_render_info(VS::RenderInfo p_info);
  1257. void set_base_framebuffer(GLuint p_id, Vector2 p_size = Vector2(0, 0));
  1258. virtual void flush_frame(); //not necesary in most cases
  1259. void set_extensions(const char *p_strings);
  1260. virtual bool needs_to_draw_next_frame() const;
  1261. void set_use_framebuffers(bool p_use);
  1262. void reload_vram();
  1263. virtual bool has_feature(VS::Features p_feature) const;
  1264. virtual void restore_framebuffer();
  1265. static RasterizerGLES2 *get_singleton();
  1266. virtual void set_force_16_bits_fbo(bool p_force);
  1267. RasterizerGLES2(bool p_compress_arrays = false, bool p_keep_ram_copy = true, bool p_default_fragment_lighting = true, bool p_use_reload_hooks = false);
  1268. virtual ~RasterizerGLES2();
  1269. };
  1270. #endif
  1271. #endif