Fix vertex/weight paint showing edge selection
[blender.git] / source / blender / draw / modes / paint_weight_mode.c
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 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw
20  */
21
22 #include "DRW_render.h"
23
24 /* If builtin shaders are needed */
25 #include "GPU_shader.h"
26
27 #include "draw_common.h"
28
29 #include "DNA_mesh_types.h"
30 #include "DNA_view3d_types.h"
31
32 #include "DEG_depsgraph_query.h"
33
34 extern char datatoc_paint_face_vert_glsl[];
35 extern char datatoc_paint_weight_vert_glsl[];
36 extern char datatoc_paint_weight_frag_glsl[];
37 extern char datatoc_paint_wire_vert_glsl[];
38 extern char datatoc_paint_wire_frag_glsl[];
39 extern char datatoc_paint_vert_frag_glsl[];
40 extern char datatoc_common_globals_lib_glsl[];
41
42 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
43
44 /* *********** LISTS *********** */
45
46 typedef struct PAINT_WEIGHT_PassList {
47         struct DRWPass *weight_faces;
48         struct DRWPass *wire_overlay;
49         struct DRWPass *wire_select_overlay;
50         struct DRWPass *face_select_overlay;
51         struct DRWPass *vert_select_overlay;
52 } PAINT_WEIGHT_PassList;
53
54 typedef struct PAINT_WEIGHT_StorageList {
55         struct PAINT_WEIGHT_PrivateData *g_data;
56 } PAINT_WEIGHT_StorageList;
57
58 typedef struct PAINT_WEIGHT_Data {
59         void *engine_type;
60         DRWViewportEmptyList *fbl;
61         DRWViewportEmptyList *txl;
62         PAINT_WEIGHT_PassList *psl;
63         PAINT_WEIGHT_StorageList *stl;
64 } PAINT_WEIGHT_Data;
65
66 typedef struct PAINT_WEIGHT_Shaders {
67         struct GPUShader *weight_face;
68         struct GPUShader *wire_overlay;
69         struct GPUShader *wire_select_overlay;
70         struct GPUShader *face_select_overlay;
71         struct GPUShader *vert_select_overlay;
72 } PAINT_WEIGHT_Shaders;
73
74 /* *********** STATIC *********** */
75
76 static struct {
77         PAINT_WEIGHT_Shaders sh_data[GPU_SHADER_CFG_LEN];
78
79         int actdef;
80 } e_data = {NULL}; /* Engine data */
81
82 typedef struct PAINT_WEIGHT_PrivateData {
83         DRWShadingGroup *fweights_shgrp;
84         DRWShadingGroup *lwire_shgrp;
85         DRWShadingGroup *lwire_select_shgrp;
86         DRWShadingGroup *face_shgrp;
87         DRWShadingGroup *vert_select_shgrp;
88 } PAINT_WEIGHT_PrivateData;
89
90 /* *********** FUNCTIONS *********** */
91
92 static void PAINT_WEIGHT_engine_init(void *UNUSED(vedata))
93 {
94         const DRWContextState *draw_ctx = DRW_context_state_get();
95         PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
96         const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
97
98         if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
99                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
100         }
101
102         if (!sh_data->weight_face) {
103                 sh_data->weight_face = GPU_shader_create_from_arrays({
104                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_weight_vert_glsl, NULL},
105                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_weight_frag_glsl, NULL},
106                         .defs = (const char *[]){sh_cfg_data->def, NULL},
107                 });
108
109                 sh_data->wire_overlay = GPU_shader_create_from_arrays({
110                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
111                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
112                         .defs = (const char *[]){sh_cfg_data->def, "#define WEIGHT_MODE\n", NULL},
113                 });
114                 sh_data->wire_select_overlay = GPU_shader_create_from_arrays({
115                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
116                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
117                         .defs = (const char *[]){sh_cfg_data->def, "#define WEIGHT_MODE\n" "#define USE_SELECT\n", NULL},
118                 });
119                 sh_data->face_select_overlay = GPU_shader_create_from_arrays({
120                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_paint_face_vert_glsl, NULL},
121                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
122                         .defs = (const char *[]){sh_cfg_data->def, NULL},
123                 });
124
125                 sh_data->vert_select_overlay = GPU_shader_create_from_arrays({
126                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
127                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_vert_frag_glsl, NULL},
128                         .defs = (const char *[]){sh_cfg_data->def, "#define USE_SELECT\n", NULL},
129                 });
130         }
131 }
132
133 static void PAINT_WEIGHT_cache_init(void *vedata)
134 {
135         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
136         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
137         const DRWContextState *draw_ctx = DRW_context_state_get();
138         const View3D *v3d = draw_ctx->v3d;
139         RegionView3D *rv3d = draw_ctx->rv3d;
140         PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
141
142         if (!stl->g_data) {
143                 /* Alloc transient pointers */
144                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
145         }
146
147         {
148                 /* Create a pass */
149                 psl->weight_faces = DRW_pass_create(
150                         "Weight Pass",
151                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
152
153                 stl->g_data->fweights_shgrp = DRW_shgroup_create(sh_data->weight_face, psl->weight_faces);
154
155                 DRW_shgroup_uniform_bool_copy(stl->g_data->fweights_shgrp, "drawContours", (v3d->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0);
156
157                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &v3d->overlay.weight_paint_mode_opacity, 1);
158                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", G_draw.weight_ramp);
159                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
160                 if (rv3d->rflag & RV3D_CLIPPING) {
161                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
162                 }
163         }
164
165         {
166                 psl->wire_overlay = DRW_pass_create(
167                         "Wire Pass",
168                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
169
170                 stl->g_data->lwire_shgrp = DRW_shgroup_create(sh_data->wire_overlay, psl->wire_overlay);
171                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", G_draw.block_ubo);
172                 if (rv3d->rflag & RV3D_CLIPPING) {
173                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_shgrp, rv3d);
174                 }
175         }
176
177         {
178                 psl->wire_select_overlay = DRW_pass_create(
179                         "Wire Mask Pass",
180                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
181
182                 stl->g_data->lwire_select_shgrp = DRW_shgroup_create(sh_data->wire_select_overlay, psl->wire_overlay);
183                 DRW_shgroup_uniform_block(stl->g_data->lwire_select_shgrp, "globalsBlock", G_draw.block_ubo);
184                 if (rv3d->rflag & RV3D_CLIPPING) {
185                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_select_shgrp, rv3d);
186                 }
187         }
188
189         {
190                 psl->face_select_overlay = DRW_pass_create(
191                         "Face Mask Pass",
192                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
193
194                 stl->g_data->face_shgrp = DRW_shgroup_create(sh_data->face_select_overlay, psl->face_select_overlay);
195
196                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
197                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
198                 if (rv3d->rflag & RV3D_CLIPPING) {
199                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->face_shgrp, rv3d);
200                 }
201         }
202
203         {
204                 psl->vert_select_overlay = DRW_pass_create(
205                         "Vert Mask Pass",
206                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
207
208                 stl->g_data->vert_select_shgrp = DRW_shgroup_create(sh_data->vert_select_overlay, psl->vert_select_overlay);
209                 DRW_shgroup_uniform_block(stl->g_data->vert_select_shgrp, "globalsBlock", G_draw.block_ubo);
210                 if (rv3d->rflag & RV3D_CLIPPING) {
211                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_select_shgrp, rv3d);
212                 }
213         }
214 }
215
216 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
217 {
218         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
219         const DRWContextState *draw_ctx = DRW_context_state_get();
220         const View3D *v3d = draw_ctx->v3d;
221
222         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
223                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
224                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
225                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
226                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
227                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
228                 struct GPUBatch *geom;
229
230                 if (use_surface) {
231                         geom = DRW_cache_mesh_surface_weights_get(ob);
232                         DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
233                 }
234
235                 if (use_face_sel || use_wire) {
236                         DRWShadingGroup *shgrp = use_face_sel ? stl->g_data->lwire_select_shgrp : stl->g_data->lwire_shgrp;
237                         geom = DRW_cache_mesh_surface_edges_get(ob);
238                         DRW_shgroup_call_add(shgrp, geom, ob->obmat);
239                 }
240
241                 if (use_face_sel) {
242                         geom = DRW_cache_mesh_surface_get(ob);
243                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
244                 }
245
246                 if (use_vert_sel) {
247                         geom = DRW_cache_mesh_all_verts_get(ob);
248                         DRW_shgroup_call_add(stl->g_data->vert_select_shgrp, geom, ob->obmat);
249                 }
250         }
251 }
252
253 static void PAINT_WEIGHT_draw_scene(void *vedata)
254 {
255         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
256
257         DRW_draw_pass(psl->weight_faces);
258         DRW_draw_pass(psl->wire_overlay);
259         DRW_draw_pass(psl->wire_select_overlay);
260         DRW_draw_pass(psl->vert_select_overlay);
261         DRW_draw_pass(psl->face_select_overlay);
262 }
263
264 static void PAINT_WEIGHT_engine_free(void)
265 {
266         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
267                 PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[sh_data_index];
268                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
269                 for (int i = 0; i < (sizeof(PAINT_WEIGHT_Shaders) / sizeof(GPUShader *)); i++) {
270                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
271                 }
272         }
273 }
274
275 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
276
277 DrawEngineType draw_engine_paint_weight_type = {
278         NULL, NULL,
279         N_("PaintWeightMode"),
280         &PAINT_WEIGHT_data_size,
281         &PAINT_WEIGHT_engine_init,
282         &PAINT_WEIGHT_engine_free,
283         &PAINT_WEIGHT_cache_init,
284         &PAINT_WEIGHT_cache_populate,
285         NULL,
286         NULL,
287         &PAINT_WEIGHT_draw_scene,
288         NULL,
289         NULL,
290 };