Compare commits

...

2 Commits

Author SHA1 Message Date
cosmonaut 3a89e9a091 pack directional shadow maps into a cubemap 2020-08-27 22:57:15 -07:00
cosmonaut 28bc8c79e7 basic directional light shadow mapping 2020-08-27 14:46:20 -07:00
10 changed files with 158 additions and 34 deletions

View File

@ -9,8 +9,10 @@ namespace Kav
EffectParameter gAlbedoParam; EffectParameter gAlbedoParam;
EffectParameter gNormalParam; EffectParameter gNormalParam;
EffectParameter gMetallicRoughnessParam; EffectParameter gMetallicRoughnessParam;
EffectParameter shadowMapParam;
EffectParameter eyePositionParam; EffectParameter eyePositionParam;
PointLightCollection pointLightCollection; PointLightCollection pointLightCollection;
DirectionalLightCollection directionalLightCollection; DirectionalLightCollection directionalLightCollection;
@ -18,6 +20,7 @@ namespace Kav
public Texture2D GAlbedo { get; set; } public Texture2D GAlbedo { get; set; }
public Texture2D GNormal { get; set; } public Texture2D GNormal { get; set; }
public Texture2D GMetallicRoughness { get; set; } public Texture2D GMetallicRoughness { get; set; }
public TextureCube DirectionalShadowMap { get; set; }
public Vector3 EyePosition { get; set; } public Vector3 EyePosition { get; set; }
@ -49,7 +52,8 @@ namespace Kav
DirectionalLights = new DirectionalLightCollection( DirectionalLights = new DirectionalLightCollection(
Parameters["DirectionalLightDirections"], Parameters["DirectionalLightDirections"],
Parameters["DirectionalLightColors"] Parameters["DirectionalLightColors"],
Parameters["DirectionalLightMatrices"]
); );
} }
@ -75,7 +79,8 @@ namespace Kav
DirectionalLights = new DirectionalLightCollection( DirectionalLights = new DirectionalLightCollection(
Parameters["DirectionalLightDirections"], Parameters["DirectionalLightDirections"],
Parameters["DirectionalLightColors"] Parameters["DirectionalLightColors"],
Parameters["DirectionalLightMatrices"]
); );
for (int i = 0; i < MaxDirectionalLights; i++) for (int i = 0; i < MaxDirectionalLights; i++)
@ -95,6 +100,7 @@ namespace Kav
gAlbedoParam.SetValue(GAlbedo); gAlbedoParam.SetValue(GAlbedo);
gNormalParam.SetValue(GNormal); gNormalParam.SetValue(GNormal);
gMetallicRoughnessParam.SetValue(GMetallicRoughness); gMetallicRoughnessParam.SetValue(GMetallicRoughness);
shadowMapParam.SetValue(DirectionalShadowMap);
eyePositionParam.SetValue(EyePosition); eyePositionParam.SetValue(EyePosition);
} }
@ -105,6 +111,7 @@ namespace Kav
gAlbedoParam = Parameters["gAlbedo"]; gAlbedoParam = Parameters["gAlbedo"];
gNormalParam = Parameters["gNormal"]; gNormalParam = Parameters["gNormal"];
gMetallicRoughnessParam = Parameters["gMetallicRoughness"]; gMetallicRoughnessParam = Parameters["gMetallicRoughness"];
shadowMapParam = Parameters["shadowMap"];
eyePositionParam = Parameters["EyePosition"]; eyePositionParam = Parameters["EyePosition"];
} }

View File

