cf7a7858f7a23423cbf995878fc5f3a67e929244
[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         }
320         select_forward_shaders(wpd);
321
322         const float *viewport_size = DRW_viewport_size_get();
323         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
324
325         e_data.object_id_tx = DRW_texture_pool_query_2D(
326                 size[0], size[1], GPU_R32UI, &draw_engine_workbench_transparent);
327         e_data.transparent_accum_tx = DRW_texture_pool_query_2D(
328                 size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_transparent);
329 #ifdef WORKBENCH_REVEALAGE_ENABLED
330         e_data.transparent_revealage_tx = DRW_texture_pool_query_2D(
331                 size[0], size[1], GPU_R16F, &draw_engine_workbench_transparent);
332 #endif
333         e_data.composite_buffer_tx = DRW_texture_pool_query_2D(
334                 size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_transparent);
335         GPU_framebuffer_ensure_config(&fbl->object_outline_fb, {
336                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
337                 GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
338         });
339         GPU_framebuffer_ensure_config(&fbl->transparent_accum_fb, {
340                 GPU_ATTACHMENT_NONE,
341                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_accum_tx),
342         });
343
344 #ifdef WORKBENCH_REVEALAGE_ENABLED
345         GPU_framebuffer_ensure_config(&fbl->transparent_revealage_fb, {
346                 GPU_ATTACHMENT_NONE,
347                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_revealage_tx),
348         });
349 #endif
350
351         GPU_framebuffer_ensure_config(&fbl->composite_fb, {
352                 GPU_ATTACHMENT_NONE,
353                 GPU_ATTACHMENT_TEXTURE(e_data.composite_buffer_tx),
354         });
355         const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
356         DRW_stats_group_start("Clear Buffers");
357         GPU_framebuffer_bind(fbl->transparent_accum_fb);
358         GPU_framebuffer_clear_color(fbl->transparent_accum_fb, clear_color);
359
360 #ifdef WORKBENCH_REVEALAGE_ENABLED
361         const float clear_color1[4] = {1.0f, 1.0f, 1.0f, 1.0f};
362         GPU_framebuffer_bind(fbl->transparent_revealage_fb);
363         GPU_framebuffer_clear_color(fbl->transparent_revealage_fb, clear_color1);
364 #endif
365         GPU_framebuffer_bind(fbl->object_outline_fb);
366         GPU_framebuffer_clear_color_depth(fbl->object_outline_fb, clear_color, 1.0f);
367         DRW_stats_group_end();
368
369         /* Transparency Accum */
370         {
371                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE_FULL;
372                 psl->transparent_accum_pass = DRW_pass_create("Transparent Accum", state);
373         }
374
375 #ifdef WORKBENCH_REVEALAGE_ENABLED
376         /* Transparency Revealage */
377         {
378                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_TRANSPARENT_REVEALAGE;
379                 psl->transparent_revealage_pass = DRW_pass_create("Transparent Revealage", state);
380                 grp = DRW_shgroup_create(e_data.transparent_revealage_sh, psl->transparent_revealage_pass);
381                 DRW_shgroup_uniform_float(grp, "alpha", &wpd->shading.xray_alpha, 1);
382                 wpd->transparent_revealage_shgrp = grp;
383         }
384 #endif
385
386         /* Depth */
387         {
388                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
389                 psl->object_outline_pass = DRW_pass_create("Object Outline Pass", state);
390         }
391         /* Composite */
392         {
393                 int state = DRW_STATE_WRITE_COLOR;
394                 psl->composite_pass = DRW_pass_create("Composite", state);
395
396                 grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
397                 DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
398                 DRW_shgroup_uniform_texture_ref(grp, "transparentAccum", &e_data.transparent_accum_tx);
399 #ifdef WORKBENCH_REVEALAGE_ENABLED
400                 DRW_shgroup_uniform_texture_ref(grp, "transparentRevealage", &e_data.transparent_revealage_tx);
401 #endif
402                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
403                 DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
404                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
405         }
406         /* Checker Depth */
407         {
408                 int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS;
409                 psl->checker_depth_pass = DRW_pass_create("Checker Depth", state);
410                 grp = DRW_shgroup_create(e_data.checker_depth_sh, psl->checker_depth_pass);
411                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
412         }
413 }
414
415 void workbench_forward_engine_free()
416 {
417         for (int index = 0; index < MAX_SHADERS; index++) {
418                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
419                 DRW_SHADER_FREE_SAFE(e_data.transparent_accum_sh_cache[index]);
420         }
421 #ifdef WORKBENCH_REVEALAGE_ENABLED
422         DRW_SHADER_FREE_SAFE(e_data.transparent_revealage_sh);
423         DRW_SHADER_FREE_SAFE(e_data.transparent_revealage_hair_sh);
424 #endif
425         DRW_SHADER_FREE_SAFE(e_data.object_outline_sh);
426         DRW_SHADER_FREE_SAFE(e_data.object_outline_hair_sh);
427         DRW_SHADER_FREE_SAFE(e_data.checker_depth_sh);
428 }
429
430 void workbench_forward_cache_init(WORKBENCH_Data *UNUSED(vedata))
431 {
432 }
433
434 static void workbench_forward_cache_populate_particles(WORKBENCH_Data *vedata, Object *ob)
435 {
436         WORKBENCH_StorageList *stl = vedata->stl;
437         WORKBENCH_PassList *psl = vedata->psl;
438         WORKBENCH_PrivateData *wpd = stl->g_data;
439         const DRWContextState *draw_ctx = DRW_context_state_get();
440
441         if (ob == draw_ctx->object_edit) {
442                 return;
443         }
444         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
445                 if (md->type != eModifierType_ParticleSystem) {
446                         continue;
447                 }
448                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
449                 if (!psys_check_enabled(ob, psys, false)) {
450                         continue;
451                 }
452                 if (!DRW_check_psys_visible_within_active_context(ob, psys)) {
453                         return;
454                 }
455                 ParticleSettings *part = psys->part;
456                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
457
458                 if (draw_as == PART_DRAW_PATH) {
459                         Image *image = NULL;
460                         Material *mat = give_current_material(ob, part->omat);
461                         int mat_drawtype = OB_SOLID;
462
463                         if (wpd->drawtype == OB_TEXTURE) {
464                                 ED_object_get_active_image(ob, part->omat, &image, NULL, NULL, NULL);
465                                 /* use OB_SOLID when no texture could be determined */
466                                 if (image) {
467                                         mat_drawtype = OB_TEXTURE;
468                                 }
469                         }
470
471                         WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
472
473                         struct GPUShader *shader = (mat_drawtype == OB_SOLID)
474                                                    ? wpd->transparent_accum_hair_sh
475                                                    : wpd->transparent_accum_texture_hair_sh;
476                         DRWShadingGroup *shgrp = DRW_shgroup_hair_create(
477                                                         ob, psys, md,
478                                                         psl->transparent_accum_pass,
479                                                         shader);
480                         workbench_material_set_normal_world_matrix(shgrp, wpd, e_data.normal_world_matrix);
481                         DRW_shgroup_uniform_block(shgrp, "world_block", wpd->world_ubo);
482                         DRW_shgroup_uniform_block(shgrp, "material_block", material->material_ubo);
483                         DRW_shgroup_uniform_float(shgrp, "alpha", &wpd->shading.xray_alpha, 1);
484                         if (image) {
485                                 GPUTexture *tex = GPU_texture_from_blender(image, NULL, GL_TEXTURE_2D, false, false, false);
486                                 DRW_shgroup_uniform_texture(shgrp, "image", tex);
487                         }
488 #ifdef WORKBENCH_REVEALAGE_ENABLED
489                         shgrp = DRW_shgroup_hair_create(ob, psys, md,
490                                                 psl->transparent_revealage_pass,
491                                                 e_data.transparent_revealage_hair_sh);
492                         DRW_shgroup_uniform_float(shgrp, "alpha", &wpd->shading.xray_alpha, 1);
493 #endif
494                         shgrp = DRW_shgroup_hair_create(ob, psys, md,
495                                                 vedata->psl->object_outline_pass,
496                                                 e_data.object_outline_hair_sh);
497                         DRW_shgroup_uniform_int(shgrp, "object_id", &material->object_id, 1);
498                 }
499         }
500 }
501
502 void workbench_forward_cache_populate(WORKBENCH_Data *vedata, Object *ob)
503 {
504         WORKBENCH_StorageList *stl = vedata->stl;
505         WORKBENCH_PrivateData *wpd = stl->g_data;
506
507         if (!DRW_object_is_renderable(ob))
508                 return;
509
510         if (ob->type == OB_MESH) {
511                 workbench_forward_cache_populate_particles(vedata, ob);
512         }
513         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
514                 const DRWContextState *draw_ctx = DRW_context_state_get();
515                 const bool is_active = (ob == draw_ctx->obact);
516                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
517                 bool is_drawn = false;
518
519                 WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, NULL, NULL, OB_SOLID);
520                 if (!is_sculpt_mode && wpd->drawtype == OB_TEXTURE && ob->type == OB_MESH) {
521                         const Mesh *me = ob->data;
522                         if (me->mloopuv) {
523                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
524                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
525                                 struct Gwn_Batch **geom_array = me->totcol ? DRW_cache_mesh_surface_texpaint_get(ob) : NULL;
526                                 if (materials_len > 0 && geom_array) {
527                                         for (int i = 0; i < materials_len; i++) {
528                                                 Material *mat = give_current_material(ob, i + 1);
529                                                 Image *image;
530                                                 ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL);
531                                                 /* use OB_SOLID when no texture could be determined */
532                                                 int mat_drawtype = OB_SOLID;
533                                                 if (image) {
534                                                         mat_drawtype = OB_TEXTURE;
535                                                 }
536                                                 material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
537 #ifdef WORKBENCH_REVEALAGE_ENABLED
538                                                 DRW_shgroup_call_object_add(wpd->transparent_revealage_shgrp, geom_array[i], ob);
539 #endif
540                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, geom_array[i], ob);
541                                                 DRW_shgroup_call_object_add(material->shgrp, geom_array[i], ob);
542                                         }
543                                         is_drawn = true;
544                                 }
545                         }
546                 }
547
548                 /* Fallback from not drawn OB_TEXTURE mode or just OB_SOLID mode */
549                 if (!is_drawn) {
550                         if ((wpd->shading.color_type != V3D_SHADING_MATERIAL_COLOR) || is_sculpt_mode) {
551                                 /* No material split needed */
552                                 struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
553                                 if (geom) {
554                                         if (is_sculpt_mode) {
555 #ifdef WORKBENCH_REVEALAGE_ENABLED
556                                                 DRW_shgroup_call_sculpt_add(wpd->transparent_revealage_shgrp, ob, ob->obmat);
557 #endif
558                                                 DRW_shgroup_call_sculpt_add(material->shgrp_object_outline, ob, ob->obmat);
559                                                 DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
560                                         }
561                                         else {
562 #ifdef WORKBENCH_REVEALAGE_ENABLED
563                                                 DRW_shgroup_call_object_add(wpd->transparent_revealage_shgrp, geom, ob);
564 #endif
565                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, geom, ob);
566                                                 DRW_shgroup_call_object_add(material->shgrp, geom, ob);
567                                         }
568                                 }
569                         }
570                         else {
571                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
572                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
573                                 for (int i = 0; i < materials_len; i++) {
574                                         gpumat_array[i] = NULL;
575                                 }
576
577                                 struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(
578                                         ob, gpumat_array, materials_len, NULL, NULL, NULL);
579                                 if (mat_geom) {
580                                         for (int i = 0; i < materials_len; ++i) {
581                                                 Material *mat = give_current_material(ob, i + 1);
582                                                 material = get_or_create_material_data(vedata, ob, mat, NULL, OB_SOLID);
583 #ifdef WORKBENCH_REVEALAGE_ENABLED
584                                                 DRW_shgroup_call_object_add(wpd->transparent_revealage_shgrp, mat_geom[i], ob);
585 #endif
586                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, mat_geom[i], ob);
587                                                 DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
588                                         }
589                                 }
590                         }
591                 }
592         }
593 }
594
595 void workbench_forward_cache_finish(WORKBENCH_Data *UNUSED(vedata))
596 {
597 }
598
599 void workbench_forward_draw_background(WORKBENCH_Data *UNUSED(vedata))
600 {
601         const float clear_depth = 1.0f;
602         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
603         DRW_stats_group_start("Clear Background");
604         GPU_framebuffer_bind(dfbl->default_fb);
605         GPU_framebuffer_clear_depth(dfbl->default_fb, clear_depth);
606         DRW_stats_group_end();
607 }
608
609 void workbench_forward_draw_scene(WORKBENCH_Data *vedata)
610 {
611         WORKBENCH_PassList *psl = vedata->psl;
612         WORKBENCH_StorageList *stl = vedata->stl;
613         WORKBENCH_FramebufferList *fbl = vedata->fbl;
614         WORKBENCH_PrivateData *wpd = stl->g_data;
615         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
616
617         /* Write Depth + Object ID */
618         GPU_framebuffer_bind(fbl->object_outline_fb);
619         DRW_draw_pass(psl->object_outline_pass);
620
621         if (wpd->shading.xray_alpha > 0.0) {
622                 /* Shade */
623                 GPU_framebuffer_bind(fbl->transparent_accum_fb);
624                 DRW_draw_pass(psl->transparent_accum_pass);
625 #ifdef WORKBENCH_REVEALAGE_ENABLED
626                 GPU_framebuffer_bind(fbl->transparent_revealage_fb);
627                 DRW_draw_pass(psl->transparent_revealage_pass);
628 #endif
629         }
630
631         /* Composite */
632         GPU_framebuffer_bind(fbl->composite_fb);
633         DRW_draw_pass(psl->composite_pass);
634
635         /* Color correct */
636         GPU_framebuffer_bind(dfbl->color_only_fb);
637         DRW_transform_to_display(e_data.composite_buffer_tx);
638
639         GPU_framebuffer_bind(dfbl->depth_only_fb);
640         DRW_draw_pass(psl->checker_depth_pass);
641
642         workbench_private_data_free(wpd);
643 }