1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2016, Blender Foundation.
*/
/** \file
* \ingroup draw
*/
#include "DRW_engine.h"
#include "DRW_render.h"
#include "BIF_gl.h"
#include "BKE_node.h"
#include "BLI_string_utils.h"
/* If builtin shaders are needed */
#include "GPU_shader.h"
#include "GPU_texture.h"
#include "draw_common.h"
#include "draw_mode_engines.h"
#include "DNA_mesh_types.h"
#include "DEG_depsgraph_query.h"
extern char datatoc_common_colormanagement_lib_glsl[];
extern char datatoc_common_globals_lib_glsl[];
extern char datatoc_common_view_lib_glsl[];
extern char datatoc_paint_texture_vert_glsl[];
extern char datatoc_paint_texture_frag_glsl[];
extern char datatoc_paint_wire_vert_glsl[];
extern char datatoc_paint_wire_frag_glsl[];
extern char datatoc_paint_face_vert_glsl[];
extern char datatoc_paint_face_selection_vert_glsl[];
extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
/* *********** LISTS *********** */
/* All lists are per viewport specific datas.
* They are all free when viewport changes engines
* or is free itself. Use PAINT_TEXTURE_engine_init() to
* initialize most of them and PAINT_TEXTURE_cache_init()
* for PAINT_TEXTURE_PassList */
typedef struct PAINT_TEXTURE_PassList {
/* Declare all passes here and init them in
* PAINT_TEXTURE_cache_init().
* Only contains (DRWPass *) */
struct DRWPass *image_faces;
struct DRWPass *wire_select_overlay;
struct DRWPass *face_select_overlay;
} PAINT_TEXTURE_PassList;
typedef struct PAINT_TEXTURE_FramebufferList {
/* Contains all framebuffer objects needed by this engine.
* Only contains (GPUFrameBuffer *) */
struct GPUFrameBuffer *fb;
} PAINT_TEXTURE_FramebufferList;
typedef struct PAINT_TEXTURE_TextureList {
/* Contains all framebuffer textures / utility textures
* needed by this engine. Only viewport specific textures
* (not per object). Only contains (GPUTexture *) */
struct GPUTexture *texture;
} PAINT_TEXTURE_TextureList;
typedef struct PAINT_TEXTURE_StorageList {
/* Contains any other memory block that the engine needs.
* Only directly MEM_(m/c)allocN'ed blocks because they are
* free with MEM_freeN() when viewport is freed.
* (not per object) */
struct CustomStruct *block;
struct PAINT_TEXTURE_PrivateData *g_data;
} PAINT_TEXTURE_StorageList;
typedef struct PAINT_TEXTURE_Data {
/* Struct returned by DRW_viewport_engine_data_ensure.
* If you don't use one of these, just make it a (void *) */
// void *fbl;
void *engine_type; /* Required */
PAINT_TEXTURE_FramebufferList *fbl;
PAINT_TEXTURE_TextureList *txl;
PAINT_TEXTURE_PassList *psl;
PAINT_TEXTURE_StorageList *stl;
} PAINT_TEXTURE_Data;
typedef struct PAINT_TEXTURE_Shaders {
/* Custom shaders :
* Add sources to source/blender/draw/modes/shaders
* init in PAINT_TEXTURE_engine_init();
* free in PAINT_TEXTURE_engine_free(); */
struct GPUShader *fallback;
struct GPUShader *image;
struct GPUShader *image_mask;
struct GPUShader *wire_select_overlay;
struct GPUShader *face_select_overlay;
} PAINT_TEXTURE_Shaders;
/* *********** STATIC *********** */
static struct {
PAINT_TEXTURE_Shaders sh_data[GPU_SHADER_CFG_LEN];
} e_data = {{{NULL}}}; /* Engine data */
typedef struct PAINT_TEXTURE_PrivateData {
/* This keeps the references of the shading groups for
* easy access in PAINT_TEXTURE_cache_populate() */
DRWShadingGroup *shgroup_fallback;
DRWShadingGroup **shgroup_image_array;
/* face-mask */
DRWShadingGroup *lwire_select_shgrp;
DRWShadingGroup *face_select_shgrp;
DRWView *view_wires;
} PAINT_TEXTURE_PrivateData; /* Transient data */
/* *********** FUNCTIONS *********** */
/* Init Textures, Framebuffers, Storage and Shaders.
* It is called for every frames. */
static void PAINT_TEXTURE_engine_init(void *vedata)
{
PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
const DRWContextState *draw_ctx = DRW_context_state_get();
PAINT_TEXTURE_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
if (!sh_data->fallback) {
const GPUShaderConfigData *sh_cfg_data = &GPU_shader_cfg_data[draw_ctx->sh_cfg];
sh_data->fallback = GPU_shader_create_from_arrays({
.vert = (const char *[]){sh_cfg_data->lib,
datatoc_common_view_lib_glsl,
datatoc_paint_face_vert_glsl,
NULL},
.frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
.defs = (const char *[]){sh_cfg_data->def, NULL},
});
sh_data->image = GPU_shader_create_from_arrays({
.vert = (const char *[]){sh_cfg_data->lib,
datatoc_common_globals_lib_glsl,
datatoc_common_view_lib_glsl,
datatoc_paint_texture_vert_glsl,
NULL},
.frag = (const char *[]){datatoc_common_colormanagement_lib_glsl,
datatoc_paint_texture_frag_glsl,
NULL},
.defs = (const char *[]){sh_cfg_data->def, NULL},
});
sh_data->image_mask = GPU_shader_create_from_arrays({
.vert = (const char *[]){sh_cfg_data->lib,
datatoc_common_globals_lib_glsl,
datatoc_common_view_lib_glsl,
datatoc_paint_texture_vert_glsl,
NULL},
.frag = (const char *[]){datatoc_paint_texture_frag_glsl, NULL},
.defs = (const char *[]){sh_cfg_data->def, "#define TEXTURE_PAINT_MASK\n", NULL},
});
sh_data->wire_select_overlay = GPU_shader_create_from_arrays({
.vert = (const char *[]){sh_cfg_data->lib,
datatoc_common_globals_lib_glsl,
datatoc_common_view_lib_glsl,
datatoc_paint_wire_vert_glsl,
NULL},
.frag = (const char *[]){datatoc_paint_wire_frag_glsl, NULL},
.defs = (const char *[]){sh_cfg_data->def, "#define USE_SELECT\n", NULL},
});
sh_data->face_select_overlay = GPU_shader_create_from_arrays({
.vert = (const char *[]){sh_cfg_data->lib,
datatoc_common_view_lib_glsl,
datatoc_paint_face_selection_vert_glsl,
NULL},
.frag = (const char *[]){datatoc_common_view_lib_glsl,
datatoc_gpu_shader_uniform_color_frag_glsl,
NULL},
.defs = (const char *[]){sh_cfg_data->def, NULL},
});
}
if (!stl->g_data) {
/* Alloc transient pointers */
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
stl->g_data->shgroup_image_array = NULL;
}
stl->g_data->view_wires = DRW_view_create_with_zoffset(draw_ctx->rv3d, 1.0f);
}
static DRWShadingGroup *create_texture_paint_shading_group(PAINT_TEXTURE_PassList *psl,
const Image *image,
const struct GPUTexture *texture,
const DRWContextState *draw_ctx,
const bool nearest_interp)
{
PAINT_TEXTURE_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
Scene *scene = draw_ctx->scene;
const ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
const bool masking_enabled = imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL &&
imapaint->stencil != NULL;
DRWShadingGroup *grp = DRW_shgroup_create(masking_enabled ? sh_data->image_mask : sh_data->image,
psl->image_faces);
DRW_shgroup_uniform_texture(grp, "image", texture);
DRW_shgroup_uniform_bool_copy(
grp, "imagePremultiplied", (image->alpha_mode == IMA_ALPHA_PREMUL));
DRW_shgroup_uniform_float(grp, "alpha", &draw_ctx->v3d->overlay.texture_paint_mode_opacity, 1);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_bool_copy(grp, "nearestInterp", nearest_interp);
if (masking_enabled) {
const bool masking_inverted = (imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV) > 0;
GPUTexture *stencil = GPU_texture_from_blender(imapaint->stencil, NULL, GL_TEXTURE_2D);
DRW_shgroup_uniform_texture(grp, "maskingImage", stencil);
DRW_shgroup_uniform_bool_copy(
grp, "maskingImagePremultiplied", (imapaint->stencil->alpha_mode == IMA_ALPHA_PREMUL));
DRW_shgroup_uniform_vec3(grp, "maskingColor", imapaint->stencil_col, 1);
DRW_shgroup_uniform_bool_copy(grp, "maskingInvertStencil", masking_inverted);
}
return grp;
}
/* Here init all passes and shading groups
* Assume that all Passes are NULL */
static void PAINT_TEXTURE_cache_init(void *vedata)
{
PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
const DRWContextState *draw_ctx = DRW_context_state_get();
PAINT_TEXTURE_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg];
/* Create a pass */
{
DRWPass *pass = DRW_pass_create(
"Image Color Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND_ALPHA);
DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->fallback, pass);
/* Uniforms need a pointer to it's value so be sure it's accessible at
* any given time (i.e. use static vars) */
static const float color[4] = {1.0f, 0.0f, 1.0f, 1.0};
DRW_shgroup_uniform_vec4(shgrp, "color", color, 1);
if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
}
psl->image_faces = pass;
stl->g_data->shgroup_fallback = shgrp;
}
MEM_SAFE_FREE(stl->g_data->shgroup_image_array);
Object *ob = draw_ctx->obact;
if (ob && ob->type == OB_MESH) {
Scene *scene = draw_ctx->scene;
const ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
const bool use_material_slots = (imapaint->mode == IMAGEPAINT_MODE_MATERIAL);
const Mesh *me = ob->data;
const int mat_nr = max_ii(1, me->totcol);
stl->g_data->shgroup_image_array = MEM_mallocN(
sizeof(*stl->g_data->shgroup_image_array) * (use_material_slots ? mat_nr : 1), __func__);
if (use_material_slots) {
for (int i = 0; i < mat_nr; i++) {
Material *ma = give_current_material(ob, i + 1);
Image *ima = (ma && ma->texpaintslot) ? ma->texpaintslot[ma->paint_active_slot].ima : NULL;
int interp = (ma && ma->texpaintslot) ? ma->texpaintslot[ma->paint_active_slot].interp : 0;
GPUTexture *tex = GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D);
if (tex) {
DRWShadingGroup *grp = create_texture_paint_shading_group(
psl, ima, tex, draw_ctx, interp == SHD_INTERP_CLOSEST);
stl->g_data->shgroup_image_array[i] = grp;
}
else {
stl->g_data->shgroup_image_array[i] = NULL;
}
}
}
else {
Image *ima = imapaint->canvas;
GPUTexture *tex = GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D);
if (tex) {
DRWShadingGroup *grp = create_texture_paint_shading_group(
psl, ima, tex, draw_ctx, imapaint->interp == IMAGEPAINT_INTERP_CLOSEST);
stl->g_data->shgroup_image_array[0] = grp;
}
else {
stl->g_data->shgroup_image_array[0] = NULL;
}
}
}
/* Face Mask */
{
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
DRWPass *pass = DRW_pass_create("Wire Mask Pass", state);
DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->wire_select_overlay, pass);
DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
}
psl->wire_select_overlay = pass;
stl->g_data->lwire_select_shgrp = shgrp;
}
{
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
DRW_STATE_BLEND_ALPHA;
DRWPass *pass = DRW_pass_create("Face Mask Pass", state);
DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->face_select_overlay, pass);
static const float col[4] = {1.0f, 1.0f, 1.0f, 0.2f};
DRW_shgroup_uniform_vec4(shgrp, "color", col, 1);
if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
DRW_shgroup_state_enable(shgrp, DRW_STATE_CLIP_PLANES);
}
psl->face_select_overlay = pass;
stl->g_data->face_select_shgrp = shgrp;
}
}
/* Add geometry to shadingGroups. Execute for each objects */
static void PAINT_TEXTURE_cache_populate(void *vedata, Object *ob)
{
PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
const DRWContextState *draw_ctx = DRW_context_state_get();
UNUSED_VARS(psl, stl);
if ((ob->type == OB_MESH) && (draw_ctx->obact == ob)) {
/* Get geometry cache */
const Mesh *me = ob->data;
const Mesh *me_orig = DEG_get_original_object(ob)->data;
Scene *scene = draw_ctx->scene;
const bool use_surface = draw_ctx->v3d->overlay.texture_paint_mode_opacity !=
0.0; // DRW_object_is_mode_shade(ob) == true;
const bool use_material_slots = (scene->toolsettings->imapaint.mode ==
IMAGEPAINT_MODE_MATERIAL);
const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
if (use_surface) {
if (me->mloopuv != NULL) {
if (use_material_slots) {
int mat_nr = max_ii(1, me->totcol);
struct GPUBatch **geom_array = DRW_cache_mesh_surface_texpaint_get(ob);
for (int i = 0; i < mat_nr; i++) {
const int index = use_material_slots ? i : 0;
if ((i < me->totcol) && stl->g_data->shgroup_image_array[index]) {
DRW_shgroup_call(stl->g_data->shgroup_image_array[index], geom_array[i], ob);
}
else {
DRW_shgroup_call(stl->g_data->shgroup_fallback, geom_array[i], ob);
}
}
}
else {
if (stl->g_data->shgroup_image_array[0]) {
struct GPUBatch *geom = DRW_cache_mesh_surface_texpaint_single_get(ob);
DRW_shgroup_call(stl->g_data->shgroup_image_array[0], geom, ob);
}
}
}
else {
struct GPUBatch *geom = DRW_cache_mesh_surface_get(ob);
DRW_shgroup_call(stl->g_data->shgroup_fallback, geom, ob);
}
}
/* Face Mask */
if (use_face_sel) {
struct GPUBatch *geom;
geom = DRW_cache_mesh_surface_edges_get(ob);
DRW_shgroup_call(stl->g_data->lwire_select_shgrp, geom, ob);
geom = DRW_cache_mesh_surface_get(ob);
DRW_shgroup_call(stl->g_data->face_select_shgrp, geom, ob);
}
}
}
/* Optional: Post-cache_populate callback */
static void PAINT_TEXTURE_cache_finish(void *vedata)
{
PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
/* Do something here! dependent on the objects gathered */
UNUSED_VARS(psl);
MEM_SAFE_FREE(stl->g_data->shgroup_image_array);
}
/* Draw time ! Control rendering pipeline from here */
static void PAINT_TEXTURE_draw_scene(void *vedata)
{
PAINT_TEXTURE_PassList *psl = ((PAINT_TEXTURE_Data *)vedata)->psl;
PAINT_TEXTURE_FramebufferList *fbl = ((PAINT_TEXTURE_Data *)vedata)->fbl;
PAINT_TEXTURE_StorageList *stl = ((PAINT_TEXTURE_Data *)vedata)->stl;
/* Default framebuffer and texture */
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
UNUSED_VARS(fbl, dfbl, dtxl);
DRW_draw_pass(psl->image_faces);
DRW_draw_pass(psl->face_select_overlay);
DRW_view_set_active(stl->g_data->view_wires);
DRW_draw_pass(psl->wire_select_overlay);
DRW_view_set_active(NULL);
}
/* Cleanup when destroying the engine.
* This is not per viewport ! only when quitting blender.
* Mostly used for freeing shaders */
static void PAINT_TEXTURE_engine_free(void)
{
for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
PAINT_TEXTURE_Shaders *sh_data = &e_data.sh_data[sh_data_index];
GPUShader **sh_data_as_array = (GPUShader **)sh_data;
for (int i = 0; i < (sizeof(PAINT_TEXTURE_Shaders) / sizeof(GPUShader *)); i++) {
DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
}
}
}
static const DrawEngineDataSize PAINT_TEXTURE_data_size = DRW_VIEWPORT_DATA_SIZE(
PAINT_TEXTURE_Data);
DrawEngineType draw_engine_paint_texture_type = {
NULL,
NULL,
N_("PaintTextureMode"),
&PAINT_TEXTURE_data_size,
&PAINT_TEXTURE_engine_init,
&PAINT_TEXTURE_engine_free,
&PAINT_TEXTURE_cache_init,
&PAINT_TEXTURE_cache_populate,
&PAINT_TEXTURE_cache_finish,
NULL, /* draw_background but not needed by mode engines */
&PAINT_TEXTURE_draw_scene,
NULL,
NULL,
};
|