texture_storage.cpp 141 KB

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