texture_storage.cpp 108 KB

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