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