Cleanup: add trailing commas
[blender.git] / source / blender / draw / modes / paint_vertex_mode.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * Copyright 2016, Blender Foundation.
19  * Contributor(s): Blender Institute
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  *
23  */
24
25 /** \file blender/draw/modes/paint_vertex_mode.c
26  *  \ingroup draw
27  */
28
29 #include "DRW_render.h"
30
31 /* If builtin shaders are needed */
32 #include "GPU_shader.h"
33
34 #include "draw_common.h"
35
36
37 #include "DNA_mesh_types.h"
38 #include "DNA_view3d_types.h"
39
40 #include "DEG_depsgraph_query.h"
41
42 extern char datatoc_common_world_clip_lib_glsl[];
43
44 extern char datatoc_paint_vertex_vert_glsl[];
45 extern char datatoc_paint_vertex_frag_glsl[];
46 extern char datatoc_paint_wire_vert_glsl[];
47 extern char datatoc_paint_wire_frag_glsl[];
48 extern char datatoc_paint_face_vert_glsl[];
49 extern char datatoc_paint_vert_frag_glsl[];
50 extern char datatoc_common_globals_lib_glsl[];
51
52 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
53
54 /* *********** LISTS *********** */
55
56 typedef struct PAINT_VERTEX_PassList {
57         struct DRWPass *vcolor_faces;
58         struct DRWPass *wire_overlay;
59         struct DRWPass *face_overlay;
60         struct DRWPass *vert_overlay;
61 } PAINT_VERTEX_PassList;
62
63 typedef struct PAINT_VERTEX_StorageList {
64         struct PAINT_VERTEX_PrivateData *g_data;
65 } PAINT_VERTEX_StorageList;
66
67 typedef struct PAINT_VERTEX_Data {
68         void *engine_type; /* Required */
69         DRWViewportEmptyList *fbl;
70         DRWViewportEmptyList *txl;
71         PAINT_VERTEX_PassList *psl;
72         PAINT_VERTEX_StorageList *stl;
73 } PAINT_VERTEX_Data;
74
75 typedef struct PAINT_VERTEX_Shaders {
76         struct GPUShader *vcolor_face;
77         struct GPUShader *wire_overlay;
78         struct GPUShader *face_overlay;
79         struct GPUShader *vert_overlay;
80 } PAINT_VERTEX_Shaders;
81
82 /* *********** STATIC *********** */
83
84 static struct {
85         PAINT_VERTEX_Shaders sh_data[DRW_SHADER_SLOT_LEN];
86 } e_data = {{{NULL}}}; /* Engine data */
87
88 typedef struct PAINT_VERTEX_PrivateData {
89         DRWShadingGroup *fvcolor_shgrp;
90         DRWShadingGroup *lwire_shgrp;
91         DRWShadingGroup *face_shgrp;
92         DRWShadingGroup *vert_shgrp;
93 } PAINT_VERTEX_PrivateData; /* Transient data */
94
95 /* *********** FUNCTIONS *********** */
96
97 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
98 {
99         const DRWContextState *draw_ctx = DRW_context_state_get();
100         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_slot];
101         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
102
103         if (is_clip) {
104                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
105         }
106
107         if (!sh_data->vcolor_face) {
108                 const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
109                 const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
110
111                 sh_data->vcolor_face = DRW_shader_create_from_arrays({
112                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_vertex_vert_glsl, NULL},
113                         .frag = (const char *[]){datatoc_paint_vertex_frag_glsl, NULL},
114                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
115                 });
116                 sh_data->wire_overlay = DRW_shader_create_from_arrays({
117                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
118                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
119                         .defs = (const char *[]){world_clip_def_or_empty, "#define VERTEX_MODE\n", NULL},
120                 });
121                 sh_data->face_overlay = DRW_shader_create_from_arrays({
122                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_face_vert_glsl, NULL},
123                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
124                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
125                 });
126                 sh_data->vert_overlay = DRW_shader_create_from_arrays({
127                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
128                         .frag = (const char *[]){datatoc_paint_vert_frag_glsl, NULL},
129                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
130                 });
131         }
132 }
133
134 static void PAINT_VERTEX_cache_init(void *vedata)
135 {
136         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
137         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
138         const DRWContextState *draw_ctx = DRW_context_state_get();
139         const View3D *v3d = draw_ctx->v3d;
140         const RegionView3D *rv3d = draw_ctx->rv3d;
141         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_slot];
142
143         if (!stl->g_data) {
144                 /* Alloc transient pointers */
145                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
146         }
147
148         {
149                 /* Create a pass */
150                 psl->vcolor_faces = DRW_pass_create(
151                         "Vert Color Pass",
152                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
153
154                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(sh_data->vcolor_face, psl->vcolor_faces);
155                 DRW_shgroup_uniform_float_copy(stl->g_data->fvcolor_shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
156                 if (rv3d->rflag & RV3D_CLIPPING) {
157                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fvcolor_shgrp, rv3d);
158                 }
159         }
160
161         {
162                 psl->wire_overlay = DRW_pass_create(
163                         "Wire Pass",
164                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
165
166                 stl->g_data->lwire_shgrp = DRW_shgroup_create(sh_data->wire_overlay, psl->wire_overlay);
167                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", G_draw.block_ubo);
168                 if (rv3d->rflag & RV3D_CLIPPING) {
169                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_shgrp, rv3d);
170                 }
171         }
172
173         {
174                 psl->face_overlay = DRW_pass_create(
175                         "Face Mask Pass",
176                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
177
178                 stl->g_data->face_shgrp = DRW_shgroup_create(sh_data->face_overlay, psl->face_overlay);
179                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
180                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
181                 if (rv3d->rflag & RV3D_CLIPPING) {
182                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->face_shgrp, rv3d);
183                 }
184         }
185
186         {
187                 psl->vert_overlay = DRW_pass_create(
188                         "Vert Mask Pass",
189                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
190
191                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->vert_overlay, psl->vert_overlay);
192                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
193                 if (rv3d->rflag & RV3D_CLIPPING) {
194                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
195                 }
196         }
197 }
198
199 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
200 {
201         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
202         const DRWContextState *draw_ctx = DRW_context_state_get();
203         const View3D *v3d = draw_ctx->v3d;
204
205         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
206                 const Mesh *me = ob->data;
207                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
208                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
209                 const bool use_surface = v3d->overlay.vertex_paint_mode_opacity != 0.0f;
210                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
211                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
212                 struct GPUBatch *geom;
213
214                 if (me->mloopcol == NULL) {
215                         return;
216                 }
217
218                 if (use_surface) {
219                         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
220                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
221                 }
222
223                 if (use_face_sel || use_wire) {
224                         geom = DRW_cache_mesh_surface_edges_get(ob);
225                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
226                 }
227
228                 if (use_face_sel) {
229                         geom = DRW_cache_mesh_surface_get(ob);
230                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
231                 }
232
233                 if (use_vert_sel) {
234                         geom = DRW_cache_mesh_all_verts_get(ob);
235                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
236                 }
237         }
238 }
239
240 static void PAINT_VERTEX_draw_scene(void *vedata)
241 {
242         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
243
244         DRW_draw_pass(psl->vcolor_faces);
245         DRW_draw_pass(psl->face_overlay);
246         DRW_draw_pass(psl->wire_overlay);
247         DRW_draw_pass(psl->vert_overlay);
248 }
249
250 static void PAINT_VERTEX_engine_free(void)
251 {
252         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
253                 PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[sh_data_index];
254                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
255                 for (int i = 0; i < (sizeof(PAINT_VERTEX_Shaders) / sizeof(GPUShader *)); i++) {
256                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
257                 }
258         }
259 }
260
261 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
262
263 DrawEngineType draw_engine_paint_vertex_type = {
264         NULL, NULL,
265         N_("PaintVertexMode"),
266         &PAINT_VERTEX_data_size,
267         &PAINT_VERTEX_engine_init,
268         &PAINT_VERTEX_engine_free,
269         &PAINT_VERTEX_cache_init,
270         &PAINT_VERTEX_cache_populate,
271         NULL,
272         NULL,
273         &PAINT_VERTEX_draw_scene,
274         NULL,
275         NULL,
276 };