sky.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**************************************************************************/
  2. /* sky.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef SKY_RD_H
  31. #define SKY_RD_H
  32. #include "core/templates/rid_owner.h"
  33. #include "servers/rendering/renderer_compositor.h"
  34. #include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
  35. #include "servers/rendering/renderer_rd/shaders/environment/sky.glsl.gen.h"
  36. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  37. #include "servers/rendering/renderer_rd/storage_rd/render_data_rd.h"
  38. #include "servers/rendering/renderer_scene_render.h"
  39. #include "servers/rendering/rendering_device.h"
  40. #include "servers/rendering/shader_compiler.h"
  41. // Forward declare RendererSceneRenderRD so we can pass it into some of our methods, these classes are pretty tightly bound
  42. class RendererSceneRenderRD;
  43. class RenderSceneBuffersRD;
  44. namespace RendererRD {
  45. class SkyRD {
  46. public:
  47. enum SkySet {
  48. SKY_SET_UNIFORMS,
  49. SKY_SET_MATERIAL,
  50. SKY_SET_TEXTURES,
  51. SKY_SET_FOG,
  52. };
  53. const int SAMPLERS_BINDING_FIRST_INDEX = 4;
  54. // Skys need less info from Directional Lights than the normal shaders
  55. struct SkyDirectionalLightData {
  56. float direction[3];
  57. float energy;
  58. float color[3];
  59. float size;
  60. uint32_t enabled;
  61. uint32_t pad[3];
  62. };
  63. private:
  64. RD::DataFormat texture_format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  65. enum SkyTextureSetVersion {
  66. SKY_TEXTURE_SET_BACKGROUND,
  67. SKY_TEXTURE_SET_HALF_RES,
  68. SKY_TEXTURE_SET_QUARTER_RES,
  69. SKY_TEXTURE_SET_CUBEMAP,
  70. SKY_TEXTURE_SET_CUBEMAP_HALF_RES,
  71. SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES,
  72. SKY_TEXTURE_SET_MAX
  73. };
  74. enum SkyVersion {
  75. SKY_VERSION_BACKGROUND,
  76. SKY_VERSION_HALF_RES,
  77. SKY_VERSION_QUARTER_RES,
  78. SKY_VERSION_CUBEMAP,
  79. SKY_VERSION_CUBEMAP_HALF_RES,
  80. SKY_VERSION_CUBEMAP_QUARTER_RES,
  81. SKY_VERSION_BACKGROUND_MULTIVIEW,
  82. SKY_VERSION_HALF_RES_MULTIVIEW,
  83. SKY_VERSION_QUARTER_RES_MULTIVIEW,
  84. SKY_VERSION_MAX
  85. };
  86. struct SkyPushConstant {
  87. float orientation[12]; // 48 - 48
  88. float projection[4]; // 16 - 64
  89. float position[3]; // 12 - 76
  90. float time; // 4 - 80
  91. float pad[3]; // 12 - 92
  92. float luminance_multiplier; // 4 - 96
  93. // 128 is the max size of a push constant. We can replace "pad" but we can't add any more.
  94. };
  95. struct SkyShaderData : public RendererRD::MaterialStorage::ShaderData {
  96. bool valid = false;
  97. RID version;
  98. PipelineCacheRD pipelines[SKY_VERSION_MAX];
  99. Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
  100. Vector<uint32_t> ubo_offsets;
  101. uint32_t ubo_size = 0;
  102. String code;
  103. bool uses_time = false;
  104. bool uses_position = false;
  105. bool uses_half_res = false;
  106. bool uses_quarter_res = false;
  107. bool uses_light = false;
  108. virtual void set_code(const String &p_Code);
  109. virtual bool is_animated() const;
  110. virtual bool casts_shadows() const;
  111. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  112. SkyShaderData() {}
  113. virtual ~SkyShaderData();
  114. };
  115. void _render_sky(RD::DrawListID p_list, float p_time, RID p_fb, PipelineCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, const Projection &p_projection, const Basis &p_orientation, const Vector3 &p_position, float p_luminance_multiplier);
  116. public:
  117. struct SkySceneState {
  118. struct UBO {
  119. float combined_reprojection[RendererSceneRender::MAX_RENDER_VIEWS][16]; // 2 x 64 - 128
  120. float view_inv_projections[RendererSceneRender::MAX_RENDER_VIEWS][16]; // 2 x 64 - 256
  121. float view_eye_offsets[RendererSceneRender::MAX_RENDER_VIEWS][4]; // 2 x 16 - 288
  122. uint32_t volumetric_fog_enabled; // 4 - 292
  123. float volumetric_fog_inv_length; // 4 - 296
  124. float volumetric_fog_detail_spread; // 4 - 300
  125. float volumetric_fog_sky_affect; // 4 - 304
  126. uint32_t fog_enabled; // 4 - 308
  127. float fog_sky_affect; // 4 - 312
  128. float fog_density; // 4 - 316
  129. float fog_sun_scatter; // 4 - 320
  130. float fog_light_color[3]; // 12 - 332
  131. float fog_aerial_perspective; // 4 - 336
  132. float z_far; // 4 - 340
  133. uint32_t directional_light_count; // 4 - 344
  134. uint32_t pad1; // 4 - 348
  135. uint32_t pad2; // 4 - 352
  136. };
  137. UBO ubo;
  138. uint32_t view_count = 1;
  139. Transform3D cam_transform;
  140. Projection cam_projection;
  141. SkyDirectionalLightData *directional_lights = nullptr;
  142. SkyDirectionalLightData *last_frame_directional_lights = nullptr;
  143. uint32_t max_directional_lights;
  144. uint32_t last_frame_directional_light_count;
  145. RID directional_light_buffer;
  146. RID uniform_set;
  147. RID uniform_buffer;
  148. RID fog_uniform_set;
  149. RID default_fog_uniform_set;
  150. RID fog_shader;
  151. RID fog_material;
  152. RID fog_only_texture_uniform_set;
  153. } sky_scene_state;
  154. struct ReflectionData {
  155. struct Layer {
  156. struct Mipmap {
  157. RID framebuffers[6];
  158. RID views[6];
  159. Size2i size;
  160. };
  161. Vector<Mipmap> mipmaps; //per-face view
  162. Vector<RID> views; // per-cubemap view
  163. };
  164. struct DownsampleLayer {
  165. struct Mipmap {
  166. RID view;
  167. Size2i size;
  168. // for mobile only
  169. RID views[6];
  170. RID framebuffers[6];
  171. };
  172. Vector<Mipmap> mipmaps;
  173. };
  174. RID radiance_base_cubemap; //cubemap for first layer, first cubemap
  175. RID downsampled_radiance_cubemap;
  176. DownsampleLayer downsampled_layer;
  177. RID coefficient_buffer;
  178. bool dirty = true;
  179. Vector<Layer> layers;
  180. void clear_reflection_data();
  181. void 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);
  182. void create_reflection_fast_filter(bool p_use_arrays);
  183. void create_reflection_importance_sample(bool p_use_arrays, int p_cube_side, int p_base_layer, uint32_t p_sky_ggx_samples_quality);
  184. void update_reflection_mipmaps(int p_start, int p_end);
  185. };
  186. /* Sky shader */
  187. struct SkyShader {
  188. SkyShaderRD shader;
  189. ShaderCompiler compiler;
  190. RID default_shader;
  191. RID default_material;
  192. RID default_shader_rd;
  193. } sky_shader;
  194. struct SkyMaterialData : public RendererRD::MaterialStorage::MaterialData {
  195. SkyShaderData *shader_data = nullptr;
  196. RID uniform_set;
  197. bool uniform_set_updated;
  198. virtual void set_render_priority(int p_priority) {}
  199. virtual void set_next_pass(RID p_pass) {}
  200. virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  201. virtual ~SkyMaterialData();
  202. };
  203. struct Sky {
  204. RID radiance;
  205. RID quarter_res_pass;
  206. RID quarter_res_framebuffer;
  207. Size2i screen_size;
  208. RID uniform_set;
  209. RID material;
  210. RID uniform_buffer;
  211. int radiance_size = 256;
  212. RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
  213. ReflectionData reflection;
  214. bool dirty = false;
  215. int processing_layer = 0;
  216. Sky *dirty_list = nullptr;
  217. float baked_exposure = 1.0;
  218. //State to track when radiance cubemap needs updating
  219. SkyMaterialData *prev_material = nullptr;
  220. Vector3 prev_position;
  221. float prev_time;
  222. void free();
  223. RID get_textures(SkyTextureSetVersion p_version, RID p_default_shader_rd, Ref<RenderSceneBuffersRD> p_render_buffers);
  224. bool set_radiance_size(int p_radiance_size);
  225. bool set_mode(RS::SkyMode p_mode);
  226. bool set_material(RID p_material);
  227. Ref<Image> bake_panorama(float p_energy, int p_roughness_layers, const Size2i &p_size);
  228. };
  229. uint32_t sky_ggx_samples_quality;
  230. bool sky_use_cubemap_array;
  231. Sky *dirty_sky_list = nullptr;
  232. mutable RID_Owner<Sky, true> sky_owner;
  233. int roughness_layers;
  234. RendererRD::MaterialStorage::ShaderData *_create_sky_shader_func();
  235. static RendererRD::MaterialStorage::ShaderData *_create_sky_shader_funcs();
  236. RendererRD::MaterialStorage::MaterialData *_create_sky_material_func(SkyShaderData *p_shader);
  237. static RendererRD::MaterialStorage::MaterialData *_create_sky_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader);
  238. SkyRD();
  239. void init();
  240. void set_texture_format(RD::DataFormat p_texture_format);
  241. ~SkyRD();
  242. void setup_sky(const RenderDataRD *p_render_data, const Size2i p_screen_size);
  243. void update_radiance_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, const Vector3 &p_global_pos, double p_time, float p_luminance_multiplier = 1.0);
  244. void update_res_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, double p_time, float p_luminance_multiplier = 1.0);
  245. void draw_sky(RD::DrawListID p_draw_list, Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, RID p_fb, double p_time, float p_luminance_multiplier = 1.0);
  246. void invalidate_sky(Sky *p_sky);
  247. void update_dirty_skys();
  248. RID sky_get_material(RID p_sky) const;
  249. RID sky_get_radiance_texture_rd(RID p_sky) const;
  250. float sky_get_baked_exposure(RID p_sky) const;
  251. RID allocate_sky_rid();
  252. void initialize_sky_rid(RID p_rid);
  253. Sky *get_sky(RID p_sky) const;
  254. void free_sky(RID p_sky);
  255. void sky_set_radiance_size(RID p_sky, int p_radiance_size);
  256. void sky_set_mode(RID p_sky, RS::SkyMode p_mode);
  257. void sky_set_material(RID p_sky, RID p_material);
  258. Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size);
  259. };
  260. } // namespace RendererRD
  261. #endif // SKY_RD_H