sky.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_scene_render.h"
  38. #include "servers/rendering/rendering_device.h"
  39. #include "servers/rendering/shader_compiler.h"
  40. // Forward declare RendererSceneRenderRD so we can pass it into some of our methods, these classes are pretty tightly bound
  41. class RendererSceneRenderRD;
  42. class RenderSceneBuffersRD;
  43. namespace RendererRD {
  44. class SkyRD {
  45. public:
  46. enum SkySet {
  47. SKY_SET_UNIFORMS,
  48. SKY_SET_MATERIAL,
  49. SKY_SET_TEXTURES,
  50. SKY_SET_FOG,
  51. };
  52. const int SAMPLERS_BINDING_FIRST_INDEX = 4;
  53. // Skys need less info from Directional Lights than the normal shaders
  54. struct SkyDirectionalLightData {
  55. float direction[3];
  56. float energy;
  57. float color[3];
  58. float size;
  59. uint32_t enabled;
  60. uint32_t pad[3];
  61. };
  62. private:
  63. RD::DataFormat texture_format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  64. enum SkyTextureSetVersion {
  65. SKY_TEXTURE_SET_BACKGROUND,
  66. SKY_TEXTURE_SET_HALF_RES,
  67. SKY_TEXTURE_SET_QUARTER_RES,
  68. SKY_TEXTURE_SET_CUBEMAP,
  69. SKY_TEXTURE_SET_CUBEMAP_HALF_RES,
  70. SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES,
  71. SKY_TEXTURE_SET_MAX
  72. };
  73. enum SkyVersion {
  74. SKY_VERSION_BACKGROUND,
  75. SKY_VERSION_HALF_RES,
  76. SKY_VERSION_QUARTER_RES,
  77. SKY_VERSION_CUBEMAP,
  78. SKY_VERSION_CUBEMAP_HALF_RES,
  79. SKY_VERSION_CUBEMAP_QUARTER_RES,
  80. SKY_VERSION_BACKGROUND_MULTIVIEW,
  81. SKY_VERSION_HALF_RES_MULTIVIEW,
  82. SKY_VERSION_QUARTER_RES_MULTIVIEW,
  83. SKY_VERSION_MAX
  84. };
  85. struct SkyPushConstant {
  86. float orientation[12]; // 48 - 48
  87. float projection[4]; // 16 - 64
  88. float position[3]; // 12 - 76
  89. float time; // 4 - 80
  90. float pad[3]; // 12 - 92
  91. float luminance_multiplier; // 4 - 96
  92. // 128 is the max size of a push constant. We can replace "pad" but we can't add any more.
  93. };
  94. struct SkyShaderData : public RendererRD::MaterialStorage::ShaderData {
  95. bool valid = false;
  96. RID version;
  97. PipelineCacheRD pipelines[SKY_VERSION_MAX];
  98. Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
  99. Vector<uint32_t> ubo_offsets;
  100. uint32_t ubo_size = 0;
  101. String code;
  102. bool uses_time = false;
  103. bool uses_position = false;
  104. bool uses_half_res = false;
  105. bool uses_quarter_res = false;
  106. bool uses_light = false;
  107. virtual void set_code(const String &p_Code);
  108. virtual bool is_animated() const;
  109. virtual bool casts_shadows() const;
  110. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  111. SkyShaderData() {}
  112. virtual ~SkyShaderData();
  113. };
  114. 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);
  115. public:
  116. struct SkySceneState {
  117. struct UBO {
  118. float combined_reprojection[RendererSceneRender::MAX_RENDER_VIEWS][16]; // 2 x 64 - 128
  119. float view_inv_projections[RendererSceneRender::MAX_RENDER_VIEWS][16]; // 2 x 64 - 256
  120. float view_eye_offsets[RendererSceneRender::MAX_RENDER_VIEWS][4]; // 2 x 16 - 288
  121. uint32_t volumetric_fog_enabled; // 4 - 292
  122. float volumetric_fog_inv_length; // 4 - 296
  123. float volumetric_fog_detail_spread; // 4 - 300
  124. float volumetric_fog_sky_affect; // 4 - 304
  125. uint32_t fog_enabled; // 4 - 308
  126. float fog_sky_affect; // 4 - 312
  127. float fog_density; // 4 - 316
  128. float fog_sun_scatter; // 4 - 320
  129. float fog_light_color[3]; // 12 - 332
  130. float fog_aerial_perspective; // 4 - 336
  131. float z_far; // 4 - 340
  132. uint32_t directional_light_count; // 4 - 344
  133. uint32_t pad1; // 4 - 348
  134. uint32_t pad2; // 4 - 352
  135. };
  136. UBO ubo;
  137. uint32_t view_count = 1;
  138. Transform3D cam_transform;
  139. Projection cam_projection;
  140. SkyDirectionalLightData *directional_lights = nullptr;
  141. SkyDirectionalLightData *last_frame_directional_lights = nullptr;
  142. uint32_t max_directional_lights;
  143. uint32_t last_frame_directional_light_count;
  144. RID directional_light_buffer;
  145. RID uniform_set;
  146. RID uniform_buffer;
  147. RID fog_uniform_set;
  148. RID default_fog_uniform_set;
  149. RID fog_shader;
  150. RID fog_material;
  151. RID fog_only_texture_uniform_set;
  152. } sky_scene_state;
  153. struct ReflectionData {
  154. struct Layer {
  155. struct Mipmap {
  156. RID framebuffers[6];
  157. RID views[6];
  158. Size2i size;
  159. };
  160. Vector<Mipmap> mipmaps; //per-face view
  161. Vector<RID> views; // per-cubemap view
  162. };
  163. struct DownsampleLayer {
  164. struct Mipmap {
  165. RID view;
  166. Size2i size;
  167. // for mobile only
  168. RID views[6];
  169. RID framebuffers[6];
  170. };
  171. Vector<Mipmap> mipmaps;
  172. };
  173. RID radiance_base_cubemap; //cubemap for first layer, first cubemap
  174. RID downsampled_radiance_cubemap;
  175. DownsampleLayer downsampled_layer;
  176. RID coefficient_buffer;
  177. bool dirty = true;
  178. Vector<Layer> layers;
  179. void clear_reflection_data();
  180. 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);
  181. void create_reflection_fast_filter(bool p_use_arrays);
  182. void create_reflection_importance_sample(bool p_use_arrays, int p_cube_side, int p_base_layer, uint32_t p_sky_ggx_samples_quality);
  183. void update_reflection_mipmaps(int p_start, int p_end);
  184. };
  185. /* Sky shader */
  186. struct SkyShader {
  187. SkyShaderRD shader;
  188. ShaderCompiler compiler;
  189. RID default_shader;
  190. RID default_material;
  191. RID default_shader_rd;
  192. } sky_shader;
  193. struct SkyMaterialData : public RendererRD::MaterialStorage::MaterialData {
  194. SkyShaderData *shader_data = nullptr;
  195. RID uniform_set;
  196. bool uniform_set_updated;
  197. virtual void set_render_priority(int p_priority) {}
  198. virtual void set_next_pass(RID p_pass) {}
  199. virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  200. virtual ~SkyMaterialData();
  201. };
  202. struct Sky {
  203. RID radiance;
  204. RID quarter_res_pass;
  205. RID quarter_res_framebuffer;
  206. Size2i screen_size;
  207. RID uniform_set;
  208. RID material;
  209. RID uniform_buffer;
  210. int radiance_size = 256;
  211. RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
  212. ReflectionData reflection;
  213. bool dirty = false;
  214. int processing_layer = 0;
  215. Sky *dirty_list = nullptr;
  216. float baked_exposure = 1.0;
  217. //State to track when radiance cubemap needs updating
  218. SkyMaterialData *prev_material = nullptr;
  219. Vector3 prev_position;
  220. float prev_time;
  221. void free();
  222. RID get_textures(SkyTextureSetVersion p_version, RID p_default_shader_rd, Ref<RenderSceneBuffersRD> p_render_buffers);
  223. bool set_radiance_size(int p_radiance_size);
  224. bool set_mode(RS::SkyMode p_mode);
  225. bool set_material(RID p_material);
  226. Ref<Image> bake_panorama(float p_energy, int p_roughness_layers, const Size2i &p_size);
  227. };
  228. uint32_t sky_ggx_samples_quality;
  229. bool sky_use_cubemap_array;
  230. Sky *dirty_sky_list = nullptr;
  231. mutable RID_Owner<Sky, true> sky_owner;
  232. int roughness_layers;
  233. RendererRD::MaterialStorage::ShaderData *_create_sky_shader_func();
  234. static RendererRD::MaterialStorage::ShaderData *_create_sky_shader_funcs();
  235. RendererRD::MaterialStorage::MaterialData *_create_sky_material_func(SkyShaderData *p_shader);
  236. static RendererRD::MaterialStorage::MaterialData *_create_sky_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader);
  237. SkyRD();
  238. void init();
  239. void set_texture_format(RD::DataFormat p_texture_format);
  240. ~SkyRD();
  241. void setup_sky(RID p_env, Ref<RenderSceneBuffersRD> p_render_buffers, const PagedArray<RID> &p_lights, RID p_camera_attributes, uint32_t p_view_count, const Projection *p_view_projections, const Vector3 *p_view_eye_offsets, const Transform3D &p_cam_transform, const Projection &p_cam_projection, const Size2i p_screen_size, Vector2 p_jitter, RendererSceneRenderRD *p_scene_render);
  242. 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);
  243. void update_res_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_env, double p_time, float p_luminance_multiplier = 1.0);
  244. 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);
  245. void invalidate_sky(Sky *p_sky);
  246. void update_dirty_skys();
  247. RID sky_get_material(RID p_sky) const;
  248. RID sky_get_radiance_texture_rd(RID p_sky) const;
  249. float sky_get_baked_exposure(RID p_sky) const;
  250. RID allocate_sky_rid();
  251. void initialize_sky_rid(RID p_rid);
  252. Sky *get_sky(RID p_sky) const;
  253. void free_sky(RID p_sky);
  254. void sky_set_radiance_size(RID p_sky, int p_radiance_size);
  255. void sky_set_mode(RID p_sky, RS::SkyMode p_mode);
  256. void sky_set_material(RID p_sky, RID p_material);
  257. Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size);
  258. };
  259. } // namespace RendererRD
  260. #endif // SKY_RD_H