texture_storage.cpp 131 KB

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