80c35f3cfcd2996755cffcc7b87350b47f31fd05
[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         if (draw_ctx->rv3d && (draw_ctx->rv3d->rflag & RV3D_CLIPPING) && draw_ctx->rv3d->clipbb) {
373                 psl->background_pass = DRW_pass_create(
374                         "Background", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL);
375                 GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR_BACKGROUND);
376                 grp = DRW_shgroup_create(shader, psl->background_pass);
377                 wpd->world_clip_planes_batch = DRW_draw_background_clipping_batch_from_rv3d(draw_ctx->rv3d);
378                 DRW_shgroup_call_add(grp, wpd->world_clip_planes_batch, NULL);
379                 DRW_shgroup_uniform_vec4(grp, "color", &wpd->world_clip_planes_color[0], 1);
380         }
381
382         {
383                 workbench_aa_create_pass(vedata, &e_data.transparent_accum_tx);
384         }
385
386         /* Checker Depth */
387         {
388                 static float noise_offset = 0.0f;
389                 float blend_threshold = 0.0f;
390
391                 if (DRW_state_is_image_render()) {
392                         /* TODO: Should be based on the number of samples used for render. */
393                         noise_offset = fmodf(noise_offset + 1.0f / 8.0f, 1.0f);
394                 }
395
396                 if (wpd->shading.flag & XRAY_FLAG(wpd)) {
397                         blend_threshold = 1.0f - XRAY_ALPHA(wpd) * 0.9f;
398                 }
399
400                 if (wpd->shading.type == OB_WIRE) {
401                         wpd->shading.xray_alpha = 0.0f;
402                         wpd->shading.xray_alpha_wire = 0.0f;
403                 }
404
405                 int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS;
406                 psl->checker_depth_pass = DRW_pass_create("Checker Depth", state);
407                 grp = DRW_shgroup_create(e_data.checker_depth_sh, psl->checker_depth_pass);
408                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
409                 DRW_shgroup_uniform_float_copy(grp, "threshold", blend_threshold);
410                 DRW_shgroup_uniform_float_copy(grp, "offset", noise_offset);
411         }
412 }
413
414 void workbench_forward_engine_free()
415 {
416         for (int index = 0; index < 2; index++) {
417                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
418         }
419         for (int index = 0; index < MAX_ACCUM_SHADERS; index++) {
420                 DRW_SHADER_FREE_SAFE(e_data.transparent_accum_sh_cache[index]);
421         }
422         DRW_SHADER_FREE_SAFE(e_data.object_outline_sh);
423         DRW_SHADER_FREE_SAFE(e_data.object_outline_texture_sh);
424         DRW_SHADER_FREE_SAFE(e_data.object_outline_hair_sh);
425         DRW_SHADER_FREE_SAFE(e_data.checker_depth_sh);
426
427         workbench_volume_engine_free();
428         workbench_fxaa_engine_free();
429         workbench_taa_engine_free();
430 }
431
432 void workbench_forward_cache_init(WORKBENCH_Data *UNUSED(vedata))
433 {
434 }
435
436 static void workbench_forward_cache_populate_particles(WORKBENCH_Data *vedata, Object *ob)
437 {
438         WORKBENCH_StorageList *stl = vedata->stl;
439         WORKBENCH_PassList *psl = vedata->psl;
440         WORKBENCH_PrivateData *wpd = stl->g_data;
441
442         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
443                 if (md->type != eModifierType_ParticleSystem) {
444                         continue;
445                 }
446                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
447                 if (!psys_check_enabled(ob, psys, false)) {
448                         continue;
449                 }
450                 if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
451                         continue;
452                 }
453                 ParticleSettings *part = psys->part;
454                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
455
456                 if (draw_as == PART_DRAW_PATH) {
457                         Material *mat;
458                         Image *image;
459                         int interp;
460                         workbench_material_get_image_and_mat(ob, part->omat, &image, &interp, &mat);
461                         int color_type = workbench_material_determine_color_type(wpd, image, ob);
462                         WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, mat, image, color_type, interp);
463
464                         struct GPUShader *shader = (color_type != V3D_SHADING_TEXTURE_COLOR)
465                                                    ? wpd->transparent_accum_hair_sh
466                                                    : wpd->transparent_accum_texture_hair_sh;
467                         DRWShadingGroup *shgrp = DRW_shgroup_hair_create(
468                                                         ob, psys, md,
469                                                         psl->transparent_accum_pass,
470                                                         shader);
471                         DRW_shgroup_uniform_block(shgrp, "world_block", wpd->world_ubo);
472                         workbench_material_shgroup_uniform(wpd, shgrp, material, ob, false, false, interp);
473                         DRW_shgroup_uniform_vec4(shgrp, "viewvecs[0]", (float *)wpd->viewvecs, 3);
474                         /* Hairs have lots of layer and can rapidly become the most prominent surface.
475                          * So lower their alpha artificially. */
476                         float hair_alpha = XRAY_ALPHA(wpd) * 0.33f;
477                         DRW_shgroup_uniform_float_copy(shgrp, "alpha", hair_alpha);
478                         if (STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd)) {
479                                 BKE_studiolight_ensure_flag(wpd->studio_light, STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE);
480                                 DRW_shgroup_uniform_texture(shgrp, "matcapImage", wpd->studio_light->equirect_radiance_gputexture );
481                         }
482                         if (SPECULAR_HIGHLIGHT_ENABLED(wpd) || MATCAP_ENABLED(wpd)) {
483                                 DRW_shgroup_uniform_vec2(shgrp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
484                         }
485                         shgrp = DRW_shgroup_hair_create(ob, psys, md,
486                                                 vedata->psl->object_outline_pass,
487                                                 e_data.object_outline_hair_sh);
488                         DRW_shgroup_uniform_int(shgrp, "object_id", &material->object_id, 1);
489                 }
490         }
491 }
492
493 void workbench_forward_cache_populate(WORKBENCH_Data *vedata, Object *ob)
494 {
495         WORKBENCH_StorageList *stl = vedata->stl;
496         WORKBENCH_PrivateData *wpd = stl->g_data;
497         const DRWContextState *draw_ctx = DRW_context_state_get();
498         Scene *scene = draw_ctx->scene;
499         const bool is_wire = (ob->dt == OB_WIRE);
500
501         if (!DRW_object_is_renderable(ob)) {
502                 return;
503         }
504
505         if (ob->type == OB_MESH) {
506                 workbench_forward_cache_populate_particles(vedata, ob);
507         }
508
509         ModifierData *md;
510         if (((ob->base_flag & BASE_FROM_DUPLI) == 0) &&
511             (md = modifiers_findByType(ob, eModifierType_Smoke)) &&
512             (modifier_isEnabled(scene, md, eModifierMode_Realtime)) &&
513             (((SmokeModifierData *)md)->domain != NULL))
514         {
515                 workbench_volume_cache_populate(vedata, scene, ob, md);
516                 return; /* Do not draw solid in this case. */
517         }
518
519         if (!(DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) {
520                 return;
521         }
522         if (ob->dt < OB_WIRE) {
523                 return;
524         }
525
526         WORKBENCH_MaterialData *material;
527         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
528                 const bool is_active = (ob == draw_ctx->obact);
529                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
530                 bool is_drawn = false;
531
532                 if (!is_sculpt_mode && TEXTURE_DRAWING_ENABLED(wpd) && ELEM(ob->type, OB_MESH)) {
533                         const Mesh *me = ob->data;
534                         if (me->mloopuv) {
535                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
536                                 struct GPUBatch **geom_array = DRW_cache_mesh_surface_texpaint_get(ob);
537                                 for (int i = 0; i < materials_len; i++) {
538                                         Material *mat;
539                                         Image *image;
540                                         int interp;
541                                         workbench_material_get_image_and_mat(ob, i + 1, &image, &interp, &mat);
542                                         int color_type = workbench_material_determine_color_type(wpd, image, ob);
543                                         material = get_or_create_material_data(vedata, ob, mat, image, color_type, interp);
544                                         DRW_shgroup_call_object_add(material->shgrp_object_outline, geom_array[i], ob);
545                                         DRW_shgroup_call_object_add(material->shgrp, geom_array[i], ob);
546                                 }
547                                 is_drawn = true;
548                         }
549                 }
550
551                 /* Fallback from not drawn OB_TEXTURE mode or just OB_SOLID mode */
552                 if (!is_drawn) {
553                         if (ELEM(wpd->shading.color_type,
554                                  V3D_SHADING_SINGLE_COLOR, V3D_SHADING_OBJECT_COLOR, V3D_SHADING_RANDOM_COLOR))
555                         {
556                                 /* No material split needed */
557                                 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
558                                 if (geom) {
559                                         material = get_or_create_material_data(vedata, ob, NULL, NULL, wpd->shading.color_type, 0);
560                                         if (is_sculpt_mode) {
561                                                 DRW_shgroup_call_sculpt_add(material->shgrp_object_outline, ob, ob->obmat);
562                                                 if (!is_wire) {
563                                                         DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
564                                                 }
565                                         }
566                                         else {
567                                                 DRW_shgroup_call_object_add(material->shgrp_object_outline, geom, ob);
568                                                 if (!is_wire) {
569                                                         DRW_shgroup_call_object_add(material->shgrp, geom, ob);
570                                                 }
571                                         }
572                                 }
573                         }
574                         else {
575                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
576                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
577                                 for (int i = 0; i < materials_len; i++) {
578                                         gpumat_array[i] = NULL;
579                                 }
580
581                                 struct GPUBatch **mat_geom = DRW_cache_object_surface_material_get(
582                                         ob, gpumat_array, materials_len, NULL, NULL, NULL);
583                                 if (mat_geom) {
584                                         for (int i = 0; i < materials_len; ++i) {
585                                                 if (mat_geom[i] == NULL) {
586                                                         continue;
587                                                 }
588
589                                                 Material *mat = give_current_material(ob, i + 1);
590                                                 material = get_or_create_material_data(vedata, ob, mat, NULL, V3D_SHADING_MATERIAL_COLOR, 0);
591                                                 if (is_sculpt_mode) {
592                                                         DRW_shgroup_call_sculpt_add(material->shgrp_object_outline, ob, ob->obmat);
593                                                         if (!is_wire) {
594                                                                 DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
595                                                         }
596                                                 }
597                                                 else {
598                                                         DRW_shgroup_call_object_add(material->shgrp_object_outline, mat_geom[i], ob);
599                                                         if (!is_wire) {
600                                                                 DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
601                                                         }
602                                                 }
603                                         }
604                                 }
605                         }
606                 }
607         }
608 }
609
610 void workbench_forward_cache_finish(WORKBENCH_Data *UNUSED(vedata))
611 {
612 }
613
614 void workbench_forward_draw_background(WORKBENCH_Data *UNUSED(vedata))
615 {
616         const float clear_depth = 1.0f;
617         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
618         DRW_stats_group_start("Clear depth");
619         GPU_framebuffer_bind(dfbl->default_fb);
620         GPU_framebuffer_clear_depth(dfbl->default_fb, clear_depth);
621         DRW_stats_group_end();
622 }
623
624 void workbench_forward_draw_scene(WORKBENCH_Data *vedata)
625 {
626         WORKBENCH_PassList *psl = vedata->psl;
627         WORKBENCH_StorageList *stl = vedata->stl;
628         WORKBENCH_FramebufferList *fbl = vedata->fbl;
629         WORKBENCH_PrivateData *wpd = stl->g_data;
630         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
631
632         if (TAA_ENABLED(wpd)) {
633                 workbench_taa_draw_scene_start(vedata);
634         }
635
636         /* Write Depth + Object ID */
637         const float clear_outline[4] = {0.0f};
638         GPU_framebuffer_bind(fbl->object_outline_fb);
639         GPU_framebuffer_clear_color(fbl->object_outline_fb, clear_outline);
640         DRW_draw_pass(psl->object_outline_pass);
641
642         if (XRAY_ALPHA(wpd) > 0.0) {
643                 const float clear_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
644                 GPU_framebuffer_bind(fbl->transparent_accum_fb);
645                 GPU_framebuffer_clear_color(fbl->transparent_accum_fb, clear_color);
646                 DRW_draw_pass(psl->transparent_accum_pass);
647         }
648         else {
649                 /* TODO(fclem): this is unnecessary and takes up perf.
650                  * Better change the composite frag shader to not use the tx. */
651                 const float clear_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
652                 GPU_framebuffer_bind(fbl->transparent_accum_fb);
653                 GPU_framebuffer_clear_color(fbl->transparent_accum_fb, clear_color);
654         }
655
656         /* Composite */
657         GPU_framebuffer_bind(fbl->composite_fb);
658         DRW_draw_pass(psl->composite_pass);
659         DRW_draw_pass(psl->volume_pass);
660
661         /* Only when clipping is enabled. */
662         if (psl->background_pass) {
663                 DRW_draw_pass(psl->background_pass);
664         }
665
666         /* Color correct and Anti aliasing */
667         workbench_aa_draw_pass(vedata, e_data.composite_buffer_tx);
668
669         /* Apply checker pattern */
670         GPU_framebuffer_bind(dfbl->depth_only_fb);
671         DRW_draw_pass(psl->checker_depth_pass);
672 }
673
674 void workbench_forward_draw_finish(WORKBENCH_Data *vedata)
675 {
676         WORKBENCH_StorageList *stl = vedata->stl;
677         WORKBENCH_PrivateData *wpd = stl->g_data;
678
679         workbench_private_data_free(wpd);
680         workbench_volume_smoke_textures_free(wpd);
681 }