DRW: disable clipping background drawing w/ xray mode
[blender.git] / source / blender / draw / engines / workbench / workbench_forward.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * Copyright 2016, Blender Foundation.
19  * Contributor(s): Blender Institute
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  *
23  */
24
25 /** \file workbench_forward.c
26  *  \ingroup draw_engine
27  */
28
29 #include "workbench_private.h"
30
31 #include "BIF_gl.h"
32
33 #include "BLI_alloca.h"
34 #include "BLI_dynstr.h"
35 #include "BLI_string_utils.h"
36 #include "BLI_utildefines.h"
37
38 #include "BKE_node.h"
39 #include "BKE_particle.h"
40 #include "BKE_modifier.h"
41 #include "BKE_object.h"
42
43 #include "DNA_image_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_node_types.h"
47
48 #include "ED_uvedit.h"
49
50 #include "GPU_shader.h"
51 #include "GPU_texture.h"
52
53 #include "UI_resources.h"
54
55 /* *********** STATIC *********** */
56 static struct {
57         struct GPUShader *composite_sh_cache[2];
58         struct GPUShader *transparent_accum_sh_cache[MAX_ACCUM_SHADERS];
59         struct GPUShader *object_outline_sh;
60         struct GPUShader *object_outline_texture_sh;
61         struct GPUShader *object_outline_hair_sh;
62         struct GPUShader *checker_depth_sh;
63
64         struct GPUTexture *object_id_tx; /* ref only, not alloced */
65         struct GPUTexture *transparent_accum_tx; /* ref only, not alloced */
66         struct GPUTexture *transparent_revealage_tx; /* ref only, not alloced */
67         struct GPUTexture *composite_buffer_tx; /* ref only, not alloced */
68
69         int next_object_id;
70 } e_data = {{NULL}};
71
72 /* Shaders */
73 extern char datatoc_common_world_clip_lib_glsl[];
74 extern char datatoc_common_hair_lib_glsl[];
75
76 extern char datatoc_workbench_forward_composite_frag_glsl[];
77 extern char datatoc_workbench_forward_depth_frag_glsl[];
78 extern char datatoc_workbench_forward_transparent_accum_frag_glsl[];
79 extern char datatoc_workbench_data_lib_glsl[];
80 extern char datatoc_workbench_background_lib_glsl[];
81 extern char datatoc_workbench_checkerboard_depth_frag_glsl[];
82 extern char datatoc_workbench_object_outline_lib_glsl[];
83 extern char datatoc_workbench_curvature_lib_glsl[];
84 extern char datatoc_workbench_prepass_vert_glsl[];
85 extern char datatoc_workbench_common_lib_glsl[];
86 extern char datatoc_workbench_world_light_lib_glsl[];
87
88 /* static functions */
89 static char *workbench_build_forward_vert(bool is_hair)
90 {
91         char *str = NULL;
92         if (!is_hair) {
93                 return BLI_string_joinN(
94                         datatoc_common_world_clip_lib_glsl,
95                         datatoc_workbench_prepass_vert_glsl);
96         }
97
98         DynStr *ds = BLI_dynstr_new();
99
100         BLI_dynstr_append(ds, datatoc_common_hair_lib_glsl);
101         BLI_dynstr_append(ds, datatoc_common_world_clip_lib_glsl);
102         BLI_dynstr_append(ds, datatoc_workbench_prepass_vert_glsl);
103
104         str = BLI_dynstr_get_cstring(ds);
105         BLI_dynstr_free(ds);
106         return str;
107 }
108
109 static char *workbench_build_forward_transparent_accum_frag(void)
110 {
111         char *str = NULL;
112
113         DynStr *ds = BLI_dynstr_new();
114
115         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
116         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
117         BLI_dynstr_append(ds, datatoc_workbench_world_light_lib_glsl);
118         BLI_dynstr_append(ds, datatoc_workbench_forward_transparent_accum_frag_glsl);
119
120         str = BLI_dynstr_get_cstring(ds);
121         BLI_dynstr_free(ds);
122         return str;
123 }
124
125 static char *workbench_build_forward_composite_frag(void)
126 {
127         char *str = NULL;
128
129         DynStr *ds = BLI_dynstr_new();
130
131         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
132         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
133         BLI_dynstr_append(ds, datatoc_workbench_background_lib_glsl);
134         BLI_dynstr_append(ds, datatoc_workbench_object_outline_lib_glsl);
135         BLI_dynstr_append(ds, datatoc_workbench_curvature_lib_glsl);
136         BLI_dynstr_append(ds, datatoc_workbench_forward_composite_frag_glsl);
137
138         str = BLI_dynstr_get_cstring(ds);
139         BLI_dynstr_free(ds);
140         return str;
141 }
142
143 static void workbench_init_object_data(DrawData *dd)
144 {
145         WORKBENCH_ObjectData *data = (WORKBENCH_ObjectData *)dd;
146         data->object_id = ((e_data.next_object_id++) & 0xff) + 1;
147 }
148
149 static WORKBENCH_MaterialData *get_or_create_material_data(
150         WORKBENCH_Data *vedata, Object *ob, Material *mat, Image *ima, int color_type, int interp)
151 {
152         WORKBENCH_StorageList *stl = vedata->stl;
153         WORKBENCH_PassList *psl = vedata->psl;
154         WORKBENCH_PrivateData *wpd = stl->g_data;
155         WORKBENCH_MaterialData *material;
156         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_drawdata_ensure(
157                 &ob->id, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
158         WORKBENCH_MaterialData material_template;
159         DRWShadingGroup *grp;
160
161         /* Solid */
162         workbench_material_update_data(wpd, ob, mat, &material_template);
163         material_template.object_id = OBJECT_ID_PASS_ENABLED(wpd) ? engine_object_data->object_id : 1;
164         material_template.color_type = color_type;
165         material_template.ima = ima;
166         material_template.interp = interp;
167         uint hash = workbench_material_get_hash(&material_template, false);
168
169         material = BLI_ghash_lookup(wpd->material_hash, POINTER_FROM_UINT(hash));
170         if (material == NULL) {
171                 material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
172
173                 /* transparent accum */
174                 grp = DRW_shgroup_create(
175                         color_type == V3D_SHADING_TEXTURE_COLOR ? wpd->transparent_accum_texture_sh: wpd->transparent_accum_sh,
176                         psl->transparent_accum_pass);
177                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
178                 DRW_shgroup_uniform_float(grp, "alpha", &wpd->shading.xray_alpha, 1);
179                 DRW_shgroup_uniform_vec4(grp, "viewvecs[0]", (float *)wpd->viewvecs, 3);
180                 workbench_material_copy(material, &material_template);
181                 if (STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd)) {
182                         BKE_studiolight_ensure_flag(wpd->studio_light, STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE);
183                         DRW_shgroup_uniform_texture(grp, "matcapImage", wpd->studio_light->equirect_radiance_gputexture );
184                 }
185                 if (SPECULAR_HIGHLIGHT_ENABLED(wpd) || MATCAP_ENABLED(wpd)) {
186                         DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
187                 }
188
189                 workbench_material_shgroup_uniform(wpd, grp, material, ob, false, false, interp);
190                 material->shgrp = grp;
191
192                 /* Depth */
193                 if (workbench_material_determine_color_type(wpd, material->ima, ob) == V3D_SHADING_TEXTURE_COLOR) {
194                         material->shgrp_object_outline = DRW_shgroup_create(
195                                 e_data.object_outline_texture_sh, psl->object_outline_pass);
196                         GPUTexture *tex = GPU_texture_from_blender(material->ima, NULL, GL_TEXTURE_2D, false, 0.0f);
197                         DRW_shgroup_uniform_texture(material->shgrp_object_outline, "image", tex);
198                 }
199                 else {
200                         material->shgrp_object_outline = DRW_shgroup_create(
201                                 e_data.object_outline_sh, psl->object_outline_pass);
202                 }
203                 material->object_id = engine_object_data->object_id;
204                 DRW_shgroup_uniform_int(material->shgrp_object_outline, "object_id", &material->object_id, 1);
205                 if (wpd->world_clip_planes) {
206                         const DRWContextState *draw_ctx = DRW_context_state_get();
207                         RegionView3D *rv3d = draw_ctx->rv3d;
208                         DRW_shgroup_world_clip_planes_from_rv3d(material->shgrp_object_outline, rv3d);
209                 }
210                 BLI_ghash_insert(wpd->material_hash, POINTER_FROM_UINT(hash), material);
211         }
212         return material;
213 }
214
215 static GPUShader *ensure_forward_accum_shaders(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair)
216 {
217         int index = workbench_material_get_accum_shader_index(wpd, use_textures, is_hair);
218         if (e_data.transparent_accum_sh_cache[index] == NULL) {
219                 char *defines = workbench_material_build_defines(wpd, use_textures, is_hair);
220                 char *transparent_accum_vert = workbench_build_forward_vert(is_hair);
221                 char *transparent_accum_frag = workbench_build_forward_transparent_accum_frag();
222                 e_data.transparent_accum_sh_cache[index] = DRW_shader_create(
223                         transparent_accum_vert, NULL,
224                         transparent_accum_frag, defines);
225                 MEM_freeN(transparent_accum_vert);
226                 MEM_freeN(transparent_accum_frag);
227                 MEM_freeN(defines);
228         }
229         return e_data.transparent_accum_sh_cache[index];
230 }
231
232 static GPUShader *ensure_forward_composite_shaders(WORKBENCH_PrivateData *wpd)
233 {
234         int index = OBJECT_OUTLINE_ENABLED(wpd) ? 1 : 0;
235         if (e_data.composite_sh_cache[index] == NULL) {
236                 char *defines = workbench_material_build_defines(wpd, false, false);
237                 char *composite_frag = workbench_build_forward_composite_frag();
238                 e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines);
239                 MEM_freeN(composite_frag);
240                 MEM_freeN(defines);
241         }
242         return e_data.composite_sh_cache[index];
243 }
244
245 static void select_forward_shaders(WORKBENCH_PrivateData *wpd)
246 {
247         wpd->composite_sh = ensure_forward_composite_shaders(wpd);
248         wpd->transparent_accum_sh = ensure_forward_accum_shaders(wpd, false, false);
249         wpd->transparent_accum_hair_sh = ensure_forward_accum_shaders(wpd, false, true);
250         wpd->transparent_accum_texture_sh = ensure_forward_accum_shaders(wpd, true, false);
251         wpd->transparent_accum_texture_hair_sh = ensure_forward_accum_shaders(wpd, true, true);
252 }
253
254 /* public functions */
255 void workbench_forward_engine_init(WORKBENCH_Data *vedata)
256 {
257         WORKBENCH_FramebufferList *fbl = vedata->fbl;
258         WORKBENCH_PassList *psl = vedata->psl;
259         WORKBENCH_StorageList *stl = vedata->stl;
260         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
261         const DRWContextState *draw_ctx = DRW_context_state_get();
262         DRWShadingGroup *grp;
263
264         if (!stl->g_data) {
265                 /* Alloc transient pointers */
266                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
267         }
268         if (!stl->effects) {
269                 stl->effects = MEM_callocN(sizeof(*stl->effects), __func__);
270                 workbench_effect_info_init(stl->effects);
271         }
272         WORKBENCH_PrivateData *wpd = stl->g_data;
273         workbench_private_data_init(wpd);
274         float light_direction[3];
275         workbench_private_data_get_light_direction(wpd, light_direction);
276
277         if (!e_data.next_object_id) {
278                 e_data.next_object_id = 1;
279                 memset(e_data.composite_sh_cache, 0x00, sizeof(e_data.composite_sh_cache));
280                 memset(e_data.transparent_accum_sh_cache, 0x00, sizeof(e_data.transparent_accum_sh_cache));
281
282                 char *defines = workbench_material_build_defines(wpd, false, false);
283                 char *defines_texture = workbench_material_build_defines(wpd, true, false);
284                 char *defines_hair = workbench_material_build_defines(wpd, false, true);
285                 char *forward_vert = workbench_build_forward_vert(false);
286                 char *forward_hair_vert = workbench_build_forward_vert(true);
287                 e_data.object_outline_sh = DRW_shader_create(
288                         forward_vert, NULL,
289                         datatoc_workbench_forward_depth_frag_glsl, defines);
290                 e_data.object_outline_texture_sh = DRW_shader_create(
291                         forward_vert, NULL,
292                         datatoc_workbench_forward_depth_frag_glsl, defines_texture);
293                 e_data.object_outline_hair_sh = DRW_shader_create(
294                         forward_hair_vert, NULL,
295                         datatoc_workbench_forward_depth_frag_glsl, defines_hair);
296
297
298                 e_data.checker_depth_sh = DRW_shader_create_fullscreen(
299                         datatoc_workbench_checkerboard_depth_frag_glsl, NULL);
300                 MEM_freeN(forward_hair_vert);
301                 MEM_freeN(forward_vert);
302                 MEM_freeN(defines);
303                 MEM_freeN(defines_texture);
304                 MEM_freeN(defines_hair);
305         }
306         workbench_volume_engine_init();
307         workbench_fxaa_engine_init();
308         workbench_taa_engine_init(vedata);
309
310         select_forward_shaders(wpd);
311
312         const float *viewport_size = DRW_viewport_size_get();
313         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
314
315         e_data.object_id_tx = DRW_texture_pool_query_2D(
316                 size[0], size[1], GPU_R32UI, &draw_engine_workbench_transparent);
317         e_data.transparent_accum_tx = DRW_texture_pool_query_2D(
318                 size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_transparent);
319         e_data.transparent_revealage_tx = DRW_texture_pool_query_2D(
320                 size[0], size[1], GPU_R16F, &draw_engine_workbench_transparent);
321         e_data.composite_buffer_tx = DRW_texture_pool_query_2D(
322                 size[0], size[1], GPU_R11F_G11F_B10F, &draw_engine_workbench_transparent);
323
324         GPU_framebuffer_ensure_config(&fbl->object_outline_fb, {
325                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
326                 GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
327         });
328         GPU_framebuffer_ensure_config(&fbl->transparent_accum_fb, {
329                 GPU_ATTACHMENT_NONE,
330                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_accum_tx),
331                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_revealage_tx),
332         });
333         GPU_framebuffer_ensure_config(&fbl->composite_fb, {
334                 GPU_ATTACHMENT_NONE,
335                 GPU_ATTACHMENT_TEXTURE(e_data.composite_buffer_tx),
336         });
337         GPU_framebuffer_ensure_config(&fbl->effect_fb, {
338                 GPU_ATTACHMENT_NONE,
339                 GPU_ATTACHMENT_TEXTURE(e_data.transparent_accum_tx),
340         });
341
342         workbench_volume_cache_init(vedata);
343         const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->shading.flag & V3D_SHADING_BACKFACE_CULLING));
344         const int cull_state = (do_cull) ? DRW_STATE_CULL_BACK : 0;
345
346         /* Transparency Accum */
347         {
348                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_OIT | cull_state;
349                 psl->transparent_accum_pass = DRW_pass_create("Transparent Accum", state);
350         }
351         /* Depth */
352         {
353                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | cull_state;
354                 psl->object_outline_pass = DRW_pass_create("Object Outline Pass", state);
355         }
356         /* Composite */
357         {
358                 int state = DRW_STATE_WRITE_COLOR;
359                 psl->composite_pass = DRW_pass_create("Composite", state);
360
361                 grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
362                 if (OBJECT_ID_PASS_ENABLED(wpd)) {
363                         DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
364                 }
365                 DRW_shgroup_uniform_texture_ref(grp, "transparentAccum", &e_data.transparent_accum_tx);
366                 DRW_shgroup_uniform_texture_ref(grp, "transparentRevealage", &e_data.transparent_revealage_tx);
367                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
368                 DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
369                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
370         }
371
372         /* TODO(campbell): displays but masks geometry. */
373         if (0 && (draw_ctx->rv3d && (draw_ctx->rv3d->rflag & RV3D_CLIPPING) && draw_ctx->rv3d->clipbb)) {
374                 psl->background_pass = DRW_pass_create(
375                         "Background", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL);
376                 GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR_BACKGROUND);
377                 grp = DRW_shgroup_create(shader, psl->background_pass);
378                 wpd->world_clip_planes_batch = DRW_draw_background_clipping_batch_from_rv3d(draw_ctx->rv3d);
379                 DRW_shgroup_call_add(grp, wpd->world_clip_planes_batch, NULL);
380                 DRW_shgroup_uniform_vec4(grp, "color", &wpd->world_clip_planes_color[0], 1);
381         }
382
383         {
384                 workbench_aa_create_pass(vedata, &e_data.transparent_accum_tx);
385         }
386
387         /* Checker Depth */
388         {
389                 static float noise_offset = 0.0f;
390                 float blend_threshold = 0.0f;
391
392                 if (DRW_state_is_image_render()) {
393                         /* TODO: Should be based on the number of samples used for render. */
394                         noise_offset = fmodf(noise_offset + 1.0f / 8.0f, 1.0f);
395                 }
396
397                 if (wpd->shading.flag & XRAY_FLAG(wpd)) {
398                         blend_threshold = 1.0f - XRAY_ALPHA(wpd) * 0.9f;
399                 }
400
401                 if (wpd->shading.type == OB_WIRE) {
402                         wpd->shading.xray_alpha = 0.0f;
403                         wpd->shading.xray_alpha_wire = 0.0f;
404                 }
405
406                 int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS;
407                 psl->checker_depth_pass = DRW_pass_create("Checker Depth", state);
408                 grp = DRW_shgroup_create(e_data.checker_depth_sh, psl->checker_depth_pass);
409                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
410                 DRW_shgroup_uniform_float_copy(grp, "threshold", blend_threshold);
411                 DRW_shgroup_uniform_float_copy(grp, "offset", noise_offset);
412         }
413 }
414
415 void workbench_forward_engine_free()
416 {
417         for (int index = 0; index < 2; index++) {
418                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
419         }
420         for (int index = 0; index < MAX_ACCUM_SHADERS; index++) {
421                 DRW_SHADER_FREE_SAFE(e_data.transparent_accum_sh_cache[index]);
422         }
423         DRW_SHADER_FREE_SAFE(e_data.object_outline_sh);
424         DRW_SHADER_FREE_SAFE(e_data.object_outline_texture_sh);
425         DRW_SHADER_FREE_SAFE(e_data.object_outline_hair_sh);
426         DRW_SHADER_FREE_SAFE(e_data.checker_depth_sh);
427
428         workbench_volume_engine_free();
429         workbench_fxaa_engine_free();
430         workbench_taa_engine_free();
431 }
432
433 void workbench_forward_cache_init(WORKBENCH_Data *UNUSED(vedata))
434 {
435 }
436
437 static void workbench_forward_cache_populate_particles(WORKBENCH_Data *vedata, Object *ob)
438 {
439         WORKBENCH_StorageList *stl = vedata->stl;
440         WORKBENCH_PassList *psl = vedata->psl;
441         WORKBENCH_PrivateData *wpd = stl->g_data;
442
443         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
444                 if (md->type != eModifierType_ParticleSystem) {
445                         continue;
446                 }
447                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
448                 if (!psys_check_enabled(ob, psys, false)) {
449                         continue;
450                 }
451                 if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
452                         continue;
453                 }
454                 ParticleSettings *part = psys->part;
455                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
456
457                 if (draw_as == PART_DRAW_PATH) {
458                         Material *mat;
459                         Image *image;
460                         int interp;
461                         workbench_material_get_image_and_mat(ob, part->omat, &image, &interp, &mat);
462                         int color_type = workbench_material_determine_color_type(wpd, image, ob);
463                         WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, mat, image, color_type, interp);
464
465                         struct GPUShader *shader = (color_type != V3D_SHADING_TEXTURE_COLOR)
466                                                    ? wpd->transparent_accum_hair_sh
467                                                    : wpd->transparent_accum_texture_hair_sh;
468                         DRWShadingGroup *shgrp = DRW_shgroup_hair_create(
469                                                         ob, psys, md,
470                                                         psl->transparent_accum_pass,
471                                                         shader);
472                         DRW_shgroup_uniform_block(shgrp, "world_block", wpd->world_ubo);
473                         workbench_material_shgroup_uniform(wpd, shgrp, material, ob, false, false, interp);
474                         DRW_shgroup_uniform_vec4(shgrp, "viewvecs[0]", (float *)wpd->viewvecs, 3);
475                         /* Hairs have lots of layer and can rapidly become the most prominent surface.
476                          * So lower their alpha artificially. */
477                         float hair_alpha = XRAY_ALPHA(wpd) * 0.33f;
478                         DRW_shgroup_uniform_float_copy(shgrp, "alpha", hair_alpha);
479                         if (STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd)) {
480                                 BKE_studiolight_ensure_flag(wpd->studio_light, STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE);
481                                 DRW_shgroup_uniform_texture(shgrp, "matcapImage", wpd->studio_light->equirect_radiance_gputexture );
482                         }
483                         if (SPECULAR_HIGHLIGHT_ENABLED(wpd) || MATCAP_ENABLED(wpd)) {
484                                 DRW_shgroup_uniform_vec2(shgrp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
485                         }
486                         shgrp = DRW_shgroup_hair_create(ob, psys, md,
487                                                 vedata->psl->object_outline_pass,
488                                                 e_data.object_outline_hair_sh);
489                         DRW_shgroup_uniform_int(shgrp, "object_id", &material->object_id, 1);
490                 }
491         }
492 }
493
494 void workbench_forward_cache_populate(WORKBENCH_Data *vedata, Object *ob)
495 {
496         WORKBENCH_StorageList *stl = vedata->stl;
497         WORKBENCH_PrivateData *wpd = stl->g_data;
498         const DRWContextState *draw_ctx = DRW_context_state_get();
499         Scene *scene = draw_ctx->scene;
500         const bool is_wire = (ob->dt == OB_WIRE);
501
502         if (!DRW_object_is_renderable(ob)) {
503                 return;
504         }
505
506         if (ob->type == OB_MESH) {
507                 workbench_forward_cache_populate_particles(vedata, ob);
508         }
509
510         ModifierData *md;
511         if (((ob->base_flag & BASE_FROM_DUPLI) == 0) &&
512             (md = modifiers_findByType(ob, eModifierType_Smoke)) &&
513             (modifier_isEnabled(scene, md, eModifierMode_Realtime)) &&
514             (((SmokeModifierData *)md)->domain != NULL))
515         {
516                 workbench_volume_cache_populate(vedata, scene, ob, md);
517                 return; /* Do not draw solid in this case. */
518         }
519
520         if (!(DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) {
521                 return;
522         }
523         if (ob->dt < OB_WIRE) {
524                 return;
525         }
526
527         WORKBENCH_MaterialData *material;
528         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
529                 const bool is_active = (ob == draw_ctx->obact);
530                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
531                 bool is_drawn = false;
532
533                 if (!is_sculpt_mode && TEXTURE_DRAWING_ENABLED(wpd) && ELEM(ob->type, OB_MESH)) {
534                         const Mesh *me = ob->data;
535                         if (me->mloopuv) {
536                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
537                                 struct GPUBatch **geom_array = DRW_cache_mesh_surface_texpaint_get(ob);
538                                 for (int i = 0; i < materials_len; i++) {
539                                         Material *mat;
540                                         Image *image;
541                                         int interp;
542                                         workbench_material_get_image_and_mat(ob, i + 1, &image, &interp, &mat);
543                                         int color_type = workbench_material_determine_color_type(wpd, image, ob);
544                                         material = get_or_create_material_data(vedata, ob, mat, image, color_type, interp);
545                                         DRW_shgroup_call_object_add(material->shgrp_object_outline, geom_array[i], ob);
546                                         DRW_shgroup_call_object_add(material->shgrp, geom_array[i], ob);
547                                 }
548                                 is_drawn = true;
549                         }
550                 }
551
552                 /* Fallback from not drawn OB_TEXTURE mode or just OB_SOLID mode */
553                 if (!is_drawn) {
554                         if (ELEM(wpd->shading.color_type,
555                                  V3D_SHADING_SINGLE_COLOR, V3D_SHADING_OBJECT_COLOR, V3D_SHADING_RANDOM_COLOR))
556                         {
557                                 /* No material split needed */
558                                 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
559                                 if (geom) {
560                                         material = get_or_create_material_data(vedata, ob, NULL, NULL, wpd->shading.color_type, 0);
561                                         if (is_sculpt_mode) {
562                                                 DRW_shgroup_call_sculpt_add(material->shgrp_object_outline, ob, ob->obmat);
563                                                 if (!is_wire) {
564                                                         DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
565                                                 }
566                                         }
567                                         else {
568                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, geom, ob);
569                                                 if (!is_wire) {
570                                                         DRW_shgroup_call_object_add(material->shgrp, geom, ob);
571                                                 }
572                                         }
573                                 }
574                         }
575                         else {
576                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
577                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
578                                 for (int i = 0; i < materials_len; i++) {
579                                         gpumat_array[i] = NULL;
580                                 }
581
582                                 struct GPUBatch **mat_geom = DRW_cache_object_surface_material_get(
583                                         ob, gpumat_array, materials_len, NULL, NULL, NULL);
584                                 if (mat_geom) {
585                                         for (int i = 0; i < materials_len; ++i) {
586                                                 if (mat_geom[i] == NULL) {
587                                                         continue;
588                                                 }
589
590                                                 Material *mat = give_current_material(ob, i + 1);
591                                                 material = get_or_create_material_data(vedata, ob, mat, NULL, V3D_SHADING_MATERIAL_COLOR, 0);
592                                                 if (is_sculpt_mode) {
593                                                         DRW_shgroup_call_sculpt_add(material->shgrp_object_outline, ob, ob->obmat);
594                                                         if (!is_wire) {
595                                                                 DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
596                                                         }
597                                                 }
598                                                 else {
599                                                         DRW_shgroup_call_object_add(material->shgrp_object_outline, mat_geom[i], ob);
600                                                         if (!is_wire) {
601                                                                 DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
602                                                         }
603                                                 }
604                                         }
605                                 }
606                         }
607                 }
608         }
609 }
610
611 void workbench_forward_cache_finish(WORKBENCH_Data *UNUSED(vedata))
612 {
613 }
614
615 void workbench_forward_draw_background(WORKBENCH_Data *UNUSED(vedata))
616 {
617         const float clear_depth = 1.0f;
618         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
619         DRW_stats_group_start("Clear depth");
620         GPU_framebuffer_bind(dfbl->default_fb);
621         GPU_framebuffer_clear_depth(dfbl->default_fb, clear_depth);
622         DRW_stats_group_end();
623 }
624
625 void workbench_forward_draw_scene(WORKBENCH_Data *vedata)
626 {
627         WORKBENCH_PassList *psl = vedata->psl;
628         WORKBENCH_StorageList *stl = vedata->stl;
629         WORKBENCH_FramebufferList *fbl = vedata->fbl;
630         WORKBENCH_PrivateData *wpd = stl->g_data;
631         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
632
633         if (TAA_ENABLED(wpd)) {
634                 workbench_taa_draw_scene_start(vedata);
635         }
636
637         /* Write Depth + Object ID */
638         const float clear_outline[4] = {0.0f};
639         GPU_framebuffer_bind(fbl->object_outline_fb);
640         GPU_framebuffer_clear_color(fbl->object_outline_fb, clear_outline);
641         DRW_draw_pass(psl->object_outline_pass);
642
643         if (XRAY_ALPHA(wpd) > 0.0) {
644                 const float clear_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
645                 GPU_framebuffer_bind(fbl->transparent_accum_fb);
646                 GPU_framebuffer_clear_color(fbl->transparent_accum_fb, clear_color);
647                 DRW_draw_pass(psl->transparent_accum_pass);
648         }
649         else {
650                 /* TODO(fclem): this is unnecessary and takes up perf.
651                  * Better change the composite frag shader to not use the tx. */
652                 const float clear_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
653                 GPU_framebuffer_bind(fbl->transparent_accum_fb);
654                 GPU_framebuffer_clear_color(fbl->transparent_accum_fb, clear_color);
655         }
656
657         /* Composite */
658         GPU_framebuffer_bind(fbl->composite_fb);
659         DRW_draw_pass(psl->composite_pass);
660         DRW_draw_pass(psl->volume_pass);
661
662         /* Only when clipping is enabled. */
663         if (psl->background_pass) {
664                 DRW_draw_pass(psl->background_pass);
665         }
666
667         /* Color correct and Anti aliasing */
668         workbench_aa_draw_pass(vedata, e_data.composite_buffer_tx);
669
670         /* Apply checker pattern */
671         GPU_framebuffer_bind(dfbl->depth_only_fb);
672         DRW_draw_pass(psl->checker_depth_pass);
673 }
674
675 void workbench_forward_draw_finish(WORKBENCH_Data *vedata)
676 {
677         WORKBENCH_StorageList *stl = vedata->stl;
678         WORKBENCH_PrivateData *wpd = stl->g_data;
679
680         workbench_private_data_free(wpd);
681         workbench_volume_smoke_textures_free(wpd);
682 }