diff --git a/ANX.Framework.sln b/ANX.Framework.sln index f5f1a2f1..e4162f27 100644 --- a/ANX.Framework.sln +++ b/ANX.Framework.sln @@ -186,6 +186,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaveUtils", "SoundSystems\W EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DualTextureSample", "Samples\DualTextureSample\DualTextureSample.csproj", "{9259CC4E-AE6B-403C-8FAB-2408448C3935}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ANX.BaseDirectX", "RenderSystems\ANX.BaseDirectX\ANX.BaseDirectX.csproj", "{A4D3AD34-E49C-4142-8620-2AFF44ED6719}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -728,6 +730,16 @@ Global {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|Mixed Platforms.Build.0 = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.ActiveCfg = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.Build.0 = Release|x86 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|x86.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -748,6 +760,7 @@ Global {B30DE9C2-0926-46B6-8351-9AF276C472D5} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {FF0AB665-2796-4354-9630-76C2751DB3C2} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {2B71A7C2-0D18-4E3D-AE5A-320641D1162A} = {D421509A-9AE3-4D7E-881B-EAFED598B028} + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {F945515B-394D-4ED4-80E0-98EB59B69D24} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {AAA20E99-A897-4C91-A23E-D02B8F08ACA5} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {BC79B021-10E4-4D01-945A-7048FFF53A22} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} diff --git a/ANX.Framework_Linux.sln b/ANX.Framework_Linux.sln index e1db0428..b6d14a71 100644 --- a/ANX.Framework_Linux.sln +++ b/ANX.Framework_Linux.sln @@ -186,6 +186,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaveUtils", "SoundSystems\W EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DualTextureSample", "Samples\DualTextureSample\DualTextureSample_Linux.csproj", "{9259CC4E-AE6B-403C-8FAB-2408448C3935}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ANX.BaseDirectX", "RenderSystems\ANX.BaseDirectX\ANX.BaseDirectX_Linux.csproj", "{A4D3AD34-E49C-4142-8620-2AFF44ED6719}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -728,6 +730,16 @@ Global {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|Mixed Platforms.Build.0 = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.ActiveCfg = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.Build.0 = Release|x86 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|x86.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -748,6 +760,7 @@ Global {B30DE9C2-0926-46B6-8351-9AF276C472D5} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {FF0AB665-2796-4354-9630-76C2751DB3C2} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {2B71A7C2-0D18-4E3D-AE5A-320641D1162A} = {D421509A-9AE3-4D7E-881B-EAFED598B028} + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {F945515B-394D-4ED4-80E0-98EB59B69D24} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {AAA20E99-A897-4C91-A23E-D02B8F08ACA5} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {BC79B021-10E4-4D01-945A-7048FFF53A22} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} diff --git a/ANX.Framework_PSVita.sln b/ANX.Framework_PSVita.sln index e7dba3f6..76dd8cb1 100644 --- a/ANX.Framework_PSVita.sln +++ b/ANX.Framework_PSVita.sln @@ -186,6 +186,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaveUtils", "SoundSystems\W EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DualTextureSample", "Samples\DualTextureSample\DualTextureSample_PSVita.csproj", "{9259CC4E-AE6B-403C-8FAB-2408448C3935}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ANX.BaseDirectX", "RenderSystems\ANX.BaseDirectX\ANX.BaseDirectX_PSVita.csproj", "{A4D3AD34-E49C-4142-8620-2AFF44ED6719}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -728,6 +730,16 @@ Global {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|Mixed Platforms.Build.0 = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.ActiveCfg = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.Build.0 = Release|x86 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|x86.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -748,6 +760,7 @@ Global {B30DE9C2-0926-46B6-8351-9AF276C472D5} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {FF0AB665-2796-4354-9630-76C2751DB3C2} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {2B71A7C2-0D18-4E3D-AE5A-320641D1162A} = {D421509A-9AE3-4D7E-881B-EAFED598B028} + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {F945515B-394D-4ED4-80E0-98EB59B69D24} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {AAA20E99-A897-4C91-A23E-D02B8F08ACA5} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {BC79B021-10E4-4D01-945A-7048FFF53A22} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} diff --git a/ANX.Framework_WindowsMetro.sln b/ANX.Framework_WindowsMetro.sln index 9436dfc7..3d992977 100644 --- a/ANX.Framework_WindowsMetro.sln +++ b/ANX.Framework_WindowsMetro.sln @@ -186,6 +186,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaveUtils", "SoundSystems\W EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DualTextureSample", "Samples\DualTextureSample\DualTextureSample_WindowsMetro.csproj", "{9259CC4E-AE6B-403C-8FAB-2408448C3935}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ANX.BaseDirectX", "RenderSystems\ANX.BaseDirectX\ANX.BaseDirectX_WindowsMetro.csproj", "{A4D3AD34-E49C-4142-8620-2AFF44ED6719}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -728,6 +730,16 @@ Global {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|Mixed Platforms.Build.0 = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.ActiveCfg = Release|x86 {9259CC4E-AE6B-403C-8FAB-2408448C3935}.Release|x86.Build.0 = Release|x86 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Debug|x86.ActiveCfg = Debug|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Any CPU.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {A4D3AD34-E49C-4142-8620-2AFF44ED6719}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -748,6 +760,7 @@ Global {B30DE9C2-0926-46B6-8351-9AF276C472D5} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {FF0AB665-2796-4354-9630-76C2751DB3C2} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {2B71A7C2-0D18-4E3D-AE5A-320641D1162A} = {D421509A-9AE3-4D7E-881B-EAFED598B028} + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} = {D421509A-9AE3-4D7E-881B-EAFED598B028} {F945515B-394D-4ED4-80E0-98EB59B69D24} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {AAA20E99-A897-4C91-A23E-D02B8F08ACA5} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} {BC79B021-10E4-4D01-945A-7048FFF53A22} = {9B0DD48B-3912-4F33-AF3F-691AF02B73F9} diff --git a/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX.csproj b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX.csproj new file mode 100644 index 00000000..f1fa41ce --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX.csproj @@ -0,0 +1,73 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + Library + Properties + ANX.BaseDirectX + ANX.BaseDirectX + v4.0 + 512 + Client + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + x86 + true + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + x86 + true + + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.dll + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.DXGI.dll + + + + + + + + + + + + + + + + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + + + + + \ No newline at end of file diff --git a/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_Linux.csproj b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_Linux.csproj new file mode 100644 index 00000000..bba45b70 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_Linux.csproj @@ -0,0 +1,72 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + Library + Properties + ANX.BaseDirectX + ANX.BaseDirectX + v4.0 + 512 + + + true + full + false + bin\Debug\ + TRACE;DEBUG;LINUX; + prompt + 4 + x86 + true + + + pdbonly + true + bin\Release\ + TRACE;LINUX; + prompt + 4 + x86 + true + + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.dll + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.DXGI.dll + + + + + + + + + + + + + + + + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + + + + + \ No newline at end of file diff --git a/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_PSVita.csproj b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_PSVita.csproj new file mode 100644 index 00000000..2e0550ed --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_PSVita.csproj @@ -0,0 +1,73 @@ + + + + Debug + AnyCPU + 10.0.0 + 2.0 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + Library + Properties + ANX.BaseDirectX + ANX.BaseDirectX + 512 + {69878862-DA7D-4DC6-B0A1-50D8FAB4242F};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + + true + full + false + bin\Debug\ + TRACE;DEBUG;PSVITA; + prompt + 4 + x86 + true + + + pdbonly + true + bin\Release\ + TRACE;PSVITA; + prompt + 4 + x86 + true + + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.dll + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.DXGI.dll + + + + + + + + + + + + + + + + + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + + + + + \ No newline at end of file diff --git a/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_WindowsMetro.csproj b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_WindowsMetro.csproj new file mode 100644 index 00000000..251c3d06 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/ANX.BaseDirectX_WindowsMetro.csproj @@ -0,0 +1,78 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + Library + Properties + ANX.BaseDirectX + ANX.BaseDirectX + 512 + {BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + en-US + Test_TemporaryKey.pfx + + + true + full + false + bin\Debug\ + TRACE;DEBUG;WINDOWSMETRO; + prompt + 4 + x86 + true + + + pdbonly + true + bin\Release\ + TRACE;WINDOWSMETRO; + prompt + 4 + x86 + true + + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.dll + + + False + ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.DXGI.dll + + + + + + + + + + + + + + + + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + + + + + + 11.0 + + + \ No newline at end of file diff --git a/RenderSystems/ANX.BaseDirectX/BaseEffectTechnique.cs b/RenderSystems/ANX.BaseDirectX/BaseEffectTechnique.cs new file mode 100644 index 00000000..d098efa6 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/BaseEffectTechnique.cs @@ -0,0 +1,25 @@ +using System; +using ANX.Framework.Graphics; + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX +{ + public abstract class BaseEffectTechnique where T : class, IDisposable + { + protected Effect parentEffect; + + public T NativeTechnique { get; protected set; } + + public BaseEffectTechnique(Effect parentEffect, T nativeTechnique) + { + if (parentEffect == null) + throw new ArgumentNullException("parentEffect"); + + this.parentEffect = parentEffect; + NativeTechnique = nativeTechnique; + } + } +} diff --git a/RenderSystems/ANX.BaseDirectX/BaseFormatConverter.cs b/RenderSystems/ANX.BaseDirectX/BaseFormatConverter.cs new file mode 100644 index 00000000..7380db6f --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/BaseFormatConverter.cs @@ -0,0 +1,158 @@ +using System; +using ANX.Framework.Graphics; +using SharpDX.Direct3D; +using SharpDX.DXGI; + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX +{ + public static class BaseFormatConverter + { + #region FormatSize (SurfaceFormat) + public static int FormatSize(SurfaceFormat format) + { + switch (format) + { + case SurfaceFormat.Vector4: + return 16; + //case SurfaceFormat.Vector3: + // return 12; + case SurfaceFormat.Vector2: + return 8; + case SurfaceFormat.Single: + case SurfaceFormat.Color: + //case SurfaceFormat.RGBA1010102: + //case SurfaceFormat.RG32: + return 4; + //case SurfaceFormat.BGR565: + //case SurfaceFormat.BGRA5551: + // return 2; + case SurfaceFormat.Dxt1: + case SurfaceFormat.Dxt3: + case SurfaceFormat.Dxt5: + case SurfaceFormat.Alpha8: + return 1; + } + + throw new ArgumentException("Invalid format '" + format + "'."); + } + #endregion + + #region Translate (SurfaceFormat) + public static SharpDX.DXGI.Format Translate(SurfaceFormat surfaceFormat) + { + switch (surfaceFormat) + { + case SurfaceFormat.Color: + return SharpDX.DXGI.Format.R8G8B8A8_UNorm; + case SurfaceFormat.Dxt3: + return SharpDX.DXGI.Format.BC2_UNorm; + case SurfaceFormat.Dxt5: + return SharpDX.DXGI.Format.BC3_UNorm; + } + + throw new Exception("can't translate SurfaceFormat: " + surfaceFormat.ToString()); + } + #endregion + + #region Translate (DepthFormat) + public static Format Translate(DepthFormat depthFormat) + { + switch (depthFormat) + { + case DepthFormat.Depth16: + return Format.D16_UNorm; + case DepthFormat.Depth24: + //TODO: no DirectX10 24Bit depth format??? + case DepthFormat.Depth24Stencil8: + return Format.D24_UNorm_S8_UInt; + case DepthFormat.None: + return Format.Unknown; + } + + throw new Exception("can't translate DepthFormat: " + depthFormat.ToString()); + } + #endregion + + #region Translate (Format) + public static SurfaceFormat Translate(SharpDX.DXGI.Format format) + { + switch (format) + { + case SharpDX.DXGI.Format.R8G8B8A8_UNorm: + return SurfaceFormat.Color; + case SharpDX.DXGI.Format.BC2_UNorm: + return SurfaceFormat.Dxt3; + case SharpDX.DXGI.Format.BC3_UNorm: + return SurfaceFormat.Dxt5; + } + + throw new Exception("can't translate Format: " + format.ToString()); + } + #endregion + + #region Translate (PrimitiveType) + public static PrimitiveTopology Translate(PrimitiveType primitiveType) + { + switch (primitiveType) + { + case PrimitiveType.LineList: + return PrimitiveTopology.LineList; + case PrimitiveType.LineStrip: + return PrimitiveTopology.LineStrip; + case PrimitiveType.TriangleList: + return PrimitiveTopology.TriangleList; + case PrimitiveType.TriangleStrip: + return PrimitiveTopology.TriangleStrip; + } + + throw new InvalidOperationException("unknown PrimitiveType: " + primitiveType); + } + #endregion + + #region Translate (IndexElementSize) + public static SharpDX.DXGI.Format Translate(IndexElementSize indexElementSize) + { + switch (indexElementSize) + { + case IndexElementSize.SixteenBits: + return Format.R16_UInt; + case IndexElementSize.ThirtyTwoBits: + return Format.R32_UInt; + } + + throw new InvalidOperationException("unknown IndexElementSize: " + indexElementSize); + } + #endregion + + #region Translate (VertexElement) + public static string Translate(ref VertexElement element) + { + //TODO: map the other Usages + if (element.VertexElementUsage == VertexElementUsage.TextureCoordinate) + return "TEXCOORD"; + else + return element.VertexElementUsage.ToString().ToUpperInvariant(); + } + #endregion + + #region CalculateVertexCount + public static int CalculateVertexCount(PrimitiveType type, int primitiveCount) + { + if (type == PrimitiveType.TriangleList) + return primitiveCount * 3; + else if (type == PrimitiveType.LineList) + return primitiveCount * 2; + else if (type == PrimitiveType.LineStrip) + return primitiveCount + 1; + else if (type == PrimitiveType.TriangleStrip) + return primitiveCount + 2; + + throw new NotImplementedException("Couldn't calculate vertex count for PrimitiveType '" + type + "'."); + } + #endregion + } +} diff --git a/RenderSystems/ANX.BaseDirectX/BaseIndexBuffer.cs b/RenderSystems/ANX.BaseDirectX/BaseIndexBuffer.cs new file mode 100644 index 00000000..c7536678 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/BaseIndexBuffer.cs @@ -0,0 +1,94 @@ +using System; +using ANX.Framework.Graphics; +using SharpDX; +using System.IO; + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX +{ + public abstract class BaseIndexBuffer : IDisposable where T : class, IDisposable + { + protected IndexElementSize elementSize; + public T NativeBuffer { get; protected set; } + + #region SetData + public void SetData(GraphicsDevice graphicsDevice, S[] data) where S : struct + { + SetData(graphicsDevice, data, 0, data.Length); + } + + public void SetData(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct + { + SetData(graphicsDevice, 0, data, startIndex, elementCount); + } + + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) + where S : struct + { + //TODO: check offsetInBytes parameter for bounds etc. + + using (var stream = MapBufferWrite()) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); + + if (startIndex > 0 || elementCount < data.Length) + for (int i = startIndex; i < startIndex + elementCount; i++) + stream.Write(data[i]); + else + for (int i = 0; i < data.Length; i++) + stream.Write(data[i]); + + UnmapBuffer(); + } + } + #endregion + + #region GetData + public void GetData(S[] data) where S : struct + { + GetData(0, data, 0, data.Length); + } + + public void GetData(S[] data, int startIndex, int elementCount) where S : struct + { + GetData(0, data, 0, data.Length); + } + + public void GetData(int offsetInBytes, S[] data, int startIndex, int elementCount) where S : struct + { + using (var stream = MapBufferRead()) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); + + stream.ReadRange(data, startIndex, elementCount); + UnmapBuffer(); + } + } + #endregion + + protected int GetSizeInBytes(int indexCount) + { + return (elementSize == IndexElementSize.SixteenBits ? 2 : 4) * indexCount; + } + + protected abstract DataStream MapBufferWrite(); + protected abstract DataStream MapBufferRead(); + protected abstract void UnmapBuffer(); + + #region Dispose + public void Dispose() + { + if (NativeBuffer != null) + { + NativeBuffer.Dispose(); + NativeBuffer = null; + } + } + #endregion + } +} diff --git a/RenderSystems/ANX.BaseDirectX/BaseStateObject.cs b/RenderSystems/ANX.BaseDirectX/BaseStateObject.cs new file mode 100644 index 00000000..9d3f9ac0 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/BaseStateObject.cs @@ -0,0 +1,86 @@ +using System; +using ANX.Framework.Graphics; + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX +{ + public abstract class BaseStateObject : IDisposable where T : class, IDisposable + { + protected const int IntMaxOver16 = int.MaxValue / 16; + protected const float ColorByteToFloatFactor = 1f / 255f; + + #region Private + protected bool isDirty; + protected T nativeState; + #endregion + + #region Public + public bool IsBound { get; protected set; } + #endregion + + #region Constructor + protected BaseStateObject() + { + isDirty = true; + Init(); + } + #endregion + + #region Release + public void Release() + { + IsBound = false; + } + #endregion + + #region SetValueIfDifferentAndMarkDirty + protected void SetValueIfDifferentAndMarkDirty(ref T oldValue, ref T newValue) + { + if (oldValue.Equals(newValue) == false) + { + isDirty = true; + oldValue = newValue; + } + } + #endregion + + #region Dispose + public void Dispose() + { + if (nativeState != null) + { + nativeState.Dispose(); + nativeState = null; + } + } + #endregion + + #region Apply + public void Apply(GraphicsDevice graphics) + { + UpdateNativeBlendState(graphics); + IsBound = true; + ApplyNativeState(graphics); + } + #endregion + + #region UpdateNativeBlendState + private void UpdateNativeBlendState(GraphicsDevice graphics) + { + if (isDirty || nativeState == null) + { + Dispose(); + nativeState = CreateNativeState(graphics); + isDirty = false; + } + } + #endregion + + protected virtual void Init() { } + protected abstract T CreateNativeState(GraphicsDevice graphics); + protected abstract void ApplyNativeState(GraphicsDevice graphics); + } +} diff --git a/RenderSystems/ANX.BaseDirectX/BaseTexture2D.cs b/RenderSystems/ANX.BaseDirectX/BaseTexture2D.cs new file mode 100644 index 00000000..6f8379a8 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/BaseTexture2D.cs @@ -0,0 +1,182 @@ +using System; +using System.Runtime.InteropServices; +using ANX.Framework.Graphics; +using SharpDX; + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX +{ + public abstract class BaseTexture2D : IDisposable where S : class, IDisposable + { + #region Private + protected int tempSubresource; + protected int pitch; + protected int formatSize; + protected SurfaceFormat surfaceFormat; + + protected bool IsDxtTexture + { + get + { + return surfaceFormat == SurfaceFormat.Dxt5 || surfaceFormat == SurfaceFormat.Dxt3 || + surfaceFormat == SurfaceFormat.Dxt1; + } + } + #endregion + + #region Public + public abstract int Width { get; } + public abstract int Height { get; } + + public S NativeTexture + { + get; + protected set; + } + + public GraphicsDevice GraphicsDevice + { + get; + protected set; + } + #endregion + + #region Constructor + protected BaseTexture2D(GraphicsDevice graphicsDevice) + { + GraphicsDevice = graphicsDevice; + } + + protected BaseTexture2D(GraphicsDevice graphicsDevice, SurfaceFormat setSurfaceFormat) + { + GraphicsDevice = graphicsDevice; + surfaceFormat = setSurfaceFormat; + + // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx + // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx + formatSize = BaseFormatConverter.FormatSize(surfaceFormat); + } + #endregion + + #region SetData + public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct + { + SetData(graphicsDevice, 0, data, 0, data.Length); + } + + public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct + { + SetData(graphicsDevice, 0, data, startIndex, elementCount); + } + + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) + where T : struct + { + //TODO: handle offsetInBytes parameter + //TODO: handle startIndex parameter + //TODO: handle elementCount parameter + + if (surfaceFormat == SurfaceFormat.Color) + { + IntPtr dataPtr = MapWrite(); + + unsafe + { + GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); + byte* colorData = (byte*)handle.AddrOfPinnedObject(); + + byte* pTexels = (byte*)dataPtr; + int srcIndex = 0; + + for (int row = 0; row < Height; row++) + { + int rowStart = row * pitch; + + for (int col = 0; col < Width; col++) + { + int colStart = col * formatSize; + pTexels[rowStart + colStart + 0] = colorData[srcIndex++]; + pTexels[rowStart + colStart + 1] = colorData[srcIndex++]; + pTexels[rowStart + colStart + 2] = colorData[srcIndex++]; + pTexels[rowStart + colStart + 3] = colorData[srcIndex++]; + } + } + + handle.Free(); + } + + Unmap(); + } + else if (IsDxtTexture) + { + unsafe + { + GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); + byte* colorData = (byte*)handle.AddrOfPinnedObject(); + + int w = (Width + 3) >> 2; + int h = (Height + 3) >> 2; + formatSize = (surfaceFormat == SurfaceFormat.Dxt1) ? 8 : 16; + + IntPtr dataPtr = MapWrite(); + var ds = new DataStream(dataPtr, Width * Height * 4 * 2, true, true); + int col = 0; + int index = 0; // startIndex + int count = data.Length; // elementCount + int actWidth = w * formatSize; + + for (int i = 0; i < h; i++) + { + ds.Position = (i * pitch) + (col * formatSize); + if (count <= 0) + break; + else if (count < actWidth) + { + for (int idx = index; idx < index + count; idx++) + ds.WriteByte(colorData[idx]); + break; + } + + for (int idx = index; idx < index + actWidth; idx++) + ds.WriteByte(colorData[idx]); + + index += actWidth; + count -= actWidth; + } + + handle.Free(); + + Unmap(); + } + } + else + throw new Exception(String.Format("creating textures of format {0} not yet implemented...", surfaceFormat)); + } + #endregion + + #region SetData (TODO) + public void SetData(int level, Framework.Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct + { + throw new NotImplementedException(); + } + #endregion + + protected abstract IntPtr MapWrite(); + protected abstract IntPtr MapRead(); + protected abstract void Unmap(); + + #region Dispose + public virtual void Dispose() + { + if (NativeTexture != null) + { + NativeTexture.Dispose(); + NativeTexture = null; + } + } + #endregion + } +} diff --git a/RenderSystems/ANX.BaseDirectX/BaseVertexBuffer.cs b/RenderSystems/ANX.BaseDirectX/BaseVertexBuffer.cs new file mode 100644 index 00000000..c43d1770 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/BaseVertexBuffer.cs @@ -0,0 +1,96 @@ +using System; +using System.IO; +using ANX.Framework.Graphics; +using SharpDX; + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX +{ + public abstract class BaseVertexBuffer : IDisposable where T : class, IDisposable + { + protected int vertexStride; + public T NativeBuffer { get; protected set; } + + #region SetData + public void SetData(GraphicsDevice graphicsDevice, S[] data) where S : struct + { + SetData(graphicsDevice, data, 0, data.Length); + } + + public void SetData(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct + { + SetData(graphicsDevice, 0, data, startIndex, elementCount); + } + + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) + where S : struct + { + //TODO: check offsetInBytes parameter for bounds etc. + + using (var stream = MapBufferWrite()) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); + + if (startIndex > 0 || elementCount < data.Length) + for (int i = startIndex; i < startIndex + elementCount; i++) + stream.Write(data[i]); + else + for (int i = 0; i < data.Length; i++) + stream.Write(data[i]); + + UnmapBuffer(); + } + } + + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount, + int vertexStride) where S : struct + { + throw new NotImplementedException(); + } + #endregion + + #region GetData + public void GetData(S[] data) where S : struct + { + GetData(data, 0, data.Length); + } + + public void GetData(S[] data, int startIndex, int elementCount) where S : struct + { + GetData(0, data, startIndex, elementCount, vertexStride); + } + + public void GetData(int offsetInBytes, S[] data, int startIndex, int elementCount, int vertexStride) + where S : struct + { + using (var stream = MapBufferRead()) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); + + stream.ReadRange(data, startIndex, elementCount); + UnmapBuffer(); + } + } + #endregion + + protected abstract DataStream MapBufferWrite(); + protected abstract DataStream MapBufferRead(); + protected abstract void UnmapBuffer(); + + #region Dispose + public void Dispose() + { + if (NativeBuffer != null) + { + NativeBuffer.Dispose(); + NativeBuffer = null; + } + } + #endregion + } +} diff --git a/RenderSystems/ANX.BaseDirectX/Properties/AssemblyInfo.cs b/RenderSystems/ANX.BaseDirectX/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..3166f466 --- /dev/null +++ b/RenderSystems/ANX.BaseDirectX/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("ANX.BaseDirectX")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("ANX.Framework Team")] +[assembly: AssemblyProduct("ANX.BaseDirectX")] +[assembly: AssemblyCopyright("Copyright © ANX.Framework Team 2012")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("380bb0b2-4535-4b10-b6a7-4c7fc453460f")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RenderSystems/ANX.Framework.Windows.DX10/Helpers/WindowHelper.cs b/RenderSystems/ANX.BaseDirectX/WindowHelper.cs similarity index 77% rename from RenderSystems/ANX.Framework.Windows.DX10/Helpers/WindowHelper.cs rename to RenderSystems/ANX.BaseDirectX/WindowHelper.cs index a9c33ae1..1c14ce91 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/Helpers/WindowHelper.cs +++ b/RenderSystems/ANX.BaseDirectX/WindowHelper.cs @@ -2,17 +2,21 @@ using System.Runtime.InteropServices; using ANX.Framework.Graphics; -namespace ANX.RenderSystem.Windows.DX10.Helpers +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +namespace ANX.BaseDirectX { - internal static class WindowHelper + public static class WindowHelper { [StructLayout(LayoutKind.Sequential)] public struct RECT { - public int Left; // x position of upper-left corner - public int Top; // y position of upper-left corner - public int Right; // x position of lower-right corner - public int Bottom; // y position of lower-right corner + public int Left; + public int Top; + public int Right; + public int Bottom; } [DllImport("user32.dll")] diff --git a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj index 4d3a6b37..e30e9125 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj +++ b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj @@ -85,7 +85,6 @@ - @@ -102,6 +101,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + diff --git a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_Linux.csproj b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_Linux.csproj index 1af8d4c7..d5a8819b 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_Linux.csproj +++ b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_Linux.csproj @@ -85,7 +85,6 @@ - @@ -102,6 +101,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + diff --git a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_PSVita.csproj b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_PSVita.csproj index c4c28039..b756ce45 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_PSVita.csproj +++ b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_PSVita.csproj @@ -86,7 +86,6 @@ - @@ -103,6 +102,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + diff --git a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_WindowsMetro.csproj b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_WindowsMetro.csproj index f86133c5..0a33517e 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_WindowsMetro.csproj +++ b/RenderSystems/ANX.Framework.Windows.DX10/ANX.RenderSystem.Windows.DX10_WindowsMetro.csproj @@ -87,7 +87,6 @@ - @@ -104,6 +103,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + diff --git a/RenderSystems/ANX.Framework.Windows.DX10/BlendState_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/BlendState_DX10.cs index 2e304c81..bed48119 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/BlendState_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/BlendState_DX10.cs @@ -1,3 +1,5 @@ +using System; +using ANX.BaseDirectX; using ANX.Framework; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; @@ -10,25 +12,13 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class BlendState_DX10 : INativeBlendState + public class BlendState_DX10 : BaseStateObject, INativeBlendState { - private const float ColorByteToFloatFactor = 1f / 255f; - - #region Private private Dx10.BlendStateDescription description; - private Dx10.BlendState nativeBlendState; - private bool isDirty; private SharpDX.Color4 blendFactor; private int multiSampleMask; - #endregion #region Public - public bool IsBound - { - get; - private set; - } - public Color BlendFactor { set @@ -53,7 +43,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.BlendOperation alphaBlendOperation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.AlphaBlendOperation, ref alphaBlendOperation); + SetValueIfDifferentAndMarkDirty(ref description.AlphaBlendOperation, ref alphaBlendOperation); } } @@ -62,7 +52,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.BlendOperation blendOperation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.BlendOperation, ref blendOperation); + SetValueIfDifferentAndMarkDirty(ref description.BlendOperation, ref blendOperation); } } @@ -71,7 +61,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.BlendOption destinationAlphaBlend = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.DestinationAlphaBlend, ref destinationAlphaBlend); + SetValueIfDifferentAndMarkDirty(ref description.DestinationAlphaBlend, ref destinationAlphaBlend); } } @@ -80,7 +70,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.BlendOption destinationBlend = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.DestinationBlend, ref destinationBlend); + SetValueIfDifferentAndMarkDirty(ref description.DestinationBlend, ref destinationBlend); } } @@ -89,7 +79,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.ColorWriteMaskFlags writeMask = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.RenderTargetWriteMask[0], ref writeMask); + SetValueIfDifferentAndMarkDirty(ref description.RenderTargetWriteMask[0], ref writeMask); } } @@ -98,7 +88,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.ColorWriteMaskFlags writeMask = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.RenderTargetWriteMask[1], ref writeMask); + SetValueIfDifferentAndMarkDirty(ref description.RenderTargetWriteMask[1], ref writeMask); } } @@ -107,7 +97,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.ColorWriteMaskFlags writeMask = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.RenderTargetWriteMask[2], ref writeMask); + SetValueIfDifferentAndMarkDirty(ref description.RenderTargetWriteMask[2], ref writeMask); } } @@ -116,7 +106,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.ColorWriteMaskFlags writeMask = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.RenderTargetWriteMask[3], ref writeMask); + SetValueIfDifferentAndMarkDirty(ref description.RenderTargetWriteMask[3], ref writeMask); } } @@ -125,7 +115,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.BlendOption sourceAlphaBlend = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.SourceAlphaBlend, ref sourceAlphaBlend); + SetValueIfDifferentAndMarkDirty(ref description.SourceAlphaBlend, ref sourceAlphaBlend); } } @@ -134,71 +124,27 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.BlendOption sourceBlend = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.SourceBlend, ref sourceBlend); + SetValueIfDifferentAndMarkDirty(ref description.SourceBlend, ref sourceBlend); } } #endregion - #region Constructor - public BlendState_DX10() + protected override void Init() { - isDirty = true; - for (int i = 0; i < description.IsBlendEnabled.Length; i++) - description.IsBlendEnabled[i] = (i < 4); - } - #endregion - - #region Apply - public void Apply(GraphicsDevice graphics) - { - Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; - - UpdateNativeBlendState(device); - IsBound = true; - - device.OutputMerger.SetBlendState(nativeBlendState, blendFactor, multiSampleMask); - } - #endregion - - #region Release - public void Release() - { - IsBound = false; + for (int i = 0; i < description.IsBlendEnabled.Length; i++) + description.IsBlendEnabled[i] = (i < 4); } - #endregion - #region Dispose - public void Dispose() - { - if (nativeBlendState != null) - { - nativeBlendState.Dispose(); - nativeBlendState = null; - } - } - #endregion - - #region UpdateNativeBlendState - private void UpdateNativeBlendState(Dx10.Device device) - { - if (isDirty || nativeBlendState == null) - { - Dispose(); - nativeBlendState = new Dx10.BlendState(device, ref description); - isDirty = false; - } - } - #endregion - - #region UpdateValueAndMarkDirtyIfNeeded - private void UpdateValueAndMarkDirtyIfNeeded(ref T currentValue, ref T value) + protected override Dx10.BlendState CreateNativeState(GraphicsDevice graphics) { - if (value.Equals(currentValue) == false) - { - isDirty = true; - currentValue = value; - } + Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + return new Dx10.BlendState(device, ref description); + } + + protected override void ApplyNativeState(GraphicsDevice graphics) + { + Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + device.OutputMerger.SetBlendState(nativeState, blendFactor, multiSampleMask); } - #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs b/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs index e33b0816..40631445 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs @@ -8,6 +8,7 @@ using ANX.Framework.NonXNA; using ANX.Framework.NonXNA.RenderSystem; using ANX.RenderSystem.Windows.DX10.Helpers; using SharpDX.DXGI; +using ANX.BaseDirectX; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -197,7 +198,7 @@ namespace ANX.RenderSystem.Windows.DX10 { DisplayMode displayMode = new DisplayMode() { - Format = FormatConverter.Translate(modeDescription.Format), + Format = BaseFormatConverter.Translate(modeDescription.Format), Width = modeDescription.Width, Height = modeDescription.Height, AspectRatio = (float)modeDescription.Width / (float)modeDescription.Height, diff --git a/RenderSystems/ANX.Framework.Windows.DX10/DepthStencilState_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/DepthStencilState_DX10.cs index 9904ad90..0f20128f 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/DepthStencilState_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/DepthStencilState_DX10.cs @@ -1,3 +1,5 @@ +using System; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; using ANX.RenderSystem.Windows.DX10.Helpers; @@ -9,28 +11,18 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class DepthStencilState_DX10 : INativeDepthStencilState + public class DepthStencilState_DX10 : BaseStateObject, INativeDepthStencilState { - #region Private private Dx10.DepthStencilStateDescription description; - private Dx10.DepthStencilState nativeDepthStencilState; - private bool isDirty; private int referenceStencil; - #endregion #region Public (TODO) - public bool IsBound - { - get; - private set; - } - public StencilOperation CounterClockwiseStencilDepthBufferFail { set { Dx10.StencilOperation operation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.BackFace.DepthFailOperation, ref operation); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.DepthFailOperation, ref operation); } } @@ -39,7 +31,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.StencilOperation operation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.BackFace.FailOperation, ref operation); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.FailOperation, ref operation); } } @@ -48,7 +40,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.Comparison comparison = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.BackFace.Comparison, ref comparison); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.Comparison, ref comparison); } } @@ -57,7 +49,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.StencilOperation operation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.BackFace.PassOperation, ref operation); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.PassOperation, ref operation); } } @@ -78,7 +70,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.Comparison comparison = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.DepthComparison, ref comparison); + SetValueIfDifferentAndMarkDirty(ref description.DepthComparison, ref comparison); } } @@ -87,7 +79,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.DepthWriteMask writeMask = value ? Dx10.DepthWriteMask.All : Dx10.DepthWriteMask.Zero; - UpdateValueAndMarkDirtyIfNeeded(ref description.DepthWriteMask, ref writeMask); + SetValueIfDifferentAndMarkDirty(ref description.DepthWriteMask, ref writeMask); } } @@ -95,7 +87,7 @@ namespace ANX.RenderSystem.Windows.DX10 { set { - UpdateValueAndMarkDirtyIfNeeded(ref referenceStencil, ref value); + SetValueIfDifferentAndMarkDirty(ref referenceStencil, ref value); } } @@ -104,7 +96,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.StencilOperation operation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.FrontFace.DepthFailOperation, ref operation); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.DepthFailOperation, ref operation); } } @@ -125,7 +117,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.StencilOperation operation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.FrontFace.FailOperation, ref operation); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.FailOperation, ref operation); } } @@ -134,7 +126,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.Comparison comparison = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.FrontFace.Comparison, ref comparison); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.Comparison, ref comparison); } } @@ -143,7 +135,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { byte stencilMask = (byte)value; //TODO: check range - UpdateValueAndMarkDirtyIfNeeded(ref description.StencilReadMask, ref stencilMask); + SetValueIfDifferentAndMarkDirty(ref description.StencilReadMask, ref stencilMask); } } @@ -152,7 +144,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.StencilOperation operation = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.FrontFace.PassOperation, ref operation); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.PassOperation, ref operation); } } @@ -161,7 +153,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { byte stencilWriteMask = (byte)value; //TODO: check range - UpdateValueAndMarkDirtyIfNeeded(ref description.StencilWriteMask, ref stencilWriteMask); + SetValueIfDifferentAndMarkDirty(ref description.StencilWriteMask, ref stencilWriteMask); } } @@ -174,63 +166,16 @@ namespace ANX.RenderSystem.Windows.DX10 } #endregion - #region Constructor - public DepthStencilState_DX10() - { - isDirty = true; - } - #endregion - - #region Apply - public void Apply(GraphicsDevice graphicsDevice) - { - Dx10.Device device = (graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; - UpdateNativeDepthStencilState(device); - IsBound = true; - - device.OutputMerger.SetDepthStencilState(nativeDepthStencilState, referenceStencil); - } - #endregion - - #region Release - public void Release() - { - IsBound = false; - } - #endregion - - #region Dispose - public void Dispose() - { - if (nativeDepthStencilState != null) - { - nativeDepthStencilState.Dispose(); - nativeDepthStencilState = null; - } - } - #endregion - - #region UpdateNativeDepthStencilState - private void UpdateNativeDepthStencilState(Dx10.Device device) - { - if (isDirty == true || nativeDepthStencilState == null) - { - Dispose(); - nativeDepthStencilState = new Dx10.DepthStencilState(device, ref description); - isDirty = false; - } - } - #endregion - - #region UpdateValueAndMarkDirtyIfNeeded - private void UpdateValueAndMarkDirtyIfNeeded(ref T currentValue, ref T value) + protected override Dx10.DepthStencilState CreateNativeState(GraphicsDevice graphics) { - if (value.Equals(currentValue) == false) - { - isDirty = true; - currentValue = value; - } + Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + return new Dx10.DepthStencilState(device, ref description); + } + + protected override void ApplyNativeState(GraphicsDevice graphics) + { + Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + device.OutputMerger.SetDepthStencilState(nativeState, referenceStencil); } - #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/EffectParameter_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/EffectParameter_DX10.cs index ac41e999..2e7b61e0 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/EffectParameter_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/EffectParameter_DX10.cs @@ -149,9 +149,6 @@ namespace ANX.RenderSystem.Windows.DX10 throw new ArgumentNullException("value"); var tex = value.NativeTexture as Texture2D_DX10; - var graphicsDX10 = tex.GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10; - Dx10.Device device = graphicsDX10.NativeDevice; - NativeParameter.AsShaderResource().SetResource(tex.NativeShaderResourceView); } #endregion diff --git a/RenderSystems/ANX.Framework.Windows.DX10/EffectPass_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/EffectPass_DX10.cs index e3896d7c..0127490d 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/EffectPass_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/EffectPass_DX10.cs @@ -8,20 +8,20 @@ using SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { public class EffectPass_DX10 : INativeEffectPass - { - public EffectPass NativePass { get; internal set; } + { + private EffectPass nativePass; public string Name { get { - return NativePass.Description.Name; + return nativePass.Description.Name; } } internal EffectPass_DX10(EffectPass setNativePass) { - NativePass = setNativePass; + nativePass = setNativePass; } } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/EffectTechnique_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/EffectTechnique_DX10.cs index 20437cf4..ae008b87 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/EffectTechnique_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/EffectTechnique_DX10.cs @@ -1,5 +1,5 @@ -using System; using System.Collections.Generic; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; using Dx10 = SharpDX.Direct3D10; @@ -10,12 +10,8 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class EffectTechnique_DX10 : INativeEffectTechnique + public class EffectTechnique_DX10 : BaseEffectTechnique, INativeEffectTechnique { - private Effect parentEffect; - - public Dx10.EffectTechnique NativeTechnique { get; internal set; } - public string Name { get @@ -30,20 +26,16 @@ namespace ANX.RenderSystem.Windows.DX10 { for (int i = 0; i < NativeTechnique.Description.PassCount; i++) { - EffectPass_DX10 passDx10 = new EffectPass_DX10(NativeTechnique.GetPassByIndex(i)); - EffectPass pass = new EffectPass(this.parentEffect); + var passDx10 = new EffectPass_DX10(NativeTechnique.GetPassByIndex(i)); // TODO: wire up native pass and managed pass? - yield return pass; + yield return new EffectPass(this.parentEffect); } } } - internal EffectTechnique_DX10(Effect parentEffect) + public EffectTechnique_DX10(Effect parentEffect, Dx10.EffectTechnique nativeTechnique) + : base(parentEffect, nativeTechnique) { - if (parentEffect == null) - throw new ArgumentNullException("parentEffect"); - - this.parentEffect = parentEffect; } } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/Effect_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/Effect_DX10.cs index feb2a2d8..fd6fb17a 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/Effect_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/Effect_DX10.cs @@ -34,8 +34,7 @@ namespace ANX.RenderSystem.Windows.DX10 { for (int i = 0; i < NativeEffect.Description.TechniqueCount; i++) { - EffectTechnique_DX10 teqDx10 = new EffectTechnique_DX10(this.managedEffect); - teqDx10.NativeTechnique = NativeEffect.GetTechniqueByIndex(i); + var teqDx10 = new EffectTechnique_DX10(managedEffect, NativeEffect.GetTechniqueByIndex(i)); yield return new EffectTechnique(this.managedEffect, teqDx10); } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/GraphicsDeviceWindowsDX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/GraphicsDeviceWindowsDX10.cs index 6d9a30a7..78fb0086 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/GraphicsDeviceWindowsDX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/GraphicsDeviceWindowsDX10.cs @@ -6,6 +6,7 @@ using ANX.RenderSystem.Windows.DX10.Helpers; using SharpDX.D3DCompiler; using SharpDX.DXGI; using Dx10 = SharpDX.Direct3D10; +using ANX.BaseDirectX; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -58,7 +59,7 @@ namespace ANX.RenderSystem.Windows.DX10 BufferCount = 1, ModeDescription = new ModeDescription(presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight, new Rational(60, 1), - FormatConverter.Translate(presentationParameters.BackBufferFormat)), + BaseFormatConverter.Translate(presentationParameters.BackBufferFormat)), IsWindowed = true, OutputHandle = presentationParameters.DeviceWindowHandle, SampleDescription = new SampleDescription(1, 0), @@ -91,7 +92,7 @@ namespace ANX.RenderSystem.Windows.DX10 // // create the depth stencil buffer // - Format depthFormat = FormatConverter.Translate(presentationParameters.DepthStencilFormat); + Format depthFormat = BaseFormatConverter.Translate(presentationParameters.DepthStencilFormat); if (depthFormat != Format.Unknown) CreateDepthStencilBuffer(depthFormat); } @@ -248,9 +249,9 @@ namespace ANX.RenderSystem.Windows.DX10 int startIndex, int primitiveCount) { Dx10.EffectTechnique technique = SetupEffectForDraw(); - int vertexCount = FormatConverter.CalculateVertexCount(primitiveType, primitiveCount); + int vertexCount = BaseFormatConverter.CalculateVertexCount(primitiveType, primitiveCount); - device.InputAssembler.PrimitiveTopology = FormatConverter.Translate(primitiveType); + device.InputAssembler.PrimitiveTopology = BaseFormatConverter.Translate(primitiveType); device.Rasterizer.SetViewports(currentViewport); device.OutputMerger.SetTargets(this.depthStencilView, this.renderView); @@ -267,7 +268,7 @@ namespace ANX.RenderSystem.Windows.DX10 { Dx10.EffectTechnique technique = SetupEffectForDraw(); - device.InputAssembler.PrimitiveTopology = FormatConverter.Translate(primitiveType); + device.InputAssembler.PrimitiveTopology = BaseFormatConverter.Translate(primitiveType); device.Rasterizer.SetViewports(currentViewport); device.OutputMerger.SetTargets(this.depthStencilView, this.renderView); @@ -334,7 +335,7 @@ namespace ANX.RenderSystem.Windows.DX10 device.InputAssembler.InputLayout = layout; // Prepare All the stages - device.InputAssembler.PrimitiveTopology = FormatConverter.Translate(primitiveType); + device.InputAssembler.PrimitiveTopology = BaseFormatConverter.Translate(primitiveType); device.Rasterizer.SetViewports(currentViewport); //device.OutputMerger.SetTargets(this.depthStencilView, this.renderView); @@ -388,7 +389,8 @@ namespace ANX.RenderSystem.Windows.DX10 if (nativeIndexBuffer != null) { - device.InputAssembler.SetIndexBuffer(nativeIndexBuffer.NativeBuffer, FormatConverter.Translate(indexBuffer.IndexElementSize), 0); + device.InputAssembler.SetIndexBuffer(nativeIndexBuffer.NativeBuffer, + BaseFormatConverter.Translate(indexBuffer.IndexElementSize), 0); } else { @@ -469,7 +471,7 @@ namespace ANX.RenderSystem.Windows.DX10 #region CreateInputElementFromVertexElement private Dx10.InputElement CreateInputElementFromVertexElement(VertexElement vertexElement) { - string elementName = FormatConverter.Translate(ref vertexElement); + string elementName = BaseFormatConverter.Translate(ref vertexElement); Format elementFormat = ConvertVertexElementFormat(vertexElement.VertexElementFormat); return new Dx10.InputElement(elementName, vertexElement.UsageIndex, elementFormat, vertexElement.Offset, 0); } @@ -616,7 +618,7 @@ namespace ANX.RenderSystem.Windows.DX10 presentationParameters.BackBufferHeight); // create the depth stencil buffer - Format depthFormat = FormatConverter.Translate(presentationParameters.DepthStencilFormat); + Format depthFormat = BaseFormatConverter.Translate(presentationParameters.DepthStencilFormat); if (depthFormat != Format.Unknown) CreateDepthStencilBuffer(depthFormat); } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/Helpers/FormatConverter.cs b/RenderSystems/ANX.Framework.Windows.DX10/Helpers/FormatConverter.cs index 31d9d485..a6bc5dbe 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/Helpers/FormatConverter.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/Helpers/FormatConverter.cs @@ -12,89 +12,6 @@ namespace ANX.RenderSystem.Windows.DX10.Helpers { internal class FormatConverter { - #region FormatSize (SurfaceFormat) - public static int FormatSize(SurfaceFormat format) - { - switch (format) - { - case SurfaceFormat.Vector4: - return 16; - //case SurfaceFormat.Vector3: - // return 12; - case SurfaceFormat.Vector2: - return 8; - case SurfaceFormat.Single: - case SurfaceFormat.Color: - //case SurfaceFormat.RGBA1010102: - //case SurfaceFormat.RG32: - return 4; - //case SurfaceFormat.BGR565: - //case SurfaceFormat.BGRA5551: - // return 2; - case SurfaceFormat.Dxt1: - case SurfaceFormat.Dxt3: - case SurfaceFormat.Dxt5: - case SurfaceFormat.Alpha8: - return 1; - } - - throw new ArgumentException("Invalid format '" + format + "'."); - } - #endregion - - #region Translate (SurfaceFormat) - public static SharpDX.DXGI.Format Translate(SurfaceFormat surfaceFormat) - { - switch (surfaceFormat) - { - case SurfaceFormat.Color: - return SharpDX.DXGI.Format.R8G8B8A8_UNorm; - case SurfaceFormat.Dxt3: - return SharpDX.DXGI.Format.BC2_UNorm; - case SurfaceFormat.Dxt5: - return SharpDX.DXGI.Format.BC3_UNorm; - } - - throw new Exception("can't translate SurfaceFormat: " + surfaceFormat.ToString()); - } - #endregion - - #region Translate (DepthFormat) - public static Format Translate(DepthFormat depthFormat) - { - switch (depthFormat) - { - case DepthFormat.Depth16: - return Format.D16_UNorm; - case DepthFormat.Depth24: - //TODO: no DirectX10 24Bit depth format??? - case DepthFormat.Depth24Stencil8: - return Format.D24_UNorm_S8_UInt; - case DepthFormat.None: - return Format.Unknown; - } - - throw new Exception("can't translate DepthFormat: " + depthFormat.ToString()); - } - #endregion - - #region Translate (Format) - public static SurfaceFormat Translate(SharpDX.DXGI.Format format) - { - switch (format) - { - case SharpDX.DXGI.Format.R8G8B8A8_UNorm: - return SurfaceFormat.Color; - case SharpDX.DXGI.Format.BC2_UNorm: - return SurfaceFormat.Dxt3; - case SharpDX.DXGI.Format.BC3_UNorm: - return SurfaceFormat.Dxt5; - } - - throw new Exception("can't translate Format: " + format.ToString()); - } - #endregion - #region Translate (TextureFilter) public static Dx10.Filter Translate(TextureFilter filter) { @@ -141,51 +58,6 @@ namespace ANX.RenderSystem.Windows.DX10.Helpers } #endregion - #region Translate (PrimitiveType) - public static PrimitiveTopology Translate(PrimitiveType primitiveType) - { - switch (primitiveType) - { - case PrimitiveType.LineList: - return PrimitiveTopology.LineList; - case PrimitiveType.LineStrip: - return PrimitiveTopology.LineStrip; - case PrimitiveType.TriangleList: - return PrimitiveTopology.TriangleList; - case PrimitiveType.TriangleStrip: - return PrimitiveTopology.TriangleStrip; - } - - throw new InvalidOperationException("unknown PrimitiveType: " + primitiveType); - } - #endregion - - #region Translate (IndexElementSize) - public static SharpDX.DXGI.Format Translate(IndexElementSize indexElementSize) - { - switch (indexElementSize) - { - case IndexElementSize.SixteenBits: - return Format.R16_UInt; - case IndexElementSize.ThirtyTwoBits: - return Format.R32_UInt; - } - - throw new InvalidOperationException("unknown IndexElementSize: " + indexElementSize); - } - #endregion - - #region Translate (VertexElement) - public static string Translate(ref VertexElement element) - { - //TODO: map the other Usages - if (element.VertexElementUsage == VertexElementUsage.TextureCoordinate) - return "TEXCOORD"; - else - return element.VertexElementUsage.ToString().ToUpperInvariant(); - } - #endregion - #region Translate (BlendFunction) public static Dx10.BlendOperation Translate(BlendFunction blendFunction) { @@ -340,21 +212,5 @@ namespace ANX.RenderSystem.Windows.DX10.Helpers return fillMode == FillMode.WireFrame ? Dx10.FillMode.Wireframe : Dx10.FillMode.Solid; } #endregion - - #region CalculateVertexCount - public static int CalculateVertexCount(PrimitiveType type, int primitiveCount) - { - if (type == PrimitiveType.TriangleList) - return primitiveCount * 3; - else if (type == PrimitiveType.LineList) - return primitiveCount * 2; - else if (type == PrimitiveType.LineStrip) - return primitiveCount + 1; - else if (type == PrimitiveType.TriangleStrip) - return primitiveCount + 2; - - throw new NotImplementedException("Couldn't calculate vertex count for PrimitiveType '" + type + "'."); - } - #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/IndexBuffer_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/IndexBuffer_DX10.cs index 8f8e0683..59d8fa68 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/IndexBuffer_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/IndexBuffer_DX10.cs @@ -1,8 +1,9 @@ using System; -using System.IO; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using SharpDX.Direct3D10; +using SharpDX; +using Dx10 = SharpDX.Direct3D10; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -10,123 +11,56 @@ using SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class IndexBuffer_DX10 : INativeIndexBuffer, IDisposable + public class IndexBuffer_DX10 : BaseIndexBuffer, INativeIndexBuffer, IDisposable { - private IndexElementSize size; - - public SharpDX.Direct3D10.Buffer NativeBuffer { get; private set; } - #region Constructor public IndexBuffer_DX10(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) { - this.size = size; - - //TODO: translate and use usage - + elementSize = size; GraphicsDeviceWindowsDX10 gd10 = graphics.NativeDevice as GraphicsDeviceWindowsDX10; - Device device = gd10 != null ? gd10.NativeDevice as Device : null; + Dx10.Device device = gd10 != null ? gd10.NativeDevice as Dx10.Device : null; InitializeBuffer(device, size, indexCount, usage); } - internal IndexBuffer_DX10(Device device, IndexElementSize size, int indexCount, BufferUsage usage) + internal IndexBuffer_DX10(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) { - this.size = size; + elementSize = size; InitializeBuffer(device, size, indexCount, usage); } #endregion #region InitializeBuffer - private void InitializeBuffer(Device device, IndexElementSize size, int indexCount, BufferUsage usage) + private void InitializeBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) { - BufferDescription description = new BufferDescription() + //TODO: translate and use usage + var description = new Dx10.BufferDescription() { - Usage = ResourceUsage.Dynamic, - SizeInBytes = (size == IndexElementSize.SixteenBits ? 2 : 4) * indexCount, - BindFlags = BindFlags.IndexBuffer, - CpuAccessFlags = CpuAccessFlags.Write, - OptionFlags = ResourceOptionFlags.None + Usage = Dx10.ResourceUsage.Dynamic, + SizeInBytes = GetSizeInBytes(indexCount), + BindFlags = Dx10.BindFlags.IndexBuffer, + CpuAccessFlags = Dx10.CpuAccessFlags.Write, + OptionFlags = Dx10.ResourceOptionFlags.None }; - NativeBuffer = new SharpDX.Direct3D10.Buffer(device, description); + NativeBuffer = new Dx10.Buffer(device, description); NativeBuffer.Unmap(); } #endregion - #region SetData - public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct + protected override DataStream MapBufferWrite() { - SetData(graphicsDevice, data, 0, data.Length); + return NativeBuffer.Map(Dx10.MapMode.WriteDiscard); } - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) - where T : struct + protected override DataStream MapBufferRead() { - //TODO: check offsetInBytes parameter for bounds etc. - - using (var stream = NativeBuffer.Map(MapMode.WriteDiscard)) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - if (startIndex > 0 || elementCount < data.Length) - for (int i = startIndex; i < startIndex + elementCount; i++) - stream.Write(data[i]); - else - for (int i = 0; i < data.Length; i++) - stream.Write(data[i]); - - NativeBuffer.Unmap(); - } + return NativeBuffer.Map(Dx10.MapMode.Read); } - public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct + protected override void UnmapBuffer() { - SetData(graphicsDevice, 0, data, startIndex, elementCount); + NativeBuffer.Unmap(); } - #endregion - - #region Dispose - public void Dispose() - { - if (NativeBuffer != null) - { - NativeBuffer.Dispose(); - NativeBuffer = null; - } - } - #endregion - - #region GetData - public void GetData(T[] data) where T : struct - { - using (var stream = NativeBuffer.Map(MapMode.Read)) - { - stream.ReadRange(data, 0, data.Length); - NativeBuffer.Unmap(); - } - } - - public void GetData(T[] data, int startIndex, int elementCount) where T : struct - { - using (var stream = NativeBuffer.Map(MapMode.Read)) - { - stream.ReadRange(data, startIndex, elementCount); - NativeBuffer.Unmap(); - } - } - - public void GetData(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct - { - using (var stream = NativeBuffer.Map(MapMode.Read)) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - stream.ReadRange(data, startIndex, elementCount); - NativeBuffer.Unmap(); - } - } - #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/RasterizerState_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/RasterizerState_DX10.cs index 9c6dad9f..e38ce912 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/RasterizerState_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/RasterizerState_DX10.cs @@ -1,3 +1,5 @@ +using System; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; using ANX.RenderSystem.Windows.DX10.Helpers; @@ -9,25 +11,17 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class RasterizerState_DX10 : INativeRasterizerState + public class RasterizerState_DX10 : BaseStateObject, INativeRasterizerState { - private const int intMaxOver16 = int.MaxValue / 16; - - #region Private private Dx10.RasterizerStateDescription description; - private Dx10.RasterizerState nativeRasterizerState; - private bool isDirty; - #endregion #region Public - public bool IsBound { get; private set; } - public CullMode CullMode { set { Dx10.CullMode cullMode = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.CullMode, ref cullMode); + SetValueIfDifferentAndMarkDirty(ref description.CullMode, ref cullMode); } } @@ -38,8 +32,8 @@ namespace ANX.RenderSystem.Windows.DX10 // XNA uses a float value in the range of 0f..16f as value // DirectX 10 uses a INT value - int depthBiasValue = (int)(value * intMaxOver16); - UpdateValueAndMarkDirtyIfNeeded(ref description.DepthBias, ref depthBiasValue); + int depthBiasValue = (int)(value * IntMaxOver16); + SetValueIfDifferentAndMarkDirty(ref description.DepthBias, ref depthBiasValue); } } @@ -48,7 +42,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.FillMode fillMode = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.FillMode, ref fillMode); + SetValueIfDifferentAndMarkDirty(ref description.FillMode, ref fillMode); } } @@ -80,70 +74,26 @@ namespace ANX.RenderSystem.Windows.DX10 { set { - UpdateValueAndMarkDirtyIfNeeded(ref description.SlopeScaledDepthBias, ref value); + SetValueIfDifferentAndMarkDirty(ref description.SlopeScaledDepthBias, ref value); } } #endregion - #region Constructor - public RasterizerState_DX10() - { - description.IsAntialiasedLineEnabled = false; - isDirty = true; - } - #endregion - - #region Apply - public void Apply(GraphicsDevice graphicsDevice) - { - UpdateNativeRasterizerState(graphicsDevice); - IsBound = true; - } - #endregion - - #region Release - public void Release() - { - IsBound = false; - } - #endregion - - #region Dispose - public void Dispose() - { - if (nativeRasterizerState != null) - { - nativeRasterizerState.Dispose(); - nativeRasterizerState = null; - } - } - #endregion - - #region UpdateNativeRasterizerState - private void UpdateNativeRasterizerState(GraphicsDevice graphicsDevice) + protected override void Init() { - Dx10.Device device = (graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; - - if (isDirty == true || nativeRasterizerState == null) - { - Dispose(); - nativeRasterizerState = new Dx10.RasterizerState(device, ref description); - isDirty = false; - } - - device.Rasterizer.State = nativeRasterizerState; + description.IsAntialiasedLineEnabled = false; } - #endregion - #region UpdateValueAndMarkDirtyIfNeeded - private void UpdateValueAndMarkDirtyIfNeeded(ref T currentValue, ref T value) + protected override Dx10.RasterizerState CreateNativeState(GraphicsDevice graphics) { - if (value.Equals(currentValue) == false) - { - isDirty = true; - currentValue = value; - } + Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + return new Dx10.RasterizerState(device, ref description); + } + + protected override void ApplyNativeState(GraphicsDevice graphics) + { + Dx10.Device device = (graphics.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + device.Rasterizer.State = nativeState; } - #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/RenderTarget2D_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/RenderTarget2D_DX10.cs index 381b5598..e8ecef0e 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/RenderTarget2D_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/RenderTarget2D_DX10.cs @@ -1,7 +1,7 @@ using System; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using ANX.RenderSystem.Windows.DX10.Helpers; using Dx10 = SharpDX.Direct3D10; // This file is part of the ANX.Framework created by the @@ -13,38 +13,30 @@ namespace ANX.RenderSystem.Windows.DX10 public class RenderTarget2D_DX10 : Texture2D_DX10, INativeRenderTarget2D, INativeTexture2D { #region Constructor - public RenderTarget2D_DX10(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat preferredFormat, + public RenderTarget2D_DX10(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat surfaceFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) - : base(graphics) + : base(graphics, surfaceFormat) { if (mipMap) throw new NotImplementedException("creating RenderTargets with mip map not yet implemented"); - - this.surfaceFormat = surfaceFormat; - - GraphicsDeviceWindowsDX10 graphicsDX10 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10; - Dx10.Device device = graphicsDX10.NativeDevice; - + var description = new Dx10.Texture2DDescription() { Width = width, Height = height, MipLevels = 1, ArraySize = 1, - Format = FormatConverter.Translate(preferredFormat), + Format = BaseFormatConverter.Translate(surfaceFormat), SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), Usage = Dx10.ResourceUsage.Default, BindFlags = Dx10.BindFlags.ShaderResource | Dx10.BindFlags.RenderTarget, CpuAccessFlags = Dx10.CpuAccessFlags.None, OptionFlags = Dx10.ResourceOptionFlags.None, }; - nativeTexture = new Dx10.Texture2D(graphicsDX10.NativeDevice, description); - nativeShaderResourceView = new Dx10.ShaderResourceView(graphicsDX10.NativeDevice, nativeTexture); - // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx - // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx - - formatSize = FormatConverter.FormatSize(surfaceFormat); + Dx10.Device device = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + NativeTexture = new Dx10.Texture2D(device, description); + NativeShaderResourceView = new Dx10.ShaderResourceView(device, NativeTexture); } #endregion } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/SamplerState_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/SamplerState_DX10.cs index 6e6fc70a..f623f489 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/SamplerState_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/SamplerState_DX10.cs @@ -1,3 +1,5 @@ +using System; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; using ANX.RenderSystem.Windows.DX10.Helpers; @@ -9,41 +11,37 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class SamplerState_DX10 : INativeSamplerState + public class SamplerState_DX10 : BaseStateObject, INativeSamplerState { #region Private private Dx10.SamplerStateDescription description; - private Dx10.SamplerState nativeSamplerState; - private bool isDirty; #endregion #region Public - public bool IsBound { get; private set; } - - public ANX.Framework.Graphics.TextureAddressMode AddressU + public TextureAddressMode AddressU { set { Dx10.TextureAddressMode mode = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.AddressU, ref mode); + SetValueIfDifferentAndMarkDirty(ref description.AddressU, ref mode); } } - public ANX.Framework.Graphics.TextureAddressMode AddressV + public TextureAddressMode AddressV { set { Dx10.TextureAddressMode mode = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.AddressV, ref mode); + SetValueIfDifferentAndMarkDirty(ref description.AddressV, ref mode); } } - public ANX.Framework.Graphics.TextureAddressMode AddressW + public TextureAddressMode AddressW { set { Dx10.TextureAddressMode mode = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.AddressW, ref mode); + SetValueIfDifferentAndMarkDirty(ref description.AddressW, ref mode); } } @@ -52,7 +50,7 @@ namespace ANX.RenderSystem.Windows.DX10 set { Dx10.Filter filter = FormatConverter.Translate(value); - UpdateValueAndMarkDirtyIfNeeded(ref description.Filter, ref filter); + SetValueIfDifferentAndMarkDirty(ref description.Filter, ref filter); } } @@ -60,7 +58,7 @@ namespace ANX.RenderSystem.Windows.DX10 { set { - UpdateValueAndMarkDirtyIfNeeded(ref description.MaximumAnisotropy, ref value); + SetValueIfDifferentAndMarkDirty(ref description.MaximumAnisotropy, ref value); } } @@ -80,18 +78,11 @@ namespace ANX.RenderSystem.Windows.DX10 { set { - UpdateValueAndMarkDirtyIfNeeded(ref description.MipLodBias, ref value); + SetValueIfDifferentAndMarkDirty(ref description.MipLodBias, ref value); } } #endregion - #region Constructor - public SamplerState_DX10() - { - isDirty = true; - } - #endregion - #region Apply public void Apply(GraphicsDevice graphicsDevice, int index) { @@ -99,49 +90,29 @@ namespace ANX.RenderSystem.Windows.DX10 UpdateNativeSamplerState(device); IsBound = true; - device.PixelShader.SetSampler(index, nativeSamplerState); + device.PixelShader.SetSampler(index, nativeState); } #endregion - #region Release - public void Release() - { - IsBound = false; - } - #endregion - - #region Dispose - public void Dispose() - { - if (nativeSamplerState != null) - { - nativeSamplerState.Dispose(); - nativeSamplerState = null; - } - } - #endregion - #region UpdateNativeSamplerState private void UpdateNativeSamplerState(Dx10.Device device) { - if (isDirty == true || nativeSamplerState == null) + if (isDirty == true || nativeState == null) { Dispose(); - nativeSamplerState = new Dx10.SamplerState(device, ref description); + nativeState = new Dx10.SamplerState(device, ref description); isDirty = false; } } #endregion - #region UpdateValueAndMarkDirtyIfNeeded - private void UpdateValueAndMarkDirtyIfNeeded(ref T currentValue, ref T value) + protected override Dx10.SamplerState CreateNativeState(GraphicsDevice graphics) + { + return null; + } + + protected override void ApplyNativeState(GraphicsDevice graphics) { - if (value.Equals(currentValue) == false) - { - isDirty = true; - currentValue = value; - } } - #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/Texture2D_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/Texture2D_DX10.cs index 94218103..88284f0f 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/Texture2D_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/Texture2D_DX10.cs @@ -1,10 +1,9 @@ using System; using System.IO; -using System.Runtime.InteropServices; -using ANX.Framework; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using ANX.RenderSystem.Windows.DX10.Helpers; +using SharpDX; using Dx10 = SharpDX.Direct3D10; // This file is part of the ANX.Framework created by the @@ -13,117 +12,57 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class Texture2D_DX10 : INativeTexture2D + public class Texture2D_DX10 : BaseTexture2D, INativeTexture2D { - #region Private - protected internal Dx10.Texture2D nativeTexture; - protected internal Dx10.ShaderResourceView nativeShaderResourceView; - protected internal int formatSize; - protected internal SurfaceFormat surfaceFormat; - protected internal GraphicsDevice graphicsDevice; - #endregion - #region Public - internal Dx10.Texture2D NativeTexture + public override int Width { get { - return this.nativeTexture; - } - set - { - if (this.nativeTexture != value) - { - if (this.nativeTexture != null) - this.nativeTexture.Dispose(); - - this.nativeTexture = value; - } + return NativeTexture != null ? NativeTexture.Description.Width : 0; } } - internal Dx10.ShaderResourceView NativeShaderResourceView + public override int Height { get { - return this.nativeShaderResourceView; - } - set - { - if (this.nativeShaderResourceView != value) - { - if (this.nativeShaderResourceView != null) - { - this.nativeShaderResourceView.Dispose(); - } - - this.nativeShaderResourceView = value; - } + return NativeTexture != null ? NativeTexture.Description.Height : 0; } } - public int Width - { - get - { - return (this.nativeTexture != null) ? this.nativeTexture.Description.Width : 0; - } - } - - public int Height - { - get - { - return (this.nativeTexture != null) ? this.nativeTexture.Description.Height : 0; - } - } - - public GraphicsDevice GraphicsDevice - { - get - { - return this.graphicsDevice; - } - } + protected internal Dx10.ShaderResourceView NativeShaderResourceView { get; protected set; } #endregion #region Constructor - internal Texture2D_DX10(GraphicsDevice graphicsDevice) + internal Texture2D_DX10(GraphicsDevice graphicsDevice, SurfaceFormat surfaceFormat) + : base(graphicsDevice, surfaceFormat) { - this.graphicsDevice = graphicsDevice; } public Texture2D_DX10(GraphicsDevice graphicsDevice, int width, int height, SurfaceFormat surfaceFormat, int mipCount) + : base(graphicsDevice, surfaceFormat) { if (mipCount > 1) throw new Exception("creating textures with mip map not yet implemented"); - this.graphicsDevice = graphicsDevice; - this.surfaceFormat = surfaceFormat; - - GraphicsDeviceWindowsDX10 graphicsDX10 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10; - Dx10.Device device = graphicsDX10.NativeDevice; - Dx10.Texture2DDescription description = new Dx10.Texture2DDescription() { Width = width, Height = height, MipLevels = mipCount, ArraySize = mipCount, - Format = FormatConverter.Translate(surfaceFormat), + Format = BaseFormatConverter.Translate(surfaceFormat), SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), Usage = Dx10.ResourceUsage.Dynamic, BindFlags = Dx10.BindFlags.ShaderResource, CpuAccessFlags = Dx10.CpuAccessFlags.Write, OptionFlags = Dx10.ResourceOptionFlags.None, }; - this.nativeTexture = new Dx10.Texture2D(graphicsDX10.NativeDevice, description); - this.nativeShaderResourceView = new Dx10.ShaderResourceView(graphicsDX10.NativeDevice, this.nativeTexture); - // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx - // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx - - this.formatSize = FormatConverter.FormatSize(surfaceFormat); + Dx10.Device device = (graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX10).NativeDevice; + NativeTexture = new Dx10.Texture2D(device, description); + NativeShaderResourceView = new Dx10.ShaderResourceView(device, NativeTexture); } #endregion @@ -134,136 +73,16 @@ namespace ANX.RenderSystem.Windows.DX10 } #endregion - #region SetData - public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct - { - SetData(graphicsDevice, 0, data, 0, data.Length); - } - - public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct - { - SetData(graphicsDevice, 0, data, startIndex, elementCount); - } - - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) - where T : struct - { - //TODO: handle offsetInBytes parameter - //TODO: handle startIndex parameter - //TODO: handle elementCount parameter - - if (this.surfaceFormat == SurfaceFormat.Color) - { - int subresource = Dx10.Texture2D.CalculateSubResourceIndex(0, 0, 1); - SharpDX.DataRectangle rectangle = this.nativeTexture.Map(subresource, Dx10.MapMode.WriteDiscard, Dx10.MapFlags.None); - int rowPitch = rectangle.Pitch; - - unsafe - { - GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); - byte* colorData = (byte*)handle.AddrOfPinnedObject(); - - byte* pTexels = (byte*)rectangle.DataPointer; - int srcIndex = 0; - - for (int row = 0; row < Height; row++) - { - int rowStart = row * rowPitch; - - for (int col = 0; col < Width; col++) - { - int colStart = col * formatSize; - pTexels[rowStart + colStart + 0] = colorData[srcIndex++]; - pTexels[rowStart + colStart + 1] = colorData[srcIndex++]; - pTexels[rowStart + colStart + 2] = colorData[srcIndex++]; - pTexels[rowStart + colStart + 3] = colorData[srcIndex++]; - } - } - - handle.Free(); - } - - this.nativeTexture.Unmap(subresource); - } - else if (surfaceFormat == SurfaceFormat.Dxt5 || surfaceFormat == SurfaceFormat.Dxt3 || surfaceFormat == SurfaceFormat.Dxt1) - { - unsafe - { - GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); - byte* colorData = (byte*)handle.AddrOfPinnedObject(); - - int w = (Width + 3) >> 2; - int h = (Height + 3) >> 2; - formatSize = (surfaceFormat == SurfaceFormat.Dxt1) ? 8 : 16; - - int subresource = Dx10.Texture2D.CalculateSubResourceIndex(0, 0, 1); - SharpDX.DataRectangle rectangle = this.nativeTexture.Map(subresource, Dx10.MapMode.WriteDiscard, Dx10.MapFlags.None); - SharpDX.DataStream ds = new SharpDX.DataStream(rectangle.DataPointer, Width * Height * 4 * 2, true, true); - int pitch = rectangle.Pitch; - int col = 0; - int index = 0; // startIndex - int count = data.Length; // elementCount - int actWidth = w * formatSize; - - for (int i = 0; i < h; i++) - { - ds.Position = (i * pitch) + (col * formatSize); - if (count <= 0) - { - break; - } - else if (count < actWidth) - { - for (int idx = index; idx < index + count; idx++) - { - ds.WriteByte(colorData[idx]); - } - //ds.WriteRange(colorDataArray, index, count); - - break; - } - - for (int idx = index; idx < index + actWidth; idx++) - { - ds.WriteByte(colorData[idx]); - } - //ds.WriteRange(colorDataArray, index, actWidth); - - index += actWidth; - count -= actWidth; - } - - handle.Free(); - - this.nativeTexture.Unmap(subresource); - } - } - else - { - throw new Exception(string.Format("creating textures of format {0} not yet implemented...", surfaceFormat.ToString())); - } - } - - public void SetData(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct - { - throw new NotImplementedException(); - } - #endregion - #region Dispose - public void Dispose() + public override void Dispose() { - if (this.nativeShaderResourceView != null) + if (NativeShaderResourceView != null) { - this.nativeShaderResourceView.Dispose(); - this.nativeShaderResourceView = null; + NativeShaderResourceView.Dispose(); + NativeShaderResourceView = null; } - if (this.nativeTexture != null) - { - this.nativeTexture.Dispose(); - this.nativeTexture = null; - } + base.Dispose(); } #endregion @@ -284,7 +103,7 @@ namespace ANX.RenderSystem.Windows.DX10 #region GetData (TODO) public void GetData(T[] data) where T : struct { - throw new NotImplementedException(); + GetData(data, 0, data.Length); } public void GetData(T[] data, int startIndex, int elementCount) where T : struct @@ -292,10 +111,37 @@ namespace ANX.RenderSystem.Windows.DX10 throw new NotImplementedException(); } - public void GetData(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct + public void GetData(int level, Framework.Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct { throw new NotImplementedException(); } #endregion + + #region MapWrite + protected override IntPtr MapWrite() + { + tempSubresource = Dx10.Texture2D.CalculateSubResourceIndex(0, 0, 1); + DataRectangle rect = NativeTexture.Map(tempSubresource, Dx10.MapMode.WriteDiscard, Dx10.MapFlags.None); + pitch = rect.Pitch; + return rect.DataPointer; + } + #endregion + + #region MapRead + protected override IntPtr MapRead() + { + tempSubresource = Dx10.Texture2D.CalculateSubResourceIndex(0, 0, 1); + DataRectangle rect = NativeTexture.Map(tempSubresource, Dx10.MapMode.Read, Dx10.MapFlags.None); + pitch = rect.Pitch; + return rect.DataPointer; + } + #endregion + + #region Unmap + protected override void Unmap() + { + NativeTexture.Unmap(tempSubresource); + } + #endregion } } diff --git a/RenderSystems/ANX.Framework.Windows.DX10/VertexBuffer_DX10.cs b/RenderSystems/ANX.Framework.Windows.DX10/VertexBuffer_DX10.cs index 57c63682..7e59789d 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/VertexBuffer_DX10.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/VertexBuffer_DX10.cs @@ -1,8 +1,9 @@ using System; -using System.IO; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using SharpDX.Direct3D10; +using SharpDX; +using Dx10 = SharpDX.Direct3D10; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -10,29 +11,25 @@ using SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public class VertexBuffer_DX10 : INativeVertexBuffer, IDisposable + public class VertexBuffer_DX10 : BaseVertexBuffer, INativeVertexBuffer, IDisposable { - int vertexStride; - - public SharpDX.Direct3D10.Buffer NativeBuffer { get; private set; } - #region Constructor public VertexBuffer_DX10(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { GraphicsDeviceWindowsDX10 gd10 = graphics.NativeDevice as GraphicsDeviceWindowsDX10; - Device device = gd10 != null ? gd10.NativeDevice as Device : null; + Dx10.Device device = gd10 != null ? gd10.NativeDevice as Dx10.Device : null; InitializeBuffer(device, vertexDeclaration, vertexCount, usage); } - internal VertexBuffer_DX10(Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + internal VertexBuffer_DX10(Dx10.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { InitializeBuffer(device, vertexDeclaration, vertexCount, usage); } #endregion #region InitializeBuffer - private void InitializeBuffer(Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + private void InitializeBuffer(Dx10.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { this.vertexStride = vertexDeclaration.VertexStride; @@ -40,101 +37,34 @@ namespace ANX.RenderSystem.Windows.DX10 if (device != null) { - BufferDescription description = new BufferDescription() + var description = new Dx10.BufferDescription() { - Usage = ResourceUsage.Dynamic, + Usage = Dx10.ResourceUsage.Dynamic, SizeInBytes = vertexDeclaration.VertexStride * vertexCount, - BindFlags = BindFlags.VertexBuffer, - CpuAccessFlags = CpuAccessFlags.Write, - OptionFlags = ResourceOptionFlags.None + BindFlags = Dx10.BindFlags.VertexBuffer, + CpuAccessFlags = Dx10.CpuAccessFlags.Write, + OptionFlags = Dx10.ResourceOptionFlags.None }; - NativeBuffer = new SharpDX.Direct3D10.Buffer(device, description); + NativeBuffer = new Dx10.Buffer(device, description); NativeBuffer.Unmap(); } } #endregion - #region SetData - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) - where T : struct + protected override DataStream MapBufferWrite() { - //TODO: check offsetInBytes parameter for bounds etc. - - using (var stream = NativeBuffer.Map(MapMode.WriteDiscard)) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - if (startIndex > 0 || elementCount < data.Length) - for (int i = startIndex; i < startIndex + elementCount; i++) - stream.Write(data[i]); - else - for (int i = 0; i < data.Length; i++) - stream.Write(data[i]); - - NativeBuffer.Unmap(); - } + return NativeBuffer.Map(Dx10.MapMode.WriteDiscard); } - public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct + protected override DataStream MapBufferRead() { - SetData(graphicsDevice, data, 0, data.Length); + return NativeBuffer.Map(Dx10.MapMode.Read); } - public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct + protected override void UnmapBuffer() { - SetData(graphicsDevice, 0, data, startIndex, elementCount); + NativeBuffer.Unmap(); } - - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount, - int vertexStride) where T : struct - { - throw new NotImplementedException(); - } - #endregion - - #region GetData - public void GetData(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct - { - using (var stream = NativeBuffer.Map(MapMode.Read)) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - stream.ReadRange(data, startIndex, elementCount); - NativeBuffer.Unmap(); - } - } - - public void GetData(T[] data) where T : struct - { - using (var stream = NativeBuffer.Map(MapMode.Read)) - { - stream.ReadRange(data, 0, data.Length); - NativeBuffer.Unmap(); - } - } - - public void GetData(T[] data, int startIndex, int elementCount) where T : struct - { - using (var stream = NativeBuffer.Map(MapMode.Read)) - { - stream.ReadRange(data, startIndex, elementCount); - NativeBuffer.Unmap(); - } - } - #endregion - - #region Dispose - public void Dispose() - { - if (this.NativeBuffer != null) - { - NativeBuffer.Dispose(); - NativeBuffer = null; - } - } - #endregion } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj index cc930c85..9deb4edd 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj @@ -63,6 +63,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + {5BE49183-2F6F-4527-AC90-D816911FCF90} ANX.RenderSystem.Windows.DX10 @@ -98,7 +102,7 @@ - + diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_Linux.csproj b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_Linux.csproj index 711bdc95..22545e5d 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_Linux.csproj +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_Linux.csproj @@ -62,6 +62,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + {5BE49183-2F6F-4527-AC90-D816911FCF90} ANX.RenderSystem.Windows.DX10 @@ -97,7 +101,7 @@ - + diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_PSVita.csproj b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_PSVita.csproj index 9413080e..2fc5ee09 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_PSVita.csproj +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_PSVita.csproj @@ -62,6 +62,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + {5BE49183-2F6F-4527-AC90-D816911FCF90} ANX.RenderSystem.Windows.DX10 @@ -98,7 +102,7 @@ - + diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_WindowsMetro.csproj b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_WindowsMetro.csproj index 3b8f5730..91a5affb 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_WindowsMetro.csproj +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11_WindowsMetro.csproj @@ -64,6 +64,10 @@ {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} ANX.Framework + + {A4D3AD34-E49C-4142-8620-2AFF44ED6719} + ANX.BaseDirectX + @@ -95,7 +99,7 @@ - + diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/BlendState_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/BlendState_DX11.cs index 5803ad1a..1ec50ce5 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/BlendState_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/BlendState_DX11.cs @@ -1,14 +1,9 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using SharpDX.Direct3D11; +using ANX.BaseDirectX; +using ANX.Framework; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; -using ANX.Framework; - -#endregion // Using Statements +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -16,270 +11,225 @@ using ANX.Framework; namespace ANX.RenderSystem.Windows.DX11 { - public class BlendState_DX11 : INativeBlendState + public class BlendState_DX11 : BaseStateObject, INativeBlendState { - #region Private Members - private BlendStateDescription blendStateDescription; - private SharpDX.Direct3D11.BlendState nativeBlendState; - private bool nativeBlendStateDirty; + private Dx11.BlendStateDescription blendStateDescription; private SharpDX.Color4 blendFactor; private int multiSampleMask; - private bool bound; - #endregion // Private Members - - public BlendState_DX11() - { - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - blendStateDescription.RenderTarget[i] = new RenderTargetBlendDescription(); - blendStateDescription.RenderTarget[i].IsBlendEnabled = (i < 4); - blendStateDescription.IndependentBlendEnable = true; - } - - nativeBlendStateDirty = true; - } - - public void Apply(GraphicsDevice graphics) - { - GraphicsDeviceWindowsDX11 gdx11 = graphics.NativeDevice as GraphicsDeviceWindowsDX11; - SharpDX.Direct3D11.DeviceContext context = gdx11.NativeDevice; - - UpdateNativeBlendState(context.Device); - this.bound = true; - - context.OutputMerger.SetBlendState(nativeBlendState, this.blendFactor, this.multiSampleMask); - } - - public void Release() - { - this.bound = false; - } - - public void Dispose() - { - if (this.nativeBlendState != null) - { - this.nativeBlendState.Dispose(); - this.nativeBlendState = null; - } - } - - public bool IsBound - { - get - { - return this.bound; - } - } - - public Color BlendFactor - { - set - { + #region Public + public Color BlendFactor + { + set + { const float colorConvert = 1f / 255f; blendFactor.Red = value.R * colorConvert; blendFactor.Green = value.G * colorConvert; blendFactor.Blue = value.B * colorConvert; blendFactor.Alpha = value.A * colorConvert; - } - } + } + } - public int MultiSampleMask - { - set - { - this.multiSampleMask = value; - } - } + public int MultiSampleMask + { + set + { + this.multiSampleMask = value; + } + } - public BlendFunction AlphaBlendFunction - { - set - { - BlendOperation alphaBlendOperation = FormatConverter.Translate(value); + public BlendFunction AlphaBlendFunction + { + set + { + Dx11.BlendOperation alphaBlendOperation = FormatConverter.Translate(value); + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + if (blendStateDescription.RenderTarget[i].AlphaBlendOperation != alphaBlendOperation) + { + isDirty = true; + blendStateDescription.RenderTarget[i].AlphaBlendOperation = alphaBlendOperation; + } - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - if (blendStateDescription.RenderTarget[i].AlphaBlendOperation != alphaBlendOperation) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[i].AlphaBlendOperation = alphaBlendOperation; - } + } + } + } - } - } - } + public BlendFunction ColorBlendFunction + { + set + { + Dx11.BlendOperation blendOperation = FormatConverter.Translate(value); - public BlendFunction ColorBlendFunction - { - set - { - BlendOperation blendOperation = FormatConverter.Translate(value); + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + if (blendStateDescription.RenderTarget[i].BlendOperation != blendOperation) + { + isDirty = true; + blendStateDescription.RenderTarget[i].BlendOperation = blendOperation; + } - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - if (blendStateDescription.RenderTarget[i].BlendOperation != blendOperation) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[i].BlendOperation = blendOperation; - } + } + } + } - } - } - } + public Blend AlphaDestinationBlend + { + set + { + Dx11.BlendOption destinationAlphaBlend = FormatConverter.Translate(value); - public Blend AlphaDestinationBlend - { - set - { - BlendOption destinationAlphaBlend = FormatConverter.Translate(value); + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + if (blendStateDescription.RenderTarget[i].DestinationAlphaBlend != destinationAlphaBlend) + { + isDirty = true; + blendStateDescription.RenderTarget[i].DestinationAlphaBlend = destinationAlphaBlend; + } - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - if (blendStateDescription.RenderTarget[i].DestinationAlphaBlend != destinationAlphaBlend) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[i].DestinationAlphaBlend = destinationAlphaBlend; - } + } + } + } - } - } - } + public Blend ColorDestinationBlend + { + set + { + Dx11.BlendOption destinationBlend = FormatConverter.Translate(value); - public Blend ColorDestinationBlend - { - set - { - BlendOption destinationBlend = FormatConverter.Translate(value); + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + if (blendStateDescription.RenderTarget[i].DestinationBlend != destinationBlend) + { + isDirty = true; + blendStateDescription.RenderTarget[i].DestinationBlend = destinationBlend; + } - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - if (blendStateDescription.RenderTarget[i].DestinationBlend != destinationBlend) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[i].DestinationBlend = destinationBlend; - } + } + } + } - } - } - } + public ColorWriteChannels ColorWriteChannels + { + set + { + Dx11.ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); - public ColorWriteChannels ColorWriteChannels - { - set - { - ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); + //TODO: range check - //TODO: range check + if (blendStateDescription.RenderTarget[0].RenderTargetWriteMask != renderTargetWriteMask) + { + isDirty = true; + blendStateDescription.RenderTarget[0].RenderTargetWriteMask = renderTargetWriteMask; + } + } + } - if (blendStateDescription.RenderTarget[0].RenderTargetWriteMask != renderTargetWriteMask) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[0].RenderTargetWriteMask = renderTargetWriteMask; - } - } - } + public ColorWriteChannels ColorWriteChannels1 + { + set + { + Dx11.ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); - public ColorWriteChannels ColorWriteChannels1 - { - set - { - ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); + //TODO: range check - //TODO: range check + if (blendStateDescription.RenderTarget[1].RenderTargetWriteMask != renderTargetWriteMask) + { + isDirty = true; + blendStateDescription.RenderTarget[1].RenderTargetWriteMask = renderTargetWriteMask; + } + } + } - if (blendStateDescription.RenderTarget[1].RenderTargetWriteMask != renderTargetWriteMask) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[1].RenderTargetWriteMask = renderTargetWriteMask; - } - } - } + public ColorWriteChannels ColorWriteChannels2 + { + set + { + Dx11.ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); - public ColorWriteChannels ColorWriteChannels2 - { - set - { - ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); + //TODO: range check - //TODO: range check + if (blendStateDescription.RenderTarget[2].RenderTargetWriteMask != renderTargetWriteMask) + { + isDirty = true; + blendStateDescription.RenderTarget[2].RenderTargetWriteMask = renderTargetWriteMask; + } + } + } - if (blendStateDescription.RenderTarget[2].RenderTargetWriteMask != renderTargetWriteMask) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[2].RenderTargetWriteMask = renderTargetWriteMask; - } - } - } + public ColorWriteChannels ColorWriteChannels3 + { + set + { + Dx11.ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); - public ColorWriteChannels ColorWriteChannels3 - { - set - { - ColorWriteMaskFlags renderTargetWriteMask = FormatConverter.Translate(value); + //TODO: range check - //TODO: range check + if (blendStateDescription.RenderTarget[3].RenderTargetWriteMask != renderTargetWriteMask) + { + isDirty = true; + blendStateDescription.RenderTarget[3].RenderTargetWriteMask = renderTargetWriteMask; + } + } + } - if (blendStateDescription.RenderTarget[3].RenderTargetWriteMask != renderTargetWriteMask) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[3].RenderTargetWriteMask = renderTargetWriteMask; - } - } - } + public Blend AlphaSourceBlend + { + set + { + Dx11.BlendOption sourceAlphaBlend = FormatConverter.Translate(value); - public Blend AlphaSourceBlend - { - set - { - BlendOption sourceAlphaBlend = FormatConverter.Translate(value); + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + if (blendStateDescription.RenderTarget[i].SourceAlphaBlend != sourceAlphaBlend) + { + isDirty = true; + blendStateDescription.RenderTarget[i].SourceAlphaBlend = sourceAlphaBlend; + } - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - if (blendStateDescription.RenderTarget[i].SourceAlphaBlend != sourceAlphaBlend) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[i].SourceAlphaBlend = sourceAlphaBlend; - } + } + } + } - } - } - } + public Blend ColorSourceBlend + { + set + { + Dx11.BlendOption sourceBlend = FormatConverter.Translate(value); - public Blend ColorSourceBlend - { - set - { - BlendOption sourceBlend = FormatConverter.Translate(value); + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + if (blendStateDescription.RenderTarget[i].SourceBlend != sourceBlend) + { + isDirty = true; + blendStateDescription.RenderTarget[i].SourceBlend = sourceBlend; + } - for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) - { - if (blendStateDescription.RenderTarget[i].SourceBlend != sourceBlend) - { - nativeBlendStateDirty = true; - blendStateDescription.RenderTarget[i].SourceBlend = sourceBlend; - } + } + } + } + #endregion + + protected override void Init() + { + for (int i = 0; i < blendStateDescription.RenderTarget.Length; i++) + { + blendStateDescription.RenderTarget[i].IsBlendEnabled = (i < 4); + blendStateDescription.IndependentBlendEnable = true; + } + } - } - } - } + protected override Dx11.BlendState CreateNativeState(GraphicsDevice graphics) + { + Dx11.DeviceContext context = (graphics.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + return new Dx11.BlendState(context.Device, ref blendStateDescription); + } - private void UpdateNativeBlendState(SharpDX.Direct3D11.Device device) - { - if (this.nativeBlendStateDirty == true || this.nativeBlendState == null) - { - if (this.nativeBlendState != null) - { - this.nativeBlendState.Dispose(); - this.nativeBlendState = null; - } - - this.nativeBlendState = new SharpDX.Direct3D11.BlendState(device, ref this.blendStateDescription); - - this.nativeBlendStateDirty = false; - } - } + protected override void ApplyNativeState(GraphicsDevice graphics) + { + Dx11.DeviceContext context = (graphics.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + context.OutputMerger.SetBlendState(nativeState, blendFactor, multiSampleMask); + } } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs index d87b75a3..faddea63 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs @@ -1,17 +1,12 @@ -#region Using Statements using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using ANX.Framework.Graphics; using System.IO; -using ANX.Framework.NonXNA; -using System.Runtime.InteropServices; -using SharpDX.DXGI; -using ANX.Framework.NonXNA.RenderSystem; using ANX.Framework; - -#endregion // Using Statements +using ANX.Framework.Graphics; +using ANX.Framework.NonXNA; +using ANX.Framework.NonXNA.RenderSystem; +using SharpDX.DXGI; +using ANX.BaseDirectX; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -21,6 +16,7 @@ namespace ANX.RenderSystem.Windows.DX11 { public class Creator : IRenderSystemCreator { + #region Public public string Name { get { return "DirectX11"; } @@ -40,99 +36,104 @@ namespace ANX.RenderSystem.Windows.DX11 } } + public EffectSourceLanguage GetStockShaderSourceLanguage + { + get + { + return EffectSourceLanguage.HLSL_FX; + } + } + #endregion + + #region CreateGraphicsDevice public INativeGraphicsDevice CreateGraphicsDevice(PresentationParameters presentationParameters) { - AddInSystemFactory.Instance.PreventSystemChange( - AddInType.RenderSystem); + PreventSystemChange(); return new GraphicsDeviceWindowsDX11(presentationParameters); } + #endregion - public INativeIndexBuffer CreateIndexBuffer(GraphicsDevice graphics, - IndexBuffer managedBuffer, IndexElementSize size, int indexCount, BufferUsage usage) + #region CreateIndexBuffer + public INativeIndexBuffer CreateIndexBuffer(GraphicsDevice graphics, IndexBuffer managedBuffer, IndexElementSize size, + int indexCount, BufferUsage usage) { - AddInSystemFactory.Instance.PreventSystemChange( - AddInType.RenderSystem); + PreventSystemChange(); return new IndexBuffer_DX11(graphics, size, indexCount, usage); } + #endregion - public INativeVertexBuffer CreateVertexBuffer(GraphicsDevice graphics, - VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + #region CreateVertexBuffer + public INativeVertexBuffer CreateVertexBuffer(GraphicsDevice graphics, VertexBuffer managedBuffer, + VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); return new VertexBuffer_DX11(graphics, vertexDeclaration, vertexCount, usage); } + #endregion #if XNAEXT #region CreateConstantBuffer - public INativeConstantBuffer CreateConstantBuffer(GraphicsDevice graphics, ConstantBuffer managedBuffer, BufferUsage usage) - { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + public INativeConstantBuffer CreateConstantBuffer(GraphicsDevice graphics, ConstantBuffer managedBuffer, + BufferUsage usage) + { + PreventSystemChange(); throw new NotImplementedException(); } #endregion #endif - public INativeEffect CreateEffect(GraphicsDevice graphics, ANX.Framework.Graphics.Effect managedEffect, Stream vertexShaderByteCode, Stream pixelShaderByteCode) + #region CreateEffect + public INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect, Stream vertexShaderByteCode, + Stream pixelShaderByteCode) { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - - Effect_DX11 effect = new Effect_DX11(graphics, managedEffect, vertexShaderByteCode, pixelShaderByteCode); - - return effect; + PreventSystemChange(); + return new Effect_DX11(graphics, managedEffect, vertexShaderByteCode, pixelShaderByteCode); } - public INativeEffect CreateEffect(GraphicsDevice graphics, ANX.Framework.Graphics.Effect managedEffect, System.IO.Stream byteCode) + public INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect, Stream byteCode) { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - - Effect_DX11 effect = new Effect_DX11(graphics, managedEffect, byteCode); - - return effect; - } - - public Texture2D CreateTexture(GraphicsDevice graphics, string fileName) - { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - - //TODO: implement - throw new NotImplementedException(); - - //GraphicsDeviceWindowsDX10 graphicsDX10 = graphics.NativeDevice as GraphicsDeviceWindowsDX10; - //SharpDX.Direct3D10.Texture2D nativeTexture = SharpDX.Direct3D10.Texture2D.FromFile(graphicsDX10.NativeDevice, fileName); - //Texture2D_DX10 texture = new Texture2D_DX10(graphics, nativeTexture.Description.Width, nativeTexture.Description.Height, FormatConverter.Translate(nativeTexture.Description.Format), nativeTexture.Description.MipLevels); - //texture.NativeTexture = nativeTexture; - - //return texture; + PreventSystemChange(); + return new Effect_DX11(graphics, managedEffect, byteCode); } + #endregion + #region CreateBlendState public INativeBlendState CreateBlendState() { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); return new BlendState_DX11(); } + #endregion + #region CreateRasterizerState public INativeRasterizerState CreateRasterizerState() { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); return new RasterizerState_DX11(); } + #endregion + #region CreateDepthStencilState public INativeDepthStencilState CreateDepthStencilState() { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); return new DepthStencilState_DX11(); } + #endregion + #region CreateSamplerState public INativeSamplerState CreateSamplerState() { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); return new SamplerState_DX11(); } + #endregion + #region GetShaderByteCode public byte[] GetShaderByteCode(PreDefinedShader type) { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); if (type == PreDefinedShader.SpriteBatch) { @@ -159,22 +160,16 @@ namespace ANX.RenderSystem.Windows.DX11 return ShaderByteCode.SkinnedEffectByteCode; } - throw new NotImplementedException("ByteCode for '" + type.ToString() + "' is not yet available"); + throw new NotImplementedException("ByteCode for '" + type + "' is not yet available"); } + #endregion - public EffectSourceLanguage GetStockShaderSourceLanguage - { - get - { - return EffectSourceLanguage.HLSL_FX; - } - } - + #region GetAdapterList public System.Collections.ObjectModel.ReadOnlyCollection GetAdapterList() { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + PreventSystemChange(); - SharpDX.DXGI.Factory factory = new Factory(); + var factory = new Factory(); List adapterList = new List(); DisplayModeCollection displayModeCollection = new DisplayModeCollection(); @@ -202,7 +197,7 @@ namespace ANX.RenderSystem.Windows.DX11 { DisplayMode displayMode = new DisplayMode() { - Format = FormatConverter.Translate(modeDescription.Format), + Format = BaseFormatConverter.Translate(modeDescription.Format), Width = modeDescription.Width, Height = modeDescription.Height, AspectRatio = (float)modeDescription.Width / (float)modeDescription.Height, @@ -223,29 +218,39 @@ namespace ANX.RenderSystem.Windows.DX11 return new System.Collections.ObjectModel.ReadOnlyCollection(adapterList); } + #endregion - public INativeTexture2D CreateTexture(GraphicsDevice graphics, SurfaceFormat surfaceFormat, int width, int height, int mipCount) + #region CreateTexture + public INativeTexture2D CreateTexture(GraphicsDevice graphics, SurfaceFormat surfaceFormat, int width, int height, + int mipCount) { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - + PreventSystemChange(); return new Texture2D_DX11(graphics, width, height, surfaceFormat, mipCount); } + #endregion - public INativeRenderTarget2D CreateRenderTarget(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) + #region CreateRenderTarget + public INativeRenderTarget2D CreateRenderTarget(GraphicsDevice graphics, int width, int height, bool mipMap, + SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, + RenderTargetUsage usage) { - AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - - return new RenderTarget2D_DX11(graphics, width, height, mipMap, preferredFormat, preferredDepthFormat, preferredMultiSampleCount, usage); + PreventSystemChange(); + return new RenderTarget2D_DX11(graphics, width, height, mipMap, preferredFormat, preferredDepthFormat, + preferredMultiSampleCount, usage); } + #endregion - public bool IsLanguageSupported(EffectSourceLanguage sourceLanguage) + #region IsLanguageSupported + public bool IsLanguageSupported(EffectSourceLanguage sourceLanguage) { return sourceLanguage == EffectSourceLanguage.HLSL_FX || sourceLanguage == EffectSourceLanguage.HLSL; } + #endregion #region CreateOcclusionQuery (TODO) public IOcclusionQuery CreateOcclusionQuery() { + PreventSystemChange(); throw new NotImplementedException(); } #endregion @@ -253,8 +258,16 @@ namespace ANX.RenderSystem.Windows.DX11 #region SetTextureSampler (TODO) public void SetTextureSampler(int index, Texture value) { + PreventSystemChange(); throw new NotImplementedException(); } #endregion + + #region PreventSystemChange + private void PreventSystemChange() + { + AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); + } + #endregion } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/DepthStencilState_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/DepthStencilState_DX11.cs index 35a26198..552e8f2f 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/DepthStencilState_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/DepthStencilState_DX11.cs @@ -1,14 +1,8 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using SharpDX.Direct3D11; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; -using ANX.Framework; - -#endregion // Using Statements +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -16,284 +10,171 @@ using ANX.Framework; namespace ANX.RenderSystem.Windows.DX11 { - public class DepthStencilState_DX11 : INativeDepthStencilState + public class DepthStencilState_DX11 : BaseStateObject, INativeDepthStencilState { - #region Private Members - private DepthStencilStateDescription description; - private SharpDX.Direct3D11.DepthStencilState nativeDepthStencilState; - private bool nativeDepthStencilStateDirty; - private bool bound; - + private Dx11.DepthStencilStateDescription description; private int referenceStencil; - #endregion // Private Members + #region Public + public StencilOperation CounterClockwiseStencilDepthBufferFail + { + set + { + Dx11.StencilOperation operation = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.DepthFailOperation, ref operation); + } + } - public DepthStencilState_DX11() - { - this.description = new DepthStencilStateDescription(); + public StencilOperation CounterClockwiseStencilFail + { + set + { + Dx11.StencilOperation operation = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.FailOperation, ref operation); + } + } - this.nativeDepthStencilStateDirty = true; - } + public CompareFunction CounterClockwiseStencilFunction + { + set + { + Dx11.Comparison comparison = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.Comparison, ref comparison); + } + } - public void Apply(ANX.Framework.Graphics.GraphicsDevice graphicsDevice) - { - GraphicsDeviceWindowsDX11 gdx11 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - DeviceContext context = gdx11.NativeDevice; + public StencilOperation CounterClockwiseStencilPass + { + set + { + Dx11.StencilOperation operation = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.BackFace.PassOperation, ref operation); + } + } - UpdateNativeDepthStencilState(context.Device); - this.bound = true; + public bool DepthBufferEnable + { + set + { + if (description.IsDepthEnabled != value) + { + description.IsDepthEnabled = value; + isDirty = true; + } + } + } - context.OutputMerger.SetDepthStencilState(nativeDepthStencilState, this.referenceStencil); - } + public CompareFunction DepthBufferFunction + { + set + { + Dx11.Comparison comparison = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.DepthComparison, ref comparison); + } + } - public void Release() - { - this.bound = false; - } + public bool DepthBufferWriteEnable + { + set + { + Dx11.DepthWriteMask writeMask = value ? Dx11.DepthWriteMask.All : Dx11.DepthWriteMask.Zero; + SetValueIfDifferentAndMarkDirty(ref description.DepthWriteMask, ref writeMask); + } + } - public void Dispose() - { - if (this.nativeDepthStencilState != null) - { - this.nativeDepthStencilState.Dispose(); - this.nativeDepthStencilState = null; - } - } + public int ReferenceStencil + { + set + { + SetValueIfDifferentAndMarkDirty(ref referenceStencil, ref value); + } + } - public bool IsBound - { - get - { - return this.bound; - } - } + public StencilOperation StencilDepthBufferFail + { + set + { + Dx11.StencilOperation operation = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.DepthFailOperation, ref operation); + } + } - public ANX.Framework.Graphics.StencilOperation CounterClockwiseStencilDepthBufferFail - { - set - { - SharpDX.Direct3D11.StencilOperation operation = FormatConverter.Translate(value); + public bool StencilEnable + { + set + { + if (description.IsStencilEnabled != value) + { + description.IsStencilEnabled = value; + isDirty = true; + } + } + } - if (description.BackFace.DepthFailOperation != operation) - { - description.BackFace.DepthFailOperation = operation; - nativeDepthStencilStateDirty = true; - } - } - } + public StencilOperation StencilFail + { + set + { + Dx11.StencilOperation operation = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.FailOperation, ref operation); + } + } - public ANX.Framework.Graphics.StencilOperation CounterClockwiseStencilFail - { - set - { - SharpDX.Direct3D11.StencilOperation operation = FormatConverter.Translate(value); + public CompareFunction StencilFunction + { + set + { + Dx11.Comparison comparison = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.Comparison, ref comparison); + } + } - if (description.BackFace.FailOperation != operation) - { - description.BackFace.FailOperation = operation; - nativeDepthStencilStateDirty = true; - } - } - } + public int StencilMask + { + set + { + byte stencilMask = (byte)value; //TODO: check range + SetValueIfDifferentAndMarkDirty(ref description.StencilReadMask, ref stencilMask); + } + } - public ANX.Framework.Graphics.CompareFunction CounterClockwiseStencilFunction - { - set - { - SharpDX.Direct3D11.Comparison comparison = FormatConverter.Translate(value); + public StencilOperation StencilPass + { + set + { + Dx11.StencilOperation operation = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.FrontFace.PassOperation, ref operation); + } + } - if (description.BackFace.Comparison != comparison) - { - description.BackFace.Comparison = comparison; - nativeDepthStencilStateDirty = true; - } - } - } + public int StencilWriteMask + { + set + { + byte stencilWriteMask = (byte)value; //TODO: check range + SetValueIfDifferentAndMarkDirty(ref description.StencilWriteMask, ref stencilWriteMask); + } + } - public ANX.Framework.Graphics.StencilOperation CounterClockwiseStencilPass - { - set - { - SharpDX.Direct3D11.StencilOperation operation = FormatConverter.Translate(value); + public bool TwoSidedStencilMode + { + set + { + //TODO: check if we really need this. in xna this enables only counter clockwise stencil operations + } + } + #endregion - if (description.BackFace.PassOperation != operation) - { - description.BackFace.PassOperation = operation; - nativeDepthStencilStateDirty = true; - } - } - } + protected override Dx11.DepthStencilState CreateNativeState(GraphicsDevice graphics) + { + Dx11.DeviceContext context = (graphics.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + return new Dx11.DepthStencilState(context.Device, ref description); + } - public bool DepthBufferEnable - { - set - { - if (description.IsDepthEnabled != value) - { - description.IsDepthEnabled = value; - nativeDepthStencilStateDirty = true; - } - } - } - - public ANX.Framework.Graphics.CompareFunction DepthBufferFunction - { - set - { - SharpDX.Direct3D11.Comparison comparison = FormatConverter.Translate(value); - - if (description.DepthComparison != comparison) - { - description.DepthComparison = comparison; - nativeDepthStencilStateDirty = true; - } - } - } - - public bool DepthBufferWriteEnable - { - set - { - DepthWriteMask writeMask = value ? DepthWriteMask.All : DepthWriteMask.Zero; - - if (description.DepthWriteMask != writeMask) - { - description.DepthWriteMask = writeMask; - nativeDepthStencilStateDirty = true; - } - } - } - - public int ReferenceStencil - { - set - { - if (this.referenceStencil != value) - { - this.referenceStencil = value; - this.nativeDepthStencilStateDirty = true; - } - } - } - - public ANX.Framework.Graphics.StencilOperation StencilDepthBufferFail - { - set - { - SharpDX.Direct3D11.StencilOperation operation = FormatConverter.Translate(value); - - if (description.FrontFace.DepthFailOperation != operation) - { - description.FrontFace.DepthFailOperation = operation; - nativeDepthStencilStateDirty = true; - } - } - } - - public bool StencilEnable - { - set - { - if (description.IsStencilEnabled != value) - { - description.IsStencilEnabled = value; - nativeDepthStencilStateDirty = true; - } - } - } - - public ANX.Framework.Graphics.StencilOperation StencilFail - { - set - { - SharpDX.Direct3D11.StencilOperation operation = FormatConverter.Translate(value); - - if (description.FrontFace.FailOperation != operation) - { - description.FrontFace.FailOperation = operation; - nativeDepthStencilStateDirty = true; - } - } - } - - public ANX.Framework.Graphics.CompareFunction StencilFunction - { - set - { - SharpDX.Direct3D11.Comparison comparison = FormatConverter.Translate(value); - - if (description.FrontFace.Comparison != comparison) - { - description.FrontFace.Comparison = comparison; - nativeDepthStencilStateDirty = true; - } - } - } - - public int StencilMask - { - set - { - byte stencilMask = (byte)value; //TODO: check range - - if (description.StencilReadMask != stencilMask) - { - description.StencilReadMask = stencilMask; - nativeDepthStencilStateDirty = true; - } - } - } - - public ANX.Framework.Graphics.StencilOperation StencilPass - { - set - { - SharpDX.Direct3D11.StencilOperation operation = FormatConverter.Translate(value); - - if (description.FrontFace.PassOperation != operation) - { - description.FrontFace.PassOperation = operation; - nativeDepthStencilStateDirty = true; - } - } - } - - public int StencilWriteMask - { - set - { - byte stencilWriteMask = (byte)value; //TODO: check range - - if (description.StencilWriteMask != stencilWriteMask) - { - description.StencilWriteMask = stencilWriteMask; - nativeDepthStencilStateDirty = true; - } - } - } - - public bool TwoSidedStencilMode - { - set - { - //TODO: check if we really need this. in xna this enables only counter clockwise stencil operations - } - } - - private void UpdateNativeDepthStencilState(Device device) - { - if (this.nativeDepthStencilStateDirty == true || this.nativeDepthStencilState == null) - { - if (this.nativeDepthStencilState != null) - { - this.nativeDepthStencilState.Dispose(); - this.nativeDepthStencilState = null; - } - - this.nativeDepthStencilState = new SharpDX.Direct3D11.DepthStencilState(device, ref this.description); - - this.nativeDepthStencilStateDirty = false; - } - } - } + protected override void ApplyNativeState(GraphicsDevice graphics) + { + Dx11.DeviceContext context = (graphics.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + context.OutputMerger.SetDepthStencilState(nativeState, referenceStencil); + } + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectParameter_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectParameter_DX11.cs index f40aed25..3f11f0e7 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectParameter_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectParameter_DX11.cs @@ -1,289 +1,275 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using ANX.Framework.NonXNA; -using SharpDX.Direct3D11; -using ANX.Framework.Graphics; -using ANX.Framework.NonXNA.RenderSystem; using ANX.Framework; - -#endregion // Using Statements +using ANX.Framework.Graphics; +using ANX.Framework.NonXNA; +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license -using Texture2D = ANX.Framework.Graphics.Texture2D; -using Texture3D = ANX.Framework.Graphics.Texture3D; - namespace ANX.RenderSystem.Windows.DX11 { - public class EffectParameter_DX11 : INativeEffectParameter - { - private EffectVariable nativeEffectVariable; - - public EffectVariable NativeParameter - { - get - { - return this.nativeEffectVariable; - } - internal set - { - this.nativeEffectVariable = value; - } - } - - public void SetValue(bool value) - { - nativeEffectVariable.AsScalar().Set(value); - } - - public void SetValue(bool[] value) - { - nativeEffectVariable.AsScalar().Set(value); - } - - public void SetValue(int value) - { - nativeEffectVariable.AsScalar().Set(value); - } - - public void SetValue(int[] value) - { - nativeEffectVariable.AsScalar().Set(value); - } - - public void SetValue(Matrix value, bool transpose) - { - // TODO: handle transpose! - SharpDX.Matrix m = new SharpDX.Matrix(value.M11, value.M12, value.M13, value.M14, value.M21, value.M22, value.M23, value.M24, value.M31, value.M32, value.M33, value.M34, value.M41, value.M42, value.M43, value.M44); - nativeEffectVariable.AsMatrix().SetMatrix(m); - } - - public void SetValue(Matrix[] value, bool transpose) - { - // TODO: handle transpose! - int count = value.Length; - SharpDX.Matrix[] m = new SharpDX.Matrix[count]; - Matrix anxMatrix; - for (int i = 0; i < count; i++) - { - anxMatrix = value[i]; - m[i] = new SharpDX.Matrix(anxMatrix.M11, anxMatrix.M12, anxMatrix.M13, anxMatrix.M14, - anxMatrix.M21, anxMatrix.M22, anxMatrix.M23, anxMatrix.M24, - anxMatrix.M31, anxMatrix.M32, anxMatrix.M33, anxMatrix.M34, - anxMatrix.M41, anxMatrix.M42, anxMatrix.M43, anxMatrix.M44); - } - - nativeEffectVariable.AsMatrix().SetMatrix(m); - } - - public void SetValue(Quaternion value) - { - SharpDX.Vector4 q = new SharpDX.Vector4(value.X, value.Y, value.Z, value.W); - nativeEffectVariable.AsVector().Set(q); - } - - public void SetValue(Quaternion[] value) - { - int count = value.Length; - SharpDX.Vector4[] q = new SharpDX.Vector4[count]; - for (int i = 0; i < count; i++) - { - q[i] = new SharpDX.Vector4(value[i].X, value[i].Y, value[i].Z, value[i].W); - } - nativeEffectVariable.AsVector().Set(q); - } - - public void SetValue(float value) - { - nativeEffectVariable.AsScalar().Set(value); - } - - public void SetValue(float[] value) - { - nativeEffectVariable.AsScalar().Set(value); - } - - public void SetValue(Vector2 value) - { - SharpDX.Vector2 v = new SharpDX.Vector2(value.X, value.Y); - nativeEffectVariable.AsVector().Set(v); - } - - public void SetValue(Vector2[] value) - { - throw new NotImplementedException(); - } - - public void SetValue(Vector3 value) - { - SharpDX.Vector3 v = new SharpDX.Vector3(value.X, value.Y, value.Z); - nativeEffectVariable.AsVector().Set(v); - } - - public void SetValue(Vector3[] value) - { - throw new NotImplementedException(); - } - - public void SetValue(Vector4 value) - { - SharpDX.Vector4 v = new SharpDX.Vector4(value.X, value.Y, value.Z, value.W); - nativeEffectVariable.AsVector().Set(v); - } - - public void SetValue(Vector4[] value) - { - int count = value.Length; - SharpDX.Vector4[] q = new SharpDX.Vector4[count]; - for (int i = 0; i < count; i++) - { - q[i] = new SharpDX.Vector4(value[i].X, value[i].Y, value[i].Z, value[i].W); - } - nativeEffectVariable.AsVector().Set(q); - } - - public void SetValue(Texture value) - { - Texture2D_DX11 tex = value.NativeTexture as Texture2D_DX11; - nativeEffectVariable.AsShaderResource().SetResource(tex.NativeShaderResourceView); - } - - public string Name - { - get - { - return nativeEffectVariable.Description.Name; - } - } - - #region INativeEffectParameter Member - - - public bool GetValueBoolean() - { - throw new NotImplementedException(); - } - - public bool[] GetValueBooleanArray(int count) - { - throw new NotImplementedException(); - } - - public int GetValueInt32() - { - throw new NotImplementedException(); - } - - public int[] GetValueInt32Array(int count) - { - throw new NotImplementedException(); - } - - public Matrix GetValueMatrix() - { - throw new NotImplementedException(); - } - - public Matrix[] GetValueMatrixArray(int count) - { - throw new NotImplementedException(); - } - - public Matrix GetValueMatrixTranspose() - { - throw new NotImplementedException(); - } - - public Matrix[] GetValueMatrixTransposeArray(int count) - { - throw new NotImplementedException(); - } - - public Quaternion GetValueQuaternion() - { - throw new NotImplementedException(); - } - - public Quaternion[] GetValueQuaternionArray(int count) - { - throw new NotImplementedException(); - } - - public float GetValueSingle() - { - throw new NotImplementedException(); - } - - public float[] GetValueSingleArray(int count) - { - throw new NotImplementedException(); - } - - public string GetValueString() - { - throw new NotImplementedException(); - } - - public Framework.Graphics.Texture2D GetValueTexture2D() - { - throw new NotImplementedException(); - } - - public Framework.Graphics.Texture3D GetValueTexture3D() - { - throw new NotImplementedException(); - } - - public TextureCube GetValueTextureCube() - { - throw new NotImplementedException(); - } - - public Vector2 GetValueVector2() - { - throw new NotImplementedException(); - } - - public Vector2[] GetValueVector2Array(int count) - { - throw new NotImplementedException(); - } - - public Vector3 GetValueVector3() - { - throw new NotImplementedException(); - } - - public Vector3[] GetValueVector3Array(int count) - { - throw new NotImplementedException(); - } - - public Vector4 GetValueVector4() - { - throw new NotImplementedException(); - } - - public Vector4[] GetValueVector4Array(int count) - { - throw new NotImplementedException(); - } - - #endregion - - #region INativeEffectParameter Member - - - public void SetValue(string value) - { - throw new NotImplementedException(); - } - - #endregion + public class EffectParameter_DX11 : INativeEffectParameter + { + #region Public + public Dx11.EffectVariable NativeParameter + { + get; + internal set; } + + public string Name + { + get + { + return NativeParameter.Description.Name; + } + } + #endregion + + #region SetValue (bool) + public void SetValue(bool value) + { + NativeParameter.AsScalar().Set(value); + } + #endregion + + #region SetValue (bool[]) + public void SetValue(bool[] value) + { + NativeParameter.AsScalar().Set(value); + } + #endregion + + #region SetValue (int) + public void SetValue(int value) + { + NativeParameter.AsScalar().Set(value); + } + #endregion + + #region SetValue (int[]) + public void SetValue(int[] value) + { + NativeParameter.AsScalar().Set(value); + } + #endregion + + #region SetValue (Matrix, transpose) (TODO) + public void SetValue(Matrix value, bool transpose) + { + // TODO: handle transpose! + NativeParameter.AsMatrix().SetMatrix(value); + } + #endregion + + #region SetValue (Matrix[], transpose) (TODO) + public void SetValue(Matrix[] value, bool transpose) + { + // TODO: handle transpose! + NativeParameter.AsMatrix().SetMatrix(value); + } + #endregion + + #region SetValue (Quaternion) + public void SetValue(Quaternion value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Quaternion[]) + public void SetValue(Quaternion[] value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (float) + public void SetValue(float value) + { + NativeParameter.AsScalar().Set(value); + } + #endregion + + #region SetValue (float[]) + public void SetValue(float[] value) + { + NativeParameter.AsScalar().Set(value); + } + #endregion + + #region SetValue (Vector2) + public void SetValue(Vector2 value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Vector2[]) + public void SetValue(Vector2[] value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Vector3) + public void SetValue(Vector3 value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Vector3[]) + public void SetValue(Vector3[] value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Vector4) + public void SetValue(Vector4 value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Vector4[]) + public void SetValue(Vector4[] value) + { + NativeParameter.AsVector().Set(value); + } + #endregion + + #region SetValue (Texture) + public void SetValue(Texture value) + { + if (value == null) + throw new ArgumentNullException("value"); + + var tex = value.NativeTexture as Texture2D_DX11; + NativeParameter.AsShaderResource().SetResource(tex.NativeShaderResourceView); + } + #endregion + + #region SetValue (string) (TODO) + public void SetValue(string value) + { + throw new NotImplementedException(); + } + #endregion + + #region Get (TODO) + public bool GetValueBoolean() + { + throw new NotImplementedException(); + } + + public bool[] GetValueBooleanArray(int count) + { + throw new NotImplementedException(); + } + + public int GetValueInt32() + { + throw new NotImplementedException(); + } + + public int[] GetValueInt32Array(int count) + { + throw new NotImplementedException(); + } + + public Matrix GetValueMatrix() + { + throw new NotImplementedException(); + } + + public Matrix[] GetValueMatrixArray(int count) + { + throw new NotImplementedException(); + } + + public Matrix GetValueMatrixTranspose() + { + throw new NotImplementedException(); + } + + public Matrix[] GetValueMatrixTransposeArray(int count) + { + throw new NotImplementedException(); + } + + public Quaternion GetValueQuaternion() + { + throw new NotImplementedException(); + } + + public Quaternion[] GetValueQuaternionArray(int count) + { + throw new NotImplementedException(); + } + + public float GetValueSingle() + { + throw new NotImplementedException(); + } + + public float[] GetValueSingleArray(int count) + { + throw new NotImplementedException(); + } + + public string GetValueString() + { + throw new NotImplementedException(); + } + + public Texture2D GetValueTexture2D() + { + throw new NotImplementedException(); + } + + public Texture3D GetValueTexture3D() + { + throw new NotImplementedException(); + } + + public TextureCube GetValueTextureCube() + { + throw new NotImplementedException(); + } + + public Vector2 GetValueVector2() + { + throw new NotImplementedException(); + } + + public Vector2[] GetValueVector2Array(int count) + { + throw new NotImplementedException(); + } + + public Vector3 GetValueVector3() + { + throw new NotImplementedException(); + } + + public Vector3[] GetValueVector3Array(int count) + { + throw new NotImplementedException(); + } + + public Vector4 GetValueVector4() + { + throw new NotImplementedException(); + } + + public Vector4[] GetValueVector4Array(int count) + { + throw new NotImplementedException(); + } + #endregion + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectPass_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectPass_DX11.cs index afa6d9e2..b118236c 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectPass_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectPass_DX11.cs @@ -1,13 +1,6 @@ -#region Using Statements -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using ANX.Framework.NonXNA; using SharpDX.Direct3D11; -#endregion // Using Statements - // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license @@ -16,26 +9,19 @@ namespace ANX.RenderSystem.Windows.DX11 { public class EffectPass_DX11 : INativeEffectPass { - private EffectPass nativePass; - - public EffectPass NativePass - { - get - { - return this.nativePass; - } - internal set - { - this.nativePass = value; - } - } + private EffectPass nativePass; public string Name { get { - return nativePass.Description.Name; + return nativePass.Description.Name; } } + + internal EffectPass_DX11(EffectPass setNativePass) + { + nativePass = setNativePass; + } } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectTechnique_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectTechnique_DX11.cs index 0e18a516..975c70b3 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectTechnique_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/EffectTechnique_DX11.cs @@ -1,12 +1,8 @@ -#region Using Statements -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; +using ANX.BaseDirectX; +using ANX.Framework.Graphics; using ANX.Framework.NonXNA; -using SharpDX.Direct3D11; - -#endregion // Using Statements +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -14,56 +10,32 @@ using SharpDX.Direct3D11; namespace ANX.RenderSystem.Windows.DX11 { - public class EffectTechnique_DX11 : INativeEffectTechnique - { - private EffectTechnique nativeTechnique; - private ANX.Framework.Graphics.Effect parentEffect; + public class EffectTechnique_DX11 : BaseEffectTechnique, INativeEffectTechnique + { + public string Name + { + get + { + return NativeTechnique.Description.Name; + } + } - internal EffectTechnique_DX11(ANX.Framework.Graphics.Effect parentEffect) - { - if (parentEffect == null) - { - throw new ArgumentNullException("parentEffect"); - } + public IEnumerable Passes + { + get + { + for (int i = 0; i < NativeTechnique.Description.PassCount; i++) + { + var passDx11 = new EffectPass_DX11(NativeTechnique.GetPassByIndex(i)); + // TODO: wire up native pass and managed pass? + yield return new EffectPass(this.parentEffect); + } + } + } - this.parentEffect = parentEffect; - } - - public EffectTechnique NativeTechnique - { - get - { - return this.nativeTechnique; - } - internal set - { - this.nativeTechnique = value; - } - } - - public string Name - { - get - { - return nativeTechnique.Description.Name; - } - } - - - public IEnumerable Passes - { - get - { - for (int i = 0; i < nativeTechnique.Description.PassCount; i++) - { - EffectPass_DX11 passDx11 = new EffectPass_DX11(); - passDx11.NativePass = nativeTechnique.GetPassByIndex(i); - - ANX.Framework.Graphics.EffectPass pass = new ANX.Framework.Graphics.EffectPass(this.parentEffect); - - yield return pass; - } - } - } - } + public EffectTechnique_DX11(Effect parentEffect, Dx11.EffectTechnique nativeTechnique) + : base(parentEffect, nativeTechnique) + { + } + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/Effect_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/Effect_DX11.cs index bb5675fb..4dad8125 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/Effect_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/Effect_DX11.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; -using SharpDX.D3DCompiler; using System.IO; -using ANX.Framework.NonXNA; using ANX.Framework.Graphics; +using ANX.Framework.NonXNA; +using ANX.RenderSystem.Windows.DX11.Helpers; +using SharpDX.D3DCompiler; using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the @@ -33,8 +34,7 @@ namespace ANX.RenderSystem.Windows.DX11 { for (int i = 0; i < NativeEffect.Description.TechniqueCount; i++) { - EffectTechnique_DX11 teqDx11 = new EffectTechnique_DX11(this.managedEffect); - teqDx11.NativeTechnique = NativeEffect.GetTechniqueByIndex(i); + var teqDx11 = new EffectTechnique_DX11(managedEffect, NativeEffect.GetTechniqueByIndex(i)); yield return new EffectTechnique(this.managedEffect, teqDx11); } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/FormatConverter.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/FormatConverter.cs index d526c84a..122b3fcb 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/FormatConverter.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/FormatConverter.cs @@ -15,82 +15,6 @@ namespace ANX.RenderSystem.Windows.DX11 { internal class FormatConverter { - - public static int FormatSize(SurfaceFormat format) - { - switch (format) - { - case SurfaceFormat.Vector4: - return 16; - //case SurfaceFormat.Vector3: - // return 12; - case SurfaceFormat.Vector2: - return 8; - case SurfaceFormat.Single: - case SurfaceFormat.Color: - //case SurfaceFormat.RGBA1010102: - //case SurfaceFormat.RG32: - return 4; - //case SurfaceFormat.BGR565: - //case SurfaceFormat.BGRA5551: - // return 2; - case SurfaceFormat.Dxt1: - case SurfaceFormat.Dxt3: - case SurfaceFormat.Dxt5: - case SurfaceFormat.Alpha8: - return 1; - default: - throw new ArgumentException("Invalid format"); - } - } - - public static SharpDX.DXGI.Format Translate(SurfaceFormat surfaceFormat) - { - switch (surfaceFormat) - { - case SurfaceFormat.Color: - return SharpDX.DXGI.Format.R8G8B8A8_UNorm; - case SurfaceFormat.Dxt3: - return SharpDX.DXGI.Format.BC2_UNorm; - case SurfaceFormat.Dxt5: - return SharpDX.DXGI.Format.BC3_UNorm; - } - - throw new Exception("can't translate SurfaceFormat: " + surfaceFormat.ToString()); - } - - public static Format Translate(ANX.Framework.Graphics.DepthFormat depthFormat) - { - switch (depthFormat) - { - case DepthFormat.Depth16: - return Format.D16_UNorm; - case DepthFormat.Depth24: - //TODO: no DirectX10 24Bit depth format??? - case DepthFormat.Depth24Stencil8: - return Format.D24_UNorm_S8_UInt; - case DepthFormat.None: - return Format.Unknown; - } - - throw new Exception("can't translate DepthFormat: " + depthFormat.ToString()); - } - - public static SurfaceFormat Translate(SharpDX.DXGI.Format format) - { - switch (format) - { - case SharpDX.DXGI.Format.R8G8B8A8_UNorm: - return SurfaceFormat.Color; - case SharpDX.DXGI.Format.BC2_UNorm: - return SurfaceFormat.Dxt3; - case SharpDX.DXGI.Format.BC3_UNorm: - return SurfaceFormat.Dxt5; - } - - throw new Exception("can't translate Format: " + format.ToString()); - } - public static Filter Translate(TextureFilter filter) { switch (filter) @@ -133,49 +57,6 @@ namespace ANX.RenderSystem.Windows.DX11 return SharpDX.Direct3D11.TextureAddressMode.Clamp; } - public static PrimitiveTopology Translate(PrimitiveType primitiveType) - { - switch (primitiveType) - { - case PrimitiveType.LineList: - return PrimitiveTopology.LineList; - case PrimitiveType.LineStrip: - return PrimitiveTopology.LineStrip; - case PrimitiveType.TriangleList: - return PrimitiveTopology.TriangleList; - case PrimitiveType.TriangleStrip: - return PrimitiveTopology.TriangleStrip; - default: - throw new InvalidOperationException("unknown PrimitiveType: " + primitiveType.ToString()); - } - } - - public static SharpDX.DXGI.Format Translate(IndexElementSize indexElementSize) - { - switch (indexElementSize) - { - case IndexElementSize.SixteenBits: - return Format.R16_UInt; - case IndexElementSize.ThirtyTwoBits: - return Format.R32_UInt; - default: - throw new InvalidOperationException("unknown IndexElementSize: " + indexElementSize.ToString()); - } - } - - public static string Translate(VertexElementUsage usage) - { - //TODO: map the other Usages - if (usage == VertexElementUsage.TextureCoordinate) - { - return "TEXCOORD"; - } - else - { - return usage.ToString().ToUpperInvariant(); - } - } - public static BlendOperation Translate(BlendFunction blendFunction) { switch (blendFunction) @@ -235,29 +116,19 @@ namespace ANX.RenderSystem.Windows.DX11 ColorWriteMaskFlags mask = 0; if ((colorWriteChannels & ColorWriteChannels.All) == ColorWriteChannels.All) - { mask |= ColorWriteMaskFlags.All; - } if ((colorWriteChannels & ColorWriteChannels.Alpha) == ColorWriteChannels.Alpha) - { mask |= ColorWriteMaskFlags.Alpha; - } if ((colorWriteChannels & ColorWriteChannels.Blue) == ColorWriteChannels.Blue) - { mask |= ColorWriteMaskFlags.Blue; - } if ((colorWriteChannels & ColorWriteChannels.Green) == ColorWriteChannels.Green) - { mask |= ColorWriteMaskFlags.Green; - } if ((colorWriteChannels & ColorWriteChannels.Red) == ColorWriteChannels.Red) - { mask |= ColorWriteMaskFlags.Red; - } return mask; } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/GraphicsDeviceWindowsDX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/GraphicsDeviceWindowsDX11.cs index 3f27b251..20f78a1b 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/GraphicsDeviceWindowsDX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/GraphicsDeviceWindowsDX11.cs @@ -1,22 +1,19 @@ -#region Using Statements using System; -using SharpDX.DXGI; -using SharpDX.Direct3D; -using SharpDX.D3DCompiler; -using SharpDX.Direct3D11; using ANX.Framework; -using ANX.Framework.NonXNA; using ANX.Framework.Graphics; -using System.Runtime.InteropServices; - -#endregion // Using Statements +using ANX.Framework.NonXNA; +using ANX.RenderSystem.Windows.DX11.Helpers; +using SharpDX.D3DCompiler; +using SharpDX.Direct3D; +using SharpDX.Direct3D11; +using SharpDX.DXGI; +using Device = SharpDX.Direct3D11.Device; +using ANX.BaseDirectX; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license -using Device = SharpDX.Direct3D11.Device; - namespace ANX.RenderSystem.Windows.DX11 { public class GraphicsDeviceWindowsDX11 : INativeGraphicsDevice @@ -25,30 +22,7 @@ namespace ANX.RenderSystem.Windows.DX11 private const float ColorMultiplier = 1f / 255f; #endregion - #region Interop - [DllImport("user32.dll")] - private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int width, int height, uint uFlags); - - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); - - [DllImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); - - [StructLayout(LayoutKind.Sequential)] - public struct RECT - { - public int Left; // x position of upper-left corner - public int Top; // y position of upper-left corner - public int Right; // x position of lower-right corner - public int Bottom; // y position of lower-right corner - } - - #endregion - - #region Private Members + #region Private private DeviceContext deviceContext; private SwapChain swapChain; private RenderTargetView renderView; @@ -62,19 +36,29 @@ namespace ANX.RenderSystem.Windows.DX11 private SharpDX.Direct3D11.Viewport currentViewport; private uint lastClearColor; private SharpDX.Color4 clearColor; - private bool vSyncEnabled; + #endregion - #endregion // Private Members + internal DeviceContext NativeDevice + { + get + { + return this.deviceContext; + } + } + + public bool VSync { get; set; } public GraphicsDeviceWindowsDX11(PresentationParameters presentationParameters) { - this.vSyncEnabled = true; + VSync = true; // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, - ModeDescription = new ModeDescription(presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight, new Rational(60, 1), FormatConverter.Translate(presentationParameters.BackBufferFormat)), + ModeDescription = new ModeDescription(presentationParameters.BackBufferWidth, + presentationParameters.BackBufferHeight, new Rational(60, 1), + BaseFormatConverter.Translate(presentationParameters.BackBufferFormat)), IsWindowed = true, OutputHandle = presentationParameters.DeviceWindowHandle, SampleDescription = new SampleDescription(1, 0), @@ -97,7 +81,7 @@ namespace ANX.RenderSystem.Windows.DX11 Factory factory = swapChain.GetParent(); factory.MakeWindowAssociation(presentationParameters.DeviceWindowHandle, WindowAssociationFlags.IgnoreAll); - ResizeRenderWindow(presentationParameters); + WindowHelper.ResizeRenderWindow(presentationParameters); // New RenderTargetView from the backbuffer backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain(swapChain, 0); @@ -108,7 +92,7 @@ namespace ANX.RenderSystem.Windows.DX11 // // create the depth stencil buffer // - Format depthFormat = FormatConverter.Translate(presentationParameters.DepthStencilFormat); + Format depthFormat = BaseFormatConverter.Translate(presentationParameters.DepthStencilFormat); if (depthFormat != Format.Unknown) { CreateDepthStencilBuffer(depthFormat); @@ -254,10 +238,9 @@ namespace ANX.RenderSystem.Windows.DX11 #region Present public void Present() { - swapChain.Present(this.vSyncEnabled ? 1 : 0, PresentFlags.None); + swapChain.Present(VSync ? 1 : 0, PresentFlags.None); } - - #endregion // Present + #endregion #region DrawPrimitives & DrawIndexedPrimitives public void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, @@ -271,7 +254,7 @@ namespace ANX.RenderSystem.Windows.DX11 SetupInputLayout(passSignature); // Prepare All the stages - deviceContext.InputAssembler.PrimitiveTopology = FormatConverter.Translate(primitiveType); + deviceContext.InputAssembler.PrimitiveTopology = BaseFormatConverter.Translate(primitiveType); deviceContext.Rasterizer.SetViewports(currentViewport); deviceContext.OutputMerger.SetTargets(this.depthStencilView, this.renderView); @@ -279,7 +262,7 @@ namespace ANX.RenderSystem.Windows.DX11 for (int i = 0; i < technique.Description.PassCount; ++i) { pass.Apply(deviceContext); - deviceContext.DrawIndexed(CalculateVertexCount(primitiveType, primitiveCount), startIndex, baseVertex); + deviceContext.DrawIndexed(BaseFormatConverter.CalculateVertexCount(primitiveType, primitiveCount), startIndex, baseVertex); } } @@ -291,7 +274,7 @@ namespace ANX.RenderSystem.Windows.DX11 SetupInputLayout(passSignature); // Prepare All the stages - deviceContext.InputAssembler.PrimitiveTopology = FormatConverter.Translate(primitiveType); + deviceContext.InputAssembler.PrimitiveTopology = BaseFormatConverter.Translate(primitiveType); deviceContext.Rasterizer.SetViewports(currentViewport); deviceContext.OutputMerger.SetTargets(this.depthStencilView, this.renderView); @@ -358,7 +341,7 @@ namespace ANX.RenderSystem.Windows.DX11 deviceContext.InputAssembler.InputLayout = layout; // Prepare All the stages - deviceContext.InputAssembler.PrimitiveTopology = FormatConverter.Translate(primitiveType); + deviceContext.InputAssembler.PrimitiveTopology = BaseFormatConverter.Translate(primitiveType); deviceContext.Rasterizer.SetViewports(currentViewport); //device.OutputMerger.SetTargets(this.depthStencilView, this.renderView); @@ -372,14 +355,6 @@ namespace ANX.RenderSystem.Windows.DX11 #endregion // DrawUserPrimitives - internal DeviceContext NativeDevice - { - get - { - return this.deviceContext; - } - } - private void SetupEffectForDraw(out SharpDX.Direct3D11.EffectPass pass, out SharpDX.Direct3D11.EffectTechnique technique, out ShaderBytecode passSignature) { // get the current effect @@ -404,30 +379,6 @@ namespace ANX.RenderSystem.Windows.DX11 deviceContext.InputAssembler.InputLayout = layout; } - private int CalculateVertexCount(PrimitiveType type, int primitiveCount) - { - if (type == PrimitiveType.TriangleList) - { - return primitiveCount * 3; - } - else if (type == PrimitiveType.LineList) - { - return primitiveCount * 2; - } - else if (type == PrimitiveType.LineStrip) - { - return primitiveCount + 1; - } - else if (type == PrimitiveType.TriangleStrip) - { - return primitiveCount + 2; - } - else - { - throw new NotImplementedException("couldn't calculate vertex count for PrimitiveType '" + type.ToString() + "'"); - } - } - public void SetIndexBuffer(IndexBuffer indexBuffer) { if (indexBuffer == null) @@ -441,7 +392,8 @@ namespace ANX.RenderSystem.Windows.DX11 if (nativeIndexBuffer != null) { - deviceContext.InputAssembler.SetIndexBuffer(nativeIndexBuffer.NativeBuffer, FormatConverter.Translate(indexBuffer.IndexElementSize), 0); + deviceContext.InputAssembler.SetIndexBuffer(nativeIndexBuffer.NativeBuffer, + BaseFormatConverter.Translate(indexBuffer.IndexElementSize), 0); } else { @@ -514,8 +466,8 @@ namespace ANX.RenderSystem.Windows.DX11 } private InputElement CreateInputElementFromVertexElement(VertexElement vertexElement) - { - string elementName = FormatConverter.Translate(vertexElement.VertexElementUsage); + { + string elementName = BaseFormatConverter.Translate(ref vertexElement); Format elementFormat; switch (vertexElement.VertexElementFormat) @@ -628,40 +580,14 @@ namespace ANX.RenderSystem.Windows.DX11 // // create the depth stencil buffer // - Format depthFormat = FormatConverter.Translate(presentationParameters.DepthStencilFormat); + Format depthFormat = BaseFormatConverter.Translate(presentationParameters.DepthStencilFormat); if (depthFormat != Format.Unknown) { CreateDepthStencilBuffer(depthFormat); } } - ResizeRenderWindow(presentationParameters); - } - - private void ResizeRenderWindow(PresentationParameters presentationParameters) - { - RECT windowRect; - RECT clientRect; - if (GetWindowRect(presentationParameters.DeviceWindowHandle, out windowRect) && - GetClientRect(presentationParameters.DeviceWindowHandle, out clientRect)) - { - int width = presentationParameters.BackBufferWidth + ((windowRect.Right - windowRect.Left) - clientRect.Right); - int height = presentationParameters.BackBufferHeight + ((windowRect.Bottom - windowRect.Top) - clientRect.Bottom); - - SetWindowPos(presentationParameters.DeviceWindowHandle, IntPtr.Zero, windowRect.Left, windowRect.Top, width, height, 0); - } - } - - public bool VSync - { - get - { - return this.vSyncEnabled; - } - set - { - this.vSyncEnabled = value; - } + WindowHelper.ResizeRenderWindow(presentationParameters); } public void Dispose() diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/IncludeHandler.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/Helpers/IncludeHandler.cs similarity index 62% rename from RenderSystems/ANX.RenderSystem.Windows.DX11/IncludeHandler.cs rename to RenderSystems/ANX.RenderSystem.Windows.DX11/Helpers/IncludeHandler.cs index 9973397d..7dba364b 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/IncludeHandler.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/Helpers/IncludeHandler.cs @@ -1,24 +1,18 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using SharpDX.D3DCompiler; using System.IO; -using ANX.Framework.NonXNA; -using ANX.Framework.Graphics; - -#endregion // Using Statements +using SharpDX.D3DCompiler; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license -namespace ANX.RenderSystem.Windows.DX11 +namespace ANX.RenderSystem.Windows.DX11.Helpers { internal class IncludeHandler : Include { - private string directory; + private string directory; + + public IDisposable Shadow { get; set; } public IncludeHandler(string directory) { @@ -34,13 +28,7 @@ namespace ANX.RenderSystem.Windows.DX11 { //Console.WriteLine("Including {0} file {1} from directory {2}", type.ToString(), fileName, directory); - return System.IO.File.OpenRead(System.IO.Path.Combine(directory, fileName)); - } - - public IDisposable Shadow - { - get; - set; + return File.OpenRead(Path.Combine(directory, fileName)); } public void Dispose() diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/IndexBuffer_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/IndexBuffer_DX11.cs index 22a083aa..f67ef04c 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/IndexBuffer_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/IndexBuffer_DX11.cs @@ -1,8 +1,9 @@ using System; -using System.IO; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using SharpDX.Direct3D11; +using SharpDX; +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -10,133 +11,63 @@ using SharpDX.Direct3D11; namespace ANX.RenderSystem.Windows.DX11 { - public class IndexBuffer_DX11 : INativeIndexBuffer, IDisposable + public class IndexBuffer_DX11 : BaseIndexBuffer, INativeIndexBuffer, IDisposable { - private IndexElementSize size; - - public SharpDX.Direct3D11.Buffer NativeBuffer { get; private set; } - #region Constructor public IndexBuffer_DX11(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) { - this.size = size; - - //TODO: translate and use usage - + elementSize = size; GraphicsDeviceWindowsDX11 gd11 = graphics.NativeDevice as GraphicsDeviceWindowsDX11; - SharpDX.Direct3D11.DeviceContext context = gd11 != null ? - gd11.NativeDevice as SharpDX.Direct3D11.DeviceContext : - null; + Dx11.DeviceContext context = gd11 != null ? gd11.NativeDevice as Dx11.DeviceContext : null; InitializeBuffer(context.Device, size, indexCount, usage); } internal IndexBuffer_DX11(SharpDX.Direct3D11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) { - this.size = size; + elementSize = size; InitializeBuffer(device, size, indexCount, usage); } #endregion #region InitializeBuffer - private void InitializeBuffer(SharpDX.Direct3D11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) + private void InitializeBuffer(Dx11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) { - BufferDescription description = new BufferDescription() + //TODO: translate and use usage + var description = new Dx11.BufferDescription() { // TODO: translate usage - Usage = ResourceUsage.Dynamic, - SizeInBytes = (size == IndexElementSize.SixteenBits ? 2 : 4) * indexCount, - BindFlags = BindFlags.IndexBuffer, - CpuAccessFlags = CpuAccessFlags.Write, - OptionFlags = ResourceOptionFlags.None + Usage = Dx11.ResourceUsage.Dynamic, + SizeInBytes = GetSizeInBytes(indexCount), + BindFlags = Dx11.BindFlags.IndexBuffer, + CpuAccessFlags = Dx11.CpuAccessFlags.Write, + OptionFlags = Dx11.ResourceOptionFlags.None }; NativeBuffer = new SharpDX.Direct3D11.Buffer(device, description); } #endregion - #region SetData - public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct + protected override DataStream MapBufferWrite() { - SetData(graphicsDevice, data, 0, data.Length); + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream); + return stream; } - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) - where T : struct + protected override DataStream MapBufferRead() { - GraphicsDeviceWindowsDX11 dx11GraphicsDevice = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - DeviceContext context = dx11GraphicsDevice.NativeDevice; - - //TODO: check offsetInBytes parameter for bounds etc. - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.WriteDiscard, MapFlags.None, out stream); - - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - if (startIndex > 0 || elementCount < data.Length) - for (int i = startIndex; i < startIndex + elementCount; i++) - stream.Write(data[i]); - else - for (int i = 0; i < data.Length; i++) - stream.Write(data[i]); + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(NativeBuffer, Dx11.MapMode.Read, Dx11.MapFlags.None, out stream); + return stream; + } + protected override void UnmapBuffer() + { + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; context.UnmapSubresource(NativeBuffer, 0); } - - public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct - { - SetData(graphicsDevice, 0, data, startIndex, elementCount); - } - #endregion - - #region Dispose - public void Dispose() - { - if (NativeBuffer != null) - { - NativeBuffer.Dispose(); - NativeBuffer = null; - } - } - #endregion - - #region GetData - public void GetData(T[] data) where T : struct - { - DeviceContext context = NativeBuffer.Device.ImmediateContext; - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.Read, MapFlags.None, out stream); - stream.ReadRange(data, 0, data.Length); - context.UnmapSubresource(NativeBuffer, 0); - } - - public void GetData(T[] data, int startIndex, int elementCount) where T : struct - { - DeviceContext context = NativeBuffer.Device.ImmediateContext; - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.Read, MapFlags.None, out stream); - stream.ReadRange(data, startIndex, elementCount); - context.UnmapSubresource(NativeBuffer, 0); - } - - public void GetData(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct - { - DeviceContext context = NativeBuffer.Device.ImmediateContext; - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.Read, MapFlags.None, out stream); - - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - stream.ReadRange(data, startIndex, elementCount); - context.UnmapSubresource(NativeBuffer, 0); - } - - #endregion } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/RasterizerState_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/RasterizerState_DX11.cs index 4848e85e..3a680a48 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/RasterizerState_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/RasterizerState_DX11.cs @@ -1,13 +1,8 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using SharpDX.Direct3D11; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; - -#endregion // Using Statements +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -15,155 +10,89 @@ using ANX.Framework.NonXNA; namespace ANX.RenderSystem.Windows.DX11 { - public class RasterizerState_DX11 : INativeRasterizerState - { - #region Private Members - private RasterizerStateDescription description; - private SharpDX.Direct3D11.RasterizerState nativeRasterizerState; - private bool nativeRasterizerStateDirty; - private bool bound; + public class RasterizerState_DX11 : BaseStateObject, INativeRasterizerState + { + private Dx11.RasterizerStateDescription description; - private const int intMaxOver16 = int.MaxValue / 16; + #region Public + public CullMode CullMode + { + set + { + SharpDX.Direct3D11.CullMode cullMode = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.CullMode, ref cullMode); + } + } - #endregion // Private Members + public float DepthBias + { + set + { + // XNA uses a float value in the range of 0f..16f as value + // DirectX 10 uses a INT value - public RasterizerState_DX11() - { - this.description = new RasterizerStateDescription(); + int depthBiasValue = (int)(value * IntMaxOver16); + SetValueIfDifferentAndMarkDirty(ref description.DepthBias, ref depthBiasValue); + } + } - this.description.IsAntialiasedLineEnabled = false; + public FillMode FillMode + { + set + { + SharpDX.Direct3D11.FillMode fillMode = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.FillMode, ref fillMode); + } + } - this.nativeRasterizerStateDirty = true; - } + public bool MultiSampleAntiAlias + { + set + { + if (description.IsMultisampleEnabled != value) + { + isDirty = true; + description.IsMultisampleEnabled = value; + } + } + } - public void Apply(ANX.Framework.Graphics.GraphicsDevice graphicsDevice) - { - GraphicsDeviceWindowsDX11 gdx11 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - DeviceContext context = gdx11.NativeDevice; + public bool ScissorTestEnable + { + set + { + if (description.IsScissorEnabled != value) + { + isDirty = true; + description.IsScissorEnabled = value; + } + } + } - UpdateNativeRasterizerState(context.Device); - this.bound = true; + public float SlopeScaleDepthBias + { + set + { + SetValueIfDifferentAndMarkDirty(ref description.SlopeScaledDepthBias, ref value); + } + } + #endregion - context.Rasterizer.State = this.nativeRasterizerState; - } + protected override void Init() + { + description.IsAntialiasedLineEnabled = false; + } - public void Release() - { - this.bound = false; - } + protected override Dx11.RasterizerState CreateNativeState(GraphicsDevice graphics) + { + Dx11.DeviceContext context = (graphics.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + return new Dx11.RasterizerState(context.Device, ref description); + } - public void Dispose() - { - if (this.nativeRasterizerState != null) - { - this.nativeRasterizerState.Dispose(); - this.nativeRasterizerState = null; - } - } - - public bool IsBound - { - get - { - return this.bound; - } - } - - public ANX.Framework.Graphics.CullMode CullMode - { - set - { - SharpDX.Direct3D11.CullMode cullMode = FormatConverter.Translate(value); - - if (description.CullMode != cullMode) - { - nativeRasterizerStateDirty = true; - description.CullMode = cullMode; - } - } - } - - public float DepthBias - { - set - { - // XNA uses a float value in the range of 0f..16f as value - // DirectX 10 uses a INT value - - int depthBiasValue = (int)(value * intMaxOver16); - - if (description.DepthBias != depthBiasValue) - { - nativeRasterizerStateDirty = true; - description.DepthBias = depthBiasValue; - } - } - } - - public ANX.Framework.Graphics.FillMode FillMode - { - set - { - SharpDX.Direct3D11.FillMode fillMode = FormatConverter.Translate(value); - - if (description.FillMode != fillMode) - { - nativeRasterizerStateDirty = true; - description.FillMode = fillMode; - } - } - } - - public bool MultiSampleAntiAlias - { - set - { - if (description.IsMultisampleEnabled != value) - { - nativeRasterizerStateDirty = true; - description.IsMultisampleEnabled = value; - } - } - } - - public bool ScissorTestEnable - { - set - { - if (description.IsScissorEnabled != value) - { - nativeRasterizerStateDirty = true; - description.IsScissorEnabled = value; - } - } - } - - public float SlopeScaleDepthBias - { - set - { - if (description.SlopeScaledDepthBias != value) - { - nativeRasterizerStateDirty = true; - description.SlopeScaledDepthBias = value; - } - } - } - - private void UpdateNativeRasterizerState(Device device) - { - if (this.nativeRasterizerStateDirty == true || this.nativeRasterizerState == null) - { - if (this.nativeRasterizerState != null) - { - this.nativeRasterizerState.Dispose(); - this.nativeRasterizerState = null; - } - - this.nativeRasterizerState = new SharpDX.Direct3D11.RasterizerState(device, ref this.description); - - this.nativeRasterizerStateDirty = false; - } - } + protected override void ApplyNativeState(GraphicsDevice graphics) + { + Dx11.DeviceContext context = (graphics.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + context.Rasterizer.State = nativeState; + } } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/RenderTarget2D_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/RenderTarget2D_DX11.cs index fd183d24..0fa3e3bf 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/RenderTarget2D_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/RenderTarget2D_DX11.cs @@ -1,13 +1,7 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using SharpDX.Direct3D11; - -#endregion // Using Statements // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -15,46 +9,32 @@ using SharpDX.Direct3D11; namespace ANX.RenderSystem.Windows.DX11 { - public class RenderTarget2D_DX11 : Texture2D_DX11, INativeRenderTarget2D, INativeTexture2D - { - #region Private Members + public class RenderTarget2D_DX11 : Texture2D_DX11, INativeRenderTarget2D, INativeTexture2D + { + public RenderTarget2D_DX11(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat surfaceFormat, + DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) + : base(graphics, surfaceFormat) + { + if (mipMap) + throw new NotImplementedException("creating RenderTargets with mip map not yet implemented"); - #endregion // Private Members + var description = new SharpDX.Direct3D11.Texture2DDescription() + { + Width = width, + Height = height, + MipLevels = 1, + ArraySize = 1, + Format = BaseFormatConverter.Translate(surfaceFormat), + SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), + Usage = SharpDX.Direct3D11.ResourceUsage.Default, + BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, + CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, + OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, + }; - public RenderTarget2D_DX11(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage) - : base(graphics) - { - if (mipMap) - { - throw new NotImplementedException("creating RenderTargets with mip map not yet implemented"); - } - - this.surfaceFormat = surfaceFormat; - - GraphicsDeviceWindowsDX11 graphicsDX11 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - SharpDX.Direct3D11.DeviceContext device = graphicsDX11.NativeDevice; - - SharpDX.Direct3D11.Texture2DDescription description = new SharpDX.Direct3D11.Texture2DDescription() - { - Width = width, - Height = height, - MipLevels = 1, - ArraySize = 1, - Format = FormatConverter.Translate(preferredFormat), - SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), - Usage = SharpDX.Direct3D11.ResourceUsage.Default, - BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, - CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, - OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, - }; - this.nativeTexture = new SharpDX.Direct3D11.Texture2D(graphicsDX11.NativeDevice.Device, description); - this.nativeShaderResourceView = new SharpDX.Direct3D11.ShaderResourceView(graphicsDX11.NativeDevice.Device, this.nativeTexture); - - // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx - // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx - - this.formatSize = FormatConverter.FormatSize(surfaceFormat); - } - - } + SharpDX.Direct3D11.DeviceContext device = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + NativeTexture = new SharpDX.Direct3D11.Texture2D(device.Device, description); + NativeShaderResourceView = new SharpDX.Direct3D11.ShaderResourceView(device.Device, NativeTexture); + } + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/SamplerState_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/SamplerState_DX11.cs index bfda985b..8f078722 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/SamplerState_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/SamplerState_DX11.cs @@ -1,10 +1,8 @@ -#region Using Statements -using System; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA; -using SharpDX.Direct3D11; - -#endregion // Using Statements +using Dx11 = SharpDX.Direct3D11; +using System; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -12,162 +10,107 @@ using SharpDX.Direct3D11; namespace ANX.RenderSystem.Windows.DX11 { - public class SamplerState_DX11 : INativeSamplerState + public class SamplerState_DX11 : BaseStateObject, INativeSamplerState { - #region Private Members - private SamplerStateDescription description; - private SharpDX.Direct3D11.SamplerState nativeSamplerState; - private bool nativeSamplerStateDirty; - private bool bound; + private Dx11.SamplerStateDescription description; - #endregion // Private Members + #region Public + public TextureAddressMode AddressU + { + set + { + Dx11.TextureAddressMode mode = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.AddressU, ref mode); + } + } - public SamplerState_DX11() + public TextureAddressMode AddressV + { + set + { + Dx11.TextureAddressMode mode = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.AddressV, ref mode); + } + } + + public TextureAddressMode AddressW + { + set + { + Dx11.TextureAddressMode mode = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.AddressW, ref mode); + } + } + + public TextureFilter Filter + { + set + { + Dx11.Filter filter = FormatConverter.Translate(value); + SetValueIfDifferentAndMarkDirty(ref description.Filter, ref filter); + } + } + + public int MaxAnisotropy + { + set + { + SetValueIfDifferentAndMarkDirty(ref description.MaximumAnisotropy, ref value); + } + } + + public int MaxMipLevel + { + set + { + if (description.MaximumLod != value) + { + description.MaximumLod = value; + isDirty = true; + } + } + } + + public float MipMapLevelOfDetailBias + { + set + { + SetValueIfDifferentAndMarkDirty(ref description.MipLodBias, ref value); + } + } + #endregion + + #region Apply + public void Apply(GraphicsDevice graphicsDevice, int index) { - this.description = new SamplerStateDescription(); - - this.nativeSamplerStateDirty = true; - } - - public void Apply(GraphicsDevice graphicsDevice, int index) - { - GraphicsDeviceWindowsDX11 gdx11 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - DeviceContext context = gdx11.NativeDevice; + Dx11.DeviceContext context = (graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; UpdateNativeSamplerState(context.Device); - this.bound = true; + IsBound = true; - context.PixelShader.SetSampler(index, this.nativeSamplerState); - } + context.PixelShader.SetSampler(index, nativeState); + } + #endregion - public void Release() + #region UpdateNativeSamplerState + private void UpdateNativeSamplerState(Dx11.Device device) { - this.bound = false; - } - - public bool IsBound - { - get - { - return this.bound; - } - } - - public ANX.Framework.Graphics.TextureAddressMode AddressU - { - set + if (isDirty || nativeState == null) { - SharpDX.Direct3D11.TextureAddressMode mode = FormatConverter.Translate(value); - - if (description.AddressU != mode) - { - description.AddressU = mode; - nativeSamplerStateDirty = true; - } + Dispose(); + nativeState = new Dx11.SamplerState(device, ref description); + isDirty = false; } } + #endregion - public ANX.Framework.Graphics.TextureAddressMode AddressV - { - set - { - SharpDX.Direct3D11.TextureAddressMode mode = FormatConverter.Translate(value); + protected override Dx11.SamplerState CreateNativeState(GraphicsDevice graphics) + { + return null; + } - if (description.AddressV != mode) - { - description.AddressV = mode; - nativeSamplerStateDirty = true; - } - } - } - - public ANX.Framework.Graphics.TextureAddressMode AddressW - { - set - { - SharpDX.Direct3D11.TextureAddressMode mode = FormatConverter.Translate(value); - - if (description.AddressW != mode) - { - description.AddressW = mode; - nativeSamplerStateDirty = true; - } - } - } - - public TextureFilter Filter - { - set - { - SharpDX.Direct3D11.Filter filter = FormatConverter.Translate(value); - - if (description.Filter != filter) - { - description.Filter = filter; - nativeSamplerStateDirty = true; - } - } - } - - public int MaxAnisotropy - { - set - { - if (description.MaximumAnisotropy != value) - { - description.MaximumAnisotropy = value; - nativeSamplerStateDirty = true; - } - } - } - - public int MaxMipLevel - { - set - { - if (description.MaximumLod != value) - { - description.MaximumLod = value; - nativeSamplerStateDirty = true; - } - } - } - - public float MipMapLevelOfDetailBias - { - set - { - if (description.MipLodBias != value) - { - description.MipLodBias = value; - nativeSamplerStateDirty = true; - } - } - } - - public void Dispose() - { - if (this.nativeSamplerState != null) - { - this.nativeSamplerState.Dispose(); - this.nativeSamplerState = null; - } - } - - private void UpdateNativeSamplerState(Device device) - { - if (this.nativeSamplerStateDirty == true || this.nativeSamplerState == null) - { - if (this.nativeSamplerState != null) - { - this.nativeSamplerState.Dispose(); - this.nativeSamplerState = null; - } - - this.nativeSamplerState = new SharpDX.Direct3D11.SamplerState(device, ref this.description); - - this.nativeSamplerStateDirty = false; - } - } + protected override void ApplyNativeState(GraphicsDevice graphics) + { + } } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/Texture2D_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/Texture2D_DX11.cs index 6d3fc9ba..e400b9aa 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/Texture2D_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/Texture2D_DX11.cs @@ -1,15 +1,10 @@ -#region Using Statements using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using ANX.Framework.Graphics; -using SharpDX.Direct3D11; -using ANX.Framework.NonXNA.RenderSystem; using System.IO; -using System.Runtime.InteropServices; - -#endregion // Using Statements +using ANX.BaseDirectX; +using ANX.Framework.Graphics; +using ANX.Framework.NonXNA.RenderSystem; +using SharpDX; +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -17,301 +12,139 @@ using System.Runtime.InteropServices; namespace ANX.RenderSystem.Windows.DX11 { - public class Texture2D_DX11 : INativeTexture2D - { - #region Private Members - protected internal SharpDX.Direct3D11.Texture2D nativeTexture; - protected internal SharpDX.Direct3D11.ShaderResourceView nativeShaderResourceView; - protected internal int formatSize; - protected internal SurfaceFormat surfaceFormat; - protected internal GraphicsDevice graphicsDevice; - - #endregion // Private Members - - internal Texture2D_DX11(GraphicsDevice graphicsDevice) - { - this.graphicsDevice = graphicsDevice; - } - - public Texture2D_DX11(GraphicsDevice graphicsDevice, int width, int height, SurfaceFormat surfaceFormat, int mipCount) - { - if (mipCount > 1) - { - throw new Exception("creating textures with mip map not yet implemented"); - } - - this.graphicsDevice = graphicsDevice; - this.surfaceFormat = surfaceFormat; - - GraphicsDeviceWindowsDX11 graphicsDX11 = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - SharpDX.Direct3D11.DeviceContext context = graphicsDX11.NativeDevice; - - SharpDX.Direct3D11.Texture2DDescription description = new SharpDX.Direct3D11.Texture2DDescription() - { - Width = width, - Height = height, - MipLevels = mipCount, - ArraySize = mipCount, - Format = FormatConverter.Translate(surfaceFormat), - SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), - Usage = SharpDX.Direct3D11.ResourceUsage.Dynamic, - BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource, - CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Write, - OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, - }; - this.nativeTexture = new SharpDX.Direct3D11.Texture2D(context.Device, description); - this.nativeShaderResourceView = new SharpDX.Direct3D11.ShaderResourceView(context.Device, this.nativeTexture); - - // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx - // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx - - this.formatSize = FormatConverter.FormatSize(surfaceFormat); - } - - public override int GetHashCode() - { - return NativeTexture.NativePointer.ToInt32(); - } - - internal SharpDX.Direct3D11.Texture2D NativeTexture - { - get - { - return this.nativeTexture; - } - set - { - if (this.nativeTexture != value) - { - if (this.nativeTexture != null) - { - this.nativeTexture.Dispose(); - } - - this.nativeTexture = value; - } - } - } - - internal SharpDX.Direct3D11.ShaderResourceView NativeShaderResourceView - { - get - { - return this.nativeShaderResourceView; - } - set - { - if (this.nativeShaderResourceView != value) - { - if (this.nativeShaderResourceView != null) - { - this.nativeShaderResourceView.Dispose(); - } - - this.nativeShaderResourceView = value; - } - } - } - - public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct - { - SetData(graphicsDevice, 0, data, 0, data.Length); - } - - public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct - { - SetData(graphicsDevice, 0, data, startIndex, elementCount); - } - - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct - { - //TODO: handle offsetInBytes parameter - //TODO: handle startIndex parameter - //TODO: handle elementCount parameter - - GraphicsDeviceWindowsDX11 dx11GraphicsDevice = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - DeviceContext context = dx11GraphicsDevice.NativeDevice; - - if (this.surfaceFormat == SurfaceFormat.Color) - { - int subresource = SharpDX.Direct3D11.Texture2D.CalculateSubResourceIndex(0, 0, 1); - SharpDX.DataBox rectangle = context.MapSubresource(this.nativeTexture, subresource, MapMode.WriteDiscard, MapFlags.None); - int rowPitch = rectangle.RowPitch; - - unsafe - { - GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); - byte* colorData = (byte*)handle.AddrOfPinnedObject(); - - byte* pTexels = (byte*)rectangle.DataPointer; - int srcIndex = 0; - - for (int row = 0; row < Height; row++) - { - int rowStart = row * rowPitch; - - for (int col = 0; col < Width; col++) - { - int colStart = col * formatSize; - pTexels[rowStart + colStart + 0] = colorData[srcIndex++]; - pTexels[rowStart + colStart + 1] = colorData[srcIndex++]; - pTexels[rowStart + colStart + 2] = colorData[srcIndex++]; - pTexels[rowStart + colStart + 3] = colorData[srcIndex++]; - } - } - - handle.Free(); - } - - context.UnmapSubresource(this.nativeTexture, subresource); - } - else if (surfaceFormat == SurfaceFormat.Dxt5 || surfaceFormat == SurfaceFormat.Dxt3 || surfaceFormat == SurfaceFormat.Dxt1) - { - unsafe - { - GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); - byte* colorData = (byte*)handle.AddrOfPinnedObject(); - - int w = (Width + 3) >> 2; - int h = (Height + 3) >> 2; - formatSize = (surfaceFormat == SurfaceFormat.Dxt1) ? 8 : 16; - - int subresource = SharpDX.Direct3D11.Texture2D.CalculateSubResourceIndex(0, 0, 1); - SharpDX.DataBox rectangle = context.MapSubresource(this.nativeTexture, subresource, MapMode.WriteDiscard, MapFlags.None); - SharpDX.DataStream ds = new SharpDX.DataStream(rectangle.DataPointer, Width * Height * 4 * 2, true, true); - int pitch = rectangle.RowPitch; - int col = 0; - int index = 0; // startIndex - int count = data.Length; // elementCount - int actWidth = w * formatSize; - - for (int i = 0; i < h; i++) - { - ds.Position = (i * pitch) + (col * formatSize); - if (count <= 0) - { - break; - } - else if (count < actWidth) - { - for (int idx = index; idx < index + count; idx++) - { - ds.WriteByte(colorData[idx]); - } - break; - } - - for (int idx = index; idx < index + actWidth; idx++) - { - ds.WriteByte(colorData[idx]); - } - - index += actWidth; - count -= actWidth; - } - - handle.Free(); - - context.UnmapSubresource(this.nativeTexture, subresource); - } - } - else - { - throw new Exception(string.Format("creating textures of format {0} not yet implemented...", surfaceFormat.ToString())); - } - } - - public int Width - { - get - { - if (this.nativeTexture != null) - { - return this.nativeTexture.Description.Width; - } - - return 0; - } - } - - public int Height - { - get - { - if (this.nativeTexture != null) - { - return this.nativeTexture.Description.Height; - } - - return 0; - } - } - - public GraphicsDevice GraphicsDevice - { - get - { - return this.graphicsDevice; - } - } - - public void Dispose() - { - if (this.nativeShaderResourceView != null) - { - this.nativeShaderResourceView.Dispose(); - this.nativeShaderResourceView = null; - } - - if (this.nativeTexture != null) - { - this.nativeTexture.Dispose(); - this.nativeTexture = null; - } - } - - #region SaveAsJpeg (TODO) - public void SaveAsJpeg(Stream stream, int width, int height) - { - throw new NotImplementedException(); - } - #endregion - - #region SaveAsPng (TODO) - public void SaveAsPng(Stream stream, int width, int height) - { - throw new NotImplementedException(); - } - #endregion - - #region INativeTexture2D Member - - - public void GetData(int level, Framework.Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct - { - throw new NotImplementedException(); - } - - public void SetData(int level, Framework.Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct - { - throw new NotImplementedException(); - } - - #endregion - - #region INativeBuffer Member - - - public void GetData(T[] data) where T : struct - { - throw new NotImplementedException(); - } - - public void GetData(T[] data, int startIndex, int elementCount) where T : struct - { - throw new NotImplementedException(); - } - - #endregion + public class Texture2D_DX11 : BaseTexture2D, INativeTexture2D + { + #region Public + public override int Width + { + get + { + return NativeTexture != null ? NativeTexture.Description.Width : 0; + } } + + public override int Height + { + get + { + return NativeTexture != null ? NativeTexture.Description.Height : 0; + } + } + + protected internal Dx11.ShaderResourceView NativeShaderResourceView { get; protected set; } + #endregion + + #region Constructor + internal Texture2D_DX11(GraphicsDevice graphicsDevice, SurfaceFormat surfaceFormat) + : base(graphicsDevice, surfaceFormat) + { + } + + public Texture2D_DX11(GraphicsDevice graphicsDevice, int width, int height, SurfaceFormat surfaceFormat, int mipCount) + : base(graphicsDevice, surfaceFormat) + { + if (mipCount > 1) + throw new Exception("creating textures with mip map not yet implemented"); + + var description = new Dx11.Texture2DDescription() + { + Width = width, + Height = height, + MipLevels = mipCount, + ArraySize = mipCount, + Format = BaseFormatConverter.Translate(surfaceFormat), + SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), + Usage = Dx11.ResourceUsage.Dynamic, + BindFlags = Dx11.BindFlags.ShaderResource, + CpuAccessFlags = Dx11.CpuAccessFlags.Write, + OptionFlags = Dx11.ResourceOptionFlags.None, + }; + + Dx11.DeviceContext context = (graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + NativeTexture = new Dx11.Texture2D(context.Device, description); + NativeShaderResourceView = new Dx11.ShaderResourceView(context.Device, NativeTexture); + } + #endregion + + #region GetHashCode + public override int GetHashCode() + { + return NativeTexture.NativePointer.ToInt32(); + } + #endregion + + #region Dispose + public override void Dispose() + { + if (NativeShaderResourceView != null) + { + NativeShaderResourceView.Dispose(); + NativeShaderResourceView = null; + } + + base.Dispose(); + } + #endregion + + #region SaveAsJpeg (TODO) + public void SaveAsJpeg(Stream stream, int width, int height) + { + throw new NotImplementedException(); + } + #endregion + + #region SaveAsPng (TODO) + public void SaveAsPng(Stream stream, int width, int height) + { + throw new NotImplementedException(); + } + #endregion + + #region GetData (TODO) + public void GetData(T[] data) where T : struct + { + GetData(data, 0, data.Length); + } + + public void GetData(T[] data, int startIndex, int elementCount) where T : struct + { + throw new NotImplementedException(); + } + + public void GetData(int level, Framework.Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct + { + throw new NotImplementedException(); + } + #endregion + + #region MapWrite + protected override IntPtr MapWrite() + { + Dx11.DeviceContext context = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + tempSubresource = Dx11.Texture2D.CalculateSubResourceIndex(0, 0, 1); + DataBox box = context.MapSubresource(NativeTexture, tempSubresource, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None); + pitch = box.RowPitch; + return box.DataPointer; + } + #endregion + + #region MapRead + protected override IntPtr MapRead() + { + Dx11.DeviceContext context = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + tempSubresource = Dx11.Texture2D.CalculateSubResourceIndex(0, 0, 1); + DataBox box = context.MapSubresource(NativeTexture, tempSubresource, Dx11.MapMode.Read, Dx11.MapFlags.None); + pitch = box.RowPitch; + return box.DataPointer; + } + #endregion + + #region Unmap + protected override void Unmap() + { + Dx11.DeviceContext context = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice; + context.UnmapSubresource(NativeTexture, tempSubresource); + } + #endregion + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/VertexBuffer_DX11.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/VertexBuffer_DX11.cs index f73d7298..652facdc 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/VertexBuffer_DX11.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/VertexBuffer_DX11.cs @@ -1,8 +1,9 @@ using System; -using System.IO; +using ANX.BaseDirectX; using ANX.Framework.Graphics; using ANX.Framework.NonXNA.RenderSystem; -using SharpDX.Direct3D11; +using SharpDX; +using Dx11 = SharpDX.Direct3D11; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. @@ -10,141 +11,66 @@ using SharpDX.Direct3D11; namespace ANX.RenderSystem.Windows.DX11 { - public class VertexBuffer_DX11 : INativeVertexBuffer, IDisposable + public class VertexBuffer_DX11 : BaseVertexBuffer, INativeVertexBuffer, IDisposable { - int vertexStride; - - public SharpDX.Direct3D11.Buffer NativeBuffer { get; private set; } - #region Constructor public VertexBuffer_DX11(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { var gd11 = graphics.NativeDevice as GraphicsDeviceWindowsDX11; - SharpDX.Direct3D11.DeviceContext context = gd11 != null ? - gd11.NativeDevice as SharpDX.Direct3D11.DeviceContext : - null; + Dx11.DeviceContext context = gd11 != null ? gd11.NativeDevice as Dx11.DeviceContext : null; InitializeBuffer(context.Device, vertexDeclaration, vertexCount, usage); } - internal VertexBuffer_DX11(SharpDX.Direct3D11.Device device, VertexDeclaration vertexDeclaration, int vertexCount, - BufferUsage usage) + internal VertexBuffer_DX11(Dx11.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { InitializeBuffer(device, vertexDeclaration, vertexCount, usage); } #endregion #region InitializeBuffer (TODO) - private void InitializeBuffer(SharpDX.Direct3D11.Device device, VertexDeclaration vertexDeclaration, int vertexCount, - BufferUsage usage) + private void InitializeBuffer(Dx11.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) { - this.vertexStride = vertexDeclaration.VertexStride; + vertexStride = vertexDeclaration.VertexStride; //TODO: translate and use usage if (device != null) { - BufferDescription description = new BufferDescription() + var description = new Dx11.BufferDescription() { - Usage = ResourceUsage.Dynamic, + Usage = Dx11.ResourceUsage.Dynamic, SizeInBytes = vertexDeclaration.VertexStride * vertexCount, - BindFlags = BindFlags.VertexBuffer, - CpuAccessFlags = CpuAccessFlags.Write, - OptionFlags = ResourceOptionFlags.None + BindFlags = Dx11.BindFlags.VertexBuffer, + CpuAccessFlags = Dx11.CpuAccessFlags.Write, + OptionFlags = Dx11.ResourceOptionFlags.None }; - NativeBuffer = new SharpDX.Direct3D11.Buffer(device, description); + NativeBuffer = new Dx11.Buffer(device, description); } } #endregion - #region SetData (TODO) - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount) - where T : struct + protected override SharpDX.DataStream MapBufferWrite() { - GraphicsDeviceWindowsDX11 dx11GraphicsDevice = graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11; - DeviceContext context = dx11GraphicsDevice.NativeDevice; + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream); + return stream; + } - //TODO: check offsetInBytes parameter for bounds etc. - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.WriteDiscard, MapFlags.None, out stream); - - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - if (startIndex > 0 || elementCount < data.Length) - for (int i = startIndex; i < startIndex + elementCount; i++) - stream.Write(data[i]); - else - for (int i = 0; i < data.Length; i++) - stream.Write(data[i]); + protected override SharpDX.DataStream MapBufferRead() + { + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(NativeBuffer, Dx11.MapMode.Read, Dx11.MapFlags.None, out stream); + return stream; + } + protected override void UnmapBuffer() + { + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; context.UnmapSubresource(NativeBuffer, 0); } - - public void SetData(GraphicsDevice graphicsDevice, T[] data) where T : struct - { - SetData(graphicsDevice, data, 0, data.Length); - } - - public void SetData(GraphicsDevice graphicsDevice, T[] data, int startIndex, int elementCount) where T : struct - { - SetData(graphicsDevice, 0, data, startIndex, elementCount); - } - - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data, int startIndex, int elementCount, - int vertexStride) where T : struct - { - throw new NotImplementedException(); - } - #endregion - - #region Dispose - public void Dispose() - { - if (NativeBuffer != null) - { - NativeBuffer.Dispose(); - NativeBuffer = null; - } - } - #endregion - - #region GetData - public void GetData(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct - { - DeviceContext context = NativeBuffer.Device.ImmediateContext; - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.Read, MapFlags.None, out stream); - - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); - - stream.ReadRange(data, startIndex, elementCount); - context.UnmapSubresource(NativeBuffer, 0); - } - - public void GetData(T[] data) where T : struct - { - DeviceContext context = NativeBuffer.Device.ImmediateContext; - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.Read, MapFlags.None, out stream); - stream.ReadRange(data, 0, data.Length); - context.UnmapSubresource(NativeBuffer, 0); - } - - public void GetData(T[] data, int startIndex, int elementCount) where T : struct - { - DeviceContext context = NativeBuffer.Device.ImmediateContext; - - SharpDX.DataStream stream; - context.MapSubresource(NativeBuffer, MapMode.Read, MapFlags.None, out stream); - stream.ReadRange(data, startIndex, elementCount); - context.UnmapSubresource(NativeBuffer, 0); - } - #endregion } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.Metro/SamplerState_Metro.cs b/RenderSystems/ANX.RenderSystem.Windows.Metro/SamplerState_Metro.cs index b9f5008a..6caaa4d2 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.Metro/SamplerState_Metro.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.Metro/SamplerState_Metro.cs @@ -86,7 +86,6 @@ namespace ANX.RenderSystem.Windows.Metro public SamplerState_Metro() : base() { - description = new Dx11.SamplerStateDescription(); } #endregion @@ -96,8 +95,7 @@ namespace ANX.RenderSystem.Windows.Metro UpdateNativeSamplerState(); bound = true; - NativeDxDevice.Current.NativeContext.PixelShader.SetSampler( - index, this.nativeSamplerState); + NativeDxDevice.Current.NativeContext.PixelShader.SetSampler(index, this.nativeSamplerState); } #endregion diff --git a/Samples/DualTextureSample/Game1.cs b/Samples/DualTextureSample/Game1.cs index b0d10445..6420979f 100644 --- a/Samples/DualTextureSample/Game1.cs +++ b/Samples/DualTextureSample/Game1.cs @@ -125,7 +125,7 @@ namespace DualTextureSample dualTextureEffect.FogEnabled = (mode == DualTextureMode.VertexColorFog || mode == DualTextureMode.Fog); dualTextureEffect.FogColor = Color.Red.ToVector3(); dualTextureEffect.FogStart = 0f; - dualTextureEffect.FogEnd = 50f; + dualTextureEffect.FogEnd = 25f; dualTextureEffect.Alpha = 1f; dualTextureEffect.Texture = texture1;