rasterizer_storage_gles3.h 48 KB

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