screen_space_reflection.glsl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. layout(rgba16f, set = 0, binding = 0) uniform restrict readonly image2D source_diffuse;
  6. layout(r32f, set = 0, binding = 1) uniform restrict readonly image2D source_depth;
  7. layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly image2D ssr_image;
  8. #ifdef MODE_ROUGH
  9. layout(r8, set = 1, binding = 1) uniform restrict writeonly image2D blur_radius_image;
  10. #endif
  11. layout(rgba8, set = 2, binding = 0) uniform restrict readonly image2D source_normal_roughness;
  12. layout(set = 3, binding = 0) uniform sampler2D source_metallic;
  13. layout(push_constant, std430) uniform Params {
  14. vec4 proj_info;
  15. ivec2 screen_size;
  16. float camera_z_near;
  17. float camera_z_far;
  18. int num_steps;
  19. float depth_tolerance;
  20. float distance_fade;
  21. float curve_fade_in;
  22. bool orthogonal;
  23. float filter_mipmap_levels;
  24. bool use_half_res;
  25. uint view_index;
  26. }
  27. params;
  28. #include "screen_space_reflection_inc.glsl"
  29. vec2 view_to_screen(vec3 view_pos, out float w) {
  30. vec4 projected = scene_data.projection[params.view_index] * vec4(view_pos, 1.0);
  31. projected.xyz /= projected.w;
  32. projected.xy = projected.xy * 0.5 + 0.5;
  33. w = projected.w;
  34. return projected.xy;
  35. }
  36. #define M_PI 3.14159265359
  37. void main() {
  38. // Pixel being shaded
  39. ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
  40. if (any(greaterThanEqual(ssC.xy, params.screen_size))) { //too large, do nothing
  41. return;
  42. }
  43. vec2 pixel_size = 1.0 / vec2(params.screen_size);
  44. vec2 uv = vec2(ssC.xy) * pixel_size;
  45. uv += pixel_size * 0.5;
  46. float base_depth = imageLoad(source_depth, ssC).r;
  47. // World space point being shaded
  48. vec3 vertex = reconstructCSPosition(uv * vec2(params.screen_size), base_depth);
  49. vec4 normal_roughness = imageLoad(source_normal_roughness, ssC);
  50. vec3 normal = normal_roughness.xyz * 2.0 - 1.0;
  51. float roughness = normal_roughness.w;
  52. // The roughness cutoff of 0.6 is chosen to match the roughness fadeout from GH-69828.
  53. if (roughness > 0.6) {
  54. // Do not compute SSR for rough materials to improve performance at the cost of
  55. // subtle artifacting.
  56. #ifdef MODE_ROUGH
  57. imageStore(blur_radius_image, ssC, vec4(0.0));
  58. #endif
  59. imageStore(ssr_image, ssC, vec4(0.0));
  60. return;
  61. }
  62. normal = normalize(normal);
  63. normal.y = -normal.y; //because this code reads flipped
  64. vec3 view_dir;
  65. if (sc_multiview) {
  66. view_dir = normalize(vertex + scene_data.eye_offset[params.view_index].xyz);
  67. } else {
  68. view_dir = normalize(vertex);
  69. }
  70. vec3 ray_dir = normalize(reflect(view_dir, normal));
  71. if (dot(ray_dir, normal) < 0.001) {
  72. imageStore(ssr_image, ssC, vec4(0.0));
  73. return;
  74. }
  75. ////////////////
  76. // make ray length and clip it against the near plane (don't want to trace beyond visible)
  77. float ray_len = (vertex.z + ray_dir.z * params.camera_z_far) > -params.camera_z_near ? (-params.camera_z_near - vertex.z) / ray_dir.z : params.camera_z_far;
  78. vec3 ray_end = vertex + ray_dir * ray_len;
  79. float w_begin;
  80. vec2 vp_line_begin = view_to_screen(vertex, w_begin);
  81. float w_end;
  82. vec2 vp_line_end = view_to_screen(ray_end, w_end);
  83. vec2 vp_line_dir = vp_line_end - vp_line_begin;
  84. // we need to interpolate w along the ray, to generate perspective correct reflections
  85. w_begin = 1.0 / w_begin;
  86. w_end = 1.0 / w_end;
  87. float z_begin = vertex.z * w_begin;
  88. float z_end = ray_end.z * w_end;
  89. vec2 line_begin = vp_line_begin / pixel_size;
  90. vec2 line_dir = vp_line_dir / pixel_size;
  91. float z_dir = z_end - z_begin;
  92. float w_dir = w_end - w_begin;
  93. // clip the line to the viewport edges
  94. float scale_max_x = min(1.0, 0.99 * (1.0 - vp_line_begin.x) / max(1e-5, vp_line_dir.x));
  95. float scale_max_y = min(1.0, 0.99 * (1.0 - vp_line_begin.y) / max(1e-5, vp_line_dir.y));
  96. float scale_min_x = min(1.0, 0.99 * vp_line_begin.x / max(1e-5, -vp_line_dir.x));
  97. float scale_min_y = min(1.0, 0.99 * vp_line_begin.y / max(1e-5, -vp_line_dir.y));
  98. float line_clip = min(scale_max_x, scale_max_y) * min(scale_min_x, scale_min_y);
  99. line_dir *= line_clip;
  100. z_dir *= line_clip;
  101. w_dir *= line_clip;
  102. // clip z and w advance to line advance
  103. vec2 line_advance = normalize(line_dir); // down to pixel
  104. float step_size = 1.0 / length(line_dir);
  105. float z_advance = z_dir * step_size; // adapt z advance to line advance
  106. float w_advance = w_dir * step_size; // adapt w advance to line advance
  107. // make line advance faster if direction is closer to pixel edges (this avoids sampling the same pixel twice)
  108. float advance_angle_adj = 1.0 / max(abs(line_advance.x), abs(line_advance.y));
  109. line_advance *= advance_angle_adj; // adapt z advance to line advance
  110. z_advance *= advance_angle_adj;
  111. w_advance *= advance_angle_adj;
  112. vec2 pos = line_begin;
  113. float z = z_begin;
  114. float w = w_begin;
  115. float z_from = z / w;
  116. float z_to = z_from;
  117. float depth;
  118. vec2 prev_pos = pos;
  119. if (ivec2(pos + line_advance - 0.5) == ssC) {
  120. // It is possible for rounding to cause our first pixel to check to be the pixel we're reflecting.
  121. // Make sure we skip it
  122. pos += line_advance;
  123. z += z_advance;
  124. w += w_advance;
  125. }
  126. bool found = false;
  127. float steps_taken = 0.0;
  128. for (int i = 0; i < params.num_steps; i++) {
  129. pos += line_advance;
  130. z += z_advance;
  131. w += w_advance;
  132. // convert to linear depth
  133. ivec2 test_pos = ivec2(pos - 0.5);
  134. depth = imageLoad(source_depth, test_pos).r;
  135. if (sc_multiview) {
  136. depth = depth * 2.0 - 1.0;
  137. depth = 2.0 * params.camera_z_near * params.camera_z_far / (params.camera_z_far + params.camera_z_near - depth * (params.camera_z_far - params.camera_z_near));
  138. depth = -depth;
  139. }
  140. z_from = z_to;
  141. z_to = z / w;
  142. if (depth > z_to) {
  143. // Test if our ray is hitting the "right" side of the surface, if not we're likely self reflecting and should skip.
  144. vec4 test_normal_roughness = imageLoad(source_normal_roughness, test_pos);
  145. vec3 test_normal = test_normal_roughness.xyz * 2.0 - 1.0;
  146. test_normal = normalize(test_normal);
  147. test_normal.y = -test_normal.y; // Because this code reads flipped.
  148. if (dot(ray_dir, test_normal) < 0.001) {
  149. // if depth was surpassed
  150. if (depth <= max(z_to, z_from) + params.depth_tolerance && -depth < params.camera_z_far * 0.95) {
  151. // check the depth tolerance and far clip
  152. // check that normal is valid
  153. found = true;
  154. }
  155. break;
  156. }
  157. }
  158. steps_taken += 1.0;
  159. prev_pos = pos;
  160. }
  161. if (found) {
  162. float margin_blend = 1.0;
  163. vec2 final_pos = pos;
  164. vec2 margin = vec2((params.screen_size.x + params.screen_size.y) * 0.05); // make a uniform margin
  165. if (any(bvec4(lessThan(pos, vec2(0.0, 0.0)), greaterThan(pos, params.screen_size)))) {
  166. // clip at the screen edges
  167. imageStore(ssr_image, ssC, vec4(0.0));
  168. return;
  169. }
  170. {
  171. //blend fading out towards inner margin
  172. // 0.5 = midpoint of reflection
  173. vec2 margin_grad = mix(params.screen_size - pos, pos, lessThan(pos, params.screen_size * 0.5));
  174. margin_blend = smoothstep(0.0, margin.x * margin.y, margin_grad.x * margin_grad.y);
  175. //margin_blend = 1.0;
  176. }
  177. // Fade In / Fade Out
  178. float grad = (steps_taken + 1.0) / float(params.num_steps);
  179. float initial_fade = params.curve_fade_in == 0.0 ? 1.0 : pow(clamp(grad, 0.0, 1.0), params.curve_fade_in);
  180. float fade = pow(clamp(1.0 - grad, 0.0, 1.0), params.distance_fade) * initial_fade;
  181. // Ensure that precision errors do not introduce any fade. Even if it is just slightly below 1.0,
  182. // strong specular light can leak through the reflection.
  183. if (fade > 0.999) {
  184. fade = 1.0;
  185. }
  186. // This is an ad-hoc term to fade out the SSR as roughness increases. Values used
  187. // are meant to match the visual appearance of a ReflectionProbe.
  188. float roughness_fade = smoothstep(0.4, 0.7, 1.0 - normal_roughness.w);
  189. // Schlick term.
  190. float metallic = texelFetch(source_metallic, ssC << 1, 0).w;
  191. // F0 is the reflectance of normally incident light (perpendicular to the surface).
  192. // Dielectric materials have a widely accepted default value of 0.04. We assume that metals reflect all light, so their F0 is 1.0.
  193. float f0 = mix(0.04, 1.0, metallic);
  194. float m = clamp(1.0 - dot(normal, -view_dir), 0.0, 1.0);
  195. float m2 = m * m;
  196. m = m2 * m2 * m; // pow(m,5)
  197. float fresnel_term = f0 + (1.0 - f0) * m; // Fresnel Schlick term.
  198. // The alpha value of final_color controls the blending with specular light in specular_merge.glsl.
  199. // Note that the Fresnel term is multiplied with the RGB color instead of being a part of the alpha value.
  200. // There is a key difference:
  201. // - multiplying a term with RGB darkens the SSR light without introducing/taking away specular light.
  202. // - combining a term into the Alpha value introduces specular light at the expense of the SSR light.
  203. vec4 final_color = vec4(imageLoad(source_diffuse, ivec2(final_pos - 0.5)).rgb * fresnel_term, fade * margin_blend * roughness_fade);
  204. imageStore(ssr_image, ssC, final_color);
  205. #ifdef MODE_ROUGH
  206. // if roughness is enabled, do screen space cone tracing
  207. float blur_radius = 0.0;
  208. if (roughness > 0.001) {
  209. float cone_angle = min(roughness, 0.999) * M_PI * 0.5;
  210. float cone_len = length(final_pos - line_begin);
  211. float op_len = 2.0 * tan(cone_angle) * cone_len; // opposite side of iso triangle
  212. {
  213. // fit to sphere inside cone (sphere ends at end of cone), something like this:
  214. // ___
  215. // \O/
  216. // V
  217. //
  218. // as it avoids bleeding from beyond the reflection as much as possible. As a plus
  219. // it also makes the rough reflection more elongated.
  220. float a = op_len;
  221. float h = cone_len;
  222. float a2 = a * a;
  223. float fh2 = 4.0f * h * h;
  224. blur_radius = (a * (sqrt(a2 + fh2) - a)) / (4.0f * h);
  225. }
  226. }
  227. imageStore(blur_radius_image, ssC, vec4(blur_radius / 255.0)); //stored in r8
  228. #endif // MODE_ROUGH
  229. } else {
  230. #ifdef MODE_ROUGH
  231. imageStore(blur_radius_image, ssC, vec4(0.0));
  232. #endif
  233. imageStore(ssr_image, ssC, vec4(0.0));
  234. }
  235. }