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