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