texture_storage.cpp 111 KB

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