texture_storage.cpp 96 KB

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