texture_storage.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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_RD_H
  31. #define TEXTURE_STORAGE_RD_H
  32. #include "core/templates/paged_array.h"
  33. #include "core/templates/rid_owner.h"
  34. #include "servers/rendering/renderer_rd/shaders/canvas_sdf.glsl.gen.h"
  35. #include "servers/rendering/renderer_rd/storage_rd/forward_id_storage.h"
  36. #include "servers/rendering/rendering_server_default.h"
  37. #include "servers/rendering/storage/texture_storage.h"
  38. #include "servers/rendering/storage/utilities.h"
  39. namespace RendererRD {
  40. class LightStorage;
  41. class MaterialStorage;
  42. class TextureStorage : public RendererTextureStorage {
  43. public:
  44. enum DefaultRDTexture {
  45. DEFAULT_RD_TEXTURE_WHITE,
  46. DEFAULT_RD_TEXTURE_BLACK,
  47. DEFAULT_RD_TEXTURE_TRANSPARENT,
  48. DEFAULT_RD_TEXTURE_NORMAL,
  49. DEFAULT_RD_TEXTURE_ANISO,
  50. DEFAULT_RD_TEXTURE_DEPTH,
  51. DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER,
  52. DEFAULT_RD_TEXTURE_CUBEMAP_BLACK,
  53. DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK,
  54. DEFAULT_RD_TEXTURE_CUBEMAP_WHITE,
  55. DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_WHITE,
  56. DEFAULT_RD_TEXTURE_3D_WHITE,
  57. DEFAULT_RD_TEXTURE_3D_BLACK,
  58. DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE,
  59. DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK,
  60. DEFAULT_RD_TEXTURE_2D_ARRAY_NORMAL,
  61. DEFAULT_RD_TEXTURE_2D_ARRAY_DEPTH,
  62. DEFAULT_RD_TEXTURE_2D_UINT,
  63. DEFAULT_RD_TEXTURE_VRS,
  64. DEFAULT_RD_TEXTURE_MAX
  65. };
  66. enum TextureType {
  67. TYPE_2D,
  68. TYPE_LAYERED,
  69. TYPE_3D
  70. };
  71. struct CanvasTextureInfo {
  72. RID diffuse;
  73. RID normal;
  74. RID specular;
  75. RID sampler;
  76. Size2i size;
  77. Color specular_color;
  78. bool use_normal = false;
  79. bool use_specular = false;
  80. _FORCE_INLINE_ bool is_valid() const { return diffuse.is_valid(); }
  81. _FORCE_INLINE_ bool is_null() const { return diffuse.is_null(); }
  82. };
  83. typedef void (*InvalidationCallback)(bool p_deleted, void *p_userdata);
  84. private:
  85. friend class LightStorage;
  86. friend class MaterialStorage;
  87. static TextureStorage *singleton;
  88. RID default_rd_textures[DEFAULT_RD_TEXTURE_MAX];
  89. /* Canvas Texture API */
  90. struct CanvasTextureCache {
  91. RID diffuse;
  92. RID normal;
  93. RID specular;
  94. };
  95. class CanvasTexture {
  96. public:
  97. RID diffuse;
  98. RID normal_map;
  99. RID specular;
  100. Color specular_color = Color(1, 1, 1, 1);
  101. float shininess = 1.0;
  102. RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  103. RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  104. CanvasTextureCache info_cache[2];
  105. InvalidationCallback invalidated_callback = nullptr;
  106. void *invalidated_callback_userdata = nullptr;
  107. Size2i size_cache = Size2i(1, 1);
  108. bool use_normal_cache = false;
  109. bool use_specular_cache = false;
  110. void clear_cache();
  111. ~CanvasTexture();
  112. };
  113. RID_Owner<CanvasTexture, true> canvas_texture_owner;
  114. /* Texture API */
  115. struct RenderTarget;
  116. class Texture {
  117. public:
  118. TextureType type;
  119. RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;
  120. RenderingDevice::TextureType rd_type;
  121. RID rd_texture;
  122. RID rd_texture_srgb;
  123. RenderingDevice::DataFormat rd_format;
  124. RenderingDevice::DataFormat rd_format_srgb;
  125. RD::TextureView rd_view;
  126. Image::Format format;
  127. Image::Format validated_format;
  128. int width;
  129. int height;
  130. int depth;
  131. int layers;
  132. int mipmaps;
  133. int height_2d;
  134. int width_2d;
  135. struct BufferSlice3D {
  136. Size2i size;
  137. uint32_t offset = 0;
  138. uint32_t buffer_size = 0;
  139. };
  140. Vector<BufferSlice3D> buffer_slices_3d;
  141. uint32_t buffer_size_3d = 0;
  142. RenderTarget *render_target = nullptr;
  143. bool is_render_target;
  144. bool is_proxy;
  145. Ref<Image> image_cache_2d;
  146. String path;
  147. RID proxy_to;
  148. Vector<RID> proxies;
  149. HashSet<RID> lightmap_users;
  150. RS::TextureDetectCallback detect_3d_callback = nullptr;
  151. void *detect_3d_callback_ud = nullptr;
  152. RS::TextureDetectCallback detect_normal_callback = nullptr;
  153. void *detect_normal_callback_ud = nullptr;
  154. RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;
  155. void *detect_roughness_callback_ud = nullptr;
  156. CanvasTexture *canvas_texture = nullptr;
  157. void cleanup();
  158. };
  159. // Textures can be created from threads, so this RID_Owner is thread safe.
  160. mutable RID_Owner<Texture, true> texture_owner;
  161. Texture *get_texture(RID p_rid) { return texture_owner.get_or_null(p_rid); }
  162. struct TextureToRDFormat {
  163. RD::DataFormat format;
  164. RD::DataFormat format_srgb;
  165. RD::TextureSwizzle swizzle_r;
  166. RD::TextureSwizzle swizzle_g;
  167. RD::TextureSwizzle swizzle_b;
  168. RD::TextureSwizzle swizzle_a;
  169. TextureToRDFormat() {
  170. format = RD::DATA_FORMAT_MAX;
  171. format_srgb = RD::DATA_FORMAT_MAX;
  172. swizzle_r = RD::TEXTURE_SWIZZLE_R;
  173. swizzle_g = RD::TEXTURE_SWIZZLE_G;
  174. swizzle_b = RD::TEXTURE_SWIZZLE_B;
  175. swizzle_a = RD::TEXTURE_SWIZZLE_A;
  176. }
  177. };
  178. Ref<Image> _validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format);
  179. void _texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0, bool p_immediate = false);
  180. struct TextureFromRDFormat {
  181. Image::Format image_format;
  182. RD::DataFormat rd_format;
  183. RD::DataFormat rd_format_srgb;
  184. RD::TextureSwizzle swizzle_r;
  185. RD::TextureSwizzle swizzle_g;
  186. RD::TextureSwizzle swizzle_b;
  187. RD::TextureSwizzle swizzle_a;
  188. TextureFromRDFormat() {
  189. image_format = Image::FORMAT_MAX;
  190. rd_format = RD::DATA_FORMAT_MAX;
  191. rd_format_srgb = RD::DATA_FORMAT_MAX;
  192. swizzle_r = RD::TEXTURE_SWIZZLE_R;
  193. swizzle_g = RD::TEXTURE_SWIZZLE_G;
  194. swizzle_b = RD::TEXTURE_SWIZZLE_B;
  195. swizzle_a = RD::TEXTURE_SWIZZLE_A;
  196. }
  197. };
  198. void _texture_format_from_rd(RD::DataFormat p_rd_format, TextureFromRDFormat &r_format);
  199. /* DECAL API */
  200. struct DecalAtlas {
  201. struct Texture {
  202. int panorama_to_dp_users;
  203. int users;
  204. Rect2 uv_rect;
  205. };
  206. struct SortItem {
  207. RID texture;
  208. Size2i pixel_size;
  209. Size2i size;
  210. Point2i pos;
  211. bool operator<(const SortItem &p_item) const {
  212. //sort larger to smaller
  213. if (size.height == p_item.size.height) {
  214. return size.width > p_item.size.width;
  215. } else {
  216. return size.height > p_item.size.height;
  217. }
  218. }
  219. };
  220. HashMap<RID, Texture> textures;
  221. bool dirty = true;
  222. int mipmaps = 5;
  223. RID texture;
  224. RID texture_srgb;
  225. struct MipMap {
  226. RID fb;
  227. RID texture;
  228. Size2i size;
  229. };
  230. Vector<MipMap> texture_mipmaps;
  231. Size2i size;
  232. } decal_atlas;
  233. struct Decal {
  234. Vector3 size = Vector3(2, 2, 2);
  235. RID textures[RS::DECAL_TEXTURE_MAX];
  236. float emission_energy = 1.0;
  237. float albedo_mix = 1.0;
  238. Color modulate = Color(1, 1, 1, 1);
  239. uint32_t cull_mask = (1 << 20) - 1;
  240. float upper_fade = 0.3;
  241. float lower_fade = 0.3;
  242. bool distance_fade = false;
  243. float distance_fade_begin = 40.0;
  244. float distance_fade_length = 10.0;
  245. float normal_fade = 0.0;
  246. Dependency dependency;
  247. };
  248. mutable RID_Owner<Decal, true> decal_owner;
  249. /* DECAL INSTANCE */
  250. struct DecalInstance {
  251. RID decal;
  252. Transform3D transform;
  253. float sorting_offset = 0.0;
  254. uint32_t cull_mask = 0;
  255. RendererRD::ForwardID forward_id = -1;
  256. };
  257. mutable RID_Owner<DecalInstance> decal_instance_owner;
  258. /* DECAL DATA (UBO) */
  259. struct DecalData {
  260. float xform[16];
  261. float inv_extents[3];
  262. float albedo_mix;
  263. float albedo_rect[4];
  264. float normal_rect[4];
  265. float orm_rect[4];
  266. float emission_rect[4];
  267. float modulate[4];
  268. float emission_energy;
  269. uint32_t mask;
  270. float upper_fade;
  271. float lower_fade;
  272. float normal_xform[12];
  273. float normal[3];
  274. float normal_fade;
  275. };
  276. struct DecalInstanceSort {
  277. float depth;
  278. DecalInstance *decal_instance;
  279. Decal *decal;
  280. bool operator<(const DecalInstanceSort &p_sort) const {
  281. return depth < p_sort.depth;
  282. }
  283. };
  284. uint32_t max_decals = 0;
  285. uint32_t decal_count = 0;
  286. DecalData *decals = nullptr;
  287. DecalInstanceSort *decal_sort = nullptr;
  288. RID decal_buffer;
  289. /* RENDER TARGET API */
  290. struct RenderTarget {
  291. Size2i size;
  292. uint32_t view_count;
  293. RID color;
  294. Vector<RID> color_slices;
  295. RID color_multisample; // Needed when 2D MSAA is enabled.
  296. RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED; // 2D MSAA mode
  297. bool msaa_needs_resolve = false; // 2D MSAA needs resolved
  298. //used for retrieving from CPU
  299. RD::DataFormat color_format = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  300. RD::DataFormat color_format_srgb = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  301. Image::Format image_format = Image::FORMAT_L8;
  302. bool is_transparent = false;
  303. bool use_hdr = false;
  304. bool sdf_enabled = false;
  305. RID backbuffer; //used for effects
  306. RID backbuffer_fb;
  307. RID backbuffer_mipmap0;
  308. Vector<RID> backbuffer_mipmaps;
  309. RID framebuffer_uniform_set;
  310. RID backbuffer_uniform_set;
  311. RID sdf_buffer_write;
  312. RID sdf_buffer_write_fb;
  313. RID sdf_buffer_process[2];
  314. RID sdf_buffer_read;
  315. RID sdf_buffer_process_uniform_sets[2];
  316. RS::ViewportSDFOversize sdf_oversize = RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT;
  317. RS::ViewportSDFScale sdf_scale = RS::VIEWPORT_SDF_SCALE_50_PERCENT;
  318. Size2i process_size;
  319. // VRS
  320. RS::ViewportVRSMode vrs_mode = RS::VIEWPORT_VRS_DISABLED;
  321. RS::ViewportVRSUpdateMode vrs_update_mode = RS::VIEWPORT_VRS_UPDATE_ONCE;
  322. RID vrs_texture;
  323. Rect2i render_region;
  324. // overridden textures
  325. struct RTOverridden {
  326. RID color;
  327. RID depth;
  328. RID velocity;
  329. // In a multiview scenario, which is the most likely where we
  330. // override our destination textures, we need to obtain slices
  331. // for each layer of these textures.
  332. // These are likely changing every frame as we loop through
  333. // texture chains hence we add a cache to manage these slices.
  334. // For this we define a key using the RID of the texture and
  335. // the layer for which we create a slice.
  336. struct SliceKey {
  337. RID rid;
  338. uint32_t layer = 0;
  339. bool operator==(const SliceKey &p_val) const {
  340. return (rid == p_val.rid) && (layer == p_val.layer);
  341. }
  342. static uint32_t hash(const SliceKey &p_val) {
  343. uint32_t h = hash_one_uint64(p_val.rid.get_id());
  344. h = hash_murmur3_one_32(p_val.layer, h);
  345. return hash_fmix32(h);
  346. }
  347. SliceKey() {}
  348. SliceKey(RID p_rid, uint32_t p_layer) {
  349. rid = p_rid;
  350. layer = p_layer;
  351. }
  352. };
  353. mutable HashMap<SliceKey, RID, SliceKey> cached_slices;
  354. } overridden;
  355. //texture generated for this owner (nor RD).
  356. RID texture;
  357. bool was_used;
  358. //clear request
  359. bool clear_requested;
  360. Color clear_color;
  361. RID get_framebuffer();
  362. };
  363. mutable RID_Owner<RenderTarget> render_target_owner;
  364. RenderTarget *get_render_target(RID p_rid) const { return render_target_owner.get_or_null(p_rid); }
  365. void _clear_render_target(RenderTarget *rt);
  366. void _update_render_target(RenderTarget *rt);
  367. void _create_render_target_backbuffer(RenderTarget *rt);
  368. void _render_target_allocate_sdf(RenderTarget *rt);
  369. void _render_target_clear_sdf(RenderTarget *rt);
  370. Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;
  371. struct RenderTargetSDF {
  372. enum {
  373. SHADER_LOAD,
  374. SHADER_LOAD_SHRINK,
  375. SHADER_PROCESS,
  376. SHADER_PROCESS_OPTIMIZED,
  377. SHADER_STORE,
  378. SHADER_STORE_SHRINK,
  379. SHADER_MAX
  380. };
  381. struct PushConstant {
  382. int32_t size[2];
  383. int32_t stride;
  384. int32_t shift;
  385. int32_t base_size[2];
  386. int32_t pad[2];
  387. };
  388. CanvasSdfShaderRD shader;
  389. RID shader_version;
  390. RID pipelines[SHADER_MAX];
  391. } rt_sdf;
  392. public:
  393. static TextureStorage *get_singleton();
  394. _FORCE_INLINE_ RID texture_rd_get_default(DefaultRDTexture p_texture) {
  395. return default_rd_textures[p_texture];
  396. }
  397. TextureStorage();
  398. virtual ~TextureStorage();
  399. bool free(RID p_rid);
  400. /* Canvas Texture API */
  401. bool owns_canvas_texture(RID p_rid) { return canvas_texture_owner.owns(p_rid); }
  402. virtual RID canvas_texture_allocate() override;
  403. virtual void canvas_texture_initialize(RID p_rid) override;
  404. virtual void canvas_texture_free(RID p_rid) override;
  405. virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;
  406. virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override;
  407. virtual void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override;
  408. virtual void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override;
  409. CanvasTextureInfo canvas_texture_get_info(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, bool p_use_srgb, bool p_texture_is_data);
  410. void canvas_texture_set_invalidation_callback(RID p_canvas_texture, InvalidationCallback p_callback, void *p_userdata);
  411. /* Texture API */
  412. bool owns_texture(RID p_rid) const { return texture_owner.owns(p_rid); }
  413. virtual RID texture_allocate() override;
  414. virtual void texture_free(RID p_rid) override;
  415. virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;
  416. virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;
  417. 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;
  418. virtual void texture_external_initialize(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) override;
  419. virtual void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent
  420. virtual RID texture_create_from_native_handle(RS::TextureType p_type, Image::Format p_format, uint64_t p_native_handle, int p_width, int p_height, int p_depth, int p_layers = 1, RS::TextureLayeredType p_layered_type = RS::TEXTURE_LAYERED_2D_ARRAY) override;
  421. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;
  422. virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override;
  423. virtual void texture_external_update(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) override;
  424. virtual void texture_proxy_update(RID p_proxy, RID p_base) override;
  425. Ref<Image> texture_2d_placeholder;
  426. Vector<Ref<Image>> texture_2d_array_placeholder;
  427. Vector<Ref<Image>> cubemap_placeholder;
  428. Vector<Ref<Image>> texture_3d_placeholder;
  429. //these two APIs can be used together or in combination with the others.
  430. virtual void texture_2d_placeholder_initialize(RID p_texture) override;
  431. virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;
  432. virtual void texture_3d_placeholder_initialize(RID p_texture) override;
  433. virtual Ref<Image> texture_2d_get(RID p_texture) const override;
  434. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override;
  435. virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const override;
  436. virtual void texture_replace(RID p_texture, RID p_by_texture) override;
  437. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) override;
  438. virtual void texture_set_path(RID p_texture, const String &p_path) override;
  439. virtual String texture_get_path(RID p_texture) const override;
  440. virtual Image::Format texture_get_format(RID p_texture) const override;
  441. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  442. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  443. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override;
  444. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info) override;
  445. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;
  446. virtual Size2 texture_size_with_proxy(RID p_proxy) override;
  447. 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;
  448. virtual RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) const override;
  449. virtual uint64_t texture_get_native_handle(RID p_texture, bool p_srgb = false) const override;
  450. //internal usage
  451. _FORCE_INLINE_ TextureType texture_get_type(RID p_texture) {
  452. RendererRD::TextureStorage::Texture *tex = texture_owner.get_or_null(p_texture);
  453. if (tex == nullptr) {
  454. return TYPE_2D;
  455. }
  456. return tex->type;
  457. }
  458. _FORCE_INLINE_ int texture_get_layers(RID p_texture) {
  459. RendererRD::TextureStorage::Texture *tex = texture_owner.get_or_null(p_texture);
  460. if (tex == nullptr) {
  461. return 1;
  462. }
  463. return tex->layers;
  464. }
  465. _FORCE_INLINE_ Size2i texture_2d_get_size(RID p_texture) {
  466. if (p_texture.is_null()) {
  467. return Size2i();
  468. }
  469. RendererRD::TextureStorage::Texture *tex = texture_owner.get_or_null(p_texture);
  470. if (!tex) {
  471. return Size2i();
  472. }
  473. return Size2i(tex->width_2d, tex->height_2d);
  474. }
  475. /* DECAL API */
  476. void update_decal_atlas();
  477. bool owns_decal(RID p_rid) const { return decal_owner.owns(p_rid); }
  478. RID decal_atlas_get_texture() const;
  479. RID decal_atlas_get_texture_srgb() const;
  480. _FORCE_INLINE_ Rect2 decal_atlas_get_texture_rect(RID p_texture) {
  481. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  482. if (!t) {
  483. return Rect2();
  484. }
  485. return t->uv_rect;
  486. }
  487. virtual RID decal_allocate() override;
  488. virtual void decal_initialize(RID p_decal) override;
  489. virtual void decal_free(RID p_rid) override;
  490. virtual void decal_set_size(RID p_decal, const Vector3 &p_size) override;
  491. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;
  492. virtual void decal_set_emission_energy(RID p_decal, float p_energy) override;
  493. virtual void decal_set_albedo_mix(RID p_decal, float p_mix) override;
  494. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate) override;
  495. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;
  496. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;
  497. virtual void decal_set_fade(RID p_decal, float p_above, float p_below) override;
  498. virtual void decal_set_normal_fade(RID p_decal, float p_fade) override;
  499. void decal_atlas_mark_dirty_on_texture(RID p_texture);
  500. void decal_atlas_remove_texture(RID p_texture);
  501. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override;
  502. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override;
  503. _FORCE_INLINE_ Vector3 decal_get_size(RID p_decal) {
  504. const Decal *decal = decal_owner.get_or_null(p_decal);
  505. return decal->size;
  506. }
  507. _FORCE_INLINE_ RID decal_get_texture(RID p_decal, RS::DecalTexture p_texture) {
  508. const Decal *decal = decal_owner.get_or_null(p_decal);
  509. return decal->textures[p_texture];
  510. }
  511. _FORCE_INLINE_ Color decal_get_modulate(RID p_decal) {
  512. const Decal *decal = decal_owner.get_or_null(p_decal);
  513. return decal->modulate;
  514. }
  515. _FORCE_INLINE_ float decal_get_emission_energy(RID p_decal) {
  516. const Decal *decal = decal_owner.get_or_null(p_decal);
  517. return decal->emission_energy;
  518. }
  519. _FORCE_INLINE_ float decal_get_albedo_mix(RID p_decal) {
  520. const Decal *decal = decal_owner.get_or_null(p_decal);
  521. return decal->albedo_mix;
  522. }
  523. _FORCE_INLINE_ uint32_t decal_get_cull_mask(RID p_decal) {
  524. const Decal *decal = decal_owner.get_or_null(p_decal);
  525. return decal->cull_mask;
  526. }
  527. _FORCE_INLINE_ float decal_get_upper_fade(RID p_decal) {
  528. const Decal *decal = decal_owner.get_or_null(p_decal);
  529. return decal->upper_fade;
  530. }
  531. _FORCE_INLINE_ float decal_get_lower_fade(RID p_decal) {
  532. const Decal *decal = decal_owner.get_or_null(p_decal);
  533. return decal->lower_fade;
  534. }
  535. _FORCE_INLINE_ float decal_get_normal_fade(RID p_decal) {
  536. const Decal *decal = decal_owner.get_or_null(p_decal);
  537. return decal->normal_fade;
  538. }
  539. _FORCE_INLINE_ bool decal_is_distance_fade_enabled(RID p_decal) {
  540. const Decal *decal = decal_owner.get_or_null(p_decal);
  541. return decal->distance_fade;
  542. }
  543. _FORCE_INLINE_ float decal_get_distance_fade_begin(RID p_decal) {
  544. const Decal *decal = decal_owner.get_or_null(p_decal);
  545. return decal->distance_fade_begin;
  546. }
  547. _FORCE_INLINE_ float decal_get_distance_fade_length(RID p_decal) {
  548. const Decal *decal = decal_owner.get_or_null(p_decal);
  549. return decal->distance_fade_length;
  550. }
  551. virtual AABB decal_get_aabb(RID p_decal) const override;
  552. virtual uint32_t decal_get_cull_mask(RID p_decal) const override;
  553. Dependency *decal_get_dependency(RID p_decal);
  554. /* DECAL INSTANCE API */
  555. bool owns_decal_instance(RID p_rid) const { return decal_instance_owner.owns(p_rid); }
  556. virtual RID decal_instance_create(RID p_decal) override;
  557. virtual void decal_instance_free(RID p_decal_instance) override;
  558. virtual void decal_instance_set_transform(RID p_decal_instance, const Transform3D &p_transform) override;
  559. virtual void decal_instance_set_sorting_offset(RID p_decal_instance, float p_sorting_offset) override;
  560. _FORCE_INLINE_ RID decal_instance_get_base(RID p_decal_instance) const {
  561. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  562. return di->decal;
  563. }
  564. _FORCE_INLINE_ RendererRD::ForwardID decal_instance_get_forward_id(RID p_decal_instance) const {
  565. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  566. return di->forward_id;
  567. }
  568. _FORCE_INLINE_ Transform3D decal_instance_get_transform(RID p_decal_instance) const {
  569. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  570. return di->transform;
  571. }
  572. _FORCE_INLINE_ ForwardID decal_instance_get_forward_id(RID p_decal_instance) {
  573. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  574. return di->forward_id;
  575. }
  576. _FORCE_INLINE_ void decal_instance_set_cullmask(RID p_decal_instance, uint32_t p_cull_mask) const {
  577. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  578. di->cull_mask = p_cull_mask;
  579. }
  580. /* DECAL DATA API */
  581. void free_decal_data();
  582. void set_max_decals(const uint32_t p_max_decals);
  583. RID get_decal_buffer() { return decal_buffer; }
  584. void update_decal_buffer(const PagedArray<RID> &p_decals, const Transform3D &p_camera_xform);
  585. /* RENDER TARGET API */
  586. bool owns_render_target(RID p_rid) const { return render_target_owner.owns(p_rid); }
  587. virtual RID render_target_create() override;
  588. virtual void render_target_free(RID p_rid) override;
  589. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override;
  590. virtual Point2i render_target_get_position(RID p_render_target) const override;
  591. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
  592. virtual Size2i render_target_get_size(RID p_render_target) const override;
  593. virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;
  594. virtual bool render_target_get_transparent(RID p_render_target) const override;
  595. virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;
  596. virtual bool render_target_get_direct_to_screen(RID p_render_target) const override;
  597. virtual bool render_target_was_used(RID p_render_target) const override;
  598. virtual void render_target_set_as_unused(RID p_render_target) override;
  599. virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override;
  600. virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override;
  601. virtual void render_target_set_msaa_needs_resolve(RID p_render_target, bool p_needs_resolve) override;
  602. virtual bool render_target_get_msaa_needs_resolve(RID p_render_target) const override;
  603. virtual void render_target_do_msaa_resolve(RID p_render_target) override;
  604. virtual void render_target_set_use_hdr(RID p_render_target, bool p_use_hdr) override;
  605. virtual bool render_target_is_using_hdr(RID p_render_target) const override;
  606. void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);
  607. void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);
  608. void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);
  609. RID render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader);
  610. virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;
  611. virtual bool render_target_is_clear_requested(RID p_render_target) override;
  612. virtual Color render_target_get_clear_request_color(RID p_render_target) override;
  613. virtual void render_target_disable_clear_request(RID p_render_target) override;
  614. virtual void render_target_do_clear_request(RID p_render_target) override;
  615. virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
  616. RID render_target_get_sdf_texture(RID p_render_target);
  617. RID render_target_get_sdf_framebuffer(RID p_render_target);
  618. void render_target_sdf_process(RID p_render_target);
  619. virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
  620. virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;
  621. bool render_target_is_sdf_enabled(RID p_render_target) const;
  622. virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override;
  623. virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override;
  624. virtual void render_target_set_vrs_update_mode(RID p_render_target, RS::ViewportVRSUpdateMode p_mode) override;
  625. virtual RS::ViewportVRSUpdateMode render_target_get_vrs_update_mode(RID p_render_target) const override;
  626. virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override;
  627. virtual RID render_target_get_vrs_texture(RID p_render_target) const override;
  628. virtual void render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture, RID p_velocity_depth_texture) override;
  629. virtual RID render_target_get_override_color(RID p_render_target) const override;
  630. virtual RID render_target_get_override_depth(RID p_render_target) const override;
  631. RID render_target_get_override_depth_slice(RID p_render_target, const uint32_t p_layer) const;
  632. virtual RID render_target_get_override_velocity(RID p_render_target) const override;
  633. RID render_target_get_override_velocity_slice(RID p_render_target, const uint32_t p_layer) const;
  634. virtual RID render_target_get_override_velocity_depth(RID p_render_target) const override { return RID(); }
  635. virtual void render_target_set_render_region(RID p_render_target, const Rect2i &p_render_region) override;
  636. virtual Rect2i render_target_get_render_region(RID p_render_target) const override;
  637. virtual RID render_target_get_texture(RID p_render_target) override;
  638. virtual void render_target_set_velocity_target_size(RID p_render_target, const Size2i &p_target_size) override {}
  639. virtual Size2i render_target_get_velocity_target_size(RID p_render_target) const override { return Size2i(0, 0); }
  640. RID render_target_get_rd_framebuffer(RID p_render_target);
  641. RID render_target_get_rd_texture(RID p_render_target);
  642. RID render_target_get_rd_texture_slice(RID p_render_target, uint32_t p_layer);
  643. RID render_target_get_rd_texture_msaa(RID p_render_target);
  644. RID render_target_get_rd_backbuffer(RID p_render_target);
  645. RID render_target_get_rd_backbuffer_framebuffer(RID p_render_target);
  646. RID render_target_get_framebuffer_uniform_set(RID p_render_target);
  647. RID render_target_get_backbuffer_uniform_set(RID p_render_target);
  648. void render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  649. void render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  650. static RD::DataFormat render_target_get_color_format(bool p_use_hdr, bool p_srgb);
  651. static uint32_t render_target_get_color_usage_bits(bool p_msaa);
  652. };
  653. } // namespace RendererRD
  654. #endif // TEXTURE_STORAGE_RD_H