Cleanup: vertex painting variable use
[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
30 #include "DNA_mesh_types.h"
31 #include "DNA_view3d_types.h"
32
33 #include "DEG_depsgraph_query.h"
34
35 extern char datatoc_paint_vertex_vert_glsl[];
36 extern char datatoc_paint_vertex_frag_glsl[];
37 extern char datatoc_paint_wire_vert_glsl[];
38 extern char datatoc_paint_wire_frag_glsl[];
39 extern char datatoc_paint_face_vert_glsl[];
40 extern char datatoc_paint_vert_frag_glsl[];
41 extern char datatoc_common_globals_lib_glsl[];
42
43 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
44
45 /* *********** LISTS *********** */
46
47 typedef struct PAINT_VERTEX_PassList {
48         struct DRWPass *vcolor_faces;
49         struct DRWPass *wire_overlay;
50         struct DRWPass *wire_select_overlay;
51         struct DRWPass *face_select_overlay;
52         struct DRWPass *vert_select_overlay;
53 } PAINT_VERTEX_PassList;
54
55 typedef struct PAINT_VERTEX_StorageList {
56         struct PAINT_VERTEX_PrivateData *g_data;
57 } PAINT_VERTEX_StorageList;
58
59 typedef struct PAINT_VERTEX_Data {
60         void *engine_type; /* Required */
61         DRWViewportEmptyList *fbl;
62         DRWViewportEmptyList *txl;
63         PAINT_VERTEX_PassList *psl;
64         PAINT_VERTEX_StorageList *stl;
65 } PAINT_VERTEX_Data;
66
67 typedef struct PAINT_VERTEX_Shaders {
68         struct GPUShader *vcolor_face;
69         struct GPUShader *wire_overlay;
70         struct GPUShader *wire_select_overlay;
71         struct GPUShader *face_select_overlay;
72         struct GPUShader *vert_select_overlay;
73 } PAINT_VERTEX_Shaders;
74
75 /* *********** STATIC *********** */
76
77 static struct {
78         PAINT_VERTEX_Shaders sh_data[GPU_SHADER_CFG_LEN];
79 } e_data = {{{NULL}}}; /* Engine data */
80
81 typedef struct PAINT_VERTEX_PrivateData {
82         DRWShadingGroup *fvcolor_shgrp;
83         DRWShadingGroup *lwire_shgrp;
84         DRWShadingGroup *lwire_select_shgrp;
85         DRWShadingGroup *face_select_shgrp;
86         DRWShadingGroup *vert_select_shgrp;
87 } PAINT_VERTEX_PrivateData; /* Transient data */
88
89 /* *********** FUNCTIONS *********** */
90
91 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
92 {
93         const DRWContextState *draw_ctx = DRW_context_state_get();
94         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
95
96         if (draw_ctx->sh_cfg) {
97                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
98         }
99         const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
100
101         if (!sh_data->vcolor_face) {
102
103                 sh_data->vcolor_face = GPU_shader_create_from_arrays({
104                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_paint_vertex_vert_glsl, NULL},
105                         .frag = (const char *[]){datatoc_paint_vertex_frag_glsl, NULL},
106                         .defs = (const char *[]){sh_cfg_data->def, NULL},
107                 });
108                 sh_data->wire_overlay = GPU_shader_create_from_arrays({
109                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
110                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
111                         .defs = (const char *[]){sh_cfg_data->def, "#define VERTEX_MODE\n", NULL},
112                 });
113                 sh_data->wire_select_overlay = GPU_shader_create_from_arrays({
114                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
115                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
116                         .defs = (const char *[]){sh_cfg_data->def, "#define VERTEX_MODE\n" "#define USE_SELECT\n", NULL},
117                 });
118                 sh_data->face_select_overlay = GPU_shader_create_from_arrays({
119                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_paint_face_vert_glsl, NULL},
120                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
121                         .defs = (const char *[]){sh_cfg_data->def, NULL},
122                 });
123                 sh_data->vert_select_overlay = GPU_shader_create_from_arrays({
124                         .vert = (const char *[]){sh_cfg_data->lib, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
125                         .frag = (const char *[]){datatoc_paint_vert_frag_glsl, NULL},
126                         .defs = (const char *[]){sh_cfg_data->def, "#define USE_SELECT\n", NULL},
127                 });
128         }
129 }
130
131 static void PAINT_VERTEX_cache_init(void *vedata)
132 {
133         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
134         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
135         const DRWContextState *draw_ctx = DRW_context_state_get();
136         const View3D *v3d = draw_ctx->v3d;
137         const RegionView3D *rv3d = draw_ctx->rv3d;
138         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_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                 DRWPass *pass = DRW_pass_create(
148                         "Vert Color Pass",
149                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
150                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->vcolor_face, pass);
151                 DRW_shgroup_uniform_float_copy(shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
152                 if (rv3d->rflag & RV3D_CLIPPING) {
153                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
154                 }
155                 psl->vcolor_faces = pass;
156                 stl->g_data->fvcolor_shgrp = shgrp;
157         }
158
159         {
160                 DRWPass *pass = DRW_pass_create(
161                         "Wire Pass",
162                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
163                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->wire_overlay, pass);
164                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
165                 if (rv3d->rflag & RV3D_CLIPPING) {
166                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
167                 }
168                 psl->wire_overlay = pass;
169                 stl->g_data->lwire_shgrp = shgrp;
170         }
171
172         {
173                 DRWPass *pass = DRW_pass_create(
174                         "Wire Mask Pass",
175                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
176                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->wire_select_overlay, pass);
177                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
178                 if (rv3d->rflag & RV3D_CLIPPING) {
179                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
180                 }
181                 psl->wire_select_overlay = pass;
182                 stl->g_data->lwire_select_shgrp = shgrp;
183         }
184
185         {
186                 DRWPass *pass = DRW_pass_create(
187                         "Face Mask Pass",
188                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
189                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->face_select_overlay, pass);
190                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
191                 DRW_shgroup_uniform_vec4(shgrp, "color", col, 1);
192                 if (rv3d->rflag & RV3D_CLIPPING) {
193                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
194                 }
195                 psl->face_select_overlay = pass;
196                 stl->g_data->face_select_shgrp = shgrp;
197         }
198
199         {
200                 DRWPass *pass = DRW_pass_create(
201                         "Vert Mask Pass",
202                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
203                 DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->vert_select_overlay, pass);
204                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
205                 if (rv3d->rflag & RV3D_CLIPPING) {
206                         DRW_shgroup_world_clip_planes_from_rv3d(shgrp, rv3d);
207                 }
208                 psl->vert_select_overlay = pass;
209                 stl->g_data->vert_select_shgrp = shgrp;
210         }
211 }
212
213 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
214 {
215         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
216         const DRWContextState *draw_ctx = DRW_context_state_get();
217         const View3D *v3d = draw_ctx->v3d;
218
219         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
220                 const Mesh *me = ob->data;
221                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
222                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
223                 const bool use_surface = v3d->overlay.vertex_paint_mode_opacity != 0.0f;
224                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
225                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
226                 struct GPUBatch *geom;
227
228                 if (me->mloopcol == NULL) {
229                         return;
230                 }
231
232                 if (use_surface) {
233                         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
234                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
235                 }
236
237                 if (use_face_sel || use_wire) {
238                         DRWShadingGroup *shgrp = use_face_sel ? stl->g_data->lwire_select_shgrp : stl->g_data->lwire_shgrp;
239                         geom = DRW_cache_mesh_surface_edges_get(ob);
240                         DRW_shgroup_call_add(shgrp, geom, ob->obmat);
241                 }
242
243                 if (use_face_sel) {
244                         geom = DRW_cache_mesh_surface_get(ob);
245                         DRW_shgroup_call_add(stl->g_data->face_select_shgrp, geom, ob->obmat);
246                 }
247
248                 if (use_vert_sel) {
249                         geom = DRW_cache_mesh_all_verts_get(ob);
250                         DRW_shgroup_call_add(stl->g_data->vert_select_shgrp, geom, ob->obmat);
251                 }
252         }
253 }
254
255 static void PAINT_VERTEX_draw_scene(void *vedata)
256 {
257         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
258
259         DRW_draw_pass(psl->vcolor_faces);
260         DRW_draw_pass(psl->wire_overlay);
261         DRW_draw_pass(psl->wire_select_overlay);
262         DRW_draw_pass(psl->vert_select_overlay);
263         DRW_draw_pass(psl->face_select_overlay);
264 }
265
266 static void PAINT_VERTEX_engine_free(void)
267 {
268         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
269                 PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[sh_data_index];
270                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
271                 for (int i = 0; i < (sizeof(PAINT_VERTEX_Shaders) / sizeof(GPUShader *)); i++) {
272                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
273                 }
274         }
275 }
276
277 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
278
279 DrawEngineType draw_engine_paint_vertex_type = {
280         NULL, NULL,
281         N_("PaintVertexMode"),
282         &PAINT_VERTEX_data_size,
283         &PAINT_VERTEX_engine_init,
284         &PAINT_VERTEX_engine_free,
285         &PAINT_VERTEX_cache_init,
286         &PAINT_VERTEX_cache_populate,
287         NULL,
288         NULL,
289         &PAINT_VERTEX_draw_scene,
290         NULL,
291         NULL,
292 };