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