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