texture_storage.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /**************************************************************************/
  2. /* texture_storage.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 TEXTURE_STORAGE_GLES3_H
  31. #define TEXTURE_STORAGE_GLES3_H
  32. #ifdef GLES3_ENABLED
  33. #include "platform_gl.h"
  34. #include "config.h"
  35. #include "core/os/os.h"
  36. #include "core/templates/rid_owner.h"
  37. #include "servers/rendering/renderer_compositor.h"
  38. #include "servers/rendering/storage/texture_storage.h"
  39. #include "drivers/gles3/shaders/canvas_sdf.glsl.gen.h"
  40. namespace GLES3 {
  41. #define _GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
  42. #define _GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
  43. #define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  44. #define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  45. #define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  46. #define _EXT_COMPRESSED_RED_RGTC1_EXT 0x8DBB
  47. #define _EXT_COMPRESSED_RED_RGTC1 0x8DBB
  48. #define _EXT_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
  49. #define _EXT_COMPRESSED_RG_RGTC2 0x8DBD
  50. #define _EXT_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE
  51. #define _EXT_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
  52. #define _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
  53. #define _EXT_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
  54. #define _EXT_ETC1_RGB8_OES 0x8D64
  55. #define _EXT_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C
  56. #define _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D
  57. #define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E
  58. #define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F
  59. #define _EXT_COMPRESSED_R11_EAC 0x9270
  60. #define _EXT_COMPRESSED_SIGNED_R11_EAC 0x9271
  61. #define _EXT_COMPRESSED_RG11_EAC 0x9272
  62. #define _EXT_COMPRESSED_SIGNED_RG11_EAC 0x9273
  63. #define _EXT_COMPRESSED_RGB8_ETC2 0x9274
  64. #define _EXT_COMPRESSED_SRGB8_ETC2 0x9275
  65. #define _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  66. #define _EXT_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  67. #define _EXT_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  68. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  69. #define _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
  70. #define _EXT_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B1
  71. #define _EXT_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
  72. #define _EXT_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B3
  73. #define _EXT_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
  74. #define _EXT_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
  75. #define _EXT_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
  76. #define _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7
  77. #define _EXT_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
  78. #define _EXT_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B9
  79. #define _EXT_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA
  80. #define _EXT_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB
  81. #define _EXT_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC
  82. #define _EXT_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD
  83. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
  84. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1
  85. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
  86. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3
  87. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
  88. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
  89. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
  90. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7
  91. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
  92. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9
  93. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA
  94. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB
  95. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC
  96. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD
  97. #define _GL_TEXTURE_EXTERNAL_OES 0x8D65
  98. #define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
  99. enum DefaultGLTexture {
  100. DEFAULT_GL_TEXTURE_WHITE,
  101. DEFAULT_GL_TEXTURE_BLACK,
  102. DEFAULT_GL_TEXTURE_TRANSPARENT,
  103. DEFAULT_GL_TEXTURE_NORMAL,
  104. DEFAULT_GL_TEXTURE_ANISO,
  105. DEFAULT_GL_TEXTURE_DEPTH,
  106. DEFAULT_GL_TEXTURE_CUBEMAP_BLACK,
  107. //DEFAULT_GL_TEXTURE_CUBEMAP_ARRAY_BLACK, // Cubemap Arrays not supported in GL 3.3 or GL ES 3.0
  108. DEFAULT_GL_TEXTURE_CUBEMAP_WHITE,
  109. DEFAULT_GL_TEXTURE_3D_WHITE,
  110. DEFAULT_GL_TEXTURE_3D_BLACK,
  111. DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE,
  112. DEFAULT_GL_TEXTURE_2D_UINT,
  113. DEFAULT_GL_TEXTURE_MAX
  114. };
  115. struct CanvasTexture {
  116. RID diffuse;
  117. RID normal_map;
  118. RID specular;
  119. Color specular_color = Color(1, 1, 1, 1);
  120. float shininess = 1.0;
  121. RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  122. RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  123. };
  124. struct RenderTarget;
  125. struct Texture {
  126. RID self;
  127. bool is_proxy = false;
  128. bool is_external = false;
  129. bool is_render_target = false;
  130. RID proxy_to;
  131. Vector<RID> proxies;
  132. String path;
  133. int width = 0;
  134. int height = 0;
  135. int depth = 0;
  136. int mipmaps = 1;
  137. int layers = 1;
  138. int alloc_width = 0;
  139. int alloc_height = 0;
  140. Image::Format format = Image::FORMAT_R8;
  141. Image::Format real_format = Image::FORMAT_R8;
  142. enum Type {
  143. TYPE_2D,
  144. TYPE_LAYERED,
  145. TYPE_3D
  146. };
  147. Type type;
  148. RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;
  149. GLenum target = GL_TEXTURE_2D;
  150. GLenum gl_format_cache = 0;
  151. GLenum gl_internal_format_cache = 0;
  152. GLenum gl_type_cache = 0;
  153. int total_data_size = 0;
  154. bool compressed = false;
  155. bool resize_to_po2 = false;
  156. bool active = false;
  157. GLuint tex_id = 0;
  158. uint16_t stored_cube_sides = 0;
  159. RenderTarget *render_target = nullptr;
  160. Ref<Image> image_cache_2d;
  161. bool redraw_if_visible = false;
  162. RS::TextureDetectCallback detect_3d_callback = nullptr;
  163. void *detect_3d_callback_ud = nullptr;
  164. RS::TextureDetectCallback detect_normal_callback = nullptr;
  165. void *detect_normal_callback_ud = nullptr;
  166. RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;
  167. void *detect_roughness_callback_ud = nullptr;
  168. CanvasTexture *canvas_texture = nullptr;
  169. void copy_from(const Texture &o) {
  170. proxy_to = o.proxy_to;
  171. is_proxy = o.is_proxy;
  172. is_external = o.is_external;
  173. width = o.width;
  174. height = o.height;
  175. alloc_width = o.alloc_width;
  176. alloc_height = o.alloc_height;
  177. format = o.format;
  178. type = o.type;
  179. layered_type = o.layered_type;
  180. target = o.target;
  181. total_data_size = o.total_data_size;
  182. compressed = o.compressed;
  183. mipmaps = o.mipmaps;
  184. resize_to_po2 = o.resize_to_po2;
  185. active = o.active;
  186. tex_id = o.tex_id;
  187. stored_cube_sides = o.stored_cube_sides;
  188. render_target = o.render_target;
  189. is_render_target = o.is_render_target;
  190. redraw_if_visible = o.redraw_if_visible;
  191. detect_3d_callback = o.detect_3d_callback;
  192. detect_3d_callback_ud = o.detect_3d_callback_ud;
  193. detect_normal_callback = o.detect_normal_callback;
  194. detect_normal_callback_ud = o.detect_normal_callback_ud;
  195. detect_roughness_callback = o.detect_roughness_callback;
  196. detect_roughness_callback_ud = o.detect_roughness_callback_ud;
  197. }
  198. // texture state
  199. void gl_set_filter(RS::CanvasItemTextureFilter p_filter) {
  200. if (p_filter == state_filter) {
  201. return;
  202. }
  203. Config *config = Config::get_singleton();
  204. state_filter = p_filter;
  205. GLenum pmin = GL_NEAREST;
  206. GLenum pmag = GL_NEAREST;
  207. GLint max_lod = 0;
  208. GLfloat anisotropy = 1.0f;
  209. switch (state_filter) {
  210. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  211. pmin = GL_NEAREST;
  212. pmag = GL_NEAREST;
  213. max_lod = 0;
  214. } break;
  215. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  216. pmin = GL_LINEAR;
  217. pmag = GL_LINEAR;
  218. max_lod = 0;
  219. } break;
  220. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  221. anisotropy = config->anisotropic_level;
  222. };
  223. [[fallthrough]];
  224. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  225. pmag = GL_NEAREST;
  226. if (mipmaps <= 1) {
  227. pmin = GL_NEAREST;
  228. max_lod = 0;
  229. } else if (config->use_nearest_mip_filter) {
  230. pmin = GL_NEAREST_MIPMAP_NEAREST;
  231. max_lod = 1000;
  232. } else {
  233. pmin = GL_NEAREST_MIPMAP_LINEAR;
  234. max_lod = 1000;
  235. }
  236. } break;
  237. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  238. anisotropy = config->anisotropic_level;
  239. };
  240. [[fallthrough]];
  241. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  242. pmag = GL_LINEAR;
  243. if (mipmaps <= 1) {
  244. pmin = GL_LINEAR;
  245. max_lod = 0;
  246. } else if (config->use_nearest_mip_filter) {
  247. pmin = GL_LINEAR_MIPMAP_NEAREST;
  248. max_lod = 1000;
  249. } else {
  250. pmin = GL_LINEAR_MIPMAP_LINEAR;
  251. max_lod = 1000;
  252. }
  253. } break;
  254. default: {
  255. return;
  256. } break;
  257. }
  258. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, pmin);
  259. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, pmag);
  260. glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
  261. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, max_lod);
  262. if (config->support_anisotropic_filter) {
  263. glTexParameterf(target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
  264. }
  265. }
  266. void gl_set_repeat(RS::CanvasItemTextureRepeat p_repeat) {
  267. if (p_repeat == state_repeat) {
  268. return;
  269. }
  270. state_repeat = p_repeat;
  271. GLenum prep = GL_CLAMP_TO_EDGE;
  272. switch (state_repeat) {
  273. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  274. prep = GL_CLAMP_TO_EDGE;
  275. } break;
  276. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  277. prep = GL_REPEAT;
  278. } break;
  279. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  280. prep = GL_MIRRORED_REPEAT;
  281. } break;
  282. default: {
  283. return;
  284. } break;
  285. }
  286. glTexParameteri(target, GL_TEXTURE_WRAP_T, prep);
  287. glTexParameteri(target, GL_TEXTURE_WRAP_R, prep);
  288. glTexParameteri(target, GL_TEXTURE_WRAP_S, prep);
  289. }
  290. private:
  291. RS::CanvasItemTextureFilter state_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
  292. RS::CanvasItemTextureRepeat state_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
  293. };
  294. struct RenderTarget {
  295. Point2i position = Point2i(0, 0);
  296. Size2i size = Size2i(0, 0);
  297. uint32_t view_count = 1;
  298. int mipmap_count = 1;
  299. RID self;
  300. GLuint fbo = 0;
  301. GLuint color = 0;
  302. GLuint depth = 0;
  303. GLuint backbuffer_fbo = 0;
  304. GLuint backbuffer = 0;
  305. GLuint backbuffer_depth = 0;
  306. GLuint color_internal_format = GL_RGBA8;
  307. GLuint color_format = GL_RGBA;
  308. GLuint color_type = GL_UNSIGNED_BYTE;
  309. Image::Format image_format = Image::FORMAT_RGBA8;
  310. GLuint sdf_texture_write = 0;
  311. GLuint sdf_texture_write_fb = 0;
  312. GLuint sdf_texture_process[2] = { 0, 0 };
  313. GLuint sdf_texture_read = 0;
  314. RS::ViewportSDFOversize sdf_oversize = RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT;
  315. RS::ViewportSDFScale sdf_scale = RS::VIEWPORT_SDF_SCALE_50_PERCENT;
  316. Size2i process_size;
  317. bool sdf_enabled = false;
  318. bool is_transparent = false;
  319. bool direct_to_screen = false;
  320. bool used_in_frame = false;
  321. RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED;
  322. struct RTOverridden {
  323. bool is_overridden = false;
  324. RID color;
  325. RID depth;
  326. RID velocity;
  327. struct FBOCacheEntry {
  328. GLuint fbo;
  329. GLuint color;
  330. GLuint depth;
  331. Size2i size;
  332. Vector<GLuint> allocated_textures;
  333. };
  334. RBMap<uint32_t, FBOCacheEntry> fbo_cache;
  335. } overridden;
  336. RID texture;
  337. Color clear_color = Color(1, 1, 1, 1);
  338. bool clear_requested = false;
  339. RenderTarget() {
  340. }
  341. };
  342. class TextureStorage : public RendererTextureStorage {
  343. private:
  344. static TextureStorage *singleton;
  345. RID default_gl_textures[DEFAULT_GL_TEXTURE_MAX];
  346. /* Canvas Texture API */
  347. RID_Owner<CanvasTexture, true> canvas_texture_owner;
  348. /* Texture API */
  349. // Textures can be created from threads, so this RID_Owner is thread safe.
  350. mutable RID_Owner<Texture, true> texture_owner;
  351. Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, 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;
  352. /* TEXTURE ATLAS API */
  353. struct TextureAtlas {
  354. struct Texture {
  355. int users;
  356. Rect2 uv_rect;
  357. };
  358. struct SortItem {
  359. RID texture;
  360. Size2i pixel_size;
  361. Size2i size;
  362. Point2i pos;
  363. bool operator<(const SortItem &p_item) const {
  364. //sort larger to smaller
  365. if (size.height == p_item.size.height) {
  366. return size.width > p_item.size.width;
  367. } else {
  368. return size.height > p_item.size.height;
  369. }
  370. }
  371. };
  372. HashMap<RID, Texture> textures;
  373. bool dirty = true;
  374. GLuint texture = 0;
  375. GLuint framebuffer = 0;
  376. Size2i size;
  377. } texture_atlas;
  378. /* Render Target API */
  379. mutable RID_Owner<RenderTarget> render_target_owner;
  380. void _clear_render_target(RenderTarget *rt);
  381. void _update_render_target(RenderTarget *rt);
  382. void _create_render_target_backbuffer(RenderTarget *rt);
  383. void _render_target_allocate_sdf(RenderTarget *rt);
  384. void _render_target_clear_sdf(RenderTarget *rt);
  385. Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;
  386. void _texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer, bool initialize);
  387. struct RenderTargetSDF {
  388. CanvasSdfShaderGLES3 shader;
  389. RID shader_version;
  390. } sdf_shader;
  391. public:
  392. static TextureStorage *get_singleton();
  393. TextureStorage();
  394. virtual ~TextureStorage();
  395. _FORCE_INLINE_ RID texture_gl_get_default(DefaultGLTexture p_texture) {
  396. return default_gl_textures[p_texture];
  397. }
  398. /* Canvas Texture API */
  399. CanvasTexture *get_canvas_texture(RID p_rid) { return canvas_texture_owner.get_or_null(p_rid); };
  400. bool owns_canvas_texture(RID p_rid) { return canvas_texture_owner.owns(p_rid); };
  401. virtual RID canvas_texture_allocate() override;
  402. virtual void canvas_texture_initialize(RID p_rid) override;
  403. virtual void canvas_texture_free(RID p_rid) override;
  404. virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;
  405. virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override;
  406. virtual void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override;
  407. virtual void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override;
  408. /* Texture API */
  409. Texture *get_texture(RID p_rid) {
  410. Texture *texture = texture_owner.get_or_null(p_rid);
  411. if (texture && texture->is_proxy) {
  412. return texture_owner.get_or_null(texture->proxy_to);
  413. }
  414. return texture;
  415. };
  416. bool owns_texture(RID p_rid) { return texture_owner.owns(p_rid); };
  417. virtual bool can_create_resources_async() const override;
  418. virtual RID texture_allocate() override;
  419. virtual void texture_free(RID p_rid) override;
  420. virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;
  421. virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;
  422. virtual 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;
  423. virtual void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent
  424. RID texture_create_external(Texture::Type p_type, Image::Format p_format, unsigned int p_image, int p_width, int p_height, int p_depth, int p_layers, RS::TextureLayeredType p_layered_type = RS::TEXTURE_LAYERED_2D_ARRAY);
  425. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;
  426. virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override{};
  427. virtual void texture_proxy_update(RID p_proxy, RID p_base) override;
  428. //these two APIs can be used together or in combination with the others.
  429. virtual void texture_2d_placeholder_initialize(RID p_texture) override;
  430. virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;
  431. virtual void texture_3d_placeholder_initialize(RID p_texture) override;
  432. virtual Ref<Image> texture_2d_get(RID p_texture) const override;
  433. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override { return Ref<Image>(); };
  434. virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const override { return Vector<Ref<Image>>(); };
  435. virtual void texture_replace(RID p_texture, RID p_by_texture) override;
  436. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) override;
  437. virtual void texture_set_path(RID p_texture, const String &p_path) override;
  438. virtual String texture_get_path(RID p_texture) const override;
  439. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  440. void texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  441. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  442. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override;
  443. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info) override;
  444. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;
  445. virtual Size2 texture_size_with_proxy(RID p_proxy) override;
  446. virtual void texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY) override;
  447. virtual RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) const override;
  448. virtual uint64_t texture_get_native_handle(RID p_texture, bool p_srgb = false) const override;
  449. void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  450. virtual Image::Format texture_get_format(RID p_texture) const override;
  451. uint32_t texture_get_texid(RID p_texture) const;
  452. uint32_t texture_get_width(RID p_texture) const;
  453. uint32_t texture_get_height(RID p_texture) const;
  454. uint32_t texture_get_depth(RID p_texture) const;
  455. void texture_bind(RID p_texture, uint32_t p_texture_no);
  456. /* TEXTURE ATLAS API */
  457. void update_texture_atlas();
  458. GLuint texture_atlas_get_texture() const;
  459. _FORCE_INLINE_ Rect2 texture_atlas_get_texture_rect(RID p_texture) {
  460. TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);
  461. if (!t) {
  462. return Rect2();
  463. }
  464. return t->uv_rect;
  465. }
  466. void texture_add_to_texture_atlas(RID p_texture);
  467. void texture_remove_from_texture_atlas(RID p_texture);
  468. void texture_atlas_mark_dirty_on_texture(RID p_texture);
  469. void texture_atlas_remove_texture(RID p_texture);
  470. /* DECAL API */
  471. virtual RID decal_allocate() override;
  472. virtual void decal_initialize(RID p_rid) override;
  473. virtual void decal_free(RID p_rid) override{};
  474. virtual void decal_set_size(RID p_decal, const Vector3 &p_size) override;
  475. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;
  476. virtual void decal_set_emission_energy(RID p_decal, float p_energy) override;
  477. virtual void decal_set_albedo_mix(RID p_decal, float p_mix) override;
  478. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate) override;
  479. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;
  480. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;
  481. virtual void decal_set_fade(RID p_decal, float p_above, float p_below) override;
  482. virtual void decal_set_normal_fade(RID p_decal, float p_fade) override;
  483. virtual AABB decal_get_aabb(RID p_decal) const override;
  484. virtual uint32_t decal_get_cull_mask(RID p_decal) const override { return 0; }
  485. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
  486. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
  487. /* DECAL INSTANCE */
  488. virtual RID decal_instance_create(RID p_decal) override { return RID(); }
  489. virtual void decal_instance_free(RID p_decal_instance) override {}
  490. virtual void decal_instance_set_transform(RID p_decal, const Transform3D &p_transform) override {}
  491. virtual void decal_instance_set_sorting_offset(RID p_decal_instance, float p_sorting_offset) override {}
  492. /* RENDER TARGET API */
  493. static GLuint system_fbo;
  494. RenderTarget *get_render_target(RID p_rid) { return render_target_owner.get_or_null(p_rid); };
  495. bool owns_render_target(RID p_rid) { return render_target_owner.owns(p_rid); };
  496. void copy_scene_to_backbuffer(RenderTarget *rt, const bool uses_screen_texture, const bool uses_depth_texture);
  497. virtual RID render_target_create() override;
  498. virtual void render_target_free(RID p_rid) override;
  499. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override;
  500. virtual Point2i render_target_get_position(RID p_render_target) const override;
  501. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
  502. virtual Size2i render_target_get_size(RID p_render_target) const override;
  503. virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;
  504. virtual bool render_target_get_transparent(RID p_render_target) const override;
  505. virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;
  506. virtual bool render_target_get_direct_to_screen(RID p_render_target) const override;
  507. virtual bool render_target_was_used(RID p_render_target) const override;
  508. void render_target_clear_used(RID p_render_target);
  509. virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override;
  510. virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override;
  511. virtual void render_target_set_msaa_needs_resolve(RID p_render_target, bool p_needs_resolve) override {}
  512. virtual bool render_target_get_msaa_needs_resolve(RID p_render_target) const override { return false; }
  513. virtual void render_target_do_msaa_resolve(RID p_render_target) override {}
  514. virtual void render_target_set_use_hdr(RID p_render_target, bool p_use_hdr_2d) override {}
  515. virtual bool render_target_is_using_hdr(RID p_render_target) const override { return false; }
  516. // new
  517. void render_target_set_as_unused(RID p_render_target) override {
  518. render_target_clear_used(p_render_target);
  519. }
  520. void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;
  521. bool render_target_is_clear_requested(RID p_render_target) override;
  522. Color render_target_get_clear_request_color(RID p_render_target) override;
  523. void render_target_disable_clear_request(RID p_render_target) override;
  524. void render_target_do_clear_request(RID p_render_target) override;
  525. virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
  526. virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
  527. GLuint render_target_get_sdf_texture(RID p_render_target);
  528. GLuint render_target_get_sdf_framebuffer(RID p_render_target);
  529. void render_target_sdf_process(RID p_render_target);
  530. virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;
  531. bool render_target_is_sdf_enabled(RID p_render_target) const;
  532. void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);
  533. void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);
  534. void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);
  535. virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override {}
  536. virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override { return RS::VIEWPORT_VRS_DISABLED; }
  537. virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override {}
  538. virtual RID render_target_get_vrs_texture(RID p_render_target) const override { return RID(); }
  539. virtual void render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) override;
  540. virtual RID render_target_get_override_color(RID p_render_target) const override;
  541. virtual RID render_target_get_override_depth(RID p_render_target) const override;
  542. virtual RID render_target_get_override_velocity(RID p_render_target) const override;
  543. virtual RID render_target_get_texture(RID p_render_target) override;
  544. void bind_framebuffer(GLuint framebuffer) {
  545. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  546. }
  547. void bind_framebuffer_system() {
  548. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  549. }
  550. String get_framebuffer_error(GLenum p_status);
  551. };
  552. inline String TextureStorage::get_framebuffer_error(GLenum p_status) {
  553. #if defined(DEBUG_ENABLED) && defined(GL_API_ENABLED)
  554. if (p_status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
  555. return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
  556. } else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) {
  557. return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
  558. } else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER) {
  559. return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER";
  560. } else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER) {
  561. return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER";
  562. }
  563. #endif
  564. return itos(p_status);
  565. }
  566. } // namespace GLES3
  567. #endif // GLES3_ENABLED
  568. #endif // TEXTURE_STORAGE_GLES3_H