Blender  V3.3
eevee_view.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation.
3  */
4 
18 #include "BKE_global.h"
19 #include "DRW_render.h"
20 
21 #include "eevee_instance.hh"
22 
23 #include "eevee_view.hh"
24 
25 namespace blender::eevee {
26 
27 /* -------------------------------------------------------------------- */
32 {
33  // dof_.init();
34  // mb_.init();
35 }
36 
38 {
39  int2 render_extent = inst_.film.render_extent_get();
40 
41  if (false /* inst_.camera.is_panoramic() */) {
42  int64_t render_pixel_count = render_extent.x * (int64_t)render_extent.y;
43  /* Divide pixel count between the 6 views. Rendering to a square target. */
44  extent_[0] = extent_[1] = ceilf(sqrtf(1 + (render_pixel_count / 6)));
45  /* TODO(@fclem): Clip unused views here. */
46  is_enabled_ = true;
47  }
48  else {
49  extent_ = render_extent;
50  /* Only enable -Z view. */
51  is_enabled_ = (StringRefNull(name_) == "negZ_view");
52  }
53 
54  if (!is_enabled_) {
55  return;
56  }
57 
58  /* Create views. */
59  const CameraData &cam = inst_.camera.data_get();
60 
61  float4x4 viewmat, winmat;
62  const float(*viewmat_p)[4] = viewmat.ptr(), (*winmat_p)[4] = winmat.ptr();
63  if (false /* inst_.camera.is_panoramic() */) {
64  /* TODO(@fclem) Over-scans. */
65  /* For now a mandatory 5% over-scan for DoF. */
66  float side = cam.clip_near * 1.05f;
67  float near = cam.clip_near;
68  float far = cam.clip_far;
69  perspective_m4(winmat.ptr(), -side, side, -side, side, near, far);
70  viewmat = face_matrix_ * cam.viewmat;
71  }
72  else {
73  viewmat_p = cam.viewmat.ptr();
74  winmat_p = cam.winmat.ptr();
75  }
76 
77  main_view_ = DRW_view_create(viewmat_p, winmat_p, nullptr, nullptr, nullptr);
78  sub_view_ = DRW_view_create_sub(main_view_, viewmat_p, winmat_p);
79  render_view_ = DRW_view_create_sub(main_view_, viewmat_p, winmat_p);
80 
81  // dof_.sync(winmat_p, extent_);
82  // mb_.sync(extent_);
83  // rt_buffer_opaque_.sync(extent_);
84  // rt_buffer_refract_.sync(extent_);
85  // inst_.hiz_back.view_sync(extent_);
86  // inst_.hiz_front.view_sync(extent_);
87  // inst_.gbuffer.view_sync(extent_);
88 
89  postfx_tx_.sync();
90 }
91 
93 {
94  if (!is_enabled_) {
95  return;
96  }
97 
98  /* Query temp textures and create frame-buffers. */
99  /* HACK: View name should be unique and static.
100  * With this, we can reuse the same texture across views. */
101  DrawEngineType *owner = (DrawEngineType *)name_;
102 
103  RenderBuffers &rbufs = inst_.render_buffers;
104  rbufs.acquire(extent_, owner);
105  combined_fb_.ensure(GPU_ATTACHMENT_TEXTURE(rbufs.depth_tx),
106  GPU_ATTACHMENT_TEXTURE(rbufs.combined_tx));
107  prepass_fb_.ensure(GPU_ATTACHMENT_TEXTURE(rbufs.depth_tx),
108  GPU_ATTACHMENT_TEXTURE(rbufs.vector_tx));
109 
110  update_view();
111 
112  DRW_stats_group_start(name_);
113  DRW_view_set_active(render_view_);
114 
115  /* If camera has any motion, compute motion vector in the film pass. Otherwise, we avoid float
116  * precision issue by setting the motion of all static geometry to 0. */
117  float4 clear_velocity = float4(inst_.velocity.camera_has_motion() ? VELOCITY_INVALID : 0.0f);
118 
119  GPU_framebuffer_bind(prepass_fb_);
120  GPU_framebuffer_clear_color(prepass_fb_, clear_velocity);
121  /* Alpha stores transmittance. So start at 1. */
122  float4 clear_color = {0.0f, 0.0f, 0.0f, 1.0f};
123  GPU_framebuffer_bind(combined_fb_);
124  GPU_framebuffer_clear_color_depth(combined_fb_, clear_color, 1.0f);
125 
126  inst_.pipelines.world.render();
127 
128  // inst_.pipelines.deferred.render(
129  // render_view_, rt_buffer_opaque_, rt_buffer_refract_, depth_tx_, combined_tx_);
130 
131  // inst_.lightprobes.draw_cache_display();
132 
133  // inst_.lookdev.render_overlay(view_fb_);
134 
135  inst_.pipelines.forward.render(
136  render_view_, prepass_fb_, combined_fb_, rbufs.depth_tx, rbufs.combined_tx);
137 
138  // inst_.lights.debug_draw(view_fb_);
139  // inst_.shadows.debug_draw(view_fb_);
140 
141  // GPUTexture *final_radiance_tx = render_post(combined_tx_);
142 
143  inst_.film.accumulate(sub_view_);
144 
145  rbufs.release();
146  postfx_tx_.release();
147 
149 }
150 
152 {
153 #if 0
154  if (!dof_.postfx_enabled() && !mb_.enabled()) {
155  return input_tx;
156  }
157  /* HACK: View name should be unique and static.
158  * With this, we can reuse the same texture across views. */
159  postfx_tx_.acquire(extent_, GPU_RGBA16F, (void *)name_);
160 
161  GPUTexture *velocity_tx = velocity_.view_vectors_get();
162  GPUTexture *output_tx = postfx_tx_;
163 
164  /* Swapping is done internally. Actual output is set to the next input. */
165  dof_.render(depth_tx_, &input_tx, &output_tx);
166  mb_.render(depth_tx_, velocity_tx, &input_tx, &output_tx);
167 #endif
168  return input_tx;
169 }
170 
171 void ShadingView::update_view()
172 {
173  float4x4 viewmat, winmat;
174  DRW_view_viewmat_get(main_view_, viewmat.ptr(), false);
175  DRW_view_winmat_get(main_view_, winmat.ptr(), false);
176 
177  /* TODO(fclem): Mixed-resolution rendering: We need to make sure we render with exactly the same
178  * distances between pixels to line up render samples and target pixels.
179  * So if the target resolution is not a multiple of the resolution divisor, we need to make the
180  * projection window bigger in the +X and +Y directions. */
181 
182  /* Anti-Aliasing / Super-Sampling jitter. */
183  float2 jitter = inst_.film.pixel_jitter_get() / float2(extent_);
184  /* Transform to NDC space. */
185  jitter *= 2.0f;
186 
187  window_translate_m4(winmat.ptr(), winmat.ptr(), UNPACK2(jitter));
188  DRW_view_update_sub(sub_view_, viewmat.ptr(), winmat.ptr());
189 
190  /* FIXME(fclem): The offset may be is noticeably large and the culling might make object pop
191  * out of the blurring radius. To fix this, use custom enlarged culling matrix. */
192  // dof_.jitter_apply(winmat, viewmat);
193  DRW_view_update_sub(render_view_, viewmat.ptr(), winmat.ptr());
194 
195  // inst_.lightprobes.set_view(render_view_, extent_);
196  // inst_.lights.set_view(render_view_, extent_, !inst_.use_scene_lights());
197 }
198 
201 } // namespace blender::eevee
typedef float(TangentPoint)[2]
void window_translate_m4(float winmat[4][4], float perspmat[4][4], float x, float y)
Definition: math_geom.c:4587
void perspective_m4(float mat[4][4], float left, float right, float bottom, float top, float nearClip, float farClip)
Definition: math_geom.c:4542
#define UNPACK2(a)
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
struct GPUTexture GPUTexture
Definition: GPU_texture.h:17
const CameraData & data_get() const
Definition: eevee_camera.hh:98
float2 pixel_jitter_get() const
Definition: eevee_film.cc:438
void accumulate(const DRWView *view)
Definition: eevee_film.cc:541
int2 render_extent_get() const
Definition: eevee_film.hh:85
void render(const DRWView *view, Framebuffer &prepass_fb, Framebuffer &combined_fb, GPUTexture *depth_tx, GPUTexture *combined_tx)
RenderBuffers render_buffers
void acquire(int2 extent, void *owner)
GPUTexture * render_post(GPUTexture *input_tx)
Definition: eevee_view.cc:151
void DRW_view_winmat_get(const DRWView *view, float mat[4][4], bool inverse)
DRWView * DRW_view_create(const float viewmat[4][4], const float winmat[4][4], const float(*culling_viewmat)[4], const float(*culling_winmat)[4], DRWCallVisibilityFn *visibility_fn)
DRWView * DRW_view_create_sub(const DRWView *parent_view, const float viewmat[4][4], const float winmat[4][4])
void DRW_view_update_sub(DRWView *view, const float viewmat[4][4], const float winmat[4][4])
void DRW_view_viewmat_get(const DRWView *view, float mat[4][4], bool inverse)
void DRW_view_set_active(const DRWView *view)
void DRW_stats_group_start(const char *name)
void DRW_stats_group_end(void)
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img GPU_RGBA16F
#define VELOCITY_INVALID
smooth(Type::VEC3, "prev") .smooth(Type CameraData
#define ceilf(x)
Definition: metal/compat.h:225
#define sqrtf(x)
Definition: metal/compat.h:243
vec_base< float, 4 > float4
vec_base< float, 2 > float2
__int64 int64_t
Definition: stdint.h:89
c_style_float4x4 & ptr()