texture_storage.h 27 KB

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