Workbench: Fix memleaks.
[blender.git] / source / blender / draw / engines / workbench / workbench_forward.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 workbench_forward.c
23  *  \ingroup draw_engine
24  */
25
26 #include "workbench_private.h"
27
28 #include "BIF_gl.h"
29
30 #include "BLI_alloca.h"
31 #include "BLI_dynstr.h"
32 #include "BLI_utildefines.h"
33
34 #include "BKE_node.h"
35 #include "BKE_particle.h"
36
37 #include "DNA_image_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_modifier_types.h"
40 #include "DNA_node_types.h"
41
42 #include "ED_uvedit.h"
43
44 #include "GPU_shader.h"
45 #include "GPU_texture.h"
46
47 #include "UI_resources.h"
48
49 /* *********** STATIC *********** */
50 static struct {
51         struct GPUShader *composite_sh_cache[MAX_SHADERS];
52         struct GPUShader *transparent_accum_sh_cache[MAX_SHADERS];
53         struct GPUShader *transparent_revealage_sh;
54         struct GPUShader *transparent_revealage_hair_sh;
55         struct GPUShader *object_outline_sh;
56         struct GPUShader *object_outline_hair_sh;
57         struct GPUShader *checker_depth_sh;
58
59         struct GPUTexture *object_id_tx; /* ref only, not alloced */
60         struct GPUTexture *transparent_accum_tx; /* ref only, not alloced */
61 #ifdef WORKBENCH_REVEALAGE_ENABLED
62         struct GPUTexture *transparent_revealage_tx; /* ref only, not alloced */
63 #endif
64         struct GPUTexture *composite_buffer_tx; /* ref only, not alloced */
65         int next_object_id;
66         float normal_world_matrix[3][3];
67 } e_data = {{NULL}};
68
69 /* Shaders */
70 extern char datatoc_common_hair_lib_glsl[];
71 extern char datatoc_workbench_forward_composite_frag_glsl[];
72 extern char datatoc_workbench_forward_depth_frag_glsl[];
73 extern char datatoc_workbench_forward_transparent_accum_frag_glsl[];
74 #ifdef WORKBENCH_REVEALAGE_ENABLED
75 extern char datatoc_workbench_forward_transparent_revealage_frag_glsl[];
76 #endif
77 extern char datatoc_workbench_data_lib_glsl[];
78 extern char datatoc_workbench_background_lib_glsl[];
79 extern char datatoc_workbench_checkerboard_depth_frag_glsl[];
80 extern char datatoc_workbench_object_outline_lib_glsl[];
81 extern char datatoc_workbench_prepass_vert_glsl[];
82 extern char datatoc_workbench_common_lib_glsl[];
83 extern char datatoc_workbench_world_light_lib_glsl[];
84
85 /* static functions */
86 static char *workbench_build_forward_depth_frag(void)
87 {
88         char *str = NULL;
89
90         DynStr *ds = BLI_dynstr_new();
91
92         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
93         BLI_dynstr_append(ds, datatoc_workbench_forward_depth_frag_glsl);
94
95         str = BLI_dynstr_get_cstring(ds);
96         BLI_dynstr_free(ds);
97         return str;
98 }
99
100 static char *workbench_build_forward_vert(void)
101 {
102         char *str = NULL;
103
104         DynStr *ds = BLI_dynstr_new();
105
106         BLI_dynstr_append(ds, datatoc_common_hair_lib_glsl);
107         BLI_dynstr_append(ds, datatoc_workbench_prepass_vert_glsl);
108
109         str = BLI_dynstr_get_cstring(ds);
110         BLI_dynstr_free(ds);
111         return str;
112 }
113
114 static char *workbench_build_forward_transparent_accum_frag(void)
115 {
116         char *str = NULL;
117
118         DynStr *ds = BLI_dynstr_new();
119
120         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
121         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
122         BLI_dynstr_append(ds, datatoc_workbench_world_light_lib_glsl);
123         BLI_dynstr_append(ds, datatoc_workbench_forward_transparent_accum_frag_glsl);
124
125         str = BLI_dynstr_get_cstring(ds);
126         BLI_dynstr_free(ds);
127         return str;
128 }
129
130 #ifdef WORKBENCH_REVEALAGE_ENABLED
131 static char *workbench_build_forward_transparent_revealage_frag(void)
132 {
133         char *str = NULL;
134
135         DynStr *ds = BLI_dynstr_new();
136
137         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
138         BLI_dynstr_append(ds, datatoc_workbench_forward_transparent_revealage_frag_glsl);
139
140         str = BLI_dynstr_get_cstring(ds);
141         BLI_dynstr_free(ds);
142         return str;
143 }
144 #endif
145
146 static char *workbench_build_forward_composite_frag(void)
147 {
148         char *str = NULL;
149
150         DynStr *ds = BLI_dynstr_new();
151
152         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
153         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
154         BLI_dynstr_append(ds, datatoc_workbench_background_lib_glsl);
155         BLI_dynstr_append(ds, datatoc_workbench_object_outline_lib_glsl);
156         BLI_dynstr_append(ds, datatoc_workbench_forward_composite_frag_glsl);
157
158         str = BLI_dynstr_get_cstring(ds);
159         BLI_dynstr_free(ds);
160         return str;
161 }
162
163 static void workbench_init_object_data(ObjectEngineData *engine_data)
164 {
165         WORKBENCH_ObjectData *data = (WORKBENCH_ObjectData *)engine_data;
166         data->object_id = e_data.next_object_id++;
167 }
168
169 static WORKBENCH_MaterialData *get_or_create_material_data(
170         WORKBENCH_Data *vedata, Object *ob, Material *mat, Image *ima, int drawtype)
171 {
172         WORKBENCH_StorageList *stl = vedata->stl;
173         WORKBENCH_PassList *psl = vedata->psl;
174         WORKBENCH_PrivateData *wpd = stl->g_data;
175         WORKBENCH_MaterialData *material;
176         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
177                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
178         WORKBENCH_MaterialData material_template;
179         DRWShadingGroup *grp;
180
181         /* Solid */
182         workbench_material_update_data(wpd, ob, mat, &material_template);
183         material_template.object_id = engine_object_data->object_id;
184         material_template.drawtype = drawtype;
185         material_template.ima = ima;
186         uint hash = workbench_material_get_hash(&material_template);
187
188         material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
189         if (material == NULL) {
190                 material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
191
192                 /* transparent accum */
193                 grp = DRW_shgroup_create(
194                         drawtype == OB_SOLID ? wpd->transparent_accum_sh : wpd->transparent_accum_texture_sh,
195                         psl->transparent_accum_pass);
196                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
197                 DRW_shgroup_uniform_float(grp, "alpha", &wpd->shading.xray_alpha, 1);
198                 workbench_material_set_normal_world_matrix(grp, wpd, e_data.normal_world_matrix);
199                 material->object_id = engine_object_data->object_id;
200                 copy_v4_v4(material->material_data.diffuse_color, material_template.material_data.diffuse_color);
201                 copy_v4_v4(material->material_data.specular_color, material_template.material_data.specular_color);
202                 material->material_data.roughness = material_template.material_data.roughness;
203                 switch (drawtype) {
204                         case OB_SOLID:
205                                 break;
206
207                         case OB_TEXTURE:
208                         {
209                                 GPUTexture *tex = GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D, false, false, false);
210                                 DRW_shgroup_uniform_texture(grp, "image", tex);
211                                 break;
212                         }
213                 }
214                 material->material_ubo = DRW_uniformbuffer_create(sizeof(WORKBENCH_UBO_Material), &material->material_data);
215                 DRW_shgroup_uniform_block(grp, "material_block", material->material_ubo);
216                 material->shgrp = grp;
217
218                 /* Depth */
219                 material->shgrp_object_outline = DRW_shgroup_create(e_data.object_outline_sh, psl->object_outline_pass);
220                 material->object_id = engine_object_data->object_id;
221                 DRW_shgroup_uniform_int(material->shgrp_object_outline, "object_id", &material->object_id, 1);
222                 BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
223         }
224         return material;
225 }
226
227 static void ensure_forward_shaders(WORKBENCH_PrivateData *wpd, int index, int drawtype, bool is_hair)
228 {
229         if (e_data.composite_sh_cache[index] == NULL && drawtype == OB_SOLID && !is_hair) {
230                 char *defines = workbench_material_build_defines(wpd, drawtype, is_hair);
231                 char *composite_frag = workbench_build_forward_composite_frag();
232                 e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines);
233                 MEM_freeN(composite_frag);
234                 MEM_freeN(defines);
235         }
236
237         if (e_data.transparent_accum_sh_cache[index] == NULL) {
238                 char *defines = workbench_material_build_defines(wpd, drawtype, is_hair);
239                 char *transparent_accum_vert = workbench_build_forward_vert();
240                 char *transparent_accum_frag = workbench_build_forward_transparent_accum_frag();
241                 e_data.transparent_accum_sh_cache[index] = DRW_shader_create(
242                         transparent_accum_vert, NULL,
243                         transparent_accum_frag, defines);
244                 MEM_freeN(transparent_accum_vert);
245                 MEM_freeN(transparent_accum_frag);
246                 MEM_freeN(defines);
247         }
248 }
249
250 static void select_forward_shaders(WORKBENCH_PrivateData *wpd)
251 {
252         int index_solid = workbench_material_get_shader_index(wpd, OB_SOLID, false);
253         int index_solid_hair = workbench_material_get_shader_index(wpd, OB_SOLID, true);
254         int index_texture = workbench_material_get_shader_index(wpd, OB_TEXTURE, false);
255         int index_texture_hair = workbench_material_get_shader_index(wpd, OB_TEXTURE, true);
256
257         ensure_forward_shaders(wpd, index_solid, OB_SOLID, false);
258         ensure_forward_shaders(wpd, index_solid_hair, OB_SOLID, true);
259         ensure_forward_shaders(wpd, index_texture, OB_TEXTURE, false);
260         ensure_forward_shaders(wpd, index_texture_hair, OB_TEXTURE, true);
261
262         wpd->composite_sh = e_data.composite_sh_cache[index_solid];
263         wpd->transparent_accum_sh = e_data.transparent_accum_sh_cache[index_solid];
264         wpd->transparent_accum_hair_sh = e_data.transparent_accum_sh_cache[index_solid_hair];
265         wpd->transparent_accum_texture_sh = e_data.transparent_accum_sh_cache[index_texture];
266         wpd->transparent_accum_texture_hair_sh = e_data.transparent_accum_sh_cache[index_texture_hair];
267 }
268
269 /* public functions */
270 void workbench_forward_engine_init(WORKBENCH_Data *vedata)
271 {
272         WORKBENCH_FramebufferList *fbl = vedata->fbl;
273         WORKBENCH_PassList *psl = vedata->psl;
274         WORKBENCH_StorageList *stl = vedata->stl;
275         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
276         DRWShadingGroup *grp;
277
278         if (!stl->g_data) {
279                 /* Alloc transient pointers */
280                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
281         }
282         WORKBENCH_PrivateData *wpd = stl->g_data;
283         workbench_private_data_init(wpd);
284         float light_direction[3];
285         workbench_private_data_get_light_direction(wpd, light_direction);
286
287         if (!e_data.next_object_id) {
288                 e_data.next_object_id = 1;
289                 memset(e_data.composite_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
290                 memset(e_data.transparent_accum_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
291
292                 char *defines = workbench_material_build_defines(wpd, OB_SOLID, false);
293                 char *defines_hair = workbench_material_build_defines(wpd, OB_SOLID, true);
294                 char *forward_vert = workbench_build_forward_vert();
295                 char *forward_depth_frag = workbench_build_forward_depth_frag();
296                 e_data.object_outline_sh = DRW_shader_create(
297                         forward_vert, NULL,
298                         forward_depth_frag, defines);
299                 e_data.object_outline_hair_sh = DRW_shader_create(
300                         forward_vert, NULL,
301                         forward_depth_frag, defines_hair);
302
303 #ifdef WORKBENCH_REVEALAGE_ENABLED
304                 char *forward_transparent_revealage_frag = workbench_build_forward_transparent_revealage_frag();
305                 e_data.transparent_revealage_sh = DRW_shader_create(
306                         forward_vert, NULL,
307                         forward_transparent_revealage_frag, defines);
308                 e_data.transparent_revealage_hair_sh = DRW_shader_create(
309                         forward_vert, NULL,
310                         forward_transparent_revealage_frag, defines_hair);
311                 MEM_freeN(forward_transparent_revealage_frag);
312 #endif
313
314                 e_data.checker_depth_sh = DRW_shader_create_fullscreen(
315                         datatoc_workbench_checkerboard_depth_frag_glsl, NULL);
316                 MEM_freeN(forward_vert);
317                 MEM_freeN(forward_depth_frag);
318                 MEM_freeN(defines);
319                 MEM_freeN(defines_hair);
320         }
321         select_forward_shaders(wpd);
322
323         const float *viewport_size = DRW_viewport_size_get();
324         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
325
326         e_data.object_id_tx = DRW_texture_pool_query_2D(
327                 size[0], size[1], GPU_R32UI, &draw_engine_workbench_transparent);
328         e_data.transparent_accum_tx = DRW_texture_pool_query_2D(
329                 size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_transparent);
330 #ifdef WORKBENCH_REVEALAGE_ENABLED
331         e_data.transparent_revealage_tx = DRW_texture_pool_query_2D(
332                 size[0], size[1], GPU_R16F, &draw_engine_workbench_transparent);
333 #endif
334         e_data.composite_buffer_tx = DRW_texture_pool_query_2D(
335                 size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_transparent);
336         GPU_framebuffer_ensure_config(&fbl->object_outline_fb, {
337                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
338                 GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
339         });
340         GPU_framebuffer_ensure_config(&fbl->transparent_accum_fb, {
341                 GPU_ATTACHMENT_NONE,
342                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_accum_tx),
343         });
344
345 #ifdef WORKBENCH_REVEALAGE_ENABLED
346         GPU_framebuffer_ensure_config(&fbl->transparent_revealage_fb, {
347                 GPU_ATTACHMENT_NONE,
348                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_revealage_tx),
349         });
350 #endif
351
352         GPU_framebuffer_ensure_config(&fbl->composite_fb, {
353                 GPU_ATTACHMENT_NONE,
354                 GPU_ATTACHMENT_TEXTURE(e_data.composite_buffer_tx),
355         });
356         const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
357         DRW_stats_group_start("Clear Buffers");
358         GPU_framebuffer_bind(fbl->transparent_accum_fb);
359         GPU_framebuffer_clear_color(fbl->transparent_accum_fb, clear_color);
360
361 #ifdef WORKBENCH_REVEALAGE_ENABLED
362         const float clear_color1[4] = {1.0f, 1.0f, 1.0f, 1.0f};
363         GPU_framebuffer_bind(fbl->transparent_revealage_fb);
364         GPU_framebuffer_clear_color(fbl->transparent_revealage_fb, clear_color1);
365 #endif
366         GPU_framebuffer_bind(fbl->object_outline_fb);
367         GPU_framebuffer_clear_color_depth(fbl->object_outline_fb, clear_color, 1.0f);
368         DRW_stats_group_end();
369
370         /* Transparency Accum */
371         {
372                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE_FULL;
373                 psl->transparent_accum_pass = DRW_pass_create("Transparent Accum", state);
374         }
375
376 #ifdef WORKBENCH_REVEALAGE_ENABLED
377         /* Transparency Revealage */
378         {
379                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_TRANSPARENT_REVEALAGE;
380                 psl->transparent_revealage_pass = DRW_pass_create("Transparent Revealage", state);
381                 grp = DRW_shgroup_create(e_data.transparent_revealage_sh, psl->transparent_revealage_pass);
382                 DRW_shgroup_uniform_float(grp, "alpha", &wpd->shading.xray_alpha, 1);
383                 wpd->transparent_revealage_shgrp = grp;
384         }
385 #endif
386
387         /* Depth */
388         {
389                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
390                 psl->object_outline_pass = DRW_pass_create("Object Outline Pass", state);
391         }
392         /* Composite */
393         {
394                 int state = DRW_STATE_WRITE_COLOR;
395                 psl->composite_pass = DRW_pass_create("Composite", state);
396
397                 grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
398                 DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
399                 DRW_shgroup_uniform_texture_ref(grp, "transparentAccum", &e_data.transparent_accum_tx);
400 #ifdef WORKBENCH_REVEALAGE_ENABLED
401                 DRW_shgroup_uniform_texture_ref(grp, "transparentRevealage", &e_data.transparent_revealage_tx);
402 #endif
403                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
404                 DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
405                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
406         }
407         /* Checker Depth */
408         {
409                 int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS;
410                 psl->checker_depth_pass = DRW_pass_create("Checker Depth", state);
411                 grp = DRW_shgroup_create(e_data.checker_depth_sh, psl->checker_depth_pass);
412                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
413         }
414 }
415
416 void workbench_forward_engine_free()
417 {
418         for (int index = 0; index < MAX_SHADERS; index++) {
419                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
420                 DRW_SHADER_FREE_SAFE(e_data.transparent_accum_sh_cache[index]);
421         }
422 #ifdef WORKBENCH_REVEALAGE_ENABLED
423         DRW_SHADER_FREE_SAFE(e_data.transparent_revealage_sh);
424         DRW_SHADER_FREE_SAFE(e_data.transparent_revealage_hair_sh);
425 #endif
426         DRW_SHADER_FREE_SAFE(e_data.object_outline_sh);
427         DRW_SHADER_FREE_SAFE(e_data.object_outline_hair_sh);
428         DRW_SHADER_FREE_SAFE(e_data.checker_depth_sh);
429 }
430
431 void workbench_forward_cache_init(WORKBENCH_Data *UNUSED(vedata))
432 {
433 }
434
435 static void workbench_forward_cache_populate_particles(WORKBENCH_Data *vedata, Object *ob)
436 {
437         WORKBENCH_StorageList *stl = vedata->stl;
438         WORKBENCH_PassList *psl = vedata->psl;
439         WORKBENCH_PrivateData *wpd = stl->g_data;
440         const DRWContextState *draw_ctx = DRW_context_state_get();
441
442         if (ob == draw_ctx->object_edit) {
443                 return;
444         }
445         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
446                 if (md->type != eModifierType_ParticleSystem) {
447                         continue;
448                 }
449                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
450                 if (!psys_check_enabled(ob, psys, false)) {
451                         continue;
452                 }
453                 if (!DRW_check_psys_visible_within_active_context(ob, psys)) {
454                         return;
455                 }
456                 ParticleSettings *part = psys->part;
457                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
458
459                 if (draw_as == PART_DRAW_PATH) {
460                         Image *image = NULL;
461                         Material *mat = give_current_material(ob, part->omat);
462                         int mat_drawtype = OB_SOLID;
463
464                         if (wpd->drawtype == OB_TEXTURE) {
465                                 ED_object_get_active_image(ob, part->omat, &image, NULL, NULL, NULL);
466                                 /* use OB_SOLID when no texture could be determined */
467                                 if (image) {
468                                         mat_drawtype = OB_TEXTURE;
469                                 }
470                         }
471
472                         WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
473
474                         struct GPUShader *shader = (mat_drawtype == OB_SOLID)
475                                                    ? wpd->transparent_accum_hair_sh
476                                                    : wpd->transparent_accum_texture_hair_sh;
477                         DRWShadingGroup *shgrp = DRW_shgroup_hair_create(
478                                                         ob, psys, md,
479                                                         psl->transparent_accum_pass,
480                                                         shader);
481                         workbench_material_set_normal_world_matrix(shgrp, wpd, e_data.normal_world_matrix);
482                         DRW_shgroup_uniform_block(shgrp, "world_block", wpd->world_ubo);
483                         DRW_shgroup_uniform_block(shgrp, "material_block", material->material_ubo);
484                         DRW_shgroup_uniform_float(shgrp, "alpha", &wpd->shading.xray_alpha, 1);
485                         if (image) {
486                                 GPUTexture *tex = GPU_texture_from_blender(image, NULL, GL_TEXTURE_2D, false, false, false);
487                                 DRW_shgroup_uniform_texture(shgrp, "image", tex);
488                         }
489 #ifdef WORKBENCH_REVEALAGE_ENABLED
490                         shgrp = DRW_shgroup_hair_create(ob, psys, md,
491                                                 psl->transparent_revealage_pass,
492                                                 e_data.transparent_revealage_hair_sh);
493                         DRW_shgroup_uniform_float(shgrp, "alpha", &wpd->shading.xray_alpha, 1);
494 #endif
495                         shgrp = DRW_shgroup_hair_create(ob, psys, md,
496                                                 vedata->psl->object_outline_pass,
497                                                 e_data.object_outline_hair_sh);
498                         DRW_shgroup_uniform_int(shgrp, "object_id", &material->object_id, 1);
499                 }
500         }
501 }
502
503 void workbench_forward_cache_populate(WORKBENCH_Data *vedata, Object *ob)
504 {
505         WORKBENCH_StorageList *stl = vedata->stl;
506         WORKBENCH_PrivateData *wpd = stl->g_data;
507
508         if (!DRW_object_is_renderable(ob))
509                 return;
510
511         if (ob->type == OB_MESH) {
512                 workbench_forward_cache_populate_particles(vedata, ob);
513         }
514         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
515                 const DRWContextState *draw_ctx = DRW_context_state_get();
516                 const bool is_active = (ob == draw_ctx->obact);
517                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
518                 bool is_drawn = false;
519
520                 WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, NULL, NULL, OB_SOLID);
521                 if (!is_sculpt_mode && wpd->drawtype == OB_TEXTURE && ob->type == OB_MESH) {
522                         const Mesh *me = ob->data;
523                         if (me->mloopuv) {
524                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
525                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
526                                 struct Gwn_Batch **geom_array = me->totcol ? DRW_cache_mesh_surface_texpaint_get(ob) : NULL;
527                                 if (materials_len > 0 && geom_array) {
528                                         for (int i = 0; i < materials_len; i++) {
529                                                 Material *mat = give_current_material(ob, i + 1);
530                                                 Image *image;
531                                                 ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL);
532                                                 /* use OB_SOLID when no texture could be determined */
533                                                 int mat_drawtype = OB_SOLID;
534                                                 if (image) {
535                                                         mat_drawtype = OB_TEXTURE;
536                                                 }
537                                                 material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
538 #ifdef WORKBENCH_REVEALAGE_ENABLED
539                                                 DRW_shgroup_call_object_add(wpd->transparent_revealage_shgrp, geom_array[i], ob);
540 #endif
541                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, geom_array[i], ob);
542                                                 DRW_shgroup_call_object_add(material->shgrp, geom_array[i], ob);
543                                         }
544                                         is_drawn = true;
545                                 }
546                         }
547                 }
548
549                 /* Fallback from not drawn OB_TEXTURE mode or just OB_SOLID mode */
550                 if (!is_drawn) {
551                         if ((wpd->shading.color_type != V3D_SHADING_MATERIAL_COLOR) || is_sculpt_mode) {
552                                 /* No material split needed */
553                                 struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
554                                 if (geom) {
555                                         if (is_sculpt_mode) {
556 #ifdef WORKBENCH_REVEALAGE_ENABLED
557                                                 DRW_shgroup_call_sculpt_add(wpd->transparent_revealage_shgrp, ob, ob->obmat);
558 #endif
559                                                 DRW_shgroup_call_sculpt_add(material->shgrp_object_outline, ob, ob->obmat);
560                                                 DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
561                                         }
562                                         else {
563 #ifdef WORKBENCH_REVEALAGE_ENABLED
564                                                 DRW_shgroup_call_object_add(wpd->transparent_revealage_shgrp, geom, ob);
565 #endif
566                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, geom, ob);
567                                                 DRW_shgroup_call_object_add(material->shgrp, geom, ob);
568                                         }
569                                 }
570                         }
571                         else {
572                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
573                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
574                                 for (int i = 0; i < materials_len; i++) {
575                                         gpumat_array[i] = NULL;
576                                 }
577
578                                 struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(
579                                         ob, gpumat_array, materials_len, NULL, NULL, NULL);
580                                 if (mat_geom) {
581                                         for (int i = 0; i < materials_len; ++i) {
582                                                 Material *mat = give_current_material(ob, i + 1);
583                                                 material = get_or_create_material_data(vedata, ob, mat, NULL, OB_SOLID);
584 #ifdef WORKBENCH_REVEALAGE_ENABLED
585                                                 DRW_shgroup_call_object_add(wpd->transparent_revealage_shgrp, mat_geom[i], ob);
586 #endif
587                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, mat_geom[i], ob);
588                                                 DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
589                                         }
590                                 }
591                         }
592                 }
593         }
594 }
595
596 void workbench_forward_cache_finish(WORKBENCH_Data *UNUSED(vedata))
597 {
598 }
599
600 void workbench_forward_draw_background(WORKBENCH_Data *UNUSED(vedata))
601 {
602         const float clear_depth = 1.0f;
603         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
604         DRW_stats_group_start("Clear Background");
605         GPU_framebuffer_bind(dfbl->default_fb);
606         GPU_framebuffer_clear_depth(dfbl->default_fb, clear_depth);
607         DRW_stats_group_end();
608 }
609
610 void workbench_forward_draw_scene(WORKBENCH_Data *vedata)
611 {
612         WORKBENCH_PassList *psl = vedata->psl;
613         WORKBENCH_StorageList *stl = vedata->stl;
614         WORKBENCH_FramebufferList *fbl = vedata->fbl;
615         WORKBENCH_PrivateData *wpd = stl->g_data;
616         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
617
618         /* Write Depth + Object ID */
619         GPU_framebuffer_bind(fbl->object_outline_fb);
620         DRW_draw_pass(psl->object_outline_pass);
621
622         if (wpd->shading.xray_alpha > 0.0) {
623                 /* Shade */
624                 GPU_framebuffer_bind(fbl->transparent_accum_fb);
625                 DRW_draw_pass(psl->transparent_accum_pass);
626 #ifdef WORKBENCH_REVEALAGE_ENABLED
627                 GPU_framebuffer_bind(fbl->transparent_revealage_fb);
628                 DRW_draw_pass(psl->transparent_revealage_pass);
629 #endif
630         }
631
632         /* Composite */
633         GPU_framebuffer_bind(fbl->composite_fb);
634         DRW_draw_pass(psl->composite_pass);
635
636         /* Color correct */
637         GPU_framebuffer_bind(dfbl->color_only_fb);
638         DRW_transform_to_display(e_data.composite_buffer_tx);
639
640         GPU_framebuffer_bind(dfbl->depth_only_fb);
641         DRW_draw_pass(psl->checker_depth_pass);
642
643         workbench_private_data_free(wpd);
644 }