Blender V5.3
eevee_view.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
17
18#include "DRW_render.hh"
19
20#include "draw_common.hh"
21
22#include "GPU_debug.hh"
23
24#include "eevee_instance.hh"
25
26#include "eevee_view.hh"
27
28namespace blender::eevee {
29
30/* -------------------------------------------------------------------- */
33
35
37{
38 extent_ = inst_.film.render_extent_get();
39 const CameraData &cam = inst_.camera.data_get();
40
41 if (inst_.camera.is_panoramic()) {
42 is_enabled_ = (cam.panoramic_view_mask & (1u << uint(face_id_))) != 0u;
43 }
44 else {
45 /* Only enable -Z view. */
46 is_enabled_ = (face_id_ == PANORAMIC_FACE_NEG_Z);
47 }
48
49 if (!is_enabled_) {
50 return;
51 }
52
53 /* Create views. */
54 float4x4 viewmat, winmat;
55 if (inst_.camera.is_panoramic()) {
56 /* panoramic_view_overscan has included the 5% overscan (see Camera::sync). */
57 float side = cam.clip_near * cam.panoramic_view_overscan;
58 if (face_id_ == PANORAMIC_FACE_NEG_Z) {
60 }
61 float near = cam.clip_near;
62 float far = cam.clip_far;
63 winmat = math::projection::perspective(-side, side, -side, side, near, far);
64 viewmat = face_matrix_ * cam.viewmat;
65 }
66 else {
67 viewmat = cam.viewmat;
68 winmat = cam.winmat;
69 }
70
71 main_view_.sync(viewmat, winmat);
72
73 inst_.uniform_data.pipeline.is_main_view_inverted = main_view_.is_inverted();
74}
75
77{
78 if (!is_enabled_) {
79 return;
80 }
81
82 update_view();
83 inst_.shadows.set_view(render_view_, extent_);
84 inst_.volume.set_view(main_view_, extent_);
85 inst_.uniform_data.data.push_update();
86 /* Need to be set early for planar probe rendering (if using ray-cast node) and ray-cast nodes in
87 * deferred / forward pipelines. */
88 inst_.raytracing.thickness_parameters_setup(render_view_.winmat(), extent_);
89 inst_.uniform_data.raytrace.push_update();
90
92
93 /* Needs to be before anything else because it query its own gbuffer. */
94 inst_.planar_probes.set_view(render_view_, extent_);
95
96 /* Hand off gsplat compute workload for the current view before draws. */
97 DRW_gsplat_ensure_radiance(*inst_.manager, render_view_);
98
99 RenderBuffers &rbufs = inst_.render_buffers;
100 rbufs.acquire(extent_);
101
102 combined_fb_.ensure(GPU_ATTACHMENT_TEXTURE(rbufs.depth_tx),
104
105 const bool with_raycast = inst_.pipelines.has_raycast;
106 prepass_fb_.ensure(
111
112 GBuffer &gbuf = inst_.gbuffer;
113 gbuf.acquire(extent_,
114 inst_.pipelines.deferred.header_layer_count(),
115 inst_.pipelines.deferred.closure_layer_count(),
116 inst_.pipelines.deferred.normal_layer_count());
117
118 gbuffer_fb_.ensure(GPU_ATTACHMENT_TEXTURE(rbufs.depth_tx),
124
125 /* If camera has any motion, compute motion vector in the film pass. Otherwise, we avoid float
126 * precision issue by setting the motion of all static geometry to 0. */
127 /* TODO: Clear using GPU_framebuffer. */
128 float4 clear_velocity = float4(inst_.velocity.camera_has_motion() ? VELOCITY_INVALID : 0.0f);
129 GPU_texture_clear(rbufs.vector_tx, GPU_DATA_FLOAT, &clear_velocity);
130 if (with_raycast) {
131 rbufs.object_id_tx.clear(uint4(0));
132 rbufs.prepass_normal_tx.clear(float4(0.0f));
133 }
134
135 GPU_framebuffer_bind(combined_fb_);
136 /* Alpha stores transmittance. So start at 1. */
138 combined_fb_, {0.0, 0.0, 0.0, 1.0}, inst_.film.depth.clear_value);
139 inst_.pipelines.background.clear(render_view_);
140
141 /* TODO(fclem): Move it after the first prepass (and hiz update) once pipeline is stabilized. */
142 inst_.lights.set_view(render_view_, extent_);
143
144 inst_.hiz_buffer.set_source(&inst_.render_buffers.depth_tx);
145
146 inst_.volume.draw_prepass(main_view_);
147
148 inst_.pipelines.deferred.render(main_view_,
149 render_view_,
150 prepass_fb_,
151 combined_fb_,
152 gbuffer_fb_,
153 extent_,
154 rt_buffer_opaque_,
155 rt_buffer_refract_);
156
157 inst_.pipelines.background.render(render_view_, combined_fb_);
158
159 inst_.gbuffer.release();
160
161 inst_.volume.draw_compute(main_view_, extent_);
162
163 inst_.volume.draw_resolve(main_view_);
164
165 inst_.ambient_occlusion.render_pass(render_view_);
166
167 inst_.pipelines.forward.render(
168 render_view_, rbufs.depth_tx, prepass_fb_, transparent_fb_, combined_fb_, extent_);
169
170 inst_.lights.shape_display_draw(render_view_, combined_fb_);
171
172 inst_.lights.debug_draw(render_view_, combined_fb_);
173 inst_.hiz_buffer.debug_draw(render_view_, combined_fb_);
174 inst_.shadows.debug_draw(render_view_, combined_fb_);
175 inst_.volume_probes.viewport_draw(render_view_, combined_fb_);
176 inst_.sphere_probes.viewport_draw(render_view_, combined_fb_);
177 inst_.planar_probes.viewport_draw(render_view_, combined_fb_);
178
179 gpu::Texture *combined_final_tx = render_postfx(rbufs.combined_tx);
180 inst_.film.accumulate(jitter_view_, combined_final_tx);
181
182 rbufs.release();
183 postfx_tx_.release();
184
186}
187
188gpu::Texture *ShadingView::render_postfx(gpu::Texture *input_tx)
189{
190 if (!inst_.depth_of_field.postfx_enabled() && !inst_.motion_blur.postfx_enabled()) {
191 return input_tx;
192 }
193 postfx_tx_.acquire_2d(extent_, gpu::TextureFormat::SFLOAT_16_16_16_16);
194
195 /* Fix a sync bug on AMD + Mesa when volume + motion blur create artifacts
196 * except if there is a clear event between them. */
197 if (inst_.volume.enabled() && inst_.motion_blur.postfx_enabled() &&
200 {
201 postfx_tx_.clear(float4(0.0f));
202 }
203
204 gpu::Texture *output_tx = postfx_tx_;
205
206 /* Swapping is done internally. Actual output is set to the next input. */
207 inst_.motion_blur.render(render_view_, &input_tx, &output_tx);
208 inst_.depth_of_field.render(render_view_, &input_tx, &output_tx, dof_buffer_);
209
210 return input_tx;
211}
212
213void ShadingView::update_view()
214{
215 const Film &film = inst_.film;
216
217 float4x4 viewmat = main_view_.viewmat();
218 float4x4 winmat = main_view_.winmat();
219
220 if (film.scaling_factor_get() > 1) {
221 /* This whole section ensures that the render target pixel grid will match the film pixel grid.
222 * Otherwise the weight computation inside the film accumulation will be wrong. */
223
224 float left, right, bottom, top, near, far;
225 projmat_dimensions(winmat.ptr(), &left, &right, &bottom, &top, &near, &far);
226 const float2 bottom_left_with_overscan = float2(left, bottom);
227 const float2 top_right_with_overscan = float2(right, top);
228 const float2 render_size_with_overscan = top_right_with_overscan - bottom_left_with_overscan;
229
230 float2 bottom_left = bottom_left_with_overscan;
231 float2 top_right = top_right_with_overscan;
232 float2 render_size = render_size_with_overscan;
233
234 float overscan = inst_.camera.overscan();
235 if (overscan > 0.0f) {
236 /* Size of overscan on the screen. */
237 const float max_size_with_overscan = math::reduce_max(render_size);
238 const float max_size_original = max_size_with_overscan / (1.0f + 2.0f * overscan);
239 const float overscan_size = (max_size_with_overscan - max_size_original) / 2.0f;
240 /* Undo overscan to get the initial dimension of the screen. */
241 bottom_left = bottom_left_with_overscan + overscan_size;
242 top_right = top_right_with_overscan - overscan_size;
243 /* Render target size on the screen (without overscan). */
244 render_size = top_right - bottom_left;
245 }
246
247 /* Final pixel size on the screen. */
248 const float2 pixel_size = render_size / float2(film.film_extent_get());
249
250 /* Render extent in final film pixel unit. */
251 const int2 render_extent = film.render_extent_original_get() * film.scaling_factor_get();
252 const int overscan_pixels = film.render_overscan_get() * film.scaling_factor_get();
253
254 const float2 render_bottom_left = bottom_left - pixel_size * float(overscan_pixels);
255 const float2 render_top_right = render_bottom_left + pixel_size * float2(render_extent);
256
257 if (main_view_.is_persp()) {
258 winmat = math::projection::perspective(render_bottom_left.x,
259 render_top_right.x,
260 render_bottom_left.y,
261 render_top_right.y,
262 near,
263 far);
264 }
265 else {
266 winmat = math::projection::orthographic(render_bottom_left.x,
267 render_top_right.x,
268 render_bottom_left.y,
269 render_top_right.y,
270 near,
271 far);
272 }
273 }
274
275 /* Anti-Aliasing / Super-Sampling jitter. */
276 float2 jitter = inst_.film.pixel_jitter_get() / float2(extent_);
277 /* Transform to NDC space. */
278 jitter *= 2.0f;
279
280 window_translate_m4(winmat.ptr(), winmat.ptr(), UNPACK2(jitter));
281 jitter_view_.sync(viewmat, winmat);
282
283 /* FIXME(fclem): The offset may be noticeably large and the culling might make object pop
284 * out of the blurring radius. To fix this, use custom enlarged culling matrix. */
285 inst_.depth_of_field.jitter_apply(winmat, viewmat);
286 render_view_.sync(viewmat, winmat);
287}
288
290
291/* -------------------------------------------------------------------- */
294
296{
297 const auto update_info = inst_.sphere_probes.world_update_info_pop();
298 if (!update_info.has_value()) {
299 return;
300 }
301
302 View view = {"Capture.View"};
303 GPU_debug_group_begin("World.Capture");
304
305 if (update_info->do_render) {
306 inst_.sphere_probes.ensure_cubemap_render_target(update_info->cube_target_extent);
307
308 auto render_cubemap = [&](RayPipelineType ray_type) {
309 if (assign_if_different(inst_.pipelines.data.ray_type, ray_type)) {
310 inst_.uniform_data.pipeline.push_update();
311 }
312
313 for (int face : IndexRange(6)) {
314 float4x4 view_m4 = cubeface_mat(face);
315 float4x4 win_m4 = math::projection::perspective(-update_info->clipping_distances.x,
316 update_info->clipping_distances.x,
317 -update_info->clipping_distances.x,
318 update_info->clipping_distances.x,
319 update_info->clipping_distances.x,
320 update_info->clipping_distances.y);
321 view.sync(view_m4, win_m4);
322
323 combined_fb_.ensure(
325 GPU_ATTACHMENT_TEXTURE_CUBEFACE(inst_.sphere_probes.cubemap_tx_, face));
326 GPU_framebuffer_bind(combined_fb_);
327 inst_.pipelines.world.render(view);
328 }
329 };
330
331 if (inst_.pipelines.world.use_lightpath_node()) {
332 render_cubemap(RAY_TYPE_DIFFUSE);
333 inst_.sphere_probes.remap_to_octahedral_projection(
334 update_info->atlas_coord, false, true, WORLD_SUN_DIFFUSE);
335
336 render_cubemap(RAY_TYPE_GLOSSY);
337 inst_.sphere_probes.remap_to_octahedral_projection(
338 update_info->atlas_coord, true, false, WORLD_SUN_GLOSSY);
339 }
340 else {
341 render_cubemap(RAY_TYPE_GLOSSY);
342 inst_.sphere_probes.remap_to_octahedral_projection(
343 update_info->atlas_coord, true, true, WORLD_SUN_COMBINED);
344 }
345
346 /* All volume probe that needs to composite the world probe need to be updated. */
347 inst_.volume_probes.update_world_irradiance();
348 }
349
350 inst_.sphere_probes.release_render_target();
351
352 if (assign_if_different(inst_.pipelines.data.ray_type, RAY_TYPE_CAMERA)) {
353 inst_.uniform_data.pipeline.push_update();
354 }
355
357}
358
360{
361 Framebuffer prepass_fb;
362 View view = {"Capture.View"};
363
364 /* Any 90 degree FOV view will do it. */
365 float4x4 win_m4 = math::projection::perspective(-0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 10.0f);
366 /* Check if uniform_data needs to be updated. */
367 int prev_extent = 0;
368
369 while (const auto update_info = inst_.sphere_probes.probe_update_info_pop()) {
370 GPU_debug_group_begin("Probe.Capture");
371
372 inst_.sphere_probes.ensure_cubemap_render_target(update_info->cube_target_extent);
373
374 if (assign_if_different(inst_.pipelines.data.ray_type, RAY_TYPE_GLOSSY)) {
375 inst_.uniform_data.pipeline.push_update();
376 }
377 if (prev_extent != update_info->cube_target_extent) {
378 /* Set correct thickness for raycast node in probe pipelines. */
379 inst_.raytracing.thickness_parameters_setup(win_m4, int2(update_info->cube_target_extent));
380 inst_.uniform_data.raytrace.push_update();
381 }
382
383 prev_extent = update_info->cube_target_extent;
384
385 int2 extent = int2(update_info->cube_target_extent);
386 RenderBuffers &rbufs = inst_.render_buffers;
387 rbufs.acquire(extent);
388
389 const bool with_raycast = inst_.pipelines.has_raycast;
390 prepass_fb.ensure(
394 GPU_ATTACHMENT_NONE /* Motion vectors not supported. */);
395
396 inst_.gbuffer.acquire(extent,
397 inst_.pipelines.probe.header_layer_count(),
398 inst_.pipelines.probe.closure_layer_count(),
399 inst_.pipelines.probe.normal_layer_count());
400
401 for (int face : IndexRange(6)) {
402 float4x4 view_m4 = cubeface_mat(face);
403 view_m4 = math::translate(view_m4, -update_info->probe_pos);
404 float4x4 win_m4 = math::projection::perspective(-update_info->clipping_distances.x,
405 update_info->clipping_distances.x,
406 -update_info->clipping_distances.x,
407 update_info->clipping_distances.x,
408 update_info->clipping_distances.x,
409 update_info->clipping_distances.y);
410 view.sync(view_m4, win_m4);
411
412 /* Hand off gsplat compute workload for the capture before draws. */
413 DRW_gsplat_ensure_radiance(*inst_.manager, view);
414
415 inst_.shadows.set_view(view, extent);
416 inst_.volume.set_view(view, extent);
417 inst_.uniform_data.data.push_update();
418
419 combined_fb_.ensure(GPU_ATTACHMENT_TEXTURE(inst_.render_buffers.depth_tx),
420 GPU_ATTACHMENT_TEXTURE_CUBEFACE(inst_.sphere_probes.cubemap_tx_, face));
421
422 gbuffer_fb_.ensure(GPU_ATTACHMENT_TEXTURE(inst_.render_buffers.depth_tx),
423 GPU_ATTACHMENT_TEXTURE_CUBEFACE(inst_.sphere_probes.cubemap_tx_, face),
424 GPU_ATTACHMENT_TEXTURE_LAYER(inst_.gbuffer.header_tx.layer_view(0), 0),
425 GPU_ATTACHMENT_TEXTURE_LAYER(inst_.gbuffer.normal_tx.layer_view(0), 0),
426 GPU_ATTACHMENT_TEXTURE_LAYER(inst_.gbuffer.closure_tx.layer_view(0), 0),
427 GPU_ATTACHMENT_TEXTURE_LAYER(inst_.gbuffer.closure_tx.layer_view(1), 0));
428
429 /* TODO: Clear from gbuffer.bind ? */
430 GPU_framebuffer_bind(combined_fb_);
431 /* Alpha stores transmittance. So start at 1. */
432 GPU_framebuffer_clear_color(combined_fb_, {0.0, 0.0, 0.0, 1.0});
433 inst_.pipelines.probe.render(view, prepass_fb, combined_fb_, gbuffer_fb_, extent);
434 }
435
436 inst_.render_buffers.release();
437 inst_.gbuffer.release();
439 inst_.sphere_probes.remap_to_octahedral_projection(update_info->atlas_coord, true, false);
440 }
441
442 inst_.sphere_probes.release_render_target();
443
444 if (assign_if_different(inst_.pipelines.data.ray_type, RAY_TYPE_CAMERA)) {
445 inst_.uniform_data.pipeline.push_update();
446 }
447}
448
450
451/* -------------------------------------------------------------------- */
454
456{
457 if (!inst_.lookdev.use_reference_spheres_) {
458 return;
459 }
460 GPU_debug_group_begin("Lookdev");
461
462 const float radius = inst_.lookdev.sphere_radius_;
463 const float clip = inst_.camera.data_get().clip_near;
465 -radius, radius, -radius, radius, clip);
466 const float4x4 &view_m4 = inst_.camera.data_get().viewmat;
467 view_.sync(view_m4, win_m4);
468
469 inst_.lookdev.draw(view_);
470 inst_.lookdev.display();
471
473}
474
476
477} // namespace blender::eevee
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_TEXTURE_CUBEFACE(_texture, _face)
#define GPU_ATTACHMENT_NONE
#define GPU_ATTACHMENT_TEXTURE_LAYER(_texture, _layer)
#define UNPACK2(a)
constexpr IndexRange()=default
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
void clear(float4 values)
gpu::Texture * layer_view(int layer)
void sync(const float4x4 &view_mat, const float4x4 &win_mat, int view_id=0)
Definition draw_view.cc:27
void render(View &view, gpu::Texture **input_tx, gpu::Texture **output_tx, DepthOfFieldBuffer &dof_buffer)
MotionBlurModule motion_blur
void render(View &view, gpu::Texture **input_tx, gpu::Texture **output_tx)
void acquire(int2 extent, gpu::TextureFormat raycast_depth_format=depth_format)
#define VELOCITY_INVALID
void DRW_gsplat_ensure_radiance(draw::Manager &manager, draw::View &view)
float4x4 cubeface_mat(int face)
MatBase< T, 4, 4 > orthographic(T left, T right, T bottom, T top, T near_clip, T far_clip)
Create an orthographic projection matrix using OpenGL coordinate convention: Maps each axis range to ...
MatBase< T, 4, 4 > orthographic_infinite(T left, T right, T bottom, T top)
Create an orthographic projection matrix using OpenGL coordinate convention: Maps each axis range to ...
MatBase< T, 4, 4 > perspective(T left, T right, T bottom, T top, T near_clip, T far_clip)
Create a perspective projection matrix using OpenGL coordinate convention: Maps each axis range to [-...
T reduce_max(const VecBase< T, Size > &a)
MatBase< T, NumCol, NumRow > translate(const MatBase< T, NumCol, NumRow > &mat, const VectorT &translation)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
unsigned int uint
Definition BKE_text.h:55
bool assign_if_different(T &old_value, T &&new_value)
void projmat_dimensions(const float winmat[4][4], float *r_left, float *r_right, float *r_bottom, float *r_top, float *r_near, float *r_far)
VecBase< uint32_t, 4 > uint4
void window_translate_m4(float winmat[4][4], float perspmat[4][4], float x, float y)
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
@ GPU_DRIVER_OFFICIAL
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void GPU_framebuffer_clear_color_depth(gpu::FrameBuffer *fb, const double4 clear_col, float clear_depth)
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const c_style_mat & ptr() const
TextureFromPool header_tx
void acquire(int2 extent, int header_count, int data_count, int normal_count)
TextureFromPool closure_tx
TextureFromPool normal_tx
float x
float y