a34421243d103b2a29239fa1d9a22e053b958c9f
[blender.git] / source / blender / draw / engines / eevee / eevee_materials.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 eevee_materials.c
23  *  \ingroup draw_engine
24  */
25
26 #include "DRW_render.h"
27
28 #include "BLI_dynstr.h"
29 #include "BLI_ghash.h"
30 #include "BLI_alloca.h"
31 #include "BLI_rand.h"
32 #include "BLI_string_utils.h"
33
34 #include "BKE_particle.h"
35 #include "BKE_paint.h"
36 #include "BKE_pbvh.h"
37 #include "BKE_studiolight.h"
38
39 #include "DNA_world_types.h"
40 #include "DNA_modifier_types.h"
41 #include "DNA_view3d_types.h"
42
43 #include "GPU_material.h"
44
45 #include "eevee_engine.h"
46 #include "eevee_lut.h"
47 #include "eevee_private.h"
48
49 /* *********** STATIC *********** */
50 static struct {
51         char *frag_shader_lib;
52         char *vert_shader_str;
53         char *volume_shader_lib;
54
55         struct GPUShader *default_prepass_sh;
56         struct GPUShader *default_prepass_clip_sh;
57         struct GPUShader *default_hair_prepass_sh;
58         struct GPUShader *default_hair_prepass_clip_sh;
59         struct GPUShader *default_lit[VAR_MAT_MAX];
60         struct GPUShader *default_background;
61         struct GPUShader *default_studiolight_background;
62         struct GPUShader *update_noise_sh;
63
64         /* 64*64 array texture containing all LUTs and other utilitarian arrays.
65          * Packing enables us to same precious textures slots. */
66         struct GPUTexture *util_tex;
67         struct GPUTexture *noise_tex;
68
69         uint sss_count;
70
71         float alpha_hash_offset;
72         float noise_offsets[3];
73 } e_data = {NULL}; /* Engine data */
74
75 extern char datatoc_lamps_lib_glsl[];
76 extern char datatoc_lightprobe_lib_glsl[];
77 extern char datatoc_ambient_occlusion_lib_glsl[];
78 extern char datatoc_prepass_frag_glsl[];
79 extern char datatoc_prepass_vert_glsl[];
80 extern char datatoc_default_frag_glsl[];
81 extern char datatoc_default_world_frag_glsl[];
82 extern char datatoc_ltc_lib_glsl[];
83 extern char datatoc_bsdf_lut_frag_glsl[];
84 extern char datatoc_btdf_lut_frag_glsl[];
85 extern char datatoc_bsdf_common_lib_glsl[];
86 extern char datatoc_bsdf_direct_lib_glsl[];
87 extern char datatoc_bsdf_sampling_lib_glsl[];
88 extern char datatoc_common_uniforms_lib_glsl[];
89 extern char datatoc_common_hair_lib_glsl[];
90 extern char datatoc_common_view_lib_glsl[];
91 extern char datatoc_irradiance_lib_glsl[];
92 extern char datatoc_octahedron_lib_glsl[];
93 extern char datatoc_lit_surface_frag_glsl[];
94 extern char datatoc_lit_surface_vert_glsl[];
95 extern char datatoc_raytrace_lib_glsl[];
96 extern char datatoc_ssr_lib_glsl[];
97 extern char datatoc_shadow_vert_glsl[];
98 extern char datatoc_lightprobe_geom_glsl[];
99 extern char datatoc_lightprobe_vert_glsl[];
100 extern char datatoc_background_vert_glsl[];
101 extern char datatoc_update_noise_frag_glsl[];
102 extern char datatoc_volumetric_vert_glsl[];
103 extern char datatoc_volumetric_geom_glsl[];
104 extern char datatoc_volumetric_frag_glsl[];
105 extern char datatoc_volumetric_lib_glsl[];
106
107 extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
108
109 extern Material defmaterial;
110 extern GlobalsUboStorage ts;
111
112 /* *********** FUNCTIONS *********** */
113
114 #if 0 /* Used only to generate the LUT values */
115 static struct GPUTexture *create_ggx_lut_texture(int UNUSED(w), int UNUSED(h))
116 {
117         struct GPUTexture *tex;
118         struct GPUFrameBuffer *fb = NULL;
119         static float samples_ct = 8192.0f;
120         static float inv_samples_ct = 1.0f / 8192.0f;
121
122         char *lib_str = BLI_string_joinN(
123                 datatoc_bsdf_common_lib_glsl,
124                 datatoc_bsdf_sampling_lib_glsl);
125
126         struct GPUShader *sh = DRW_shader_create_with_lib(
127                 datatoc_lightprobe_vert_glsl, datatoc_lightprobe_geom_glsl, datatoc_bsdf_lut_frag_glsl, lib_str,
128                 "#define HAMMERSLEY_SIZE 8192\n"
129                 "#define BRDF_LUT_SIZE 64\n"
130                 "#define NOISE_SIZE 64\n");
131
132         DRWPass *pass = DRW_pass_create("LightProbe Filtering", DRW_STATE_WRITE_COLOR);
133         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
134         DRW_shgroup_uniform_float(grp, "sampleCount", &samples_ct, 1);
135         DRW_shgroup_uniform_float(grp, "invSampleCount", &inv_samples_ct, 1);
136         DRW_shgroup_uniform_texture(grp, "texHammersley", e_data.hammersley);
137         DRW_shgroup_uniform_texture(grp, "texJitter", e_data.jitter);
138
139         struct Gwn_Batch *geom = DRW_cache_fullscreen_quad_get();
140         DRW_shgroup_call_add(grp, geom, NULL);
141
142         float *texels = MEM_mallocN(sizeof(float[2]) * w * h, "lut");
143
144         tex = DRW_texture_create_2D(w, h, GPU_RG16F, DRW_TEX_FILTER, (float *)texels);
145
146         DRWFboTexture tex_filter = {&tex, GPU_RG16F, DRW_TEX_FILTER};
147         GPU_framebuffer_init(&fb, &draw_engine_eevee_type, w, h, &tex_filter, 1);
148
149         GPU_framebuffer_bind(fb);
150         DRW_draw_pass(pass);
151
152         float *data = MEM_mallocN(sizeof(float[3]) * w * h, "lut");
153         glReadBuffer(GL_COLOR_ATTACHMENT0);
154         glReadPixels(0, 0, w, h, GL_RGB, GL_FLOAT, data);
155
156         printf("{");
157         for (int i = 0; i < w*h * 3; i+=3) {
158                 printf("%ff, %ff, ", data[i],  data[i+1]); i+=3;
159                 printf("%ff, %ff, ", data[i],  data[i+1]); i+=3;
160                 printf("%ff, %ff, ", data[i],  data[i+1]); i+=3;
161                 printf("%ff, %ff, \n", data[i],  data[i+1]);
162         }
163         printf("}");
164
165         MEM_freeN(texels);
166         MEM_freeN(data);
167
168         return tex;
169 }
170
171 static struct GPUTexture *create_ggx_refraction_lut_texture(int w, int h)
172 {
173         struct GPUTexture *tex;
174         struct GPUTexture *hammersley = create_hammersley_sample_texture(8192);
175         struct GPUFrameBuffer *fb = NULL;
176         static float samples_ct = 8192.0f;
177         static float a2 = 0.0f;
178         static float inv_samples_ct = 1.0f / 8192.0f;
179
180         char *frag_str = BLI_string_joinN(
181                 datatoc_bsdf_common_lib_glsl,
182                 datatoc_bsdf_sampling_lib_glsl,
183                 datatoc_btdf_lut_frag_glsl);
184
185         struct GPUShader *sh = DRW_shader_create_fullscreen(frag_str,
186                 "#define HAMMERSLEY_SIZE 8192\n"
187                 "#define BRDF_LUT_SIZE 64\n"
188                 "#define NOISE_SIZE 64\n"
189                 "#define LUT_SIZE 64\n");
190
191         MEM_freeN(frag_str);
192
193         DRWPass *pass = DRW_pass_create("LightProbe Filtering", DRW_STATE_WRITE_COLOR);
194         DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
195         DRW_shgroup_uniform_float(grp, "a2", &a2, 1);
196         DRW_shgroup_uniform_float(grp, "sampleCount", &samples_ct, 1);
197         DRW_shgroup_uniform_float(grp, "invSampleCount", &inv_samples_ct, 1);
198         DRW_shgroup_uniform_texture(grp, "texHammersley", hammersley);
199         DRW_shgroup_uniform_texture(grp, "utilTex", e_data.util_tex);
200
201         struct Gwn_Batch *geom = DRW_cache_fullscreen_quad_get();
202         DRW_shgroup_call_add(grp, geom, NULL);
203
204         float *texels = MEM_mallocN(sizeof(float[2]) * w * h, "lut");
205
206         tex = DRW_texture_create_2D(w, h, GPU_R16F, DRW_TEX_FILTER, (float *)texels);
207
208         DRWFboTexture tex_filter = {&tex, GPU_R16F, DRW_TEX_FILTER};
209         GPU_framebuffer_init(&fb, &draw_engine_eevee_type, w, h, &tex_filter, 1);
210
211         GPU_framebuffer_bind(fb);
212
213         float *data = MEM_mallocN(sizeof(float[3]) * w * h, "lut");
214
215         float inc = 1.0f / 31.0f;
216         float roughness = 1e-8f - inc;
217         FILE *f = BLI_fopen("btdf_split_sum_ggx.h", "w");
218         fprintf(f, "static float btdf_split_sum_ggx[32][64 * 64] = {\n");
219         do {
220                 roughness += inc;
221                 CLAMP(roughness, 1e-4f, 1.0f);
222                 a2 = powf(roughness, 4.0f);
223                 DRW_draw_pass(pass);
224
225                 GPU_framebuffer_read_data(0, 0, w, h, 3, 0, data);
226
227 #if 1
228                 fprintf(f, "\t{\n\t\t");
229                 for (int i = 0; i < w*h * 3; i+=3) {
230                         fprintf(f, "%ff,", data[i]);
231                         if (((i/3)+1) % 12 == 0) fprintf(f, "\n\t\t");
232                         else fprintf(f, " ");
233                 }
234                 fprintf(f, "\n\t},\n");
235 #else
236                 for (int i = 0; i < w*h * 3; i+=3) {
237                         if (data[i] < 0.01) printf(" ");
238                         else if (data[i] < 0.3) printf(".");
239                         else if (data[i] < 0.6) printf("+");
240                         else if (data[i] < 0.9) printf("%%");
241                         else printf("#");
242                         if ((i/3+1) % 64 == 0) printf("\n");
243                 }
244 #endif
245
246         } while (roughness < 1.0f);
247         fprintf(f, "\n};\n");
248
249         fclose(f);
250
251         MEM_freeN(texels);
252         MEM_freeN(data);
253
254         return tex;
255 }
256 #endif
257 /* XXX TODO define all shared resources in a shared place without duplication */
258 struct GPUTexture *EEVEE_materials_get_util_tex(void)
259 {
260         return e_data.util_tex;
261 }
262
263 static int eevee_material_shadow_option(int shadow_method)
264 {
265         switch (shadow_method) {
266                 case SHADOW_ESM: return VAR_MAT_ESM;
267                 case SHADOW_VSM: return VAR_MAT_VSM;
268                 default:
269                         BLI_assert(!"Incorrect Shadow Method");
270                         break;
271         }
272
273         return 0;
274 }
275
276 static char *eevee_get_defines(int options)
277 {
278         char *str = NULL;
279
280         DynStr *ds = BLI_dynstr_new();
281         BLI_dynstr_appendf(ds, SHADER_DEFINES);
282
283         if ((options & VAR_MAT_MESH) != 0) {
284                 BLI_dynstr_appendf(ds, "#define MESH_SHADER\n");
285         }
286         if ((options & VAR_MAT_HAIR) != 0) {
287                 BLI_dynstr_appendf(ds, "#define HAIR_SHADER\n");
288         }
289         if ((options & VAR_MAT_PROBE) != 0) {
290                 BLI_dynstr_appendf(ds, "#define PROBE_CAPTURE\n");
291         }
292         if ((options & VAR_MAT_FLAT) != 0) {
293                 BLI_dynstr_appendf(ds, "#define USE_FLAT_NORMAL\n");
294         }
295         if ((options & VAR_MAT_CLIP) != 0) {
296                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_CLIP\n");
297         }
298         if ((options & VAR_MAT_SHADOW) != 0) {
299                 BLI_dynstr_appendf(ds, "#define SHADOW_SHADER\n");
300         }
301         if ((options & VAR_MAT_HASH) != 0) {
302                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_HASH\n");
303         }
304         if ((options & VAR_MAT_BLEND) != 0) {
305                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_BLEND\n");
306         }
307         if ((options & VAR_MAT_MULT) != 0) {
308                 BLI_dynstr_appendf(ds, "#define USE_MULTIPLY\n");
309         }
310         if ((options & VAR_MAT_REFRACT) != 0) {
311                 BLI_dynstr_appendf(ds, "#define USE_REFRACTION\n");
312         }
313         if ((options & VAR_MAT_SSS) != 0) {
314                 BLI_dynstr_appendf(ds, "#define USE_SSS\n");
315         }
316         if ((options & VAR_MAT_SSSALBED) != 0) {
317                 BLI_dynstr_appendf(ds, "#define USE_SSS_ALBEDO\n");
318         }
319         if ((options & VAR_MAT_TRANSLUC) != 0) {
320                 BLI_dynstr_appendf(ds, "#define USE_TRANSLUCENCY\n");
321         }
322         if ((options & VAR_MAT_VSM) != 0) {
323                 BLI_dynstr_appendf(ds, "#define SHADOW_VSM\n");
324         }
325         if ((options & VAR_MAT_ESM) != 0) {
326                 BLI_dynstr_appendf(ds, "#define SHADOW_ESM\n");
327         }
328         if (((options & VAR_MAT_VOLUME) != 0) && ((options & VAR_MAT_BLEND) != 0)) {
329                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_BLEND_VOLUMETRICS\n");
330         }
331         if ((options & VAR_MAT_LOOKDEV) != 0) {
332                 BLI_dynstr_appendf(ds, "#define LOOKDEV\n");
333         }
334
335         str = BLI_dynstr_get_cstring(ds);
336         BLI_dynstr_free(ds);
337
338         return str;
339 }
340
341 static char *eevee_get_volume_defines(int options)
342 {
343         char *str = NULL;
344
345         DynStr *ds = BLI_dynstr_new();
346         BLI_dynstr_appendf(ds, SHADER_DEFINES);
347         BLI_dynstr_appendf(ds, "#define VOLUMETRICS\n");
348
349         if ((options & VAR_MAT_VOLUME) != 0) {
350                 BLI_dynstr_appendf(ds, "#define MESH_SHADER\n");
351         }
352
353         str = BLI_dynstr_get_cstring(ds);
354         BLI_dynstr_free(ds);
355
356         return str;
357 }
358
359 /**
360  * ssr_id can be null to disable ssr contribution.
361  **/
362 static void add_standard_uniforms(
363         DRWShadingGroup *shgrp, EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata,
364         int *ssr_id, float *refract_depth, bool use_ssrefraction, bool use_alpha_blend)
365 {
366         if (ssr_id == NULL) {
367                 static int no_ssr = -1.0f;
368                 ssr_id = &no_ssr;
369         }
370
371         DRW_shgroup_uniform_block(shgrp, "probe_block", sldata->probe_ubo);
372         DRW_shgroup_uniform_block(shgrp, "grid_block", sldata->grid_ubo);
373         DRW_shgroup_uniform_block(shgrp, "planar_block", sldata->planar_ubo);
374         DRW_shgroup_uniform_block(shgrp, "light_block", sldata->light_ubo);
375         DRW_shgroup_uniform_block(shgrp, "shadow_block", sldata->shadow_ubo);
376         DRW_shgroup_uniform_block(shgrp, "common_block", sldata->common_ubo);
377         DRW_shgroup_uniform_block(shgrp, "clip_block", sldata->clip_ubo);
378
379         /* TODO if glossy or diffuse bsdf */
380         if (true) {
381                 DRW_shgroup_uniform_texture(shgrp, "utilTex", e_data.util_tex);
382                 DRW_shgroup_uniform_texture_ref(shgrp, "shadowCubeTexture", &sldata->shadow_cube_pool);
383                 DRW_shgroup_uniform_texture_ref(shgrp, "shadowCascadeTexture", &sldata->shadow_cascade_pool);
384                 DRW_shgroup_uniform_texture_ref(shgrp, "maxzBuffer", &vedata->txl->maxzbuffer);
385
386                 if ((vedata->stl->effects->enabled_effects & EFFECT_GTAO) != 0) {
387                         DRW_shgroup_uniform_texture_ref(shgrp, "horizonBuffer", &vedata->stl->effects->gtao_horizons);
388                 }
389                 else {
390                         /* Use maxzbuffer as fallback to avoid sampling problem on certain platform, see: T52593 */
391                         DRW_shgroup_uniform_texture_ref(shgrp, "horizonBuffer", &vedata->txl->maxzbuffer);
392                 }
393         }
394
395         /* TODO if diffuse bsdf */
396         if (true) {
397                 DRW_shgroup_uniform_texture_ref(shgrp, "irradianceGrid", &sldata->irradiance_pool);
398         }
399
400         /* TODO if glossy bsdf */
401         if (true) {
402                 DRW_shgroup_uniform_texture_ref(shgrp, "probeCubes", &sldata->probe_pool);
403                 DRW_shgroup_uniform_texture_ref(shgrp, "probePlanars", &vedata->txl->planar_pool);
404                 DRW_shgroup_uniform_int(shgrp, "outputSsrId", ssr_id, 1);
405         }
406
407         if (use_ssrefraction) {
408                 BLI_assert(refract_depth != NULL);
409                 DRW_shgroup_uniform_float(shgrp, "refractionDepth", refract_depth, 1);
410                 DRW_shgroup_uniform_texture_ref(shgrp, "colorBuffer", &vedata->txl->refract_color);
411         }
412
413         if ((vedata->stl->effects->enabled_effects & EFFECT_VOLUMETRIC) != 0 &&
414              use_alpha_blend)
415         {
416                 /* Do not use history buffers as they already have been swapped */
417                 DRW_shgroup_uniform_texture_ref(shgrp, "inScattering", &vedata->txl->volume_scatter);
418                 DRW_shgroup_uniform_texture_ref(shgrp, "inTransmittance", &vedata->txl->volume_transmittance);
419         }
420 }
421
422 static void create_default_shader(int options)
423 {
424         char *frag_str = BLI_string_joinN(
425                 e_data.frag_shader_lib,
426                 datatoc_default_frag_glsl);
427
428         char *defines = eevee_get_defines(options);
429
430         e_data.default_lit[options] = DRW_shader_create(e_data.vert_shader_str, NULL, frag_str, defines);
431
432         MEM_freeN(defines);
433         MEM_freeN(frag_str);
434 }
435
436 static void eevee_init_noise_texture(void)
437 {
438         e_data.noise_tex = DRW_texture_create_2D(64, 64, GPU_RGBA16F, 0, (float *)blue_noise);
439 }
440
441 static void eevee_init_util_texture(void)
442 {
443         const int layers = 3 + 16;
444         float (*texels)[4] = MEM_mallocN(sizeof(float[4]) * 64 * 64 * layers, "utils texels");
445         float (*texels_layer)[4] = texels;
446
447         /* Copy ltc_mat_ggx into 1st layer */
448         memcpy(texels_layer, ltc_mat_ggx, sizeof(float[4]) * 64 * 64);
449         texels_layer += 64 * 64;
450
451         /* Copy bsdf_split_sum_ggx into 2nd layer red and green channels.
452            Copy ltc_mag_ggx into 2nd layer blue channel. */
453         for (int i = 0; i < 64 * 64; i++) {
454                 texels_layer[i][0] = bsdf_split_sum_ggx[i * 2 + 0];
455                 texels_layer[i][1] = bsdf_split_sum_ggx[i * 2 + 1];
456                 texels_layer[i][2] = ltc_mag_ggx[i];
457                 texels_layer[i][3] = ltc_disk_integral[i];
458         }
459         texels_layer += 64 * 64;
460
461         /* Copy blue noise in 3rd layer  */
462         for (int i = 0; i < 64 * 64; i++) {
463                 texels_layer[i][0] = blue_noise[i][0];
464                 texels_layer[i][1] = blue_noise[i][2];
465                 texels_layer[i][2] = cosf(blue_noise[i][1] * 2.0f * M_PI);
466                 texels_layer[i][3] = sinf(blue_noise[i][1] * 2.0f * M_PI);
467         }
468         texels_layer += 64 * 64;
469
470         /* Copy Refraction GGX LUT in layer 4 - 20 */
471         for (int j = 0; j < 16; ++j) {
472                 for (int i = 0; i < 64 * 64; i++) {
473                         texels_layer[i][0] = btdf_split_sum_ggx[j * 2][i];
474                         texels_layer[i][1] = btdf_split_sum_ggx[j * 2][i];
475                         texels_layer[i][2] = btdf_split_sum_ggx[j * 2][i];
476                         texels_layer[i][3] = btdf_split_sum_ggx[j * 2][i];
477                 }
478                 texels_layer += 64 * 64;
479         }
480
481         e_data.util_tex = DRW_texture_create_2D_array(
482                 64, 64, layers, GPU_RGBA16F, DRW_TEX_FILTER | DRW_TEX_WRAP, (float *)texels);
483
484         MEM_freeN(texels);
485 }
486
487 void EEVEE_update_noise(EEVEE_PassList *psl, EEVEE_FramebufferList *fbl, const double offsets[3])
488 {
489         e_data.noise_offsets[0] = offsets[0];
490         e_data.noise_offsets[1] = offsets[1];
491         e_data.noise_offsets[2] = offsets[2];
492
493         /* Attach & detach because we don't currently support multiple FB per texture,
494          * and this would be the case for multiple viewport. */
495         GPU_framebuffer_bind(fbl->update_noise_fb);
496         DRW_draw_pass(psl->update_noise_pass);
497 }
498
499 static void EEVEE_update_viewvecs(float invproj[4][4], float winmat[4][4], float (*r_viewvecs)[4])
500 {
501         /* view vectors for the corners of the view frustum.
502          * Can be used to recreate the world space position easily */
503         float view_vecs[4][4] = {
504             {-1.0f, -1.0f, -1.0f, 1.0f},
505             { 1.0f, -1.0f, -1.0f, 1.0f},
506             {-1.0f,  1.0f, -1.0f, 1.0f},
507             {-1.0f, -1.0f,  1.0f, 1.0f}
508         };
509
510         /* convert the view vectors to view space */
511         const bool is_persp = (winmat[3][3] == 0.0f);
512         for (int i = 0; i < 4; i++) {
513                 mul_project_m4_v3(invproj, view_vecs[i]);
514                 /* normalized trick see:
515                  * http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
516                 if (is_persp) {
517                         /* Divide XY by Z. */
518                         mul_v2_fl(view_vecs[i], 1.0f / view_vecs[i][2]);
519                 }
520         }
521
522         /**
523          * If ortho : view_vecs[0] is the near-bottom-left corner of the frustum and
524          *            view_vecs[1] is the vector going from the near-bottom-left corner to
525          *            the far-top-right corner.
526          * If Persp : view_vecs[0].xy and view_vecs[1].xy are respectively the bottom-left corner
527          *            when Z = 1, and top-left corner if Z = 1.
528          *            view_vecs[0].z the near clip distance and view_vecs[1].z is the (signed)
529          *            distance from the near plane to the far clip plane.
530          **/
531         copy_v4_v4(r_viewvecs[0], view_vecs[0]);
532
533         /* we need to store the differences */
534         r_viewvecs[1][0] = view_vecs[1][0] - view_vecs[0][0];
535         r_viewvecs[1][1] = view_vecs[2][1] - view_vecs[0][1];
536         r_viewvecs[1][2] = view_vecs[3][2] - view_vecs[0][2];
537 }
538
539 void EEVEE_materials_init(EEVEE_ViewLayerData *sldata, EEVEE_StorageList *stl, EEVEE_FramebufferList *fbl)
540 {
541         if (!e_data.frag_shader_lib) {
542                 /* Shaders */
543                 e_data.frag_shader_lib = BLI_string_joinN(
544                         datatoc_common_view_lib_glsl,
545                         datatoc_common_uniforms_lib_glsl,
546                         datatoc_bsdf_common_lib_glsl,
547                         datatoc_bsdf_sampling_lib_glsl,
548                         datatoc_ambient_occlusion_lib_glsl,
549                         datatoc_raytrace_lib_glsl,
550                         datatoc_ssr_lib_glsl,
551                         datatoc_octahedron_lib_glsl,
552                         datatoc_irradiance_lib_glsl,
553                         datatoc_lightprobe_lib_glsl,
554                         datatoc_ltc_lib_glsl,
555                         datatoc_bsdf_direct_lib_glsl,
556                         datatoc_lamps_lib_glsl,
557                         /* Add one for each Closure */
558                         datatoc_lit_surface_frag_glsl,
559                         datatoc_lit_surface_frag_glsl,
560                         datatoc_lit_surface_frag_glsl,
561                         datatoc_lit_surface_frag_glsl,
562                         datatoc_lit_surface_frag_glsl,
563                         datatoc_lit_surface_frag_glsl,
564                         datatoc_lit_surface_frag_glsl,
565                         datatoc_lit_surface_frag_glsl,
566                         datatoc_volumetric_lib_glsl);
567
568                 e_data.volume_shader_lib = BLI_string_joinN(
569                         datatoc_common_view_lib_glsl,
570                         datatoc_common_uniforms_lib_glsl,
571                         datatoc_bsdf_common_lib_glsl,
572                         datatoc_ambient_occlusion_lib_glsl,
573                         datatoc_octahedron_lib_glsl,
574                         datatoc_irradiance_lib_glsl,
575                         datatoc_lightprobe_lib_glsl,
576                         datatoc_ltc_lib_glsl,
577                         datatoc_bsdf_direct_lib_glsl,
578                         datatoc_lamps_lib_glsl,
579                         datatoc_volumetric_lib_glsl,
580                         datatoc_volumetric_frag_glsl);
581
582                 e_data.vert_shader_str = BLI_string_joinN(
583                         datatoc_common_view_lib_glsl,
584                         datatoc_common_hair_lib_glsl,
585                         datatoc_lit_surface_vert_glsl);
586
587                 e_data.default_background = DRW_shader_create(
588                         datatoc_background_vert_glsl, NULL, datatoc_default_world_frag_glsl,
589                         NULL);
590
591                 e_data.default_studiolight_background = DRW_shader_create(
592                         datatoc_background_vert_glsl, NULL, datatoc_default_world_frag_glsl,
593                         "#define LOOKDEV\n");
594
595                 e_data.default_prepass_sh = DRW_shader_create(
596                         datatoc_prepass_vert_glsl, NULL, datatoc_prepass_frag_glsl,
597                         NULL);
598
599                 e_data.default_prepass_clip_sh = DRW_shader_create(
600                         datatoc_prepass_vert_glsl, NULL, datatoc_prepass_frag_glsl,
601                         "#define CLIP_PLANES\n");
602
603                 char *vert_str = BLI_string_joinN(
604                         datatoc_common_view_lib_glsl,
605                         datatoc_common_hair_lib_glsl,
606                         datatoc_prepass_vert_glsl);
607
608                 e_data.default_hair_prepass_sh = DRW_shader_create(
609                         vert_str, NULL, datatoc_prepass_frag_glsl,
610                         "#define HAIR_SHADER\n");
611
612                 e_data.default_hair_prepass_clip_sh = DRW_shader_create(
613                         vert_str, NULL, datatoc_prepass_frag_glsl,
614                         "#define HAIR_SHADER\n"
615                         "#define CLIP_PLANES\n");
616
617                 MEM_freeN(vert_str);
618
619                 e_data.update_noise_sh = DRW_shader_create_fullscreen(
620                         datatoc_update_noise_frag_glsl, NULL);
621
622                 eevee_init_util_texture();
623                 eevee_init_noise_texture();
624         }
625
626         if (!DRW_state_is_image_render() &&
627             ((stl->effects->enabled_effects & EFFECT_TAA) == 0))
628         {
629                 e_data.alpha_hash_offset = 0.0f;
630         }
631         else {
632                 double r;
633                 BLI_halton_1D(5, 0.0, stl->effects->taa_current_sample - 1, &r);
634                 e_data.alpha_hash_offset = (float)r;
635         }
636
637         {
638                 /* Update view_vecs */
639                 float invproj[4][4], winmat[4][4];
640                 DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
641                 DRW_viewport_matrix_get(invproj, DRW_MAT_WININV);
642
643                 EEVEE_update_viewvecs(invproj, winmat, sldata->common_data.view_vecs);
644         }
645
646         {
647                 /* Update noise Framebuffer. */
648                 GPU_framebuffer_ensure_config(&fbl->update_noise_fb, {
649                         GPU_ATTACHMENT_NONE,
650                         GPU_ATTACHMENT_TEXTURE_LAYER(e_data.util_tex, 2)
651                 });
652         }
653 }
654
655 struct GPUMaterial *EEVEE_material_world_lightprobe_get(struct Scene *scene, World *wo)
656 {
657         const void *engine = &DRW_engine_viewport_eevee_type;
658         const int options = VAR_WORLD_PROBE;
659
660         GPUMaterial *mat = DRW_shader_find_from_world(wo, engine, options);
661         if (mat != NULL) {
662                 return mat;
663         }
664         return DRW_shader_create_from_world(
665                 scene, wo, engine, options,
666                 datatoc_background_vert_glsl, NULL, e_data.frag_shader_lib,
667                 SHADER_DEFINES "#define PROBE_CAPTURE\n");
668 }
669
670 struct GPUMaterial *EEVEE_material_world_background_get(struct Scene *scene, World *wo)
671 {
672         const void *engine = &DRW_engine_viewport_eevee_type;
673         int options = VAR_WORLD_BACKGROUND;
674
675         GPUMaterial *mat = DRW_shader_find_from_world(wo, engine, options);
676         if (mat != NULL) {
677                 return mat;
678         }
679         return DRW_shader_create_from_world(
680                 scene, wo, engine, options,
681                 datatoc_background_vert_glsl, NULL, e_data.frag_shader_lib,
682                 SHADER_DEFINES "#define WORLD_BACKGROUND\n");
683 }
684
685 struct GPUMaterial *EEVEE_material_world_volume_get(struct Scene *scene, World *wo)
686 {
687         const void *engine = &DRW_engine_viewport_eevee_type;
688         int options = VAR_WORLD_VOLUME;
689
690         GPUMaterial *mat = DRW_shader_find_from_world(wo, engine, options);
691         if (mat != NULL) {
692                 return mat;
693         }
694
695         char *defines = eevee_get_volume_defines(options);
696
697         mat = DRW_shader_create_from_world(
698                 scene, wo, engine, options,
699                 datatoc_volumetric_vert_glsl, datatoc_volumetric_geom_glsl, e_data.volume_shader_lib,
700                 defines);
701
702         MEM_freeN(defines);
703
704         return mat;
705 }
706
707 struct GPUMaterial *EEVEE_material_mesh_get(
708         struct Scene *scene, Material *ma, EEVEE_Data *vedata,
709         bool use_blend, bool use_multiply, bool use_refract, bool use_sss, bool use_translucency, int shadow_method)
710 {
711         EEVEE_EffectsInfo *effects = vedata->stl->effects;
712         const void *engine = &DRW_engine_viewport_eevee_type;
713         int options = VAR_MAT_MESH;
714
715         if (use_blend) options |= VAR_MAT_BLEND;
716         if (use_multiply) options |= VAR_MAT_MULT;
717         if (use_refract) options |= VAR_MAT_REFRACT;
718         if (use_sss) options |= VAR_MAT_SSS;
719         if (use_sss && effects->sss_separate_albedo) options |= VAR_MAT_SSSALBED;
720         if (use_translucency) options |= VAR_MAT_TRANSLUC;
721         if (((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) && use_blend) options |= VAR_MAT_VOLUME;
722
723         options |= eevee_material_shadow_option(shadow_method);
724
725         GPUMaterial *mat = DRW_shader_find_from_material(ma, engine, options);
726         if (mat) {
727                 return mat;
728         }
729
730         char *defines = eevee_get_defines(options);
731
732         mat = DRW_shader_create_from_material(
733                 scene, ma, engine, options,
734                 e_data.vert_shader_str, NULL, e_data.frag_shader_lib,
735                 defines);
736
737         MEM_freeN(defines);
738
739         return mat;
740 }
741
742 struct GPUMaterial *EEVEE_material_mesh_volume_get(struct Scene *scene, Material *ma)
743 {
744         const void *engine = &DRW_engine_viewport_eevee_type;
745         int options = VAR_MAT_VOLUME;
746
747         GPUMaterial *mat = DRW_shader_find_from_material(ma, engine, options);
748         if (mat != NULL) {
749                 return mat;
750         }
751
752         char *defines = eevee_get_volume_defines(options);
753
754         mat = DRW_shader_create_from_material(
755                 scene, ma, engine, options,
756                 datatoc_volumetric_vert_glsl, datatoc_volumetric_geom_glsl, e_data.volume_shader_lib,
757                 defines);
758
759         MEM_freeN(defines);
760
761         return mat;
762 }
763
764 struct GPUMaterial *EEVEE_material_mesh_depth_get(
765         struct Scene *scene, Material *ma,
766         bool use_hashed_alpha, bool is_shadow)
767 {
768         const void *engine = &DRW_engine_viewport_eevee_type;
769         int options = VAR_MAT_MESH;
770
771         if (use_hashed_alpha) {
772                 options |= VAR_MAT_HASH;
773         }
774         else {
775                 options |= VAR_MAT_CLIP;
776         }
777
778         if (is_shadow)
779                 options |= VAR_MAT_SHADOW;
780
781         GPUMaterial *mat = DRW_shader_find_from_material(ma, engine, options);
782         if (mat) {
783                 return mat;
784         }
785
786         char *defines = eevee_get_defines(options);
787
788         char *frag_str = BLI_string_joinN(
789                 e_data.frag_shader_lib,
790                 datatoc_prepass_frag_glsl);
791
792         mat = DRW_shader_create_from_material(
793                 scene, ma, engine, options,
794                 (is_shadow) ? datatoc_shadow_vert_glsl : e_data.vert_shader_str,
795                 NULL,
796                 frag_str,
797                 defines);
798
799         MEM_freeN(frag_str);
800         MEM_freeN(defines);
801
802         return mat;
803 }
804
805 struct GPUMaterial *EEVEE_material_hair_get(
806         struct Scene *scene, Material *ma, int shadow_method)
807 {
808         const void *engine = &DRW_engine_viewport_eevee_type;
809         int options = VAR_MAT_MESH | VAR_MAT_HAIR;
810
811         options |= eevee_material_shadow_option(shadow_method);
812
813         GPUMaterial *mat = DRW_shader_find_from_material(ma, engine, options);
814         if (mat) {
815                 return mat;
816         }
817
818         char *defines = eevee_get_defines(options);
819
820         mat = DRW_shader_create_from_material(
821                 scene, ma, engine, options,
822                 e_data.vert_shader_str, NULL, e_data.frag_shader_lib,
823                 defines);
824
825         MEM_freeN(defines);
826
827         return mat;
828 }
829
830 /**
831  * Create a default shading group inside the given pass.
832  **/
833 static struct DRWShadingGroup *EEVEE_default_shading_group_create(
834         EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata, DRWPass *pass,
835         bool is_hair, bool is_flat_normal, bool use_blend, bool use_ssr, int shadow_method)
836 {
837         EEVEE_EffectsInfo *effects = vedata->stl->effects;
838         static int ssr_id;
839         ssr_id = (use_ssr) ? 1 : -1;
840         int options = VAR_MAT_MESH;
841
842         if (is_hair) options |= VAR_MAT_HAIR;
843         if (is_flat_normal) options |= VAR_MAT_FLAT;
844         if (use_blend) options |= VAR_MAT_BLEND;
845         if (((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) && use_blend) options |= VAR_MAT_VOLUME;
846
847         options |= eevee_material_shadow_option(shadow_method);
848
849         if (e_data.default_lit[options] == NULL) {
850                 create_default_shader(options);
851         }
852
853         DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.default_lit[options], pass);
854         add_standard_uniforms(shgrp, sldata, vedata, &ssr_id, NULL, false, use_blend);
855
856         return shgrp;
857 }
858
859 /**
860  * Create a default shading group inside the default pass without standard uniforms.
861  **/
862 static struct DRWShadingGroup *EEVEE_default_shading_group_get(
863         EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata,
864         Object *ob, ParticleSystem *psys, ModifierData *md,
865         bool is_hair, bool is_flat_normal, bool use_ssr, int shadow_method)
866 {
867         static int ssr_id;
868         ssr_id = (use_ssr) ? 1 : -1;
869         int options = VAR_MAT_MESH;
870
871         BLI_assert(!is_hair || (ob && psys && md));
872
873         if (is_hair) options |= VAR_MAT_HAIR;
874         if (is_flat_normal) options |= VAR_MAT_FLAT;
875
876         options |= eevee_material_shadow_option(shadow_method);
877
878         if (e_data.default_lit[options] == NULL) {
879                 create_default_shader(options);
880         }
881
882         if (vedata->psl->default_pass[options] == NULL) {
883                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
884                 vedata->psl->default_pass[options] = DRW_pass_create("Default Lit Pass", state);
885
886                 /* XXX / WATCH: This creates non persistent binds for the ubos and textures.
887                  * But it's currently OK because the following shgroups does not add any bind.
888                  * EDIT: THIS IS NOT THE CASE FOR HAIRS !!! DUMMY!!! */
889                 if (!is_hair) {
890                         DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.default_lit[options], vedata->psl->default_pass[options]);
891                         add_standard_uniforms(shgrp, sldata, vedata, &ssr_id, NULL, false, false);
892                 }
893         }
894
895         if (is_hair) {
896                 DRWShadingGroup *shgrp = DRW_shgroup_hair_create(ob, psys, md,
897                                                                  vedata->psl->default_pass[options], vedata->psl->hair_tf_pass,
898                                                                  e_data.default_lit[options]);
899                 add_standard_uniforms(shgrp, sldata, vedata, NULL, NULL, false, false);
900                 return shgrp;
901         }
902         else {
903                 return DRW_shgroup_create(e_data.default_lit[options], vedata->psl->default_pass[options]);
904         }
905 }
906
907 /**
908  * Create a default shading group inside the lookdev pass without standard uniforms.
909  **/
910 static struct DRWShadingGroup *EEVEE_lookdev_shading_group_get(
911         EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata,
912         bool use_ssr, int shadow_method)
913 {
914         static int ssr_id;
915         ssr_id = (use_ssr) ? 1 : -1;
916         int options = VAR_MAT_MESH | VAR_MAT_LOOKDEV;
917
918         options |= eevee_material_shadow_option(shadow_method);
919
920         if (e_data.default_lit[options] == NULL) {
921                 create_default_shader(options);
922         }
923
924         if (vedata->psl->lookdev_pass == NULL) {
925                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_CULL_BACK;
926                 vedata->psl->lookdev_pass = DRW_pass_create("LookDev Pass", state);
927
928                 DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.default_lit[options], vedata->psl->lookdev_pass);
929                 /* XXX / WATCH: This creates non persistent binds for the ubos and textures.
930                  * But it's currently OK because the following shgroups does not add any bind. */
931                 add_standard_uniforms(shgrp, sldata, vedata, &ssr_id, NULL, false, false);
932         }
933
934         return DRW_shgroup_create(e_data.default_lit[options], vedata->psl->lookdev_pass);
935 }
936 void EEVEE_materials_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
937 {
938         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
939         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
940
941         /* Create Material Ghash */
942         {
943                 stl->g_data->material_hash = BLI_ghash_ptr_new("Eevee_material ghash");
944                 stl->g_data->hair_material_hash = BLI_ghash_ptr_new("Eevee_hair_material ghash");
945         }
946
947         {
948                 psl->background_pass = DRW_pass_create("Background Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL);
949
950                 struct Gwn_Batch *geom = DRW_cache_fullscreen_quad_get();
951                 DRWShadingGroup *grp = NULL;
952
953                 const DRWContextState *draw_ctx = DRW_context_state_get();
954                 Scene *scene = draw_ctx->scene;
955                 World *wo = scene->world;
956
957                 float *col = ts.colorBackground;
958
959                 /* LookDev */
960                 EEVEE_lookdev_cache_init(vedata, &grp, e_data.default_studiolight_background, psl->background_pass, wo, NULL);
961                 /* END */
962
963                 if (!grp && wo) {
964                         col = &wo->horr;
965
966                         if (wo->use_nodes && wo->nodetree) {
967                                 static float error_col[3] = {1.0f, 0.0f, 1.0f};
968                                 static float compile_col[3] = {0.5f, 0.5f, 0.5f};
969                                 struct GPUMaterial *gpumat = EEVEE_material_world_background_get(scene, wo);
970
971                                 switch (GPU_material_status(gpumat)) {
972                                         case GPU_MAT_SUCCESS:
973                                                 grp = DRW_shgroup_material_create(gpumat, psl->background_pass);
974                                                 DRW_shgroup_uniform_float(grp, "backgroundAlpha", &stl->g_data->background_alpha, 1);
975                                                 DRW_shgroup_call_add(grp, geom, NULL);
976                                                 break;
977                                         case GPU_MAT_QUEUED:
978                                                 sldata->probes->all_materials_updated = false;
979                                                 /* TODO Bypass probe compilation. */
980                                                 col = compile_col;
981                                                 break;
982                                         case GPU_MAT_FAILED:
983                                         default:
984                                                 col = error_col;
985                                                 break;
986                                 }
987                         }
988                 }
989
990                 /* Fallback if shader fails or if not using nodetree. */
991                 if (grp == NULL) {
992                         grp = DRW_shgroup_create(e_data.default_background, psl->background_pass);
993                         DRW_shgroup_uniform_vec3(grp, "color", col, 1);
994                         DRW_shgroup_uniform_float(grp, "backgroundAlpha", &stl->g_data->background_alpha, 1);
995                         DRW_shgroup_call_add(grp, geom, NULL);
996                 }
997         }
998
999         {
1000                 DRWState state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE;
1001                 psl->depth_pass = DRW_pass_create("Depth Pass", state);
1002                 stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.default_prepass_sh, psl->depth_pass);
1003
1004                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK;
1005                 psl->depth_pass_cull = DRW_pass_create("Depth Pass Cull", state);
1006                 stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.default_prepass_sh, psl->depth_pass_cull);
1007
1008                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
1009                 psl->depth_pass_clip = DRW_pass_create("Depth Pass Clip", state);
1010                 stl->g_data->depth_shgrp_clip = DRW_shgroup_create(e_data.default_prepass_clip_sh, psl->depth_pass_clip);
1011                 DRW_shgroup_uniform_block(stl->g_data->depth_shgrp_clip, "clip_block", sldata->clip_ubo);
1012
1013                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_CULL_BACK;
1014                 psl->depth_pass_clip_cull = DRW_pass_create("Depth Pass Cull Clip", state);
1015                 stl->g_data->depth_shgrp_clip_cull = DRW_shgroup_create(
1016                         e_data.default_prepass_clip_sh, psl->depth_pass_clip_cull);
1017                 DRW_shgroup_uniform_block(stl->g_data->depth_shgrp_clip_cull, "clip_block", sldata->clip_ubo);
1018         }
1019
1020         {
1021                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
1022                 psl->material_pass = DRW_pass_create("Material Shader Pass", state);
1023         }
1024
1025         {
1026                 DRWState state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE;
1027                 psl->refract_depth_pass = DRW_pass_create("Refract Depth Pass", state);
1028                 stl->g_data->refract_depth_shgrp = DRW_shgroup_create(e_data.default_prepass_sh, psl->refract_depth_pass);
1029
1030                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK;
1031                 psl->refract_depth_pass_cull = DRW_pass_create("Refract Depth Pass Cull", state);
1032                 stl->g_data->refract_depth_shgrp_cull = DRW_shgroup_create(
1033                         e_data.default_prepass_sh, psl->refract_depth_pass_cull);
1034
1035                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
1036                 psl->refract_depth_pass_clip = DRW_pass_create("Refract Depth Pass Clip", state);
1037                 stl->g_data->refract_depth_shgrp_clip = DRW_shgroup_create(
1038                         e_data.default_prepass_clip_sh, psl->refract_depth_pass_clip);
1039                 DRW_shgroup_uniform_block(stl->g_data->refract_depth_shgrp_clip, "clip_block", sldata->clip_ubo);
1040
1041                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_CULL_BACK;
1042                 psl->refract_depth_pass_clip_cull = DRW_pass_create("Refract Depth Pass Cull Clip", state);
1043                 stl->g_data->refract_depth_shgrp_clip_cull = DRW_shgroup_create(
1044                         e_data.default_prepass_clip_sh, psl->refract_depth_pass_clip_cull);
1045                 DRW_shgroup_uniform_block(stl->g_data->refract_depth_shgrp_clip_cull, "clip_block", sldata->clip_ubo);
1046         }
1047
1048         {
1049                 DRWState state = (
1050                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_CLIP_PLANES |
1051                         DRW_STATE_WIRE);
1052                 psl->refract_pass = DRW_pass_create("Opaque Refraction Pass", state);
1053         }
1054
1055         {
1056                 DRWState state = (
1057                         DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_CLIP_PLANES |
1058                         DRW_STATE_WIRE | DRW_STATE_WRITE_STENCIL);
1059                 psl->sss_pass = DRW_pass_create("Subsurface Pass", state);
1060                 e_data.sss_count = 0;
1061         }
1062
1063         {
1064                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
1065                 psl->transparent_pass = DRW_pass_create("Material Transparent Pass", state);
1066         }
1067
1068         {
1069                 psl->update_noise_pass = DRW_pass_create("Update Noise Pass", DRW_STATE_WRITE_COLOR);
1070                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.update_noise_sh, psl->update_noise_pass);
1071                 DRW_shgroup_uniform_texture(grp, "blueNoise", e_data.noise_tex);
1072                 DRW_shgroup_uniform_vec3(grp, "offsets", e_data.noise_offsets, 1);
1073                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
1074         }
1075
1076         {
1077                 psl->hair_tf_pass = DRW_pass_create("Update Hair Pass", DRW_STATE_TRANS_FEEDBACK);
1078         }
1079 }
1080
1081 #define ADD_SHGROUP_CALL(shgrp, ob, geom, oedata) do { \
1082         if (is_sculpt_mode_draw) { \
1083                 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat); \
1084         } \
1085         else { \
1086                 if (oedata) { \
1087                         DRW_shgroup_call_object_add_with_callback(shgrp, geom, ob, EEVEE_lightprobes_obj_visibility_cb, oedata); \
1088                 } \
1089                 else { \
1090                         DRW_shgroup_call_object_add(shgrp, geom, ob); \
1091                 } \
1092         } \
1093 } while (0)
1094
1095 #define ADD_SHGROUP_CALL_SAFE(shgrp, ob, geom, oedata) do { \
1096         if (shgrp) { \
1097                 ADD_SHGROUP_CALL(shgrp, ob, geom, oedata); \
1098         } \
1099 } while (0)
1100
1101 typedef struct EeveeMaterialShadingGroups {
1102         struct DRWShadingGroup *shading_grp;
1103         struct DRWShadingGroup *depth_grp;
1104         struct DRWShadingGroup *depth_clip_grp;
1105 } EeveeMaterialShadingGroups;
1106
1107 static void material_opaque(
1108         Material *ma, GHash *material_hash, EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata,
1109         bool do_cull, bool use_flat_nor, struct GPUMaterial **gpumat, struct GPUMaterial **gpumat_depth,
1110         struct DRWShadingGroup **shgrp, struct DRWShadingGroup **shgrp_depth, struct DRWShadingGroup **shgrp_depth_clip)
1111 {
1112         EEVEE_EffectsInfo *effects = vedata->stl->effects;
1113         const DRWContextState *draw_ctx = DRW_context_state_get();
1114         Scene *scene = draw_ctx->scene;
1115         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
1116         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
1117         EEVEE_LampsInfo *linfo = sldata->lamps;
1118
1119         float *color_p = &ma->r;
1120         float *metal_p = &ma->metallic;
1121         float *spec_p = &ma->spec;
1122         float *rough_p = &ma->roughness;
1123
1124         const bool use_gpumat = (ma->use_nodes && ma->nodetree);
1125         const bool use_refract = ((ma->blend_flag & MA_BL_SS_REFRACTION) != 0) &&
1126                                  ((effects->enabled_effects & EFFECT_REFRACT) != 0);
1127         const bool use_sss = ((ma->blend_flag & MA_BL_SS_SUBSURFACE) != 0) &&
1128                              ((effects->enabled_effects & EFFECT_SSS) != 0);
1129         const bool use_translucency = use_sss && ((ma->blend_flag & MA_BL_TRANSLUCENCY) != 0);
1130
1131         EeveeMaterialShadingGroups *emsg = BLI_ghash_lookup(material_hash, (const void *)ma);
1132
1133         if (emsg) {
1134                 *shgrp = emsg->shading_grp;
1135                 *shgrp_depth = emsg->depth_grp;
1136                 *shgrp_depth_clip = emsg->depth_clip_grp;
1137
1138                 /* This will have been created already, just perform a lookup. */
1139                 *gpumat = (use_gpumat) ? EEVEE_material_mesh_get(
1140                         scene, ma, vedata, false, false, use_refract, use_sss, use_translucency, linfo->shadow_method) : NULL;
1141                 *gpumat_depth = (use_gpumat) ? EEVEE_material_mesh_depth_get(
1142                         scene, ma, (ma->blend_method == MA_BM_HASHED), false) : NULL;
1143                 return;
1144         }
1145
1146         if (use_gpumat) {
1147                 static float error_col[3] = {1.0f, 0.0f, 1.0f};
1148                 static float compile_col[3] = {0.5f, 0.5f, 0.5f};
1149                 static float half = 0.5f;
1150
1151                 /* Shading */
1152                 *gpumat = EEVEE_material_mesh_get(
1153                         scene, ma, vedata, false, false, use_refract,
1154                         use_sss, use_translucency, linfo->shadow_method);
1155
1156                 GPUMaterialStatus status_mat_surface = GPU_material_status(*gpumat);
1157
1158                 /* Alpha CLipped : Discard pixel from depth pass, then
1159                  * fail the depth test for shading. */
1160                 if (ELEM(ma->blend_method, MA_BM_CLIP, MA_BM_HASHED)) {
1161                         *gpumat_depth = EEVEE_material_mesh_depth_get(scene, ma, (ma->blend_method == MA_BM_HASHED), false);
1162
1163                         GPUMaterialStatus status_mat_depth = GPU_material_status(*gpumat_depth);
1164                         if (status_mat_depth != GPU_MAT_SUCCESS) {
1165                                 /* Mixing both flags. If depth shader fails, show it to the user by not using
1166                                  * the surface shader. */
1167                                 status_mat_surface = status_mat_depth;
1168                         }
1169                         else if (use_refract) {
1170                                 *shgrp_depth = DRW_shgroup_material_create(
1171                                         *gpumat_depth, (do_cull) ? psl->refract_depth_pass_cull : psl->refract_depth_pass);
1172                                 *shgrp_depth_clip = DRW_shgroup_material_create(
1173                                         *gpumat_depth, (do_cull) ? psl->refract_depth_pass_clip_cull : psl->refract_depth_pass_clip);
1174                         }
1175                         else {
1176                                 *shgrp_depth = DRW_shgroup_material_create(
1177                                         *gpumat_depth, (do_cull) ? psl->depth_pass_cull : psl->depth_pass);
1178                                 *shgrp_depth_clip = DRW_shgroup_material_create(
1179                                         *gpumat_depth, (do_cull) ? psl->depth_pass_clip_cull : psl->depth_pass_clip);
1180                         }
1181
1182                         if (*shgrp_depth != NULL) {
1183                                 add_standard_uniforms(*shgrp_depth, sldata, vedata, NULL, NULL, false, false);
1184                                 add_standard_uniforms(*shgrp_depth_clip, sldata, vedata, NULL, NULL, false, false);
1185
1186                                 if (ma->blend_method == MA_BM_CLIP) {
1187                                         DRW_shgroup_uniform_float(*shgrp_depth, "alphaThreshold", &ma->alpha_threshold, 1);
1188                                         DRW_shgroup_uniform_float(*shgrp_depth_clip, "alphaThreshold", &ma->alpha_threshold, 1);
1189                                 }
1190                                 else if (ma->blend_method == MA_BM_HASHED) {
1191                                         DRW_shgroup_uniform_float(*shgrp_depth, "hashAlphaOffset", &e_data.alpha_hash_offset, 1);
1192                                         DRW_shgroup_uniform_float(*shgrp_depth_clip, "hashAlphaOffset", &e_data.alpha_hash_offset, 1);
1193                                 }
1194                         }
1195                 }
1196
1197                 switch (status_mat_surface) {
1198                         case GPU_MAT_SUCCESS:
1199                         {
1200                                 static int no_ssr = -1;
1201                                 static int first_ssr = 1;
1202                                 int *ssr_id = (((effects->enabled_effects & EFFECT_SSR) != 0) && !use_refract) ? &first_ssr : &no_ssr;
1203
1204                                 *shgrp = DRW_shgroup_material_create(
1205                                         *gpumat,
1206                                         (use_refract) ? psl->refract_pass :
1207                                         (use_sss) ? psl->sss_pass : psl->material_pass);
1208                                 add_standard_uniforms(*shgrp, sldata, vedata, ssr_id, &ma->refract_depth, use_refract, false);
1209
1210                                 if (use_sss) {
1211                                         struct GPUTexture *sss_tex_profile = NULL;
1212                                         struct GPUUniformBuffer *sss_profile = GPU_material_sss_profile_get(
1213                                                 *gpumat,
1214                                                 stl->effects->sss_sample_count,
1215                                                 &sss_tex_profile);
1216
1217                                         if (sss_profile) {
1218                                                 if (use_translucency) {
1219                                                         DRW_shgroup_uniform_block(*shgrp, "sssProfile", sss_profile);
1220                                                         DRW_shgroup_uniform_texture(*shgrp, "sssTexProfile", sss_tex_profile);
1221                                                 }
1222
1223                                                 /* Limit of 8 bit stencil buffer. ID 255 is refraction. */
1224                                                 if (e_data.sss_count < 254) {
1225                                                         DRW_shgroup_stencil_mask(*shgrp, e_data.sss_count + 1);
1226                                                         EEVEE_subsurface_add_pass(sldata, vedata, e_data.sss_count + 1, sss_profile);
1227                                                         e_data.sss_count++;
1228                                                 }
1229                                                 else {
1230                                                         /* TODO : display message. */
1231                                                         printf("Error: Too many different Subsurface shader in the scene.\n");
1232                                                 }
1233                                         }
1234                                 }
1235                                 break;
1236                         }
1237                         case GPU_MAT_QUEUED:
1238                         {
1239                                 sldata->probes->all_materials_updated = false;
1240                                 /* TODO Bypass probe compilation. */
1241                                 color_p = compile_col;
1242                                 metal_p = spec_p = rough_p = &half;
1243                                 break;
1244                         }
1245                         case GPU_MAT_FAILED:
1246                         default:
1247                                 color_p = error_col;
1248                                 metal_p = spec_p = rough_p = &half;
1249                                 break;
1250                 }
1251         }
1252
1253         /* Fallback to default shader */
1254         if (*shgrp == NULL) {
1255                 bool use_ssr = ((effects->enabled_effects & EFFECT_SSR) != 0);
1256                 *shgrp = EEVEE_default_shading_group_get(sldata, vedata,
1257                                                          NULL, NULL, NULL,
1258                                                          false, use_flat_nor, use_ssr, linfo->shadow_method);
1259                 DRW_shgroup_uniform_vec3(*shgrp, "basecol", color_p, 1);
1260                 DRW_shgroup_uniform_float(*shgrp, "metallic", metal_p, 1);
1261                 DRW_shgroup_uniform_float(*shgrp, "specular", spec_p, 1);
1262                 DRW_shgroup_uniform_float(*shgrp, "roughness", rough_p, 1);
1263         }
1264
1265         /* Fallback default depth prepass */
1266         if (*shgrp_depth == NULL) {
1267                 if (use_refract) {
1268                         *shgrp_depth = (do_cull) ? stl->g_data->refract_depth_shgrp_cull : stl->g_data->refract_depth_shgrp;
1269                         *shgrp_depth_clip = (do_cull) ? stl->g_data->refract_depth_shgrp_clip_cull : stl->g_data->refract_depth_shgrp_clip;
1270                 }
1271                 else {
1272                         *shgrp_depth = (do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp;
1273                         *shgrp_depth_clip = (do_cull) ? stl->g_data->depth_shgrp_clip_cull : stl->g_data->depth_shgrp_clip;
1274                 }
1275         }
1276
1277         emsg = MEM_mallocN(sizeof(EeveeMaterialShadingGroups), "EeveeMaterialShadingGroups");
1278         emsg->shading_grp = *shgrp;
1279         emsg->depth_grp = *shgrp_depth;
1280         emsg->depth_clip_grp = *shgrp_depth_clip;
1281         BLI_ghash_insert(material_hash, ma, emsg);
1282 }
1283
1284 static void material_transparent(
1285         Material *ma, EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata,
1286         bool do_cull, bool use_flat_nor,
1287         struct GPUMaterial **gpumat, struct DRWShadingGroup **shgrp, struct DRWShadingGroup **shgrp_depth)
1288 {
1289         const DRWContextState *draw_ctx = DRW_context_state_get();
1290         Scene *scene = draw_ctx->scene;
1291         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
1292         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
1293         EEVEE_LampsInfo *linfo = sldata->lamps;
1294
1295         const bool use_refract = (
1296                 ((ma->blend_flag & MA_BL_SS_REFRACTION) != 0) &&
1297                 ((stl->effects->enabled_effects & EFFECT_REFRACT) != 0)
1298         );
1299         float *color_p = &ma->r;
1300         float *metal_p = &ma->metallic;
1301         float *spec_p = &ma->spec;
1302         float *rough_p = &ma->roughness;
1303
1304         if (ma->use_nodes && ma->nodetree) {
1305                 static float error_col[3] = {1.0f, 0.0f, 1.0f};
1306                 static float compile_col[3] = {0.5f, 0.5f, 0.5f};
1307                 static float half = 0.5f;
1308
1309                 /* Shading */
1310                 *gpumat = EEVEE_material_mesh_get(
1311                         scene, ma, vedata, true, (ma->blend_method == MA_BM_MULTIPLY), use_refract,
1312                         false, false, linfo->shadow_method);
1313
1314                 switch (GPU_material_status(*gpumat)) {
1315                         case GPU_MAT_SUCCESS:
1316                         {
1317                                 static int ssr_id = -1; /* TODO transparent SSR */
1318                                 bool use_blend = (ma->blend_method & MA_BM_BLEND) != 0;
1319
1320                                 *shgrp = DRW_shgroup_material_create(*gpumat, psl->transparent_pass);
1321                                 add_standard_uniforms(*shgrp, sldata, vedata, &ssr_id, &ma->refract_depth, use_refract, use_blend);
1322                                 break;
1323                         }
1324                         case GPU_MAT_QUEUED:
1325                         {
1326                                 sldata->probes->all_materials_updated = false;
1327                                 /* TODO Bypass probe compilation. */
1328                                 color_p = compile_col;
1329                                 metal_p = spec_p = rough_p = &half;
1330                                 break;
1331                         }
1332                         case GPU_MAT_FAILED:
1333                         default:
1334                                 color_p = error_col;
1335                                 metal_p = spec_p = rough_p = &half;
1336                                 break;
1337                 }
1338         }
1339
1340         /* Fallback to default shader */
1341         if (*shgrp == NULL) {
1342                 *shgrp = EEVEE_default_shading_group_create(
1343                         sldata, vedata, psl->transparent_pass,
1344                         false, use_flat_nor, true, false, linfo->shadow_method);
1345                 DRW_shgroup_uniform_vec3(*shgrp, "basecol", color_p, 1);
1346                 DRW_shgroup_uniform_float(*shgrp, "metallic", metal_p, 1);
1347                 DRW_shgroup_uniform_float(*shgrp, "specular", spec_p, 1);
1348                 DRW_shgroup_uniform_float(*shgrp, "roughness", rough_p, 1);
1349         }
1350
1351         const bool use_prepass = ((ma->blend_flag & MA_BL_HIDE_BACKSIDE) != 0);
1352
1353         DRWState all_state = (
1354                 DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_CULL_BACK |
1355                 DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_DEPTH_EQUAL |
1356                 DRW_STATE_BLEND | DRW_STATE_ADDITIVE | DRW_STATE_MULTIPLY
1357         );
1358
1359         DRWState cur_state = DRW_STATE_WRITE_COLOR;
1360         cur_state |= (use_prepass) ? DRW_STATE_DEPTH_EQUAL : DRW_STATE_DEPTH_LESS_EQUAL;
1361         cur_state |= (do_cull) ? DRW_STATE_CULL_BACK : 0;
1362
1363         switch (ma->blend_method) {
1364                 case MA_BM_ADD:
1365                         cur_state |= DRW_STATE_ADDITIVE;
1366                         break;
1367                 case MA_BM_MULTIPLY:
1368                         cur_state |= DRW_STATE_MULTIPLY;
1369                         break;
1370                 case MA_BM_BLEND:
1371                         cur_state |= DRW_STATE_BLEND;
1372                         break;
1373                 default:
1374                         BLI_assert(0);
1375                         break;
1376         }
1377
1378         /* Disable other blend modes and use the one we want. */
1379         DRW_shgroup_state_disable(*shgrp, all_state);
1380         DRW_shgroup_state_enable(*shgrp, cur_state);
1381
1382         /* Depth prepass */
1383         if (use_prepass) {
1384                 *shgrp_depth = DRW_shgroup_create(e_data.default_prepass_clip_sh, psl->transparent_pass);
1385                 DRW_shgroup_uniform_block(*shgrp_depth, "clip_block", sldata->clip_ubo);
1386
1387                 cur_state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
1388                 cur_state |= (do_cull) ? DRW_STATE_CULL_BACK : 0;
1389
1390                 DRW_shgroup_state_disable(*shgrp_depth, all_state);
1391                 DRW_shgroup_state_enable(*shgrp_depth, cur_state);
1392         }
1393 }
1394
1395 void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob, bool *cast_shadow)
1396 {
1397         EEVEE_PassList *psl = vedata->psl;
1398         EEVEE_StorageList *stl = vedata->stl;
1399         const DRWContextState *draw_ctx = DRW_context_state_get();
1400         Scene *scene = draw_ctx->scene;
1401         GHash *material_hash = stl->g_data->material_hash;
1402
1403         const bool do_cull = (draw_ctx->v3d && (draw_ctx->v3d->flag2 & V3D_BACKFACE_CULLING));
1404         const bool is_active = (ob == draw_ctx->obact);
1405         const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
1406 #if 0
1407         const bool is_sculpt_mode_draw = is_sculpt_mode && (draw_ctx->v3d->flag2 & V3D_SHOW_MODE_SHADE_OVERRIDE) == 0;
1408 #else
1409         /* For now just force fully shaded with eevee when supported. */
1410         const bool is_sculpt_mode_draw =
1411                 is_sculpt_mode &&
1412                 ((ob->sculpt && ob->sculpt->pbvh) && (BKE_pbvh_type(ob->sculpt->pbvh) != PBVH_FACES));
1413 #endif
1414         const bool is_default_mode_shader = is_sculpt_mode;
1415
1416         /* First get materials for this mesh. */
1417         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1418                 const int materials_len = MAX2(1, (is_sculpt_mode_draw ? 1 : ob->totcol));
1419
1420                 struct DRWShadingGroup **shgrp_array = BLI_array_alloca(shgrp_array, materials_len);
1421                 struct DRWShadingGroup **shgrp_depth_array = BLI_array_alloca(shgrp_depth_array, materials_len);
1422                 struct DRWShadingGroup **shgrp_depth_clip_array = BLI_array_alloca(shgrp_depth_clip_array, materials_len);
1423
1424                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
1425                 struct GPUMaterial **gpumat_depth_array = BLI_array_alloca(gpumat_array, materials_len);
1426
1427                 bool use_flat_nor = false;
1428
1429                 if (is_default_mode_shader) {
1430                         if (is_sculpt_mode_draw) {
1431                                 use_flat_nor = DRW_object_is_flat_normal(ob);
1432                         }
1433                 }
1434
1435                 for (int i = 0; i < materials_len; ++i) {
1436                         Material *ma;
1437
1438                         if (is_sculpt_mode_draw) {
1439                                 ma = NULL;
1440                         }
1441                         else {
1442                                 ma = give_current_material(ob, i + 1);
1443                         }
1444
1445                         gpumat_array[i] = NULL;
1446                         gpumat_depth_array[i] = NULL;
1447                         shgrp_array[i] = NULL;
1448                         shgrp_depth_array[i] = NULL;
1449                         shgrp_depth_clip_array[i] = NULL;
1450
1451                         if (ma == NULL)
1452                                 ma = &defmaterial;
1453
1454                         switch (ma->blend_method) {
1455                                 case MA_BM_SOLID:
1456                                 case MA_BM_CLIP:
1457                                 case MA_BM_HASHED:
1458                                         material_opaque(ma, material_hash, sldata, vedata, do_cull, use_flat_nor,
1459                                                 &gpumat_array[i], &gpumat_depth_array[i],
1460                                                 &shgrp_array[i], &shgrp_depth_array[i], &shgrp_depth_clip_array[i]);
1461                                         break;
1462                                 case MA_BM_ADD:
1463                                 case MA_BM_MULTIPLY:
1464                                 case MA_BM_BLEND:
1465                                         material_transparent(ma, sldata, vedata, do_cull, use_flat_nor,
1466                                                 &gpumat_array[i], &shgrp_array[i], &shgrp_depth_array[i]);
1467                                         break;
1468                                 default:
1469                                         BLI_assert(0);
1470                                         break;
1471                         }
1472                 }
1473
1474                 if (is_sculpt_mode && is_sculpt_mode_draw == false) {
1475                         DRW_cache_mesh_sculpt_coords_ensure(ob);
1476                 }
1477
1478                 /* Only support single volume material for now. */
1479                 /* XXX We rely on the previously compiled surface shader
1480                  * to know if the material has a "volume nodetree".
1481                  */
1482                 bool use_volume_material = (gpumat_array[0] && GPU_material_use_domain_volume(gpumat_array[0]));
1483
1484                 /* Get per-material split surface */
1485                 char *auto_layer_names;
1486                 int *auto_layer_is_srgb;
1487                 int auto_layer_count;
1488                 struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(
1489                         ob, gpumat_array, materials_len,
1490                         &auto_layer_names,
1491                         &auto_layer_is_srgb,
1492                         &auto_layer_count);
1493                 if (mat_geom) {
1494                         for (int i = 0; i < materials_len; ++i) {
1495                                 EEVEE_ObjectEngineData *oedata = NULL;
1496                                 Material *ma = give_current_material(ob, i + 1);
1497
1498                                 if (ma == NULL)
1499                                         ma = &defmaterial;
1500
1501                                 /* Do not render surface if we are rendering a volume object
1502                                  * and do not have a surface closure. */
1503                                 if (use_volume_material &&
1504                                     (gpumat_array[i] && !GPU_material_use_domain_surface(gpumat_array[i])))
1505                                 {
1506                                         continue;
1507                                 }
1508
1509                                 /* XXX TODO rewrite this to include the dupli objects.
1510                                  * This means we cannot exclude dupli objects from reflections!!! */
1511                                 if ((ob->base_flag & BASE_FROMDUPLI) == 0) {
1512                                         oedata = EEVEE_object_data_ensure(ob);
1513                                         oedata->ob = ob;
1514                                         oedata->test_data = &sldata->probes->vis_data;
1515                                 }
1516
1517                                 /* Shading pass */
1518                                 ADD_SHGROUP_CALL(shgrp_array[i], ob, mat_geom[i], oedata);
1519
1520                                 /* Depth Prepass */
1521                                 ADD_SHGROUP_CALL_SAFE(shgrp_depth_array[i], ob, mat_geom[i], oedata);
1522                                 ADD_SHGROUP_CALL_SAFE(shgrp_depth_clip_array[i], ob, mat_geom[i], oedata);
1523
1524                                 char *name = auto_layer_names;
1525                                 for (int j = 0; j < auto_layer_count; ++j) {
1526                                         /* TODO don't add these uniform when not needed (default pass shaders). */
1527                                         if (shgrp_array[i]) {
1528                                                 DRW_shgroup_uniform_bool(shgrp_array[i], name, &auto_layer_is_srgb[j], 1);
1529                                         }
1530                                         if (shgrp_depth_array[i]) {
1531                                                 DRW_shgroup_uniform_bool(shgrp_depth_array[i], name, &auto_layer_is_srgb[j], 1);
1532                                         }
1533                                         if (shgrp_depth_clip_array[i]) {
1534                                                 DRW_shgroup_uniform_bool(shgrp_depth_clip_array[i], name, &auto_layer_is_srgb[j], 1);
1535                                         }
1536                                         /* Go to next layer name. */
1537                                         while (*name != '\0') { name++; }
1538                                         name += 1;
1539                                 }
1540
1541                                 /* Shadow Pass */
1542                                 if (ma->use_nodes && ma->nodetree && (ma->blend_method != MA_BM_SOLID)) {
1543                                         struct GPUMaterial *gpumat;
1544                                         switch (ma->blend_shadow) {
1545                                                 case MA_BS_SOLID:
1546                                                         EEVEE_lights_cache_shcaster_add(
1547                                                                 sldata, stl, mat_geom[i], ob);
1548                                                         *cast_shadow = true;
1549                                                         break;
1550                                                 case MA_BS_CLIP:
1551                                                         gpumat = EEVEE_material_mesh_depth_get(scene, ma, false, true);
1552                                                         EEVEE_lights_cache_shcaster_material_add(
1553                                                                 sldata, psl, gpumat, mat_geom[i], ob, &ma->alpha_threshold);
1554                                                         *cast_shadow = true;
1555                                                         break;
1556                                                 case MA_BS_HASHED:
1557                                                         gpumat = EEVEE_material_mesh_depth_get(scene, ma, true, true);
1558                                                         EEVEE_lights_cache_shcaster_material_add(
1559                                                                 sldata, psl, gpumat, mat_geom[i], ob, NULL);
1560                                                         *cast_shadow = true;
1561                                                         break;
1562                                                 case MA_BS_NONE:
1563                                                 default:
1564                                                         break;
1565                                         }
1566                                 }
1567                                 else {
1568                                         EEVEE_lights_cache_shcaster_add(sldata, stl, mat_geom[i], ob);
1569                                         *cast_shadow = true;
1570                                 }
1571                         }
1572                 }
1573
1574                 /* Volumetrics */
1575                 if (((stl->effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) && use_volume_material) {
1576                         EEVEE_volumes_cache_object_add(sldata, vedata, scene, ob);
1577                 }
1578         }
1579 }
1580
1581 void EEVEE_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob, bool *cast_shadow)
1582 {
1583         EEVEE_PassList *psl = vedata->psl;
1584         EEVEE_StorageList *stl = vedata->stl;
1585         const DRWContextState *draw_ctx = DRW_context_state_get();
1586         Scene *scene = draw_ctx->scene;
1587
1588         if (ob->type == OB_MESH) {
1589                 if (ob != draw_ctx->object_edit) {
1590                         material_hash = stl->g_data->hair_material_hash;
1591                         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
1592                                 if (md->type != eModifierType_ParticleSystem) {
1593                                         continue;
1594                                 }
1595                                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
1596                                 if (!psys_check_enabled(ob, psys, false)) {
1597                                         continue;
1598                                 }
1599                                 if (!DRW_check_psys_visible_within_active_context(ob, psys)) {
1600                                         continue;
1601                                 }
1602                                 ParticleSettings *part = psys->part;
1603                                 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
1604                                 if (draw_as != PART_DRAW_PATH) {
1605                                         continue;
1606                                 }
1607
1608                                 DRWShadingGroup *shgrp = NULL;
1609                                 Material *ma = give_current_material(ob, part->omat);
1610
1611                                 if (ma == NULL) {
1612                                         ma = &defmaterial;
1613                                 }
1614
1615                                 float *color_p = &ma->r;
1616                                 float *metal_p = &ma->metallic;
1617                                 float *spec_p = &ma->spec;
1618                                 float *rough_p = &ma->roughness;
1619
1620                                 shgrp = DRW_shgroup_hair_create(
1621                                         ob, psys, md,
1622                                         psl->depth_pass, psl->hair_tf_pass,
1623                                         e_data.default_hair_prepass_sh);
1624
1625                                 shgrp = DRW_shgroup_hair_create(
1626                                         ob, psys, md,
1627                                         psl->depth_pass_clip, psl->hair_tf_pass,
1628                                         e_data.default_hair_prepass_clip_sh);
1629                                 DRW_shgroup_uniform_block(shgrp, "clip_block", sldata->clip_ubo);
1630
1631                                 shgrp = NULL;
1632                                 if (ma->use_nodes && ma->nodetree) {
1633                                         static float half = 0.5f;
1634                                         static float error_col[3] = {1.0f, 0.0f, 1.0f};
1635                                         static float compile_col[3] = {0.5f, 0.5f, 0.5f};
1636                                         struct GPUMaterial *gpumat = EEVEE_material_hair_get(scene, ma, sldata->lamps->shadow_method);
1637
1638                                         switch (GPU_material_status(gpumat)) {
1639                                                 case GPU_MAT_SUCCESS:
1640                                                 {
1641                                                         shgrp = DRW_shgroup_material_hair_create(
1642                                                                 ob, psys, md,
1643                                                                 psl->material_pass, psl->hair_tf_pass,
1644                                                                 gpumat);
1645                                                         add_standard_uniforms(shgrp, sldata, vedata, NULL, NULL, false, false);
1646                                                         break;
1647                                                 }
1648                                                 case GPU_MAT_QUEUED:
1649                                                 {
1650                                                         sldata->probes->all_materials_updated = false;
1651                                                         color_p = compile_col;
1652                                                         metal_p = spec_p = rough_p = &half;
1653                                                         break;
1654                                                 }
1655                                                 case GPU_MAT_FAILED:
1656                                                 default:
1657                                                         color_p = error_col;
1658                                                         metal_p = spec_p = rough_p = &half;
1659                                                         break;
1660                                         }
1661                                 }
1662
1663                                 /* Fallback to default shader */
1664                                 if (shgrp == NULL) {
1665                                         bool use_ssr = ((stl->effects->enabled_effects & EFFECT_SSR) != 0);
1666                                         shgrp = EEVEE_default_shading_group_get(sldata, vedata,
1667                                                                                 ob, psys, md,
1668                                                                                 true, false, use_ssr,
1669                                                                                 sldata->lamps->shadow_method);
1670                                         DRW_shgroup_uniform_vec3(shgrp, "basecol", color_p, 1);
1671                                         DRW_shgroup_uniform_float(shgrp, "metallic", metal_p, 1);
1672                                         DRW_shgroup_uniform_float(shgrp, "specular", spec_p, 1);
1673                                         DRW_shgroup_uniform_float(shgrp, "roughness", rough_p, 1);
1674                                 }
1675                         }
1676                 }
1677         }
1678 }
1679
1680 void EEVEE_materials_cache_finish(EEVEE_Data *vedata)
1681 {
1682         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
1683
1684         /* Look-Dev */
1685         const DRWContextState *draw_ctx = DRW_context_state_get();
1686         const View3D *v3d = draw_ctx->v3d;
1687         if (LOOK_DEV_OVERLAY_ENABLED(v3d)) {
1688                 EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
1689                 EEVEE_LampsInfo *linfo = sldata->lamps;
1690                 struct Gwn_Batch *sphere = DRW_cache_sphere_get();
1691                 static float mat1[4][4];
1692                 static float color[3] = {0.8f, 0.8f, 0.8f};
1693                 static float metallic_on = 1.0f;
1694                 static float metallic_off = 0.00f;
1695                 static float specular_off = 0.5f;
1696                 static float specular_on = 1.0f;
1697                 static float roughness_off = 0.05f;
1698                 static float roughness_on = 1.00f;
1699
1700                 float view_mat[4][4];
1701                 DRW_viewport_matrix_get(view_mat, DRW_MAT_VIEWINV);
1702
1703                 DRWShadingGroup *shgrp = EEVEE_lookdev_shading_group_get(sldata, vedata, false, linfo->shadow_method);
1704                 DRW_shgroup_uniform_vec3(shgrp, "basecol", color, 1);
1705                 DRW_shgroup_uniform_float(shgrp, "metallic", &metallic_on, 1);
1706                 DRW_shgroup_uniform_float(shgrp, "specular", &specular_on, 1);
1707                 DRW_shgroup_uniform_float(shgrp, "roughness", &roughness_off, 1);
1708                 unit_m4(mat1);
1709                 mul_m4_m4m4(mat1, mat1, view_mat);
1710                 translate_m4(mat1, -1.5f, 0.0f, -5.0f);
1711                 DRW_shgroup_call_add(shgrp, sphere, mat1);
1712
1713                 shgrp = EEVEE_lookdev_shading_group_get(sldata, vedata, false, linfo->shadow_method);
1714                 DRW_shgroup_uniform_vec3(shgrp, "basecol", color, 1);
1715                 DRW_shgroup_uniform_float(shgrp, "metallic", &metallic_off, 1);
1716                 DRW_shgroup_uniform_float(shgrp, "specular", &specular_off, 1);
1717                 DRW_shgroup_uniform_float(shgrp, "roughness", &roughness_on, 1);
1718                 translate_m4(mat1, 3.0f, 0.0f, 0.0f);
1719                 DRW_shgroup_call_add(shgrp, sphere, mat1);
1720         }
1721         /* END */
1722
1723         BLI_ghash_free(stl->g_data->material_hash, NULL, MEM_freeN);
1724         BLI_ghash_free(stl->g_data->hair_material_hash, NULL, NULL);
1725 }
1726
1727 void EEVEE_materials_free(void)
1728 {
1729         for (int i = 0; i < VAR_MAT_MAX; ++i) {
1730                 DRW_SHADER_FREE_SAFE(e_data.default_lit[i]);
1731         }
1732         MEM_SAFE_FREE(e_data.frag_shader_lib);
1733         MEM_SAFE_FREE(e_data.vert_shader_str);
1734         MEM_SAFE_FREE(e_data.volume_shader_lib);
1735         DRW_SHADER_FREE_SAFE(e_data.default_hair_prepass_sh);
1736         DRW_SHADER_FREE_SAFE(e_data.default_hair_prepass_clip_sh);
1737         DRW_SHADER_FREE_SAFE(e_data.default_prepass_sh);
1738         DRW_SHADER_FREE_SAFE(e_data.default_prepass_clip_sh);
1739         DRW_SHADER_FREE_SAFE(e_data.default_background);
1740         DRW_SHADER_FREE_SAFE(e_data.default_studiolight_background);
1741         DRW_SHADER_FREE_SAFE(e_data.update_noise_sh);
1742         DRW_TEXTURE_FREE_SAFE(e_data.util_tex);
1743         DRW_TEXTURE_FREE_SAFE(e_data.noise_tex);
1744 }
1745
1746 void EEVEE_draw_default_passes(EEVEE_PassList *psl)
1747 {
1748         for (int i = 0; i < VAR_MAT_MAX; ++i) {
1749                 if (psl->default_pass[i]) {
1750                         DRW_draw_pass(psl->default_pass[i]);
1751                 }
1752         }
1753 }