texture_storage.cpp 91 KB

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