Paint Vertex Mode: Fix shader not free.
[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
38 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
39 extern struct GlobalsUboStorage ts; /* draw_common.c */
40
41 extern char datatoc_paint_vertex_vert_glsl[];
42 extern char datatoc_paint_vertex_frag_glsl[];
43 extern char datatoc_paint_wire_vert_glsl[];
44 extern char datatoc_paint_wire_frag_glsl[];
45 extern char datatoc_common_globals_lib_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 } PAINT_VERTEX_PassList;
54
55 typedef struct PAINT_VERTEX_StorageList {
56         struct PAINT_VERTEX_PrivateData *g_data;
57 } PAINT_VERTEX_StorageList;
58
59 typedef struct PAINT_VERTEX_Data {
60         void *engine_type; /* Required */
61         DRWViewportEmptyList *fbl;
62         DRWViewportEmptyList *txl;
63         PAINT_VERTEX_PassList *psl;
64         PAINT_VERTEX_StorageList *stl;
65 } PAINT_VERTEX_Data;
66
67 /* *********** STATIC *********** */
68
69 static struct {
70         struct GPUShader *vcolor_face_shader;
71         struct GPUShader *wire_overlay_shader;
72         struct GPUShader *face_overlay_shader;
73 } e_data = {NULL}; /* Engine data */
74
75 typedef struct PAINT_VERTEX_PrivateData {
76         DRWShadingGroup *fvcolor_shgrp;
77         DRWShadingGroup *lwire_shgrp;
78         DRWShadingGroup *face_shgrp;
79 } PAINT_VERTEX_PrivateData; /* Transient data */
80
81 /* *********** FUNCTIONS *********** */
82
83 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
84 {
85         if (!e_data.vcolor_face_shader) {
86                 e_data.vcolor_face_shader = DRW_shader_create(
87                         datatoc_paint_vertex_vert_glsl, NULL,
88                         datatoc_paint_vertex_frag_glsl, NULL);
89
90                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
91                         datatoc_paint_wire_vert_glsl, NULL,
92                         datatoc_paint_wire_frag_glsl,
93                         datatoc_common_globals_lib_glsl, "#define VERTEX_MODE\n");
94
95                 e_data.face_overlay_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
96         }
97 }
98
99 static void PAINT_VERTEX_cache_init(void *vedata)
100 {
101         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
102         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
103
104         if (!stl->g_data) {
105                 /* Alloc transient pointers */
106                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
107         }
108
109         {
110                 /* Create a pass */
111                 psl->vcolor_faces = DRW_pass_create(
112                         "Vert Color Pass",
113                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_MULTIPLY);
114
115                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
116         }
117
118         {
119                 psl->wire_overlay = DRW_pass_create(
120                         "Wire Pass",
121                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
122
123                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
124         }
125
126         {
127                 psl->face_overlay = DRW_pass_create(
128                         "Face Mask Pass",
129                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND);
130
131                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
132
133                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
134                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
135         }
136 }
137
138 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
139 {
140         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
141         const DRWContextState *draw_ctx = DRW_context_state_get();
142
143         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
144                 IDProperty *ces_mode_pw = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_PAINT_VERTEX, "");
145                 const Mesh *me = ob->data;
146                 const bool use_wire = BKE_collection_engine_property_value_get_bool(ces_mode_pw, "use_wire");
147                 const bool use_surface = DRW_object_is_mode_shade(ob) == true;
148                 const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
149                 struct Gwn_Batch *geom;
150
151                 if (use_surface) {
152                         geom = DRW_cache_mesh_surface_vert_colors_get(ob);
153                         DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
154                 }
155
156                 if (use_face_sel || use_wire) {
157                         geom = DRW_cache_mesh_edges_paint_overlay_get(ob, use_wire, use_face_sel);
158                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
159                 }
160
161                 if (use_face_sel) {
162                         geom = DRW_cache_mesh_faces_weight_overlay_get(ob);
163                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
164                 }
165         }
166 }
167
168 static void PAINT_VERTEX_draw_scene(void *vedata)
169 {
170         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
171
172         DRW_draw_pass(psl->vcolor_faces);
173         DRW_draw_pass(psl->face_overlay);
174         DRW_draw_pass(psl->wire_overlay);
175 }
176
177 static void PAINT_VERTEX_engine_free(void)
178 {
179         DRW_SHADER_FREE_SAFE(e_data.vcolor_face_shader);
180         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
181 }
182
183 void PAINT_VERTEX_collection_settings_create(IDProperty *properties)
184 {
185         BLI_assert(properties &&
186                    properties->type == IDP_GROUP &&
187                    properties->subtype == IDP_GROUP_SUB_MODE_PAINT_VERTEX);
188
189         BKE_collection_engine_property_add_bool(properties, "use_wire", false);
190 }
191
192 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
193
194 DrawEngineType draw_engine_paint_vertex_type = {
195         NULL, NULL,
196         N_("PaintVertexMode"),
197         &PAINT_VERTEX_data_size,
198         &PAINT_VERTEX_engine_init,
199         &PAINT_VERTEX_engine_free,
200         &PAINT_VERTEX_cache_init,
201         &PAINT_VERTEX_cache_populate,
202         NULL,
203         NULL,
204         &PAINT_VERTEX_draw_scene,
205         NULL,
206         NULL,
207 };