Eevee: Refactor of shading code to be more modular.

This will enable creating shading models more easily.
This commit is contained in:
2017-06-28 16:31:25 +02:00
parent 0782c9f8dc
commit 1982e724f4
13 changed files with 620 additions and 577 deletions

View File

@@ -120,6 +120,8 @@ data_to_c_simple(engines/eevee/shaders/default_world_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/background_vert.glsl SRC)
data_to_c_simple(engines/eevee/shaders/ambient_occlusion_lib.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_minmaxz_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/lamps_lib.glsl SRC)
data_to_c_simple(engines/eevee/shaders/lightprobe_lib.glsl SRC)
data_to_c_simple(engines/eevee/shaders/lightprobe_filter_glossy_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/lightprobe_filter_diffuse_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/lightprobe_geom.glsl SRC)

View File

@@ -87,6 +87,7 @@ extern char datatoc_lightprobe_cube_display_vert_glsl[];
extern char datatoc_lightprobe_grid_display_frag_glsl[];
extern char datatoc_lightprobe_grid_display_vert_glsl[];
extern char datatoc_irradiance_lib_glsl[];
extern char datatoc_lightprobe_lib_glsl[];
extern char datatoc_octahedron_lib_glsl[];
extern char datatoc_bsdf_common_lib_glsl[];
extern char datatoc_bsdf_sampling_lib_glsl[];
@@ -217,7 +218,9 @@ void EEVEE_lightprobes_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *UNUSED(ved
ds_frag = BLI_dynstr_new();
BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_irradiance_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_grid_display_frag_glsl);
shader_str = BLI_dynstr_get_cstring(ds_frag);
BLI_dynstr_free(ds_frag);
@@ -237,6 +240,8 @@ void EEVEE_lightprobes_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *UNUSED(ved
ds_frag = BLI_dynstr_new();
BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_cube_display_frag_glsl);
shader_str = BLI_dynstr_get_cstring(ds_frag);
BLI_dynstr_free(ds_frag);
@@ -247,7 +252,9 @@ void EEVEE_lightprobes_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *UNUSED(ved
MEM_freeN(shader_str);
ds_frag = BLI_dynstr_new();
BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_planar_display_frag_glsl);
shader_str = BLI_dynstr_get_cstring(ds_frag);
BLI_dynstr_free(ds_frag);
@@ -377,7 +384,7 @@ void EEVEE_lightprobes_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *veda
DRW_shgroup_uniform_float(grp, "invSampleCount", &sldata->probes->invsamples_ct, 1);
DRW_shgroup_uniform_float(grp, "roughnessSquared", &sldata->probes->roughness, 1);
DRW_shgroup_uniform_float(grp, "lodFactor", &sldata->probes->lodfactor, 1);
DRW_shgroup_uniform_float(grp, "lodMax", &sldata->probes->lodmax, 1);
DRW_shgroup_uniform_float(grp, "lodMax", &sldata->probes->lod_rt_max, 1);
DRW_shgroup_uniform_float(grp, "texelSize", &sldata->probes->texel_size, 1);
DRW_shgroup_uniform_float(grp, "paddingSize", &sldata->probes->padding_size, 1);
DRW_shgroup_uniform_int(grp, "Layer", &sldata->probes->layer, 1);
@@ -398,7 +405,7 @@ void EEVEE_lightprobes_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *veda
DRW_shgroup_uniform_float(grp, "sampleCount", &sldata->probes->samples_ct, 1);
DRW_shgroup_uniform_float(grp, "invSampleCount", &sldata->probes->invsamples_ct, 1);
DRW_shgroup_uniform_float(grp, "lodFactor", &sldata->probes->lodfactor, 1);
DRW_shgroup_uniform_float(grp, "lodMax", &sldata->probes->lodmax, 1);
DRW_shgroup_uniform_float(grp, "lodMax", &sldata->probes->lod_rt_max, 1);
DRW_shgroup_uniform_texture(grp, "texHammersley", e_data.hammersley);
#endif
DRW_shgroup_uniform_texture(grp, "probeHdr", sldata->probe_rt);
@@ -416,7 +423,7 @@ void EEVEE_lightprobes_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *veda
DRW_shgroup_attrib_float(grp, "probe_id", 1); /* XXX this works because we are still uploading 4bytes and using the right stride */
DRW_shgroup_attrib_float(grp, "probe_location", 3);
DRW_shgroup_attrib_float(grp, "sphere_size", 1);
DRW_shgroup_uniform_float(grp, "lodMax", &sldata->probes->lodmax, 1);
DRW_shgroup_uniform_float(grp, "lodCubeMax", &sldata->probes->lod_cube_max, 1);
DRW_shgroup_uniform_buffer(grp, "probeCubes", &sldata->probe_pool);
}
@@ -870,7 +877,7 @@ static void glossy_filter_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *ps
pinfo->invsamples_ct = 1.0f / pinfo->samples_ct;
pinfo->lodfactor = bias + 0.5f * log((float)(PROBE_RT_SIZE * PROBE_RT_SIZE) * pinfo->invsamples_ct) / log(2);
pinfo->lodmax = floorf(log2f(PROBE_RT_SIZE)) - 2.0f;
pinfo->lod_rt_max = floorf(log2f(PROBE_RT_SIZE)) - 2.0f;
DRW_framebuffer_texture_attach(sldata->probe_filter_fb, sldata->probe_pool, 0, i);
DRW_framebuffer_viewport_size(sldata->probe_filter_fb, 0, 0, mipsize, mipsize);
@@ -881,7 +888,7 @@ static void glossy_filter_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *ps
CLAMP_MIN(mipsize, 1);
}
/* For shading, save max level of the octahedron map */
pinfo->lodmax = (float)(maxlevel - min_lod_level) - 1.0f;
pinfo->lod_cube_max = (float)(maxlevel - min_lod_level) - 1.0f;
/* reattach to have a valid framebuffer. */
DRW_framebuffer_texture_attach(sldata->probe_filter_fb, sldata->probe_pool, 0, 0);
@@ -892,9 +899,6 @@ static void diffuse_filter_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *p
{
EEVEE_LightProbesInfo *pinfo = sldata->probes;
/* TODO do things properly */
float lodmax = pinfo->lodmax;
/* 4 - Compute spherical harmonics */
/* Tweaking parameters to balance perf. vs precision */
DRW_framebuffer_bind(sldata->probe_filter_fb);
@@ -924,10 +928,10 @@ static void diffuse_filter_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *p
const float bias = 0.0f;
pinfo->invsamples_ct = 1.0f / pinfo->samples_ct;
pinfo->lodfactor = bias + 0.5f * log((float)(PROBE_RT_SIZE * PROBE_RT_SIZE) * pinfo->invsamples_ct) / log(2);
pinfo->lodmax = floorf(log2f(PROBE_RT_SIZE)) - 2.0f;
pinfo->lod_rt_max = floorf(log2f(PROBE_RT_SIZE)) - 2.0f;
#else
pinfo->shres = 32; /* Less texture fetches & reduce branches */
pinfo->lodmax = 2.0f; /* Improve cache reuse */
pinfo->lod_rt_max = 2.0f; /* Improve cache reuse */
#endif
DRW_framebuffer_viewport_size(sldata->probe_filter_fb, x, y, size[0], size[1]);
@@ -936,9 +940,6 @@ static void diffuse_filter_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *p
/* reattach to have a valid framebuffer. */
DRW_framebuffer_texture_detach(sldata->irradiance_rt);
DRW_framebuffer_texture_attach(sldata->probe_filter_fb, sldata->probe_pool, 0, 0);
/* restore */
pinfo->lodmax = lodmax;
}
/* Render the scene to the probe_rt texture. */
@@ -1294,7 +1295,10 @@ update_planar:
/* If there is at least one planar probe */
if (pinfo->num_planar > 0) {
DRW_framebuffer_recursive_downsample(vedata->fbl->minmaxz_fb, txl->planar_pool, 5, &downsample_planar, vedata);
const int max_lod = 5;
DRW_framebuffer_recursive_downsample(vedata->fbl->minmaxz_fb, txl->planar_pool, max_lod, &downsample_planar, vedata);
/* For shading, save max level of the planar map */
pinfo->lod_planar_max = (float)(max_lod);
}
}

