texture_storage.cpp 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949
  1. /**************************************************************************/
  2. /* texture_storage.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "texture_storage.h"
  31. #include "../effects/copy_effects.h"
  32. #include "../framebuffer_cache_rd.h"
  33. #include "material_storage.h"
  34. #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
  35. using namespace RendererRD;
  36. ///////////////////////////////////////////////////////////////////////////
  37. // TextureStorage::CanvasTexture
  38. void TextureStorage::CanvasTexture::clear_sets() {
  39. if (cleared_cache) {
  40. return;
  41. }
  42. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  43. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  44. for (int k = 0; k < 2; k++) {
  45. if (RD::get_singleton()->uniform_set_is_valid(uniform_sets[i][j][k])) {
  46. RD::get_singleton()->free(uniform_sets[i][j][k]);
  47. uniform_sets[i][j][k] = RID();
  48. }
  49. }
  50. }
  51. }
  52. cleared_cache = true;
  53. }
  54. TextureStorage::CanvasTexture::~CanvasTexture() {
  55. clear_sets();
  56. }
  57. ///////////////////////////////////////////////////////////////////////////
  58. // TextureStorage::Texture
  59. void TextureStorage::Texture::cleanup() {
  60. if (RD::get_singleton()->texture_is_valid(rd_texture_srgb)) {
  61. //erase this first, as it's a dependency of the one below
  62. RD::get_singleton()->free(rd_texture_srgb);
  63. }
  64. if (RD::get_singleton()->texture_is_valid(rd_texture)) {
  65. RD::get_singleton()->free(rd_texture);
  66. }
  67. if (canvas_texture) {
  68. memdelete(canvas_texture);
  69. }
  70. }
  71. ///////////////////////////////////////////////////////////////////////////
  72. // TextureStorage
  73. TextureStorage *TextureStorage::singleton = nullptr;
  74. TextureStorage *TextureStorage::get_singleton() {
  75. return singleton;
  76. }
  77. TextureStorage::TextureStorage() {
  78. singleton = this;
  79. { //create default textures
  80. RD::TextureFormat tformat;
  81. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  82. tformat.width = 4;
  83. tformat.height = 4;
  84. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  85. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  86. Vector<uint8_t> pv;
  87. pv.resize(16 * 4);
  88. for (int i = 0; i < 16; i++) {
  89. // Opaque white.
  90. pv.set(i * 4 + 0, 255);
  91. pv.set(i * 4 + 1, 255);
  92. pv.set(i * 4 + 2, 255);
  93. pv.set(i * 4 + 3, 255);
  94. }
  95. {
  96. Vector<Vector<uint8_t>> vpv;
  97. vpv.push_back(pv);
  98. default_rd_textures[DEFAULT_RD_TEXTURE_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  99. }
  100. for (int i = 0; i < 16; i++) {
  101. // Opaque black.
  102. pv.set(i * 4 + 0, 0);
  103. pv.set(i * 4 + 1, 0);
  104. pv.set(i * 4 + 2, 0);
  105. pv.set(i * 4 + 3, 255);
  106. }
  107. {
  108. Vector<Vector<uint8_t>> vpv;
  109. vpv.push_back(pv);
  110. default_rd_textures[DEFAULT_RD_TEXTURE_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  111. }
  112. for (int i = 0; i < 16; i++) {
  113. // Transparent black.
  114. pv.set(i * 4 + 0, 0);
  115. pv.set(i * 4 + 1, 0);
  116. pv.set(i * 4 + 2, 0);
  117. pv.set(i * 4 + 3, 0);
  118. }
  119. {
  120. Vector<Vector<uint8_t>> vpv;
  121. vpv.push_back(pv);
  122. default_rd_textures[DEFAULT_RD_TEXTURE_TRANSPARENT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  123. }
  124. for (int i = 0; i < 16; i++) {
  125. // Opaque normal map "flat" color.
  126. pv.set(i * 4 + 0, 128);
  127. pv.set(i * 4 + 1, 128);
  128. pv.set(i * 4 + 2, 255);
  129. pv.set(i * 4 + 3, 255);
  130. }
  131. {
  132. Vector<Vector<uint8_t>> vpv;
  133. vpv.push_back(pv);
  134. default_rd_textures[DEFAULT_RD_TEXTURE_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  135. }
  136. for (int i = 0; i < 16; i++) {
  137. // Opaque flowmap "flat" color.
  138. pv.set(i * 4 + 0, 255);
  139. pv.set(i * 4 + 1, 128);
  140. pv.set(i * 4 + 2, 255);
  141. pv.set(i * 4 + 3, 255);
  142. }
  143. {
  144. Vector<Vector<uint8_t>> vpv;
  145. vpv.push_back(pv);
  146. default_rd_textures[DEFAULT_RD_TEXTURE_ANISO] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  147. }
  148. {
  149. RD::TextureFormat tf;
  150. tf.format = RD::DATA_FORMAT_D16_UNORM;
  151. tf.width = 4;
  152. tf.height = 4;
  153. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  154. tf.texture_type = RD::TEXTURE_TYPE_2D;
  155. Vector<uint8_t> sv;
  156. sv.resize(16 * 2);
  157. uint16_t *ptr = (uint16_t *)sv.ptrw();
  158. for (int i = 0; i < 16; i++) {
  159. ptr[i] = Math::make_half_float(1.0f);
  160. }
  161. Vector<Vector<uint8_t>> vpv;
  162. vpv.push_back(sv);
  163. default_rd_textures[DEFAULT_RD_TEXTURE_DEPTH] = RD::get_singleton()->texture_create(tf, RD::TextureView(), vpv);
  164. }
  165. for (int i = 0; i < 16; i++) {
  166. pv.set(i * 4 + 0, 0);
  167. pv.set(i * 4 + 1, 0);
  168. pv.set(i * 4 + 2, 0);
  169. pv.set(i * 4 + 3, 0);
  170. }
  171. default_rd_textures[DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER] = RD::get_singleton()->texture_buffer_create(16, RD::DATA_FORMAT_R8G8B8A8_UNORM, pv);
  172. for (int i = 0; i < 16; i++) {
  173. pv.set(i * 4 + 0, 0);
  174. pv.set(i * 4 + 1, 0);
  175. pv.set(i * 4 + 2, 0);
  176. pv.set(i * 4 + 3, 0);
  177. }
  178. {
  179. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  180. Vector<Vector<uint8_t>> vpv;
  181. vpv.push_back(pv);
  182. default_rd_textures[DEFAULT_RD_TEXTURE_2D_UINT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  183. }
  184. }
  185. { //create default black cubemap array
  186. RD::TextureFormat tformat;
  187. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  188. tformat.width = 4;
  189. tformat.height = 4;
  190. tformat.array_layers = 6;
  191. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  192. tformat.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  193. Vector<uint8_t> pv;
  194. pv.resize(16 * 4);
  195. for (int i = 0; i < 16; i++) {
  196. pv.set(i * 4 + 0, 0);
  197. pv.set(i * 4 + 1, 0);
  198. pv.set(i * 4 + 2, 0);
  199. pv.set(i * 4 + 3, 0);
  200. }
  201. {
  202. Vector<Vector<uint8_t>> vpv;
  203. for (int i = 0; i < 6; i++) {
  204. vpv.push_back(pv);
  205. }
  206. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  207. }
  208. }
  209. { //create default white cubemap array
  210. RD::TextureFormat tformat;
  211. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  212. tformat.width = 4;
  213. tformat.height = 4;
  214. tformat.array_layers = 6;
  215. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  216. tformat.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  217. Vector<uint8_t> pv;
  218. pv.resize(16 * 4);
  219. for (int i = 0; i < 16; i++) {
  220. pv.set(i * 4 + 0, 255);
  221. pv.set(i * 4 + 1, 255);
  222. pv.set(i * 4 + 2, 255);
  223. pv.set(i * 4 + 3, 255);
  224. }
  225. {
  226. Vector<Vector<uint8_t>> vpv;
  227. for (int i = 0; i < 6; i++) {
  228. vpv.push_back(pv);
  229. }
  230. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  231. }
  232. }
  233. { //create default black cubemap
  234. RD::TextureFormat tformat;
  235. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  236. tformat.width = 4;
  237. tformat.height = 4;
  238. tformat.array_layers = 6;
  239. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  240. tformat.texture_type = RD::TEXTURE_TYPE_CUBE;
  241. Vector<uint8_t> pv;
  242. pv.resize(16 * 4);
  243. for (int i = 0; i < 16; i++) {
  244. pv.set(i * 4 + 0, 0);
  245. pv.set(i * 4 + 1, 0);
  246. pv.set(i * 4 + 2, 0);
  247. pv.set(i * 4 + 3, 0);
  248. }
  249. {
  250. Vector<Vector<uint8_t>> vpv;
  251. for (int i = 0; i < 6; i++) {
  252. vpv.push_back(pv);
  253. }
  254. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  255. }
  256. }
  257. { //create default white cubemap
  258. RD::TextureFormat tformat;
  259. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  260. tformat.width = 4;
  261. tformat.height = 4;
  262. tformat.array_layers = 6;
  263. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  264. tformat.texture_type = RD::TEXTURE_TYPE_CUBE;
  265. Vector<uint8_t> pv;
  266. pv.resize(16 * 4);
  267. for (int i = 0; i < 16; i++) {
  268. pv.set(i * 4 + 0, 255);
  269. pv.set(i * 4 + 1, 255);
  270. pv.set(i * 4 + 2, 255);
  271. pv.set(i * 4 + 3, 255);
  272. }
  273. {
  274. Vector<Vector<uint8_t>> vpv;
  275. for (int i = 0; i < 6; i++) {
  276. vpv.push_back(pv);
  277. }
  278. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  279. }
  280. }
  281. { //create default 3D
  282. RD::TextureFormat tformat;
  283. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  284. tformat.width = 4;
  285. tformat.height = 4;
  286. tformat.depth = 4;
  287. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  288. tformat.texture_type = RD::TEXTURE_TYPE_3D;
  289. Vector<uint8_t> pv;
  290. pv.resize(64 * 4);
  291. for (int i = 0; i < 64; i++) {
  292. pv.set(i * 4 + 0, 0);
  293. pv.set(i * 4 + 1, 0);
  294. pv.set(i * 4 + 2, 0);
  295. pv.set(i * 4 + 3, 0);
  296. }
  297. {
  298. Vector<Vector<uint8_t>> vpv;
  299. vpv.push_back(pv);
  300. default_rd_textures[DEFAULT_RD_TEXTURE_3D_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  301. }
  302. for (int i = 0; i < 64; i++) {
  303. pv.set(i * 4 + 0, 255);
  304. pv.set(i * 4 + 1, 255);
  305. pv.set(i * 4 + 2, 255);
  306. pv.set(i * 4 + 3, 255);
  307. }
  308. {
  309. Vector<Vector<uint8_t>> vpv;
  310. vpv.push_back(pv);
  311. default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  312. }
  313. }
  314. { //create default array white
  315. RD::TextureFormat tformat;
  316. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  317. tformat.width = 4;
  318. tformat.height = 4;
  319. tformat.array_layers = 1;
  320. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  321. tformat.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  322. Vector<uint8_t> pv;
  323. pv.resize(16 * 4);
  324. for (int i = 0; i < 16; i++) {
  325. pv.set(i * 4 + 0, 255);
  326. pv.set(i * 4 + 1, 255);
  327. pv.set(i * 4 + 2, 255);
  328. pv.set(i * 4 + 3, 255);
  329. }
  330. {
  331. Vector<Vector<uint8_t>> vpv;
  332. vpv.push_back(pv);
  333. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  334. }
  335. }
  336. { //create default array black
  337. RD::TextureFormat tformat;
  338. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  339. tformat.width = 4;
  340. tformat.height = 4;
  341. tformat.array_layers = 1;
  342. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  343. tformat.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  344. Vector<uint8_t> pv;
  345. pv.resize(16 * 4);
  346. for (int i = 0; i < 16; i++) {
  347. pv.set(i * 4 + 0, 0);
  348. pv.set(i * 4 + 1, 0);
  349. pv.set(i * 4 + 2, 0);
  350. pv.set(i * 4 + 3, 0);
  351. }
  352. {
  353. Vector<Vector<uint8_t>> vpv;
  354. vpv.push_back(pv);
  355. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  356. }
  357. }
  358. { //create default array normal
  359. RD::TextureFormat tformat;
  360. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  361. tformat.width = 4;
  362. tformat.height = 4;
  363. tformat.array_layers = 1;
  364. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  365. tformat.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  366. Vector<uint8_t> pv;
  367. pv.resize(16 * 4);
  368. for (int i = 0; i < 16; i++) {
  369. pv.set(i * 4 + 0, 128);
  370. pv.set(i * 4 + 1, 128);
  371. pv.set(i * 4 + 2, 255);
  372. pv.set(i * 4 + 3, 255);
  373. }
  374. {
  375. Vector<Vector<uint8_t>> vpv;
  376. vpv.push_back(pv);
  377. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  378. }
  379. }
  380. { //create default array depth
  381. RD::TextureFormat tformat;
  382. tformat.format = RD::DATA_FORMAT_D16_UNORM;
  383. tformat.width = 4;
  384. tformat.height = 4;
  385. tformat.array_layers = 1;
  386. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  387. tformat.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  388. Vector<uint8_t> sv;
  389. sv.resize(16 * 2);
  390. uint16_t *ptr = (uint16_t *)sv.ptrw();
  391. for (int i = 0; i < 16; i++) {
  392. ptr[i] = Math::make_half_float(1.0f);
  393. }
  394. {
  395. Vector<Vector<uint8_t>> vsv;
  396. vsv.push_back(sv);
  397. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_DEPTH] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vsv);
  398. }
  399. }
  400. { // default atlas texture
  401. RD::TextureFormat tformat;
  402. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  403. tformat.width = 4;
  404. tformat.height = 4;
  405. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  406. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  407. Vector<uint8_t> pv;
  408. pv.resize(16 * 4);
  409. for (int i = 0; i < 16; i++) {
  410. pv.set(i * 4 + 0, 0);
  411. pv.set(i * 4 + 1, 0);
  412. pv.set(i * 4 + 2, 0);
  413. pv.set(i * 4 + 3, 255);
  414. }
  415. {
  416. Vector<Vector<uint8_t>> vpv;
  417. vpv.push_back(pv);
  418. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  419. decal_atlas.texture_srgb = decal_atlas.texture;
  420. }
  421. }
  422. { //create default VRS
  423. RD::TextureFormat tformat;
  424. tformat.format = RD::DATA_FORMAT_R8_UINT;
  425. tformat.width = 4;
  426. tformat.height = 4;
  427. tformat.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_VRS_ATTACHMENT_BIT;
  428. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  429. if (!RD::get_singleton()->has_feature(RD::SUPPORTS_ATTACHMENT_VRS)) {
  430. tformat.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  431. }
  432. Vector<uint8_t> pv;
  433. pv.resize(4 * 4);
  434. for (int i = 0; i < 4 * 4; i++) {
  435. pv.set(i, 0);
  436. }
  437. {
  438. Vector<Vector<uint8_t>> vpv;
  439. vpv.push_back(pv);
  440. default_rd_textures[DEFAULT_RD_TEXTURE_VRS] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  441. }
  442. }
  443. {
  444. Vector<String> sdf_modes;
  445. sdf_modes.push_back("\n#define MODE_LOAD\n");
  446. sdf_modes.push_back("\n#define MODE_LOAD_SHRINK\n");
  447. sdf_modes.push_back("\n#define MODE_PROCESS\n");
  448. sdf_modes.push_back("\n#define MODE_PROCESS_OPTIMIZED\n");
  449. sdf_modes.push_back("\n#define MODE_STORE\n");
  450. sdf_modes.push_back("\n#define MODE_STORE_SHRINK\n");
  451. rt_sdf.shader.initialize(sdf_modes);
  452. rt_sdf.shader_version = rt_sdf.shader.version_create();
  453. for (int i = 0; i < RenderTargetSDF::SHADER_MAX; i++) {
  454. rt_sdf.pipelines[i] = RD::get_singleton()->compute_pipeline_create(rt_sdf.shader.version_get_shader(rt_sdf.shader_version, i));
  455. }
  456. }
  457. }
  458. TextureStorage::~TextureStorage() {
  459. rt_sdf.shader.version_free(rt_sdf.shader_version);
  460. free_decal_data();
  461. if (decal_atlas.textures.size()) {
  462. ERR_PRINT("Decal Atlas: " + itos(decal_atlas.textures.size()) + " textures were not removed from the atlas.");
  463. }
  464. if (decal_atlas.texture.is_valid()) {
  465. RD::get_singleton()->free(decal_atlas.texture);
  466. }
  467. //def textures
  468. for (int i = 0; i < DEFAULT_RD_TEXTURE_MAX; i++) {
  469. if (default_rd_textures[i].is_valid()) {
  470. RD::get_singleton()->free(default_rd_textures[i]);
  471. }
  472. }
  473. singleton = nullptr;
  474. }
  475. bool TextureStorage::free(RID p_rid) {
  476. if (owns_texture(p_rid)) {
  477. texture_free(p_rid);
  478. return true;
  479. } else if (owns_canvas_texture(p_rid)) {
  480. canvas_texture_free(p_rid);
  481. return true;
  482. } else if (owns_decal(p_rid)) {
  483. decal_free(p_rid);
  484. return true;
  485. } else if (owns_decal_instance(p_rid)) {
  486. decal_instance_free(p_rid);
  487. return true;
  488. } else if (owns_render_target(p_rid)) {
  489. render_target_free(p_rid);
  490. return true;
  491. }
  492. return false;
  493. }
  494. bool TextureStorage::can_create_resources_async() const {
  495. return true;
  496. }
  497. /* Canvas Texture API */
  498. RID TextureStorage::canvas_texture_allocate() {
  499. return canvas_texture_owner.allocate_rid();
  500. }
  501. void TextureStorage::canvas_texture_initialize(RID p_rid) {
  502. canvas_texture_owner.initialize_rid(p_rid);
  503. }
  504. void TextureStorage::canvas_texture_free(RID p_rid) {
  505. canvas_texture_owner.free(p_rid);
  506. }
  507. void TextureStorage::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
  508. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  509. ERR_FAIL_NULL(ct);
  510. switch (p_channel) {
  511. case RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE: {
  512. ct->diffuse = p_texture;
  513. } break;
  514. case RS::CANVAS_TEXTURE_CHANNEL_NORMAL: {
  515. ct->normal_map = p_texture;
  516. } break;
  517. case RS::CANVAS_TEXTURE_CHANNEL_SPECULAR: {
  518. ct->specular = p_texture;
  519. } break;
  520. }
  521. ct->clear_sets();
  522. }
  523. void TextureStorage::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess) {
  524. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  525. ERR_FAIL_NULL(ct);
  526. ct->specular_color.r = p_specular_color.r;
  527. ct->specular_color.g = p_specular_color.g;
  528. ct->specular_color.b = p_specular_color.b;
  529. ct->specular_color.a = p_shininess;
  530. ct->clear_sets();
  531. }
  532. void TextureStorage::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
  533. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  534. ERR_FAIL_NULL(ct);
  535. ct->texture_filter = p_filter;
  536. ct->clear_sets();
  537. }
  538. void TextureStorage::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
  539. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  540. ERR_FAIL_NULL(ct);
  541. ct->texture_repeat = p_repeat;
  542. ct->clear_sets();
  543. }
  544. 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, bool p_use_srgb, RID &r_uniform_set, Size2i &r_size, Color &r_specular_shininess, bool &r_use_normal, bool &r_use_specular, bool p_texture_is_data) {
  545. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  546. CanvasTexture *ct = nullptr;
  547. Texture *t = get_texture(p_texture);
  548. // TODO once we have our texture storage split off we'll look into moving this code into canvas_texture
  549. if (t) {
  550. //regular texture
  551. if (!t->canvas_texture) {
  552. t->canvas_texture = memnew(CanvasTexture);
  553. t->canvas_texture->diffuse = p_texture;
  554. }
  555. ct = t->canvas_texture;
  556. if (t->render_target) {
  557. t->render_target->was_used = true;
  558. }
  559. } else {
  560. ct = canvas_texture_owner.get_or_null(p_texture);
  561. }
  562. if (!ct) {
  563. return false; //invalid texture RID
  564. }
  565. RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
  566. ERR_FAIL_COND_V(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, false);
  567. RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
  568. ERR_FAIL_COND_V(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT, false);
  569. RID uniform_set = ct->uniform_sets[filter][repeat][int(p_use_srgb)];
  570. if (!RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  571. //create and update
  572. Vector<RD::Uniform> uniforms;
  573. { //diffuse
  574. RD::Uniform u;
  575. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  576. u.binding = 0;
  577. t = get_texture(ct->diffuse);
  578. if (!t) {
  579. u.append_id(texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE));
  580. ct->size_cache = Size2i(1, 1);
  581. } else {
  582. u.append_id(t->rd_texture_srgb.is_valid() && p_use_srgb && !p_texture_is_data ? t->rd_texture_srgb : t->rd_texture);
  583. ct->size_cache = Size2i(t->width_2d, t->height_2d);
  584. if (t->render_target) {
  585. t->render_target->was_used = true;
  586. }
  587. }
  588. uniforms.push_back(u);
  589. }
  590. { //normal
  591. RD::Uniform u;
  592. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  593. u.binding = 1;
  594. t = get_texture(ct->normal_map);
  595. if (!t) {
  596. u.append_id(texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL));
  597. ct->use_normal_cache = false;
  598. } else {
  599. u.append_id(t->rd_texture);
  600. ct->use_normal_cache = true;
  601. if (t->render_target) {
  602. t->render_target->was_used = true;
  603. }
  604. }
  605. uniforms.push_back(u);
  606. }
  607. { //specular
  608. RD::Uniform u;
  609. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  610. u.binding = 2;
  611. t = get_texture(ct->specular);
  612. if (!t) {
  613. u.append_id(texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE));
  614. ct->use_specular_cache = false;
  615. } else {
  616. u.append_id(t->rd_texture);
  617. ct->use_specular_cache = true;
  618. if (t->render_target) {
  619. t->render_target->was_used = true;
  620. }
  621. }
  622. uniforms.push_back(u);
  623. }
  624. { //sampler
  625. RD::Uniform u;
  626. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  627. u.binding = 3;
  628. u.append_id(material_storage->sampler_rd_get_default(filter, repeat));
  629. uniforms.push_back(u);
  630. }
  631. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_base_shader, p_base_set);
  632. ct->uniform_sets[filter][repeat][int(p_use_srgb)] = uniform_set;
  633. ct->cleared_cache = false;
  634. }
  635. r_uniform_set = uniform_set;
  636. r_size = ct->size_cache;
  637. r_specular_shininess = ct->specular_color;
  638. r_use_normal = ct->use_normal_cache;
  639. r_use_specular = ct->use_specular_cache;
  640. return true;
  641. }
  642. /* Texture API */
  643. RID TextureStorage::texture_allocate() {
  644. return texture_owner.allocate_rid();
  645. }
  646. void TextureStorage::texture_free(RID p_texture) {
  647. Texture *t = texture_owner.get_or_null(p_texture);
  648. ERR_FAIL_COND(!t);
  649. ERR_FAIL_COND(t->is_render_target);
  650. t->cleanup();
  651. if (t->is_proxy && t->proxy_to.is_valid()) {
  652. Texture *proxy_to = texture_owner.get_or_null(t->proxy_to);
  653. if (proxy_to) {
  654. proxy_to->proxies.erase(p_texture);
  655. }
  656. }
  657. decal_atlas_remove_texture(p_texture);
  658. for (int i = 0; i < t->proxies.size(); i++) {
  659. Texture *p = texture_owner.get_or_null(t->proxies[i]);
  660. ERR_CONTINUE(!p);
  661. p->proxy_to = RID();
  662. p->rd_texture = RID();
  663. p->rd_texture_srgb = RID();
  664. }
  665. texture_owner.free(p_texture);
  666. }
  667. void TextureStorage::texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) {
  668. ERR_FAIL_COND(p_image.is_null());
  669. TextureToRDFormat ret_format;
  670. Ref<Image> image = _validate_texture_format(p_image, ret_format);
  671. Texture texture;
  672. texture.type = TextureStorage::TYPE_2D;
  673. texture.width = p_image->get_width();
  674. texture.height = p_image->get_height();
  675. texture.layers = 1;
  676. texture.mipmaps = p_image->get_mipmap_count() + 1;
  677. texture.depth = 1;
  678. texture.format = p_image->get_format();
  679. texture.validated_format = image->get_format();
  680. texture.rd_type = RD::TEXTURE_TYPE_2D;
  681. texture.rd_format = ret_format.format;
  682. texture.rd_format_srgb = ret_format.format_srgb;
  683. RD::TextureFormat rd_format;
  684. RD::TextureView rd_view;
  685. { //attempt register
  686. rd_format.format = texture.rd_format;
  687. rd_format.width = texture.width;
  688. rd_format.height = texture.height;
  689. rd_format.depth = 1;
  690. rd_format.array_layers = 1;
  691. rd_format.mipmaps = texture.mipmaps;
  692. rd_format.texture_type = texture.rd_type;
  693. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  694. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  695. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  696. rd_format.shareable_formats.push_back(texture.rd_format);
  697. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  698. }
  699. }
  700. {
  701. rd_view.swizzle_r = ret_format.swizzle_r;
  702. rd_view.swizzle_g = ret_format.swizzle_g;
  703. rd_view.swizzle_b = ret_format.swizzle_b;
  704. rd_view.swizzle_a = ret_format.swizzle_a;
  705. }
  706. Vector<uint8_t> data = image->get_data(); //use image data
  707. Vector<Vector<uint8_t>> data_slices;
  708. data_slices.push_back(data);
  709. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  710. ERR_FAIL_COND(texture.rd_texture.is_null());
  711. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  712. rd_view.format_override = texture.rd_format_srgb;
  713. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  714. if (texture.rd_texture_srgb.is_null()) {
  715. RD::get_singleton()->free(texture.rd_texture);
  716. ERR_FAIL_COND(texture.rd_texture_srgb.is_null());
  717. }
  718. }
  719. //used for 2D, overridable
  720. texture.width_2d = texture.width;
  721. texture.height_2d = texture.height;
  722. texture.is_render_target = false;
  723. texture.rd_view = rd_view;
  724. texture.is_proxy = false;
  725. texture_owner.initialize_rid(p_texture, texture);
  726. }
  727. void TextureStorage::texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  728. ERR_FAIL_COND(p_layers.size() == 0);
  729. ERR_FAIL_COND(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP && p_layers.size() != 6);
  730. ERR_FAIL_COND(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP_ARRAY && (p_layers.size() < 6 || (p_layers.size() % 6) != 0));
  731. TextureToRDFormat ret_format;
  732. Vector<Ref<Image>> images;
  733. {
  734. int valid_width = 0;
  735. int valid_height = 0;
  736. bool valid_mipmaps = false;
  737. Image::Format valid_format = Image::FORMAT_MAX;
  738. for (int i = 0; i < p_layers.size(); i++) {
  739. ERR_FAIL_COND(p_layers[i]->is_empty());
  740. if (i == 0) {
  741. valid_width = p_layers[i]->get_width();
  742. valid_height = p_layers[i]->get_height();
  743. valid_format = p_layers[i]->get_format();
  744. valid_mipmaps = p_layers[i]->has_mipmaps();
  745. } else {
  746. ERR_FAIL_COND(p_layers[i]->get_width() != valid_width);
  747. ERR_FAIL_COND(p_layers[i]->get_height() != valid_height);
  748. ERR_FAIL_COND(p_layers[i]->get_format() != valid_format);
  749. ERR_FAIL_COND(p_layers[i]->has_mipmaps() != valid_mipmaps);
  750. }
  751. images.push_back(_validate_texture_format(p_layers[i], ret_format));
  752. }
  753. }
  754. Texture texture;
  755. texture.type = TextureStorage::TYPE_LAYERED;
  756. texture.layered_type = p_layered_type;
  757. texture.width = p_layers[0]->get_width();
  758. texture.height = p_layers[0]->get_height();
  759. texture.layers = p_layers.size();
  760. texture.mipmaps = p_layers[0]->get_mipmap_count() + 1;
  761. texture.depth = 1;
  762. texture.format = p_layers[0]->get_format();
  763. texture.validated_format = images[0]->get_format();
  764. switch (p_layered_type) {
  765. case RS::TEXTURE_LAYERED_2D_ARRAY: {
  766. texture.rd_type = RD::TEXTURE_TYPE_2D_ARRAY;
  767. } break;
  768. case RS::TEXTURE_LAYERED_CUBEMAP: {
  769. texture.rd_type = RD::TEXTURE_TYPE_CUBE;
  770. } break;
  771. case RS::TEXTURE_LAYERED_CUBEMAP_ARRAY: {
  772. texture.rd_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  773. } break;
  774. default:
  775. ERR_FAIL(); // Shouldn't happen, silence warnings.
  776. }
  777. texture.rd_format = ret_format.format;
  778. texture.rd_format_srgb = ret_format.format_srgb;
  779. RD::TextureFormat rd_format;
  780. RD::TextureView rd_view;
  781. { //attempt register
  782. rd_format.format = texture.rd_format;
  783. rd_format.width = texture.width;
  784. rd_format.height = texture.height;
  785. rd_format.depth = 1;
  786. rd_format.array_layers = texture.layers;
  787. rd_format.mipmaps = texture.mipmaps;
  788. rd_format.texture_type = texture.rd_type;
  789. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  790. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  791. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  792. rd_format.shareable_formats.push_back(texture.rd_format);
  793. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  794. }
  795. }
  796. {
  797. rd_view.swizzle_r = ret_format.swizzle_r;
  798. rd_view.swizzle_g = ret_format.swizzle_g;
  799. rd_view.swizzle_b = ret_format.swizzle_b;
  800. rd_view.swizzle_a = ret_format.swizzle_a;
  801. }
  802. Vector<Vector<uint8_t>> data_slices;
  803. for (int i = 0; i < images.size(); i++) {
  804. Vector<uint8_t> data = images[i]->get_data(); //use image data
  805. data_slices.push_back(data);
  806. }
  807. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  808. ERR_FAIL_COND(texture.rd_texture.is_null());
  809. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  810. rd_view.format_override = texture.rd_format_srgb;
  811. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  812. if (texture.rd_texture_srgb.is_null()) {
  813. RD::get_singleton()->free(texture.rd_texture);
  814. ERR_FAIL_COND(texture.rd_texture_srgb.is_null());
  815. }
  816. }
  817. //used for 2D, overridable
  818. texture.width_2d = texture.width;
  819. texture.height_2d = texture.height;
  820. texture.is_render_target = false;
  821. texture.rd_view = rd_view;
  822. texture.is_proxy = false;
  823. texture_owner.initialize_rid(p_texture, texture);
  824. }
  825. 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) {
  826. ERR_FAIL_COND(p_data.size() == 0);
  827. Image::Image3DValidateError verr = Image::validate_3d_image(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
  828. if (verr != Image::VALIDATE_3D_OK) {
  829. ERR_FAIL_MSG(Image::get_3d_image_validation_error_text(verr));
  830. }
  831. TextureToRDFormat ret_format;
  832. Image::Format validated_format = Image::FORMAT_MAX;
  833. Vector<uint8_t> all_data;
  834. uint32_t mipmap_count = 0;
  835. Vector<Texture::BufferSlice3D> slices;
  836. {
  837. Vector<Ref<Image>> images;
  838. uint32_t all_data_size = 0;
  839. images.resize(p_data.size());
  840. for (int i = 0; i < p_data.size(); i++) {
  841. TextureToRDFormat f;
  842. images.write[i] = _validate_texture_format(p_data[i], f);
  843. if (i == 0) {
  844. ret_format = f;
  845. validated_format = images[0]->get_format();
  846. }
  847. all_data_size += images[i]->get_data().size();
  848. }
  849. all_data.resize(all_data_size); //consolidate all data here
  850. uint32_t offset = 0;
  851. Size2i prev_size;
  852. for (int i = 0; i < p_data.size(); i++) {
  853. uint32_t s = images[i]->get_data().size();
  854. memcpy(&all_data.write[offset], images[i]->get_data().ptr(), s);
  855. {
  856. Texture::BufferSlice3D slice;
  857. slice.size.width = images[i]->get_width();
  858. slice.size.height = images[i]->get_height();
  859. slice.offset = offset;
  860. slice.buffer_size = s;
  861. slices.push_back(slice);
  862. }
  863. offset += s;
  864. Size2i img_size(images[i]->get_width(), images[i]->get_height());
  865. if (img_size != prev_size) {
  866. mipmap_count++;
  867. }
  868. prev_size = img_size;
  869. }
  870. }
  871. Texture texture;
  872. texture.type = TextureStorage::TYPE_3D;
  873. texture.width = p_width;
  874. texture.height = p_height;
  875. texture.depth = p_depth;
  876. texture.mipmaps = mipmap_count;
  877. texture.format = p_data[0]->get_format();
  878. texture.validated_format = validated_format;
  879. texture.buffer_size_3d = all_data.size();
  880. texture.buffer_slices_3d = slices;
  881. texture.rd_type = RD::TEXTURE_TYPE_3D;
  882. texture.rd_format = ret_format.format;
  883. texture.rd_format_srgb = ret_format.format_srgb;
  884. RD::TextureFormat rd_format;
  885. RD::TextureView rd_view;
  886. { //attempt register
  887. rd_format.format = texture.rd_format;
  888. rd_format.width = texture.width;
  889. rd_format.height = texture.height;
  890. rd_format.depth = texture.depth;
  891. rd_format.array_layers = 1;
  892. rd_format.mipmaps = texture.mipmaps;
  893. rd_format.texture_type = texture.rd_type;
  894. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  895. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  896. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  897. rd_format.shareable_formats.push_back(texture.rd_format);
  898. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  899. }
  900. }
  901. {
  902. rd_view.swizzle_r = ret_format.swizzle_r;
  903. rd_view.swizzle_g = ret_format.swizzle_g;
  904. rd_view.swizzle_b = ret_format.swizzle_b;
  905. rd_view.swizzle_a = ret_format.swizzle_a;
  906. }
  907. Vector<Vector<uint8_t>> data_slices;
  908. data_slices.push_back(all_data); //one slice
  909. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  910. ERR_FAIL_COND(texture.rd_texture.is_null());
  911. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  912. rd_view.format_override = texture.rd_format_srgb;
  913. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  914. if (texture.rd_texture_srgb.is_null()) {
  915. RD::get_singleton()->free(texture.rd_texture);
  916. ERR_FAIL_COND(texture.rd_texture_srgb.is_null());
  917. }
  918. }
  919. //used for 2D, overridable
  920. texture.width_2d = texture.width;
  921. texture.height_2d = texture.height;
  922. texture.is_render_target = false;
  923. texture.rd_view = rd_view;
  924. texture.is_proxy = false;
  925. texture_owner.initialize_rid(p_texture, texture);
  926. }
  927. void TextureStorage::texture_proxy_initialize(RID p_texture, RID p_base) {
  928. Texture *tex = texture_owner.get_or_null(p_base);
  929. ERR_FAIL_COND(!tex);
  930. Texture proxy_tex = *tex;
  931. proxy_tex.rd_view.format_override = tex->rd_format;
  932. proxy_tex.rd_texture = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  933. if (proxy_tex.rd_texture_srgb.is_valid()) {
  934. proxy_tex.rd_view.format_override = tex->rd_format_srgb;
  935. proxy_tex.rd_texture_srgb = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  936. }
  937. proxy_tex.proxy_to = p_base;
  938. proxy_tex.is_render_target = false;
  939. proxy_tex.is_proxy = true;
  940. proxy_tex.proxies.clear();
  941. texture_owner.initialize_rid(p_texture, proxy_tex);
  942. tex->proxies.push_back(p_texture);
  943. }
  944. void TextureStorage::_texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate) {
  945. ERR_FAIL_COND(p_image.is_null() || p_image->is_empty());
  946. Texture *tex = texture_owner.get_or_null(p_texture);
  947. ERR_FAIL_COND(!tex);
  948. ERR_FAIL_COND(tex->is_render_target);
  949. ERR_FAIL_COND(p_image->get_width() != tex->width || p_image->get_height() != tex->height);
  950. ERR_FAIL_COND(p_image->get_format() != tex->format);
  951. if (tex->type == TextureStorage::TYPE_LAYERED) {
  952. ERR_FAIL_INDEX(p_layer, tex->layers);
  953. }
  954. #ifdef TOOLS_ENABLED
  955. tex->image_cache_2d.unref();
  956. #endif
  957. TextureToRDFormat f;
  958. Ref<Image> validated = _validate_texture_format(p_image, f);
  959. RD::get_singleton()->texture_update(tex->rd_texture, p_layer, validated->get_data());
  960. }
  961. void TextureStorage::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  962. _texture_2d_update(p_texture, p_image, p_layer, false);
  963. }
  964. void TextureStorage::texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) {
  965. Texture *tex = texture_owner.get_or_null(p_texture);
  966. ERR_FAIL_COND(!tex);
  967. ERR_FAIL_COND(tex->type != TextureStorage::TYPE_3D);
  968. Image::Image3DValidateError verr = Image::validate_3d_image(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps > 1, p_data);
  969. if (verr != Image::VALIDATE_3D_OK) {
  970. ERR_FAIL_MSG(Image::get_3d_image_validation_error_text(verr));
  971. }
  972. Vector<uint8_t> all_data;
  973. {
  974. Vector<Ref<Image>> images;
  975. uint32_t all_data_size = 0;
  976. images.resize(p_data.size());
  977. for (int i = 0; i < p_data.size(); i++) {
  978. Ref<Image> image = p_data[i];
  979. if (image->get_format() != tex->validated_format) {
  980. image = image->duplicate();
  981. image->convert(tex->validated_format);
  982. }
  983. all_data_size += image->get_data().size();
  984. images.write[i] = image;
  985. }
  986. all_data.resize(all_data_size); //consolidate all data here
  987. uint32_t offset = 0;
  988. for (int i = 0; i < p_data.size(); i++) {
  989. uint32_t s = images[i]->get_data().size();
  990. memcpy(&all_data.write[offset], images[i]->get_data().ptr(), s);
  991. offset += s;
  992. }
  993. }
  994. RD::get_singleton()->texture_update(tex->rd_texture, 0, all_data);
  995. }
  996. void TextureStorage::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  997. Texture *tex = texture_owner.get_or_null(p_texture);
  998. ERR_FAIL_COND(!tex);
  999. ERR_FAIL_COND(!tex->is_proxy);
  1000. Texture *proxy_to = texture_owner.get_or_null(p_proxy_to);
  1001. ERR_FAIL_COND(!proxy_to);
  1002. ERR_FAIL_COND(proxy_to->is_proxy);
  1003. if (tex->proxy_to.is_valid()) {
  1004. //unlink proxy
  1005. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  1006. RD::get_singleton()->free(tex->rd_texture);
  1007. tex->rd_texture = RID();
  1008. }
  1009. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  1010. RD::get_singleton()->free(tex->rd_texture_srgb);
  1011. tex->rd_texture_srgb = RID();
  1012. }
  1013. Texture *prev_tex = texture_owner.get_or_null(tex->proxy_to);
  1014. ERR_FAIL_COND(!prev_tex);
  1015. prev_tex->proxies.erase(p_texture);
  1016. }
  1017. // Copy canvas_texture so it doesn't leak.
  1018. CanvasTexture *canvas_texture = tex->canvas_texture;
  1019. *tex = *proxy_to;
  1020. tex->proxy_to = p_proxy_to;
  1021. tex->is_render_target = false;
  1022. tex->is_proxy = true;
  1023. tex->proxies.clear();
  1024. proxy_to->proxies.push_back(p_texture);
  1025. tex->canvas_texture = canvas_texture;
  1026. tex->rd_view.format_override = tex->rd_format;
  1027. tex->rd_texture = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  1028. if (tex->rd_texture_srgb.is_valid()) {
  1029. tex->rd_view.format_override = tex->rd_format_srgb;
  1030. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  1031. }
  1032. }
  1033. //these two APIs can be used together or in combination with the others.
  1034. void TextureStorage::texture_2d_placeholder_initialize(RID p_texture) {
  1035. //this could be better optimized to reuse an existing image , done this way
  1036. //for now to get it working
  1037. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  1038. image->fill(Color(1, 0, 1, 1));
  1039. texture_2d_initialize(p_texture, image);
  1040. }
  1041. void TextureStorage::texture_2d_layered_placeholder_initialize(RID p_texture, RS::TextureLayeredType p_layered_type) {
  1042. //this could be better optimized to reuse an existing image , done this way
  1043. //for now to get it working
  1044. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  1045. image->fill(Color(1, 0, 1, 1));
  1046. Vector<Ref<Image>> images;
  1047. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  1048. images.push_back(image);
  1049. } else {
  1050. //cube
  1051. for (int i = 0; i < 6; i++) {
  1052. images.push_back(image);
  1053. }
  1054. }
  1055. texture_2d_layered_initialize(p_texture, images, p_layered_type);
  1056. }
  1057. void TextureStorage::texture_3d_placeholder_initialize(RID p_texture) {
  1058. //this could be better optimized to reuse an existing image , done this way
  1059. //for now to get it working
  1060. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  1061. image->fill(Color(1, 0, 1, 1));
  1062. Vector<Ref<Image>> images;
  1063. //cube
  1064. for (int i = 0; i < 4; i++) {
  1065. images.push_back(image);
  1066. }
  1067. texture_3d_initialize(p_texture, Image::FORMAT_RGBA8, 4, 4, 4, false, images);
  1068. }
  1069. Ref<Image> TextureStorage::texture_2d_get(RID p_texture) const {
  1070. Texture *tex = texture_owner.get_or_null(p_texture);
  1071. ERR_FAIL_COND_V(!tex, Ref<Image>());
  1072. #ifdef TOOLS_ENABLED
  1073. if (tex->image_cache_2d.is_valid() && !tex->is_render_target) {
  1074. return tex->image_cache_2d;
  1075. }
  1076. #endif
  1077. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  1078. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  1079. Ref<Image> image;
  1080. // Expand RGB10_A2 into RGBAH. This is needed for capturing viewport data
  1081. // when using the mobile renderer with HDR mode on.
  1082. if (tex->rd_format == RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32) {
  1083. Vector<uint8_t> new_data;
  1084. new_data.resize(data.size() * 2);
  1085. uint16_t *ndp = (uint16_t *)new_data.ptr();
  1086. uint32_t *ptr = (uint32_t *)data.ptr();
  1087. uint32_t num_pixels = data.size() / 4;
  1088. for (uint32_t ofs = 0; ofs < num_pixels; ofs++) {
  1089. uint32_t px = ptr[ofs];
  1090. uint32_t r = (px & 0x3FF);
  1091. uint32_t g = ((px >> 10) & 0x3FF);
  1092. uint32_t b = ((px >> 20) & 0x3FF);
  1093. uint32_t a = ((px >> 30) & 0x3);
  1094. ndp[ofs * 4 + 0] = Math::make_half_float(float(r) / 1023.0);
  1095. ndp[ofs * 4 + 1] = Math::make_half_float(float(g) / 1023.0);
  1096. ndp[ofs * 4 + 2] = Math::make_half_float(float(b) / 1023.0);
  1097. ndp[ofs * 4 + 3] = Math::make_half_float(float(a) / 3.0);
  1098. }
  1099. image = Image::create_from_data(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, new_data);
  1100. } else {
  1101. image = Image::create_from_data(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  1102. }
  1103. ERR_FAIL_COND_V(image->is_empty(), Ref<Image>());
  1104. if (tex->format != tex->validated_format) {
  1105. image->convert(tex->format);
  1106. }
  1107. #ifdef TOOLS_ENABLED
  1108. if (Engine::get_singleton()->is_editor_hint() && !tex->is_render_target) {
  1109. tex->image_cache_2d = image;
  1110. }
  1111. #endif
  1112. return image;
  1113. }
  1114. Ref<Image> TextureStorage::texture_2d_layer_get(RID p_texture, int p_layer) const {
  1115. Texture *tex = texture_owner.get_or_null(p_texture);
  1116. ERR_FAIL_COND_V(!tex, Ref<Image>());
  1117. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, p_layer);
  1118. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  1119. Ref<Image> image = Image::create_from_data(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  1120. ERR_FAIL_COND_V(image->is_empty(), Ref<Image>());
  1121. if (tex->format != tex->validated_format) {
  1122. image->convert(tex->format);
  1123. }
  1124. return image;
  1125. }
  1126. Vector<Ref<Image>> TextureStorage::texture_3d_get(RID p_texture) const {
  1127. Texture *tex = texture_owner.get_or_null(p_texture);
  1128. ERR_FAIL_COND_V(!tex, Vector<Ref<Image>>());
  1129. ERR_FAIL_COND_V(tex->type != TextureStorage::TYPE_3D, Vector<Ref<Image>>());
  1130. Vector<uint8_t> all_data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  1131. ERR_FAIL_COND_V(all_data.size() != (int)tex->buffer_size_3d, Vector<Ref<Image>>());
  1132. Vector<Ref<Image>> ret;
  1133. for (int i = 0; i < tex->buffer_slices_3d.size(); i++) {
  1134. const Texture::BufferSlice3D &bs = tex->buffer_slices_3d[i];
  1135. ERR_FAIL_COND_V(bs.offset >= (uint32_t)all_data.size(), Vector<Ref<Image>>());
  1136. ERR_FAIL_COND_V(bs.offset + bs.buffer_size > (uint32_t)all_data.size(), Vector<Ref<Image>>());
  1137. Vector<uint8_t> sub_region = all_data.slice(bs.offset, bs.offset + bs.buffer_size);
  1138. Ref<Image> img = Image::create_from_data(bs.size.width, bs.size.height, false, tex->validated_format, sub_region);
  1139. ERR_FAIL_COND_V(img->is_empty(), Vector<Ref<Image>>());
  1140. if (tex->format != tex->validated_format) {
  1141. img->convert(tex->format);
  1142. }
  1143. ret.push_back(img);
  1144. }
  1145. return ret;
  1146. }
  1147. void TextureStorage::texture_replace(RID p_texture, RID p_by_texture) {
  1148. Texture *tex = texture_owner.get_or_null(p_texture);
  1149. ERR_FAIL_COND(!tex);
  1150. ERR_FAIL_COND(tex->proxy_to.is_valid()); //can't replace proxy
  1151. Texture *by_tex = texture_owner.get_or_null(p_by_texture);
  1152. ERR_FAIL_COND(!by_tex);
  1153. ERR_FAIL_COND(by_tex->proxy_to.is_valid()); //can't replace proxy
  1154. if (tex == by_tex) {
  1155. return;
  1156. }
  1157. if (tex->rd_texture_srgb.is_valid()) {
  1158. RD::get_singleton()->free(tex->rd_texture_srgb);
  1159. }
  1160. RD::get_singleton()->free(tex->rd_texture);
  1161. if (tex->canvas_texture) {
  1162. memdelete(tex->canvas_texture);
  1163. tex->canvas_texture = nullptr;
  1164. }
  1165. Vector<RID> proxies_to_update = tex->proxies;
  1166. Vector<RID> proxies_to_redirect = by_tex->proxies;
  1167. *tex = *by_tex;
  1168. tex->proxies = proxies_to_update; //restore proxies, so they can be updated
  1169. if (tex->canvas_texture) {
  1170. tex->canvas_texture->diffuse = p_texture; //update
  1171. }
  1172. for (int i = 0; i < proxies_to_update.size(); i++) {
  1173. texture_proxy_update(proxies_to_update[i], p_texture);
  1174. }
  1175. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  1176. texture_proxy_update(proxies_to_redirect[i], p_texture);
  1177. }
  1178. //delete last, so proxies can be updated
  1179. texture_owner.free(p_by_texture);
  1180. decal_atlas_mark_dirty_on_texture(p_texture);
  1181. }
  1182. void TextureStorage::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  1183. Texture *tex = texture_owner.get_or_null(p_texture);
  1184. ERR_FAIL_COND(!tex);
  1185. ERR_FAIL_COND(tex->type != TextureStorage::TYPE_2D);
  1186. tex->width_2d = p_width;
  1187. tex->height_2d = p_height;
  1188. }
  1189. void TextureStorage::texture_set_path(RID p_texture, const String &p_path) {
  1190. Texture *tex = texture_owner.get_or_null(p_texture);
  1191. ERR_FAIL_COND(!tex);
  1192. tex->path = p_path;
  1193. }
  1194. String TextureStorage::texture_get_path(RID p_texture) const {
  1195. Texture *tex = texture_owner.get_or_null(p_texture);
  1196. ERR_FAIL_COND_V(!tex, String());
  1197. return tex->path;
  1198. }
  1199. Image::Format TextureStorage::texture_get_format(RID p_texture) const {
  1200. Texture *tex = texture_owner.get_or_null(p_texture);
  1201. ERR_FAIL_COND_V(!tex, Image::FORMAT_MAX);
  1202. return tex->format;
  1203. }
  1204. void TextureStorage::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1205. Texture *tex = texture_owner.get_or_null(p_texture);
  1206. ERR_FAIL_COND(!tex);
  1207. tex->detect_3d_callback_ud = p_userdata;
  1208. tex->detect_3d_callback = p_callback;
  1209. }
  1210. void TextureStorage::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1211. Texture *tex = texture_owner.get_or_null(p_texture);
  1212. ERR_FAIL_COND(!tex);
  1213. tex->detect_normal_callback_ud = p_userdata;
  1214. tex->detect_normal_callback = p_callback;
  1215. }
  1216. void TextureStorage::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  1217. Texture *tex = texture_owner.get_or_null(p_texture);
  1218. ERR_FAIL_COND(!tex);
  1219. tex->detect_roughness_callback_ud = p_userdata;
  1220. tex->detect_roughness_callback = p_callback;
  1221. }
  1222. void TextureStorage::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  1223. }
  1224. void TextureStorage::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  1225. }
  1226. Size2 TextureStorage::texture_size_with_proxy(RID p_proxy) {
  1227. return texture_2d_get_size(p_proxy);
  1228. }
  1229. void TextureStorage::texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type) {
  1230. ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_rd_texture));
  1231. // TODO : investigate if we can support this, will need to be able to obtain the order and obtain the slice info
  1232. ERR_FAIL_COND_MSG(RD::get_singleton()->texture_is_shared(p_rd_texture), "Please create the texture object using the original texture");
  1233. RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_rd_texture);
  1234. ERR_FAIL_COND(!(tf.usage_bits & RD::TEXTURE_USAGE_SAMPLING_BIT));
  1235. TextureFromRDFormat imfmt;
  1236. _texture_format_from_rd(tf.format, imfmt);
  1237. ERR_FAIL_COND(imfmt.image_format == Image::FORMAT_MAX);
  1238. Texture texture;
  1239. switch (tf.texture_type) {
  1240. case RD::TEXTURE_TYPE_2D: {
  1241. ERR_FAIL_COND(tf.array_layers != 1);
  1242. texture.type = TextureStorage::TYPE_2D;
  1243. } break;
  1244. case RD::TEXTURE_TYPE_2D_ARRAY: {
  1245. // RenderingDevice doesn't distinguish between Array textures and Cube textures
  1246. // this condition covers TextureArrays, TextureCube, and TextureCubeArray.
  1247. ERR_FAIL_COND(tf.array_layers == 1);
  1248. texture.type = TextureStorage::TYPE_LAYERED;
  1249. texture.layered_type = p_layer_type;
  1250. } break;
  1251. case RD::TEXTURE_TYPE_3D: {
  1252. ERR_FAIL_COND(tf.array_layers != 1);
  1253. texture.type = TextureStorage::TYPE_3D;
  1254. } break;
  1255. default: {
  1256. ERR_FAIL_MSG("This RD texture can't be used as a render texture");
  1257. } break;
  1258. }
  1259. texture.width = tf.width;
  1260. texture.height = tf.height;
  1261. texture.depth = tf.depth;
  1262. texture.layers = tf.array_layers;
  1263. texture.mipmaps = tf.mipmaps;
  1264. texture.format = imfmt.image_format;
  1265. texture.validated_format = texture.format; // ??
  1266. RD::TextureView rd_view;
  1267. rd_view.format_override = imfmt.rd_format == tf.format ? RD::DATA_FORMAT_MAX : imfmt.rd_format;
  1268. rd_view.swizzle_r = imfmt.swizzle_r;
  1269. rd_view.swizzle_g = imfmt.swizzle_g;
  1270. rd_view.swizzle_b = imfmt.swizzle_b;
  1271. rd_view.swizzle_a = imfmt.swizzle_a;
  1272. texture.rd_type = tf.texture_type;
  1273. texture.rd_view = rd_view;
  1274. texture.rd_format = imfmt.rd_format;
  1275. // We create a shared texture here even if our view matches, so we don't obtain ownership.
  1276. texture.rd_texture = RD::get_singleton()->texture_create_shared(rd_view, p_rd_texture);
  1277. if (imfmt.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  1278. rd_view.format_override = imfmt.rd_format_srgb == tf.format ? RD::DATA_FORMAT_MAX : imfmt.rd_format;
  1279. texture.rd_format_srgb = imfmt.rd_format_srgb;
  1280. // We create a shared texture here even if our view matches, so we don't obtain ownership.
  1281. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, p_rd_texture);
  1282. }
  1283. // TODO figure out what to do with slices
  1284. texture.width_2d = texture.width;
  1285. texture.height_2d = texture.height;
  1286. texture.is_render_target = false;
  1287. texture.is_proxy = false;
  1288. texture_owner.initialize_rid(p_texture, texture);
  1289. }
  1290. RID TextureStorage::texture_get_rd_texture(RID p_texture, bool p_srgb) const {
  1291. if (p_texture.is_null()) {
  1292. return RID();
  1293. }
  1294. Texture *tex = texture_owner.get_or_null(p_texture);
  1295. if (!tex) {
  1296. return RID();
  1297. }
  1298. return (p_srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1299. }
  1300. uint64_t TextureStorage::texture_get_native_handle(RID p_texture, bool p_srgb) const {
  1301. Texture *tex = texture_owner.get_or_null(p_texture);
  1302. ERR_FAIL_COND_V(!tex, 0);
  1303. if (p_srgb && tex->rd_texture_srgb.is_valid()) {
  1304. return RD::get_singleton()->texture_get_native_handle(tex->rd_texture_srgb);
  1305. } else {
  1306. return RD::get_singleton()->texture_get_native_handle(tex->rd_texture);
  1307. }
  1308. }
  1309. Ref<Image> TextureStorage::_validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format) {
  1310. Ref<Image> image = p_image->duplicate();
  1311. switch (p_image->get_format()) {
  1312. case Image::FORMAT_L8: {
  1313. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1314. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1315. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  1316. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1317. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1318. } break; //luminance
  1319. case Image::FORMAT_LA8: {
  1320. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1321. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1322. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  1323. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1324. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_G;
  1325. } break; //luminance-alpha
  1326. case Image::FORMAT_R8: {
  1327. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1328. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1329. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1330. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1331. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1332. } break;
  1333. case Image::FORMAT_RG8: {
  1334. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1335. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1336. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1337. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1338. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1339. } break;
  1340. case Image::FORMAT_RGB8: {
  1341. //this format is not mandatory for specification, check if supported first
  1342. 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)) {
  1343. r_format.format = RD::DATA_FORMAT_R8G8B8_UNORM;
  1344. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8_SRGB;
  1345. } else {
  1346. //not supported, reconvert
  1347. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1348. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1349. image->convert(Image::FORMAT_RGBA8);
  1350. }
  1351. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1352. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1353. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1354. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1355. } break;
  1356. case Image::FORMAT_RGBA8: {
  1357. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1358. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1359. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1360. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1361. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1362. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1363. } break;
  1364. case Image::FORMAT_RGBA4444: {
  1365. r_format.format = RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16;
  1366. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B; //needs swizzle
  1367. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1368. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1369. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1370. } break;
  1371. case Image::FORMAT_RGB565: {
  1372. r_format.format = RD::DATA_FORMAT_B5G6R5_UNORM_PACK16;
  1373. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B;
  1374. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1375. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1376. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1377. } break;
  1378. case Image::FORMAT_RF: {
  1379. r_format.format = RD::DATA_FORMAT_R32_SFLOAT;
  1380. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1381. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1382. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1383. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1384. } break; //float
  1385. case Image::FORMAT_RGF: {
  1386. r_format.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  1387. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1388. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1389. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1390. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1391. } break;
  1392. case Image::FORMAT_RGBF: {
  1393. //this format is not mandatory for specification, check if supported first
  1394. 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)) {
  1395. r_format.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  1396. } else {
  1397. //not supported, reconvert
  1398. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1399. image->convert(Image::FORMAT_RGBAF);
  1400. }
  1401. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1402. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1403. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1404. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1405. } break;
  1406. case Image::FORMAT_RGBAF: {
  1407. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1408. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1409. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1410. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1411. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1412. } break;
  1413. case Image::FORMAT_RH: {
  1414. r_format.format = RD::DATA_FORMAT_R16_SFLOAT;
  1415. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1416. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1417. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1418. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1419. } break; //half float
  1420. case Image::FORMAT_RGH: {
  1421. r_format.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  1422. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1423. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1424. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1425. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1426. } break;
  1427. case Image::FORMAT_RGBH: {
  1428. //this format is not mandatory for specification, check if supported first
  1429. 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)) {
  1430. r_format.format = RD::DATA_FORMAT_R16G16B16_SFLOAT;
  1431. } else {
  1432. //not supported, reconvert
  1433. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1434. image->convert(Image::FORMAT_RGBAH);
  1435. }
  1436. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1437. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1438. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1439. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1440. } break;
  1441. case Image::FORMAT_RGBAH: {
  1442. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1443. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1444. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1445. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1446. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1447. } break;
  1448. case Image::FORMAT_RGBE9995: {
  1449. r_format.format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  1450. // TODO: Need to make a function in Image to swap bits for this.
  1451. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_IDENTITY;
  1452. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_IDENTITY;
  1453. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_IDENTITY;
  1454. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_IDENTITY;
  1455. } break;
  1456. case Image::FORMAT_DXT1: {
  1457. 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)) {
  1458. r_format.format = RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK;
  1459. r_format.format_srgb = RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK;
  1460. } else {
  1461. //not supported, reconvert
  1462. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1463. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1464. image->decompress();
  1465. image->convert(Image::FORMAT_RGBA8);
  1466. }
  1467. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1468. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1469. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1470. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1471. } break; //s3tc bc1
  1472. case Image::FORMAT_DXT3: {
  1473. 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)) {
  1474. r_format.format = RD::DATA_FORMAT_BC2_UNORM_BLOCK;
  1475. r_format.format_srgb = RD::DATA_FORMAT_BC2_SRGB_BLOCK;
  1476. } else {
  1477. //not supported, reconvert
  1478. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1479. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1480. image->decompress();
  1481. image->convert(Image::FORMAT_RGBA8);
  1482. }
  1483. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1484. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1485. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1486. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1487. } break; //bc2
  1488. case Image::FORMAT_DXT5: {
  1489. 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)) {
  1490. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  1491. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  1492. } else {
  1493. //not supported, reconvert
  1494. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1495. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1496. image->decompress();
  1497. image->convert(Image::FORMAT_RGBA8);
  1498. }
  1499. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1500. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1501. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1502. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1503. } break; //bc3
  1504. case Image::FORMAT_RGTC_R: {
  1505. 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)) {
  1506. r_format.format = RD::DATA_FORMAT_BC4_UNORM_BLOCK;
  1507. } else {
  1508. //not supported, reconvert
  1509. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1510. image->decompress();
  1511. image->convert(Image::FORMAT_R8);
  1512. }
  1513. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1514. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1515. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1516. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1517. } break;
  1518. case Image::FORMAT_RGTC_RG: {
  1519. 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)) {
  1520. r_format.format = RD::DATA_FORMAT_BC5_UNORM_BLOCK;
  1521. } else {
  1522. //not supported, reconvert
  1523. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1524. image->decompress();
  1525. image->convert(Image::FORMAT_RG8);
  1526. }
  1527. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1528. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1529. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1530. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1531. } break;
  1532. case Image::FORMAT_BPTC_RGBA: {
  1533. 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)) {
  1534. r_format.format = RD::DATA_FORMAT_BC7_UNORM_BLOCK;
  1535. r_format.format_srgb = RD::DATA_FORMAT_BC7_SRGB_BLOCK;
  1536. } else {
  1537. //not supported, reconvert
  1538. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1539. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1540. image->decompress();
  1541. image->convert(Image::FORMAT_RGBA8);
  1542. }
  1543. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1544. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1545. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1546. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1547. } break; //btpc bc7
  1548. case Image::FORMAT_BPTC_RGBF: {
  1549. 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)) {
  1550. r_format.format = RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK;
  1551. } else {
  1552. //not supported, reconvert
  1553. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1554. image->decompress();
  1555. image->convert(Image::FORMAT_RGBAH);
  1556. }
  1557. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1558. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1559. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1560. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1561. } break; //float bc6h
  1562. case Image::FORMAT_BPTC_RGBFU: {
  1563. 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)) {
  1564. r_format.format = RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK;
  1565. } else {
  1566. //not supported, reconvert
  1567. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1568. image->decompress();
  1569. image->convert(Image::FORMAT_RGBAH);
  1570. }
  1571. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1572. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1573. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1574. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1575. } break; //unsigned float bc6hu
  1576. case Image::FORMAT_ETC2_R11: {
  1577. 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)) {
  1578. r_format.format = RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK;
  1579. } else {
  1580. //not supported, reconvert
  1581. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  1582. image->decompress();
  1583. image->convert(Image::FORMAT_R8);
  1584. }
  1585. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1586. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1587. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1588. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1589. } break; //etc2
  1590. case Image::FORMAT_ETC2_R11S: {
  1591. 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)) {
  1592. r_format.format = RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK;
  1593. } else {
  1594. //not supported, reconvert
  1595. r_format.format = RD::DATA_FORMAT_R8_SNORM;
  1596. image->decompress();
  1597. image->convert(Image::FORMAT_R8);
  1598. }
  1599. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1600. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1601. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1602. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1603. } break; //signed: {} break; NOT srgb.
  1604. case Image::FORMAT_ETC2_RG11: {
  1605. 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)) {
  1606. r_format.format = RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK;
  1607. } else {
  1608. //not supported, reconvert
  1609. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  1610. image->decompress();
  1611. image->convert(Image::FORMAT_RG8);
  1612. }
  1613. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1614. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1615. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1616. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1617. } break;
  1618. case Image::FORMAT_ETC2_RG11S: {
  1619. 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)) {
  1620. r_format.format = RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK;
  1621. } else {
  1622. //not supported, reconvert
  1623. r_format.format = RD::DATA_FORMAT_R8G8_SNORM;
  1624. image->decompress();
  1625. image->convert(Image::FORMAT_RG8);
  1626. }
  1627. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1628. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1629. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1630. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1631. } break;
  1632. case Image::FORMAT_ETC:
  1633. case Image::FORMAT_ETC2_RGB8: {
  1634. //ETC2 is backwards compatible with ETC1, and all modern platforms support it
  1635. 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)) {
  1636. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
  1637. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK;
  1638. } else {
  1639. //not supported, reconvert
  1640. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1641. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1642. image->decompress();
  1643. image->convert(Image::FORMAT_RGBA8);
  1644. }
  1645. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1646. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1647. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1648. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1649. } break;
  1650. case Image::FORMAT_ETC2_RGBA8: {
  1651. 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)) {
  1652. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  1653. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  1654. } else {
  1655. //not supported, reconvert
  1656. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1657. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1658. image->decompress();
  1659. image->convert(Image::FORMAT_RGBA8);
  1660. }
  1661. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1662. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1663. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1664. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1665. } break;
  1666. case Image::FORMAT_ETC2_RGB8A1: {
  1667. 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)) {
  1668. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
  1669. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK;
  1670. } else {
  1671. //not supported, reconvert
  1672. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1673. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1674. image->decompress();
  1675. image->convert(Image::FORMAT_RGBA8);
  1676. }
  1677. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1678. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1679. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1680. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1681. } break;
  1682. case Image::FORMAT_ETC2_RA_AS_RG: {
  1683. 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)) {
  1684. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  1685. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  1686. } else {
  1687. //not supported, reconvert
  1688. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1689. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1690. image->decompress();
  1691. image->convert(Image::FORMAT_RGBA8);
  1692. }
  1693. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1694. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  1695. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1696. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1697. } break;
  1698. case Image::FORMAT_DXT5_RA_AS_RG: {
  1699. 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)) {
  1700. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  1701. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  1702. } else {
  1703. //not supported, reconvert
  1704. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1705. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1706. image->decompress();
  1707. image->convert(Image::FORMAT_RGBA8);
  1708. }
  1709. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1710. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  1711. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1712. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1713. } break;
  1714. case Image::FORMAT_ASTC_4x4:
  1715. case Image::FORMAT_ASTC_4x4_HDR: {
  1716. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ASTC_4x4_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1717. r_format.format = RD::DATA_FORMAT_ASTC_4x4_UNORM_BLOCK;
  1718. if (p_image->get_format() == Image::FORMAT_ASTC_4x4) {
  1719. r_format.format_srgb = RD::DATA_FORMAT_ASTC_4x4_SRGB_BLOCK;
  1720. }
  1721. } else {
  1722. //not supported, reconvert
  1723. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1724. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1725. image->decompress();
  1726. image->convert(Image::FORMAT_RGBA8);
  1727. }
  1728. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1729. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1730. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1731. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1732. } break; // astc 4x4
  1733. case Image::FORMAT_ASTC_8x8:
  1734. case Image::FORMAT_ASTC_8x8_HDR: {
  1735. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ASTC_8x8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  1736. r_format.format = RD::DATA_FORMAT_ASTC_8x8_UNORM_BLOCK;
  1737. if (p_image->get_format() == Image::FORMAT_ASTC_8x8) {
  1738. r_format.format_srgb = RD::DATA_FORMAT_ASTC_8x8_SRGB_BLOCK;
  1739. }
  1740. } else {
  1741. //not supported, reconvert
  1742. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1743. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1744. image->decompress();
  1745. image->convert(Image::FORMAT_RGBA8);
  1746. }
  1747. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1748. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1749. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1750. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1751. } break; // astc 8x8
  1752. default: {
  1753. }
  1754. }
  1755. return image;
  1756. }
  1757. void TextureStorage::_texture_format_from_rd(RD::DataFormat p_rd_format, TextureFromRDFormat &r_format) {
  1758. switch (p_rd_format) {
  1759. case RD::DATA_FORMAT_R8_UNORM: {
  1760. r_format.image_format = Image::FORMAT_L8;
  1761. r_format.rd_format = RD::DATA_FORMAT_R8_UNORM;
  1762. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1763. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  1764. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1765. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1766. } break; //luminance
  1767. case RD::DATA_FORMAT_R8G8_UNORM: {
  1768. r_format.image_format = Image::FORMAT_LA8;
  1769. r_format.rd_format = RD::DATA_FORMAT_R8G8_UNORM;
  1770. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1771. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  1772. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1773. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_G;
  1774. } break; //luminance-alpha
  1775. /* already maps to L8/LA8
  1776. case RD::DATA_FORMAT_R8_UNORM: {
  1777. r_format.image_format = Image::FORMAT_R8;
  1778. r_format.rd_format = RD::DATA_FORMAT_R8_UNORM;
  1779. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1780. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1781. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1782. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1783. } break;
  1784. case RD::DATA_FORMAT_R8G8_UNORM: {
  1785. r_format.image_format = Image::FORMAT_RG8;
  1786. r_format.rd_format = RD::DATA_FORMAT_R8G8_UNORM;
  1787. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1788. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1789. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1790. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1791. } break;
  1792. */
  1793. case RD::DATA_FORMAT_R8G8B8_UNORM:
  1794. case RD::DATA_FORMAT_R8G8B8_SRGB: {
  1795. r_format.image_format = Image::FORMAT_RGB8;
  1796. r_format.rd_format = RD::DATA_FORMAT_R8G8B8_UNORM;
  1797. r_format.rd_format_srgb = RD::DATA_FORMAT_R8G8B8_SRGB;
  1798. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1799. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1800. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1801. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1802. } break;
  1803. case RD::DATA_FORMAT_R8G8B8A8_UNORM:
  1804. case RD::DATA_FORMAT_R8G8B8A8_SRGB: {
  1805. r_format.image_format = Image::FORMAT_RGBA8;
  1806. r_format.rd_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1807. r_format.rd_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  1808. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1809. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1810. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1811. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1812. } break;
  1813. case RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16: {
  1814. r_format.image_format = Image::FORMAT_RGBA4444;
  1815. r_format.rd_format = RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16;
  1816. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B; //needs swizzle
  1817. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1818. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1819. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1820. } break;
  1821. case RD::DATA_FORMAT_B5G6R5_UNORM_PACK16: {
  1822. r_format.image_format = Image::FORMAT_RGB565;
  1823. r_format.rd_format = RD::DATA_FORMAT_B5G6R5_UNORM_PACK16;
  1824. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B;
  1825. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1826. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  1827. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1828. } break;
  1829. case RD::DATA_FORMAT_R32_SFLOAT: {
  1830. r_format.image_format = Image::FORMAT_RF;
  1831. r_format.rd_format = RD::DATA_FORMAT_R32_SFLOAT;
  1832. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1833. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1834. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1835. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1836. } break; //float
  1837. case RD::DATA_FORMAT_R32G32_SFLOAT: {
  1838. r_format.image_format = Image::FORMAT_RGF;
  1839. r_format.rd_format = RD::DATA_FORMAT_R32G32_SFLOAT;
  1840. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1841. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1842. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1843. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1844. } break;
  1845. case RD::DATA_FORMAT_R32G32B32_SFLOAT: {
  1846. r_format.image_format = Image::FORMAT_RGBF;
  1847. r_format.rd_format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  1848. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1849. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1850. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1851. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1852. } break;
  1853. case RD::DATA_FORMAT_R32G32B32A32_SFLOAT: {
  1854. r_format.image_format = Image::FORMAT_RGBF;
  1855. r_format.rd_format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1856. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1857. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1858. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1859. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1860. } break;
  1861. case RD::DATA_FORMAT_R16_SFLOAT: {
  1862. r_format.image_format = Image::FORMAT_RH;
  1863. r_format.rd_format = RD::DATA_FORMAT_R16_SFLOAT;
  1864. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1865. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1866. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1867. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1868. } break; //half float
  1869. case RD::DATA_FORMAT_R16G16_SFLOAT: {
  1870. r_format.image_format = Image::FORMAT_RGH;
  1871. r_format.rd_format = RD::DATA_FORMAT_R16G16_SFLOAT;
  1872. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1873. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1874. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1875. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1876. } break;
  1877. case RD::DATA_FORMAT_R16G16B16_SFLOAT: {
  1878. r_format.image_format = Image::FORMAT_RGBH;
  1879. r_format.rd_format = RD::DATA_FORMAT_R16G16B16_SFLOAT;
  1880. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1881. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1882. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1883. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1884. } break;
  1885. case RD::DATA_FORMAT_R16G16B16A16_SFLOAT: {
  1886. r_format.image_format = Image::FORMAT_RGBAH;
  1887. r_format.rd_format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1888. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1889. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1890. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1891. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1892. } break;
  1893. case RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32: {
  1894. r_format.image_format = Image::FORMAT_RGBE9995;
  1895. r_format.rd_format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  1896. // TODO: Need to make a function in Image to swap bits for this.
  1897. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_IDENTITY;
  1898. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_IDENTITY;
  1899. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_IDENTITY;
  1900. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_IDENTITY;
  1901. } break;
  1902. case RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK:
  1903. case RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK: {
  1904. r_format.image_format = Image::FORMAT_DXT1;
  1905. r_format.rd_format = RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK;
  1906. r_format.rd_format_srgb = RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK;
  1907. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1908. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1909. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1910. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1911. } break; //s3tc bc1
  1912. case RD::DATA_FORMAT_BC2_UNORM_BLOCK:
  1913. case RD::DATA_FORMAT_BC2_SRGB_BLOCK: {
  1914. r_format.image_format = Image::FORMAT_DXT3;
  1915. r_format.rd_format = RD::DATA_FORMAT_BC2_UNORM_BLOCK;
  1916. r_format.rd_format_srgb = RD::DATA_FORMAT_BC2_SRGB_BLOCK;
  1917. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1918. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1919. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1920. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1921. } break; //bc2
  1922. case RD::DATA_FORMAT_BC3_UNORM_BLOCK:
  1923. case RD::DATA_FORMAT_BC3_SRGB_BLOCK: {
  1924. r_format.image_format = Image::FORMAT_DXT5;
  1925. r_format.rd_format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  1926. r_format.rd_format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  1927. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1928. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1929. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1930. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1931. } break; //bc3
  1932. case RD::DATA_FORMAT_BC4_UNORM_BLOCK: {
  1933. r_format.image_format = Image::FORMAT_RGTC_R;
  1934. r_format.rd_format = RD::DATA_FORMAT_BC4_UNORM_BLOCK;
  1935. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1936. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1937. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1938. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1939. } break;
  1940. case RD::DATA_FORMAT_BC5_UNORM_BLOCK: {
  1941. r_format.image_format = Image::FORMAT_RGTC_RG;
  1942. r_format.rd_format = RD::DATA_FORMAT_BC5_UNORM_BLOCK;
  1943. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1944. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1945. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1946. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1947. } break;
  1948. case RD::DATA_FORMAT_BC7_UNORM_BLOCK:
  1949. case RD::DATA_FORMAT_BC7_SRGB_BLOCK: {
  1950. r_format.image_format = Image::FORMAT_BPTC_RGBA;
  1951. r_format.rd_format = RD::DATA_FORMAT_BC7_UNORM_BLOCK;
  1952. r_format.rd_format_srgb = RD::DATA_FORMAT_BC7_SRGB_BLOCK;
  1953. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1954. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1955. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1956. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  1957. } break; //btpc bc7
  1958. case RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK: {
  1959. r_format.image_format = Image::FORMAT_BPTC_RGBF;
  1960. r_format.rd_format = RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK;
  1961. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1962. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1963. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1964. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1965. } break; //float bc6h
  1966. case RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK: {
  1967. r_format.image_format = Image::FORMAT_BPTC_RGBFU;
  1968. r_format.rd_format = RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK;
  1969. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1970. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1971. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  1972. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1973. } break; //unsigned float bc6hu
  1974. case RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK: {
  1975. r_format.image_format = Image::FORMAT_ETC2_R11;
  1976. r_format.rd_format = RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK;
  1977. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1978. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1979. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1980. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1981. } break; //etc2
  1982. case RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK: {
  1983. r_format.image_format = Image::FORMAT_ETC2_R11S;
  1984. r_format.rd_format = RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK;
  1985. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1986. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  1987. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1988. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1989. } break; //signed: {} break; NOT srgb.
  1990. case RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK: {
  1991. r_format.image_format = Image::FORMAT_ETC2_RG11;
  1992. r_format.rd_format = RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK;
  1993. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  1994. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  1995. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  1996. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  1997. } break;
  1998. case RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK: {
  1999. r_format.image_format = Image::FORMAT_ETC2_RG11S;
  2000. r_format.rd_format = RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK;
  2001. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2002. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2003. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  2004. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  2005. } break;
  2006. case RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
  2007. case RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK: {
  2008. r_format.image_format = Image::FORMAT_ETC2_RGB8;
  2009. r_format.rd_format = RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
  2010. r_format.rd_format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK;
  2011. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2012. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2013. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  2014. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  2015. } break;
  2016. /* already maps to FORMAT_ETC2_RGBA8
  2017. case RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
  2018. case RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK: {
  2019. r_format.image_format = Image::FORMAT_ETC2_RGBA8;
  2020. r_format.rd_format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  2021. r_format.rd_format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  2022. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2023. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2024. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  2025. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  2026. } break;
  2027. */
  2028. case RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
  2029. case RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK: {
  2030. r_format.image_format = Image::FORMAT_ETC2_RGB8A1;
  2031. r_format.rd_format = RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
  2032. r_format.rd_format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK;
  2033. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2034. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2035. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  2036. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  2037. } break;
  2038. case RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
  2039. case RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK: {
  2040. r_format.image_format = Image::FORMAT_ETC2_RA_AS_RG;
  2041. r_format.rd_format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  2042. r_format.rd_format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  2043. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2044. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  2045. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  2046. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  2047. } break;
  2048. /* already maps to FORMAT_DXT5
  2049. case RD::DATA_FORMAT_BC3_UNORM_BLOCK:
  2050. case RD::DATA_FORMAT_BC3_SRGB_BLOCK: {
  2051. r_format.image_format = Image::FORMAT_DXT5_RA_AS_RG;
  2052. r_format.rd_format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  2053. r_format.rd_format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  2054. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2055. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  2056. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  2057. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  2058. } break;
  2059. */
  2060. case RD::DATA_FORMAT_ASTC_4x4_UNORM_BLOCK: {
  2061. // Q: Do we do as we do below, just create the sRGB variant?
  2062. r_format.image_format = Image::FORMAT_ASTC_4x4;
  2063. r_format.rd_format = RD::DATA_FORMAT_ASTC_4x4_UNORM_BLOCK;
  2064. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2065. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2066. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  2067. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  2068. } break;
  2069. case RD::DATA_FORMAT_ASTC_4x4_SRGB_BLOCK: {
  2070. r_format.image_format = Image::FORMAT_ASTC_4x4_HDR;
  2071. r_format.rd_format = RD::DATA_FORMAT_ASTC_4x4_UNORM_BLOCK;
  2072. r_format.rd_format_srgb = RD::DATA_FORMAT_ASTC_4x4_SRGB_BLOCK;
  2073. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2074. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2075. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  2076. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  2077. } break; // astc 4x4
  2078. case RD::DATA_FORMAT_ASTC_8x8_UNORM_BLOCK: {
  2079. // Q: Do we do as we do below, just create the sRGB variant?
  2080. r_format.image_format = Image::FORMAT_ASTC_8x8;
  2081. r_format.rd_format = RD::DATA_FORMAT_ASTC_8x8_UNORM_BLOCK;
  2082. } break;
  2083. case RD::DATA_FORMAT_ASTC_8x8_SRGB_BLOCK: {
  2084. r_format.image_format = Image::FORMAT_ASTC_8x8_HDR;
  2085. r_format.rd_format = RD::DATA_FORMAT_ASTC_8x8_UNORM_BLOCK;
  2086. r_format.rd_format_srgb = RD::DATA_FORMAT_ASTC_8x8_SRGB_BLOCK;
  2087. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  2088. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  2089. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  2090. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  2091. } break; // astc 8x8
  2092. default: {
  2093. ERR_FAIL_MSG("Unsupported image format");
  2094. }
  2095. }
  2096. }
  2097. /* DECAL API */
  2098. RID TextureStorage::decal_atlas_get_texture() const {
  2099. return decal_atlas.texture;
  2100. }
  2101. RID TextureStorage::decal_atlas_get_texture_srgb() const {
  2102. return decal_atlas.texture_srgb;
  2103. }
  2104. RID TextureStorage::decal_allocate() {
  2105. return decal_owner.allocate_rid();
  2106. }
  2107. void TextureStorage::decal_initialize(RID p_decal) {
  2108. decal_owner.initialize_rid(p_decal, Decal());
  2109. }
  2110. void TextureStorage::decal_free(RID p_rid) {
  2111. Decal *decal = decal_owner.get_or_null(p_rid);
  2112. for (int i = 0; i < RS::DECAL_TEXTURE_MAX; i++) {
  2113. if (decal->textures[i].is_valid() && owns_texture(decal->textures[i])) {
  2114. texture_remove_from_decal_atlas(decal->textures[i]);
  2115. }
  2116. }
  2117. decal->dependency.deleted_notify(p_rid);
  2118. decal_owner.free(p_rid);
  2119. }
  2120. void TextureStorage::decal_set_size(RID p_decal, const Vector3 &p_size) {
  2121. Decal *decal = decal_owner.get_or_null(p_decal);
  2122. ERR_FAIL_COND(!decal);
  2123. decal->size = p_size;
  2124. decal->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  2125. }
  2126. void TextureStorage::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  2127. Decal *decal = decal_owner.get_or_null(p_decal);
  2128. ERR_FAIL_COND(!decal);
  2129. ERR_FAIL_INDEX(p_type, RS::DECAL_TEXTURE_MAX);
  2130. if (decal->textures[p_type] == p_texture) {
  2131. return;
  2132. }
  2133. ERR_FAIL_COND(p_texture.is_valid() && !owns_texture(p_texture));
  2134. if (decal->textures[p_type].is_valid() && owns_texture(decal->textures[p_type])) {
  2135. texture_remove_from_decal_atlas(decal->textures[p_type]);
  2136. }
  2137. decal->textures[p_type] = p_texture;
  2138. if (decal->textures[p_type].is_valid()) {
  2139. texture_add_to_decal_atlas(decal->textures[p_type]);
  2140. }
  2141. decal->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_DECAL);
  2142. }
  2143. void TextureStorage::decal_set_emission_energy(RID p_decal, float p_energy) {
  2144. Decal *decal = decal_owner.get_or_null(p_decal);
  2145. ERR_FAIL_COND(!decal);
  2146. decal->emission_energy = p_energy;
  2147. }
  2148. void TextureStorage::decal_set_albedo_mix(RID p_decal, float p_mix) {
  2149. Decal *decal = decal_owner.get_or_null(p_decal);
  2150. ERR_FAIL_COND(!decal);
  2151. decal->albedo_mix = p_mix;
  2152. }
  2153. void TextureStorage::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  2154. Decal *decal = decal_owner.get_or_null(p_decal);
  2155. ERR_FAIL_COND(!decal);
  2156. decal->modulate = p_modulate;
  2157. }
  2158. void TextureStorage::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  2159. Decal *decal = decal_owner.get_or_null(p_decal);
  2160. ERR_FAIL_COND(!decal);
  2161. decal->cull_mask = p_layers;
  2162. decal->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_DECAL);
  2163. }
  2164. void TextureStorage::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  2165. Decal *decal = decal_owner.get_or_null(p_decal);
  2166. ERR_FAIL_COND(!decal);
  2167. decal->distance_fade = p_enabled;
  2168. decal->distance_fade_begin = p_begin;
  2169. decal->distance_fade_length = p_length;
  2170. }
  2171. void TextureStorage::decal_set_fade(RID p_decal, float p_above, float p_below) {
  2172. Decal *decal = decal_owner.get_or_null(p_decal);
  2173. ERR_FAIL_COND(!decal);
  2174. decal->upper_fade = p_above;
  2175. decal->lower_fade = p_below;
  2176. }
  2177. void TextureStorage::decal_set_normal_fade(RID p_decal, float p_fade) {
  2178. Decal *decal = decal_owner.get_or_null(p_decal);
  2179. ERR_FAIL_COND(!decal);
  2180. decal->normal_fade = p_fade;
  2181. }
  2182. void TextureStorage::decal_atlas_mark_dirty_on_texture(RID p_texture) {
  2183. if (decal_atlas.textures.has(p_texture)) {
  2184. //belongs to decal atlas..
  2185. decal_atlas.dirty = true; //mark it dirty since it was most likely modified
  2186. }
  2187. }
  2188. void TextureStorage::decal_atlas_remove_texture(RID p_texture) {
  2189. if (decal_atlas.textures.has(p_texture)) {
  2190. decal_atlas.textures.erase(p_texture);
  2191. //there is not much a point of making it dirty, just let it be.
  2192. }
  2193. }
  2194. AABB TextureStorage::decal_get_aabb(RID p_decal) const {
  2195. Decal *decal = decal_owner.get_or_null(p_decal);
  2196. ERR_FAIL_COND_V(!decal, AABB());
  2197. return AABB(-decal->size / 2, decal->size);
  2198. }
  2199. uint32_t TextureStorage::decal_get_cull_mask(RID p_decal) const {
  2200. Decal *decal = decal_owner.get_or_null(p_decal);
  2201. ERR_FAIL_COND_V(!decal, 0);
  2202. return decal->cull_mask;
  2203. }
  2204. Dependency *TextureStorage::decal_get_dependency(RID p_decal) {
  2205. Decal *decal = decal_owner.get_or_null(p_decal);
  2206. ERR_FAIL_COND_V(!decal, nullptr);
  2207. return &decal->dependency;
  2208. }
  2209. void TextureStorage::update_decal_atlas() {
  2210. CopyEffects *copy_effects = CopyEffects::get_singleton();
  2211. ERR_FAIL_NULL(copy_effects);
  2212. if (!decal_atlas.dirty) {
  2213. return; //nothing to do
  2214. }
  2215. decal_atlas.dirty = false;
  2216. if (decal_atlas.texture.is_valid()) {
  2217. RD::get_singleton()->free(decal_atlas.texture);
  2218. decal_atlas.texture = RID();
  2219. decal_atlas.texture_srgb = RID();
  2220. decal_atlas.texture_mipmaps.clear();
  2221. }
  2222. int border = 1 << decal_atlas.mipmaps;
  2223. if (decal_atlas.textures.size()) {
  2224. //generate atlas
  2225. Vector<DecalAtlas::SortItem> itemsv;
  2226. itemsv.resize(decal_atlas.textures.size());
  2227. int base_size = 8;
  2228. int idx = 0;
  2229. for (const KeyValue<RID, DecalAtlas::Texture> &E : decal_atlas.textures) {
  2230. DecalAtlas::SortItem &si = itemsv.write[idx];
  2231. Texture *src_tex = get_texture(E.key);
  2232. si.size.width = (src_tex->width / border) + 1;
  2233. si.size.height = (src_tex->height / border) + 1;
  2234. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  2235. if (base_size < si.size.width) {
  2236. base_size = nearest_power_of_2_templated(si.size.width);
  2237. }
  2238. si.texture = E.key;
  2239. idx++;
  2240. }
  2241. //sort items by size
  2242. itemsv.sort();
  2243. //attempt to create atlas
  2244. int item_count = itemsv.size();
  2245. DecalAtlas::SortItem *items = itemsv.ptrw();
  2246. int atlas_height = 0;
  2247. while (true) {
  2248. Vector<int> v_offsetsv;
  2249. v_offsetsv.resize(base_size);
  2250. int *v_offsets = v_offsetsv.ptrw();
  2251. memset(v_offsets, 0, sizeof(int) * base_size);
  2252. int max_height = 0;
  2253. for (int i = 0; i < item_count; i++) {
  2254. //best fit
  2255. DecalAtlas::SortItem &si = items[i];
  2256. int best_idx = -1;
  2257. int best_height = 0x7FFFFFFF;
  2258. for (int j = 0; j <= base_size - si.size.width; j++) {
  2259. int height = 0;
  2260. for (int k = 0; k < si.size.width; k++) {
  2261. int h = v_offsets[k + j];
  2262. if (h > height) {
  2263. height = h;
  2264. if (height > best_height) {
  2265. break; //already bad
  2266. }
  2267. }
  2268. }
  2269. if (height < best_height) {
  2270. best_height = height;
  2271. best_idx = j;
  2272. }
  2273. }
  2274. //update
  2275. for (int k = 0; k < si.size.width; k++) {
  2276. v_offsets[k + best_idx] = best_height + si.size.height;
  2277. }
  2278. si.pos.x = best_idx;
  2279. si.pos.y = best_height;
  2280. if (si.pos.y + si.size.height > max_height) {
  2281. max_height = si.pos.y + si.size.height;
  2282. }
  2283. }
  2284. if (max_height <= base_size * 2) {
  2285. atlas_height = max_height;
  2286. break; //good ratio, break;
  2287. }
  2288. base_size *= 2;
  2289. }
  2290. decal_atlas.size.width = base_size * border;
  2291. decal_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  2292. for (int i = 0; i < item_count; i++) {
  2293. DecalAtlas::Texture *t = decal_atlas.textures.getptr(items[i].texture);
  2294. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  2295. t->uv_rect.size = items[i].pixel_size;
  2296. t->uv_rect.position /= Size2(decal_atlas.size);
  2297. t->uv_rect.size /= Size2(decal_atlas.size);
  2298. }
  2299. } else {
  2300. //use border as size, so it at least has enough mipmaps
  2301. decal_atlas.size.width = border;
  2302. decal_atlas.size.height = border;
  2303. }
  2304. //blit textures
  2305. RD::TextureFormat tformat;
  2306. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  2307. tformat.width = decal_atlas.size.width;
  2308. tformat.height = decal_atlas.size.height;
  2309. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  2310. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  2311. tformat.mipmaps = decal_atlas.mipmaps;
  2312. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  2313. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  2314. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  2315. RD::get_singleton()->texture_clear(decal_atlas.texture, Color(0, 0, 0, 0), 0, decal_atlas.mipmaps, 0, 1);
  2316. {
  2317. //create the framebuffer
  2318. Size2i s = decal_atlas.size;
  2319. for (int i = 0; i < decal_atlas.mipmaps; i++) {
  2320. DecalAtlas::MipMap mm;
  2321. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), decal_atlas.texture, 0, i);
  2322. Vector<RID> fb;
  2323. fb.push_back(mm.texture);
  2324. mm.fb = RD::get_singleton()->framebuffer_create(fb);
  2325. mm.size = s;
  2326. decal_atlas.texture_mipmaps.push_back(mm);
  2327. s.width = MAX(1, s.width >> 1);
  2328. s.height = MAX(1, s.height >> 1);
  2329. }
  2330. {
  2331. //create the SRGB variant
  2332. RD::TextureView rd_view;
  2333. rd_view.format_override = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  2334. decal_atlas.texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, decal_atlas.texture);
  2335. }
  2336. }
  2337. RID prev_texture;
  2338. for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {
  2339. const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];
  2340. Color clear_color(0, 0, 0, 0);
  2341. if (decal_atlas.textures.size()) {
  2342. if (i == 0) {
  2343. Vector<Color> cc;
  2344. cc.push_back(clear_color);
  2345. 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);
  2346. for (const KeyValue<RID, DecalAtlas::Texture> &E : decal_atlas.textures) {
  2347. DecalAtlas::Texture *t = decal_atlas.textures.getptr(E.key);
  2348. Texture *src_tex = get_texture(E.key);
  2349. copy_effects->copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);
  2350. }
  2351. RD::get_singleton()->draw_list_end();
  2352. prev_texture = mm.texture;
  2353. } else {
  2354. copy_effects->copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));
  2355. prev_texture = mm.texture;
  2356. }
  2357. } else {
  2358. RD::get_singleton()->texture_clear(mm.texture, clear_color, 0, 1, 0, 1);
  2359. }
  2360. }
  2361. }
  2362. void TextureStorage::texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  2363. if (!decal_atlas.textures.has(p_texture)) {
  2364. DecalAtlas::Texture t;
  2365. t.users = 1;
  2366. t.panorama_to_dp_users = p_panorama_to_dp ? 1 : 0;
  2367. decal_atlas.textures[p_texture] = t;
  2368. decal_atlas.dirty = true;
  2369. } else {
  2370. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  2371. t->users++;
  2372. if (p_panorama_to_dp) {
  2373. t->panorama_to_dp_users++;
  2374. }
  2375. }
  2376. }
  2377. void TextureStorage::texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  2378. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  2379. ERR_FAIL_COND(!t);
  2380. t->users--;
  2381. if (p_panorama_to_dp) {
  2382. ERR_FAIL_COND(t->panorama_to_dp_users == 0);
  2383. t->panorama_to_dp_users--;
  2384. }
  2385. if (t->users == 0) {
  2386. decal_atlas.textures.erase(p_texture);
  2387. //do not mark it dirty, there is no need to since it remains working
  2388. }
  2389. }
  2390. /* DECAL INSTANCE API */
  2391. RID TextureStorage::decal_instance_create(RID p_decal) {
  2392. DecalInstance di;
  2393. di.decal = p_decal;
  2394. di.forward_id = ForwardIDStorage::get_singleton()->allocate_forward_id(FORWARD_ID_TYPE_DECAL);
  2395. return decal_instance_owner.make_rid(di);
  2396. }
  2397. void TextureStorage::decal_instance_free(RID p_decal_instance) {
  2398. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  2399. ForwardIDStorage::get_singleton()->free_forward_id(FORWARD_ID_TYPE_DECAL, di->forward_id);
  2400. decal_instance_owner.free(p_decal_instance);
  2401. }
  2402. void TextureStorage::decal_instance_set_transform(RID p_decal_instance, const Transform3D &p_transform) {
  2403. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  2404. ERR_FAIL_COND(!di);
  2405. di->transform = p_transform;
  2406. }
  2407. void TextureStorage::decal_instance_set_sorting_offset(RID p_decal_instance, float p_sorting_offset) {
  2408. DecalInstance *di = decal_instance_owner.get_or_null(p_decal_instance);
  2409. ERR_FAIL_COND(!di);
  2410. di->sorting_offset = p_sorting_offset;
  2411. }
  2412. /* DECAL DATA API */
  2413. void TextureStorage::free_decal_data() {
  2414. if (decal_buffer.is_valid()) {
  2415. RD::get_singleton()->free(decal_buffer);
  2416. decal_buffer = RID();
  2417. }
  2418. if (decals != nullptr) {
  2419. memdelete_arr(decals);
  2420. decals = nullptr;
  2421. }
  2422. if (decal_sort != nullptr) {
  2423. memdelete_arr(decal_sort);
  2424. decal_sort = nullptr;
  2425. }
  2426. }
  2427. void TextureStorage::set_max_decals(const uint32_t p_max_decals) {
  2428. max_decals = p_max_decals;
  2429. uint32_t decal_buffer_size = max_decals * sizeof(DecalData);
  2430. decals = memnew_arr(DecalData, max_decals);
  2431. decal_sort = memnew_arr(DecalInstanceSort, max_decals);
  2432. decal_buffer = RD::get_singleton()->storage_buffer_create(decal_buffer_size);
  2433. }
  2434. void TextureStorage::update_decal_buffer(const PagedArray<RID> &p_decals, const Transform3D &p_camera_xform) {
  2435. ForwardIDStorage *forward_id_storage = ForwardIDStorage::get_singleton();
  2436. Transform3D uv_xform;
  2437. uv_xform.basis.scale(Vector3(2.0, 1.0, 2.0));
  2438. uv_xform.origin = Vector3(-1.0, 0.0, -1.0);
  2439. uint32_t decals_size = p_decals.size();
  2440. decal_count = 0;
  2441. for (uint32_t i = 0; i < decals_size; i++) {
  2442. if (decal_count == max_decals) {
  2443. break;
  2444. }
  2445. DecalInstance *decal_instance = decal_instance_owner.get_or_null(p_decals[i]);
  2446. if (!decal_instance) {
  2447. continue;
  2448. }
  2449. Decal *decal = decal_owner.get_or_null(decal_instance->decal);
  2450. Transform3D xform = decal_instance->transform;
  2451. real_t distance = p_camera_xform.origin.distance_to(xform.origin);
  2452. if (decal->distance_fade) {
  2453. float fade_begin = decal->distance_fade_begin;
  2454. float fade_length = decal->distance_fade_length;
  2455. if (distance > fade_begin) {
  2456. if (distance > fade_begin + fade_length) {
  2457. continue; // do not use this decal, its invisible
  2458. }
  2459. }
  2460. }
  2461. decal_sort[decal_count].decal_instance = decal_instance;
  2462. decal_sort[decal_count].decal = decal;
  2463. decal_sort[decal_count].depth = distance - decal_instance->sorting_offset;
  2464. decal_count++;
  2465. }
  2466. if (decal_count > 0) {
  2467. SortArray<DecalInstanceSort> sort_array;
  2468. sort_array.sort(decal_sort, decal_count);
  2469. }
  2470. bool using_forward_ids = forward_id_storage->uses_forward_ids();
  2471. for (uint32_t i = 0; i < decal_count; i++) {
  2472. DecalInstance *decal_instance = decal_sort[i].decal_instance;
  2473. Decal *decal = decal_sort[i].decal;
  2474. if (using_forward_ids) {
  2475. forward_id_storage->map_forward_id(FORWARD_ID_TYPE_DECAL, decal_instance->forward_id, i);
  2476. }
  2477. decal_instance->cull_mask = decal->cull_mask;
  2478. float fade = 1.0;
  2479. if (decal->distance_fade) {
  2480. const real_t distance = decal_sort[i].depth + decal_instance->sorting_offset;
  2481. const float fade_begin = decal->distance_fade_begin;
  2482. const float fade_length = decal->distance_fade_length;
  2483. if (distance > fade_begin) {
  2484. // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
  2485. fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_begin) / fade_length);
  2486. }
  2487. }
  2488. DecalData &dd = decals[i];
  2489. Vector3 decal_extents = decal->size / 2;
  2490. Transform3D scale_xform;
  2491. scale_xform.basis.scale(decal_extents);
  2492. Transform3D xform = decal_instance->transform;
  2493. Transform3D camera_inverse_xform = p_camera_xform.affine_inverse();
  2494. Transform3D to_decal_xform = (camera_inverse_xform * xform * scale_xform * uv_xform).affine_inverse();
  2495. MaterialStorage::store_transform(to_decal_xform, dd.xform);
  2496. Vector3 normal = xform.basis.get_column(Vector3::AXIS_Y).normalized();
  2497. normal = camera_inverse_xform.basis.xform(normal); //camera is normalized, so fine
  2498. dd.normal[0] = normal.x;
  2499. dd.normal[1] = normal.y;
  2500. dd.normal[2] = normal.z;
  2501. dd.normal_fade = decal->normal_fade;
  2502. RID albedo_tex = decal->textures[RS::DECAL_TEXTURE_ALBEDO];
  2503. RID emission_tex = decal->textures[RS::DECAL_TEXTURE_EMISSION];
  2504. if (albedo_tex.is_valid()) {
  2505. Rect2 rect = decal_atlas_get_texture_rect(albedo_tex);
  2506. dd.albedo_rect[0] = rect.position.x;
  2507. dd.albedo_rect[1] = rect.position.y;
  2508. dd.albedo_rect[2] = rect.size.x;
  2509. dd.albedo_rect[3] = rect.size.y;
  2510. } else {
  2511. if (!emission_tex.is_valid()) {
  2512. continue; //no albedo, no emission, no decal.
  2513. }
  2514. dd.albedo_rect[0] = 0;
  2515. dd.albedo_rect[1] = 0;
  2516. dd.albedo_rect[2] = 0;
  2517. dd.albedo_rect[3] = 0;
  2518. }
  2519. RID normal_tex = decal->textures[RS::DECAL_TEXTURE_NORMAL];
  2520. if (normal_tex.is_valid()) {
  2521. Rect2 rect = decal_atlas_get_texture_rect(normal_tex);
  2522. dd.normal_rect[0] = rect.position.x;
  2523. dd.normal_rect[1] = rect.position.y;
  2524. dd.normal_rect[2] = rect.size.x;
  2525. dd.normal_rect[3] = rect.size.y;
  2526. Basis normal_xform = camera_inverse_xform.basis * xform.basis.orthonormalized();
  2527. MaterialStorage::store_basis_3x4(normal_xform, dd.normal_xform);
  2528. } else {
  2529. dd.normal_rect[0] = 0;
  2530. dd.normal_rect[1] = 0;
  2531. dd.normal_rect[2] = 0;
  2532. dd.normal_rect[3] = 0;
  2533. }
  2534. RID orm_tex = decal->textures[RS::DECAL_TEXTURE_ORM];
  2535. if (orm_tex.is_valid()) {
  2536. Rect2 rect = decal_atlas_get_texture_rect(orm_tex);
  2537. dd.orm_rect[0] = rect.position.x;
  2538. dd.orm_rect[1] = rect.position.y;
  2539. dd.orm_rect[2] = rect.size.x;
  2540. dd.orm_rect[3] = rect.size.y;
  2541. } else {
  2542. dd.orm_rect[0] = 0;
  2543. dd.orm_rect[1] = 0;
  2544. dd.orm_rect[2] = 0;
  2545. dd.orm_rect[3] = 0;
  2546. }
  2547. if (emission_tex.is_valid()) {
  2548. Rect2 rect = decal_atlas_get_texture_rect(emission_tex);
  2549. dd.emission_rect[0] = rect.position.x;
  2550. dd.emission_rect[1] = rect.position.y;
  2551. dd.emission_rect[2] = rect.size.x;
  2552. dd.emission_rect[3] = rect.size.y;
  2553. } else {
  2554. dd.emission_rect[0] = 0;
  2555. dd.emission_rect[1] = 0;
  2556. dd.emission_rect[2] = 0;
  2557. dd.emission_rect[3] = 0;
  2558. }
  2559. Color modulate = decal->modulate;
  2560. dd.modulate[0] = modulate.r;
  2561. dd.modulate[1] = modulate.g;
  2562. dd.modulate[2] = modulate.b;
  2563. dd.modulate[3] = modulate.a * fade;
  2564. dd.emission_energy = decal->emission_energy * fade;
  2565. dd.albedo_mix = decal->albedo_mix;
  2566. dd.mask = decal->cull_mask;
  2567. dd.upper_fade = decal->upper_fade;
  2568. dd.lower_fade = decal->lower_fade;
  2569. // hook for subclass to do further processing.
  2570. RendererSceneRenderRD::get_singleton()->setup_added_decal(xform, decal_extents);
  2571. }
  2572. if (decal_count > 0) {
  2573. RD::get_singleton()->buffer_update(decal_buffer, 0, sizeof(DecalData) * decal_count, decals, RD::BARRIER_MASK_RASTER | RD::BARRIER_MASK_COMPUTE);
  2574. }
  2575. }
  2576. /* RENDER TARGET API */
  2577. RID TextureStorage::RenderTarget::get_framebuffer() {
  2578. // Note that if we're using an overridden color buffer, we're likely cycling through a texture chain.
  2579. // this is where our framebuffer cache comes in clutch..
  2580. if (msaa != RS::VIEWPORT_MSAA_DISABLED) {
  2581. return FramebufferCacheRD::get_singleton()->get_cache_multiview(view_count, color_multisample, overridden.color.is_valid() ? overridden.color : color);
  2582. } else {
  2583. return FramebufferCacheRD::get_singleton()->get_cache_multiview(view_count, overridden.color.is_valid() ? overridden.color : color);
  2584. }
  2585. }
  2586. void TextureStorage::_clear_render_target(RenderTarget *rt) {
  2587. // clear overrides, we assume these are freed by the object that created them
  2588. rt->overridden.color = RID();
  2589. rt->overridden.depth = RID();
  2590. rt->overridden.velocity = RID();
  2591. rt->overridden.cached_slices.clear(); // these are automatically freed when their parent textures are freed so just clear
  2592. // free in reverse dependency order
  2593. if (rt->framebuffer_uniform_set.is_valid()) {
  2594. rt->framebuffer_uniform_set = RID(); //chain deleted
  2595. }
  2596. if (rt->color.is_valid()) {
  2597. RD::get_singleton()->free(rt->color);
  2598. }
  2599. rt->color_slices.clear(); // these are automatically freed.
  2600. if (rt->color_multisample.is_valid()) {
  2601. RD::get_singleton()->free(rt->color_multisample);
  2602. }
  2603. if (rt->backbuffer.is_valid()) {
  2604. RD::get_singleton()->free(rt->backbuffer);
  2605. rt->backbuffer = RID();
  2606. rt->backbuffer_mipmaps.clear();
  2607. rt->backbuffer_uniform_set = RID(); //chain deleted
  2608. }
  2609. _render_target_clear_sdf(rt);
  2610. rt->color = RID();
  2611. rt->color_multisample = RID();
  2612. if (rt->texture.is_valid()) {
  2613. Texture *tex = get_texture(rt->texture);
  2614. tex->render_target = nullptr;
  2615. }
  2616. }
  2617. void TextureStorage::_update_render_target(RenderTarget *rt) {
  2618. if (rt->texture.is_null()) {
  2619. //create a placeholder until updated
  2620. rt->texture = texture_allocate();
  2621. texture_2d_placeholder_initialize(rt->texture);
  2622. Texture *tex = get_texture(rt->texture);
  2623. tex->is_render_target = true;
  2624. }
  2625. _clear_render_target(rt);
  2626. if (rt->size.width == 0 || rt->size.height == 0) {
  2627. return;
  2628. }
  2629. if (rt->use_hdr) {
  2630. rt->color_format = RendererSceneRenderRD::get_singleton()->_render_buffers_get_color_format();
  2631. rt->color_format_srgb = rt->color_format;
  2632. rt->image_format = rt->is_transparent ? Image::FORMAT_RGBAH : Image::FORMAT_RGBH;
  2633. } else {
  2634. rt->color_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  2635. rt->color_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  2636. rt->image_format = rt->is_transparent ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8;
  2637. }
  2638. RD::TextureFormat rd_color_attachment_format;
  2639. RD::TextureView rd_view;
  2640. { //attempt register
  2641. rd_color_attachment_format.format = rt->color_format;
  2642. rd_color_attachment_format.width = rt->size.width;
  2643. rd_color_attachment_format.height = rt->size.height;
  2644. rd_color_attachment_format.depth = 1;
  2645. rd_color_attachment_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
  2646. rd_color_attachment_format.mipmaps = 1;
  2647. if (rd_color_attachment_format.array_layers > 1) { // why are we not using rt->texture_type ??
  2648. rd_color_attachment_format.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  2649. } else {
  2650. rd_color_attachment_format.texture_type = RD::TEXTURE_TYPE_2D;
  2651. }
  2652. rd_color_attachment_format.samples = RD::TEXTURE_SAMPLES_1;
  2653. rd_color_attachment_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  2654. rd_color_attachment_format.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT; // FIXME we need this only when FSR is enabled
  2655. rd_color_attachment_format.shareable_formats.push_back(rt->color_format);
  2656. rd_color_attachment_format.shareable_formats.push_back(rt->color_format_srgb);
  2657. if (rt->msaa != RS::VIEWPORT_MSAA_DISABLED) {
  2658. rd_color_attachment_format.is_resolve_buffer = true;
  2659. }
  2660. }
  2661. // TODO see if we can lazy create this once we actually use it as we may not need to create this if we have an overridden color buffer...
  2662. rt->color = RD::get_singleton()->texture_create(rd_color_attachment_format, rd_view);
  2663. ERR_FAIL_COND(rt->color.is_null());
  2664. if (rt->msaa != RS::VIEWPORT_MSAA_DISABLED) {
  2665. // Use the texture format of the color attachment for the multisample color attachment.
  2666. RD::TextureFormat rd_color_multisample_format = rd_color_attachment_format;
  2667. const RD::TextureSamples texture_samples[RS::VIEWPORT_MSAA_MAX] = {
  2668. RD::TEXTURE_SAMPLES_1,
  2669. RD::TEXTURE_SAMPLES_2,
  2670. RD::TEXTURE_SAMPLES_4,
  2671. RD::TEXTURE_SAMPLES_8,
  2672. };
  2673. rd_color_multisample_format.samples = texture_samples[rt->msaa];
  2674. rd_color_multisample_format.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  2675. RD::TextureView rd_view_multisample;
  2676. rd_color_multisample_format.is_resolve_buffer = false;
  2677. rt->color_multisample = RD::get_singleton()->texture_create(rd_color_multisample_format, rd_view_multisample);
  2678. ERR_FAIL_COND(rt->color_multisample.is_null());
  2679. }
  2680. { //update texture
  2681. Texture *tex = get_texture(rt->texture);
  2682. //free existing textures
  2683. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  2684. RD::get_singleton()->free(tex->rd_texture);
  2685. }
  2686. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  2687. RD::get_singleton()->free(tex->rd_texture_srgb);
  2688. }
  2689. tex->rd_texture = RID();
  2690. tex->rd_texture_srgb = RID();
  2691. tex->render_target = rt;
  2692. //create shared textures to the color buffer,
  2693. //so transparent can be supported
  2694. RD::TextureView view;
  2695. view.format_override = rt->color_format;
  2696. if (!rt->is_transparent) {
  2697. view.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  2698. }
  2699. tex->rd_texture = RD::get_singleton()->texture_create_shared(view, rt->color);
  2700. if (rt->color_format_srgb != RD::DATA_FORMAT_MAX) {
  2701. view.format_override = rt->color_format_srgb;
  2702. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(view, rt->color);
  2703. }
  2704. tex->rd_view = view;
  2705. tex->width = rt->size.width;
  2706. tex->height = rt->size.height;
  2707. tex->width_2d = rt->size.width;
  2708. tex->height_2d = rt->size.height;
  2709. tex->rd_format = rt->color_format;
  2710. tex->rd_format_srgb = rt->color_format_srgb;
  2711. tex->format = rt->image_format;
  2712. tex->validated_format = rt->use_hdr ? Image::FORMAT_RGBAH : Image::FORMAT_RGBA8;
  2713. Vector<RID> proxies = tex->proxies; //make a copy, since update may change it
  2714. for (int i = 0; i < proxies.size(); i++) {
  2715. texture_proxy_update(proxies[i], rt->texture);
  2716. }
  2717. }
  2718. }
  2719. void TextureStorage::_create_render_target_backbuffer(RenderTarget *rt) {
  2720. ERR_FAIL_COND(rt->backbuffer.is_valid());
  2721. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rt->size.width, rt->size.height, Image::FORMAT_RGBA8);
  2722. RD::TextureFormat tf;
  2723. tf.format = rt->color_format;
  2724. tf.width = rt->size.width;
  2725. tf.height = rt->size.height;
  2726. tf.texture_type = RD::TEXTURE_TYPE_2D;
  2727. 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;
  2728. tf.mipmaps = mipmaps_required;
  2729. rt->backbuffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2730. RD::get_singleton()->set_resource_name(rt->backbuffer, "Render Target Back Buffer");
  2731. rt->backbuffer_mipmap0 = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, 0);
  2732. RD::get_singleton()->set_resource_name(rt->backbuffer_mipmap0, "Back Buffer slice mipmap 0");
  2733. {
  2734. Vector<RID> fb_tex;
  2735. fb_tex.push_back(rt->backbuffer_mipmap0);
  2736. rt->backbuffer_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  2737. }
  2738. if (rt->framebuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->framebuffer_uniform_set)) {
  2739. //the new one will require the backbuffer.
  2740. RD::get_singleton()->free(rt->framebuffer_uniform_set);
  2741. rt->framebuffer_uniform_set = RID();
  2742. }
  2743. //create mipmaps
  2744. for (uint32_t i = 1; i < mipmaps_required; i++) {
  2745. RID mipmap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, i);
  2746. RD::get_singleton()->set_resource_name(mipmap, "Back Buffer slice mip: " + itos(i));
  2747. rt->backbuffer_mipmaps.push_back(mipmap);
  2748. }
  2749. }
  2750. RID TextureStorage::render_target_create() {
  2751. RenderTarget render_target;
  2752. render_target.was_used = false;
  2753. render_target.clear_requested = false;
  2754. _update_render_target(&render_target);
  2755. return render_target_owner.make_rid(render_target);
  2756. }
  2757. void TextureStorage::render_target_free(RID p_rid) {
  2758. RenderTarget *rt = render_target_owner.get_or_null(p_rid);
  2759. _clear_render_target(rt);
  2760. if (rt->texture.is_valid()) {
  2761. Texture *tex = get_texture(rt->texture);
  2762. tex->is_render_target = false;
  2763. texture_free(rt->texture);
  2764. }
  2765. render_target_owner.free(p_rid);
  2766. }
  2767. void TextureStorage::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  2768. //unused for this render target
  2769. }
  2770. Point2i TextureStorage::render_target_get_position(RID p_render_target) const {
  2771. //unused for this render target
  2772. return Point2i();
  2773. }
  2774. void TextureStorage::render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) {
  2775. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2776. ERR_FAIL_COND(!rt);
  2777. if (rt->size.x != p_width || rt->size.y != p_height || rt->view_count != p_view_count) {
  2778. rt->size.x = p_width;
  2779. rt->size.y = p_height;
  2780. rt->view_count = p_view_count;
  2781. _update_render_target(rt);
  2782. }
  2783. }
  2784. Size2i TextureStorage::render_target_get_size(RID p_render_target) const {
  2785. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2786. ERR_FAIL_COND_V(!rt, Size2i());
  2787. return rt->size;
  2788. }
  2789. RID TextureStorage::render_target_get_texture(RID p_render_target) {
  2790. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2791. ERR_FAIL_COND_V(!rt, RID());
  2792. return rt->texture;
  2793. }
  2794. void TextureStorage::render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) {
  2795. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2796. ERR_FAIL_COND(!rt);
  2797. rt->overridden.color = p_color_texture;
  2798. rt->overridden.depth = p_depth_texture;
  2799. rt->overridden.velocity = p_velocity_texture;
  2800. }
  2801. RID TextureStorage::render_target_get_override_color(RID p_render_target) const {
  2802. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2803. ERR_FAIL_COND_V(!rt, RID());
  2804. return rt->overridden.color;
  2805. }
  2806. RID TextureStorage::render_target_get_override_depth(RID p_render_target) const {
  2807. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2808. ERR_FAIL_COND_V(!rt, RID());
  2809. return rt->overridden.depth;
  2810. }
  2811. RID TextureStorage::render_target_get_override_depth_slice(RID p_render_target, const uint32_t p_layer) const {
  2812. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2813. ERR_FAIL_COND_V(!rt, RID());
  2814. if (rt->overridden.depth.is_null()) {
  2815. return RID();
  2816. } else if (rt->view_count == 1) {
  2817. return rt->overridden.depth;
  2818. } else {
  2819. RenderTarget::RTOverridden::SliceKey key(rt->overridden.depth, p_layer);
  2820. if (!rt->overridden.cached_slices.has(key)) {
  2821. rt->overridden.cached_slices[key] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->overridden.depth, p_layer, 0);
  2822. }
  2823. return rt->overridden.cached_slices[key];
  2824. }
  2825. }
  2826. RID TextureStorage::render_target_get_override_velocity(RID p_render_target) const {
  2827. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2828. ERR_FAIL_COND_V(!rt, RID());
  2829. return rt->overridden.velocity;
  2830. }
  2831. RID TextureStorage::render_target_get_override_velocity_slice(RID p_render_target, const uint32_t p_layer) const {
  2832. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2833. ERR_FAIL_COND_V(!rt, RID());
  2834. if (rt->overridden.velocity.is_null()) {
  2835. return RID();
  2836. } else if (rt->view_count == 1) {
  2837. return rt->overridden.velocity;
  2838. } else {
  2839. RenderTarget::RTOverridden::SliceKey key(rt->overridden.velocity, p_layer);
  2840. if (!rt->overridden.cached_slices.has(key)) {
  2841. rt->overridden.cached_slices[key] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->overridden.velocity, p_layer, 0);
  2842. }
  2843. return rt->overridden.cached_slices[key];
  2844. }
  2845. }
  2846. void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_is_transparent) {
  2847. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2848. ERR_FAIL_COND(!rt);
  2849. rt->is_transparent = p_is_transparent;
  2850. _update_render_target(rt);
  2851. }
  2852. bool TextureStorage::render_target_get_transparent(RID p_render_target) const {
  2853. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2854. ERR_FAIL_COND_V(!rt, false);
  2855. return rt->is_transparent;
  2856. }
  2857. void TextureStorage::render_target_set_direct_to_screen(RID p_render_target, bool p_value) {
  2858. }
  2859. bool TextureStorage::render_target_get_direct_to_screen(RID p_render_target) const {
  2860. return false;
  2861. }
  2862. bool TextureStorage::render_target_was_used(RID p_render_target) const {
  2863. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2864. ERR_FAIL_COND_V(!rt, false);
  2865. return rt->was_used;
  2866. }
  2867. void TextureStorage::render_target_set_as_unused(RID p_render_target) {
  2868. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2869. ERR_FAIL_COND(!rt);
  2870. rt->was_used = false;
  2871. }
  2872. void TextureStorage::render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) {
  2873. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2874. ERR_FAIL_COND(!rt);
  2875. if (p_msaa == rt->msaa) {
  2876. return;
  2877. }
  2878. rt->msaa = p_msaa;
  2879. _update_render_target(rt);
  2880. }
  2881. RS::ViewportMSAA TextureStorage::render_target_get_msaa(RID p_render_target) const {
  2882. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2883. ERR_FAIL_COND_V(!rt, RS::VIEWPORT_MSAA_DISABLED);
  2884. return rt->msaa;
  2885. }
  2886. void TextureStorage::render_target_set_use_hdr(RID p_render_target, bool p_use_hdr) {
  2887. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2888. ERR_FAIL_COND(!rt);
  2889. if (p_use_hdr == rt->use_hdr) {
  2890. return;
  2891. }
  2892. rt->use_hdr = p_use_hdr;
  2893. _update_render_target(rt);
  2894. }
  2895. bool TextureStorage::render_target_is_using_hdr(RID p_render_target) const {
  2896. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2897. ERR_FAIL_COND_V(!rt, false);
  2898. return rt->use_hdr;
  2899. }
  2900. RID TextureStorage::render_target_get_rd_framebuffer(RID p_render_target) {
  2901. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2902. ERR_FAIL_COND_V(!rt, RID());
  2903. return rt->get_framebuffer();
  2904. }
  2905. RID TextureStorage::render_target_get_rd_texture(RID p_render_target) {
  2906. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2907. ERR_FAIL_COND_V(!rt, RID());
  2908. if (rt->overridden.color.is_valid()) {
  2909. return rt->overridden.color;
  2910. } else {
  2911. return rt->color;
  2912. }
  2913. }
  2914. RID TextureStorage::render_target_get_rd_texture_slice(RID p_render_target, uint32_t p_layer) {
  2915. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2916. ERR_FAIL_COND_V(!rt, RID());
  2917. if (rt->view_count == 1) {
  2918. return rt->color;
  2919. } else {
  2920. ERR_FAIL_UNSIGNED_INDEX_V(p_layer, rt->view_count, RID());
  2921. if (rt->color_slices.size() == 0) {
  2922. for (uint32_t v = 0; v < rt->view_count; v++) {
  2923. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->color, v, 0);
  2924. rt->color_slices.push_back(slice);
  2925. }
  2926. }
  2927. return rt->color_slices[p_layer];
  2928. }
  2929. }
  2930. RID TextureStorage::render_target_get_rd_texture_msaa(RID p_render_target) {
  2931. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2932. ERR_FAIL_COND_V(!rt, RID());
  2933. return rt->color_multisample;
  2934. }
  2935. RID TextureStorage::render_target_get_rd_backbuffer(RID p_render_target) {
  2936. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2937. ERR_FAIL_COND_V(!rt, RID());
  2938. return rt->backbuffer;
  2939. }
  2940. RID TextureStorage::render_target_get_rd_backbuffer_framebuffer(RID p_render_target) {
  2941. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2942. ERR_FAIL_COND_V(!rt, RID());
  2943. if (!rt->backbuffer.is_valid()) {
  2944. _create_render_target_backbuffer(rt);
  2945. }
  2946. return rt->backbuffer_fb;
  2947. }
  2948. void TextureStorage::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  2949. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2950. ERR_FAIL_COND(!rt);
  2951. rt->clear_requested = true;
  2952. rt->clear_color = p_clear_color;
  2953. }
  2954. bool TextureStorage::render_target_is_clear_requested(RID p_render_target) {
  2955. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2956. ERR_FAIL_COND_V(!rt, false);
  2957. return rt->clear_requested;
  2958. }
  2959. Color TextureStorage::render_target_get_clear_request_color(RID p_render_target) {
  2960. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2961. ERR_FAIL_COND_V(!rt, Color());
  2962. return rt->use_hdr ? rt->clear_color.srgb_to_linear() : rt->clear_color;
  2963. }
  2964. void TextureStorage::render_target_disable_clear_request(RID p_render_target) {
  2965. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2966. ERR_FAIL_COND(!rt);
  2967. rt->clear_requested = false;
  2968. }
  2969. void TextureStorage::render_target_do_clear_request(RID p_render_target) {
  2970. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2971. ERR_FAIL_COND(!rt);
  2972. if (!rt->clear_requested) {
  2973. return;
  2974. }
  2975. Vector<Color> clear_colors;
  2976. clear_colors.push_back(rt->use_hdr ? rt->clear_color.srgb_to_linear() : rt->clear_color);
  2977. RD::get_singleton()->draw_list_begin(rt->get_framebuffer(), RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors);
  2978. RD::get_singleton()->draw_list_end();
  2979. rt->clear_requested = false;
  2980. }
  2981. void TextureStorage::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  2982. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2983. ERR_FAIL_COND(!rt);
  2984. if (rt->sdf_oversize == p_size && rt->sdf_scale == p_scale) {
  2985. return;
  2986. }
  2987. rt->sdf_oversize = p_size;
  2988. rt->sdf_scale = p_scale;
  2989. _render_target_clear_sdf(rt);
  2990. }
  2991. Rect2i TextureStorage::_render_target_get_sdf_rect(const RenderTarget *rt) const {
  2992. Size2i margin;
  2993. int scale;
  2994. switch (rt->sdf_oversize) {
  2995. case RS::VIEWPORT_SDF_OVERSIZE_100_PERCENT: {
  2996. scale = 100;
  2997. } break;
  2998. case RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT: {
  2999. scale = 120;
  3000. } break;
  3001. case RS::VIEWPORT_SDF_OVERSIZE_150_PERCENT: {
  3002. scale = 150;
  3003. } break;
  3004. case RS::VIEWPORT_SDF_OVERSIZE_200_PERCENT: {
  3005. scale = 200;
  3006. } break;
  3007. default: {
  3008. }
  3009. }
  3010. margin = (rt->size * scale / 100) - rt->size;
  3011. Rect2i r(Vector2i(), rt->size);
  3012. r.position -= margin;
  3013. r.size += margin * 2;
  3014. return r;
  3015. }
  3016. Rect2i TextureStorage::render_target_get_sdf_rect(RID p_render_target) const {
  3017. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3018. ERR_FAIL_COND_V(!rt, Rect2i());
  3019. return _render_target_get_sdf_rect(rt);
  3020. }
  3021. void TextureStorage::render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) {
  3022. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3023. ERR_FAIL_COND(!rt);
  3024. rt->sdf_enabled = p_enabled;
  3025. }
  3026. bool TextureStorage::render_target_is_sdf_enabled(RID p_render_target) const {
  3027. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3028. ERR_FAIL_COND_V(!rt, false);
  3029. return rt->sdf_enabled;
  3030. }
  3031. RID TextureStorage::render_target_get_sdf_texture(RID p_render_target) {
  3032. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3033. ERR_FAIL_COND_V(!rt, RID());
  3034. if (rt->sdf_buffer_read.is_null()) {
  3035. // no texture, create a dummy one for the 2D uniform set
  3036. RD::TextureFormat tformat;
  3037. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  3038. tformat.width = 4;
  3039. tformat.height = 4;
  3040. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
  3041. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  3042. Vector<uint8_t> pv;
  3043. pv.resize(16 * 4);
  3044. memset(pv.ptrw(), 0, 16 * 4);
  3045. Vector<Vector<uint8_t>> vpv;
  3046. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  3047. }
  3048. return rt->sdf_buffer_read;
  3049. }
  3050. void TextureStorage::_render_target_allocate_sdf(RenderTarget *rt) {
  3051. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_valid());
  3052. if (rt->sdf_buffer_read.is_valid()) {
  3053. RD::get_singleton()->free(rt->sdf_buffer_read);
  3054. rt->sdf_buffer_read = RID();
  3055. }
  3056. Size2i size = _render_target_get_sdf_rect(rt).size;
  3057. RD::TextureFormat tformat;
  3058. tformat.format = RD::DATA_FORMAT_R8_UNORM;
  3059. tformat.width = size.width;
  3060. tformat.height = size.height;
  3061. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3062. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  3063. rt->sdf_buffer_write = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  3064. {
  3065. Vector<RID> write_fb;
  3066. write_fb.push_back(rt->sdf_buffer_write);
  3067. rt->sdf_buffer_write_fb = RD::get_singleton()->framebuffer_create(write_fb);
  3068. }
  3069. int scale;
  3070. switch (rt->sdf_scale) {
  3071. case RS::VIEWPORT_SDF_SCALE_100_PERCENT: {
  3072. scale = 100;
  3073. } break;
  3074. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  3075. scale = 50;
  3076. } break;
  3077. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  3078. scale = 25;
  3079. } break;
  3080. default: {
  3081. scale = 100;
  3082. } break;
  3083. }
  3084. rt->process_size = size * scale / 100;
  3085. rt->process_size.x = MAX(rt->process_size.x, 1);
  3086. rt->process_size.y = MAX(rt->process_size.y, 1);
  3087. tformat.format = RD::DATA_FORMAT_R16G16_SINT;
  3088. tformat.width = rt->process_size.width;
  3089. tformat.height = rt->process_size.height;
  3090. tformat.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  3091. rt->sdf_buffer_process[0] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  3092. rt->sdf_buffer_process[1] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  3093. tformat.format = RD::DATA_FORMAT_R16_SNORM;
  3094. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  3095. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  3096. {
  3097. Vector<RD::Uniform> uniforms;
  3098. {
  3099. RD::Uniform u;
  3100. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3101. u.binding = 1;
  3102. u.append_id(rt->sdf_buffer_write);
  3103. uniforms.push_back(u);
  3104. }
  3105. {
  3106. RD::Uniform u;
  3107. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3108. u.binding = 2;
  3109. u.append_id(rt->sdf_buffer_read);
  3110. uniforms.push_back(u);
  3111. }
  3112. {
  3113. RD::Uniform u;
  3114. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3115. u.binding = 3;
  3116. u.append_id(rt->sdf_buffer_process[0]);
  3117. uniforms.push_back(u);
  3118. }
  3119. {
  3120. RD::Uniform u;
  3121. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3122. u.binding = 4;
  3123. u.append_id(rt->sdf_buffer_process[1]);
  3124. uniforms.push_back(u);
  3125. }
  3126. 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);
  3127. RID aux2 = uniforms.write[2].get_id(0);
  3128. RID aux3 = uniforms.write[3].get_id(0);
  3129. uniforms.write[2].set_id(0, aux3);
  3130. uniforms.write[3].set_id(0, aux2);
  3131. 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);
  3132. }
  3133. }
  3134. void TextureStorage::_render_target_clear_sdf(RenderTarget *rt) {
  3135. if (rt->sdf_buffer_read.is_valid()) {
  3136. RD::get_singleton()->free(rt->sdf_buffer_read);
  3137. rt->sdf_buffer_read = RID();
  3138. }
  3139. if (rt->sdf_buffer_write_fb.is_valid()) {
  3140. RD::get_singleton()->free(rt->sdf_buffer_write);
  3141. RD::get_singleton()->free(rt->sdf_buffer_process[0]);
  3142. RD::get_singleton()->free(rt->sdf_buffer_process[1]);
  3143. rt->sdf_buffer_write = RID();
  3144. rt->sdf_buffer_write_fb = RID();
  3145. rt->sdf_buffer_process[0] = RID();
  3146. rt->sdf_buffer_process[1] = RID();
  3147. rt->sdf_buffer_process_uniform_sets[0] = RID();
  3148. rt->sdf_buffer_process_uniform_sets[1] = RID();
  3149. }
  3150. }
  3151. RID TextureStorage::render_target_get_sdf_framebuffer(RID p_render_target) {
  3152. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3153. ERR_FAIL_COND_V(!rt, RID());
  3154. if (rt->sdf_buffer_write_fb.is_null()) {
  3155. _render_target_allocate_sdf(rt);
  3156. }
  3157. return rt->sdf_buffer_write_fb;
  3158. }
  3159. void TextureStorage::render_target_sdf_process(RID p_render_target) {
  3160. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3161. ERR_FAIL_COND(!rt);
  3162. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_null());
  3163. RenderTargetSDF::PushConstant push_constant;
  3164. Rect2i r = _render_target_get_sdf_rect(rt);
  3165. push_constant.size[0] = r.size.width;
  3166. push_constant.size[1] = r.size.height;
  3167. push_constant.stride = 0;
  3168. push_constant.shift = 0;
  3169. push_constant.base_size[0] = r.size.width;
  3170. push_constant.base_size[1] = r.size.height;
  3171. bool shrink = false;
  3172. switch (rt->sdf_scale) {
  3173. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  3174. push_constant.size[0] >>= 1;
  3175. push_constant.size[1] >>= 1;
  3176. push_constant.shift = 1;
  3177. shrink = true;
  3178. } break;
  3179. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  3180. push_constant.size[0] >>= 2;
  3181. push_constant.size[1] >>= 2;
  3182. push_constant.shift = 2;
  3183. shrink = true;
  3184. } break;
  3185. default: {
  3186. };
  3187. }
  3188. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3189. /* Load */
  3190. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_LOAD_SHRINK : RenderTargetSDF::SHADER_LOAD]);
  3191. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[1], 0); //fill [0]
  3192. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  3193. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1);
  3194. /* Process */
  3195. int stride = nearest_power_of_2_templated(MAX(push_constant.size[0], push_constant.size[1]) / 2);
  3196. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[RenderTargetSDF::SHADER_PROCESS]);
  3197. RD::get_singleton()->compute_list_add_barrier(compute_list);
  3198. bool swap = false;
  3199. //jumpflood
  3200. while (stride > 0) {
  3201. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  3202. push_constant.stride = stride;
  3203. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  3204. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1);
  3205. stride /= 2;
  3206. swap = !swap;
  3207. RD::get_singleton()->compute_list_add_barrier(compute_list);
  3208. }
  3209. /* Store */
  3210. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_STORE_SHRINK : RenderTargetSDF::SHADER_STORE]);
  3211. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  3212. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  3213. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1);
  3214. RD::get_singleton()->compute_list_end();
  3215. }
  3216. void TextureStorage::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps) {
  3217. CopyEffects *copy_effects = CopyEffects::get_singleton();
  3218. ERR_FAIL_NULL(copy_effects);
  3219. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3220. ERR_FAIL_COND(!rt);
  3221. if (!rt->backbuffer.is_valid()) {
  3222. _create_render_target_backbuffer(rt);
  3223. }
  3224. Rect2i region;
  3225. if (p_region == Rect2i()) {
  3226. region.size = rt->size;
  3227. } else {
  3228. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  3229. if (region.size == Size2i()) {
  3230. return; //nothing to do
  3231. }
  3232. }
  3233. // TODO figure out stereo support here
  3234. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  3235. copy_effects->copy_to_rect(rt->color, rt->backbuffer_mipmap0, region, false, false, false, !rt->use_hdr, true);
  3236. } else {
  3237. copy_effects->copy_to_fb_rect(rt->color, rt->backbuffer_fb, region, false, false, false, false, RID(), false, true);
  3238. }
  3239. if (!p_gen_mipmaps) {
  3240. return;
  3241. }
  3242. RD::get_singleton()->draw_command_begin_label("Gaussian Blur Mipmaps");
  3243. //then mipmap blur
  3244. RID prev_texture = rt->color; //use color, not backbuffer, as bb has mipmaps.
  3245. Size2i texture_size = rt->size;
  3246. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  3247. region.position.x >>= 1;
  3248. region.position.y >>= 1;
  3249. region.size.x = MAX(1, region.size.x >> 1);
  3250. region.size.y = MAX(1, region.size.y >> 1);
  3251. texture_size.x = MAX(1, texture_size.x >> 1);
  3252. texture_size.y = MAX(1, texture_size.y >> 1);
  3253. RID mipmap = rt->backbuffer_mipmaps[i];
  3254. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  3255. copy_effects->gaussian_blur(prev_texture, mipmap, region, texture_size, !rt->use_hdr);
  3256. } else {
  3257. copy_effects->gaussian_blur_raster(prev_texture, mipmap, region, texture_size);
  3258. }
  3259. prev_texture = mipmap;
  3260. }
  3261. RD::get_singleton()->draw_command_end_label();
  3262. }
  3263. void TextureStorage::render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color) {
  3264. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3265. ERR_FAIL_COND(!rt);
  3266. CopyEffects *copy_effects = CopyEffects::get_singleton();
  3267. ERR_FAIL_NULL(copy_effects);
  3268. if (!rt->backbuffer.is_valid()) {
  3269. _create_render_target_backbuffer(rt);
  3270. }
  3271. Rect2i region;
  3272. if (p_region == Rect2i()) {
  3273. region.size = rt->size;
  3274. } else {
  3275. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  3276. if (region.size == Size2i()) {
  3277. return; //nothing to do
  3278. }
  3279. }
  3280. // Single texture copy for backbuffer.
  3281. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  3282. copy_effects->set_color(rt->backbuffer_mipmap0, p_color, region, !rt->use_hdr);
  3283. } else {
  3284. copy_effects->set_color_raster(rt->backbuffer_mipmap0, p_color, region);
  3285. }
  3286. }
  3287. void TextureStorage::render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region) {
  3288. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3289. ERR_FAIL_COND(!rt);
  3290. CopyEffects *copy_effects = CopyEffects::get_singleton();
  3291. ERR_FAIL_NULL(copy_effects);
  3292. if (!rt->backbuffer.is_valid()) {
  3293. _create_render_target_backbuffer(rt);
  3294. }
  3295. Rect2i region;
  3296. if (p_region == Rect2i()) {
  3297. region.size = rt->size;
  3298. } else {
  3299. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  3300. if (region.size == Size2i()) {
  3301. return; //nothing to do
  3302. }
  3303. }
  3304. RD::get_singleton()->draw_command_begin_label("Gaussian Blur Mipmaps2");
  3305. //then mipmap blur
  3306. RID prev_texture = rt->backbuffer_mipmap0;
  3307. Size2i texture_size = rt->size;
  3308. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  3309. region.position.x >>= 1;
  3310. region.position.y >>= 1;
  3311. region.size.x = MAX(1, region.size.x >> 1);
  3312. region.size.y = MAX(1, region.size.y >> 1);
  3313. texture_size.x = MAX(1, texture_size.x >> 1);
  3314. texture_size.y = MAX(1, texture_size.y >> 1);
  3315. RID mipmap = rt->backbuffer_mipmaps[i];
  3316. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  3317. copy_effects->gaussian_blur(prev_texture, mipmap, region, texture_size, !rt->use_hdr);
  3318. } else {
  3319. copy_effects->gaussian_blur_raster(prev_texture, mipmap, region, texture_size);
  3320. }
  3321. prev_texture = mipmap;
  3322. }
  3323. RD::get_singleton()->draw_command_end_label();
  3324. }
  3325. RID TextureStorage::render_target_get_framebuffer_uniform_set(RID p_render_target) {
  3326. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3327. ERR_FAIL_COND_V(!rt, RID());
  3328. return rt->framebuffer_uniform_set;
  3329. }
  3330. RID TextureStorage::render_target_get_backbuffer_uniform_set(RID p_render_target) {
  3331. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3332. ERR_FAIL_COND_V(!rt, RID());
  3333. return rt->backbuffer_uniform_set;
  3334. }
  3335. void TextureStorage::render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  3336. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3337. ERR_FAIL_COND(!rt);
  3338. rt->framebuffer_uniform_set = p_uniform_set;
  3339. }
  3340. void TextureStorage::render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  3341. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3342. ERR_FAIL_COND(!rt);
  3343. rt->backbuffer_uniform_set = p_uniform_set;
  3344. }
  3345. void TextureStorage::render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) {
  3346. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3347. ERR_FAIL_COND(!rt);
  3348. rt->vrs_mode = p_mode;
  3349. }
  3350. RS::ViewportVRSMode TextureStorage::render_target_get_vrs_mode(RID p_render_target) const {
  3351. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3352. ERR_FAIL_COND_V(!rt, RS::VIEWPORT_VRS_DISABLED);
  3353. return rt->vrs_mode;
  3354. }
  3355. void TextureStorage::render_target_set_vrs_texture(RID p_render_target, RID p_texture) {
  3356. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3357. ERR_FAIL_COND(!rt);
  3358. rt->vrs_texture = p_texture;
  3359. }
  3360. RID TextureStorage::render_target_get_vrs_texture(RID p_render_target) const {
  3361. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  3362. ERR_FAIL_COND_V(!rt, RID());
  3363. return rt->vrs_texture;
  3364. }