DRW: use light wire for weight paint mode
[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                 DRWPass *pass = DRW_pass_create(
150                         "Weight Pass",
151                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
152                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->weight_face, pass);
153                 DRW_shgroup_uniform_bool_copy(shgrp, "drawContours", (v3d->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0);
154                 DRW_shgroup_uniform_float(shgrp, "opacity", &v3d->overlay.weight_paint_mode_opacity, 1);
155                 DRW_shgroup_uniform_texture(shgrp, "colorramp", G_draw.weight_ramp);
156                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
157                 if (rv3d->rflag & RV3D_CLIPPING) {
158                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
159                 }
160                 psl->weight_faces = pass;
161                 stl->g_data->fweights_shgrp = shgrp;
162         }
163
164         {
165                 DRWPass *pass = DRW_pass_create(
166                         "Wire Pass",
167                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
168                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->wire_overlay, pass);
169                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
170                 if (rv3d->rflag & RV3D_CLIPPING) {
171                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
172                 }
173                 psl->wire_overlay = pass;
174                 stl->g_data->lwire_shgrp = shgrp;
175         }
176
177         {
178                 DRWPass *pass = 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                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->wire_select_overlay, pass);
182                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
183                 if (rv3d->rflag & RV3D_CLIPPING) {
184                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
185                 }
186                 psl->wire_select_overlay = pass;
187                 stl->g_data->lwire_select_shgrp = shgrp;
188         }
189
190         {
191                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
192                 DRWPass *pass = DRW_pass_create(
193                         "Face Mask Pass",
194                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
195                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->face_select_overlay, pass);
196                 DRW_shgroup_uniform_vec4(shgrp, "color", col, 1);
197                 if (rv3d->rflag & RV3D_CLIPPING) {
198                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
199                 }
200                 psl->face_select_overlay = pass;
201                 stl->g_data->face_shgrp = shgrp;
202         }
203
204         {
205                 DRWPass *pass = DRW_pass_create(
206                         "Vert Mask Pass",
207                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
208                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->vert_select_overlay, pass);
209                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
210                 if (rv3d->rflag & RV3D_CLIPPING) {
211                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
212                 }
213                 psl->vert_select_overlay = pass;
214                 stl->g_data->vert_select_shgrp = shgrp;
215         }
216 }
217
218 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
219 {
220         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
221         const DRWContextState *draw_ctx = DRW_context_state_get();
222         const View3D *v3d = draw_ctx->v3d;
223
224         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
225                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
226                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
227                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
228                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
229                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
230                 struct GPUBatch *geom;
231
232                 if (use_surface) {
233                         geom = DRW_cache_mesh_surface_weights_get(ob);
234                         DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
235                 }
236
237                 if (use_face_sel || use_wire) {
238                         DRWShadingGroup *shgrp = use_face_sel ? stl->g_data->lwire_select_shgrp : stl->g_data->lwire_shgrp;
239                         geom = DRW_cache_mesh_surface_edges_get(ob);
240                         DRW_shgroup_call_add(shgrp, geom, ob->obmat);
241                 }
242
243                 if (use_face_sel) {
244                         geom = DRW_cache_mesh_surface_get(ob);
245                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
246                 }
247
248                 if (use_vert_sel) {
249                         geom = DRW_cache_mesh_all_verts_get(ob);
250                         DRW_shgroup_call_add(stl->g_data->vert_select_shgrp, geom, ob->obmat);
251                 }
252         }
253 }
254
255 static void PAINT_WEIGHT_draw_scene(void *vedata)
256 {
257         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
258
259         DRW_draw_pass(psl->weight_faces);
260         DRW_draw_pass(psl->wire_overlay);
261         DRW_draw_pass(psl->wire_select_overlay);
262         DRW_draw_pass(psl->vert_select_overlay);
263         DRW_draw_pass(psl->face_select_overlay);
264 }
265
266 static void PAINT_WEIGHT_engine_free(void)
267 {
268         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
269                 PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[sh_data_index];
270                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
271                 for (int i = 0; i < (sizeof(PAINT_WEIGHT_Shaders) / sizeof(GPUShader *)); i++) {
272                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
273                 }
274         }
275 }
276
277 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
278
279 DrawEngineType draw_engine_paint_weight_type = {
280         NULL, NULL,
281         N_("PaintWeightMode"),
282         &PAINT_WEIGHT_data_size,
283         &PAINT_WEIGHT_engine_init,
284         &PAINT_WEIGHT_engine_free,
285         &PAINT_WEIGHT_cache_init,
286         &PAINT_WEIGHT_cache_populate,
287         NULL,
288         NULL,
289         &PAINT_WEIGHT_draw_scene,
290         NULL,
291         NULL,
292 };