Cleanup: add BEGIN/END to GPL headers
[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[2];
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 int PAINT_VERTEX_sh_data_index_from_rv3d(const RegionView3D *rv3d)
100 {
101         if (rv3d->rflag & RV3D_CLIPPING) {
102                 return 1;
103         }
104         return 0;
105 }
106
107 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
108 {
109         const DRWContextState *draw_ctx = DRW_context_state_get();
110         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[PAINT_VERTEX_sh_data_index_from_rv3d(draw_ctx->rv3d)];
111         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
112
113         if (is_clip) {
114                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
115         }
116
117         if (!sh_data->vcolor_face) {
118                 const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
119                 const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
120
121                 sh_data->vcolor_face = DRW_shader_create_from_arrays({
122                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_vertex_vert_glsl, NULL},
123                         .frag = (const char *[]){datatoc_paint_vertex_frag_glsl, NULL},
124                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
125
126                 sh_data->wire_overlay = DRW_shader_create_from_arrays({
127                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
128                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
129                         .defs = (const char *[]){world_clip_def_or_empty, "#define VERTEX_MODE\n", NULL}});
130
131                 sh_data->face_overlay = DRW_shader_create_from_arrays({
132                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_face_vert_glsl, NULL},
133                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
134                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
135
136                 sh_data->vert_overlay = DRW_shader_create_from_arrays({
137                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
138                         .frag = (const char *[]){datatoc_paint_vert_frag_glsl, NULL},
139                         .defs = (const char *[]){world_clip_def_or_empty, NULL}});
140         }
141 }
142
143 static void PAINT_VERTEX_cache_init(void *vedata)
144 {
145         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
146         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
147         const DRWContextState *draw_ctx = DRW_context_state_get();
148         const View3D *v3d = draw_ctx->v3d;
149         const RegionView3D *rv3d = draw_ctx->rv3d;
150         PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[PAINT_VERTEX_sh_data_index_from_rv3d(rv3d)];
151
152         if (!stl->g_data) {
153                 /* Alloc transient pointers */
154                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
155         }
156
157         {
158                 /* Create a pass */
159                 psl->vcolor_faces = DRW_pass_create(
160                         "Vert Color Pass",
161                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
162
163                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(sh_data->vcolor_face, psl->vcolor_faces);
164                 DRW_shgroup_uniform_float_copy(stl->g_data->fvcolor_shgrp, "white_factor", 1.0f - v3d->overlay.vertex_paint_mode_opacity);
165                 if (rv3d->rflag & RV3D_CLIPPING) {
166                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fvcolor_shgrp, rv3d);
167                 }
168         }
169
170         {
171                 psl->wire_overlay = DRW_pass_create(
172                         "Wire Pass",
173                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
174
175                 stl->g_data->lwire_shgrp = DRW_shgroup_create(sh_data->wire_overlay, psl->wire_overlay);
176                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", G_draw.block_ubo);
177                 if (rv3d->rflag & RV3D_CLIPPING) {
178                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_shgrp, rv3d);
179                 }
180         }
181
182         {
183                 psl->face_overlay = DRW_pass_create(
184                         "Face Mask Pass",
185                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
186
187                 stl->g_data->face_shgrp = DRW_shgroup_create(sh_data->face_overlay, psl->face_overlay);
188                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
189                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
190                 if (rv3d->rflag & RV3D_CLIPPING) {
191                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->face_shgrp, rv3d);
192                 }
193         }
194
195         {
196                 psl->vert_overlay = DRW_pass_create(
197                         "Vert Mask Pass",
198                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
199
200                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->vert_overlay, psl->vert_overlay);
201                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
202                 if (rv3d->rflag & RV3D_CLIPPING) {
203                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
204                 }
205         }
206 }
207
208 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
209 {
210         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
211         const DRWContextState *draw_ctx = DRW_context_state_get();
212         const View3D *v3d = draw_ctx->v3d;
213
214         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
215                 const Mesh *me = ob->data;
216                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
217                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
218                 const bool use_surface = v3d->overlay.vertex_paint_mode_opacity != 0.0f;
219                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
220                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
221                 struct GPUBatch *geom;
222
223                 if (me->mloopcol == NULL) {
224                         return;
225                 }
226
227                 if (use_surface) {
228                         geom = DRW_cache_mesh_surface_vertpaint_get(ob);
229                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
230                 }
231
232                 if (use_face_sel || use_wire) {
233                         geom = DRW_cache_mesh_surface_edges_get(ob);
234                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
235                 }
236
237                 if (use_face_sel) {
238                         geom = DRW_cache_mesh_surface_get(ob);
239                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
240                 }
241
242                 if (use_vert_sel) {
243                         geom = DRW_cache_mesh_all_verts_get(ob);
244                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
245                 }
246         }
247 }
248
249 static void PAINT_VERTEX_draw_scene(void *vedata)
250 {
251         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
252
253         DRW_draw_pass(psl->vcolor_faces);
254         DRW_draw_pass(psl->face_overlay);
255         DRW_draw_pass(psl->wire_overlay);
256         DRW_draw_pass(psl->vert_overlay);
257 }
258
259 static void PAINT_VERTEX_engine_free(void)
260 {
261         for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
262                 PAINT_VERTEX_Shaders *sh_data = &e_data.sh_data[sh_data_index];
263                 GPUShader **sh_data_as_array = (GPUShader **)sh_data;
264                 for (int i = 0; i < (sizeof(PAINT_VERTEX_Shaders) / sizeof(GPUShader *)); i++) {
265                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
266                 }
267         }
268 }
269
270 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
271
272 DrawEngineType draw_engine_paint_vertex_type = {
273         NULL, NULL,
274         N_("PaintVertexMode"),
275         &PAINT_VERTEX_data_size,
276         &PAINT_VERTEX_engine_init,
277         &PAINT_VERTEX_engine_free,
278         &PAINT_VERTEX_cache_init,
279         &PAINT_VERTEX_cache_populate,
280         NULL,
281         NULL,
282         &PAINT_VERTEX_draw_scene,
283         NULL,
284         NULL,
285 };