GPU: refactor clipped drawing from DRW into GPU
[blender.git] / source / blender / draw / modes / paint_weight_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 blender/draw/modes/paint_weight_mode.c
20  *  \ingroup draw
21  */
22
23 #include "DRW_render.h"
24
25 /* If builtin shaders are needed */
26 #include "GPU_shader.h"
27
28 #include "draw_common.h"
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_face_vert_glsl[];
38 extern char datatoc_paint_weight_vert_glsl[];
39 extern char datatoc_paint_weight_frag_glsl[];
40 extern char datatoc_paint_wire_vert_glsl[];
41 extern char datatoc_paint_wire_frag_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_WEIGHT_PassList {
50         struct DRWPass *weight_faces;
51         struct DRWPass *wire_overlay;
52         struct DRWPass *face_overlay;
53         struct DRWPass *vert_overlay;
54 } PAINT_WEIGHT_PassList;
55
56 typedef struct PAINT_WEIGHT_StorageList {
57         struct PAINT_WEIGHT_PrivateData *g_data;
58 } PAINT_WEIGHT_StorageList;
59
60 typedef struct PAINT_WEIGHT_Data {
61         void *engine_type;
62         DRWViewportEmptyList *fbl;
63         DRWViewportEmptyList *txl;
64         PAINT_WEIGHT_PassList *psl;
65         PAINT_WEIGHT_StorageList *stl;
66 } PAINT_WEIGHT_Data;
67
68 typedef struct PAINT_WEIGHT_Shaders {
69         struct GPUShader *weight_face;
70         struct GPUShader *wire_overlay;
71         struct GPUShader *face_overlay;
72         struct GPUShader *vert_overlay;
73 } PAINT_WEIGHT_Shaders;
74
75 /* *********** STATIC *********** */
76
77 static struct {
78         PAINT_WEIGHT_Shaders sh_data[GPU_SHADER_CFG_LEN];
79
80         int actdef;
81 } e_data = {NULL}; /* Engine data */
82
83 typedef struct PAINT_WEIGHT_PrivateData {
84         DRWShadingGroup *fweights_shgrp;
85         DRWShadingGroup *lwire_shgrp;
86         DRWShadingGroup *face_shgrp;
87         DRWShadingGroup *vert_shgrp;
88 } PAINT_WEIGHT_PrivateData;
89
90 /* *********** FUNCTIONS *********** */
91
92 static void PAINT_WEIGHT_engine_init(void *UNUSED(vedata))
93 {
94         const DRWContextState *draw_ctx = DRW_context_state_get();
95         PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
96         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
97
98         if (is_clip) {
99                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
100         }
101
102         if (!sh_data->weight_face) {
103                 const char *world_clip_lib_or_empty = is_clip ? datatoc_gpu_shader_cfg_world_clip_lib_glsl : "";
104                 const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
105
106                 sh_data->weight_face = GPU_shader_create_from_arrays({
107                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_weight_vert_glsl, NULL},
108                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_weight_frag_glsl, NULL},
109                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
110                 });
111
112                 sh_data->wire_overlay = GPU_shader_create_from_arrays({
113                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
114                         .frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
115                         .defs = (const char *[]){world_clip_def_or_empty, "#define WEIGHT_MODE\n", NULL},
116                 });
117
118                 sh_data->face_overlay = GPU_shader_create_from_arrays({
119                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_paint_face_vert_glsl, NULL},
120                         .frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
121                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
122                 });
123
124                 sh_data->vert_overlay = GPU_shader_create_from_arrays({
125                         .vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_wire_vert_glsl, NULL},
126                         .frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_vert_frag_glsl, NULL},
127                         .defs = (const char *[]){world_clip_def_or_empty, NULL},
128                 });
129         }
130 }
131
132 static void PAINT_WEIGHT_cache_init(void *vedata)
133 {
134         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
135         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
136         const DRWContextState *draw_ctx = DRW_context_state_get();
137         const View3D *v3d = draw_ctx->v3d;
138         RegionView3D *rv3d = draw_ctx->rv3d;
139         PAINT_WEIGHT_Shaders *sh_data = &e_data.sh_data[draw_ctx->shader_cfg];
140
141         if (!stl->g_data) {
142                 /* Alloc transient pointers */
143                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
144         }
145
146         {
147                 /* Create a pass */
148                 psl->weight_faces = DRW_pass_create(
149                         "Weight Pass",
150                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_MULTIPLY);
151
152                 stl->g_data->fweights_shgrp = DRW_shgroup_create(sh_data->weight_face, psl->weight_faces);
153
154                 DRW_shgroup_uniform_bool_copy(stl->g_data->fweights_shgrp, "drawContours", (v3d->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0);
155
156                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &v3d->overlay.weight_paint_mode_opacity, 1);
157                 DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", G_draw.weight_ramp);
158                 DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
159                 if (rv3d->rflag & RV3D_CLIPPING) {
160                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
161                 }
162         }
163
164         {
165                 psl->wire_overlay = DRW_pass_create(
166                         "Wire Pass",
167                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
168
169                 stl->g_data->lwire_shgrp = DRW_shgroup_create(sh_data->wire_overlay, psl->wire_overlay);
170                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", G_draw.block_ubo);
171                 if (rv3d->rflag & RV3D_CLIPPING) {
172                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lwire_shgrp, rv3d);
173                 }
174         }
175
176         {
177                 psl->face_overlay = DRW_pass_create(
178                         "Face Mask Pass",
179                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
180
181                 stl->g_data->face_shgrp = DRW_shgroup_create(sh_data->face_overlay, psl->face_overlay);
182
183                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
184                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
185                 if (rv3d->rflag & RV3D_CLIPPING) {
186                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->face_shgrp, rv3d);
187                 }
188         }
189
190         {
191                 psl->vert_overlay = DRW_pass_create(
192                         "Vert Mask Pass",
193                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
194
195                 stl->g_data->vert_shgrp = DRW_shgroup_create(sh_data->vert_overlay, psl->vert_overlay);
196                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", G_draw.block_ubo);
197                 if (rv3d->rflag & RV3D_CLIPPING) {
198                         DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vert_shgrp, rv3d);
199                 }
200         }
201 }
202
203 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
204 {
205         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
206         const DRWContextState *draw_ctx = DRW_context_state_get();
207         const View3D *v3d = draw_ctx->v3d;
208
209         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
210                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
211                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
212                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
213                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
214                 const bool use_vert_sel = (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
215                 struct GPUBatch *geom;
216
217                 if (use_surface) {
218                         geom = DRW_cache_mesh_surface_weights_get(ob);
219                         DRW_shgroup_call_add(stl->g_data->fweights_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_WEIGHT_draw_scene(void *vedata)
240 {
241         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
242
243         DRW_draw_pass(psl->weight_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_WEIGHT_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_WEIGHT_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_WEIGHT_Shaders) / sizeof(GPUShader *)); i++) {
255                         DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
256                 }
257         }
258 }
259
260 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
261
262 DrawEngineType draw_engine_paint_weight_type = {
263         NULL, NULL,
264         N_("PaintWeightMode"),
265         &PAINT_WEIGHT_data_size,
266         &PAINT_WEIGHT_engine_init,
267         &PAINT_WEIGHT_engine_free,
268         &PAINT_WEIGHT_cache_init,
269         &PAINT_WEIGHT_cache_populate,
270         NULL,
271         NULL,
272         &PAINT_WEIGHT_draw_scene,
273         NULL,
274         NULL,
275 };