sky.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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_LESS_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_COND(!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);
  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, 0);
  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, 1);
  198. }
  199. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_texture_set, 2);
  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, 3);
  203. } else {
  204. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, sky_scene_state.default_fog_uniform_set, 3);
  205. }
  206. }
  207. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  208. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky_push_constant, sizeof(SkyPushConstant));
  209. RD::get_singleton()->draw_list_draw(draw_list, true);
  210. }
  211. ////////////////////////////////////////////////////////////////////////////////
  212. // ReflectionData
  213. void SkyRD::ReflectionData::clear_reflection_data() {
  214. layers.clear();
  215. radiance_base_cubemap = RID();
  216. if (downsampled_radiance_cubemap.is_valid()) {
  217. RD::get_singleton()->free(downsampled_radiance_cubemap);
  218. }
  219. downsampled_radiance_cubemap = RID();
  220. downsampled_layer.mipmaps.clear();
  221. coefficient_buffer = RID();
  222. }
  223. 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) {
  224. //recreate radiance and all data
  225. int mipmaps = p_mipmaps;
  226. uint32_t w = p_size, h = p_size;
  227. bool render_buffers_can_be_storage = RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage();
  228. if (p_use_array) {
  229. int num_layers = p_low_quality ? 8 : p_roughness_layers;
  230. for (int i = 0; i < num_layers; i++) {
  231. ReflectionData::Layer layer;
  232. uint32_t mmw = w;
  233. uint32_t mmh = h;
  234. layer.mipmaps.resize(mipmaps);
  235. layer.views.resize(mipmaps);
  236. for (int j = 0; j < mipmaps; j++) {
  237. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  238. mm.size.width = mmw;
  239. mm.size.height = mmh;
  240. for (int k = 0; k < 6; k++) {
  241. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6 + k, j);
  242. Vector<RID> fbtex;
  243. fbtex.push_back(mm.views[k]);
  244. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  245. }
  246. 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);
  247. mmw = MAX(1u, mmw >> 1);
  248. mmh = MAX(1u, mmh >> 1);
  249. }
  250. layers.push_back(layer);
  251. }
  252. } else {
  253. mipmaps = p_low_quality ? 8 : mipmaps;
  254. //regular cubemap, lower quality (aliasing, less memory)
  255. ReflectionData::Layer layer;
  256. uint32_t mmw = w;
  257. uint32_t mmh = h;
  258. layer.mipmaps.resize(mipmaps);
  259. layer.views.resize(mipmaps);
  260. for (int j = 0; j < mipmaps; j++) {
  261. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  262. mm.size.width = mmw;
  263. mm.size.height = mmh;
  264. for (int k = 0; k < 6; k++) {
  265. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + k, j);
  266. Vector<RID> fbtex;
  267. fbtex.push_back(mm.views[k]);
  268. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  269. }
  270. 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);
  271. mmw = MAX(1u, mmw >> 1);
  272. mmh = MAX(1u, mmh >> 1);
  273. }
  274. layers.push_back(layer);
  275. }
  276. 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);
  277. RD::get_singleton()->set_resource_name(radiance_base_cubemap, "radiance base cubemap");
  278. RD::TextureFormat tf;
  279. tf.format = p_texture_format;
  280. tf.width = p_low_quality ? 64 : p_size >> 1; // Always 64x64 when using REALTIME.
  281. tf.height = p_low_quality ? 64 : p_size >> 1;
  282. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  283. tf.array_layers = 6;
  284. tf.mipmaps = p_low_quality ? 7 : mipmaps - 1;
  285. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  286. if (render_buffers_can_be_storage) {
  287. tf.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT;
  288. }
  289. downsampled_radiance_cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  290. RD::get_singleton()->set_resource_name(downsampled_radiance_cubemap, "downsampled radiance cubemap");
  291. {
  292. uint32_t mmw = tf.width;
  293. uint32_t mmh = tf.height;
  294. downsampled_layer.mipmaps.resize(tf.mipmaps);
  295. for (int j = 0; j < downsampled_layer.mipmaps.size(); j++) {
  296. ReflectionData::DownsampleLayer::Mipmap &mm = downsampled_layer.mipmaps.write[j];
  297. mm.size.width = mmw;
  298. mm.size.height = mmh;
  299. mm.view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), downsampled_radiance_cubemap, 0, j, 1, RD::TEXTURE_SLICE_CUBEMAP);
  300. RD::get_singleton()->set_resource_name(mm.view, "Downsampled Radiance Cubemap Mip " + itos(j) + " ");
  301. if (!render_buffers_can_be_storage) {
  302. // we need a framebuffer for each side of our cubemap
  303. for (int k = 0; k < 6; k++) {
  304. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), downsampled_radiance_cubemap, k, j);
  305. RD::get_singleton()->set_resource_name(mm.view, "Downsampled Radiance Cubemap Mip: " + itos(j) + " Face: " + itos(k) + " ");
  306. Vector<RID> fbtex;
  307. fbtex.push_back(mm.views[k]);
  308. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  309. }
  310. }
  311. mmw = MAX(1u, mmw >> 1);
  312. mmh = MAX(1u, mmh >> 1);
  313. }
  314. }
  315. }
  316. void SkyRD::ReflectionData::create_reflection_fast_filter(bool p_use_arrays) {
  317. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  318. ERR_FAIL_NULL_MSG(copy_effects, "Effects haven't been initialized");
  319. bool prefer_raster_effects = copy_effects->get_prefer_raster_effects();
  320. if (prefer_raster_effects) {
  321. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  322. for (int k = 0; k < 6; k++) {
  323. copy_effects->cubemap_downsample_raster(radiance_base_cubemap, downsampled_layer.mipmaps[0].framebuffers[k], k, downsampled_layer.mipmaps[0].size);
  324. }
  325. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  326. for (int k = 0; k < 6; k++) {
  327. copy_effects->cubemap_downsample_raster(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].framebuffers[k], k, downsampled_layer.mipmaps[i].size);
  328. }
  329. }
  330. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  331. if (p_use_arrays) {
  332. RD::get_singleton()->draw_command_begin_label("filter radiance map into array heads");
  333. for (int i = 0; i < layers.size(); i++) {
  334. for (int k = 0; k < 6; k++) {
  335. copy_effects->cubemap_filter_raster(downsampled_radiance_cubemap, layers[i].mipmaps[0].framebuffers[k], k, i);
  336. }
  337. }
  338. } else {
  339. RD::get_singleton()->draw_command_begin_label("filter radiance map into mipmaps directly");
  340. for (int j = 0; j < layers[0].mipmaps.size(); j++) {
  341. for (int k = 0; k < 6; k++) {
  342. copy_effects->cubemap_filter_raster(downsampled_radiance_cubemap, layers[0].mipmaps[j].framebuffers[k], k, j);
  343. }
  344. }
  345. }
  346. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  347. } else {
  348. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  349. copy_effects->cubemap_downsample(radiance_base_cubemap, downsampled_layer.mipmaps[0].view, downsampled_layer.mipmaps[0].size);
  350. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  351. copy_effects->cubemap_downsample(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].view, downsampled_layer.mipmaps[i].size);
  352. }
  353. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  354. Vector<RID> views;
  355. if (p_use_arrays) {
  356. for (int i = 1; i < layers.size(); i++) {
  357. views.push_back(layers[i].views[0]);
  358. }
  359. } else {
  360. for (int i = 1; i < layers[0].views.size(); i++) {
  361. views.push_back(layers[0].views[i]);
  362. }
  363. }
  364. RD::get_singleton()->draw_command_begin_label("Fast filter radiance");
  365. copy_effects->cubemap_filter(downsampled_radiance_cubemap, views, p_use_arrays);
  366. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  367. }
  368. }
  369. 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) {
  370. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  371. ERR_FAIL_NULL_MSG(copy_effects, "Effects haven't been initialized");
  372. bool prefer_raster_effects = copy_effects->get_prefer_raster_effects();
  373. if (prefer_raster_effects) {
  374. if (p_base_layer == 1) {
  375. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  376. for (int k = 0; k < 6; k++) {
  377. copy_effects->cubemap_downsample_raster(radiance_base_cubemap, downsampled_layer.mipmaps[0].framebuffers[k], k, downsampled_layer.mipmaps[0].size);
  378. }
  379. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  380. for (int k = 0; k < 6; k++) {
  381. copy_effects->cubemap_downsample_raster(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].framebuffers[k], k, downsampled_layer.mipmaps[i].size);
  382. }
  383. }
  384. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  385. }
  386. RD::get_singleton()->draw_command_begin_label("High Quality filter radiance");
  387. if (p_use_arrays) {
  388. for (int k = 0; k < 6; k++) {
  389. copy_effects->cubemap_roughness_raster(
  390. downsampled_radiance_cubemap,
  391. layers[p_base_layer].mipmaps[0].framebuffers[k],
  392. k,
  393. p_sky_ggx_samples_quality,
  394. float(p_base_layer) / (layers.size() - 1.0),
  395. layers[p_base_layer].mipmaps[0].size.x);
  396. }
  397. } else {
  398. for (int k = 0; k < 6; k++) {
  399. copy_effects->cubemap_roughness_raster(
  400. downsampled_radiance_cubemap,
  401. layers[0].mipmaps[p_base_layer].framebuffers[k],
  402. k,
  403. p_sky_ggx_samples_quality,
  404. float(p_base_layer) / (layers[0].mipmaps.size() - 1.0),
  405. layers[0].mipmaps[p_base_layer].size.x);
  406. }
  407. }
  408. } else {
  409. if (p_base_layer == 1) {
  410. RD::get_singleton()->draw_command_begin_label("Downsample radiance map");
  411. copy_effects->cubemap_downsample(radiance_base_cubemap, downsampled_layer.mipmaps[0].view, downsampled_layer.mipmaps[0].size);
  412. for (int i = 1; i < downsampled_layer.mipmaps.size(); i++) {
  413. copy_effects->cubemap_downsample(downsampled_layer.mipmaps[i - 1].view, downsampled_layer.mipmaps[i].view, downsampled_layer.mipmaps[i].size);
  414. }
  415. RD::get_singleton()->draw_command_end_label(); // Downsample Radiance
  416. }
  417. RD::get_singleton()->draw_command_begin_label("High Quality filter radiance");
  418. if (p_use_arrays) {
  419. 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);
  420. } else {
  421. copy_effects->cubemap_roughness(
  422. downsampled_radiance_cubemap,
  423. layers[0].views[p_base_layer],
  424. p_cube_side,
  425. p_sky_ggx_samples_quality,
  426. float(p_base_layer) / (layers[0].mipmaps.size() - 1.0),
  427. layers[0].mipmaps[p_base_layer].size.x);
  428. }
  429. }
  430. RD::get_singleton()->draw_command_end_label(); // Filter radiance
  431. }
  432. void SkyRD::ReflectionData::update_reflection_mipmaps(int p_start, int p_end) {
  433. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  434. ERR_FAIL_NULL_MSG(copy_effects, "Effects haven't been initialized");
  435. bool prefer_raster_effects = copy_effects->get_prefer_raster_effects();
  436. RD::get_singleton()->draw_command_begin_label("Update Radiance Cubemap Array Mipmaps");
  437. for (int i = p_start; i < p_end; i++) {
  438. for (int j = 0; j < layers[i].views.size() - 1; j++) {
  439. RID view = layers[i].views[j];
  440. Size2i size = layers[i].mipmaps[j + 1].size;
  441. if (prefer_raster_effects) {
  442. for (int k = 0; k < 6; k++) {
  443. RID framebuffer = layers[i].mipmaps[j + 1].framebuffers[k];
  444. copy_effects->cubemap_downsample_raster(view, framebuffer, k, size);
  445. }
  446. } else {
  447. RID texture = layers[i].views[j + 1];
  448. copy_effects->cubemap_downsample(view, texture, size);
  449. }
  450. }
  451. }
  452. RD::get_singleton()->draw_command_end_label();
  453. }
  454. ////////////////////////////////////////////////////////////////////////////////
  455. // SkyRD::Sky
  456. void SkyRD::Sky::free() {
  457. if (radiance.is_valid()) {
  458. RD::get_singleton()->free(radiance);
  459. radiance = RID();
  460. }
  461. reflection.clear_reflection_data();
  462. if (uniform_buffer.is_valid()) {
  463. RD::get_singleton()->free(uniform_buffer);
  464. uniform_buffer = RID();
  465. }
  466. if (material.is_valid()) {
  467. RSG::material_storage->material_free(material);
  468. material = RID();
  469. }
  470. }
  471. RID SkyRD::Sky::get_textures(SkyTextureSetVersion p_version, RID p_default_shader_rd, Ref<RenderSceneBuffersRD> p_render_buffers) {
  472. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  473. Vector<RD::Uniform> uniforms;
  474. {
  475. RD::Uniform u;
  476. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  477. u.binding = 0;
  478. if (radiance.is_valid() && p_version <= SKY_TEXTURE_SET_QUARTER_RES) {
  479. u.append_id(radiance);
  480. } else {
  481. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  482. }
  483. uniforms.push_back(u);
  484. }
  485. {
  486. RD::Uniform u;
  487. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  488. u.binding = 1; // half res
  489. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  490. 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) {
  491. u.append_id(reflection.layers[0].views[1]);
  492. } else {
  493. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  494. }
  495. } else {
  496. 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();
  497. if (half_texture.is_valid() && p_version != SKY_TEXTURE_SET_HALF_RES) {
  498. u.append_id(half_texture);
  499. } else {
  500. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  501. }
  502. }
  503. uniforms.push_back(u);
  504. }
  505. {
  506. RD::Uniform u;
  507. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  508. u.binding = 2; // quarter res
  509. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  510. 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) {
  511. u.append_id(reflection.layers[0].views[2]);
  512. } else {
  513. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  514. }
  515. } else {
  516. 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();
  517. if (quarter_texture.is_valid() && p_version != SKY_TEXTURE_SET_QUARTER_RES) {
  518. u.append_id(quarter_texture);
  519. } else {
  520. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  521. }
  522. }
  523. uniforms.push_back(u);
  524. }
  525. return UniformSetCacheRD::get_singleton()->get_cache_vec(p_default_shader_rd, SKY_SET_TEXTURES, uniforms);
  526. }
  527. bool SkyRD::Sky::set_radiance_size(int p_radiance_size) {
  528. ERR_FAIL_COND_V(p_radiance_size < 32 || p_radiance_size > 2048, false);
  529. if (radiance_size == p_radiance_size) {
  530. return false;
  531. }
  532. radiance_size = p_radiance_size;
  533. if (mode == RS::SKY_MODE_REALTIME && radiance_size != 256) {
  534. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  535. radiance_size = 256;
  536. }
  537. if (radiance.is_valid()) {
  538. RD::get_singleton()->free(radiance);
  539. radiance = RID();
  540. }
  541. reflection.clear_reflection_data();
  542. return true;
  543. }
  544. bool SkyRD::Sky::set_mode(RS::SkyMode p_mode) {
  545. if (mode == p_mode) {
  546. return false;
  547. }
  548. mode = p_mode;
  549. if (mode == RS::SKY_MODE_REALTIME && radiance_size != 256) {
  550. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  551. set_radiance_size(256);
  552. }
  553. if (radiance.is_valid()) {
  554. RD::get_singleton()->free(radiance);
  555. radiance = RID();
  556. }
  557. reflection.clear_reflection_data();
  558. return true;
  559. }
  560. bool SkyRD::Sky::set_material(RID p_material) {
  561. if (material == p_material) {
  562. return false;
  563. }
  564. material = p_material;
  565. return true;
  566. }
  567. Ref<Image> SkyRD::Sky::bake_panorama(float p_energy, int p_roughness_layers, const Size2i &p_size) {
  568. if (radiance.is_valid()) {
  569. RendererRD::CopyEffects *copy_effects = RendererRD::CopyEffects::get_singleton();
  570. RD::TextureFormat tf;
  571. tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; // Could be RGBA16
  572. tf.width = p_size.width;
  573. tf.height = p_size.height;
  574. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  575. RID rad_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  576. copy_effects->copy_cubemap_to_panorama(radiance, rad_tex, p_size, p_roughness_layers, reflection.layers.size() > 1);
  577. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rad_tex, 0);
  578. RD::get_singleton()->free(rad_tex);
  579. Ref<Image> img = Image::create_from_data(p_size.width, p_size.height, false, Image::FORMAT_RGBAF, data);
  580. for (int i = 0; i < p_size.width; i++) {
  581. for (int j = 0; j < p_size.height; j++) {
  582. Color c = img->get_pixel(i, j);
  583. c.r *= p_energy;
  584. c.g *= p_energy;
  585. c.b *= p_energy;
  586. img->set_pixel(i, j, c);
  587. }
  588. }
  589. return img;
  590. }
  591. return Ref<Image>();
  592. }
  593. ////////////////////////////////////////////////////////////////////////////////
  594. // SkyRD
  595. RendererRD::MaterialStorage::ShaderData *SkyRD::_create_sky_shader_func() {
  596. SkyShaderData *shader_data = memnew(SkyShaderData);
  597. return shader_data;
  598. }
  599. RendererRD::MaterialStorage::ShaderData *SkyRD::_create_sky_shader_funcs() {
  600. // !BAS! Why isn't _create_sky_shader_func not just static too?
  601. return static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton)->sky._create_sky_shader_func();
  602. };
  603. RendererRD::MaterialStorage::MaterialData *SkyRD::_create_sky_material_func(SkyShaderData *p_shader) {
  604. SkyMaterialData *material_data = memnew(SkyMaterialData);
  605. material_data->shader_data = p_shader;
  606. //update will happen later anyway so do nothing.
  607. return material_data;
  608. }
  609. RendererRD::MaterialStorage::MaterialData *SkyRD::_create_sky_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
  610. // !BAS! same here, we could just make _create_sky_material_func static?
  611. return static_cast<RendererSceneRenderRD *>(RendererSceneRenderRD::singleton)->sky._create_sky_material_func(static_cast<SkyShaderData *>(p_shader));
  612. };
  613. SkyRD::SkyRD() {
  614. roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers");
  615. sky_ggx_samples_quality = GLOBAL_GET("rendering/reflections/sky_reflections/ggx_samples");
  616. sky_use_cubemap_array = GLOBAL_GET("rendering/reflections/sky_reflections/texture_array_reflections");
  617. }
  618. void SkyRD::init() {
  619. RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton();
  620. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  621. {
  622. // Start with the directional lights for the sky
  623. sky_scene_state.max_directional_lights = 4;
  624. uint32_t directional_light_buffer_size = sky_scene_state.max_directional_lights * sizeof(SkyDirectionalLightData);
  625. sky_scene_state.directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  626. sky_scene_state.last_frame_directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  627. sky_scene_state.last_frame_directional_light_count = sky_scene_state.max_directional_lights + 1;
  628. sky_scene_state.directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  629. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_scene_state.max_directional_lights) + "\n";
  630. // Initialize sky
  631. Vector<String> sky_modes;
  632. sky_modes.push_back(""); // Full size
  633. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n"); // Half Res
  634. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n"); // Quarter res
  635. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n"); // Cubemap
  636. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_HALF_RES_PASS\n"); // Half Res Cubemap
  637. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_QUARTER_RES_PASS\n"); // Quarter res Cubemap
  638. sky_modes.push_back("\n#define USE_MULTIVIEW\n"); // Full size multiview
  639. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n#define USE_MULTIVIEW\n"); // Half Res multiview
  640. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n#define USE_MULTIVIEW\n"); // Quarter res multiview
  641. sky_shader.shader.initialize(sky_modes, defines);
  642. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  643. sky_shader.shader.set_variant_enabled(SKY_VERSION_BACKGROUND_MULTIVIEW, false);
  644. sky_shader.shader.set_variant_enabled(SKY_VERSION_HALF_RES_MULTIVIEW, false);
  645. sky_shader.shader.set_variant_enabled(SKY_VERSION_QUARTER_RES_MULTIVIEW, false);
  646. }
  647. }
  648. // register our shader funds
  649. material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_SKY, _create_sky_shader_funcs);
  650. material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_SKY, _create_sky_material_funcs);
  651. {
  652. ShaderCompiler::DefaultIdentifierActions actions;
  653. actions.renames["COLOR"] = "color";
  654. actions.renames["ALPHA"] = "alpha";
  655. actions.renames["EYEDIR"] = "cube_normal";
  656. actions.renames["POSITION"] = "params.position";
  657. actions.renames["SKY_COORDS"] = "panorama_coords";
  658. actions.renames["SCREEN_UV"] = "uv";
  659. actions.renames["FRAGCOORD"] = "gl_FragCoord";
  660. actions.renames["TIME"] = "params.time";
  661. actions.renames["PI"] = _MKSTR(Math_PI);
  662. actions.renames["TAU"] = _MKSTR(Math_TAU);
  663. actions.renames["E"] = _MKSTR(Math_E);
  664. actions.renames["HALF_RES_COLOR"] = "half_res_color";
  665. actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
  666. actions.renames["RADIANCE"] = "radiance";
  667. actions.renames["FOG"] = "custom_fog";
  668. actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
  669. actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
  670. actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
  671. actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
  672. actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
  673. actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
  674. actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
  675. actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
  676. actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
  677. actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
  678. actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
  679. actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
  680. actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
  681. actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
  682. actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
  683. actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
  684. actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
  685. actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
  686. actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";
  687. actions.renames["LIGHT3_SIZE"] = "directional_lights.data[3].color_size.w";
  688. actions.renames["AT_CUBEMAP_PASS"] = "AT_CUBEMAP_PASS";
  689. actions.renames["AT_HALF_RES_PASS"] = "AT_HALF_RES_PASS";
  690. actions.renames["AT_QUARTER_RES_PASS"] = "AT_QUARTER_RES_PASS";
  691. actions.custom_samplers["RADIANCE"] = "material_samplers[3]";
  692. actions.usage_defines["HALF_RES_COLOR"] = "\n#define USES_HALF_RES_COLOR\n";
  693. actions.usage_defines["QUARTER_RES_COLOR"] = "\n#define USES_QUARTER_RES_COLOR\n";
  694. actions.render_mode_defines["disable_fog"] = "#define DISABLE_FOG\n";
  695. actions.render_mode_defines["use_debanding"] = "#define USE_DEBANDING\n";
  696. actions.sampler_array_name = "material_samplers";
  697. actions.base_texture_binding_index = 1;
  698. actions.texture_layout_set = 1;
  699. actions.base_uniform_string = "material.";
  700. actions.base_varying_index = 10;
  701. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  702. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  703. actions.global_buffer_array_variable = "global_shader_uniforms.data";
  704. sky_shader.compiler.initialize(actions);
  705. }
  706. {
  707. // default material and shader for sky shader
  708. sky_shader.default_shader = material_storage->shader_allocate();
  709. material_storage->shader_initialize(sky_shader.default_shader);
  710. material_storage->shader_set_code(sky_shader.default_shader, R"(
  711. // Default sky shader.
  712. shader_type sky;
  713. void sky() {
  714. COLOR = vec3(0.0);
  715. }
  716. )");
  717. sky_shader.default_material = material_storage->material_allocate();
  718. material_storage->material_initialize(sky_shader.default_material);
  719. material_storage->material_set_shader(sky_shader.default_material, sky_shader.default_shader);
  720. SkyMaterialData *md = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  721. sky_shader.default_shader_rd = sky_shader.shader.version_get_shader(md->shader_data->version, SKY_VERSION_BACKGROUND);
  722. sky_scene_state.uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SkySceneState::UBO));
  723. Vector<RD::Uniform> uniforms;
  724. {
  725. Vector<RID> ids;
  726. ids.resize(12);
  727. RID *ids_ptr = ids.ptrw();
  728. ids_ptr[0] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  729. ids_ptr[1] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  730. ids_ptr[2] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  731. ids_ptr[3] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  732. ids_ptr[4] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  733. ids_ptr[5] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  734. ids_ptr[6] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  735. ids_ptr[7] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  736. ids_ptr[8] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  737. ids_ptr[9] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  738. ids_ptr[10] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  739. ids_ptr[11] = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  740. RD::Uniform u(RD::UNIFORM_TYPE_SAMPLER, 0, ids);
  741. uniforms.push_back(u);
  742. }
  743. {
  744. RD::Uniform u;
  745. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  746. u.binding = 1;
  747. u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer());
  748. uniforms.push_back(u);
  749. }
  750. {
  751. RD::Uniform u;
  752. u.binding = 2;
  753. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  754. u.append_id(sky_scene_state.uniform_buffer);
  755. uniforms.push_back(u);
  756. }
  757. {
  758. RD::Uniform u;
  759. u.binding = 3;
  760. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  761. u.append_id(sky_scene_state.directional_light_buffer);
  762. uniforms.push_back(u);
  763. }
  764. sky_scene_state.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_UNIFORMS);
  765. }
  766. {
  767. Vector<RD::Uniform> uniforms;
  768. {
  769. RD::Uniform u;
  770. u.binding = 0;
  771. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  772. RID vfog = texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
  773. u.append_id(vfog);
  774. uniforms.push_back(u);
  775. }
  776. sky_scene_state.default_fog_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_FOG);
  777. }
  778. {
  779. // Need defaults for using fog with clear color
  780. sky_scene_state.fog_shader = material_storage->shader_allocate();
  781. material_storage->shader_initialize(sky_scene_state.fog_shader);
  782. material_storage->shader_set_code(sky_scene_state.fog_shader, R"(
  783. // Default clear color sky shader.
  784. shader_type sky;
  785. uniform vec4 clear_color;
  786. void sky() {
  787. COLOR = clear_color.rgb;
  788. }
  789. )");
  790. sky_scene_state.fog_material = material_storage->material_allocate();
  791. material_storage->material_initialize(sky_scene_state.fog_material);
  792. material_storage->material_set_shader(sky_scene_state.fog_material, sky_scene_state.fog_shader);
  793. Vector<RD::Uniform> uniforms;
  794. {
  795. RD::Uniform u;
  796. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  797. u.binding = 0;
  798. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  799. uniforms.push_back(u);
  800. }
  801. {
  802. RD::Uniform u;
  803. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  804. u.binding = 1;
  805. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  806. uniforms.push_back(u);
  807. }
  808. {
  809. RD::Uniform u;
  810. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  811. u.binding = 2;
  812. u.append_id(texture_storage->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE));
  813. uniforms.push_back(u);
  814. }
  815. sky_scene_state.fog_only_texture_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_TEXTURES);
  816. }
  817. { //create index array for copy shaders
  818. Vector<uint8_t> pv;
  819. pv.resize(6 * 4);
  820. {
  821. uint8_t *w = pv.ptrw();
  822. int *p32 = (int *)w;
  823. p32[0] = 0;
  824. p32[1] = 1;
  825. p32[2] = 2;
  826. p32[3] = 0;
  827. p32[4] = 2;
  828. p32[5] = 3;
  829. }
  830. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  831. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  832. }
  833. }
  834. void SkyRD::set_texture_format(RD::DataFormat p_texture_format) {
  835. texture_format = p_texture_format;
  836. }
  837. SkyRD::~SkyRD() {
  838. // cleanup anything created in init...
  839. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  840. SkyMaterialData *md = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_shader.default_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  841. sky_shader.shader.version_free(md->shader_data->version);
  842. RD::get_singleton()->free(sky_scene_state.directional_light_buffer);
  843. RD::get_singleton()->free(sky_scene_state.uniform_buffer);
  844. memdelete_arr(sky_scene_state.directional_lights);
  845. memdelete_arr(sky_scene_state.last_frame_directional_lights);
  846. material_storage->shader_free(sky_shader.default_shader);
  847. material_storage->material_free(sky_shader.default_material);
  848. material_storage->shader_free(sky_scene_state.fog_shader);
  849. material_storage->material_free(sky_scene_state.fog_material);
  850. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.uniform_set)) {
  851. RD::get_singleton()->free(sky_scene_state.uniform_set);
  852. }
  853. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.default_fog_uniform_set)) {
  854. RD::get_singleton()->free(sky_scene_state.default_fog_uniform_set);
  855. }
  856. if (RD::get_singleton()->uniform_set_is_valid(sky_scene_state.fog_only_texture_uniform_set)) {
  857. RD::get_singleton()->free(sky_scene_state.fog_only_texture_uniform_set);
  858. }
  859. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  860. }
  861. 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 Size2i p_screen_size, RendererSceneRenderRD *p_scene_render) {
  862. RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
  863. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  864. ERR_FAIL_COND(p_env.is_null());
  865. ERR_FAIL_COND(p_render_buffers.is_null());
  866. // make sure we support our view count
  867. ERR_FAIL_COND(p_view_count == 0);
  868. ERR_FAIL_COND(p_view_count > RendererSceneRender::MAX_RENDER_VIEWS);
  869. SkyMaterialData *material = nullptr;
  870. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  871. RID sky_material;
  872. SkyShaderData *shader_data = nullptr;
  873. if (sky) {
  874. sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  875. if (sky_material.is_valid()) {
  876. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  877. if (!material || !material->shader_data->valid) {
  878. material = nullptr;
  879. }
  880. }
  881. if (!material) {
  882. sky_material = sky_shader.default_material;
  883. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  884. }
  885. ERR_FAIL_COND(!material);
  886. shader_data = material->shader_data;
  887. ERR_FAIL_COND(!shader_data);
  888. material->set_as_used();
  889. // Save our screen size, our buffers will already have been cleared
  890. sky->screen_size.x = p_screen_size.x < 4 ? 4 : p_screen_size.x;
  891. sky->screen_size.y = p_screen_size.y < 4 ? 4 : p_screen_size.y;
  892. // Trigger updating radiance buffers
  893. if (sky->radiance.is_null()) {
  894. invalidate_sky(sky);
  895. update_dirty_skys();
  896. }
  897. if (shader_data->uses_time && p_scene_render->time - sky->prev_time > 0.00001) {
  898. sky->prev_time = p_scene_render->time;
  899. sky->reflection.dirty = true;
  900. RenderingServerDefault::redraw_request();
  901. }
  902. if (material != sky->prev_material) {
  903. sky->prev_material = material;
  904. sky->reflection.dirty = true;
  905. }
  906. if (material->uniform_set_updated) {
  907. material->uniform_set_updated = false;
  908. sky->reflection.dirty = true;
  909. }
  910. if (!p_cam_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  911. sky->prev_position = p_cam_transform.origin;
  912. sky->reflection.dirty = true;
  913. }
  914. sky_scene_state.ubo.directional_light_count = 0;
  915. if (shader_data->uses_light) {
  916. // Run through the list of lights in the scene and pick out the Directional Lights.
  917. // This can't be done in RenderSceneRenderRD::_setup lights because that needs to be called
  918. // after the depth prepass, but this runs before the depth prepass
  919. for (int i = 0; i < (int)p_lights.size(); i++) {
  920. if (!light_storage->owns_light_instance(p_lights[i])) {
  921. continue;
  922. }
  923. RID base = light_storage->light_instance_get_base_light(p_lights[i]);
  924. ERR_CONTINUE(base.is_null());
  925. RS::LightType type = light_storage->light_get_type(base);
  926. if (type == RS::LIGHT_DIRECTIONAL && light_storage->light_directional_get_sky_mode(base) != RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_ONLY) {
  927. SkyDirectionalLightData &sky_light_data = sky_scene_state.directional_lights[sky_scene_state.ubo.directional_light_count];
  928. Transform3D light_transform = light_storage->light_instance_get_base_transform(p_lights[i]);
  929. Vector3 world_direction = light_transform.basis.xform(Vector3(0, 0, 1)).normalized();
  930. sky_light_data.direction[0] = world_direction.x;
  931. sky_light_data.direction[1] = world_direction.y;
  932. sky_light_data.direction[2] = world_direction.z;
  933. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  934. sky_light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  935. if (p_scene_render->is_using_physical_light_units()) {
  936. sky_light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  937. }
  938. if (p_camera_attributes.is_valid()) {
  939. sky_light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_camera_attributes);
  940. }
  941. Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
  942. sky_light_data.color[0] = linear_col.r;
  943. sky_light_data.color[1] = linear_col.g;
  944. sky_light_data.color[2] = linear_col.b;
  945. sky_light_data.enabled = true;
  946. float angular_diameter = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  947. if (angular_diameter > 0.0) {
  948. // I know tan(0) is 0, but let's not risk it with numerical precision.
  949. // technically this will keep expanding until reaching the sun, but all we care
  950. // is expand until we reach the radius of the near plane (there can't be more occluders than that)
  951. angular_diameter = Math::tan(Math::deg_to_rad(angular_diameter));
  952. } else {
  953. angular_diameter = 0.0;
  954. }
  955. sky_light_data.size = angular_diameter;
  956. sky_scene_state.ubo.directional_light_count++;
  957. if (sky_scene_state.ubo.directional_light_count >= sky_scene_state.max_directional_lights) {
  958. break;
  959. }
  960. }
  961. }
  962. // Check whether the directional_light_buffer changes
  963. bool light_data_dirty = false;
  964. // Light buffer is dirty if we have fewer or more lights
  965. // If we have fewer lights, make sure that old lights are disabled
  966. if (sky_scene_state.ubo.directional_light_count != sky_scene_state.last_frame_directional_light_count) {
  967. light_data_dirty = true;
  968. for (uint32_t i = sky_scene_state.ubo.directional_light_count; i < sky_scene_state.max_directional_lights; i++) {
  969. sky_scene_state.directional_lights[i].enabled = false;
  970. sky_scene_state.last_frame_directional_lights[i].enabled = false;
  971. }
  972. }
  973. if (!light_data_dirty) {
  974. for (uint32_t i = 0; i < sky_scene_state.ubo.directional_light_count; i++) {
  975. if (sky_scene_state.directional_lights[i].direction[0] != sky_scene_state.last_frame_directional_lights[i].direction[0] ||
  976. sky_scene_state.directional_lights[i].direction[1] != sky_scene_state.last_frame_directional_lights[i].direction[1] ||
  977. sky_scene_state.directional_lights[i].direction[2] != sky_scene_state.last_frame_directional_lights[i].direction[2] ||
  978. sky_scene_state.directional_lights[i].energy != sky_scene_state.last_frame_directional_lights[i].energy ||
  979. sky_scene_state.directional_lights[i].color[0] != sky_scene_state.last_frame_directional_lights[i].color[0] ||
  980. sky_scene_state.directional_lights[i].color[1] != sky_scene_state.last_frame_directional_lights[i].color[1] ||
  981. sky_scene_state.directional_lights[i].color[2] != sky_scene_state.last_frame_directional_lights[i].color[2] ||
  982. sky_scene_state.directional_lights[i].enabled != sky_scene_state.last_frame_directional_lights[i].enabled ||
  983. sky_scene_state.directional_lights[i].size != sky_scene_state.last_frame_directional_lights[i].size) {
  984. light_data_dirty = true;
  985. break;
  986. }
  987. }
  988. }
  989. if (light_data_dirty) {
  990. 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);
  991. SkyDirectionalLightData *temp = sky_scene_state.last_frame_directional_lights;
  992. sky_scene_state.last_frame_directional_lights = sky_scene_state.directional_lights;
  993. sky_scene_state.directional_lights = temp;
  994. sky_scene_state.last_frame_directional_light_count = sky_scene_state.ubo.directional_light_count;
  995. sky->reflection.dirty = true;
  996. }
  997. }
  998. }
  999. //setup fog variables
  1000. sky_scene_state.ubo.volumetric_fog_enabled = false;
  1001. if (p_render_buffers.is_valid()) {
  1002. if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
  1003. Ref<RendererRD::Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);
  1004. sky_scene_state.ubo.volumetric_fog_enabled = true;
  1005. float fog_end = fog->length;
  1006. if (fog_end > 0.0) {
  1007. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0 / fog_end;
  1008. } else {
  1009. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0;
  1010. }
  1011. float fog_detail_spread = fog->spread; //reverse lookup
  1012. if (fog_detail_spread > 0.0) {
  1013. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0 / fog_detail_spread;
  1014. } else {
  1015. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0;
  1016. }
  1017. sky_scene_state.fog_uniform_set = fog->sky_uniform_set;
  1018. }
  1019. }
  1020. sky_scene_state.view_count = p_view_count;
  1021. sky_scene_state.cam_transform = p_cam_transform;
  1022. sky_scene_state.cam_projection = p_view_projections[0]; // We only use this when rendering a single view
  1023. // Our info in our UBO is only used if we're rendering stereo
  1024. for (uint32_t i = 0; i < p_view_count; i++) {
  1025. RendererRD::MaterialStorage::store_camera(p_view_projections[i].inverse(), sky_scene_state.ubo.view_inv_projections[i]);
  1026. sky_scene_state.ubo.view_eye_offsets[i][0] = p_view_eye_offsets[i].x;
  1027. sky_scene_state.ubo.view_eye_offsets[i][1] = p_view_eye_offsets[i].y;
  1028. sky_scene_state.ubo.view_eye_offsets[i][2] = p_view_eye_offsets[i].z;
  1029. sky_scene_state.ubo.view_eye_offsets[i][3] = 0.0;
  1030. }
  1031. sky_scene_state.ubo.z_far = p_view_projections[0].get_z_far(); // Should be the same for all projection
  1032. sky_scene_state.ubo.fog_enabled = RendererSceneRenderRD::get_singleton()->environment_get_fog_enabled(p_env);
  1033. sky_scene_state.ubo.fog_density = RendererSceneRenderRD::get_singleton()->environment_get_fog_density(p_env);
  1034. sky_scene_state.ubo.fog_aerial_perspective = RendererSceneRenderRD::get_singleton()->environment_get_fog_aerial_perspective(p_env);
  1035. Color fog_color = RendererSceneRenderRD::get_singleton()->environment_get_fog_light_color(p_env).srgb_to_linear();
  1036. float fog_energy = RendererSceneRenderRD::get_singleton()->environment_get_fog_light_energy(p_env);
  1037. sky_scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
  1038. sky_scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
  1039. sky_scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
  1040. sky_scene_state.ubo.fog_sun_scatter = RendererSceneRenderRD::get_singleton()->environment_get_fog_sun_scatter(p_env);
  1041. sky_scene_state.ubo.fog_sky_affect = RendererSceneRenderRD::get_singleton()->environment_get_fog_sky_affect(p_env);
  1042. sky_scene_state.ubo.volumetric_fog_sky_affect = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_sky_affect(p_env);
  1043. RD::get_singleton()->buffer_update(sky_scene_state.uniform_buffer, 0, sizeof(SkySceneState::UBO), &sky_scene_state.ubo);
  1044. }
  1045. 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) {
  1046. ERR_FAIL_COND(p_render_buffers.is_null());
  1047. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  1048. ERR_FAIL_COND(p_env.is_null());
  1049. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1050. ERR_FAIL_COND(!sky);
  1051. RID sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1052. SkyMaterialData *material = nullptr;
  1053. if (sky_material.is_valid()) {
  1054. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1055. if (!material || !material->shader_data->valid) {
  1056. material = nullptr;
  1057. }
  1058. }
  1059. if (!material) {
  1060. sky_material = sky_shader.default_material;
  1061. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1062. }
  1063. ERR_FAIL_COND(!material);
  1064. SkyShaderData *shader_data = material->shader_data;
  1065. ERR_FAIL_COND(!shader_data);
  1066. bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
  1067. RS::SkyMode sky_mode = sky->mode;
  1068. if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
  1069. if (shader_data->uses_time || shader_data->uses_position) {
  1070. update_single_frame = true;
  1071. sky_mode = RS::SKY_MODE_REALTIME;
  1072. } else if (shader_data->uses_light || shader_data->ubo_size > 0) {
  1073. update_single_frame = false;
  1074. sky_mode = RS::SKY_MODE_INCREMENTAL;
  1075. } else {
  1076. update_single_frame = true;
  1077. sky_mode = RS::SKY_MODE_QUALITY;
  1078. }
  1079. }
  1080. if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
  1081. // On the first frame after creating sky, rebuild in single frame
  1082. update_single_frame = true;
  1083. sky_mode = RS::SKY_MODE_QUALITY;
  1084. }
  1085. int max_processing_layer = sky_use_cubemap_array ? sky->reflection.layers.size() : sky->reflection.layers[0].mipmaps.size();
  1086. // Update radiance cubemap
  1087. if (sky->reflection.dirty && (sky->processing_layer >= max_processing_layer || update_single_frame)) {
  1088. static const Vector3 view_normals[6] = {
  1089. Vector3(+1, 0, 0),
  1090. Vector3(-1, 0, 0),
  1091. Vector3(0, +1, 0),
  1092. Vector3(0, -1, 0),
  1093. Vector3(0, 0, +1),
  1094. Vector3(0, 0, -1)
  1095. };
  1096. static const Vector3 view_up[6] = {
  1097. Vector3(0, -1, 0),
  1098. Vector3(0, -1, 0),
  1099. Vector3(0, 0, +1),
  1100. Vector3(0, 0, -1),
  1101. Vector3(0, -1, 0),
  1102. Vector3(0, -1, 0)
  1103. };
  1104. Projection cm;
  1105. cm.set_perspective(90, 1, 0.01, 10.0);
  1106. Projection correction;
  1107. correction.set_depth_correction(true);
  1108. cm = correction * cm;
  1109. // Note, we ignore environment_get_sky_orientation here as this is applied when we do our lookup in our scene shader.
  1110. if (shader_data->uses_quarter_res && roughness_layers >= 3) {
  1111. RD::get_singleton()->draw_command_begin_label("Render Sky to Quarter Res Cubemap");
  1112. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_QUARTER_RES];
  1113. Vector<Color> clear_colors;
  1114. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1115. RD::DrawListID cubemap_draw_list;
  1116. for (int i = 0; i < 6; i++) {
  1117. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1118. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES, sky_shader.default_shader_rd, p_render_buffers);
  1119. 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);
  1120. _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);
  1121. RD::get_singleton()->draw_list_end();
  1122. }
  1123. RD::get_singleton()->draw_command_end_label();
  1124. } else if (shader_data->uses_quarter_res && roughness_layers < 3) {
  1125. 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.");
  1126. }
  1127. if (shader_data->uses_half_res && roughness_layers >= 2) {
  1128. RD::get_singleton()->draw_command_begin_label("Render Sky to Half Res Cubemap");
  1129. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_HALF_RES];
  1130. Vector<Color> clear_colors;
  1131. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1132. RD::DrawListID cubemap_draw_list;
  1133. for (int i = 0; i < 6; i++) {
  1134. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1135. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_CUBEMAP_HALF_RES, sky_shader.default_shader_rd, p_render_buffers);
  1136. 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);
  1137. _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);
  1138. RD::get_singleton()->draw_list_end();
  1139. }
  1140. RD::get_singleton()->draw_command_end_label();
  1141. } else if (shader_data->uses_half_res && roughness_layers < 2) {
  1142. 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.");
  1143. }
  1144. RD::DrawListID cubemap_draw_list;
  1145. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP];
  1146. RD::get_singleton()->draw_command_begin_label("Render Sky Cubemap");
  1147. for (int i = 0; i < 6; i++) {
  1148. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  1149. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_CUBEMAP, sky_shader.default_shader_rd, p_render_buffers);
  1150. 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);
  1151. _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);
  1152. RD::get_singleton()->draw_list_end();
  1153. }
  1154. RD::get_singleton()->draw_command_end_label();
  1155. if (sky_mode == RS::SKY_MODE_REALTIME) {
  1156. sky->reflection.create_reflection_fast_filter(sky_use_cubemap_array);
  1157. if (sky_use_cubemap_array) {
  1158. sky->reflection.update_reflection_mipmaps(0, sky->reflection.layers.size());
  1159. }
  1160. } else {
  1161. if (update_single_frame) {
  1162. for (int i = 1; i < max_processing_layer; i++) {
  1163. sky->reflection.create_reflection_importance_sample(sky_use_cubemap_array, 10, i, sky_ggx_samples_quality);
  1164. }
  1165. if (sky_use_cubemap_array) {
  1166. sky->reflection.update_reflection_mipmaps(0, sky->reflection.layers.size());
  1167. }
  1168. } else {
  1169. if (sky_use_cubemap_array) {
  1170. // Multi-Frame so just update the first array level
  1171. sky->reflection.update_reflection_mipmaps(0, 1);
  1172. }
  1173. }
  1174. sky->processing_layer = 1;
  1175. }
  1176. sky->baked_exposure = p_luminance_multiplier;
  1177. sky->reflection.dirty = false;
  1178. } else {
  1179. if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
  1180. sky->reflection.create_reflection_importance_sample(sky_use_cubemap_array, 10, sky->processing_layer, sky_ggx_samples_quality);
  1181. if (sky_use_cubemap_array) {
  1182. sky->reflection.update_reflection_mipmaps(sky->processing_layer, sky->processing_layer + 1);
  1183. }
  1184. sky->processing_layer++;
  1185. }
  1186. }
  1187. }
  1188. void SkyRD::update_res_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, double p_time, float p_luminance_multiplier) {
  1189. ERR_FAIL_COND(p_render_buffers.is_null());
  1190. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  1191. ERR_FAIL_COND(p_env.is_null());
  1192. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1193. SkyMaterialData *material = nullptr;
  1194. RID sky_material;
  1195. RS::EnvironmentBG background = RendererSceneRenderRD::get_singleton()->environment_get_background(p_env);
  1196. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1197. ERR_FAIL_COND(!sky);
  1198. sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1199. if (sky_material.is_valid()) {
  1200. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1201. if (!material || !material->shader_data->valid) {
  1202. material = nullptr;
  1203. }
  1204. }
  1205. if (!material) {
  1206. sky_material = sky_shader.default_material;
  1207. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1208. }
  1209. }
  1210. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1211. sky_material = sky_scene_state.fog_material;
  1212. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1213. }
  1214. ERR_FAIL_COND(!material);
  1215. SkyShaderData *shader_data = material->shader_data;
  1216. ERR_FAIL_COND(!shader_data);
  1217. if (!shader_data->uses_quarter_res && !shader_data->uses_half_res) {
  1218. return;
  1219. }
  1220. material->set_as_used();
  1221. RENDER_TIMESTAMP("Setup Sky Resolution Buffers");
  1222. RD::get_singleton()->draw_command_begin_label("Setup Sky Resolution Buffers");
  1223. Basis sky_transform = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_env);
  1224. sky_transform.invert();
  1225. float custom_fov = RendererSceneRenderRD::get_singleton()->environment_get_sky_custom_fov(p_env);
  1226. // Camera
  1227. Projection projection = sky_scene_state.cam_projection;
  1228. if (custom_fov && sky_scene_state.view_count == 1) {
  1229. // With custom fov we don't support stereo...
  1230. float near_plane = projection.get_z_near();
  1231. float far_plane = projection.get_z_far();
  1232. float aspect = projection.get_aspect();
  1233. projection.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1234. }
  1235. sky_transform = sky_transform * sky_scene_state.cam_transform.basis;
  1236. if (shader_data->uses_quarter_res) {
  1237. PipelineCacheRD *pipeline = &shader_data->pipelines[sky_scene_state.view_count > 1 ? SKY_VERSION_QUARTER_RES_MULTIVIEW : SKY_VERSION_QUARTER_RES];
  1238. // Grab texture and framebuffer from cache, create if needed...
  1239. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1240. Size2i quarter_size = sky->screen_size / 4;
  1241. RID texture = p_render_buffers->create_texture(RB_SCOPE_SKY, RB_QUARTER_TEXTURE, texture_format, usage_bits, RD::TEXTURE_SAMPLES_1, quarter_size);
  1242. RID framebuffer = FramebufferCacheRD::get_singleton()->get_cache_multiview(sky_scene_state.view_count, texture);
  1243. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_QUARTER_RES, sky_shader.default_shader_rd, p_render_buffers);
  1244. Vector<Color> clear_colors;
  1245. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1246. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1247. _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);
  1248. RD::get_singleton()->draw_list_end();
  1249. }
  1250. if (shader_data->uses_half_res) {
  1251. PipelineCacheRD *pipeline = &shader_data->pipelines[sky_scene_state.view_count > 1 ? SKY_VERSION_HALF_RES_MULTIVIEW : SKY_VERSION_HALF_RES];
  1252. // Grab texture and framebuffer from cache, create if needed...
  1253. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1254. Size2i half_size = sky->screen_size / 2;
  1255. RID texture = p_render_buffers->create_texture(RB_SCOPE_SKY, RB_HALF_TEXTURE, texture_format, usage_bits, RD::TEXTURE_SAMPLES_1, half_size);
  1256. RID framebuffer = FramebufferCacheRD::get_singleton()->get_cache_multiview(sky_scene_state.view_count, texture);
  1257. RID texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_HALF_RES, sky_shader.default_shader_rd, p_render_buffers);
  1258. Vector<Color> clear_colors;
  1259. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1260. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1261. _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);
  1262. RD::get_singleton()->draw_list_end();
  1263. }
  1264. RD::get_singleton()->draw_command_end_label(); // Setup Sky resolution buffers
  1265. }
  1266. 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) {
  1267. ERR_FAIL_COND(p_render_buffers.is_null());
  1268. RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
  1269. ERR_FAIL_COND(p_env.is_null());
  1270. Sky *sky = get_sky(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1271. SkyMaterialData *material = nullptr;
  1272. RID sky_material;
  1273. RS::EnvironmentBG background = RendererSceneRenderRD::get_singleton()->environment_get_background(p_env);
  1274. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1275. ERR_FAIL_COND(!sky);
  1276. sky_material = sky_get_material(RendererSceneRenderRD::get_singleton()->environment_get_sky(p_env));
  1277. if (sky_material.is_valid()) {
  1278. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1279. if (!material || !material->shader_data->valid) {
  1280. material = nullptr;
  1281. }
  1282. }
  1283. if (!material) {
  1284. sky_material = sky_shader.default_material;
  1285. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1286. }
  1287. }
  1288. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1289. sky_material = sky_scene_state.fog_material;
  1290. material = static_cast<SkyMaterialData *>(material_storage->material_get_data(sky_material, RendererRD::MaterialStorage::SHADER_TYPE_SKY));
  1291. }
  1292. ERR_FAIL_COND(!material);
  1293. SkyShaderData *shader_data = material->shader_data;
  1294. ERR_FAIL_COND(!shader_data);
  1295. material->set_as_used();
  1296. Basis sky_transform = RendererSceneRenderRD::get_singleton()->environment_get_sky_orientation(p_env);
  1297. sky_transform.invert();
  1298. float custom_fov = RendererSceneRenderRD::get_singleton()->environment_get_sky_custom_fov(p_env);
  1299. // Camera
  1300. Projection projection = sky_scene_state.cam_projection;
  1301. if (custom_fov && sky_scene_state.view_count == 1) {
  1302. // With custom fov we don't support stereo...
  1303. float near_plane = projection.get_z_near();
  1304. float far_plane = projection.get_z_far();
  1305. float aspect = projection.get_aspect();
  1306. projection.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1307. }
  1308. sky_transform = sky_transform * sky_scene_state.cam_transform.basis;
  1309. PipelineCacheRD *pipeline = &shader_data->pipelines[sky_scene_state.view_count > 1 ? SKY_VERSION_BACKGROUND_MULTIVIEW : SKY_VERSION_BACKGROUND];
  1310. RID texture_uniform_set;
  1311. if (sky) {
  1312. texture_uniform_set = sky->get_textures(SKY_TEXTURE_SET_BACKGROUND, sky_shader.default_shader_rd, p_render_buffers);
  1313. } else {
  1314. texture_uniform_set = sky_scene_state.fog_only_texture_uniform_set;
  1315. }
  1316. _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);
  1317. }
  1318. void SkyRD::invalidate_sky(Sky *p_sky) {
  1319. if (!p_sky->dirty) {
  1320. p_sky->dirty = true;
  1321. p_sky->dirty_list = dirty_sky_list;
  1322. dirty_sky_list = p_sky;
  1323. }
  1324. }
  1325. void SkyRD::update_dirty_skys() {
  1326. Sky *sky = dirty_sky_list;
  1327. while (sky) {
  1328. //update sky configuration if texture is missing
  1329. // TODO See if we can move this into `update_radiance_buffers` and remove our dirty_sky logic.
  1330. // As this is basically a duplicate of the logic in reflection probes we could move this logic
  1331. // into RenderSceneBuffersRD and use that from both places.
  1332. if (sky->radiance.is_null()) {
  1333. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  1334. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  1335. int layers = roughness_layers;
  1336. if (sky->mode == RS::SKY_MODE_REALTIME) {
  1337. layers = 8;
  1338. if (roughness_layers != 8) {
  1339. 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.");
  1340. }
  1341. }
  1342. if (sky_use_cubemap_array) {
  1343. //array (higher quality, 6 times more memory)
  1344. RD::TextureFormat tf;
  1345. tf.array_layers = layers * 6;
  1346. tf.format = texture_format;
  1347. tf.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  1348. tf.mipmaps = mipmaps;
  1349. tf.width = w;
  1350. tf.height = h;
  1351. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1352. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  1353. tf.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT;
  1354. }
  1355. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1356. sky->reflection.update_reflection_data(sky->radiance_size, mipmaps, true, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME, roughness_layers, texture_format);
  1357. } else {
  1358. //regular cubemap, lower quality (aliasing, less memory)
  1359. RD::TextureFormat tf;
  1360. tf.array_layers = 6;
  1361. tf.format = texture_format;
  1362. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  1363. tf.mipmaps = MIN(mipmaps, layers);
  1364. tf.width = w;
  1365. tf.height = h;
  1366. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  1367. if (RendererSceneRenderRD::get_singleton()->_render_buffers_can_be_storage()) {
  1368. tf.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT;
  1369. }
  1370. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1371. 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);
  1372. }
  1373. }
  1374. sky->reflection.dirty = true;
  1375. sky->processing_layer = 0;
  1376. Sky *next = sky->dirty_list;
  1377. sky->dirty_list = nullptr;
  1378. sky->dirty = false;
  1379. sky = next;
  1380. }
  1381. dirty_sky_list = nullptr;
  1382. }
  1383. RID SkyRD::sky_get_material(RID p_sky) const {
  1384. Sky *sky = get_sky(p_sky);
  1385. ERR_FAIL_COND_V(!sky, RID());
  1386. return sky->material;
  1387. }
  1388. float SkyRD::sky_get_baked_exposure(RID p_sky) const {
  1389. Sky *sky = get_sky(p_sky);
  1390. ERR_FAIL_COND_V(!sky, 1.0);
  1391. return sky->baked_exposure;
  1392. }
  1393. RID SkyRD::allocate_sky_rid() {
  1394. return sky_owner.allocate_rid();
  1395. }
  1396. void SkyRD::initialize_sky_rid(RID p_rid) {
  1397. sky_owner.initialize_rid(p_rid, Sky());
  1398. }
  1399. SkyRD::Sky *SkyRD::get_sky(RID p_sky) const {
  1400. return sky_owner.get_or_null(p_sky);
  1401. }
  1402. void SkyRD::free_sky(RID p_sky) {
  1403. Sky *sky = get_sky(p_sky);
  1404. ERR_FAIL_COND(!sky);
  1405. sky->free();
  1406. sky_owner.free(p_sky);
  1407. }
  1408. void SkyRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  1409. Sky *sky = get_sky(p_sky);
  1410. ERR_FAIL_COND(!sky);
  1411. if (sky->set_radiance_size(p_radiance_size)) {
  1412. invalidate_sky(sky);
  1413. }
  1414. }
  1415. void SkyRD::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  1416. Sky *sky = get_sky(p_sky);
  1417. ERR_FAIL_COND(!sky);
  1418. if (sky->set_mode(p_mode)) {
  1419. invalidate_sky(sky);
  1420. }
  1421. }
  1422. void SkyRD::sky_set_material(RID p_sky, RID p_material) {
  1423. Sky *sky = get_sky(p_sky);
  1424. ERR_FAIL_COND(!sky);
  1425. if (sky->set_material(p_material)) {
  1426. invalidate_sky(sky);
  1427. }
  1428. }
  1429. Ref<Image> SkyRD::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  1430. Sky *sky = get_sky(p_sky);
  1431. ERR_FAIL_COND_V(!sky, Ref<Image>());
  1432. update_dirty_skys();
  1433. return sky->bake_panorama(p_energy, p_bake_irradiance ? roughness_layers : 0, p_size);
  1434. }
  1435. RID SkyRD::sky_get_radiance_texture_rd(RID p_sky) const {
  1436. Sky *sky = get_sky(p_sky);
  1437. ERR_FAIL_COND_V(!sky, RID());
  1438. return sky->radiance;
  1439. }