texture_storage.cpp 113 KB

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