Workbench: Codestyle
[blender.git] / source / blender / draw / engines / workbench / workbench_deferred.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_deferred.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
48 /* *********** STATIC *********** */
49
50 // #define DEBUG_SHADOW_VOLUME
51
52 static struct {
53         struct GPUShader *prepass_sh_cache[MAX_SHADERS];
54         struct GPUShader *composite_sh_cache[MAX_SHADERS];
55         struct GPUShader *shadow_fail_sh;
56         struct GPUShader *shadow_fail_manifold_sh;
57         struct GPUShader *shadow_pass_sh;
58         struct GPUShader *shadow_pass_manifold_sh;
59         struct GPUShader *shadow_caps_sh;
60         struct GPUShader *shadow_caps_manifold_sh;
61
62         struct GPUTexture *object_id_tx; /* ref only, not alloced */
63         struct GPUTexture *color_buffer_tx; /* ref only, not alloced */
64         struct GPUTexture *normal_buffer_tx; /* ref only, not alloced */
65         struct GPUTexture *composite_buffer_tx; /* ref only, not alloced */
66
67         SceneDisplay display; /* world light direction for shadows */
68         float light_direction_vs[3];
69         int next_object_id;
70         float normal_world_matrix[3][3];
71 } e_data = {NULL};
72
73 /* Shaders */
74 extern char datatoc_workbench_prepass_vert_glsl[];
75 extern char datatoc_workbench_prepass_frag_glsl[];
76 extern char datatoc_workbench_deferred_composite_frag_glsl[];
77
78 extern char datatoc_workbench_shadow_vert_glsl[];
79 extern char datatoc_workbench_shadow_geom_glsl[];
80 extern char datatoc_workbench_shadow_caps_geom_glsl[];
81 extern char datatoc_workbench_shadow_debug_frag_glsl[];
82
83 extern char datatoc_workbench_background_lib_glsl[];
84 extern char datatoc_workbench_common_lib_glsl[];
85 extern char datatoc_workbench_data_lib_glsl[];
86 extern char datatoc_workbench_object_outline_lib_glsl[];
87 extern char datatoc_workbench_world_light_lib_glsl[];
88
89 static char *workbench_build_composite_frag(WORKBENCH_PrivateData *wpd)
90 {
91         char *str = NULL;
92
93         DynStr *ds = BLI_dynstr_new();
94
95         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
96         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
97         BLI_dynstr_append(ds, datatoc_workbench_background_lib_glsl);
98
99         if (wpd->shading.light & V3D_LIGHTING_STUDIO) {
100                 BLI_dynstr_append(ds, datatoc_workbench_world_light_lib_glsl);
101         }
102         if (wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE) {
103                 BLI_dynstr_append(ds, datatoc_workbench_object_outline_lib_glsl);
104         }
105
106         BLI_dynstr_append(ds, datatoc_workbench_deferred_composite_frag_glsl);
107
108         str = BLI_dynstr_get_cstring(ds);
109         BLI_dynstr_free(ds);
110         return str;
111 }
112
113 static char *workbench_build_prepass_frag(void)
114 {
115         char *str = NULL;
116
117         DynStr *ds = BLI_dynstr_new();
118
119         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
120         BLI_dynstr_append(ds, datatoc_workbench_prepass_frag_glsl);
121
122         str = BLI_dynstr_get_cstring(ds);
123         BLI_dynstr_free(ds);
124         return str;
125 }
126
127 static void ensure_deferred_shaders(WORKBENCH_PrivateData *wpd, int index, int drawtype)
128 {
129         if (e_data.prepass_sh_cache[index] == NULL) {
130                 char *defines = workbench_material_build_defines(wpd, drawtype);
131                 char *composite_frag = workbench_build_composite_frag(wpd);
132                 char *prepass_frag = workbench_build_prepass_frag();
133                 e_data.prepass_sh_cache[index] = DRW_shader_create(
134                         datatoc_workbench_prepass_vert_glsl, NULL, prepass_frag, defines);
135                 if (drawtype == OB_SOLID) {
136                         e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines);
137                 }
138                 MEM_freeN(prepass_frag);
139                 MEM_freeN(composite_frag);
140                 MEM_freeN(defines);
141         }
142 }
143
144 static void select_deferred_shaders(WORKBENCH_PrivateData *wpd)
145 {
146         int index_solid = workbench_material_get_shader_index(wpd, OB_SOLID);
147         int index_texture = workbench_material_get_shader_index(wpd, OB_TEXTURE);
148
149         ensure_deferred_shaders(wpd, index_solid, OB_SOLID);
150         ensure_deferred_shaders(wpd, index_texture, OB_TEXTURE);
151
152         wpd->prepass_solid_sh = e_data.prepass_sh_cache[index_solid];
153         wpd->prepass_texture_sh = e_data.prepass_sh_cache[index_texture];
154         wpd->composite_sh = e_data.composite_sh_cache[index_solid];
155 }
156
157 /* Functions */
158
159
160 static void workbench_init_object_data(ObjectEngineData *engine_data)
161 {
162         WORKBENCH_ObjectData *data = (WORKBENCH_ObjectData *)engine_data;
163         data->object_id = e_data.next_object_id++;
164 }
165
166 void workbench_deferred_engine_init(WORKBENCH_Data *vedata)
167 {
168         WORKBENCH_FramebufferList *fbl = vedata->fbl;
169         WORKBENCH_StorageList *stl = vedata->stl;
170         WORKBENCH_PassList *psl = vedata->psl;
171         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
172
173         if (!e_data.next_object_id) {
174                 memset(e_data.prepass_sh_cache,   0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
175                 memset(e_data.composite_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
176                 e_data.next_object_id = 1;
177 #ifdef DEBUG_SHADOW_VOLUME
178                 const char *shadow_frag = datatoc_workbench_shadow_debug_frag_glsl;
179 #else
180                 const char *shadow_frag = NULL;
181 #endif
182                 e_data.shadow_pass_sh = DRW_shader_create(
183                         datatoc_workbench_shadow_vert_glsl,
184                         datatoc_workbench_shadow_geom_glsl,
185                         shadow_frag,
186                         "#define SHADOW_PASS\n"
187                         "#define DOUBLE_MANIFOLD\n");
188                 e_data.shadow_pass_manifold_sh = DRW_shader_create(
189                         datatoc_workbench_shadow_vert_glsl,
190                         datatoc_workbench_shadow_geom_glsl,
191                         shadow_frag,
192                         "#define SHADOW_PASS\n");
193                 e_data.shadow_fail_sh = DRW_shader_create(
194                         datatoc_workbench_shadow_vert_glsl,
195                         datatoc_workbench_shadow_geom_glsl,
196                         shadow_frag,
197                         "#define SHADOW_FAIL\n"
198                         "#define DOUBLE_MANIFOLD\n");
199                 e_data.shadow_fail_manifold_sh = DRW_shader_create(
200                         datatoc_workbench_shadow_vert_glsl,
201                         datatoc_workbench_shadow_geom_glsl,
202                         shadow_frag,
203                         "#define SHADOW_FAIL\n");
204                 e_data.shadow_caps_sh = DRW_shader_create(
205                         datatoc_workbench_shadow_vert_glsl,
206                         datatoc_workbench_shadow_caps_geom_glsl,
207                         shadow_frag,
208                         "#define SHADOW_FAIL\n"
209                         "#define DOUBLE_MANIFOLD\n");
210                 e_data.shadow_caps_manifold_sh = DRW_shader_create(
211                         datatoc_workbench_shadow_vert_glsl,
212                         datatoc_workbench_shadow_caps_geom_glsl,
213                         shadow_frag,
214                         "#define SHADOW_FAIL\n");
215         }
216
217         if (!stl->g_data) {
218                 /* Alloc transient pointers */
219                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
220         }
221
222         workbench_private_data_init(stl->g_data);
223
224         {
225                 const float *viewport_size = DRW_viewport_size_get();
226                 const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
227                 e_data.object_id_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_R32UI, &draw_engine_workbench_solid);
228                 e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid);
229                 e_data.composite_buffer_tx = DRW_texture_pool_query_2D(
230                         size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_solid);
231
232                 if (NORMAL_ENCODING_ENABLED()) {
233                         e_data.normal_buffer_tx = DRW_texture_pool_query_2D(
234                                 size[0], size[1], GPU_RG16, &draw_engine_workbench_solid);
235                 }
236                 else {
237                         e_data.normal_buffer_tx = DRW_texture_pool_query_2D(
238                                 size[0], size[1], GPU_RGBA32F, &draw_engine_workbench_solid);
239                 }
240
241                 GPU_framebuffer_ensure_config(&fbl->prepass_fb, {
242                         GPU_ATTACHMENT_TEXTURE(dtxl->depth),
243                         GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
244                         GPU_ATTACHMENT_TEXTURE(e_data.color_buffer_tx),
245                         GPU_ATTACHMENT_TEXTURE(e_data.normal_buffer_tx),
246                 });
247                 GPU_framebuffer_ensure_config(&fbl->composite_fb, {
248                         GPU_ATTACHMENT_TEXTURE(dtxl->depth),
249                         GPU_ATTACHMENT_TEXTURE(e_data.composite_buffer_tx),
250                 });
251         }
252
253         /* Prepass */
254         {
255                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
256                 psl->prepass_pass = DRW_pass_create("Prepass", state);
257         }
258 }
259
260 void workbench_deferred_engine_free()
261 {
262         for (int index = 0; index < MAX_SHADERS; index++) {
263                 DRW_SHADER_FREE_SAFE(e_data.prepass_sh_cache[index]);
264                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
265         }
266         DRW_SHADER_FREE_SAFE(e_data.shadow_pass_sh);
267         DRW_SHADER_FREE_SAFE(e_data.shadow_pass_manifold_sh);
268         DRW_SHADER_FREE_SAFE(e_data.shadow_fail_sh);
269         DRW_SHADER_FREE_SAFE(e_data.shadow_fail_manifold_sh);
270         DRW_SHADER_FREE_SAFE(e_data.shadow_caps_sh);
271         DRW_SHADER_FREE_SAFE(e_data.shadow_caps_manifold_sh);
272 }
273
274 static void workbench_composite_uniforms(WORKBENCH_PrivateData *wpd, DRWShadingGroup *grp)
275 {
276         DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &e_data.color_buffer_tx);
277         DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
278         if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) {
279                 DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &e_data.normal_buffer_tx);
280         }
281         DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
282         DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
283
284         workbench_material_set_normal_world_matrix(grp, wpd, e_data.normal_world_matrix);
285 }
286
287 void workbench_deferred_cache_init(WORKBENCH_Data *vedata)
288 {
289         WORKBENCH_StorageList *stl = vedata->stl;
290         WORKBENCH_PassList *psl = vedata->psl;
291         WORKBENCH_PrivateData *wpd = stl->g_data;
292         DRWShadingGroup *grp;
293         const DRWContextState *draw_ctx = DRW_context_state_get();
294         static float light_multiplier = 1.0f;
295
296
297         Scene *scene = draw_ctx->scene;
298
299         select_deferred_shaders(wpd);
300         /* Deferred Mix Pass */
301         {
302                 copy_v3_v3(e_data.display.light_direction, scene->display.light_direction);
303                 negate_v3(e_data.display.light_direction);
304 #if 0
305         if (STUDIOLIGHT_ORIENTATION_WORLD_ENABLED(wpd)) {
306                 BKE_studiolight_ensure_flag(wpd->studio_light, STUDIOLIGHT_LIGHT_DIRECTION_CALCULATED);
307                 float rot_matrix[3][3];
308                 // float dir[3] = {0.57, 0.57, -0.57};
309                 axis_angle_to_mat3_single(rot_matrix, 'Z', wpd->shading.studiolight_rot_z);
310                 mul_v3_m3v3(e_data.display.light_direction, rot_matrix, wpd->studio_light->light_direction);
311         }
312 #endif
313                 float view_matrix[4][4];
314                 DRW_viewport_matrix_get(view_matrix, DRW_MAT_VIEW);
315                 mul_v3_mat3_m4v3(e_data.light_direction_vs, view_matrix, e_data.display.light_direction);
316
317                 e_data.display.shadow_shift = scene->display.shadow_shift;
318
319                 if (SHADOW_ENABLED(wpd)) {
320                         psl->composite_pass = DRW_pass_create(
321                                 "Composite", DRW_STATE_WRITE_COLOR | DRW_STATE_STENCIL_EQUAL);
322                         grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
323                         workbench_composite_uniforms(wpd, grp);
324                         DRW_shgroup_stencil_mask(grp, 0x00);
325                         DRW_shgroup_uniform_vec3(grp, "lightDirection", e_data.light_direction_vs, 1);
326                         DRW_shgroup_uniform_float(grp, "lightMultiplier", &light_multiplier, 1);
327                         DRW_shgroup_uniform_float(grp, "shadowMultiplier", &wpd->shadow_multiplier, 1);
328                         DRW_shgroup_uniform_float(grp, "shadowShift", &scene->display.shadow_shift, 1);
329                         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
330
331                         /* Stencil Shadow passes. */
332 #ifdef DEBUG_SHADOW_VOLUME
333                         DRWState depth_pass_state = DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE;
334                         DRWState depth_fail_state = DRW_STATE_DEPTH_GREATER_EQUAL | DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE;
335 #else
336                         DRWState depth_pass_state = DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_STENCIL_SHADOW_PASS;
337                         DRWState depth_fail_state = DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_STENCIL_SHADOW_FAIL;
338 #endif
339                         psl->shadow_depth_pass_pass = DRW_pass_create("Shadow Pass", depth_pass_state);
340                         psl->shadow_depth_pass_mani_pass = DRW_pass_create("Shadow Pass Mani", depth_pass_state);
341                         psl->shadow_depth_fail_pass = DRW_pass_create("Shadow Fail", depth_fail_state);
342                         psl->shadow_depth_fail_mani_pass = DRW_pass_create("Shadow Fail Mani", depth_fail_state);
343                         psl->shadow_depth_fail_caps_pass = DRW_pass_create("Shadow Fail Caps", depth_fail_state);
344                         psl->shadow_depth_fail_caps_mani_pass = DRW_pass_create("Shadow Fail Caps Mani", depth_fail_state);
345
346 #ifndef DEBUG_SHADOW_VOLUME
347                         grp = DRW_shgroup_create(e_data.shadow_caps_sh, psl->shadow_depth_fail_caps_pass);
348                         DRW_shgroup_stencil_mask(grp, 0xFF);
349                         grp = DRW_shgroup_create(e_data.shadow_pass_sh, psl->shadow_depth_pass_pass);
350                         DRW_shgroup_stencil_mask(grp, 0xFF);
351                         grp = DRW_shgroup_create(e_data.shadow_pass_manifold_sh, psl->shadow_depth_pass_mani_pass);
352                         DRW_shgroup_stencil_mask(grp, 0xFF);
353                         grp = DRW_shgroup_create(e_data.shadow_fail_sh, psl->shadow_depth_fail_pass);
354                         DRW_shgroup_stencil_mask(grp, 0xFF);
355                         grp = DRW_shgroup_create(e_data.shadow_fail_manifold_sh, psl->shadow_depth_fail_mani_pass);
356                         DRW_shgroup_stencil_mask(grp, 0xFF);
357                         grp = DRW_shgroup_create(e_data.shadow_caps_sh, psl->shadow_depth_fail_caps_pass);
358                         DRW_shgroup_stencil_mask(grp, 0xFF);
359                         grp = DRW_shgroup_create(e_data.shadow_caps_manifold_sh, psl->shadow_depth_fail_caps_mani_pass);
360                         DRW_shgroup_stencil_mask(grp, 0xFF);
361
362                         psl->composite_shadow_pass = DRW_pass_create("Composite Shadow", DRW_STATE_WRITE_COLOR | DRW_STATE_STENCIL_NEQUAL);
363                         grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_shadow_pass);
364                         DRW_shgroup_stencil_mask(grp, 0x00);
365                         workbench_composite_uniforms(wpd, grp);
366                         DRW_shgroup_uniform_vec3(grp, "lightDirection", e_data.light_direction_vs, 1);
367                         DRW_shgroup_uniform_float(grp, "lightMultiplier", &wpd->shadow_multiplier, 1);
368                         DRW_shgroup_uniform_float(grp, "shadowMultiplier", &wpd->shadow_multiplier, 1);
369                         DRW_shgroup_uniform_float(grp, "shadowShift", &scene->display.shadow_shift, 1);
370                         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
371 #endif
372                 }
373                 else {
374                         psl->composite_pass = DRW_pass_create(
375                                 "Composite", DRW_STATE_WRITE_COLOR);
376                         grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
377                         workbench_composite_uniforms(wpd, grp);
378                         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
379                 }
380         }
381 }
382
383 static WORKBENCH_MaterialData *get_or_create_material_data(
384         WORKBENCH_Data *vedata, Object *ob, Material *mat, Image *ima, int drawtype)
385 {
386         WORKBENCH_StorageList *stl = vedata->stl;
387         WORKBENCH_PassList *psl = vedata->psl;
388         WORKBENCH_PrivateData *wpd = stl->g_data;
389         WORKBENCH_MaterialData *material;
390         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
391                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
392         WORKBENCH_MaterialData material_template;
393
394         /* Solid */
395         workbench_material_get_solid_color(wpd, ob, mat, material_template.color);
396         material_template.object_id = engine_object_data->object_id;
397         material_template.drawtype = drawtype;
398         material_template.ima = ima;
399         uint hash = workbench_material_get_hash(&material_template);
400
401         material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
402         if (material == NULL) {
403                 material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
404                 material->shgrp = DRW_shgroup_create(
405                         drawtype == OB_SOLID ? wpd->prepass_solid_sh : wpd->prepass_texture_sh, psl->prepass_pass);
406                 DRW_shgroup_stencil_mask(material->shgrp, 0xFF);
407                 material->object_id = engine_object_data->object_id;
408                 copy_v4_v4(material->color, material_template.color);
409                 switch (drawtype) {
410                         case OB_SOLID:
411                                 DRW_shgroup_uniform_vec3(material->shgrp, "object_color", material->color, 1);
412                                 break;
413
414                         case OB_TEXTURE:
415                         {
416                                 GPUTexture *tex = GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D, false, false, false);
417                                 DRW_shgroup_uniform_texture(material->shgrp, "image", tex);
418                                 break;
419                         }
420                 }
421                 DRW_shgroup_uniform_int(material->shgrp, "object_id", &material->object_id, 1);
422                 BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
423         }
424         return material;
425 }
426
427 static void workbench_cache_populate_particles(WORKBENCH_Data *vedata, Object *ob)
428 {
429         const DRWContextState *draw_ctx = DRW_context_state_get();
430         if (ob == draw_ctx->object_edit) {
431                 return;
432         }
433         for (ParticleSystem *psys = ob->particlesystem.first; psys != NULL; psys = psys->next) {
434                 if (!psys_check_enabled(ob, psys, false)) {
435                         continue;
436                 }
437                 if (!DRW_check_psys_visible_within_active_context(ob, psys)) {
438                         return;
439                 }
440                 ParticleSettings *part = psys->part;
441                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
442
443                 static float mat[4][4];
444                 unit_m4(mat);
445
446                 if (draw_as == PART_DRAW_PATH) {
447                         struct Gwn_Batch *geom = DRW_cache_particles_get_hair(ob, psys, NULL);
448                         WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, NULL, NULL, OB_SOLID);
449                         DRW_shgroup_call_add(material->shgrp, geom, mat);
450                 }
451         }
452 }
453
454 void workbench_deferred_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob)
455 {
456         WORKBENCH_StorageList *stl = vedata->stl;
457         WORKBENCH_PassList *psl = vedata->psl;
458         WORKBENCH_PrivateData *wpd = stl->g_data;
459
460         if (!DRW_object_is_renderable(ob))
461                 return;
462
463         if (ob->type == OB_MESH) {
464                 workbench_cache_populate_particles(vedata, ob);
465         }
466
467         WORKBENCH_MaterialData *material;
468         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
469                 const DRWContextState *draw_ctx = DRW_context_state_get();
470                 const bool is_active = (ob == draw_ctx->obact);
471                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
472                 bool is_drawn = false;
473                 if (!is_sculpt_mode && wpd->drawtype == OB_TEXTURE && ob->type == OB_MESH) {
474                         const Mesh *me = ob->data;
475                         if (me->mloopuv) {
476                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
477                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
478                                 struct Gwn_Batch **geom_array = me->totcol ? DRW_cache_mesh_surface_texpaint_get(ob) : NULL;
479                                 if (materials_len > 0 && geom_array) {
480                                         for (int i = 0; i < materials_len; i++) {
481                                                 Material *mat = give_current_material(ob, i + 1);
482                                                 Image *image;
483                                                 ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL);
484                                                 /* use OB_SOLID when no texture could be determined */
485                                                 int mat_drawtype = OB_SOLID;
486                                                 if (image) {
487                                                         mat_drawtype = OB_TEXTURE;
488                                                 }
489                                                 material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
490                                                 DRW_shgroup_call_object_add(material->shgrp, geom_array[i], ob);
491                                         }
492                                         is_drawn = true;
493                                 }
494                         }
495                 }
496
497                 /* Fallback from not drawn OB_TEXTURE mode or just OB_SOLID mode */
498                 if (!is_drawn) {
499                         if ((wpd->shading.color_type != V3D_SHADING_MATERIAL_COLOR) || is_sculpt_mode) {
500                                 /* No material split needed */
501                                 struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
502                                 if (geom) {
503                                         material = get_or_create_material_data(vedata, ob, NULL, NULL, OB_SOLID);
504                                         if (is_sculpt_mode) {
505                                                 DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
506                                         }
507                                         else {
508                                                 DRW_shgroup_call_object_add(material->shgrp, geom, ob);
509                                         }
510                                 }
511                         }
512                         else { /* MATERIAL colors */
513                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
514                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
515                                 for (int i = 0; i < materials_len; i++) {
516                                         gpumat_array[i] = NULL;
517                                 }
518
519                                 struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(
520                                         ob, gpumat_array, materials_len, NULL, NULL, NULL);
521                                 if (mat_geom) {
522                                         for (int i = 0; i < materials_len; ++i) {
523                                                 Material *mat = give_current_material(ob, i + 1);
524                                                 material = get_or_create_material_data(vedata, ob, mat, NULL, OB_SOLID);
525                                                 DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
526                                         }
527                                 }
528                         }
529                 }
530
531                 if (SHADOW_ENABLED(wpd) && (ob->display.flag & OB_SHOW_SHADOW) > 0) {
532                         bool is_manifold;
533                         struct Gwn_Batch *geom_shadow = DRW_cache_object_edge_detection_get(ob, &is_manifold);
534                         if (geom_shadow) {
535                                 if (is_sculpt_mode) {
536                                         /* Currently unsupported in sculpt mode. We could revert to the slow
537                                          * method in this case but i'm not sure if it's a good idea given that
538                                          * sculped meshes are heavy to begin with. */
539                                         // DRW_shgroup_call_sculpt_add(wpd->shadow_shgrp, ob, ob->obmat);
540                                 }
541                                 else {
542                                         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
543                                                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
544
545                                         invert_m4_m4(ob->imat, ob->obmat);
546                                         mul_v3_mat3_m4v3(engine_object_data->shadow_dir, ob->imat, e_data.display.light_direction);
547
548                                         DRWShadingGroup *grp;
549                                         /* TODO(fclem): only use shadow pass technique if camera is not in shadow. */
550                                         const bool use_shadow_pass_technique = true;
551                                         if (use_shadow_pass_technique) {
552                                                 if (is_manifold) {
553                                                         grp = DRW_shgroup_create(e_data.shadow_pass_manifold_sh, psl->shadow_depth_pass_mani_pass);
554                                                 }
555                                                 else {
556                                                         grp = DRW_shgroup_create(e_data.shadow_pass_sh, psl->shadow_depth_pass_pass);
557                                                 }
558                                                 DRW_shgroup_uniform_vec3(grp, "lightDirection", engine_object_data->shadow_dir, 1);
559                                                 DRW_shgroup_call_object_add(grp, geom_shadow, ob);
560                                         }
561                                         else {
562                                                 /* TODO(fclem): only use caps if they are in the view frustum. */
563                                                 const bool need_caps = true;
564                                                 if (need_caps) {
565                                                         if (is_manifold) {
566                                                                 grp = DRW_shgroup_create(e_data.shadow_caps_manifold_sh, psl->shadow_depth_fail_caps_mani_pass);
567                                                         }
568                                                         else {
569                                                                 grp = DRW_shgroup_create(e_data.shadow_caps_sh, psl->shadow_depth_fail_caps_pass);
570                                                         }
571                                                         DRW_shgroup_uniform_vec3(grp, "lightDirection", engine_object_data->shadow_dir, 1);
572                                                         DRW_shgroup_call_object_add(grp, DRW_cache_object_surface_get(ob), ob);
573                                                 }
574
575                                                 if (is_manifold) {
576                                                         grp = DRW_shgroup_create(e_data.shadow_fail_manifold_sh, psl->shadow_depth_fail_mani_pass);
577                                                 }
578                                                 else {
579                                                         grp = DRW_shgroup_create(e_data.shadow_fail_sh, psl->shadow_depth_fail_pass);
580                                                 }
581                                                 DRW_shgroup_uniform_vec3(grp, "lightDirection", engine_object_data->shadow_dir, 1);
582                                                 DRW_shgroup_call_object_add(grp, geom_shadow, ob);
583                                         }
584                                 }
585                         }
586                 }
587         }
588 }
589
590 void workbench_deferred_cache_finish(WORKBENCH_Data *UNUSED(vedata))
591 {
592 }
593
594 void workbench_deferred_draw_background(WORKBENCH_Data *vedata)
595 {
596         WORKBENCH_StorageList *stl = vedata->stl;
597         WORKBENCH_FramebufferList *fbl = vedata->fbl;
598         WORKBENCH_PrivateData *wpd = stl->g_data;
599         const float clear_depth = 1.0f;
600         const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
601         uint clear_stencil = 0xFF;
602
603         DRW_stats_group_start("Clear Background");
604         GPU_framebuffer_bind(fbl->prepass_fb);
605         int clear_bits = GPU_DEPTH_BIT | GPU_COLOR_BIT;
606         SET_FLAG_FROM_TEST(clear_bits, SHADOW_ENABLED(wpd), GPU_STENCIL_BIT);
607         GPU_framebuffer_clear(fbl->prepass_fb, clear_bits, clear_color, clear_depth, clear_stencil);
608         DRW_stats_group_end();
609 }
610
611 void workbench_deferred_draw_scene(WORKBENCH_Data *vedata)
612 {
613         WORKBENCH_PassList *psl = vedata->psl;
614         WORKBENCH_StorageList *stl = vedata->stl;
615         WORKBENCH_FramebufferList *fbl = vedata->fbl;
616         WORKBENCH_PrivateData *wpd = stl->g_data;
617         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
618
619         /* clear in background */
620         GPU_framebuffer_bind(fbl->prepass_fb);
621         DRW_draw_pass(psl->prepass_pass);
622         if (SHADOW_ENABLED(wpd)) {
623 #ifdef DEBUG_SHADOW_VOLUME
624                 GPU_framebuffer_bind(fbl->composite_fb);
625                 DRW_draw_pass(psl->composite_pass);
626 #else
627                 GPU_framebuffer_bind(dfbl->depth_only_fb);
628 #endif
629                 DRW_draw_pass(psl->shadow_depth_pass_pass);
630                 DRW_draw_pass(psl->shadow_depth_pass_mani_pass);
631                 DRW_draw_pass(psl->shadow_depth_fail_pass);
632                 DRW_draw_pass(psl->shadow_depth_fail_mani_pass);
633                 DRW_draw_pass(psl->shadow_depth_fail_caps_pass);
634                 DRW_draw_pass(psl->shadow_depth_fail_caps_mani_pass);
635 #ifndef DEBUG_SHADOW_VOLUME
636                 GPU_framebuffer_bind(fbl->composite_fb);
637                 DRW_draw_pass(psl->composite_pass);
638                 DRW_draw_pass(psl->composite_shadow_pass);
639 #endif
640         }
641         else {
642                 GPU_framebuffer_bind(fbl->composite_fb);
643                 DRW_draw_pass(psl->composite_pass);
644         }
645
646         GPU_framebuffer_bind(dfbl->color_only_fb);
647         DRW_transform_to_display(e_data.composite_buffer_tx);
648
649         workbench_private_data_free(wpd);
650 }