Eevee: SSR: Add roughness random rays.

This commit is contained in:
2017-07-19 15:39:37 +02:00
parent 7938848b63
commit b1a8803c24
3 changed files with 37 additions and 15 deletions

View File

@@ -90,6 +90,7 @@ static struct {
} e_data = {NULL}; /* Engine data */
extern char datatoc_bsdf_common_lib_glsl[];
extern char datatoc_bsdf_sampling_lib_glsl[];
extern char datatoc_octahedron_lib_glsl[];
extern char datatoc_effect_ssr_frag_glsl[];
extern char datatoc_effect_minmaxz_frag_glsl[];
@@ -178,6 +179,7 @@ void EEVEE_effects_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
if (!e_data.motion_blur_sh) {
DynStr *ds_frag = BLI_dynstr_new();
BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_bsdf_sampling_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_octahedron_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_lightprobe_lib_glsl);
BLI_dynstr_append(ds_frag, datatoc_raytrace_lib_glsl);
@@ -683,7 +685,8 @@ void EEVEE_effects_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
DRWShadingGroup *grp = DRW_shgroup_create(e_data.ssr_raytrace_sh, psl->ssr_raytrace);
DRW_shgroup_uniform_buffer(grp, "depthBuffer", &e_data.depth_src);
DRW_shgroup_uniform_buffer(grp, "normalBuffer", &txl->ssr_normal_input);
DRW_shgroup_uniform_buffer(grp, "specRoughBuffer", &txl->ssr_specrough_input);
DRW_shgroup_uniform_buffer(grp, "specroughBuffer", &txl->ssr_specrough_input);
DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
DRW_shgroup_uniform_vec4(grp, "viewvecs[0]", (float *)stl->g_data->viewvecs, 2);
DRW_shgroup_uniform_mat4(grp, "PixelProjMatrix", (float *)&e_data.pixelprojmat);
DRW_shgroup_call_add(grp, quad, NULL);
@@ -693,6 +696,7 @@ void EEVEE_effects_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
DRW_shgroup_uniform_buffer(grp, "depthBuffer", &e_data.depth_src);
DRW_shgroup_uniform_buffer(grp, "normalBuffer", &txl->ssr_normal_input);
DRW_shgroup_uniform_buffer(grp, "specroughBuffer", &txl->ssr_specrough_input);
DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
DRW_shgroup_uniform_buffer(grp, "colorBuffer", &txl->color_double_buffer);
DRW_shgroup_uniform_buffer(grp, "hitBuffer", &txl->ssr_hit_output);
DRW_shgroup_uniform_buffer(grp, "pdfBuffer", &txl->ssr_pdf_output);

View File

@@ -6,11 +6,14 @@ uniform float invSampleCount;
vec2 jitternoise = vec2(0.0);
#ifdef NOISE_SIZE
void setup_noise(void)
{
jitternoise = texture(texJitter, gl_FragCoord.xy / NOISE_SIZE).rg; /* Global variable */
}
#endif
#ifdef HAMMERSLEY_SIZE
vec3 hammersley_3d(float i, float invsamplenbr)
{
vec3 Xi; /* Theta, cos(Phi), sin(Phi) */
@@ -29,6 +32,7 @@ vec3 hammersley_3d(float i)
{
return hammersley_3d(i, invSampleCount);
}
#endif
/* -------------- BSDFS -------------- */
@@ -42,22 +46,26 @@ float pdf_hemisphere()
return 0.5 * M_1_PI;
}
vec3 sample_ggx(float nsample, float a2, vec3 N, vec3 T, vec3 B)
vec3 sample_ggx(vec3 rand, float a2, vec3 N, vec3 T, vec3 B)
{
vec3 Xi = hammersley_3d(nsample);
/* Theta is the aperture angle of the cone */
float z = sqrt( (1.0 - Xi.x) / ( 1.0 + a2 * Xi.x - Xi.x ) ); /* cos theta */
float z = sqrt( (1.0 - rand.x) / ( 1.0 + a2 * rand.x - rand.x ) ); /* cos theta */
float r = sqrt( 1.0 - z * z ); /* sin theta */
float x = r * Xi.y;
float y = r * Xi.z;
float x = r * rand.y;
float y = r * rand.z;
/* Microfacet Normal */
vec3 Ht = vec3(x, y, z);
return tangent_to_world(Ht, N, T, B);
}
#ifdef HAMMERSLEY_SIZE
vec3 sample_ggx(float nsample, float a2, vec3 N, vec3 T, vec3 B)
{
vec3 Xi = hammersley_3d(nsample);
return sample_ggx(Xi, a2, N, T, B);
}
vec3 sample_hemisphere(float nsample, vec3 N, vec3 T, vec3 B)
{
vec3 Xi = hammersley_3d(nsample);
@@ -70,4 +78,5 @@ vec3 sample_hemisphere(float nsample, vec3 N, vec3 T, vec3 B)
vec3 Ht = vec3(x, y, z);
return tangent_to_world(Ht, N, T, B);
}
}
#endif

View File

@@ -1,7 +1,16 @@
vec3 generate_ray(vec3 V, vec3 N)
#ifndef UTIL_TEX
#define UTIL_TEX
uniform sampler2DArray utilTex;
#endif /* UTIL_TEX */
vec3 generate_ray(ivec2 pix, vec3 V, vec3 N, float roughnessSquared)
{
return -reflect(-V, N);
vec3 T, B;
make_orthonormal_basis(N, T, B); /* Generate tangent space */
vec3 rand = texelFetch(utilTex, ivec3(pix % LUT_SIZE, 2), 0).rba;
vec3 H = sample_ggx(rand, roughnessSquared, N, T, B); /* Microfacet normal */
return -reflect(-V, H);
}
#ifdef STEP_RAYTRACE
@@ -19,7 +28,7 @@ void main()
ivec2 halfres_texel = ivec2(gl_FragCoord.xy);
float depth = texelFetch(depthBuffer, fullres_texel, 0).r;
/* Early discard */
/* Early out */
if (depth == 1.0)
discard;
@@ -36,7 +45,7 @@ void main()
float roughnessSquared = roughness * roughness;
/* Generate Ray */
vec3 R = generate_ray(V, N);
vec3 R = generate_ray(halfres_texel, V, N, roughnessSquared);
/* Search for the planar reflection affecting this pixel */
/* If no planar is found, fallback to screen space */
@@ -164,7 +173,7 @@ void main()
float depth = textureLod(depthBuffer, uvs, 0.0).r;
/* Early discard */
/* Early out */
if (depth == 1.0)
discard;
@@ -182,7 +191,7 @@ void main()
/* We generate the same rays that has been generated in the raycast step.
* But we add this ray from our resolve pixel position, increassing accuracy. */
vec3 R = generate_ray(-V, N);
vec3 R = generate_ray(halfres_texel, -V, N, roughnessSquared);
float ray_length = texelFetch(hitBuffer, halfres_texel, 0).r;
if (ray_length != -1.0) {