@ -8,14 +8,20 @@ namespace Kav
private readonly Vector3[] directions = new Vector3[4]; private readonly Vector3[] directions = new Vector3[4];
private readonly Vector3[] colors = new Vector3[4]; private readonly Vector3[] colors = new Vector3[4];
private readonly float[] intensities = new float[4]; private readonly float[] intensities = new float[4];
private readonly Matrix[] lightSpaceMatrices = new Matrix[4];
readonly EffectParameter lightDirectionsParam; readonly EffectParameter lightDirectionsParam;
readonly EffectParameter lightColorsParam; readonly EffectParameter lightColorsParam;
readonly EffectParameter lightSpaceMatricesParam;
public DirectionalLightCollection(EffectParameter lightDirectionsParam, EffectParameter lightColorsParam) public DirectionalLightCollection(
{ EffectParameter lightDirectionsParam,
EffectParameter lightColorsParam,
EffectParameter lightSpaceMatricesParam
) {
this.lightDirectionsParam = lightDirectionsParam; this.lightDirectionsParam = lightDirectionsParam;
this.lightColorsParam = lightColorsParam; this.lightColorsParam = lightColorsParam;
this.lightSpaceMatricesParam = lightSpaceMatricesParam;
} }
public DirectionalLight this[int i] public DirectionalLight this[int i]
@ -39,8 +45,10 @@ namespace Kav
directions[i] = value.Direction; directions[i] = value.Direction;
colors[i] = value.Color.ToVector3() * value.Intensity; colors[i] = value.Color.ToVector3() * value.Intensity;
intensities[i] = value.Intensity; intensities[i] = value.Intensity;
lightSpaceMatrices[i] = value.View * value.Projection;
lightDirectionsParam.SetValue(directions); lightDirectionsParam.SetValue(directions);
lightColorsParam.SetValue(colors); lightColorsParam.SetValue(colors);
lightSpaceMatricesParam.SetValue(lightSpaceMatrices);
} }
} }
} }

Binary file not shown.

Binary file not shown.

View File

