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