0d6b2edba936944fc299042692923bdeba332cbe
[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                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
107                         datatoc_paint_wire_vert_glsl, NULL,
108                         datatoc_paint_wire_frag_glsl,
109                         datatoc_common_globals_lib_glsl, "#define WEIGHT_MODE\n");
110
111                 e_data.face_overlay_shader = DRW_shader_create(
112                         datatoc_paint_face_vert_glsl, NULL,
113                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
114
115                 e_data.vert_overlay_shader = DRW_shader_create_with_lib(
116                         datatoc_paint_wire_vert_glsl, NULL,
117                         datatoc_paint_vert_frag_glsl,
118                         datatoc_common_globals_lib_glsl, NULL);
119         }
120 }
121
122 static void PAINT_WEIGHT_cache_init(void *vedata)
123 {
124         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
125         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
126         const DRWContextState *draw_ctx = DRW_context_state_get();
127         const View3D *v3d = draw_ctx->v3d;
128
129         if (!stl->g_data) {
130                 /* Alloc transient pointers */
131                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
132         }
133
134         {
135                 /* Create a pass */
136                 psl->weight_faces = DRW_pass_create(
137                         "Weight Pass",
138                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
139
140                 stl->g_data->fweights_shgrp = DRW_shgroup_create(e_data.weight_face_shader, psl->weight_faces);
141
142                 DRW_shgroup_uniform_bool_copy(stl->g_data->fweights_shgrp, "drawContours", (v3d->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0);
143
144                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &v3d->overlay.weight_paint_mode_opacity, 1);
145                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", globals_weight_ramp);
146                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", globals_ubo);
147         }
148
149         {
150                 psl->wire_overlay = DRW_pass_create(
151                         "Wire Pass",
152                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
153
154                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
155                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
156         }
157
158         {
159                 psl->face_overlay = DRW_pass_create(
160                         "Face Mask Pass",
161                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
162
163                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
164
165                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
166                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
167         }
168
169         {
170                 psl->vert_overlay = DRW_pass_create(
171                         "Vert Mask Pass",
172                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
173
174                 stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.vert_overlay_shader, psl->vert_overlay);
175                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", globals_ubo);
176         }
177 }
178
179 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
180 {
181         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
182         const DRWContextState *draw_ctx = DRW_context_state_get();
183         const View3D *v3d = draw_ctx->v3d;
184
185         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
186                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
187                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
188                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
189                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
190                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
191                 struct GPUBatch *geom;
192
193                 if (use_surface) {
194                         geom = DRW_cache_mesh_surface_weights_get(ob);
195                         DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
196                 }
197
198                 if (use_face_sel || use_wire) {
199                         geom = DRW_cache_mesh_surface_edges_get(ob);
200                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
201                 }
202
203                 if (use_face_sel) {
204                         geom = DRW_cache_mesh_surface_get(ob);
205                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
206                 }
207
208                 if (use_vert_sel) {
209                         geom = DRW_cache_mesh_all_verts_get(ob);
210                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
211                 }
212         }
213 }
214
215 static void PAINT_WEIGHT_draw_scene(void *vedata)
216 {
217         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
218
219         DRW_draw_pass(psl->weight_faces);
220         DRW_draw_pass(psl->face_overlay);
221         DRW_draw_pass(psl->wire_overlay);
222         DRW_draw_pass(psl->vert_overlay);
223 }
224
225 static void PAINT_WEIGHT_engine_free(void)
226 {
227         DRW_SHADER_FREE_SAFE(e_data.weight_face_shader);
228         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
229         DRW_SHADER_FREE_SAFE(e_data.vert_overlay_shader);
230         DRW_SHADER_FREE_SAFE(e_data.face_overlay_shader);
231 }
232
233 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
234
235 DrawEngineType draw_engine_paint_weight_type = {
236         NULL, NULL,
237         N_("PaintWeightMode"),
238         &PAINT_WEIGHT_data_size,
239         &PAINT_WEIGHT_engine_init,
240         &PAINT_WEIGHT_engine_free,
241         &PAINT_WEIGHT_cache_init,
242         &PAINT_WEIGHT_cache_populate,
243         NULL,
244         NULL,
245         &PAINT_WEIGHT_draw_scene,
246         NULL,
247         NULL,
248 };