rasterizer_storage_gles3.h 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /*************************************************************************/
  2. /* rasterizer_storage_gles3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RASTERIZER_STORAGE_OPENGL_H
  31. #define RASTERIZER_STORAGE_OPENGL_H
  32. #ifdef GLES3_ENABLED
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/rid_owner.h"
  35. #include "core/templates/self_list.h"
  36. #include "servers/rendering/renderer_compositor.h"
  37. #include "servers/rendering/renderer_storage.h"
  38. #include "servers/rendering/shader_compiler.h"
  39. #include "servers/rendering/shader_language.h"
  40. #include "shaders/copy.glsl.gen.h"
  41. class RasterizerCanvasGLES3;
  42. class RasterizerSceneGLES3;
  43. class RasterizerStorageGLES3 : public RendererStorage {
  44. friend class RasterizerGLES3;
  45. Thread::ID _main_thread_id = 0;
  46. bool _is_main_thread();
  47. public:
  48. RasterizerCanvasGLES3 *canvas;
  49. RasterizerSceneGLES3 *scene;
  50. static GLuint system_fbo;
  51. struct Config {
  52. bool shrink_textures_x2;
  53. bool use_fast_texture_filter;
  54. bool use_skeleton_software;
  55. int max_vertex_texture_image_units;
  56. int max_texture_image_units;
  57. int max_texture_size;
  58. // TODO implement wireframe in OpenGL
  59. // bool generate_wireframes;
  60. Set<String> extensions;
  61. bool float_texture_supported;
  62. bool s3tc_supported;
  63. bool latc_supported;
  64. bool rgtc_supported;
  65. bool bptc_supported;
  66. bool etc_supported;
  67. bool etc2_supported;
  68. bool pvrtc_supported;
  69. bool srgb_decode_supported;
  70. bool keep_original_textures;
  71. bool force_vertex_shading;
  72. bool use_rgba_2d_shadows;
  73. bool use_rgba_3d_shadows;
  74. bool support_32_bits_indices;
  75. bool support_write_depth;
  76. bool support_half_float_vertices;
  77. bool support_npot_repeat_mipmap;
  78. bool support_depth_texture;
  79. bool support_depth_cubemaps;
  80. bool support_shadow_cubemaps;
  81. bool render_to_mipmap_supported;
  82. GLuint depth_internalformat;
  83. GLuint depth_type;
  84. GLuint depth_buffer_internalformat;
  85. // in some cases the legacy render didn't orphan. We will mark these
  86. // so the user can switch orphaning off for them.
  87. bool should_orphan;
  88. } config;
  89. struct Resources {
  90. GLuint white_tex;
  91. GLuint black_tex;
  92. GLuint normal_tex;
  93. GLuint aniso_tex;
  94. GLuint mipmap_blur_fbo;
  95. GLuint mipmap_blur_color;
  96. GLuint radical_inverse_vdc_cache_tex;
  97. bool use_rgba_2d_shadows;
  98. GLuint quadie;
  99. size_t skeleton_transform_buffer_size;
  100. GLuint skeleton_transform_buffer;
  101. LocalVector<float> skeleton_transform_cpu_buffer;
  102. } resources;
  103. mutable struct Shaders {
  104. ShaderCompiler compiler;
  105. CopyShaderGLES3 copy;
  106. RID copy_version;
  107. //CubemapFilterShaderGLES3 cubemap_filter;
  108. ShaderCompiler::IdentifierActions actions_canvas;
  109. ShaderCompiler::IdentifierActions actions_scene;
  110. ShaderCompiler::IdentifierActions actions_particles;
  111. } shaders;
  112. struct Info {
  113. uint64_t texture_mem;
  114. uint64_t vertex_mem;
  115. struct Render {
  116. uint32_t object_count;
  117. uint32_t draw_call_count;
  118. uint32_t material_switch_count;
  119. uint32_t surface_switch_count;
  120. uint32_t shader_rebind_count;
  121. uint32_t vertices_count;
  122. uint32_t _2d_item_count;
  123. uint32_t _2d_draw_call_count;
  124. void reset() {
  125. object_count = 0;
  126. draw_call_count = 0;
  127. material_switch_count = 0;
  128. surface_switch_count = 0;
  129. shader_rebind_count = 0;
  130. vertices_count = 0;
  131. _2d_item_count = 0;
  132. _2d_draw_call_count = 0;
  133. }
  134. } render, render_final, snap;
  135. Info() :
  136. texture_mem(0),
  137. vertex_mem(0) {
  138. render.reset();
  139. render_final.reset();
  140. }
  141. } info;
  142. void bind_quad_array() const;
  143. /////////////////////////////////////////////////////////////////////////////////////////
  144. //////////////////////////////////API////////////////////////////////////////////////////
  145. /////////////////////////////////////////////////////////////////////////////////////////
  146. bool can_create_resources_async() const override;
  147. // TEXTURE API
  148. enum OpenGLTextureFlags {
  149. TEXTURE_FLAG_MIPMAPS = 1, /// Enable automatic mipmap generation - when available
  150. TEXTURE_FLAG_REPEAT = 2, /// Repeat texture (Tiling), otherwise Clamping
  151. TEXTURE_FLAG_FILTER = 4, /// Create texture with linear (or available) filter
  152. TEXTURE_FLAG_ANISOTROPIC_FILTER = 8,
  153. TEXTURE_FLAG_CONVERT_TO_LINEAR = 16,
  154. TEXTURE_FLAG_MIRRORED_REPEAT = 32, /// Repeat texture, with alternate sections mirrored
  155. TEXTURE_FLAG_USED_FOR_STREAMING = 2048,
  156. TEXTURE_FLAGS_DEFAULT = TEXTURE_FLAG_REPEAT | TEXTURE_FLAG_MIPMAPS | TEXTURE_FLAG_FILTER
  157. };
  158. /* CANVAS TEXTURE API (2D) */
  159. struct CanvasTexture {
  160. RID diffuse;
  161. RID normal_map;
  162. RID specular;
  163. Color specular_color = Color(1, 1, 1, 1);
  164. float shininess = 1.0;
  165. RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  166. RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  167. Size2i size_cache = Size2i(1, 1);
  168. bool use_normal_cache = false;
  169. bool use_specular_cache = false;
  170. bool cleared_cache = true;
  171. };
  172. RID_Owner<CanvasTexture, true> canvas_texture_owner;
  173. struct RenderTarget;
  174. struct Texture {
  175. RID self;
  176. Texture *proxy;
  177. Set<Texture *> proxy_owners;
  178. String path;
  179. uint32_t flags;
  180. int width, height, depth;
  181. int alloc_width, alloc_height;
  182. Image::Format format;
  183. RenderingDevice::TextureType type;
  184. GLenum target;
  185. GLenum gl_format_cache;
  186. GLenum gl_internal_format_cache;
  187. GLenum gl_type_cache;
  188. int data_size;
  189. int total_data_size;
  190. bool ignore_mipmaps;
  191. bool compressed;
  192. bool srgb;
  193. int mipmaps;
  194. bool resize_to_po2;
  195. bool active;
  196. GLenum tex_id;
  197. uint16_t stored_cube_sides;
  198. RenderTarget *render_target;
  199. Vector<Ref<Image>> images;
  200. bool redraw_if_visible;
  201. RS::TextureDetectCallback detect_3d;
  202. void *detect_3d_ud;
  203. RS::TextureDetectCallback detect_srgb;
  204. void *detect_srgb_ud;
  205. RS::TextureDetectCallback detect_normal;
  206. void *detect_normal_ud;
  207. CanvasTexture *canvas_texture = nullptr;
  208. // some silly opengl shenanigans where
  209. // texture coords start from bottom left, means we need to draw render target textures upside down
  210. // to be compatible with vulkan etc.
  211. bool is_upside_down() const {
  212. if (proxy)
  213. return proxy->is_upside_down();
  214. return render_target != nullptr;
  215. }
  216. Texture() {
  217. create();
  218. }
  219. _ALWAYS_INLINE_ Texture *get_ptr() {
  220. if (proxy) {
  221. return proxy; //->get_ptr(); only one level of indirection, else not inlining possible.
  222. } else {
  223. return this;
  224. }
  225. }
  226. ~Texture() {
  227. destroy();
  228. if (tex_id != 0) {
  229. glDeleteTextures(1, &tex_id);
  230. }
  231. }
  232. void copy_from(const Texture &o) {
  233. proxy = o.proxy;
  234. flags = o.flags;
  235. width = o.width;
  236. height = o.height;
  237. alloc_width = o.alloc_width;
  238. alloc_height = o.alloc_height;
  239. format = o.format;
  240. type = o.type;
  241. target = o.target;
  242. data_size = o.data_size;
  243. total_data_size = o.total_data_size;
  244. ignore_mipmaps = o.ignore_mipmaps;
  245. compressed = o.compressed;
  246. mipmaps = o.mipmaps;
  247. resize_to_po2 = o.resize_to_po2;
  248. active = o.active;
  249. tex_id = o.tex_id;
  250. stored_cube_sides = o.stored_cube_sides;
  251. render_target = o.render_target;
  252. redraw_if_visible = o.redraw_if_visible;
  253. detect_3d = o.detect_3d;
  254. detect_3d_ud = o.detect_3d_ud;
  255. detect_srgb = o.detect_srgb;
  256. detect_srgb_ud = o.detect_srgb_ud;
  257. detect_normal = o.detect_normal;
  258. detect_normal_ud = o.detect_normal_ud;
  259. images.clear();
  260. }
  261. void create() {
  262. proxy = nullptr;
  263. flags = 0;
  264. width = 0;
  265. height = 0;
  266. alloc_width = 0;
  267. alloc_height = 0;
  268. format = Image::FORMAT_L8;
  269. type = RenderingDevice::TEXTURE_TYPE_2D;
  270. target = 0;
  271. data_size = 0;
  272. total_data_size = 0;
  273. ignore_mipmaps = false;
  274. compressed = false;
  275. mipmaps = 0;
  276. resize_to_po2 = false;
  277. active = false;
  278. tex_id = 0;
  279. stored_cube_sides = 0;
  280. render_target = nullptr;
  281. redraw_if_visible = false;
  282. detect_3d = nullptr;
  283. detect_3d_ud = nullptr;
  284. detect_srgb = nullptr;
  285. detect_srgb_ud = nullptr;
  286. detect_normal = nullptr;
  287. detect_normal_ud = nullptr;
  288. }
  289. void destroy() {
  290. images.clear();
  291. for (Set<Texture *>::Element *E = proxy_owners.front(); E; E = E->next()) {
  292. E->get()->proxy = NULL;
  293. }
  294. if (proxy) {
  295. proxy->proxy_owners.erase(this);
  296. }
  297. }
  298. // texture state
  299. void GLSetFilter(GLenum p_target, RS::CanvasItemTextureFilter p_filter) {
  300. if (p_filter == state_filter)
  301. return;
  302. state_filter = p_filter;
  303. GLint pmin = GL_LINEAR; // param min
  304. GLint pmag = GL_LINEAR; // param mag
  305. switch (state_filter) {
  306. default: {
  307. } break;
  308. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  309. pmin = GL_LINEAR_MIPMAP_LINEAR;
  310. pmag = GL_LINEAR;
  311. } break;
  312. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  313. pmin = GL_NEAREST;
  314. pmag = GL_NEAREST;
  315. } break;
  316. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  317. pmin = GL_NEAREST_MIPMAP_NEAREST;
  318. pmag = GL_NEAREST;
  319. } break;
  320. }
  321. glTexParameteri(p_target, GL_TEXTURE_MIN_FILTER, pmin);
  322. glTexParameteri(p_target, GL_TEXTURE_MAG_FILTER, pmag);
  323. }
  324. void GLSetRepeat(GLenum p_target, RS::CanvasItemTextureRepeat p_repeat) {
  325. if (p_repeat == state_repeat)
  326. return;
  327. state_repeat = p_repeat;
  328. GLint prep = GL_CLAMP_TO_EDGE; // parameter repeat
  329. switch (state_repeat) {
  330. default: {
  331. } break;
  332. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  333. prep = GL_REPEAT;
  334. } break;
  335. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  336. prep = GL_MIRRORED_REPEAT;
  337. } break;
  338. }
  339. glTexParameteri(p_target, GL_TEXTURE_WRAP_S, prep);
  340. glTexParameteri(p_target, GL_TEXTURE_WRAP_T, prep);
  341. }
  342. private:
  343. RS::CanvasItemTextureFilter state_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
  344. RS::CanvasItemTextureRepeat state_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
  345. };
  346. mutable RID_PtrOwner<Texture> texture_owner;
  347. Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const;
  348. void _texture_set_state_from_flags(Texture *p_tex);
  349. // new
  350. RID texture_allocate() override;
  351. void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;
  352. void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;
  353. void texture_3d_initialize(RID p_texture, Image::Format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) override;
  354. void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent
  355. void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;
  356. void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override {}
  357. void texture_proxy_update(RID p_proxy, RID p_base) override {}
  358. void texture_2d_placeholder_initialize(RID p_texture) override;
  359. void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;
  360. void texture_3d_placeholder_initialize(RID p_texture) override;
  361. Ref<Image> texture_2d_get(RID p_texture) const override;
  362. Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override { return Ref<Image>(); }
  363. Vector<Ref<Image>> texture_3d_get(RID p_texture) const override { return Vector<Ref<Image>>(); }
  364. void texture_replace(RID p_texture, RID p_by_texture) override;
  365. //void texture_set_size_override(RID p_texture, int p_width, int p_height) override {}
  366. void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
  367. void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
  368. // old
  369. uint32_t texture_get_width(RID p_texture) const;
  370. uint32_t texture_get_height(RID p_texture) const;
  371. private:
  372. RID texture_create();
  373. //void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, RenderingDevice::TextureType p_type, uint32_t p_flags = TEXTURE_FLAGS_DEFAULT);
  374. void _texture_allocate_internal(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, RenderingDevice::TextureType p_type, uint32_t p_flags = TEXTURE_FLAGS_DEFAULT);
  375. void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  376. 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);
  377. //Ref<Image> texture_get_data(RID p_texture, int p_layer = 0) const;
  378. void texture_set_flags(RID p_texture, uint32_t p_flags);
  379. uint32_t texture_get_flags(RID p_texture) const;
  380. Image::Format texture_get_format(RID p_texture) const;
  381. RenderingDevice::TextureType texture_get_type(RID p_texture) const;
  382. uint32_t texture_get_texid(RID p_texture) const;
  383. uint32_t texture_get_depth(RID p_texture) const;
  384. void texture_set_size_override(RID p_texture, int p_width, int p_height) override;
  385. void texture_bind(RID p_texture, uint32_t p_texture_no);
  386. void texture_set_path(RID p_texture, const String &p_path) override;
  387. String texture_get_path(RID p_texture) const override;
  388. void texture_set_shrink_all_x2_on_set_data(bool p_enable);
  389. void texture_debug_usage(List<RS::TextureInfo> *r_info) override;
  390. RID texture_create_radiance_cubemap(RID p_source, int p_resolution = -1) const;
  391. void textures_keep_original(bool p_enable);
  392. void texture_set_proxy(RID p_texture, RID p_proxy);
  393. Size2 texture_size_with_proxy(RID p_texture) override;
  394. void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  395. void texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  396. void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  397. void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override {}
  398. void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;
  399. public:
  400. RID canvas_texture_allocate() override;
  401. void canvas_texture_initialize(RID p_rid) override;
  402. void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;
  403. void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess) override;
  404. void canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) override;
  405. void canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) override;
  406. /* SKY API */
  407. // not sure if used in godot 4?
  408. struct Sky {
  409. RID self;
  410. RID panorama;
  411. GLuint radiance;
  412. int radiance_size;
  413. };
  414. mutable RID_PtrOwner<Sky> sky_owner;
  415. RID sky_create();
  416. void sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size);
  417. // SHADER API
  418. struct Material;
  419. struct Shader {
  420. RID self;
  421. RS::ShaderMode mode;
  422. ShaderGLES3 *shader;
  423. String code;
  424. SelfList<Material>::List materials;
  425. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  426. RID version;
  427. SelfList<Shader> dirty_list;
  428. Map<StringName, Map<int, RID>> default_textures;
  429. Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
  430. bool valid;
  431. String path;
  432. uint32_t index;
  433. uint64_t last_pass;
  434. struct CanvasItem {
  435. enum BlendMode {
  436. BLEND_MODE_MIX,
  437. BLEND_MODE_ADD,
  438. BLEND_MODE_SUB,
  439. BLEND_MODE_MUL,
  440. BLEND_MODE_PMALPHA,
  441. };
  442. int blend_mode;
  443. enum LightMode {
  444. LIGHT_MODE_NORMAL,
  445. LIGHT_MODE_UNSHADED,
  446. LIGHT_MODE_LIGHT_ONLY
  447. };
  448. int light_mode;
  449. bool uses_screen_texture;
  450. bool uses_screen_uv;
  451. bool uses_time;
  452. bool uses_modulate;
  453. bool uses_color;
  454. bool uses_vertex;
  455. // all these should disable item joining if used in a custom shader
  456. bool uses_world_matrix;
  457. bool uses_extra_matrix;
  458. bool uses_projection_matrix;
  459. bool uses_instance_custom;
  460. } canvas_item;
  461. struct Spatial {
  462. enum BlendMode {
  463. BLEND_MODE_MIX,
  464. BLEND_MODE_ADD,
  465. BLEND_MODE_SUB,
  466. BLEND_MODE_MUL,
  467. };
  468. int blend_mode;
  469. enum DepthDrawMode {
  470. DEPTH_DRAW_OPAQUE,
  471. DEPTH_DRAW_ALWAYS,
  472. DEPTH_DRAW_NEVER,
  473. DEPTH_DRAW_ALPHA_PREPASS,
  474. };
  475. int depth_draw_mode;
  476. enum CullMode {
  477. CULL_MODE_FRONT,
  478. CULL_MODE_BACK,
  479. CULL_MODE_DISABLED,
  480. };
  481. int cull_mode;
  482. bool uses_alpha;
  483. bool uses_alpha_scissor;
  484. bool unshaded;
  485. bool no_depth_test;
  486. bool uses_vertex;
  487. bool uses_discard;
  488. bool uses_sss;
  489. bool uses_screen_texture;
  490. bool uses_depth_texture;
  491. bool uses_time;
  492. bool uses_tangent;
  493. bool uses_ensure_correct_normals;
  494. bool writes_modelview_or_projection;
  495. bool uses_vertex_lighting;
  496. bool uses_world_coordinates;
  497. } spatial;
  498. struct Particles {
  499. } particles;
  500. bool uses_vertex_time;
  501. bool uses_fragment_time;
  502. Shader() :
  503. dirty_list(this) {
  504. shader = NULL;
  505. valid = false;
  506. version = RID();
  507. last_pass = 0;
  508. }
  509. };
  510. mutable RID_PtrOwner<Shader> shader_owner;
  511. mutable SelfList<Shader>::List _shader_dirty_list;
  512. void _shader_make_dirty(Shader *p_shader);
  513. RID shader_allocate() override;
  514. void shader_initialize(RID p_rid) override;
  515. //RID shader_create() override;
  516. void shader_set_code(RID p_shader, const String &p_code) override;
  517. String shader_get_code(RID p_shader) const override;
  518. void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const override;
  519. void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture, int p_index) override;
  520. RID shader_get_default_texture_param(RID p_shader, const StringName &p_name, int p_index) const override;
  521. RS::ShaderNativeSourceCode shader_get_native_source_code(RID p_shader) const override { return RS::ShaderNativeSourceCode(); };
  522. void _update_shader(Shader *p_shader) const;
  523. void update_dirty_shaders();
  524. // new
  525. Variant shader_get_param_default(RID p_material, const StringName &p_param) const override { return Variant(); }
  526. // COMMON MATERIAL API
  527. struct Material {
  528. RID self;
  529. Shader *shader;
  530. Map<StringName, Variant> params;
  531. SelfList<Material> list;
  532. SelfList<Material> dirty_list;
  533. Vector<Pair<StringName, RID>> textures;
  534. float line_width;
  535. int render_priority;
  536. RID next_pass;
  537. uint32_t index;
  538. uint64_t last_pass;
  539. // Map<Geometry *, int> geometry_owners;
  540. // Map<InstanceBaseDependency *, int> instance_owners;
  541. bool can_cast_shadow_cache;
  542. bool is_animated_cache;
  543. Material() :
  544. list(this),
  545. dirty_list(this) {
  546. can_cast_shadow_cache = false;
  547. is_animated_cache = false;
  548. shader = NULL;
  549. line_width = 1.0;
  550. last_pass = 0;
  551. render_priority = 0;
  552. }
  553. };
  554. mutable SelfList<Material>::List _material_dirty_list;
  555. void _material_make_dirty(Material *p_material) const;
  556. // void _material_add_geometry(RID p_material, Geometry *p_geometry);
  557. // void _material_remove_geometry(RID p_material, Geometry *p_geometry);
  558. void _update_material(Material *p_material);
  559. mutable RID_PtrOwner<Material> material_owner;
  560. // new
  561. void material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) override {}
  562. void material_update_dependency(RID p_material, DependencyTracker *p_instance) override {}
  563. // old
  564. RID material_allocate() override;
  565. void material_initialize(RID p_rid) override;
  566. //RID material_create() override;
  567. void material_set_shader(RID p_material, RID p_shader) override;
  568. RID material_get_shader(RID p_material) const;
  569. void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) override;
  570. Variant material_get_param(RID p_material, const StringName &p_param) const override;
  571. Variant material_get_param_default(RID p_material, const StringName &p_param) const;
  572. void material_set_line_width(RID p_material, float p_width);
  573. void material_set_next_pass(RID p_material, RID p_next_material) override;
  574. bool material_is_animated(RID p_material) override;
  575. bool material_casts_shadows(RID p_material) override;
  576. bool material_uses_tangents(RID p_material);
  577. bool material_uses_ensure_correct_normals(RID p_material);
  578. void material_add_instance_owner(RID p_material, DependencyTracker *p_instance);
  579. void material_remove_instance_owner(RID p_material, DependencyTracker *p_instance);
  580. void material_set_render_priority(RID p_material, int priority) override;
  581. void update_dirty_materials();
  582. /* MESH API */
  583. RID mesh_allocate() override;
  584. void mesh_initialize(RID p_rid) override;
  585. void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) override;
  586. bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton) override;
  587. RID mesh_instance_create(RID p_base) override;
  588. void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) override;
  589. void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) override;
  590. void mesh_instance_check_for_update(RID p_mesh_instance) override;
  591. void update_mesh_instances() override;
  592. void reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) override;
  593. float reflection_probe_get_mesh_lod_threshold(RID p_probe) const override;
  594. void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) override;
  595. int mesh_get_blend_shape_count(RID p_mesh) const override;
  596. void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) override;
  597. RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const override;
  598. void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  599. void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  600. void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  601. void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override;
  602. RID mesh_surface_get_material(RID p_mesh, int p_surface) const override;
  603. RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const override;
  604. int mesh_get_surface_count(RID p_mesh) const override;
  605. void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override;
  606. AABB mesh_get_custom_aabb(RID p_mesh) const override;
  607. AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override;
  608. void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override;
  609. void mesh_clear(RID p_mesh) override;
  610. /* MULTIMESH API */
  611. struct MultiMesh {
  612. RID mesh;
  613. int instances = 0;
  614. RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D;
  615. bool uses_colors = false;
  616. bool uses_custom_data = false;
  617. int visible_instances = -1;
  618. AABB aabb;
  619. bool aabb_dirty = false;
  620. bool buffer_set = false;
  621. uint32_t stride_cache = 0;
  622. uint32_t color_offset_cache = 0;
  623. uint32_t custom_data_offset_cache = 0;
  624. Vector<float> data_cache; //used if individual setting is used
  625. bool *data_cache_dirty_regions = nullptr;
  626. uint32_t data_cache_used_dirty_regions = 0;
  627. RID buffer; //storage buffer
  628. RID uniform_set_3d;
  629. RID uniform_set_2d;
  630. bool dirty = false;
  631. MultiMesh *dirty_list = nullptr;
  632. Dependency dependency;
  633. };
  634. mutable RID_Owner<MultiMesh, true> multimesh_owner;
  635. MultiMesh *multimesh_dirty_list = nullptr;
  636. _FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const;
  637. _FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb);
  638. _FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb);
  639. _FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances);
  640. void _update_dirty_multimeshes();
  641. RID multimesh_allocate() override;
  642. void multimesh_initialize(RID p_rid) override;
  643. void multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false) override;
  644. int multimesh_get_instance_count(RID p_multimesh) const override;
  645. void multimesh_set_mesh(RID p_multimesh, RID p_mesh) override;
  646. void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) override;
  647. void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) override;
  648. void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) override;
  649. void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) override;
  650. RID multimesh_get_mesh(RID p_multimesh) const override;
  651. AABB multimesh_get_aabb(RID p_multimesh) const override;
  652. Transform3D multimesh_instance_get_transform(RID p_multimesh, int p_index) const override;
  653. Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const override;
  654. Color multimesh_instance_get_color(RID p_multimesh, int p_index) const override;
  655. Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const override;
  656. void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) override;
  657. Vector<float> multimesh_get_buffer(RID p_multimesh) const override;
  658. void multimesh_set_visible_instances(RID p_multimesh, int p_visible) override;
  659. int multimesh_get_visible_instances(RID p_multimesh) const override;
  660. _FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const {
  661. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  662. return multimesh->xform_format;
  663. }
  664. _FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const {
  665. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  666. return multimesh->uses_colors;
  667. }
  668. _FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const {
  669. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  670. return multimesh->uses_custom_data;
  671. }
  672. _FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const {
  673. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  674. if (multimesh->visible_instances >= 0) {
  675. return multimesh->visible_instances;
  676. }
  677. return multimesh->instances;
  678. }
  679. /* SKELETON API */
  680. RID skeleton_allocate() override;
  681. void skeleton_initialize(RID p_rid) override;
  682. void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) override;
  683. void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) override;
  684. int skeleton_get_bone_count(RID p_skeleton) const override;
  685. void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) override;
  686. Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const override;
  687. void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) override;
  688. Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const override;
  689. /* Light API */
  690. RID directional_light_allocate() override;
  691. void directional_light_initialize(RID p_rid) override;
  692. RID omni_light_allocate() override;
  693. void omni_light_initialize(RID p_rid) override;
  694. RID spot_light_allocate() override;
  695. void spot_light_initialize(RID p_rid) override;
  696. RID reflection_probe_allocate() override;
  697. void reflection_probe_initialize(RID p_rid) override;
  698. void light_set_color(RID p_light, const Color &p_color) override;
  699. void light_set_param(RID p_light, RS::LightParam p_param, float p_value) override;
  700. void light_set_shadow(RID p_light, bool p_enabled) override;
  701. void light_set_shadow_color(RID p_light, const Color &p_color) override;
  702. void light_set_projector(RID p_light, RID p_texture) override;
  703. void light_set_negative(RID p_light, bool p_enable) override;
  704. void light_set_cull_mask(RID p_light, uint32_t p_mask) override;
  705. void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) override;
  706. void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) override;
  707. void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) override;
  708. void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) override;
  709. void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) override;
  710. void light_directional_set_blend_splits(RID p_light, bool p_enable) override;
  711. bool light_directional_get_blend_splits(RID p_light) const override;
  712. void light_directional_set_sky_only(RID p_light, bool p_sky_only) override;
  713. bool light_directional_is_sky_only(RID p_light) const override;
  714. RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) override;
  715. RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) override;
  716. bool light_has_shadow(RID p_light) const override;
  717. bool light_has_projector(RID p_light) const override;
  718. RS::LightType light_get_type(RID p_light) const override;
  719. AABB light_get_aabb(RID p_light) const override;
  720. float light_get_param(RID p_light, RS::LightParam p_param) override;
  721. Color light_get_color(RID p_light) override;
  722. RS::LightBakeMode light_get_bake_mode(RID p_light) override;
  723. uint32_t light_get_max_sdfgi_cascade(RID p_light) override;
  724. uint64_t light_get_version(RID p_light) const override;
  725. /* PROBE API */
  726. void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) override;
  727. void reflection_probe_set_intensity(RID p_probe, float p_intensity) override;
  728. void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) override;
  729. void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) override;
  730. void reflection_probe_set_ambient_energy(RID p_probe, float p_energy) override;
  731. void reflection_probe_set_max_distance(RID p_probe, float p_distance) override;
  732. void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) override;
  733. void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) override;
  734. void reflection_probe_set_as_interior(RID p_probe, bool p_enable) override;
  735. void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) override;
  736. void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) override;
  737. void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) override;
  738. void reflection_probe_set_resolution(RID p_probe, int p_resolution) override;
  739. AABB reflection_probe_get_aabb(RID p_probe) const override;
  740. RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const override;
  741. uint32_t reflection_probe_get_cull_mask(RID p_probe) const override;
  742. Vector3 reflection_probe_get_extents(RID p_probe) const override;
  743. Vector3 reflection_probe_get_origin_offset(RID p_probe) const override;
  744. float reflection_probe_get_origin_max_distance(RID p_probe) const override;
  745. bool reflection_probe_renders_shadows(RID p_probe) const override;
  746. void base_update_dependency(RID p_base, DependencyTracker *p_instance) override;
  747. void skeleton_update_dependency(RID p_base, DependencyTracker *p_instance) override;
  748. /* DECAL API */
  749. RID decal_allocate() override;
  750. void decal_initialize(RID p_rid) override;
  751. void decal_set_extents(RID p_decal, const Vector3 &p_extents) override;
  752. void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;
  753. void decal_set_emission_energy(RID p_decal, float p_energy) override;
  754. void decal_set_albedo_mix(RID p_decal, float p_mix) override;
  755. void decal_set_modulate(RID p_decal, const Color &p_modulate) override;
  756. void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;
  757. void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;
  758. void decal_set_fade(RID p_decal, float p_above, float p_below) override;
  759. void decal_set_normal_fade(RID p_decal, float p_fade) override;
  760. AABB decal_get_aabb(RID p_decal) const override;
  761. /* VOXEL GI API */
  762. RID voxel_gi_allocate() override;
  763. void voxel_gi_initialize(RID p_rid) override;
  764. void voxel_gi_allocate_data(RID p_voxel_gi, const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) override;
  765. AABB voxel_gi_get_bounds(RID p_voxel_gi) const override;
  766. Vector3i voxel_gi_get_octree_size(RID p_voxel_gi) const override;
  767. Vector<uint8_t> voxel_gi_get_octree_cells(RID p_voxel_gi) const override;
  768. Vector<uint8_t> voxel_gi_get_data_cells(RID p_voxel_gi) const override;
  769. Vector<uint8_t> voxel_gi_get_distance_field(RID p_voxel_gi) const override;
  770. Vector<int> voxel_gi_get_level_counts(RID p_voxel_gi) const override;
  771. Transform3D voxel_gi_get_to_cell_xform(RID p_voxel_gi) const override;
  772. void voxel_gi_set_dynamic_range(RID p_voxel_gi, float p_range) override;
  773. float voxel_gi_get_dynamic_range(RID p_voxel_gi) const override;
  774. void voxel_gi_set_propagation(RID p_voxel_gi, float p_range) override;
  775. float voxel_gi_get_propagation(RID p_voxel_gi) const override;
  776. void voxel_gi_set_energy(RID p_voxel_gi, float p_range) override;
  777. float voxel_gi_get_energy(RID p_voxel_gi) const override;
  778. void voxel_gi_set_bias(RID p_voxel_gi, float p_range) override;
  779. float voxel_gi_get_bias(RID p_voxel_gi) const override;
  780. void voxel_gi_set_normal_bias(RID p_voxel_gi, float p_range) override;
  781. float voxel_gi_get_normal_bias(RID p_voxel_gi) const override;
  782. void voxel_gi_set_interior(RID p_voxel_gi, bool p_enable) override;
  783. bool voxel_gi_is_interior(RID p_voxel_gi) const override;
  784. void voxel_gi_set_use_two_bounces(RID p_voxel_gi, bool p_enable) override;
  785. bool voxel_gi_is_using_two_bounces(RID p_voxel_gi) const override;
  786. void voxel_gi_set_anisotropy_strength(RID p_voxel_gi, float p_strength) override;
  787. float voxel_gi_get_anisotropy_strength(RID p_voxel_gi) const override;
  788. uint32_t voxel_gi_get_version(RID p_voxel_gi) override;
  789. /* LIGHTMAP CAPTURE */
  790. RID lightmap_allocate() override;
  791. void lightmap_initialize(RID p_rid) override;
  792. void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) override;
  793. void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) override;
  794. void lightmap_set_probe_interior(RID p_lightmap, bool p_interior) override;
  795. void lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) override;
  796. PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const override;
  797. PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const override;
  798. PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const override;
  799. PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const override;
  800. AABB lightmap_get_aabb(RID p_lightmap) const override;
  801. void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) override;
  802. bool lightmap_is_interior(RID p_lightmap) const override;
  803. void lightmap_set_probe_capture_update_speed(float p_speed) override;
  804. float lightmap_get_probe_capture_update_speed() const override;
  805. /* OCCLUDER */
  806. void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices);
  807. /* PARTICLES */
  808. RID particles_allocate() override;
  809. void particles_initialize(RID p_rid) override;
  810. void particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) override;
  811. void particles_emit(RID p_particles, const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) override;
  812. void particles_set_emitting(RID p_particles, bool p_emitting) override;
  813. void particles_set_amount(RID p_particles, int p_amount) override;
  814. void particles_set_lifetime(RID p_particles, double p_lifetime) override;
  815. void particles_set_one_shot(RID p_particles, bool p_one_shot) override;
  816. void particles_set_pre_process_time(RID p_particles, double p_time) override;
  817. void particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) override;
  818. void particles_set_randomness_ratio(RID p_particles, real_t p_ratio) override;
  819. void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) override;
  820. void particles_set_speed_scale(RID p_particles, double p_scale) override;
  821. void particles_set_use_local_coordinates(RID p_particles, bool p_enable) override;
  822. void particles_set_process_material(RID p_particles, RID p_material) override;
  823. void particles_set_fixed_fps(RID p_particles, int p_fps) override;
  824. void particles_set_interpolate(RID p_particles, bool p_enable) override;
  825. void particles_set_fractional_delta(RID p_particles, bool p_enable) override;
  826. void particles_set_subemitter(RID p_particles, RID p_subemitter_particles) override;
  827. void particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) override;
  828. void particles_set_collision_base_size(RID p_particles, real_t p_size) override;
  829. void particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) override;
  830. void particles_set_trails(RID p_particles, bool p_enable, double p_length) override;
  831. void particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) override;
  832. void particles_restart(RID p_particles) override;
  833. void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) override;
  834. void particles_set_draw_passes(RID p_particles, int p_count) override;
  835. void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) override;
  836. void particles_request_process(RID p_particles) override;
  837. AABB particles_get_current_aabb(RID p_particles) override;
  838. AABB particles_get_aabb(RID p_particles) const override;
  839. void particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) override;
  840. bool particles_get_emitting(RID p_particles) override;
  841. int particles_get_draw_passes(RID p_particles) const override;
  842. RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const override;
  843. void particles_add_collision(RID p_particles, RID p_instance) override;
  844. void particles_remove_collision(RID p_particles, RID p_instance) override;
  845. void particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, RID p_texture) override;
  846. void update_particles() override;
  847. /* PARTICLES COLLISION */
  848. RID particles_collision_allocate() override;
  849. void particles_collision_initialize(RID p_rid) override;
  850. void particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) override;
  851. void particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) override;
  852. void particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) override;
  853. void particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) override;
  854. void particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) override;
  855. void particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) override;
  856. void particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) override;
  857. void particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) override;
  858. void particles_collision_height_field_update(RID p_particles_collision) override;
  859. void particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) override;
  860. AABB particles_collision_get_aabb(RID p_particles_collision) const override;
  861. bool particles_collision_is_heightfield(RID p_particles_collision) const override;
  862. RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const override;
  863. RID particles_collision_instance_create(RID p_collision) override;
  864. void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) override;
  865. void particles_collision_instance_set_active(RID p_collision_instance, bool p_active) override;
  866. /* FOG VOLUMES */
  867. RID fog_volume_allocate() override;
  868. void fog_volume_initialize(RID p_rid) override;
  869. void fog_volume_set_shape(RID p_fog_volume, RS::FogVolumeShape p_shape) override;
  870. void fog_volume_set_extents(RID p_fog_volume, const Vector3 &p_extents) override;
  871. void fog_volume_set_material(RID p_fog_volume, RID p_material) override;
  872. AABB fog_volume_get_aabb(RID p_fog_volume) const override;
  873. RS::FogVolumeShape fog_volume_get_shape(RID p_fog_volume) const override;
  874. /* VISIBILITY NOTIFIER */
  875. RID visibility_notifier_allocate() override;
  876. void visibility_notifier_initialize(RID p_notifier) override;
  877. void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) override;
  878. void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) override;
  879. AABB visibility_notifier_get_aabb(RID p_notifier) const override;
  880. void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) override;
  881. /* GLOBAL VARIABLES */
  882. void global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) override;
  883. void global_variable_remove(const StringName &p_name) override;
  884. Vector<StringName> global_variable_get_list() const override;
  885. void global_variable_set(const StringName &p_name, const Variant &p_value) override;
  886. void global_variable_set_override(const StringName &p_name, const Variant &p_value) override;
  887. Variant global_variable_get(const StringName &p_name) const override;
  888. RS::GlobalVariableType global_variable_get_type(const StringName &p_name) const override;
  889. void global_variables_load_settings(bool p_load_textures = true) override;
  890. void global_variables_clear() override;
  891. int32_t global_variables_instance_allocate(RID p_instance) override;
  892. void global_variables_instance_free(RID p_instance) override;
  893. void global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) override;
  894. bool particles_is_inactive(RID p_particles) const override;
  895. // RENDER TARGET
  896. struct RenderTarget {
  897. RID self;
  898. GLuint fbo;
  899. GLuint color;
  900. GLuint depth;
  901. GLuint multisample_fbo;
  902. GLuint multisample_color;
  903. GLuint multisample_depth;
  904. bool multisample_active;
  905. struct Effect {
  906. GLuint fbo;
  907. int width;
  908. int height;
  909. GLuint color;
  910. Effect() :
  911. fbo(0),
  912. width(0),
  913. height(0),
  914. color(0) {
  915. }
  916. };
  917. Effect copy_screen_effect;
  918. struct MipMaps {
  919. struct Size {
  920. GLuint fbo;
  921. GLuint color;
  922. int width;
  923. int height;
  924. };
  925. Vector<Size> sizes;
  926. GLuint color;
  927. int levels;
  928. MipMaps() :
  929. color(0),
  930. levels(0) {
  931. }
  932. };
  933. MipMaps mip_maps[2];
  934. struct External {
  935. GLuint fbo;
  936. GLuint color;
  937. GLuint depth;
  938. RID texture;
  939. External() :
  940. fbo(0),
  941. color(0),
  942. depth(0) {
  943. }
  944. } external;
  945. int x, y, width, height;
  946. bool flags[RENDER_TARGET_FLAG_MAX];
  947. // instead of allocating sized render targets immediately,
  948. // defer this for faster startup
  949. bool allocate_is_dirty = false;
  950. bool used_in_frame;
  951. RS::ViewportMSAA msaa;
  952. bool use_fxaa;
  953. bool use_debanding;
  954. RID texture;
  955. bool used_dof_blur_near;
  956. bool mip_maps_allocated;
  957. Color clear_color;
  958. bool clear_requested;
  959. RenderTarget() :
  960. fbo(0),
  961. color(0),
  962. depth(0),
  963. multisample_fbo(0),
  964. multisample_color(0),
  965. multisample_depth(0),
  966. multisample_active(false),
  967. x(0),
  968. y(0),
  969. width(0),
  970. height(0),
  971. used_in_frame(false),
  972. msaa(RS::VIEWPORT_MSAA_DISABLED),
  973. use_fxaa(false),
  974. use_debanding(false),
  975. used_dof_blur_near(false),
  976. mip_maps_allocated(false),
  977. clear_color(Color(1, 1, 1, 1)),
  978. clear_requested(false) {
  979. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; ++i) {
  980. flags[i] = false;
  981. }
  982. external.fbo = 0;
  983. }
  984. };
  985. mutable RID_PtrOwner<RenderTarget> render_target_owner;
  986. void _render_target_clear(RenderTarget *rt);
  987. void _render_target_allocate(RenderTarget *rt);
  988. void _set_current_render_target(RID p_render_target);
  989. RID render_target_create() override;
  990. void render_target_set_position(RID p_render_target, int p_x, int p_y) override;
  991. void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
  992. Size2i render_target_get_size(RID p_render_target);
  993. RID render_target_get_texture(RID p_render_target) override;
  994. void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) override;
  995. void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) override;
  996. bool render_target_was_used(RID p_render_target) override;
  997. void render_target_clear_used(RID p_render_target);
  998. void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa);
  999. void render_target_set_use_fxaa(RID p_render_target, bool p_fxaa);
  1000. void render_target_set_use_debanding(RID p_render_target, bool p_debanding);
  1001. // new
  1002. void render_target_set_as_unused(RID p_render_target) override {
  1003. render_target_clear_used(p_render_target);
  1004. }
  1005. void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;
  1006. bool render_target_is_clear_requested(RID p_render_target) override;
  1007. Color render_target_get_clear_request_color(RID p_render_target) override;
  1008. void render_target_disable_clear_request(RID p_render_target) override;
  1009. void render_target_do_clear_request(RID p_render_target) override;
  1010. void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
  1011. Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
  1012. void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;
  1013. // access from canvas
  1014. // RenderTarget * render_target_get(RID p_render_target);
  1015. /* CANVAS SHADOW */
  1016. struct CanvasLightShadow {
  1017. RID self;
  1018. int size;
  1019. int height;
  1020. GLuint fbo;
  1021. GLuint depth;
  1022. GLuint distance; //for older devices
  1023. };
  1024. RID_PtrOwner<CanvasLightShadow> canvas_light_shadow_owner;
  1025. RID canvas_light_shadow_buffer_create(int p_width);
  1026. /* LIGHT SHADOW MAPPING */
  1027. /*
  1028. struct CanvasOccluder {
  1029. RID self;
  1030. GLuint vertex_id; // 0 means, unconfigured
  1031. GLuint index_id; // 0 means, unconfigured
  1032. LocalVector<Vector2> lines;
  1033. int len;
  1034. };
  1035. RID_Owner<CanvasOccluder> canvas_occluder_owner;
  1036. RID canvas_light_occluder_create();
  1037. void canvas_light_occluder_set_polylines(RID p_occluder, const LocalVector<Vector2> &p_lines);
  1038. */
  1039. RS::InstanceType get_base_type(RID p_rid) const override;
  1040. bool free(RID p_rid) override;
  1041. struct Frame {
  1042. RenderTarget *current_rt;
  1043. // these 2 may have been superseded by the equivalents in the render target.
  1044. // these may be able to be removed.
  1045. bool clear_request;
  1046. Color clear_request_color;
  1047. float time;
  1048. float delta;
  1049. uint64_t count;
  1050. Frame() {
  1051. // current_rt = nullptr;
  1052. // clear_request = false;
  1053. }
  1054. } frame;
  1055. void initialize();
  1056. void finalize();
  1057. void _copy_screen();
  1058. void update_memory_info() override;
  1059. uint64_t get_rendering_info(RS::RenderingInfo p_info) override;
  1060. bool has_os_feature(const String &p_feature) const override;
  1061. void update_dirty_resources() override;
  1062. void set_debug_generate_wireframes(bool p_generate) override;
  1063. // void render_info_begin_capture() override;
  1064. // void render_info_end_capture() override;
  1065. // int get_captured_render_info(RS::RenderInfo p_info) override;
  1066. // int get_render_info(RS::RenderInfo p_info) override;
  1067. String get_video_adapter_name() const override;
  1068. String get_video_adapter_vendor() const override;
  1069. RenderingDevice::DeviceType get_video_adapter_type() const override;
  1070. void capture_timestamps_begin() override {}
  1071. void capture_timestamp(const String &p_name) override {}
  1072. uint32_t get_captured_timestamps_count() const override {
  1073. return 0;
  1074. }
  1075. uint64_t get_captured_timestamps_frame() const override {
  1076. return 0;
  1077. }
  1078. uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const override {
  1079. return 0;
  1080. }
  1081. uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const override {
  1082. return 0;
  1083. }
  1084. String get_captured_timestamp_name(uint32_t p_index) const override {
  1085. return String();
  1086. }
  1087. // make access easier to these
  1088. struct Dimensions {
  1089. // render target
  1090. int rt_width;
  1091. int rt_height;
  1092. // window
  1093. int win_width;
  1094. int win_height;
  1095. Dimensions() {
  1096. rt_width = 0;
  1097. rt_height = 0;
  1098. win_width = 0;
  1099. win_height = 0;
  1100. }
  1101. } _dims;
  1102. void buffer_orphan_and_upload(unsigned int p_buffer_size, unsigned int p_offset, unsigned int p_data_size, const void *p_data, GLenum p_target = GL_ARRAY_BUFFER, GLenum p_usage = GL_DYNAMIC_DRAW, bool p_optional_orphan = false) const;
  1103. bool safe_buffer_sub_data(unsigned int p_total_buffer_size, GLenum p_target, unsigned int p_offset, unsigned int p_data_size, const void *p_data, unsigned int &r_offset_after) const;
  1104. void bind_framebuffer(GLuint framebuffer) {
  1105. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  1106. }
  1107. void bind_framebuffer_system() {
  1108. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  1109. }
  1110. RasterizerStorageGLES3();
  1111. ~RasterizerStorageGLES3();
  1112. };
  1113. inline bool RasterizerStorageGLES3::safe_buffer_sub_data(unsigned int p_total_buffer_size, GLenum p_target, unsigned int p_offset, unsigned int p_data_size, const void *p_data, unsigned int &r_offset_after) const {
  1114. r_offset_after = p_offset + p_data_size;
  1115. #ifdef DEBUG_ENABLED
  1116. // we are trying to write across the edge of the buffer
  1117. if (r_offset_after > p_total_buffer_size)
  1118. return false;
  1119. #endif
  1120. glBufferSubData(p_target, p_offset, p_data_size, p_data);
  1121. return true;
  1122. }
  1123. // standardize the orphan / upload in one place so it can be changed per platform as necessary, and avoid future
  1124. // bugs causing pipeline stalls
  1125. inline void RasterizerStorageGLES3::buffer_orphan_and_upload(unsigned int p_buffer_size, unsigned int p_offset, unsigned int p_data_size, const void *p_data, GLenum p_target, GLenum p_usage, bool p_optional_orphan) const {
  1126. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  1127. // Was previously #ifndef GLES_OVER_GL however this causes stalls on desktop mac also (and possibly other)
  1128. if (!p_optional_orphan || (config.should_orphan)) {
  1129. glBufferData(p_target, p_buffer_size, NULL, p_usage);
  1130. #ifdef RASTERIZER_EXTRA_CHECKS
  1131. // fill with garbage off the end of the array
  1132. if (p_buffer_size) {
  1133. unsigned int start = p_offset + p_data_size;
  1134. unsigned int end = start + 1024;
  1135. if (end < p_buffer_size) {
  1136. uint8_t *garbage = (uint8_t *)alloca(1024);
  1137. for (int n = 0; n < 1024; n++) {
  1138. garbage[n] = Math::random(0, 255);
  1139. }
  1140. glBufferSubData(p_target, start, 1024, garbage);
  1141. }
  1142. }
  1143. #endif
  1144. }
  1145. glBufferSubData(p_target, p_offset, p_data_size, p_data);
  1146. }
  1147. #endif // GLES3_ENABLED
  1148. #endif // RASTERIZER_STORAGE_OPENGL_H