Blender V5.3
overlay_prepass.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_paint.hh"
12
13#include "DNA_particle_types.h"
14
15#include "draw_sculpt.hh"
16
17#include "overlay_base.hh"
19#include "overlay_particle.hh"
20
21namespace blender::draw::overlay {
22
23/* Add prepass which will write to the depth buffer so that the
24 * alpha-under overlays (alpha checker) will draw correctly for external engines.
25 * NOTE: Use the same Z-depth value as in the regular image drawing engine. */
27 private:
28 PassSimple ps_ = {"ImagePrepass"};
29
30 public:
31 void begin_sync(Resources &res, const State &state) final
32 {
33 enabled_ = state.is_space_image() && state.is_image_valid && !res.is_selection();
34
35 if (!enabled_) {
36 return;
37 }
38
39 ps_.init();
41 ps_.shader_set(res.shaders->mesh_edit_depth.get());
42 ps_.push_constant("retopology_offset", 0.0f);
43 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
44 ps_.draw(res.shapes.image_quad.get());
45 }
46
47 void draw_on_render(gpu::FrameBuffer *framebuffer, Manager &manager, View &view) final
48 {
49 if (!enabled_) {
50 return;
51 }
52
53 GPU_framebuffer_bind(framebuffer);
54 manager.submit(ps_, view);
55 }
56};
57
63 private:
64 PassMain ps_ = {"prepass"};
65 PassMain::Sub *mesh_ps_ = nullptr;
66 PassMain::Sub *mesh_flat_ps_ = nullptr;
67 PassMain::Sub *hair_ps_ = nullptr;
68 PassMain::Sub *curves_ps_ = nullptr;
69 PassMain::Sub *pointcloud_ps_ = nullptr;
70 PassMain::Sub *gsplat_ps_ = nullptr;
71 PassMain::Sub *grease_pencil_ps_ = nullptr;
72
73 bool use_material_slot_selection_ = false;
74
75 public:
76 void begin_sync(Resources &res, const State &state) final
77 {
78 enabled_ = state.is_space_v3d() && (!state.xray_enabled || res.is_selection());
79
80 if (!enabled_) {
81 /* Not used. But release the data. */
82 ps_.init();
83 mesh_ps_ = nullptr;
84 curves_ps_ = nullptr;
85 pointcloud_ps_ = nullptr;
86 gsplat_ps_ = nullptr;
87 return;
88 }
89
90 use_material_slot_selection_ = state.is_material_select;
91
92 bool use_cull = res.globals_buf.backface_culling;
93 DRWState backface_cull_state = use_cull ? DRW_STATE_CULL_BACK : DRWState(0);
94
95 ps_.init();
96 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
97 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
98 ps_.state_set(DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | backface_cull_state,
99 state.clipping_plane_count);
100 res.select_bind(ps_);
101 {
102 auto &sub = ps_.sub("Mesh");
103 sub.shader_set(res.is_selection() ? res.shaders->depth_mesh_conservative.get() :
104 res.shaders->depth_mesh.get());
105 mesh_ps_ = ⊂
106 }
107 {
108 auto &sub = ps_.sub("MeshFlat");
109 sub.shader_set(res.shaders->depth_mesh.get());
110 mesh_flat_ps_ = ⊂
111 }
112 {
113 auto &sub = ps_.sub("Hair");
114 sub.shader_set(res.shaders->depth_mesh.get());
115 hair_ps_ = ⊂
116 }
117 {
118 auto &sub = ps_.sub("Curves");
119 sub.shader_set(res.shaders->depth_curves.get());
120 curves_ps_ = ⊂
121 }
122 {
123 auto &sub = ps_.sub("PointCloud");
124 sub.shader_set(res.shaders->depth_pointcloud.get());
125 pointcloud_ps_ = ⊂
126 }
127 {
128 auto &sub = ps_.sub("GSplat");
129 sub.shader_set(res.shaders->depth_gsplat.get());
130 gsplat_ps_ = ⊂
131 }
132 {
133 auto &sub = ps_.sub(RE_PASSNAME_GREASE_PENCIL);
134 sub.shader_set(res.shaders->depth_grease_pencil.get());
135 grease_pencil_ps_ = ⊂
136 }
137 }
138
139 void particle_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state)
140 {
141 if (state.skip_particles) {
142 return;
143 }
144
145 Object *ob = ob_ref.object;
146
147 ResourceHandleRange handle = {};
148
149 for (ParticleSystem &psys : ob->particlesystem) {
151 continue;
152 }
153
154 const ParticleSettings *part = psys.part;
155 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
156 switch (draw_as) {
157 case PART_DRAW_PATH:
158 if ((state.is_wireframe_mode == false) && (part->draw_as == PART_DRAW_REND)) {
159 /* Case where the render engine should have rendered it, but we need to draw it for
160 * selection purpose. */
161 if (!handle.is_valid()) {
162 handle = manager.resource_handle_for_psys(ob_ref, ob_ref.particles_matrix());
163 }
164
165 select::ID select_id = use_material_slot_selection_ ?
166 res.select_id(ob_ref, part->omat << 16) :
167 res.select_id(ob_ref);
168
169 gpu::Batch *geom = DRW_cache_particles_get_hair(ob, &psys, nullptr);
170 if (res.is_selection()) {
171 /* Conservative shader needs expanded draw-call. */
172 mesh_ps_->draw_expand(geom, GPU_PRIM_TRIS, 1, 1, handle, select_id.get());
173 }
174 else {
175 mesh_ps_->draw(geom, handle, select_id.get());
176 }
177 break;
178 }
179 break;
180 default:
181 /* Other draw modes should be handled by the particle overlay. */
182 break;
183 }
184 }
185 }
186
187 void sculpt_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res)
188 {
189 ResourceHandleRange handle = manager.unique_handle_for_sculpt(ob_ref);
190
192 select::ID select_id = use_material_slot_selection_ ?
193 res.select_id(ob_ref, (batch.material_slot + 1) << 16) :
194 res.select_id(ob_ref);
195
196 if (res.is_selection()) {
197 /* Conservative shader needs expanded draw-call. */
198 mesh_ps_->draw_expand(batch.batch, GPU_PRIM_TRIS, 1, 1, handle, select_id.get());
199 }
200 else {
201 mesh_ps_->draw(batch.batch, handle, select_id.get());
202 }
203 }
204 }
205
206 void object_sync(Manager &manager,
207 const ObjectRef &ob_ref,
208 Resources &res,
209 const State &state) final
210 {
211 bool is_solid = ob_ref.object->dt >= OB_SOLID ||
212 (state.v3d->shading.type == OB_RENDER &&
213 !(ob_ref.object->visibility_flag & OB_HIDE_CAMERA));
214
215 if (!enabled_ || !is_solid) {
216 return;
217 }
218
219 particle_sync(manager, ob_ref, res, state);
220
221 const bool use_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob_ref.object, state.rv3d) &&
222 !state.is_image_render;
223
224 if (use_sculpt_pbvh) {
225 sculpt_sync(manager, ob_ref, res);
226 return;
227 }
228
229 gpu::Batch *geom_single = nullptr;
230 Span<gpu::Batch *> geom_list(&geom_single, 1);
231
232 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
233
234 PassMain::Sub *pass = nullptr;
235 switch (ob_ref.object->type) {
236 case OB_MESH:
237 if (use_material_slot_selection_) {
238 /* TODO(fclem): Improve the API. */
239 const int materials_len = BKE_object_material_used_with_fallback_eval(*ob_ref.object);
240 Array<GPUMaterial *> materials(materials_len, nullptr);
241 geom_list = DRW_cache_mesh_surface_shaded_get(ob_ref.object, materials);
242 }
243 else {
244 geom_single = DRW_cache_mesh_surface_get(ob_ref.object);
245
246 if (res.is_selection() && !use_material_slot_selection_ &&
248 {
249 /* Avoid losing flat objects when in ortho views (see #56549) */
250 mesh_flat_ps_->draw(DRW_cache_mesh_all_edges_get(ob_ref.object),
251 manager.unique_handle(ob_ref),
252 res.select_id(ob_ref).get());
253 }
254 }
255 pass = mesh_ps_;
256 break;
257 case OB_VOLUME:
258 if (!res.is_selection()) {
259 /* Disable during display, only enable for selection. */
260 /* TODO(fclem): Would be nice to have even when not selecting to occlude overlays. */
261 return;
262 }
263 geom_single = DRW_cache_volume_selection_surface_get(ob_ref.object);
264 pass = mesh_ps_;
265 /* TODO(fclem): Get rid of these check and enforce correct API on the batch cache. */
266 if (geom_single == nullptr) {
267 return;
268 }
269 break;
270 case OB_POINTCLOUD:
271 pass = pointcloud_is_gsplat(ob_ref.object) ? gsplat_ps_ : pointcloud_ps_;
272 geom_single = pointcloud_sub_pass_setup(*pass, ob_ref, res_handle);
273 break;
274 case OB_CURVES: {
275 const char *error = nullptr;
276 /* The error string will always have been printed by the engine already.
277 * No need to display it twice. */
278 geom_single = curves_sub_pass_setup(*curves_ps_, state.scene, ob_ref.object, error);
279 pass = curves_ps_;
280 break;
281 }
282 case OB_GREASE_PENCIL:
283 if (!res.is_selection() && state.is_render_depth_available) {
284 /* Disable during display, only enable for selection.
285 * The grease pencil engine already renders it properly. */
286 return;
287 }
289 *grease_pencil_ps_,
290 state.scene,
291 ob_ref.object,
292 manager.unique_handle(ob_ref),
293 res.select_id(ob_ref));
294 return;
295 default:
296 break;
297 }
298
299 if (pass == nullptr) {
300 return;
301 }
302
303 for (int material_id : geom_list.index_range()) {
304 /* Meshes with more than 16 materials can have nullptr in the geometry list as materials are
305 * not filled for unused materials indices. We should actually use `material_indices_used`
306 * but these are only available for meshes. */
307 if (geom_list[material_id] == nullptr) {
308 continue;
309 }
310
311 select::ID select_id = use_material_slot_selection_ ?
312 res.select_id(ob_ref, (material_id + 1) << 16) :
313 res.select_id(ob_ref);
314 if (res.is_selection() && (pass == mesh_ps_)) {
315 /* Conservative shader needs expanded draw-call. */
316 pass->draw_expand(
317 geom_list[material_id], GPU_PRIM_TRIS, 1, 1, res_handle, select_id.get());
318 }
319 else {
320 pass->draw(geom_list[material_id], res_handle, select_id.get());
321 }
322 }
323 }
324
325 void pre_draw(Manager &manager, View &view) final
326 {
327 if (!enabled_) {
328 return;
329 }
330
331 manager.generate_commands(ps_, view);
332 }
333
334 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
335 {
336 if (!enabled_) {
337 return;
338 }
339 /* Should be fine to use the line buffer since the prepass only writes to the depth buffer. */
340 GPU_framebuffer_bind(framebuffer);
341 manager.submit_only(ps_, view);
342 }
343};
344
345} // namespace blender::draw::overlay
#define RE_PASSNAME_GREASE_PENCIL
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
ResourceHandle resource_handle_for_psys(const ObjectRef &ref, const float4x4 &model_matrix)
ResourceHandleRange unique_handle_for_sculpt(const ObjectRef &ref)
float4x4 particles_matrix() const
void draw_expand(gpu::Batch *batch, GPUPrimType primitive_type, uint primitive_len, uint instance_len, uint vertex_len, uint vertex_first, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:938
void draw(gpu::Batch *batch, uint instance_len=-1, uint vertex_len=-1, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:906
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
static void draw_grease_pencil(Resources &res, PassMain::Sub &pass, const Scene *scene, Object *ob, ResourceHandleRange res_handle, select::ID select_id=select::SelectMap::select_invalid_id())
void begin_sync(Resources &res, const State &state) final
void draw_on_render(gpu::FrameBuffer *framebuffer, Manager &manager, View &view) final
void sculpt_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res)
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state) final
void begin_sync(Resources &res, const State &state) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void pre_draw(Manager &manager, View &view) final
void particle_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * curves_sub_pass_setup(PassMain::Sub &ps, const Scene *scene, Object *ob, const char *&r_error, GPUMaterial *gpu_material=nullptr)
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
bool pointcloud_is_gsplat(Object *ob)
Span< gpu::Batch * > DRW_cache_mesh_surface_shaded_get(Object *ob, const Span< const GPUMaterial * > materials)
gpu::Batch * pointcloud_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
gpu::Batch * DRW_cache_particles_get_hair(Object *object, ParticleSystem *psys, ModifierData *md)
gpu::Batch * DRW_cache_volume_selection_surface_get(Object *ob)
gpu::Batch * DRW_cache_mesh_surface_get(Object *ob)
gpu::Batch * DRW_cache_mesh_all_edges_get(Object *ob)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
int BKE_object_material_used_with_fallback_eval(const Object &ob)
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:38
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
ListBaseT< struct ParticleSystem > particlesystem
static int flat_axis_index_get(const ObjectRef &ob_ref)
const ID select_id(const ObjectRef &ob_ref, uint sub_object_id=0)