texture_storage.cpp 113 KB

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