diff options
Diffstat (limited to 'source/blender/draw/engines/eevee/shaders/raytrace_lib.glsl')
-rw-r--r-- | source/blender/draw/engines/eevee/shaders/raytrace_lib.glsl | 380 |
1 files changed, 171 insertions, 209 deletions
diff --git a/source/blender/draw/engines/eevee/shaders/raytrace_lib.glsl b/source/blender/draw/engines/eevee/shaders/raytrace_lib.glsl index fbc13d832bd..0adfea9ca73 100644 --- a/source/blender/draw/engines/eevee/shaders/raytrace_lib.glsl +++ b/source/blender/draw/engines/eevee/shaders/raytrace_lib.glsl @@ -3,253 +3,215 @@ #pragma BLENDER_REQUIRE(common_math_lib.glsl) #pragma BLENDER_REQUIRE(common_uniforms_lib.glsl) +/** + * Screen-Space Raytracing functions. + */ + uniform sampler2D maxzBuffer; uniform sampler2DArray planarDepth; -#define MAX_STEP 256 +struct Ray { + vec3 origin; + vec3 direction; +}; -float sample_depth(vec2 uv, int index, float lod) +vec3 raytrace_offset(vec3 P, vec3 Ng) { -#ifdef PLANAR_PROBE_RAYTRACE - if (index > -1) { - return textureLod(planarDepth, vec3(uv, index), 0.0).r; - } - else { -#endif - return textureLod(maxzBuffer, uv * hizUvScale.xy, floor(lod)).r; -#ifdef PLANAR_PROBE_RAYTRACE - } -#endif + /* TODO(fclem) better offset */ + const float epsilon_f = 1e-4; + return P + epsilon_f * Ng; } -vec4 sample_depth_grouped(vec4 uv1, vec4 uv2, int index, float lod) +/* Inputs expected to be in viewspace. */ +void raytrace_clip_ray_to_near_plane(inout Ray ray) { - vec4 depths; -#ifdef PLANAR_PROBE_RAYTRACE - if (index > -1) { - depths.x = textureLod(planarDepth, vec3(uv1.xy, index), 0.0).r; - depths.y = textureLod(planarDepth, vec3(uv1.zw, index), 0.0).r; - depths.z = textureLod(planarDepth, vec3(uv2.xy, index), 0.0).r; - depths.w = textureLod(planarDepth, vec3(uv2.zw, index), 0.0).r; + float near_dist = get_view_z_from_depth(0.0); + if ((ray.origin.z + ray.direction.z) > near_dist) { + ray.direction *= abs((near_dist - ray.origin.z) / ray.direction.z); } - else { -#endif - depths.x = textureLod(maxzBuffer, uv1.xy * hizUvScale.xy, lod).r; - depths.y = textureLod(maxzBuffer, uv1.zw * hizUvScale.xy, lod).r; - depths.z = textureLod(maxzBuffer, uv2.xy * hizUvScale.xy, lod).r; - depths.w = textureLod(maxzBuffer, uv2.zw * hizUvScale.xy, lod).r; -#ifdef PLANAR_PROBE_RAYTRACE - } -#endif - return depths; } -float refine_isect(float prev_delta, float curr_delta) -{ - /** - * Simplification of 2D intersection : - * r0 = (0.0, prev_ss_ray.z); - * r1 = (1.0, curr_ss_ray.z); - * d0 = (0.0, prev_hit_depth_sample); - * d1 = (1.0, curr_hit_depth_sample); - * vec2 r = r1 - r0; - * vec2 d = d1 - d0; - * vec2 isect = ((d * cross(r1, r0)) - (r * cross(d1, d0))) / cross(r,d); - * - * We only want isect.x to know how much stride we need. So it simplifies : - * - * isect_x = (cross(r1, r0) - cross(d1, d0)) / cross(r,d); - * isect_x = (prev_ss_ray.z - prev_hit_depth_sample.z) / cross(r,d); - */ - return saturate(prev_delta / (prev_delta - curr_delta)); -} +/* Screenspace ray ([0..1] "uv" range) where direction is normalize to be as small as one + * full-resolution pixel. The ray is also clipped to all frustum sides. + */ +struct ScreenSpaceRay { + vec4 origin; + vec4 direction; + float max_time; +}; -void prepare_raycast(vec3 ray_origin, - vec3 ray_dir, - float thickness, - int index, - out vec4 ss_step, - out vec4 ss_ray, - out float max_time) +void raytrace_screenspace_ray_finalize(inout ScreenSpaceRay ray) { - /* Negate the ray direction if it goes towards the camera. - * This way we don't need to care if the projected point - * is behind the near plane. */ - float z_sign = -sign(ray_dir.z); - vec3 ray_end = ray_origin + z_sign * ray_dir; - - /* Project into screen space. */ - vec4 ss_start, ss_end; - ss_start.xyz = project_point(ProjectionMatrix, ray_origin); - ss_end.xyz = project_point(ProjectionMatrix, ray_end); - - /* We interpolate the ray Z + thickness values to check if depth is within threshold. */ - ray_origin.z -= thickness; - ray_end.z -= thickness; - ss_start.w = project_point(ProjectionMatrix, ray_origin).z; - ss_end.w = project_point(ProjectionMatrix, ray_end).z; - - /* XXX This is a hack. A better method is welcome! */ - /* We take the delta between the offsetted depth and the depth and subtract it from the ray - * depth. This will change the world space thickness appearance a bit but we can have negative - * values without worries. We cannot do this in viewspace because of the perspective division. */ - ss_start.w = 2.0 * ss_start.z - ss_start.w; - ss_end.w = 2.0 * ss_end.z - ss_end.w; - - ss_step = ss_end - ss_start; - max_time = length(ss_step.xyz); - ss_step = z_sign * ss_step / length(ss_step.xyz); - + /* Constant bias (due to depth buffer precision). Helps with self intersection. */ + /* Magic numbers for 24bits of precision. + * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ + const float bias = -2.4e-7 * 2.0; + ray.origin.zw += bias; + ray.direction.zw += bias; + + ray.direction -= ray.origin; + float ray_len_sqr = len_squared(ray.direction.xyz); /* If the line is degenerate, make it cover at least one pixel * to not have to handle zero-pixel extent as a special case later */ - if (dot(ss_step.xy, ss_step.xy) < 0.00001) { - ss_step.xy = vec2(0.0, 0.0001); + if (ray_len_sqr < 0.00001) { + ray.direction.xy = vec2(0.0, 0.0001); } - - /* Make ss_step cover one pixel. */ - ss_step /= max(abs(ss_step.x), abs(ss_step.y)); - ss_step *= (abs(ss_step.x) > abs(ss_step.y)) ? ssrPixelSize.x : ssrPixelSize.y; - + /* Make ray.direction cover one pixel. */ + bool is_more_vertical = abs(ray.direction.x) < abs(ray.direction.y); + ray.direction /= (is_more_vertical) ? abs(ray.direction.y) : abs(ray.direction.x); + ray.direction *= (is_more_vertical) ? ssrPixelSize.y : ssrPixelSize.x; /* Clip to segment's end. */ - max_time /= length(ss_step.xyz); + ray.max_time = sqrt(ray_len_sqr * safe_rcp(len_squared(ray.direction.xyz))); /* Clipping to frustum sides. */ - max_time = min(max_time, line_unit_box_intersect_dist(ss_start.xyz, ss_step.xyz)); + float clip_dist = line_unit_box_intersect_dist(ray.origin.xyz, ray.direction.xyz); + ray.max_time = min(ray.max_time, clip_dist); /* Avoid no iteration. */ - max_time = max(max_time, 1.0); + ray.max_time = max(ray.max_time, 1.1); + /* Convert to texture coords [0..1] range. */ + ray.origin = ray.origin * 0.5 + 0.5; + ray.direction *= 0.5; +} - /* Convert to texture coords. Z component included - * since this is how it's stored in the depth buffer. - * 4th component how far we are on the ray */ -#ifdef PLANAR_PROBE_RAYTRACE - /* Planar Reflections have X mirrored. */ - vec2 m = (index > -1) ? vec2(-0.5, 0.5) : vec2(0.5); -#else - const vec2 m = vec2(0.5); -#endif - ss_ray = ss_start * m.xyyy + 0.5; - ss_step *= m.xyyy; +ScreenSpaceRay raytrace_screenspace_ray_create(Ray ray) +{ + ScreenSpaceRay ssray; + ssray.origin.xyz = project_point(ProjectionMatrix, ray.origin); + ssray.direction.xyz = project_point(ProjectionMatrix, ray.origin + ray.direction); + + raytrace_screenspace_ray_finalize(ssray); + return ssray; } -/* See times_and_deltas. */ -#define curr_time times_and_deltas.x -#define prev_time times_and_deltas.y -#define curr_delta times_and_deltas.z -#define prev_delta times_and_deltas.w +ScreenSpaceRay raytrace_screenspace_ray_create(Ray ray, float thickness) +{ + ScreenSpaceRay ssray; + ssray.origin.xyz = project_point(ProjectionMatrix, ray.origin); + ssray.direction.xyz = project_point(ProjectionMatrix, ray.origin + ray.direction); + /* Interpolate thickness in screen space. + * Calculate thickness further away to avoid near plane clipping issues. */ + ssray.origin.w = get_depth_from_view_z(ray.origin.z - thickness) * 2.0 - 1.0; + ssray.direction.w = get_depth_from_view_z(ray.origin.z + ray.direction.z - thickness) * 2.0 - + 1.0; + + raytrace_screenspace_ray_finalize(ssray); + return ssray; +} -// #define GROUPED_FETCHES /* is still slower, need to see where is the bottleneck. */ -/* Return the hit position, and negate the z component (making it positive) if not hit occurred. */ +struct RayTraceParameters { + /** ViewSpace thickness the objects */ + float thickness; + /** Jitter along the ray to avoid banding artifact when steps are too large. */ + float jitter; + /** Determine how fast the sample steps are getting bigger. */ + float trace_quality; + /** Determine how we can use lower depth mipmaps to make the tracing faster. */ + float roughness; +}; + +/* Return true on hit. */ /* __ray_dir__ is the ray direction premultiplied by its maximum length */ -vec3 raycast(int index, - vec3 ray_origin, - vec3 ray_dir, - float thickness, - float ray_jitter, - float trace_quality, - float roughness, - const bool discard_backface) +/* TODO fclem remove the backface check and do it the SSR resolve code. */ +bool raytrace(Ray ray, + RayTraceParameters params, + const bool discard_backface, + out vec3 hit_position) { - vec4 ss_step, ss_start; - float max_time; - prepare_raycast(ray_origin, ray_dir, thickness, index, ss_step, ss_start, max_time); - - float max_trace_time = max(0.01, max_time - 0.01); + /* Clip to near plane for perspective view where there is a singularity at the camera origin. */ + if (ProjectionMatrix[3][3] == 0.0) { + raytrace_clip_ray_to_near_plane(ray); + } -#ifdef GROUPED_FETCHES - ray_jitter *= 0.25; -#endif + ScreenSpaceRay ssray = raytrace_screenspace_ray_create(ray, params.thickness); - /* x : current_time, y: previous_time, z: current_delta, w: previous_delta */ - vec4 times_and_deltas = vec4(0.0); + float prev_delta = 0.0, prev_time = 0.0; + float depth_sample = get_depth_from_view_z(ray.origin.z); + float delta = depth_sample - ssray.origin.z; - float ray_time = 0.0; - float depth_sample = sample_depth(ss_start.xy, index, 0.0); - curr_delta = depth_sample - ss_start.z; + float lod_fac = saturate(fast_sqrt(params.roughness) * 2.0 - 0.4); - float lod_fac = saturate(fast_sqrt(roughness) * 2.0 - 0.4); + /* Cross at least one pixel. */ + float t = 1.001, time = 1.001; bool hit = false; - float iter; - for (iter = 1.0; !hit && (ray_time < max_time) && (iter < MAX_STEP); iter++) { - float stride = max(1.01, iter * trace_quality); - float lod = log2(stride * 0.5 * trace_quality) * lod_fac; - ray_time += stride; - - /* Save previous values. */ - times_and_deltas.xyzw = times_and_deltas.yxwz; - -#ifdef GROUPED_FETCHES - stride *= 4.0; - vec4 jit_stride = mix(vec4(2.0), vec4(stride), vec4(0.0, 0.25, 0.5, 0.75) + ray_jitter); - - vec4 times = min(vec4(ray_time) + jit_stride, vec4(max_trace_time)); - - vec4 uv1 = ss_start.xyxy + ss_step.xyxy * times.xxyy; - vec4 uv2 = ss_start.xyxy + ss_step.xyxy * times.zzww; - - vec4 depth_samples = sample_depth_grouped(uv1, uv2, index, lod); - - vec4 ray_z = ss_start.zzzz + ss_step.zzzz * times.xyzw; - vec4 ray_w = ss_start.wwww + ss_step.wwww * vec4(prev_time, times.xyz); - - vec4 deltas = depth_samples - ray_z; - /* Same as component wise (curr_delta <= 0.0) && (prev_w <= depth_sample). */ - bvec4 test = equal(step(deltas, vec4(0.0)) * step(ray_w, depth_samples), vec4(1.0)); - hit = any(test); - - if (hit) { - vec2 m = vec2(1.0, 0.0); /* Mask */ - - vec4 ret_times_and_deltas = times.wzzz * m.xxyy + deltas.wwwz * m.yyxx; - ret_times_and_deltas = (test.z) ? times.zyyy * m.xxyy + deltas.zzzy * m.yyxx : - ret_times_and_deltas; - ret_times_and_deltas = (test.y) ? times.yxxx * m.xxyy + deltas.yyyx * m.yyxx : - ret_times_and_deltas; - times_and_deltas = (test.x) ? times.xxxx * m.xyyy + deltas.xxxx * m.yyxy + - times_and_deltas.yyww * m.yxyx : - ret_times_and_deltas; - - depth_sample = depth_samples.w; - depth_sample = (test.z) ? depth_samples.z : depth_sample; - depth_sample = (test.y) ? depth_samples.y : depth_sample; - depth_sample = (test.x) ? depth_samples.x : depth_sample; - } - else { - curr_time = times.w; - curr_delta = deltas.w; - } -#else - float jit_stride = mix(2.0, stride, ray_jitter); - - curr_time = min(ray_time + jit_stride, max_trace_time); - vec4 ss_ray = ss_start + ss_step * curr_time; - - depth_sample = sample_depth(ss_ray.xy, index, lod); - - float prev_w = ss_start.w + ss_step.w * prev_time; - curr_delta = depth_sample - ss_ray.z; - hit = (curr_delta <= 0.0) && (prev_w <= depth_sample); -#endif - } + const float max_steps = 255.0; + for (float iter = 1.0; !hit && (time < ssray.max_time) && (iter < max_steps); iter++) { + float stride = 1.0 + iter * params.trace_quality; + float lod = log2(stride) * lod_fac; - /* Discard backface hits. Only do this if the ray traveled enough to avoid losing intricate - * contact reflections. This is only used for SSReflections. */ - if (discard_backface && prev_delta < 0.0 && curr_time > 4.1) { - hit = false; - } + prev_time = time; + prev_delta = delta; + time = min(t + stride * params.jitter, ssray.max_time); + t += stride; + + vec4 ss_p = ssray.origin + ssray.direction * time; + depth_sample = textureLod(maxzBuffer, ss_p.xy * hizUvScale.xy, floor(lod)).r; + + delta = depth_sample - ss_p.z; + /* Check if the ray is below the surface ... */ + hit = (delta < 0.0); + /* ... and above it with the added thickness. */ + hit = hit && (delta > ss_p.z - ss_p.w); + } + /* Discard backface hits. */ + hit = hit && !(discard_backface && prev_delta < 0.0); /* Reject hit if background. */ hit = hit && (depth_sample != 1.0); + /* Refine hit using intersection between the sampled heightfield and the ray. + * This simplifies nicely to this single line. */ + time = mix(prev_time, time, saturate(prev_delta / (prev_delta - delta))); + + hit_position = ssray.origin.xyz + ssray.direction.xyz * time; + + return hit; +} - curr_time = (hit) ? mix(prev_time, curr_time, refine_isect(prev_delta, curr_delta)) : curr_time; - ray_time = (hit) ? curr_time : ray_time; +bool raytrace_planar(Ray ray, RayTraceParameters params, int planar_ref_id, out vec3 hit_position) +{ + /* Clip to near plane for perspective view where there is a singularity at the camera origin. */ + if (ProjectionMatrix[3][3] == 0.0) { + raytrace_clip_ray_to_near_plane(ray); + } + + ScreenSpaceRay ssray = raytrace_screenspace_ray_create(ray); + + /* Planar Reflections have X mirrored. */ + ssray.origin.x = 1.0 - ssray.origin.x; + ssray.direction.x = -ssray.direction.x; - /* Clip to frustum. */ - ray_time = max(0.001, min(ray_time, max_time - 1.5)); + float prev_delta = 0.0, prev_time = 0.0; + float depth_sample = get_depth_from_view_z(ray.origin.z); + float delta = depth_sample - ssray.origin.z; + + /* Cross at least one pixel. */ + float t = 1.001, time = 1.001; + bool hit = false; + const float max_steps = 255.0; + for (float iter = 1.0; !hit && (time < ssray.max_time) && (iter < max_steps); iter++) { + float stride = 1.0 + iter * params.trace_quality; + + prev_time = time; + prev_delta = delta; + + time = min(t + stride * params.jitter, ssray.max_time); + t += stride; + + vec4 ss_ray = ssray.origin + ssray.direction * time; + + depth_sample = texture(planarDepth, vec3(ss_ray.xy, planar_ref_id)).r; + + delta = depth_sample - ss_ray.z; + /* Check if the ray is below the surface. */ + hit = (delta < 0.0); + } + /* Reject hit if background. */ + hit = hit && (depth_sample != 1.0); + /* Refine hit using intersection between the sampled heightfield and the ray. + * This simplifies nicely to this single line. */ + time = mix(prev_time, time, saturate(prev_delta / (prev_delta - delta))); - vec4 ss_ray = ss_start + ss_step * ray_time; + hit_position = ssray.origin.xyz + ssray.direction.xyz * time; - /* Tag Z if ray failed. */ - ss_ray.z *= (hit) ? 1.0 : -1.0; - return ss_ray.xyz; + return hit; } float screen_border_mask(vec2 hit_co) |