-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
34 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
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
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,123 @@ | ||
#version 450 | ||
|
||
#extension GL_GOOGLE_include_directive : enable | ||
#extension GL_ARB_separate_shader_objects : enable | ||
#extension GL_EXT_samplerless_texture_functions : enable | ||
// #extension GL_KHR_memory_scope_semantics : enable | ||
|
||
#include "virtual_shadow/vsm_common.glsl" | ||
#include "lighting/tonemapping.glsl" | ||
#include "scene.glsl" | ||
#include "common.glsl" | ||
|
||
layout(local_size_x = 32, local_size_y = 32) in; | ||
|
||
const uint NumThreads = gl_WorkGroupSize.x*gl_WorkGroupSize.y*gl_WorkGroupSize.z; | ||
|
||
layout(binding = 0, std140) uniform UboScene { | ||
SceneDesc scene; | ||
}; | ||
layout(binding = 1) uniform texture2D gbufDiffuse; | ||
layout(binding = 2) uniform utexture2D gbufNormal; | ||
layout(binding = 3) uniform texture2D gbufDepth; | ||
layout(binding = 4, r32ui) uniform uimage3D pageTbl; | ||
layout(binding = 5, r32ui) uniform uimage3D pageTblDepth; | ||
// layout(binding = 6, r32ui) uniform writeonly restrict uimage2D occlusionLut; | ||
|
||
const float dFogMin = 0; | ||
const float dFogMax = 0.9999; | ||
|
||
ivec2 fragCoord = ivec2(gl_GlobalInvocationID.xy); | ||
|
||
shared uint tileDepthMin, tileDepthMax; | ||
|
||
void markPage(ivec3 at, float z) { | ||
imageAtomicExchange(pageTbl, at, 1u); | ||
imageAtomicMin(pageTblDepth, at, floatBitsToUint(z)); | ||
} | ||
|
||
void hiTile(float depth) { | ||
uint d = floatBitsToUint(depth); | ||
atomicMax(tileDepthMax, d); | ||
atomicMin(tileDepthMin, d); | ||
} | ||
|
||
float interleavedGradientNoise() { | ||
return interleavedGradientNoise(fragCoord.xy); | ||
} | ||
|
||
vec3 project(mat4 m, vec3 pos) { | ||
vec4 p = m*vec4(pos,1); | ||
return p.xyz/p.w; | ||
} | ||
|
||
void shadowFactor(vec4 shPos) { | ||
vec3 shPos0 = shPos.xyz/shPos.w; | ||
|
||
int mip = vsmCalcMipIndex(shPos0.xy, VSM_FOG_MIP); | ||
vec2 page = shPos0.xy / (1 << mip); | ||
if(any(greaterThan(abs(page), vec2(1)))) | ||
return; | ||
|
||
ivec2 pageI = ivec2((page*0.5+0.5)*VSM_PAGE_TBL_SIZE); | ||
ivec3 at = ivec3(pageI, mip); | ||
markPage(at, shPos0.z); | ||
} | ||
|
||
void fogPages(const float depthMax) { | ||
const int steps = 32; | ||
const uint lane = gl_LocalInvocationIndex; | ||
const float noise = interleavedGradientNoise()/steps; | ||
const ivec2 size = textureSize(gbufDepth, 0).xy; | ||
|
||
const vec2 frPos = vec2(gl_WorkGroupID.xy*gl_WorkGroupSize.xy) + vec2(gl_WorkGroupSize.xy)*0.5; | ||
const vec2 inPos = (vec2(frPos+vec2(0.5))/vec2(size))*2.0 - vec2(1.0); | ||
|
||
const vec3 pos0 = project(scene.viewProjectLwcInv, vec3(inPos, dFogMin)); | ||
const vec3 pos1 = project(scene.viewProjectLwcInv, vec3(inPos, dFogMax)); | ||
const vec3 posz = project(scene.viewProjectLwcInv, vec3(inPos, depthMax)); | ||
|
||
const vec4 shPos0 = scene.viewVirtualShadowLwc*vec4(pos0, 1); | ||
const vec4 shPos1 = scene.viewVirtualShadowLwc*vec4(posz, 1); | ||
|
||
const vec3 ray = pos1.xyz - pos0.xyz; | ||
const float dist = length(ray)*0.01; // meters | ||
const float distZ = length(posz-pos0)*0.01; // meters | ||
|
||
if(lane < steps) { | ||
float t = (lane+0.3)/float(steps); | ||
float dd = (t*distZ)/(dist); | ||
vec4 shPos = mix(shPos0, shPos1, t+noise); | ||
|
||
shadowFactor(shPos); | ||
} | ||
/* | ||
uint occlusion = 0; | ||
[[dont_unroll]] | ||
for(uint i=0; i<steps; ++i) { | ||
float t = (i+0.3)/float(steps); | ||
float dd = (t*distZ)/(dist); | ||
vec4 shPos = mix(shPos0,shPos1,t+noise); | ||
bool shadow = shadowFactor(shPos); | ||
occlusion = occlusion | ((shadow ? 1u : 0u) << uint(i)); | ||
} | ||
imageStore(occlusionLut, ivec2(invocationID.xy), uvec4(occlusion)); | ||
*/ | ||
} | ||
|
||
void main() { | ||
tileDepthMin = floatBitsToUint(1); | ||
tileDepthMax = floatBitsToUint(0); | ||
barrier(); | ||
|
||
const ivec2 imgSize = ivec2(textureSize(gbufDepth, 0)); | ||
const bool valid = all(lessThan(fragCoord, imgSize)); | ||
// const vec3 normal = valid ? normalFetch(gbufNormal, pix) : vec3(0); | ||
const float depth = valid ? texelFetch(gbufDepth, fragCoord, 0).x : 1; | ||
|
||
if(valid) | ||
hiTile(depth); | ||
barrier(); | ||
|
||
fogPages(uintBitsToFloat(tileDepthMax)); | ||
} |