texture_storage.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. RID vrs_texture;
  304. // overridden textures
  305. struct RTOverridden {
  306. RID color;
  307. RID depth;
  308. RID velocity;
  309. // In a multiview scenario, which is the most likely where we
  310. // override our destination textures, we need to obtain slices
  311. // for each layer of these textures.
  312. // These are likely changing every frame as we loop through
  313. // texture chains hence we add a cache to manage these slices.
  314. // For this we define a key using the RID of the texture and
  315. // the layer for which we create a slice.
  316. struct SliceKey {
  317. RID rid;
  318. uint32_t layer = 0;
  319. bool operator==(const SliceKey &p_val) const {
  320. return (rid == p_val.rid) && (layer == p_val.layer);
  321. }
  322. static uint32_t hash(const SliceKey &p_val) {
  323. uint32_t h = hash_one_uint64(p_val.rid.get_id());
  324. h = hash_murmur3_one_32(p_val.layer, h);
  325. return hash_fmix32(h);
  326. }
  327. SliceKey() {}
  328. SliceKey(RID p_rid, uint32_t p_layer) {
  329. rid = p_rid;
  330. layer = p_layer;
  331. }
  332. };
  333. mutable HashMap<SliceKey, RID, SliceKey> cached_slices;
  334. } overridden;
  335. //texture generated for this owner (nor RD).
  336. RID texture;
  337. bool was_used;
  338. //clear request
  339. bool clear_requested;
  340. Color clear_color;
  341. RID get_framebuffer();
  342. };
  343. mutable RID_Owner<RenderTarget> render_target_owner;
  344. RenderTarget *get_render_target(RID p_rid) const { return render_target_owner.get_or_null(p_rid); };
  345. void _clear_render_target(RenderTarget *rt);
  346. void _update_render_target(RenderTarget *rt);
  347. void _create_render_target_backbuffer(RenderTarget *rt);
  348. void _render_target_allocate_sdf(RenderTarget *rt);
  349. void _render_target_clear_sdf(RenderTarget *rt);
  350. Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;
  351. struct RenderTargetSDF {
  352. enum {
  353. SHADER_LOAD,
  354. SHADER_LOAD_SHRINK,
  355. SHADER_PROCESS,
  356. SHADER_PROCESS_OPTIMIZED,
  357. SHADER_STORE,
  358. SHADER_STORE_SHRINK,
  359. SHADER_MAX
  360. };
  361. struct PushConstant {
  362. int32_t size[2];
  363. int32_t stride;
  364. int32_t shift;
  365. int32_t base_size[2];
  366. int32_t pad[2];
  367. };
  368. CanvasSdfShaderRD shader;
  369. RID shader_version;
  370. RID pipelines[SHADER_MAX];
  371. } rt_sdf;
  372. public:
  373. static TextureStorage *get_singleton();
  374. _FORCE_INLINE_ RID texture_rd_get_default(DefaultRDTexture p_texture) {
  375. return default_rd_textures[p_texture];
  376. }
  377. TextureStorage();
  378. virtual ~TextureStorage();
  379. bool free(RID p_rid);
  380. /* Canvas Texture API */
  381. bool owns_canvas_texture(RID p_rid) { return canvas_texture_owner.owns(p_rid); };
  382. virtual RID canvas_texture_allocate() override;
  383. virtual void canvas_texture_initialize(RID p_rid) override;
  384. virtual void canvas_texture_free(RID p_rid) override;
  385. virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;
  386. virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override;
  387. virtual void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override;
  388. virtual void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override;
  389. 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);
  390. /* Texture API */
  391. bool owns_texture(RID p_rid) const { return texture_owner.owns(p_rid); };
  392. virtual bool can_create_resources_async() const override;
  393. virtual RID texture_allocate() override;
  394. virtual void texture_free(RID p_rid) override;
  395. virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;
  396. virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;
  397. 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;
  398. virtual void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent
  399. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;
  400. virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override;
  401. virtual void texture_proxy_update(RID p_proxy, RID p_base) override;
  402. //these two APIs can be used together or in combination with the others.
  403. virtual void texture_2d_placeholder_initialize(RID p_texture) override;
  404. virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;
  405. virtual void texture_3d_placeholder_initialize(RID p_texture) override;
  406. virtual Ref<Image> texture_2d_get(RID p_texture) const override;
  407. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override;
  408. virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const override;
  409. virtual void texture_replace(RID p_texture, RID p_by_texture) override;
  410. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) override;
  411. virtual void texture_set_path(RID p_texture, const String &p_path) override;
  412. virtual String texture_get_path(RID p_texture) const override;
  413. virtual Image::Format texture_get_format(RID p_texture) const override;
  414. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  415. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  416. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override;
  417. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info) override;
  418. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;
  419. virtual Size2 texture_size_with_proxy(RID p_proxy) override;
  420. 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;
  421. virtual RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) const override;
  422. virtual uint64_t texture_get_native_handle(RID p_texture, bool p_srgb = false) const override;
  423. //internal usage
  424. _FORCE_INLINE_ TextureType texture_get_type(RID p_texture) {
  425. RendererRD::TextureStorage::Texture *tex = texture_owner.get_or_null(p_texture);
  426. if (tex == nullptr) {
  427. return TYPE_2D;
  428. }
  429. return tex->type;
  430. }
  431. _FORCE_INLINE_ int texture_get_layers(RID p_texture) {
  432. RendererRD::TextureStorage::Texture *tex = texture_owner.get_or_null(p_texture);
  433. if (tex == nullptr) {
  434. return 1;
  435. }
  436. return tex->layers;
  437. }
  438. _FORCE_INLINE_ Size2i texture_2d_get_size(RID p_texture) {
  439. if (p_texture.is_null()) {
  440. return Size2i();
  441. }
  442. RendererRD::TextureStorage::Texture *tex = texture_owner.get_or_null(p_texture);
  443. if (!tex) {
  444. return Size2i();
  445. }
  446. return Size2i(tex->width_2d, tex->height_2d);
  447. }
  448. /* DECAL API */
  449. void update_decal_atlas();
  450. bool owns_decal(RID p_rid) const { return decal_owner.owns(p_rid); };
  451. RID decal_atlas_get_texture() const;
  452. RID decal_atlas_get_texture_srgb() const;
  453. _FORCE_INLINE_ Rect2 decal_atlas_get_texture_rect(RID p_texture) {
  454. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  455. if (!t) {
  456. return Rect2();
  457. }
  458. return t->uv_rect;
  459. }
  460. virtual RID decal_allocate() override;
  461. virtual void decal_initialize(RID p_decal) override;
  462. virtual void decal_free(RID p_rid) override;
  463. virtual void decal_set_size(RID p_decal, const Vector3 &p_size) override;
  464. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;
  465. virtual void decal_set_emission_energy(RID p_decal, float p_energy) override;
  466. virtual void decal_set_albedo_mix(RID p_decal, float p_mix) override;
  467. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate) override;
  468. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;
  469. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;
  470. virtual void decal_set_fade(RID p_decal, float p_above, float p_below) override;
  471. virtual void decal_set_normal_fade(RID p_decal, float p_fade) override;
  472. void decal_atlas_mark_dirty_on_texture(RID p_texture);
  473. void decal_atlas_remove_texture(RID p_texture);
  474. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override;
  475. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override;
  476. _FORCE_INLINE_ Vector3 decal_get_size(RID p_decal) {
  477. const Decal *decal = decal_owner.get_or_null(p_decal);
  478. return decal->size;
  479. }
  480. _FORCE_INLINE_ RID decal_get_texture(RID p_decal, RS::DecalTexture p_texture) {
  481. const Decal *decal = decal_owner.get_or_null(p_decal);
  482. return decal->textures[p_texture];
  483. }
  484. _FORCE_INLINE_ Color decal_get_modulate(RID p_decal) {
  485. const Decal *decal = decal_owner.get_or_null(p_decal);
  486. return decal->modulate;
  487. }
  488. _FORCE_INLINE_ float decal_get_emission_energy(RID p_decal) {
  489. const Decal *decal = decal_owner.get_or_null(p_decal);
  490. return decal->emission_energy;
  491. }
  492. _FORCE_INLINE_ float decal_get_albedo_mix(RID p_decal) {
  493. const Decal *decal = decal_owner.get_or_null(p_decal);
  494. return decal->albedo_mix;
  495. }
  496. _FORCE_INLINE_ uint32_t decal_get_cull_mask(RID p_decal) {
  497. const Decal *decal = decal_owner.get_or_null(p_decal);
  498. return decal->cull_mask;
  499. }
  500. _FORCE_INLINE_ float decal_get_upper_fade(RID p_decal) {
  501. const Decal *decal = decal_owner.get_or_null(p_decal);
  502. return decal->upper_fade;
  503. }
  504. _FORCE_INLINE_ float decal_get_lower_fade(RID p_decal) {
  505. const Decal *decal = decal_owner.get_or_null(p_decal);
  506. return decal->lower_fade;
  507. }
  508. _FORCE_INLINE_ float decal_get_normal_fade(RID p_decal) {
  509. const Decal *decal = decal_owner.get_or_null(p_decal);
  510. return decal->normal_fade;
  511. }
  512. _FORCE_INLINE_ bool decal_is_distance_fade_enabled(RID p_decal) {
  513. const Decal *decal = decal_owner.get_or_null(p_decal);
  514. return decal->distance_fade;
  515. }
  516. _FORCE_INLINE_ float decal_get_distance_fade_begin(RID p_decal) {
  517. const Decal *decal = decal_owner.get_or_null(p_decal);
  518. return decal->distance_fade_begin;
  519. }
  520. _FORCE_INLINE_ float decal_get_distance_fade_length(RID p_decal) {
  521. const Decal *decal = decal_owner.get_or_null(p_decal);
  522. return decal->distance_fade_length;
  523. }
  524. virtual AABB decal_get_aabb(RID p_decal) const override;
  525. virtual uint32_t decal_get_cull_mask(RID p_decal) const override;
  526. Dependency *decal_get_dependency(RID p_decal);
  527. /* DECAL INSTANCE API */
  528. bool owns_decal_instance(RID p_rid) const { return decal_instance_owner.owns(p_rid); }
  529. virtual RID decal_instance_create(RID p_decal) override;
  530. virtual void decal_instance_free(RID p_decal_instance) override;
  531. virtual void decal_instance_set_transform(RID p_decal_instance, const Transform3D &p_transform) override;
  532. virtual void decal_instance_set_sorting_offset(RID p_decal_instance, float p_sorting_offset) override;
  533. _FORCE_INLINE_ RID decal_instance_get_base(RID p_decal_instance) const {
  534. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  535. return di->decal;
  536. }
  537. _FORCE_INLINE_ RendererRD::ForwardID decal_instance_get_forward_id(RID p_decal_instance) const {
  538. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  539. return di->forward_id;
  540. }
  541. _FORCE_INLINE_ Transform3D decal_instance_get_transform(RID p_decal_instance) const {
  542. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  543. return di->transform;
  544. }
  545. _FORCE_INLINE_ ForwardID decal_instance_get_forward_id(RID p_decal_instance) {
  546. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  547. return di->forward_id;
  548. }
  549. _FORCE_INLINE_ void decal_instance_set_cullmask(RID p_decal_instance, uint32_t p_cull_mask) const {
  550. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  551. di->cull_mask = p_cull_mask;
  552. }
  553. /* DECAL DATA API */
  554. void free_decal_data();
  555. void set_max_decals(const uint32_t p_max_decals);
  556. RID get_decal_buffer() { return decal_buffer; }
  557. void update_decal_buffer(const PagedArray<RID> &p_decals, const Transform3D &p_camera_xform);
  558. /* RENDER TARGET API */
  559. bool owns_render_target(RID p_rid) const { return render_target_owner.owns(p_rid); };
  560. virtual RID render_target_create() override;
  561. virtual void render_target_free(RID p_rid) override;
  562. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override;
  563. virtual Point2i render_target_get_position(RID p_render_target) const override;
  564. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
  565. virtual Size2i render_target_get_size(RID p_render_target) const override;
  566. virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;
  567. virtual bool render_target_get_transparent(RID p_render_target) const override;
  568. virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;
  569. virtual bool render_target_get_direct_to_screen(RID p_render_target) const override;
  570. virtual bool render_target_was_used(RID p_render_target) const override;
  571. virtual void render_target_set_as_unused(RID p_render_target) override;
  572. virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override;
  573. virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override;
  574. virtual void render_target_set_msaa_needs_resolve(RID p_render_target, bool p_needs_resolve) override;
  575. virtual bool render_target_get_msaa_needs_resolve(RID p_render_target) const override;
  576. virtual void render_target_do_msaa_resolve(RID p_render_target) override;
  577. virtual void render_target_set_use_hdr(RID p_render_target, bool p_use_hdr) override;
  578. virtual bool render_target_is_using_hdr(RID p_render_target) const override;
  579. void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);
  580. void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);
  581. void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);
  582. RID render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader);
  583. virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;
  584. virtual bool render_target_is_clear_requested(RID p_render_target) override;
  585. virtual Color render_target_get_clear_request_color(RID p_render_target) override;
  586. virtual void render_target_disable_clear_request(RID p_render_target) override;
  587. virtual void render_target_do_clear_request(RID p_render_target) override;
  588. virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
  589. RID render_target_get_sdf_texture(RID p_render_target);
  590. RID render_target_get_sdf_framebuffer(RID p_render_target);
  591. void render_target_sdf_process(RID p_render_target);
  592. virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
  593. virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;
  594. bool render_target_is_sdf_enabled(RID p_render_target) const;
  595. virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override;
  596. virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override;
  597. virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override;
  598. virtual RID render_target_get_vrs_texture(RID p_render_target) const override;
  599. virtual void render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) override;
  600. virtual RID render_target_get_override_color(RID p_render_target) const override;
  601. virtual RID render_target_get_override_depth(RID p_render_target) const override;
  602. RID render_target_get_override_depth_slice(RID p_render_target, const uint32_t p_layer) const;
  603. virtual RID render_target_get_override_velocity(RID p_render_target) const override;
  604. RID render_target_get_override_velocity_slice(RID p_render_target, const uint32_t p_layer) const;
  605. virtual RID render_target_get_texture(RID p_render_target) override;
  606. RID render_target_get_rd_framebuffer(RID p_render_target);
  607. RID render_target_get_rd_texture(RID p_render_target);
  608. RID render_target_get_rd_texture_slice(RID p_render_target, uint32_t p_layer);
  609. RID render_target_get_rd_texture_msaa(RID p_render_target);
  610. RID render_target_get_rd_backbuffer(RID p_render_target);
  611. RID render_target_get_rd_backbuffer_framebuffer(RID p_render_target);
  612. RID render_target_get_framebuffer_uniform_set(RID p_render_target);
  613. RID render_target_get_backbuffer_uniform_set(RID p_render_target);
  614. void render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  615. void render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  616. };
  617. } // namespace RendererRD
  618. #endif // TEXTURE_STORAGE_RD_H