renderer_scene_sky_rd.cpp 77 KB

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