texture_storage.cpp 96 KB

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