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