Blender V5.3
overlay_paint.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.hh"
12#include "BKE_image_gpu.hh"
13#include "BKE_paint.hh"
14#include "BKE_scene.hh"
15
17
18#include "draw_cache.hh"
19#include "draw_cache_impl.hh"
20
21#include "overlay_base.hh"
22
23namespace blender::draw::overlay {
24
29class Paints : Overlay {
30
31 private:
32 /* Draw selection state on top of the mesh to communicate which areas can be painted on. */
33 PassSimple paint_region_ps_ = {"paint_region_ps_"};
34 PassSimple::Sub *paint_region_edge_ps_ = nullptr;
35 PassSimple::Sub *paint_region_face_ps_ = nullptr;
36 PassSimple::Sub *paint_region_vert_ps_ = nullptr;
37
38 PassSimple weight_ps_ = {"weight_ps_"};
39 /* Used when there's not a valid pre-pass (depth <=). */
40 PassSimple::Sub *weight_opaque_ps_ = nullptr;
41 /* Used when there's a valid pre-pass (depth ==). */
42 PassSimple::Sub *weight_masked_transparency_ps_ = nullptr;
43 /* Black and white mask overlaid on top of mesh to preview painting influence. */
44 PassSimple paint_mask_ps_ = {"paint_mask_ps_"};
45
46 bool show_weight_ = false;
47 bool show_wires_ = false;
48 bool show_paint_mask_ = false;
49 bool masked_transparency_support_ = false;
50
51 public:
52 void begin_sync(Resources &res, const State &state) final
53 {
54 enabled_ =
55 state.is_space_v3d() && !res.is_selection() &&
57
58 /* Init in any case to release the data. */
59 paint_region_ps_.init();
60 weight_ps_.init();
61 paint_mask_ps_.init();
62
63 if (!enabled_) {
64 return;
65 }
66
67 show_weight_ = state.ctx_mode == CTX_MODE_PAINT_WEIGHT;
68 show_wires_ = state.overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE;
69
70 {
71 auto &pass = paint_region_ps_;
72 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
73 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
74 {
75 auto &sub = pass.sub("Face");
78 state.clipping_plane_count);
79 sub.shader_set(res.shaders->paint_region_face.get());
80 sub.push_constant("ucolor", float4(1.0, 1.0, 1.0, 0.2));
81 paint_region_face_ps_ = &sub;
82 }
83 {
84 auto &sub = pass.sub("Edge");
87 state.clipping_plane_count);
88 sub.shader_set(res.shaders->paint_region_edge.get());
89 paint_region_edge_ps_ = &sub;
90 }
91 {
92 auto &sub = pass.sub("Vert");
94 state.clipping_plane_count);
95 sub.shader_set(res.shaders->paint_region_vert.get());
96 paint_region_vert_ps_ = &sub;
97 }
98 }
99
100 if (state.ctx_mode == CTX_MODE_PAINT_WEIGHT) {
101 /* Support masked transparency in Workbench.
102 * EEVEE can't be supported since depth won't match. */
103 const eDrawType shading_type = eDrawType(state.v3d->shading.type);
104 masked_transparency_support_ = ((shading_type == OB_SOLID) ||
105 (shading_type >= OB_SOLID &&
107 !state.xray_enabled;
108 const bool shadeless = shading_type == OB_WIRE;
109 const bool draw_contours = state.overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS;
110
111 auto &pass = weight_ps_;
112 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
113 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
114 auto weight_subpass = [&](const char *name, DRWState drw_state) {
115 auto &sub = pass.sub(name);
116 sub.state_set(drw_state, state.clipping_plane_count);
117 sub.shader_set(shadeless ? res.shaders->paint_weight.get() :
118 res.shaders->paint_weight_fake_shading.get());
119 sub.bind_texture("colorramp", &res.weight_ramp_tx);
120 sub.push_constant("draw_contours", draw_contours);
121 sub.push_constant("opacity", state.overlay.weight_paint_mode_opacity);
122 if (!shadeless) {
123 /* Arbitrary light to give a hint of the geometry behind the weights. */
124 sub.push_constant("light_dir", math::normalize(float3(0.0f, 0.5f, 0.86602f)));
125 }
126 return &sub;
127 };
128 weight_opaque_ps_ = weight_subpass(
130 weight_masked_transparency_ps_ = weight_subpass(
131 "Masked Transparency",
133 }
134
135 if (state.ctx_mode == CTX_MODE_PAINT_TEXTURE) {
136 const ImagePaintSettings &paint_settings = state.scene->toolsettings->imapaint;
137 show_paint_mask_ = paint_settings.stencil &&
138 (paint_settings.flag & IMAGEPAINT_PROJECT_LAYER_STENCIL);
139
140 if (show_paint_mask_) {
141 const bool mask_premult = (paint_settings.stencil->alpha_mode == IMA_ALPHA_PREMUL);
142 const bool mask_inverted = (paint_settings.flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV);
143 gpu::Texture *mask_texture = BKE_image_acquire_gpu_texture(paint_settings.stencil,
144 nullptr);
145 DRW_manager_get()->hold_texture(mask_texture);
146
147 auto &pass = paint_mask_ps_;
149 state.clipping_plane_count);
150 pass.shader_set(res.shaders->paint_texture.get());
151 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
152 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
153 pass.bind_texture("mask_image", mask_texture);
154 pass.push_constant("maskPremult", mask_premult);
155 pass.push_constant("mask_invert_stencil", mask_inverted);
156 pass.push_constant("mask_color", float3(paint_settings.stencil_col));
157 pass.push_constant("opacity", state.overlay.texture_paint_mode_opacity);
158 }
159 }
160 }
161
162 void object_sync(Manager &manager,
163 const ObjectRef &ob_ref,
164 Resources & /*res*/,
165 const State &state) final
166 {
167 if (!enabled_) {
168 return;
169 }
170
171 if (ob_ref.object->type != OB_MESH) {
172 /* Only meshes are supported for now. */
173 return;
174 }
175
176 switch (state.ctx_mode) {
178 if (ob_ref.object->mode != OB_MODE_WEIGHT_PAINT) {
179 /* Not matching context mode. */
180 return;
181 }
182 break;
184 if (ob_ref.object->mode != OB_MODE_VERTEX_PAINT) {
185 /* Not matching context mode. */
186 return;
187 }
188 break;
190 if (ob_ref.object->mode != OB_MODE_TEXTURE_PAINT) {
191 /* Not matching context mode. */
192 return;
193 }
194 break;
195 default:
196 /* Not in paint mode. */
197 return;
198 }
199
200 switch (state.ctx_mode) {
202 gpu::Batch *geom = DRW_cache_mesh_surface_weights_get(ob_ref.object);
203 if (masked_transparency_support_ && ob_ref.object->dt >= OB_SOLID) {
204 weight_masked_transparency_ps_->draw(geom, manager.unique_handle(ob_ref));
205 }
206 else {
207 weight_opaque_ps_->draw(geom, manager.unique_handle(ob_ref));
208 }
209 break;
210 }
212 /* Drawing of vertex paint color is done by the render engine (i.e. workbench). */
213 break;
214 }
216 if (show_paint_mask_) {
217 gpu::Batch *geom = DRW_cache_mesh_surface_texpaint_single_get(ob_ref.object);
218 paint_mask_ps_.draw(geom, manager.unique_handle(ob_ref));
219 }
220 break;
221 }
222 default:
224 return;
225 }
226
227 /* Selection Display. */
228 {
229 /* NOTE(fclem): Why do we need original mesh here, only to get the flag? */
231 *DEG_get_original(ob_ref.object));
232 const bool use_face_selection = (mesh_orig.editflag & ME_EDIT_PAINT_FACE_SEL);
233 const bool use_vert_selection = (mesh_orig.editflag & ME_EDIT_PAINT_VERT_SEL);
234 /* Texture paint mode only draws the face selection without wires or vertices as we don't
235 * draw on the geometry data directly. */
236 const bool in_texture_paint_mode = state.ctx_mode == CTX_MODE_PAINT_TEXTURE;
237
238 if ((use_face_selection || show_wires_) && !in_texture_paint_mode) {
239 gpu::Batch *geom = DRW_cache_mesh_paint_overlay_edges_get(ob_ref.object);
240 paint_region_edge_ps_->push_constant("use_select", use_face_selection);
241 paint_region_edge_ps_->draw(geom, manager.unique_handle(ob_ref));
242 }
243 if (use_face_selection) {
244 gpu::Batch *geom = DRW_cache_mesh_paint_overlay_surface_get(ob_ref.object);
245 paint_region_face_ps_->draw(geom, manager.unique_handle(ob_ref));
246 }
247 if (use_vert_selection && !in_texture_paint_mode) {
248 gpu::Batch *geom = DRW_cache_mesh_paint_overlay_verts_get(ob_ref.object);
249 paint_region_vert_ps_->draw(geom, manager.unique_handle(ob_ref));
250 }
251 }
252 }
253
254 void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
255 {
256 if (!enabled_) {
257 return;
258 }
259 GPU_framebuffer_bind(framebuffer);
260 manager.submit(weight_ps_, view);
261 manager.submit(paint_mask_ps_, view);
262 /* TODO(fclem): Draw this onto the line frame-buffer to get wide-line and anti-aliasing.
263 * Just need to make sure the shaders output line data. */
264 manager.submit(paint_region_ps_, view);
265 }
266};
267
268} // namespace blender::draw::overlay
#define BLI_assert_unreachable()
#define ELEM(...)
void hold_texture(gpu::Texture *texture)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
void draw(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &, const State &state) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
gpu::Batch * DRW_cache_mesh_paint_overlay_edges_get(Object *ob)
gpu::Batch * DRW_cache_mesh_paint_overlay_verts_get(Object *ob)
gpu::Batch * DRW_cache_mesh_surface_weights_get(Object *ob)
gpu::Batch * DRW_cache_mesh_paint_overlay_surface_get(Object *ob)
gpu::Batch * DRW_cache_mesh_surface_texpaint_single_get(Object *ob)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
@ IMAGEPAINT_PROJECT_LAYER_STENCIL_INV
@ IMAGEPAINT_PROJECT_LAYER_STENCIL
@ CTX_MODE_PAINT_VERTEX
@ CTX_MODE_PAINT_TEXTURE
@ CTX_MODE_PAINT_WEIGHT
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
T & DRW_object_get_data_for_drawing(const Object &object)
@ OB_MODE_TEXTURE_PAINT
@ V3D_OVERLAY_WPAINT_CONTOURS
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_EQUAL
Definition draw_state.hh:41
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
T * DEG_get_original(T *id)
bool BKE_scene_uses_blender_workbench(const Scene *scene)
Definition scene.cc:3082
draw::Manager * DRW_manager_get()
gpu::Texture * BKE_image_acquire_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:732
eMesh_EditFlag editflag