0681780484d77d6d9e9b4ba89b33f0214de43248
[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 "DNA_world_types.h"
29 #include "DNA_modifier_types.h"
30
31 #include "BLI_dynstr.h"
32 #include "BLI_ghash.h"
33 #include "BLI_alloca.h"
34
35 #include "BKE_particle.h"
36
37 #include "GPU_material.h"
38
39 #include "eevee_engine.h"
40 #include "eevee_lut.h"
41 #include "eevee_private.h"
42
43 #if defined(IRRADIANCE_SH_L2)
44 #define SHADER_IRRADIANCE "#define IRRADIANCE_SH_L2\n"
45 #elif defined(IRRADIANCE_CUBEMAP)
46 #define SHADER_IRRADIANCE "#define IRRADIANCE_CUBEMAP\n"
47 #elif defined(IRRADIANCE_HL2)
48 #define SHADER_IRRADIANCE "#define IRRADIANCE_HL2\n"
49 #endif
50
51 #define SHADER_DEFINES \
52         "#define EEVEE_ENGINE\n" \
53         "#define MAX_PROBE " STRINGIFY(MAX_PROBE) "\n" \
54         "#define MAX_GRID " STRINGIFY(MAX_GRID) "\n" \
55         "#define MAX_PLANAR " STRINGIFY(MAX_PLANAR) "\n" \
56         "#define MAX_LIGHT " STRINGIFY(MAX_LIGHT) "\n" \
57         "#define MAX_SHADOW_CUBE " STRINGIFY(MAX_SHADOW_CUBE) "\n" \
58         "#define MAX_SHADOW_MAP " STRINGIFY(MAX_SHADOW_MAP) "\n" \
59         "#define MAX_SHADOW_CASCADE " STRINGIFY(MAX_SHADOW_CASCADE) "\n" \
60         "#define MAX_CASCADE_NUM " STRINGIFY(MAX_CASCADE_NUM) "\n" \
61         SHADER_IRRADIANCE
62
63 /* *********** STATIC *********** */
64 static struct {
65         char *frag_shader_lib;
66         char *volume_shader_lib;
67
68         struct GPUShader *default_prepass_sh;
69         struct GPUShader *default_prepass_clip_sh;
70         struct GPUShader *default_lit[VAR_MAT_MAX];
71
72         struct GPUShader *default_background;
73
74         /* 64*64 array texture containing all LUTs and other utilitarian arrays.
75          * Packing enables us to same precious textures slots. */
76         struct GPUTexture *util_tex;
77
78         float viewvecs[2][4];
79 } e_data = {NULL}; /* Engine data */
80
81 extern char datatoc_lamps_lib_glsl[];
82 extern char datatoc_lightprobe_lib_glsl[];
83 extern char datatoc_ambient_occlusion_lib_glsl[];
84 extern char datatoc_prepass_frag_glsl[];
85 extern char datatoc_prepass_vert_glsl[];
86 extern char datatoc_default_frag_glsl[];
87 extern char datatoc_default_world_frag_glsl[];
88 extern char datatoc_ltc_lib_glsl[];
89 extern char datatoc_bsdf_lut_frag_glsl[];
90 extern char datatoc_bsdf_common_lib_glsl[];
91 extern char datatoc_bsdf_direct_lib_glsl[];
92 extern char datatoc_bsdf_sampling_lib_glsl[];
93 extern char datatoc_irradiance_lib_glsl[];
94 extern char datatoc_octahedron_lib_glsl[];
95 extern char datatoc_lit_surface_frag_glsl[];
96 extern char datatoc_lit_surface_vert_glsl[];
97 extern char datatoc_shadow_frag_glsl[];
98 extern char datatoc_shadow_geom_glsl[];
99 extern char datatoc_shadow_vert_glsl[];
100 extern char datatoc_lightprobe_geom_glsl[];
101 extern char datatoc_lightprobe_vert_glsl[];
102 extern char datatoc_background_vert_glsl[];
103 extern char datatoc_volumetric_frag_glsl[];
104
105 extern Material defmaterial;
106 extern GlobalsUboStorage ts;
107
108 /* *********** FUNCTIONS *********** */
109
110 #if 0 /* Used only to generate the LUT values */
111 static struct GPUTexture *create_ggx_lut_texture(int UNUSED(w), int UNUSED(h))
112 {
113         struct GPUTexture *tex;
114         struct GPUFrameBuffer *fb = NULL;
115         static float samples_ct = 8192.0f;
116         static float inv_samples_ct = 1.0f / 8192.0f;
117
118         char *lib_str = NULL;
119
120         DynStr *ds_vert = BLI_dynstr_new();
121         BLI_dynstr_append(ds_vert, datatoc_bsdf_common_lib_glsl);
122         BLI_dynstr_append(ds_vert, datatoc_bsdf_sampling_lib_glsl);
123         lib_str = BLI_dynstr_get_cstring(ds_vert);
124         BLI_dynstr_free(ds_vert);
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, DRW_TEX_RG_16, DRW_TEX_FILTER, (float *)texels);
145
146         DRWFboTexture tex_filter = {&tex, DRW_TEX_RG_16, DRW_TEX_FILTER};
147         DRW_framebuffer_init(&fb, &draw_engine_eevee_type, w, h, &tex_filter, 1);
148
149         DRW_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 #endif
171
172 /* XXX TODO define all shared resources in a shared place without duplication */
173 struct GPUTexture *EEVEE_materials_get_util_tex(void)
174 {
175         return e_data.util_tex;
176 }
177
178 static char *eevee_get_defines(int options)
179 {
180         char *str = NULL;
181
182         DynStr *ds = BLI_dynstr_new();
183         BLI_dynstr_appendf(ds, SHADER_DEFINES);
184
185         if ((options & VAR_MAT_MESH) != 0) {
186                 BLI_dynstr_appendf(ds, "#define MESH_SHADER\n");
187         }
188         if ((options & VAR_MAT_HAIR) != 0) {
189                 BLI_dynstr_appendf(ds, "#define HAIR_SHADER\n");
190         }
191         if ((options & VAR_MAT_PROBE) != 0) {
192                 BLI_dynstr_appendf(ds, "#define PROBE_CAPTURE\n");
193         }
194         if ((options & VAR_MAT_AO) != 0) {
195                 BLI_dynstr_appendf(ds, "#define USE_AO\n");
196         }
197         if ((options & VAR_MAT_FLAT) != 0) {
198                 BLI_dynstr_appendf(ds, "#define USE_FLAT_NORMAL\n");
199         }
200         if ((options & VAR_MAT_BENT) != 0) {
201                 BLI_dynstr_appendf(ds, "#define USE_BENT_NORMAL\n");
202         }
203         if ((options & VAR_MAT_CLIP) != 0) {
204                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_CLIP\n");
205         }
206         if ((options & VAR_MAT_HASH) != 0) {
207                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_HASH\n");
208         }
209         if ((options & VAR_MAT_BLEND) != 0) {
210                 BLI_dynstr_appendf(ds, "#define USE_ALPHA_BLEND\n");
211         }
212         if ((options & VAR_MAT_MULT) != 0) {
213                 BLI_dynstr_appendf(ds, "#define USE_MULTIPLY\n");
214         }
215
216         str = BLI_dynstr_get_cstring(ds);
217         BLI_dynstr_free(ds);
218
219         return str;
220 }
221
222 static char *eevee_get_volume_defines(int options)
223 {
224         char *str = NULL;
225
226         BLI_assert(options < VAR_MAT_MAX);
227
228         DynStr *ds = BLI_dynstr_new();
229         BLI_dynstr_appendf(ds, SHADER_DEFINES);
230         BLI_dynstr_appendf(ds, "#define VOLUMETRICS\n");
231
232         if ((options & VAR_VOLUME_SHADOW) != 0) {
233                 BLI_dynstr_appendf(ds, "#define VOLUME_SHADOW\n");
234         }
235         if ((options & VAR_VOLUME_HOMO) != 0) {
236                 BLI_dynstr_appendf(ds, "#define VOLUME_HOMOGENEOUS\n");
237         }
238         if ((options & VAR_VOLUME_LIGHT) != 0) {
239                 BLI_dynstr_appendf(ds, "#define VOLUME_LIGHTING\n");
240         }
241         if ((options & VAR_VOLUME_COLOR) != 0) {
242                 BLI_dynstr_appendf(ds, "#define COLOR_TRANSMITTANCE\n");
243         }
244
245         str = BLI_dynstr_get_cstring(ds);
246         BLI_dynstr_free(ds);
247
248         return str;
249 }
250
251 static void add_standard_uniforms(DRWShadingGroup *shgrp, EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
252 {
253         DRW_shgroup_uniform_block(shgrp, "probe_block", sldata->probe_ubo);
254         DRW_shgroup_uniform_block(shgrp, "grid_block", sldata->grid_ubo);
255         DRW_shgroup_uniform_block(shgrp, "planar_block", sldata->planar_ubo);
256         DRW_shgroup_uniform_block(shgrp, "light_block", sldata->light_ubo);
257         DRW_shgroup_uniform_block(shgrp, "shadow_block", sldata->shadow_ubo);
258         DRW_shgroup_uniform_int(shgrp, "light_count", &sldata->lamps->num_light, 1);
259         DRW_shgroup_uniform_int(shgrp, "probe_count", &sldata->probes->num_render_cube, 1);
260         DRW_shgroup_uniform_int(shgrp, "grid_count", &sldata->probes->num_render_grid, 1);
261         DRW_shgroup_uniform_int(shgrp, "planar_count", &sldata->probes->num_planar, 1);
262         DRW_shgroup_uniform_bool(shgrp, "specToggle", &sldata->probes->specular_toggle, 1);
263         DRW_shgroup_uniform_float(shgrp, "lodCubeMax", &sldata->probes->lod_cube_max, 1);
264         DRW_shgroup_uniform_float(shgrp, "lodPlanarMax", &sldata->probes->lod_planar_max, 1);
265         DRW_shgroup_uniform_texture(shgrp, "utilTex", e_data.util_tex);
266         DRW_shgroup_uniform_buffer(shgrp, "probeCubes", &sldata->probe_pool);
267         DRW_shgroup_uniform_buffer(shgrp, "probePlanars", &vedata->txl->planar_pool);
268         DRW_shgroup_uniform_buffer(shgrp, "irradianceGrid", &sldata->irradiance_pool);
269         DRW_shgroup_uniform_buffer(shgrp, "shadowCubes", &sldata->shadow_depth_cube_pool);
270         DRW_shgroup_uniform_buffer(shgrp, "shadowCascades", &sldata->shadow_depth_cascade_pool);
271         if (vedata->stl->effects->use_ao) {
272                 DRW_shgroup_uniform_vec4(shgrp, "viewvecs[0]", (float *)&vedata->stl->g_data->viewvecs, 2);
273                 DRW_shgroup_uniform_buffer(shgrp, "minMaxDepthTex", &vedata->stl->g_data->minmaxz);
274                 DRW_shgroup_uniform_vec3(shgrp, "aoParameters", &vedata->stl->effects->ao_dist, 1);
275         }
276 }
277
278 static void create_default_shader(int options)
279 {
280         DynStr *ds_frag = BLI_dynstr_new();
281         BLI_dynstr_append(ds_frag, e_data.frag_shader_lib);
282         BLI_dynstr_append(ds_frag, datatoc_default_frag_glsl);
283         char *frag_str = BLI_dynstr_get_cstring(ds_frag);
284         BLI_dynstr_free(ds_frag);
285
286         char *defines = eevee_get_defines(options);
287
288         e_data.default_lit[options] = DRW_shader_create(datatoc_lit_surface_vert_glsl, NULL, frag_str, defines);
289
290         MEM_freeN(defines);
291         MEM_freeN(frag_str);
292 }
293
294 void EEVEE_materials_init(EEVEE_StorageList *stl)
295 {
296         if (!e_data.frag_shader_lib) {
297                 char *frag_str = NULL;
298
299                 /* Shaders */
300                 DynStr *ds_frag = BLI_dynstr_new();
301                 BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
302                 BLI_dynstr_append(ds_frag, datatoc_ambient_occlusion_lib_glsl);
303                 BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
304                 BLI_dynstr_append(ds_frag, datatoc_irradiance_lib_glsl);
305                 BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
306                 BLI_dynstr_append(ds_frag, datatoc_ltc_lib_glsl);
307                 BLI_dynstr_append(ds_frag, datatoc_bsdf_direct_lib_glsl);
308                 BLI_dynstr_append(ds_frag, datatoc_lamps_lib_glsl);
309                 BLI_dynstr_append(ds_frag, datatoc_lit_surface_frag_glsl);
310                 e_data.frag_shader_lib = BLI_dynstr_get_cstring(ds_frag);
311                 BLI_dynstr_free(ds_frag);
312
313                 ds_frag = BLI_dynstr_new();
314                 BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
315                 BLI_dynstr_append(ds_frag, datatoc_ambient_occlusion_lib_glsl);
316                 BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
317                 BLI_dynstr_append(ds_frag, datatoc_irradiance_lib_glsl);
318                 BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
319                 BLI_dynstr_append(ds_frag, datatoc_ltc_lib_glsl);
320                 BLI_dynstr_append(ds_frag, datatoc_bsdf_direct_lib_glsl);
321                 BLI_dynstr_append(ds_frag, datatoc_lamps_lib_glsl);
322                 BLI_dynstr_append(ds_frag, datatoc_volumetric_frag_glsl);
323                 e_data.volume_shader_lib = BLI_dynstr_get_cstring(ds_frag);
324                 BLI_dynstr_free(ds_frag);
325
326                 ds_frag = BLI_dynstr_new();
327                 BLI_dynstr_append(ds_frag, e_data.frag_shader_lib);
328                 BLI_dynstr_append(ds_frag, datatoc_default_frag_glsl);
329                 frag_str = BLI_dynstr_get_cstring(ds_frag);
330                 BLI_dynstr_free(ds_frag);
331
332                 e_data.default_background = DRW_shader_create_fullscreen(
333                         datatoc_default_world_frag_glsl, NULL);
334
335                 e_data.default_prepass_sh = DRW_shader_create(
336                         datatoc_prepass_vert_glsl, NULL, datatoc_prepass_frag_glsl,
337                         NULL);
338
339                 e_data.default_prepass_clip_sh = DRW_shader_create(
340                         datatoc_prepass_vert_glsl, NULL, datatoc_prepass_frag_glsl,
341                         "#define CLIP_PLANES\n");
342
343                 MEM_freeN(frag_str);
344
345                 /* Textures */
346                 const int layers = 3;
347                 float (*texels)[4] = MEM_mallocN(sizeof(float[4]) * 64 * 64 * layers, "utils texels");
348                 float (*texels_layer)[4] = texels;
349
350                 /* Copy ltc_mat_ggx into 1st layer */
351                 memcpy(texels_layer, ltc_mat_ggx, sizeof(float[4]) * 64 * 64);
352                 texels_layer += 64 * 64;
353
354                 /* Copy bsdf_split_sum_ggx into 2nd layer red and green channels.
355                    Copy ltc_mag_ggx into 2nd layer blue channel. */
356                 for (int i = 0; i < 64 * 64; i++) {
357                         texels_layer[i][0] = bsdf_split_sum_ggx[i*2 + 0];
358                         texels_layer[i][1] = bsdf_split_sum_ggx[i*2 + 1];
359                         texels_layer[i][2] = ltc_mag_ggx[i];
360                 }
361                 texels_layer += 64 * 64;
362
363                 for (int i = 0; i < 64 * 64; i++) {
364                         texels_layer[i][0] = blue_noise[i][0];
365                         texels_layer[i][1] = blue_noise[i][1] * 0.5 + 0.5;
366                         texels_layer[i][2] = cosf(blue_noise[i][1] * 2.0 * M_PI);
367                         texels_layer[i][3] = sinf(blue_noise[i][1] * 2.0 * M_PI);
368                 }
369
370                 e_data.util_tex = DRW_texture_create_2D_array(64, 64, layers, DRW_TEX_RGBA_16, DRW_TEX_FILTER | DRW_TEX_WRAP, (float *)texels);
371                 MEM_freeN(texels);
372         }
373
374         {
375                 /* Update viewvecs */
376                 const bool is_persp = DRW_viewport_is_persp_get();
377                 float invproj[4][4], winmat[4][4];
378                 /* view vectors for the corners of the view frustum.
379                  * Can be used to recreate the world space position easily */
380                 float viewvecs[3][4] = {
381                     {-1.0f, -1.0f, -1.0f, 1.0f},
382                     {1.0f, -1.0f, -1.0f, 1.0f},
383                     {-1.0f, 1.0f, -1.0f, 1.0f}
384                 };
385
386                 /* invert the view matrix */
387                 DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
388                 invert_m4_m4(invproj, winmat);
389
390                 /* convert the view vectors to view space */
391                 for (int i = 0; i < 3; i++) {
392                         mul_m4_v4(invproj, viewvecs[i]);
393                         /* normalized trick see:
394                          * http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
395                         mul_v3_fl(viewvecs[i], 1.0f / viewvecs[i][3]);
396                         if (is_persp)
397                                 mul_v3_fl(viewvecs[i], 1.0f / viewvecs[i][2]);
398                         viewvecs[i][3] = 1.0;
399                 }
400
401                 copy_v4_v4(stl->g_data->viewvecs[0], viewvecs[0]);
402                 copy_v4_v4(stl->g_data->viewvecs[1], viewvecs[1]);
403
404                 /* we need to store the differences */
405                 stl->g_data->viewvecs[1][0] -= viewvecs[0][0];
406                 stl->g_data->viewvecs[1][1] = viewvecs[2][1] - viewvecs[0][1];
407
408                 /* calculate a depth offset as well */
409                 if (!is_persp) {
410                         float vec_far[] = {-1.0f, -1.0f, 1.0f, 1.0f};
411                         mul_m4_v4(invproj, vec_far);
412                         mul_v3_fl(vec_far, 1.0f / vec_far[3]);
413                         stl->g_data->viewvecs[1][2] = vec_far[2] - viewvecs[0][2];
414                 }
415         }
416 }
417
418 struct GPUMaterial *EEVEE_material_world_lightprobe_get(struct Scene *scene, World *wo)
419 {
420         const void *engine = &DRW_engine_viewport_eevee_type;
421         const int options = VAR_WORLD_PROBE;
422
423         GPUMaterial *mat = GPU_material_from_nodetree_find(&wo->gpumaterial, engine, options);
424         if (mat != NULL) {
425                 return mat;
426         }
427         return GPU_material_from_nodetree(
428                 scene, wo->nodetree, &wo->gpumaterial, engine, options,
429                 datatoc_background_vert_glsl, NULL, e_data.frag_shader_lib,
430                 SHADER_DEFINES "#define PROBE_CAPTURE\n");
431 }
432
433 struct GPUMaterial *EEVEE_material_world_background_get(struct Scene *scene, World *wo)
434 {
435         const void *engine = &DRW_engine_viewport_eevee_type;
436         int options = VAR_WORLD_BACKGROUND;
437
438         GPUMaterial *mat = GPU_material_from_nodetree_find(&wo->gpumaterial, engine, options);
439         if (mat != NULL) {
440                 return mat;
441         }
442         return GPU_material_from_nodetree(
443                 scene, wo->nodetree, &wo->gpumaterial, engine, options,
444                 datatoc_background_vert_glsl, NULL, e_data.frag_shader_lib,
445                 SHADER_DEFINES "#define WORLD_BACKGROUND\n");
446 }
447
448 struct GPUMaterial *EEVEE_material_world_volume_get(
449         struct Scene *scene, World *wo,
450         bool use_lights, bool use_volume_shadows, bool is_homogeneous, bool use_color_transmit)
451 {
452         const void *engine = &DRW_engine_viewport_eevee_type;
453         int options = VAR_WORLD_VOLUME;
454
455         if (use_lights) options |= VAR_VOLUME_LIGHT;
456         if (is_homogeneous) options |= VAR_VOLUME_HOMO;
457         if (use_volume_shadows) options |= VAR_VOLUME_SHADOW;
458         if (use_color_transmit) options |= VAR_VOLUME_COLOR;
459
460         GPUMaterial *mat = GPU_material_from_nodetree_find(&wo->gpumaterial, engine, options);
461         if (mat != NULL) {
462                 return mat;
463         }
464
465         char *defines = eevee_get_volume_defines(options);
466
467         mat = GPU_material_from_nodetree(
468                 scene, wo->nodetree, &wo->gpumaterial, engine, options,
469                 datatoc_background_vert_glsl, NULL, e_data.volume_shader_lib,
470                 defines);
471
472         MEM_freeN(defines);
473
474         return mat;
475 }
476
477 struct GPUMaterial *EEVEE_material_mesh_get(
478         struct Scene *scene, Material *ma,
479         bool use_ao, bool use_bent_normals, bool use_blend, bool use_multiply)
480 {
481         const void *engine = &DRW_engine_viewport_eevee_type;
482         int options = VAR_MAT_MESH;
483
484         if (use_ao) options |= VAR_MAT_AO;
485         if (use_bent_normals) options |= VAR_MAT_BENT;
486         if (use_blend) options |= VAR_MAT_BLEND;
487         if (use_multiply) options |= VAR_MAT_MULT;
488
489         GPUMaterial *mat = GPU_material_from_nodetree_find(&ma->gpumaterial, engine, options);
490         if (mat) {
491                 return mat;
492         }
493
494         char *defines = eevee_get_defines(options);
495
496         mat = GPU_material_from_nodetree(
497                 scene, ma->nodetree, &ma->gpumaterial, engine, options,
498                 datatoc_lit_surface_vert_glsl, NULL, e_data.frag_shader_lib,
499                 defines);
500
501         MEM_freeN(defines);
502
503         return mat;
504 }
505
506 struct GPUMaterial *EEVEE_material_mesh_depth_get(struct Scene *scene, Material *ma, bool use_hashed_alpha)
507 {
508         const void *engine = &DRW_engine_viewport_eevee_type;
509         int options = VAR_MAT_MESH;
510
511         if (use_hashed_alpha) {
512                 options |= VAR_MAT_HASH;
513         }
514         else {
515                 options |= VAR_MAT_CLIP;
516         }
517
518         GPUMaterial *mat = GPU_material_from_nodetree_find(&ma->gpumaterial, engine, options);
519         if (mat) {
520                 return mat;
521         }
522
523         char *defines = eevee_get_defines(options);
524
525         DynStr *ds_frag = BLI_dynstr_new();
526         BLI_dynstr_append(ds_frag, e_data.frag_shader_lib);
527         BLI_dynstr_append(ds_frag, datatoc_prepass_frag_glsl);
528         char *frag_str = BLI_dynstr_get_cstring(ds_frag);
529         BLI_dynstr_free(ds_frag);
530
531         mat = GPU_material_from_nodetree(
532                 scene, ma->nodetree, &ma->gpumaterial, engine, options,
533                 datatoc_lit_surface_vert_glsl, NULL, frag_str,
534                 defines);
535
536         MEM_freeN(frag_str);
537         MEM_freeN(defines);
538
539         return mat;
540 }
541
542 struct GPUMaterial *EEVEE_material_hair_get(
543         struct Scene *scene, Material *ma,
544         bool use_ao, bool use_bent_normals)
545 {
546         const void *engine = &DRW_engine_viewport_eevee_type;
547         int options = VAR_MAT_MESH | VAR_MAT_HAIR;
548
549         if (use_ao) options |= VAR_MAT_AO;
550         if (use_bent_normals) options |= VAR_MAT_BENT;
551
552         GPUMaterial *mat = GPU_material_from_nodetree_find(&ma->gpumaterial, engine, options);
553         if (mat) {
554                 return mat;
555         }
556
557         char *defines = eevee_get_defines(options);
558
559         mat = GPU_material_from_nodetree(
560                 scene, ma->nodetree, &ma->gpumaterial, engine, options,
561                 datatoc_lit_surface_vert_glsl, NULL, e_data.frag_shader_lib,
562                 defines);
563
564         MEM_freeN(defines);
565
566         return mat;
567 }
568
569 /**
570  * Create a default shading group inside the given pass.
571  **/
572 static struct DRWShadingGroup *EEVEE_default_shading_group_create(
573         EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata, DRWPass *pass,
574         bool is_hair, bool is_flat_normal, bool use_ao, bool use_bent_normals, bool use_blend)
575 {
576         int options = VAR_MAT_MESH;
577
578         if (is_hair) options |= VAR_MAT_HAIR;
579         if (use_ao) options |= VAR_MAT_AO;
580         if (use_bent_normals) options |= VAR_MAT_BENT;
581         if (is_flat_normal) options |= VAR_MAT_FLAT;
582         if (use_blend) options |= VAR_MAT_BLEND;
583
584         if (e_data.default_lit[options] == NULL) {
585                 create_default_shader(options);
586         }
587
588         DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.default_lit[options], pass);
589         add_standard_uniforms(shgrp, sldata, vedata);
590
591         return shgrp;
592 }
593
594 /**
595  * Create a default shading group inside the default pass without standard uniforms.
596  **/
597 static struct DRWShadingGroup *EEVEE_default_shading_group_get(
598         EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata,
599         bool is_hair, bool is_flat_normal, bool use_ao, bool use_bent_normals)
600 {
601         int options = VAR_MAT_MESH;
602
603         if (is_hair) options |= VAR_MAT_HAIR;
604         if (use_ao) options |= VAR_MAT_AO;
605         if (use_bent_normals) options |= VAR_MAT_BENT;
606         if (is_flat_normal) options |= VAR_MAT_FLAT;
607
608         if (e_data.default_lit[options] == NULL) {
609                 create_default_shader(options);
610         }
611
612         if (vedata->psl->default_pass[options] == NULL) {
613                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
614                 vedata->psl->default_pass[options] = DRW_pass_create("Default Lit Pass", state);
615
616                 DRWShadingGroup *shgrp = DRW_shgroup_create(e_data.default_lit[options], vedata->psl->default_pass[options]);
617                 add_standard_uniforms(shgrp, sldata, vedata);
618         }
619
620         return DRW_shgroup_create(e_data.default_lit[options], vedata->psl->default_pass[options]);
621 }
622
623 void EEVEE_materials_cache_init(EEVEE_Data *vedata)
624 {
625         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
626         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
627
628         {
629                 /* Global AO Switch*/
630                 const DRWContextState *draw_ctx = DRW_context_state_get();
631                 SceneLayer *scene_layer = draw_ctx->sl;
632                 IDProperty *props = BKE_scene_layer_engine_evaluated_get(scene_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE);
633                 stl->effects->use_ao = BKE_collection_engine_property_value_get_bool(props, "gtao_enable");
634                 stl->effects->use_bent_normals = BKE_collection_engine_property_value_get_bool(props, "gtao_use_bent_normals");
635         }
636
637         /* Create Material Ghash */
638         {
639                 stl->g_data->material_hash = BLI_ghash_ptr_new("Eevee_material ghash");
640                 stl->g_data->hair_material_hash = BLI_ghash_ptr_new("Eevee_hair_material ghash");
641         }
642
643         {
644                 psl->background_pass = DRW_pass_create("Background Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR);
645
646                 struct Gwn_Batch *geom = DRW_cache_fullscreen_quad_get();
647                 DRWShadingGroup *grp = NULL;
648
649                 const DRWContextState *draw_ctx = DRW_context_state_get();
650                 Scene *scene = draw_ctx->scene;
651                 World *wo = scene->world;
652
653                 float *col = ts.colorBackground;
654
655                 if (wo) {
656                         col = &wo->horr;
657
658                         if (wo->use_nodes && wo->nodetree) {
659                                 struct GPUMaterial *gpumat = EEVEE_material_world_background_get(scene, wo);
660                                 grp = DRW_shgroup_material_create(gpumat, psl->background_pass);
661
662                                 if (grp) {
663                                         DRW_shgroup_uniform_float(grp, "backgroundAlpha", &stl->g_data->background_alpha, 1);
664                                         DRW_shgroup_call_add(grp, geom, NULL);
665                                 }
666                                 else {
667                                         /* Shader failed : pink background */
668                                         static float pink[3] = {1.0f, 0.0f, 1.0f};
669                                         col = pink;
670                                 }
671                         }
672                 }
673
674                 /* Fallback if shader fails or if not using nodetree. */
675                 if (grp == NULL) {
676                         grp = DRW_shgroup_create(e_data.default_background, psl->background_pass);
677                         DRW_shgroup_uniform_vec3(grp, "color", col, 1);
678                         DRW_shgroup_uniform_float(grp, "backgroundAlpha", &stl->g_data->background_alpha, 1);
679                         DRW_shgroup_call_add(grp, geom, NULL);
680                 }
681         }
682
683         {
684                 DRWState state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_WIRE;
685                 psl->depth_pass = DRW_pass_create("Depth Pass", state);
686                 stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.default_prepass_sh, psl->depth_pass);
687
688                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CULL_BACK;
689                 psl->depth_pass_cull = DRW_pass_create("Depth Pass Cull", state);
690                 stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.default_prepass_sh, psl->depth_pass_cull);
691
692                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
693                 psl->depth_pass_clip = DRW_pass_create("Depth Pass Clip", state);
694                 stl->g_data->depth_shgrp_clip = DRW_shgroup_create(e_data.default_prepass_clip_sh, psl->depth_pass_clip);
695
696                 state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CLIP_PLANES | DRW_STATE_CULL_BACK;
697                 psl->depth_pass_clip_cull = DRW_pass_create("Depth Pass Cull Clip", state);
698                 stl->g_data->depth_shgrp_clip_cull = DRW_shgroup_create(e_data.default_prepass_clip_sh, psl->depth_pass_clip_cull);
699         }
700
701         {
702                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
703                 psl->material_pass = DRW_pass_create("Material Shader Pass", state);
704         }
705
706         {
707                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_CLIP_PLANES | DRW_STATE_WIRE;
708                 psl->transparent_pass = DRW_pass_create("Material Transparent Pass", state);
709         }
710 }
711
712 #define ADD_SHGROUP_CALL(shgrp, ob, geom) do { \
713         if (is_sculpt_mode) { \
714                 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat); \
715         } \
716         else { \
717                 DRW_shgroup_call_object_add(shgrp, geom, ob); \
718         } \
719 } while (0)
720
721 #define ADD_SHGROUP_CALL_SAFE(shgrp, ob, geom) do { \
722         if (shgrp) { \
723                 ADD_SHGROUP_CALL(shgrp, ob, geom); \
724         } \
725 } while (0)
726
727 typedef struct EeveeMaterialShadingGroups{
728         struct DRWShadingGroup *shading_grp;
729         struct DRWShadingGroup *depth_grp;
730         struct DRWShadingGroup *depth_clip_grp;
731 } EeveeMaterialShadingGroups;
732
733 static void material_opaque(
734         Material *ma, GHash *material_hash, EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata,
735         bool do_cull, bool use_flat_nor, struct GPUMaterial **gpumat, struct GPUMaterial **gpumat_depth,
736         struct DRWShadingGroup **shgrp, struct DRWShadingGroup **shgrp_depth, struct DRWShadingGroup **shgrp_depth_clip)
737 {
738         const DRWContextState *draw_ctx = DRW_context_state_get();
739         Scene *scene = draw_ctx->scene;
740         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
741         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
742
743         float *color_p = &ma->r;
744         float *metal_p = &ma->ray_mirror;
745         float *spec_p = &ma->spec;
746         float *rough_p = &ma->gloss_mir;
747
748         const bool use_gpumat = (ma->use_nodes && ma->nodetree);
749
750         EeveeMaterialShadingGroups *emsg = BLI_ghash_lookup(material_hash, (const void *)ma);
751
752         if (emsg) {
753                 *shgrp = emsg->shading_grp;
754                 *shgrp_depth = emsg->depth_grp;
755                 *shgrp_depth_clip = emsg->depth_clip_grp;
756
757                 /* This will have been created already, just perform a lookup. */
758                 *gpumat = (use_gpumat) ? EEVEE_material_mesh_get(
759                         scene, ma, stl->effects->use_ao, stl->effects->use_bent_normals, false, false) : NULL;
760                 *gpumat_depth = (use_gpumat) ? EEVEE_material_mesh_depth_get(
761                         scene, ma, (ma->blend_method == MA_BM_HASHED)) : NULL;
762                 return;
763         }
764
765         if (use_gpumat) {
766                 /* Shading */
767                 *gpumat = EEVEE_material_mesh_get(scene, ma,
768                         stl->effects->use_ao, stl->effects->use_bent_normals, false, false);
769
770                 *shgrp = DRW_shgroup_material_create(*gpumat, psl->material_pass);
771                 if (*shgrp) {
772                         add_standard_uniforms(*shgrp, sldata, vedata);
773                 }
774                 else {
775                         /* Shader failed : pink color */
776                         static float col[3] = {1.0f, 0.0f, 1.0f};
777                         static float half = 0.5f;
778
779                         color_p = col;
780                         metal_p = spec_p = rough_p = &half;
781                 }
782
783                 /* Alpha CLipped : Discard pixel from depth pass, then
784                  * fail the depth test for shading. */
785                 if (ELEM(ma->blend_method, MA_BM_CLIP, MA_BM_HASHED)) {
786                         *gpumat_depth = EEVEE_material_mesh_depth_get(scene, ma,
787                                 (ma->blend_method == MA_BM_HASHED));
788
789                         *shgrp_depth = DRW_shgroup_material_create(*gpumat_depth, do_cull ? psl->depth_pass_cull : psl->depth_pass);
790                         *shgrp_depth_clip = DRW_shgroup_material_create(*gpumat_depth, do_cull ? psl->depth_pass_clip_cull : psl->depth_pass_clip);
791
792                         if (shgrp_depth) {
793                                 if (ma->blend_method == MA_BM_CLIP) {
794                                         DRW_shgroup_uniform_float(*shgrp_depth, "alphaThreshold", &ma->alpha_threshold, 1);
795                                         DRW_shgroup_uniform_float(*shgrp_depth_clip, "alphaThreshold", &ma->alpha_threshold, 1);
796                                 }
797                         }
798                 }
799         }
800
801         /* Fallback to default shader */
802         if (*shgrp == NULL) {
803                 *shgrp = EEVEE_default_shading_group_get(sldata, vedata, false, use_flat_nor,
804                         stl->effects->use_ao, stl->effects->use_bent_normals);
805                 DRW_shgroup_uniform_vec3(*shgrp, "basecol", color_p, 1);
806                 DRW_shgroup_uniform_float(*shgrp, "metallic", metal_p, 1);
807                 DRW_shgroup_uniform_float(*shgrp, "specular", spec_p, 1);
808                 DRW_shgroup_uniform_float(*shgrp, "roughness", rough_p, 1);
809         }
810
811         /* Fallback default depth prepass */
812         if (*shgrp_depth == NULL) {
813                 *shgrp_depth = do_cull ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp;
814                 *shgrp_depth_clip = do_cull ? stl->g_data->depth_shgrp_clip_cull : stl->g_data->depth_shgrp_clip;
815         }
816
817         emsg = MEM_mallocN(sizeof("EeveeMaterialShadingGroups"), "EeveeMaterialShadingGroups");
818         emsg->shading_grp = *shgrp;
819         emsg->depth_grp = *shgrp_depth;
820         emsg->depth_clip_grp = *shgrp_depth_clip;
821         BLI_ghash_insert(material_hash, ma, emsg);
822 }
823
824 static void material_transparent(
825         Material *ma, EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata,
826         bool do_cull, bool use_flat_nor, struct GPUMaterial **gpumat, struct DRWShadingGroup **shgrp)
827 {
828         const DRWContextState *draw_ctx = DRW_context_state_get();
829         Scene *scene = draw_ctx->scene;
830         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
831         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
832
833         float *color_p = &ma->r;
834         float *metal_p = &ma->ray_mirror;
835         float *spec_p = &ma->spec;
836         float *rough_p = &ma->gloss_mir;
837
838         if (ma->use_nodes && ma->nodetree) {
839                 /* Shading */
840                 *gpumat = EEVEE_material_mesh_get(scene, ma,
841                         stl->effects->use_ao, stl->effects->use_bent_normals,
842                         true, (ma->blend_method == MA_BM_MULTIPLY));
843
844                 *shgrp = DRW_shgroup_material_create(*gpumat, psl->transparent_pass);
845                 if (*shgrp) {
846                         add_standard_uniforms(*shgrp, sldata, vedata);
847                 }
848                 else {
849                         /* Shader failed : pink color */
850                         static float col[3] = {1.0f, 0.0f, 1.0f};
851                         static float half = 0.5f;
852
853                         color_p = col;
854                         metal_p = spec_p = rough_p = &half;
855                 }
856         }
857
858         /* Fallback to default shader */
859         if (*shgrp == NULL) {
860                 *shgrp = EEVEE_default_shading_group_create(
861                         sldata, vedata, psl->transparent_pass,
862                         false, use_flat_nor, stl->effects->use_ao, stl->effects->use_bent_normals, true);
863                 DRW_shgroup_uniform_vec3(*shgrp, "basecol", color_p, 1);
864                 DRW_shgroup_uniform_float(*shgrp, "metallic", metal_p, 1);
865                 DRW_shgroup_uniform_float(*shgrp, "specular", spec_p, 1);
866                 DRW_shgroup_uniform_float(*shgrp, "roughness", rough_p, 1);
867         }
868
869         DRWState cur_state = (do_cull) ? DRW_STATE_CULL_BACK : 0;
870         DRWState all_state = DRW_STATE_CULL_BACK | DRW_STATE_BLEND | DRW_STATE_ADDITIVE | DRW_STATE_MULTIPLY;
871
872         switch (ma->blend_method) {
873                 case MA_BM_ADD:
874                         cur_state |= DRW_STATE_ADDITIVE;
875                         break;
876                 case MA_BM_MULTIPLY:
877                         cur_state |= DRW_STATE_MULTIPLY;
878                         break;
879                 case MA_BM_BLEND:
880                         cur_state |= DRW_STATE_BLEND;
881                         break;
882                 default:
883                         BLI_assert(0);
884                         break;
885         }
886
887         /* Disable other blend modes and use the one we want. */
888         DRW_shgroup_state_disable(*shgrp, all_state);
889         DRW_shgroup_state_enable(*shgrp, cur_state);
890 }
891
892 void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sldata, Object *ob)
893 {
894         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
895         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
896         const DRWContextState *draw_ctx = DRW_context_state_get();
897         GHash *material_hash = stl->g_data->material_hash;
898
899         IDProperty *ces_mode_ob = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_OBJECT, "");
900         const bool do_cull = BKE_collection_engine_property_value_get_bool(ces_mode_ob, "show_backface_culling");
901         const bool is_active = (ob == draw_ctx->obact);
902         const bool is_sculpt_mode = is_active && (ob->mode & OB_MODE_SCULPT) != 0;
903         const bool is_default_mode_shader = is_sculpt_mode;
904
905         /* First get materials for this mesh. */
906         if (ELEM(ob->type, OB_MESH)) {
907                 const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
908
909                 struct DRWShadingGroup **shgrp_array = BLI_array_alloca(shgrp_array, materials_len);
910                 struct DRWShadingGroup **shgrp_depth_array = BLI_array_alloca(shgrp_depth_array, materials_len);
911                 struct DRWShadingGroup **shgrp_depth_clip_array = BLI_array_alloca(shgrp_depth_clip_array, materials_len);
912
913                 struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
914                 struct GPUMaterial **gpumat_depth_array = BLI_array_alloca(gpumat_array, materials_len);
915
916                 bool use_flat_nor = false;
917
918                 if (is_default_mode_shader) {
919                         if (is_sculpt_mode) {
920                                 use_flat_nor = DRW_object_is_flat_normal(ob);
921                         }
922                 }
923
924                 for (int i = 0; i < materials_len; ++i) {
925                         Material *ma = give_current_material(ob, i + 1);
926
927                         gpumat_array[i] = NULL;
928                         gpumat_depth_array[i] = NULL;
929                         shgrp_array[i] = NULL;
930                         shgrp_depth_array[i] = NULL;
931                         shgrp_depth_clip_array[i] = NULL;
932
933                         if (ma == NULL)
934                                 ma = &defmaterial;
935
936                         switch (ma->blend_method) {
937                                 case MA_BM_SOLID:
938                                 case MA_BM_CLIP:
939                                 case MA_BM_HASHED:
940                                         material_opaque(ma, material_hash, sldata, vedata, do_cull, use_flat_nor,
941                                                 &gpumat_array[i], &gpumat_depth_array[i],
942                                                 &shgrp_array[i], &shgrp_depth_array[i], &shgrp_depth_clip_array[i]);
943                                         break;
944                                 case MA_BM_ADD:
945                                 case MA_BM_MULTIPLY:
946                                 case MA_BM_BLEND:
947                                         material_transparent(ma, sldata, vedata, do_cull, use_flat_nor,
948                                                 &gpumat_array[i], &shgrp_array[i]);
949                                         break;
950                                 default:
951                                         BLI_assert(0);
952                                         break;
953                         }
954                 }
955
956                 /* Get per-material split surface */
957                 struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(ob, gpumat_array, materials_len);
958                 if (mat_geom) {
959                         for (int i = 0; i < materials_len; ++i) {
960                                 /* Shading pass */
961                                 ADD_SHGROUP_CALL(shgrp_array[i], ob, mat_geom[i]);
962
963                                 /* Depth Prepass */
964                                 ADD_SHGROUP_CALL_SAFE(shgrp_depth_array[i], ob, mat_geom[i]);
965                                 ADD_SHGROUP_CALL_SAFE(shgrp_depth_clip_array[i], ob, mat_geom[i]);
966
967                                 /* Shadow Pass */
968                                 EEVEE_lights_cache_shcaster_add(sldata, psl, mat_geom[i], ob->obmat);
969                         }
970                 }
971         }
972
973         if (ob->type == OB_MESH) {
974                 if (ob != draw_ctx->scene->obedit) {
975                         material_hash = stl->g_data->hair_material_hash;
976
977                         for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
978                                 if (md->type == eModifierType_ParticleSystem) {
979                                         ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
980
981                                         if (psys_check_enabled(ob, psys, false)) {
982                                                 ParticleSettings *part = psys->part;
983                                                 int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
984
985                                                 if (draw_as == PART_DRAW_PATH && (psys->pathcache || psys->childcache)) {
986                                                         struct Gwn_Batch *hair_geom = DRW_cache_particles_get_hair(psys, md);
987                                                         DRWShadingGroup *shgrp = NULL;
988                                                         Material *ma = give_current_material(ob, part->omat);
989                                                         static float mat[4][4];
990
991                                                         unit_m4(mat);
992
993                                                         if (ma == NULL) {
994                                                                 ma = &defmaterial;
995                                                         }
996
997                                                         float *color_p = &ma->r;
998                                                         float *metal_p = &ma->ray_mirror;
999                                                         float *spec_p = &ma->spec;
1000                                                         float *rough_p = &ma->gloss_mir;
1001
1002                                                         DRW_shgroup_call_add(stl->g_data->depth_shgrp, hair_geom, mat);
1003                                                         DRW_shgroup_call_add(stl->g_data->depth_shgrp_clip, hair_geom, mat);
1004
1005                                                         shgrp = BLI_ghash_lookup(material_hash, (const void *)ma);
1006
1007                                                         if (shgrp) {
1008                                                                 DRW_shgroup_call_add(shgrp, hair_geom, mat);
1009                                                         }
1010                                                         else {
1011                                                                 if (ma->use_nodes && ma->nodetree) {
1012                                                                         Scene *scene = draw_ctx->scene;
1013                                                                         struct GPUMaterial *gpumat = EEVEE_material_hair_get(scene, ma,
1014                                                                                 stl->effects->use_ao, stl->effects->use_bent_normals);
1015
1016                                                                         shgrp = DRW_shgroup_material_create(gpumat, psl->material_pass);
1017                                                                         if (shgrp) {
1018                                                                                 add_standard_uniforms(shgrp, sldata, vedata);
1019
1020                                                                                 BLI_ghash_insert(material_hash, ma, shgrp);
1021
1022                                                                                 DRW_shgroup_call_add(shgrp, hair_geom, mat);
1023                                                                         }
1024                                                                         else {
1025                                                                                 /* Shader failed : pink color */
1026                                                                                 static float col[3] = {1.0f, 0.0f, 1.0f};
1027                                                                                 static float half = 0.5f;
1028
1029                                                                                 color_p = col;
1030                                                                                 metal_p = spec_p = rough_p = &half;
1031                                                                         }
1032                                                                 }
1033
1034                                                                 /* Fallback to default shader */
1035                                                                 if (shgrp == NULL) {
1036                                                                         shgrp = EEVEE_default_shading_group_get(sldata, vedata, true, false,
1037                                                                                 stl->effects->use_ao, stl->effects->use_bent_normals);
1038                                                                         DRW_shgroup_uniform_vec3(shgrp, "basecol", color_p, 1);
1039                                                                         DRW_shgroup_uniform_float(shgrp, "metallic", metal_p, 1);
1040                                                                         DRW_shgroup_uniform_float(shgrp, "specular", spec_p, 1);
1041                                                                         DRW_shgroup_uniform_float(shgrp, "roughness", rough_p, 1);
1042
1043                                                                         BLI_ghash_insert(material_hash, ma, shgrp);
1044
1045                                                                         DRW_shgroup_call_add(shgrp, hair_geom, mat);
1046                                                                 }
1047                                                         }
1048                                                 }
1049                                         }
1050                                 }
1051                         }
1052                 }
1053         }
1054 }
1055
1056 void EEVEE_materials_cache_finish(EEVEE_Data *vedata)
1057 {
1058         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
1059
1060         BLI_ghash_free(stl->g_data->material_hash, NULL, MEM_freeN);
1061         BLI_ghash_free(stl->g_data->hair_material_hash, NULL, NULL);
1062 }
1063
1064 void EEVEE_materials_free(void)
1065 {
1066         for (int i = 0; i < VAR_MAT_MAX; ++i) {
1067                 DRW_SHADER_FREE_SAFE(e_data.default_lit[i]);
1068         }
1069         MEM_SAFE_FREE(e_data.frag_shader_lib);
1070         MEM_SAFE_FREE(e_data.volume_shader_lib);
1071         DRW_SHADER_FREE_SAFE(e_data.default_prepass_sh);
1072         DRW_SHADER_FREE_SAFE(e_data.default_prepass_clip_sh);
1073         DRW_SHADER_FREE_SAFE(e_data.default_background);
1074         DRW_TEXTURE_FREE_SAFE(e_data.util_tex);
1075 }
1076
1077 void EEVEE_draw_default_passes(EEVEE_PassList *psl)
1078 {
1079         for (int i = 0; i < VAR_MAT_MAX; ++i) {
1080                 if (psl->default_pass[i]) {
1081                         DRW_draw_pass(psl->default_pass[i]);
1082                 }
1083         }
1084 }