3D View: Support vert & weight paint mask clipping
[blender.git] / source / blender / draw / modes / paint_vertex_mode.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/modes/paint_vertex_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 /* If builtin shaders are needed */
30 #include "GPU_shader.h"
31
32 #include "draw_common.h"
33
34 #include "draw_mode_engines.h"
35
36 #include "DNA_mesh_types.h"
37 #include "DNA_view3d_types.h"
38
39 #include "DEG_depsgraph_query.h"
40
41 extern char datatoc_common_world_clip_lib_glsl[];
42
43 extern char datatoc_paint_vertex_vert_glsl[];
44 extern char datatoc_paint_vertex_frag_glsl[];
45 extern char datatoc_paint_wire_vert_glsl[];
46 extern char datatoc_paint_wire_frag_glsl[];
47 extern char datatoc_paint_face_vert_glsl[];
48 extern char datatoc_paint_vert_frag_glsl[];
49 extern char datatoc_common_globals_lib_glsl[];
50
51 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
52
53 /* *********** LISTS *********** */
54
55 typedef struct PAINT_VERTEX_PassList {
56         struct DRWPass *vcolor_faces;
57         struct DRWPass *wire_overlay;
58         struct DRWPass *face_overlay;
59         struct DRWPass *vert_overlay;
60 } PAINT_VERTEX_PassList;
61
62 typedef struct PAINT_VERTEX_StorageList {
63         struct PAINT_VERTEX_PrivateData *g_data;
64 } PAINT_VERTEX_StorageList;
65
66 typedef struct PAINT_VERTEX_Data {
67         void *engine_type; /* Required */
68         DRWViewportEmptyList *fbl;
69         DRWViewportEmptyList *txl;
70         PAINT_VERTEX_PassList *psl;
71         PAINT_VERTEX_StorageList *stl;
72 } PAINT_VERTEX_Data;
73
74 typedef struct PAINT_VERTEX_Shaders {
75         struct GPUShader *vcolor_face;
76         struct GPUShader *wire_overlay;
77         struct GPUShader *face_overlay;
78         struct GPUShader *vert_overlay;
79 } PAINT_VERTEX_Shaders;
80
81 /* *********** STATIC *********** */
82
83 static struct {
84         PAINT_VERTEX_Shaders sh_data[2];
85 } e_data = {NULL}; /* Engine data */
86
87 typedef struct PAINT_VERTEX_PrivateData {
88         DRWShadingGroup *fvcolor_shgrp;
89         DRWShadingGroup *lwire_shgrp;
90         DRWShadingGroup *face_shgrp;
91         DRWShadingGroup *vert_shgrp;
92 } PAINT_VERTEX_PrivateData; /* Transient data */
93
94 /* *********** FUNCTIONS *********** */
95
96 static int PAINT_VERTEX_sh_data_index_from_rv3d(const RegionView3D *rv3d)
97 {
98         if (rv3d->rflag & RV3D_CLIPPING) {
99                 return 1;
100         }
101         return 0;
102 }
103
104 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
105 {
106         const DRWContextState *draw_ctx = DRW_context_state_get();
107         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[PAINT_VERTEX_sh_data_index_from_rv3d(draw_ctx->rv3d)];
108         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
109
110         if (is_clip) {
111                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
112         }
113
114         if (!sh_data->vcolor_face) {
115                 const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
116                 const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
117
118                 sh_data->vcolor_face = DRW_shader_create_from_arrays({
119                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_vertex_vert_glsl, NULL},
120                         .frag = (const char *[]){datatoc_paint_vertex_frag_glsl, NULL},
121                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
122
123                 sh_data->wire_overlay = DRW_shader_create_from_arrays({
124                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
125                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
126                         .defs = (const char *[]){world_clip_def_or_empty, "#define VERTEX_MODE\n", NULL}});
127
128                 sh_data->face_overlay = DRW_shader_create_from_arrays({
129                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_face_vert_glsl, NULL},
130                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
131                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
132
133                 sh_data->vert_overlay = DRW_shader_create_from_arrays({
134                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
135                         .frag = (const char *[]){datatoc_paint_vert_frag_glsl, NULL},
136                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
137         }
138 }
139
140 static void PAINT_VERTEX_cache_init(void *vedata)
141 {
142         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
143         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
144         const DRWContextState *draw_ctx = DRW_context_state_get();
145         const View3D *v3d = draw_ctx->v3d;
146         const RegionView3D *rv3d = draw_ctx->rv3d;
147         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[PAINT_VERTEX_sh_data_index_from_rv3d(rv3d)];
148
149         if (!stl->g_data) {
150                 /* Alloc transient pointers */
151                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
152         }
153
154         {
155                 /* Create a pass */
156                 psl->vcolor_faces = DRW_pass_create(
157                         "Vert Color Pass",
158                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
159
160                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(sh_data->vcolor_face, psl->vcolor_faces);
161                 DRW_shgroup_uniform_float_copy(stl->g_data->fvcolor_shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
162                 if (rv3d->rflag & RV3D_CLIPPING) {
163                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fvcolor_shgrp, rv3d);
164                 }
165         }
166
167         {
168                 psl->wire_overlay = DRW_pass_create(
169                         "Wire Pass",
170                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
171
172                 stl->g_data->lwire_shgrp = DRW_shgroup_create(sh_data->wire_overlay, psl->wire_overlay);
173                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", G_draw.block_ubo);
174                 if (rv3d->rflag & RV3D_CLIPPING) {
175                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_shgrp, rv3d);
176                 }
177         }
178
179         {
180                 psl->face_overlay = DRW_pass_create(
181                         "Face Mask Pass",
182                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
183
184                 stl->g_data->face_shgrp = DRW_shgroup_create(sh_data->face_overlay, psl->face_overlay);
185                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
186                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
187                 if (rv3d->rflag & RV3D_CLIPPING) {
188                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->face_shgrp, rv3d);
189                 }
190         }
191
192         {
193                 psl->vert_overlay = DRW_pass_create(
194                         "Vert Mask Pass",
195                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
196
197                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->vert_overlay, psl->vert_overlay);
198                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
199                 if (rv3d->rflag & RV3D_CLIPPING) {
200                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
201                 }
202         }
203 }
204
205 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
206 {
207         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
208         const DRWContextState *draw_ctx = DRW_context_state_get();
209         const View3D *v3d = draw_ctx->v3d;
210
211         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
212                 const Mesh *me = ob->data;
213                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
214                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
215                 const bool use_surface = v3d->overlay.vertex_paint_mode_opacity != 0.0f;
216                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
217                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
218                 struct GPUBatch *geom;
219
220                 if (me->mloopcol == NULL) {
221                         return;
222                 }
223
224                 if (use_surface) {
225                         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
226                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
227                 }
228
229                 if (use_face_sel || use_wire) {
230                         geom = DRW_cache_mesh_surface_edges_get(ob);
231                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
232                 }
233
234                 if (use_face_sel) {
235                         geom = DRW_cache_mesh_surface_get(ob);
236                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
237                 }
238
239                 if (use_vert_sel) {
240                         geom = DRW_cache_mesh_all_verts_get(ob);
241                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
242                 }
243         }
244 }
245
246 static void PAINT_VERTEX_draw_scene(void *vedata)
247 {
248         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
249
250         DRW_draw_pass(psl->vcolor_faces);
251         DRW_draw_pass(psl->face_overlay);
252         DRW_draw_pass(psl->wire_overlay);
253         DRW_draw_pass(psl->vert_overlay);
254 }
255
256 static void PAINT_VERTEX_engine_free(void)
257 {
258         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
259                 PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[sh_data_index];
260                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
261                 for (int i = 0; i < (sizeof(PAINT_VERTEX_Shaders) / sizeof(GPUShader *)); i++) {
262                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
263                 }
264         }
265 }
266
267 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
268
269 DrawEngineType draw_engine_paint_vertex_type = {
270         NULL, NULL,
271         N_("PaintVertexMode"),
272         &PAINT_VERTEX_data_size,
273         &PAINT_VERTEX_engine_init,
274         &PAINT_VERTEX_engine_free,
275         &PAINT_VERTEX_cache_init,
276         &PAINT_VERTEX_cache_populate,
277         NULL,
278         NULL,
279         &PAINT_VERTEX_draw_scene,
280         NULL,
281         NULL,
282 };