From 892513f81ed68e05d4dedb99f51de5bc46cab6b2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 28 Jan 2024 02:25:11 +0000 Subject: Geometry shader for single pass shadow maps 2D array texture, 4 levels, geometry shader outputs to each layer for a specific band, single scene rendering. Pending massive tidy up. --- gfx/gl/shaders/commonShadowPoint.glsl | 5 +++-- gfx/gl/shaders/commonShadowPoint.gs | 22 ++++++++++++++++++++++ gfx/gl/shaders/directionalLight.fs | 7 ++++--- gfx/gl/shaders/shadowDynamicPoint.vs | 1 - gfx/gl/shaders/shadowDynamicPointInst.vs | 1 - gfx/gl/shaders/shadowFixedPoint.vs | 1 - 6 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 gfx/gl/shaders/commonShadowPoint.gs (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/commonShadowPoint.glsl b/gfx/gl/shaders/commonShadowPoint.glsl index 47722d6..ba4611a 100644 --- a/gfx/gl/shaders/commonShadowPoint.glsl +++ b/gfx/gl/shaders/commonShadowPoint.glsl @@ -1,7 +1,8 @@ +out vec4 vworldPos; + void main() { vec3 worldPos = model * position; - gl_Position = viewProjection * vec4(worldPos - viewPoint + modelPos, 1); - gl_Position.z = max(gl_Position.z, -1); + vworldPos = vec4(worldPos - viewPoint + modelPos, 1); } diff --git a/gfx/gl/shaders/commonShadowPoint.gs b/gfx/gl/shaders/commonShadowPoint.gs new file mode 100644 index 0000000..b008f29 --- /dev/null +++ b/gfx/gl/shaders/commonShadowPoint.gs @@ -0,0 +1,22 @@ +#version 330 core +#extension GL_ARB_viewport_array : enable + +uniform mat4 viewProjection[4]; +uniform int viewProjections; +in vec4 vworldPos[]; +layout(triangles) in; +layout(triangle_strip, max_vertices = 12) out; + +void +main() +{ + for (int vp = 0; vp < viewProjections; ++vp) { + for (int v = 0; v < vworldPos.length(); ++v) { + gl_Position = viewProjection[vp] * vworldPos[v]; + gl_Position.z = max(gl_Position.z, -1); + gl_Layer = vp; + EmitVertex(); + } + EndPrimitive(); + } +} diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index f36d83f..d0c5062 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -9,7 +9,7 @@ in vec2 TexCoords; layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; -layout(binding = 2) uniform sampler2D shadowMap; +layout(binding = 2) uniform sampler2DArray shadowMap; uniform vec3 lightDirection; uniform vec3 lightColour; @@ -34,8 +34,9 @@ isShaded(vec4 Position) const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { - const float lightSpaceDepth - = texture(shadowMap, PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw).r; + const float lightSpaceDepth = texture( + shadowMap, vec3(PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw, m)) + .r; return step(lightSpaceDepth, PositionInLightSpace.z * .5 + .5); } } diff --git a/gfx/gl/shaders/shadowDynamicPoint.vs b/gfx/gl/shaders/shadowDynamicPoint.vs index bf0f0a1..7335b9a 100644 --- a/gfx/gl/shaders/shadowDynamicPoint.vs +++ b/gfx/gl/shaders/shadowDynamicPoint.vs @@ -2,7 +2,6 @@ include(`meshIn.glsl') -uniform mat4 viewProjection; uniform ivec3 viewPoint; uniform mat3 model; uniform ivec3 modelPos; diff --git a/gfx/gl/shaders/shadowDynamicPointInst.vs b/gfx/gl/shaders/shadowDynamicPointInst.vs index b0b80da..d0eb649 100644 --- a/gfx/gl/shaders/shadowDynamicPointInst.vs +++ b/gfx/gl/shaders/shadowDynamicPointInst.vs @@ -2,7 +2,6 @@ include(`meshIn.glsl') -uniform mat4 viewProjection; uniform ivec3 viewPoint; layout(location = 5) in mat3 model; layout(location = 8) in ivec3 modelPos; diff --git a/gfx/gl/shaders/shadowFixedPoint.vs b/gfx/gl/shaders/shadowFixedPoint.vs index 307aac2..1376388 100644 --- a/gfx/gl/shaders/shadowFixedPoint.vs +++ b/gfx/gl/shaders/shadowFixedPoint.vs @@ -2,7 +2,6 @@ include(`meshIn.glsl') -uniform mat4 viewProjection; uniform ivec3 viewPoint; const mat3 model = mat3(1); const ivec3 modelPos = ivec3(0); -- cgit v1.2.3 From a01f1d9e834719046fbdec02a5147d2e55722bac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 29 Jan 2024 01:56:42 +0000 Subject: Remove the complications from previously storing several shadow maps in the texture --- gfx/gl/shaders/directionalLight.fs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index d0c5062..c49bb8d 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -15,35 +15,32 @@ uniform vec3 lightDirection; uniform vec3 lightColour; uniform ivec3 lightPoint; uniform mat4 lightViewProjection[MAX_MAPS]; -uniform vec4 shadowMapRegion[MAX_MAPS]; uniform uint lightViewProjectionCount; -const vec3 e1 = vec3(-1, -1, -1), e2 = vec3(1, 1, 1); +const vec3 e1 = vec3(0, 0, 0), e2 = vec3(1, 1, 1); -float + float insideShadowCube(vec3 v) { const vec3 s = step(e1, v) - step(e2, v); return s.x * s.y * s.z; } -float + float isShaded(vec4 Position) { for (uint m = 0u; m < lightViewProjectionCount; m++) { - const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; + const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz * .5 + .5; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { - const float lightSpaceDepth = texture( - shadowMap, vec3(PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw, m)) - .r; - return step(lightSpaceDepth, PositionInLightSpace.z * .5 + .5); + const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r; + return step(lightSpaceDepth, PositionInLightSpace.z); } } return 0; } -void + void main() { const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1); -- cgit v1.2.3 From 72ec5dd096a3110e8085919f4c0e3320ef176657 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 30 Jan 2024 22:13:56 +0000 Subject: Adjust light view projection matrix upfront Saves doing it per pixel, per region later --- gfx/gl/shaders/directionalLight.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index c49bb8d..24457b8 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -19,18 +19,18 @@ uniform uint lightViewProjectionCount; const vec3 e1 = vec3(0, 0, 0), e2 = vec3(1, 1, 1); - float +float insideShadowCube(vec3 v) { const vec3 s = step(e1, v) - step(e2, v); return s.x * s.y * s.z; } - float +float isShaded(vec4 Position) { for (uint m = 0u; m < lightViewProjectionCount; m++) { - const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz * .5 + .5; + const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r; @@ -40,7 +40,7 @@ isShaded(vec4 Position) return 0; } - void +void main() { const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1); -- cgit v1.2.3