rasterizer_storage_gles3.h 49 KB

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