-
-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Implement a very simple SSAO in GLES3. #109447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // S4AO (Stupid Simple Screen Space Ambient Occlusion) - Jonathan Dummer (O1S) | ||
|
|
||
| // The sample_width should be even, else the midpoint is at UV. | ||
| // Takes sample_width^2 samples in a grid, with the corners notched. | ||
| #if defined(USE_SSAO_LOW) | ||
| const int sample_width = 2; | ||
| #elif defined(USE_SSAO_HIGH) | ||
| const int sample_width = 6; | ||
| #else | ||
| const int sample_width = 4; | ||
| #endif | ||
| const int notch_01 = int(sample_width > 3); // Set to 1 to skip the corner samples, 0 to include them. | ||
| const float sample_mid = (float(sample_width) - 1.0) * 0.50001; // Can't be exactly 0.5 in case sample_width is odd. | ||
| #if defined(USE_SSAO_LOW) | ||
| const float inv_half_width = 1.0 / sample_mid; // The 2x2 sampling looks wider as all samples are at radius. | ||
| #else | ||
| const float inv_half_width = 1.7 / sample_mid; // Bake in the 1.7 scale for the random rotation. | ||
| #endif | ||
| const float average_samples = 1.0 / float(sample_width * sample_width - 4 * notch_01); // 1 / number_of_samples | ||
| const float ssao_falloff_frac = 0.25; | ||
| // Perform the SSAO. | ||
| float s4ao(vec2 UV) { | ||
| #ifdef USE_MULTIVIEW | ||
| float depth = texture(depth_buffer_array, vec3(UV, view)).r; | ||
| #else | ||
| float depth = texture(depth_buffer, UV).r; | ||
|
clayjohn marked this conversation as resolved.
|
||
| #endif | ||
| float radius = max(1e-4f, depth * ssao_radius_frac); | ||
| float inv_falloff = 1.0f / max(1e-4f, depth * ssao_falloff_frac); | ||
| // Random 2D rotation per pixel (+/-45 deg, with 0 having a lower probability). | ||
| // The random cosine vector is vec2( 0.5, -0.5 to +0.5 ) and *1.7 makes the average length ~ 1. | ||
| vec2 rcos = (inv_half_width * radius) * vec2(0.5f, fract(dot(UV, ssao_prn_UV)) - 0.5f); | ||
| vec2 rsin = rcos.yx * vec2(-1, 1); // Perpendicular to the random cosine vector. | ||
| // Grab the samples and determine the occlusion. | ||
| float occlusion = 0.0f; | ||
| vec2 base_duv = -sample_mid * rsin; | ||
| for (int j = sample_width; --j >= 0;) { | ||
| #if defined(USE_SSAO_LOW) | ||
| // Low quality uses 2x2 samples, no notching. | ||
| vec2 duv = -sample_mid * rcos + base_duv; | ||
| for (int i = sample_width; --i >= 0;) { | ||
| #else | ||
| // Will uses 4x4 or 6x6 samples, with the corners notched out. | ||
| int o = /*notch_01 &*/ int((j <= 0) || (j >= (sample_width - 1))); // Notch corners of the grid. | ||
| vec2 duv = (float(o) - sample_mid) * rcos + base_duv; | ||
| for (int i = sample_width - o - o; --i >= 0;) { | ||
| #endif | ||
| #ifdef USE_MULTIVIEW | ||
| float dz = texture(depth_buffer_array, vec3(UV + duv, view)).r - depth; | ||
| #else | ||
| float dz = texture(depth_buffer, UV + duv).r - depth; | ||
| #endif | ||
| float validity = smoothstep(1.0f, 0.0f, dz * inv_falloff); | ||
| occlusion += normalize(vec3(duv, dz)).z * validity; // How 'directly overhead' is it? | ||
| duv += rcos; // March along the rcos direction with i. | ||
| } | ||
| base_duv += rsin; // March along the rsin direction with j. | ||
| } | ||
| // Adjust the occlusion for intensity, and # samples. | ||
| occlusion *= ssao_intensity * average_samples; | ||
| occlusion = clamp(1.0f - occlusion, 0.0f, 1.0f); | ||
| return occlusion * occlusion; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // S4AO (Stupid Simple Screen Space Ambient Occlusion) - Jonathan Dummer (O1S) | ||
| // The mega version uses N concentric rings of samples. | ||
|
|
||
| #if defined(USE_SSAO_MEGA) | ||
| const int rings = 4; // Start with the outer ring. | ||
| const int samps[] = int[](24, 18, 12, 6); // ( 9, 6, 3 ) is a minimum, but I want better. | ||
| #else | ||
| const int rings = 3; // Start with the outer ring. | ||
| const int samps[] = int[](15, 10, 5, 1); // ( 9, 6, 3 ) is a minimum, but I want better. | ||
| #endif | ||
| const float average_samples = 1.0 / float(samps[0] + samps[1] * int(rings > 1) + samps[2] * int(rings > 2) + samps[3] * int(rings > 3)); | ||
| const float ssao_falloff_frac = 0.25; | ||
| // Perform the SSAO. | ||
| float s4ao(vec2 UV) { | ||
| #ifdef USE_MULTIVIEW | ||
| float depth = texture(depth_buffer_array, vec3(UV, view)).r; | ||
| #else | ||
| float depth = texture(depth_buffer, UV).r; | ||
| #endif | ||
| float inv_falloff = 1.0f / max(1e-4f, depth * ssao_falloff_frac); | ||
| // Random 2D rotation per pixel (0..1 -> parabola approximating a 180 deg arc) | ||
| float r01 = fract(dot(UV, ssao_prn_UV)); | ||
| vec2 rcos = vec2(r01 - 0.5f, 2.0f * (r01 - r01 * r01)) * (2.0f * depth * ssao_radius_frac); // 180 degrees. | ||
| vec2 rsin = rcos.yx * vec2(-1, 1); // Perpendicular to the random cosine vector. | ||
| // Grab the samples and determine the occlusion. | ||
| float occlusion = 0.0f; | ||
| float ring_shrink = 0.75f; // Shrink every ring. | ||
| for (int r = 0; r < rings; ++r) { | ||
| float dt = (6.283185307f) / float(samps[r]); | ||
| float t = float(r & 1) * 0.5f * dt; | ||
| for (int s = 0; s < samps[r]; ++s) { | ||
| vec2 duv = cos(t) * rcos + sin(t) * rsin; | ||
| #ifdef USE_MULTIVIEW | ||
| float dz = texture(depth_buffer_array, vec3(UV + duv, view)).r - depth; | ||
| #else | ||
| float dz = texture(depth_buffer, UV + duv).r - depth; | ||
| #endif | ||
| // How 'directly overhead' is it? Factor in the falloff depth. | ||
| occlusion += normalize(vec3(duv, dz)).z * smoothstep(1.0f, 0.0f, dz * inv_falloff); | ||
| t += dt; | ||
| } | ||
| // The next ring will be smaller. | ||
| rcos *= ring_shrink; | ||
| rsin *= ring_shrink; | ||
| } | ||
| // Adjust the occlusion for intensity, and # samples. | ||
| occlusion *= ssao_intensity * average_samples; | ||
| occlusion = 1.0f - clamp(occlusion, 0.0f, 1.0f); | ||
| return occlusion * occlusion; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // S4AO (Stupid Simple Screen Space Ambient Occlusion) - Jonathan Dummer (O1S) | ||
| // This micro version uses only 3 depth samples, the midpoint and a randomly-rotated, balanced pair. | ||
|
|
||
| const mediump float ssao_falloff_frac = 0.25; | ||
| // Perform the SSAO. | ||
| float s4ao(vec2 UV) { | ||
| #ifdef USE_MULTIVIEW | ||
| mediump float depth = texture(depth_buffer_array, vec3(UV, view)).r; | ||
| #else | ||
| mediump float depth = texture(depth_buffer, UV).r; | ||
| #endif | ||
| mediump float inv_falloff = 1.0f / max(1e-4f, depth * ssao_falloff_frac); | ||
| // Random 2D rotation per pixel (0..1 -> parabola approximating a 180 deg arc) | ||
| mediump float r01 = fract(dot(UV, ssao_prn_UV)); | ||
| mediump vec2 duv = vec2(r01 - 0.5f, 2.0f * (r01 - r01 * r01)) * (2.0f * depth * ssao_radius_frac); // 180 degrees. | ||
| // Grab the samples and determine the occlusion. | ||
| mediump float occlusion = 0.0f; | ||
| for (int s = 0; s < 2; ++s) { | ||
| #ifdef USE_MULTIVIEW | ||
| mediump float dz = texture(depth_buffer_array, vec3(UV + duv, view)).r - depth; | ||
| #else | ||
| mediump float dz = texture(depth_buffer, UV + duv).r - depth; | ||
| #endif | ||
| // How 'directly overhead' is it? Factor in the falloff depth. | ||
| occlusion += normalize(vec3(duv, dz)).z * mix(1.0f, 0.0f, dz * inv_falloff); | ||
| // Mirror the next sample. | ||
| duv = -duv; | ||
| } | ||
| // Adjust the occlusion for intensity, and # samples. | ||
| occlusion = 1.0f - clamp(occlusion * 0.5f * ssao_intensity, 0.0f, 1.0f); | ||
| return occlusion * occlusion; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.