texture_storage.cpp 140 KB

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