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