texture_storage.cpp 108 KB

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