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