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 struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
42 extern struct GlobalsUboStorage ts; /* draw_common.c */
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_common_globals_lib_glsl[];
50
51 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
52
53 /* *********** LISTS *********** */
54
55 typedef struct PAINT_VERTEX_PassList {
56         struct DRWPass *vcolor_faces;
57         struct DRWPass *wire_overlay;
58         struct DRWPass *face_overlay;
59 } PAINT_VERTEX_PassList;
60
61 typedef struct PAINT_VERTEX_StorageList {
62         struct PAINT_VERTEX_PrivateData *g_data;
63 } PAINT_VERTEX_StorageList;
64
65 typedef struct PAINT_VERTEX_Data {
66         void *engine_type; /* Required */
67         DRWViewportEmptyList *fbl;
68         DRWViewportEmptyList *txl;
69         PAINT_VERTEX_PassList *psl;
70         PAINT_VERTEX_StorageList *stl;
71 } PAINT_VERTEX_Data;
72
73 /* *********** STATIC *********** */
74
75 static struct {
76         struct GPUShader *vcolor_face_shader;
77         struct GPUShader *wire_overlay_shader;
78         struct GPUShader *face_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 } PAINT_VERTEX_PrivateData; /* Transient data */
86
87 /* *********** FUNCTIONS *********** */
88
89 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
90 {
91         if (!e_data.vcolor_face_shader) {
92                 e_data.vcolor_face_shader = DRW_shader_create(
93                         datatoc_paint_vertex_vert_glsl, NULL,
94                         datatoc_paint_vertex_frag_glsl, NULL);
95
96                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
97                         datatoc_paint_wire_vert_glsl, NULL,
98                         datatoc_paint_wire_frag_glsl,
99                         datatoc_common_globals_lib_glsl, "#define VERTEX_MODE\n");
100
101                 e_data.face_overlay_shader = DRW_shader_create(
102                         datatoc_paint_face_vert_glsl, NULL,
103                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
104         }
105 }
106
107 static void PAINT_VERTEX_cache_init(void *vedata)
108 {
109         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
110         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
111         const DRWContextState *draw_ctx = DRW_context_state_get();
112         const View3D *v3d = draw_ctx->v3d;
113
114         if (!stl->g_data) {
115                 /* Alloc transient pointers */
116                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
117         }
118
119         {
120                 /* Create a pass */
121                 psl->vcolor_faces = DRW_pass_create(
122                         "Vert Color Pass",
123                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
124
125                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
126                 DRW_shgroup_uniform_float_copy(stl->g_data->fvcolor_shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
127         }
128
129         {
130                 psl->wire_overlay = DRW_pass_create(
131                         "Wire Pass",
132                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
133
134                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
135                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
136         }
137
138         {
139                 psl->face_overlay = DRW_pass_create(
140                         "Face Mask Pass",
141                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
142
143                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
144
145                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
146                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
147         }
148 }
149
150 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
151 {
152         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
153         const DRWContextState *draw_ctx = DRW_context_state_get();
154         const View3D *v3d = draw_ctx->v3d;
155
156         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
157                 const Mesh *me = ob->data;
158                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
159                 const bool use_surface = v3d->overlay.vertex_paint_mode_opacity != 0.0f;
160                 const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
161                 struct GPUBatch *geom;
162
163                 if (me->mloopcol == NULL) {
164                         return;
165                 }
166
167                 if (use_surface) {
168                         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
169                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
170                 }
171
172                 if (use_face_sel || use_wire) {
173                         geom = DRW_cache_mesh_surface_edges_get(ob);
174                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
175                 }
176
177                 if (use_face_sel) {
178                         geom = DRW_cache_mesh_surface_get(ob);
179                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
180                 }
181         }
182 }
183
184 static void PAINT_VERTEX_draw_scene(void *vedata)
185 {
186         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
187
188         DRW_draw_pass(psl->vcolor_faces);
189         DRW_draw_pass(psl->face_overlay);
190         DRW_draw_pass(psl->wire_overlay);
191 }
192
193 static void PAINT_VERTEX_engine_free(void)
194 {
195         DRW_SHADER_FREE_SAFE(e_data.vcolor_face_shader);
196         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
197         DRW_SHADER_FREE_SAFE(e_data.face_overlay_shader);
198 }
199
200 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
201
202 DrawEngineType draw_engine_paint_vertex_type = {
203         NULL, NULL,
204         N_("PaintVertexMode"),
205         &PAINT_VERTEX_data_size,
206         &PAINT_VERTEX_engine_init,
207         &PAINT_VERTEX_engine_free,
208         &PAINT_VERTEX_cache_init,
209         &PAINT_VERTEX_cache_populate,
210         NULL,
211         NULL,
212         &PAINT_VERTEX_draw_scene,
213         NULL,
214         NULL,
215 };