Blender  V2.93
overlay_paint.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2019, Blender Foundation.
17  */
18 
23 #include "DRW_render.h"
24 
25 #include "BKE_image.h"
26 
27 #include "DNA_mesh_types.h"
28 
29 #include "DEG_depsgraph_query.h"
30 
31 #include "overlay_private.h"
32 
33 /* Check if the given object is rendered (partially) transparent */
35 {
36  if (v3d->shading.type == OB_WIRE) {
37  return true;
38  }
39  if (v3d->shading.type == OB_SOLID) {
40  if (v3d->shading.flag & V3D_SHADING_XRAY) {
41  return true;
42  }
43 
44  if (ob && v3d->shading.color_type == V3D_SHADING_OBJECT_COLOR) {
45  return ob->color[3] < 1.0f;
46  }
47  if (ob && ob->type == OB_MESH && ob->data &&
49  Mesh *me = ob->data;
50  for (int i = 0; i < me->totcol; i++) {
51  Material *mat = me->mat[i];
52  if (mat && mat->a < 1.0f) {
53  return true;
54  }
55  }
56  }
57  }
58  return false;
59 }
60 
62 {
63  OVERLAY_StorageList *stl = vedata->stl;
64  OVERLAY_PrivateData *pd = stl->pd;
65  const DRWContextState *draw_ctx = DRW_context_state_get();
66 
67  pd->painting.in_front = pd->use_in_front && draw_ctx->obact &&
68  (draw_ctx->obact->dtx & OB_DRAW_IN_FRONT);
70  draw_ctx->obact);
71 }
72 
74 {
75  const DRWContextState *draw_ctx = DRW_context_state_get();
76  OVERLAY_PassList *psl = vedata->psl;
77  OVERLAY_PrivateData *pd = vedata->stl->pd;
78  struct GPUShader *sh;
79  DRWShadingGroup *grp;
81 
82  const bool is_edit_mode = (pd->ctx_mode == CTX_MODE_EDIT_MESH);
83  const bool draw_contours = !is_edit_mode &&
85  float opacity = 0.0f;
86  pd->paint_depth_grp = NULL;
87  psl->paint_depth_ps = NULL;
88 
89  switch (pd->ctx_mode) {
90  case CTX_MODE_POSE:
91  case CTX_MODE_EDIT_MESH:
92  case CTX_MODE_PAINT_WEIGHT: {
93  opacity = is_edit_mode ? 1.0 : pd->overlay.weight_paint_mode_opacity;
94  if (opacity > 0.0f) {
98 
100  pd->paint_surf_grp = grp = DRW_shgroup_create(sh, psl->paint_color_ps);
101  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
102  DRW_shgroup_uniform_bool_copy(grp, "drawContours", draw_contours);
103  DRW_shgroup_uniform_bool_copy(grp, "useAlphaBlend", pd->painting.alpha_blending);
104  DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
106 
107  if (pd->painting.alpha_blending) {
112  }
113  }
114  break;
115  }
116  case CTX_MODE_PAINT_VERTEX: {
117  opacity = pd->overlay.vertex_paint_mode_opacity;
118  if (opacity > 0.0f) {
122 
124  pd->paint_surf_grp = grp = DRW_shgroup_create(sh, psl->paint_color_ps);
125  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
126  DRW_shgroup_uniform_bool_copy(grp, "useAlphaBlend", pd->painting.alpha_blending);
127  DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
128  }
129  break;
130  }
131  case CTX_MODE_PAINT_TEXTURE: {
132  const ImagePaintSettings *imapaint = &draw_ctx->scene->toolsettings->imapaint;
133  const bool mask_enabled = imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL &&
134  imapaint->stencil != NULL;
135 
136  opacity = mask_enabled ? pd->overlay.texture_paint_mode_opacity : 0.0f;
137  if (opacity > 0.0f) {
140 
142 
143  const bool mask_premult = (imapaint->stencil->alpha_mode == IMA_ALPHA_PREMUL);
144  const bool mask_inverted = (imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV) != 0;
146  pd->paint_surf_grp = grp = DRW_shgroup_create(sh, psl->paint_color_ps);
147  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
148  DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
149  DRW_shgroup_uniform_bool_copy(grp, "maskPremult", mask_premult);
150  DRW_shgroup_uniform_vec3_copy(grp, "maskColor", imapaint->stencil_col);
151  DRW_shgroup_uniform_bool_copy(grp, "maskInvertStencil", mask_inverted);
152  DRW_shgroup_uniform_texture(grp, "maskImage", tex);
153  }
154  break;
155  }
156  default:
157  BLI_assert(0);
158  break;
159  }
160 
161  if (opacity <= 0.0f) {
162  psl->paint_color_ps = NULL;
163  pd->paint_surf_grp = NULL;
164  }
165 
166  {
170  pd->paint_face_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
171  DRW_shgroup_uniform_vec4_copy(grp, "color", (float[4]){1.0f, 1.0f, 1.0f, 0.2f});
173 
176  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
177  DRW_shgroup_uniform_bool_copy(grp, "useSelect", true);
179 
180  pd->paint_wire_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
181  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
182  DRW_shgroup_uniform_bool_copy(grp, "useSelect", false);
184 
187  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
188  }
189 }
190 
192 {
193  OVERLAY_PrivateData *pd = vedata->stl->pd;
194  struct GPUBatch *geom = NULL;
195 
196  const Mesh *me_orig = DEG_get_original_object(ob)->data;
197  const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
198 
199  if (pd->paint_surf_grp) {
201  DRW_shgroup_call(pd->paint_surf_grp, geom, ob);
202  }
203 
204  if (use_face_sel) {
205  geom = DRW_cache_mesh_surface_get(ob);
206  DRW_shgroup_call(pd->paint_face_grp, geom, ob);
207  }
208 }
209 
211 {
212  OVERLAY_PrivateData *pd = vedata->stl->pd;
213  struct GPUBatch *geom = NULL;
214 
215  const Mesh *me_orig = DEG_get_original_object(ob)->data;
216  const bool is_edit_mode = (pd->ctx_mode == CTX_MODE_EDIT_MESH);
217  const bool use_wire = !is_edit_mode && (pd->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE);
218  const bool use_face_sel = !is_edit_mode && (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL);
219  const bool use_vert_sel = !is_edit_mode && (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL);
220 
222  if (pd->paint_surf_grp) {
224  DRW_shgroup_call(pd->paint_surf_grp, geom, ob);
225  }
226  if (pd->paint_depth_grp) {
228  DRW_shgroup_call(pd->paint_depth_grp, geom, ob);
229  }
230  }
231 
232  if (use_face_sel || use_wire) {
234  DRW_shgroup_call(use_face_sel ? pd->paint_wire_selected_grp : pd->paint_wire_grp, geom, ob);
235  }
236 
237  if (use_face_sel) {
238  geom = DRW_cache_mesh_surface_get(ob);
239  DRW_shgroup_call(pd->paint_face_grp, geom, ob);
240  }
241 
242  if (use_vert_sel) {
243  geom = DRW_cache_mesh_all_verts_get(ob);
244  DRW_shgroup_call(pd->paint_point_grp, geom, ob);
245  }
246 }
247 
249 {
251 }
252 
254 {
255  OVERLAY_StorageList *stl = vedata->stl;
256  OVERLAY_PrivateData *pd = stl->pd;
257 
258  OVERLAY_PassList *psl = vedata->psl;
259  OVERLAY_FramebufferList *fbl = vedata->fbl;
261 
262  if (DRW_state_is_fbo()) {
263  if (pd->painting.alpha_blending) {
265  fbl->overlay_default_fb);
266  }
267  else {
268  /* Paint overlay needs final color because of multiply blend mode. */
270  }
271  }
272 
273  if (psl->paint_depth_ps) {
275  }
276  if (psl->paint_color_ps) {
278  }
279  if (psl->paint_overlay_ps) {
281  }
282 }
@ CTX_MODE_PAINT_TEXTURE
Definition: BKE_context.h:126
@ CTX_MODE_EDIT_MESH
Definition: BKE_context.h:115
@ CTX_MODE_PAINT_VERTEX
Definition: BKE_context.h:125
@ CTX_MODE_PAINT_WEIGHT
Definition: BKE_context.h:124
@ CTX_MODE_POSE
Definition: BKE_context.h:122
struct GPUTexture * BKE_image_get_gpu_texture(struct Image *image, struct ImageUser *iuser, struct ImBuf *ibuf)
Definition: image_gpu.c:439
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define ELEM(...)
struct Object * DEG_get_original_object(struct Object *object)
@ IMA_ALPHA_PREMUL
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ OB_WIRE
@ OB_SOLID
@ OB_MODE_EDIT
@ OB_MODE_WEIGHT_PAINT
@ OB_DRAW_IN_FRONT
@ OB_MESH
#define IMAGEPAINT_PROJECT_LAYER_STENCIL
#define IMAGEPAINT_PROJECT_LAYER_STENCIL_INV
@ V3D_SHADING_MATERIAL_COLOR
@ V3D_SHADING_OBJECT_COLOR
@ V3D_OVERLAY_WPAINT_CONTOURS
@ V3D_OVERLAY_PAINT_WIRE
@ V3D_SHADING_XRAY
DRWState
Definition: DRW_render.h:312
@ DRW_STATE_BLEND_ALPHA
Definition: DRW_render.h:340
@ DRW_STATE_DEPTH_EQUAL
Definition: DRW_render.h:324
@ DRW_STATE_WRITE_DEPTH
Definition: DRW_render.h:314
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:315
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition: DRW_render.h:323
@ DRW_STATE_BLEND_MUL
Definition: DRW_render.h:345
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:593
#define DRW_shgroup_call(shgroup, geom, ob)
Definition: DRW_render.h:420
GPUBatch
Definition: GPU_batch.h:93
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
struct GPUShader GPUShader
Definition: GPU_shader.h:33
struct GPUTexture GPUTexture
Definition: GPU_texture.h:33
char alpha_mode
GPUBatch * DRW_cache_mesh_all_verts_get(Object *ob)
Definition: draw_cache.c:2835
GPUBatch * DRW_cache_mesh_surface_texpaint_single_get(Object *ob)
Definition: draw_cache.c:2887
GPUBatch * DRW_cache_mesh_surface_weights_get(Object *ob)
Definition: draw_cache.c:2905
GPUBatch * DRW_cache_mesh_surface_get(Object *ob)
Definition: draw_cache.c:2859
GPUBatch * DRW_cache_mesh_surface_edges_get(Object *ob)
Definition: draw_cache.c:2865
struct DRW_Global G_draw
Definition: draw_common.c:45
DefaultFramebufferList * DRW_viewport_framebuffer_list_get(void)
Definition: draw_manager.c:697
bool DRW_state_is_fbo(void)
const DRWContextState * DRW_context_state_get(void)
void DRW_shgroup_uniform_float_copy(DRWShadingGroup *shgroup, const char *name, const float value)
void DRW_shgroup_uniform_block(DRWShadingGroup *shgroup, const char *name, const GPUUniformBuf *ubo)
void DRW_shgroup_uniform_texture(DRWShadingGroup *shgroup, const char *name, const GPUTexture *tex)
void DRW_shgroup_state_enable(DRWShadingGroup *shgroup, DRWState state)
void DRW_shgroup_uniform_vec3_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
void DRW_shgroup_uniform_vec4_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
void DRW_shgroup_uniform_bool_copy(DRWShadingGroup *shgroup, const char *name, const bool value)
void DRW_draw_pass(DRWPass *pass)
static ulong state[N]
void OVERLAY_paint_texture_cache_populate(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_paint_init(OVERLAY_Data *vedata)
Definition: overlay_paint.c:61
static bool paint_object_is_rendered_transparent(View3D *v3d, Object *ob)
Definition: overlay_paint.c:34
void OVERLAY_paint_cache_init(OVERLAY_Data *vedata)
Definition: overlay_paint.c:73
void OVERLAY_paint_draw(OVERLAY_Data *vedata)
void OVERLAY_paint_weight_cache_populate(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_paint_vertex_cache_populate(OVERLAY_Data *vedata, Object *ob)
GPUShader * OVERLAY_shader_paint_face(void)
GPUShader * OVERLAY_shader_paint_point(void)
GPUShader * OVERLAY_shader_paint_weight(void)
GPUShader * OVERLAY_shader_depth_only(void)
GPUShader * OVERLAY_shader_paint_wire(void)
GPUShader * OVERLAY_shader_paint_vertcol(void)
GPUShader * OVERLAY_shader_paint_texture(void)
struct Object * obact
Definition: DRW_render.h:749
struct Scene * scene
Definition: DRW_render.h:745
struct View3D * v3d
Definition: DRW_render.h:742
struct GPUUniformBuf * block_ubo
Definition: draw_common.h:210
struct GPUTexture * weight_ramp
Definition: draw_common.h:213
struct GPUFrameBuffer * in_front_fb
struct GPUFrameBuffer * default_fb
struct Image * stencil
struct Material ** mat
char editflag
short totcol
OVERLAY_PassList * psl
OVERLAY_StorageList * stl
OVERLAY_FramebufferList * fbl
struct GPUFrameBuffer * overlay_in_front_fb
struct GPUFrameBuffer * overlay_default_fb
DRWPass * paint_overlay_ps
DRWPass * paint_depth_ps
DRWPass * paint_color_ps
DRWShadingGroup * paint_surf_grp
View3DOverlay overlay
DRWShadingGroup * paint_depth_grp
DRWShadingGroup * paint_wire_selected_grp
DRWShadingGroup * paint_face_grp
DRWShadingGroup * paint_point_grp
enum eContextObjectMode ctx_mode
struct OVERLAY_PrivateData::@246 painting
DRWShadingGroup * paint_wire_grp
struct OVERLAY_PrivateData * pd
float color[4]
void * data
struct ToolSettings * toolsettings
struct ImagePaintSettings imapaint
float texture_paint_mode_opacity
float vertex_paint_mode_opacity
float weight_paint_mode_opacity
View3DShading shading