View File

@@ -77,6 +77,8 @@ static struct {
float viewvecs[2][4];
} e_data = {NULL}; /* Engine data */
extern char datatoc_lamps_lib_glsl[];
extern char datatoc_lightprobe_lib_glsl[];
extern char datatoc_ambient_occlusion_lib_glsl[];
extern char datatoc_prepass_frag_glsl[];
extern char datatoc_prepass_vert_glsl[];
@@ -211,7 +213,8 @@ static void add_standard_uniforms(DRWShadingGroup *shgrp, EEVEE_SceneLayerData *
DRW_shgroup_uniform_int(shgrp, "grid_count", &sldata->probes->num_render_grid, 1);
DRW_shgroup_uniform_int(shgrp, "planar_count", &sldata->probes->num_planar, 1);
DRW_shgroup_uniform_bool(shgrp, "specToggle", &sldata->probes->specular_toggle, 1);
DRW_shgroup_uniform_float(shgrp, "lodMax", &sldata->probes->lodmax, 1);
DRW_shgroup_uniform_float(shgrp, "lodCubeMax", &sldata->probes->lod_cube_max, 1);
DRW_shgroup_uniform_float(shgrp, "lodPlanarMax", &sldata->probes->lod_planar_max, 1);
DRW_shgroup_uniform_texture(shgrp, "utilTex", e_data.util_tex);
DRW_shgroup_uniform_buffer(shgrp, "probeCubes", &sldata->probe_pool);
DRW_shgroup_uniform_buffer(shgrp, "probePlanars", &vedata->txl->planar_pool);
@@ -252,8 +255,10 @@ void EEVEE_materials_init(void)
BLI_dynstr_append(ds_frag, datatoc_ambient_occlusion_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_irradiance_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_ltc_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_bsdf_direct_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lamps_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lit_surface_frag_glsl);
e_data.frag_shader_lib = BLI_dynstr_get_cstring(ds_frag);
BLI_dynstr_free(ds_frag);

View File

@@ -250,7 +250,7 @@ typedef struct EEVEE_LightProbesInfo {
float invsamples_ct;
float roughness;
float lodfactor;
float lodmax;
float lod_rt_max, lod_cube_max, lod_planar_max;
int shres;
int shnbr;
bool specular_toggle;

View File

@@ -213,4 +213,29 @@ float gtao_multibounce(float visibility, vec3 albedo)
float x = visibility;
return max(x, ((x * a + b) * x + c) * x);
}
}
/* Use the right occlusion */
float occlusion_compute(vec3 N, vec3 vpos, float user_occlusion, vec2 randuv, out vec3 bent_normal)
{
#ifdef USE_AO /* Screen Space Occlusion */
float computed_occlusion;
vec3 vnor = mat3(ViewMatrix) * N;
#ifdef USE_BENT_NORMAL
gtao(vnor, vpos, randuv, computed_occlusion, bent_normal);
bent_normal = mat3(ViewMatrixInverse) * bent_normal;
#else
gtao(vnor, vpos, randuv, computed_occlusion);
bent_normal = N;
#endif
return min(computed_occlusion, user_occlusion);
#else /* No added Occlusion. */
bent_normal = N;
return user_occlusion;
#endif
}

View File

@@ -9,62 +9,15 @@
#define LUT_SIZE 64
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrixInverse;
uniform mat4 ViewMatrix;
uniform vec4 viewvecs[2];
#define cameraForward normalize(ViewMatrixInverse[2].xyz)
#define cameraPos ViewMatrixInverse[3].xyz
/* ------- Structures -------- */
struct ProbeData {
vec4 position_type;
vec4 attenuation_fac_type;
mat4 influencemat;
mat4 parallaxmat;
};
#define PROBE_PARALLAX_BOX 1.0
#define PROBE_ATTENUATION_BOX 1.0
#define p_position position_type.xyz
#define p_parallax_type position_type.w
#define p_atten_fac attenuation_fac_type.x
#define p_atten_type attenuation_fac_type.y
struct PlanarData {
vec4 plane_equation;
vec4 clip_vec_x_fade_scale;
vec4 clip_vec_y_fade_bias;
vec4 clip_edges;
vec4 facing_scale_bias;
mat4 reflectionmat; /* transform world space into reflection texture space */
};
#define pl_plane_eq plane_equation
#define pl_normal plane_equation.xyz
#define pl_facing_scale facing_scale_bias.x
#define pl_facing_bias facing_scale_bias.y
#define pl_fade_scale clip_vec_x_fade_scale.w
#define pl_fade_bias clip_vec_y_fade_bias.w
#define pl_clip_pos_x clip_vec_x_fade_scale.xyz
#define pl_clip_pos_y clip_vec_y_fade_bias.xyz
#define pl_clip_edges clip_edges
struct GridData {
mat4 localmat;
ivec4 resolution_offset;
vec4 ws_corner_atten_scale; /* world space corner position */
vec4 ws_increment_x_atten_bias; /* world space vector between 2 opposite cells */
vec4 ws_increment_y;
vec4 ws_increment_z;
};
#define g_corner ws_corner_atten_scale.xyz
#define g_atten_scale ws_corner_atten_scale.w
#define g_atten_bias ws_increment_x_atten_bias.w
#define g_increment_x ws_increment_x_atten_bias.xyz
#define g_increment_y ws_increment_y.xyz
#define g_increment_z ws_increment_z.xyz
#define g_resolution resolution_offset.xyz
#define g_offset resolution_offset.w
struct LightData {
vec4 position_influence; /* w : InfluenceRadius */
vec4 color_spec; /* w : Spec Intensity */
@@ -126,14 +79,15 @@ struct ShadowCascadeData {
struct ShadingData {
vec3 V; /* View vector */
vec3 N; /* World Normal of the fragment */
vec3 W; /* World Position of the fragment */
vec3 l_vector; /* Current Light vector */
};
#define cameraVec ((ProjectionMatrix[3][3] == 0.0) ? normalize(cameraPos - worldPosition) : cameraForward)
/* ------- Convenience functions --------- */
vec3 mul(mat3 m, vec3 v) { return m * v; }
mat3 mul(mat3 m1, mat3 m2) { return m1 * m2; }
vec3 transform_point(mat4 m, vec3 v) { return (m * vec4(v, 1.0)).xyz; }
float min_v3(vec3 v) { return min(v.x, min(v.y, v.z)); }
@@ -288,8 +242,9 @@ vec3 get_view_space_from_depth(vec2 uvcoords, float depth)
}
}
vec3 get_specular_dominant_dir(vec3 N, vec3 R, float roughness)
vec3 get_specular_dominant_dir(vec3 N, vec3 V, float roughness)
{
vec3 R = -reflect(V, N);
float smoothness = 1.0 - roughness;
float fac = smoothness * (sqrt(smoothness) + roughness);
return normalize(mix(N, R, fac));
@@ -362,3 +317,8 @@ float bsdf_ggx(vec3 N, vec3 L, vec3 V, float roughness)
/* bsdf = D * G / (4.0 * NL * NV); /* Reference function */
return NL * a2 / (D * G); /* NL to Fit cycles Equation : line. 345 in bsdf_microfacet.h */
}
void accumulate_light(vec3 light, float fac, inout vec4 accum)
{
accum += vec4(light, 1.0) * min(fac, (1.0 - accum.a));
}

