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[2]; // 8 - 88
  92. float luminance_multiplier; // 4 - 92
  93. float brightness_multiplier; // 4 - 96
  94. // 128 is the max size of a push constant. We can replace "pad" but we can't add any more.
  95. };
  96. struct SkyShaderData : public RendererRD::MaterialStorage::ShaderData {
  97. bool valid = false;
  98. RID version;
  99. PipelineCacheRD pipelines[SKY_VERSION_MAX];
  100. Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
  101. Vector<uint32_t> ubo_offsets;
  102. uint32_t ubo_size = 0;
  103. String code;
  104. bool uses_time = false;
  105. bool uses_position = false;
  106. bool uses_half_res = false;
  107. bool uses_quarter_res = false;
  108. bool uses_light = false;
  109. virtual void set_code(const String &p_Code);
  110. virtual bool is_animated() const;
  111. virtual bool casts_shadows() const;
  112. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  113. SkyShaderData() {}
  114. virtual ~SkyShaderData();
  115. };
  116. 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, float p_brightness_modifier);
  117. public:
  118. struct SkySceneState {
  119. struct UBO {
  120. float combined_reprojection[RendererSceneRender::MAX_RENDER_VIEWS][16]; // 2 x 64 - 128
  121. float view_inv_projections[RendererSceneRender::MAX_RENDER_VIEWS][16]; // 2 x 64 - 256
  122. float view_eye_offsets[RendererSceneRender::MAX_RENDER_VIEWS][4]; // 2 x 16 - 288
  123. uint32_t volumetric_fog_enabled; // 4 - 292
  124. float volumetric_fog_inv_length; // 4 - 296
  125. float volumetric_fog_detail_spread; // 4 - 300
  126. float volumetric_fog_sky_affect; // 4 - 304
  127. uint32_t fog_enabled; // 4 - 308
  128. float fog_sky_affect; // 4 - 312
  129. float fog_density; // 4 - 316
  130. float fog_sun_scatter; // 4 - 320
  131. float fog_light_color[3]; // 12 - 332
  132. float fog_aerial_perspective; // 4 - 336
  133. float z_far; // 4 - 340
  134. uint32_t directional_light_count; // 4 - 344
  135. uint32_t pad1; // 4 - 348
  136. uint32_t pad2; // 4 - 352
  137. };
  138. UBO ubo;
  139. uint32_t view_count = 1;
  140. Transform3D cam_transform;
  141. Projection cam_projection;
  142. SkyDirectionalLightData *directional_lights = nullptr;
  143. SkyDirectionalLightData *last_frame_directional_lights = nullptr;
  144. uint32_t max_directional_lights;
  145. uint32_t last_frame_directional_light_count;
  146. RID directional_light_buffer;
  147. RID uniform_set;
  148. RID uniform_buffer;
  149. RID fog_uniform_set;
  150. RID default_fog_uniform_set;
  151. RID fog_shader;
  152. RID fog_material;
  153. RID fog_only_texture_uniform_set;
  154. } sky_scene_state;
  155. struct ReflectionData {
  156. struct Layer {
  157. struct Mipmap {
  158. RID framebuffers[6];
  159. RID views[6];
  160. Size2i size;
  161. };
  162. Vector<Mipmap> mipmaps; //per-face view
  163. Vector<RID> views; // per-cubemap view
  164. };
  165. struct DownsampleLayer {
  166. struct Mipmap {
  167. RID view;
  168. Size2i size;
  169. // for mobile only
  170. RID views[6];
  171. RID framebuffers[6];
  172. };
  173. Vector<Mipmap> mipmaps;
  174. };
  175. RID radiance_base_cubemap; //cubemap for first layer, first cubemap
  176. RID downsampled_radiance_cubemap;
  177. DownsampleLayer downsampled_layer;
  178. RID coefficient_buffer;
  179. bool dirty = true;
  180. Vector<Layer> layers;
  181. void clear_reflection_data();
  182. 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);
  183. void create_reflection_fast_filter(bool p_use_arrays);
  184. void create_reflection_importance_sample(bool p_use_arrays, int p_cube_side, int p_base_layer, uint32_t p_sky_ggx_samples_quality);
  185. void update_reflection_mipmaps(int p_start, int p_end);
  186. };
  187. /* Sky shader */
  188. struct SkyShader {
  189. SkyShaderRD shader;
  190. ShaderCompiler compiler;
  191. RID default_shader;
  192. RID default_material;
  193. RID default_shader_rd;
  194. } sky_shader;
  195. struct SkyMaterialData : public RendererRD::MaterialStorage::MaterialData {
  196. SkyShaderData *shader_data = nullptr;
  197. RID uniform_set;
  198. bool uniform_set_updated;
  199. virtual void set_render_priority(int p_priority) {}
  200. virtual void set_next_pass(RID p_pass) {}
  201. virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  202. virtual ~SkyMaterialData();
  203. };
  204. struct Sky {
  205. RID radiance;
  206. RID quarter_res_pass;
  207. RID quarter_res_framebuffer;
  208. Size2i screen_size;
  209. RID uniform_set;
  210. RID material;
  211. RID uniform_buffer;
  212. int radiance_size = 256;
  213. RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
  214. ReflectionData reflection;
  215. bool dirty = false;
  216. int processing_layer = 0;
  217. Sky *dirty_list = nullptr;
  218. float baked_exposure = 1.0;
  219. //State to track when radiance cubemap needs updating
  220. SkyMaterialData *prev_material = nullptr;
  221. Vector3 prev_position;
  222. float prev_time;
  223. void free();
  224. RID get_textures(SkyTextureSetVersion p_version, RID p_default_shader_rd, Ref<RenderSceneBuffersRD> p_render_buffers);
  225. bool set_radiance_size(int p_radiance_size);
  226. bool set_mode(RS::SkyMode p_mode);
  227. bool set_material(RID p_material);
  228. Ref<Image> bake_panorama(float p_energy, int p_roughness_layers, const Size2i &p_size);
  229. };
  230. uint32_t sky_ggx_samples_quality;
  231. bool sky_use_cubemap_array;
  232. Sky *dirty_sky_list = nullptr;
  233. mutable RID_Owner<Sky, true> sky_owner;
  234. int roughness_layers;
  235. RendererRD::MaterialStorage::ShaderData *_create_sky_shader_func();
  236. static RendererRD::MaterialStorage::ShaderData *_create_sky_shader_funcs();
  237. RendererRD::MaterialStorage::MaterialData *_create_sky_material_func(SkyShaderData *p_shader);
  238. static RendererRD::MaterialStorage::MaterialData *_create_sky_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader);
  239. SkyRD();
  240. void init();
  241. void set_texture_format(RD::DataFormat p_texture_format);
  242. ~SkyRD();
  243. void setup_sky(const RenderDataRD *p_render_data, const Size2i p_screen_size);
  244. 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, float p_brightness_multiplier = 1.0);
  245. void update_res_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, double p_time, float p_luminance_multiplier = 1.0, float p_brightness_multiplier = 1.0);
  246. 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, float p_brightness_multiplier = 1.0);
  247. void invalidate_sky(Sky *p_sky);
  248. void update_dirty_skys();
  249. RID sky_get_material(RID p_sky) const;
  250. RID sky_get_radiance_texture_rd(RID p_sky) const;
  251. float sky_get_baked_exposure(RID p_sky) const;
  252. RID allocate_sky_rid();
  253. void initialize_sky_rid(RID p_rid);
  254. Sky *get_sky(RID p_sky) const;
  255. void free_sky(RID p_sky);
  256. void sky_set_radiance_size(RID p_sky, int p_radiance_size);
  257. void sky_set_mode(RID p_sky, RS::SkyMode p_mode);
  258. void sky_set_material(RID p_sky, RID p_material);
  259. Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size);
  260. };
  261. } // namespace RendererRD
  262. #endif // SKY_RD_H