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