Fix assert in some cases when using DataTransfer modifier for custom normals.
[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
20  * \ingroup draw
21  */
22
23 #include "DRW_render.h"
24
25 /* If builtin shaders are needed */
26 #include "GPU_shader.h"
27
28 #include "draw_common.h"
29 #include "draw_mode_engines.h"
30
31 #include "DNA_mesh_types.h"
32 #include "DNA_view3d_types.h"
33
34 #include "DEG_depsgraph_query.h"
35
36 extern char datatoc_paint_face_selection_vert_glsl[];
37 extern char datatoc_paint_weight_vert_glsl[];
38 extern char datatoc_paint_weight_frag_glsl[];
39 extern char datatoc_paint_vertex_vert_glsl[];
40 extern char datatoc_paint_vertex_frag_glsl[];
41 extern char datatoc_paint_wire_vert_glsl[];
42 extern char datatoc_paint_wire_frag_glsl[];
43 extern char datatoc_paint_vert_frag_glsl[];
44 extern char datatoc_common_globals_lib_glsl[];
45 extern char datatoc_common_view_lib_glsl[];
46
47 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
48
49 /* *********** LISTS *********** */
50
51 enum {
52   VERTEX_MODE = 0,
53   WEIGHT_MODE = 1,
54 };
55 #define MODE_LEN (WEIGHT_MODE + 1)
56
57 typedef struct PAINT_VERTEX_PassList {
58   struct {
59     struct DRWPass *color_faces;
60   } by_mode[MODE_LEN];
61   struct DRWPass *wire_overlay;
62   struct DRWPass *wire_select_overlay;
63   struct DRWPass *face_select_overlay;
64   struct DRWPass *vert_select_overlay;
65 } PAINT_VERTEX_PassList;
66
67 typedef struct PAINT_VERTEX_StorageList {
68   struct PAINT_VERTEX_PrivateData *g_data;
69 } PAINT_VERTEX_StorageList;
70
71 typedef struct PAINT_VERTEX_Data {
72   void *engine_type; /* Required */
73   DRWViewportEmptyList *fbl;
74   DRWViewportEmptyList *txl;
75   PAINT_VERTEX_PassList *psl;
76   PAINT_VERTEX_StorageList *stl;
77 } PAINT_VERTEX_Data;
78
79 typedef struct PAINT_VERTEX_Shaders {
80   struct {
81     struct GPUShader *color_face;
82     struct GPUShader *wire_overlay;
83     struct GPUShader *wire_select_overlay;
84   } by_mode[MODE_LEN];
85   struct GPUShader *face_select_overlay;
86   struct GPUShader *vert_select_overlay;
87 } PAINT_VERTEX_Shaders;
88
89 /* *********** STATIC *********** */
90
91 static struct {
92   PAINT_VERTEX_Shaders sh_data[GPU_SHADER_CFG_LEN];
93 } e_data = {{{{{NULL}}}}}; /* Engine data */
94
95 typedef struct PAINT_VERTEX_PrivateData {
96   struct {
97     DRWShadingGroup *color_shgrp;
98     DRWShadingGroup *lwire_shgrp;
99     DRWShadingGroup *lwire_select_shgrp;
100   } by_mode[MODE_LEN];
101   DRWShadingGroup *face_select_shgrp;
102   DRWShadingGroup *vert_select_shgrp;
103   DRWView *view_wires;
104 } PAINT_VERTEX_PrivateData; /* Transient data */
105
106 /* *********** FUNCTIONS *********** */
107
108 static void PAINT_VERTEX_engine_init(void *vedata)
109 {
110   PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
111   const DRWContextState *draw_ctx = DRW_context_state_get();
112   PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
113
114   const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
115
116   if (!sh_data->face_select_overlay) {
117     sh_data->by_mode[VERTEX_MODE].color_face = GPU_shader_create_from_arrays({
118         .vert = (const char *[]){sh_cfg_data->lib,
119                                  datatoc_common_view_lib_glsl,
120                                  datatoc_paint_vertex_vert_glsl,
121                                  NULL},
122         .frag = (const char *[]){datatoc_paint_vertex_frag_glsl, NULL},
123         .defs = (const char *[]){sh_cfg_data->def, NULL},
124     });
125     sh_data->by_mode[WEIGHT_MODE].color_face = GPU_shader_create_from_arrays({
126         .vert = (const char *[]){sh_cfg_data->lib,
127                                  datatoc_common_view_lib_glsl,
128                                  datatoc_common_globals_lib_glsl,
129                                  datatoc_paint_weight_vert_glsl,
130                                  NULL},
131         .frag = (const char *[]){datatoc_common_globals_lib_glsl,
132                                  datatoc_paint_weight_frag_glsl,
133                                  NULL},
134         .defs = (const char *[]){sh_cfg_data->def, NULL},
135     });
136
137     sh_data->face_select_overlay = GPU_shader_create_from_arrays({
138         .vert = (const char *[]){sh_cfg_data->lib,
139                                  datatoc_common_view_lib_glsl,
140                                  datatoc_paint_face_selection_vert_glsl,
141                                  NULL},
142         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
143         .defs = (const char *[]){sh_cfg_data->def, NULL},
144     });
145     sh_data->vert_select_overlay = GPU_shader_create_from_arrays({
146         .vert = (const char *[]){sh_cfg_data->lib,
147                                  datatoc_common_globals_lib_glsl,
148                                  datatoc_common_view_lib_glsl,
149                                  datatoc_paint_wire_vert_glsl,
150                                  NULL},
151         .frag = (const char *[]){datatoc_paint_vert_frag_glsl, NULL},
152         .defs = (const char *[]){sh_cfg_data->def, "#define USE_SELECT\n", NULL},
153     });
154
155     const char *mode_defs[MODE_LEN] = {
156         "#define VERTEX_MODE\n",
157         "#define WEIGHT_MODE\n",
158     };
159     for (int i = 0; i < MODE_LEN; i++) {
160       sh_data->by_mode[i].wire_overlay = GPU_shader_create_from_arrays({
161           .vert = (const char *[]){sh_cfg_data->lib,
162                                    datatoc_common_globals_lib_glsl,
163                                    datatoc_common_view_lib_glsl,
164                                    datatoc_paint_wire_vert_glsl,
165                                    NULL},
166           .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
167           .defs = (const char *[]){sh_cfg_data->def, mode_defs[i], NULL},
168       });
169       sh_data->by_mode[i].wire_select_overlay = GPU_shader_create_from_arrays({
170           .vert = (const char *[]){sh_cfg_data->lib,
171                                    datatoc_common_globals_lib_glsl,
172                                    datatoc_common_view_lib_glsl,
173                                    datatoc_paint_wire_vert_glsl,
174                                    NULL},
175           .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
176           .defs = (const char *[]){sh_cfg_data->def, mode_defs[i], "#define USE_SELECT\n", NULL},
177       });
178     }
179   }
180
181   if (!stl->g_data) {
182     /* Alloc transient pointers */
183     stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
184   }
185
186   stl->g_data->view_wires = DRW_view_create_with_zoffset(draw_ctx->rv3d, 1.0f);
187 }
188
189 static void PAINT_VERTEX_cache_init(void *vedata)
190 {
191   PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
192   PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
193   const DRWContextState *draw_ctx = DRW_context_state_get();
194   const View3D *v3d = draw_ctx->v3d;
195   const RegionView3D *rv3d = draw_ctx->rv3d;
196   PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
197
198   /* Vertex color pass */
199   {
200     DRWPass *pass = DRW_pass_create(
201         "Vert Color Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND_MUL);
202     DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->by_mode[VERTEX_MODE].color_face, pass);
203     DRW_shgroup_uniform_float_copy(
204         shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
205     if (rv3d->rflag & RV3D_CLIPPING) {
206       DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
207     }
208     psl->by_mode[VERTEX_MODE].color_faces = pass;
209     stl->g_data->by_mode[VERTEX_MODE].color_shgrp = shgrp;
210   }
211
212   /* Weight color pass */
213   {
214     DRWPass *pass = DRW_pass_create(
215         "Weight Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND_MUL);
216     DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->by_mode[WEIGHT_MODE].color_face, pass);
217     DRW_shgroup_uniform_bool_copy(
218         shgrp, "drawContours", (v3d->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0);
219     DRW_shgroup_uniform_float(shgrp, "opacity", &v3d->overlay.weight_paint_mode_opacity, 1);
220     DRW_shgroup_uniform_texture(shgrp, "colorramp", G_draw.weight_ramp);
221     DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
222     if (rv3d->rflag & RV3D_CLIPPING) {
223       DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
224     }
225     psl->by_mode[WEIGHT_MODE].color_faces = pass;
226     stl->g_data->by_mode[WEIGHT_MODE].color_shgrp = shgrp;
227   }
228
229   {
230     DRWPass *pass = DRW_pass_create("Wire Pass",
231                                     (DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA |
232                                      DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL));
233     for (int i = 0; i < MODE_LEN; i++) {
234       DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->by_mode[i].wire_overlay, pass);
235       DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
236       if (rv3d->rflag & RV3D_CLIPPING) {
237         DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
238       }
239       stl->g_data->by_mode[i].lwire_shgrp = shgrp;
240     }
241     psl->wire_overlay = pass;
242   }
243
244   {
245     DRWPass *pass = DRW_pass_create("Wire Mask Pass",
246                                     DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
247                                         DRW_STATE_DEPTH_LESS_EQUAL);
248     for (int i = 0; i < MODE_LEN; i++) {
249       DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->by_mode[i].wire_select_overlay, pass);
250       DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
251       if (rv3d->rflag & RV3D_CLIPPING) {
252         DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
253       }
254       stl->g_data->by_mode[i].lwire_select_shgrp = shgrp;
255     }
256     psl->wire_select_overlay = pass;
257   }
258
259   {
260     static const float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
261     DRWPass *pass = DRW_pass_create("Face Mask Pass",
262                                     DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
263                                         DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA);
264     DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->face_select_overlay, pass);
265     DRW_shgroup_uniform_vec4(shgrp, "color", col, 1);
266     if (rv3d->rflag & RV3D_CLIPPING) {
267       DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
268     }
269     psl->face_select_overlay = pass;
270     stl->g_data->face_select_shgrp = shgrp;
271   }
272
273   {
274     DRWPass *pass = DRW_pass_create("Vert Mask Pass",
275                                     DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
276                                         DRW_STATE_DEPTH_LESS_EQUAL);
277     DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->vert_select_overlay, pass);
278     DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
279     if (rv3d->rflag & RV3D_CLIPPING) {
280       DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
281     }
282     psl->vert_select_overlay = pass;
283     stl->g_data->vert_select_shgrp = shgrp;
284   }
285 }
286
287 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
288 {
289   PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
290   const DRWContextState *draw_ctx = DRW_context_state_get();
291   const View3D *v3d = draw_ctx->v3d;
292
293   if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
294     const int draw_mode = (ob->mode == OB_MODE_VERTEX_PAINT) ? VERTEX_MODE : WEIGHT_MODE;
295     const Mesh *me = ob->data;
296     const Mesh *me_orig = DEG_get_original_object(ob)->data;
297     const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
298     const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
299     const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
300
301     struct GPUBatch *geom = NULL;
302     if (draw_mode == VERTEX_MODE) {
303       if (me->mloopcol == NULL) {
304         return;
305       }
306       if (v3d->overlay.vertex_paint_mode_opacity != 0.0f) {
307         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
308       }
309     }
310     else {
311       if (v3d->overlay.weight_paint_mode_opacity != 0.0f) {
312         geom = DRW_cache_mesh_surface_weights_get(ob);
313       }
314     }
315     if (geom != NULL) {
316       DRW_shgroup_call(stl->g_data->by_mode[draw_mode].color_shgrp, geom, ob);
317     }
318
319     if (use_face_sel || use_wire) {
320       DRWShadingGroup *shgrp = use_face_sel ? stl->g_data->by_mode[draw_mode].lwire_select_shgrp :
321                                               stl->g_data->by_mode[draw_mode].lwire_shgrp;
322       geom = DRW_cache_mesh_surface_edges_get(ob);
323       DRW_shgroup_call(shgrp, geom, ob);
324     }
325
326     if (use_face_sel) {
327       geom = DRW_cache_mesh_surface_get(ob);
328       DRW_shgroup_call(stl->g_data->face_select_shgrp, geom, ob);
329     }
330
331     if (use_vert_sel) {
332       geom = DRW_cache_mesh_all_verts_get(ob);
333       DRW_shgroup_call(stl->g_data->vert_select_shgrp, geom, ob);
334     }
335   }
336 }
337
338 static void PAINT_VERTEX_draw_scene(void *vedata)
339 {
340   PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
341   PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
342   for (int i = 0; i < MODE_LEN; i++) {
343     DRW_draw_pass(psl->by_mode[i].color_faces);
344   }
345   DRW_draw_pass(psl->face_select_overlay);
346
347   DRW_view_set_active(stl->g_data->view_wires);
348   DRW_draw_pass(psl->wire_overlay);
349   DRW_draw_pass(psl->wire_select_overlay);
350   DRW_draw_pass(psl->vert_select_overlay);
351
352   DRW_view_set_active(NULL);
353 }
354
355 static void PAINT_VERTEX_engine_free(void)
356 {
357   for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
358     PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[sh_data_index];
359     GPUShader **sh_data_as_array = (GPUShader **)sh_data;
360     for (int i = 0; i < (sizeof(PAINT_VERTEX_Shaders) / sizeof(GPUShader *)); i++) {
361       DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
362     }
363   }
364 }
365
366 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
367
368 DrawEngineType draw_engine_paint_vertex_type = {
369     NULL,
370     NULL,
371     N_("PaintVertexMode"),
372     &PAINT_VERTEX_data_size,
373     &PAINT_VERTEX_engine_init,
374     &PAINT_VERTEX_engine_free,
375     &PAINT_VERTEX_cache_init,
376     &PAINT_VERTEX_cache_populate,
377     NULL,
378     NULL,
379     &PAINT_VERTEX_draw_scene,
380     NULL,
381     NULL,
382 };