sky.cpp 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /**************************************************************************/
  2. /* sky.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "sky.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/math/math_defs.h"
  33. #include "servers/rendering/renderer_rd/effects/copy_effects.h"
  34. #include "servers/rendering/renderer_rd/framebuffer_cache_rd.h"
  35. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  36. #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
  37. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  38. #include "servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
  39. #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
  40. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  41. #include "servers/rendering/rendering_server_default.h"
  42. #include "servers/rendering/rendering_server_globals.h"
  43. using namespace RendererRD;
  44. #define RB_SCOPE_SKY SNAME("sky_buffers")
  45. #define RB_HALF_TEXTURE SNAME("half_texture")
  46. #define RB_QUARTER_TEXTURE SNAME("quarter_texture")
  47. ////////////////////////////////////////////////////////////////////////////////
  48. // SKY SHADER
  49. void SkyRD::SkyShaderData::set_code(const String &p_code) {
  50. //compile
  51. code = p_code;
  52. valid = false;
  53. ubo_size = 0;
  54. uniforms.clear();
  55. if (code.is_empty()) {
  56. return; //just invalid, but no error
  57. }
  58. ShaderCompiler::GeneratedCode gen_code;
  59. ShaderCompiler::IdentifierActions actions;
  60. actions.entry_point_stages["sky"] = ShaderCompiler::STAGE_FRAGMENT;
  61. uses_time = false;
  62. uses_half_res = false;
  63. uses_quarter_res = false;
  64. uses_position = false;
  65. uses_light = false;
  66. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  67. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  68. actions.usage_flag_pointers["TIME"] = &uses_time;
  69. actions.usage_flag_pointers["POSITION"] = &uses_position;
  70. actions.usage_flag_pointers["LIGHT0_ENABLED"] = &uses_light;
  71. actions.usage_flag_pointers["LIGHT0_ENERGY"] = &uses_light;
  72. actions.usage_flag_pointers["LIGHT0_DIRECTION"] = &uses_light;
  73. actions.usage_flag_pointers["LIGHT0_COLOR"] = &uses_light;
  74. actions.usage_flag_pointers["LIGHT0_SIZE"] = &uses_light;
  75. actions.usage_flag_pointers["LIGHT1_ENABLED"] = &uses_light;
  76. actions.usage_flag_pointers["LIGHT1_ENERGY"] = &uses_light;
  77. actions.usage_flag_pointers["LIGHT1_DIRECTION"] = &uses_light;
  78. actions.usage_flag_pointers["LIGHT1_COLOR"] = &uses_light;
  79. actions.usage_flag_pointers["LIGHT1_SIZE"] = &uses_light;
  80. actions.usage_flag_pointers["LIGHT2_ENABLED"] = &uses_light;
  81. actions.usage_flag_pointers["LIGHT2_ENERGY"] = &uses_light;
  82. actions.usage_flag_pointers["LIGHT2_DIRECTION"] = &uses_light;
  83. actions.usage_flag_pointers["LIGHT2_COLOR"] = &uses_light;
  84. actions.usage_flag_pointers["LIGHT2_SIZE"] = &uses_light;
  85. actions.usage_flag_pointers["LIGHT3_ENABLED"] = &uses_light;
  86. actions.usage_flag_pointers["LIGHT3_ENERGY"] = &uses_light;
  87. actions.usage_flag_pointers["LIGHT3_DIRECTION"] = &uses_light;
  88. actions.usage_flag_pointers["LIGHT3_COLOR"] = &uses_light;
  89. actions.usage_flag_pointers["LIGHT3_SIZE"] = &uses_light;
  90. actions.uniforms = &uniforms;
  91. // !BAS! Contemplate making `SkyShader sky` accessible from this struct or even part of this struct.
  92. RendererSceneRenderRD *scene_singleton = static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton);
  93. Error err = scene_singleton->sky.sky_shader.compiler.compile(RS::SHADER_SKY, code, &actions, path, gen_code);
  94. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  95. if (version.is_null()) {
  96. version = scene_singleton->sky.sky_shader.shader.version_create();
  97. }
  98. #if 0
  99. print_line("**compiling shader:");
  100. print_line("**defines:\n");
  101. for (int i = 0; i < gen_code.defines.size(); i++) {
  102. print_line(gen_code.defines[i]);
  103. }
  104. HashMap<String, String>::Iterator el = gen_code.code.begin();
  105. while (el) {
  106. print_line("\n**code " + el->key + ":\n" + el->value);
  107. ++el;
  108. }
  109. print_line("\n**uniforms:\n" + gen_code.uniforms);
  110. print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]);
  111. print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]);
  112. #endif
  113. scene_singleton->sky.sky_shader.shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines);
  114. ERR_FAIL_COND(!scene_singleton->sky.sky_shader.shader.version_is_valid(version));
  115. ubo_size = gen_code.uniform_total_size;
  116. ubo_offsets = gen_code.uniform_offsets;
  117. texture_uniforms = gen_code.texture_uniforms;
  118. //update pipelines
  119. for (int i = 0; i < SKY_VERSION_MAX; i++) {
  120. RD::PipelineDepthStencilState depth_stencil_state;
  121. depth_stencil_state.enable_depth_test = true;
  122. depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_GREATER_OR_EQUAL;
  123. if (scene_singleton->sky.sky_shader.shader.is_variant_enabled(i)) {
  124. RID shader_variant = scene_singleton->sky.sky_shader.shader.version_get_shader(version, i);
  125. pipelines[i].setup(shader_variant, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), depth_stencil_state, RD::PipelineColorBlendState::create_disabled(), 0);
  126. } else {
  127. pipelines[i].clear();
  128. }
  129. }
  130. valid = true;
  131. }
  132. bool SkyRD::SkyShaderData::is_animated() const {
  133. return false;
  134. }
  135. bool SkyRD::SkyShaderData::casts_shadows() const {
  136. return false;
  137. }
  138. RS::ShaderNativeSourceCode SkyRD::SkyShaderData::get_native_source_code() const {
  139. RendererSceneRenderRD *scene_singleton = static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton);
  140. return scene_singleton->sky.sky_shader.shader.version_get_native_source_code(version);
  141. }
  142. SkyRD::SkyShaderData::~SkyShaderData() {
  143. RendererSceneRenderRD *scene_singleton = static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton);
  144. ERR_FAIL_NULL(scene_singleton);
  145. //pipeline variants will clear themselves if shader is gone
  146. if (version.is_valid()) {
  147. scene_singleton->sky.sky_shader.shader.version_free(version);
  148. }
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////
  151. // Sky material
  152. bool SkyRD::SkyMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  153. RendererSceneRenderRD *scene_singleton = static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton);
  154. uniform_set_updated = true;
  155. 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, true, true);
  156. }
  157. SkyRD::SkyMaterialData::~SkyMaterialData() {
  158. free_parameters_uniform_set(uniform_set);
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////
  161. // Render sky
  162. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  163. p_array[0] = p_basis.rows[0][0];
  164. p_array[1] = p_basis.rows[1][0];
  165. p_array[2] = p_basis.rows[2][0];
  166. p_array[3] = 0;
  167. p_array[4] = p_basis.rows[0][1];
  168. p_array[5] = p_basis.rows[1][1];
  169. p_array[6] = p_basis.rows[2][1];
  170. p_array[7] = 0;
  171. p_array[8] = p_basis.rows[0][2];
  172. p_array[9] = p_basis.rows[1][2];
  173. p_array[10] = p_basis.rows[2][2];
  174. p_array[11] = 0;
  175. }
  176. void SkyRD::_render_sky(RD::DrawListID p_list, float p_time, RID p_fb, PipelineCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, const Projection &p_projection, const Basis &p_orientation, const Vector3 &p_position, float p_luminance_multiplier) {
  177. SkyPushConstant sky_push_constant;
  178. memset(&sky_push_constant, 0, sizeof(SkyPushConstant));
  179. // We only need key components of our projection matrix
  180. sky_push_constant.projection[0] = p_projection.columns[2][0];
  181. sky_push_constant.projection[1] = p_projection.columns[0][0];
  182. sky_push_constant.projection[2] = p_projection.columns[2][1];
  183. sky_push_constant.projection[3] = p_projection.columns[1][1];
  184. sky_push_constant.position[0] = p_position.x;
  185. sky_push_constant.position[1] = p_position.y;
  186. sky_push_constant.position[2] = p_position.z;
  187. sky_push_constant.time = p_time;
  188. sky_push_constant.luminance_multiplier = p_luminance_multiplier;
  189. store_transform_3x3(p_orientation, sky_push_constant.orientation);
  190. RenderingDevice::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(p_fb);
  191. RD::DrawListID draw_list = p_list;
  192. 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()));
  193. // Update uniform sets.
  194. {
  195. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, sky_scene_state.uniform_set, SKY_SET_UNIFORMS);
  196. if (p_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(p_uniform_set)) { // Material may not have a uniform set.
  197. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_uniform_set, SKY_SET_MATERIAL);
  198. }
  199. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_texture_set, SKY_SET_TEXTURES);
  200. // Fog uniform set can be invalidated before drawing, so validate at draw time
  201. if (sky_scene_state.fog_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_scene_state.fog_uniform_set)) {
  202. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, sky_scene_state.fog_uniform_set, SKY_SET_FOG);
  203. } else {
  204. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, sky_scene_state.default_fog_uniform_set, SKY_SET_FOG);
  205. }
  206. }
  207. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky_push_constant, sizeof(SkyPushConstant));
  208. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  209. }
  210. ////////////////////////////////////////////////////////////////////////////////
  211. // ReflectionData
  212. void SkyRD::ReflectionData::clear_reflection_data() {
  213. layers.clear();
  214. radiance_base_cubemap = RID();
  215. if (downsampled_radiance_cubemap.is_valid()) {
  216. RD::get_singleton()->free(downsampled_radiance_cubemap);
  217. }
  218. downsampled_radiance_cubemap = RID();
  219. downsampled_layer.mipmaps.clear();
  220. coefficient_buffer = RID();
  221. }
  222. void SkyRD::ReflectionData::update_reflection_data(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) {
  223. //recreate radiance and all data
  224. int mipmaps = p_mipmaps;
  225. uint32_t w = p_size, h = p_size;
  226. bool render_buffers_can_be_storage = RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage();
  227. if (p_use_array) {
  228. int num_layers = p_low_quality ? 8 : p_roughness_layers;
  229. for (int i = 0; i < num_layers; i++) {
  230. ReflectionData::Layer layer;
  231. uint32_t mmw = w;
  232. uint32_t mmh = h;
  233. layer.mipmaps.resize(mipmaps);
  234. layer.views.resize(mipmaps);
  235. for (int j = 0; j < mipmaps; j++) {
  236. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  237. mm.size.width = mmw;
  238. mm.size.height = mmh;
  239. for (int k = 0; k < 6; k++) {
  240. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6 + k, j);
  241. Vector<RID> fbtex;
  242. fbtex.push_back(mm.views[k]);
  243. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  244. }
  245. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6, j, 1, RD::TEXTURE_SLICE_CUBEMAP);
  246. mmw = MAX(1u, mmw >> 1);
  247. mmh = MAX(1u, mmh >> 1);
  248. }
  249. layers.push_back(layer);
  250. }
  251. } else {
  252. mipmaps = p_low_quality ? 8 : mipmaps;
  253. //regular cubemap, lower quality (aliasing, less memory)
  254. ReflectionData::Layer layer;
  255. uint32_t mmw = w;
  256. uint32_t mmh = h;
  257. layer.mipmaps.resize(mipmaps);
  258. layer.views.resize(mipmaps);
  259. for (int j = 0; j < mipmaps; j++) {
  260. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  261. mm.size.width = mmw;
  262. mm.size.height = mmh;
  263. for (int k = 0; k < 6; k++) {
  264. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + k, j);
  265. Vector<RID> fbtex;
  266. fbtex.push_back(mm.views[k]);
  267. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  268. }
  269. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, j, 1, RD::TEXTURE_SLICE_CUBEMAP);
  270. mmw = MAX(1u, mmw >> 1);
  271. mmh = MAX(1u, mmh >> 1);
  272. }
  273. layers.push_back(layer);
  274. }
  275. radiance_base_cubemap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, 0, 1, RD::TEXTURE_SLICE_CUBEMAP);
  276. RD::get_singleton()->set_resource_name(radiance_base_cubemap, "radiance base cubemap");
  277. RD::TextureFormat tf;
  278. tf.format = p_texture_format;
  279. tf.width = p_low_quality ? 64 : p_size >> 1; // Always 64x64 when using REALTIME.
  280. tf.height = p_low_quality ? 64 : p_size >> 1;
  281. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  282. tf.array_layers = 6;
  283. tf.mipmaps = p_low_quality ? 7 : mipmaps - 1;
  284. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  285. if (render_buffers_can_be_storage) {
  286. tf.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT;
  287. }
  288. downsampled_radiance_cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  289. RD::get_singleton()->set_resource_name(downsampled_radiance_cubemap, "downsampled radiance cubemap");
  290. {
  291. uint32_t mmw = tf.width;
  292. uint32_t mmh = tf.height;
  293. downsampled_layer.mipmaps.resize(tf.mipmaps);
  294. for (int j = 0; j < downsampled_layer.mipmaps.size(); j++) {
  295. ReflectionData::DownsampleLayer::Mipmap &mm = downsampled_layer.mipmaps.write[j];
  296. mm.size.width = mmw;
  297. mm.size.height = mmh;
  298. mm.view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), downsampled_radiance_cubemap, 0, j, 1, RD::TEXTURE_SLICE_CUBEMAP);
  299. RD::get_singleton()->set_resource_name(mm.view, "Downsampled Radiance Cubemap Mip " + itos(j) + " ");
  300. if (!render_buffers_can_be_storage) {
  301. // we need a framebuffer for each side of our cubemap
  302. for (int k = 0; k < 6; k++) {
  303. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), downsampled_radiance_cubemap, k, j);
  304. RD::get_singleton()->set_resource_name(mm.view, "Downsampled Radiance Cubemap Mip: " + itos(j) + " Face: " + itos(k) + " ");
  305. Vector<RID> fbtex;
  306. fbtex.push_back(mm.views[k]);
  307. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  308. }
  309. }
  310. mmw = MAX(1u, mmw >> 1);
  311. mmh = MAX(1u, mmh >> 1);
  312. }
  313. }
  314. }
  315. void SkyRD::ReflectionData::create_reflection_fast_filter(bool p_use_arrays) {
  316. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  317. ERR_FAIL_NULL_MSG(copy_effects, "Effects haven't been initialized");
  318. bool prefer_raster_effects = copy_effects->get_prefer_raster_effects();
  319. if (prefer_raster_effects) {
  320. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  321. for (int k = 0; k < 6; k++) {
  322. copy_effects->cubemap_downsample_raster(radiance_base_cubemap, downsampled_layer.mipmaps[0].framebuffers[k], k, downsampled_layer.mipmaps[0].size);
  323. }
  324. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  325. for (int k = 0; k < 6; k++) {
  326. copy_effects->cubemap_downsample_raster(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].framebuffers[k], k, downsampled_layer.mipmaps[i].size);
  327. }
  328. }
  329. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  330. if (p_use_arrays) {
  331. RD::get_singleton()->draw_command_begin_label("filter radiance map into array heads");
  332. for (int i = 0; i < layers.size(); i++) {
  333. for (int k = 0; k < 6; k++) {
  334. copy_effects->cubemap_filter_raster(downsampled_radiance_cubemap, layers[i].mipmaps[0].framebuffers[k], k, i);
  335. }
  336. }
  337. } else {
  338. RD::get_singleton()->draw_command_begin_label("filter radiance map into mipmaps directly");
  339. for (int j = 0; j < layers[0].mipmaps.size(); j++) {
  340. for (int k = 0; k < 6; k++) {
  341. copy_effects->cubemap_filter_raster(downsampled_radiance_cubemap, layers[0].mipmaps[j].framebuffers[k], k, j);
  342. }
  343. }
  344. }
  345. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  346. } else {
  347. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  348. copy_effects->cubemap_downsample(radiance_base_cubemap, downsampled_layer.mipmaps[0].view, downsampled_layer.mipmaps[0].size);
  349. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  350. copy_effects->cubemap_downsample(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].view, downsampled_layer.mipmaps[i].size);
  351. }
  352. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  353. Vector<RID> views;
  354. if (p_use_arrays) {
  355. for (int i = 1; i < layers.size(); i++) {
  356. views.push_back(layers[i].views[0]);
  357. }
  358. } else {
  359. for (int i = 1; i < layers[0].views.size(); i++) {
  360. views.push_back(layers[0].views[i]);
  361. }
  362. }
  363. RD::get_singleton()->draw_command_begin_label("Fast filter radiance");
  364. copy_effects->cubemap_filter(downsampled_radiance_cubemap, views, p_use_arrays);
  365. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  366. }
  367. }
  368. void SkyRD::ReflectionData::create_reflection_importance_sample(bool p_use_arrays, int p_cube_side, int p_base_layer, uint32_t p_sky_ggx_samples_quality) {
  369. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  370. ERR_FAIL_NULL_MSG(copy_effects, "Effects haven't been initialized");
  371. bool prefer_raster_effects = copy_effects->get_prefer_raster_effects();
  372. if (prefer_raster_effects) {
  373. if (p_base_layer == 1) {
  374. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  375. for (int k = 0; k < 6; k++) {
  376. copy_effects->cubemap_downsample_raster(radiance_base_cubemap, downsampled_layer.mipmaps[0].framebuffers[k], k, downsampled_layer.mipmaps[0].size);
  377. }
  378. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  379. for (int k = 0; k < 6; k++) {
  380. copy_effects->cubemap_downsample_raster(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].framebuffers[k], k, downsampled_layer.mipmaps[i].size);
  381. }
  382. }
  383. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  384. }
  385. RD::get_singleton()->draw_command_begin_label("High Quality filter radiance");
  386. if (p_use_arrays) {
  387. for (int k = 0; k < 6; k++) {
  388. copy_effects->cubemap_roughness_raster(
  389. downsampled_radiance_cubemap,
  390. layers[p_base_layer].mipmaps[0].framebuffers[k],
  391. k,
  392. p_sky_ggx_samples_quality,
  393. float(p_base_layer) / (layers.size() - 1.0),
  394. layers[p_base_layer].mipmaps[0].size.x);
  395. }
  396. } else {
  397. for (int k = 0; k < 6; k++) {
  398. copy_effects->cubemap_roughness_raster(
  399. downsampled_radiance_cubemap,
  400. layers[0].mipmaps[p_base_layer].framebuffers[k],
  401. k,
  402. p_sky_ggx_samples_quality,
  403. float(p_base_layer) / (layers[0].mipmaps.size() - 1.0),
  404. layers[0].mipmaps[p_base_layer].size.x);
  405. }
  406. }
  407. } else {
  408. if (p_base_layer == 1) {
  409. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  410. copy_effects->cubemap_downsample(radiance_base_cubemap, downsampled_layer.mipmaps[0].view, downsampled_layer.mipmaps[0].size);
  411. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  412. copy_effects->cubemap_downsample(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].view, downsampled_layer.mipmaps[i].size);
  413. }
  414. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  415. }
  416. RD::get_singleton()->draw_command_begin_label("High Quality filter radiance");
  417. if (p_use_arrays) {
  418. copy_effects->cubemap_roughness(downsampled_radiance_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);
  419. } else {
  420. copy_effects->cubemap_roughness(
  421. downsampled_radiance_cubemap,
  422. layers[0].views[p_base_layer],
  423. p_cube_side,
  424. p_sky_ggx_samples_quality,
  425. float(p_base_layer) / (layers[0].mipmaps.size() - 1.0),
  426. layers[0].mipmaps[p_base_layer].size.x);
  427. }
  428. }
  429. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  430. }
  431. void SkyRD::ReflectionData::update_reflection_mipmaps(int p_start, int p_end) {
  432. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  433. ERR_FAIL_NULL_MSG(copy_effects, "Effects haven't been initialized");
  434. bool prefer_raster_effects = copy_effects->get_prefer_raster_effects();
  435. RD::get_singleton()->draw_command_begin_label("Update Radiance Cubemap Array Mipmaps");
  436. for (int i = p_start; i < p_end; i++) {
  437. for (int j = 0; j < layers[i].views.size() - 1; j++) {
  438. RID view = layers[i].views[j];
  439. Size2i size = layers[i].mipmaps[j + 1].size;
  440. if (prefer_raster_effects) {
  441. for (int k = 0; k < 6; k++) {
  442. RID framebuffer = layers[i].mipmaps[j + 1].framebuffers[k];
  443. copy_effects->cubemap_downsample_raster(view, framebuffer, k, size);
  444. }
  445. } else {
  446. RID texture = layers[i].views[j + 1];
  447. copy_effects->cubemap_downsample(view, texture, size);
  448. }
  449. }
  450. }
  451. RD::get_singleton()->draw_command_end_label();
  452. }
  453. ////////////////////////////////////////////////////////////////////////////////
  454. // SkyRD::Sky
  455. void SkyRD::Sky::free() {
  456. if (radiance.is_valid()) {
  457. RD::get_singleton()->free(radiance);
  458. radiance = RID();
  459. }
  460. reflection.clear_reflection_data();
  461. if (uniform_buffer.is_valid()) {
  462. RD::get_singleton()->free(uniform_buffer);
  463. uniform_buffer = RID();
  464. }
  465. if (material.is_valid()) {
  466. material = RID();
  467. }
  468. }
  469. RID SkyRD::Sky::get_textures(SkyTextureSetVersion p_version, RID p_default_shader_rd, Ref<RenderSceneBuffersRD> p_render_buffers) {
  470. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  471. Vector<RD::Uniform> uniforms;
  472. {
  473. RD::Uniform u;
  474. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  475. u.binding = 0;
  476. if (radiance.is_valid() && p_version <= SKY_TEXTURE_SET_QUARTER_RES) {
  477. u.append_id(radiance);
  478. } else {
  479. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  480. }
  481. uniforms.push_back(u);
  482. }
  483. {
  484. RD::Uniform u;
  485. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  486. u.binding = 1; // half res
  487. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  488. if (reflection.layers.size() && reflection.layers[0].views.size() >= 2 && reflection.layers[0].views[1].is_valid() && p_version != SKY_TEXTURE_SET_CUBEMAP_HALF_RES) {
  489. u.append_id(reflection.layers[0].views[1]);
  490. } else {
  491. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  492. }
  493. } else {
  494. RID half_texture = p_render_buffers->has_texture(RB_SCOPE_SKY, RB_HALF_TEXTURE) ? p_render_buffers->get_texture(RB_SCOPE_SKY, RB_HALF_TEXTURE) : RID();
  495. if (half_texture.is_valid() && p_version != SKY_TEXTURE_SET_HALF_RES) {
  496. u.append_id(half_texture);
  497. } else {
  498. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  499. }
  500. }
  501. uniforms.push_back(u);
  502. }
  503. {
  504. RD::Uniform u;
  505. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  506. u.binding = 2; // quarter res
  507. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  508. if (reflection.layers.size() && reflection.layers[0].views.size() >= 3 && reflection.layers[0].views[2].is_valid() && p_version != SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES) {
  509. u.append_id(reflection.layers[0].views[2]);
  510. } else {
  511. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  512. }
  513. } else {
  514. RID quarter_texture = p_render_buffers->has_texture(RB_SCOPE_SKY, RB_QUARTER_TEXTURE) ? p_render_buffers->get_texture(RB_SCOPE_SKY, RB_QUARTER_TEXTURE) : RID();
  515. if (quarter_texture.is_valid() && p_version != SKY_TEXTURE_SET_QUARTER_RES) {
  516. u.append_id(quarter_texture);
  517. } else {
  518. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  519. }
  520. }
  521. uniforms.push_back(u);
  522. }
  523. return UniformSetCacheRD::get_singleton()->get_cache_vec(p_default_shader_rd, SKY_SET_TEXTURES, uniforms);
  524. }
  525. bool SkyRD::Sky::set_radiance_size(int p_radiance_size) {
  526. ERR_FAIL_COND_V(p_radiance_size < 32 || p_radiance_size > 2048, false);
  527. if (radiance_size == p_radiance_size) {
  528. return false;
  529. }
  530. radiance_size = p_radiance_size;
  531. if (mode == RS::SKY_MODE_REALTIME && radiance_size != 256) {
  532. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  533. radiance_size = 256;
  534. }
  535. if (radiance.is_valid()) {
  536. RD::get_singleton()->free(radiance);
  537. radiance = RID();
  538. }
  539. reflection.clear_reflection_data();
  540. return true;
  541. }
  542. bool SkyRD::Sky::set_mode(RS::SkyMode p_mode) {
  543. if (mode == p_mode) {
  544. return false;
  545. }
  546. mode = p_mode;
  547. if (mode == RS::SKY_MODE_REALTIME && radiance_size != 256) {
  548. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  549. set_radiance_size(256);
  550. }
  551. if (radiance.is_valid()) {
  552. RD::get_singleton()->free(radiance);
  553. radiance = RID();
  554. }
  555. reflection.clear_reflection_data();
  556. return true;
  557. }
  558. bool SkyRD::Sky::set_material(RID p_material) {
  559. if (material == p_material) {
  560. return false;
  561. }
  562. material = p_material;
  563. return true;
  564. }
  565. Ref<Image> SkyRD::Sky::bake_panorama(float p_energy, int p_roughness_layers, const Size2i &p_size) {
  566. if (radiance.is_valid()) {
  567. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  568. RD::TextureFormat tf;
  569. tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; // Could be RGBA16
  570. tf.width = p_size.width;
  571. tf.height = p_size.height;
  572. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  573. RID rad_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  574. copy_effects->copy_cubemap_to_panorama(radiance, rad_tex, p_size, p_roughness_layers, reflection.layers.size() > 1);
  575. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rad_tex, 0);
  576. RD::get_singleton()->free(rad_tex);
  577. Ref<Image> img = Image::create_from_data(p_size.width, p_size.height, false, Image::FORMAT_RGBAF, data);
  578. for (int i = 0; i < p_size.width; i++) {
  579. for (int j = 0; j < p_size.height; j++) {
  580. Color c = img->get_pixel(i, j);
  581. c.r *= p_energy;
  582. c.g *= p_energy;
  583. c.b *= p_energy;
  584. img->set_pixel(i, j, c);
  585. }
  586. }
  587. return img;
  588. }
  589. return Ref<Image>();
  590. }
  591. ////////////////////////////////////////////////////////////////////////////////
  592. // SkyRD
  593. RendererRD::MaterialStorage::ShaderData *SkyRD::_create_sky_shader_func() {
  594. SkyShaderData *shader_data = memnew(SkyShaderData);
  595. return shader_data;
  596. }
  597. RendererRD::MaterialStorage::ShaderData *SkyRD::_create_sky_shader_funcs() {
  598. // !BAS! Why isn't _create_sky_shader_func not just static too?
  599. return static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton)->sky._create_sky_shader_func();
  600. };
  601. RendererRD::MaterialStorage::MaterialData *SkyRD::_create_sky_material_func(SkyShaderData *p_shader) {
  602. SkyMaterialData *material_data = memnew(SkyMaterialData);
  603. material_data->shader_data = p_shader;
  604. //update will happen later anyway so do nothing.
  605. return material_data;
  606. }
  607. RendererRD::MaterialStorage::MaterialData *SkyRD::_create_sky_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
  608. // !BAS! same here, we could just make _create_sky_material_func static?
  609. return static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton)->sky._create_sky_material_func(static_cast<SkyShaderData *>(p_shader));
  610. };
  611. SkyRD::SkyRD() {
  612. roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers");
  613. sky_ggx_samples_quality = GLOBAL_GET("rendering/reflections/sky_reflections/ggx_samples");
  614. sky_use_cubemap_array = GLOBAL_GET("rendering/reflections/sky_reflections/texture_array_reflections");
  615. }
  616. void SkyRD::init() {
  617. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  618. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  619. {
  620. // Start with the directional lights for the sky
  621. sky_scene_state.max_directional_lights = 4;
  622. uint32_t directional_light_buffer_size = sky_scene_state.max_directional_lights * sizeof(SkyDirectionalLightData);
  623. sky_scene_state.directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  624. sky_scene_state.last_frame_directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  625. sky_scene_state.last_frame_directional_light_count = sky_scene_state.max_directional_lights + 1;
  626. sky_scene_state.directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  627. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_scene_state.max_directional_lights) + "\n";
  628. defines += "\n#define SAMPLERS_BINDING_FIRST_INDEX " + itos(SAMPLERS_BINDING_FIRST_INDEX) + "\n";
  629. // Initialize sky
  630. Vector<String> sky_modes;
  631. sky_modes.push_back(""); // Full size
  632. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n"); // Half Res
  633. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n"); // Quarter res
  634. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n"); // Cubemap
  635. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_HALF_RES_PASS\n"); // Half Res Cubemap
  636. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_QUARTER_RES_PASS\n"); // Quarter res Cubemap
  637. sky_modes.push_back("\n#define USE_MULTIVIEW\n"); // Full size multiview
  638. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n#define USE_MULTIVIEW\n"); // Half Res multiview
  639. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n#define USE_MULTIVIEW\n"); // Quarter res multiview
  640. sky_shader.shader.initialize(sky_modes, defines);
  641. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  642. sky_shader.shader.set_variant_enabled(SKY_VERSION_BACKGROUND_MULTIVIEW, false);
  643. sky_shader.shader.set_variant_enabled(SKY_VERSION_HALF_RES_MULTIVIEW, false);
  644. sky_shader.shader.set_variant_enabled(SKY_VERSION_QUARTER_RES_MULTIVIEW, false);
  645. }
  646. }
  647. // register our shader funds
  648. material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_SKY, _create_sky_shader_funcs);
  649. material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_SKY, _create_sky_material_funcs);
  650. {
  651. ShaderCompiler::DefaultIdentifierActions actions;
  652. actions.renames["COLOR"] = "color";
  653. actions.renames["ALPHA"] = "alpha";
  654. actions.renames["EYEDIR"] = "cube_normal";
  655. actions.renames["POSITION"] = "params.position";
  656. actions.renames["SKY_COORDS"] = "panorama_coords";
  657. actions.renames["SCREEN_UV"] = "uv";
  658. actions.renames["FRAGCOORD"] = "gl_FragCoord";
  659. actions.renames["TIME"] = "params.time";
  660. actions.renames["PI"] = _MKSTR(Math_PI);
  661. actions.renames["TAU"] = _MKSTR(Math_TAU);
  662. actions.renames["E"] = _MKSTR(Math_E);
  663. actions.renames["HALF_RES_COLOR"] = "half_res_color";
  664. actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
  665. actions.renames["RADIANCE"] = "radiance";
  666. actions.renames["FOG"] = "custom_fog";
  667. actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
  668. actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
  669. actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
  670. actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
  671. actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
  672. actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
  673. actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
  674. actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
  675. actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
  676. actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
  677. actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
  678. actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
  679. actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
  680. actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
  681. actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
  682. actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
  683. actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
  684. actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
  685. actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";
  686. actions.renames["LIGHT3_SIZE"] = "directional_lights.data[3].color_size.w";
  687. actions.renames["AT_CUBEMAP_PASS"] = "AT_CUBEMAP_PASS";
  688. actions.renames["AT_HALF_RES_PASS"] = "AT_HALF_RES_PASS";
  689. actions.renames["AT_QUARTER_RES_PASS"] = "AT_QUARTER_RES_PASS";
  690. actions.custom_samplers["RADIANCE"] = "SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP";
  691. actions.usage_defines["HALF_RES_COLOR"] = "\n#define USES_HALF_RES_COLOR\n";
  692. actions.usage_defines["QUARTER_RES_COLOR"] = "\n#define USES_QUARTER_RES_COLOR\n";
  693. actions.render_mode_defines["disable_fog"] = "#define DISABLE_FOG\n";
  694. actions.render_mode_defines["use_debanding"] = "#define USE_DEBANDING\n";
  695. actions.base_texture_binding_index = 1;
  696. actions.texture_layout_set = 1;
  697. actions.base_uniform_string = "material.";
  698. actions.base_varying_index = 10;
  699. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  700. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  701. actions.global_buffer_array_variable = "global_shader_uniforms.data";
  702. sky_shader.compiler.initialize(actions);
  703. }
  704. {
  705. // default material and shader for sky shader
  706. sky_shader.default_shader = material_storage->shader_allocate();
  707. material_storage->shader_initialize(sky_shader.default_shader);
  708. material_storage->shader_set_code(sky_shader.default_shader, R"(
  709. // Default sky shader.
  710. shader_type sky;
  711. void sky() {
  712. COLOR = vec3(0.0);
  713. }
  714. )");
  715. sky_shader.default_material = material_storage->material_allocate();
  716. material_storage->material_initialize(sky_shader.default_material);
  717. material_storage->material_set_shader(sky_shader.default_material, sky_shader.default_shader);
  718. SkyMaterialData *md = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  719. sky_shader.default_shader_rd = sky_shader.shader.version_get_shader(md->shader_data->version, SKY_VERSION_BACKGROUND);
  720. sky_scene_state.uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SkySceneState::UBO));
  721. Vector<RD::Uniform> uniforms;
  722. {
  723. RD::Uniform u;
  724. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  725. u.binding = 1;
  726. u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer());
  727. uniforms.push_back(u);
  728. }
  729. {
  730. RD::Uniform u;
  731. u.binding = 2;
  732. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  733. u.append_id(sky_scene_state.uniform_buffer);
  734. uniforms.push_back(u);
  735. }
  736. {
  737. RD::Uniform u;
  738. u.binding = 3;
  739. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  740. u.append_id(sky_scene_state.directional_light_buffer);
  741. uniforms.push_back(u);
  742. }
  743. uniforms.append_array(material_storage->samplers_rd_get_default().get_uniforms(SAMPLERS_BINDING_FIRST_INDEX));
  744. sky_scene_state.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_UNIFORMS);
  745. }
  746. {
  747. Vector<RD::Uniform> uniforms;
  748. {
  749. RD::Uniform u;
  750. u.binding = 0;
  751. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  752. RID vfog = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
  753. u.append_id(vfog);
  754. uniforms.push_back(u);
  755. }
  756. sky_scene_state.default_fog_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_FOG);
  757. }
  758. {
  759. // Need defaults for using fog with clear color
  760. sky_scene_state.fog_shader = material_storage->shader_allocate();
  761. material_storage->shader_initialize(sky_scene_state.fog_shader);
  762. material_storage->shader_set_code(sky_scene_state.fog_shader, R"(
  763. // Default clear color sky shader.
  764. shader_type sky;
  765. uniform vec4 clear_color;
  766. void sky() {
  767. COLOR = clear_color.rgb;
  768. }
  769. )");
  770. sky_scene_state.fog_material = material_storage->material_allocate();
  771. material_storage->material_initialize(sky_scene_state.fog_material);
  772. material_storage->material_set_shader(sky_scene_state.fog_material, sky_scene_state.fog_shader);
  773. Vector<RD::Uniform> uniforms;
  774. {
  775. RD::Uniform u;
  776. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  777. u.binding = 0;
  778. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  779. uniforms.push_back(u);
  780. }
  781. {
  782. RD::Uniform u;
  783. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  784. u.binding = 1;
  785. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  786. uniforms.push_back(u);
  787. }
  788. {
  789. RD::Uniform u;
  790. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  791. u.binding = 2;
  792. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  793. uniforms.push_back(u);
  794. }
  795. sky_scene_state.fog_only_texture_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_TEXTURES);
  796. }
  797. }
  798. void SkyRD::set_texture_format(RD::DataFormat p_texture_format) {
  799. texture_format = p_texture_format;
  800. }
  801. SkyRD::~SkyRD() {
  802. // cleanup anything created in init...
  803. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  804. SkyMaterialData *md = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  805. sky_shader.shader.version_free(md->shader_data->version);
  806. RD::get_singleton()->free(sky_scene_state.directional_light_buffer);
  807. RD::get_singleton()->free(sky_scene_state.uniform_buffer);
  808. memdelete_arr(sky_scene_state.directional_lights);
  809. memdelete_arr(sky_scene_state.last_frame_directional_lights);
  810. material_storage->shader_free(sky_shader.default_shader);
  811. material_storage->material_free(sky_shader.default_material);
  812. material_storage->shader_free(sky_scene_state.fog_shader);
  813. material_storage->material_free(sky_scene_state.fog_material);
  814. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.uniform_set)) {
  815. RD::get_singleton()->free(sky_scene_state.uniform_set);
  816. }
  817. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.default_fog_uniform_set)) {
  818. RD::get_singleton()->free(sky_scene_state.default_fog_uniform_set);
  819. }
  820. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.fog_only_texture_uniform_set)) {
  821. RD::get_singleton()->free(sky_scene_state.fog_only_texture_uniform_set);
  822. }
  823. }
  824. void SkyRD::setup_sky(RID p_env, Ref<RenderSceneBuffersRD> p_render_buffers, const PagedArray<RID> &p_lights, RID p_camera_attributes, uint32_t p_view_count, const Projection *p_view_projections, const Vector3 *p_view_eye_offsets, const Transform3D &p_cam_transform, const Projection &p_cam_projection, const Size2i p_screen_size, Vector2 p_jitter, RendererSceneRenderRD *p_scene_render) {
  825. RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
  826. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  827. ERR_FAIL_COND(p_env.is_null());
  828. ERR_FAIL_COND(p_render_buffers.is_null());
  829. // make sure we support our view count
  830. ERR_FAIL_COND(p_view_count == 0);
  831. ERR_FAIL_COND(p_view_count > RendererSceneRender::MAX_RENDER_VIEWS);
  832. SkyMaterialData *material = nullptr;
  833. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  834. RID sky_material;
  835. SkyShaderData *shader_data = nullptr;
  836. if (sky) {
  837. sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  838. if (sky_material.is_valid()) {
  839. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  840. if (!material || !material->shader_data->valid) {
  841. material = nullptr;
  842. }
  843. }
  844. }
  845. if (!material) {
  846. sky_material = sky_shader.default_material;
  847. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  848. }
  849. ERR_FAIL_NULL(material);
  850. shader_data = material->shader_data;
  851. ERR_FAIL_NULL(shader_data);
  852. material->set_as_used();
  853. if (sky) {
  854. // Save our screen size; our buffers will already have been cleared.
  855. sky->screen_size.x = p_screen_size.x < 4 ? 4 : p_screen_size.x;
  856. sky->screen_size.y = p_screen_size.y < 4 ? 4 : p_screen_size.y;
  857. // Trigger updating radiance buffers.
  858. if (sky->radiance.is_null()) {
  859. invalidate_sky(sky);
  860. update_dirty_skys();
  861. }
  862. if (shader_data->uses_time && p_scene_render->time - sky->prev_time > 0.00001) {
  863. sky->prev_time = p_scene_render->time;
  864. sky->reflection.dirty = true;
  865. RenderingServerDefault::redraw_request();
  866. }
  867. if (material != sky->prev_material) {
  868. sky->prev_material = material;
  869. sky->reflection.dirty = true;
  870. }
  871. if (material->uniform_set_updated) {
  872. material->uniform_set_updated = false;
  873. sky->reflection.dirty = true;
  874. }
  875. if (!p_cam_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  876. sky->prev_position = p_cam_transform.origin;
  877. sky->reflection.dirty = true;
  878. }
  879. }
  880. sky_scene_state.ubo.directional_light_count = 0;
  881. if (shader_data->uses_light) {
  882. // Run through the list of lights in the scene and pick out the Directional Lights.
  883. // This can't be done in RenderSceneRenderRD::_setup lights because that needs to be called
  884. // after the depth prepass, but this runs before the depth prepass.
  885. for (int i = 0; i < (int)p_lights.size(); i++) {
  886. if (!light_storage->owns_light_instance(p_lights[i])) {
  887. continue;
  888. }
  889. RID base = light_storage->light_instance_get_base_light(p_lights[i]);
  890. ERR_CONTINUE(base.is_null());
  891. RS::LightType type = light_storage->light_get_type(base);
  892. if (type == RS::LIGHT_DIRECTIONAL && light_storage->light_directional_get_sky_mode(base) != RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_ONLY) {
  893. SkyDirectionalLightData &sky_light_data = sky_scene_state.directional_lights[sky_scene_state.ubo.directional_light_count];
  894. Transform3D light_transform = light_storage->light_instance_get_base_transform(p_lights[i]);
  895. Vector3 world_direction = light_transform.basis.xform(Vector3(0, 0, 1)).normalized();
  896. sky_light_data.direction[0] = world_direction.x;
  897. sky_light_data.direction[1] = world_direction.y;
  898. sky_light_data.direction[2] = world_direction.z;
  899. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  900. sky_light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  901. if (p_scene_render->is_using_physical_light_units()) {
  902. sky_light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  903. }
  904. if (p_camera_attributes.is_valid()) {
  905. sky_light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_camera_attributes);
  906. }
  907. Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
  908. sky_light_data.color[0] = linear_col.r;
  909. sky_light_data.color[1] = linear_col.g;
  910. sky_light_data.color[2] = linear_col.b;
  911. sky_light_data.enabled = true;
  912. float angular_diameter = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  913. if (angular_diameter > 0.0) {
  914. // I know tan(0) is 0, but let's not risk it with numerical precision.
  915. // Technically this will keep expanding until reaching the sun, but all we care about
  916. // is expanding until we reach the radius of the near plane. There can't be more occluders than that.
  917. angular_diameter = Math::tan(Math::deg_to_rad(angular_diameter));
  918. } else {
  919. angular_diameter = 0.0;
  920. }
  921. sky_light_data.size = angular_diameter;
  922. sky_scene_state.ubo.directional_light_count++;
  923. if (sky_scene_state.ubo.directional_light_count >= sky_scene_state.max_directional_lights) {
  924. break;
  925. }
  926. }
  927. }
  928. // Check whether the directional_light_buffer changes.
  929. bool light_data_dirty = false;
  930. // Light buffer is dirty if we have fewer or more lights.
  931. // If we have fewer lights, make sure that old lights are disabled.
  932. if (sky_scene_state.ubo.directional_light_count != sky_scene_state.last_frame_directional_light_count) {
  933. light_data_dirty = true;
  934. for (uint32_t i = sky_scene_state.ubo.directional_light_count; i < sky_scene_state.max_directional_lights; i++) {
  935. sky_scene_state.directional_lights[i].enabled = false;
  936. sky_scene_state.last_frame_directional_lights[i].enabled = false;
  937. }
  938. }
  939. if (!light_data_dirty) {
  940. for (uint32_t i = 0; i < sky_scene_state.ubo.directional_light_count; i++) {
  941. if (sky_scene_state.directional_lights[i].direction[0] != sky_scene_state.last_frame_directional_lights[i].direction[0] ||
  942. sky_scene_state.directional_lights[i].direction[1] != sky_scene_state.last_frame_directional_lights[i].direction[1] ||
  943. sky_scene_state.directional_lights[i].direction[2] != sky_scene_state.last_frame_directional_lights[i].direction[2] ||
  944. sky_scene_state.directional_lights[i].energy != sky_scene_state.last_frame_directional_lights[i].energy ||
  945. sky_scene_state.directional_lights[i].color[0] != sky_scene_state.last_frame_directional_lights[i].color[0] ||
  946. sky_scene_state.directional_lights[i].color[1] != sky_scene_state.last_frame_directional_lights[i].color[1] ||
  947. sky_scene_state.directional_lights[i].color[2] != sky_scene_state.last_frame_directional_lights[i].color[2] ||
  948. sky_scene_state.directional_lights[i].enabled != sky_scene_state.last_frame_directional_lights[i].enabled ||
  949. sky_scene_state.directional_lights[i].size != sky_scene_state.last_frame_directional_lights[i].size) {
  950. light_data_dirty = true;
  951. break;
  952. }
  953. }
  954. }
  955. if (light_data_dirty) {
  956. 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);
  957. SkyDirectionalLightData *temp = sky_scene_state.last_frame_directional_lights;
  958. sky_scene_state.last_frame_directional_lights = sky_scene_state.directional_lights;
  959. sky_scene_state.directional_lights = temp;
  960. sky_scene_state.last_frame_directional_light_count = sky_scene_state.ubo.directional_light_count;
  961. if (sky) {
  962. sky->reflection.dirty = true;
  963. }
  964. }
  965. }
  966. // Setup fog variables.
  967. sky_scene_state.ubo.volumetric_fog_enabled = false;
  968. if (p_render_buffers.is_valid()) {
  969. if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
  970. Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);
  971. sky_scene_state.ubo.volumetric_fog_enabled = true;
  972. float fog_end = fog->length;
  973. if (fog_end > 0.0) {
  974. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0 / fog_end;
  975. } else {
  976. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0;
  977. }
  978. float fog_detail_spread = fog->spread; // Reverse lookup.
  979. if (fog_detail_spread > 0.0) {
  980. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0 / fog_detail_spread;
  981. } else {
  982. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0;
  983. }
  984. sky_scene_state.fog_uniform_set = fog->sky_uniform_set;
  985. }
  986. }
  987. Projection correction;
  988. correction.set_depth_correction(false, true);
  989. correction.add_jitter_offset(p_jitter);
  990. sky_scene_state.view_count = p_view_count;
  991. sky_scene_state.cam_transform = p_cam_transform;
  992. sky_scene_state.cam_projection = correction * p_cam_projection; // We only use this when rendering a single view.
  993. // Our info in our UBO is only used if we're rendering stereo.
  994. for (uint32_t i = 0; i < p_view_count; i++) {
  995. Projection view_inv_projection = (correction * p_view_projections[i]).inverse();
  996. if (p_view_count > 1) {
  997. // Reprojection is used when we need to have things in combined space.
  998. RendererRD::MaterialStorage::store_camera(p_cam_projection * view_inv_projection, sky_scene_state.ubo.combined_reprojection[i]);
  999. } else {
  1000. // This is unused so just reset to identity.
  1001. Projection ident;
  1002. RendererRD::MaterialStorage::store_camera(ident, sky_scene_state.ubo.combined_reprojection[i]);
  1003. }
  1004. RendererRD::MaterialStorage::store_camera(view_inv_projection, sky_scene_state.ubo.view_inv_projections[i]);
  1005. sky_scene_state.ubo.view_eye_offsets[i][0] = p_view_eye_offsets[i].x;
  1006. sky_scene_state.ubo.view_eye_offsets[i][1] = p_view_eye_offsets[i].y;
  1007. sky_scene_state.ubo.view_eye_offsets[i][2] = p_view_eye_offsets[i].z;
  1008. sky_scene_state.ubo.view_eye_offsets[i][3] = 0.0;
  1009. }
  1010. sky_scene_state.ubo.z_far = p_view_projections[0].get_z_far(); // Should be the same for all projection.
  1011. sky_scene_state.ubo.fog_enabled = RendererSceneRenderRD::get_singleton()->environment_get_fog_enabled(p_env);
  1012. sky_scene_state.ubo.fog_density = RendererSceneRenderRD::get_singleton()->environment_get_fog_density(p_env);
  1013. sky_scene_state.ubo.fog_aerial_perspective = RendererSceneRenderRD::get_singleton()->environment_get_fog_aerial_perspective(p_env);
  1014. Color fog_color = RendererSceneRenderRD::get_singleton()->environment_get_fog_light_color(p_env).srgb_to_linear();
  1015. float fog_energy = RendererSceneRenderRD::get_singleton()->environment_get_fog_light_energy(p_env);
  1016. sky_scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
  1017. sky_scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
  1018. sky_scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
  1019. sky_scene_state.ubo.fog_sun_scatter = RendererSceneRenderRD::get_singleton()->environment_get_fog_sun_scatter(p_env);
  1020. sky_scene_state.ubo.fog_sky_affect = RendererSceneRenderRD::get_singleton()->environment_get_fog_sky_affect(p_env);
  1021. sky_scene_state.ubo.volumetric_fog_sky_affect = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_sky_affect(p_env);
  1022. RD::get_singleton()->buffer_update(sky_scene_state.uniform_buffer, 0, sizeof(SkySceneState::UBO), &sky_scene_state.ubo);
  1023. }
  1024. void SkyRD::update_radiance_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, const Vector3 &p_global_pos, double p_time, float p_luminance_multiplier) {
  1025. ERR_FAIL_COND(p_render_buffers.is_null());
  1026. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  1027. ERR_FAIL_COND(p_env.is_null());
  1028. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1029. ERR_FAIL_NULL(sky);
  1030. RID sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1031. SkyMaterialData *material = nullptr;
  1032. if (sky_material.is_valid()) {
  1033. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1034. if (!material || !material->shader_data->valid) {
  1035. material = nullptr;
  1036. }
  1037. }
  1038. if (!material) {
  1039. sky_material = sky_shader.default_material;
  1040. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1041. }
  1042. ERR_FAIL_NULL(material);
  1043. SkyShaderData *shader_data = material->shader_data;
  1044. ERR_FAIL_NULL(shader_data);
  1045. bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
  1046. RS::SkyMode sky_mode = sky->mode;
  1047. if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
  1048. if (shader_data->uses_time || shader_data->uses_position) {
  1049. update_single_frame = true;
  1050. sky_mode = RS::SKY_MODE_REALTIME;
  1051. } else if (shader_data->uses_light || shader_data->ubo_size > 0) {
  1052. update_single_frame = false;
  1053. sky_mode = RS::SKY_MODE_INCREMENTAL;
  1054. } else {
  1055. update_single_frame = true;
  1056. sky_mode = RS::SKY_MODE_QUALITY;
  1057. }
  1058. }
  1059. if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
  1060. // On the first frame after creating sky, rebuild in single frame
  1061. update_single_frame = true;
  1062. sky_mode = RS::SKY_MODE_QUALITY;
  1063. }
  1064. int max_processing_layer = sky_use_cubemap_array ? sky->reflection.layers.size() : sky->reflection.layers[0].mipmaps.size();
  1065. // Update radiance cubemap
  1066. if (sky->reflection.dirty && (sky->processing_layer >= max_processing_layer || update_single_frame)) {
  1067. static const Vector3 view_normals[6] = {
  1068. Vector3(+1, 0, 0),
  1069. Vector3(-1, 0, 0),
  1070. Vector3(0, +1, 0),
  1071. Vector3(0, -1, 0),
  1072. Vector3(0, 0, +1),
  1073. Vector3(0, 0, -1)
  1074. };
  1075. static const Vector3 view_up[6] = {
  1076. Vector3(0, -1, 0),
  1077. Vector3(0, -1, 0),
  1078. Vector3(0, 0, +1),
  1079. Vector3(0, 0, -1),
  1080. Vector3(0, -1, 0),
  1081. Vector3(0, -1, 0)
  1082. };
  1083. Projection cm;
  1084. cm.set_perspective(90, 1, 0.01, 10.0);
  1085. Projection correction;
  1086. correction.set_depth_correction(true);
  1087. cm = correction * cm;
  1088. // Note, we ignore environment_get_sky_orientation here as this is applied when we do our lookup in our scene shader.
  1089. if (shader_data->uses_quarter_res && roughness_layers >= 3) {
  1090. RD::get_singleton()->draw_command_begin_label("Render Sky to Quarter Res Cubemap");
  1091. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_QUARTER_RES];
  1092. Vector<Color> clear_colors;
  1093. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1094. RD::DrawListID cubemap_draw_list;
  1095. for (int i = 0; i < 6; i++) {
  1096. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1097. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES, sky_shader.default_shader_rd, p_render_buffers);
  1098. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[2].framebuffers[i], RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_DISCARD);
  1099. _render_sky(cubemap_draw_list, p_time, sky->reflection.layers[0].mipmaps[2].framebuffers[i], pipeline, material->uniform_set, texture_uniform_set, cm, local_view, p_global_pos, p_luminance_multiplier);
  1100. RD::get_singleton()->draw_list_end();
  1101. }
  1102. RD::get_singleton()->draw_command_end_label();
  1103. } else if (shader_data->uses_quarter_res && roughness_layers < 3) {
  1104. ERR_PRINT_ED("Cannot use quarter res buffer in sky shader when roughness layers is less than 3. Please increase rendering/reflections/sky_reflections/roughness_layers.");
  1105. }
  1106. if (shader_data->uses_half_res && roughness_layers >= 2) {
  1107. RD::get_singleton()->draw_command_begin_label("Render Sky to Half Res Cubemap");
  1108. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_HALF_RES];
  1109. Vector<Color> clear_colors;
  1110. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1111. RD::DrawListID cubemap_draw_list;
  1112. for (int i = 0; i < 6; i++) {
  1113. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1114. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_CUBEMAP_HALF_RES, sky_shader.default_shader_rd, p_render_buffers);
  1115. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[1].framebuffers[i], RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_DISCARD);
  1116. _render_sky(cubemap_draw_list, p_time, sky->reflection.layers[0].mipmaps[1].framebuffers[i], pipeline, material->uniform_set, texture_uniform_set, cm, local_view, p_global_pos, p_luminance_multiplier);
  1117. RD::get_singleton()->draw_list_end();
  1118. }
  1119. RD::get_singleton()->draw_command_end_label();
  1120. } else if (shader_data->uses_half_res && roughness_layers < 2) {
  1121. ERR_PRINT_ED("Cannot use half res buffer in sky shader when roughness layers is less than 2. Please increase rendering/reflections/sky_reflections/roughness_layers.");
  1122. }
  1123. RD::DrawListID cubemap_draw_list;
  1124. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP];
  1125. RD::get_singleton()->draw_command_begin_label("Render Sky Cubemap");
  1126. for (int i = 0; i < 6; i++) {
  1127. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1128. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_CUBEMAP, sky_shader.default_shader_rd, p_render_buffers);
  1129. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[0].framebuffers[i], RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, Rect2(), RDD::BreadcrumbMarker::SKY_PASS | uint32_t(i));
  1130. _render_sky(cubemap_draw_list, p_time, sky->reflection.layers[0].mipmaps[0].framebuffers[i], pipeline, material->uniform_set, texture_uniform_set, cm, local_view, p_global_pos, p_luminance_multiplier);
  1131. RD::get_singleton()->draw_list_end();
  1132. }
  1133. RD::get_singleton()->draw_command_end_label();
  1134. if (sky_mode == RS::SKY_MODE_REALTIME) {
  1135. sky->reflection.create_reflection_fast_filter(sky_use_cubemap_array);
  1136. if (sky_use_cubemap_array) {
  1137. sky->reflection.update_reflection_mipmaps(0, sky->reflection.layers.size());
  1138. }
  1139. } else {
  1140. if (update_single_frame) {
  1141. for (int i = 1; i < max_processing_layer; i++) {
  1142. sky->reflection.create_reflection_importance_sample(sky_use_cubemap_array, 10, i, sky_ggx_samples_quality);
  1143. }
  1144. if (sky_use_cubemap_array) {
  1145. sky->reflection.update_reflection_mipmaps(0, sky->reflection.layers.size());
  1146. }
  1147. } else {
  1148. if (sky_use_cubemap_array) {
  1149. // Multi-Frame so just update the first array level
  1150. sky->reflection.update_reflection_mipmaps(0, 1);
  1151. }
  1152. }
  1153. sky->processing_layer = 1;
  1154. }
  1155. sky->baked_exposure = p_luminance_multiplier;
  1156. sky->reflection.dirty = false;
  1157. } else {
  1158. if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
  1159. sky->reflection.create_reflection_importance_sample(sky_use_cubemap_array, 10, sky->processing_layer, sky_ggx_samples_quality);
  1160. if (sky_use_cubemap_array) {
  1161. sky->reflection.update_reflection_mipmaps(sky->processing_layer, sky->processing_layer + 1);
  1162. }
  1163. sky->processing_layer++;
  1164. }
  1165. }
  1166. }
  1167. void SkyRD::update_res_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, double p_time, float p_luminance_multiplier) {
  1168. ERR_FAIL_COND(p_render_buffers.is_null());
  1169. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  1170. ERR_FAIL_COND(p_env.is_null());
  1171. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1172. SkyMaterialData *material = nullptr;
  1173. RID sky_material;
  1174. RS::EnvironmentBG background = RendererSceneRenderRD::get_singleton()->environment_get_background(p_env);
  1175. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1176. ERR_FAIL_NULL(sky);
  1177. sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1178. if (sky_material.is_valid()) {
  1179. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1180. if (!material || !material->shader_data->valid) {
  1181. material = nullptr;
  1182. }
  1183. }
  1184. if (!material) {
  1185. sky_material = sky_shader.default_material;
  1186. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1187. }
  1188. }
  1189. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1190. sky_material = sky_scene_state.fog_material;
  1191. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1192. }
  1193. ERR_FAIL_NULL(material);
  1194. SkyShaderData *shader_data = material->shader_data;
  1195. ERR_FAIL_NULL(shader_data);
  1196. if (!shader_data->uses_quarter_res && !shader_data->uses_half_res) {
  1197. return;
  1198. }
  1199. material->set_as_used();
  1200. RENDER_TIMESTAMP("Setup Sky Resolution Buffers");
  1201. RD::get_singleton()->draw_command_begin_label("Setup Sky Resolution Buffers");
  1202. Basis sky_transform = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_env);
  1203. sky_transform.invert();
  1204. float custom_fov = RendererSceneRenderRD::get_singleton()->environment_get_sky_custom_fov(p_env);
  1205. // Camera
  1206. Projection projection = sky_scene_state.cam_projection;
  1207. if (custom_fov && sky_scene_state.view_count == 1) {
  1208. // With custom fov we don't support stereo...
  1209. float near_plane = projection.get_z_near();
  1210. float far_plane = projection.get_z_far();
  1211. float aspect = projection.get_aspect();
  1212. projection.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1213. }
  1214. sky_transform = sky_transform * sky_scene_state.cam_transform.basis;
  1215. if (shader_data->uses_quarter_res) {
  1216. PipelineCacheRD *pipeline = &shader_data->pipelines[sky_scene_state.view_count > 1 ? SKY_VERSION_QUARTER_RES_MULTIVIEW : SKY_VERSION_QUARTER_RES];
  1217. // Grab texture and framebuffer from cache, create if needed...
  1218. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1219. Size2i quarter_size = sky->screen_size / 4;
  1220. RID texture = p_render_buffers->create_texture(RB_SCOPE_SKY, RB_QUARTER_TEXTURE, texture_format, usage_bits, RD::TEXTURE_SAMPLES_1, quarter_size);
  1221. RID framebuffer = FramebufferCacheRD::get_singleton()->get_cache_multiview(sky_scene_state.view_count, texture);
  1222. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_QUARTER_RES, sky_shader.default_shader_rd, p_render_buffers);
  1223. Vector<Color> clear_colors;
  1224. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1225. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors, 0.0);
  1226. _render_sky(draw_list, p_time, framebuffer, pipeline, material->uniform_set, texture_uniform_set, projection, sky_transform, sky_scene_state.cam_transform.origin, p_luminance_multiplier);
  1227. RD::get_singleton()->draw_list_end();
  1228. }
  1229. if (shader_data->uses_half_res) {
  1230. PipelineCacheRD *pipeline = &shader_data->pipelines[sky_scene_state.view_count > 1 ? SKY_VERSION_HALF_RES_MULTIVIEW : SKY_VERSION_HALF_RES];
  1231. // Grab texture and framebuffer from cache, create if needed...
  1232. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1233. Size2i half_size = sky->screen_size / 2;
  1234. RID texture = p_render_buffers->create_texture(RB_SCOPE_SKY, RB_HALF_TEXTURE, texture_format, usage_bits, RD::TEXTURE_SAMPLES_1, half_size);
  1235. RID framebuffer = FramebufferCacheRD::get_singleton()->get_cache_multiview(sky_scene_state.view_count, texture);
  1236. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_HALF_RES, sky_shader.default_shader_rd, p_render_buffers);
  1237. Vector<Color> clear_colors;
  1238. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1239. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors, 0.0);
  1240. _render_sky(draw_list, p_time, framebuffer, pipeline, material->uniform_set, texture_uniform_set, projection, sky_transform, sky_scene_state.cam_transform.origin, p_luminance_multiplier);
  1241. RD::get_singleton()->draw_list_end();
  1242. }
  1243. RD::get_singleton()->draw_command_end_label(); // Setup Sky resolution buffers
  1244. }
  1245. void SkyRD::draw_sky(RD::DrawListID p_draw_list, Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, RID p_fb, double p_time, float p_luminance_multiplier) {
  1246. ERR_FAIL_COND(p_render_buffers.is_null());
  1247. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  1248. ERR_FAIL_COND(p_env.is_null());
  1249. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1250. SkyMaterialData *material = nullptr;
  1251. RID sky_material;
  1252. RS::EnvironmentBG background = RendererSceneRenderRD::get_singleton()->environment_get_background(p_env);
  1253. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1254. ERR_FAIL_NULL(sky);
  1255. sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1256. if (sky_material.is_valid()) {
  1257. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1258. if (!material || !material->shader_data->valid) {
  1259. material = nullptr;
  1260. }
  1261. }
  1262. if (!material) {
  1263. sky_material = sky_shader.default_material;
  1264. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1265. }
  1266. }
  1267. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1268. sky_material = sky_scene_state.fog_material;
  1269. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1270. }
  1271. ERR_FAIL_NULL(material);
  1272. SkyShaderData *shader_data = material->shader_data;
  1273. ERR_FAIL_NULL(shader_data);
  1274. material->set_as_used();
  1275. Basis sky_transform = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_env);
  1276. sky_transform.invert();
  1277. float custom_fov = RendererSceneRenderRD::get_singleton()->environment_get_sky_custom_fov(p_env);
  1278. // Camera
  1279. Projection projection = sky_scene_state.cam_projection;
  1280. if (custom_fov && sky_scene_state.view_count == 1) {
  1281. // With custom fov we don't support stereo...
  1282. float near_plane = projection.get_z_near();
  1283. float far_plane = projection.get_z_far();
  1284. float aspect = projection.get_aspect();
  1285. projection.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1286. }
  1287. sky_transform = sky_transform * sky_scene_state.cam_transform.basis;
  1288. PipelineCacheRD *pipeline = &shader_data->pipelines[sky_scene_state.view_count > 1 ? SKY_VERSION_BACKGROUND_MULTIVIEW : SKY_VERSION_BACKGROUND];
  1289. RID texture_uniform_set;
  1290. if (sky) {
  1291. texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_BACKGROUND, sky_shader.default_shader_rd, p_render_buffers);
  1292. } else {
  1293. texture_uniform_set = sky_scene_state.fog_only_texture_uniform_set;
  1294. }
  1295. _render_sky(p_draw_list, p_time, p_fb, pipeline, material->uniform_set, texture_uniform_set, projection, sky_transform, sky_scene_state.cam_transform.origin, p_luminance_multiplier);
  1296. }
  1297. void SkyRD::invalidate_sky(Sky *p_sky) {
  1298. if (!p_sky->dirty) {
  1299. p_sky->dirty = true;
  1300. p_sky->dirty_list = dirty_sky_list;
  1301. dirty_sky_list = p_sky;
  1302. }
  1303. }
  1304. void SkyRD::update_dirty_skys() {
  1305. Sky *sky = dirty_sky_list;
  1306. while (sky) {
  1307. //update sky configuration if texture is missing
  1308. // TODO See if we can move this into `update_radiance_buffers` and remove our dirty_sky logic.
  1309. // As this is basically a duplicate of the logic in reflection probes we could move this logic
  1310. // into RenderSceneBuffersRD and use that from both places.
  1311. if (sky->radiance.is_null()) {
  1312. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  1313. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  1314. int layers = roughness_layers;
  1315. if (sky->mode == RS::SKY_MODE_REALTIME) {
  1316. layers = 8;
  1317. if (roughness_layers != 8) {
  1318. WARN_PRINT("When using the Real-Time sky update mode (or Automatic with a sky shader using \"TIME\"), \"rendering/reflections/sky_reflections/roughness_layers\" should be set to 8 in the project settings for best quality reflections.");
  1319. }
  1320. }
  1321. if (sky_use_cubemap_array) {
  1322. //array (higher quality, 6 times more memory)
  1323. RD::TextureFormat tf;
  1324. tf.array_layers = layers * 6;
  1325. tf.format = texture_format;
  1326. tf.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  1327. tf.mipmaps = mipmaps;
  1328. tf.width = w;
  1329. tf.height = h;
  1330. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1331. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  1332. tf.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT;
  1333. }
  1334. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1335. sky->reflection.update_reflection_data(sky->radiance_size, mipmaps, true, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME, roughness_layers, texture_format);
  1336. } else {
  1337. //regular cubemap, lower quality (aliasing, less memory)
  1338. RD::TextureFormat tf;
  1339. tf.array_layers = 6;
  1340. tf.format = texture_format;
  1341. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  1342. tf.mipmaps = MIN(mipmaps, layers);
  1343. tf.width = w;
  1344. tf.height = h;
  1345. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1346. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  1347. tf.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT;
  1348. }
  1349. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1350. sky->reflection.update_reflection_data(sky->radiance_size, MIN(mipmaps, layers), false, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME, roughness_layers, texture_format);
  1351. }
  1352. }
  1353. sky->reflection.dirty = true;
  1354. sky->processing_layer = 0;
  1355. Sky *next = sky->dirty_list;
  1356. sky->dirty_list = nullptr;
  1357. sky->dirty = false;
  1358. sky = next;
  1359. }
  1360. dirty_sky_list = nullptr;
  1361. }
  1362. RID SkyRD::sky_get_material(RID p_sky) const {
  1363. Sky *sky = get_sky(p_sky);
  1364. ERR_FAIL_NULL_V(sky, RID());
  1365. return sky->material;
  1366. }
  1367. float SkyRD::sky_get_baked_exposure(RID p_sky) const {
  1368. Sky *sky = get_sky(p_sky);
  1369. ERR_FAIL_NULL_V(sky, 1.0);
  1370. return sky->baked_exposure;
  1371. }
  1372. RID SkyRD::allocate_sky_rid() {
  1373. return sky_owner.allocate_rid();
  1374. }
  1375. void SkyRD::initialize_sky_rid(RID p_rid) {
  1376. sky_owner.initialize_rid(p_rid, Sky());
  1377. }
  1378. SkyRD::Sky *SkyRD::get_sky(RID p_sky) const {
  1379. return sky_owner.get_or_null(p_sky);
  1380. }
  1381. void SkyRD::free_sky(RID p_sky) {
  1382. Sky *sky = get_sky(p_sky);
  1383. ERR_FAIL_NULL(sky);
  1384. sky->free();
  1385. sky_owner.free(p_sky);
  1386. }
  1387. void SkyRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  1388. Sky *sky = get_sky(p_sky);
  1389. ERR_FAIL_NULL(sky);
  1390. if (sky->set_radiance_size(p_radiance_size)) {
  1391. invalidate_sky(sky);
  1392. }
  1393. }
  1394. void SkyRD::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  1395. Sky *sky = get_sky(p_sky);
  1396. ERR_FAIL_NULL(sky);
  1397. if (sky->set_mode(p_mode)) {
  1398. invalidate_sky(sky);
  1399. }
  1400. }
  1401. void SkyRD::sky_set_material(RID p_sky, RID p_material) {
  1402. Sky *sky = get_sky(p_sky);
  1403. ERR_FAIL_NULL(sky);
  1404. if (sky->set_material(p_material)) {
  1405. invalidate_sky(sky);
  1406. }
  1407. }
  1408. Ref<Image> SkyRD::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  1409. Sky *sky = get_sky(p_sky);
  1410. ERR_FAIL_NULL_V(sky, Ref<Image>());
  1411. update_dirty_skys();
  1412. return sky->bake_panorama(p_energy, p_bake_irradiance ? roughness_layers : 0, p_size);
  1413. }
  1414. RID SkyRD::sky_get_radiance_texture_rd(RID p_sky) const {
  1415. Sky *sky = get_sky(p_sky);
  1416. ERR_FAIL_NULL_V(sky, RID());
  1417. return sky->radiance;
  1418. }