From 96d6ad095e49fed91808a27818f1970c930604b8 Mon Sep 17 00:00:00 2001 From: "SND\\AstrorEnales_cp" Date: Fri, 17 Aug 2012 14:25:59 +0000 Subject: [PATCH] - Fixed a small issue in variable parsing with Textures - Removed the sharpdx references from the Metro shader generator - Added a basic path searching to the metro shader generator to find the correct path to the fxc tool --- .../Graphics/GeometryContentCollection.cs | 41 ++++---- .../Graphics/IndexCollection.cs | 35 +++---- ANX.Framework/ANX.Framework.csproj | 24 ++--- ANX.Framework/ANX.Framework_Linux.csproj | 24 ++--- ANX.Framework/ANX.Framework_PSVita.csproj | 26 ++--- .../ANX.Framework_WindowsMetro.csproj | 30 +++--- .../DX11MetroShaderGenerator.csproj | 8 -- .../IncludeHandler.cs | 41 -------- .../MetroCodeGenerator.cs | 95 +++++++++++++++---- Tools/HLSLParser/HLSLParser/Variable.cs | 13 +++ .../HLSLParserTests/VariableTests.cs | 4 +- 11 files changed, 180 insertions(+), 161 deletions(-) delete mode 100644 Tools/DX11MetroShaderGenerator/IncludeHandler.cs diff --git a/ANX.Framework.Content.Pipeline/Graphics/GeometryContentCollection.cs b/ANX.Framework.Content.Pipeline/Graphics/GeometryContentCollection.cs index be624ea8..0453d5dd 100644 --- a/ANX.Framework.Content.Pipeline/Graphics/GeometryContentCollection.cs +++ b/ANX.Framework.Content.Pipeline/Graphics/GeometryContentCollection.cs @@ -1,10 +1,4 @@ -#region Using Statements -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -#endregion +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,22 +6,23 @@ using System.Text; namespace ANX.Framework.Content.Pipeline.Graphics { - public sealed class GeometryContentCollection : ChildCollection - { - protected GeometryContentCollection(MeshContent parent) - : base(parent) - { - throw new NotImplementedException(); - } + public sealed class GeometryContentCollection + : ChildCollection + { + protected GeometryContentCollection(MeshContent parent) + : base(parent) + { + throw new NotImplementedException(); + } - protected override MeshContent GetParent(GeometryContent child) - { - throw new NotImplementedException(); - } + protected override MeshContent GetParent(GeometryContent child) + { + return child.Parent; + } - protected override void SetParent(GeometryContent child, MeshContent parent) - { - throw new NotImplementedException(); - } - } + protected override void SetParent(GeometryContent child, MeshContent parent) + { + child.Parent = parent; + } + } } diff --git a/ANX.Framework.Content.Pipeline/Graphics/IndexCollection.cs b/ANX.Framework.Content.Pipeline/Graphics/IndexCollection.cs index c45a2c76..79084f4b 100644 --- a/ANX.Framework.Content.Pipeline/Graphics/IndexCollection.cs +++ b/ANX.Framework.Content.Pipeline/Graphics/IndexCollection.cs @@ -1,27 +1,30 @@ -#region Using Statements -using System; +using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Collections.ObjectModel; -#endregion - // 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.Framework.Content.Pipeline.Graphics { - public sealed class IndexCollection : Collection - { - public IndexCollection() - { - } + public sealed class IndexCollection : Collection + { + public IndexCollection() + { + } - public void AddRange(IEnumerable indices) - { - throw new NotImplementedException(); - } - } + public void AddRange(IEnumerable indices) + { + if (indices == null) + { + throw new ArgumentNullException("indices"); + } + + foreach (int index in indices) + { + Items.Add(index); + } + } + } } diff --git a/ANX.Framework/ANX.Framework.csproj b/ANX.Framework/ANX.Framework.csproj index a0b9e44e..7656bdb5 100644 --- a/ANX.Framework/ANX.Framework.csproj +++ b/ANX.Framework/ANX.Framework.csproj @@ -1,5 +1,17 @@  + + Debug + AnyCPU + Properties + Library + 512 + 8.0.30703 + 2.0 + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + ANX.Framework + true full @@ -19,18 +31,6 @@ 4 true - - Debug - AnyCPU - Properties - Library - 512 - 8.0.30703 - 2.0 - {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} - ANX.Framework - ANX.Framework - diff --git a/ANX.Framework/ANX.Framework_Linux.csproj b/ANX.Framework/ANX.Framework_Linux.csproj index 5bd3e645..1dda9aea 100644 --- a/ANX.Framework/ANX.Framework_Linux.csproj +++ b/ANX.Framework/ANX.Framework_Linux.csproj @@ -1,5 +1,17 @@  + + Debug + AnyCPU + Properties + Library + 512 + 8.0.30703 + 2.0 + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + ANX.Framework + true full @@ -19,18 +31,6 @@ 4 true - - Debug - AnyCPU - Properties - Library - 512 - 8.0.30703 - 2.0 - {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} - ANX.Framework - ANX.Framework - diff --git a/ANX.Framework/ANX.Framework_PSVita.csproj b/ANX.Framework/ANX.Framework_PSVita.csproj index 9f8d0a4c..7d3d85ff 100644 --- a/ANX.Framework/ANX.Framework_PSVita.csproj +++ b/ANX.Framework/ANX.Framework_PSVita.csproj @@ -1,5 +1,18 @@  + + Debug + AnyCPU + Properties + Library + 512 + 10.0.0 + 2.0 + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + ANX.Framework + {69878862-DA7D-4DC6-B0A1-50D8FAB4242F};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + true full @@ -19,19 +32,6 @@ 4 true - - Debug - AnyCPU - Properties - Library - 512 - 10.0.0 - 2.0 - {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} - ANX.Framework - ANX.Framework - {69878862-DA7D-4DC6-B0A1-50D8FAB4242F};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - diff --git a/ANX.Framework/ANX.Framework_WindowsMetro.csproj b/ANX.Framework/ANX.Framework_WindowsMetro.csproj index 0fe0d545..519d6014 100644 --- a/ANX.Framework/ANX.Framework_WindowsMetro.csproj +++ b/ANX.Framework/ANX.Framework_WindowsMetro.csproj @@ -1,5 +1,20 @@  + + Debug + AnyCPU + Properties + Library + 512 + 8.0.30703 + 2.0 + {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} + ANX.Framework + ANX.Framework + {BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + en-US + Test_TemporaryKey.pfx + true full @@ -19,21 +34,6 @@ 4 true - - Debug - AnyCPU - Properties - Library - 512 - 8.0.30703 - 2.0 - {6899F0C9-70B9-4EB0-9DD3-E598D4BE3E35} - ANX.Framework - ANX.Framework - {BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - en-US - Test_TemporaryKey.pfx - diff --git a/Tools/DX11MetroShaderGenerator/DX11MetroShaderGenerator.csproj b/Tools/DX11MetroShaderGenerator/DX11MetroShaderGenerator.csproj index 686d095a..577ebcf6 100644 --- a/Tools/DX11MetroShaderGenerator/DX11MetroShaderGenerator.csproj +++ b/Tools/DX11MetroShaderGenerator/DX11MetroShaderGenerator.csproj @@ -31,19 +31,11 @@ 4 - - False - ..\..\lib\SharpDX\Bin\Standard-net20\SharpDX.dll - - - ..\..\lib\SharpDX\Bin\Win8Desktop-net20\SharpDX.D3DCompiler.dll - - diff --git a/Tools/DX11MetroShaderGenerator/IncludeHandler.cs b/Tools/DX11MetroShaderGenerator/IncludeHandler.cs deleted file mode 100644 index ef95154f..00000000 --- a/Tools/DX11MetroShaderGenerator/IncludeHandler.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.IO; -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 DX11MetroShaderGenerator -{ - internal class IncludeHandler : Include - { - private string directory; - - public IncludeHandler(string directory) - { - this.directory = directory; - } - - public void Close(Stream stream) - { - stream.Close(); - } - - public Stream Open(IncludeType type, string fileName, Stream parentStream) - { - return File.OpenRead(Path.Combine(directory, fileName)); - } - - public IDisposable Shadow - { - get; - set; - } - - public void Dispose() - { - throw new NotImplementedException(); - } - } -} diff --git a/Tools/DX11MetroShaderGenerator/MetroCodeGenerator.cs b/Tools/DX11MetroShaderGenerator/MetroCodeGenerator.cs index ae3a343c..46ff77f8 100644 --- a/Tools/DX11MetroShaderGenerator/MetroCodeGenerator.cs +++ b/Tools/DX11MetroShaderGenerator/MetroCodeGenerator.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using HLSLParser; -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. @@ -108,16 +107,8 @@ namespace DX11MetroShaderGenerator { int indexOfOpenParenthesis = entryPoint.IndexOf('('); entryPoint = entryPoint.Substring(0, indexOfOpenParenthesis); - - /*ShaderBytecode byteCode = ShaderBytecode.Compile(resultSourceCode, - entryPoint, profile, ShaderFlags.None, EffectFlags.None, - null, new IncludeHandler("")); - - byte[] result = new byte[byteCode.BufferSize]; - byteCode.Data.Read(result, 0, result.Length); - return result;*/ - - return Execute(resultSourceCode, profile, entryPoint); + + return ExecuteCompiler(resultSourceCode, profile, entryPoint); } #endregion @@ -188,31 +179,35 @@ namespace DX11MetroShaderGenerator } #endregion - private byte[] Execute(string source, string profile, string entryPoint) + #region ExecuteCompiler + private byte[] ExecuteCompiler(string source, string profile, string entryPoint) { string tempSourcePath = Path.GetTempFileName() + ".fx"; string tempDestPath = Path.GetTempFileName() + "_" + profile + ".fxo"; File.WriteAllText(tempSourcePath, source); - + Process process = new Process(); - process.StartInfo.FileName = @"C:\Program Files (x86)\Windows Kits\8.0\bin\x86\fxc.exe"; - process.StartInfo.Arguments = "/E" + entryPoint + " /T" + profile + "_level_9_1 \"" + - tempSourcePath + "\" /Fo" + tempDestPath; + process.StartInfo.FileName = GetCompilerFilepath(); + process.StartInfo.Arguments = "/E" + entryPoint + " /T" + profile + + "_level_9_1 \"" + tempSourcePath + "\" /Fo" + tempDestPath; + process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardOutput = true; string output = ""; - DataReceivedEventHandler handler = delegate(object sender, DataReceivedEventArgs e) + DataReceivedEventHandler logHandler = + delegate(object sender, DataReceivedEventArgs e) { if (String.IsNullOrEmpty(e.Data) == false) { output += e.Data + "\n"; } }; - process.OutputDataReceived += handler; - process.ErrorDataReceived += handler; + + process.OutputDataReceived += logHandler; + process.ErrorDataReceived += logHandler; process.Start(); process.BeginErrorReadLine(); @@ -232,5 +227,67 @@ namespace DX11MetroShaderGenerator return new byte[0]; } + #endregion + + #region GetCompilerFilepath + private string GetCompilerFilepath() + { + string fxcToolPath = ""; + + string subDir = Environment.Is64BitOperatingSystem ? "x64" : "x86"; + + string sdkPath = Environment.GetEnvironmentVariable("DXSDK_DIR"); + if (String.IsNullOrEmpty(sdkPath) == false) + { + fxcToolPath = Path.Combine(sdkPath, subDir); + } + else + { + fxcToolPath = GetExistingPathFrom(new string[] + { + // Win8 paths + @"C:\Program Files (x86)\Windows Kits\8.0\bin\" + subDir + "\\", + @"C:\Program Files\Windows Kits\8.0\bin\" + subDir + "\\", + + // Win7 paths + @"C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\" + + subDir + "\\", + @"C:\Program Files\Microsoft DirectX SDK (June 2010)\Utilities\bin\" + + subDir + "\\", + }); + + if (String.IsNullOrEmpty(fxcToolPath)) + { + throw new Exception( + "Unable to find the DirectX SDK! Make sure to install it."); + } + } + + fxcToolPath = Path.Combine(fxcToolPath, "fxc.exe"); + + if (File.Exists(fxcToolPath) == false) + { + throw new Exception( + "Unable to compile shaders, cause the fxc.exe doesn't exist!"); + } + + return fxcToolPath; + } + #endregion + + #region GetExistingPathFrom + private string GetExistingPathFrom(string[] possiblePaths) + { + foreach (string path in possiblePaths) + { + if (Directory.Exists(path)) + { + return path; + } + } + + return null; + } + #endregion } } diff --git a/Tools/HLSLParser/HLSLParser/Variable.cs b/Tools/HLSLParser/HLSLParser/Variable.cs index 0aec97b2..eea22de1 100644 --- a/Tools/HLSLParser/HLSLParser/Variable.cs +++ b/Tools/HLSLParser/HLSLParser/Variable.cs @@ -151,6 +151,7 @@ namespace HLSLParser int nextSpaceIndex = FindInitialTypeEndSpace(currentText); string variableType = currentText.Substring(0, nextSpaceIndex); + variableType = variableType.Replace(" ", ""); walker.Seek(nextSpaceIndex + 1); variableType = ResolveMatrixAndArraySyntax(variableType); @@ -167,6 +168,18 @@ namespace HLSLParser { searchScope = text.IndexOf('>'); } + else if (text.StartsWith("Texture") || + text.StartsWith("texture")) + { + string textureGeneric = text; + textureGeneric = textureGeneric.Substring(textureGeneric.IndexOf(' ')); + textureGeneric = textureGeneric.Trim(); + if (textureGeneric.StartsWith("<")) + { + searchScope = text.IndexOf('>'); + } + } + return text.IndexOf(' ', searchScope); } #endregion diff --git a/Tools/HLSLParser/HLSLParserTests/VariableTests.cs b/Tools/HLSLParser/HLSLParserTests/VariableTests.cs index 43642128..9e9af1cf 100644 --- a/Tools/HLSLParser/HLSLParserTests/VariableTests.cs +++ b/Tools/HLSLParser/HLSLParserTests/VariableTests.cs @@ -114,14 +114,14 @@ Texture2D Texture : register(t0);"; [Test] public static void TestParseIfVariableWithGenericTexture2D() { - string variableSource = "Texture2D Texture : register(t0);"; + string variableSource = "Texture2D Texture : register(t0);"; var text = new ParseTextWalker(variableSource); var result = Variable.TryParse(text); Assert.NotNull(result); Assert.AreEqual("Texture2D", result.Type); Assert.AreEqual("Texture", result.Name); Assert.AreEqual("register(t0)", result.Register); - Assert.AreEqual(variableSource, result.ToString()); + Assert.AreEqual("Texture2D Texture : register(t0);", result.ToString()); } #endregion