910ffecd9bafa332b4536863382c218677d869cc
[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 *face_overlay;
50         struct DRWPass *vert_overlay;
51 } PAINT_WEIGHT_PassList;
52
53 typedef struct PAINT_WEIGHT_StorageList {
54         struct PAINT_WEIGHT_PrivateData *g_data;
55 } PAINT_WEIGHT_StorageList;
56
57 typedef struct PAINT_WEIGHT_Data {
58         void *engine_type;
59         DRWViewportEmptyList *fbl;
60         DRWViewportEmptyList *txl;
61         PAINT_WEIGHT_PassList *psl;
62         PAINT_WEIGHT_StorageList *stl;
63 } PAINT_WEIGHT_Data;
64
65 typedef struct PAINT_WEIGHT_Shaders {
66         struct GPUShader *weight_face;
67         struct GPUShader *wire_overlay;
68         struct GPUShader *face_overlay;
69         struct GPUShader *vert_overlay;
70 } PAINT_WEIGHT_Shaders;
71
72 /* *********** STATIC *********** */
73
74 static struct {
75         PAINT_WEIGHT_Shaders sh_data[GPU_SHADER_CFG_LEN];
76
77         int actdef;
78 } e_data = {NULL}; /* Engine data */
79
80 typedef struct PAINT_WEIGHT_PrivateData {
81         DRWShadingGroup *fweights_shgrp;
82         DRWShadingGroup *lwire_shgrp;
83         DRWShadingGroup *face_shgrp;
84         DRWShadingGroup *vert_shgrp;
85 } PAINT_WEIGHT_PrivateData;
86
87 /* *********** FUNCTIONS *********** */
88
89 static void PAINT_WEIGHT_engine_init(void *UNUSED(vedata))
90 {
91         const DRWContextState *draw_ctx = DRW_context_state_get();
92         PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
93         const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
94
95         if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
96                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
97         }
98
99         if (!sh_data->weight_face) {
100                 sh_data->weight_face = GPU_shader_create_from_arrays({
101                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_weight_vert_glsl, NULL},
102                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_weight_frag_glsl, NULL},
103                         .defs = (const char *[]){sh_cfg_data->def, NULL},
104                 });
105
106                 sh_data->wire_overlay = GPU_shader_create_from_arrays({
107                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
108                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
109                         .defs = (const char *[]){sh_cfg_data->def, "#define WEIGHT_MODE\n", NULL},
110                 });
111
112                 sh_data->face_overlay = GPU_shader_create_from_arrays({
113                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_paint_face_vert_glsl, NULL},
114                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
115                         .defs = (const char *[]){sh_cfg_data->def, NULL},
116                 });
117
118                 sh_data->vert_overlay = GPU_shader_create_from_arrays({
119                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
120                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_vert_frag_glsl, NULL},
121                         .defs = (const char *[]){sh_cfg_data->def, NULL},
122                 });
123         }
124 }
125
126 static void PAINT_WEIGHT_cache_init(void *vedata)
127 {
128         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
129         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
130         const DRWContextState *draw_ctx = DRW_context_state_get();
131         const View3D *v3d = draw_ctx->v3d;
132         RegionView3D *rv3d = draw_ctx->rv3d;
133         PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
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(sh_data->weight_face, 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", G_draw.weight_ramp);
152                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
153                 if (rv3d->rflag & RV3D_CLIPPING) {
154                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
155                 }
156         }
157
158         {
159                 psl->wire_overlay = DRW_pass_create(
160                         "Wire Pass",
161                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
162
163                 stl->g_data->lwire_shgrp = DRW_shgroup_create(sh_data->wire_overlay, psl->wire_overlay);
164                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", G_draw.block_ubo);
165                 if (rv3d->rflag & RV3D_CLIPPING) {
166                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_shgrp, rv3d);
167                 }
168         }
169
170         {
171                 psl->face_overlay = DRW_pass_create(
172                         "Face Mask Pass",
173                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
174
175                 stl->g_data->face_shgrp = DRW_shgroup_create(sh_data->face_overlay, psl->face_overlay);
176
177                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
178                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
179                 if (rv3d->rflag & RV3D_CLIPPING) {
180                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->face_shgrp, rv3d);
181                 }
182         }
183
184         {
185                 psl->vert_overlay = DRW_pass_create(
186                         "Vert Mask Pass",
187                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
188
189                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->vert_overlay, psl->vert_overlay);
190                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
191                 if (rv3d->rflag & RV3D_CLIPPING) {
192                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
193                 }
194         }
195 }
196
197 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
198 {
199         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
200         const DRWContextState *draw_ctx = DRW_context_state_get();
201         const View3D *v3d = draw_ctx->v3d;
202
203         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
204                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
205                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
206                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
207                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
208                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
209                 struct GPUBatch *geom;
210
211                 if (use_surface) {
212                         geom = DRW_cache_mesh_surface_weights_get(ob);
213                         DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
214                 }
215
216                 if (use_face_sel || use_wire) {
217                         geom = DRW_cache_mesh_surface_edges_get(ob);
218                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
219                 }
220
221                 if (use_face_sel) {
222                         geom = DRW_cache_mesh_surface_get(ob);
223                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
224                 }
225
226                 if (use_vert_sel) {
227                         geom = DRW_cache_mesh_all_verts_get(ob);
228                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
229                 }
230         }
231 }
232
233 static void PAINT_WEIGHT_draw_scene(void *vedata)
234 {
235         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
236
237         DRW_draw_pass(psl->weight_faces);
238         DRW_draw_pass(psl->face_overlay);
239         DRW_draw_pass(psl->wire_overlay);
240         DRW_draw_pass(psl->vert_overlay);
241 }
242
243 static void PAINT_WEIGHT_engine_free(void)
244 {
245         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
246                 PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[sh_data_index];
247                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
248                 for (int i = 0; i < (sizeof(PAINT_WEIGHT_Shaders) / sizeof(GPUShader *)); i++) {
249                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
250                 }
251         }
252 }
253
254 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
255
256 DrawEngineType draw_engine_paint_weight_type = {
257         NULL, NULL,
258         N_("PaintWeightMode"),
259         &PAINT_WEIGHT_data_size,
260         &PAINT_WEIGHT_engine_init,
261         &PAINT_WEIGHT_engine_free,
262         &PAINT_WEIGHT_cache_init,
263         &PAINT_WEIGHT_cache_populate,
264         NULL,
265         NULL,
266         &PAINT_WEIGHT_draw_scene,
267         NULL,
268         NULL,
269 };