-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day 26: Initial work on shadow noise/blurring
We are now using a sketchy noise function to jitter the rays we cast for shadows, and a two-pass bilateral filter to blur the results. The results are a little nicer in some cases, but also noisy in others, so there's clearly room for improvement.
- Loading branch information
Showing
4 changed files
with
222 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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,59 @@ | ||
#version 460 | ||
|
||
#extension GL_GOOGLE_include_directive: require | ||
#extension GL_EXT_ray_query: require | ||
|
||
#include "math.h" | ||
|
||
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; | ||
|
||
struct ShadeData | ||
{ | ||
vec3 cameraPosition; | ||
vec3 sunDirection; | ||
|
||
mat4 inverseViewProjection; | ||
|
||
vec2 imageSize; | ||
}; | ||
|
||
layout(push_constant) uniform block | ||
{ | ||
ShadeData shadeData; | ||
}; | ||
|
||
layout(binding = 0) uniform writeonly image2D outImage; | ||
|
||
layout(binding = 1) uniform sampler2D depthImage; | ||
layout(binding = 2) uniform accelerationStructureEXT tlas; | ||
|
||
void main() | ||
{ | ||
uvec2 pos = gl_GlobalInvocationID.xy; | ||
vec2 uv = (vec2(pos) + 0.5) / shadeData.imageSize; | ||
|
||
float depth = texture(depthImage, uv).r; | ||
|
||
vec4 clip = vec4(uv.x * 2 - 1, 1 - uv.y * 2, depth, 1); | ||
vec4 wposh = shadeData.inverseViewProjection * clip; | ||
vec3 wpos = wposh.xyz / wposh.w; | ||
|
||
uint rayflags = gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsCullNoOpaqueEXT; | ||
|
||
vec3 dir = shadeData.sunDirection; | ||
// TODO: a lot more tuning required here | ||
// TODO: this should actually be doing cone sampling, not random XZ offsets | ||
float dir0 = gradientNoise(vec2(pos.xy)); | ||
float dir1 = gradientNoise(vec2(pos.yx)); | ||
dir.x += (dir0 * 2 - 1) * 1e-2; | ||
dir.z += (dir1 * 2 - 1) * 1e-2; | ||
dir = normalize(dir); | ||
|
||
rayQueryEXT rq; | ||
rayQueryInitializeEXT(rq, tlas, rayflags, 0xff, wpos, 1e-2, dir, 1e3); | ||
rayQueryProceedEXT(rq); | ||
|
||
float shadow = (rayQueryGetIntersectionTypeEXT(rq, true) == gl_RayQueryCommittedIntersectionNoneEXT) ? 1.0 : 0.0; | ||
|
||
imageStore(outImage, ivec2(pos), vec4(shadow, 0, 0, 0)); | ||
} |
This file contains 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,57 @@ | ||
#version 460 | ||
|
||
#define BLUR 1 | ||
|
||
#extension GL_GOOGLE_include_directive: require | ||
|
||
#include "math.h" | ||
|
||
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; | ||
|
||
layout(push_constant) uniform block | ||
{ | ||
vec2 imageSize; | ||
float direction; | ||
}; | ||
|
||
layout(binding = 0) uniform writeonly image2D outImage; | ||
|
||
layout(binding = 1) uniform sampler2D shadowImage; | ||
layout(binding = 2) uniform sampler2D depthImage; | ||
|
||
void main() | ||
{ | ||
uvec2 pos = gl_GlobalInvocationID.xy; | ||
vec2 uv = (vec2(pos) + 0.5) / imageSize; | ||
|
||
#if BLUR | ||
float shadow = 0; | ||
float accumw = 0; | ||
|
||
float znear = 1; | ||
float depth = znear / texture(depthImage, uv).r; | ||
|
||
vec2 offsetScale = vec2(direction, 1 - direction) / imageSize; | ||
|
||
const int KERNEL = 10; | ||
|
||
for (int i = -KERNEL; i <= KERNEL; ++i) | ||
{ | ||
vec2 uvoff = uv + vec2(i) * offsetScale; | ||
|
||
// TODO: a lot more tuning required here | ||
float gw = exp2(-abs(i) / 10); | ||
float dv = znear / texture(depthImage, uvoff).r; | ||
float dw = exp2(-abs(depth - dv) * 20); | ||
|
||
shadow += texture(shadowImage, uvoff).r * (dw * gw); | ||
accumw += dw * gw; | ||
} | ||
|
||
shadow /= accumw; | ||
#else | ||
float shadow = texture(shadowImage, uv).r; | ||
#endif | ||
|
||
imageStore(outImage, ivec2(pos), vec4(shadow, 0, 0, 0)); | ||
} |