@ -8,6 +8,7 @@ DECLARE_TEXTURE(gPosition, 0);
DECLARE_TEXTURE(gAlbedo, 1); DECLARE_TEXTURE(gAlbedo, 1);
DECLARE_TEXTURE(gNormal, 2); DECLARE_TEXTURE(gNormal, 2);
DECLARE_TEXTURE(gMetallicRoughness, 3); DECLARE_TEXTURE(gMetallicRoughness, 3);
DECLARE_CUBEMAP(shadowMap, 4);
BEGIN_CONSTANTS BEGIN_CONSTANTS
@ -21,6 +22,8 @@ BEGIN_CONSTANTS
MATRIX_CONSTANTS MATRIX_CONSTANTS
float4x4 DirectionalLightMatrices[MAX_DIRECTIONAL_LIGHTS] _ps(c137) _cb(c137);
END_CONSTANTS END_CONSTANTS
struct PixelInput struct PixelInput
@ -71,6 +74,63 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
return ggx1 * ggx2; return ggx1 * ggx2;
} }
float3 ConvertCubeUVToXYZ(int index, float u, float v)
{
float uc = 2.0 * u - 1.0;
float vc = 2.0 * v - 1.0;
if (index == 0) { return float3(1.0, vc, -uc); }
if (index == 1) { return float3(-1.0, vc, uc); }
if (index == 2) { return float3(uc, 1.0, -vc); }
if (index == 3) { return float3(uc, -1.0, vc); }
if (index == 4) { return float3(uc, vc, -1.0); }
if (index == 5) { return float3(-uc, vc, 1.0); }
return float3(1.0, 0.0, 0.5);
}
float ComputeShadow(float4 positionLightSpace, int directionalLightIndex)
{
float bias = 0.001;
// maps to [-1, 1]
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
float3 cubeMapSampleVector;
if (directionalLightIndex == 0)
{
cubeMapSampleVector = float3(1.0f, projectionCoords.y, -projectionCoords.x);
}
else if (directionalLightIndex == 1)
{
cubeMapSampleVector = float3(-1.0f, projectionCoords.y, projectionCoords.x);
}
else if (directionalLightIndex == 2)
{
cubeMapSampleVector = float3(projectionCoords.x, 1.0f, projectionCoords.y);
}
else if (directionalLightIndex == 3)
{
cubeMapSampleVector = float3(projectionCoords.x, -1.0f, -projectionCoords.y);
}
else if (directionalLightIndex == 4)
{
cubeMapSampleVector = float3(projectionCoords.x, projectionCoords.y, 1.0);
}
else
{
cubeMapSampleVector = float3(-projectionCoords.x, projectionCoords.y, -1.0);
}
float closestDepth = SAMPLE_CUBEMAP(shadowMap, cubeMapSampleVector).r;
float currentDepth = projectionCoords.z;
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
return shadow;
}
float3 ComputeLight( float3 ComputeLight(
float3 lightDir, float3 lightDir,
float3 radiance, float3 radiance,
@ -79,7 +139,8 @@ float3 ComputeLight(
float3 N, float3 N,
float3 albedo, float3 albedo,
float metallic, float metallic,
float roughness float roughness,
float shadow
) { ) {
float3 L = normalize(lightDir); float3 L = normalize(lightDir);
float3 H = normalize(V + L); float3 H = normalize(V + L);
@ -98,7 +159,7 @@ float3 ComputeLight(
kD *= 1.0 - metallic; kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0); float NdotL = max(dot(N, L), 0.0);
return (kD * albedo / PI + specular) * radiance * NdotL; return (kD * albedo / PI + specular) * radiance * NdotL * shadow;
} }
float4 ComputeColor( float4 ComputeColor(
@ -124,16 +185,19 @@ float4 ComputeColor(
float attenuation = 1.0 / (distance * distance); float attenuation = 1.0 / (distance * distance);
float3 radiance = PointLightColors[i] * attenuation; float3 radiance = PointLightColors[i] * attenuation;
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness); Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, 1.0);
} }
// directional light // directional light
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++) for (int i = 0; i < 1; i++)
{ {
float4 positionLightSpace = mul(float4(worldPosition, 1.0), DirectionalLightMatrices[i]);
float shadow = ComputeShadow(positionLightSpace, i);
float3 lightDir = DirectionalLightDirections[i]; float3 lightDir = DirectionalLightDirections[i];
float3 radiance = DirectionalLightColors[i]; float3 radiance = DirectionalLightColors[i];
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness); Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, (1.0 - shadow));
} }
float3 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO; float3 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO;
@ -148,13 +212,13 @@ float4 ComputeColor(
float4 main_ps(PixelInput input) : SV_TARGET0 float4 main_ps(PixelInput input) : SV_TARGET0
{ {
float3 fragPosition = SAMPLE_TEXTURE(gPosition, input.TexCoord).rgb; float3 worldPosition = SAMPLE_TEXTURE(gPosition, input.TexCoord).rgb;
float3 normal = SAMPLE_TEXTURE(gNormal, input.TexCoord).xyz; float3 normal = SAMPLE_TEXTURE(gNormal, input.TexCoord).xyz;
float3 albedo = SAMPLE_TEXTURE(gAlbedo, input.TexCoord).rgb; float3 albedo = SAMPLE_TEXTURE(gAlbedo, input.TexCoord).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, input.TexCoord).rg; float2 metallicRoughness = SAMPLE_TEXTURE(gMetallicRoughness, input.TexCoord).rg;
return ComputeColor( return ComputeColor(
fragPosition, worldPosition,
normal, normal,
albedo, albedo,
metallicRoughness.r, metallicRoughness.r,

View File

@ -3,6 +3,8 @@
BEGIN_CONSTANTS BEGIN_CONSTANTS
float4x4 ModelViewProjection _vs(c0) _cb(c0); float4x4 ModelViewProjection _vs(c0) _cb(c0);
float near _vs(c4) _cb(c4);
float far _vs(c5) _cb(c5);
MATRIX_CONSTANTS MATRIX_CONSTANTS
@ -16,21 +18,31 @@ struct VertexShaderInput
struct VertexShaderOutput struct VertexShaderOutput
{ {
float4 Position : SV_Position; float4 Position : SV_Position;
float Depth : TEXCOORD0;
}; };
VertexShaderOutput main_vs(VertexShaderInput input) VertexShaderOutput main_vs(VertexShaderInput input)
{ {
VertexShaderOutput output; VertexShaderOutput output;
output.Position = mul(float4(input.Position.xyz, 1.0), ModelViewProjection); output.Position = mul(input.Position, ModelViewProjection);
output.Depth = output.Position.z / output.Position.w;
output.Depth = (output.Depth * 0.5) + 0.5;
return output; return output;
} }
float4 main_ps(VertexShaderOutput input) : SV_TARGET0
{
return float4(input.Depth, 0.0, 0.0, 0.0);
}
Technique SimpleDepth Technique SimpleDepth
{ {
Pass Pass
{ {
VertexShader = compile vs_3_0 main_vs(); VertexShader = compile vs_3_0 main_vs();
PixelShader = compile ps_3_0 main_ps();
} }
} }

View File

@ -207,7 +207,8 @@ namespace Kav
directionalLightCollection = new DirectionalLightCollection( directionalLightCollection = new DirectionalLightCollection(
Parameters["LightDirections"], Parameters["LightDirections"],
Parameters["DirectionLightColors"] Parameters["DirectionLightColors"],
Parameters["DirectionalLightMatrices"]
); );
} }
@ -232,7 +233,8 @@ namespace Kav
DirectionalLights = new DirectionalLightCollection( DirectionalLights = new DirectionalLightCollection(
Parameters["LightDirections"], Parameters["LightDirections"],
Parameters["DirectionLightColors"] Parameters["DirectionLightColors"],
Parameters["DirectionalLightMatrices"]
); );
for (int i = 0; i < MaxDirectionalLights; i++) for (int i = 0; i < MaxDirectionalLights; i++)

View File

@ -6,6 +6,8 @@ namespace Kav
public class SimpleDepthEffect : Effect public class SimpleDepthEffect : Effect
{ {
EffectParameter modelViewProjectionParam; EffectParameter modelViewProjectionParam;
EffectParameter nearParam;
EffectParameter farParam;
Matrix model; Matrix model;
Matrix view; Matrix view;
@ -42,6 +44,10 @@ namespace Kav
dirtyFlags |= EffectDirtyFlags.WorldViewProj; dirtyFlags |= EffectDirtyFlags.WorldViewProj;
} }
} }
public float Near { get; set; }
public float Far { get; set; }
public SimpleDepthEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.SimpleDepthEffect) public SimpleDepthEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.SimpleDepthEffect)
{ {
CacheEffectParameters(); CacheEffectParameters();
@ -58,11 +64,16 @@ namespace Kav
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj; dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
} }
nearParam.SetValue(Near);
farParam.SetValue(Far);
} }
private void CacheEffectParameters() private void CacheEffectParameters()
{ {
modelViewProjectionParam = Parameters["ModelViewProjection"]; modelViewProjectionParam = Parameters["ModelViewProjection"];
nearParam = Parameters["near"];
farParam = Parameters["far"];
} }
} }
} }

