renderer_scene_sky_rd.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. /*************************************************************************/
  2. /* renderer_scene_sky_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "renderer_scene_sky_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/math/math_defs.h"
  33. #include "renderer_scene_render_rd.h"
  34. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  35. #include "servers/rendering/rendering_server_default.h"
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // SKY SHADER
  38. void RendererSceneSkyRD::SkyShaderData::set_code(const String &p_code) {
  39. //compile
  40. code = p_code;
  41. valid = false;
  42. ubo_size = 0;
  43. uniforms.clear();
  44. if (code == String()) {
  45. return; //just invalid, but no error
  46. }
  47. ShaderCompilerRD::GeneratedCode gen_code;
  48. ShaderCompilerRD::IdentifierActions actions;
  49. actions.entry_point_stages["sky"] = ShaderCompilerRD::STAGE_FRAGMENT;
  50. uses_time = false;
  51. uses_half_res = false;
  52. uses_quarter_res = false;
  53. uses_position = false;
  54. uses_light = false;
  55. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  56. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  57. actions.usage_flag_pointers["TIME"] = &uses_time;
  58. actions.usage_flag_pointers["POSITION"] = &uses_position;
  59. actions.usage_flag_pointers["LIGHT0_ENABLED"] = &uses_light;
  60. actions.usage_flag_pointers["LIGHT0_ENERGY"] = &uses_light;
  61. actions.usage_flag_pointers["LIGHT0_DIRECTION"] = &uses_light;
  62. actions.usage_flag_pointers["LIGHT0_COLOR"] = &uses_light;
  63. actions.usage_flag_pointers["LIGHT0_SIZE"] = &uses_light;
  64. actions.usage_flag_pointers["LIGHT1_ENABLED"] = &uses_light;
  65. actions.usage_flag_pointers["LIGHT1_ENERGY"] = &uses_light;
  66. actions.usage_flag_pointers["LIGHT1_DIRECTION"] = &uses_light;
  67. actions.usage_flag_pointers["LIGHT1_COLOR"] = &uses_light;
  68. actions.usage_flag_pointers["LIGHT1_SIZE"] = &uses_light;
  69. actions.usage_flag_pointers["LIGHT2_ENABLED"] = &uses_light;
  70. actions.usage_flag_pointers["LIGHT2_ENERGY"] = &uses_light;
  71. actions.usage_flag_pointers["LIGHT2_DIRECTION"] = &uses_light;
  72. actions.usage_flag_pointers["LIGHT2_COLOR"] = &uses_light;
  73. actions.usage_flag_pointers["LIGHT2_SIZE"] = &uses_light;
  74. actions.usage_flag_pointers["LIGHT3_ENABLED"] = &uses_light;
  75. actions.usage_flag_pointers["LIGHT3_ENERGY"] = &uses_light;
  76. actions.usage_flag_pointers["LIGHT3_DIRECTION"] = &uses_light;
  77. actions.usage_flag_pointers["LIGHT3_COLOR"] = &uses_light;
  78. actions.usage_flag_pointers["LIGHT3_SIZE"] = &uses_light;
  79. actions.uniforms = &uniforms;
  80. // !BAS! Contemplate making `SkyShader sky` accessible from this struct or even part of this struct.
  81. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  82. Error err = scene_singleton->sky.sky_shader.compiler.compile(RS::SHADER_SKY, code, &actions, path, gen_code);
  83. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  84. if (version.is_null()) {
  85. version = scene_singleton->sky.sky_shader.shader.version_create();
  86. }
  87. #if 0
  88. print_line("**compiling shader:");
  89. print_line("**defines:\n");
  90. for (int i = 0; i < gen_code.defines.size(); i++) {
  91. print_line(gen_code.defines[i]);
  92. }
  93. print_line("\n**uniforms:\n" + gen_code.uniforms);
  94. // print_line("\n**vertex_globals:\n" + gen_code.vertex_global);
  95. // print_line("\n**vertex_code:\n" + gen_code.vertex);
  96. print_line("\n**fragment_globals:\n" + gen_code.fragment_global);
  97. print_line("\n**fragment_code:\n" + gen_code.fragment);
  98. print_line("\n**light_code:\n" + gen_code.light);
  99. #endif
  100. scene_singleton->sky.sky_shader.shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompilerRD::STAGE_VERTEX], gen_code.stage_globals[ShaderCompilerRD::STAGE_FRAGMENT], gen_code.defines);
  101. ERR_FAIL_COND(!scene_singleton->sky.sky_shader.shader.version_is_valid(version));
  102. ubo_size = gen_code.uniform_total_size;
  103. ubo_offsets = gen_code.uniform_offsets;
  104. texture_uniforms = gen_code.texture_uniforms;
  105. //update pipelines
  106. for (int i = 0; i < SKY_VERSION_MAX; i++) {
  107. RD::PipelineDepthStencilState depth_stencil_state;
  108. depth_stencil_state.enable_depth_test = true;
  109. depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  110. if (scene_singleton->sky.sky_shader.shader.is_variant_enabled(i)) {
  111. RID shader_variant = scene_singleton->sky.sky_shader.shader.version_get_shader(version, i);
  112. pipelines[i].setup(shader_variant, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), depth_stencil_state, RD::PipelineColorBlendState::create_disabled(), 0);
  113. } else {
  114. pipelines[i].clear();
  115. }
  116. }
  117. valid = true;
  118. }
  119. void RendererSceneSkyRD::SkyShaderData::set_default_texture_param(const StringName &p_name, RID p_texture) {
  120. if (!p_texture.is_valid()) {
  121. default_texture_params.erase(p_name);
  122. } else {
  123. default_texture_params[p_name] = p_texture;
  124. }
  125. }
  126. void RendererSceneSkyRD::SkyShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  127. Map<int, StringName> order;
  128. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  129. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  130. continue;
  131. }
  132. if (E->get().texture_order >= 0) {
  133. order[E->get().texture_order + 100000] = E->key();
  134. } else {
  135. order[E->get().order] = E->key();
  136. }
  137. }
  138. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  139. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E->get()]);
  140. pi.name = E->get();
  141. p_param_list->push_back(pi);
  142. }
  143. }
  144. void RendererSceneSkyRD::SkyShaderData::get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const {
  145. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  146. if (E->get().scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  147. continue;
  148. }
  149. RendererStorage::InstanceShaderParam p;
  150. p.info = ShaderLanguage::uniform_to_property_info(E->get());
  151. p.info.name = E->key(); //supply name
  152. p.index = E->get().instance_index;
  153. p.default_value = ShaderLanguage::constant_value_to_variant(E->get().default_value, E->get().type, E->get().hint);
  154. p_param_list->push_back(p);
  155. }
  156. }
  157. bool RendererSceneSkyRD::SkyShaderData::is_param_texture(const StringName &p_param) const {
  158. if (!uniforms.has(p_param)) {
  159. return false;
  160. }
  161. return uniforms[p_param].texture_order >= 0;
  162. }
  163. bool RendererSceneSkyRD::SkyShaderData::is_animated() const {
  164. return false;
  165. }
  166. bool RendererSceneSkyRD::SkyShaderData::casts_shadows() const {
  167. return false;
  168. }
  169. Variant RendererSceneSkyRD::SkyShaderData::get_default_parameter(const StringName &p_parameter) const {
  170. if (uniforms.has(p_parameter)) {
  171. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  172. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  173. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  174. }
  175. return Variant();
  176. }
  177. RS::ShaderNativeSourceCode RendererSceneSkyRD::SkyShaderData::get_native_source_code() const {
  178. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  179. return scene_singleton->sky.sky_shader.shader.version_get_native_source_code(version);
  180. }
  181. RendererSceneSkyRD::SkyShaderData::SkyShaderData() {
  182. valid = false;
  183. }
  184. RendererSceneSkyRD::SkyShaderData::~SkyShaderData() {
  185. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  186. ERR_FAIL_COND(!scene_singleton);
  187. //pipeline variants will clear themselves if shader is gone
  188. if (version.is_valid()) {
  189. scene_singleton->sky.sky_shader.shader.version_free(version);
  190. }
  191. }
  192. ////////////////////////////////////////////////////////////////////////////////
  193. // Sky material
  194. bool RendererSceneSkyRD::SkyMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  195. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  196. uniform_set_updated = true;
  197. return update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set, scene_singleton->sky.sky_shader.shader.version_get_shader(shader_data->version, 0), SKY_SET_MATERIAL);
  198. }
  199. RendererSceneSkyRD::SkyMaterialData::~SkyMaterialData() {
  200. free_parameters_uniform_set(uniform_set);
  201. }
  202. ////////////////////////////////////////////////////////////////////////////////
  203. // Render sky
  204. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  205. p_array[0] = p_basis.elements[0][0];
  206. p_array[1] = p_basis.elements[1][0];
  207. p_array[2] = p_basis.elements[2][0];
  208. p_array[3] = 0;
  209. p_array[4] = p_basis.elements[0][1];
  210. p_array[5] = p_basis.elements[1][1];
  211. p_array[6] = p_basis.elements[2][1];
  212. p_array[7] = 0;
  213. p_array[8] = p_basis.elements[0][2];
  214. p_array[9] = p_basis.elements[1][2];
  215. p_array[10] = p_basis.elements[2][2];
  216. p_array[11] = 0;
  217. }
  218. void RendererSceneSkyRD::_render_sky(RD::DrawListID p_list, float p_time, RID p_fb, PipelineCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, uint32_t p_view_count, const CameraMatrix *p_projections, const Basis &p_orientation, float p_multiplier, const Vector3 &p_position, float p_luminance_multiplier) {
  219. SkyPushConstant sky_push_constant;
  220. memset(&sky_push_constant, 0, sizeof(SkyPushConstant));
  221. for (uint32_t v = 0; v < p_view_count; v++) {
  222. // We only need key components of our projection matrix
  223. sky_push_constant.projections[v][0] = p_projections[v].matrix[2][0];
  224. sky_push_constant.projections[v][1] = p_projections[v].matrix[0][0];
  225. sky_push_constant.projections[v][2] = p_projections[v].matrix[2][1];
  226. sky_push_constant.projections[v][3] = p_projections[v].matrix[1][1];
  227. }
  228. sky_push_constant.position[0] = p_position.x;
  229. sky_push_constant.position[1] = p_position.y;
  230. sky_push_constant.position[2] = p_position.z;
  231. sky_push_constant.multiplier = p_multiplier;
  232. sky_push_constant.time = p_time;
  233. sky_push_constant.luminance_multiplier = p_luminance_multiplier;
  234. store_transform_3x3(p_orientation, sky_push_constant.orientation);
  235. RenderingDevice::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_fb);
  236. RD::DrawListID draw_list = p_list;
  237. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, p_pipeline->get_render_pipeline(RD::INVALID_ID, fb_format, false, RD::get_singleton()->draw_list_get_current_pass()));
  238. // Update uniform sets.
  239. {
  240. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, sky_scene_state.uniform_set, 0);
  241. if (p_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(p_uniform_set)) { // Material may not have a uniform set.
  242. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_uniform_set, 1);
  243. }
  244. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_texture_set, 2);
  245. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, sky_scene_state.fog_uniform_set, 3);
  246. }
  247. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  248. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky_push_constant, sizeof(SkyPushConstant));
  249. RD::get_singleton()->draw_list_draw(draw_list, true);
  250. }
  251. ////////////////////////////////////////////////////////////////////////////////
  252. // ReflectionData
  253. void RendererSceneSkyRD::ReflectionData::clear_reflection_data() {
  254. layers.clear();
  255. radiance_base_cubemap = RID();
  256. if (downsampled_radiance_cubemap.is_valid()) {
  257. RD::get_singleton()->free(downsampled_radiance_cubemap);
  258. }
  259. downsampled_radiance_cubemap = RID();
  260. downsampled_layer.mipmaps.clear();
  261. coefficient_buffer = RID();
  262. }
  263. void RendererSceneSkyRD::ReflectionData::update_reflection_data(RendererStorageRD *p_storage, int p_size, int p_mipmaps, bool p_use_array, RID p_base_cube, int p_base_layer, bool p_low_quality, int p_roughness_layers, RD::DataFormat p_texture_format) {
  264. //recreate radiance and all data
  265. int mipmaps = p_mipmaps;
  266. uint32_t w = p_size, h = p_size;
  267. EffectsRD *effects = p_storage->get_effects();
  268. ERR_FAIL_NULL_MSG(effects, "Effects haven't been initialised");
  269. bool prefer_raster_effects = effects->get_prefer_raster_effects();
  270. if (p_use_array) {
  271. int num_layers = p_low_quality ? 8 : p_roughness_layers;
  272. for (int i = 0; i < num_layers; i++) {
  273. ReflectionData::Layer layer;
  274. uint32_t mmw = w;
  275. uint32_t mmh = h;
  276. layer.mipmaps.resize(mipmaps);
  277. layer.views.resize(mipmaps);
  278. for (int j = 0; j < mipmaps; j++) {
  279. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  280. mm.size.width = mmw;
  281. mm.size.height = mmh;
  282. for (int k = 0; k < 6; k++) {
  283. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6 + k, j);
  284. Vector<RID> fbtex;
  285. fbtex.push_back(mm.views[k]);
  286. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  287. }
  288. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6, j, RD::TEXTURE_SLICE_CUBEMAP);
  289. mmw = MAX(1, mmw >> 1);
  290. mmh = MAX(1, mmh >> 1);
  291. }
  292. layers.push_back(layer);
  293. }
  294. } else {
  295. mipmaps = p_low_quality ? 8 : mipmaps;
  296. //regular cubemap, lower quality (aliasing, less memory)
  297. ReflectionData::Layer layer;
  298. uint32_t mmw = w;
  299. uint32_t mmh = h;
  300. layer.mipmaps.resize(mipmaps);
  301. layer.views.resize(mipmaps);
  302. for (int j = 0; j < mipmaps; j++) {
  303. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  304. mm.size.width = mmw;
  305. mm.size.height = mmh;
  306. for (int k = 0; k < 6; k++) {
  307. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + k, j);
  308. Vector<RID> fbtex;
  309. fbtex.push_back(mm.views[k]);
  310. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  311. }
  312. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, j, RD::TEXTURE_SLICE_CUBEMAP);
  313. mmw = MAX(1, mmw >> 1);
  314. mmh = MAX(1, mmh >> 1);
  315. }
  316. layers.push_back(layer);
  317. }
  318. radiance_base_cubemap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, 0, RD::TEXTURE_SLICE_CUBEMAP);
  319. RD::get_singleton()->set_resource_name(radiance_base_cubemap, "radiance base cubemap");
  320. RD::TextureFormat tf;
  321. tf.format = p_texture_format;
  322. tf.width = 64; // Always 64x64
  323. tf.height = 64;
  324. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  325. tf.array_layers = 6;
  326. tf.mipmaps = 7;
  327. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  328. downsampled_radiance_cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  329. RD::get_singleton()->set_resource_name(downsampled_radiance_cubemap, "downsampled radiance cubemap");
  330. {
  331. uint32_t mmw = 64;
  332. uint32_t mmh = 64;
  333. downsampled_layer.mipmaps.resize(7);
  334. for (int j = 0; j < downsampled_layer.mipmaps.size(); j++) {
  335. ReflectionData::DownsampleLayer::Mipmap &mm = downsampled_layer.mipmaps.write[j];
  336. mm.size.width = mmw;
  337. mm.size.height = mmh;
  338. mm.view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), downsampled_radiance_cubemap, 0, j, RD::TEXTURE_SLICE_CUBEMAP);
  339. RD::get_singleton()->set_resource_name(mm.view, "Downsampled Radiance Cubemap Mip " + itos(j) + " ");
  340. if (prefer_raster_effects) {
  341. // we need a framebuffer for each side of our cubemap
  342. for (int k = 0; k < 6; k++) {
  343. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), downsampled_radiance_cubemap, k, j);
  344. RD::get_singleton()->set_resource_name(mm.view, "Downsampled Radiance Cubemap Mip: " + itos(j) + " Face: " + itos(k) + " ");
  345. Vector<RID> fbtex;
  346. fbtex.push_back(mm.views[k]);
  347. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  348. }
  349. }
  350. mmw = MAX(1, mmw >> 1);
  351. mmh = MAX(1, mmh >> 1);
  352. }
  353. }
  354. }
  355. void RendererSceneSkyRD::ReflectionData::create_reflection_fast_filter(RendererStorageRD *p_storage, bool p_use_arrays) {
  356. EffectsRD *effects = p_storage->get_effects();
  357. ERR_FAIL_NULL_MSG(effects, "Effects haven't been initialised");
  358. bool prefer_raster_effects = effects->get_prefer_raster_effects();
  359. if (prefer_raster_effects) {
  360. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  361. for (int k = 0; k < 6; k++) {
  362. effects->cubemap_downsample_raster(radiance_base_cubemap, downsampled_layer.mipmaps[0].framebuffers[k], k, downsampled_layer.mipmaps[0].size);
  363. }
  364. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  365. for (int k = 0; k < 6; k++) {
  366. effects->cubemap_downsample_raster(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].framebuffers[k], k, downsampled_layer.mipmaps[i].size);
  367. }
  368. }
  369. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  370. if (p_use_arrays) {
  371. RD::get_singleton()->draw_command_begin_label("filter radiance map into array heads");
  372. for (int i = 0; i < layers.size(); i++) {
  373. for (int k = 0; k < 6; k++) {
  374. effects->cubemap_filter_raster(downsampled_radiance_cubemap, layers[i].mipmaps[0].framebuffers[k], k, i);
  375. }
  376. }
  377. } else {
  378. RD::get_singleton()->draw_command_begin_label("filter radiance map into mipmaps directly");
  379. for (int j = 0; j < layers[0].mipmaps.size(); j++) {
  380. for (int k = 0; k < 6; k++) {
  381. effects->cubemap_filter_raster(downsampled_radiance_cubemap, layers[0].mipmaps[j].framebuffers[k], k, j);
  382. }
  383. }
  384. }
  385. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  386. } else {
  387. effects->cubemap_downsample(radiance_base_cubemap, downsampled_layer.mipmaps[0].view, downsampled_layer.mipmaps[0].size);
  388. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  389. effects->cubemap_downsample(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].view, downsampled_layer.mipmaps[i].size);
  390. }
  391. Vector<RID> views;
  392. if (p_use_arrays) {
  393. for (int i = 1; i < layers.size(); i++) {
  394. views.push_back(layers[i].views[0]);
  395. }
  396. } else {
  397. for (int i = 1; i < layers[0].views.size(); i++) {
  398. views.push_back(layers[0].views[i]);
  399. }
  400. }
  401. effects->cubemap_filter(downsampled_radiance_cubemap, views, p_use_arrays);
  402. }
  403. }
  404. void RendererSceneSkyRD::ReflectionData::create_reflection_importance_sample(RendererStorageRD *p_storage, bool p_use_arrays, int p_cube_side, int p_base_layer, uint32_t p_sky_ggx_samples_quality) {
  405. EffectsRD *effects = p_storage->get_effects();
  406. ERR_FAIL_NULL_MSG(effects, "Effects haven't been initialised");
  407. bool prefer_raster_effects = effects->get_prefer_raster_effects();
  408. if (prefer_raster_effects) {
  409. // Need to ask clayjohn but p_cube_side is set to 10, looks like in the compute shader we're doing all 6 sides in one call
  410. // here we need to do them one by one so ignoring p_cube_side
  411. if (p_use_arrays) {
  412. for (int k = 0; k < 6; k++) {
  413. effects->cubemap_roughness_raster(
  414. radiance_base_cubemap,
  415. layers[p_base_layer].mipmaps[0].framebuffers[k],
  416. k,
  417. p_sky_ggx_samples_quality,
  418. float(p_base_layer) / (layers.size() - 1.0),
  419. layers[p_base_layer].mipmaps[0].size.x);
  420. }
  421. } else {
  422. for (int k = 0; k < 6; k++) {
  423. effects->cubemap_roughness_raster(
  424. layers[0].views[p_base_layer - 1],
  425. layers[0].mipmaps[p_base_layer].framebuffers[k],
  426. k,
  427. p_sky_ggx_samples_quality,
  428. float(p_base_layer) / (layers[0].mipmaps.size() - 1.0),
  429. layers[0].mipmaps[p_base_layer].size.x);
  430. }
  431. }
  432. } else {
  433. if (p_use_arrays) {
  434. //render directly to the layers
  435. effects->cubemap_roughness(radiance_base_cubemap, layers[p_base_layer].views[0], p_cube_side, p_sky_ggx_samples_quality, float(p_base_layer) / (layers.size() - 1.0), layers[p_base_layer].mipmaps[0].size.x);
  436. } else {
  437. effects->cubemap_roughness(
  438. layers[0].views[p_base_layer - 1],
  439. layers[0].views[p_base_layer],
  440. p_cube_side,
  441. p_sky_ggx_samples_quality,
  442. float(p_base_layer) / (layers[0].mipmaps.size() - 1.0),
  443. layers[0].mipmaps[p_base_layer].size.x);
  444. }
  445. }
  446. }
  447. void RendererSceneSkyRD::ReflectionData::update_reflection_mipmaps(RendererStorageRD *p_storage, int p_start, int p_end) {
  448. EffectsRD *effects = p_storage->get_effects();
  449. ERR_FAIL_NULL_MSG(effects, "Effects haven't been initialised");
  450. bool prefer_raster_effects = effects->get_prefer_raster_effects();
  451. RD::get_singleton()->draw_command_begin_label("Update Radiance Cubemap Array Mipmaps");
  452. for (int i = p_start; i < p_end; i++) {
  453. for (int j = 0; j < layers[i].views.size() - 1; j++) {
  454. RID view = layers[i].views[j];
  455. Size2i size = layers[i].mipmaps[j + 1].size;
  456. if (prefer_raster_effects) {
  457. for (int k = 0; k < 6; k++) {
  458. RID framebuffer = layers[i].mipmaps[j + 1].framebuffers[k];
  459. effects->cubemap_downsample_raster(view, framebuffer, k, size);
  460. }
  461. } else {
  462. RID texture = layers[i].views[j + 1];
  463. effects->cubemap_downsample(view, texture, size);
  464. }
  465. }
  466. }
  467. RD::get_singleton()->draw_command_end_label();
  468. }
  469. ////////////////////////////////////////////////////////////////////////////////
  470. // RendererSceneSkyRD::Sky
  471. void RendererSceneSkyRD::Sky::free(RendererStorageRD *p_storage) {
  472. if (radiance.is_valid()) {
  473. RD::get_singleton()->free(radiance);
  474. radiance = RID();
  475. }
  476. reflection.clear_reflection_data();
  477. if (uniform_buffer.is_valid()) {
  478. RD::get_singleton()->free(uniform_buffer);
  479. uniform_buffer = RID();
  480. }
  481. if (half_res_pass.is_valid()) {
  482. RD::get_singleton()->free(half_res_pass);
  483. half_res_pass = RID();
  484. }
  485. if (quarter_res_pass.is_valid()) {
  486. RD::get_singleton()->free(quarter_res_pass);
  487. quarter_res_pass = RID();
  488. }
  489. if (material.is_valid()) {
  490. p_storage->free(material);
  491. }
  492. }
  493. RID RendererSceneSkyRD::Sky::get_textures(RendererStorageRD *p_storage, SkyTextureSetVersion p_version, RID p_default_shader_rd) {
  494. if (texture_uniform_sets[p_version].is_valid() && RD::get_singleton()->uniform_set_is_valid(texture_uniform_sets[p_version])) {
  495. return texture_uniform_sets[p_version];
  496. }
  497. Vector<RD::Uniform> uniforms;
  498. {
  499. RD::Uniform u;
  500. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  501. u.binding = 0;
  502. if (radiance.is_valid() && p_version <= SKY_TEXTURE_SET_QUARTER_RES) {
  503. u.ids.push_back(radiance);
  504. } else {
  505. u.ids.push_back(p_storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  506. }
  507. uniforms.push_back(u);
  508. }
  509. {
  510. RD::Uniform u;
  511. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  512. u.binding = 1; // half res
  513. if (half_res_pass.is_valid() && p_version != SKY_TEXTURE_SET_HALF_RES && p_version != SKY_TEXTURE_SET_CUBEMAP_HALF_RES) {
  514. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  515. u.ids.push_back(reflection.layers[0].views[1]);
  516. } else {
  517. u.ids.push_back(half_res_pass);
  518. }
  519. } else {
  520. if (p_version < SKY_TEXTURE_SET_CUBEMAP) {
  521. u.ids.push_back(p_storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  522. } else {
  523. u.ids.push_back(p_storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  524. }
  525. }
  526. uniforms.push_back(u);
  527. }
  528. {
  529. RD::Uniform u;
  530. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  531. u.binding = 2; // quarter res
  532. if (quarter_res_pass.is_valid() && p_version != SKY_TEXTURE_SET_QUARTER_RES && p_version != SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES) {
  533. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  534. u.ids.push_back(reflection.layers[0].views[2]);
  535. } else {
  536. u.ids.push_back(quarter_res_pass);
  537. }
  538. } else {
  539. if (p_version < SKY_TEXTURE_SET_CUBEMAP) {
  540. u.ids.push_back(p_storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  541. } else {
  542. u.ids.push_back(p_storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  543. }
  544. }
  545. uniforms.push_back(u);
  546. }
  547. texture_uniform_sets[p_version] = RD::get_singleton()->uniform_set_create(uniforms, p_default_shader_rd, SKY_SET_TEXTURES);
  548. return texture_uniform_sets[p_version];
  549. }
  550. bool RendererSceneSkyRD::Sky::set_radiance_size(int p_radiance_size) {
  551. ERR_FAIL_COND_V(p_radiance_size < 32 || p_radiance_size > 2048, false);
  552. if (radiance_size == p_radiance_size) {
  553. return false;
  554. }
  555. radiance_size = p_radiance_size;
  556. if (mode == RS::SKY_MODE_REALTIME && radiance_size != 256) {
  557. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  558. radiance_size = 256;
  559. }
  560. if (radiance.is_valid()) {
  561. RD::get_singleton()->free(radiance);
  562. radiance = RID();
  563. }
  564. reflection.clear_reflection_data();
  565. return true;
  566. }
  567. bool RendererSceneSkyRD::Sky::set_mode(RS::SkyMode p_mode) {
  568. if (mode == p_mode) {
  569. return false;
  570. }
  571. mode = p_mode;
  572. if (mode == RS::SKY_MODE_REALTIME && radiance_size != 256) {
  573. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  574. set_radiance_size(256);
  575. }
  576. if (radiance.is_valid()) {
  577. RD::get_singleton()->free(radiance);
  578. radiance = RID();
  579. }
  580. reflection.clear_reflection_data();
  581. return true;
  582. }
  583. bool RendererSceneSkyRD::Sky::set_material(RID p_material) {
  584. if (material == p_material) {
  585. return false;
  586. }
  587. material = p_material;
  588. return true;
  589. }
  590. Ref<Image> RendererSceneSkyRD::Sky::bake_panorama(RendererStorageRD *p_storage, float p_energy, int p_roughness_layers, const Size2i &p_size) {
  591. if (radiance.is_valid()) {
  592. RD::TextureFormat tf;
  593. tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  594. tf.width = p_size.width;
  595. tf.height = p_size.height;
  596. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  597. RID rad_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  598. p_storage->get_effects()->copy_cubemap_to_panorama(radiance, rad_tex, p_size, p_roughness_layers, reflection.layers.size() > 1);
  599. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rad_tex, 0);
  600. RD::get_singleton()->free(rad_tex);
  601. Ref<Image> img;
  602. img.instantiate();
  603. img->create(p_size.width, p_size.height, false, Image::FORMAT_RGBAF, data);
  604. for (int i = 0; i < p_size.width; i++) {
  605. for (int j = 0; j < p_size.height; j++) {
  606. Color c = img->get_pixel(i, j);
  607. c.r *= p_energy;
  608. c.g *= p_energy;
  609. c.b *= p_energy;
  610. img->set_pixel(i, j, c);
  611. }
  612. }
  613. return img;
  614. }
  615. return Ref<Image>();
  616. }
  617. ////////////////////////////////////////////////////////////////////////////////
  618. // RendererSceneSkyRD
  619. RendererStorageRD::ShaderData *RendererSceneSkyRD::_create_sky_shader_func() {
  620. SkyShaderData *shader_data = memnew(SkyShaderData);
  621. return shader_data;
  622. }
  623. RendererStorageRD::ShaderData *RendererSceneSkyRD::_create_sky_shader_funcs() {
  624. // !BAS! Why isn't _create_sky_shader_func not just static too?
  625. return static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton)->sky._create_sky_shader_func();
  626. };
  627. RendererStorageRD::MaterialData *RendererSceneSkyRD::_create_sky_material_func(SkyShaderData *p_shader) {
  628. SkyMaterialData *material_data = memnew(SkyMaterialData);
  629. material_data->shader_data = p_shader;
  630. material_data->last_frame = false;
  631. //update will happen later anyway so do nothing.
  632. return material_data;
  633. }
  634. RendererStorageRD::MaterialData *RendererSceneSkyRD::_create_sky_material_funcs(RendererStorageRD::ShaderData *p_shader) {
  635. // !BAS! same here, we could just make _create_sky_material_func static?
  636. return static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton)->sky._create_sky_material_func(static_cast<SkyShaderData *>(p_shader));
  637. };
  638. RendererSceneSkyRD::RendererSceneSkyRD() {
  639. roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers");
  640. sky_ggx_samples_quality = GLOBAL_GET("rendering/reflections/sky_reflections/ggx_samples");
  641. sky_use_cubemap_array = GLOBAL_GET("rendering/reflections/sky_reflections/texture_array_reflections");
  642. }
  643. void RendererSceneSkyRD::init(RendererStorageRD *p_storage) {
  644. storage = p_storage;
  645. {
  646. // Start with the directional lights for the sky
  647. sky_scene_state.max_directional_lights = 4;
  648. uint32_t directional_light_buffer_size = sky_scene_state.max_directional_lights * sizeof(SkyDirectionalLightData);
  649. sky_scene_state.directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  650. sky_scene_state.last_frame_directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  651. sky_scene_state.last_frame_directional_light_count = sky_scene_state.max_directional_lights + 1;
  652. sky_scene_state.directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  653. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_scene_state.max_directional_lights) + "\n";
  654. // Initialize sky
  655. Vector<String> sky_modes;
  656. sky_modes.push_back(""); // Full size
  657. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n"); // Half Res
  658. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n"); // Quarter res
  659. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n"); // Cubemap
  660. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_HALF_RES_PASS\n"); // Half Res Cubemap
  661. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_QUARTER_RES_PASS\n"); // Quarter res Cubemap
  662. sky_modes.push_back("\n#define USE_MULTIVIEW\n"); // Full size multiview
  663. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n#define USE_MULTIVIEW\n"); // Half Res multiview
  664. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n#define USE_MULTIVIEW\n"); // Quarter res multiview
  665. sky_shader.shader.initialize(sky_modes, defines);
  666. if (!RendererCompositorRD::singleton->is_xr_enabled()) {
  667. sky_shader.shader.set_variant_enabled(SKY_VERSION_BACKGROUND_MULTIVIEW, false);
  668. sky_shader.shader.set_variant_enabled(SKY_VERSION_HALF_RES_MULTIVIEW, false);
  669. sky_shader.shader.set_variant_enabled(SKY_VERSION_QUARTER_RES_MULTIVIEW, false);
  670. }
  671. }
  672. // register our shader funds
  673. storage->shader_set_data_request_function(RendererStorageRD::SHADER_TYPE_SKY, _create_sky_shader_funcs);
  674. storage->material_set_data_request_function(RendererStorageRD::SHADER_TYPE_SKY, _create_sky_material_funcs);
  675. {
  676. ShaderCompilerRD::DefaultIdentifierActions actions;
  677. actions.renames["COLOR"] = "color";
  678. actions.renames["ALPHA"] = "alpha";
  679. actions.renames["EYEDIR"] = "cube_normal";
  680. actions.renames["POSITION"] = "params.position_multiplier.xyz";
  681. actions.renames["SKY_COORDS"] = "panorama_coords";
  682. actions.renames["SCREEN_UV"] = "uv";
  683. actions.renames["TIME"] = "params.time";
  684. actions.renames["PI"] = _MKSTR(Math_PI);
  685. actions.renames["TAU"] = _MKSTR(Math_TAU);
  686. actions.renames["E"] = _MKSTR(Math_E);
  687. actions.renames["HALF_RES_COLOR"] = "half_res_color";
  688. actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
  689. actions.renames["RADIANCE"] = "radiance";
  690. actions.renames["FOG"] = "custom_fog";
  691. actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
  692. actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
  693. actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
  694. actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
  695. actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
  696. actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
  697. actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
  698. actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
  699. actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
  700. actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
  701. actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
  702. actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
  703. actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
  704. actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
  705. actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
  706. actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
  707. actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
  708. actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
  709. actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";
  710. actions.renames["LIGHT3_SIZE"] = "directional_lights.data[3].color_size.w";
  711. actions.renames["AT_CUBEMAP_PASS"] = "AT_CUBEMAP_PASS";
  712. actions.renames["AT_HALF_RES_PASS"] = "AT_HALF_RES_PASS";
  713. actions.renames["AT_QUARTER_RES_PASS"] = "AT_QUARTER_RES_PASS";
  714. actions.custom_samplers["RADIANCE"] = "material_samplers[3]";
  715. actions.usage_defines["HALF_RES_COLOR"] = "\n#define USES_HALF_RES_COLOR\n";
  716. actions.usage_defines["QUARTER_RES_COLOR"] = "\n#define USES_QUARTER_RES_COLOR\n";
  717. actions.render_mode_defines["disable_fog"] = "#define DISABLE_FOG\n";
  718. actions.sampler_array_name = "material_samplers";
  719. actions.base_texture_binding_index = 1;
  720. actions.texture_layout_set = 1;
  721. actions.base_uniform_string = "material.";
  722. actions.base_varying_index = 10;
  723. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  724. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  725. actions.global_buffer_array_variable = "global_variables.data";
  726. sky_shader.compiler.initialize(actions);
  727. }
  728. {
  729. // default material and shader for sky shader
  730. sky_shader.default_shader = storage->shader_allocate();
  731. storage->shader_initialize(sky_shader.default_shader);
  732. storage->shader_set_code(sky_shader.default_shader, R"(
  733. // Default sky shader.
  734. shader_type sky;
  735. void sky() {
  736. COLOR = vec3(0.0);
  737. }
  738. )");
  739. sky_shader.default_material = storage->material_allocate();
  740. storage->material_initialize(sky_shader.default_material);
  741. storage->material_set_shader(sky_shader.default_material, sky_shader.default_shader);
  742. SkyMaterialData *md = (SkyMaterialData *)storage->material_get_data(sky_shader.default_material, RendererStorageRD::SHADER_TYPE_SKY);
  743. sky_shader.default_shader_rd = sky_shader.shader.version_get_shader(md->shader_data->version, SKY_VERSION_BACKGROUND);
  744. sky_scene_state.uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SkySceneState::UBO));
  745. Vector<RD::Uniform> uniforms;
  746. {
  747. RD::Uniform u;
  748. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  749. u.binding = 0;
  750. u.ids.resize(12);
  751. RID *ids_ptr = u.ids.ptrw();
  752. ids_ptr[0] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  753. ids_ptr[1] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  754. ids_ptr[2] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  755. ids_ptr[3] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  756. ids_ptr[4] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  757. ids_ptr[5] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  758. ids_ptr[6] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  759. ids_ptr[7] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  760. ids_ptr[8] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  761. ids_ptr[9] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  762. ids_ptr[10] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  763. ids_ptr[11] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  764. uniforms.push_back(u);
  765. }
  766. {
  767. RD::Uniform u;
  768. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  769. u.binding = 1;
  770. u.ids.push_back(storage->global_variables_get_storage_buffer());
  771. uniforms.push_back(u);
  772. }
  773. {
  774. RD::Uniform u;
  775. u.binding = 2;
  776. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  777. u.ids.push_back(sky_scene_state.uniform_buffer);
  778. uniforms.push_back(u);
  779. }
  780. {
  781. RD::Uniform u;
  782. u.binding = 3;
  783. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  784. u.ids.push_back(sky_scene_state.directional_light_buffer);
  785. uniforms.push_back(u);
  786. }
  787. sky_scene_state.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_UNIFORMS);
  788. }
  789. {
  790. Vector<RD::Uniform> uniforms;
  791. {
  792. RD::Uniform u;
  793. u.binding = 0;
  794. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  795. RID vfog = storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  796. u.ids.push_back(vfog);
  797. uniforms.push_back(u);
  798. }
  799. sky_scene_state.default_fog_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_FOG);
  800. }
  801. {
  802. // Need defaults for using fog with clear color
  803. sky_scene_state.fog_shader = storage->shader_allocate();
  804. storage->shader_initialize(sky_scene_state.fog_shader);
  805. storage->shader_set_code(sky_scene_state.fog_shader, R"(
  806. // Default clear color sky shader.
  807. shader_type sky;
  808. uniform vec4 clear_color;
  809. void sky() {
  810. COLOR = clear_color.rgb;
  811. }
  812. )");
  813. sky_scene_state.fog_material = storage->material_allocate();
  814. storage->material_initialize(sky_scene_state.fog_material);
  815. storage->material_set_shader(sky_scene_state.fog_material, sky_scene_state.fog_shader);
  816. Vector<RD::Uniform> uniforms;
  817. {
  818. RD::Uniform u;
  819. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  820. u.binding = 0;
  821. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  822. uniforms.push_back(u);
  823. }
  824. {
  825. RD::Uniform u;
  826. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  827. u.binding = 1;
  828. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  829. uniforms.push_back(u);
  830. }
  831. {
  832. RD::Uniform u;
  833. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  834. u.binding = 2;
  835. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  836. uniforms.push_back(u);
  837. }
  838. sky_scene_state.fog_only_texture_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_TEXTURES);
  839. }
  840. { //create index array for copy shaders
  841. Vector<uint8_t> pv;
  842. pv.resize(6 * 4);
  843. {
  844. uint8_t *w = pv.ptrw();
  845. int *p32 = (int *)w;
  846. p32[0] = 0;
  847. p32[1] = 1;
  848. p32[2] = 2;
  849. p32[3] = 0;
  850. p32[4] = 2;
  851. p32[5] = 3;
  852. }
  853. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  854. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  855. }
  856. }
  857. void RendererSceneSkyRD::set_texture_format(RD::DataFormat p_texture_format) {
  858. texture_format = p_texture_format;
  859. }
  860. RendererSceneSkyRD::~RendererSceneSkyRD() {
  861. // TODO cleanup anything created in init...
  862. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.uniform_set)) {
  863. RD::get_singleton()->free(sky_scene_state.uniform_set);
  864. }
  865. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.default_fog_uniform_set)) {
  866. RD::get_singleton()->free(sky_scene_state.default_fog_uniform_set);
  867. }
  868. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.fog_only_texture_uniform_set)) {
  869. RD::get_singleton()->free(sky_scene_state.fog_only_texture_uniform_set);
  870. }
  871. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  872. }
  873. void RendererSceneSkyRD::setup(RendererSceneEnvironmentRD *p_env, RID p_render_buffers, const CameraMatrix &p_projection, const Transform3D &p_transform, const Size2i p_screen_size, RendererSceneRenderRD *p_scene_render) {
  874. ERR_FAIL_COND(!p_env); // I guess without an environment we also can't have a sky...
  875. SkyMaterialData *material = nullptr;
  876. Sky *sky = get_sky(p_env->sky);
  877. RID sky_material;
  878. SkyShaderData *shader_data = nullptr;
  879. RS::EnvironmentBG background = p_env->background;
  880. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  881. // !BAS! Possibly silently fail here, we now get error spam when you select sky as the background but haven't setup the sky yet.
  882. ERR_FAIL_COND(!sky);
  883. sky_material = sky_get_material(p_env->sky);
  884. if (sky_material.is_valid()) {
  885. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  886. if (!material || !material->shader_data->valid) {
  887. material = nullptr;
  888. }
  889. }
  890. if (!material) {
  891. sky_material = sky_shader.default_material;
  892. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  893. }
  894. ERR_FAIL_COND(!material);
  895. shader_data = material->shader_data;
  896. ERR_FAIL_COND(!shader_data);
  897. }
  898. if (sky) {
  899. // Invalidate supbass buffers if screen size changes
  900. if (sky->screen_size != p_screen_size) {
  901. sky->screen_size = p_screen_size;
  902. sky->screen_size.x = sky->screen_size.x < 4 ? 4 : sky->screen_size.x;
  903. sky->screen_size.y = sky->screen_size.y < 4 ? 4 : sky->screen_size.y;
  904. if (shader_data->uses_half_res) {
  905. if (sky->half_res_pass.is_valid()) {
  906. RD::get_singleton()->free(sky->half_res_pass);
  907. sky->half_res_pass = RID();
  908. }
  909. invalidate_sky(sky);
  910. }
  911. if (shader_data->uses_quarter_res) {
  912. if (sky->quarter_res_pass.is_valid()) {
  913. RD::get_singleton()->free(sky->quarter_res_pass);
  914. sky->quarter_res_pass = RID();
  915. }
  916. invalidate_sky(sky);
  917. }
  918. }
  919. // Create new subpass buffers if necessary
  920. if ((shader_data->uses_half_res && sky->half_res_pass.is_null()) ||
  921. (shader_data->uses_quarter_res && sky->quarter_res_pass.is_null()) ||
  922. sky->radiance.is_null()) {
  923. invalidate_sky(sky);
  924. update_dirty_skys();
  925. }
  926. if (shader_data->uses_time && p_scene_render->time - sky->prev_time > 0.00001) {
  927. sky->prev_time = p_scene_render->time;
  928. sky->reflection.dirty = true;
  929. RenderingServerDefault::redraw_request();
  930. }
  931. if (material != sky->prev_material) {
  932. sky->prev_material = material;
  933. sky->reflection.dirty = true;
  934. }
  935. if (material->uniform_set_updated) {
  936. material->uniform_set_updated = false;
  937. sky->reflection.dirty = true;
  938. }
  939. if (!p_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  940. sky->prev_position = p_transform.origin;
  941. sky->reflection.dirty = true;
  942. }
  943. if (shader_data->uses_light) {
  944. // Check whether the directional_light_buffer changes
  945. bool light_data_dirty = false;
  946. if (sky_scene_state.ubo.directional_light_count != sky_scene_state.last_frame_directional_light_count) {
  947. light_data_dirty = true;
  948. for (uint32_t i = sky_scene_state.ubo.directional_light_count; i < sky_scene_state.max_directional_lights; i++) {
  949. sky_scene_state.directional_lights[i].enabled = false;
  950. }
  951. }
  952. if (!light_data_dirty) {
  953. for (uint32_t i = 0; i < sky_scene_state.ubo.directional_light_count; i++) {
  954. if (sky_scene_state.directional_lights[i].direction[0] != sky_scene_state.last_frame_directional_lights[i].direction[0] ||
  955. sky_scene_state.directional_lights[i].direction[1] != sky_scene_state.last_frame_directional_lights[i].direction[1] ||
  956. sky_scene_state.directional_lights[i].direction[2] != sky_scene_state.last_frame_directional_lights[i].direction[2] ||
  957. sky_scene_state.directional_lights[i].energy != sky_scene_state.last_frame_directional_lights[i].energy ||
  958. sky_scene_state.directional_lights[i].color[0] != sky_scene_state.last_frame_directional_lights[i].color[0] ||
  959. sky_scene_state.directional_lights[i].color[1] != sky_scene_state.last_frame_directional_lights[i].color[1] ||
  960. sky_scene_state.directional_lights[i].color[2] != sky_scene_state.last_frame_directional_lights[i].color[2] ||
  961. sky_scene_state.directional_lights[i].enabled != sky_scene_state.last_frame_directional_lights[i].enabled ||
  962. sky_scene_state.directional_lights[i].size != sky_scene_state.last_frame_directional_lights[i].size) {
  963. light_data_dirty = true;
  964. break;
  965. }
  966. }
  967. }
  968. if (light_data_dirty) {
  969. RD::get_singleton()->buffer_update(sky_scene_state.directional_light_buffer, 0, sizeof(SkyDirectionalLightData) * sky_scene_state.max_directional_lights, sky_scene_state.directional_lights);
  970. SkyDirectionalLightData *temp = sky_scene_state.last_frame_directional_lights;
  971. sky_scene_state.last_frame_directional_lights = sky_scene_state.directional_lights;
  972. sky_scene_state.directional_lights = temp;
  973. sky_scene_state.last_frame_directional_light_count = sky_scene_state.ubo.directional_light_count;
  974. sky->reflection.dirty = true;
  975. }
  976. }
  977. }
  978. //setup fog variables
  979. sky_scene_state.ubo.volumetric_fog_enabled = false;
  980. if (p_render_buffers.is_valid()) {
  981. if (p_scene_render->render_buffers_has_volumetric_fog(p_render_buffers)) {
  982. sky_scene_state.ubo.volumetric_fog_enabled = true;
  983. float fog_end = p_scene_render->render_buffers_get_volumetric_fog_end(p_render_buffers);
  984. if (fog_end > 0.0) {
  985. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0 / fog_end;
  986. } else {
  987. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0;
  988. }
  989. float fog_detail_spread = p_scene_render->render_buffers_get_volumetric_fog_detail_spread(p_render_buffers); //reverse lookup
  990. if (fog_detail_spread > 0.0) {
  991. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0 / fog_detail_spread;
  992. } else {
  993. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0;
  994. }
  995. }
  996. RID fog_uniform_set = p_scene_render->render_buffers_get_volumetric_fog_sky_uniform_set(p_render_buffers);
  997. if (fog_uniform_set != RID()) {
  998. sky_scene_state.fog_uniform_set = fog_uniform_set;
  999. } else {
  1000. sky_scene_state.fog_uniform_set = sky_scene_state.default_fog_uniform_set;
  1001. }
  1002. }
  1003. sky_scene_state.ubo.z_far = p_projection.get_z_far();
  1004. sky_scene_state.ubo.fog_enabled = p_env->fog_enabled;
  1005. sky_scene_state.ubo.fog_density = p_env->fog_density;
  1006. sky_scene_state.ubo.fog_aerial_perspective = p_env->fog_aerial_perspective;
  1007. Color fog_color = p_env->fog_light_color.to_linear();
  1008. float fog_energy = p_env->fog_light_energy;
  1009. sky_scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
  1010. sky_scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
  1011. sky_scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
  1012. sky_scene_state.ubo.fog_sun_scatter = p_env->fog_sun_scatter;
  1013. RD::get_singleton()->buffer_update(sky_scene_state.uniform_buffer, 0, sizeof(SkySceneState::UBO), &sky_scene_state.ubo);
  1014. }
  1015. void RendererSceneSkyRD::update(RendererSceneEnvironmentRD *p_env, const CameraMatrix &p_projection, const Transform3D &p_transform, double p_time, float p_luminance_multiplier) {
  1016. ERR_FAIL_COND(!p_env);
  1017. Sky *sky = get_sky(p_env->sky);
  1018. ERR_FAIL_COND(!sky);
  1019. RID sky_material = sky_get_material(p_env->sky);
  1020. SkyMaterialData *material = nullptr;
  1021. if (sky_material.is_valid()) {
  1022. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1023. if (!material || !material->shader_data->valid) {
  1024. material = nullptr;
  1025. }
  1026. }
  1027. if (!material) {
  1028. sky_material = sky_shader.default_material;
  1029. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1030. }
  1031. ERR_FAIL_COND(!material);
  1032. SkyShaderData *shader_data = material->shader_data;
  1033. ERR_FAIL_COND(!shader_data);
  1034. float multiplier = p_env->bg_energy;
  1035. bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
  1036. RS::SkyMode sky_mode = sky->mode;
  1037. if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
  1038. if (shader_data->uses_time || shader_data->uses_position) {
  1039. update_single_frame = true;
  1040. sky_mode = RS::SKY_MODE_REALTIME;
  1041. } else if (shader_data->uses_light || shader_data->ubo_size > 0) {
  1042. update_single_frame = false;
  1043. sky_mode = RS::SKY_MODE_INCREMENTAL;
  1044. } else {
  1045. update_single_frame = true;
  1046. sky_mode = RS::SKY_MODE_QUALITY;
  1047. }
  1048. }
  1049. if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
  1050. // On the first frame after creating sky, rebuild in single frame
  1051. update_single_frame = true;
  1052. sky_mode = RS::SKY_MODE_QUALITY;
  1053. }
  1054. int max_processing_layer = sky_use_cubemap_array ? sky->reflection.layers.size() : sky->reflection.layers[0].mipmaps.size();
  1055. // Update radiance cubemap
  1056. if (sky->reflection.dirty && (sky->processing_layer >= max_processing_layer || update_single_frame)) {
  1057. static const Vector3 view_normals[6] = {
  1058. Vector3(+1, 0, 0),
  1059. Vector3(-1, 0, 0),
  1060. Vector3(0, +1, 0),
  1061. Vector3(0, -1, 0),
  1062. Vector3(0, 0, +1),
  1063. Vector3(0, 0, -1)
  1064. };
  1065. static const Vector3 view_up[6] = {
  1066. Vector3(0, -1, 0),
  1067. Vector3(0, -1, 0),
  1068. Vector3(0, 0, +1),
  1069. Vector3(0, 0, -1),
  1070. Vector3(0, -1, 0),
  1071. Vector3(0, -1, 0)
  1072. };
  1073. CameraMatrix cm;
  1074. cm.set_perspective(90, 1, 0.01, 10.0);
  1075. CameraMatrix correction;
  1076. correction.set_depth_correction(true);
  1077. cm = correction * cm;
  1078. if (shader_data->uses_quarter_res) {
  1079. RD::get_singleton()->draw_command_begin_label("Render Sky to Quarter Res Cubemap");
  1080. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_QUARTER_RES];
  1081. Vector<Color> clear_colors;
  1082. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1083. RD::DrawListID cubemap_draw_list;
  1084. for (int i = 0; i < 6; i++) {
  1085. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1086. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES, sky_shader.default_shader_rd);
  1087. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[2].framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  1088. _render_sky(cubemap_draw_list, p_time, sky->reflection.layers[0].mipmaps[2].framebuffers[i], pipeline, material->uniform_set, texture_uniform_set, 1, &cm, local_view, multiplier, p_transform.origin, p_luminance_multiplier);
  1089. RD::get_singleton()->draw_list_end();
  1090. }
  1091. RD::get_singleton()->draw_command_end_label();
  1092. }
  1093. if (shader_data->uses_half_res) {
  1094. RD::get_singleton()->draw_command_begin_label("Render Sky to Half Res Cubemap");
  1095. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_HALF_RES];
  1096. Vector<Color> clear_colors;
  1097. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1098. RD::DrawListID cubemap_draw_list;
  1099. for (int i = 0; i < 6; i++) {
  1100. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1101. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_CUBEMAP_HALF_RES, sky_shader.default_shader_rd);
  1102. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[1].framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  1103. _render_sky(cubemap_draw_list, p_time, sky->reflection.layers[0].mipmaps[1].framebuffers[i], pipeline, material->uniform_set, texture_uniform_set, 1, &cm, local_view, multiplier, p_transform.origin, p_luminance_multiplier);
  1104. RD::get_singleton()->draw_list_end();
  1105. }
  1106. RD::get_singleton()->draw_command_end_label();
  1107. }
  1108. RD::DrawListID cubemap_draw_list;
  1109. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP];
  1110. RD::get_singleton()->draw_command_begin_label("Render Sky Cubemap");
  1111. for (int i = 0; i < 6; i++) {
  1112. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1113. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_CUBEMAP, sky_shader.default_shader_rd);
  1114. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[0].framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  1115. _render_sky(cubemap_draw_list, p_time, sky->reflection.layers[0].mipmaps[0].framebuffers[i], pipeline, material->uniform_set, texture_uniform_set, 1, &cm, local_view, multiplier, p_transform.origin, p_luminance_multiplier);
  1116. RD::get_singleton()->draw_list_end();
  1117. }
  1118. RD::get_singleton()->draw_command_end_label();
  1119. if (sky_mode == RS::SKY_MODE_REALTIME) {
  1120. sky->reflection.create_reflection_fast_filter(storage, sky_use_cubemap_array);
  1121. if (sky_use_cubemap_array) {
  1122. sky->reflection.update_reflection_mipmaps(storage, 0, sky->reflection.layers.size());
  1123. }
  1124. } else {
  1125. if (update_single_frame) {
  1126. for (int i = 1; i < max_processing_layer; i++) {
  1127. sky->reflection.create_reflection_importance_sample(storage, sky_use_cubemap_array, 10, i, sky_ggx_samples_quality);
  1128. }
  1129. if (sky_use_cubemap_array) {
  1130. sky->reflection.update_reflection_mipmaps(storage, 0, sky->reflection.layers.size());
  1131. }
  1132. } else {
  1133. if (sky_use_cubemap_array) {
  1134. // Multi-Frame so just update the first array level
  1135. sky->reflection.update_reflection_mipmaps(storage, 0, 1);
  1136. }
  1137. }
  1138. sky->processing_layer = 1;
  1139. }
  1140. sky->reflection.dirty = false;
  1141. } else {
  1142. if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
  1143. sky->reflection.create_reflection_importance_sample(storage, sky_use_cubemap_array, 10, sky->processing_layer, sky_ggx_samples_quality);
  1144. if (sky_use_cubemap_array) {
  1145. sky->reflection.update_reflection_mipmaps(storage, sky->processing_layer, sky->processing_layer + 1);
  1146. }
  1147. sky->processing_layer++;
  1148. }
  1149. }
  1150. }
  1151. void RendererSceneSkyRD::draw(RendererSceneEnvironmentRD *p_env, bool p_can_continue_color, bool p_can_continue_depth, RID p_fb, uint32_t p_view_count, const CameraMatrix *p_projections, const Transform3D &p_transform, double p_time) {
  1152. ERR_FAIL_COND(!p_env);
  1153. ERR_FAIL_COND(p_view_count == 0);
  1154. ERR_FAIL_COND(p_view_count > RendererSceneRender::MAX_RENDER_VIEWS);
  1155. Sky *sky = get_sky(p_env->sky);
  1156. ERR_FAIL_COND(!sky);
  1157. SkyMaterialData *material = nullptr;
  1158. RID sky_material;
  1159. RS::EnvironmentBG background = p_env->background;
  1160. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1161. ERR_FAIL_COND(!sky);
  1162. sky_material = sky_get_material(p_env->sky);
  1163. if (sky_material.is_valid()) {
  1164. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1165. if (!material || !material->shader_data->valid) {
  1166. material = nullptr;
  1167. }
  1168. }
  1169. if (!material) {
  1170. sky_material = sky_shader.default_material;
  1171. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1172. }
  1173. }
  1174. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1175. sky_material = sky_scene_state.fog_material;
  1176. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1177. }
  1178. ERR_FAIL_COND(!material);
  1179. SkyShaderData *shader_data = material->shader_data;
  1180. ERR_FAIL_COND(!shader_data);
  1181. Basis sky_transform = p_env->sky_orientation;
  1182. sky_transform.invert();
  1183. float multiplier = p_env->bg_energy;
  1184. float custom_fov = p_env->sky_custom_fov;
  1185. // Camera
  1186. CameraMatrix camera;
  1187. uint32_t view_count = p_view_count;
  1188. const CameraMatrix *projections = p_projections;
  1189. if (custom_fov) {
  1190. // With custom fov we don't support stereo...
  1191. float near_plane = p_projections[0].get_z_near();
  1192. float far_plane = p_projections[0].get_z_far();
  1193. float aspect = p_projections[0].get_aspect();
  1194. camera.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1195. view_count = 1;
  1196. projections = &camera;
  1197. }
  1198. sky_transform = p_transform.basis * sky_transform;
  1199. if (shader_data->uses_quarter_res) {
  1200. PipelineCacheRD *pipeline = &shader_data->pipelines[view_count > 1 ? SKY_VERSION_QUARTER_RES_MULTIVIEW : SKY_VERSION_QUARTER_RES];
  1201. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_QUARTER_RES, sky_shader.default_shader_rd);
  1202. Vector<Color> clear_colors;
  1203. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1204. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(sky->quarter_res_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1205. _render_sky(draw_list, p_time, sky->quarter_res_framebuffer, pipeline, material->uniform_set, texture_uniform_set, view_count, projections, sky_transform, multiplier, p_transform.origin, 1.0);
  1206. RD::get_singleton()->draw_list_end();
  1207. }
  1208. if (shader_data->uses_half_res) {
  1209. PipelineCacheRD *pipeline = &shader_data->pipelines[view_count > 1 ? SKY_VERSION_HALF_RES_MULTIVIEW : SKY_VERSION_HALF_RES];
  1210. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_HALF_RES, sky_shader.default_shader_rd);
  1211. Vector<Color> clear_colors;
  1212. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1213. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(sky->half_res_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1214. _render_sky(draw_list, p_time, sky->half_res_framebuffer, pipeline, material->uniform_set, texture_uniform_set, view_count, projections, sky_transform, multiplier, p_transform.origin, 1.0);
  1215. RD::get_singleton()->draw_list_end();
  1216. }
  1217. PipelineCacheRD *pipeline = &shader_data->pipelines[view_count > 1 ? SKY_VERSION_BACKGROUND_MULTIVIEW : SKY_VERSION_BACKGROUND];
  1218. RID texture_uniform_set;
  1219. if (sky) {
  1220. texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_BACKGROUND, sky_shader.default_shader_rd);
  1221. } else {
  1222. texture_uniform_set = sky_scene_state.fog_only_texture_uniform_set;
  1223. }
  1224. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_fb, RD::INITIAL_ACTION_CONTINUE, p_can_continue_color ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CONTINUE, p_can_continue_depth ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ);
  1225. _render_sky(draw_list, p_time, p_fb, pipeline, material->uniform_set, texture_uniform_set, view_count, projections, sky_transform, multiplier, p_transform.origin, 1.0);
  1226. RD::get_singleton()->draw_list_end();
  1227. }
  1228. void RendererSceneSkyRD::update_res_buffers(RendererSceneEnvironmentRD *p_env, uint32_t p_view_count, const CameraMatrix *p_projections, const Transform3D &p_transform, double p_time, float p_luminance_multiplier) {
  1229. ERR_FAIL_COND(!p_env);
  1230. ERR_FAIL_COND(p_view_count == 0);
  1231. ERR_FAIL_COND(p_view_count > RendererSceneRender::MAX_RENDER_VIEWS);
  1232. Sky *sky = get_sky(p_env->sky);
  1233. ERR_FAIL_COND(!sky);
  1234. SkyMaterialData *material = nullptr;
  1235. RID sky_material;
  1236. RS::EnvironmentBG background = p_env->background;
  1237. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1238. ERR_FAIL_COND(!sky);
  1239. sky_material = sky_get_material(p_env->sky);
  1240. if (sky_material.is_valid()) {
  1241. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1242. if (!material || !material->shader_data->valid) {
  1243. material = nullptr;
  1244. }
  1245. }
  1246. if (!material) {
  1247. sky_material = sky_shader.default_material;
  1248. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1249. }
  1250. }
  1251. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1252. sky_material = sky_scene_state.fog_material;
  1253. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1254. }
  1255. ERR_FAIL_COND(!material);
  1256. SkyShaderData *shader_data = material->shader_data;
  1257. ERR_FAIL_COND(!shader_data);
  1258. Basis sky_transform = p_env->sky_orientation;
  1259. sky_transform.invert();
  1260. float multiplier = p_env->bg_energy;
  1261. float custom_fov = p_env->sky_custom_fov;
  1262. // Camera
  1263. CameraMatrix camera;
  1264. uint32_t view_count = p_view_count;
  1265. const CameraMatrix *projections = p_projections;
  1266. if (custom_fov) {
  1267. // With custom fov we don't support stereo...
  1268. float near_plane = p_projections[0].get_z_near();
  1269. float far_plane = p_projections[0].get_z_far();
  1270. float aspect = p_projections[0].get_aspect();
  1271. camera.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1272. view_count = 1;
  1273. projections = &camera;
  1274. }
  1275. sky_transform = p_transform.basis * sky_transform;
  1276. if (shader_data->uses_quarter_res) {
  1277. PipelineCacheRD *pipeline = &shader_data->pipelines[view_count > 1 ? SKY_VERSION_QUARTER_RES_MULTIVIEW : SKY_VERSION_QUARTER_RES];
  1278. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_QUARTER_RES, sky_shader.default_shader_rd);
  1279. Vector<Color> clear_colors;
  1280. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1281. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(sky->quarter_res_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1282. _render_sky(draw_list, p_time, sky->quarter_res_framebuffer, pipeline, material->uniform_set, texture_uniform_set, view_count, projections, sky_transform, multiplier, p_transform.origin, p_luminance_multiplier);
  1283. RD::get_singleton()->draw_list_end();
  1284. }
  1285. if (shader_data->uses_half_res) {
  1286. PipelineCacheRD *pipeline = &shader_data->pipelines[view_count > 1 ? SKY_VERSION_HALF_RES_MULTIVIEW : SKY_VERSION_HALF_RES];
  1287. RID texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_HALF_RES, sky_shader.default_shader_rd);
  1288. Vector<Color> clear_colors;
  1289. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1290. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(sky->half_res_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1291. _render_sky(draw_list, p_time, sky->half_res_framebuffer, pipeline, material->uniform_set, texture_uniform_set, view_count, projections, sky_transform, multiplier, p_transform.origin, p_luminance_multiplier);
  1292. RD::get_singleton()->draw_list_end();
  1293. }
  1294. }
  1295. void RendererSceneSkyRD::draw(RD::DrawListID p_draw_list, RendererSceneEnvironmentRD *p_env, RID p_fb, uint32_t p_view_count, const CameraMatrix *p_projections, const Transform3D &p_transform, double p_time, float p_luminance_multiplier) {
  1296. ERR_FAIL_COND(!p_env);
  1297. ERR_FAIL_COND(p_view_count == 0);
  1298. ERR_FAIL_COND(p_view_count > RendererSceneRender::MAX_RENDER_VIEWS);
  1299. Sky *sky = get_sky(p_env->sky);
  1300. ERR_FAIL_COND(!sky);
  1301. SkyMaterialData *material = nullptr;
  1302. RID sky_material;
  1303. RS::EnvironmentBG background = p_env->background;
  1304. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1305. ERR_FAIL_COND(!sky);
  1306. sky_material = sky_get_material(p_env->sky);
  1307. if (sky_material.is_valid()) {
  1308. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1309. if (!material || !material->shader_data->valid) {
  1310. material = nullptr;
  1311. }
  1312. }
  1313. if (!material) {
  1314. sky_material = sky_shader.default_material;
  1315. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1316. }
  1317. }
  1318. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1319. sky_material = sky_scene_state.fog_material;
  1320. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1321. }
  1322. ERR_FAIL_COND(!material);
  1323. SkyShaderData *shader_data = material->shader_data;
  1324. ERR_FAIL_COND(!shader_data);
  1325. Basis sky_transform = p_env->sky_orientation;
  1326. sky_transform.invert();
  1327. float multiplier = p_env->bg_energy;
  1328. float custom_fov = p_env->sky_custom_fov;
  1329. // Camera
  1330. CameraMatrix camera;
  1331. uint32_t view_count = p_view_count;
  1332. const CameraMatrix *projections = p_projections;
  1333. if (custom_fov) {
  1334. // With custom fov we don't support stereo...
  1335. float near_plane = p_projections[0].get_z_near();
  1336. float far_plane = p_projections[0].get_z_far();
  1337. float aspect = p_projections[0].get_aspect();
  1338. camera.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1339. view_count = 1;
  1340. projections = &camera;
  1341. }
  1342. sky_transform = p_transform.basis * sky_transform;
  1343. PipelineCacheRD *pipeline = &shader_data->pipelines[view_count > 1 ? SKY_VERSION_BACKGROUND_MULTIVIEW : SKY_VERSION_BACKGROUND];
  1344. RID texture_uniform_set;
  1345. if (sky) {
  1346. texture_uniform_set = sky->get_textures(storage, SKY_TEXTURE_SET_BACKGROUND, sky_shader.default_shader_rd);
  1347. } else {
  1348. texture_uniform_set = sky_scene_state.fog_only_texture_uniform_set;
  1349. }
  1350. _render_sky(p_draw_list, p_time, p_fb, pipeline, material->uniform_set, texture_uniform_set, view_count, projections, sky_transform, multiplier, p_transform.origin, p_luminance_multiplier);
  1351. }
  1352. void RendererSceneSkyRD::invalidate_sky(Sky *p_sky) {
  1353. if (!p_sky->dirty) {
  1354. p_sky->dirty = true;
  1355. p_sky->dirty_list = dirty_sky_list;
  1356. dirty_sky_list = p_sky;
  1357. }
  1358. }
  1359. void RendererSceneSkyRD::update_dirty_skys() {
  1360. Sky *sky = dirty_sky_list;
  1361. while (sky) {
  1362. bool texture_set_dirty = false;
  1363. //update sky configuration if texture is missing
  1364. if (sky->radiance.is_null()) {
  1365. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  1366. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  1367. int layers = roughness_layers;
  1368. if (sky->mode == RS::SKY_MODE_REALTIME) {
  1369. layers = 8;
  1370. if (roughness_layers != 8) {
  1371. WARN_PRINT("When using REALTIME skies, roughness_layers should be set to 8 in the project settings for best quality reflections");
  1372. }
  1373. }
  1374. if (sky_use_cubemap_array) {
  1375. //array (higher quality, 6 times more memory)
  1376. RD::TextureFormat tf;
  1377. tf.array_layers = layers * 6;
  1378. tf.format = texture_format;
  1379. tf.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  1380. tf.mipmaps = mipmaps;
  1381. tf.width = w;
  1382. tf.height = h;
  1383. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1384. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1385. sky->reflection.update_reflection_data(storage, sky->radiance_size, mipmaps, true, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME, roughness_layers, texture_format);
  1386. } else {
  1387. //regular cubemap, lower quality (aliasing, less memory)
  1388. RD::TextureFormat tf;
  1389. tf.array_layers = 6;
  1390. tf.format = texture_format;
  1391. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  1392. tf.mipmaps = MIN(mipmaps, layers);
  1393. tf.width = w;
  1394. tf.height = h;
  1395. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1396. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1397. sky->reflection.update_reflection_data(storage, sky->radiance_size, MIN(mipmaps, layers), false, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME, roughness_layers, texture_format);
  1398. }
  1399. texture_set_dirty = true;
  1400. }
  1401. // Create subpass buffers if they haven't been created already
  1402. if (sky->half_res_pass.is_null() && !RD::get_singleton()->texture_is_valid(sky->half_res_pass) && sky->screen_size.x >= 4 && sky->screen_size.y >= 4) {
  1403. RD::TextureFormat tformat;
  1404. tformat.format = texture_format;
  1405. tformat.width = sky->screen_size.x / 2;
  1406. tformat.height = sky->screen_size.y / 2;
  1407. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1408. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  1409. sky->half_res_pass = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1410. Vector<RID> texs;
  1411. texs.push_back(sky->half_res_pass);
  1412. sky->half_res_framebuffer = RD::get_singleton()->framebuffer_create(texs);
  1413. texture_set_dirty = true;
  1414. }
  1415. if (sky->quarter_res_pass.is_null() && !RD::get_singleton()->texture_is_valid(sky->quarter_res_pass) && sky->screen_size.x >= 4 && sky->screen_size.y >= 4) {
  1416. RD::TextureFormat tformat;
  1417. tformat.format = texture_format;
  1418. tformat.width = sky->screen_size.x / 4;
  1419. tformat.height = sky->screen_size.y / 4;
  1420. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1421. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  1422. sky->quarter_res_pass = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1423. Vector<RID> texs;
  1424. texs.push_back(sky->quarter_res_pass);
  1425. sky->quarter_res_framebuffer = RD::get_singleton()->framebuffer_create(texs);
  1426. texture_set_dirty = true;
  1427. }
  1428. if (texture_set_dirty) {
  1429. for (int i = 0; i < SKY_TEXTURE_SET_MAX; i++) {
  1430. if (sky->texture_uniform_sets[i].is_valid() && RD::get_singleton()->uniform_set_is_valid(sky->texture_uniform_sets[i])) {
  1431. RD::get_singleton()->free(sky->texture_uniform_sets[i]);
  1432. sky->texture_uniform_sets[i] = RID();
  1433. }
  1434. }
  1435. }
  1436. sky->reflection.dirty = true;
  1437. sky->processing_layer = 0;
  1438. Sky *next = sky->dirty_list;
  1439. sky->dirty_list = nullptr;
  1440. sky->dirty = false;
  1441. sky = next;
  1442. }
  1443. dirty_sky_list = nullptr;
  1444. }
  1445. RID RendererSceneSkyRD::sky_get_material(RID p_sky) const {
  1446. Sky *sky = get_sky(p_sky);
  1447. ERR_FAIL_COND_V(!sky, RID());
  1448. return sky->material;
  1449. }
  1450. RID RendererSceneSkyRD::allocate_sky_rid() {
  1451. return sky_owner.allocate_rid();
  1452. }
  1453. void RendererSceneSkyRD::initialize_sky_rid(RID p_rid) {
  1454. sky_owner.initialize_rid(p_rid, Sky());
  1455. }
  1456. RendererSceneSkyRD::Sky *RendererSceneSkyRD::get_sky(RID p_sky) const {
  1457. return sky_owner.get_or_null(p_sky);
  1458. }
  1459. void RendererSceneSkyRD::free_sky(RID p_sky) {
  1460. Sky *sky = get_sky(p_sky);
  1461. ERR_FAIL_COND(!sky);
  1462. sky->free(storage);
  1463. sky_owner.free(p_sky);
  1464. }
  1465. void RendererSceneSkyRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  1466. Sky *sky = get_sky(p_sky);
  1467. ERR_FAIL_COND(!sky);
  1468. if (sky->set_radiance_size(p_radiance_size)) {
  1469. invalidate_sky(sky);
  1470. }
  1471. }
  1472. void RendererSceneSkyRD::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  1473. Sky *sky = get_sky(p_sky);
  1474. ERR_FAIL_COND(!sky);
  1475. if (sky->set_mode(p_mode)) {
  1476. invalidate_sky(sky);
  1477. }
  1478. }
  1479. void RendererSceneSkyRD::sky_set_material(RID p_sky, RID p_material) {
  1480. Sky *sky = get_sky(p_sky);
  1481. ERR_FAIL_COND(!sky);
  1482. if (sky->set_material(p_material)) {
  1483. invalidate_sky(sky);
  1484. }
  1485. }
  1486. Ref<Image> RendererSceneSkyRD::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  1487. Sky *sky = get_sky(p_sky);
  1488. ERR_FAIL_COND_V(!sky, Ref<Image>());
  1489. update_dirty_skys();
  1490. return sky->bake_panorama(storage, p_energy, p_bake_irradiance ? roughness_layers : 0, p_size);
  1491. }
  1492. RID RendererSceneSkyRD::sky_get_radiance_texture_rd(RID p_sky) const {
  1493. Sky *sky = get_sky(p_sky);
  1494. ERR_FAIL_COND_V(!sky, RID());
  1495. return sky->radiance;
  1496. }