texture_storage.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  1. /*************************************************************************/
  2. /* texture_storage.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "texture_storage.h"
  31. #include "../effects/copy_effects.h"
  32. #include "material_storage.h"
  33. using namespace RendererRD;
  34. ///////////////////////////////////////////////////////////////////////////
  35. // TextureStorage::CanvasTexture
  36. void TextureStorage::CanvasTexture::clear_sets() {
  37. if (cleared_cache) {
  38. return;
  39. }
  40. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  41. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  42. if (RD::get_singleton()->uniform_set_is_valid(uniform_sets[i][j])) {
  43. RD::get_singleton()->free(uniform_sets[i][j]);
  44. uniform_sets[i][j] = RID();
  45. }
  46. }
  47. }
  48. cleared_cache = true;
  49. }
  50. TextureStorage::CanvasTexture::~CanvasTexture() {
  51. clear_sets();
  52. }
  53. ///////////////////////////////////////////////////////////////////////////
  54. // TextureStorage::Texture
  55. void TextureStorage::Texture::cleanup() {
  56. if (RD::get_singleton()->texture_is_valid(rd_texture_srgb)) {
  57. //erase this first, as it's a dependency of the one below
  58. RD::get_singleton()->free(rd_texture_srgb);
  59. }
  60. if (RD::get_singleton()->texture_is_valid(rd_texture)) {
  61. RD::get_singleton()->free(rd_texture);
  62. }
  63. if (canvas_texture) {
  64. memdelete(canvas_texture);
  65. }
  66. }
  67. ///////////////////////////////////////////////////////////////////////////
  68. // TextureStorage
  69. TextureStorage *TextureStorage::singleton = nullptr;
  70. TextureStorage *TextureStorage::get_singleton() {
  71. return singleton;
  72. }
  73. TextureStorage::TextureStorage() {
  74. singleton = this;
  75. { //create default textures
  76. RD::TextureFormat tformat;
  77. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  78. tformat.width = 4;
  79. tformat.height = 4;
  80. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  81. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  82. Vector<uint8_t> pv;
  83. pv.resize(16 * 4);
  84. for (int i = 0; i < 16; i++) {
  85. // Opaque white.
  86. pv.set(i * 4 + 0, 255);
  87. pv.set(i * 4 + 1, 255);
  88. pv.set(i * 4 + 2, 255);
  89. pv.set(i * 4 + 3, 255);
  90. }
  91. {
  92. Vector<Vector<uint8_t>> vpv;
  93. vpv.push_back(pv);
  94. default_rd_textures[DEFAULT_RD_TEXTURE_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  95. }
  96. for (int i = 0; i < 16; i++) {
  97. // Opaque black.
  98. pv.set(i * 4 + 0, 0);
  99. pv.set(i * 4 + 1, 0);
  100. pv.set(i * 4 + 2, 0);
  101. pv.set(i * 4 + 3, 255);
  102. }
  103. {
  104. Vector<Vector<uint8_t>> vpv;
  105. vpv.push_back(pv);
  106. default_rd_textures[DEFAULT_RD_TEXTURE_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  107. }
  108. for (int i = 0; i < 16; i++) {
  109. // Transparent black.
  110. pv.set(i * 4 + 0, 0);
  111. pv.set(i * 4 + 1, 0);
  112. pv.set(i * 4 + 2, 0);
  113. pv.set(i * 4 + 3, 0);
  114. }
  115. {
  116. Vector<Vector<uint8_t>> vpv;
  117. vpv.push_back(pv);
  118. default_rd_textures[DEFAULT_RD_TEXTURE_TRANSPARENT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  119. }
  120. for (int i = 0; i < 16; i++) {
  121. // Opaque normal map "flat" color.
  122. pv.set(i * 4 + 0, 128);
  123. pv.set(i * 4 + 1, 128);
  124. pv.set(i * 4 + 2, 255);
  125. pv.set(i * 4 + 3, 255);
  126. }
  127. {
  128. Vector<Vector<uint8_t>> vpv;
  129. vpv.push_back(pv);
  130. default_rd_textures[DEFAULT_RD_TEXTURE_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  131. }
  132. for (int i = 0; i < 16; i++) {
  133. // Opaque flowmap "flat" color.
  134. pv.set(i * 4 + 0, 255);
  135. pv.set(i * 4 + 1, 128);
  136. pv.set(i * 4 + 2, 255);
  137. pv.set(i * 4 + 3, 255);
  138. }
  139. {
  140. Vector<Vector<uint8_t>> vpv;
  141. vpv.push_back(pv);
  142. default_rd_textures[DEFAULT_RD_TEXTURE_ANISO] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  143. }
  144. {
  145. RD::TextureFormat tf;
  146. tf.format = RD::DATA_FORMAT_D16_UNORM;
  147. tf.width = 4;
  148. tf.height = 4;
  149. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  150. tf.texture_type = RD::TEXTURE_TYPE_2D;
  151. Vector<uint8_t> sv;
  152. sv.resize(16 * 2);
  153. uint16_t *ptr = (uint16_t *)sv.ptrw();
  154. for (int i = 0; i < 16; i++) {
  155. ptr[i] = Math::make_half_float(1.0f);
  156. }
  157. Vector<Vector<uint8_t>> vpv;
  158. vpv.push_back(sv);
  159. default_rd_textures[DEFAULT_RD_TEXTURE_DEPTH] = RD::get_singleton()->texture_create(tf, RD::TextureView(), vpv);
  160. }
  161. for (int i = 0; i < 16; i++) {
  162. pv.set(i * 4 + 0, 0);
  163. pv.set(i * 4 + 1, 0);
  164. pv.set(i * 4 + 2, 0);
  165. pv.set(i * 4 + 3, 0);
  166. }
  167. default_rd_textures[DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER] = RD::get_singleton()->texture_buffer_create(16, RD::DATA_FORMAT_R8G8B8A8_UNORM, pv);
  168. for (int i = 0; i < 16; i++) {
  169. pv.set(i * 4 + 0, 0);
  170. pv.set(i * 4 + 1, 0);
  171. pv.set(i * 4 + 2, 0);
  172. pv.set(i * 4 + 3, 0);
  173. }
  174. {
  175. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  176. Vector<Vector<uint8_t>> vpv;
  177. vpv.push_back(pv);
  178. default_rd_textures[DEFAULT_RD_TEXTURE_2D_UINT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  179. }
  180. }
  181. { //create default black cubemap array
  182. RD::TextureFormat tformat;
  183. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  184. tformat.width = 4;
  185. tformat.height = 4;
  186. tformat.array_layers = 6;
  187. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  188. tformat.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  189. Vector<uint8_t> pv;
  190. pv.resize(16 * 4);
  191. for (int i = 0; i < 16; i++) {
  192. pv.set(i * 4 + 0, 0);
  193. pv.set(i * 4 + 1, 0);
  194. pv.set(i * 4 + 2, 0);
  195. pv.set(i * 4 + 3, 0);
  196. }
  197. {
  198. Vector<Vector<uint8_t>> vpv;
  199. for (int i = 0; i < 6; i++) {
  200. vpv.push_back(pv);
  201. }
  202. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  203. }
  204. }
  205. { //create default white cubemap array
  206. RD::TextureFormat tformat;
  207. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  208. tformat.width = 4;
  209. tformat.height = 4;
  210. tformat.array_layers = 6;
  211. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  212. tformat.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  213. Vector<uint8_t> pv;
  214. pv.resize(16 * 4);
  215. for (int i = 0; i < 16; i++) {
  216. pv.set(i * 4 + 0, 255);
  217. pv.set(i * 4 + 1, 255);
  218. pv.set(i * 4 + 2, 255);
  219. pv.set(i * 4 + 3, 255);
  220. }
  221. {
  222. Vector<Vector<uint8_t>> vpv;
  223. for (int i = 0; i < 6; i++) {
  224. vpv.push_back(pv);
  225. }
  226. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  227. }
  228. }
  229. { //create default black cubemap
  230. RD::TextureFormat tformat;
  231. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  232. tformat.width = 4;
  233. tformat.height = 4;
  234. tformat.array_layers = 6;
  235. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  236. tformat.texture_type = RD::TEXTURE_TYPE_CUBE;
  237. Vector<uint8_t> pv;
  238. pv.resize(16 * 4);
  239. for (int i = 0; i < 16; i++) {
  240. pv.set(i * 4 + 0, 0);
  241. pv.set(i * 4 + 1, 0);
  242. pv.set(i * 4 + 2, 0);
  243. pv.set(i * 4 + 3, 0);
  244. }
  245. {
  246. Vector<Vector<uint8_t>> vpv;
  247. for (int i = 0; i < 6; i++) {
  248. vpv.push_back(pv);
  249. }
  250. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  251. }
  252. }
  253. { //create default white cubemap
  254. RD::TextureFormat tformat;
  255. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  256. tformat.width = 4;
  257. tformat.height = 4;
  258. tformat.array_layers = 6;
  259. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  260. tformat.texture_type = RD::TEXTURE_TYPE_CUBE;
  261. Vector<uint8_t> pv;
  262. pv.resize(16 * 4);
  263. for (int i = 0; i < 16; i++) {
  264. pv.set(i * 4 + 0, 255);
  265. pv.set(i * 4 + 1, 255);
  266. pv.set(i * 4 + 2, 255);
  267. pv.set(i * 4 + 3, 255);
  268. }
  269. {
  270. Vector<Vector<uint8_t>> vpv;
  271. for (int i = 0; i < 6; i++) {
  272. vpv.push_back(pv);
  273. }
  274. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  275. }
  276. }
  277. { //create default 3D
  278. RD::TextureFormat tformat;
  279. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  280. tformat.width = 4;
  281. tformat.height = 4;
  282. tformat.depth = 4;
  283. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  284. tformat.texture_type = RD::TEXTURE_TYPE_3D;
  285. Vector<uint8_t> pv;
  286. pv.resize(64 * 4);
  287. for (int i = 0; i < 64; i++) {
  288. pv.set(i * 4 + 0, 0);
  289. pv.set(i * 4 + 1, 0);
  290. pv.set(i * 4 + 2, 0);
  291. pv.set(i * 4 + 3, 0);
  292. }
  293. {
  294. Vector<Vector<uint8_t>> vpv;
  295. vpv.push_back(pv);
  296. default_rd_textures[DEFAULT_RD_TEXTURE_3D_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  297. }
  298. for (int i = 0; i < 64; i++) {
  299. pv.set(i * 4 + 0, 255);
  300. pv.set(i * 4 + 1, 255);
  301. pv.set(i * 4 + 2, 255);
  302. pv.set(i * 4 + 3, 255);
  303. }
  304. {
  305. Vector<Vector<uint8_t>> vpv;
  306. vpv.push_back(pv);
  307. default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  308. }
  309. }
  310. { //create default array
  311. RD::TextureFormat tformat;
  312. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  313. tformat.width = 4;
  314. tformat.height = 4;
  315. tformat.array_layers = 1;
  316. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  317. tformat.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  318. Vector<uint8_t> pv;
  319. pv.resize(16 * 4);
  320. for (int i = 0; i < 16; i++) {
  321. pv.set(i * 4 + 0, 255);
  322. pv.set(i * 4 + 1, 255);
  323. pv.set(i * 4 + 2, 255);
  324. pv.set(i * 4 + 3, 255);
  325. }
  326. {
  327. Vector<Vector<uint8_t>> vpv;
  328. vpv.push_back(pv);
  329. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  330. }
  331. }
  332. { // default atlas texture
  333. RD::TextureFormat tformat;
  334. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  335. tformat.width = 4;
  336. tformat.height = 4;
  337. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  338. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  339. Vector<uint8_t> pv;
  340. pv.resize(16 * 4);
  341. for (int i = 0; i < 16; i++) {
  342. pv.set(i * 4 + 0, 0);
  343. pv.set(i * 4 + 1, 0);
  344. pv.set(i * 4 + 2, 0);
  345. pv.set(i * 4 + 3, 255);
  346. }
  347. {
  348. Vector<Vector<uint8_t>> vpv;
  349. vpv.push_back(pv);
  350. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  351. decal_atlas.texture_srgb = decal_atlas.texture;
  352. }
  353. }
  354. { //create default VRS
  355. RD::TextureFormat tformat;
  356. tformat.format = RD::DATA_FORMAT_R8_UINT;
  357. tformat.width = 4;
  358. tformat.height = 4;
  359. tformat.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_VRS_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  360. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  361. Vector<uint8_t> pv;
  362. pv.resize(4 * 4);
  363. for (int i = 0; i < 4 * 4; i++) {
  364. pv.set(i, 0);
  365. }
  366. {
  367. Vector<Vector<uint8_t>> vpv;
  368. vpv.push_back(pv);
  369. default_rd_textures[DEFAULT_RD_TEXTURE_VRS] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  370. }
  371. }
  372. {
  373. Vector<String> sdf_modes;
  374. sdf_modes.push_back("\n#define MODE_LOAD\n");
  375. sdf_modes.push_back("\n#define MODE_LOAD_SHRINK\n");
  376. sdf_modes.push_back("\n#define MODE_PROCESS\n");
  377. sdf_modes.push_back("\n#define MODE_PROCESS_OPTIMIZED\n");
  378. sdf_modes.push_back("\n#define MODE_STORE\n");
  379. sdf_modes.push_back("\n#define MODE_STORE_SHRINK\n");
  380. rt_sdf.shader.initialize(sdf_modes);
  381. rt_sdf.shader_version = rt_sdf.shader.version_create();
  382. for (int i = 0; i < RenderTargetSDF::SHADER_MAX; i++) {
  383. rt_sdf.pipelines[i] = RD::get_singleton()->compute_pipeline_create(rt_sdf.shader.version_get_shader(rt_sdf.shader_version, i));
  384. }
  385. }
  386. }
  387. TextureStorage::~TextureStorage() {
  388. rt_sdf.shader.version_free(rt_sdf.shader_version);
  389. if (decal_atlas.textures.size()) {
  390. ERR_PRINT("Decal Atlas: " + itos(decal_atlas.textures.size()) + " textures were not removed from the atlas.");
  391. }
  392. if (decal_atlas.texture.is_valid()) {
  393. RD::get_singleton()->free(decal_atlas.texture);
  394. }
  395. //def textures
  396. for (int i = 0; i < DEFAULT_RD_TEXTURE_MAX; i++) {
  397. if (default_rd_textures[i].is_valid()) {
  398. RD::get_singleton()->free(default_rd_textures[i]);
  399. }
  400. }
  401. singleton = nullptr;
  402. }
  403. bool TextureStorage::can_create_resources_async() const {
  404. return true;
  405. }
  406. /* Canvas Texture API */
  407. RID TextureStorage::canvas_texture_allocate() {
  408. return canvas_texture_owner.allocate_rid();
  409. }
  410. void TextureStorage::canvas_texture_initialize(RID p_rid) {
  411. canvas_texture_owner.initialize_rid(p_rid);
  412. }
  413. void TextureStorage::canvas_texture_free(RID p_rid) {
  414. canvas_texture_owner.free(p_rid);
  415. }
  416. void TextureStorage::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
  417. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  418. ERR_FAIL_NULL(ct);
  419. switch (p_channel) {
  420. case RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE: {
  421. ct->diffuse = p_texture;
  422. } break;
  423. case RS::CANVAS_TEXTURE_CHANNEL_NORMAL: {
  424. ct->normal_map = p_texture;
  425. } break;
  426. case RS::CANVAS_TEXTURE_CHANNEL_SPECULAR: {
  427. ct->specular = p_texture;
  428. } break;
  429. }
  430. ct->clear_sets();
  431. }
  432. void TextureStorage::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess) {
  433. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  434. ERR_FAIL_NULL(ct);
  435. ct->specular_color.r = p_specular_color.r;
  436. ct->specular_color.g = p_specular_color.g;
  437. ct->specular_color.b = p_specular_color.b;
  438. ct->specular_color.a = p_shininess;
  439. ct->clear_sets();
  440. }
  441. void TextureStorage::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
  442. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  443. ERR_FAIL_NULL(ct);
  444. ct->texture_filter = p_filter;
  445. ct->clear_sets();
  446. }
  447. void TextureStorage::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
  448. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  449. ERR_FAIL_NULL(ct);
  450. ct->texture_repeat = p_repeat;
  451. ct->clear_sets();
  452. }
  453. bool TextureStorage::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, RID &r_uniform_set, Size2i &r_size, Color &r_specular_shininess, bool &r_use_normal, bool &r_use_specular) {
  454. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  455. CanvasTexture *ct = nullptr;
  456. Texture *t = get_texture(p_texture);
  457. // TODO once we have our texture storage split off we'll look into moving this code into canvas_texture
  458. if (t) {
  459. //regular texture
  460. if (!t->canvas_texture) {
  461. t->canvas_texture = memnew(CanvasTexture);
  462. t->canvas_texture->diffuse = p_texture;
  463. }
  464. ct = t->canvas_texture;
  465. } else {
  466. ct = canvas_texture_owner.get_or_null(p_texture);
  467. }
  468. if (!ct) {
  469. return false; //invalid texture RID
  470. }
  471. RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
  472. ERR_FAIL_COND_V(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, false);
  473. RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
  474. ERR_FAIL_COND_V(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT, false);
  475. RID uniform_set = ct->uniform_sets[filter][repeat];
  476. if (!RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  477. //create and update
  478. Vector<RD::Uniform> uniforms;
  479. { //diffuse
  480. RD::Uniform u;
  481. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  482. u.binding = 0;
  483. t = get_texture(ct->diffuse);
  484. if (!t) {
  485. u.append_id(texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE));
  486. ct->size_cache = Size2i(1, 1);
  487. } else {
  488. u.append_id(t->rd_texture);
  489. ct->size_cache = Size2i(t->width_2d, t->height_2d);
  490. }
  491. uniforms.push_back(u);
  492. }
  493. { //normal
  494. RD::Uniform u;
  495. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  496. u.binding = 1;
  497. t = get_texture(ct->normal_map);
  498. if (!t) {
  499. u.append_id(texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL));
  500. ct->use_normal_cache = false;
  501. } else {
  502. u.append_id(t->rd_texture);
  503. ct->use_normal_cache = true;
  504. }
  505. uniforms.push_back(u);
  506. }
  507. { //specular
  508. RD::Uniform u;
  509. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  510. u.binding = 2;
  511. t = get_texture(ct->specular);
  512. if (!t) {
  513. u.append_id(texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE));
  514. ct->use_specular_cache = false;
  515. } else {
  516. u.append_id(t->rd_texture);
  517. ct->use_specular_cache = true;
  518. }
  519. uniforms.push_back(u);
  520. }
  521. { //sampler
  522. RD::Uniform u;
  523. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  524. u.binding = 3;
  525. u.append_id(material_storage->sampler_rd_get_default(filter, repeat));
  526. uniforms.push_back(u);
  527. }
  528. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_base_shader, p_base_set);
  529. ct->uniform_sets[filter][repeat] = uniform_set;
  530. ct->cleared_cache = false;
  531. }
  532. r_uniform_set = uniform_set;
  533. r_size = ct->size_cache;
  534. r_specular_shininess = ct->specular_color;
  535. r_use_normal = ct->use_normal_cache;
  536. r_use_specular = ct->use_specular_cache;
  537. return true;
  538. }
  539. /* Texture API */
  540. RID TextureStorage::texture_allocate() {
  541. return texture_owner.allocate_rid();
  542. }
  543. void TextureStorage::texture_free(RID p_texture) {
  544. Texture *t = texture_owner.get_or_null(p_texture);
  545. ERR_FAIL_COND(!t);
  546. ERR_FAIL_COND(t->is_render_target);
  547. t->cleanup();
  548. if (t->is_proxy && t->proxy_to.is_valid()) {
  549. Texture *proxy_to = texture_owner.get_or_null(t->proxy_to);
  550. if (proxy_to) {
  551. proxy_to->proxies.erase(p_texture);
  552. }
  553. }
  554. decal_atlas_remove_texture(p_texture);
  555. for (int i = 0; i < t->proxies.size(); i++) {
  556. Texture *p = texture_owner.get_or_null(t->proxies[i]);
  557. ERR_CONTINUE(!p);
  558. p->proxy_to = RID();
  559. p->rd_texture = RID();
  560. p->rd_texture_srgb = RID();
  561. }
  562. texture_owner.free(p_texture);
  563. }
  564. void TextureStorage::texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) {
  565. TextureToRDFormat ret_format;
  566. Ref<Image> image = _validate_texture_format(p_image, ret_format);
  567. Texture texture;
  568. texture.type = TextureStorage::TYPE_2D;
  569. texture.width = p_image->get_width();
  570. texture.height = p_image->get_height();
  571. texture.layers = 1;
  572. texture.mipmaps = p_image->get_mipmap_count() + 1;
  573. texture.depth = 1;
  574. texture.format = p_image->get_format();
  575. texture.validated_format = image->get_format();
  576. texture.rd_type = RD::TEXTURE_TYPE_2D;
  577. texture.rd_format = ret_format.format;
  578. texture.rd_format_srgb = ret_format.format_srgb;
  579. RD::TextureFormat rd_format;
  580. RD::TextureView rd_view;
  581. { //attempt register
  582. rd_format.format = texture.rd_format;
  583. rd_format.width = texture.width;
  584. rd_format.height = texture.height;
  585. rd_format.depth = 1;
  586. rd_format.array_layers = 1;
  587. rd_format.mipmaps = texture.mipmaps;
  588. rd_format.texture_type = texture.rd_type;
  589. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  590. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  591. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  592. rd_format.shareable_formats.push_back(texture.rd_format);
  593. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  594. }
  595. }
  596. {
  597. rd_view.swizzle_r = ret_format.swizzle_r;
  598. rd_view.swizzle_g = ret_format.swizzle_g;
  599. rd_view.swizzle_b = ret_format.swizzle_b;
  600. rd_view.swizzle_a = ret_format.swizzle_a;
  601. }
  602. Vector<uint8_t> data = image->get_data(); //use image data
  603. Vector<Vector<uint8_t>> data_slices;
  604. data_slices.push_back(data);
  605. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  606. ERR_FAIL_COND(texture.rd_texture.is_null());
  607. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  608. rd_view.format_override = texture.rd_format_srgb;
  609. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  610. if (texture.rd_texture_srgb.is_null()) {
  611. RD::get_singleton()->free(texture.rd_texture);
  612. ERR_FAIL_COND(texture.rd_texture_srgb.is_null());
  613. }
  614. }
  615. //used for 2D, overridable
  616. texture.width_2d = texture.width;
  617. texture.height_2d = texture.height;
  618. texture.is_render_target = false;
  619. texture.rd_view = rd_view;
  620. texture.is_proxy = false;
  621. texture_owner.initialize_rid(p_texture, texture);
  622. }
  623. void TextureStorage::texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  624. ERR_FAIL_COND(p_layers.size() == 0);
  625. ERR_FAIL_COND(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP && p_layers.size() != 6);
  626. ERR_FAIL_COND(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP_ARRAY && (p_layers.size() < 6 || (p_layers.size() % 6) != 0));
  627. TextureToRDFormat ret_format;
  628. Vector<Ref<Image>> images;
  629. {
  630. int valid_width = 0;
  631. int valid_height = 0;
  632. bool valid_mipmaps = false;
  633. Image::Format valid_format = Image::FORMAT_MAX;
  634. for (int i = 0; i < p_layers.size(); i++) {
  635. ERR_FAIL_COND(p_layers[i]->is_empty());
  636. if (i == 0) {
  637. valid_width = p_layers[i]->get_width();
  638. valid_height = p_layers[i]->get_height();
  639. valid_format = p_layers[i]->get_format();
  640. valid_mipmaps = p_layers[i]->has_mipmaps();
  641. } else {
  642. ERR_FAIL_COND(p_layers[i]->get_width() != valid_width);
  643. ERR_FAIL_COND(p_layers[i]->get_height() != valid_height);
  644. ERR_FAIL_COND(p_layers[i]->get_format() != valid_format);
  645. ERR_FAIL_COND(p_layers[i]->has_mipmaps() != valid_mipmaps);
  646. }
  647. images.push_back(_validate_texture_format(p_layers[i], ret_format));
  648. }
  649. }
  650. Texture texture;
  651. texture.type = TextureStorage::TYPE_LAYERED;
  652. texture.layered_type = p_layered_type;
  653. texture.width = p_layers[0]->get_width();
  654. texture.height = p_layers[0]->get_height();
  655. texture.layers = p_layers.size();
  656. texture.mipmaps = p_layers[0]->get_mipmap_count() + 1;
  657. texture.depth = 1;
  658. texture.format = p_layers[0]->get_format();
  659. texture.validated_format = images[0]->get_format();
  660. switch (p_layered_type) {
  661. case RS::TEXTURE_LAYERED_2D_ARRAY: {
  662. texture.rd_type = RD::TEXTURE_TYPE_2D_ARRAY;
  663. } break;
  664. case RS::TEXTURE_LAYERED_CUBEMAP: {
  665. texture.rd_type = RD::TEXTURE_TYPE_CUBE;
  666. } break;
  667. case RS::TEXTURE_LAYERED_CUBEMAP_ARRAY: {
  668. texture.rd_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  669. } break;
  670. }
  671. texture.rd_format = ret_format.format;
  672. texture.rd_format_srgb = ret_format.format_srgb;
  673. RD::TextureFormat rd_format;
  674. RD::TextureView rd_view;
  675. { //attempt register
  676. rd_format.format = texture.rd_format;
  677. rd_format.width = texture.width;
  678. rd_format.height = texture.height;
  679. rd_format.depth = 1;
  680. rd_format.array_layers = texture.layers;
  681. rd_format.mipmaps = texture.mipmaps;
  682. rd_format.texture_type = texture.rd_type;
  683. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  684. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  685. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  686. rd_format.shareable_formats.push_back(texture.rd_format);
  687. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  688. }
  689. }
  690. {
  691. rd_view.swizzle_r = ret_format.swizzle_r;
  692. rd_view.swizzle_g = ret_format.swizzle_g;
  693. rd_view.swizzle_b = ret_format.swizzle_b;
  694. rd_view.swizzle_a = ret_format.swizzle_a;
  695. }
  696. Vector<Vector<uint8_t>> data_slices;
  697. for (int i = 0; i < images.size(); i++) {
  698. Vector<uint8_t> data = images[i]->get_data(); //use image data
  699. data_slices.push_back(data);
  700. }
  701. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  702. ERR_FAIL_COND(texture.rd_texture.is_null());
  703. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  704. rd_view.format_override = texture.rd_format_srgb;
  705. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  706. if (texture.rd_texture_srgb.is_null()) {
  707. RD::get_singleton()->free(texture.rd_texture);
  708. ERR_FAIL_COND(texture.rd_texture_srgb.is_null());
  709. }
  710. }
  711. //used for 2D, overridable
  712. texture.width_2d = texture.width;
  713. texture.height_2d = texture.height;
  714. texture.is_render_target = false;
  715. texture.rd_view = rd_view;
  716. texture.is_proxy = false;
  717. texture_owner.initialize_rid(p_texture, texture);
  718. }
  719. void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
  720. ERR_FAIL_COND(p_data.size() == 0);
  721. Image::Image3DValidateError verr = Image::validate_3d_image(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
  722. if (verr != Image::VALIDATE_3D_OK) {
  723. ERR_FAIL_MSG(Image::get_3d_image_validation_error_text(verr));
  724. }
  725. TextureToRDFormat ret_format;
  726. Image::Format validated_format = Image::FORMAT_MAX;
  727. Vector<uint8_t> all_data;
  728. uint32_t mipmap_count = 0;
  729. Vector<Texture::BufferSlice3D> slices;
  730. {
  731. Vector<Ref<Image>> images;
  732. uint32_t all_data_size = 0;
  733. images.resize(p_data.size());
  734. for (int i = 0; i < p_data.size(); i++) {
  735. TextureToRDFormat f;
  736. images.write[i] = _validate_texture_format(p_data[i], f);
  737. if (i == 0) {
  738. ret_format = f;
  739. validated_format = images[0]->get_format();
  740. }
  741. all_data_size += images[i]->get_data().size();
  742. }
  743. all_data.resize(all_data_size); //consolidate all data here
  744. uint32_t offset = 0;
  745. Size2i prev_size;
  746. for (int i = 0; i < p_data.size(); i++) {
  747. uint32_t s = images[i]->get_data().size();
  748. memcpy(&all_data.write[offset], images[i]->get_data().ptr(), s);
  749. {
  750. Texture::BufferSlice3D slice;
  751. slice.size.width = images[i]->get_width();
  752. slice.size.height = images[i]->get_height();
  753. slice.offset = offset;
  754. slice.buffer_size = s;
  755. slices.push_back(slice);
  756. }
  757. offset += s;
  758. Size2i img_size(images[i]->get_width(), images[i]->get_height());
  759. if (img_size != prev_size) {
  760. mipmap_count++;
  761. }
  762. prev_size = img_size;
  763. }
  764. }
  765. Texture texture;
  766. texture.type = TextureStorage::TYPE_3D;
  767. texture.width = p_width;
  768. texture.height = p_height;
  769. texture.depth = p_depth;
  770. texture.mipmaps = mipmap_count;
  771. texture.format = p_data[0]->get_format();
  772. texture.validated_format = validated_format;
  773. texture.buffer_size_3d = all_data.size();
  774. texture.buffer_slices_3d = slices;
  775. texture.rd_type = RD::TEXTURE_TYPE_3D;
  776. texture.rd_format = ret_format.format;
  777. texture.rd_format_srgb = ret_format.format_srgb;
  778. RD::TextureFormat rd_format;
  779. RD::TextureView rd_view;
  780. { //attempt register
  781. rd_format.format = texture.rd_format;
  782. rd_format.width = texture.width;
  783. rd_format.height = texture.height;
  784. rd_format.depth = texture.depth;
  785. rd_format.array_layers = 1;
  786. rd_format.mipmaps = texture.mipmaps;
  787. rd_format.texture_type = texture.rd_type;
  788. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  789. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  790. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  791. rd_format.shareable_formats.push_back(texture.rd_format);
  792. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  793. }
  794. }
  795. {
  796. rd_view.swizzle_r = ret_format.swizzle_r;
  797. rd_view.swizzle_g = ret_format.swizzle_g;
  798. rd_view.swizzle_b = ret_format.swizzle_b;
  799. rd_view.swizzle_a = ret_format.swizzle_a;
  800. }
  801. Vector<Vector<uint8_t>> data_slices;
  802. data_slices.push_back(all_data); //one slice
  803. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  804. ERR_FAIL_COND(texture.rd_texture.is_null());
  805. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  806. rd_view.format_override = texture.rd_format_srgb;
  807. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  808. if (texture.rd_texture_srgb.is_null()) {
  809. RD::get_singleton()->free(texture.rd_texture);
  810. ERR_FAIL_COND(texture.rd_texture_srgb.is_null());
  811. }
  812. }
  813. //used for 2D, overridable
  814. texture.width_2d = texture.width;
  815. texture.height_2d = texture.height;
  816. texture.is_render_target = false;
  817. texture.rd_view = rd_view;
  818. texture.is_proxy = false;
  819. texture_owner.initialize_rid(p_texture, texture);
  820. }
  821. void TextureStorage::texture_proxy_initialize(RID p_texture, RID p_base) {
  822. Texture *tex = texture_owner.get_or_null(p_base);
  823. ERR_FAIL_COND(!tex);
  824. Texture proxy_tex = *tex;
  825. proxy_tex.rd_view.format_override = tex->rd_format;
  826. proxy_tex.rd_texture = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  827. if (proxy_tex.rd_texture_srgb.is_valid()) {
  828. proxy_tex.rd_view.format_override = tex->rd_format_srgb;
  829. proxy_tex.rd_texture_srgb = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  830. }
  831. proxy_tex.proxy_to = p_base;
  832. proxy_tex.is_render_target = false;
  833. proxy_tex.is_proxy = true;
  834. proxy_tex.proxies.clear();
  835. texture_owner.initialize_rid(p_texture, proxy_tex);
  836. tex->proxies.push_back(p_texture);
  837. }
  838. void TextureStorage::_texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate) {
  839. ERR_FAIL_COND(p_image.is_null() || p_image->is_empty());
  840. Texture *tex = texture_owner.get_or_null(p_texture);
  841. ERR_FAIL_COND(!tex);
  842. ERR_FAIL_COND(tex->is_render_target);
  843. ERR_FAIL_COND(p_image->get_width() != tex->width || p_image->get_height() != tex->height);
  844. ERR_FAIL_COND(p_image->get_format() != tex->format);
  845. if (tex->type == TextureStorage::TYPE_LAYERED) {
  846. ERR_FAIL_INDEX(p_layer, tex->layers);
  847. }
  848. #ifdef TOOLS_ENABLED
  849. tex->image_cache_2d.unref();
  850. #endif
  851. TextureToRDFormat f;
  852. Ref<Image> validated = _validate_texture_format(p_image, f);
  853. RD::get_singleton()->texture_update(tex->rd_texture, p_layer, validated->get_data());
  854. }
  855. void TextureStorage::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  856. _texture_2d_update(p_texture, p_image, p_layer, false);
  857. }
  858. void TextureStorage::texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) {
  859. Texture *tex = texture_owner.get_or_null(p_texture);
  860. ERR_FAIL_COND(!tex);
  861. ERR_FAIL_COND(tex->type != TextureStorage::TYPE_3D);
  862. Image::Image3DValidateError verr = Image::validate_3d_image(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps > 1, p_data);
  863. if (verr != Image::VALIDATE_3D_OK) {
  864. ERR_FAIL_MSG(Image::get_3d_image_validation_error_text(verr));
  865. }
  866. Vector<uint8_t> all_data;
  867. {
  868. Vector<Ref<Image>> images;
  869. uint32_t all_data_size = 0;
  870. images.resize(p_data.size());
  871. for (int i = 0; i < p_data.size(); i++) {
  872. Ref<Image> image = p_data[i];
  873. if (image->get_format() != tex->validated_format) {
  874. image = image->duplicate();
  875. image->convert(tex->validated_format);
  876. }
  877. all_data_size += images[i]->get_data().size();
  878. images.push_back(image);
  879. }
  880. all_data.resize(all_data_size); //consolidate all data here
  881. uint32_t offset = 0;
  882. for (int i = 0; i < p_data.size(); i++) {
  883. uint32_t s = images[i]->get_data().size();
  884. memcpy(&all_data.write[offset], images[i]->get_data().ptr(), s);
  885. offset += s;
  886. }
  887. }
  888. RD::get_singleton()->texture_update(tex->rd_texture, 0, all_data);
  889. }
  890. void TextureStorage::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  891. Texture *tex = texture_owner.get_or_null(p_texture);
  892. ERR_FAIL_COND(!tex);
  893. ERR_FAIL_COND(!tex->is_proxy);
  894. Texture *proxy_to = texture_owner.get_or_null(p_proxy_to);
  895. ERR_FAIL_COND(!proxy_to);
  896. ERR_FAIL_COND(proxy_to->is_proxy);
  897. if (tex->proxy_to.is_valid()) {
  898. //unlink proxy
  899. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  900. RD::get_singleton()->free(tex->rd_texture);
  901. tex->rd_texture = RID();
  902. }
  903. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  904. RD::get_singleton()->free(tex->rd_texture_srgb);
  905. tex->rd_texture_srgb = RID();
  906. }
  907. Texture *prev_tex = texture_owner.get_or_null(tex->proxy_to);
  908. ERR_FAIL_COND(!prev_tex);
  909. prev_tex->proxies.erase(p_texture);
  910. }
  911. *tex = *proxy_to;
  912. tex->proxy_to = p_proxy_to;
  913. tex->is_render_target = false;
  914. tex->is_proxy = true;
  915. tex->proxies.clear();
  916. proxy_to->proxies.push_back(p_texture);
  917. tex->rd_view.format_override = tex->rd_format;
  918. tex->rd_texture = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  919. if (tex->rd_texture_srgb.is_valid()) {
  920. tex->rd_view.format_override = tex->rd_format_srgb;
  921. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  922. }
  923. }
  924. //these two APIs can be used together or in combination with the others.
  925. void TextureStorage::texture_2d_placeholder_initialize(RID p_texture) {
  926. //this could be better optimized to reuse an existing image , done this way
  927. //for now to get it working
  928. Ref<Image> image;
  929. image.instantiate();
  930. image->create(4, 4, false, Image::FORMAT_RGBA8);
  931. image->fill(Color(1, 0, 1, 1));
  932. texture_2d_initialize(p_texture, image);
  933. }
  934. void TextureStorage::texture_2d_layered_placeholder_initialize(RID p_texture, RS::TextureLayeredType p_layered_type) {
  935. //this could be better optimized to reuse an existing image , done this way
  936. //for now to get it working
  937. Ref<Image> image;
  938. image.instantiate();
  939. image->create(4, 4, false, Image::FORMAT_RGBA8);
  940. image->fill(Color(1, 0, 1, 1));
  941. Vector<Ref<Image>> images;
  942. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  943. images.push_back(image);
  944. } else {
  945. //cube
  946. for (int i = 0; i < 6; i++) {
  947. images.push_back(image);
  948. }
  949. }
  950. texture_2d_layered_initialize(p_texture, images, p_layered_type);
  951. }
  952. void TextureStorage::texture_3d_placeholder_initialize(RID p_texture) {
  953. //this could be better optimized to reuse an existing image , done this way
  954. //for now to get it working
  955. Ref<Image> image;
  956. image.instantiate();
  957. image->create(4, 4, false, Image::FORMAT_RGBA8);
  958. image->fill(Color(1, 0, 1, 1));
  959. Vector<Ref<Image>> images;
  960. //cube
  961. for (int i = 0; i < 4; i++) {
  962. images.push_back(image);
  963. }
  964. texture_3d_initialize(p_texture, Image::FORMAT_RGBA8, 4, 4, 4, false, images);
  965. }
  966. Ref<Image> TextureStorage::texture_2d_get(RID p_texture) const {
  967. Texture *tex = texture_owner.get_or_null(p_texture);
  968. ERR_FAIL_COND_V(!tex, Ref<Image>());
  969. #ifdef TOOLS_ENABLED
  970. if (tex->image_cache_2d.is_valid() && !tex->is_render_target) {
  971. return tex->image_cache_2d;
  972. }
  973. #endif
  974. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  975. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  976. Ref<Image> image;
  977. image.instantiate();
  978. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  979. ERR_FAIL_COND_V(image->is_empty(), Ref<Image>());
  980. if (tex->format != tex->validated_format) {
  981. image->convert(tex->format);
  982. }
  983. #ifdef TOOLS_ENABLED
  984. if (Engine::get_singleton()->is_editor_hint() && !tex->is_render_target) {
  985. tex->image_cache_2d = image;
  986. }
  987. #endif
  988. return image;
  989. }
  990. Ref<Image> TextureStorage::texture_2d_layer_get(RID p_texture, int p_layer) const {
  991. Texture *tex = texture_owner.get_or_null(p_texture);
  992. ERR_FAIL_COND_V(!tex, Ref<Image>());
  993. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, p_layer);
  994. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  995. Ref<Image> image;
  996. image.instantiate();
  997. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  998. ERR_FAIL_COND_V(image->is_empty(), Ref<Image>());
  999. if (tex->format != tex->validated_format) {
  1000. image->convert(tex->format);
  1001. }
  1002. return image;
  1003. }
  1004. Vector<Ref<Image>> TextureStorage::texture_3d_get(RID p_texture) const {
  1005. Texture *tex = texture_owner.get_or_null(p_texture);
  1006. ERR_FAIL_COND_V(!tex, Vector<Ref<Image>>());
  1007. ERR_FAIL_COND_V(tex->type != TextureStorage::TYPE_3D, Vector<Ref<Image>>());
  1008. Vector<uint8_t> all_data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  1009. ERR_FAIL_COND_V(all_data.size() != (int)tex->buffer_size_3d, Vector<Ref<Image>>());
  1010. Vector<Ref<Image>> ret;
  1011. for (int i = 0; i < tex->buffer_slices_3d.size(); i++) {
  1012. const Texture::BufferSlice3D &bs = tex->buffer_slices_3d[i];
  1013. ERR_FAIL_COND_V(bs.offset >= (uint32_t)all_data.size(), Vector<Ref<Image>>());
  1014. ERR_FAIL_COND_V(bs.offset + bs.buffer_size > (uint32_t)all_data.size(), Vector<Ref<Image>>());
  1015. Vector<uint8_t> sub_region = all_data.slice(bs.offset, bs.offset + bs.buffer_size);
  1016. Ref<Image> img;
  1017. img.instantiate();
  1018. img->create(bs.size.width, bs.size.height, false, tex->validated_format, sub_region);
  1019. ERR_FAIL_COND_V(img->is_empty(), Vector<Ref<Image>>());
  1020. if (tex->format != tex->validated_format) {
  1021. img->convert(tex->format);
  1022. }
  1023. ret.push_back(img);
  1024. }
  1025. return ret;
  1026. }
  1027. void TextureStorage::texture_replace(RID p_texture, RID p_by_texture) {
  1028. Texture *tex = texture_owner.get_or_null(p_texture);
  1029. ERR_FAIL_COND(!tex);
  1030. ERR_FAIL_COND(tex->proxy_to.is_valid()); //can't replace proxy
  1031. Texture *by_tex = texture_owner.get_or_null(p_by_texture);
  1032. ERR_FAIL_COND(!by_tex);
  1033. ERR_FAIL_COND(by_tex->proxy_to.is_valid()); //can't replace proxy
  1034. if (tex == by_tex) {
  1035. return;
  1036. }
  1037. if (tex->rd_texture_srgb.is_valid()) {
  1038. RD::get_singleton()->free(tex->rd_texture_srgb);
  1039. }
  1040. RD::get_singleton()->free(tex->rd_texture);
  1041. if (tex->canvas_texture) {
  1042. memdelete(tex->canvas_texture);
  1043. tex->canvas_texture = nullptr;
  1044. }
  1045. Vector<RID> proxies_to_update = tex->proxies;
  1046. Vector<RID> proxies_to_redirect = by_tex->proxies;
  1047. *tex = *by_tex;
  1048. tex->proxies = proxies_to_update; //restore proxies, so they can be updated
  1049. if (tex->canvas_texture) {
  1050. tex->canvas_texture->diffuse = p_texture; //update
  1051. }
  1052. for (int i = 0; i < proxies_to_update.size(); i++) {
  1053. texture_proxy_update(proxies_to_update[i], p_texture);
  1054. }
  1055. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  1056. texture_proxy_update(proxies_to_redirect[i], p_texture);
  1057. }
  1058. //delete last, so proxies can be updated
  1059. texture_owner.free(p_by_texture);
  1060. decal_atlas_mark_dirty_on_texture(p_texture);
  1061. }
  1062. void TextureStorage::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  1063. Texture *tex = texture_owner.get_or_null(p_texture);
  1064. ERR_FAIL_COND(!tex);
  1065. ERR_FAIL_COND(tex->type != TextureStorage::TYPE_2D);
  1066. tex->width_2d = p_width;
  1067. tex->height_2d = p_height;
  1068. }
  1069. void TextureStorage::texture_set_path(RID p_texture, const String &p_path) {
  1070. Texture *tex = texture_owner.get_or_null(p_texture);
  1071. ERR_FAIL_COND(!tex);
  1072. tex->path = p_path;
  1073. }
  1074. String TextureStorage::texture_get_path(RID p_texture) const {
  1075. Texture *tex = texture_owner.get_or_null(p_texture);
  1076. ERR_FAIL_COND_V(!tex, String());
  1077. return tex->path;
  1078. }
  1079. void TextureStorage::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1080. Texture *tex = texture_owner.get_or_null(p_texture);
  1081. ERR_FAIL_COND(!tex);
  1082. tex->detect_3d_callback_ud = p_userdata;
  1083. tex->detect_3d_callback = p_callback;
  1084. }
  1085. void TextureStorage::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1086. Texture *tex = texture_owner.get_or_null(p_texture);
  1087. ERR_FAIL_COND(!tex);
  1088. tex->detect_normal_callback_ud = p_userdata;
  1089. tex->detect_normal_callback = p_callback;
  1090. }
  1091. void TextureStorage::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  1092. Texture *tex = texture_owner.get_or_null(p_texture);
  1093. ERR_FAIL_COND(!tex);
  1094. tex->detect_roughness_callback_ud = p_userdata;
  1095. tex->detect_roughness_callback = p_callback;
  1096. }
  1097. void TextureStorage::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  1098. }
  1099. void TextureStorage::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  1100. }
  1101. Size2 TextureStorage::texture_size_with_proxy(RID p_proxy) {
  1102. return texture_2d_get_size(p_proxy);
  1103. }
  1104. Ref<Image> TextureStorage::_validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format) {
  1105. Ref<Image> image = p_image->duplicate();
  1106. switch (p_image->get_format()) {
  1107. case Image::FORMAT_L8: {
  1108. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1109. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1110. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  1111. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1112. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1113. } break; //luminance
  1114. case Image::FORMAT_LA8: {
  1115. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1116. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1117. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  1118. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1119. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_G;
  1120. } break; //luminance-alpha
  1121. case Image::FORMAT_R8: {
  1122. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1123. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1124. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1125. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1126. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1127. } break;
  1128. case Image::FORMAT_RG8: {
  1129. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1130. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1131. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1132. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1133. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1134. } break;
  1135. case Image::FORMAT_RGB8: {
  1136. //this format is not mandatory for specification, check if supported first
  1137. if (false && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R8G8B8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT) && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R8G8B8_SRGB, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1138. r_format.format = RD::DATA_FORMAT_R8G8B8_UNORM;
  1139. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8_SRGB;
  1140. } else {
  1141. //not supported, reconvert
  1142. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1143. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1144. image->convert(Image::FORMAT_RGBA8);
  1145. }
  1146. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1147. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1148. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1149. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1150. } break;
  1151. case Image::FORMAT_RGBA8: {
  1152. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1153. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1154. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1155. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1156. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1157. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1158. } break;
  1159. case Image::FORMAT_RGBA4444: {
  1160. r_format.format = RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16;
  1161. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B; //needs swizzle
  1162. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1163. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1164. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1165. } break;
  1166. case Image::FORMAT_RGB565: {
  1167. r_format.format = RD::DATA_FORMAT_B5G6R5_UNORM_PACK16;
  1168. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B;
  1169. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1170. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1171. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1172. } break;
  1173. case Image::FORMAT_RF: {
  1174. r_format.format = RD::DATA_FORMAT_R32_SFLOAT;
  1175. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1176. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1177. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1178. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1179. } break; //float
  1180. case Image::FORMAT_RGF: {
  1181. r_format.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  1182. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1183. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1184. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1185. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1186. } break;
  1187. case Image::FORMAT_RGBF: {
  1188. //this format is not mandatory for specification, check if supported first
  1189. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R32G32B32_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1190. r_format.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  1191. } else {
  1192. //not supported, reconvert
  1193. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1194. image->convert(Image::FORMAT_RGBAF);
  1195. }
  1196. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1197. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1198. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1199. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1200. } break;
  1201. case Image::FORMAT_RGBAF: {
  1202. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1203. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1204. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1205. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1206. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1207. } break;
  1208. case Image::FORMAT_RH: {
  1209. r_format.format = RD::DATA_FORMAT_R16_SFLOAT;
  1210. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1211. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1212. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1213. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1214. } break; //half float
  1215. case Image::FORMAT_RGH: {
  1216. r_format.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  1217. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1218. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1219. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1220. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1221. } break;
  1222. case Image::FORMAT_RGBH: {
  1223. //this format is not mandatory for specification, check if supported first
  1224. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R16G16B16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1225. r_format.format = RD::DATA_FORMAT_R16G16B16_SFLOAT;
  1226. } else {
  1227. //not supported, reconvert
  1228. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1229. image->convert(Image::FORMAT_RGBAH);
  1230. }
  1231. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1232. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1233. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1234. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1235. } break;
  1236. case Image::FORMAT_RGBAH: {
  1237. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1238. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1239. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1240. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1241. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1242. } break;
  1243. case Image::FORMAT_RGBE9995: {
  1244. r_format.format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  1245. #ifndef _MSC_VER
  1246. #warning TODO need to make a function in Image to swap bits for this
  1247. #endif
  1248. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_IDENTITY;
  1249. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_IDENTITY;
  1250. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_IDENTITY;
  1251. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_IDENTITY;
  1252. } break;
  1253. case Image::FORMAT_DXT1: {
  1254. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1255. r_format.format = RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK;
  1256. r_format.format_srgb = RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK;
  1257. } else {
  1258. //not supported, reconvert
  1259. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1260. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1261. image->decompress();
  1262. image->convert(Image::FORMAT_RGBA8);
  1263. }
  1264. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1265. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1266. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1267. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1268. } break; //s3tc bc1
  1269. case Image::FORMAT_DXT3: {
  1270. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC2_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1271. r_format.format = RD::DATA_FORMAT_BC2_UNORM_BLOCK;
  1272. r_format.format_srgb = RD::DATA_FORMAT_BC2_SRGB_BLOCK;
  1273. } else {
  1274. //not supported, reconvert
  1275. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1276. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1277. image->decompress();
  1278. image->convert(Image::FORMAT_RGBA8);
  1279. }
  1280. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1281. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1282. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1283. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1284. } break; //bc2
  1285. case Image::FORMAT_DXT5: {
  1286. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC3_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1287. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  1288. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  1289. } else {
  1290. //not supported, reconvert
  1291. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1292. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1293. image->decompress();
  1294. image->convert(Image::FORMAT_RGBA8);
  1295. }
  1296. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1297. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1298. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1299. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1300. } break; //bc3
  1301. case Image::FORMAT_RGTC_R: {
  1302. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC4_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1303. r_format.format = RD::DATA_FORMAT_BC4_UNORM_BLOCK;
  1304. } else {
  1305. //not supported, reconvert
  1306. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1307. image->decompress();
  1308. image->convert(Image::FORMAT_R8);
  1309. }
  1310. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1311. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1312. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1313. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1314. } break;
  1315. case Image::FORMAT_RGTC_RG: {
  1316. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1317. r_format.format = RD::DATA_FORMAT_BC5_UNORM_BLOCK;
  1318. } else {
  1319. //not supported, reconvert
  1320. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1321. image->decompress();
  1322. image->convert(Image::FORMAT_RG8);
  1323. }
  1324. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1325. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1326. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1327. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1328. } break;
  1329. case Image::FORMAT_BPTC_RGBA: {
  1330. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1331. r_format.format = RD::DATA_FORMAT_BC7_UNORM_BLOCK;
  1332. r_format.format_srgb = RD::DATA_FORMAT_BC7_SRGB_BLOCK;
  1333. } else {
  1334. //not supported, reconvert
  1335. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1336. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1337. image->decompress();
  1338. image->convert(Image::FORMAT_RGBA8);
  1339. }
  1340. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1341. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1342. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1343. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1344. } break; //btpc bc7
  1345. case Image::FORMAT_BPTC_RGBF: {
  1346. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1347. r_format.format = RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK;
  1348. } else {
  1349. //not supported, reconvert
  1350. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1351. image->decompress();
  1352. image->convert(Image::FORMAT_RGBAH);
  1353. }
  1354. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1355. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1356. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1357. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1358. } break; //float bc6h
  1359. case Image::FORMAT_BPTC_RGBFU: {
  1360. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1361. r_format.format = RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK;
  1362. } else {
  1363. //not supported, reconvert
  1364. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1365. image->decompress();
  1366. image->convert(Image::FORMAT_RGBAH);
  1367. }
  1368. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1369. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1370. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1371. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1372. } break; //unsigned float bc6hu
  1373. case Image::FORMAT_ETC2_R11: {
  1374. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1375. r_format.format = RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK;
  1376. } else {
  1377. //not supported, reconvert
  1378. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1379. image->decompress();
  1380. image->convert(Image::FORMAT_R8);
  1381. }
  1382. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1383. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1384. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1385. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1386. } break; //etc2
  1387. case Image::FORMAT_ETC2_R11S: {
  1388. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1389. r_format.format = RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK;
  1390. } else {
  1391. //not supported, reconvert
  1392. r_format.format = RD::DATA_FORMAT_R8_SNORM;
  1393. image->decompress();
  1394. image->convert(Image::FORMAT_R8);
  1395. }
  1396. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1397. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1398. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1399. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1400. } break; //signed: {} break; NOT srgb.
  1401. case Image::FORMAT_ETC2_RG11: {
  1402. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1403. r_format.format = RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK;
  1404. } else {
  1405. //not supported, reconvert
  1406. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1407. image->decompress();
  1408. image->convert(Image::FORMAT_RG8);
  1409. }
  1410. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1411. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1412. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1413. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1414. } break;
  1415. case Image::FORMAT_ETC2_RG11S: {
  1416. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1417. r_format.format = RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK;
  1418. } else {
  1419. //not supported, reconvert
  1420. r_format.format = RD::DATA_FORMAT_R8G8_SNORM;
  1421. image->decompress();
  1422. image->convert(Image::FORMAT_RG8);
  1423. }
  1424. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1425. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1426. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1427. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1428. } break;
  1429. case Image::FORMAT_ETC:
  1430. case Image::FORMAT_ETC2_RGB8: {
  1431. //ETC2 is backwards compatible with ETC1, and all modern platforms support it
  1432. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1433. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
  1434. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK;
  1435. } else {
  1436. //not supported, reconvert
  1437. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1438. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1439. image->decompress();
  1440. image->convert(Image::FORMAT_RGBA8);
  1441. }
  1442. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1443. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1444. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1445. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1446. } break;
  1447. case Image::FORMAT_ETC2_RGBA8: {
  1448. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1449. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  1450. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  1451. } else {
  1452. //not supported, reconvert
  1453. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1454. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1455. image->decompress();
  1456. image->convert(Image::FORMAT_RGBA8);
  1457. }
  1458. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1459. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1460. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1461. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1462. } break;
  1463. case Image::FORMAT_ETC2_RGB8A1: {
  1464. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1465. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
  1466. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK;
  1467. } else {
  1468. //not supported, reconvert
  1469. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1470. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1471. image->decompress();
  1472. image->convert(Image::FORMAT_RGBA8);
  1473. }
  1474. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1475. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1476. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1477. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1478. } break;
  1479. case Image::FORMAT_ETC2_RA_AS_RG: {
  1480. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1481. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  1482. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  1483. } else {
  1484. //not supported, reconvert
  1485. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1486. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1487. image->decompress();
  1488. image->convert(Image::FORMAT_RGBA8);
  1489. }
  1490. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1491. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  1492. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1493. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1494. } break;
  1495. case Image::FORMAT_DXT5_RA_AS_RG: {
  1496. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC3_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1497. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  1498. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  1499. } else {
  1500. //not supported, reconvert
  1501. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1502. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1503. image->decompress();
  1504. image->convert(Image::FORMAT_RGBA8);
  1505. }
  1506. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1507. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  1508. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1509. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1510. } break;
  1511. default: {
  1512. }
  1513. }
  1514. return image;
  1515. }
  1516. /* DECAL API */
  1517. RID TextureStorage::decal_atlas_get_texture() const {
  1518. return decal_atlas.texture;
  1519. }
  1520. RID TextureStorage::decal_atlas_get_texture_srgb() const {
  1521. return decal_atlas.texture_srgb;
  1522. }
  1523. RID TextureStorage::decal_allocate() {
  1524. return decal_owner.allocate_rid();
  1525. }
  1526. void TextureStorage::decal_initialize(RID p_decal) {
  1527. decal_owner.initialize_rid(p_decal, Decal());
  1528. }
  1529. void TextureStorage::decal_free(RID p_rid) {
  1530. Decal *decal = decal_owner.get_or_null(p_rid);
  1531. for (int i = 0; i < RS::DECAL_TEXTURE_MAX; i++) {
  1532. if (decal->textures[i].is_valid() && owns_texture(decal->textures[i])) {
  1533. texture_remove_from_decal_atlas(decal->textures[i]);
  1534. }
  1535. }
  1536. decal->dependency.deleted_notify(p_rid);
  1537. decal_owner.free(p_rid);
  1538. }
  1539. void TextureStorage::decal_set_extents(RID p_decal, const Vector3 &p_extents) {
  1540. Decal *decal = decal_owner.get_or_null(p_decal);
  1541. ERR_FAIL_COND(!decal);
  1542. decal->extents = p_extents;
  1543. decal->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1544. }
  1545. void TextureStorage::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  1546. Decal *decal = decal_owner.get_or_null(p_decal);
  1547. ERR_FAIL_COND(!decal);
  1548. ERR_FAIL_INDEX(p_type, RS::DECAL_TEXTURE_MAX);
  1549. if (decal->textures[p_type] == p_texture) {
  1550. return;
  1551. }
  1552. ERR_FAIL_COND(p_texture.is_valid() && !owns_texture(p_texture));
  1553. if (decal->textures[p_type].is_valid() && owns_texture(decal->textures[p_type])) {
  1554. texture_remove_from_decal_atlas(decal->textures[p_type]);
  1555. }
  1556. decal->textures[p_type] = p_texture;
  1557. if (decal->textures[p_type].is_valid()) {
  1558. texture_add_to_decal_atlas(decal->textures[p_type]);
  1559. }
  1560. decal->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_DECAL);
  1561. }
  1562. void TextureStorage::decal_set_emission_energy(RID p_decal, float p_energy) {
  1563. Decal *decal = decal_owner.get_or_null(p_decal);
  1564. ERR_FAIL_COND(!decal);
  1565. decal->emission_energy = p_energy;
  1566. }
  1567. void TextureStorage::decal_set_albedo_mix(RID p_decal, float p_mix) {
  1568. Decal *decal = decal_owner.get_or_null(p_decal);
  1569. ERR_FAIL_COND(!decal);
  1570. decal->albedo_mix = p_mix;
  1571. }
  1572. void TextureStorage::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  1573. Decal *decal = decal_owner.get_or_null(p_decal);
  1574. ERR_FAIL_COND(!decal);
  1575. decal->modulate = p_modulate;
  1576. }
  1577. void TextureStorage::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  1578. Decal *decal = decal_owner.get_or_null(p_decal);
  1579. ERR_FAIL_COND(!decal);
  1580. decal->cull_mask = p_layers;
  1581. decal->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1582. }
  1583. void TextureStorage::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  1584. Decal *decal = decal_owner.get_or_null(p_decal);
  1585. ERR_FAIL_COND(!decal);
  1586. decal->distance_fade = p_enabled;
  1587. decal->distance_fade_begin = p_begin;
  1588. decal->distance_fade_length = p_length;
  1589. }
  1590. void TextureStorage::decal_set_fade(RID p_decal, float p_above, float p_below) {
  1591. Decal *decal = decal_owner.get_or_null(p_decal);
  1592. ERR_FAIL_COND(!decal);
  1593. decal->upper_fade = p_above;
  1594. decal->lower_fade = p_below;
  1595. }
  1596. void TextureStorage::decal_set_normal_fade(RID p_decal, float p_fade) {
  1597. Decal *decal = decal_owner.get_or_null(p_decal);
  1598. ERR_FAIL_COND(!decal);
  1599. decal->normal_fade = p_fade;
  1600. }
  1601. void TextureStorage::decal_atlas_mark_dirty_on_texture(RID p_texture) {
  1602. if (decal_atlas.textures.has(p_texture)) {
  1603. //belongs to decal atlas..
  1604. decal_atlas.dirty = true; //mark it dirty since it was most likely modified
  1605. }
  1606. }
  1607. void TextureStorage::decal_atlas_remove_texture(RID p_texture) {
  1608. if (decal_atlas.textures.has(p_texture)) {
  1609. decal_atlas.textures.erase(p_texture);
  1610. //there is not much a point of making it dirty, just let it be.
  1611. }
  1612. }
  1613. AABB TextureStorage::decal_get_aabb(RID p_decal) const {
  1614. Decal *decal = decal_owner.get_or_null(p_decal);
  1615. ERR_FAIL_COND_V(!decal, AABB());
  1616. return AABB(-decal->extents, decal->extents * 2.0);
  1617. }
  1618. Dependency *TextureStorage::decal_get_dependency(RID p_decal) {
  1619. Decal *decal = decal_owner.get_or_null(p_decal);
  1620. ERR_FAIL_COND_V(!decal, nullptr);
  1621. return &decal->dependency;
  1622. }
  1623. void TextureStorage::update_decal_atlas() {
  1624. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  1625. ERR_FAIL_NULL(copy_effects);
  1626. if (!decal_atlas.dirty) {
  1627. return; //nothing to do
  1628. }
  1629. decal_atlas.dirty = false;
  1630. if (decal_atlas.texture.is_valid()) {
  1631. RD::get_singleton()->free(decal_atlas.texture);
  1632. decal_atlas.texture = RID();
  1633. decal_atlas.texture_srgb = RID();
  1634. decal_atlas.texture_mipmaps.clear();
  1635. }
  1636. int border = 1 << decal_atlas.mipmaps;
  1637. if (decal_atlas.textures.size()) {
  1638. //generate atlas
  1639. Vector<DecalAtlas::SortItem> itemsv;
  1640. itemsv.resize(decal_atlas.textures.size());
  1641. int base_size = 8;
  1642. int idx = 0;
  1643. for (const KeyValue<RID, DecalAtlas::Texture> &E : decal_atlas.textures) {
  1644. DecalAtlas::SortItem &si = itemsv.write[idx];
  1645. Texture *src_tex = get_texture(E.key);
  1646. si.size.width = (src_tex->width / border) + 1;
  1647. si.size.height = (src_tex->height / border) + 1;
  1648. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  1649. if (base_size < si.size.width) {
  1650. base_size = nearest_power_of_2_templated(si.size.width);
  1651. }
  1652. si.texture = E.key;
  1653. idx++;
  1654. }
  1655. //sort items by size
  1656. itemsv.sort();
  1657. //attempt to create atlas
  1658. int item_count = itemsv.size();
  1659. DecalAtlas::SortItem *items = itemsv.ptrw();
  1660. int atlas_height = 0;
  1661. while (true) {
  1662. Vector<int> v_offsetsv;
  1663. v_offsetsv.resize(base_size);
  1664. int *v_offsets = v_offsetsv.ptrw();
  1665. memset(v_offsets, 0, sizeof(int) * base_size);
  1666. int max_height = 0;
  1667. for (int i = 0; i < item_count; i++) {
  1668. //best fit
  1669. DecalAtlas::SortItem &si = items[i];
  1670. int best_idx = -1;
  1671. int best_height = 0x7FFFFFFF;
  1672. for (int j = 0; j <= base_size - si.size.width; j++) {
  1673. int height = 0;
  1674. for (int k = 0; k < si.size.width; k++) {
  1675. int h = v_offsets[k + j];
  1676. if (h > height) {
  1677. height = h;
  1678. if (height > best_height) {
  1679. break; //already bad
  1680. }
  1681. }
  1682. }
  1683. if (height < best_height) {
  1684. best_height = height;
  1685. best_idx = j;
  1686. }
  1687. }
  1688. //update
  1689. for (int k = 0; k < si.size.width; k++) {
  1690. v_offsets[k + best_idx] = best_height + si.size.height;
  1691. }
  1692. si.pos.x = best_idx;
  1693. si.pos.y = best_height;
  1694. if (si.pos.y + si.size.height > max_height) {
  1695. max_height = si.pos.y + si.size.height;
  1696. }
  1697. }
  1698. if (max_height <= base_size * 2) {
  1699. atlas_height = max_height;
  1700. break; //good ratio, break;
  1701. }
  1702. base_size *= 2;
  1703. }
  1704. decal_atlas.size.width = base_size * border;
  1705. decal_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  1706. for (int i = 0; i < item_count; i++) {
  1707. DecalAtlas::Texture *t = decal_atlas.textures.getptr(items[i].texture);
  1708. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  1709. t->uv_rect.size = items[i].pixel_size;
  1710. t->uv_rect.position /= Size2(decal_atlas.size);
  1711. t->uv_rect.size /= Size2(decal_atlas.size);
  1712. }
  1713. } else {
  1714. //use border as size, so it at least has enough mipmaps
  1715. decal_atlas.size.width = border;
  1716. decal_atlas.size.height = border;
  1717. }
  1718. //blit textures
  1719. RD::TextureFormat tformat;
  1720. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1721. tformat.width = decal_atlas.size.width;
  1722. tformat.height = decal_atlas.size.height;
  1723. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  1724. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  1725. tformat.mipmaps = decal_atlas.mipmaps;
  1726. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  1727. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  1728. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1729. RD::get_singleton()->texture_clear(decal_atlas.texture, Color(0, 0, 0, 0), 0, decal_atlas.mipmaps, 0, 1);
  1730. {
  1731. //create the framebuffer
  1732. Size2i s = decal_atlas.size;
  1733. for (int i = 0; i < decal_atlas.mipmaps; i++) {
  1734. DecalAtlas::MipMap mm;
  1735. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), decal_atlas.texture, 0, i);
  1736. Vector<RID> fb;
  1737. fb.push_back(mm.texture);
  1738. mm.fb = RD::get_singleton()->framebuffer_create(fb);
  1739. mm.size = s;
  1740. decal_atlas.texture_mipmaps.push_back(mm);
  1741. s.width = MAX(1, s.width >> 1);
  1742. s.height = MAX(1, s.height >> 1);
  1743. }
  1744. {
  1745. //create the SRGB variant
  1746. RD::TextureView rd_view;
  1747. rd_view.format_override = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1748. decal_atlas.texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, decal_atlas.texture);
  1749. }
  1750. }
  1751. RID prev_texture;
  1752. for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {
  1753. const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];
  1754. Color clear_color(0, 0, 0, 0);
  1755. if (decal_atlas.textures.size()) {
  1756. if (i == 0) {
  1757. Vector<Color> cc;
  1758. cc.push_back(clear_color);
  1759. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(mm.fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, cc);
  1760. for (const KeyValue<RID, DecalAtlas::Texture> &E : decal_atlas.textures) {
  1761. DecalAtlas::Texture *t = decal_atlas.textures.getptr(E.key);
  1762. Texture *src_tex = get_texture(E.key);
  1763. copy_effects->copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);
  1764. }
  1765. RD::get_singleton()->draw_list_end();
  1766. prev_texture = mm.texture;
  1767. } else {
  1768. copy_effects->copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));
  1769. prev_texture = mm.texture;
  1770. }
  1771. } else {
  1772. RD::get_singleton()->texture_clear(mm.texture, clear_color, 0, 1, 0, 1);
  1773. }
  1774. }
  1775. }
  1776. void TextureStorage::texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  1777. if (!decal_atlas.textures.has(p_texture)) {
  1778. DecalAtlas::Texture t;
  1779. t.users = 1;
  1780. t.panorama_to_dp_users = p_panorama_to_dp ? 1 : 0;
  1781. decal_atlas.textures[p_texture] = t;
  1782. decal_atlas.dirty = true;
  1783. } else {
  1784. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  1785. t->users++;
  1786. if (p_panorama_to_dp) {
  1787. t->panorama_to_dp_users++;
  1788. }
  1789. }
  1790. }
  1791. void TextureStorage::texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  1792. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  1793. ERR_FAIL_COND(!t);
  1794. t->users--;
  1795. if (p_panorama_to_dp) {
  1796. ERR_FAIL_COND(t->panorama_to_dp_users == 0);
  1797. t->panorama_to_dp_users--;
  1798. }
  1799. if (t->users == 0) {
  1800. decal_atlas.textures.erase(p_texture);
  1801. //do not mark it dirty, there is no need to since it remains working
  1802. }
  1803. }
  1804. /* RENDER TARGET API */
  1805. void TextureStorage::_clear_render_target(RenderTarget *rt) {
  1806. //free in reverse dependency order
  1807. if (rt->framebuffer.is_valid()) {
  1808. RD::get_singleton()->free(rt->framebuffer);
  1809. rt->framebuffer_uniform_set = RID(); //chain deleted
  1810. }
  1811. if (rt->color.is_valid()) {
  1812. RD::get_singleton()->free(rt->color);
  1813. }
  1814. if (rt->backbuffer.is_valid()) {
  1815. RD::get_singleton()->free(rt->backbuffer);
  1816. rt->backbuffer = RID();
  1817. rt->backbuffer_mipmaps.clear();
  1818. rt->backbuffer_uniform_set = RID(); //chain deleted
  1819. }
  1820. _render_target_clear_sdf(rt);
  1821. rt->framebuffer = RID();
  1822. rt->color = RID();
  1823. }
  1824. void TextureStorage::_update_render_target(RenderTarget *rt) {
  1825. if (rt->texture.is_null()) {
  1826. //create a placeholder until updated
  1827. rt->texture = texture_allocate();
  1828. texture_2d_placeholder_initialize(rt->texture);
  1829. Texture *tex = get_texture(rt->texture);
  1830. tex->is_render_target = true;
  1831. }
  1832. _clear_render_target(rt);
  1833. if (rt->size.width == 0 || rt->size.height == 0) {
  1834. return;
  1835. }
  1836. //until we implement support for HDR monitors (and render target is attached to screen), this is enough.
  1837. rt->color_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1838. rt->color_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1839. rt->image_format = rt->is_transparent ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8;
  1840. RD::TextureFormat rd_format;
  1841. RD::TextureView rd_view;
  1842. { //attempt register
  1843. rd_format.format = rt->color_format;
  1844. rd_format.width = rt->size.width;
  1845. rd_format.height = rt->size.height;
  1846. rd_format.depth = 1;
  1847. rd_format.array_layers = rt->view_count; // for stereo we create two (or more) layers, need to see if we can make fallback work like this too if we don't have multiview
  1848. rd_format.mipmaps = 1;
  1849. if (rd_format.array_layers > 1) { // why are we not using rt->texture_type ??
  1850. rd_format.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  1851. } else {
  1852. rd_format.texture_type = RD::TEXTURE_TYPE_2D;
  1853. }
  1854. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  1855. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  1856. rd_format.shareable_formats.push_back(rt->color_format);
  1857. rd_format.shareable_formats.push_back(rt->color_format_srgb);
  1858. }
  1859. rt->color = RD::get_singleton()->texture_create(rd_format, rd_view);
  1860. ERR_FAIL_COND(rt->color.is_null());
  1861. Vector<RID> fb_textures;
  1862. fb_textures.push_back(rt->color);
  1863. rt->framebuffer = RD::get_singleton()->framebuffer_create(fb_textures, RenderingDevice::INVALID_ID, rt->view_count);
  1864. if (rt->framebuffer.is_null()) {
  1865. _clear_render_target(rt);
  1866. ERR_FAIL_COND(rt->framebuffer.is_null());
  1867. }
  1868. { //update texture
  1869. Texture *tex = get_texture(rt->texture);
  1870. //free existing textures
  1871. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  1872. RD::get_singleton()->free(tex->rd_texture);
  1873. }
  1874. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  1875. RD::get_singleton()->free(tex->rd_texture_srgb);
  1876. }
  1877. tex->rd_texture = RID();
  1878. tex->rd_texture_srgb = RID();
  1879. //create shared textures to the color buffer,
  1880. //so transparent can be supported
  1881. RD::TextureView view;
  1882. view.format_override = rt->color_format;
  1883. if (!rt->is_transparent) {
  1884. view.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1885. }
  1886. tex->rd_texture = RD::get_singleton()->texture_create_shared(view, rt->color);
  1887. if (rt->color_format_srgb != RD::DATA_FORMAT_MAX) {
  1888. view.format_override = rt->color_format_srgb;
  1889. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(view, rt->color);
  1890. }
  1891. tex->rd_view = view;
  1892. tex->width = rt->size.width;
  1893. tex->height = rt->size.height;
  1894. tex->width_2d = rt->size.width;
  1895. tex->height_2d = rt->size.height;
  1896. tex->rd_format = rt->color_format;
  1897. tex->rd_format_srgb = rt->color_format_srgb;
  1898. tex->format = rt->image_format;
  1899. Vector<RID> proxies = tex->proxies; //make a copy, since update may change it
  1900. for (int i = 0; i < proxies.size(); i++) {
  1901. texture_proxy_update(proxies[i], rt->texture);
  1902. }
  1903. }
  1904. }
  1905. void TextureStorage::_create_render_target_backbuffer(RenderTarget *rt) {
  1906. ERR_FAIL_COND(rt->backbuffer.is_valid());
  1907. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rt->size.width, rt->size.height, Image::FORMAT_RGBA8);
  1908. RD::TextureFormat tf;
  1909. tf.format = rt->color_format;
  1910. tf.width = rt->size.width;
  1911. tf.height = rt->size.height;
  1912. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1913. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  1914. tf.mipmaps = mipmaps_required;
  1915. rt->backbuffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1916. RD::get_singleton()->set_resource_name(rt->backbuffer, "Render Target Back Buffer");
  1917. rt->backbuffer_mipmap0 = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, 0);
  1918. RD::get_singleton()->set_resource_name(rt->backbuffer_mipmap0, "Back Buffer slice mipmap 0");
  1919. {
  1920. Vector<RID> fb_tex;
  1921. fb_tex.push_back(rt->backbuffer_mipmap0);
  1922. rt->backbuffer_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  1923. }
  1924. if (rt->framebuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->framebuffer_uniform_set)) {
  1925. //the new one will require the backbuffer.
  1926. RD::get_singleton()->free(rt->framebuffer_uniform_set);
  1927. rt->framebuffer_uniform_set = RID();
  1928. }
  1929. //create mipmaps
  1930. for (uint32_t i = 1; i < mipmaps_required; i++) {
  1931. RID mipmap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, i);
  1932. RD::get_singleton()->set_resource_name(mipmap, "Back Buffer slice mip: " + itos(i));
  1933. rt->backbuffer_mipmaps.push_back(mipmap);
  1934. }
  1935. }
  1936. RID TextureStorage::render_target_create() {
  1937. RenderTarget render_target;
  1938. render_target.was_used = false;
  1939. render_target.clear_requested = false;
  1940. _update_render_target(&render_target);
  1941. return render_target_owner.make_rid(render_target);
  1942. }
  1943. void TextureStorage::render_target_free(RID p_rid) {
  1944. RenderTarget *rt = render_target_owner.get_or_null(p_rid);
  1945. _clear_render_target(rt);
  1946. if (rt->texture.is_valid()) {
  1947. Texture *tex = get_texture(rt->texture);
  1948. tex->is_render_target = false;
  1949. texture_free(rt->texture);
  1950. }
  1951. render_target_owner.free(p_rid);
  1952. }
  1953. void TextureStorage::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  1954. //unused for this render target
  1955. }
  1956. void TextureStorage::render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) {
  1957. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1958. ERR_FAIL_COND(!rt);
  1959. if (rt->size.x != p_width || rt->size.y != p_height || rt->view_count != p_view_count) {
  1960. rt->size.x = p_width;
  1961. rt->size.y = p_height;
  1962. rt->view_count = p_view_count;
  1963. _update_render_target(rt);
  1964. }
  1965. }
  1966. RID TextureStorage::render_target_get_texture(RID p_render_target) {
  1967. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1968. ERR_FAIL_COND_V(!rt, RID());
  1969. return rt->texture;
  1970. }
  1971. void TextureStorage::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) {
  1972. }
  1973. void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_is_transparent) {
  1974. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1975. ERR_FAIL_COND(!rt);
  1976. rt->is_transparent = p_is_transparent;
  1977. _update_render_target(rt);
  1978. }
  1979. void TextureStorage::render_target_set_direct_to_screen(RID p_render_target, bool p_value) {
  1980. }
  1981. bool TextureStorage::render_target_was_used(RID p_render_target) {
  1982. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1983. ERR_FAIL_COND_V(!rt, false);
  1984. return rt->was_used;
  1985. }
  1986. void TextureStorage::render_target_set_as_unused(RID p_render_target) {
  1987. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1988. ERR_FAIL_COND(!rt);
  1989. rt->was_used = false;
  1990. }
  1991. Size2 TextureStorage::render_target_get_size(RID p_render_target) {
  1992. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1993. ERR_FAIL_COND_V(!rt, Size2());
  1994. return rt->size;
  1995. }
  1996. RID TextureStorage::render_target_get_rd_framebuffer(RID p_render_target) {
  1997. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1998. ERR_FAIL_COND_V(!rt, RID());
  1999. return rt->framebuffer;
  2000. }
  2001. RID TextureStorage::render_target_get_rd_texture(RID p_render_target) {
  2002. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2003. ERR_FAIL_COND_V(!rt, RID());
  2004. return rt->color;
  2005. }
  2006. RID TextureStorage::render_target_get_rd_backbuffer(RID p_render_target) {
  2007. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2008. ERR_FAIL_COND_V(!rt, RID());
  2009. return rt->backbuffer;
  2010. }
  2011. RID TextureStorage::render_target_get_rd_backbuffer_framebuffer(RID p_render_target) {
  2012. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2013. ERR_FAIL_COND_V(!rt, RID());
  2014. if (!rt->backbuffer.is_valid()) {
  2015. _create_render_target_backbuffer(rt);
  2016. }
  2017. return rt->backbuffer_fb;
  2018. }
  2019. void TextureStorage::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  2020. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2021. ERR_FAIL_COND(!rt);
  2022. rt->clear_requested = true;
  2023. rt->clear_color = p_clear_color;
  2024. }
  2025. bool TextureStorage::render_target_is_clear_requested(RID p_render_target) {
  2026. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2027. ERR_FAIL_COND_V(!rt, false);
  2028. return rt->clear_requested;
  2029. }
  2030. Color TextureStorage::render_target_get_clear_request_color(RID p_render_target) {
  2031. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2032. ERR_FAIL_COND_V(!rt, Color());
  2033. return rt->clear_color;
  2034. }
  2035. void TextureStorage::render_target_disable_clear_request(RID p_render_target) {
  2036. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2037. ERR_FAIL_COND(!rt);
  2038. rt->clear_requested = false;
  2039. }
  2040. void TextureStorage::render_target_do_clear_request(RID p_render_target) {
  2041. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2042. ERR_FAIL_COND(!rt);
  2043. if (!rt->clear_requested) {
  2044. return;
  2045. }
  2046. Vector<Color> clear_colors;
  2047. clear_colors.push_back(rt->clear_color);
  2048. RD::get_singleton()->draw_list_begin(rt->framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors);
  2049. RD::get_singleton()->draw_list_end();
  2050. rt->clear_requested = false;
  2051. }
  2052. void TextureStorage::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  2053. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2054. ERR_FAIL_COND(!rt);
  2055. if (rt->sdf_oversize == p_size && rt->sdf_scale == p_scale) {
  2056. return;
  2057. }
  2058. rt->sdf_oversize = p_size;
  2059. rt->sdf_scale = p_scale;
  2060. _render_target_clear_sdf(rt);
  2061. }
  2062. Rect2i TextureStorage::_render_target_get_sdf_rect(const RenderTarget *rt) const {
  2063. Size2i margin;
  2064. int scale;
  2065. switch (rt->sdf_oversize) {
  2066. case RS::VIEWPORT_SDF_OVERSIZE_100_PERCENT: {
  2067. scale = 100;
  2068. } break;
  2069. case RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT: {
  2070. scale = 120;
  2071. } break;
  2072. case RS::VIEWPORT_SDF_OVERSIZE_150_PERCENT: {
  2073. scale = 150;
  2074. } break;
  2075. case RS::VIEWPORT_SDF_OVERSIZE_200_PERCENT: {
  2076. scale = 200;
  2077. } break;
  2078. default: {
  2079. }
  2080. }
  2081. margin = (rt->size * scale / 100) - rt->size;
  2082. Rect2i r(Vector2i(), rt->size);
  2083. r.position -= margin;
  2084. r.size += margin * 2;
  2085. return r;
  2086. }
  2087. Rect2i TextureStorage::render_target_get_sdf_rect(RID p_render_target) const {
  2088. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2089. ERR_FAIL_COND_V(!rt, Rect2i());
  2090. return _render_target_get_sdf_rect(rt);
  2091. }
  2092. void TextureStorage::render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) {
  2093. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2094. ERR_FAIL_COND(!rt);
  2095. rt->sdf_enabled = p_enabled;
  2096. }
  2097. bool TextureStorage::render_target_is_sdf_enabled(RID p_render_target) const {
  2098. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2099. ERR_FAIL_COND_V(!rt, false);
  2100. return rt->sdf_enabled;
  2101. }
  2102. RID TextureStorage::render_target_get_sdf_texture(RID p_render_target) {
  2103. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2104. ERR_FAIL_COND_V(!rt, RID());
  2105. if (rt->sdf_buffer_read.is_null()) {
  2106. // no texture, create a dummy one for the 2D uniform set
  2107. RD::TextureFormat tformat;
  2108. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  2109. tformat.width = 4;
  2110. tformat.height = 4;
  2111. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
  2112. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  2113. Vector<uint8_t> pv;
  2114. pv.resize(16 * 4);
  2115. memset(pv.ptrw(), 0, 16 * 4);
  2116. Vector<Vector<uint8_t>> vpv;
  2117. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  2118. }
  2119. return rt->sdf_buffer_read;
  2120. }
  2121. void TextureStorage::_render_target_allocate_sdf(RenderTarget *rt) {
  2122. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_valid());
  2123. if (rt->sdf_buffer_read.is_valid()) {
  2124. RD::get_singleton()->free(rt->sdf_buffer_read);
  2125. rt->sdf_buffer_read = RID();
  2126. }
  2127. Size2i size = _render_target_get_sdf_rect(rt).size;
  2128. RD::TextureFormat tformat;
  2129. tformat.format = RD::DATA_FORMAT_R8_UNORM;
  2130. tformat.width = size.width;
  2131. tformat.height = size.height;
  2132. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  2133. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  2134. rt->sdf_buffer_write = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  2135. {
  2136. Vector<RID> write_fb;
  2137. write_fb.push_back(rt->sdf_buffer_write);
  2138. rt->sdf_buffer_write_fb = RD::get_singleton()->framebuffer_create(write_fb);
  2139. }
  2140. int scale;
  2141. switch (rt->sdf_scale) {
  2142. case RS::VIEWPORT_SDF_SCALE_100_PERCENT: {
  2143. scale = 100;
  2144. } break;
  2145. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  2146. scale = 50;
  2147. } break;
  2148. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  2149. scale = 25;
  2150. } break;
  2151. default: {
  2152. scale = 100;
  2153. } break;
  2154. }
  2155. rt->process_size = size * scale / 100;
  2156. rt->process_size.x = MAX(rt->process_size.x, 1);
  2157. rt->process_size.y = MAX(rt->process_size.y, 1);
  2158. tformat.format = RD::DATA_FORMAT_R16G16_SINT;
  2159. tformat.width = rt->process_size.width;
  2160. tformat.height = rt->process_size.height;
  2161. tformat.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  2162. rt->sdf_buffer_process[0] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  2163. rt->sdf_buffer_process[1] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  2164. tformat.format = RD::DATA_FORMAT_R16_SNORM;
  2165. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2166. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  2167. {
  2168. Vector<RD::Uniform> uniforms;
  2169. {
  2170. RD::Uniform u;
  2171. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2172. u.binding = 1;
  2173. u.append_id(rt->sdf_buffer_write);
  2174. uniforms.push_back(u);
  2175. }
  2176. {
  2177. RD::Uniform u;
  2178. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2179. u.binding = 2;
  2180. u.append_id(rt->sdf_buffer_read);
  2181. uniforms.push_back(u);
  2182. }
  2183. {
  2184. RD::Uniform u;
  2185. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2186. u.binding = 3;
  2187. u.append_id(rt->sdf_buffer_process[0]);
  2188. uniforms.push_back(u);
  2189. }
  2190. {
  2191. RD::Uniform u;
  2192. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  2193. u.binding = 4;
  2194. u.append_id(rt->sdf_buffer_process[1]);
  2195. uniforms.push_back(u);
  2196. }
  2197. rt->sdf_buffer_process_uniform_sets[0] = RD::get_singleton()->uniform_set_create(uniforms, rt_sdf.shader.version_get_shader(rt_sdf.shader_version, 0), 0);
  2198. RID aux2 = uniforms.write[2].get_id(0);
  2199. RID aux3 = uniforms.write[3].get_id(0);
  2200. uniforms.write[2].set_id(0, aux3);
  2201. uniforms.write[3].set_id(0, aux2);
  2202. rt->sdf_buffer_process_uniform_sets[1] = RD::get_singleton()->uniform_set_create(uniforms, rt_sdf.shader.version_get_shader(rt_sdf.shader_version, 0), 0);
  2203. }
  2204. }
  2205. void TextureStorage::_render_target_clear_sdf(RenderTarget *rt) {
  2206. if (rt->sdf_buffer_read.is_valid()) {
  2207. RD::get_singleton()->free(rt->sdf_buffer_read);
  2208. rt->sdf_buffer_read = RID();
  2209. }
  2210. if (rt->sdf_buffer_write_fb.is_valid()) {
  2211. RD::get_singleton()->free(rt->sdf_buffer_write);
  2212. RD::get_singleton()->free(rt->sdf_buffer_process[0]);
  2213. RD::get_singleton()->free(rt->sdf_buffer_process[1]);
  2214. rt->sdf_buffer_write = RID();
  2215. rt->sdf_buffer_write_fb = RID();
  2216. rt->sdf_buffer_process[0] = RID();
  2217. rt->sdf_buffer_process[1] = RID();
  2218. rt->sdf_buffer_process_uniform_sets[0] = RID();
  2219. rt->sdf_buffer_process_uniform_sets[1] = RID();
  2220. }
  2221. }
  2222. RID TextureStorage::render_target_get_sdf_framebuffer(RID p_render_target) {
  2223. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2224. ERR_FAIL_COND_V(!rt, RID());
  2225. if (rt->sdf_buffer_write_fb.is_null()) {
  2226. _render_target_allocate_sdf(rt);
  2227. }
  2228. return rt->sdf_buffer_write_fb;
  2229. }
  2230. void TextureStorage::render_target_sdf_process(RID p_render_target) {
  2231. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2232. ERR_FAIL_COND(!rt);
  2233. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_null());
  2234. RenderTargetSDF::PushConstant push_constant;
  2235. Rect2i r = _render_target_get_sdf_rect(rt);
  2236. push_constant.size[0] = r.size.width;
  2237. push_constant.size[1] = r.size.height;
  2238. push_constant.stride = 0;
  2239. push_constant.shift = 0;
  2240. push_constant.base_size[0] = r.size.width;
  2241. push_constant.base_size[1] = r.size.height;
  2242. bool shrink = false;
  2243. switch (rt->sdf_scale) {
  2244. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  2245. push_constant.size[0] >>= 1;
  2246. push_constant.size[1] >>= 1;
  2247. push_constant.shift = 1;
  2248. shrink = true;
  2249. } break;
  2250. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  2251. push_constant.size[0] >>= 2;
  2252. push_constant.size[1] >>= 2;
  2253. push_constant.shift = 2;
  2254. shrink = true;
  2255. } break;
  2256. default: {
  2257. };
  2258. }
  2259. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  2260. /* Load */
  2261. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_LOAD_SHRINK : RenderTargetSDF::SHADER_LOAD]);
  2262. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[1], 0); //fill [0]
  2263. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  2264. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1);
  2265. /* Process */
  2266. int stride = nearest_power_of_2_templated(MAX(push_constant.size[0], push_constant.size[1]) / 2);
  2267. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[RenderTargetSDF::SHADER_PROCESS]);
  2268. RD::get_singleton()->compute_list_add_barrier(compute_list);
  2269. bool swap = false;
  2270. //jumpflood
  2271. while (stride > 0) {
  2272. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  2273. push_constant.stride = stride;
  2274. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  2275. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1);
  2276. stride /= 2;
  2277. swap = !swap;
  2278. RD::get_singleton()->compute_list_add_barrier(compute_list);
  2279. }
  2280. /* Store */
  2281. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_STORE_SHRINK : RenderTargetSDF::SHADER_STORE]);
  2282. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  2283. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  2284. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1);
  2285. RD::get_singleton()->compute_list_end();
  2286. }
  2287. void TextureStorage::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps) {
  2288. CopyEffects *copy_effects = CopyEffects::get_singleton();
  2289. ERR_FAIL_NULL(copy_effects);
  2290. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2291. ERR_FAIL_COND(!rt);
  2292. if (!rt->backbuffer.is_valid()) {
  2293. _create_render_target_backbuffer(rt);
  2294. }
  2295. Rect2i region;
  2296. if (p_region == Rect2i()) {
  2297. region.size = rt->size;
  2298. } else {
  2299. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2300. if (region.size == Size2i()) {
  2301. return; //nothing to do
  2302. }
  2303. }
  2304. // TODO figure out stereo support here
  2305. //single texture copy for backbuffer
  2306. //RD::get_singleton()->texture_copy(rt->color, rt->backbuffer_mipmap0, Vector3(region.position.x, region.position.y, 0), Vector3(region.position.x, region.position.y, 0), Vector3(region.size.x, region.size.y, 1), 0, 0, 0, 0, true);
  2307. copy_effects->copy_to_rect(rt->color, rt->backbuffer_mipmap0, region, false, false, false, true, true);
  2308. if (!p_gen_mipmaps) {
  2309. return;
  2310. }
  2311. RD::get_singleton()->draw_command_begin_label("Gaussian Blur Mipmaps");
  2312. //then mipmap blur
  2313. RID prev_texture = rt->color; //use color, not backbuffer, as bb has mipmaps.
  2314. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  2315. region.position.x >>= 1;
  2316. region.position.y >>= 1;
  2317. region.size.x = MAX(1, region.size.x >> 1);
  2318. region.size.y = MAX(1, region.size.y >> 1);
  2319. RID mipmap = rt->backbuffer_mipmaps[i];
  2320. copy_effects->gaussian_blur(prev_texture, mipmap, region, true);
  2321. prev_texture = mipmap;
  2322. }
  2323. RD::get_singleton()->draw_command_end_label();
  2324. }
  2325. void TextureStorage::render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color) {
  2326. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2327. ERR_FAIL_COND(!rt);
  2328. CopyEffects *copy_effects = CopyEffects::get_singleton();
  2329. ERR_FAIL_NULL(copy_effects);
  2330. if (!rt->backbuffer.is_valid()) {
  2331. _create_render_target_backbuffer(rt);
  2332. }
  2333. Rect2i region;
  2334. if (p_region == Rect2i()) {
  2335. region.size = rt->size;
  2336. } else {
  2337. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2338. if (region.size == Size2i()) {
  2339. return; //nothing to do
  2340. }
  2341. }
  2342. //single texture copy for backbuffer
  2343. copy_effects->set_color(rt->backbuffer_mipmap0, p_color, region, true);
  2344. }
  2345. void TextureStorage::render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region) {
  2346. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2347. ERR_FAIL_COND(!rt);
  2348. CopyEffects *copy_effects = CopyEffects::get_singleton();
  2349. ERR_FAIL_NULL(copy_effects);
  2350. if (!rt->backbuffer.is_valid()) {
  2351. _create_render_target_backbuffer(rt);
  2352. }
  2353. Rect2i region;
  2354. if (p_region == Rect2i()) {
  2355. region.size = rt->size;
  2356. } else {
  2357. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2358. if (region.size == Size2i()) {
  2359. return; //nothing to do
  2360. }
  2361. }
  2362. RD::get_singleton()->draw_command_begin_label("Gaussian Blur Mipmaps2");
  2363. //then mipmap blur
  2364. RID prev_texture = rt->backbuffer_mipmap0;
  2365. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  2366. region.position.x >>= 1;
  2367. region.position.y >>= 1;
  2368. region.size.x = MAX(1, region.size.x >> 1);
  2369. region.size.y = MAX(1, region.size.y >> 1);
  2370. RID mipmap = rt->backbuffer_mipmaps[i];
  2371. copy_effects->gaussian_blur(prev_texture, mipmap, region, true);
  2372. prev_texture = mipmap;
  2373. }
  2374. RD::get_singleton()->draw_command_end_label();
  2375. }
  2376. RID TextureStorage::render_target_get_framebuffer_uniform_set(RID p_render_target) {
  2377. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2378. ERR_FAIL_COND_V(!rt, RID());
  2379. return rt->framebuffer_uniform_set;
  2380. }
  2381. RID TextureStorage::render_target_get_backbuffer_uniform_set(RID p_render_target) {
  2382. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2383. ERR_FAIL_COND_V(!rt, RID());
  2384. return rt->backbuffer_uniform_set;
  2385. }
  2386. void TextureStorage::render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  2387. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2388. ERR_FAIL_COND(!rt);
  2389. rt->framebuffer_uniform_set = p_uniform_set;
  2390. }
  2391. void TextureStorage::render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  2392. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2393. ERR_FAIL_COND(!rt);
  2394. rt->backbuffer_uniform_set = p_uniform_set;
  2395. }
  2396. void TextureStorage::render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) {
  2397. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2398. ERR_FAIL_COND(!rt);
  2399. rt->vrs_mode = p_mode;
  2400. }
  2401. void TextureStorage::render_target_set_vrs_texture(RID p_render_target, RID p_texture) {
  2402. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2403. ERR_FAIL_COND(!rt);
  2404. rt->vrs_texture = p_texture;
  2405. }
  2406. RS::ViewportVRSMode TextureStorage::render_target_get_vrs_mode(RID p_render_target) const {
  2407. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2408. ERR_FAIL_COND_V(!rt, RS::VIEWPORT_VRS_DISABLED);
  2409. return rt->vrs_mode;
  2410. }
  2411. RID TextureStorage::render_target_get_vrs_texture(RID p_render_target) const {
  2412. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2413. ERR_FAIL_COND_V(!rt, RID());
  2414. return rt->vrs_texture;
  2415. }