texture_storage.h 28 KB

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