texture_storage.cpp 107 KB

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