sky.cpp 79 KB

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