Blender V5.3
overlay_empty.hh
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
9#pragma once
10
11#include "BKE_image_gpu.hh"
12
13#include "DRW_render.hh"
14#include "overlay_base.hh"
15#include "overlay_image.hh"
16
17namespace blender::draw::overlay {
18
23 friend class Cameras;
24 using EmptyInstanceBuf = ShapeInstanceBuf<ExtraInstanceData>;
25
26 private:
27 /* Images added by Image > Background. Both added in preset view (like Top, Front, ..) and in
28 * custom view. Object property "In Front" unchecked. */
29 PassSortable images_back_ps_ = {"images_back_ps_"};
30 /* All Empty images from cases of `images_ps_`, `images_blend_ps_`, `images_back_ps_`
31 * with object property "In Front" checked. */
32 PassSortable images_front_ps_ = {"images_front_ps_"};
33
34 /* Images added by Empty > Image and Image > Reference with unchecked image "Opacity".
35 * Object property "In Front" unchecked. */
36 PassMain images_ps_ = {"images_ps_"};
37 /* Images added by Empty > Image and Image > Reference with image "Opacity" checked.
38 * Object property "In Front" unchecked. */
39 PassSortable images_blend_ps_ = {"images_blend_ps_"};
40
41 PassSimple ps_ = {"Empties"};
42
43 struct CallBuffers {
44 const SelectionType selection_type_;
45 EmptyInstanceBuf plain_axes_buf = {selection_type_, "plain_axes_buf"};
46 EmptyInstanceBuf single_arrow_buf = {selection_type_, "single_arrow_buf"};
47 EmptyInstanceBuf cube_buf = {selection_type_, "cube_buf"};
48 EmptyInstanceBuf circle_buf = {selection_type_, "circle_buf"};
49 EmptyInstanceBuf sphere_buf = {selection_type_, "sphere_buf"};
50 EmptyInstanceBuf cone_buf = {selection_type_, "cone_buf"};
51 EmptyInstanceBuf arrows_buf = {selection_type_, "arrows_buf"};
52 EmptyInstanceBuf image_buf = {selection_type_, "image_buf"};
53 } call_buffers_;
54
55 View::OffsetData offset_data_;
56 float4x4 depth_bias_winmat_;
57
58 public:
59 Empties(const SelectionType selection_type) : call_buffers_{selection_type} {};
60
61 void begin_sync(Resources &res, const State &state) final
62 {
63 enabled_ = state.is_space_v3d() && state.show_extras();
64
65 if (!enabled_) {
66 return;
67 }
68
69 offset_data_ = state.offset_data_get();
70
71 auto init_pass = [&](PassMain &pass, DRWState draw_state) {
72 pass.init();
73 pass.state_set(draw_state, state.clipping_plane_count);
74 pass.shader_set(res.shaders->image_plane_depth_bias.get());
75 pass.push_constant("depth_bias_winmat", &depth_bias_winmat_);
76 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
77 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
78 res.select_bind(pass);
79 };
80
81 auto init_sortable = [&](PassSortable &pass, DRWState draw_state) {
82 pass.init();
83 PassMain::Sub &sub = pass.sub("ResourceBind", -FLT_MAX);
84 sub.state_set(draw_state, state.clipping_plane_count);
85 res.select_bind(pass, sub);
86 };
87
88 DRWState draw_state;
89
91 init_pass(images_ps_, draw_state);
92
94 init_sortable(images_back_ps_, draw_state);
95 init_sortable(images_blend_ps_, draw_state);
96
98 init_sortable(images_front_ps_, draw_state);
99
100 begin_sync(call_buffers_);
101 }
102
103 static void begin_sync(CallBuffers &call_buffers)
104 {
105 call_buffers.plain_axes_buf.clear();
106 call_buffers.single_arrow_buf.clear();
107 call_buffers.cube_buf.clear();
108 call_buffers.circle_buf.clear();
109 call_buffers.sphere_buf.clear();
110 call_buffers.cone_buf.clear();
111 call_buffers.arrows_buf.clear();
112 call_buffers.image_buf.clear();
113 }
114
115 void object_sync(Manager &manager,
116 const ObjectRef &ob_ref,
117 Resources &res,
118 const State &state) final
119 {
120 if (!enabled_) {
121 return;
122 }
123
124 const float4 color = res.object_wire_color(ob_ref, state);
125 const select::ID select_id = res.select_id(ob_ref);
126 if (ob_ref.object->empty_drawtype == OB_EMPTY_IMAGE) {
127 image_sync(ob_ref, select_id, manager, res, state, call_buffers_.image_buf);
128 return;
129 }
130 /* Only draw the empty overlay if it's not a collection instance and the evaluated geometry set
131 * is empty. Since we draw overlays for e.g. generated geometry, it is redundant to draw the
132 * empty overlay here. */
133 const bool empty_has_geometry = ob_ref.object->runtime->geometry_set_eval &&
134 !ob_ref.object->runtime->geometry_set_eval->is_empty();
135 if (empty_has_geometry && ob_ref.object->instance_collection == nullptr) {
136 return;
137 }
138 object_sync(select_id,
139 ob_ref.object->object_to_world(),
140 ob_ref.object->empty_drawsize,
141 ob_ref.object->empty_drawtype,
142 color,
143 call_buffers_);
144 }
145
146 static void object_sync(const select::ID select_id,
147 const float4x4 &matrix,
148 const float draw_size,
149 const char empty_drawtype,
150 const float4 &color,
151 CallBuffers &call_buffers)
152 {
153 ExtraInstanceData data(matrix, color, draw_size);
154
155 switch (empty_drawtype) {
156 case OB_PLAINAXES:
157 call_buffers.plain_axes_buf.append(data, select_id);
158 break;
159 case OB_SINGLE_ARROW:
160 call_buffers.single_arrow_buf.append(data, select_id);
161 break;
162 case OB_CUBE:
163 call_buffers.cube_buf.append(data, select_id);
164 break;
165 case OB_CIRCLE:
166 call_buffers.circle_buf.append(data, select_id);
167 break;
168 case OB_EMPTY_SPHERE:
169 call_buffers.sphere_buf.append(data, select_id);
170 break;
171 case OB_EMPTY_CONE:
172 call_buffers.cone_buf.append(data, select_id);
173 break;
174 case OB_ARROWS:
175 call_buffers.arrows_buf.append(data, select_id);
176 break;
177 }
178 }
179
180 void end_sync(Resources &res, const State &state) final
181 {
182 if (!enabled_) {
183 return;
184 }
185
186 ps_.init();
187 res.select_bind(ps_);
188 end_sync(res, state, ps_, call_buffers_);
189 }
190
191 static void end_sync(Resources &res,
192 const State &state,
193 PassSimple::Sub &ps,
194 CallBuffers &call_buffers)
195 {
197 state.clipping_plane_count);
201
202 call_buffers.plain_axes_buf.end_sync(ps, res.shapes.plain_axes.get());
203 call_buffers.single_arrow_buf.end_sync(ps, res.shapes.single_arrow.get());
204 call_buffers.cube_buf.end_sync(ps, res.shapes.cube.get());
205 call_buffers.circle_buf.end_sync(ps, res.shapes.circle.get());
206 call_buffers.sphere_buf.end_sync(ps, res.shapes.empty_sphere.get());
207 call_buffers.cone_buf.end_sync(ps, res.shapes.empty_cone.get());
208 call_buffers.arrows_buf.end_sync(ps, res.shapes.arrows.get());
209 call_buffers.image_buf.end_sync(ps, res.shapes.quad_wire.get());
210 }
211
212 void pre_draw(Manager &manager, View &view) final
213 {
214 if (!enabled_) {
215 return;
216 }
217
218 manager.generate_commands(images_back_ps_, view);
219 manager.generate_commands(images_ps_, view);
220 manager.generate_commands(images_blend_ps_, view);
221 manager.generate_commands(images_front_ps_, view);
222
223 depth_bias_winmat_ = offset_data_.winmat_polygon_offset(view.winmat(), -1.0f);
224 }
225
226 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
227 {
228 if (!enabled_) {
229 return;
230 }
231
232 GPU_framebuffer_bind(framebuffer);
233 manager.submit(ps_, view);
234 }
235
236 void draw_background_images(Framebuffer &framebuffer, Manager &manager, View &view)
237 {
238 if (!enabled_) {
239 return;
240 }
241
242 GPU_framebuffer_bind(framebuffer);
243 manager.submit_only(images_back_ps_, view);
244 }
245
246 void draw_images(Framebuffer &framebuffer, Manager &manager, View &view)
247 {
248 if (!enabled_) {
249 return;
250 }
251
252 GPU_framebuffer_bind(framebuffer);
253
254 manager.submit_only(images_ps_, view);
255 manager.submit_only(images_blend_ps_, view);
256 }
257
258 void draw_in_front_images(Framebuffer &framebuffer, Manager &manager, View &view)
259 {
260 if (!enabled_) {
261 return;
262 }
263
264 GPU_framebuffer_bind(framebuffer);
265
266 manager.submit_only(images_front_ps_, view);
267 }
268
269 private:
270 void image_sync(const ObjectRef &ob_ref,
271 select::ID select_id,
272 Manager &manager,
273 Resources &res,
274 const State &state,
275 EmptyInstanceBuf &empty_image_buf)
276 {
277 Object *ob = ob_ref.object;
278 gpu::Texture *tex = nullptr;
279 blender::Image *ima = id_cast<blender::Image *>(ob_ref.object->data);
280 float4x4 mat;
281
282 const bool show_frame = BKE_object_empty_image_frame_is_visible_in_view3d(ob, state.rv3d);
283 const bool show_image = show_frame &&
285 const bool use_alpha_blend = (ob_ref.object->empty_image_flag &
287 const bool use_alpha_premult = ima && (ima->alpha_mode == IMA_ALPHA_PREMUL);
288
289 if (!show_frame) {
290 return;
291 }
292
293 {
294 /* Calling #BKE_image_get_size may free the texture. Get the size from 'tex' instead,
295 * see: #59347 */
296 int2 size = int2(0);
297 if (ima != nullptr) {
298 ImageUser iuser = *ob->iuser;
299 Images::stereo_setup(state.scene, state.v3d, ima, &iuser);
300 tex = BKE_image_acquire_gpu_texture(ima, &iuser);
301 if (tex) {
304 }
305 }
306 CLAMP_MIN(size.x, 1);
307 CLAMP_MIN(size.y, 1);
308
309 float2 image_aspect;
310 calc_image_aspect(ima, size, image_aspect);
311
312 mat = ob->object_to_world();
313 mat.x_axis() *= image_aspect.x * 0.5f * ob->empty_drawsize;
314 mat.y_axis() *= image_aspect.y * 0.5f * ob->empty_drawsize;
315 mat.location() += (mat.x_axis() * (ob->ima_ofs[0] * 2.0f + 1.0f) +
316 mat.y_axis() * (ob->ima_ofs[1] * 2.0f + 1.0f));
317 }
318
319 if (show_frame) {
320 const float4 color = res.object_wire_color(ob_ref, state);
321 empty_image_buf.append(ExtraInstanceData(mat, color, 1.0f), select_id);
322 }
323
324 if (show_image && tex && ((ob->color[3] > 0.0f) || !use_alpha_blend)) {
325 /* Use the actual depth if we are doing depth tests to determine the distance to the
326 * object. */
327 const eObject_EmptyImageDepth depth_mode = state.is_depth_only_drawing ?
330 PassMain::Sub &pass = create_subpass(state, *ob, use_alpha_blend, mat, res);
331 pass.bind_texture("img_tx", tex);
332 pass.push_constant("img_premultiplied", use_alpha_premult);
333 pass.push_constant("img_alpha_blend", use_alpha_blend);
334 pass.push_constant("is_camera_background", false);
335 pass.push_constant("depth_set", depth_mode != OB_EMPTY_IMAGE_DEPTH_DEFAULT);
336 pass.push_constant("ucolor", float4(ob->color));
337 ResourceHandleRange res_handle = manager.resource_handle(mat);
338 pass.draw(res.shapes.quad_solid.get(), res_handle, select_id.get());
339 }
340 }
341
342 PassMain::Sub &create_subpass(const State &state,
343 const Object &ob,
344 const bool use_alpha_blend,
345 const float4x4 &mat,
346 Resources &res)
347 {
348 const bool in_front = state.use_in_front && (ob.dtx & OB_DRAW_IN_FRONT);
349 if (in_front) {
350 return create_subpass(state, mat, res, images_front_ps_, true);
351 }
352 const eObject_EmptyImageDepth depth_mode = state.is_depth_only_drawing ?
354 ob.empty_image_depth;
355 switch (depth_mode) {
357 return create_subpass(state, mat, res, images_back_ps_, false);
359 return create_subpass(state, mat, res, images_front_ps_, true);
361 default:
362 return use_alpha_blend ? create_subpass(state, mat, res, images_blend_ps_, true) :
363 images_ps_;
364 }
365 }
366
367 PassMain::Sub &create_subpass(const State &state,
368 const float4x4 &mat,
369 Resources &res,
370 PassSortable &parent,
371 bool depth_bias)
372 {
373 const float3 tmp = state.camera_position - mat.location();
374 const float z = -math::dot(state.camera_forward, tmp);
375 PassMain::Sub &sub = parent.sub("Sub", z);
376 if (depth_bias) {
377 sub.shader_set(res.shaders->image_plane_depth_bias.get());
378 sub.push_constant("depth_bias_winmat", &depth_bias_winmat_);
379 }
380 else {
381 sub.shader_set(res.shaders->image_plane.get());
382 }
383 sub.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
384 sub.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
385 return sub;
386 };
387
388 static void calc_image_aspect(blender::Image *ima, const int2 &size, float2 &r_image_aspect)
389 {
390 /* if no image, make it a 1x1 empty square, honor scale & offset */
391 const float2 ima_dim = ima ? float2(size.x, size.y) : float2(1.0f);
392
393 /* Get the image aspect even if the buffer is invalid */
394 float2 sca(1.0f);
395 if (ima) {
396 if (ima->aspx > ima->aspy) {
397 sca.y = ima->aspy / ima->aspx;
398 }
399 else if (ima->aspx < ima->aspy) {
400 sca.x = ima->aspx / ima->aspy;
401 }
402 }
403
404 const float2 scale_inv(ima_dim.x * sca.x, ima_dim.y * sca.y);
405 r_image_aspect = (scale_inv.x > scale_inv.y) ? float2(1.0f, scale_inv.y / scale_inv.x) :
406 float2(scale_inv.x / scale_inv.y, 1.0f);
407 }
408};
409
410} // namespace blender::draw::overlay
#define CLAMP_MIN(a, b)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void hold_texture(gpu::Texture *texture)
void submit_only(PassMain &pass, View &view)
ResourceHandleRange resource_handle(const ObjectRef &ref, float inflate_bounds=0.0f)
void shader_set(gpu::Shader *shader)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
void state_set(DRWState state, int clip_plane_count=0)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
void push_constant(const char *name, const float &data)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
void pre_draw(Manager &manager, View &view) final
static void object_sync(const select::ID select_id, const float4x4 &matrix, const float draw_size, const char empty_drawtype, const float4 &color, CallBuffers &call_buffers)
void draw_in_front_images(Framebuffer &framebuffer, Manager &manager, View &view)
void draw_images(Framebuffer &framebuffer, Manager &manager, View &view)
void begin_sync(Resources &res, const State &state) final
void end_sync(Resources &res, const State &state) final
Empties(const SelectionType selection_type)
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
static void begin_sync(CallBuffers &call_buffers)
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state) final
static void end_sync(Resources &res, const State &state, PassSimple::Sub &ps, CallBuffers &call_buffers)
void draw_background_images(Framebuffer &framebuffer, Manager &manager, View &view)
static void stereo_setup(const Scene *scene, const View3D *v3d, blender::Image *ima, ImageUser *iuser)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
#define FLT_MAX
const int state
select::SelectionType SelectionType
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
int size(const AssetLibraryReference *library_reference)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool BKE_object_empty_image_frame_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
Definition object.cc:3877
bool BKE_object_empty_image_data_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
Definition object.cc:3887
VecBase< int32_t, 2 > int2
BMesh const char void * data
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition draw_state.hh:59
@ DRW_STATE_DEPTH_LESS
Definition draw_state.hh:39
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
@ OB_EMPTY_IMAGE_USE_ALPHA_BLEND
@ OB_EMPTY_IMAGE_DEPTH_DEFAULT
@ OB_EMPTY_IMAGE_DEPTH_BACK
@ OB_EMPTY_IMAGE_DEPTH_FRONT
draw::Manager * DRW_manager_get()
gpu::Texture * BKE_image_acquire_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:732
int GPU_texture_original_height(const gpu::Texture *texture)
int GPU_texture_original_width(const gpu::Texture *texture)
ExtraInstanceData(const float4x4 &object_to_world, const float4 &color, float draw_size)
eObject_EmptyImageDepth empty_image_depth
eObject_EmptyImageFlag empty_image_flag
const float4 & object_wire_color(const ObjectRef &ob_ref, ThemeColorID theme_id) const
draw::UniformArrayBuffer< float4, 6 > clip_planes_buf
draw::UniformBuffer< UniformData > globals_buf
float x
float y