Weight Paint Overlay: Refactor wire drawing
[blender.git] / source / blender / draw / modes / paint_weight_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_weight_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 "BKE_mesh.h"
40
41 #include "DEG_depsgraph_query.h"
42
43 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
44 extern struct GlobalsUboStorage ts; /* draw_common.c */
45
46 extern struct GPUTexture *globals_weight_ramp; /* draw_common.c */
47
48 extern char datatoc_paint_face_vert_glsl[];
49 extern char datatoc_paint_weight_vert_glsl[];
50 extern char datatoc_paint_weight_frag_glsl[];
51 extern char datatoc_paint_wire_vert_glsl[];
52 extern char datatoc_paint_wire_frag_glsl[];
53 extern char datatoc_paint_vert_frag_glsl[];
54 extern char datatoc_common_globals_lib_glsl[];
55
56 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
57
58 /* *********** LISTS *********** */
59
60 typedef struct PAINT_WEIGHT_PassList {
61         struct DRWPass *weight_faces;
62         struct DRWPass *wire_overlay;
63         struct DRWPass *face_overlay;
64         struct DRWPass *vert_overlay;
65 } PAINT_WEIGHT_PassList;
66
67 typedef struct PAINT_WEIGHT_StorageList {
68         struct PAINT_WEIGHT_PrivateData *g_data;
69 } PAINT_WEIGHT_StorageList;
70
71 typedef struct PAINT_WEIGHT_Data {
72         void *engine_type;
73         DRWViewportEmptyList *fbl;
74         DRWViewportEmptyList *txl;
75         PAINT_WEIGHT_PassList *psl;
76         PAINT_WEIGHT_StorageList *stl;
77 } PAINT_WEIGHT_Data;
78
79 /* *********** STATIC *********** */
80
81 static struct {
82         struct GPUShader *weight_face_shader;
83         struct GPUShader *wire_overlay_shader;
84         struct GPUShader *face_overlay_shader;
85         struct GPUShader *vert_overlay_shader;
86         int actdef;
87 } e_data = {NULL}; /* Engine data */
88
89 typedef struct PAINT_WEIGHT_PrivateData {
90         DRWShadingGroup *fweights_shgrp;
91         DRWShadingGroup *lwire_shgrp;
92         DRWShadingGroup *face_shgrp;
93         DRWShadingGroup *vert_shgrp;
94 } PAINT_WEIGHT_PrivateData;
95
96 /* *********** FUNCTIONS *********** */
97
98 static void PAINT_WEIGHT_engine_init(void *UNUSED(vedata))
99 {
100         if (!e_data.weight_face_shader) {
101                 e_data.weight_face_shader = DRW_shader_create_with_lib(
102                         datatoc_paint_weight_vert_glsl, NULL,
103                         datatoc_paint_weight_frag_glsl,
104                         datatoc_common_globals_lib_glsl, NULL);
105         }
106
107         if (!e_data.wire_overlay_shader) {
108                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
109                         datatoc_paint_wire_vert_glsl, NULL,
110                         datatoc_paint_wire_frag_glsl,
111                         datatoc_common_globals_lib_glsl, "#define WEIGHT_MODE\n");
112         }
113
114         if (!e_data.face_overlay_shader) {
115                 e_data.face_overlay_shader = DRW_shader_create(
116                         datatoc_paint_face_vert_glsl, NULL,
117                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
118         }
119
120         if (!e_data.vert_overlay_shader) {
121                 e_data.vert_overlay_shader = DRW_shader_create_with_lib(
122                         datatoc_paint_wire_vert_glsl, NULL,
123                         datatoc_paint_vert_frag_glsl,
124                         datatoc_common_globals_lib_glsl, NULL);
125         }
126 }
127
128 static void PAINT_WEIGHT_cache_init(void *vedata)
129 {
130         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
131         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
132         const DRWContextState *draw_ctx = DRW_context_state_get();
133         const View3D *v3d = draw_ctx->v3d;
134
135         if (!stl->g_data) {
136                 /* Alloc transient pointers */
137                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
138         }
139
140         {
141                 /* Create a pass */
142                 psl->weight_faces = DRW_pass_create(
143                         "Weight Pass",
144                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
145
146                 stl->g_data->fweights_shgrp = DRW_shgroup_create(e_data.weight_face_shader, psl->weight_faces);
147
148                 DRW_shgroup_uniform_bool_copy(stl->g_data->fweights_shgrp, "drawContours", (v3d->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0);
149
150                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &v3d->overlay.weight_paint_mode_opacity, 1);
151                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", globals_weight_ramp);
152                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", globals_ubo);
153         }
154
155         {
156                 psl->wire_overlay = DRW_pass_create(
157                         "Wire Pass",
158                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
159
160                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
161                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
162         }
163
164         {
165                 psl->face_overlay = DRW_pass_create(
166                         "Face Mask Pass",
167                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
168
169                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
170
171                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
172                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
173         }
174
175         {
176                 psl->vert_overlay = DRW_pass_create(
177                         "Vert Mask Pass",
178                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
179
180                 stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.vert_overlay_shader, psl->vert_overlay);
181                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", globals_ubo);
182         }
183 }
184
185 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
186 {
187         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
188         const DRWContextState *draw_ctx = DRW_context_state_get();
189         const View3D *v3d = draw_ctx->v3d;
190
191         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
192                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
193                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
194                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
195                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
196                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
197                 struct GPUBatch *geom;
198
199                 if (use_surface) {
200                         geom = DRW_cache_mesh_surface_weights_get(ob, draw_ctx->scene->toolsettings, true);
201                         DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
202                 }
203
204                 if (use_face_sel || use_wire) {
205                         geom = DRW_cache_mesh_wire_get(ob);
206                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
207                 }
208
209                 if (use_face_sel) {
210                         geom = DRW_cache_mesh_surface_get(ob);
211                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
212                 }
213
214                 if (use_vert_sel) {
215                         geom = DRW_cache_mesh_verts_get(ob);
216                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
217                 }
218         }
219 }
220
221 static void PAINT_WEIGHT_draw_scene(void *vedata)
222 {
223         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
224
225         DRW_draw_pass(psl->weight_faces);
226         DRW_draw_pass(psl->face_overlay);
227         DRW_draw_pass(psl->wire_overlay);
228         DRW_draw_pass(psl->vert_overlay);
229 }
230
231 static void PAINT_WEIGHT_engine_free(void)
232 {
233         DRW_SHADER_FREE_SAFE(e_data.weight_face_shader);
234         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
235         DRW_SHADER_FREE_SAFE(e_data.vert_overlay_shader);
236         DRW_SHADER_FREE_SAFE(e_data.face_overlay_shader);
237 }
238
239 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
240
241 DrawEngineType draw_engine_paint_weight_type = {
242         NULL, NULL,
243         N_("PaintWeightMode"),
244         &PAINT_WEIGHT_data_size,
245         &PAINT_WEIGHT_engine_init,
246         &PAINT_WEIGHT_engine_free,
247         &PAINT_WEIGHT_cache_init,
248         &PAINT_WEIGHT_cache_populate,
249         NULL,
250         NULL,
251         &PAINT_WEIGHT_draw_scene,
252         NULL,
253         NULL,
254 };