renderer_scene_sky_rd.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. private:
  43. RendererStorageRD *storage;
  44. public:
  45. enum SkySet {
  46. SKY_SET_UNIFORMS,
  47. SKY_SET_MATERIAL,
  48. SKY_SET_TEXTURES,
  49. SKY_SET_FOG,
  50. SKY_SET_MAX
  51. };
  52. enum SkyTextureSetVersion {
  53. SKY_TEXTURE_SET_BACKGROUND,
  54. SKY_TEXTURE_SET_HALF_RES,
  55. SKY_TEXTURE_SET_QUARTER_RES,
  56. SKY_TEXTURE_SET_CUBEMAP,
  57. SKY_TEXTURE_SET_CUBEMAP_HALF_RES,
  58. SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES,
  59. SKY_TEXTURE_SET_MAX
  60. };
  61. enum SkyVersion {
  62. SKY_VERSION_BACKGROUND,
  63. SKY_VERSION_HALF_RES,
  64. SKY_VERSION_QUARTER_RES,
  65. SKY_VERSION_CUBEMAP,
  66. SKY_VERSION_CUBEMAP_HALF_RES,
  67. SKY_VERSION_CUBEMAP_QUARTER_RES,
  68. SKY_VERSION_MAX
  69. };
  70. // Skys need less info from Directional Lights than the normal shaders
  71. struct SkyDirectionalLightData {
  72. float direction[3];
  73. float energy;
  74. float color[3];
  75. float size;
  76. uint32_t enabled;
  77. uint32_t pad[3];
  78. };
  79. struct SkySceneState {
  80. struct UBO {
  81. uint32_t volumetric_fog_enabled;
  82. float volumetric_fog_inv_length;
  83. float volumetric_fog_detail_spread;
  84. float fog_aerial_perspective;
  85. float fog_light_color[3];
  86. float fog_sun_scatter;
  87. uint32_t fog_enabled;
  88. float fog_density;
  89. float z_far;
  90. uint32_t directional_light_count;
  91. };
  92. UBO ubo;
  93. SkyDirectionalLightData *directional_lights;
  94. SkyDirectionalLightData *last_frame_directional_lights;
  95. uint32_t max_directional_lights;
  96. uint32_t last_frame_directional_light_count;
  97. RID directional_light_buffer;
  98. RID uniform_set;
  99. RID uniform_buffer;
  100. RID fog_uniform_set;
  101. RID default_fog_uniform_set;
  102. RID fog_shader;
  103. RID fog_material;
  104. RID fog_only_texture_uniform_set;
  105. } sky_scene_state;
  106. struct ReflectionData {
  107. struct Layer {
  108. struct Mipmap {
  109. RID framebuffers[6];
  110. RID views[6];
  111. Size2i size;
  112. };
  113. Vector<Mipmap> mipmaps; //per-face view
  114. Vector<RID> views; // per-cubemap view
  115. };
  116. struct DownsampleLayer {
  117. struct Mipmap {
  118. RID view;
  119. Size2i size;
  120. };
  121. Vector<Mipmap> mipmaps;
  122. };
  123. RID radiance_base_cubemap; //cubemap for first layer, first cubemap
  124. RID downsampled_radiance_cubemap;
  125. DownsampleLayer downsampled_layer;
  126. RID coefficient_buffer;
  127. bool dirty = true;
  128. Vector<Layer> layers;
  129. void clear_reflection_data();
  130. 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);
  131. void create_reflection_fast_filter(RendererStorageRD *p_storage, bool p_use_arrays);
  132. 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);
  133. void update_reflection_mipmaps(RendererStorageRD *p_storage, int p_start, int p_end);
  134. };
  135. struct SkyShaderData : public RendererStorageRD::ShaderData {
  136. bool valid;
  137. RID version;
  138. PipelineCacheRD pipelines[SKY_VERSION_MAX];
  139. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  140. Vector<ShaderCompilerRD::GeneratedCode::Texture> texture_uniforms;
  141. Vector<uint32_t> ubo_offsets;
  142. uint32_t ubo_size;
  143. String path;
  144. String code;
  145. Map<StringName, RID> default_texture_params;
  146. bool uses_time;
  147. bool uses_position;
  148. bool uses_half_res;
  149. bool uses_quarter_res;
  150. bool uses_light;
  151. virtual void set_code(const String &p_Code);
  152. virtual void set_default_texture_param(const StringName &p_name, RID p_texture);
  153. virtual void get_param_list(List<PropertyInfo> *p_param_list) const;
  154. virtual void get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const;
  155. virtual bool is_param_texture(const StringName &p_param) const;
  156. virtual bool is_animated() const;
  157. virtual bool casts_shadows() const;
  158. virtual Variant get_default_parameter(const StringName &p_parameter) const;
  159. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  160. SkyShaderData();
  161. virtual ~SkyShaderData();
  162. };
  163. /* Sky shader */
  164. struct SkyShader {
  165. SkyShaderRD shader;
  166. ShaderCompilerRD compiler;
  167. RID default_shader;
  168. RID default_material;
  169. RID default_shader_rd;
  170. } sky_shader;
  171. struct SkyMaterialData : public RendererStorageRD::MaterialData {
  172. uint64_t last_frame;
  173. SkyShaderData *shader_data;
  174. RID uniform_buffer;
  175. RID uniform_set;
  176. Vector<RID> texture_cache;
  177. Vector<uint8_t> ubo_data;
  178. bool uniform_set_updated;
  179. virtual void set_render_priority(int p_priority) {}
  180. virtual void set_next_pass(RID p_pass) {}
  181. virtual void update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  182. virtual ~SkyMaterialData();
  183. };
  184. struct Sky {
  185. RID radiance;
  186. RID half_res_pass;
  187. RID half_res_framebuffer;
  188. RID quarter_res_pass;
  189. RID quarter_res_framebuffer;
  190. Size2i screen_size;
  191. RID texture_uniform_sets[SKY_TEXTURE_SET_MAX];
  192. RID uniform_set;
  193. RID material;
  194. RID uniform_buffer;
  195. int radiance_size = 256;
  196. RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
  197. ReflectionData reflection;
  198. bool dirty = false;
  199. int processing_layer = 0;
  200. Sky *dirty_list = nullptr;
  201. //State to track when radiance cubemap needs updating
  202. SkyMaterialData *prev_material;
  203. Vector3 prev_position;
  204. float prev_time;
  205. void free(RendererStorageRD *p_storage);
  206. RID get_textures(RendererStorageRD *p_storage, SkyTextureSetVersion p_version, RID p_default_shader_rd);
  207. bool set_radiance_size(int p_radiance_size);
  208. bool set_mode(RS::SkyMode p_mode);
  209. bool set_material(RID p_material);
  210. Ref<Image> bake_panorama(RendererStorageRD *p_storage, float p_energy, int p_roughness_layers, const Size2i &p_size);
  211. };
  212. uint32_t sky_ggx_samples_quality;
  213. bool sky_use_cubemap_array;
  214. Sky *dirty_sky_list = nullptr;
  215. mutable RID_Owner<Sky, true> sky_owner;
  216. int roughness_layers;
  217. RendererStorageRD::ShaderData *_create_sky_shader_func();
  218. static RendererStorageRD::ShaderData *_create_sky_shader_funcs();
  219. RendererStorageRD::MaterialData *_create_sky_material_func(SkyShaderData *p_shader);
  220. static RendererStorageRD::MaterialData *_create_sky_material_funcs(RendererStorageRD::ShaderData *p_shader);
  221. RendererSceneSkyRD();
  222. void init(RendererStorageRD *p_storage);
  223. void setup(RendererSceneEnvironmentRD *p_env, RID p_render_buffers, const CameraMatrix &p_projection, const Transform &p_transform, const Size2i p_screen_size, RendererSceneRenderRD *p_scene_render);
  224. void update(RendererSceneEnvironmentRD *p_env, const CameraMatrix &p_projection, const Transform &p_transform, double p_time);
  225. void draw(RendererSceneEnvironmentRD *p_env, bool p_can_continue_color, bool p_can_continue_depth, RID p_fb, const CameraMatrix &p_projection, const Transform &p_transform, double p_time);
  226. void invalidate_sky(Sky *p_sky);
  227. void update_dirty_skys();
  228. RID sky_get_material(RID p_sky) const;
  229. RID allocate_sky_rid();
  230. void initialize_sky_rid(RID p_rid);
  231. Sky *get_sky(RID p_sky) const;
  232. void free_sky(RID p_sky);
  233. void sky_set_radiance_size(RID p_sky, int p_radiance_size);
  234. void sky_set_mode(RID p_sky, RS::SkyMode p_mode);
  235. void sky_set_material(RID p_sky, RID p_material);
  236. Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size);
  237. RID sky_get_radiance_texture_rd(RID p_sky) const;
  238. };
  239. #endif /* RENDERING_SERVER_SCENE_SKY_RD_H */