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