View File

@ -12,7 +12,7 @@ namespace Kav
{ {
get get
{ {
return Matrix.CreateLookAt(-Direction * 100f, Vector3.Zero, Vector3.Up); return Matrix.CreateLookAt(Direction * 4.5f, Vector3.Zero, Vector3.Up);
} }
} }
@ -20,7 +20,7 @@ namespace Kav
{ {
get get
{ {
return Matrix.CreateOrthographic(20f, 20f, 1f, 101f); return Matrix.CreateOrthographic(20f, 20f, 1f, 7.5f);
} }
} }

View File

@ -10,7 +10,7 @@ namespace Kav
private int RenderDimensionsX { get; } private int RenderDimensionsX { get; }
private int RenderDimensionsY { get; } private int RenderDimensionsY { get; }
private RenderTarget2D DepthRenderTarget { get; } private RenderTargetCube DirectionalLightDepthTarget { get; }
private SimpleDepthEffect SimpleDepthEffect { get; } private SimpleDepthEffect SimpleDepthEffect { get; }
private RenderTarget2D gPosition { get; } private RenderTarget2D gPosition { get; }
@ -31,12 +31,11 @@ namespace Kav
RenderDimensionsX = renderDimensionsX; RenderDimensionsX = renderDimensionsX;
RenderDimensionsY = renderDimensionsY; RenderDimensionsY = renderDimensionsY;
DepthRenderTarget = new RenderTarget2D( DirectionalLightDepthTarget = new RenderTargetCube(
GraphicsDevice, GraphicsDevice,
renderDimensionsX, 1024,
renderDimensionsY,
false, false,
SurfaceFormat.HalfSingle, // unused SurfaceFormat.Single,
DepthFormat.Depth24 DepthFormat.Depth24
); );
@ -96,6 +95,13 @@ namespace Kav
GraphicsDevice.SetRenderTarget(deferredRenderTarget); GraphicsDevice.SetRenderTarget(deferredRenderTarget);
graphicsDevice.Clear(Color.White); graphicsDevice.Clear(Color.White);
for (int i = 0; i < 6; i++)
{
GraphicsDevice.SetRenderTarget(DirectionalLightDepthTarget, (CubeMapFace) i);
GraphicsDevice.Clear(Color.White);
}
GraphicsDevice.SetRenderTarget(null); GraphicsDevice.SetRenderTarget(null);
} }
@ -105,6 +111,15 @@ namespace Kav
IEnumerable<PointLight> pointLights, IEnumerable<PointLight> pointLights,
IEnumerable<DirectionalLight> directionalLights IEnumerable<DirectionalLight> directionalLights
) { ) {
var directionalLightIndex = 0;
foreach (var directionalLight in directionalLights)
{
if (directionalLightIndex > 5) { break; }
ShadowMapRender(modelTransforms, directionalLight, (CubeMapFace) directionalLightIndex);
directionalLightIndex += 1;
}
GraphicsDevice.SetRenderTargets(GBuffer); GraphicsDevice.SetRenderTargets(GBuffer);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0); GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.DepthStencilState = DepthStencilState.Default;
@ -150,6 +165,7 @@ namespace Kav
DeferredPBREffect.GAlbedo = gAlbedo; DeferredPBREffect.GAlbedo = gAlbedo;
DeferredPBREffect.GNormal = gNormal; DeferredPBREffect.GNormal = gNormal;
DeferredPBREffect.GMetallicRoughness = gMetallicRoughness; DeferredPBREffect.GMetallicRoughness = gMetallicRoughness;
DeferredPBREffect.DirectionalShadowMap = DirectionalLightDepthTarget;
DeferredPBREffect.EyePosition = Matrix.Invert(camera.View).Translation; DeferredPBREffect.EyePosition = Matrix.Invert(camera.View).Translation;
int i = 0; int i = 0;
@ -173,23 +189,18 @@ namespace Kav
SpriteBatch.End(); SpriteBatch.End();
} }
public void Render(
Camera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
IEnumerable<PointLight> pointLights,
IEnumerable<DirectionalLight> directionalLights
) {
Render(camera.View, camera.Projection, modelTransforms, pointLights, directionalLights);
}
// for shadow mapping // for shadow mapping
public void DepthRender(IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight) public void ShadowMapRender(IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight, CubeMapFace face)
{ {
GraphicsDevice.SetRenderTarget(DepthRenderTarget); GraphicsDevice.SetRenderTarget(DirectionalLightDepthTarget, face);
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0); GraphicsDevice.Clear(Color.White);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
SimpleDepthEffect.View = directionalLight.View; SimpleDepthEffect.View = directionalLight.View;
SimpleDepthEffect.Projection = directionalLight.Projection; SimpleDepthEffect.Projection = directionalLight.Projection;
SimpleDepthEffect.Near = 0.1f; // FIXME: this is a kludge
SimpleDepthEffect.Far = 200f;
foreach (var (model, transform) in modelTransforms) foreach (var (model, transform) in modelTransforms)
{ {
@ -220,6 +231,15 @@ namespace Kav
} }
} }
public void Render(
Camera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
IEnumerable<PointLight> pointLights,
IEnumerable<DirectionalLight> directionalLights
) {
Render(camera.View, camera.Projection, modelTransforms, pointLights, directionalLights);
}
private void Render( private void Render(
Matrix view, Matrix view,
Matrix projection, Matrix projection,