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