texture_storage.cpp 95 KB

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