rasterizer_scene_rd.cpp 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925
  1. /*************************************************************************/
  2. /* rasterizer_scene_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "rasterizer_scene_rd.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "servers/visual/visual_server_raster.h"
  34. uint64_t RasterizerSceneRD::auto_exposure_counter = 2;
  35. void RasterizerSceneRD::_clear_reflection_data(ReflectionData &rd) {
  36. rd.layers.clear();
  37. rd.radiance_base_cubemap = RID();
  38. }
  39. void RasterizerSceneRD::_update_reflection_data(ReflectionData &rd, int p_size, int p_mipmaps, bool p_use_array, RID p_base_cube, int p_base_layer) {
  40. //recreate radiance and all data
  41. int mipmaps = p_mipmaps;
  42. uint32_t w = p_size, h = p_size;
  43. if (p_use_array) {
  44. for (int i = 0; i < roughness_layers; i++) {
  45. ReflectionData::Layer layer;
  46. uint32_t mmw = w;
  47. uint32_t mmh = h;
  48. layer.mipmaps.resize(mipmaps);
  49. for (int j = 0; j < mipmaps; j++) {
  50. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  51. mm.size.width = mmw;
  52. mm.size.height = mmh;
  53. for (int k = 0; k < 6; k++) {
  54. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6 + k, j);
  55. Vector<RID> fbtex;
  56. fbtex.push_back(mm.views[k]);
  57. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  58. }
  59. mmw = MAX(1, mmw >> 1);
  60. mmh = MAX(1, mmh >> 1);
  61. }
  62. rd.layers.push_back(layer);
  63. }
  64. } else {
  65. //regular cubemap, lower quality (aliasing, less memory)
  66. ReflectionData::Layer layer;
  67. uint32_t mmw = w;
  68. uint32_t mmh = h;
  69. layer.mipmaps.resize(roughness_layers);
  70. for (int j = 0; j < roughness_layers; j++) {
  71. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  72. mm.size.width = mmw;
  73. mm.size.height = mmh;
  74. for (int k = 0; k < 6; k++) {
  75. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + k, j);
  76. Vector<RID> fbtex;
  77. fbtex.push_back(mm.views[k]);
  78. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  79. }
  80. mmw = MAX(1, mmw >> 1);
  81. mmh = MAX(1, mmh >> 1);
  82. }
  83. rd.layers.push_back(layer);
  84. }
  85. rd.radiance_base_cubemap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, 0, RD::TEXTURE_SLICE_CUBEMAP);
  86. }
  87. void RasterizerSceneRD::_create_reflection_from_panorama(ReflectionData &rd, RID p_panorama, bool p_quality) {
  88. #ifndef _MSC_VER
  89. #warning TODO, should probably use this algorithm instead. Volunteers? - https://www.ppsloan.org/publications/ggx_filtering.pdf / https://github.com/dariomanesku/cmft
  90. #endif
  91. if (sky_use_cubemap_array) {
  92. if (p_quality) {
  93. //render directly to the layers
  94. for (int i = 0; i < rd.layers.size(); i++) {
  95. for (int j = 0; j < 6; j++) {
  96. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[i].mipmaps[0].framebuffers[j], j, sky_ggx_samples_quality, float(i) / (rd.layers.size() - 1.0));
  97. }
  98. }
  99. } else {
  100. //render to first mipmap
  101. for (int j = 0; j < 6; j++) {
  102. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[0].mipmaps[0].framebuffers[j], j, sky_ggx_samples_realtime, 0.0);
  103. }
  104. //do the rest in other mipmaps and use cubemap itself as source
  105. for (int i = 1; i < roughness_layers; i++) {
  106. //render using a smaller mipmap, then copy to main layer
  107. for (int j = 0; j < 6; j++) {
  108. //storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[0].mipmaps[i].framebuffers[0], j, sky_ggx_samples_realtime, float(i) / (rd.layers.size() - 1.0));
  109. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[0].mipmaps[i].framebuffers[0], j, sky_ggx_samples_realtime, float(i) / (rd.layers.size() - 1.0));
  110. storage->get_effects()->region_copy(rd.layers[0].mipmaps[i].views[0], rd.layers[i].mipmaps[0].framebuffers[j], Rect2());
  111. }
  112. }
  113. }
  114. } else {
  115. if (p_quality) {
  116. //render directly to the layers
  117. for (int i = 0; i < rd.layers[0].mipmaps.size(); i++) {
  118. for (int j = 0; j < 6; j++) {
  119. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[0].mipmaps[i].framebuffers[j], j, sky_ggx_samples_quality, float(i) / (rd.layers[0].mipmaps.size() - 1.0));
  120. }
  121. }
  122. } else {
  123. for (int j = 0; j < 6; j++) {
  124. storage->get_effects()->cubemap_roughness(p_panorama, true, rd.layers[0].mipmaps[0].framebuffers[j], j, sky_ggx_samples_realtime, 0);
  125. }
  126. for (int i = 1; i < rd.layers[0].mipmaps.size(); i++) {
  127. for (int j = 0; j < 6; j++) {
  128. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[0].mipmaps[i].framebuffers[j], j, sky_ggx_samples_realtime, float(i) / (rd.layers[0].mipmaps.size() - 1.0));
  129. }
  130. }
  131. }
  132. }
  133. }
  134. void RasterizerSceneRD::_create_reflection_from_base_mipmap(ReflectionData &rd, bool p_use_arrays, bool p_quality, int p_cube_side) {
  135. if (p_use_arrays) {
  136. if (p_quality) {
  137. //render directly to the layers
  138. for (int i = 1; i < rd.layers.size(); i++) {
  139. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[i].mipmaps[0].framebuffers[p_cube_side], p_cube_side, sky_ggx_samples_quality, float(i) / (rd.layers.size() - 1.0));
  140. }
  141. } else {
  142. //do the rest in other mipmaps and use cubemap itself as source
  143. for (int i = 1; i < roughness_layers; i++) {
  144. //render using a smaller mipmap, then copy to main layer
  145. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[0].mipmaps[i].framebuffers[0], p_cube_side, sky_ggx_samples_realtime, float(i) / (rd.layers.size() - 1.0));
  146. storage->get_effects()->region_copy(rd.layers[0].mipmaps[i].views[0], rd.layers[i].mipmaps[0].framebuffers[p_cube_side], Rect2());
  147. }
  148. }
  149. } else {
  150. if (p_quality) {
  151. //render directly to the layers
  152. for (int i = 1; i < rd.layers[0].mipmaps.size(); i++) {
  153. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[0].mipmaps[i].framebuffers[p_cube_side], p_cube_side, sky_ggx_samples_quality, float(i) / (rd.layers[0].mipmaps.size() - 1.0));
  154. }
  155. } else {
  156. for (int i = 1; i < rd.layers[0].mipmaps.size(); i++) {
  157. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, false, rd.layers[0].mipmaps[i].framebuffers[p_cube_side], p_cube_side, sky_ggx_samples_realtime, float(i) / (rd.layers[0].mipmaps.size() - 1.0));
  158. }
  159. }
  160. }
  161. }
  162. void RasterizerSceneRD::_update_reflection_mipmaps(ReflectionData &rd, bool p_quality) {
  163. if (sky_use_cubemap_array) {
  164. for (int i = 0; i < rd.layers.size(); i++) {
  165. for (int j = 0; j < rd.layers[i].mipmaps.size() - 1; j++) {
  166. for (int k = 0; k < 6; k++) {
  167. RID view = rd.layers[i].mipmaps[j].views[k];
  168. RID fb = rd.layers[i].mipmaps[j + 1].framebuffers[k];
  169. Vector2 size = rd.layers[i].mipmaps[j].size;
  170. size = Vector2(1.0 / size.x, 1.0 / size.y);
  171. storage->get_effects()->make_mipmap(view, fb, size);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. RID RasterizerSceneRD::sky_create() {
  178. return sky_owner.make_rid(Sky());
  179. }
  180. void RasterizerSceneRD::_sky_invalidate(Sky *p_sky) {
  181. if (!p_sky->dirty) {
  182. p_sky->dirty = true;
  183. p_sky->dirty_list = dirty_sky_list;
  184. dirty_sky_list = p_sky;
  185. }
  186. }
  187. void RasterizerSceneRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  188. Sky *sky = sky_owner.getornull(p_sky);
  189. ERR_FAIL_COND(!sky);
  190. ERR_FAIL_COND(p_radiance_size < 32 || p_radiance_size > 2048);
  191. if (sky->radiance_size == p_radiance_size) {
  192. return;
  193. }
  194. sky->radiance_size = p_radiance_size;
  195. _sky_invalidate(sky);
  196. if (sky->radiance.is_valid()) {
  197. RD::get_singleton()->free(sky->radiance);
  198. sky->radiance = RID();
  199. }
  200. _clear_reflection_data(sky->reflection);
  201. }
  202. void RasterizerSceneRD::sky_set_mode(RID p_sky, VS::SkyMode p_mode) {
  203. Sky *sky = sky_owner.getornull(p_sky);
  204. ERR_FAIL_COND(!sky);
  205. if (sky->mode == p_mode) {
  206. return;
  207. }
  208. sky->mode = p_mode;
  209. _sky_invalidate(sky);
  210. }
  211. void RasterizerSceneRD::sky_set_texture(RID p_sky, RID p_panorama) {
  212. Sky *sky = sky_owner.getornull(p_sky);
  213. ERR_FAIL_COND(!sky);
  214. if (sky->panorama.is_valid()) {
  215. sky->panorama = RID();
  216. if (sky->radiance.is_valid()) {
  217. RD::get_singleton()->free(sky->radiance);
  218. sky->radiance = RID();
  219. }
  220. _clear_reflection_data(sky->reflection);
  221. }
  222. sky->panorama = p_panorama;
  223. if (!sky->panorama.is_valid())
  224. return; //cleared
  225. _sky_invalidate(sky);
  226. }
  227. void RasterizerSceneRD::_update_dirty_skys() {
  228. Sky *sky = dirty_sky_list;
  229. while (sky) {
  230. //update sky configuration if texture is missing
  231. if (sky->radiance.is_null()) {
  232. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  233. if (sky->mode != VS::SKY_MODE_QUALITY) {
  234. //use less mipmaps
  235. mipmaps = MIN(8, mipmaps);
  236. }
  237. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  238. if (sky_use_cubemap_array) {
  239. //array (higher quality, 6 times more memory)
  240. RD::TextureFormat tf;
  241. tf.array_layers = roughness_layers * 6;
  242. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  243. tf.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  244. tf.mipmaps = mipmaps;
  245. tf.width = w;
  246. tf.height = h;
  247. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  248. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  249. _update_reflection_data(sky->reflection, sky->radiance_size, mipmaps, true, sky->radiance, 0);
  250. } else {
  251. //regular cubemap, lower quality (aliasing, less memory)
  252. RD::TextureFormat tf;
  253. tf.array_layers = 6;
  254. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  255. tf.type = RD::TEXTURE_TYPE_CUBE;
  256. tf.mipmaps = roughness_layers;
  257. tf.width = w;
  258. tf.height = h;
  259. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  260. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  261. _update_reflection_data(sky->reflection, sky->radiance_size, mipmaps, false, sky->radiance, 0);
  262. }
  263. }
  264. RID panorama_texture = storage->texture_get_rd_texture(sky->panorama);
  265. if (panorama_texture.is_valid()) {
  266. //is there a panorama texture?
  267. _create_reflection_from_panorama(sky->reflection, panorama_texture, sky->mode == VS::SKY_MODE_QUALITY);
  268. _update_reflection_mipmaps(sky->reflection, sky->mode == VS::SKY_MODE_QUALITY);
  269. }
  270. Sky *next = sky->dirty_list;
  271. sky->dirty_list = nullptr;
  272. sky->dirty = false;
  273. sky = next;
  274. }
  275. dirty_sky_list = nullptr;
  276. }
  277. RID RasterizerSceneRD::sky_get_panorama_texture_rd(RID p_sky) const {
  278. Sky *sky = sky_owner.getornull(p_sky);
  279. ERR_FAIL_COND_V(!sky, RID());
  280. if (sky->panorama.is_null()) {
  281. return RID();
  282. }
  283. return storage->texture_get_rd_texture(sky->panorama, true);
  284. }
  285. RID RasterizerSceneRD::sky_get_radiance_texture_rd(RID p_sky) const {
  286. Sky *sky = sky_owner.getornull(p_sky);
  287. ERR_FAIL_COND_V(!sky, RID());
  288. return sky->radiance;
  289. }
  290. RID RasterizerSceneRD::environment_create() {
  291. return environment_owner.make_rid(Environent());
  292. }
  293. void RasterizerSceneRD::environment_set_background(RID p_env, VS::EnvironmentBG p_bg) {
  294. Environent *env = environment_owner.getornull(p_env);
  295. ERR_FAIL_COND(!env);
  296. env->background = p_bg;
  297. }
  298. void RasterizerSceneRD::environment_set_sky(RID p_env, RID p_sky) {
  299. Environent *env = environment_owner.getornull(p_env);
  300. ERR_FAIL_COND(!env);
  301. env->sky = p_sky;
  302. }
  303. void RasterizerSceneRD::environment_set_sky_custom_fov(RID p_env, float p_scale) {
  304. Environent *env = environment_owner.getornull(p_env);
  305. ERR_FAIL_COND(!env);
  306. env->sky_custom_fov = p_scale;
  307. }
  308. void RasterizerSceneRD::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
  309. Environent *env = environment_owner.getornull(p_env);
  310. ERR_FAIL_COND(!env);
  311. env->sky_orientation = p_orientation;
  312. }
  313. void RasterizerSceneRD::environment_set_bg_color(RID p_env, const Color &p_color) {
  314. Environent *env = environment_owner.getornull(p_env);
  315. ERR_FAIL_COND(!env);
  316. env->bg_color = p_color;
  317. }
  318. void RasterizerSceneRD::environment_set_bg_energy(RID p_env, float p_energy) {
  319. Environent *env = environment_owner.getornull(p_env);
  320. ERR_FAIL_COND(!env);
  321. env->bg_energy = p_energy;
  322. }
  323. void RasterizerSceneRD::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
  324. Environent *env = environment_owner.getornull(p_env);
  325. ERR_FAIL_COND(!env);
  326. env->canvas_max_layer = p_max_layer;
  327. }
  328. void RasterizerSceneRD::environment_set_ambient_light(RID p_env, const Color &p_color, VS::EnvironmentAmbientSource p_ambient, float p_energy, float p_sky_contribution, VS::EnvironmentReflectionSource p_reflection_source) {
  329. Environent *env = environment_owner.getornull(p_env);
  330. ERR_FAIL_COND(!env);
  331. env->ambient_light = p_color;
  332. env->ambient_source = p_ambient;
  333. env->ambient_light_energy = p_energy;
  334. env->ambient_sky_contribution = p_sky_contribution;
  335. env->reflection_source = p_reflection_source;
  336. }
  337. VS::EnvironmentBG RasterizerSceneRD::environment_get_background(RID p_env) const {
  338. Environent *env = environment_owner.getornull(p_env);
  339. ERR_FAIL_COND_V(!env, VS::ENV_BG_MAX);
  340. return env->background;
  341. }
  342. RID RasterizerSceneRD::environment_get_sky(RID p_env) const {
  343. Environent *env = environment_owner.getornull(p_env);
  344. ERR_FAIL_COND_V(!env, RID());
  345. return env->sky;
  346. }
  347. float RasterizerSceneRD::environment_get_sky_custom_fov(RID p_env) const {
  348. Environent *env = environment_owner.getornull(p_env);
  349. ERR_FAIL_COND_V(!env, 0);
  350. return env->sky_custom_fov;
  351. }
  352. Basis RasterizerSceneRD::environment_get_sky_orientation(RID p_env) const {
  353. Environent *env = environment_owner.getornull(p_env);
  354. ERR_FAIL_COND_V(!env, Basis());
  355. return env->sky_orientation;
  356. }
  357. Color RasterizerSceneRD::environment_get_bg_color(RID p_env) const {
  358. Environent *env = environment_owner.getornull(p_env);
  359. ERR_FAIL_COND_V(!env, Color());
  360. return env->bg_color;
  361. }
  362. float RasterizerSceneRD::environment_get_bg_energy(RID p_env) const {
  363. Environent *env = environment_owner.getornull(p_env);
  364. ERR_FAIL_COND_V(!env, 0);
  365. return env->bg_energy;
  366. }
  367. int RasterizerSceneRD::environment_get_canvas_max_layer(RID p_env) const {
  368. Environent *env = environment_owner.getornull(p_env);
  369. ERR_FAIL_COND_V(!env, 0);
  370. return env->canvas_max_layer;
  371. }
  372. Color RasterizerSceneRD::environment_get_ambient_light_color(RID p_env) const {
  373. Environent *env = environment_owner.getornull(p_env);
  374. ERR_FAIL_COND_V(!env, Color());
  375. return env->ambient_light;
  376. }
  377. VS::EnvironmentAmbientSource RasterizerSceneRD::environment_get_ambient_light_ambient_source(RID p_env) const {
  378. Environent *env = environment_owner.getornull(p_env);
  379. ERR_FAIL_COND_V(!env, VS::ENV_AMBIENT_SOURCE_BG);
  380. return env->ambient_source;
  381. }
  382. float RasterizerSceneRD::environment_get_ambient_light_ambient_energy(RID p_env) const {
  383. Environent *env = environment_owner.getornull(p_env);
  384. ERR_FAIL_COND_V(!env, 0);
  385. return env->ambient_light_energy;
  386. }
  387. float RasterizerSceneRD::environment_get_ambient_sky_contribution(RID p_env) const {
  388. Environent *env = environment_owner.getornull(p_env);
  389. ERR_FAIL_COND_V(!env, 0);
  390. return env->ambient_sky_contribution;
  391. }
  392. VS::EnvironmentReflectionSource RasterizerSceneRD::environment_get_reflection_source(RID p_env) const {
  393. Environent *env = environment_owner.getornull(p_env);
  394. ERR_FAIL_COND_V(!env, VS::ENV_REFLECTION_SOURCE_DISABLED);
  395. return env->reflection_source;
  396. }
  397. void RasterizerSceneRD::environment_set_tonemap(RID p_env, VS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) {
  398. Environent *env = environment_owner.getornull(p_env);
  399. ERR_FAIL_COND(!env);
  400. env->exposure = p_exposure;
  401. env->tone_mapper = p_tone_mapper;
  402. if (!env->auto_exposure && p_auto_exposure) {
  403. env->auto_exposure_version = ++auto_exposure_counter;
  404. }
  405. env->auto_exposure = p_auto_exposure;
  406. env->white = p_white;
  407. env->min_luminance = p_min_luminance;
  408. env->max_luminance = p_max_luminance;
  409. env->auto_exp_speed = p_auto_exp_speed;
  410. env->auto_exp_scale = p_auto_exp_scale;
  411. }
  412. void RasterizerSceneRD::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale) {
  413. Environent *env = environment_owner.getornull(p_env);
  414. ERR_FAIL_COND(!env);
  415. env->glow_enabled = p_enable;
  416. env->glow_levels = p_level_flags;
  417. env->glow_intensity = p_intensity;
  418. env->glow_strength = p_strength;
  419. env->glow_mix = p_mix;
  420. env->glow_bloom = p_bloom_threshold;
  421. env->glow_blend_mode = p_blend_mode;
  422. env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
  423. env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
  424. env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
  425. env->glow_bicubic_upscale = p_bicubic_upscale;
  426. }
  427. bool RasterizerSceneRD::is_environment(RID p_env) const {
  428. return environment_owner.owns(p_env);
  429. }
  430. ////////////////////////////////////////////////////////////
  431. RID RasterizerSceneRD::reflection_atlas_create() {
  432. ReflectionAtlas ra;
  433. ra.count = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_count");
  434. ra.size = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_size");
  435. return reflection_atlas_owner.make_rid(ra);
  436. }
  437. void RasterizerSceneRD::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  438. ReflectionAtlas *ra = reflection_atlas_owner.getornull(p_ref_atlas);
  439. ERR_FAIL_COND(!ra);
  440. if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
  441. return; //no changes
  442. }
  443. if (ra->reflection.is_valid()) {
  444. //clear and invalidate everything
  445. RD::get_singleton()->free(ra->reflection);
  446. ra->reflection = RID();
  447. for (int i = 0; i < ra->reflections.size(); i++) {
  448. if (ra->reflections[i].owner.is_null()) {
  449. continue;
  450. }
  451. reflection_probe_release_atlas_index(ra->reflections[i].owner);
  452. //rp->atlasindex clear
  453. }
  454. ra->reflections.clear();
  455. }
  456. }
  457. ////////////////////////
  458. RID RasterizerSceneRD::reflection_probe_instance_create(RID p_probe) {
  459. ReflectionProbeInstance rpi;
  460. rpi.probe = p_probe;
  461. return reflection_probe_instance_owner.make_rid(rpi);
  462. }
  463. void RasterizerSceneRD::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) {
  464. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  465. ERR_FAIL_COND(!rpi);
  466. rpi->transform = p_transform;
  467. rpi->dirty = true;
  468. }
  469. void RasterizerSceneRD::reflection_probe_release_atlas_index(RID p_instance) {
  470. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  471. ERR_FAIL_COND(!rpi);
  472. if (rpi->atlas.is_null()) {
  473. return; //nothing to release
  474. }
  475. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  476. ERR_FAIL_COND(!atlas);
  477. ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
  478. atlas->reflections.write[rpi->atlas_index].owner = RID();
  479. rpi->atlas_index = -1;
  480. rpi->atlas = RID();
  481. }
  482. bool RasterizerSceneRD::reflection_probe_instance_needs_redraw(RID p_instance) {
  483. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  484. ERR_FAIL_COND_V(!rpi, false);
  485. if (rpi->rendering) {
  486. return false;
  487. }
  488. if (rpi->dirty) {
  489. return true;
  490. }
  491. if (storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  492. return true;
  493. }
  494. return rpi->atlas_index == -1;
  495. }
  496. bool RasterizerSceneRD::reflection_probe_instance_has_reflection(RID p_instance) {
  497. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  498. ERR_FAIL_COND_V(!rpi, false);
  499. return rpi->atlas.is_valid();
  500. }
  501. bool RasterizerSceneRD::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  502. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  503. ERR_FAIL_COND_V(!atlas, false);
  504. if (atlas->reflection.is_null()) {
  505. {
  506. //reflection atlas was unused, create:
  507. RD::TextureFormat tf;
  508. tf.array_layers = 6 * atlas->count;
  509. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  510. tf.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  511. tf.mipmaps = roughness_layers;
  512. tf.width = atlas->size;
  513. tf.height = atlas->size;
  514. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  515. atlas->reflection = RD::get_singleton()->texture_create(tf, RD::TextureView());
  516. }
  517. {
  518. RD::TextureFormat tf;
  519. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  520. tf.width = atlas->size;
  521. tf.height = atlas->size;
  522. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  523. atlas->depth_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  524. }
  525. atlas->reflections.resize(atlas->count);
  526. for (int i = 0; i < atlas->count; i++) {
  527. _update_reflection_data(atlas->reflections.write[i].data, atlas->size, roughness_layers, false, atlas->reflection, i * 6);
  528. for (int j = 0; j < 6; j++) {
  529. Vector<RID> fb;
  530. fb.push_back(atlas->reflections.write[i].data.layers[0].mipmaps[0].views[j]);
  531. fb.push_back(atlas->depth_buffer);
  532. atlas->reflections.write[i].fbs[j] = RD::get_singleton()->framebuffer_create(fb);
  533. }
  534. }
  535. Vector<RID> fb;
  536. fb.push_back(atlas->depth_buffer);
  537. atlas->depth_fb = RD::get_singleton()->framebuffer_create(fb);
  538. }
  539. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  540. ERR_FAIL_COND_V(!rpi, false);
  541. if (rpi->atlas_index == -1) {
  542. for (int i = 0; i < atlas->reflections.size(); i++) {
  543. if (atlas->reflections[i].owner.is_null()) {
  544. rpi->atlas_index = i;
  545. break;
  546. }
  547. }
  548. //find the one used last
  549. if (rpi->atlas_index == -1) {
  550. //everything is in use, find the one least used via LRU
  551. uint64_t pass_min = 0;
  552. for (int i = 0; i < atlas->reflections.size(); i++) {
  553. ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.getornull(atlas->reflections[i].owner);
  554. if (rpi2->last_pass < pass_min) {
  555. pass_min = rpi2->last_pass;
  556. rpi->atlas_index = i;
  557. }
  558. }
  559. }
  560. }
  561. rpi->atlas = p_reflection_atlas;
  562. rpi->rendering = true;
  563. rpi->dirty = false;
  564. rpi->processing_side = 0;
  565. return true;
  566. }
  567. bool RasterizerSceneRD::reflection_probe_instance_postprocess_step(RID p_instance) {
  568. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  569. ERR_FAIL_COND_V(!rpi, false);
  570. ERR_FAIL_COND_V(!rpi->rendering, false);
  571. ERR_FAIL_COND_V(rpi->atlas.is_null(), false);
  572. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  573. if (!atlas || rpi->atlas_index == -1) {
  574. //does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering)
  575. rpi->rendering = false;
  576. return false;
  577. }
  578. _create_reflection_from_base_mipmap(atlas->reflections.write[rpi->atlas_index].data, false, storage->reflection_probe_get_update_mode(rpi->probe) == VS::REFLECTION_PROBE_UPDATE_ONCE, rpi->processing_side);
  579. rpi->processing_side++;
  580. if (rpi->processing_side == 6) {
  581. rpi->rendering = false;
  582. rpi->processing_side = 0;
  583. return true;
  584. } else {
  585. return false;
  586. }
  587. }
  588. uint32_t RasterizerSceneRD::reflection_probe_instance_get_resolution(RID p_instance) {
  589. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  590. ERR_FAIL_COND_V(!rpi, 0);
  591. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  592. ERR_FAIL_COND_V(!atlas, 0);
  593. return atlas->size;
  594. }
  595. RID RasterizerSceneRD::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
  596. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  597. ERR_FAIL_COND_V(!rpi, RID());
  598. ERR_FAIL_INDEX_V(p_index, 6, RID());
  599. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  600. ERR_FAIL_COND_V(!atlas, RID());
  601. return atlas->reflections[rpi->atlas_index].fbs[p_index];
  602. }
  603. RID RasterizerSceneRD::reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index) {
  604. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  605. ERR_FAIL_COND_V(!rpi, RID());
  606. ERR_FAIL_INDEX_V(p_index, 6, RID());
  607. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  608. ERR_FAIL_COND_V(!atlas, RID());
  609. return atlas->depth_fb;
  610. }
  611. ///////////////////////////////////////////////////////////
  612. RID RasterizerSceneRD::shadow_atlas_create() {
  613. return shadow_atlas_owner.make_rid(ShadowAtlas());
  614. }
  615. void RasterizerSceneRD::shadow_atlas_set_size(RID p_atlas, int p_size) {
  616. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  617. ERR_FAIL_COND(!shadow_atlas);
  618. ERR_FAIL_COND(p_size < 0);
  619. p_size = next_power_of_2(p_size);
  620. p_size = MAX(p_size, 1 << roughness_layers);
  621. if (p_size == shadow_atlas->size)
  622. return;
  623. // erasing atlas
  624. if (shadow_atlas->depth.is_valid()) {
  625. RD::get_singleton()->free(shadow_atlas->depth);
  626. shadow_atlas->depth = RID();
  627. shadow_atlas->fb = RID();
  628. }
  629. for (int i = 0; i < 4; i++) {
  630. //clear subdivisions
  631. shadow_atlas->quadrants[i].shadows.resize(0);
  632. shadow_atlas->quadrants[i].shadows.resize(1 << shadow_atlas->quadrants[i].subdivision);
  633. }
  634. //erase shadow atlas reference from lights
  635. for (Map<RID, uint32_t>::Element *E = shadow_atlas->shadow_owners.front(); E; E = E->next()) {
  636. LightInstance *li = light_instance_owner.getornull(E->key());
  637. ERR_CONTINUE(!li);
  638. li->shadow_atlases.erase(p_atlas);
  639. }
  640. //clear owners
  641. shadow_atlas->shadow_owners.clear();
  642. shadow_atlas->size = p_size;
  643. if (shadow_atlas->size) {
  644. RD::TextureFormat tf;
  645. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  646. tf.width = shadow_atlas->size;
  647. tf.height = shadow_atlas->size;
  648. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  649. shadow_atlas->depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  650. Vector<RID> fb;
  651. fb.push_back(shadow_atlas->depth);
  652. shadow_atlas->fb = RD::get_singleton()->framebuffer_create(fb);
  653. }
  654. }
  655. void RasterizerSceneRD::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  656. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  657. ERR_FAIL_COND(!shadow_atlas);
  658. ERR_FAIL_INDEX(p_quadrant, 4);
  659. ERR_FAIL_INDEX(p_subdivision, 16384);
  660. uint32_t subdiv = next_power_of_2(p_subdivision);
  661. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  662. subdiv <<= 1;
  663. }
  664. subdiv = int(Math::sqrt((float)subdiv));
  665. //obtain the number that will be x*x
  666. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv)
  667. return;
  668. //erase all data from quadrant
  669. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  670. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  671. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  672. LightInstance *li = light_instance_owner.getornull(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  673. ERR_CONTINUE(!li);
  674. li->shadow_atlases.erase(p_atlas);
  675. }
  676. }
  677. shadow_atlas->quadrants[p_quadrant].shadows.resize(0);
  678. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  679. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  680. //cache the smallest subdiv (for faster allocation in light update)
  681. shadow_atlas->smallest_subdiv = 1 << 30;
  682. for (int i = 0; i < 4; i++) {
  683. if (shadow_atlas->quadrants[i].subdivision) {
  684. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  685. }
  686. }
  687. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  688. shadow_atlas->smallest_subdiv = 0;
  689. }
  690. //resort the size orders, simple bublesort for 4 elements..
  691. int swaps = 0;
  692. do {
  693. swaps = 0;
  694. for (int i = 0; i < 3; i++) {
  695. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  696. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  697. swaps++;
  698. }
  699. }
  700. } while (swaps > 0);
  701. }
  702. bool RasterizerSceneRD::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
  703. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  704. int qidx = p_in_quadrants[i];
  705. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  706. return false;
  707. }
  708. //look for an empty space
  709. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  710. ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptrw();
  711. int found_free_idx = -1; //found a free one
  712. int found_used_idx = -1; //found existing one, must steal it
  713. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
  714. for (int j = 0; j < sc; j++) {
  715. if (!sarr[j].owner.is_valid()) {
  716. found_free_idx = j;
  717. break;
  718. }
  719. LightInstance *sli = light_instance_owner.getornull(sarr[j].owner);
  720. ERR_CONTINUE(!sli);
  721. if (sli->last_scene_pass != scene_pass) {
  722. //was just allocated, don't kill it so soon, wait a bit..
  723. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec)
  724. continue;
  725. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  726. found_used_idx = j;
  727. min_pass = sli->last_scene_pass;
  728. }
  729. }
  730. }
  731. if (found_free_idx == -1 && found_used_idx == -1)
  732. continue; //nothing found
  733. if (found_free_idx == -1 && found_used_idx != -1) {
  734. found_free_idx = found_used_idx;
  735. }
  736. r_quadrant = qidx;
  737. r_shadow = found_free_idx;
  738. return true;
  739. }
  740. return false;
  741. }
  742. bool RasterizerSceneRD::shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) {
  743. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  744. ERR_FAIL_COND_V(!shadow_atlas, false);
  745. LightInstance *li = light_instance_owner.getornull(p_light_intance);
  746. ERR_FAIL_COND_V(!li, false);
  747. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  748. return false;
  749. }
  750. uint32_t quad_size = shadow_atlas->size >> 1;
  751. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  752. int valid_quadrants[4];
  753. int valid_quadrant_count = 0;
  754. int best_size = -1; //best size found
  755. int best_subdiv = -1; //subdiv for the best size
  756. //find the quadrants this fits into, and the best possible size it can fit into
  757. for (int i = 0; i < 4; i++) {
  758. int q = shadow_atlas->size_order[i];
  759. int sd = shadow_atlas->quadrants[q].subdivision;
  760. if (sd == 0)
  761. continue; //unused
  762. int max_fit = quad_size / sd;
  763. if (best_size != -1 && max_fit > best_size)
  764. break; //too large
  765. valid_quadrants[valid_quadrant_count++] = q;
  766. best_subdiv = sd;
  767. if (max_fit >= desired_fit) {
  768. best_size = max_fit;
  769. }
  770. }
  771. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  772. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  773. //see if it already exists
  774. if (shadow_atlas->shadow_owners.has(p_light_intance)) {
  775. //it does!
  776. uint32_t key = shadow_atlas->shadow_owners[p_light_intance];
  777. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  778. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  779. bool should_realloc = shadow_atlas->quadrants[q].subdivision != (uint32_t)best_subdiv && (shadow_atlas->quadrants[q].shadows[s].alloc_tick - tick > shadow_atlas_realloc_tolerance_msec);
  780. bool should_redraw = shadow_atlas->quadrants[q].shadows[s].version != p_light_version;
  781. if (!should_realloc) {
  782. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  783. //already existing, see if it should redraw or it's just OK
  784. return should_redraw;
  785. }
  786. int new_quadrant, new_shadow;
  787. //find a better place
  788. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, shadow_atlas->quadrants[q].subdivision, tick, new_quadrant, new_shadow)) {
  789. //found a better place!
  790. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  791. if (sh->owner.is_valid()) {
  792. //is taken, but is invalid, erasing it
  793. shadow_atlas->shadow_owners.erase(sh->owner);
  794. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  795. sli->shadow_atlases.erase(p_atlas);
  796. }
  797. //erase previous
  798. shadow_atlas->quadrants[q].shadows.write[s].version = 0;
  799. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  800. sh->owner = p_light_intance;
  801. sh->alloc_tick = tick;
  802. sh->version = p_light_version;
  803. li->shadow_atlases.insert(p_atlas);
  804. //make new key
  805. key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  806. key |= new_shadow;
  807. //update it in map
  808. shadow_atlas->shadow_owners[p_light_intance] = key;
  809. //make it dirty, as it should redraw anyway
  810. return true;
  811. }
  812. //no better place for this shadow found, keep current
  813. //already existing, see if it should redraw or it's just OK
  814. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  815. return should_redraw;
  816. }
  817. int new_quadrant, new_shadow;
  818. //find a better place
  819. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, -1, tick, new_quadrant, new_shadow)) {
  820. //found a better place!
  821. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  822. if (sh->owner.is_valid()) {
  823. //is taken, but is invalid, erasing it
  824. shadow_atlas->shadow_owners.erase(sh->owner);
  825. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  826. sli->shadow_atlases.erase(p_atlas);
  827. }
  828. sh->owner = p_light_intance;
  829. sh->alloc_tick = tick;
  830. sh->version = p_light_version;
  831. li->shadow_atlases.insert(p_atlas);
  832. //make new key
  833. uint32_t key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  834. key |= new_shadow;
  835. //update it in map
  836. shadow_atlas->shadow_owners[p_light_intance] = key;
  837. //make it dirty, as it should redraw anyway
  838. return true;
  839. }
  840. //no place to allocate this light, apologies
  841. return false;
  842. }
  843. void RasterizerSceneRD::directional_shadow_atlas_set_size(int p_size) {
  844. p_size = nearest_power_of_2_templated(p_size);
  845. if (directional_shadow.size == p_size) {
  846. return;
  847. }
  848. directional_shadow.size = p_size;
  849. if (directional_shadow.depth.is_valid()) {
  850. RD::get_singleton()->free(directional_shadow.depth);
  851. directional_shadow.depth = RID();
  852. directional_shadow.fb = RID();
  853. }
  854. if (p_size > 0) {
  855. RD::TextureFormat tf;
  856. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  857. tf.width = p_size;
  858. tf.height = p_size;
  859. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  860. directional_shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  861. Vector<RID> fb;
  862. fb.push_back(directional_shadow.depth);
  863. directional_shadow.fb = RD::get_singleton()->framebuffer_create(fb);
  864. }
  865. }
  866. void RasterizerSceneRD::set_directional_shadow_count(int p_count) {
  867. directional_shadow.light_count = p_count;
  868. directional_shadow.current_light = 0;
  869. }
  870. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  871. int split_h = 1;
  872. int split_v = 1;
  873. while (split_h * split_v < p_shadow_count) {
  874. if (split_h == split_v) {
  875. split_h <<= 1;
  876. } else {
  877. split_v <<= 1;
  878. }
  879. }
  880. Rect2i rect(0, 0, p_size, p_size);
  881. rect.size.width /= split_h;
  882. rect.size.height /= split_v;
  883. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  884. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  885. return rect;
  886. }
  887. int RasterizerSceneRD::get_directional_light_shadow_size(RID p_light_intance) {
  888. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  889. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  890. LightInstance *light_instance = light_instance_owner.getornull(p_light_intance);
  891. ERR_FAIL_COND_V(!light_instance, 0);
  892. switch (storage->light_directional_get_shadow_mode(light_instance->light)) {
  893. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  894. break; //none
  895. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: r.size.height /= 2; break;
  896. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: r.size /= 2; break;
  897. }
  898. return MAX(r.size.width, r.size.height);
  899. }
  900. //////////////////////////////////////////////////
  901. RID RasterizerSceneRD::camera_effects_create() {
  902. return camera_effects_owner.make_rid(CameraEffects());
  903. }
  904. void RasterizerSceneRD::camera_effects_set_dof_blur(RID p_camera_effects, bool p_far_enable, float p_far_distance, float p_far_transition, bool p_near_enable, float p_near_distance, float p_near_transition, float p_amount, VS::DOFBlurQuality p_quality) {
  905. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  906. ERR_FAIL_COND(!camfx);
  907. camfx->dof_blur_far_enabled = p_far_enable;
  908. camfx->dof_blur_far_distance = p_far_distance;
  909. camfx->dof_blur_far_transition = p_far_transition;
  910. camfx->dof_blur_near_enabled = p_near_enable;
  911. camfx->dof_blur_near_distance = p_near_distance;
  912. camfx->dof_blur_near_transition = p_near_transition;
  913. camfx->dof_blur_quality = p_quality;
  914. camfx->dof_blur_amount = p_amount;
  915. }
  916. void RasterizerSceneRD::camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure) {
  917. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  918. ERR_FAIL_COND(!camfx);
  919. camfx->override_exposure_enabled = p_enable;
  920. camfx->override_exposure = p_exposure;
  921. }
  922. RID RasterizerSceneRD::light_instance_create(RID p_light) {
  923. RID li = light_instance_owner.make_rid(LightInstance());
  924. LightInstance *light_instance = light_instance_owner.getornull(li);
  925. light_instance->self = li;
  926. light_instance->light = p_light;
  927. light_instance->light_type = storage->light_get_type(p_light);
  928. return li;
  929. }
  930. void RasterizerSceneRD::light_instance_set_transform(RID p_light_instance, const Transform &p_transform) {
  931. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  932. ERR_FAIL_COND(!light_instance);
  933. light_instance->transform = p_transform;
  934. }
  935. void RasterizerSceneRD::light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_bias_scale) {
  936. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  937. ERR_FAIL_COND(!light_instance);
  938. if (storage->light_get_type(light_instance->light) != VS::LIGHT_DIRECTIONAL) {
  939. p_pass = 0;
  940. }
  941. ERR_FAIL_INDEX(p_pass, 4);
  942. light_instance->shadow_transform[p_pass].camera = p_projection;
  943. light_instance->shadow_transform[p_pass].transform = p_transform;
  944. light_instance->shadow_transform[p_pass].farplane = p_far;
  945. light_instance->shadow_transform[p_pass].split = p_split;
  946. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  947. }
  948. void RasterizerSceneRD::light_instance_mark_visible(RID p_light_instance) {
  949. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  950. ERR_FAIL_COND(!light_instance);
  951. light_instance->last_scene_pass = scene_pass;
  952. }
  953. RasterizerSceneRD::ShadowCubemap *RasterizerSceneRD::_get_shadow_cubemap(int p_size) {
  954. if (!shadow_cubemaps.has(p_size)) {
  955. ShadowCubemap sc;
  956. {
  957. RD::TextureFormat tf;
  958. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  959. tf.width = p_size;
  960. tf.height = p_size;
  961. tf.type = RD::TEXTURE_TYPE_CUBE;
  962. tf.array_layers = 6;
  963. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  964. sc.cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  965. }
  966. for (int i = 0; i < 6; i++) {
  967. RID side_texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), sc.cubemap, i, 0);
  968. Vector<RID> fbtex;
  969. fbtex.push_back(side_texture);
  970. sc.side_fb[i] = RD::get_singleton()->framebuffer_create(fbtex);
  971. }
  972. shadow_cubemaps[p_size] = sc;
  973. }
  974. return &shadow_cubemaps[p_size];
  975. }
  976. RasterizerSceneRD::ShadowMap *RasterizerSceneRD::_get_shadow_map(const Size2i &p_size) {
  977. if (!shadow_maps.has(p_size)) {
  978. ShadowMap sm;
  979. {
  980. RD::TextureFormat tf;
  981. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  982. tf.width = p_size.width;
  983. tf.height = p_size.height;
  984. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  985. sm.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  986. }
  987. Vector<RID> fbtex;
  988. fbtex.push_back(sm.depth);
  989. sm.fb = RD::get_singleton()->framebuffer_create(fbtex);
  990. shadow_maps[p_size] = sm;
  991. }
  992. return &shadow_maps[p_size];
  993. }
  994. /////////////////////////////////
  995. RID RasterizerSceneRD::gi_probe_instance_create(RID p_base) {
  996. //find a free slot
  997. int index = -1;
  998. for (int i = 0; i < gi_probe_slots.size(); i++) {
  999. if (gi_probe_slots[i] == RID()) {
  1000. index = i;
  1001. break;
  1002. }
  1003. }
  1004. ERR_FAIL_COND_V(index == -1, RID());
  1005. GIProbeInstance gi_probe;
  1006. gi_probe.slot = index;
  1007. gi_probe.probe = p_base;
  1008. RID rid = gi_probe_instance_owner.make_rid(gi_probe);
  1009. gi_probe_slots.write[index] = rid;
  1010. return rid;
  1011. }
  1012. void RasterizerSceneRD::gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) {
  1013. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  1014. ERR_FAIL_COND(!gi_probe);
  1015. gi_probe->transform = p_xform;
  1016. }
  1017. bool RasterizerSceneRD::gi_probe_needs_update(RID p_probe) const {
  1018. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  1019. ERR_FAIL_COND_V(!gi_probe, false);
  1020. //return true;
  1021. return gi_probe->last_probe_version != storage->gi_probe_get_version(gi_probe->probe);
  1022. }
  1023. void RasterizerSceneRD::gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, int p_dynamic_object_count, InstanceBase **p_dynamic_objects) {
  1024. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  1025. ERR_FAIL_COND(!gi_probe);
  1026. uint32_t data_version = storage->gi_probe_get_data_version(gi_probe->probe);
  1027. // (RE)CREATE IF NEEDED
  1028. if (gi_probe->last_probe_data_version != data_version) {
  1029. //need to re-create everything
  1030. if (gi_probe->texture.is_valid()) {
  1031. RD::get_singleton()->free(gi_probe->texture);
  1032. if (gi_probe_use_anisotropy) {
  1033. RD::get_singleton()->free(gi_probe->anisotropy_r16[0]);
  1034. RD::get_singleton()->free(gi_probe->anisotropy_r16[1]);
  1035. }
  1036. RD::get_singleton()->free(gi_probe->write_buffer);
  1037. gi_probe->mipmaps.clear();
  1038. }
  1039. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  1040. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  1041. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  1042. }
  1043. gi_probe->dynamic_maps.clear();
  1044. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1045. if (octree_size != Vector3i()) {
  1046. //can create a 3D texture
  1047. PoolVector<int> levels = storage->gi_probe_get_level_counts(gi_probe->probe);
  1048. RD::TextureFormat tf;
  1049. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1050. tf.width = octree_size.x;
  1051. tf.height = octree_size.y;
  1052. tf.depth = octree_size.z;
  1053. tf.type = RD::TEXTURE_TYPE_3D;
  1054. tf.mipmaps = levels.size();
  1055. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  1056. gi_probe->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1057. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  1058. if (gi_probe_use_anisotropy) {
  1059. tf.format = RD::DATA_FORMAT_R16_UINT;
  1060. tf.shareable_formats.push_back(RD::DATA_FORMAT_R16_UINT);
  1061. tf.shareable_formats.push_back(RD::DATA_FORMAT_R5G6B5_UNORM_PACK16);
  1062. //need to create R16 first, else driver does not like the storage bit for compute..
  1063. gi_probe->anisotropy_r16[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1064. gi_probe->anisotropy_r16[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1065. RD::TextureView tv;
  1066. tv.format_override = RD::DATA_FORMAT_R5G6B5_UNORM_PACK16;
  1067. gi_probe->anisotropy[0] = RD::get_singleton()->texture_create_shared(tv, gi_probe->anisotropy_r16[0]);
  1068. gi_probe->anisotropy[1] = RD::get_singleton()->texture_create_shared(tv, gi_probe->anisotropy_r16[1]);
  1069. RD::get_singleton()->texture_clear(gi_probe->anisotropy[0], Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  1070. RD::get_singleton()->texture_clear(gi_probe->anisotropy[1], Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  1071. }
  1072. {
  1073. int total_elements = 0;
  1074. for (int i = 0; i < levels.size(); i++) {
  1075. total_elements += levels[i];
  1076. }
  1077. if (gi_probe_use_anisotropy) {
  1078. total_elements *= 6;
  1079. }
  1080. gi_probe->write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
  1081. }
  1082. for (int i = 0; i < levels.size(); i++) {
  1083. GIProbeInstance::Mipmap mipmap;
  1084. mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), gi_probe->texture, 0, i, RD::TEXTURE_SLICE_3D);
  1085. if (gi_probe_use_anisotropy) {
  1086. RD::TextureView tv;
  1087. tv.format_override = RD::DATA_FORMAT_R16_UINT;
  1088. mipmap.anisotropy[0] = RD::get_singleton()->texture_create_shared_from_slice(tv, gi_probe->anisotropy[0], 0, i, RD::TEXTURE_SLICE_3D);
  1089. mipmap.anisotropy[1] = RD::get_singleton()->texture_create_shared_from_slice(tv, gi_probe->anisotropy[1], 0, i, RD::TEXTURE_SLICE_3D);
  1090. }
  1091. mipmap.level = levels.size() - i - 1;
  1092. mipmap.cell_offset = 0;
  1093. for (uint32_t j = 0; j < mipmap.level; j++) {
  1094. mipmap.cell_offset += levels[j];
  1095. }
  1096. mipmap.cell_count = levels[mipmap.level];
  1097. Vector<RD::Uniform> uniforms;
  1098. {
  1099. RD::Uniform u;
  1100. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1101. u.binding = 1;
  1102. u.ids.push_back(storage->gi_probe_get_octree_buffer(gi_probe->probe));
  1103. uniforms.push_back(u);
  1104. }
  1105. {
  1106. RD::Uniform u;
  1107. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1108. u.binding = 2;
  1109. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  1110. uniforms.push_back(u);
  1111. }
  1112. {
  1113. RD::Uniform u;
  1114. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1115. u.binding = 4;
  1116. u.ids.push_back(gi_probe->write_buffer);
  1117. uniforms.push_back(u);
  1118. }
  1119. {
  1120. RD::Uniform u;
  1121. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1122. u.binding = 9;
  1123. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  1124. uniforms.push_back(u);
  1125. }
  1126. {
  1127. RD::Uniform u;
  1128. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1129. u.binding = 10;
  1130. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1131. uniforms.push_back(u);
  1132. }
  1133. {
  1134. Vector<RD::Uniform> copy_uniforms = uniforms;
  1135. if (i == 0) {
  1136. {
  1137. RD::Uniform u;
  1138. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1139. u.binding = 3;
  1140. u.ids.push_back(gi_probe_lights_uniform);
  1141. copy_uniforms.push_back(u);
  1142. }
  1143. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT], 0);
  1144. copy_uniforms = uniforms; //restore
  1145. {
  1146. RD::Uniform u;
  1147. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1148. u.binding = 5;
  1149. u.ids.push_back(gi_probe->texture);
  1150. copy_uniforms.push_back(u);
  1151. }
  1152. if (gi_probe_use_anisotropy) {
  1153. {
  1154. RD::Uniform u;
  1155. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1156. u.binding = 7;
  1157. u.ids.push_back(gi_probe->anisotropy[0]);
  1158. copy_uniforms.push_back(u);
  1159. }
  1160. {
  1161. RD::Uniform u;
  1162. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1163. u.binding = 8;
  1164. u.ids.push_back(gi_probe->anisotropy[1]);
  1165. copy_uniforms.push_back(u);
  1166. }
  1167. }
  1168. mipmap.second_bounce_uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE], 0);
  1169. } else {
  1170. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP], 0);
  1171. }
  1172. }
  1173. {
  1174. RD::Uniform u;
  1175. u.type = RD::UNIFORM_TYPE_IMAGE;
  1176. u.binding = 5;
  1177. u.ids.push_back(mipmap.texture);
  1178. uniforms.push_back(u);
  1179. }
  1180. if (gi_probe_use_anisotropy) {
  1181. {
  1182. RD::Uniform u;
  1183. u.type = RD::UNIFORM_TYPE_IMAGE;
  1184. u.binding = 6;
  1185. u.ids.push_back(mipmap.anisotropy[0]);
  1186. uniforms.push_back(u);
  1187. }
  1188. {
  1189. RD::Uniform u;
  1190. u.type = RD::UNIFORM_TYPE_IMAGE;
  1191. u.binding = 7;
  1192. u.ids.push_back(mipmap.anisotropy[1]);
  1193. uniforms.push_back(u);
  1194. }
  1195. }
  1196. mipmap.write_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE], 0);
  1197. gi_probe->mipmaps.push_back(mipmap);
  1198. }
  1199. {
  1200. uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  1201. uint32_t oversample = nearest_power_of_2_templated(4);
  1202. int mipmap_index = 0;
  1203. while (mipmap_index < gi_probe->mipmaps.size()) {
  1204. GIProbeInstance::DynamicMap dmap;
  1205. if (oversample > 0) {
  1206. dmap.size = dynamic_map_size * (1 << oversample);
  1207. dmap.mipmap = -1;
  1208. oversample--;
  1209. } else {
  1210. dmap.size = dynamic_map_size >> mipmap_index;
  1211. dmap.mipmap = mipmap_index;
  1212. mipmap_index++;
  1213. }
  1214. RD::TextureFormat dtf;
  1215. dtf.width = dmap.size;
  1216. dtf.height = dmap.size;
  1217. dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1218. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  1219. if (gi_probe->dynamic_maps.size() == 0) {
  1220. dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1221. }
  1222. dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1223. if (gi_probe->dynamic_maps.size() == 0) {
  1224. //render depth for first one
  1225. dtf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  1226. dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1227. dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1228. }
  1229. //just use depth as-is
  1230. dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
  1231. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1232. dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1233. if (gi_probe->dynamic_maps.size() == 0) {
  1234. dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  1235. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1236. dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1237. dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1238. dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  1239. Vector<RID> fb;
  1240. fb.push_back(dmap.albedo);
  1241. fb.push_back(dmap.normal);
  1242. fb.push_back(dmap.orm);
  1243. fb.push_back(dmap.texture); //emission
  1244. fb.push_back(dmap.depth);
  1245. fb.push_back(dmap.fb_depth);
  1246. dmap.fb = RD::get_singleton()->framebuffer_create(fb);
  1247. {
  1248. Vector<RD::Uniform> uniforms;
  1249. {
  1250. RD::Uniform u;
  1251. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1252. u.binding = 3;
  1253. u.ids.push_back(gi_probe_lights_uniform);
  1254. uniforms.push_back(u);
  1255. }
  1256. {
  1257. RD::Uniform u;
  1258. u.type = RD::UNIFORM_TYPE_IMAGE;
  1259. u.binding = 5;
  1260. u.ids.push_back(dmap.albedo);
  1261. uniforms.push_back(u);
  1262. }
  1263. {
  1264. RD::Uniform u;
  1265. u.type = RD::UNIFORM_TYPE_IMAGE;
  1266. u.binding = 6;
  1267. u.ids.push_back(dmap.normal);
  1268. uniforms.push_back(u);
  1269. }
  1270. {
  1271. RD::Uniform u;
  1272. u.type = RD::UNIFORM_TYPE_IMAGE;
  1273. u.binding = 7;
  1274. u.ids.push_back(dmap.orm);
  1275. uniforms.push_back(u);
  1276. }
  1277. {
  1278. RD::Uniform u;
  1279. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1280. u.binding = 8;
  1281. u.ids.push_back(dmap.fb_depth);
  1282. uniforms.push_back(u);
  1283. }
  1284. {
  1285. RD::Uniform u;
  1286. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1287. u.binding = 9;
  1288. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  1289. uniforms.push_back(u);
  1290. }
  1291. {
  1292. RD::Uniform u;
  1293. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1294. u.binding = 10;
  1295. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1296. uniforms.push_back(u);
  1297. }
  1298. {
  1299. RD::Uniform u;
  1300. u.type = RD::UNIFORM_TYPE_IMAGE;
  1301. u.binding = 11;
  1302. u.ids.push_back(dmap.texture);
  1303. uniforms.push_back(u);
  1304. }
  1305. {
  1306. RD::Uniform u;
  1307. u.type = RD::UNIFORM_TYPE_IMAGE;
  1308. u.binding = 12;
  1309. u.ids.push_back(dmap.depth);
  1310. uniforms.push_back(u);
  1311. }
  1312. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING], 0);
  1313. }
  1314. } else {
  1315. bool plot = dmap.mipmap >= 0;
  1316. bool write = dmap.mipmap < (gi_probe->mipmaps.size() - 1);
  1317. Vector<RD::Uniform> uniforms;
  1318. {
  1319. RD::Uniform u;
  1320. u.type = RD::UNIFORM_TYPE_IMAGE;
  1321. u.binding = 5;
  1322. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].texture);
  1323. uniforms.push_back(u);
  1324. }
  1325. {
  1326. RD::Uniform u;
  1327. u.type = RD::UNIFORM_TYPE_IMAGE;
  1328. u.binding = 6;
  1329. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].depth);
  1330. uniforms.push_back(u);
  1331. }
  1332. if (write) {
  1333. {
  1334. RD::Uniform u;
  1335. u.type = RD::UNIFORM_TYPE_IMAGE;
  1336. u.binding = 7;
  1337. u.ids.push_back(dmap.texture);
  1338. uniforms.push_back(u);
  1339. }
  1340. {
  1341. RD::Uniform u;
  1342. u.type = RD::UNIFORM_TYPE_IMAGE;
  1343. u.binding = 8;
  1344. u.ids.push_back(dmap.depth);
  1345. uniforms.push_back(u);
  1346. }
  1347. }
  1348. {
  1349. RD::Uniform u;
  1350. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1351. u.binding = 9;
  1352. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  1353. uniforms.push_back(u);
  1354. }
  1355. {
  1356. RD::Uniform u;
  1357. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1358. u.binding = 10;
  1359. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1360. uniforms.push_back(u);
  1361. }
  1362. if (plot) {
  1363. {
  1364. RD::Uniform u;
  1365. u.type = RD::UNIFORM_TYPE_IMAGE;
  1366. u.binding = 11;
  1367. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].texture);
  1368. uniforms.push_back(u);
  1369. }
  1370. if (gi_probe_is_anisotropic()) {
  1371. {
  1372. RD::Uniform u;
  1373. u.type = RD::UNIFORM_TYPE_IMAGE;
  1374. u.binding = 12;
  1375. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].anisotropy[0]);
  1376. uniforms.push_back(u);
  1377. }
  1378. {
  1379. RD::Uniform u;
  1380. u.type = RD::UNIFORM_TYPE_IMAGE;
  1381. u.binding = 13;
  1382. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].anisotropy[1]);
  1383. uniforms.push_back(u);
  1384. }
  1385. }
  1386. }
  1387. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[(write && plot) ? GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT : write ? GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE : GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT], 0);
  1388. }
  1389. gi_probe->dynamic_maps.push_back(dmap);
  1390. }
  1391. }
  1392. }
  1393. gi_probe->last_probe_data_version = data_version;
  1394. gi_probe_slots_dirty = true;
  1395. p_update_light_instances = true; //just in case
  1396. }
  1397. // UDPDATE TIME
  1398. if (gi_probe->has_dynamic_object_data) {
  1399. //if it has dynamic object data, it needs to be cleared
  1400. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  1401. if (gi_probe_is_anisotropic()) {
  1402. RD::get_singleton()->texture_clear(gi_probe->anisotropy[0], Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  1403. RD::get_singleton()->texture_clear(gi_probe->anisotropy[1], Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  1404. }
  1405. }
  1406. uint32_t light_count = 0;
  1407. if (p_update_light_instances || p_dynamic_object_count > 0) {
  1408. light_count = MIN(gi_probe_max_lights, (uint32_t)p_light_instances.size());
  1409. {
  1410. Transform to_cell = storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  1411. Transform to_probe_xform = (gi_probe->transform * to_cell.affine_inverse()).affine_inverse();
  1412. //update lights
  1413. for (uint32_t i = 0; i < light_count; i++) {
  1414. GIProbeLight &l = gi_probe_lights[i];
  1415. RID light_instance = p_light_instances[i];
  1416. RID light = light_instance_get_base_light(light_instance);
  1417. l.type = storage->light_get_type(light);
  1418. l.attenuation = storage->light_get_param(light, VS::LIGHT_PARAM_ATTENUATION);
  1419. l.energy = storage->light_get_param(light, VS::LIGHT_PARAM_ENERGY) * storage->light_get_param(light, VS::LIGHT_PARAM_INDIRECT_ENERGY);
  1420. l.radius = to_cell.basis.xform(Vector3(storage->light_get_param(light, VS::LIGHT_PARAM_RANGE), 0, 0)).length();
  1421. Color color = storage->light_get_color(light).to_linear();
  1422. l.color[0] = color.r;
  1423. l.color[1] = color.g;
  1424. l.color[2] = color.b;
  1425. l.spot_angle_radians = Math::deg2rad(storage->light_get_param(light, VS::LIGHT_PARAM_SPOT_ANGLE));
  1426. l.spot_attenuation = storage->light_get_param(light, VS::LIGHT_PARAM_SPOT_ATTENUATION);
  1427. Transform xform = light_instance_get_base_transform(light_instance);
  1428. Vector3 pos = to_probe_xform.xform(xform.origin);
  1429. Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_axis(2)).normalized();
  1430. l.position[0] = pos.x;
  1431. l.position[1] = pos.y;
  1432. l.position[2] = pos.z;
  1433. l.direction[0] = dir.x;
  1434. l.direction[1] = dir.y;
  1435. l.direction[2] = dir.z;
  1436. l.has_shadow = storage->light_has_shadow(light);
  1437. }
  1438. RD::get_singleton()->buffer_update(gi_probe_lights_uniform, 0, sizeof(GIProbeLight) * light_count, gi_probe_lights, true);
  1439. }
  1440. }
  1441. if (gi_probe->has_dynamic_object_data || p_update_light_instances || p_dynamic_object_count) {
  1442. // PROCESS MIPMAPS
  1443. if (gi_probe->mipmaps.size()) {
  1444. //can update mipmaps
  1445. Vector3i probe_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1446. GIProbePushConstant push_constant;
  1447. push_constant.limits[0] = probe_size.x;
  1448. push_constant.limits[1] = probe_size.y;
  1449. push_constant.limits[2] = probe_size.z;
  1450. push_constant.stack_size = gi_probe->mipmaps.size();
  1451. push_constant.emission_scale = 1.0;
  1452. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  1453. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  1454. push_constant.light_count = light_count;
  1455. push_constant.aniso_strength = storage->gi_probe_get_anisotropy_strength(gi_probe->probe);
  1456. /* print_line("probe update to version " + itos(gi_probe->last_probe_version));
  1457. print_line("propagation " + rtos(push_constant.propagation));
  1458. print_line("dynrange " + rtos(push_constant.dynamic_range));
  1459. */
  1460. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1461. int passes;
  1462. if (p_update_light_instances) {
  1463. passes = storage->gi_probe_is_using_two_bounces(gi_probe->probe) ? 2 : 1;
  1464. } else {
  1465. passes = 1; //only re-blitting is necessary
  1466. }
  1467. int wg_size = 64;
  1468. int wg_limit_x = RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  1469. for (int pass = 0; pass < passes; pass++) {
  1470. if (p_update_light_instances) {
  1471. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  1472. if (i == 0) {
  1473. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[pass == 0 ? GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT : GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE]);
  1474. } else if (i == 1) {
  1475. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP]);
  1476. }
  1477. if (pass == 1 || i > 0) {
  1478. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  1479. }
  1480. if (pass == 0 || i > 0) {
  1481. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].uniform_set, 0);
  1482. } else {
  1483. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].second_bounce_uniform_set, 0);
  1484. }
  1485. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  1486. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  1487. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  1488. while (wg_todo) {
  1489. int wg_count = MIN(wg_todo, wg_limit_x);
  1490. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  1491. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  1492. wg_todo -= wg_count;
  1493. push_constant.cell_offset += wg_count * wg_size;
  1494. }
  1495. }
  1496. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  1497. }
  1498. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE]);
  1499. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  1500. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].write_uniform_set, 0);
  1501. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  1502. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  1503. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  1504. while (wg_todo) {
  1505. int wg_count = MIN(wg_todo, wg_limit_x);
  1506. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  1507. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  1508. wg_todo -= wg_count;
  1509. push_constant.cell_offset += wg_count * wg_size;
  1510. }
  1511. }
  1512. }
  1513. RD::get_singleton()->compute_list_end();
  1514. }
  1515. }
  1516. gi_probe->has_dynamic_object_data = false; //clear until dynamic object data is used again
  1517. if (p_dynamic_object_count && gi_probe->dynamic_maps.size()) {
  1518. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1519. int multiplier = gi_probe->dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  1520. Transform oversample_scale;
  1521. oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
  1522. Transform to_cell = oversample_scale * storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  1523. Transform to_world_xform = gi_probe->transform * to_cell.affine_inverse();
  1524. Transform to_probe_xform = to_world_xform.affine_inverse();
  1525. AABB probe_aabb(Vector3(), octree_size);
  1526. //this could probably be better parallelized in compute..
  1527. for (int i = 0; i < p_dynamic_object_count; i++) {
  1528. InstanceBase *instance = p_dynamic_objects[i];
  1529. //not used, so clear
  1530. instance->depth_layer = 0;
  1531. instance->depth = 0;
  1532. //transform aabb to giprobe
  1533. AABB aabb = (to_probe_xform * instance->transform).xform(instance->aabb);
  1534. //this needs to wrap to grid resolution to avoid jitter
  1535. //also extend margin a bit just in case
  1536. Vector3i begin = aabb.position - Vector3i(1, 1, 1);
  1537. Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
  1538. for (int j = 0; j < 3; j++) {
  1539. if ((end[j] - begin[j]) & 1) {
  1540. end[j]++; //for half extents split, it needs to be even
  1541. }
  1542. begin[j] = MAX(begin[j], 0);
  1543. end[j] = MIN(end[j], octree_size[j] * multiplier);
  1544. }
  1545. //aabb = aabb.intersection(probe_aabb); //intersect
  1546. aabb.position = begin;
  1547. aabb.size = end - begin;
  1548. //print_line("aabb: " + aabb);
  1549. for (int j = 0; j < 6; j++) {
  1550. //if (j != 0 && j != 3) {
  1551. // continue;
  1552. //}
  1553. static const Vector3 render_z[6] = {
  1554. Vector3(1, 0, 0),
  1555. Vector3(0, 1, 0),
  1556. Vector3(0, 0, 1),
  1557. Vector3(-1, 0, 0),
  1558. Vector3(0, -1, 0),
  1559. Vector3(0, 0, -1),
  1560. };
  1561. static const Vector3 render_up[6] = {
  1562. Vector3(0, 1, 0),
  1563. Vector3(0, 0, 1),
  1564. Vector3(0, 1, 0),
  1565. Vector3(0, 1, 0),
  1566. Vector3(0, 0, 1),
  1567. Vector3(0, 1, 0),
  1568. };
  1569. Vector3 render_dir = render_z[j];
  1570. Vector3 up_dir = render_up[j];
  1571. Vector3 center = aabb.position + aabb.size * 0.5;
  1572. Transform xform;
  1573. xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
  1574. Vector3 x_dir = xform.basis.get_axis(0).abs();
  1575. int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
  1576. Vector3 y_dir = xform.basis.get_axis(1).abs();
  1577. int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
  1578. Vector3 z_dir = -xform.basis.get_axis(2);
  1579. int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
  1580. Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
  1581. bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(0)) < 0);
  1582. bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(1)) < 0);
  1583. bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(2)) > 0);
  1584. CameraMatrix cm;
  1585. cm.set_orthogonal(-rect.size.width / 2, rect.size.width / 2, -rect.size.height / 2, rect.size.height / 2, 0.0001, aabb.size[z_axis]);
  1586. _render_material(to_world_xform * xform, cm, true, &instance, 1, gi_probe->dynamic_maps[0].fb, Rect2i(Vector2i(), rect.size));
  1587. GIProbeDynamicPushConstant push_constant;
  1588. zeromem(&push_constant, sizeof(GIProbeDynamicPushConstant));
  1589. push_constant.limits[0] = octree_size.x;
  1590. push_constant.limits[1] = octree_size.y;
  1591. push_constant.limits[2] = octree_size.z;
  1592. push_constant.light_count = p_light_instances.size();
  1593. push_constant.x_dir[0] = x_dir[0];
  1594. push_constant.x_dir[1] = x_dir[1];
  1595. push_constant.x_dir[2] = x_dir[2];
  1596. push_constant.y_dir[0] = y_dir[0];
  1597. push_constant.y_dir[1] = y_dir[1];
  1598. push_constant.y_dir[2] = y_dir[2];
  1599. push_constant.z_dir[0] = z_dir[0];
  1600. push_constant.z_dir[1] = z_dir[1];
  1601. push_constant.z_dir[2] = z_dir[2];
  1602. push_constant.z_base = xform.origin[z_axis];
  1603. push_constant.z_sign = (z_flip ? -1.0 : 1.0);
  1604. push_constant.pos_multiplier = float(1.0) / multiplier;
  1605. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  1606. push_constant.flip_x = x_flip;
  1607. push_constant.flip_y = y_flip;
  1608. push_constant.rect_pos[0] = rect.position[0];
  1609. push_constant.rect_pos[1] = rect.position[1];
  1610. push_constant.rect_size[0] = rect.size[0];
  1611. push_constant.rect_size[1] = rect.size[1];
  1612. push_constant.prev_rect_ofs[0] = 0;
  1613. push_constant.prev_rect_ofs[1] = 0;
  1614. push_constant.prev_rect_size[0] = 0;
  1615. push_constant.prev_rect_size[1] = 0;
  1616. push_constant.on_mipmap = false;
  1617. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  1618. push_constant.pad[0] = 0;
  1619. push_constant.pad[1] = 0;
  1620. push_constant.pad[2] = 0;
  1621. //process lighting
  1622. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1623. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING]);
  1624. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[0].uniform_set, 0);
  1625. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  1626. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  1627. //print_line("rect: " + itos(i) + ": " + rect);
  1628. for (int k = 1; k < gi_probe->dynamic_maps.size(); k++) {
  1629. // enlarge the rect if needed so all pixels fit when downscaled,
  1630. // this ensures downsampling is smooth and optimal because no pixels are left behind
  1631. //x
  1632. if (rect.position.x & 1) {
  1633. rect.size.x++;
  1634. push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
  1635. } else {
  1636. push_constant.prev_rect_ofs[0] = 0;
  1637. }
  1638. if (rect.size.x & 1) {
  1639. rect.size.x++;
  1640. }
  1641. rect.position.x >>= 1;
  1642. rect.size.x = MAX(1, rect.size.x >> 1);
  1643. //y
  1644. if (rect.position.y & 1) {
  1645. rect.size.y++;
  1646. push_constant.prev_rect_ofs[1] = 1;
  1647. } else {
  1648. push_constant.prev_rect_ofs[1] = 0;
  1649. }
  1650. if (rect.size.y & 1) {
  1651. rect.size.y++;
  1652. }
  1653. rect.position.y >>= 1;
  1654. rect.size.y = MAX(1, rect.size.y >> 1);
  1655. //shrink limits to ensure plot does not go outside map
  1656. if (gi_probe->dynamic_maps[k].mipmap > 0) {
  1657. for (int l = 0; l < 3; l++) {
  1658. push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
  1659. }
  1660. }
  1661. //print_line("rect: " + itos(i) + ": " + rect);
  1662. push_constant.rect_pos[0] = rect.position[0];
  1663. push_constant.rect_pos[1] = rect.position[1];
  1664. push_constant.prev_rect_size[0] = push_constant.rect_size[0];
  1665. push_constant.prev_rect_size[1] = push_constant.rect_size[1];
  1666. push_constant.rect_size[0] = rect.size[0];
  1667. push_constant.rect_size[1] = rect.size[1];
  1668. push_constant.on_mipmap = gi_probe->dynamic_maps[k].mipmap > 0;
  1669. RD::get_singleton()->compute_list_add_barrier(compute_list);
  1670. if (gi_probe->dynamic_maps[k].mipmap < 0) {
  1671. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE]);
  1672. } else if (k < gi_probe->dynamic_maps.size() - 1) {
  1673. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT]);
  1674. } else {
  1675. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT]);
  1676. }
  1677. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[k].uniform_set, 0);
  1678. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  1679. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  1680. }
  1681. RD::get_singleton()->compute_list_end();
  1682. }
  1683. }
  1684. gi_probe->has_dynamic_object_data = true; //clear until dynamic object data is used again
  1685. }
  1686. gi_probe->last_probe_version = storage->gi_probe_get_version(gi_probe->probe);
  1687. }
  1688. void RasterizerSceneRD::_debug_giprobe(RID p_gi_probe, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
  1689. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_gi_probe);
  1690. ERR_FAIL_COND(!gi_probe);
  1691. if (gi_probe->mipmaps.size() == 0) {
  1692. return;
  1693. }
  1694. CameraMatrix transform = (p_camera_with_transform * CameraMatrix(gi_probe->transform)) * CameraMatrix(storage->gi_probe_get_to_cell_xform(gi_probe->probe).affine_inverse());
  1695. int level = 0;
  1696. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  1697. GIProbeDebugPushConstant push_constant;
  1698. push_constant.alpha = p_alpha;
  1699. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  1700. push_constant.cell_offset = gi_probe->mipmaps[level].cell_offset;
  1701. push_constant.level = level;
  1702. push_constant.bounds[0] = octree_size.x >> level;
  1703. push_constant.bounds[1] = octree_size.y >> level;
  1704. push_constant.bounds[2] = octree_size.z >> level;
  1705. push_constant.pad = 0;
  1706. for (int i = 0; i < 4; i++) {
  1707. for (int j = 0; j < 4; j++) {
  1708. push_constant.projection[i * 4 + j] = transform.matrix[i][j];
  1709. }
  1710. }
  1711. if (giprobe_debug_uniform_set.is_valid()) {
  1712. RD::get_singleton()->free(giprobe_debug_uniform_set);
  1713. }
  1714. Vector<RD::Uniform> uniforms;
  1715. {
  1716. RD::Uniform u;
  1717. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1718. u.binding = 1;
  1719. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  1720. uniforms.push_back(u);
  1721. }
  1722. {
  1723. RD::Uniform u;
  1724. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1725. u.binding = 2;
  1726. u.ids.push_back(gi_probe->texture);
  1727. uniforms.push_back(u);
  1728. }
  1729. {
  1730. RD::Uniform u;
  1731. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1732. u.binding = 3;
  1733. u.ids.push_back(storage->sampler_rd_get_default(VS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, VS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1734. uniforms.push_back(u);
  1735. }
  1736. if (gi_probe_use_anisotropy) {
  1737. {
  1738. RD::Uniform u;
  1739. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1740. u.binding = 4;
  1741. u.ids.push_back(gi_probe->anisotropy[0]);
  1742. uniforms.push_back(u);
  1743. }
  1744. {
  1745. RD::Uniform u;
  1746. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1747. u.binding = 5;
  1748. u.ids.push_back(gi_probe->anisotropy[1]);
  1749. uniforms.push_back(u);
  1750. }
  1751. }
  1752. int cell_count;
  1753. if (!p_emission && p_lighting && gi_probe->has_dynamic_object_data) {
  1754. cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
  1755. } else {
  1756. cell_count = gi_probe->mipmaps[level].cell_count;
  1757. }
  1758. giprobe_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_debug_shader_version_shaders[0], 0);
  1759. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, giprobe_debug_shader_version_pipelines[p_emission ? GI_PROBE_DEBUG_EMISSION : p_lighting ? (gi_probe->has_dynamic_object_data ? GI_PROBE_DEBUG_LIGHT_FULL : GI_PROBE_DEBUG_LIGHT) : GI_PROBE_DEBUG_COLOR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  1760. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, giprobe_debug_uniform_set, 0);
  1761. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(GIProbeDebugPushConstant));
  1762. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
  1763. }
  1764. const Vector<RID> &RasterizerSceneRD::gi_probe_get_slots() const {
  1765. return gi_probe_slots;
  1766. }
  1767. bool RasterizerSceneRD::gi_probe_slots_are_dirty() const {
  1768. return gi_probe_slots_dirty;
  1769. }
  1770. void RasterizerSceneRD::gi_probe_slots_make_not_dirty() {
  1771. gi_probe_slots_dirty = false;
  1772. }
  1773. RasterizerSceneRD::GIProbeQuality RasterizerSceneRD::gi_probe_get_quality() const {
  1774. return gi_probe_quality;
  1775. }
  1776. ////////////////////////////////
  1777. RID RasterizerSceneRD::render_buffers_create() {
  1778. RenderBuffers rb;
  1779. rb.data = _create_render_buffer_data();
  1780. return render_buffers_owner.make_rid(rb);
  1781. }
  1782. void RasterizerSceneRD::_allocate_blur_textures(RenderBuffers *rb) {
  1783. ERR_FAIL_COND(!rb->blur[0].texture.is_null());
  1784. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rb->width, rb->height, Image::FORMAT_RGBAH);
  1785. RD::TextureFormat tf;
  1786. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1787. tf.width = rb->width;
  1788. tf.height = rb->height;
  1789. tf.type = RD::TEXTURE_TYPE_2D;
  1790. 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;
  1791. tf.mipmaps = mipmaps_required;
  1792. rb->blur[0].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1793. //the second one is smaller (only used for separatable part of blur)
  1794. tf.width >>= 1;
  1795. tf.height >>= 1;
  1796. tf.mipmaps--;
  1797. rb->blur[1].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1798. int base_width = rb->width;
  1799. int base_height = rb->height;
  1800. for (uint32_t i = 0; i < mipmaps_required; i++) {
  1801. RenderBuffers::Blur::Mipmap mm;
  1802. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[0].texture, 0, i);
  1803. {
  1804. Vector<RID> fbs;
  1805. fbs.push_back(mm.texture);
  1806. mm.framebuffer = RD::get_singleton()->framebuffer_create(fbs);
  1807. }
  1808. mm.width = base_width;
  1809. mm.height = base_height;
  1810. rb->blur[0].mipmaps.push_back(mm);
  1811. if (i > 0) {
  1812. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[1].texture, 0, i - 1);
  1813. {
  1814. Vector<RID> fbs;
  1815. fbs.push_back(mm.texture);
  1816. mm.framebuffer = RD::get_singleton()->framebuffer_create(fbs);
  1817. }
  1818. rb->blur[1].mipmaps.push_back(mm);
  1819. }
  1820. base_width = MAX(1, base_width >> 1);
  1821. base_height = MAX(1, base_height >> 1);
  1822. }
  1823. }
  1824. void RasterizerSceneRD::_allocate_luminance_textures(RenderBuffers *rb) {
  1825. ERR_FAIL_COND(!rb->luminance.current.is_null());
  1826. int w = rb->width;
  1827. int h = rb->height;
  1828. while (true) {
  1829. w = MAX(w / 8, 1);
  1830. h = MAX(h / 8, 1);
  1831. RD::TextureFormat tf;
  1832. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  1833. tf.width = w;
  1834. tf.height = h;
  1835. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  1836. bool final = w == 1 && h == 1;
  1837. if (final) {
  1838. tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT;
  1839. }
  1840. RID texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1841. rb->luminance.reduce.push_back(texture);
  1842. if (final) {
  1843. rb->luminance.current = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1844. break;
  1845. }
  1846. }
  1847. }
  1848. void RasterizerSceneRD::_free_render_buffer_data(RenderBuffers *rb) {
  1849. if (rb->texture.is_valid()) {
  1850. RD::get_singleton()->free(rb->texture);
  1851. rb->texture = RID();
  1852. }
  1853. if (rb->depth_texture.is_valid()) {
  1854. RD::get_singleton()->free(rb->depth_texture);
  1855. rb->depth_texture = RID();
  1856. }
  1857. for (int i = 0; i < 2; i++) {
  1858. if (rb->blur[i].texture.is_valid()) {
  1859. RD::get_singleton()->free(rb->blur[i].texture);
  1860. rb->blur[i].texture = RID();
  1861. rb->blur[i].mipmaps.clear();
  1862. }
  1863. }
  1864. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  1865. RD::get_singleton()->free(rb->luminance.reduce[i]);
  1866. }
  1867. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  1868. RD::get_singleton()->free(rb->luminance.reduce[i]);
  1869. }
  1870. rb->luminance.reduce.clear();
  1871. if (rb->luminance.current.is_valid()) {
  1872. RD::get_singleton()->free(rb->luminance.current);
  1873. rb->luminance.current = RID();
  1874. }
  1875. }
  1876. void RasterizerSceneRD::_render_buffers_post_process_and_tonemap(RID p_render_buffers, RID p_environment, RID p_camera_effects, const CameraMatrix &p_projection) {
  1877. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1878. ERR_FAIL_COND(!rb);
  1879. Environent *env = environment_owner.getornull(p_environment);
  1880. //glow (if enabled)
  1881. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  1882. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  1883. if (can_use_effects && camfx && (camfx->dof_blur_near_enabled || camfx->dof_blur_far_enabled) && camfx->dof_blur_amount > 0.0) {
  1884. if (rb->blur[0].texture.is_null()) {
  1885. _allocate_blur_textures(rb);
  1886. }
  1887. float bokeh_size = camfx->dof_blur_amount * 20.0;
  1888. storage->get_effects()->bokeh_dof(rb->texture, rb->depth_texture, Size2i(rb->width, rb->height), rb->blur[1].mipmaps[0].texture, camfx->dof_blur_far_enabled, camfx->dof_blur_far_distance, camfx->dof_blur_far_transition, camfx->dof_blur_near_enabled, camfx->dof_blur_near_distance, camfx->dof_blur_near_transition, bokeh_size, camfx->dof_blur_quality, p_projection.get_z_near(), p_projection.get_z_far(), p_projection.is_orthogonal());
  1889. }
  1890. if (can_use_effects && env && env->auto_exposure) {
  1891. if (rb->luminance.current.is_null()) {
  1892. _allocate_luminance_textures(rb);
  1893. }
  1894. bool set_immediate = env->auto_exposure_version != rb->auto_exposure_version;
  1895. rb->auto_exposure_version = env->auto_exposure_version;
  1896. double step = env->auto_exp_speed * time_step;
  1897. storage->get_effects()->luminance_reduction(rb->texture, Size2i(rb->width, rb->height), rb->luminance.reduce, rb->luminance.current, env->min_luminance, env->max_luminance, step, set_immediate);
  1898. //swap final reduce with prev luminance
  1899. SWAP(rb->luminance.current, rb->luminance.reduce.write[rb->luminance.reduce.size() - 1]);
  1900. VisualServerRaster::redraw_request(); //redraw all the time if auto exposure rendering is on
  1901. }
  1902. int max_glow_level = -1;
  1903. int glow_mask = 0;
  1904. if (can_use_effects && env && env->glow_enabled) {
  1905. /* see that blur textures are allocated */
  1906. if (rb->blur[0].texture.is_null()) {
  1907. _allocate_blur_textures(rb);
  1908. }
  1909. for (int i = 0; i < VS::MAX_GLOW_LEVELS; i++) {
  1910. if (env->glow_levels & (1 << i)) {
  1911. if (i >= rb->blur[1].mipmaps.size()) {
  1912. max_glow_level = rb->blur[1].mipmaps.size() - 1;
  1913. glow_mask |= 1 << max_glow_level;
  1914. } else {
  1915. max_glow_level = i;
  1916. glow_mask |= (1 << i);
  1917. }
  1918. }
  1919. }
  1920. for (int i = 0; i < (max_glow_level + 1); i++) {
  1921. int vp_w = rb->blur[1].mipmaps[i].width;
  1922. int vp_h = rb->blur[1].mipmaps[i].height;
  1923. if (i == 0) {
  1924. RID luminance_texture;
  1925. if (env->auto_exposure && rb->luminance.current.is_valid()) {
  1926. luminance_texture = rb->luminance.current;
  1927. }
  1928. storage->get_effects()->gaussian_glow(rb->texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength, true, env->glow_hdr_luminance_cap, env->exposure, env->glow_bloom, env->glow_hdr_bleed_threshold, env->glow_hdr_bleed_scale, luminance_texture, env->auto_exp_scale);
  1929. } else {
  1930. storage->get_effects()->gaussian_glow(rb->blur[1].mipmaps[i - 1].texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength);
  1931. }
  1932. }
  1933. }
  1934. {
  1935. //tonemap
  1936. RasterizerEffectsRD::TonemapSettings tonemap;
  1937. tonemap.color_correction_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  1938. if (can_use_effects && env && env->auto_exposure && rb->luminance.current.is_valid()) {
  1939. tonemap.use_auto_exposure = true;
  1940. tonemap.exposure_texture = rb->luminance.current;
  1941. tonemap.auto_exposure_grey = env->auto_exp_scale;
  1942. } else {
  1943. tonemap.exposure_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE);
  1944. }
  1945. if (can_use_effects && env && env->glow_enabled) {
  1946. tonemap.use_glow = true;
  1947. tonemap.glow_mode = RasterizerEffectsRD::TonemapSettings::GlowMode(env->glow_blend_mode);
  1948. tonemap.glow_intensity = env->glow_blend_mode == VS::GLOW_BLEND_MODE_MIX ? env->glow_mix : env->glow_intensity;
  1949. tonemap.glow_level_flags = glow_mask;
  1950. tonemap.glow_texture_size.x = rb->blur[1].mipmaps[0].width;
  1951. tonemap.glow_texture_size.y = rb->blur[1].mipmaps[0].height;
  1952. tonemap.glow_use_bicubic_upscale = env->glow_bicubic_upscale;
  1953. tonemap.glow_texture = rb->blur[1].texture;
  1954. } else {
  1955. tonemap.glow_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_BLACK);
  1956. }
  1957. if (env) {
  1958. tonemap.tonemap_mode = env->tone_mapper;
  1959. tonemap.white = env->white;
  1960. tonemap.exposure = env->exposure;
  1961. }
  1962. storage->get_effects()->tonemapper(rb->texture, storage->render_target_get_rd_framebuffer(rb->render_target), tonemap);
  1963. }
  1964. storage->render_target_disable_clear_request(rb->render_target);
  1965. }
  1966. void RasterizerSceneRD::_render_buffers_debug_draw(RID p_render_buffers, RID p_shadow_atlas) {
  1967. RasterizerEffectsRD *effects = storage->get_effects();
  1968. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1969. ERR_FAIL_COND(!rb);
  1970. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS) {
  1971. if (p_shadow_atlas.is_valid()) {
  1972. RID shadow_atlas_texture = shadow_atlas_get_texture(p_shadow_atlas);
  1973. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  1974. effects->copy_to_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 2));
  1975. }
  1976. }
  1977. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) {
  1978. if (directional_shadow_get_texture().is_valid()) {
  1979. RID shadow_atlas_texture = directional_shadow_get_texture();
  1980. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  1981. effects->copy_to_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 2));
  1982. }
  1983. }
  1984. if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE) {
  1985. if (rb->luminance.current.is_valid()) {
  1986. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  1987. effects->copy_to_rect(rb->luminance.current, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 8));
  1988. }
  1989. }
  1990. }
  1991. RID RasterizerSceneRD::render_buffers_get_back_buffer_texture(RID p_render_buffers) {
  1992. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1993. ERR_FAIL_COND_V(!rb, RID());
  1994. ERR_FAIL_COND_V(!rb->blur[0].texture.is_valid(), RID()); //should have been created for some reason?
  1995. return rb->blur[0].texture;
  1996. }
  1997. void RasterizerSceneRD::render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, VS::ViewportMSAA p_msaa) {
  1998. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1999. rb->width = p_width;
  2000. rb->height = p_height;
  2001. rb->render_target = p_render_target;
  2002. rb->msaa = p_msaa;
  2003. _free_render_buffer_data(rb);
  2004. {
  2005. RD::TextureFormat tf;
  2006. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2007. tf.width = rb->width;
  2008. tf.height = rb->height;
  2009. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  2010. rb->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2011. }
  2012. {
  2013. RD::TextureFormat tf;
  2014. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D24_UNORM_S8_UINT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D24_UNORM_S8_UINT : RD::DATA_FORMAT_D32_SFLOAT_S8_UINT;
  2015. tf.width = p_width;
  2016. tf.height = p_height;
  2017. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  2018. rb->depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2019. }
  2020. rb->data->configure(rb->texture, rb->depth_texture, p_width, p_height, p_msaa);
  2021. }
  2022. int RasterizerSceneRD::get_roughness_layers() const {
  2023. return roughness_layers;
  2024. }
  2025. bool RasterizerSceneRD::is_using_radiance_cubemap_array() const {
  2026. return sky_use_cubemap_array;
  2027. }
  2028. RasterizerSceneRD::RenderBufferData *RasterizerSceneRD::render_buffers_get_data(RID p_render_buffers) {
  2029. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2030. ERR_FAIL_COND_V(!rb, NULL);
  2031. return rb->data;
  2032. }
  2033. void RasterizerSceneRD::render_scene(RID p_render_buffers, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, RID p_environment, RID p_camera_effects, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  2034. Color clear_color;
  2035. if (p_render_buffers.is_valid()) {
  2036. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  2037. ERR_FAIL_COND(!rb);
  2038. clear_color = storage->render_target_get_clear_request_color(rb->render_target);
  2039. } else {
  2040. clear_color = storage->get_default_clear_color();
  2041. }
  2042. _render_scene(p_render_buffers, p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_light_cull_result, p_light_cull_count, p_reflection_probe_cull_result, p_reflection_probe_cull_count, p_gi_probe_cull_result, p_gi_probe_cull_count, p_environment, p_camera_effects, p_shadow_atlas, p_reflection_atlas, p_reflection_probe, p_reflection_probe_pass, clear_color);
  2043. if (p_render_buffers.is_valid()) {
  2044. RENDER_TIMESTAMP("Tonemap");
  2045. _render_buffers_post_process_and_tonemap(p_render_buffers, p_environment, p_camera_effects, p_cam_projection);
  2046. _render_buffers_debug_draw(p_render_buffers, p_shadow_atlas);
  2047. }
  2048. }
  2049. void RasterizerSceneRD::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) {
  2050. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  2051. ERR_FAIL_COND(!light_instance);
  2052. Rect2i atlas_rect;
  2053. RID atlas_fb;
  2054. int atlas_fb_size;
  2055. bool using_dual_paraboloid = false;
  2056. bool using_dual_paraboloid_flip = false;
  2057. float zfar = 0;
  2058. RID render_fb;
  2059. RID render_texture;
  2060. float bias = 0;
  2061. float normal_bias = 0;
  2062. bool render_cubemap = false;
  2063. bool finalize_cubemap = false;
  2064. CameraMatrix light_projection;
  2065. Transform light_transform;
  2066. if (storage->light_get_type(light_instance->light) == VS::LIGHT_DIRECTIONAL) {
  2067. //set pssm stuff
  2068. if (light_instance->last_scene_shadow_pass != scene_pass) {
  2069. light_instance->directional_rect = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  2070. directional_shadow.current_light++;
  2071. light_instance->last_scene_shadow_pass = scene_pass;
  2072. }
  2073. light_projection = light_instance->shadow_transform[p_pass].camera;
  2074. light_transform = light_instance->shadow_transform[p_pass].transform;
  2075. atlas_rect.position.x = light_instance->directional_rect.position.x;
  2076. atlas_rect.position.y = light_instance->directional_rect.position.y;
  2077. atlas_rect.size.width = light_instance->directional_rect.size.x;
  2078. atlas_rect.size.height = light_instance->directional_rect.size.y;
  2079. if (storage->light_directional_get_shadow_mode(light_instance->light) == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  2080. atlas_rect.size.width /= 2;
  2081. atlas_rect.size.height /= 2;
  2082. if (p_pass == 1) {
  2083. atlas_rect.position.x += atlas_rect.size.width;
  2084. } else if (p_pass == 2) {
  2085. atlas_rect.position.y += atlas_rect.size.height;
  2086. } else if (p_pass == 3) {
  2087. atlas_rect.position.x += atlas_rect.size.width;
  2088. atlas_rect.position.y += atlas_rect.size.height;
  2089. }
  2090. } else if (storage->light_directional_get_shadow_mode(light_instance->light) == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  2091. atlas_rect.size.height /= 2;
  2092. if (p_pass == 0) {
  2093. } else {
  2094. atlas_rect.position.y += atlas_rect.size.height;
  2095. }
  2096. }
  2097. light_instance->shadow_transform[p_pass].atlas_rect = atlas_rect;
  2098. light_instance->shadow_transform[p_pass].atlas_rect.position /= directional_shadow.size;
  2099. light_instance->shadow_transform[p_pass].atlas_rect.size /= directional_shadow.size;
  2100. float bias_mult = Math::lerp(1.0f, light_instance->shadow_transform[p_pass].bias_scale, storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE));
  2101. zfar = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_RANGE);
  2102. bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_BIAS) * bias_mult;
  2103. normal_bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * bias_mult;
  2104. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  2105. render_fb = shadow_map->fb;
  2106. render_texture = shadow_map->depth;
  2107. atlas_fb = directional_shadow.fb;
  2108. atlas_fb_size = directional_shadow.size;
  2109. } else {
  2110. //set from shadow atlas
  2111. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2112. ERR_FAIL_COND(!shadow_atlas);
  2113. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  2114. uint32_t key = shadow_atlas->shadow_owners[p_light];
  2115. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2116. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2117. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  2118. uint32_t quadrant_size = shadow_atlas->size >> 1;
  2119. atlas_rect.position.x = (quadrant & 1) * quadrant_size;
  2120. atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
  2121. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2122. atlas_rect.position.x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2123. atlas_rect.position.y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2124. atlas_rect.size.width = shadow_size;
  2125. atlas_rect.size.height = shadow_size;
  2126. atlas_fb = shadow_atlas->fb;
  2127. atlas_fb_size = shadow_atlas->size;
  2128. zfar = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_RANGE);
  2129. bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_BIAS);
  2130. normal_bias = storage->light_get_param(light_instance->light, VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS);
  2131. if (storage->light_get_type(light_instance->light) == VS::LIGHT_OMNI) {
  2132. if (storage->light_omni_get_shadow_mode(light_instance->light) == VS::LIGHT_OMNI_SHADOW_CUBE) {
  2133. ShadowCubemap *cubemap = _get_shadow_cubemap(shadow_size / 2);
  2134. render_fb = cubemap->side_fb[p_pass];
  2135. render_texture = cubemap->cubemap;
  2136. light_projection = light_instance->shadow_transform[0].camera;
  2137. light_transform = light_instance->shadow_transform[0].transform;
  2138. render_cubemap = true;
  2139. finalize_cubemap = p_pass == 5;
  2140. } else {
  2141. light_projection = light_instance->shadow_transform[0].camera;
  2142. light_transform = light_instance->shadow_transform[0].transform;
  2143. atlas_rect.size.height /= 2;
  2144. atlas_rect.position.y += p_pass * atlas_rect.size.height;
  2145. using_dual_paraboloid = true;
  2146. using_dual_paraboloid_flip = p_pass == 1;
  2147. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  2148. render_fb = shadow_map->fb;
  2149. render_texture = shadow_map->depth;
  2150. }
  2151. } else if (storage->light_get_type(light_instance->light) == VS::LIGHT_SPOT) {
  2152. light_projection = light_instance->shadow_transform[0].camera;
  2153. light_transform = light_instance->shadow_transform[0].transform;
  2154. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  2155. render_fb = shadow_map->fb;
  2156. render_texture = shadow_map->depth;
  2157. }
  2158. }
  2159. if (render_cubemap) {
  2160. //rendering to cubemap
  2161. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, 0, 0, false, false);
  2162. if (finalize_cubemap) {
  2163. //reblit
  2164. atlas_rect.size.height /= 2;
  2165. storage->get_effects()->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect, light_projection.get_z_near(), light_projection.get_z_far(), bias, false);
  2166. atlas_rect.position.y += atlas_rect.size.height;
  2167. storage->get_effects()->copy_cubemap_to_dp(render_texture, atlas_fb, atlas_rect, light_projection.get_z_near(), light_projection.get_z_far(), bias, true);
  2168. }
  2169. } else {
  2170. //render shadow
  2171. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, bias, normal_bias, using_dual_paraboloid, using_dual_paraboloid_flip);
  2172. //copy to atlas
  2173. storage->get_effects()->copy_to_rect(render_texture, atlas_fb, atlas_rect, true);
  2174. //does not work from depth to color
  2175. //RD::get_singleton()->texture_copy(render_texture, atlas_texture, Vector3(0, 0, 0), Vector3(atlas_rect.position.x, atlas_rect.position.y, 0), Vector3(atlas_rect.size.x, atlas_rect.size.y, 1), 0, 0, 0, 0, true);
  2176. }
  2177. }
  2178. void RasterizerSceneRD::render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) {
  2179. _render_material(p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_framebuffer, p_region);
  2180. }
  2181. bool RasterizerSceneRD::free(RID p_rid) {
  2182. if (render_buffers_owner.owns(p_rid)) {
  2183. RenderBuffers *rb = render_buffers_owner.getornull(p_rid);
  2184. _free_render_buffer_data(rb);
  2185. memdelete(rb->data);
  2186. render_buffers_owner.free(p_rid);
  2187. } else if (environment_owner.owns(p_rid)) {
  2188. //not much to delete, just free it
  2189. environment_owner.free(p_rid);
  2190. } else if (camera_effects_owner.owns(p_rid)) {
  2191. //not much to delete, just free it
  2192. camera_effects_owner.free(p_rid);
  2193. } else if (reflection_atlas_owner.owns(p_rid)) {
  2194. reflection_atlas_set_size(p_rid, 0, 0);
  2195. reflection_atlas_owner.free(p_rid);
  2196. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  2197. //not much to delete, just free it
  2198. //ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_rid);
  2199. reflection_probe_release_atlas_index(p_rid);
  2200. reflection_probe_instance_owner.free(p_rid);
  2201. } else if (gi_probe_instance_owner.owns(p_rid)) {
  2202. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_rid);
  2203. if (gi_probe->texture.is_valid()) {
  2204. RD::get_singleton()->free(gi_probe->texture);
  2205. RD::get_singleton()->free(gi_probe->write_buffer);
  2206. }
  2207. if (gi_probe->anisotropy[0].is_valid()) {
  2208. RD::get_singleton()->free(gi_probe->anisotropy[0]);
  2209. RD::get_singleton()->free(gi_probe->anisotropy[1]);
  2210. }
  2211. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  2212. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  2213. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  2214. }
  2215. gi_probe_slots.write[gi_probe->slot] = RID();
  2216. gi_probe_instance_owner.free(p_rid);
  2217. } else if (sky_owner.owns(p_rid)) {
  2218. _update_dirty_skys();
  2219. Sky *sky = sky_owner.getornull(p_rid);
  2220. if (sky->radiance.is_valid()) {
  2221. RD::get_singleton()->free(sky->radiance);
  2222. sky->radiance = RID();
  2223. }
  2224. _clear_reflection_data(sky->reflection);
  2225. sky_owner.free(p_rid);
  2226. } else if (light_instance_owner.owns(p_rid)) {
  2227. LightInstance *light_instance = light_instance_owner.getornull(p_rid);
  2228. //remove from shadow atlases..
  2229. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  2230. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(E->get());
  2231. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  2232. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  2233. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2234. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2235. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  2236. shadow_atlas->shadow_owners.erase(p_rid);
  2237. }
  2238. light_instance_owner.free(p_rid);
  2239. } else if (shadow_atlas_owner.owns(p_rid)) {
  2240. shadow_atlas_set_size(p_rid, 0);
  2241. shadow_atlas_owner.free(p_rid);
  2242. } else {
  2243. return false;
  2244. }
  2245. return true;
  2246. }
  2247. void RasterizerSceneRD::set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) {
  2248. debug_draw = p_debug_draw;
  2249. }
  2250. void RasterizerSceneRD::update() {
  2251. _update_dirty_skys();
  2252. }
  2253. void RasterizerSceneRD::set_time(double p_time, double p_step) {
  2254. time_step = p_step;
  2255. }
  2256. RasterizerSceneRD::RasterizerSceneRD(RasterizerStorageRD *p_storage) {
  2257. storage = p_storage;
  2258. roughness_layers = GLOBAL_GET("rendering/quality/reflections/roughness_layers");
  2259. sky_ggx_samples_quality = GLOBAL_GET("rendering/quality/reflections/ggx_samples");
  2260. sky_ggx_samples_realtime = GLOBAL_GET("rendering/quality/reflections/ggx_samples_realtime");
  2261. sky_use_cubemap_array = GLOBAL_GET("rendering/quality/reflections/texture_array_reflections");
  2262. // sky_use_cubemap_array = false;
  2263. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  2264. {
  2265. //kinda complicated to compute the amount of slots, we try to use as many as we can
  2266. gi_probe_max_lights = 32;
  2267. gi_probe_lights = memnew_arr(GIProbeLight, gi_probe_max_lights);
  2268. gi_probe_lights_uniform = RD::get_singleton()->uniform_buffer_create(gi_probe_max_lights * sizeof(GIProbeLight));
  2269. gi_probe_use_anisotropy = GLOBAL_GET("rendering/quality/gi_probes/anisotropic");
  2270. gi_probe_quality = GIProbeQuality(CLAMP(int(GLOBAL_GET("rendering/quality/gi_probes/quality")), 0, 2));
  2271. if (textures_per_stage <= 16) {
  2272. gi_probe_slots.resize(2); //thats all you can get
  2273. gi_probe_use_anisotropy = false;
  2274. } else if (textures_per_stage <= 31) {
  2275. gi_probe_slots.resize(4); //thats all you can get, iOS
  2276. gi_probe_use_anisotropy = false;
  2277. } else if (textures_per_stage <= 128) {
  2278. gi_probe_slots.resize(32); //old intel
  2279. gi_probe_use_anisotropy = false;
  2280. } else if (textures_per_stage <= 256) {
  2281. gi_probe_slots.resize(64); //old intel too
  2282. gi_probe_use_anisotropy = false;
  2283. } else {
  2284. if (gi_probe_use_anisotropy) {
  2285. gi_probe_slots.resize(1024 / 3); //needs 3 textures
  2286. } else {
  2287. gi_probe_slots.resize(1024); //modern intel, nvidia, 8192 or greater
  2288. }
  2289. }
  2290. String defines = "\n#define MAX_LIGHTS " + itos(gi_probe_max_lights) + "\n";
  2291. if (gi_probe_use_anisotropy) {
  2292. defines += "\n#define MODE_ANISOTROPIC\n";
  2293. }
  2294. Vector<String> versions;
  2295. versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
  2296. versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
  2297. versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
  2298. versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
  2299. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
  2300. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  2301. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
  2302. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  2303. giprobe_shader.initialize(versions, defines);
  2304. giprobe_lighting_shader_version = giprobe_shader.version_create();
  2305. for (int i = 0; i < GI_PROBE_SHADER_VERSION_MAX; i++) {
  2306. giprobe_lighting_shader_version_shaders[i] = giprobe_shader.version_get_shader(giprobe_lighting_shader_version, i);
  2307. giprobe_lighting_shader_version_pipelines[i] = RD::get_singleton()->compute_pipeline_create(giprobe_lighting_shader_version_shaders[i]);
  2308. }
  2309. }
  2310. {
  2311. String defines;
  2312. if (gi_probe_use_anisotropy) {
  2313. defines += "\n#define USE_ANISOTROPY\n";
  2314. }
  2315. Vector<String> versions;
  2316. versions.push_back("\n#define MODE_DEBUG_COLOR\n");
  2317. versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
  2318. versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
  2319. versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
  2320. giprobe_debug_shader.initialize(versions, defines);
  2321. giprobe_debug_shader_version = giprobe_debug_shader.version_create();
  2322. for (int i = 0; i < GI_PROBE_DEBUG_MAX; i++) {
  2323. giprobe_debug_shader_version_shaders[i] = giprobe_debug_shader.version_get_shader(giprobe_debug_shader_version, i);
  2324. RD::PipelineRasterizationState rs;
  2325. rs.cull_mode = RD::POLYGON_CULL_FRONT;
  2326. RD::PipelineDepthStencilState ds;
  2327. ds.enable_depth_test = true;
  2328. ds.enable_depth_write = true;
  2329. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  2330. giprobe_debug_shader_version_pipelines[i].setup(giprobe_debug_shader_version_shaders[i], RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
  2331. }
  2332. }
  2333. }
  2334. RasterizerSceneRD::~RasterizerSceneRD() {
  2335. directional_shadow_atlas_set_size(0);
  2336. for (Map<Vector2i, ShadowMap>::Element *E = shadow_maps.front(); E; E = E->next()) {
  2337. RD::get_singleton()->free(E->get().depth);
  2338. }
  2339. for (Map<int, ShadowCubemap>::Element *E = shadow_cubemaps.front(); E; E = E->next()) {
  2340. RD::get_singleton()->free(E->get().cubemap);
  2341. }
  2342. RD::get_singleton()->free(gi_probe_lights_uniform);
  2343. memdelete_arr(gi_probe_lights);
  2344. }