texture_storage.cpp 142 KB

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