Fix T56657: Crash when creating a collection instance containing probes
[blender.git] / source / blender / draw / modes / paint_weight_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_weight_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 #include "DNA_view3d_types.h"
38
39 #include "BKE_mesh.h"
40
41 #include "DEG_depsgraph_query.h"
42
43 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
44 extern struct GlobalsUboStorage ts; /* draw_common.c */
45
46 extern char datatoc_paint_wire_vert_glsl[];
47 extern char datatoc_paint_wire_frag_glsl[];
48 extern char datatoc_paint_vert_frag_glsl[];
49 extern char datatoc_common_globals_lib_glsl[];
50
51 /* *********** LISTS *********** */
52
53 typedef struct PAINT_WEIGHT_PassList {
54         struct DRWPass *weight_faces;
55         struct DRWPass *wire_overlay;
56         struct DRWPass *face_overlay;
57         struct DRWPass *vert_overlay;
58 } PAINT_WEIGHT_PassList;
59
60 typedef struct PAINT_WEIGHT_StorageList {
61         struct PAINT_WEIGHT_PrivateData *g_data;
62 } PAINT_WEIGHT_StorageList;
63
64 typedef struct PAINT_WEIGHT_Data {
65         void *engine_type;
66         DRWViewportEmptyList *fbl;
67         DRWViewportEmptyList *txl;
68         PAINT_WEIGHT_PassList *psl;
69         PAINT_WEIGHT_StorageList *stl;
70 } PAINT_WEIGHT_Data;
71
72 /* *********** STATIC *********** */
73
74 static struct {
75         struct GPUShader *weight_face_shader;
76         struct GPUShader *wire_overlay_shader;
77         struct GPUShader *face_overlay_shader;
78         struct GPUShader *vert_overlay_shader;
79         int actdef;
80 } e_data = {NULL}; /* Engine data */
81
82 typedef struct PAINT_WEIGHT_PrivateData {
83         DRWShadingGroup *fweights_shgrp;
84         DRWShadingGroup *lwire_shgrp;
85         DRWShadingGroup *face_shgrp;
86         DRWShadingGroup *vert_shgrp;
87 } PAINT_WEIGHT_PrivateData;
88
89 /* *********** FUNCTIONS *********** */
90
91 static void PAINT_WEIGHT_engine_init(void *UNUSED(vedata))
92 {
93         if (!e_data.weight_face_shader) {
94                 e_data.weight_face_shader = GPU_shader_get_builtin_shader(GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA);
95         }
96
97         if (!e_data.wire_overlay_shader) {
98                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
99                         datatoc_paint_wire_vert_glsl, NULL,
100                         datatoc_paint_wire_frag_glsl,
101                         datatoc_common_globals_lib_glsl, "#define WEIGHT_MODE\n");
102         }
103
104         if (!e_data.face_overlay_shader) {
105                 e_data.face_overlay_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
106         }
107
108         if (!e_data.vert_overlay_shader) {
109                 e_data.vert_overlay_shader = DRW_shader_create_with_lib(
110                         datatoc_paint_wire_vert_glsl, NULL,
111                         datatoc_paint_vert_frag_glsl,
112                         datatoc_common_globals_lib_glsl, NULL);
113         }
114 }
115
116 static void PAINT_WEIGHT_cache_init(void *vedata)
117 {
118         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
119         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
120         const DRWContextState *draw_ctx = DRW_context_state_get();
121         const View3D *v3d = draw_ctx->v3d;
122
123         if (!stl->g_data) {
124                 /* Alloc transient pointers */
125                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
126         }
127
128         {
129                 /* Create a pass */
130                 psl->weight_faces = DRW_pass_create(
131                         "Weight Pass",
132                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND);
133
134                 stl->g_data->fweights_shgrp = DRW_shgroup_create(e_data.weight_face_shader, psl->weight_faces);
135
136                 static float light[3] = {-0.3f, 0.5f, 1.0f};
137                 static float world_light = 1.0f;
138                 DRW_shgroup_uniform_vec3(stl->g_data->fweights_shgrp, "light", light, 1);
139                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "alpha", &v3d->overlay.weight_paint_mode_opacity, 1);
140                 DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "global", &world_light, 1);
141         }
142
143         {
144                 psl->wire_overlay = DRW_pass_create(
145                         "Wire Pass",
146                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
147
148                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
149                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
150         }
151
152         {
153                 psl->face_overlay = DRW_pass_create(
154                         "Face Mask Pass",
155                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
156
157                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
158
159                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
160                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
161         }
162
163         {
164                 psl->vert_overlay = DRW_pass_create(
165                         "Vert Mask Pass",
166                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
167
168                 stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.vert_overlay_shader, psl->vert_overlay);
169                 DRW_shgroup_uniform_block(stl->g_data->vert_shgrp, "globalsBlock", globals_ubo);
170         }
171 }
172
173 static void PAINT_WEIGHT_cache_populate(void *vedata, Object *ob)
174 {
175         PAINT_WEIGHT_StorageList *stl = ((PAINT_WEIGHT_Data *)vedata)->stl;
176         const DRWContextState *draw_ctx = DRW_context_state_get();
177         const View3D *v3d = draw_ctx->v3d;
178
179         if ((ob->type == OB_MESH) && (ob == draw_ctx->obact)) {
180                 /* We're always painting on original, display original data. */
181                 ob = DEG_get_original_object(ob);
182                 const Mesh *me = ob->data;
183                 const bool use_wire = (v3d->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE) != 0;
184                 const bool use_surface = v3d->overlay.weight_paint_mode_opacity != 0.0f;
185                 const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
186                 const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
187                 struct GPUBatch *geom;
188
189                 if (use_surface) {
190                         geom = DRW_cache_mesh_surface_weights_get(ob);
191                         DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
192                 }
193
194                 if (use_face_sel || use_wire) {
195                         geom = DRW_cache_mesh_edges_paint_overlay_get(ob, use_wire, use_face_sel);
196                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
197                 }
198
199                 if (use_face_sel) {
200                         geom = DRW_cache_mesh_faces_weight_overlay_get(ob);
201                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
202                 }
203
204                 if (use_vert_sel) {
205                         geom = DRW_cache_mesh_verts_weight_overlay_get(ob);
206                         DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
207                 }
208         }
209 }
210
211 static void PAINT_WEIGHT_draw_scene(void *vedata)
212 {
213         PAINT_WEIGHT_PassList *psl = ((PAINT_WEIGHT_Data *)vedata)->psl;
214
215         DRW_draw_pass(psl->weight_faces);
216         DRW_draw_pass(psl->face_overlay);
217         DRW_draw_pass(psl->wire_overlay);
218         DRW_draw_pass(psl->vert_overlay);
219 }
220
221 static void PAINT_WEIGHT_engine_free(void)
222 {
223         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
224         DRW_SHADER_FREE_SAFE(e_data.vert_overlay_shader);
225 }
226
227 static const DrawEngineDataSize PAINT_WEIGHT_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_WEIGHT_Data);
228
229 DrawEngineType draw_engine_paint_weight_type = {
230         NULL, NULL,
231         N_("PaintWeightMode"),
232         &PAINT_WEIGHT_data_size,
233         &PAINT_WEIGHT_engine_init,
234         &PAINT_WEIGHT_engine_free,
235         &PAINT_WEIGHT_cache_init,
236         &PAINT_WEIGHT_cache_populate,
237         NULL,
238         NULL,
239         &PAINT_WEIGHT_draw_scene,
240         NULL,
241         NULL,
242 };