Debian Codegraph @

servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* render_scene_data_rd.cpp */
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 #include "render_scene_data_rd.h"
32 #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
33 #include "servers/rendering/renderer_rd/storage_rd/light_storage.h"
34 #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
35
36 Transform3D RenderSceneDataRD::get_cam_transform() const {
37 return cam_transform;
38 }
39
40 Projection RenderSceneDataRD::get_cam_projection() const {
41 Projection correction;
42 correction.set_depth_correction(flip_y);
43 correction.add_jitter_offset(taa_jitter);
44
45 return correction * cam_projection;
46 }
47
48 uint32_t RenderSceneDataRD::get_view_count() const {
49 return view_count;
50 }
51
52 Vector3 RenderSceneDataRD::get_view_eye_offset(uint32_t p_view) const {
53 ERR_FAIL_UNSIGNED_INDEX_V(p_view, view_count, Vector3());
54
55 return view_eye_offset[p_view];
56 }
57
58 Projection RenderSceneDataRD::get_view_projection(uint32_t p_view) const {
59 ERR_FAIL_UNSIGNED_INDEX_V(p_view, view_count, Projection());
60
61 Projection correction;
62 correction.set_depth_correction(flip_y);
63 correction.add_jitter_offset(taa_jitter);
64
65 return correction * view_projection[p_view];
66 }
67
68 RID RenderSceneDataRD::create_uniform_buffer() {
69 return RD::get_singleton()->uniform_buffer_create(sizeof(UBODATA));
70 }
71
72 void RenderSceneDataRD::update_ubo(RID p_uniform_buffer, RS::ViewportDebugDraw p_debug_mode, RID p_env, RID p_reflection_probe_instance, RID p_camera_attributes, bool p_pancake_shadows, const Size2i &p_screen_size, const Size2 &p_viewport_size, const Color &p_default_bg_color, float p_luminance_multiplier, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier) {
73 RendererSceneRenderRD *render_scene_render = RendererSceneRenderRD::get_singleton();
74
75 UBODATA ubo_data;
76 memset(&ubo_data, 0, sizeof(UBODATA));
77
78 // just for easy access..
79 UBO &ubo = ubo_data.ubo;
80 UBO &prev_ubo = ubo_data.prev_ubo;
81
82 Projection correction;
83 correction.set_depth_correction(flip_y);
84 correction.add_jitter_offset(taa_jitter);
85 Projection projection = correction * cam_projection;
86
87 //store camera into ubo
88 RendererRD::MaterialStorage::store_camera(projection, ubo.projection_matrix);
89 RendererRD::MaterialStorage::store_camera(projection.inverse(), ubo.inv_projection_matrix);
90 RendererRD::MaterialStorage::store_transform_transposed_3x4(cam_transform, ubo.inv_view_matrix);
91 RendererRD::MaterialStorage::store_transform_transposed_3x4(cam_transform.affine_inverse(), ubo.view_matrix);
92
93 #ifdef REAL_T_IS_DOUBLE
94 RendererRD::MaterialStorage::split_double(-cam_transform.origin.x, &ubo.inv_view_matrix[3], &ubo.inv_view_precision[0]);
95 RendererRD::MaterialStorage::split_double(-cam_transform.origin.y, &ubo.inv_view_matrix[7], &ubo.inv_view_precision[1]);
96 RendererRD::MaterialStorage::split_double(-cam_transform.origin.z, &ubo.inv_view_matrix[11], &ubo.inv_view_precision[2]);
97 #endif
98
99 for (uint32_t v = 0; v < view_count; v++) {
100 projection = correction * view_projection[v];
101 RendererRD::MaterialStorage::store_camera(projection, ubo.projection_matrix_view[v]);
102 RendererRD::MaterialStorage::store_camera(projection.inverse(), ubo.inv_projection_matrix_view[v]);
103
104 ubo.eye_offset[v][0] = view_eye_offset[v].x;
105 ubo.eye_offset[v][1] = view_eye_offset[v].y;
106 ubo.eye_offset[v][2] = view_eye_offset[v].z;
107 ubo.eye_offset[v][3] = 0.0;
108 }
109
110 RendererRD::MaterialStorage::store_transform(main_cam_transform, ubo.main_cam_inv_view_matrix);
111
112 ubo.taa_jitter[0] = taa_jitter.x;
113 ubo.taa_jitter[1] = taa_jitter.y;
114 ubo.taa_frame_count = taa_frame_count;
115
116 ubo.z_far = z_far;
117 ubo.z_near = z_near;
118
119 ubo.flags = 0;
120
121 ubo.flags |= p_pancake_shadows ? SCENE_DATA_FLAGS_USE_PANCAKE_SHADOWS : 0;
122
123 RendererRD::MaterialStorage::store_soft_shadow_kernel(render_scene_render->directional_penumbra_shadow_kernel_get(), ubo.directional_penumbra_shadow_kernel);
124 RendererRD::MaterialStorage::store_soft_shadow_kernel(render_scene_render->directional_soft_shadow_kernel_get(), ubo.directional_soft_shadow_kernel);
125 RendererRD::MaterialStorage::store_soft_shadow_kernel(render_scene_render->penumbra_shadow_kernel_get(), ubo.penumbra_shadow_kernel);
126 RendererRD::MaterialStorage::store_soft_shadow_kernel(render_scene_render->soft_shadow_kernel_get(), ubo.soft_shadow_kernel);
127 ubo.camera_visible_layers = camera_visible_layers;
128 ubo.pass_alpha_multiplier = p_opaque_render_buffers && p_apply_alpha_multiplier ? 0.0f : 1.0f;
129
130 ubo.viewport_size[0] = p_viewport_size.x;
131 ubo.viewport_size[1] = p_viewport_size.y;
132
133 Size2 screen_pixel_size = Vector2(1.0, 1.0) / Size2(p_screen_size);
134 ubo.screen_pixel_size[0] = screen_pixel_size.x;
135 ubo.screen_pixel_size[1] = screen_pixel_size.y;
136
137 ubo.shadow_atlas_pixel_size[0] = shadow_atlas_pixel_size.x;
138 ubo.shadow_atlas_pixel_size[1] = shadow_atlas_pixel_size.y;
139
140 ubo.directional_shadow_pixel_size[0] = directional_shadow_pixel_size.x;
141 ubo.directional_shadow_pixel_size[1] = directional_shadow_pixel_size.y;
142
143 ubo.radiance_pixel_size = radiance_pixel_size;
144 ubo.radiance_border_size = radiance_border_size;
145
146 ubo.reflection_atlas_border_size[0] = reflection_atlas_border_size.x;
147 ubo.reflection_atlas_border_size[1] = reflection_atlas_border_size.y;
148
149 ubo.time = time;
150
151 ubo.directional_light_count = directional_light_count;
152 ubo.dual_paraboloid_side = dual_paraboloid_side;
153 ubo.opaque_prepass_threshold = opaque_prepass_threshold;
154 ubo.flags |= material_uv2_mode ? SCENE_DATA_FLAGS_USE_UV2_MATERIAL : 0;
155 ubo.flags |= shadow_pass ? SCENE_DATA_FLAGS_IN_SHADOW_PASS : 0;
156
157 if (p_debug_mode == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
158 ubo.flags |= SCENE_DATA_FLAGS_USE_AMBIENT_LIGHT;
159 ubo.ambient_light_color_energy[0] = 1;
160 ubo.ambient_light_color_energy[1] = 1;
161 ubo.ambient_light_color_energy[2] = 1;
162 ubo.ambient_light_color_energy[3] = 1.0;
163 } else if (p_env.is_valid()) {
164 RS::EnvironmentBG env_bg = render_scene_render->environment_get_background(p_env);
165 RS::EnvironmentAmbientSource ambient_src = render_scene_render->environment_get_ambient_source(p_env);
166
167 float bg_energy_multiplier = render_scene_render->environment_get_bg_energy_multiplier(p_env);
168
169 ubo.ambient_light_color_energy[3] = bg_energy_multiplier;
170
171 ubo.ambient_color_sky_mix = render_scene_render->environment_get_ambient_sky_contribution(p_env);
172
173 //ambient
174 if (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && (env_bg == RS::ENV_BG_CLEAR_COLOR || env_bg == RS::ENV_BG_COLOR)) {
175 Color color = env_bg == RS::ENV_BG_CLEAR_COLOR ? p_default_bg_color : render_scene_render->environment_get_bg_color(p_env);
176 color = color.srgb_to_linear();
177
178 ubo.ambient_light_color_energy[0] = color.r * bg_energy_multiplier;
179 ubo.ambient_light_color_energy[1] = color.g * bg_energy_multiplier;
180 ubo.ambient_light_color_energy[2] = color.b * bg_energy_multiplier;
181 ubo.flags |= SCENE_DATA_FLAGS_USE_AMBIENT_LIGHT;
182 } else {
183 float energy = render_scene_render->environment_get_ambient_light_energy(p_env);
184 Color color = render_scene_render->environment_get_ambient_light(p_env);
185 color = color.srgb_to_linear();
186 ubo.ambient_light_color_energy[0] = color.r * energy;
187 ubo.ambient_light_color_energy[1] = color.g * energy;
188 ubo.ambient_light_color_energy[2] = color.b * energy;
189
190 bool use_ambient_cubemap = (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ambient_src == RS::ENV_AMBIENT_SOURCE_SKY;
191 bool use_ambient_light = use_ambient_cubemap || ambient_src == RS::ENV_AMBIENT_SOURCE_COLOR;
192 ubo.flags |= use_ambient_cubemap ? SCENE_DATA_FLAGS_USE_AMBIENT_CUBEMAP : 0;
193 ubo.flags |= use_ambient_light ? SCENE_DATA_FLAGS_USE_AMBIENT_LIGHT : 0;
194 }
195
196 //specular
197 RS::EnvironmentReflectionSource ref_src = render_scene_render->environment_get_reflection_source(p_env);
198 if ((ref_src == RS::ENV_REFLECTION_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ref_src == RS::ENV_REFLECTION_SOURCE_SKY) {
199 ubo.flags |= SCENE_DATA_FLAGS_USE_REFLECTION_CUBEMAP;
200 }
201
202 if ((ubo.flags & SCENE_DATA_FLAGS_USE_AMBIENT_CUBEMAP) || (ubo.flags & SCENE_DATA_FLAGS_USE_REFLECTION_CUBEMAP)) {
203 Basis sky_transform = render_scene_render->environment_get_sky_orientation(p_env);
204 sky_transform = sky_transform.inverse() * cam_transform.basis;
205 RendererRD::MaterialStorage::store_transform_3x3(sky_transform, ubo.radiance_inverse_xform);
206 }
207
208 ubo.flags |= render_scene_render->environment_get_fog_enabled(p_env) ? SCENE_DATA_FLAGS_USE_FOG : 0;
209 ubo.fog_density = render_scene_render->environment_get_fog_density(p_env);
210 ubo.fog_height = render_scene_render->environment_get_fog_height(p_env);
211 ubo.fog_height_density = render_scene_render->environment_get_fog_height_density(p_env);
212 ubo.fog_aerial_perspective = render_scene_render->environment_get_fog_aerial_perspective(p_env);
213
214 ubo.fog_depth_curve = render_scene_render->environment_get_fog_depth_curve(p_env);
215 ubo.fog_depth_end = render_scene_render->environment_get_fog_depth_end(p_env) > 0.0 ? render_scene_render->environment_get_fog_depth_end(p_env) : ubo.z_far;
216 ubo.fog_depth_begin = MIN(render_scene_render->environment_get_fog_depth_begin(p_env), ubo.fog_depth_end - 0.001);
217
218 Color fog_color = render_scene_render->environment_get_fog_light_color(p_env).srgb_to_linear();
219 float fog_energy = render_scene_render->environment_get_fog_light_energy(p_env);
220
221 ubo.fog_light_color[0] = fog_color.r * fog_energy;
222 ubo.fog_light_color[1] = fog_color.g * fog_energy;
223 ubo.fog_light_color[2] = fog_color.b * fog_energy;
224
225 ubo.fog_sun_scatter = render_scene_render->environment_get_fog_sun_scatter(p_env);
226 } else {
227 if (!(p_reflection_probe_instance.is_valid() && RendererRD::LightStorage::get_singleton()->reflection_probe_is_interior(p_reflection_probe_instance))) {
228 ubo.flags |= SCENE_DATA_FLAGS_USE_AMBIENT_LIGHT;
229 Color clear_color = p_default_bg_color;
230 clear_color = clear_color.srgb_to_linear();
231 ubo.ambient_light_color_energy[0] = clear_color.r;
232 ubo.ambient_light_color_energy[1] = clear_color.g;
233 ubo.ambient_light_color_energy[2] = clear_color.b;
234 ubo.ambient_light_color_energy[3] = 1.0;
235 }
236 }
237
238 if (p_camera_attributes.is_valid()) {
239 ubo.emissive_exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_camera_attributes);
240 ubo.IBL_exposure_normalization = 1.0;
241 if (p_env.is_valid()) {
242 RID sky_rid = render_scene_render->environment_get_sky(p_env);
243 if (sky_rid.is_valid()) {
244 float current_exposure = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_camera_attributes) * render_scene_render->environment_get_bg_intensity(p_env) / p_luminance_multiplier;
245 ubo.IBL_exposure_normalization = current_exposure / MAX(0.001, render_scene_render->get_sky()->sky_get_baked_exposure(sky_rid));
246 }
247 }
248 } else if (emissive_exposure_normalization > 0.0) {
249 // This branch is triggered when using render_material().
250 // Emissive is set outside the function.
251 ubo.emissive_exposure_normalization = emissive_exposure_normalization;
252 // IBL isn't used don't set it.
253 } else {
254 ubo.emissive_exposure_normalization = 1.0;
255 ubo.IBL_exposure_normalization = 1.0;
256 }
257
258 bool roughness_limiter_enabled = p_opaque_render_buffers && render_scene_render->screen_space_roughness_limiter_is_active();
259 ubo.flags |= roughness_limiter_enabled ? SCENE_DATA_FLAGS_USE_ROUGHNESS_LIMITER : 0;
260 ubo.roughness_limiter_amount = render_scene_render->screen_space_roughness_limiter_get_amount();
261 ubo.roughness_limiter_limit = render_scene_render->screen_space_roughness_limiter_get_limit();
262
263 if (calculate_motion_vectors) {
264 // Q : Should we make a complete copy or should we define a separate UBO with just the components we need?
265 memcpy(&prev_ubo, &ubo, sizeof(UBO));
266
267 Projection prev_correction;
268 prev_correction.set_depth_correction(true);
269 prev_correction.add_jitter_offset(prev_taa_jitter);
270 Projection prev_projection = prev_correction * prev_cam_projection;
271
272 //store camera into ubo
273 RendererRD::MaterialStorage::store_camera(prev_projection, prev_ubo.projection_matrix);
274 RendererRD::MaterialStorage::store_camera(prev_projection.inverse(), prev_ubo.inv_projection_matrix);
275 RendererRD::MaterialStorage::store_transform_transposed_3x4(prev_cam_transform, prev_ubo.inv_view_matrix);
276 RendererRD::MaterialStorage::store_transform_transposed_3x4(prev_cam_transform.affine_inverse(), prev_ubo.view_matrix);
277
278 #ifdef REAL_T_IS_DOUBLE
279 RendererRD::MaterialStorage::split_double(-prev_cam_transform.origin.x, &prev_ubo.inv_view_matrix[3], &prev_ubo.inv_view_precision[0]);
280 RendererRD::MaterialStorage::split_double(-prev_cam_transform.origin.y, &prev_ubo.inv_view_matrix[7], &prev_ubo.inv_view_precision[1]);
281 RendererRD::MaterialStorage::split_double(-prev_cam_transform.origin.z, &prev_ubo.inv_view_matrix[11], &prev_ubo.inv_view_precision[2]);
282 #endif
283
284 for (uint32_t v = 0; v < view_count; v++) {
285 prev_projection = prev_correction * view_projection[v];
286 RendererRD::MaterialStorage::store_camera(prev_projection, prev_ubo.projection_matrix_view[v]);
287 RendererRD::MaterialStorage::store_camera(prev_projection.inverse(), prev_ubo.inv_projection_matrix_view[v]);
288 }
289 prev_ubo.taa_jitter[0] = prev_taa_jitter.x;
290 prev_ubo.taa_jitter[1] = prev_taa_jitter.y;
291 prev_ubo.time -= time_step;
292 }
293
294 uniform_buffer = p_uniform_buffer;
295 RD::get_singleton()->buffer_update(uniform_buffer, 0, sizeof(UBODATA), &ubo);
296 }
297
298 RID RenderSceneDataRD::get_uniform_buffer() const {
299 return uniform_buffer;
300 }