Vertex Paint: support vertex masking
[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 struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
42 extern struct GlobalsUboStorage ts; /* draw_common.c */
43
44 extern char datatoc_paint_vertex_vert_glsl[];
45 extern char datatoc_paint_vertex_frag_glsl[];
46 extern char datatoc_paint_wire_vert_glsl[];
47 extern char datatoc_paint_wire_frag_glsl[];
48 extern char datatoc_paint_face_vert_glsl[];
49 extern char datatoc_paint_vert_frag_glsl[];
50 extern char datatoc_common_globals_lib_glsl[];
51
52 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
53
54 /* *********** LISTS *********** */
55
56 typedef struct PAINT_VERTEX_PassList {
57         struct DRWPass *vcolor_faces;
58         struct DRWPass *wire_overlay;
59         struct DRWPass *face_overlay;
60         struct DRWPass *vert_overlay;
61 } PAINT_VERTEX_PassList;
62
63 typedef struct PAINT_VERTEX_StorageList {
64         struct PAINT_VERTEX_PrivateData *g_data;
65 } PAINT_VERTEX_StorageList;
66
67 typedef struct PAINT_VERTEX_Data {
68         void *engine_type; /* Required */
69         DRWViewportEmptyList *fbl;
70         DRWViewportEmptyList *txl;
71         PAINT_VERTEX_PassList *psl;
72         PAINT_VERTEX_StorageList *stl;
73 } PAINT_VERTEX_Data;
74
75 /* *********** STATIC *********** */
76
77 static struct {
78         struct GPUShader *vcolor_face_shader;
79         struct GPUShader *wire_overlay_shader;
80         struct GPUShader *face_overlay_shader;
81         struct GPUShader *vert_overlay_shader;
82 } e_data = {NULL}; /* Engine data */
83
84 typedef struct PAINT_VERTEX_PrivateData {
85         DRWShadingGroup *fvcolor_shgrp;
86         DRWShadingGroup *lwire_shgrp;
87         DRWShadingGroup *face_shgrp;
88         DRWShadingGroup *vert_shgrp;
89 } PAINT_VERTEX_PrivateData; /* Transient data */
90
91 /* *********** FUNCTIONS *********** */
92
93 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
94 {
95         if (!e_data.vcolor_face_shader) {
96                 e_data.vcolor_face_shader = DRW_shader_create(
97                         datatoc_paint_vertex_vert_glsl, NULL,
98                         datatoc_paint_vertex_frag_glsl, NULL);
99
100                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
101                         datatoc_paint_wire_vert_glsl, NULL,
102                         datatoc_paint_wire_frag_glsl,
103                         datatoc_common_globals_lib_glsl, "#define VERTEX_MODE\n");
104
105                 e_data.face_overlay_shader = DRW_shader_create(
106                         datatoc_paint_face_vert_glsl, NULL,
107                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
108
109                 e_data.vert_overlay_shader = DRW_shader_create_with_lib(
110                         datatoc_paint_wire_vert_glsl, NULL,
111                         datatoc_paint_vert_frag_glsl,
112                         datatoc_common_globals_lib_glsl, NULL);
113         }
114 }
115
116 static void PAINT_VERTEX_cache_init(void *vedata)
117 {
118         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
119         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
120         const DRWContextState *draw_ctx = DRW_context_state_get();
121         const View3D *v3d = draw_ctx->v3d;
122
123         if (!stl->g_data) {
124                 /* Alloc transient pointers */
125                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
126         }
127
128         {
129                 /* Create a pass */
130                 psl->vcolor_faces = DRW_pass_create(
131                         "Vert Color Pass",
132                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
133
134                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
135                 DRW_shgroup_uniform_float_copy(stl->g_data->fvcolor_shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
136         }
137
138         {
139                 psl->wire_overlay = DRW_pass_create(
140                         "Wire Pass",
141                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
142
143                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
144                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
145         }
146
147         {
148                 psl->face_overlay = DRW_pass_create(
149                         "Face Mask Pass",
150                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
151
152                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
153
154                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
155                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
156         }
157
158         {
159                 psl->vert_overlay = DRW_pass_create(
160                         "Vert Mask Pass",
161                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
162
163                 stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.vert_overlay_shader, psl->vert_overlay);
164                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", globals_ubo);
165         }
166 }
167
168 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
169 {
170         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
171         const DRWContextState *draw_ctx = DRW_context_state_get();
172         const View3D *v3d = draw_ctx->v3d;
173
174         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
175                 const Mesh *me = ob->data;
176                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
177                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
178                 const bool use_surface = v3d->overlay.vertex_paint_mode_opacity != 0.0f;
179                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
180                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
181                 struct GPUBatch *geom;
182
183                 if (me->mloopcol == NULL) {
184                         return;
185                 }
186
187                 if (use_surface) {
188                         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
189                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
190                 }
191
192                 if (use_face_sel || use_wire) {
193                         geom = DRW_cache_mesh_surface_edges_get(ob);
194                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
195                 }
196
197                 if (use_face_sel) {
198                         geom = DRW_cache_mesh_surface_get(ob);
199                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
200                 }
201
202                 if (use_vert_sel) {
203                         geom = DRW_cache_mesh_all_verts_get(ob);
204                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
205                 }
206         }
207 }
208
209 static void PAINT_VERTEX_draw_scene(void *vedata)
210 {
211         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
212
213         DRW_draw_pass(psl->vcolor_faces);
214         DRW_draw_pass(psl->face_overlay);
215         DRW_draw_pass(psl->wire_overlay);
216         DRW_draw_pass(psl->vert_overlay);
217 }
218
219 static void PAINT_VERTEX_engine_free(void)
220 {
221         DRW_SHADER_FREE_SAFE(e_data.vcolor_face_shader);
222         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
223         DRW_SHADER_FREE_SAFE(e_data.vert_overlay_shader);
224         DRW_SHADER_FREE_SAFE(e_data.face_overlay_shader);
225 }
226
227 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
228
229 DrawEngineType draw_engine_paint_vertex_type = {
230         NULL, NULL,
231         N_("PaintVertexMode"),
232         &PAINT_VERTEX_data_size,
233         &PAINT_VERTEX_engine_init,
234         &PAINT_VERTEX_engine_free,
235         &PAINT_VERTEX_cache_init,
236         &PAINT_VERTEX_cache_populate,
237         NULL,
238         NULL,
239         &PAINT_VERTEX_draw_scene,
240         NULL,
241         NULL,
242 };