From 900d19a41bc1886e7a809d99d6119b12235a4f0a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jul 2024 10:57:17 +0100 Subject: Initial commit of basic shadow depth map creation --- gfx/gl/shaders/shadowStencil.fs | 0 gfx/gl/shaders/shadowStencil.vs | 11 +++++++++++ 2 files changed, 11 insertions(+) create mode 100644 gfx/gl/shaders/shadowStencil.fs create mode 100644 gfx/gl/shaders/shadowStencil.vs (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowStencil.fs b/gfx/gl/shaders/shadowStencil.fs new file mode 100644 index 0000000..e69de29 diff --git a/gfx/gl/shaders/shadowStencil.vs b/gfx/gl/shaders/shadowStencil.vs new file mode 100644 index 0000000..4f4c250 --- /dev/null +++ b/gfx/gl/shaders/shadowStencil.vs @@ -0,0 +1,11 @@ +#version 330 core +#extension GL_ARB_shading_language_420pack : enable + +include(`meshIn.glsl') +uniform mat4 viewProjection; + +void +main() +{ + gl_Position = viewProjection * vec4(position, 1); +} -- cgit v1.2.3 From f737aada2b7164683303beb3bb490c30a4949fb0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jul 2024 02:33:24 +0100 Subject: Use texture alpha in shadow stencil --- gfx/gl/shaders/shadowStencil.fs | 16 ++++++++++++++++ gfx/gl/shaders/shadowStencil.vs | 7 +++++++ 2 files changed, 23 insertions(+) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowStencil.fs b/gfx/gl/shaders/shadowStencil.fs index e69de29..f149c23 100644 --- a/gfx/gl/shaders/shadowStencil.fs +++ b/gfx/gl/shaders/shadowStencil.fs @@ -0,0 +1,16 @@ +#version 330 core +#extension GL_ARB_shading_language_420pack : enable + +layout(binding = 0) uniform sampler2D textureAlbedo; + +include(`materialInterface.glsl') +include(`materialCommon.glsl') + +void +main() +{ + if (getTextureColour(Material, TexCoords).a < 0.5) { + discard; + } + gl_FragDepth = gl_FragCoord.z; +} diff --git a/gfx/gl/shaders/shadowStencil.vs b/gfx/gl/shaders/shadowStencil.vs index 4f4c250..45a0867 100644 --- a/gfx/gl/shaders/shadowStencil.vs +++ b/gfx/gl/shaders/shadowStencil.vs @@ -1,11 +1,18 @@ #version 330 core #extension GL_ARB_shading_language_420pack : enable +layout(binding = 1) uniform usampler2DRect materialData; + include(`meshIn.glsl') +include(`materialInterface.glsl') +include(`getMaterialDetail.glsl') uniform mat4 viewProjection; void main() { + TexCoords = texCoord; + Material = getMaterialDetail(material); + gl_Position = viewProjection * vec4(position, 1); } -- cgit v1.2.3 From 87ebf7a7587e832d922635021a32c887823d92c7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 18 Aug 2024 13:11:46 +0100 Subject: Offset shaded position to reduce chance of self-shadowing --- gfx/gl/shaders/directionalLight.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index 24457b8..86447ec 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -34,10 +34,10 @@ isShaded(vec4 Position) const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r; - return step(lightSpaceDepth, PositionInLightSpace.z); + return step(PositionInLightSpace.z, lightSpaceDepth + 0.001); } } - return 0; + return 1; } void @@ -46,5 +46,5 @@ main() const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1); const vec3 Normal = texture(gNormal, TexCoords).rgb; const float shaded = isShaded(Position); - FragColor = (1 - shaded) * max(dot(-lightDirection, Normal) * lightColour, 0); + FragColor = shaded * max(dot(-lightDirection, Normal) * lightColour, 0); } -- cgit v1.2.3 From ae9087aea7aa2b19bf78daa6593d14dfba98183d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 28 Aug 2024 03:48:19 +0100 Subject: Initial cut of shadow map creation with support for billboard shadows --- gfx/gl/shaders/shadowDynamicPointStencil.fs | 16 +++++++++++++++ gfx/gl/shaders/shadowDynamicPointStencil.gs | 32 +++++++++++++++++++++++++++++ gfx/gl/shaders/shadowDynamicPointStencil.vs | 17 +++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 gfx/gl/shaders/shadowDynamicPointStencil.fs create mode 100644 gfx/gl/shaders/shadowDynamicPointStencil.gs create mode 100644 gfx/gl/shaders/shadowDynamicPointStencil.vs (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.fs b/gfx/gl/shaders/shadowDynamicPointStencil.fs new file mode 100644 index 0000000..57b8aa3 --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointStencil.fs @@ -0,0 +1,16 @@ +#version 330 core +#extension GL_ARB_shading_language_420pack : enable + +layout(binding = 0) uniform sampler2D stencilDepth; +flat in vec3 scale; +in vec2 texCoord; + +void +main() +{ + float stDepth = texture(stencilDepth, texCoord).r; + if (stDepth >= 1) { + discard; + } + gl_FragDepth = gl_FragCoord.z + (stDepth * scale.z); +} diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.gs b/gfx/gl/shaders/shadowDynamicPointStencil.gs new file mode 100644 index 0000000..6d707ae --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointStencil.gs @@ -0,0 +1,32 @@ +#version 330 core +#extension GL_ARB_viewport_array : enable + +const vec2[] corners = vec2[4](vec2(-1, -1), vec2(-1, 1), vec2(1, -1), vec2(1, 1)); + +uniform mat4 viewProjection[4]; +uniform int viewProjections; +uniform vec3 sizes[4]; +uniform float size; + +in ivec3 vworldPos[]; + +flat out vec3 scale; +out vec2 texCoord; + +layout(points) in; +layout(triangle_strip, max_vertices = 16) out; + +void +main() +{ + for (gl_Layer = 0; gl_Layer < viewProjections; ++gl_Layer) { + scale = 2.0 * size / sizes[gl_Layer]; + vec4 pos = viewProjection[gl_Layer] * vec4(vworldPos[0], 1); + for (int c = 0; c < corners.length(); ++c) { + gl_Position = pos + vec4(scale.xy * corners[c], 0, 0); + texCoord = (corners[c] * 0.5) + 0.5; + EmitVertex(); + } + EndPrimitive(); + } +} diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.vs b/gfx/gl/shaders/shadowDynamicPointStencil.vs new file mode 100644 index 0000000..dadf9c2 --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointStencil.vs @@ -0,0 +1,17 @@ +#version 330 core +#extension GL_ARB_shading_language_420pack : enable + +layout(location = 0) in mat3 model; +layout(location = 3) in ivec3 worldPos; +uniform ivec3 viewPoint; +uniform vec3 centre; + +out mat3 vmodel; +out ivec3 vworldPos; + +void +main() +{ + vmodel = model; + vworldPos = worldPos - viewPoint + ivec3(centre); +} -- cgit v1.2.3 From 7299b08977acafe7eeba620f229a7169256f23cf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 1 Sep 2024 18:42:27 +0100 Subject: Extend depth of shadow box 10m to allow for depth offsets and clamp stencil to it --- gfx/gl/shaders/shadowDynamicPointStencil.gs | 1 + 1 file changed, 1 insertion(+) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.gs b/gfx/gl/shaders/shadowDynamicPointStencil.gs index 6d707ae..2873691 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.gs +++ b/gfx/gl/shaders/shadowDynamicPointStencil.gs @@ -24,6 +24,7 @@ main() vec4 pos = viewProjection[gl_Layer] * vec4(vworldPos[0], 1); for (int c = 0; c < corners.length(); ++c) { gl_Position = pos + vec4(scale.xy * corners[c], 0, 0); + gl_Position.z = max(gl_Position.z, -1); texCoord = (corners[c] * 0.5) + 0.5; EmitVertex(); } -- cgit v1.2.3 From 052b2b49d9bfdaaa7f7c5603fa7c47b881df93c2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 2 Sep 2024 21:00:41 +0100 Subject: Update stencil texture to 2d array --- gfx/gl/shaders/shadowDynamicPointStencil.fs | 4 ++-- gfx/gl/shaders/shadowDynamicPointStencil.gs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.fs b/gfx/gl/shaders/shadowDynamicPointStencil.fs index 57b8aa3..e612691 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.fs +++ b/gfx/gl/shaders/shadowDynamicPointStencil.fs @@ -1,9 +1,9 @@ #version 330 core #extension GL_ARB_shading_language_420pack : enable -layout(binding = 0) uniform sampler2D stencilDepth; +layout(binding = 0) uniform sampler2DArray stencilDepth; flat in vec3 scale; -in vec2 texCoord; +in vec3 texCoord; void main() diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.gs b/gfx/gl/shaders/shadowDynamicPointStencil.gs index 2873691..b91937d 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.gs +++ b/gfx/gl/shaders/shadowDynamicPointStencil.gs @@ -11,7 +11,7 @@ uniform float size; in ivec3 vworldPos[]; flat out vec3 scale; -out vec2 texCoord; +out vec3 texCoord; layout(points) in; layout(triangle_strip, max_vertices = 16) out; @@ -25,7 +25,7 @@ main() for (int c = 0; c < corners.length(); ++c) { gl_Position = pos + vec4(scale.xy * corners[c], 0, 0); gl_Position.z = max(gl_Position.z, -1); - texCoord = (corners[c] * 0.5) + 0.5; + texCoord = vec3((corners[c] * 0.5) + 0.5, 0); EmitVertex(); } EndPrimitive(); -- cgit v1.2.3 From f22f79330f9b8851cda76d1e11555877cbb72d80 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 3 Sep 2024 01:37:03 +0100 Subject: Populate all layers of the stencil texture Albeit with the same projection --- gfx/gl/shaders/shadowStencil.fs | 6 ++++-- gfx/gl/shaders/shadowStencil.gs | 28 ++++++++++++++++++++++++++++ gfx/gl/shaders/shadowStencil.vs | 10 ++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 gfx/gl/shaders/shadowStencil.gs (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowStencil.fs b/gfx/gl/shaders/shadowStencil.fs index f149c23..1164cc9 100644 --- a/gfx/gl/shaders/shadowStencil.fs +++ b/gfx/gl/shaders/shadowStencil.fs @@ -3,13 +3,15 @@ layout(binding = 0) uniform sampler2D textureAlbedo; -include(`materialInterface.glsl') +include(`materialDetail.glsl') include(`materialCommon.glsl') +in vec2 gTexCoords; +flat in MaterialDetail gMaterial; void main() { - if (getTextureColour(Material, TexCoords).a < 0.5) { + if (getTextureColour(gMaterial, gTexCoords).a < 0.5) { discard; } gl_FragDepth = gl_FragCoord.z; diff --git a/gfx/gl/shaders/shadowStencil.gs b/gfx/gl/shaders/shadowStencil.gs new file mode 100644 index 0000000..eea4643 --- /dev/null +++ b/gfx/gl/shaders/shadowStencil.gs @@ -0,0 +1,28 @@ +#version 330 core +#extension GL_ARB_viewport_array : enable + +include(`materialDetail.glsl') + +layout(triangles) in; +layout(triangle_strip, max_vertices = 24) out; + +uniform mat4 viewProjection; +in vec3 FragPos[]; +in vec2 TexCoords[]; +flat in MaterialDetail Material[]; +out vec2 gTexCoords; +flat out MaterialDetail gMaterial; + +void +main() +{ + for (gl_Layer = 0; gl_Layer < 8; ++gl_Layer) { + for (int v = 0; v < FragPos.length(); ++v) { + gl_Position = viewProjection * vec4(FragPos[v], 1); + gTexCoords = TexCoords[v]; + gMaterial = Material[v]; + EmitVertex(); + } + EndPrimitive(); + } +} diff --git a/gfx/gl/shaders/shadowStencil.vs b/gfx/gl/shaders/shadowStencil.vs index 45a0867..a15c4fb 100644 --- a/gfx/gl/shaders/shadowStencil.vs +++ b/gfx/gl/shaders/shadowStencil.vs @@ -4,15 +4,17 @@ layout(binding = 1) uniform usampler2DRect materialData; include(`meshIn.glsl') -include(`materialInterface.glsl') +include(`materialDetail.glsl') include(`getMaterialDetail.glsl') -uniform mat4 viewProjection; + +out vec3 FragPos; +out vec2 TexCoords; +flat out MaterialDetail Material; void main() { TexCoords = texCoord; Material = getMaterialDetail(material); - - gl_Position = viewProjection * vec4(position, 1); + FragPos = position; } -- cgit v1.2.3 From 5ec74c35fb1f2524add7fd03ace78667f93edb2e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 7 Sep 2024 12:35:03 +0100 Subject: Populate all layers of shadow stencil with view from all around --- gfx/gl/shaders/shadowStencil.gs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowStencil.gs b/gfx/gl/shaders/shadowStencil.gs index eea4643..2c3f9bd 100644 --- a/gfx/gl/shaders/shadowStencil.gs +++ b/gfx/gl/shaders/shadowStencil.gs @@ -6,7 +6,7 @@ include(`materialDetail.glsl') layout(triangles) in; layout(triangle_strip, max_vertices = 24) out; -uniform mat4 viewProjection; +uniform mat4 viewProjection[8]; in vec3 FragPos[]; in vec2 TexCoords[]; flat in MaterialDetail Material[]; @@ -16,9 +16,9 @@ flat out MaterialDetail gMaterial; void main() { - for (gl_Layer = 0; gl_Layer < 8; ++gl_Layer) { + for (gl_Layer = 0; gl_Layer < viewProjection.length(); ++gl_Layer) { for (int v = 0; v < FragPos.length(); ++v) { - gl_Position = viewProjection * vec4(FragPos[v], 1); + gl_Position = viewProjection[gl_Layer] * vec4(FragPos[v], 1); gTexCoords = TexCoords[v]; gMaterial = Material[v]; EmitVertex(); -- cgit v1.2.3 From c9bf24c7d1c10e2ff7349fa2d9010895860977a9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 10 Oct 2024 00:48:06 +0100 Subject: Fix stencil shadow depth offset --- gfx/gl/shaders/shadowDynamicPointStencil.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.fs b/gfx/gl/shaders/shadowDynamicPointStencil.fs index e612691..fe91b07 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.fs +++ b/gfx/gl/shaders/shadowDynamicPointStencil.fs @@ -12,5 +12,5 @@ main() if (stDepth >= 1) { discard; } - gl_FragDepth = gl_FragCoord.z + (stDepth * scale.z); + gl_FragDepth = gl_FragCoord.z + ((stDepth - 0.5) * scale.z); } -- cgit v1.2.3 From 439630ae86b01b714574335d4dd2e583f724dba3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 10 Oct 2024 00:59:41 +0100 Subject: Include foliage yaw rotation in vertex, pass through instead of whole model rotation matrix --- gfx/gl/shaders/shadowDynamicPointStencil.vs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.vs b/gfx/gl/shaders/shadowDynamicPointStencil.vs index dadf9c2..0dd2d79 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.vs +++ b/gfx/gl/shaders/shadowDynamicPointStencil.vs @@ -1,17 +1,17 @@ #version 330 core #extension GL_ARB_shading_language_420pack : enable -layout(location = 0) in mat3 model; -layout(location = 3) in ivec3 worldPos; +layout(location = 0) in ivec3 worldPos; +layout(location = 1) in float modelYaw; uniform ivec3 viewPoint; uniform vec3 centre; -out mat3 vmodel; +out float vmodelYaw; out ivec3 vworldPos; void main() { - vmodel = model; + vmodelYaw = modelYaw; vworldPos = worldPos - viewPoint + ivec3(centre); } -- cgit v1.2.3 From 493af2b220d6ba7f883d35a6bb30cb5d07b4a4b5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 10 Oct 2024 01:01:24 +0100 Subject: Use foliage yaw rotation to select shadow stencil layer --- gfx/gl/shaders/shadowDynamicPointStencil.gs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'gfx/gl/shaders') diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.gs b/gfx/gl/shaders/shadowDynamicPointStencil.gs index b91937d..7e81d97 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.gs +++ b/gfx/gl/shaders/shadowDynamicPointStencil.gs @@ -2,12 +2,14 @@ #extension GL_ARB_viewport_array : enable const vec2[] corners = vec2[4](vec2(-1, -1), vec2(-1, 1), vec2(1, -1), vec2(1, 1)); +const float tau = 6.28318531; uniform mat4 viewProjection[4]; uniform int viewProjections; uniform vec3 sizes[4]; uniform float size; +in float vmodelYaw[]; in ivec3 vworldPos[]; flat out vec3 scale; @@ -19,13 +21,14 @@ layout(triangle_strip, max_vertices = 16) out; void main() { + int viewAngle = int(round(4.0 + (vmodelYaw[0] / tau))) % 8; for (gl_Layer = 0; gl_Layer < viewProjections; ++gl_Layer) { scale = 2.0 * size / sizes[gl_Layer]; vec4 pos = viewProjection[gl_Layer] * vec4(vworldPos[0], 1); for (int c = 0; c < corners.length(); ++c) { gl_Position = pos + vec4(scale.xy * corners[c], 0, 0); gl_Position.z = max(gl_Position.z, -1); - texCoord = vec3((corners[c] * 0.5) + 0.5, 0); + texCoord = vec3((corners[c] * 0.5) + 0.5, viewAngle); EmitVertex(); } EndPrimitive(); -- cgit v1.2.3