texture_storage.cpp 93 KB

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