texture_storage.cpp 103 KB

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