DRW: Cleanup / Renaming of mesh batch cache functions
[blender.git] / source / blender / draw / modes / paint_texture_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_texture_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "BIF_gl.h"
30
31 /* If builtin shaders are needed */
32 #include "GPU_shader.h"
33 #include "GPU_texture.h"
34
35 #include "draw_common.h"
36
37 #include "draw_mode_engines.h"
38
39 #include "DNA_mesh_types.h"
40
41 #include "DEG_depsgraph_query.h"
42
43 extern char datatoc_common_globals_lib_glsl[];
44 extern char datatoc_paint_texture_vert_glsl[];
45 extern char datatoc_paint_texture_frag_glsl[];
46 extern char datatoc_paint_wire_vert_glsl[];
47 extern char datatoc_paint_wire_frag_glsl[];
48 extern char datatoc_paint_face_vert_glsl[];
49
50 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
51
52 /* If needed, contains all global/Theme colors
53  * Add needed theme colors / values to DRW_globals_update() and update UBO
54  * Not needed for constant color. */
55 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
56 extern struct GlobalsUboStorage ts; /* draw_common.c */
57
58 /* *********** LISTS *********** */
59 /* All lists are per viewport specific datas.
60  * They are all free when viewport changes engines
61  * or is free itself. Use PAINT_TEXTURE_engine_init() to
62  * initialize most of them and PAINT_TEXTURE_cache_init()
63  * for PAINT_TEXTURE_PassList */
64
65 typedef struct PAINT_TEXTURE_PassList {
66         /* Declare all passes here and init them in
67          * PAINT_TEXTURE_cache_init().
68          * Only contains (DRWPass *) */
69         struct DRWPass *image_faces;
70
71         struct DRWPass *wire_overlay;
72         struct DRWPass *face_overlay;
73 } PAINT_TEXTURE_PassList;
74
75 typedef struct PAINT_TEXTURE_FramebufferList {
76         /* Contains all framebuffer objects needed by this engine.
77          * Only contains (GPUFrameBuffer *) */
78         struct GPUFrameBuffer *fb;
79 } PAINT_TEXTURE_FramebufferList;
80
81 typedef struct PAINT_TEXTURE_TextureList {
82         /* Contains all framebuffer textures / utility textures
83          * needed by this engine. Only viewport specific textures
84          * (not per object). Only contains (GPUTexture *) */
85         struct GPUTexture *texture;
86 } PAINT_TEXTURE_TextureList;
87
88 typedef struct PAINT_TEXTURE_StorageList {
89         /* Contains any other memory block that the engine needs.
90          * Only directly MEM_(m/c)allocN'ed blocks because they are
91          * free with MEM_freeN() when viewport is freed.
92          * (not per object) */
93         struct CustomStruct *block;
94         struct PAINT_TEXTURE_PrivateData *g_data;
95 } PAINT_TEXTURE_StorageList;
96
97 typedef struct PAINT_TEXTURE_Data {
98         /* Struct returned by DRW_viewport_engine_data_ensure.
99          * If you don't use one of these, just make it a (void *) */
100         // void *fbl;
101         void *engine_type; /* Required */
102         PAINT_TEXTURE_FramebufferList *fbl;
103         PAINT_TEXTURE_TextureList *txl;
104         PAINT_TEXTURE_PassList *psl;
105         PAINT_TEXTURE_StorageList *stl;
106 } PAINT_TEXTURE_Data;
107
108 /* *********** STATIC *********** */
109
110 static struct {
111         /* Custom shaders :
112          * Add sources to source/blender/draw/modes/shaders
113          * init in PAINT_TEXTURE_engine_init();
114          * free in PAINT_TEXTURE_engine_free(); */
115         struct GPUShader *fallback_sh;
116         struct GPUShader *image_sh;
117
118         struct GPUShader *wire_overlay_shader;
119         struct GPUShader *face_overlay_shader;
120 } e_data = {NULL}; /* Engine data */
121
122 typedef struct PAINT_TEXTURE_PrivateData {
123         /* This keeps the references of the shading groups for
124          * easy access in PAINT_TEXTURE_cache_populate() */
125         DRWShadingGroup *shgroup_fallback;
126         DRWShadingGroup **shgroup_image_array;
127
128         /* face-mask  */
129         DRWShadingGroup *lwire_shgrp;
130         DRWShadingGroup *face_shgrp;
131 } PAINT_TEXTURE_PrivateData; /* Transient data */
132
133 /* *********** FUNCTIONS *********** */
134
135 /* Init Textures, Framebuffers, Storage and Shaders.
136  * It is called for every frames. */
137 static void PAINT_TEXTURE_engine_init(void *UNUSED(vedata))
138 {
139         if (!e_data.fallback_sh) {
140                 e_data.fallback_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
141
142                 e_data.image_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
143
144                 e_data.image_sh = DRW_shader_create_with_lib(
145                         datatoc_paint_texture_vert_glsl, NULL,
146                         datatoc_paint_texture_frag_glsl,
147                         datatoc_common_globals_lib_glsl, NULL);
148
149                 e_data.wire_overlay_shader = DRW_shader_create_with_lib(
150                         datatoc_paint_wire_vert_glsl, NULL,
151                         datatoc_paint_wire_frag_glsl,
152                         datatoc_common_globals_lib_glsl,
153                         "#define VERTEX_MODE\n");
154
155                 e_data.face_overlay_shader = DRW_shader_create(
156                         datatoc_paint_face_vert_glsl, NULL,
157                         datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
158         }
159 }
160
161 /* Here init all passes and shading groups
162  * Assume that all Passes are NULL */
163 static void PAINT_TEXTURE_cache_init(void *vedata)
164 {
165         PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
166         PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
167
168         if (!stl->g_data) {
169                 /* Alloc transient pointers */
170                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
171                 stl->g_data->shgroup_image_array = NULL;
172         }
173
174         {
175                 /* Create a pass */
176                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND;
177                 psl->image_faces = DRW_pass_create("Image Color Pass", state);
178
179                 stl->g_data->shgroup_fallback = DRW_shgroup_create(e_data.fallback_sh, psl->image_faces);
180
181                 /* Uniforms need a pointer to it's value so be sure it's accessible at
182                  * any given time (i.e. use static vars) */
183                 static float color[4] = {1.0f, 0.0f, 1.0f, 1.0};
184                 DRW_shgroup_uniform_vec4(stl->g_data->shgroup_fallback, "color", color, 1);
185
186                 MEM_SAFE_FREE(stl->g_data->shgroup_image_array);
187
188                 const DRWContextState *draw_ctx = DRW_context_state_get();
189                 Object *ob = draw_ctx->obact;
190                 if (ob && ob->type == OB_MESH) {
191                         Scene *scene = draw_ctx->scene;
192                         const bool use_material_slots = (scene->toolsettings->imapaint.mode == IMAGEPAINT_MODE_MATERIAL);
193                         const Mesh *me = ob->data;
194
195                         stl->g_data->shgroup_image_array = MEM_mallocN(
196                                 sizeof(*stl->g_data->shgroup_image_array) * (use_material_slots ? me->totcol : 1), __func__);
197
198                         if (use_material_slots) {
199                                 for (int i = 0; i < me->totcol; i++) {
200                                         Material *ma = give_current_material(ob, i + 1);
201                                         Image *ima = (ma && ma->texpaintslot) ? ma->texpaintslot[ma->paint_active_slot].ima : NULL;
202                                         GPUTexture *tex = ima ?
203                                                 GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D, false, 0.0f) : NULL;
204
205                                         if (tex) {
206                                                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.image_sh, psl->image_faces);
207                                                 DRW_shgroup_uniform_texture(grp, "image", tex);
208                                                 DRW_shgroup_uniform_float(grp, "alpha", &draw_ctx->v3d->overlay.texture_paint_mode_opacity, 1);
209                                                 DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
210                                                 stl->g_data->shgroup_image_array[i] = grp;
211                                         }
212                                         else {
213                                                 stl->g_data->shgroup_image_array[i] = NULL;
214                                         }
215                                 }
216                         }
217                         else {
218                                 Image *ima = scene->toolsettings->imapaint.canvas;
219                                 GPUTexture *tex = ima ?
220                                         GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D, false, 0.0f) : NULL;
221
222                                 if (tex) {
223                                         DRWShadingGroup *grp = DRW_shgroup_create(e_data.image_sh, psl->image_faces);
224                                         DRW_shgroup_uniform_texture(grp, "image", tex);
225                                         DRW_shgroup_uniform_float(grp, "alpha", &draw_ctx->v3d->overlay.texture_paint_mode_opacity, 1);
226                                         DRW_shgroup_uniform_block(grp, "globalsBlock", globals_ubo);
227                                         stl->g_data->shgroup_image_array[0] = grp;
228                                 }
229                                 else {
230                                         stl->g_data->shgroup_image_array[0] = NULL;
231                                 }
232                         }
233                 }
234         }
235
236         /* Face Mask */
237         {
238                 psl->wire_overlay = DRW_pass_create(
239                         "Wire Pass",
240                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
241
242                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
243                 DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
244         }
245
246         {
247                 psl->face_overlay = DRW_pass_create(
248                         "Face Mask Pass",
249                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
250
251                 stl->g_data->face_shgrp = DRW_shgroup_create(e_data.face_overlay_shader, psl->face_overlay);
252
253                 static float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
254                 DRW_shgroup_uniform_vec4(stl->g_data->face_shgrp, "color", col, 1);
255         }
256 }
257
258 /* Add geometry to shadingGroups. Execute for each objects */
259 static void PAINT_TEXTURE_cache_populate(void *vedata, Object *ob)
260 {
261         PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
262         PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
263         const DRWContextState *draw_ctx = DRW_context_state_get();
264
265         UNUSED_VARS(psl, stl);
266
267         if ((ob->type == OB_MESH) && (draw_ctx->obact == ob)) {
268                 /* Get geometry cache */
269                 const Mesh *me = ob->data;
270                 const Mesh *me_orig = DEG_get_original_object(ob)->data;
271                 Scene *scene = draw_ctx->scene;
272                 const bool use_surface = draw_ctx->v3d->overlay.texture_paint_mode_opacity != 0.0; //DRW_object_is_mode_shade(ob) == true;
273                 const bool use_material_slots = (scene->toolsettings->imapaint.mode == IMAGEPAINT_MODE_MATERIAL);
274                 const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
275
276                 if (use_surface) {
277                         if (me->mloopuv != NULL) {
278                                 if (use_material_slots) {
279                                         int mat_nr = max_ii(1, me->totcol);
280                                         struct GPUBatch **geom_array = DRW_cache_mesh_surface_texpaint_get(ob);
281
282                                         for (int i = 0; i < mat_nr; i++) {
283                                                 const int index = use_material_slots ? i : 0;
284                                                 if (stl->g_data->shgroup_image_array[index]) {
285                                                         DRW_shgroup_call_add(stl->g_data->shgroup_image_array[index], geom_array[i], ob->obmat);
286                                                 }
287                                                 else {
288                                                         DRW_shgroup_call_add(stl->g_data->shgroup_fallback, geom_array[i], ob->obmat);
289                                                 }
290                                         }
291                                 }
292                                 else {
293                                         if (stl->g_data->shgroup_image_array[0]) {
294                                                 struct GPUBatch *geom = DRW_cache_mesh_surface_texpaint_single_get(ob);
295                                                 DRW_shgroup_call_add(stl->g_data->shgroup_image_array[0], geom, ob->obmat);
296                                         }
297                                 }
298                         }
299                         else {
300                                 struct GPUBatch *geom = DRW_cache_mesh_surface_get(ob);
301                                 DRW_shgroup_call_add(stl->g_data->shgroup_fallback, geom, ob->obmat);
302                         }
303                 }
304
305                 /* Face Mask */
306                 if (use_face_sel) {
307                         struct GPUBatch *geom;
308                         geom = DRW_cache_mesh_surface_edges_get(ob);
309                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
310
311                         geom = DRW_cache_mesh_surface_get(ob);
312                         DRW_shgroup_call_add(stl->g_data->face_shgrp, geom, ob->obmat);
313                 }
314         }
315 }
316
317 /* Optional: Post-cache_populate callback */
318 static void PAINT_TEXTURE_cache_finish(void *vedata)
319 {
320         PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
321         PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
322
323         /* Do something here! dependent on the objects gathered */
324         UNUSED_VARS(psl);
325
326         MEM_SAFE_FREE(stl->g_data->shgroup_image_array);
327 }
328
329 /* Draw time ! Control rendering pipeline from here */
330 static void PAINT_TEXTURE_draw_scene(void *vedata)
331 {
332         PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
333         PAINT_TEXTURE_FramebufferList *fbl = ((PAINT_TEXTURE_Data *)vedata)->fbl;
334
335         /* Default framebuffer and texture */
336         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
337         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
338
339         UNUSED_VARS(fbl, dfbl, dtxl);
340
341         DRW_draw_pass(psl->image_faces);
342
343         DRW_draw_pass(psl->face_overlay);
344         DRW_draw_pass(psl->wire_overlay);
345 }
346
347 /* Cleanup when destroying the engine.
348  * This is not per viewport ! only when quitting blender.
349  * Mostly used for freeing shaders */
350 static void PAINT_TEXTURE_engine_free(void)
351 {
352         DRW_SHADER_FREE_SAFE(e_data.image_sh);
353         DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
354         DRW_SHADER_FREE_SAFE(e_data.face_overlay_shader);
355 }
356
357 static const DrawEngineDataSize PAINT_TEXTURE_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_TEXTURE_Data);
358
359 DrawEngineType draw_engine_paint_texture_type = {
360         NULL, NULL,
361         N_("PaintTextureMode"),
362         &PAINT_TEXTURE_data_size,
363         &PAINT_TEXTURE_engine_init,
364         &PAINT_TEXTURE_engine_free,
365         &PAINT_TEXTURE_cache_init,
366         &PAINT_TEXTURE_cache_populate,
367         &PAINT_TEXTURE_cache_finish,
368         NULL, /* draw_background but not needed by mode engines */
369         &PAINT_TEXTURE_draw_scene,
370         NULL,
371         NULL,
372 };