View File

@@ -11,10 +11,10 @@
/* ------------ Diffuse ------------- */
float direct_diffuse_point(LightData ld, ShadingData sd)
float direct_diffuse_point(ShadingData sd, vec4 l_vector)
{
float dist = length(sd.l_vector);
vec3 L = sd.l_vector / dist;
float dist = l_vector.w;
vec3 L = l_vector.xyz / dist;
float bsdf = max(0.0, dot(sd.N, L));
bsdf /= dist * dist;
return bsdf;
@@ -29,12 +29,12 @@ float direct_diffuse_sun(LightData ld, ShadingData sd)
}
/* From Frostbite PBR Course
* Analitical irradiance from a sphere with correct horizon handling
* Analytical irradiance from a sphere with correct horizon handling
* http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */
float direct_diffuse_sphere(LightData ld, ShadingData sd)
float direct_diffuse_sphere(LightData ld, ShadingData sd, vec4 l_vector)
{
float dist = length(sd.l_vector);
vec3 L = sd.l_vector / dist;
float dist = l_vector.w;
vec3 L = l_vector.xyz / dist;
float radius = max(ld.l_sizex, 0.0001);
float costheta = clamp(dot(sd.N, L), -0.999, 0.999);
float h = min(ld.l_radius / dist , 0.9999);
@@ -61,13 +61,13 @@ float direct_diffuse_sphere(LightData ld, ShadingData sd)
/* From Frostbite PBR Course
* http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */
float direct_diffuse_rectangle(LightData ld, ShadingData sd)
float direct_diffuse_rectangle(LightData ld, ShadingData sd, vec4 l_vector)
{
vec3 corners[4];
corners[0] = sd.l_vector + ld.l_right * -ld.l_sizex + ld.l_up * ld.l_sizey;
corners[1] = sd.l_vector + ld.l_right * -ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[2] = sd.l_vector + ld.l_right * ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[3] = sd.l_vector + ld.l_right * ld.l_sizex + ld.l_up * ld.l_sizey;
corners[0] = l_vector.xyz + ld.l_right * -ld.l_sizex + ld.l_up * ld.l_sizey;
corners[1] = l_vector.xyz + ld.l_right * -ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[2] = l_vector.xyz + ld.l_right * ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[3] = l_vector.xyz + ld.l_right * ld.l_sizex + ld.l_up * ld.l_sizey;
float bsdf = ltc_evaluate(sd.N, sd.V, mat3(1.0), corners);
bsdf *= M_1_2PI;
@@ -83,10 +83,10 @@ float direct_diffuse_unit_disc(vec3 N, vec3 L)
#endif
/* ----------- GGx ------------ */
vec3 direct_ggx_point(ShadingData sd, float roughness, vec3 f0)
vec3 direct_ggx_point(ShadingData sd, vec4 l_vector, float roughness, vec3 f0)
{
float dist = length(sd.l_vector);
vec3 L = sd.l_vector / dist;
float dist = l_vector.w;
vec3 L = l_vector.xyz / dist;
float bsdf = bsdf_ggx(sd.N, L, sd.V, roughness);
bsdf /= dist * dist;
@@ -102,13 +102,13 @@ vec3 direct_ggx_sun(LightData ld, ShadingData sd, float roughness, vec3 f0)
return F_schlick(f0, VH) * bsdf;
}
vec3 direct_ggx_sphere(LightData ld, ShadingData sd, float roughness, vec3 f0)
vec3 direct_ggx_sphere(LightData ld, ShadingData sd, vec4 l_vector, float roughness, vec3 f0)
{
vec3 L = normalize(sd.l_vector);
vec3 spec_dir = get_specular_dominant_dir(sd.N, reflect(-sd.V, sd.N), roughness);
vec3 P = line_aligned_plane_intersect(vec3(0.0), spec_dir, sd.l_vector);
vec3 L = l_vector.xyz / l_vector.w;
vec3 spec_dir = get_specular_dominant_dir(sd.N, sd.V, roughness);
vec3 P = line_aligned_plane_intersect(vec3(0.0), spec_dir, l_vector.xyz);
vec3 Px = normalize(P - sd.l_vector) * ld.l_radius;
vec3 Px = normalize(P - l_vector.xyz) * ld.l_radius;
vec3 Py = cross(Px, L);
vec2 uv = lut_coords(dot(sd.N, sd.V), sqrt(roughness));
@@ -123,21 +123,21 @@ vec3 direct_ggx_sphere(LightData ld, ShadingData sd, float roughness, vec3 f0)
/* counter clockwise */
vec3 points[8];
points[0] = sd.l_vector + Px;
points[1] = sd.l_vector - Pxy2;
points[2] = sd.l_vector - Py;
points[3] = sd.l_vector - Pxy1;
points[4] = sd.l_vector - Px;
points[5] = sd.l_vector + Pxy2;
points[6] = sd.l_vector + Py;
points[7] = sd.l_vector + Pxy1;
points[0] = l_vector.xyz + Px;
points[1] = l_vector.xyz - Pxy2;
points[2] = l_vector.xyz - Py;
points[3] = l_vector.xyz - Pxy1;
points[4] = l_vector.xyz - Px;
points[5] = l_vector.xyz + Pxy2;
points[6] = l_vector.xyz + Py;
points[7] = l_vector.xyz + Pxy1;
float bsdf = ltc_evaluate_circle(sd.N, sd.V, ltc_mat, points);
#else
vec3 points[4];
points[0] = sd.l_vector + Px;
points[1] = sd.l_vector - Py;
points[2] = sd.l_vector - Px;
points[3] = sd.l_vector + Py;
points[0] = l_vector.xyz + Px;
points[1] = l_vector.xyz - Py;
points[2] = l_vector.xyz - Px;
points[3] = l_vector.xyz + Py;
float bsdf = ltc_evaluate(sd.N, sd.V, ltc_mat, points);
/* sqrt(pi/2) difference between square and disk area */
bsdf *= 1.25331413731;
@@ -151,13 +151,13 @@ vec3 direct_ggx_sphere(LightData ld, ShadingData sd, float roughness, vec3 f0)
return spec;
}
vec3 direct_ggx_rectangle(LightData ld, ShadingData sd, float roughness, vec3 f0)
vec3 direct_ggx_rectangle(LightData ld, ShadingData sd, vec4 l_vector, float roughness, vec3 f0)
{
vec3 corners[4];
corners[0] = sd.l_vector + ld.l_right * -ld.l_sizex + ld.l_up * ld.l_sizey;
corners[1] = sd.l_vector + ld.l_right * -ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[2] = sd.l_vector + ld.l_right * ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[3] = sd.l_vector + ld.l_right * ld.l_sizex + ld.l_up * ld.l_sizey;
corners[0] = l_vector.xyz + ld.l_right * -ld.l_sizex + ld.l_up * ld.l_sizey;
corners[1] = l_vector.xyz + ld.l_right * -ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[2] = l_vector.xyz + ld.l_right * ld.l_sizex + ld.l_up * -ld.l_sizey;
corners[3] = l_vector.xyz + ld.l_right * ld.l_sizex + ld.l_up * ld.l_sizey;
vec2 uv = lut_coords(dot(sd.N, sd.V), sqrt(roughness));
vec3 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rgb;

View File

@@ -1,6 +1,8 @@
uniform sampler2D irradianceGrid;
#define IRRADIANCE_LIB
#ifdef IRRADIANCE_CUBEMAP
struct IrradianceData {
vec3 color;
@@ -132,10 +134,8 @@ vec3 compute_irradiance(vec3 N, IrradianceData ird)
#endif
}
vec3 get_cell_color(ivec3 localpos, ivec3 gridres, int offset, vec3 ir_dir)
vec3 irradiance_from_cell_get(int cell, vec3 ir_dir)
{
/* Keep in sync with update_irradiance_probe */
int cell = offset + localpos.z + localpos.y * gridres.z + localpos.x * gridres.z * gridres.y;
IrradianceData ir_data = load_irradiance_cell(cell, ir_dir);
return compute_irradiance(ir_dir, ir_data);
}

View File

@@ -0,0 +1,193 @@
uniform sampler2DArray shadowCubes;
uniform sampler2DArrayShadow shadowCascades;
layout(std140) uniform shadow_block {
ShadowCubeData shadows_cube_data[MAX_SHADOW_CUBE];
ShadowMapData shadows_map_data[MAX_SHADOW_MAP];
ShadowCascadeData shadows_cascade_data[MAX_SHADOW_CASCADE];
};
layout(std140) uniform probe_block {
CubeData probes_data[MAX_PROBE];
};
layout(std140) uniform grid_block {
GridData grids_data[MAX_GRID];
};
layout(std140) uniform planar_block {
PlanarData planars_data[MAX_PLANAR];
};
layout(std140) uniform light_block {
LightData lights_data[MAX_LIGHT];
};
/* type */
#define POINT 0.0
#define SUN 1.0
#define SPOT 2.0
#define HEMI 3.0
#define AREA 4.0
float shadow_cubemap(float shid, vec3 l_vector, vec3 W)
{
ShadowCubeData scd = shadows_cube_data[int(shid)];
vec3 cubevec = W - l_vector;
float dist = length(cubevec) - scd.sh_cube_bias;
float z = texture_octahedron(shadowCubes, vec4(cubevec, shid)).r;
float esm_test = saturate(exp(scd.sh_cube_exp * (z - dist)));
// float sh_test = step(0, z - dist);
return esm_test;
}
float shadow_cascade(float shid, vec3 W)
{
/* Shadow Cascade */
shid -= (MAX_SHADOW_CUBE + MAX_SHADOW_MAP);
ShadowCascadeData smd = shadows_cascade_data[int(shid)];
/* Finding Cascade index */
vec4 z = vec4(-dot(cameraPos - W, cameraForward));
vec4 comp = step(z, smd.split_distances);
float cascade = dot(comp, comp);
mat4 shadowmat;
float bias;
/* Manual Unrolling of a loop for better performance.
* Doing fetch directly with cascade index leads to
* major performance impact. (0.27ms -> 10.0ms for 1 light) */
if (cascade == 0.0) {
shadowmat = smd.shadowmat[0];
bias = smd.bias[0];
}
else if (cascade == 1.0) {
shadowmat = smd.shadowmat[1];
bias = smd.bias[1];
}
else if (cascade == 2.0) {
shadowmat = smd.shadowmat[2];
bias = smd.bias[2];
}
else {
shadowmat = smd.shadowmat[3];
bias = smd.bias[3];
}
vec4 shpos = shadowmat * vec4(W, 1.0);
shpos.z -= bias * shpos.w;
shpos.xyz /= shpos.w;
return texture(shadowCascades, vec4(shpos.xy, shid * float(MAX_CASCADE_NUM) + cascade, shpos.z));
}
float light_visibility(LightData ld, vec3 W, vec3 l_vector)
{
float vis = 1.0;
if (ld.l_type == SPOT) {
float z = dot(ld.l_forward, l_vector);
vec3 lL = l_vector / z;
float x = dot(ld.l_right, lL) / ld.l_sizex;
float y = dot(ld.l_up, lL) / ld.l_sizey;
float ellipse = 1.0 / sqrt(1.0 + x * x + y * y);
float spotmask = smoothstep(0.0, 1.0, (ellipse - ld.l_spot_size) / ld.l_spot_blend);
vis *= spotmask;
vis *= step(0.0, -dot(l_vector, ld.l_forward));
}
else if (ld.l_type == AREA) {
vis *= step(0.0, -dot(l_vector, ld.l_forward));
}
/* shadowing */
if (ld.l_shadowid >= (MAX_SHADOW_MAP + MAX_SHADOW_CUBE)) {
vis *= shadow_cascade(ld.l_shadowid, W);
}
else if (ld.l_shadowid >= 0.0) {
vis *= shadow_cubemap(ld.l_shadowid, l_vector, W);
}
return vis;
}
float light_diffuse(LightData ld, ShadingData sd, vec4 l_vector)
{
#ifdef USE_LTC
if (ld.l_type == SUN) {
/* TODO disk area light */
return direct_diffuse_sun(ld, sd);
}
else if (ld.l_type == AREA) {
return direct_diffuse_rectangle(ld, sd, l_vector);
}
else {
return direct_diffuse_sphere(ld, sd, l_vector);
}
#else
if (ld.l_type == SUN) {
return direct_diffuse_sun(ld, sd);
}
else {
return direct_diffuse_point(sd, l_vector);
}
#endif
}
vec3 light_specular(LightData ld, ShadingData sd, vec4 l_vector, float roughness, vec3 f0)
{
#ifdef USE_LTC
if (ld.l_type == SUN) {
/* TODO disk area light */
return direct_ggx_sun(ld, sd, roughness, f0);
}
else if (ld.l_type == AREA) {
return direct_ggx_rectangle(ld, sd, l_vector, roughness, f0);
}
else {
return direct_ggx_sphere(ld, sd, l_vector, roughness, f0);
}
#else
if (ld.l_type == SUN) {
return direct_ggx_sun(ld, sd, roughness, f0);
}
else {
return direct_ggx_point(sd, l_vector, roughness, f0);
}
#endif
}
#ifdef HAIR_SHADER
void light_hair_common(
LightData ld, ShadingData sd, vec4 l_vector, vec3 norm_view,
out float occlu_trans, out float occlu,
out vec3 norm_lamp, out vec3 view_vec)
{
const float transmission = 0.3; /* Uniform internal scattering factor */
vec3 lamp_vec;
if (ld.l_type == SUN || ld.l_type == AREA) {
lamp_vec = ld.l_forward;
}
else {
lamp_vec = -l_vector.xyz;
}
norm_lamp = cross(lamp_vec, sd.N);
norm_lamp = normalize(cross(sd.N, norm_lamp)); /* Normal facing lamp */
/* Rotate view vector onto the cross(tangent, light) plane */
view_vec = normalize(norm_lamp * dot(norm_view, sd.V) + sd.N * dot(sd.N, sd.V));
float occlusion = (dot(norm_view, norm_lamp) * 0.5 + 0.5);
float occltrans = transmission + (occlusion * (1.0 - transmission)); /* Includes transmission component */
}
#endif

View File

@@ -1,24 +1,15 @@
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrixInverse;
uniform sampler2DArray probeCubes;
uniform float lodMax;
flat in int pid;
in vec3 worldNormal;
in vec3 worldPosition;
out vec4 FragColor;
#define cameraForward normalize(ViewMatrixInverse[2].xyz)
#define cameraPos ViewMatrixInverse[3].xyz
void main()
{
vec3 V = (ProjectionMatrix[3][3] == 0.0) /* if perspective */
? normalize(cameraPos - worldPosition)
: cameraForward;
vec3 N = normalize(worldNormal);
FragColor = vec4(textureLod_octahedron(probeCubes, vec4(reflect(-V, N), pid), 0.0, lodMax).rgb, 1.0);
FragColor = vec4(textureLod_octahedron(probeCubes, vec4(reflect(-V, N), pid), 0.0, lodCubeMax).rgb, 1.0);
}

View File

@@ -0,0 +1,245 @@
/* ----------- Uniforms --------- */
uniform sampler2DArray probePlanars;
uniform float lodPlanarMax;
uniform sampler2DArray probeCubes;
uniform float lodCubeMax;
/* ----------- Structures --------- */
struct CubeData {
vec4 position_type;
vec4 attenuation_fac_type;
mat4 influencemat;
mat4 parallaxmat;
};
#define PROBE_PARALLAX_BOX 1.0
#define PROBE_ATTENUATION_BOX 1.0
#define p_position position_type.xyz
#define p_parallax_type position_type.w
#define p_atten_fac attenuation_fac_type.x
#define p_atten_type attenuation_fac_type.y
struct PlanarData {
vec4 plane_equation;
vec4 clip_vec_x_fade_scale;
vec4 clip_vec_y_fade_bias;
vec4 clip_edges;
vec4 facing_scale_bias;
mat4 reflectionmat; /* transform world space into reflection texture space */
};
#define pl_plane_eq plane_equation
#define pl_normal plane_equation.xyz
#define pl_facing_scale facing_scale_bias.x
#define pl_facing_bias facing_scale_bias.y
#define pl_fade_scale clip_vec_x_fade_scale.w
#define pl_fade_bias clip_vec_y_fade_bias.w
#define pl_clip_pos_x clip_vec_x_fade_scale.xyz
#define pl_clip_pos_y clip_vec_y_fade_bias.xyz
#define pl_clip_edges clip_edges
struct GridData {
mat4 localmat;
ivec4 resolution_offset;
vec4 ws_corner_atten_scale; /* world space corner position */
vec4 ws_increment_x_atten_bias; /* world space vector between 2 opposite cells */
vec4 ws_increment_y;
vec4 ws_increment_z;
};
#define g_corner ws_corner_atten_scale.xyz
#define g_atten_scale ws_corner_atten_scale.w
#define g_atten_bias ws_increment_x_atten_bias.w
#define g_increment_x ws_increment_x_atten_bias.xyz
#define g_increment_y ws_increment_y.xyz
#define g_increment_z ws_increment_z.xyz
#define g_resolution resolution_offset.xyz
#define g_offset resolution_offset.w
/* ----------- Functions --------- */
float probe_attenuation_cube(CubeData pd, vec3 W)
{
vec3 localpos = transform_point(pd.influencemat, W);
float fac;
if (pd.p_atten_type == PROBE_ATTENUATION_BOX) {
vec3 axes_fac = saturate(pd.p_atten_fac - pd.p_atten_fac * abs(localpos));
fac = min_v3(axes_fac);
}
else {
fac = saturate(pd.p_atten_fac - pd.p_atten_fac * length(localpos));
}
return fac;
}
float probe_attenuation_planar(PlanarData pd, vec3 W, vec3 N)
{
/* Normal Facing */
float fac = saturate(dot(pd.pl_normal, N) * pd.pl_facing_scale + pd.pl_facing_bias);
/* Distance from plane */
fac *= saturate(abs(dot(pd.pl_plane_eq, vec4(W, 1.0))) * pd.pl_fade_scale + pd.pl_fade_bias);
/* Fancy fast clipping calculation */
vec2 dist_to_clip;
dist_to_clip.x = dot(pd.pl_clip_pos_x, W);
dist_to_clip.y = dot(pd.pl_clip_pos_y, W);
fac *= step(2.0, dot(step(pd.pl_clip_edges, dist_to_clip.xxyy), vec2(-1.0, 1.0).xyxy)); /* compare and add all tests */
return fac;
}
float probe_attenuation_grid(GridData gd, vec3 W, out vec3 localpos)
{
localpos = transform_point(gd.localmat, W);
float fade = min(1.0, min_v3(1.0 - abs(localpos)));
return saturate(fade * gd.g_atten_scale + gd.g_atten_bias);
}
vec3 probe_evaluate_cube(float id, CubeData cd, vec3 W, vec3 R, float roughness)
{
/* Correct reflection ray using parallax volume intersection. */
vec3 localpos = transform_point(cd.parallaxmat, W);
vec3 localray = mat3(cd.parallaxmat) * R;
float dist;
if (cd.p_parallax_type == PROBE_PARALLAX_BOX) {
dist = line_unit_box_intersect_dist(localpos, localray);
}
else {
dist = line_unit_sphere_intersect_dist(localpos, localray);
}
/* Use Distance in WS directly to recover intersection */
vec3 intersection = W + R * dist - cd.p_position;
/* From Frostbite PBR Course
* Distance based roughness
* http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */
float original_roughness = roughness;
float linear_roughness = sqrt(roughness);
float distance_roughness = saturate(dist * linear_roughness / length(intersection));
linear_roughness = mix(distance_roughness, linear_roughness, linear_roughness);
roughness = linear_roughness * linear_roughness;
float fac = saturate(original_roughness * 2.0 - 1.0);
R = mix(intersection, R, fac * fac);
return textureLod_octahedron(probeCubes, vec4(R, id), roughness * lodCubeMax, lodCubeMax).rgb;
}
vec3 probe_evaluate_world_spec(vec3 R, float roughness)
{
return textureLod_octahedron(probeCubes, vec4(R, 0.0), roughness * lodCubeMax, lodCubeMax).rgb;
}
vec3 probe_evaluate_planar(
float id, PlanarData pd, vec3 W, vec3 N, vec3 V,
float rand, vec3 camera_pos, float roughness,
inout float fade)
{
/* Sample reflection depth. */
vec4 refco = pd.reflectionmat * vec4(W, 1.0);
refco.xy /= refco.w;
float ref_depth = textureLod(probePlanars, vec3(refco.xy, id), 0.0).a;
/* Find view vector / reflection plane intersection. (dist_to_plane is negative) */
float dist_to_plane = line_plane_intersect_dist(camera_pos, V, pd.pl_plane_eq);
vec3 point_on_plane = camera_pos + V * dist_to_plane;
/* How far the pixel is from the plane. */
ref_depth = ref_depth + dist_to_plane;
/* Compute distorded reflection vector based on the distance to the reflected object.
* In other words find intersection between reflection vector and the sphere center
* around point_on_plane. */
vec3 proj_ref = reflect(reflect(-V, N) * ref_depth, pd.pl_normal);
/* Final point in world space. */
vec3 ref_pos = point_on_plane + proj_ref;
/* Reproject to find texture coords. */
refco = pd.reflectionmat * vec4(ref_pos, 1.0);
refco.xy /= refco.w;
/* Distance to roughness */
float linear_roughness = sqrt(roughness);
float distance_roughness = min(linear_roughness, ref_depth * linear_roughness);
linear_roughness = mix(distance_roughness, linear_roughness, linear_roughness);
/* Decrease influence for high roughness */
fade *= saturate((1.0 - linear_roughness) * 5.0 - 2.0);
float lod = linear_roughness * 2.5 * lodPlanarMax;
vec3 sample = textureLod(probePlanars, vec3(refco.xy, id), lod).rgb;
/* Use a second sample randomly rotated to blur out the lowres aspect */
vec2 rot_sample = (1.0 / vec2(textureSize(probePlanars, 0).xy)) * vec2(cos(rand * M_2PI), sin(rand * M_2PI)) * lod;
sample += textureLod(probePlanars, vec3(refco.xy + rot_sample, id), lod).rgb;
sample *= 0.5;
return sample;
}
#ifdef IRRADIANCE_LIB
vec3 probe_evaluate_grid(GridData gd, vec3 W, vec3 N, vec3 localpos)
{
localpos = localpos * 0.5 + 0.5;
localpos = localpos * vec3(gd.g_resolution) - 0.5;
vec3 localpos_floored = floor(localpos);
vec3 trilinear_weight = fract(localpos);
float weight_accum = 0.0;
vec3 irradiance_accum = vec3(0.0);
/* For each neighboor cells */
for (int i = 0; i < 8; ++i) {
ivec3 offset = ivec3(i, i >> 1, i >> 2) & ivec3(1);
vec3 cell_cos = clamp(localpos_floored + vec3(offset), vec3(0.0), vec3(gd.g_resolution) - 1.0);
/* Keep in sync with update_irradiance_probe */
ivec3 icell_cos = ivec3(cell_cos);
int cell = gd.g_offset + icell_cos.z + icell_cos.y * gd.g_resolution.z + icell_cos.x * gd.g_resolution.z * gd.g_resolution.y;
vec3 color = irradiance_from_cell_get(cell, N);
/* We need this because we render probes in world space (so we need light vector in WS).
* And rendering them in local probe space is too much problem. */
vec3 ws_cell_location = gd.g_corner +
(gd.g_increment_x * cell_cos.x +
gd.g_increment_y * cell_cos.y +
gd.g_increment_z * cell_cos.z);
// vec3 ws_point_to_cell = ws_cell_location - W;
// vec3 ws_light = normalize(ws_point_to_cell);
vec3 trilinear = mix(1 - trilinear_weight, trilinear_weight, offset);
float weight = trilinear.x * trilinear.y * trilinear.z;
/* Smooth backface test */
// weight *= sqrt(max(0.002, dot(ws_light, N)));
/* Avoid zero weight */
weight = max(0.00001, weight);
weight_accum += weight;
irradiance_accum += color * weight;
}
return irradiance_accum / weight_accum;
}
vec3 probe_evaluate_world_diff(vec3 N)
{
return irradiance_from_cell_get(0, N);
}
#endif /* IRRADIANCE_LIB */

View File

@@ -1,6 +1,5 @@
uniform int probeIdx;
uniform sampler2DArray probePlanars;
layout(std140) uniform planar_block {
PlanarData planars_data[MAX_PLANAR];

View File

@@ -3,13 +3,7 @@ uniform int light_count;
uniform int probe_count;
uniform int grid_count;
uniform int planar_count;
uniform mat4 ViewMatrix;
uniform mat4 ViewMatrixInverse;
uniform sampler2DArray probePlanars;
uniform sampler2DArray probeCubes;
uniform float lodMax;
uniform bool specToggle;
#ifndef UTIL_TEX
@@ -17,31 +11,6 @@ uniform bool specToggle;
uniform sampler2DArray utilTex;
#endif /* UTIL_TEX */
uniform sampler2DArray shadowCubes;
uniform sampler2DArrayShadow shadowCascades;
layout(std140) uniform probe_block {
ProbeData probes_data[MAX_PROBE];
};
layout(std140) uniform grid_block {
GridData grids_data[MAX_GRID];
};
layout(std140) uniform planar_block {
PlanarData planars_data[MAX_PLANAR];
};
layout(std140) uniform light_block {
LightData lights_data[MAX_LIGHT];
};
layout(std140) uniform shadow_block {
ShadowCubeData shadows_cube_data[MAX_SHADOW_CUBE];
ShadowMapData shadows_map_data[MAX_SHADOW_MAP];
ShadowCascadeData shadows_cascade_data[MAX_SHADOW_CASCADE];
};
in vec3 worldPosition;
in vec3 viewPosition;
@@ -53,481 +22,131 @@ in vec3 worldNormal;
in vec3 viewNormal;
#endif
#define cameraForward normalize(ViewMatrixInverse[2].xyz)
#define cameraPos ViewMatrixInverse[3].xyz
/* type */
#define POINT 0.0
#define SUN 1.0
#define SPOT 2.0
#define HEMI 3.0
#define AREA 4.0
#ifdef HAIR_SHADER
vec3 light_diffuse(LightData ld, ShadingData sd, vec3 albedo)
{
if (ld.l_type == SUN) {
return direct_diffuse_sun(ld, sd) * albedo;
}
else if (ld.l_type == AREA) {
return direct_diffuse_rectangle(ld, sd) * albedo;
}
else {
return direct_diffuse_sphere(ld, sd) * albedo;
}
}
vec3 light_specular(LightData ld, ShadingData sd, float roughness, vec3 f0)
{
if (ld.l_type == SUN) {
return direct_ggx_sun(ld, sd, roughness, f0);
}
else if (ld.l_type == AREA) {
return direct_ggx_rectangle(ld, sd, roughness, f0);
}
else {
return direct_ggx_sphere(ld, sd, roughness, f0);
}
}
void light_shade(
LightData ld, ShadingData sd, vec3 albedo, float roughness, vec3 f0,
out vec3 diffuse, out vec3 specular)
{
const float transmission = 0.3; /* Uniform internal scattering factor */
ShadingData sd_new = sd;
vec3 lamp_vec;
if (ld.l_type == SUN || ld.l_type == AREA) {
lamp_vec = ld.l_forward;
}
else {
lamp_vec = -sd.l_vector;
}
vec3 norm_view = cross(sd.V, sd.N);
norm_view = normalize(cross(norm_view, sd.N)); /* Normal facing view */
vec3 norm_lamp = cross(lamp_vec, sd.N);
norm_lamp = normalize(cross(sd.N, norm_lamp)); /* Normal facing lamp */
/* Rotate view vector onto the cross(tangent, light) plane */
vec3 view_vec = normalize(norm_lamp * dot(norm_view, sd.V) + sd.N * dot(sd.N, sd.V));
float occlusion = (dot(norm_view, norm_lamp) * 0.5 + 0.5);
float occltrans = transmission + (occlusion * (1.0 - transmission)); /* Includes transmission component */
sd_new.N = -norm_lamp;
diffuse = light_diffuse(ld, sd_new, albedo) * occltrans;
sd_new.V = view_vec;
specular = light_specular(ld, sd_new, roughness, f0) * occlusion;
}
#else
void light_shade(
LightData ld, ShadingData sd, vec3 albedo, float roughness, vec3 f0,
out vec3 diffuse, out vec3 specular)
{
#ifdef USE_LTC
if (ld.l_type == SUN) {
/* TODO disk area light */
diffuse = direct_diffuse_sun(ld, sd) * albedo;
specular = direct_ggx_sun(ld, sd, roughness, f0);
}
else if (ld.l_type == AREA) {
diffuse = direct_diffuse_rectangle(ld, sd) * albedo;
specular = direct_ggx_rectangle(ld, sd, roughness, f0);
}
else {
diffuse = direct_diffuse_sphere(ld, sd) * albedo;
specular = direct_ggx_sphere(ld, sd, roughness, f0);
}
#else
if (ld.l_type == SUN) {
diffuse = direct_diffuse_sun(ld, sd) * albedo;
specular = direct_ggx_sun(ld, sd, roughness, f0);
}
else {
diffuse = direct_diffuse_point(ld, sd) * albedo;
specular = direct_ggx_point(sd, roughness, f0);
}
#endif
specular *= float(specToggle);
}
#endif
void light_visibility(LightData ld, ShadingData sd, out float vis)
{
vis = 1.0;
if (ld.l_type == SPOT) {
float z = dot(ld.l_forward, sd.l_vector);
vec3 lL = sd.l_vector / z;
float x = dot(ld.l_right, lL) / ld.l_sizex;
float y = dot(ld.l_up, lL) / ld.l_sizey;
float ellipse = 1.0 / sqrt(1.0 + x * x + y * y);
float spotmask = smoothstep(0.0, 1.0, (ellipse - ld.l_spot_size) / ld.l_spot_blend);
vis *= spotmask;
vis *= step(0.0, -dot(sd.l_vector, ld.l_forward));
}
else if (ld.l_type == AREA) {
vis *= step(0.0, -dot(sd.l_vector, ld.l_forward));
}
/* shadowing */
if (ld.l_shadowid >= (MAX_SHADOW_MAP + MAX_SHADOW_CUBE)) {
/* Shadow Cascade */
float shid = ld.l_shadowid - (MAX_SHADOW_CUBE + MAX_SHADOW_MAP);
ShadowCascadeData smd = shadows_cascade_data[int(shid)];
/* Finding Cascade index */
vec4 z = vec4(-dot(cameraPos - worldPosition, cameraForward));
vec4 comp = step(z, smd.split_distances);
float cascade = dot(comp, comp);
mat4 shadowmat;
float bias;
/* Manual Unrolling of a loop for better performance.
* Doing fetch directly with cascade index leads to
* major performance impact. (0.27ms -> 10.0ms for 1 light) */
if (cascade == 0.0) {
shadowmat = smd.shadowmat[0];
bias = smd.bias[0];
}
else if (cascade == 1.0) {
shadowmat = smd.shadowmat[1];
bias = smd.bias[1];
}
else if (cascade == 2.0) {
shadowmat = smd.shadowmat[2];
bias = smd.bias[2];
}
else {
shadowmat = smd.shadowmat[3];
bias = smd.bias[3];
}
vec4 shpos = shadowmat * vec4(sd.W, 1.0);
shpos.z -= bias * shpos.w;
shpos.xyz /= shpos.w;
vis *= texture(shadowCascades, vec4(shpos.xy, shid * float(MAX_CASCADE_NUM) + cascade, shpos.z));
}
else if (ld.l_shadowid >= 0.0) {
/* Shadow Cube */
float shid = ld.l_shadowid;
ShadowCubeData scd = shadows_cube_data[int(shid)];
vec3 cubevec = sd.W - ld.l_position;
float dist = length(cubevec) - scd.sh_cube_bias;
float z = texture_octahedron(shadowCubes, vec4(cubevec, shid)).r;
float esm_test = saturate(exp(scd.sh_cube_exp * (z - dist)));
float sh_test = step(0, z - dist);
vis *= esm_test;
}
}
vec3 probe_parallax_correction(vec3 W, vec3 spec_dir, ProbeData pd, inout float roughness)
{
vec3 localpos = (pd.parallaxmat * vec4(W, 1.0)).xyz;
vec3 localray = (pd.parallaxmat * vec4(spec_dir, 0.0)).xyz;
float dist;
if (pd.p_parallax_type == PROBE_PARALLAX_BOX) {
dist = line_unit_box_intersect_dist(localpos, localray);
}
else {
dist = line_unit_sphere_intersect_dist(localpos, localray);
}
/* Use Distance in WS directly to recover intersection */
vec3 intersection = W + spec_dir * dist - pd.p_position;
/* From Frostbite PBR Course
* Distance based roughness
* http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */
float original_roughness = roughness;
float linear_roughness = sqrt(roughness);
float distance_roughness = saturate(dist * linear_roughness / length(intersection));
linear_roughness = mix(distance_roughness, linear_roughness, linear_roughness);
roughness = linear_roughness * linear_roughness;
float fac = saturate(original_roughness * 2.0 - 1.0);
return mix(intersection, spec_dir, fac * fac);
}
float probe_attenuation(vec3 W, ProbeData pd)
{
vec3 localpos = (pd.influencemat * vec4(W, 1.0)).xyz;
float fac;
if (pd.p_atten_type == PROBE_ATTENUATION_BOX) {
vec3 axes_fac = saturate(pd.p_atten_fac - pd.p_atten_fac * abs(localpos));
fac = min_v3(axes_fac);
}
else {
fac = saturate(pd.p_atten_fac - pd.p_atten_fac * length(localpos));
}
return fac;
}
float planar_attenuation(vec3 W, vec3 N, PlanarData pd)
{
float fac;
/* Normal Facing */
fac = saturate(dot(pd.pl_normal, N) * pd.pl_facing_scale + pd.pl_facing_bias);
/* Distance from plane */
fac *= saturate(abs(dot(pd.pl_plane_eq, vec4(W, 1.0))) * pd.pl_fade_scale + pd.pl_fade_bias);
/* Fancy fast clipping calculation */
vec2 dist_to_clip;
dist_to_clip.x = dot(pd.pl_clip_pos_x, W);
dist_to_clip.y = dot(pd.pl_clip_pos_y, W);
fac *= step(2.0, dot(step(pd.pl_clip_edges, dist_to_clip.xxyy), vec2(-1.0, 1.0).xyxy)); /* compare and add all tests */
return fac;
}
float compute_occlusion(vec3 N, float micro_occlusion, vec2 randuv, out vec3 bent_normal)
{
#ifdef USE_AO /* Screen Space Occlusion */
float macro_occlusion;
vec3 vnor = mat3(ViewMatrix) * N;
#ifdef USE_BENT_NORMAL
gtao(vnor, viewPosition, randuv, macro_occlusion, bent_normal);
bent_normal = mat3(ViewMatrixInverse) * bent_normal;
#else
gtao(vnor, viewPosition, randuv, macro_occlusion);
bent_normal = N;
#endif
return min(macro_occlusion, micro_occlusion);
#else /* No added Occlusion. */
bent_normal = N;
return micro_occlusion;
#endif
}
vec3 eevee_surface_lit(vec3 world_normal, vec3 albedo, vec3 f0, float roughness, float ao)
{
roughness = clamp(roughness, 1e-8, 0.9999);
float roughnessSquared = roughness * roughness;
ShadingData sd;
sd.N = normalize(world_normal);
sd.V = (ProjectionMatrix[3][3] == 0.0) /* if perspective */
? normalize(cameraPos - worldPosition)
: cameraForward;
sd.W = worldPosition;
ShadingData sd = ShadingData(cameraVec, normalize(world_normal));
vec3 radiance = vec3(0.0);
vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));
/* ---------------- SCENE LAMPS LIGHTING ----------------- */
#ifdef HAIR_SHADER
/* View facing normal */
vec3 norm_view = cross(sd.V, sd.N);
norm_view = normalize(cross(norm_view, sd.N)); /* Normal facing view */
vec3 norm_view = cross(sd.V, sd.N);
norm_view = normalize(cross(norm_view, sd.N)); /* Normal facing view */
#endif
/* Analytic Lights */
vec3 diff = vec3(0.0);
vec3 spec = vec3(0.0);
for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
LightData ld = lights_data[i];
vec3 diff, spec;
float vis = 1.0;
sd.l_vector = ld.l_position - worldPosition;
light_visibility(ld, sd, vis);
light_shade(ld, sd, albedo, roughnessSquared, f0, diff, spec);
vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
l_vector.xyz = ld.l_position - worldPosition;
l_vector.w = length(l_vector.xyz);
radiance += vis * (diff + spec) * ld.l_color;
vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, l_vector.xyz);
#ifdef HAIR_SHADER
vec3 norm_lamp, view_vec;
float occlu_trans, occlu;
light_hair_common(ld, sd, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);
ShadingData hsd = sd;
hsd.N = -norm_lamp;
diff += l_color_vis * light_diffuse(ld, hsd, l_vector) * occlu_trans;
hsd.V = view_vec;
spec += l_color_vis * light_specular(ld, hsd, l_vector, roughnessSquared, f0) * occlu;
#else
diff += l_color_vis * light_diffuse(ld, sd, l_vector);
spec += l_color_vis * light_specular(ld, sd, l_vector, roughnessSquared, f0);
#endif
}
/* Accumulate outgoing radiance */
vec3 out_light = diff * albedo + spec * float(specToggle);
#ifdef HAIR_SHADER
sd.N = -norm_view;
#endif
vec3 bent_normal;
vec4 rand = textureLod(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0), 0.0).rgba;
float final_ao = compute_occlusion(sd.N, ao, rand.rg, bent_normal);
/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */
/* Envmaps */
vec3 R = reflect(-sd.V, sd.N);
vec3 spec_dir = get_specular_dominant_dir(sd.N, R, roughnessSquared);
vec2 uv = lut_coords(dot(sd.N, sd.V), roughness);
vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;
vec3 spec_dir = get_specular_dominant_dir(sd.N, sd.V, roughnessSquared);
/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
vec4 spec_accum = vec4(0.0);
vec4 diff_accum = vec4(0.0);
/* Planar Reflections */
for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) {
PlanarData pd = planars_data[i];
float influence = planar_attenuation(sd.W, sd.N, pd);
float fade = probe_attenuation_planar(pd, worldPosition, sd.N);
if (influence > 0.0) {
float influ_spec = min(influence, (1.0 - spec_accum.a));
/* Sample reflection depth. */
vec4 refco = pd.reflectionmat * vec4(sd.W, 1.0);
refco.xy /= refco.w;
float ref_depth = textureLod(probePlanars, vec3(refco.xy, i), 0.0).a;
/* Find view vector / reflection plane intersection. (dist_to_plane is negative) */
float dist_to_plane = line_plane_intersect_dist(cameraPos, sd.V, pd.pl_plane_eq);
vec3 point_on_plane = cameraPos + sd.V * dist_to_plane;
/* How far the pixel is from the plane. */
ref_depth = ref_depth + dist_to_plane;
/* Compute distorded reflection vector based on the distance to the reflected object.
* In other words find intersection between reflection vector and the sphere center
* around point_on_plane. */
vec3 proj_ref = reflect(R * ref_depth, pd.pl_normal);
/* Final point in world space. */
vec3 ref_pos = point_on_plane + proj_ref;
/* Reproject to find texture coords. */
refco = pd.reflectionmat * vec4(ref_pos, 1.0);
refco.xy /= refco.w;
/* Distance to roughness */
float linear_roughness = sqrt(roughness);
float distance_roughness = min(linear_roughness, ref_depth * linear_roughness);
linear_roughness = mix(distance_roughness, linear_roughness, linear_roughness);
/* Decrease influence for high roughness */
influ_spec *= saturate((1.0 - linear_roughness) * 5.0 - 2.0);
float lod = linear_roughness * 2.5 * 5.0;
vec3 sample = textureLod(probePlanars, vec3(refco.xy, i), lod).rgb;
/* Use a second sample randomly rotated to blur out the lowres aspect */
vec2 rot_sample = (1.0 / vec2(textureSize(probePlanars, 0).xy)) * vec2(cos(rand.a * M_2PI), sin(rand.a * M_2PI)) * lod;
sample += textureLod(probePlanars, vec3(refco.xy + rot_sample, i), lod).rgb;
sample *= 0.5;
spec_accum.rgb += sample * influ_spec;
spec_accum.a += influ_spec;
if (fade > 0.0) {
vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, sd.N, sd.V, rand.a, cameraPos, roughness, fade);
accumulate_light(spec, fade, spec_accum);
}
}
/* Specular probes */
/* Start at 1 because 0 is world probe */
/* Starts at 1 because 0 is world probe */
for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) {
ProbeData pd = probes_data[i];
CubeData cd = probes_data[i];
float dist_attenuation = probe_attenuation(sd.W, pd);
float fade = probe_attenuation_cube(cd, worldPosition);
if (dist_attenuation > 0.0) {
float roughness_copy = roughness;
vec3 sample_vec = probe_parallax_correction(sd.W, spec_dir, pd, roughness_copy);
vec4 sample = textureLod_octahedron(probeCubes, vec4(sample_vec, i), roughness_copy * lodMax, lodMax).rgba;
float influ_spec = min(dist_attenuation, (1.0 - spec_accum.a));
spec_accum.rgb += sample.rgb * influ_spec;
spec_accum.a += influ_spec;
if (fade > 0.0) {
vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
accumulate_light(spec, fade, spec_accum);
}
}
/* World Specular */
if (spec_accum.a < 1.0) {
vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
accumulate_light(spec, 1.0, spec_accum);
}
/* Ambient Occlusion */
vec3 bent_normal;
float final_ao = occlusion_compute(sd.N, viewPosition, ao, rand.rg, bent_normal);
/* Get Brdf intensity */
vec2 uv = lut_coords(dot(sd.N, sd.V), roughness);
vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;
out_light += spec_accum.rgb * F_ibl(f0, brdf_lut) * specular_occlusion(dot(sd.N, sd.V), final_ao, roughness) * float(specToggle);
/* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */
/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
vec4 diff_accum = vec4(0.0);
/* Start at 1 because 0 is world irradiance */
for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) {
GridData gd = grids_data[i];
vec3 localpos = (gd.localmat * vec4(sd.W, 1.0)).xyz;
float fade = min(1.0, min_v3(1.0 - abs(localpos)));
fade = saturate(fade * gd.g_atten_scale + gd.g_atten_bias);
vec3 localpos;
float fade = probe_attenuation_grid(gd, worldPosition, localpos);
if (fade > 0.0) {
localpos = localpos * 0.5 + 0.5;
localpos = localpos * vec3(gd.g_resolution) - 0.5;
vec3 localpos_floored = floor(localpos);
vec3 trilinear_weight = fract(localpos);
float weight_accum = 0.0;
vec3 irradiance_accum = vec3(0.0);
/* For each neighboor cells */
for (int i = 0; i < 8; ++i) {
ivec3 offset = ivec3(i, i >> 1, i >> 2) & ivec3(1);
vec3 cell_cos = clamp(localpos_floored + vec3(offset), vec3(0.0), vec3(gd.g_resolution) - 1.0);
/* We need this because we render probes in world space (so we need light vector in WS).
* And rendering them in local probe space is too much problem. */
vec3 ws_cell_location = gd.g_corner +
(gd.g_increment_x * cell_cos.x +
gd.g_increment_y * cell_cos.y +
gd.g_increment_z * cell_cos.z);
vec3 ws_point_to_cell = ws_cell_location - sd.W;
vec3 ws_light = normalize(ws_point_to_cell);
vec3 trilinear = mix(1 - trilinear_weight, trilinear_weight, offset);
float weight = trilinear.x * trilinear.y * trilinear.z;
/* Smooth backface test */
// weight *= sqrt(max(0.002, dot(ws_light, sd.N)));
/* Avoid zero weight */
weight = max(0.00001, weight);
vec3 color = get_cell_color(ivec3(cell_cos), gd.g_resolution, gd.g_offset, bent_normal);
weight_accum += weight;
irradiance_accum += color * weight;
}
vec3 indirect_diffuse = irradiance_accum / weight_accum;
float influ_diff = min(fade, (1.0 - diff_accum.a));
diff_accum.rgb += indirect_diffuse * influ_diff;
diff_accum.a += influ_diff;
/* For Debug purpose */
// return texture(irradianceGrid, sd.W.xy).rgb;
vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos);
accumulate_light(diff, fade, diff_accum);
}
}
/* World probe */
/* World Diffuse */
if (diff_accum.a < 1.0 && grid_count > 0) {
IrradianceData ir_data = load_irradiance_cell(0, bent_normal);
vec3 diff = compute_irradiance(bent_normal, ir_data);
diff_accum.rgb += diff * (1.0 - diff_accum.a);
vec3 diff = probe_evaluate_world_diff(bent_normal);
accumulate_light(diff, 1.0, diff_accum);
}
if (spec_accum.a < 1.0) {
ProbeData pd = probes_data[0];
out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo);
vec3 spec = textureLod_octahedron(probeCubes, vec4(spec_dir, 0), roughness * lodMax, lodMax).rgb;
spec_accum.rgb += spec * (1.0 - spec_accum.a);
}
vec3 indirect_radiance =
spec_accum.rgb * F_ibl(f0, brdf_lut) * float(specToggle) * specular_occlusion(dot(sd.N, sd.V), final_ao, roughness) +
diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo);
return radiance + indirect_radiance;
return out_light;
}