Workbench: Matcaps T55291
[blender.git] / source / blender / draw / engines / workbench / workbench_deferred.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file workbench_deferred.c
23  *  \ingroup draw_engine
24  */
25
26 #include "workbench_private.h"
27
28 #include "BIF_gl.h"
29
30 #include "BLI_alloca.h"
31 #include "BLI_dynstr.h"
32 #include "BLI_utildefines.h"
33
34 #include "BKE_node.h"
35 #include "BKE_particle.h"
36
37 #include "DNA_image_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_modifier_types.h"
40 #include "DNA_node_types.h"
41
42 #include "ED_uvedit.h"
43
44 #include "GPU_shader.h"
45 #include "GPU_texture.h"
46
47
48 /* *********** STATIC *********** */
49
50 // #define DEBUG_SHADOW_VOLUME
51
52 #ifdef DEBUG_SHADOW_VOLUME
53 #  include "draw_debug.h"
54 #endif
55
56 static struct {
57         struct GPUShader *prepass_sh_cache[MAX_SHADERS];
58         struct GPUShader *composite_sh_cache[MAX_SHADERS];
59         struct GPUShader *shadow_fail_sh;
60         struct GPUShader *shadow_fail_manifold_sh;
61         struct GPUShader *shadow_pass_sh;
62         struct GPUShader *shadow_pass_manifold_sh;
63         struct GPUShader *shadow_caps_sh;
64         struct GPUShader *shadow_caps_manifold_sh;
65
66         struct GPUTexture *object_id_tx; /* ref only, not alloced */
67         struct GPUTexture *color_buffer_tx; /* ref only, not alloced */
68         struct GPUTexture *specular_buffer_tx; /* ref only, not alloced */
69         struct GPUTexture *normal_buffer_tx; /* ref only, not alloced */
70         struct GPUTexture *composite_buffer_tx; /* ref only, not alloced */
71
72         SceneDisplay display; /* world light direction for shadows */
73         float light_direction_vs[3];
74         int next_object_id;
75         float normal_world_matrix[3][3];
76 } e_data = {{NULL}};
77
78 /* Shaders */
79 extern char datatoc_common_hair_lib_glsl[];
80
81 extern char datatoc_workbench_prepass_vert_glsl[];
82 extern char datatoc_workbench_prepass_frag_glsl[];
83 extern char datatoc_workbench_deferred_composite_frag_glsl[];
84
85 extern char datatoc_workbench_shadow_vert_glsl[];
86 extern char datatoc_workbench_shadow_geom_glsl[];
87 extern char datatoc_workbench_shadow_caps_geom_glsl[];
88 extern char datatoc_workbench_shadow_debug_frag_glsl[];
89
90 extern char datatoc_workbench_background_lib_glsl[];
91 extern char datatoc_workbench_common_lib_glsl[];
92 extern char datatoc_workbench_data_lib_glsl[];
93 extern char datatoc_workbench_object_outline_lib_glsl[];
94 extern char datatoc_workbench_world_light_lib_glsl[];
95
96 static char *workbench_build_composite_frag(WORKBENCH_PrivateData *wpd)
97 {
98         char *str = NULL;
99
100         DynStr *ds = BLI_dynstr_new();
101
102         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
103         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
104         BLI_dynstr_append(ds, datatoc_workbench_background_lib_glsl);
105
106         if ((wpd->shading.light & V3D_LIGHTING_STUDIO) || (wpd->shading.flag & V3D_SHADING_SPECULAR_HIGHLIGHT)) {
107                 BLI_dynstr_append(ds, datatoc_workbench_world_light_lib_glsl);
108         }
109         if (wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE) {
110                 BLI_dynstr_append(ds, datatoc_workbench_object_outline_lib_glsl);
111         }
112
113         BLI_dynstr_append(ds, datatoc_workbench_deferred_composite_frag_glsl);
114
115         str = BLI_dynstr_get_cstring(ds);
116         BLI_dynstr_free(ds);
117         return str;
118 }
119
120 static char *workbench_build_prepass_frag(void)
121 {
122         char *str = NULL;
123
124         DynStr *ds = BLI_dynstr_new();
125
126         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
127         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
128         BLI_dynstr_append(ds, datatoc_workbench_prepass_frag_glsl);
129
130         str = BLI_dynstr_get_cstring(ds);
131         BLI_dynstr_free(ds);
132         return str;
133 }
134
135 static char *workbench_build_prepass_vert(void)
136 {
137         char *str = NULL;
138
139         DynStr *ds = BLI_dynstr_new();
140
141         BLI_dynstr_append(ds, datatoc_common_hair_lib_glsl);
142         BLI_dynstr_append(ds, datatoc_workbench_prepass_vert_glsl);
143
144         str = BLI_dynstr_get_cstring(ds);
145         BLI_dynstr_free(ds);
146         return str;
147 }
148
149 static void ensure_deferred_shaders(WORKBENCH_PrivateData *wpd, int index, int drawtype, bool is_hair)
150 {
151         if (e_data.prepass_sh_cache[index] == NULL) {
152                 char *defines = workbench_material_build_defines(wpd, drawtype, is_hair);
153                 char *composite_frag = workbench_build_composite_frag(wpd);
154                 char *prepass_vert = workbench_build_prepass_vert();
155                 char *prepass_frag = workbench_build_prepass_frag();
156                 e_data.prepass_sh_cache[index] = DRW_shader_create(
157                         prepass_vert, NULL,
158                         prepass_frag, defines);
159                 if (drawtype == OB_SOLID && !is_hair) {
160                         e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines);
161                 }
162                 MEM_freeN(prepass_vert);
163                 MEM_freeN(prepass_frag);
164                 MEM_freeN(composite_frag);
165                 MEM_freeN(defines);
166         }
167 }
168
169 static void select_deferred_shaders(WORKBENCH_PrivateData *wpd)
170 {
171         int index_solid = workbench_material_get_shader_index(wpd, OB_SOLID, false);
172         int index_solid_hair = workbench_material_get_shader_index(wpd, OB_SOLID, true);
173         int index_texture = workbench_material_get_shader_index(wpd, OB_TEXTURE, false);
174         int index_texture_hair = workbench_material_get_shader_index(wpd, OB_TEXTURE, true);
175
176         ensure_deferred_shaders(wpd, index_solid, OB_SOLID, false);
177         ensure_deferred_shaders(wpd, index_solid_hair, OB_SOLID, true);
178         ensure_deferred_shaders(wpd, index_texture, OB_TEXTURE, false);
179         ensure_deferred_shaders(wpd, index_texture_hair, OB_TEXTURE, true);
180
181         wpd->prepass_solid_sh = e_data.prepass_sh_cache[index_solid];
182         wpd->prepass_solid_hair_sh = e_data.prepass_sh_cache[index_solid_hair];
183         wpd->prepass_texture_sh = e_data.prepass_sh_cache[index_texture];
184         wpd->prepass_texture_hair_sh = e_data.prepass_sh_cache[index_texture_hair];
185         wpd->composite_sh = e_data.composite_sh_cache[index_solid];
186 }
187
188 /* Functions */
189
190
191 static void workbench_init_object_data(ObjectEngineData *engine_data)
192 {
193         WORKBENCH_ObjectData *data = (WORKBENCH_ObjectData *)engine_data;
194         data->object_id = e_data.next_object_id++;
195         data->shadow_bbox_dirty = true;
196 }
197
198 void workbench_deferred_engine_init(WORKBENCH_Data *vedata)
199 {
200         WORKBENCH_FramebufferList *fbl = vedata->fbl;
201         WORKBENCH_StorageList *stl = vedata->stl;
202         WORKBENCH_PassList *psl = vedata->psl;
203         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
204
205         if (!e_data.next_object_id) {
206                 memset(e_data.prepass_sh_cache,   0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
207                 memset(e_data.composite_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
208                 e_data.next_object_id = 1;
209 #ifdef DEBUG_SHADOW_VOLUME
210                 const char *shadow_frag = datatoc_workbench_shadow_debug_frag_glsl;
211 #else
212                 const char *shadow_frag = NULL;
213 #endif
214                 e_data.shadow_pass_sh = DRW_shader_create(
215                         datatoc_workbench_shadow_vert_glsl,
216                         datatoc_workbench_shadow_geom_glsl,
217                         shadow_frag,
218                         "#define SHADOW_PASS\n"
219                         "#define DOUBLE_MANIFOLD\n");
220                 e_data.shadow_pass_manifold_sh = DRW_shader_create(
221                         datatoc_workbench_shadow_vert_glsl,
222                         datatoc_workbench_shadow_geom_glsl,
223                         shadow_frag,
224                         "#define SHADOW_PASS\n");
225                 e_data.shadow_fail_sh = DRW_shader_create(
226                         datatoc_workbench_shadow_vert_glsl,
227                         datatoc_workbench_shadow_geom_glsl,
228                         shadow_frag,
229                         "#define SHADOW_FAIL\n"
230                         "#define DOUBLE_MANIFOLD\n");
231                 e_data.shadow_fail_manifold_sh = DRW_shader_create(
232                         datatoc_workbench_shadow_vert_glsl,
233                         datatoc_workbench_shadow_geom_glsl,
234                         shadow_frag,
235                         "#define SHADOW_FAIL\n");
236                 e_data.shadow_caps_sh = DRW_shader_create(
237                         datatoc_workbench_shadow_vert_glsl,
238                         datatoc_workbench_shadow_caps_geom_glsl,
239                         shadow_frag,
240                         "#define SHADOW_FAIL\n"
241                         "#define DOUBLE_MANIFOLD\n");
242                 e_data.shadow_caps_manifold_sh = DRW_shader_create(
243                         datatoc_workbench_shadow_vert_glsl,
244                         datatoc_workbench_shadow_caps_geom_glsl,
245                         shadow_frag,
246                         "#define SHADOW_FAIL\n");
247         }
248
249         if (!stl->g_data) {
250                 /* Alloc transient pointers */
251                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
252         }
253
254         workbench_private_data_init(stl->g_data);
255
256         {
257                 const float *viewport_size = DRW_viewport_size_get();
258                 const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
259                 e_data.object_id_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_R32UI, &draw_engine_workbench_solid);
260                 e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid);
261                 e_data.specular_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid);
262                 e_data.composite_buffer_tx = DRW_texture_pool_query_2D(
263                         size[0], size[1], GPU_RGBA16F, &draw_engine_workbench_solid);
264
265                 if (NORMAL_ENCODING_ENABLED()) {
266                         e_data.normal_buffer_tx = DRW_texture_pool_query_2D(
267                                 size[0], size[1], GPU_RG16, &draw_engine_workbench_solid);
268                 }
269                 else {
270                         e_data.normal_buffer_tx = DRW_texture_pool_query_2D(
271                                 size[0], size[1], GPU_RGBA32F, &draw_engine_workbench_solid);
272                 }
273
274                 GPU_framebuffer_ensure_config(&fbl->prepass_fb, {
275                         GPU_ATTACHMENT_TEXTURE(dtxl->depth),
276                         GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
277                         GPU_ATTACHMENT_TEXTURE(e_data.color_buffer_tx),
278                         GPU_ATTACHMENT_TEXTURE(e_data.specular_buffer_tx),
279                         GPU_ATTACHMENT_TEXTURE(e_data.normal_buffer_tx),
280                 });
281                 GPU_framebuffer_ensure_config(&fbl->composite_fb, {
282                         GPU_ATTACHMENT_TEXTURE(dtxl->depth),
283                         GPU_ATTACHMENT_TEXTURE(e_data.composite_buffer_tx),
284                 });
285         }
286
287         /* Prepass */
288         {
289                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
290                 psl->prepass_pass = DRW_pass_create("Prepass", state);
291                 psl->prepass_hair_pass = DRW_pass_create("Prepass", state);
292         }
293 }
294
295 void workbench_deferred_engine_free()
296 {
297         for (int index = 0; index < MAX_SHADERS; index++) {
298                 DRW_SHADER_FREE_SAFE(e_data.prepass_sh_cache[index]);
299                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
300         }
301         DRW_SHADER_FREE_SAFE(e_data.shadow_pass_sh);
302         DRW_SHADER_FREE_SAFE(e_data.shadow_pass_manifold_sh);
303         DRW_SHADER_FREE_SAFE(e_data.shadow_fail_sh);
304         DRW_SHADER_FREE_SAFE(e_data.shadow_fail_manifold_sh);
305         DRW_SHADER_FREE_SAFE(e_data.shadow_caps_sh);
306         DRW_SHADER_FREE_SAFE(e_data.shadow_caps_manifold_sh);
307 }
308
309 static void workbench_composite_uniforms(WORKBENCH_PrivateData *wpd, DRWShadingGroup *grp)
310 {
311         DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &e_data.color_buffer_tx);
312         DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
313         if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) {
314                 DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &e_data.normal_buffer_tx);
315         }
316         if (SPECULAR_HIGHLIGHT_ENABLED(wpd) || MATCAP_ENABLED(wpd)) {
317                 DRW_shgroup_uniform_texture_ref(grp, "specularBuffer", &e_data.specular_buffer_tx);
318
319 #if 0
320                 float invwinmat[4][4];
321                 DRW_viewport_matrix_get(invwinmat, DRW_MAT_WININV);
322
323                 copy_v4_fl4(e_data.screenvecs[0],  1.0f, -1.0f, 0.0f, 1.0f);
324                 copy_v4_fl4(e_data.screenvecs[1], -1.0f,  1.0f, 0.0f, 1.0f);
325                 copy_v4_fl4(e_data.screenvecs[2], -1.0f, -1.0f, 0.0f, 1.0f);
326                 for (int i = 0; i < 3; i++) {
327                         mul_m4_v4(invwinmat, e_data.screenvecs[i]);
328                         e_data.screenvecs[i][0] /= e_data.screenvecs[i][3]; /* perspective divide */
329                         e_data.screenvecs[i][1] /= e_data.screenvecs[i][3]; /* perspective divide */
330                         e_data.screenvecs[i][2] /= e_data.screenvecs[i][3]; /* perspective divide */
331                         e_data.screenvecs[i][3] = 1.0f;
332                 }
333                 sub_v3_v3(e_data.screenvecs[0], e_data.screenvecs[2]);
334                 sub_v3_v3(e_data.screenvecs[1], e_data.screenvecs[2]);
335                 DRW_shgroup_uniform_vec4(grp, "screenvecs[0]", e_data.screenvecs[0], 3);
336 #endif
337         }
338         DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
339         DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
340
341         if (STUDIOLIGHT_ORIENTATION_VIEWNORMAL_ENABLED(wpd)) {
342                 BKE_studiolight_ensure_flag(wpd->studio_light, STUDIOLIGHT_EQUIRECTANGULAR_RADIANCE_GPUTEXTURE);
343                 DRW_shgroup_uniform_texture(grp, "matcapImage", wpd->studio_light->equirectangular_radiance_gputexture);
344         }
345
346         workbench_material_set_normal_world_matrix(grp, wpd, e_data.normal_world_matrix);
347 }
348
349 void workbench_deferred_cache_init(WORKBENCH_Data *vedata)
350 {
351         WORKBENCH_StorageList *stl = vedata->stl;
352         WORKBENCH_PassList *psl = vedata->psl;
353         WORKBENCH_PrivateData *wpd = stl->g_data;
354         DRWShadingGroup *grp;
355         const DRWContextState *draw_ctx = DRW_context_state_get();
356         static float light_multiplier = 1.0f;
357
358
359         Scene *scene = draw_ctx->scene;
360
361         select_deferred_shaders(wpd);
362         /* Deferred Mix Pass */
363         {
364                 workbench_private_data_get_light_direction(wpd, e_data.display.light_direction);
365
366                 e_data.display.shadow_shift = scene->display.shadow_shift;
367                 copy_v3_v3(e_data.light_direction_vs, wpd->world_data.lights[0].light_direction_vs);
368
369                 if (SHADOW_ENABLED(wpd)) {
370                         psl->composite_pass = DRW_pass_create(
371                                 "Composite", DRW_STATE_WRITE_COLOR | DRW_STATE_STENCIL_EQUAL);
372                         grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
373                         workbench_composite_uniforms(wpd, grp);
374                         DRW_shgroup_stencil_mask(grp, 0x00);
375                         DRW_shgroup_uniform_float(grp, "lightMultiplier", &light_multiplier, 1);
376                         DRW_shgroup_uniform_float(grp, "shadowMultiplier", &wpd->shadow_multiplier, 1);
377                         DRW_shgroup_uniform_float(grp, "shadowShift", &scene->display.shadow_shift, 1);
378                         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
379
380                         /* Stencil Shadow passes. */
381 #ifdef DEBUG_SHADOW_VOLUME
382                         DRWState depth_pass_state = DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE;
383                         DRWState depth_fail_state = DRW_STATE_DEPTH_GREATER_EQUAL | DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE;
384 #else
385                         DRWState depth_pass_state = DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_STENCIL_SHADOW_PASS;
386                         DRWState depth_fail_state = DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_STENCIL_SHADOW_FAIL;
387 #endif
388                         psl->shadow_depth_pass_pass = DRW_pass_create("Shadow Pass", depth_pass_state);
389                         psl->shadow_depth_pass_mani_pass = DRW_pass_create("Shadow Pass Mani", depth_pass_state);
390                         psl->shadow_depth_fail_pass = DRW_pass_create("Shadow Fail", depth_fail_state);
391                         psl->shadow_depth_fail_mani_pass = DRW_pass_create("Shadow Fail Mani", depth_fail_state);
392                         psl->shadow_depth_fail_caps_pass = DRW_pass_create("Shadow Fail Caps", depth_fail_state);
393                         psl->shadow_depth_fail_caps_mani_pass = DRW_pass_create("Shadow Fail Caps Mani", depth_fail_state);
394
395 #ifndef DEBUG_SHADOW_VOLUME
396                         grp = DRW_shgroup_create(e_data.shadow_pass_sh, psl->shadow_depth_pass_pass);
397                         DRW_shgroup_stencil_mask(grp, 0xFF);
398                         grp = DRW_shgroup_create(e_data.shadow_pass_manifold_sh, psl->shadow_depth_pass_mani_pass);
399                         DRW_shgroup_stencil_mask(grp, 0xFF);
400                         grp = DRW_shgroup_create(e_data.shadow_fail_sh, psl->shadow_depth_fail_pass);
401                         DRW_shgroup_stencil_mask(grp, 0xFF);
402                         grp = DRW_shgroup_create(e_data.shadow_fail_manifold_sh, psl->shadow_depth_fail_mani_pass);
403                         DRW_shgroup_stencil_mask(grp, 0xFF);
404                         grp = DRW_shgroup_create(e_data.shadow_caps_sh, psl->shadow_depth_fail_caps_pass);
405                         DRW_shgroup_stencil_mask(grp, 0xFF);
406                         grp = DRW_shgroup_create(e_data.shadow_caps_manifold_sh, psl->shadow_depth_fail_caps_mani_pass);
407                         DRW_shgroup_stencil_mask(grp, 0xFF);
408
409                         psl->composite_shadow_pass = DRW_pass_create("Composite Shadow", DRW_STATE_WRITE_COLOR | DRW_STATE_STENCIL_NEQUAL);
410                         grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_shadow_pass);
411                         DRW_shgroup_stencil_mask(grp, 0x00);
412                         workbench_composite_uniforms(wpd, grp);
413                         DRW_shgroup_uniform_float(grp, "lightMultiplier", &wpd->shadow_multiplier, 1);
414                         DRW_shgroup_uniform_float(grp, "shadowMultiplier", &wpd->shadow_multiplier, 1);
415                         DRW_shgroup_uniform_float(grp, "shadowShift", &scene->display.shadow_shift, 1);
416                         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
417 #endif
418
419                         studiolight_update_light(wpd, e_data.display.light_direction);
420                 }
421                 else {
422                         psl->composite_pass = DRW_pass_create(
423                                 "Composite", DRW_STATE_WRITE_COLOR);
424                         grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
425                         workbench_composite_uniforms(wpd, grp);
426                         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
427                 }
428         }
429 }
430
431 static WORKBENCH_MaterialData *get_or_create_material_data(
432         WORKBENCH_Data *vedata, Object *ob, Material *mat, Image *ima, int drawtype)
433 {
434         WORKBENCH_StorageList *stl = vedata->stl;
435         WORKBENCH_PassList *psl = vedata->psl;
436         WORKBENCH_PrivateData *wpd = stl->g_data;
437         WORKBENCH_MaterialData *material;
438         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
439                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
440         WORKBENCH_MaterialData material_template;
441
442         /* Solid */
443         workbench_material_update_data(wpd, ob, mat, &material_template);
444         material_template.object_id = engine_object_data->object_id;
445         material_template.drawtype = drawtype;
446         material_template.ima = ima;
447         uint hash = workbench_material_get_hash(&material_template);
448
449         material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
450         if (material == NULL) {
451                 material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
452                 material->shgrp = DRW_shgroup_create(
453                         drawtype == OB_SOLID ? wpd->prepass_solid_sh : wpd->prepass_texture_sh, psl->prepass_pass);
454                 DRW_shgroup_stencil_mask(material->shgrp, 0xFF);
455                 material->object_id = engine_object_data->object_id;
456                 copy_v4_v4(material->material_data.diffuse_color, material_template.material_data.diffuse_color);
457                 copy_v4_v4(material->material_data.specular_color, material_template.material_data.specular_color);
458                 material->material_data.roughness = material_template.material_data.roughness;
459                 switch (drawtype) {
460                         case OB_SOLID:
461                                 break;
462
463                         case OB_TEXTURE:
464                         {
465                                 GPUTexture *tex = GPU_texture_from_blender(ima, NULL, GL_TEXTURE_2D, false, false, false);
466                                 DRW_shgroup_uniform_texture(material->shgrp, "image", tex);
467                                 break;
468                         }
469                 }
470                 DRW_shgroup_uniform_int(material->shgrp, "object_id", &material->object_id, 1);
471                 material->material_ubo = DRW_uniformbuffer_create(sizeof(WORKBENCH_UBO_Material), &material->material_data);
472                 DRW_shgroup_uniform_block(material->shgrp, "material_block", material->material_ubo);
473
474                 BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
475         }
476         return material;
477 }
478
479 static void workbench_cache_populate_particles(WORKBENCH_Data *vedata, Object *ob)
480 {
481         WORKBENCH_StorageList *stl = vedata->stl;
482         WORKBENCH_PassList *psl = vedata->psl;
483         WORKBENCH_PrivateData *wpd = stl->g_data;
484         const DRWContextState *draw_ctx = DRW_context_state_get();
485         if (ob == draw_ctx->object_edit) {
486                 return;
487         }
488         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
489                 if (md->type != eModifierType_ParticleSystem) {
490                         continue;
491                 }
492                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
493                 if (!psys_check_enabled(ob, psys, false)) {
494                         continue;
495                 }
496                 if (!DRW_check_psys_visible_within_active_context(ob, psys)) {
497                         return;
498                 }
499                 ParticleSettings *part = psys->part;
500                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
501
502                 if (draw_as == PART_DRAW_PATH) {
503                         Image *image = NULL;
504                         Material *mat = give_current_material(ob, part->omat);
505                         int mat_drawtype = OB_SOLID;
506
507                         if (wpd->drawtype == OB_TEXTURE) {
508                                 ED_object_get_active_image(ob, part->omat, &image, NULL, NULL, NULL);
509                                 /* use OB_SOLID when no texture could be determined */
510                                 if (image) {
511                                         mat_drawtype = OB_TEXTURE;
512                                 }
513                         }
514
515                         WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
516
517                         struct GPUShader *shader = (mat_drawtype == OB_SOLID)
518                                                    ? wpd->prepass_solid_hair_sh
519                                                    : wpd->prepass_texture_hair_sh;
520                         DRWShadingGroup *shgrp = DRW_shgroup_hair_create(
521                                                         ob, psys, md,
522                                                         psl->prepass_hair_pass,
523                                                         shader);
524                         DRW_shgroup_stencil_mask(shgrp, 0xFF);
525                         DRW_shgroup_uniform_int(shgrp, "object_id", &material->object_id, 1);
526                         DRW_shgroup_uniform_block(shgrp, "material_block", material->material_ubo);
527                         if (image) {
528                                 GPUTexture *tex = GPU_texture_from_blender(image, NULL, GL_TEXTURE_2D, false, false, false);
529                                 DRW_shgroup_uniform_texture(shgrp, "image", tex);
530                         }
531                 }
532         }
533 }
534
535 void workbench_deferred_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob)
536 {
537         WORKBENCH_StorageList *stl = vedata->stl;
538         WORKBENCH_PassList *psl = vedata->psl;
539         WORKBENCH_PrivateData *wpd = stl->g_data;
540
541         if (!DRW_object_is_renderable(ob))
542                 return;
543
544         if (ob->type == OB_MESH) {
545                 workbench_cache_populate_particles(vedata, ob);
546         }
547
548         WORKBENCH_MaterialData *material;
549         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
550                 const DRWContextState *draw_ctx = DRW_context_state_get();
551                 const bool is_active = (ob == draw_ctx->obact);
552                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
553                 bool is_drawn = false;
554                 if (!is_sculpt_mode && wpd->drawtype == OB_TEXTURE && ob->type == OB_MESH) {
555                         const Mesh *me = ob->data;
556                         if (me->mloopuv) {
557                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
558                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
559                                 struct Gwn_Batch **geom_array = me->totcol ? DRW_cache_mesh_surface_texpaint_get(ob) : NULL;
560                                 if (materials_len > 0 && geom_array) {
561                                         for (int i = 0; i < materials_len; i++) {
562                                                 Material *mat = give_current_material(ob, i + 1);
563                                                 Image *image;
564                                                 ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL);
565                                                 /* use OB_SOLID when no texture could be determined */
566                                                 int mat_drawtype = OB_SOLID;
567                                                 if (image) {
568                                                         mat_drawtype = OB_TEXTURE;
569                                                 }
570                                                 material = get_or_create_material_data(vedata, ob, mat, image, mat_drawtype);
571                                                 DRW_shgroup_call_object_add(material->shgrp, geom_array[i], ob);
572                                         }
573                                         is_drawn = true;
574                                 }
575                         }
576                 }
577
578                 /* Fallback from not drawn OB_TEXTURE mode or just OB_SOLID mode */
579                 if (!is_drawn) {
580                         if ((wpd->shading.color_type != V3D_SHADING_MATERIAL_COLOR) || is_sculpt_mode) {
581                                 /* No material split needed */
582                                 struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
583                                 if (geom) {
584                                         material = get_or_create_material_data(vedata, ob, NULL, NULL, OB_SOLID);
585                                         if (is_sculpt_mode) {
586                                                 DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
587                                         }
588                                         else {
589                                                 DRW_shgroup_call_object_add(material->shgrp, geom, ob);
590                                         }
591                                 }
592                         }
593                         else { /* MATERIAL colors */
594                                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
595                                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
596                                 for (int i = 0; i < materials_len; i++) {
597                                         gpumat_array[i] = NULL;
598                                 }
599
600                                 struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(
601                                         ob, gpumat_array, materials_len, NULL, NULL, NULL);
602                                 if (mat_geom) {
603                                         for (int i = 0; i < materials_len; ++i) {
604                                                 Material *mat = give_current_material(ob, i + 1);
605                                                 material = get_or_create_material_data(vedata, ob, mat, NULL, OB_SOLID);
606                                                 DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
607                                         }
608                                 }
609                         }
610                 }
611
612                 if (SHADOW_ENABLED(wpd) && (ob->display.flag & OB_SHOW_SHADOW) > 0) {
613                         bool is_manifold;
614                         struct Gwn_Batch *geom_shadow = DRW_cache_object_edge_detection_get(ob, &is_manifold);
615                         if (geom_shadow) {
616                                 if (is_sculpt_mode) {
617                                         /* Currently unsupported in sculpt mode. We could revert to the slow
618                                          * method in this case but i'm not sure if it's a good idea given that
619                                          * sculped meshes are heavy to begin with. */
620                                         // DRW_shgroup_call_sculpt_add(wpd->shadow_shgrp, ob, ob->obmat);
621                                 }
622                                 else {
623                                         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
624                                                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
625
626                                         if (studiolight_object_cast_visible_shadow(wpd, ob, engine_object_data)) {
627
628                                                 invert_m4_m4(ob->imat, ob->obmat);
629                                                 mul_v3_mat3_m4v3(engine_object_data->shadow_dir, ob->imat, e_data.display.light_direction);
630
631                                                 DRWShadingGroup *grp;
632                                                 bool use_shadow_pass_technique = !studiolight_camera_in_object_shadow(wpd, ob, engine_object_data);
633
634                                                 if (use_shadow_pass_technique) {
635                                                         if (is_manifold) {
636                                                                 grp = DRW_shgroup_create(e_data.shadow_pass_manifold_sh, psl->shadow_depth_pass_mani_pass);
637                                                         }
638                                                         else {
639                                                                 grp = DRW_shgroup_create(e_data.shadow_pass_sh, psl->shadow_depth_pass_pass);
640                                                         }
641                                                         DRW_shgroup_uniform_vec3(grp, "lightDirection", engine_object_data->shadow_dir, 1);
642                                                         DRW_shgroup_uniform_float_copy(grp, "lightDistance", 1e5f);
643                                                         DRW_shgroup_call_add(grp, geom_shadow, ob->obmat);
644 #ifdef DEBUG_SHADOW_VOLUME
645                                                         DRW_debug_bbox(&engine_object_data->shadow_bbox, (float[4]){1.0f, 0.0f, 0.0f, 1.0f});
646 #endif
647                                                 }
648                                                 else {
649                                                         float extrude_distance = studiolight_object_shadow_distance(wpd, ob, engine_object_data);
650
651                                                         /* TODO(fclem): only use caps if they are in the view frustum. */
652                                                         const bool need_caps = true;
653                                                         if (need_caps) {
654                                                                 if (is_manifold) {
655                                                                         grp = DRW_shgroup_create(e_data.shadow_caps_manifold_sh, psl->shadow_depth_fail_caps_mani_pass);
656                                                                 }
657                                                                 else {
658                                                                         grp = DRW_shgroup_create(e_data.shadow_caps_sh, psl->shadow_depth_fail_caps_pass);
659                                                                 }
660                                                                 DRW_shgroup_uniform_vec3(grp, "lightDirection", engine_object_data->shadow_dir, 1);
661                                                                 DRW_shgroup_uniform_float_copy(grp, "lightDistance", extrude_distance);
662                                                                 DRW_shgroup_call_add(grp, DRW_cache_object_surface_get(ob), ob->obmat);
663                                                         }
664
665                                                         if (is_manifold) {
666                                                                 grp = DRW_shgroup_create(e_data.shadow_fail_manifold_sh, psl->shadow_depth_fail_mani_pass);
667                                                         }
668                                                         else {
669                                                                 grp = DRW_shgroup_create(e_data.shadow_fail_sh, psl->shadow_depth_fail_pass);
670                                                         }
671                                                         DRW_shgroup_uniform_vec3(grp, "lightDirection", engine_object_data->shadow_dir, 1);
672                                                         DRW_shgroup_uniform_float_copy(grp, "lightDistance", extrude_distance);
673                                                         DRW_shgroup_call_add(grp, geom_shadow, ob->obmat);
674 #ifdef DEBUG_SHADOW_VOLUME
675                                                         DRW_debug_bbox(&engine_object_data->shadow_bbox, (float[4]){0.0f, 1.0f, 0.0f, 1.0f});
676 #endif
677                                                 }
678                                         }
679                                 }
680                         }
681                 }
682         }
683 }
684
685 void workbench_deferred_cache_finish(WORKBENCH_Data *UNUSED(vedata))
686 {
687 }
688
689 void workbench_deferred_draw_background(WORKBENCH_Data *vedata)
690 {
691         WORKBENCH_StorageList *stl = vedata->stl;
692         WORKBENCH_FramebufferList *fbl = vedata->fbl;
693         WORKBENCH_PrivateData *wpd = stl->g_data;
694         const float clear_depth = 1.0f;
695         const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
696         uint clear_stencil = 0xFF;
697
698         DRW_stats_group_start("Clear Background");
699         GPU_framebuffer_bind(fbl->prepass_fb);
700         int clear_bits = GPU_DEPTH_BIT | GPU_COLOR_BIT;
701         SET_FLAG_FROM_TEST(clear_bits, SHADOW_ENABLED(wpd), GPU_STENCIL_BIT);
702         GPU_framebuffer_clear(fbl->prepass_fb, clear_bits, clear_color, clear_depth, clear_stencil);
703         DRW_stats_group_end();
704 }
705
706 void workbench_deferred_draw_scene(WORKBENCH_Data *vedata)
707 {
708         WORKBENCH_PassList *psl = vedata->psl;
709         WORKBENCH_StorageList *stl = vedata->stl;
710         WORKBENCH_FramebufferList *fbl = vedata->fbl;
711         WORKBENCH_PrivateData *wpd = stl->g_data;
712         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
713
714         /* clear in background */
715         GPU_framebuffer_bind(fbl->prepass_fb);
716         DRW_draw_pass(psl->prepass_pass);
717         DRW_draw_pass(psl->prepass_hair_pass);
718         if (SHADOW_ENABLED(wpd)) {
719 #ifdef DEBUG_SHADOW_VOLUME
720                 GPU_framebuffer_bind(fbl->composite_fb);
721                 DRW_draw_pass(psl->composite_pass);
722 #else
723                 GPU_framebuffer_bind(dfbl->depth_only_fb);
724 #endif
725                 DRW_draw_pass(psl->shadow_depth_pass_pass);
726                 DRW_draw_pass(psl->shadow_depth_pass_mani_pass);
727                 DRW_draw_pass(psl->shadow_depth_fail_pass);
728                 DRW_draw_pass(psl->shadow_depth_fail_mani_pass);
729                 DRW_draw_pass(psl->shadow_depth_fail_caps_pass);
730                 DRW_draw_pass(psl->shadow_depth_fail_caps_mani_pass);
731 #ifndef DEBUG_SHADOW_VOLUME
732                 GPU_framebuffer_bind(fbl->composite_fb);
733                 DRW_draw_pass(psl->composite_pass);
734                 DRW_draw_pass(psl->composite_shadow_pass);
735 #endif
736         }
737         else {
738                 GPU_framebuffer_bind(fbl->composite_fb);
739                 DRW_draw_pass(psl->composite_pass);
740         }
741
742         GPU_framebuffer_bind(dfbl->color_only_fb);
743         DRW_transform_to_display(e_data.composite_buffer_tx);
744
745         workbench_private_data_free(wpd);
746 }