texture_storage.cpp 110 KB

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