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