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