Blender V5.3
eevee_lightprobe_planar.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "eevee_instance.hh"
11
12#include "draw_common.hh"
13
14namespace blender::eevee {
15
16using namespace blender::math;
17
18/* -------------------------------------------------------------------- */
21
23{
24 /* Invert the up axis to avoid changing handedness (see #137022). */
25 this->viewmat = from_scale<float4x4>(float3(1, -1, 1)) * view.viewmat() *
26 reflection_matrix_get();
27 this->winmat = view.winmat();
28 /* Invert Y offset in the projection matrix to compensate the flip above (see #141112). */
29 this->winmat[2][1] = -this->winmat[2][1];
30
31 this->wininv = invert(this->winmat);
32
35
36 float3 view_vec = view.is_persp() ? view.location() - plane_to_world.location() : view.forward();
37 bool view_is_below_plane = dot(view_vec, plane_to_world.z_axis()) < 0.0;
38 if (view_is_below_plane) {
39 this->normal = -this->normal;
40 }
41 this->layer_id = layer_id;
42}
43
45
46/* -------------------------------------------------------------------- */
49
51{
52 /* This triggers the compilation of clipped shader only if we can detect light-probe planes. */
53 if (inst_.is_viewport() && !inst_.is_image_render) {
54 /* This check needs to happen upfront before sync, so we use the previous sync result. */
55 update_probes_ = !inst_.light_probes.planar_map_.is_empty();
56 }
57 else {
58 /* TODO(jbakker): should we check on the subtype as well? Now it also populates even when
59 * there are other light probes in the scene. */
60 update_probes_ = DEG_id_type_any_exists(inst_.depsgraph, ID_LP);
61 }
62
63 do_display_draw_ = false;
64
66 /* Tag the end of the array. */
70 gpu::TextureFormat::UFLOAT_11_11_10, int2(1), 1, usage);
72 gpu::TextureFormat::SFLOAT_32_DEPTH, int2(1), 1, usage);
73}
74
76{
77 /* When first planar probes are enabled it can happen that the first sample is off. */
78 if (!update_probes_ && !inst_.light_probes.planar_map_.is_empty()) {
80 }
81}
82
83void PlanarProbeModule::set_view(const draw::View &main_view, int2 main_view_extent)
84{
85 GBuffer &gbuf = inst_.gbuffer;
86
87 const int64_t num_probes = inst_.light_probes.planar_map_.size();
88
89 /* TODO resolution percentage. */
90 int2 extent = main_view_extent;
91 int layer_count = num_probes;
92
93 const gpu::TextureFormat depth_format = gpu::TextureFormat::SFLOAT_32_DEPTH;
94
95 if (num_probes == 0) {
96 /* Create valid dummy texture. */
97 extent = int2(1);
98 layer_count = 1;
99 }
100 else {
101 inst_.render_buffers.acquire(extent, depth_format);
102 }
103
105 radiance_tx_.ensure_2d_array(gpu::TextureFormat::UFLOAT_11_11_10, extent, layer_count, usage);
106 depth_tx_.ensure_2d_array(depth_format, extent, layer_count, usage);
107 depth_tx_.ensure_layer_views();
108
109 do_display_draw_ = inst_.draw_overlays && num_probes > 0;
110
111 int resource_index = 0;
112 int display_index = 0;
113 for (PlanarProbe &probe : inst_.light_probes.planar_map_.values()) {
114 if (resource_index == PLANAR_PROBE_MAX) {
115 break;
116 }
117
118 PlanarResources &res = resources_[resource_index];
119
120 /* TODO Cull out of view planars. */
121
122 probe.set_view(main_view, resource_index);
123 probe_planar_buf_[resource_index] = probe;
124
125 res.view.sync(probe.viewmat, probe.winmat);
126
127 world_clip_buf_.plane = probe.reflection_clip_plane_get();
128 world_clip_buf_.push_update();
129
130 /* Hand off gsplat compute workload for the planar view before draws. */
131 DRW_gsplat_ensure_radiance(*inst_.manager, res.view);
132
133 RenderBuffers &rbufs = inst_.render_buffers;
134 const bool with_raycast = inst_.pipelines.has_raycast;
135
136 res.prepass_fb.ensure(
137 GPU_ATTACHMENT_TEXTURE_LAYER(depth_tx_, resource_index),
140 GPU_ATTACHMENT_NONE /* motion vectors */);
141
142 if (with_raycast) {
143 rbufs.object_id_tx.clear(uint4(0));
144 rbufs.prepass_normal_tx.clear(float4(0.0f));
145 }
146
147 gbuf.acquire(extent,
148 inst_.pipelines.deferred.header_layer_count(),
149 inst_.pipelines.deferred.closure_layer_count(),
150 inst_.pipelines.deferred.normal_layer_count());
151
152 res.combined_fb.ensure(GPU_ATTACHMENT_TEXTURE_LAYER(depth_tx_, resource_index),
153 GPU_ATTACHMENT_TEXTURE_LAYER(radiance_tx_, resource_index));
154
155 res.gbuffer_fb.ensure(GPU_ATTACHMENT_TEXTURE_LAYER(depth_tx_, resource_index),
156 GPU_ATTACHMENT_TEXTURE_LAYER(radiance_tx_, resource_index),
161
162 inst_.pipelines.planar.render(res.view,
163 depth_tx_.layer_view(resource_index),
164 res.prepass_fb,
165 res.gbuffer_fb,
166 res.combined_fb,
167 extent);
168
169 if (do_display_draw_ && probe.viewport_display) {
170 display_data_buf_.get_or_resize(display_index++) = {probe.plane_to_world, resource_index};
171 }
172
173 resource_index++;
174 }
175
176 gbuf.release();
177
178 if (resource_index < PLANAR_PROBE_MAX) {
179 /* Tag the end of the array. */
180 probe_planar_buf_[resource_index].layer_id = -1;
181 }
182 probe_planar_buf_.push_update();
183
184 do_display_draw_ = display_index > 0;
185 if (do_display_draw_) {
186 display_data_buf_.resize(display_index);
187 display_data_buf_.push_update();
188 }
189}
190
192{
193 if (!do_display_draw_) {
194 return;
195 }
196
197 viewport_display_ps_.init();
198 viewport_display_ps_.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
199 DRW_STATE_CLIP_CONTROL_UNIT_RANGE | inst_.film.depth.test_state |
201 viewport_display_ps_.framebuffer_set(&view_fb);
202 viewport_display_ps_.shader_set(inst_.shaders.static_shader_get(DISPLAY_PROBE_PLANAR));
203 SphereProbeData &world_data = *static_cast<SphereProbeData *>(&inst_.light_probes.world_sphere_);
204 viewport_display_ps_.push_constant("world_coord_packed",
205 reinterpret_cast<int4 *>(&world_data.atlas_coord));
206 viewport_display_ps_.bind_resources(*this);
207 viewport_display_ps_.bind_resources(inst_.sphere_probes);
208 viewport_display_ps_.bind_ssbo("display_data_buf", display_data_buf_);
209 viewport_display_ps_.draw_procedural(GPU_PRIM_TRIS, 1, display_data_buf_.size() * 6);
210
211 inst_.manager->submit(viewport_display_ps_, view);
212}
213
215
216} // namespace blender::eevee
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define GPU_ATTACHMENT_TEXTURE_LAYER(_texture, _layer)
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)
void clear(float4 values)
bool ensure_2d_array(gpu::TextureFormat format, int2 extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
gpu::Texture * layer_view(int layer)
const float3 & location(int view_id=0) const
Definition draw_view.hh:124
bool is_persp(int view_id=0) const
Definition draw_view.hh:93
const float4x4 & winmat(int view_id=0) const
Definition draw_view.hh:148
const float4x4 & viewmat(int view_id=0) const
Definition draw_view.hh:136
const float3 & forward(int view_id=0) const
Definition draw_view.hh:130
void sync(const float4x4 &view_mat, const float4x4 &win_mat, int view_id=0)
Definition draw_view.cc:27
struct blender::eevee::PlanarProbeModule::Dummy dummy_resources
void set_view(const draw::View &main_view, int2 main_view_extent)
void viewport_draw(View &view, gpu::FrameBuffer *view_fb)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
MatBase< R, C > transpose(MatBase< C, R >) RET
float2x2 invert(float2x2 mat)
float2x2 from_scale(float2 scale)
void DRW_gsplat_ensure_radiance(draw::Manager &manager, draw::View &view)
__int64 int64_t
Definition BKE_text.h:90
VecBase< int32_t, 4 > int4
VecBase< uint32_t, 4 > uint4
MatBase< float, 3, 4 > float3x4
VecBase< float, 4 > float4
bool DEG_id_type_any_exists(const Depsgraph *depsgraph, short id_type)
VecBase< int32_t, 2 > int2
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_CLIP_CONTROL_UNIT_RANGE
Definition draw_state.hh:71
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
VecBase< float, 3 > float3
void DRW_viewport_request_redraw()
TextureFromPool header_tx
void acquire(int2 extent, int header_count, int data_count, int normal_count)
TextureFromPool closure_tx
TextureFromPool normal_tx
void set_view(const draw::View &view, int layer_id)