renderer_scene_sky_rd.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*************************************************************************/
  2. /* renderer_scene_sky_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifndef RENDERING_SERVER_SCENE_SKY_RD_H
  31. #define RENDERING_SERVER_SCENE_SKY_RD_H
  32. #include "core/templates/rid_owner.h"
  33. #include "servers/rendering/renderer_compositor.h"
  34. #include "servers/rendering/renderer_rd/renderer_scene_environment_rd.h"
  35. #include "servers/rendering/renderer_rd/renderer_storage_rd.h"
  36. #include "servers/rendering/renderer_rd/shaders/sky.glsl.gen.h"
  37. #include "servers/rendering/renderer_scene_render.h"
  38. #include "servers/rendering/rendering_device.h"
  39. // Forward declare RendererSceneRenderRD so we can pass it into some of our methods, these classes are pretty tightly bound
  40. class RendererSceneRenderRD;
  41. class RendererSceneSkyRD {
  42. public:
  43. enum SkySet {
  44. SKY_SET_UNIFORMS,
  45. SKY_SET_MATERIAL,
  46. SKY_SET_TEXTURES,
  47. SKY_SET_FOG,
  48. SKY_SET_MAX
  49. };
  50. // Skys need less info from Directional Lights than the normal shaders
  51. struct SkyDirectionalLightData {
  52. float direction[3];
  53. float energy;
  54. float color[3];
  55. float size;
  56. uint32_t enabled;
  57. uint32_t pad[3];
  58. };
  59. private:
  60. RendererStorageRD *storage;
  61. RD::DataFormat texture_format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  62. RID index_buffer;
  63. RID index_array;
  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 projections[RendererSceneRender::MAX_RENDER_VIEWS][4]; // 2 x 16 - 80
  88. float position[3]; // 12 - 92
  89. float multiplier; // 4 - 96
  90. float time; // 4 - 100
  91. float luminance_multiplier; // 4 - 104
  92. float pad[2]; // 8 - 112 // Using pad to align on 16 bytes
  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 RendererStorageRD::ShaderData {
  96. bool valid;
  97. RID version;
  98. PipelineCacheRD pipelines[SKY_VERSION_MAX];
  99. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  100. Vector<ShaderCompilerRD::GeneratedCode::Texture> texture_uniforms;
  101. Vector<uint32_t> ubo_offsets;
  102. uint32_t ubo_size;
  103. String path;
  104. String code;
  105. Map<StringName, RID> default_texture_params;
  106. bool uses_time;
  107. bool uses_position;
  108. bool uses_half_res;
  109. bool uses_quarter_res;
  110. bool uses_light;
  111. virtual void set_code(const String &p_Code);
  112. virtual void set_default_texture_param(const StringName &p_name, RID p_texture);
  113. virtual void get_param_list(List<PropertyInfo> *p_param_list) const;
  114. virtual void get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const;
  115. virtual bool is_param_texture(const StringName &p_param) const;
  116. virtual bool is_animated() const;
  117. virtual bool casts_shadows() const;
  118. virtual Variant get_default_parameter(const StringName &p_parameter) const;
  119. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  120. SkyShaderData();
  121. virtual ~SkyShaderData();
  122. };
  123. void _render_sky(RD::DrawListID p_list, float p_time, RID p_fb, PipelineCacheRD *p_pipeline, RID p_uniform_set, RID p_texture_set, uint32_t p_view_count, const CameraMatrix *p_projections, const Basis &p_orientation, float p_multiplier, const Vector3 &p_position, float p_luminance_multiplier);
  124. public:
  125. struct SkySceneState {
  126. struct UBO {
  127. uint32_t volumetric_fog_enabled;
  128. float volumetric_fog_inv_length;
  129. float volumetric_fog_detail_spread;
  130. float fog_aerial_perspective;
  131. float fog_light_color[3];
  132. float fog_sun_scatter;
  133. uint32_t fog_enabled;
  134. float fog_density;
  135. float z_far;
  136. uint32_t directional_light_count;
  137. };
  138. UBO ubo;
  139. SkyDirectionalLightData *directional_lights;
  140. SkyDirectionalLightData *last_frame_directional_lights;
  141. uint32_t max_directional_lights;
  142. uint32_t last_frame_directional_light_count;
  143. RID directional_light_buffer;
  144. RID uniform_set;
  145. RID uniform_buffer;
  146. RID fog_uniform_set;
  147. RID default_fog_uniform_set;
  148. RID fog_shader;
  149. RID fog_material;
  150. RID fog_only_texture_uniform_set;
  151. } sky_scene_state;
  152. struct ReflectionData {
  153. struct Layer {
  154. struct Mipmap {
  155. RID framebuffers[6];
  156. RID views[6];
  157. Size2i size;
  158. };
  159. Vector<Mipmap> mipmaps; //per-face view
  160. Vector<RID> views; // per-cubemap view
  161. };
  162. struct DownsampleLayer {
  163. struct Mipmap {
  164. RID view;
  165. Size2i size;
  166. // for mobile only
  167. RID views[6];
  168. RID framebuffers[6];
  169. };
  170. Vector<Mipmap> mipmaps;
  171. };
  172. RID radiance_base_cubemap; //cubemap for first layer, first cubemap
  173. RID downsampled_radiance_cubemap;
  174. DownsampleLayer downsampled_layer;
  175. RID coefficient_buffer;
  176. bool dirty = true;
  177. Vector<Layer> layers;
  178. void clear_reflection_data();
  179. void update_reflection_data(RendererStorageRD *p_storage, int p_size, int p_mipmaps, bool p_use_array, RID p_base_cube, int p_base_layer, bool p_low_quality, int p_roughness_layers, RD::DataFormat p_texture_format);
  180. void create_reflection_fast_filter(RendererStorageRD *p_storage, bool p_use_arrays);
  181. void create_reflection_importance_sample(RendererStorageRD *p_storage, bool p_use_arrays, int p_cube_side, int p_base_layer, uint32_t p_sky_ggx_samples_quality);
  182. void update_reflection_mipmaps(RendererStorageRD *p_storage, int p_start, int p_end);
  183. };
  184. /* Sky shader */
  185. struct SkyShader {
  186. SkyShaderRD shader;
  187. ShaderCompilerRD compiler;
  188. RID default_shader;
  189. RID default_material;
  190. RID default_shader_rd;
  191. } sky_shader;
  192. struct SkyMaterialData : public RendererStorageRD::MaterialData {
  193. uint64_t last_frame;
  194. SkyShaderData *shader_data;
  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 Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  200. virtual ~SkyMaterialData();
  201. };
  202. struct Sky {
  203. RID radiance;
  204. RID half_res_pass;
  205. RID half_res_framebuffer;
  206. RID quarter_res_pass;
  207. RID quarter_res_framebuffer;
  208. Size2i screen_size;
  209. RID texture_uniform_sets[SKY_TEXTURE_SET_MAX];
  210. RID uniform_set;
  211. RID material;
  212. RID uniform_buffer;
  213. int radiance_size = 256;
  214. RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
  215. ReflectionData reflection;
  216. bool dirty = false;
  217. int processing_layer = 0;
  218. Sky *dirty_list = nullptr;
  219. //State to track when radiance cubemap needs updating
  220. SkyMaterialData *prev_material;
  221. Vector3 prev_position;
  222. float prev_time;
  223. void free(RendererStorageRD *p_storage);
  224. RID get_textures(RendererStorageRD *p_storage, SkyTextureSetVersion p_version, RID p_default_shader_rd);
  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(RendererStorageRD *p_storage, 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. RendererStorageRD::ShaderData *_create_sky_shader_func();
  236. static RendererStorageRD::ShaderData *_create_sky_shader_funcs();
  237. RendererStorageRD::MaterialData *_create_sky_material_func(SkyShaderData *p_shader);
  238. static RendererStorageRD::MaterialData *_create_sky_material_funcs(RendererStorageRD::ShaderData *p_shader);
  239. RendererSceneSkyRD();
  240. void init(RendererStorageRD *p_storage);
  241. void set_texture_format(RD::DataFormat p_texture_format);
  242. ~RendererSceneSkyRD();
  243. void setup(RendererSceneEnvironmentRD *p_env, RID p_render_buffers, const CameraMatrix &p_projection, const Transform3D &p_transform, const Size2i p_screen_size, RendererSceneRenderRD *p_scene_render);
  244. void update(RendererSceneEnvironmentRD *p_env, const CameraMatrix &p_projection, const Transform3D &p_transform, double p_time, float p_luminance_multiplier = 1.0);
  245. void draw(RendererSceneEnvironmentRD *p_env, bool p_can_continue_color, bool p_can_continue_depth, RID p_fb, uint32_t p_view_count, const CameraMatrix *p_projections, const Transform3D &p_transform, double p_time); // only called by clustered renderer
  246. void update_res_buffers(RendererSceneEnvironmentRD *p_env, uint32_t p_view_count, const CameraMatrix *p_projections, const Transform3D &p_transform, double p_time, float p_luminance_multiplier = 1.0);
  247. void draw(RD::DrawListID p_draw_list, RendererSceneEnvironmentRD *p_env, RID p_fb, uint32_t p_view_count, const CameraMatrix *p_projections, const Transform3D &p_transform, double p_time, float p_luminance_multiplier = 1.0);
  248. void invalidate_sky(Sky *p_sky);
  249. void update_dirty_skys();
  250. RID sky_get_material(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. RID sky_get_radiance_texture_rd(RID p_sky) const;
  260. };
  261. #endif /* RENDERING_SERVER_SCENE_SKY_RD_H */