public static Effect Load(string filePathAndName) { string extension = Path.GetExtension(filePathAndName); string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string fullPath = Path.Combine(baseDir, filePathAndName); if (!File.Exists(fullPath)) { Debugger.Break(); } if (extension == ".fx") { EffectCompiler compiler = new EffectCompiler(); var result = compiler.CompileFromFile(fullPath, EffectCompilerFlags.Debug); if (result.HasErrors) { var thing = result.Logger.Messages; Debugger.Break(); return null; } else return new Effect(Engine.GraphicsContext.Device, result.EffectData); } if (extension == ".fxo") { // todo } return null; }
private bool _isRebuildingAfterReset; // on compilation error, we try to rebuild it again with the original data public ShaderBuilder(ShaderData data) { if (data == null) throw new ArgumentNullException("data"); _data = data; _effectCompiler = new EffectCompiler(); }
public override bool Execute() { SetupD3DCompilerPath(); compiler = new EffectCompiler(); return base.Execute(); }
protected override void Initialize() { SetupD3DCompilerPath(); compiler = new EffectCompiler(); parseLogMessages = true; base.Initialize(); }
public static EffectData CompileEffect(string path) { var effectCompiler = new EffectCompiler(); var compilerResult = effectCompiler.CompileFromFile(path, EffectCompilerFlags.Debug | EffectCompilerFlags.SkipOptimization); if (compilerResult.HasErrors) { throw new Exception(); } return compilerResult.EffectData; }
private void ReloadShader() { if(Effect != null) Effect.Dispose(); EffectCompilerFlags compilerFlags = EffectCompilerFlags.None; #if DEBUG compilerFlags |= EffectCompilerFlags.Debug; #endif var shaderCompiler = new EffectCompiler(); // relaxed cone mapping compute shader var compileResult = shaderCompiler.CompileFromFile(shaderFilename, compilerFlags, shaderMacros); if (compileResult.HasErrors) { System.Console.WriteLine(compileResult.Logger.Messages); System.Diagnostics.Debugger.Break(); } Effect = new Effect(graphicsDevice, compileResult.EffectData); if (OnReload != null) OnReload(); }
private void DumpBytecode(EffectCompiler compiler, EffectData effectData) { Console.WriteLine(); for (int i = 0; i < effectData.Shaders.Count; i++) { var shader = effectData.Shaders[i]; Color(ConsoleColor.White); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine("Shader[{0}] {1}Type: {2} Level: {3} Visibility: {4} ByteCode Size: {5}", i, shader.Name == null ? string.Empty : string.Format("{0} ", shader.Name), shader.Type, shader.Level, shader.Name != null ? "public" : "private", shader.Bytecode.Length); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine(); ResetColor(); DumpHtmlToConsole(compiler.DisassembleShader(shader)); Console.WriteLine(); } Color(ConsoleColor.White); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine("effect {0}", effectData.Description.Name); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine(); ResetColor(); const string tab = " "; var effect = effectData.Description; { Console.WriteLine("{"); foreach (var technique in effect.Techniques) { Console.Write(tab + "technique"); Color(ConsoleColor.LightGreen); Console.WriteLine(" {0}", technique.Name); ResetColor(); Console.WriteLine(tab + "{"); for (int passIndex = 0; passIndex < technique.Passes.Count; passIndex++) { var pass = technique.Passes[passIndex]; var passtab = pass.IsSubPass ? tab + tab + tab : tab + tab; Console.Write("{0}{1} #{2}", passtab, ((pass.IsSubPass) ? "subpass" : "pass"), passIndex); Color(ConsoleColor.LightGreen); Console.WriteLine(" {0}", pass.Name); ResetColor(); Console.WriteLine(passtab + "{"); for (int i = 0; i < pass.Pipeline.Links.Length; i++) { var shaderType = (EffectShaderType)i; var link = pass.Pipeline.Links[i]; if (link == null) { continue; } Color(ConsoleColor.LightYellow); Console.Write(passtab + tab + "{0}", shaderType); ResetColor(); Console.Write(" = "); Color(ConsoleColor.White); Console.WriteLine("{0}", link.IsImport ? link.ImportName : link.IsNullShader ? "null" : string.Format("Shader[{0}] => {1}", link.Index, effectData.Shaders[link.Index].Name == null ? "private" : "public " + effectData.Shaders[link.Index].Name)); ResetColor(); } if (pass.Properties.Count > 0) { Console.WriteLine(); foreach (var attribute in pass.Properties) { var typeName = attribute.Value != null?attribute.Value.GetType().FullName.StartsWith("SharpDX") ? attribute.Value.GetType().FullName : null : null; Console.WriteLine(passtab + tab + "{0} = {1}", attribute.Key, typeName == null ? attribute.Value : string.Format("{0}({1})", typeName, attribute.Value)); } } Console.WriteLine(passtab + "}"); } Console.WriteLine(tab + "}"); } Console.WriteLine("}"); } }
void Run(string[] args) { var assemblyUri = new Uri(Assembly.GetEntryAssembly().CodeBase); var assemblyPath = Path.GetDirectoryName(assemblyUri.LocalPath); var newPath = Path.GetFullPath(Path.Combine(assemblyPath, @"..\Redist\D3D\" + (IntPtr.Size == 4 ? "x86" : "x64"))) + ";" + Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", newPath); // Print the exe header PrintHeader(); // Parse the command line if (!ParseCommandLine(args)) { Environment.Exit(-1); } var options = this; // ---------------------------------------------------------------- // Process macros // ---------------------------------------------------------------- var macros = new List <EffectData.ShaderMacro>(); foreach (var define in options.Defines) { var nameValue = define.Split('='); string name = nameValue[0]; string value = null; if (nameValue.Length > 1) { value = nameValue[1]; } macros.Add(new EffectData.ShaderMacro(name, value)); } // ---------------------------------------------------------------- // Setup compiler flags // ---------------------------------------------------------------- var flags = EffectCompilerFlags.None; if (options.Debug) { flags |= EffectCompilerFlags.Debug; } switch (options.OptimizationLevel) { case 0: flags |= EffectCompilerFlags.OptimizationLevel0; break; case 1: flags |= EffectCompilerFlags.OptimizationLevel1; break; case 2: flags |= EffectCompilerFlags.OptimizationLevel2; break; case 3: flags |= EffectCompilerFlags.OptimizationLevel3; break; } if (options.PackRowMajor) { flags |= EffectCompilerFlags.PackMatrixRowMajor; } if (options.PackColumnMajor) { flags |= EffectCompilerFlags.PackMatrixColumnMajor; } if (options.AvoidFlowControl) { flags |= EffectCompilerFlags.AvoidFlowControl; } if (options.PreferFlowControl) { flags |= EffectCompilerFlags.PreferFlowControl; } if (options.EnableStrictness) { flags |= EffectCompilerFlags.EnableStrictness; } if (options.EnableBackwardsCompatibility) { flags |= EffectCompilerFlags.EnableBackwardsCompatibility; } if (options.IeeeStrictness) { flags |= EffectCompilerFlags.IeeeStrictness; } hasErrors = false; // ---------------------------------------------------------------- // Process each fx files / tkfxo files // ---------------------------------------------------------------- var fxFile = options.FxFile; var filePath = Path.Combine(Environment.CurrentDirectory, fxFile); // Check that input file exists if (!File.Exists(filePath)) { ErrorColor(); Console.Error.WriteLine("File [{0}] does not exist", fxFile); ResetColor(); Abort(); } // ---------------------------------------------------------------- // Pre check files // ---------------------------------------------------------------- if (options.OutputClassFile == null && options.OutputFile == null) { options.OutputFile = Path.GetFileNameWithoutExtension(options.FxFile) + ".tkb"; } // Check for output files bool outputFileExist = options.OutputClassFile != null && File.Exists(options.OutputClassFile); if (options.OutputFile != null && !File.Exists(options.OutputFile)) { outputFileExist = false; } // New Compiler var compiler = new EffectCompiler(); string outputDependencyDirPath = Path.Combine(Environment.CurrentDirectory, OutputDependencyDirectory); string outputDependencyFilePath = Path.Combine(outputDependencyDirPath, compiler.GetDependencyFileNameFromSourcePath(options.FxFile)); if (AllowDynamicCompiling) { CompileOnlyIfNewer = true; } if (CompileOnlyIfNewer) { if (!compiler.CheckForChanges(outputDependencyFilePath) && outputFileExist) { Console.Error.WriteLine("Nothing to compile. Output file [{0}] is up-to-date", options.OutputFile); Environment.Exit(0); } } var viewOnly = false; // Try to load this file as a precompiled file var effectData = EffectData.Load(fxFile); EffectCompilerResult compilerResult = null; if (effectData != null) { Console.WriteLine("Load Compiled File [{0}]", fxFile); viewOnly = true; } else { // Compile the fx file Console.WriteLine("Compile Effect File [{0}]", filePath); compilerResult = compiler.Compile(File.ReadAllText(filePath), filePath, flags, macros, options.IncludeDirs, AllowDynamicCompiling, CompileOnlyIfNewer ? outputDependencyFilePath : null); // If there is any warning, errors, turn Error color on if (compilerResult.Logger.Messages.Count > 0) { ErrorColor(); } // Show a message error for the current file if (compilerResult.HasErrors) { Console.Error.WriteLine("Error when compiling file [{0}]:", fxFile); hasErrors = true; } // Print all messages (warning and errors) foreach (var logMessage in compilerResult.Logger.Messages) { Console.WriteLine(logMessage); } // If we have some messages, reset the color back if (compilerResult.Logger.Messages.Count > 0) { ResetColor(); } effectData = compilerResult.EffectData; } if (!NoDisassembly && effectData != null) { DumpBytecode(compiler, effectData); } if (hasErrors) { Abort(); } if (!viewOnly) { Console.WriteLine(); if (CompileOnlyIfNewer && compilerResult.DependencyFilePath != null) { // Dependency file save to Console.WriteLine("Save dependency list to [{0}]", outputDependencyFilePath); } if (OutputClassFile != null) { var codeWriter = new EffectDataCodeWriter { Namespace = OutputNamespace, ClassName = OutputClassname ?? Path.GetFileNameWithoutExtension(OutputClassFile), FieldName = OutputFieldName, }; Console.WriteLine("Save C# code output to [{0}]", OutputClassFile); using (var stream = new NativeFileStream(OutputClassFile, NativeFileMode.Create, NativeFileAccess.Write, NativeFileShare.Write)) codeWriter.Write(effectData, new StreamWriter(stream, Encoding.UTF8)); } if (options.OutputFile != null) { Console.WriteLine("Save binary output to [{0}]", options.OutputFile); // Save the result effectData.Save(options.OutputFile); } } }
public void Initialize() { cube = GeometricPrimitive.Cube.New(graphicsDevice); var cubeBounds = new OrientedBoundingBox(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0.5f, 0.5f, 0.5f)); cubeMesh = new RenderMesh { BoundingBox = cubeBounds, IndexBuffer = cube.IndexBuffer, IsIndex32Bits = cube.IsIndex32Bits, InputLayout = VertexInputLayout.New<VertexPositionNormalTexture>(0), ModelTransform = Matrix.Identity, VertexBuffer = cube.VertexBuffer }; basicEffect = new BasicEffect(graphicsDevice); basicEffect.EnableDefaultLighting(); // enable default lightning, useful for quick prototyping var debugEffectCompilerResult = new EffectCompiler().CompileFromFile("shaders/debug_solid.hlsl", EffectCompilerFlags.Debug); debugEffect = new Effect(graphicsDevice, debugEffectCompilerResult.EffectData, graphicsDevice.DefaultEffectPool); debugBatch = new PrimitiveBatch<VertexPositionColor>(graphicsDevice); }
void Run(string[] args) { var assemblyUri = new Uri(Assembly.GetEntryAssembly().CodeBase); var assemblyPath = Path.GetDirectoryName(assemblyUri.LocalPath); var newPath = Path.GetFullPath(Path.Combine(assemblyPath, @"..\Redist\D3D\" + (IntPtr.Size == 4 ? "x86" : "x64"))) + ";" + Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", newPath); // Print the exe header PrintHeader(); // Parse the command line if (!ParseCommandLine(args)) Environment.Exit(-1); var options = this; // ---------------------------------------------------------------- // Process macros // ---------------------------------------------------------------- var macros = new List<EffectData.ShaderMacro>(); foreach (var define in options.Defines) { var nameValue = define.Split('='); string name = nameValue[0]; string value = null; if (nameValue.Length > 1) { value = nameValue[1]; } macros.Add(new EffectData.ShaderMacro(name, value)); } // ---------------------------------------------------------------- // Setup compiler flags // ---------------------------------------------------------------- var flags = EffectCompilerFlags.None; if (options.Debug) flags |= EffectCompilerFlags.Debug; switch (options.OptimizationLevel) { case 0: flags |= EffectCompilerFlags.OptimizationLevel0; break; case 1: flags |= EffectCompilerFlags.OptimizationLevel1; break; case 2: flags |= EffectCompilerFlags.OptimizationLevel2; break; case 3: flags |= EffectCompilerFlags.OptimizationLevel3; break; } if (options.PackRowMajor) flags |= EffectCompilerFlags.PackMatrixRowMajor; if (options.PackColumnMajor) flags |= EffectCompilerFlags.PackMatrixColumnMajor; hasErrors = false; // ---------------------------------------------------------------- // Pre check files // ---------------------------------------------------------------- if (options.OutputClassFile == null && options.OutputFile == null) { options.OutputFile = "output.tkfxo"; } // Check for output files bool outputFileExist = options.OutputClassFile != null && File.Exists(options.OutputClassFile); if (options.OutputFile != null && !File.Exists(options.OutputFile)) { outputFileExist = false; } // ---------------------------------------------------------------- // Process each fx files / tkfxo files // ---------------------------------------------------------------- var fxFile = options.FxFile; var filePath = Path.Combine(Environment.CurrentDirectory, fxFile); // Check that input file exists if (!File.Exists(filePath)) { ErrorColor(); Console.Error.WriteLine("File [{0}] does not exist", fxFile); ResetColor(); Abort(); } // New Compiler var compiler = new EffectCompiler(); string outputDependencyDirPath = Path.Combine(Environment.CurrentDirectory, OutputDependencyDirectory); string outputDependencyFilePath = Path.Combine(outputDependencyDirPath, compiler.GetDependencyFileNameFromEffectPath(options.FxFile)); if (AllowDynamicCompiling) { CompileOnlyIfNewer = true; } if (CompileOnlyIfNewer) { if (!compiler.CheckForChanges(outputDependencyFilePath) && outputFileExist) { Console.Error.WriteLine("Nothing to compile. Output file [{0}] is up-to-date", options.OutputFile); Environment.Exit(0); } } var viewOnly = false; // Try to load this file as a precompiled file var effectData = EffectData.Load(fxFile); EffectCompilerResult compilerResult = null; if (effectData != null) { Console.WriteLine("Load Compiled File [{0}]", fxFile); viewOnly = true; } else { // Compile the fx file Console.WriteLine("Compile Effect File [{0}]", filePath); compilerResult = compiler.Compile(File.ReadAllText(filePath), filePath, flags, macros, options.IncludeDirs, AllowDynamicCompiling, CompileOnlyIfNewer ? outputDependencyFilePath : null); // If there is any warning, errors, turn Error color on if (compilerResult.Logger.Messages.Count > 0) { ErrorColor(); } // Show a message error for the current file if (compilerResult.HasErrors) { Console.Error.WriteLine("Error when compiling file [{0}]:", fxFile); hasErrors = true; } // Print all messages (warning and errors) foreach (var logMessage in compilerResult.Logger.Messages) { Console.WriteLine(logMessage); } // If we have some messages, reset the color back if (compilerResult.Logger.Messages.Count > 0) { ResetColor(); } effectData = compilerResult.EffectData; } if (!NoDisassembly && effectData != null) { DumpBytecode(compiler, effectData); } if (hasErrors) { Abort(); } if (!viewOnly) { Console.WriteLine(); if (CompileOnlyIfNewer && compilerResult.DependencyFilePath != null) { // Dependency file save to Console.WriteLine("Save dependency list to [{0}]", outputDependencyFilePath); } if (OutputClassFile != null) { var codeWriter = new EffectDataCodeWriter { Namespace = OutputNamespace, ClassName = OutputClassname ?? Path.GetFileNameWithoutExtension(OutputClassFile), FieldName = OutputFieldName, }; Console.WriteLine("Save C# code output to [{0}]", OutputClassFile); using (var stream = new NativeFileStream(OutputClassFile, NativeFileMode.Create, NativeFileAccess.Write, NativeFileShare.Write)) codeWriter.Write(effectData, new StreamWriter(stream, Encoding.UTF8)); } if (options.OutputFile != null) { Console.WriteLine("Save binary output to [{0}]", options.OutputFile); // Save the result effectData.Save(options.OutputFile); } } }
private void DumpBytecode(EffectCompiler compiler, EffectData effectData) { Console.WriteLine(); for (int i = 0; i < effectData.Shaders.Count; i++) { var shader = effectData.Shaders[i]; Color(ConsoleColor.White); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine("Shader[{0}] {1}Type: {2} Level: {3} Visibility: {4} ByteCode Size: {5}", i, shader.Name == null ? string.Empty : string.Format("{0} ", shader.Name), shader.Type, shader.Level, shader.Name != null ? "public" : "private", shader.Bytecode.Length); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine(); ResetColor(); DumpHtmlToConsole(compiler.DisassembleShader(shader)); Console.WriteLine(); } Color(ConsoleColor.White); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine("Effects"); Console.WriteLine("--------------------------------------------------------------------------------"); Console.WriteLine(); ResetColor(); const string tab = " "; foreach (var effect in effectData.Effects) { Console.Write("effect"); Color(ConsoleColor.LightGreen); Console.WriteLine(" {0}", effect.Name); ResetColor(); Console.WriteLine("{"); foreach (var technique in effect.Techniques) { Console.Write(tab + "technique"); Color(ConsoleColor.LightGreen); Console.WriteLine(" {0}", technique.Name); ResetColor(); Console.WriteLine(tab + "{"); for (int passIndex = 0; passIndex < technique.Passes.Count; passIndex++) { var pass = technique.Passes[passIndex]; var passtab = pass.IsSubPass ? tab + tab + tab : tab + tab; Console.Write("{0}{1} #{2}", passtab, ((pass.IsSubPass) ? "subpass" : "pass"), passIndex); Color(ConsoleColor.LightGreen); Console.WriteLine(" {0}", pass.Name); ResetColor(); Console.WriteLine(passtab + "{"); for (int i = 0; i < pass.Pipeline.Links.Length; i++) { var shaderType = (EffectShaderType) i; var link = pass.Pipeline.Links[i]; if (link == null) continue; Color(ConsoleColor.LightYellow); Console.Write(passtab + tab + "{0}", shaderType); ResetColor(); Console.Write(" = "); Color(ConsoleColor.White); Console.WriteLine("{0}", link.IsImport ? link.ImportName : link.IsNullShader ? "null" : string.Format("Shader[{0}] => {1}", link.Index, effectData.Shaders[link.Index].Name == null ? "private" : "public " + effectData.Shaders[link.Index].Name)); ResetColor(); } if (pass.Attributes.Count > 0) { Console.WriteLine(); foreach (var attribute in pass.Attributes) { var typeName = attribute.Value != null ? attribute.Value.GetType().FullName.StartsWith("SharpDX") ? attribute.Value.GetType().FullName : null : null; Console.WriteLine(passtab + tab + "{0} = {1}", attribute.Name, typeName == null ? attribute.Value : string.Format("{0}({1})", typeName, attribute.Value)); } } Console.WriteLine(passtab + "}"); } Console.WriteLine(tab + "}"); } Console.WriteLine("}"); } }