From 0c576668cbd800e76c062542e7e632b0853eaf98 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 8 Dec 2020 16:02:58 -0800 Subject: [PATCH] add instanced linear depth effect + fix platforms --- Effects/FXB/LinearDepthEffectInstanced.fxb | 3 + Effects/HLSL/LinearDepthEffectInstanced.fx | 55 ++++++++++++++++++ Effects/HLSL/SimpleDepthEffectInstanced.fx | 2 +- Effects/LinearDepthEffectInstanced.cs | 67 ++++++++++++++++++++++ Kav.Core.csproj | 3 + Kav.Framework.csproj | 5 +- Resources.cs | 13 +++++ 7 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 Effects/FXB/LinearDepthEffectInstanced.fxb create mode 100644 Effects/HLSL/LinearDepthEffectInstanced.fx create mode 100644 Effects/LinearDepthEffectInstanced.cs diff --git a/Effects/FXB/LinearDepthEffectInstanced.fxb b/Effects/FXB/LinearDepthEffectInstanced.fxb new file mode 100644 index 0000000..e1fbece --- /dev/null +++ b/Effects/FXB/LinearDepthEffectInstanced.fxb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d0b9a114d0e1ed09d5e6824938e1504660e71b5cf70f6ec07f17b7eef530d8e6 +size 1416 diff --git a/Effects/HLSL/LinearDepthEffectInstanced.fx b/Effects/HLSL/LinearDepthEffectInstanced.fx new file mode 100644 index 0000000..14ec301 --- /dev/null +++ b/Effects/HLSL/LinearDepthEffectInstanced.fx @@ -0,0 +1,55 @@ +#include "Macros.fxh" + +BEGIN_CONSTANTS + + float4x4 ViewProjection _vs(c4) _cb(c4); + + float3 LightPosition _ps(c0) _cb(c8); + float FarPlane _ps(c1) _cb(c9); + +END_CONSTANTS + +struct VertexShaderInput +{ + float4 Position : POSITION; +}; + +struct VertexShaderOutput +{ + float4 Position : SV_Position; + float3 PositionWorld : TEXCOORD0; +}; + +VertexShaderOutput instanced_vs(VertexShaderInput input, float3 Translation : TEXCOORD2) +{ + VertexShaderOutput output; + + float4x4 world = float4x4( + float4(1, 0, 0, 0), + float4(0, 1, 0, 0), + float4(0, 0, 1, 0), + float4(Translation.x, Translation.y, Translation.z, 1) + ); + + float4x4 worldViewProjection = mul(world, ViewProjection); + output.Position = mul(input.Position, worldViewProjection); + output.Position.x *= -1; // otherwise cube map render will be horizontally flipped + output.PositionWorld = mul(input.Position, world); + return output; +} + +float4 main_ps(VertexShaderOutput input) : SV_TARGET0 +{ + float lightDistance = length(input.PositionWorld - LightPosition); + lightDistance /= FarPlane; + return float4(lightDistance, 0.0, 0.0, 0.0); +} + +Technique SimpleDepth +{ + Pass + { + VertexShader = compile vs_3_0 instanced_vs(); + PixelShader = compile ps_3_0 main_ps(); + } +} diff --git a/Effects/HLSL/SimpleDepthEffectInstanced.fx b/Effects/HLSL/SimpleDepthEffectInstanced.fx index f35471e..11c49fb 100644 --- a/Effects/HLSL/SimpleDepthEffectInstanced.fx +++ b/Effects/HLSL/SimpleDepthEffectInstanced.fx @@ -20,7 +20,7 @@ VertexShaderOutput instanced_vs(VertexShaderInput input, float3 Translation : TE float4(0, 1, 0, 0), float4(0, 0, 1, 0), float4(Translation.x, Translation.y, Translation.z, 1) - ); + ); float4x4 worldViewProjection = mul(world, ViewProjection); diff --git a/Effects/LinearDepthEffectInstanced.cs b/Effects/LinearDepthEffectInstanced.cs new file mode 100644 index 0000000..83df4a8 --- /dev/null +++ b/Effects/LinearDepthEffectInstanced.cs @@ -0,0 +1,67 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace Kav +{ + public class LinearDepthEffectInstanced : Effect + { + EffectParameter viewProjectionParam; + EffectParameter lightPositionParam; + EffectParameter farPlaneParam; + + EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All; + + Matrix view; + Matrix projection; + + public Vector3 LightPosition { get; set; } + public float FarPlane { get; set; } + + public Matrix View + { + get { return view; } + set + { + view = value; + dirtyFlags |= EffectDirtyFlags.ViewProj; + } + } + + public Matrix Projection + { + get { return projection; } + set + { + projection = value; + dirtyFlags |= EffectDirtyFlags.ViewProj; + } + } + + public LinearDepthEffectInstanced(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.LinearDepthEffectInstanced) + { + CacheEffectParameters(); + } + + protected override void OnApply() + { + if ((dirtyFlags & EffectDirtyFlags.ViewProj) != 0) + { + Matrix.Multiply(ref view, ref projection, out Matrix viewProjection); + viewProjectionParam.SetValue(viewProjection); + + dirtyFlags &= ~EffectDirtyFlags.ViewProj; + } + + lightPositionParam.SetValue(LightPosition); + farPlaneParam.SetValue(FarPlane); + } + + private void CacheEffectParameters() + { + viewProjectionParam = Parameters["ViewProjection"]; + + lightPositionParam = Parameters["LightPosition"]; + farPlaneParam = Parameters["FarPlane"]; + } + } +} diff --git a/Kav.Core.csproj b/Kav.Core.csproj index 6077186..7ed1272 100644 --- a/Kav.Core.csproj +++ b/Kav.Core.csproj @@ -49,6 +49,9 @@ Kav.Resources.LinearDepthEffect.fxb + + Kav.Resources.LinearDepthEffectInstanced.fxb + Kav.Resources.SkyboxEffect.fxb diff --git a/Kav.Framework.csproj b/Kav.Framework.csproj index 60a7849..494053c 100644 --- a/Kav.Framework.csproj +++ b/Kav.Framework.csproj @@ -7,7 +7,7 @@ Evan Hemsley 2020 true Kav - x64 + x64;x86 @@ -49,6 +49,9 @@ Kav.Resources.LinearDepthEffect.fxb + + Kav.Resources.LinearDepthEffectInstanced.fxb + Kav.Resources.SkyboxEffect.fxb diff --git a/Resources.cs b/Resources.cs index 2924998..da6c5e8 100644 --- a/Resources.cs +++ b/Resources.cs @@ -135,6 +135,18 @@ namespace Kav } } + public static byte[] LinearDepthEffectInstanced + { + get + { + if (linearDepthEffectInstanced == null) + { + linearDepthEffectInstanced = GetResource("LinearDepthEffectInstanced.fxb"); + } + return linearDepthEffectInstanced; + } + } + public static byte[] SkyboxEffect { get @@ -182,6 +194,7 @@ namespace Kav private static byte[] simpleDepthEffect; private static byte[] simpleDepthEffectInstanced; private static byte[] linearDepthEffect; + private static byte[] linearDepthEffectInstanced; private static byte[] skyboxEffect; private static byte[] diffuseLitSpriteEffect;