void TryRoundTrip(string sourcePrefix, string compilerSource, byte[] expectedBytes) { var shaderSourceCode = sourcePrefix + compilerSource; var preprocessorDefines = new CompilerMacro[2]; preprocessorDefines[0].Name = "XBOX"; preprocessorDefines[1].Name = "XBOX360"; var includeHandler = new NopIncludeHandler(); var options = CompilerOptions.None; var compiledShader = ShaderCompiler.AssembleFromSource( shaderSourceCode, preprocessorDefines, includeHandler, options, Microsoft.Xna.Framework.TargetPlatform.Xbox360); var compiledBytes = compiledShader.GetShaderCode(); if (compiledBytes == null || compiledBytes.Length != expectedBytes.Length || !MemCmp(compiledBytes, expectedBytes)) { compilerUcodeTextBox.BackColor = System.Drawing.Color.Red; } else { compilerUcodeTextBox.BackColor = System.Drawing.SystemColors.Control; } }
static void ShaderCompiler(string inputfile, string outputfile, ShaderProfile shaderProfile, bool isAsm, string entryname) { CompilerMacro[] macroArray = null; macroArray = new CompilerMacro[2]; macroArray[0].Name = "XBOX"; macroArray[1].Name = "XBOX360"; CompiledShader compiledShader; if (isAsm) { compiledShader = Microsoft.Xna.Framework.Graphics.ShaderCompiler.AssembleFromFile(inputfile, macroArray, null, CompilerOptions.None, TargetPlatform.Xbox360); } else { compiledShader = Microsoft.Xna.Framework.Graphics.ShaderCompiler.CompileFromFile(inputfile, macroArray, null, CompilerOptions.None, entryname, shaderProfile, TargetPlatform.Xbox360); } if (compiledShader.Success) { System.IO.File.WriteAllBytes(outputfile, compiledShader.GetShaderCode()); } else { Console.WriteLine("Error compiling shader:"); } if (compiledShader.ErrorsAndWarnings != string.Empty) { Console.WriteLine(compiledShader.ErrorsAndWarnings); } }
public override TOutput Process(TInput input, ContentProcessorContext context) { #if !XNA4 // Populate preprocessor defines string stringBuffer = string.Empty; List <CompilerMacro> defines = new List <CompilerMacro>(); if (preprocessorDefines != string.Empty) { stringBuffer = preprocessorDefines; // Split preprocessor defines and build up macro array if (stringBuffer.Contains(",")) { string[] definesArr = stringBuffer.Split(','); foreach (string def in definesArr) { CompilerMacro macro = new CompilerMacro(); macro.Definition = "1\0"; macro.Name = def + "\0"; defines.Add(macro); } } } CompiledShader shader = ShaderCompiler.CompileFromSource(input, defines.ToArray(), includeHandler, CompilerOptions.PackMatrixRowMajor, entryPoint, shaderProfile, context.TargetPlatform); if (!shader.Success) { throw new InvalidContentException(shader.ErrorsAndWarnings); } HlslCompiledShader compiledShader = new HlslCompiledShader(entryPoint, shader.GetShaderCode()); #else throw new NotImplementedException(); //var shader = new EffectProcessor().Process(new EffectContent { EffectCode = input }, context); HlslCompiledShader compiledShader = new HlslCompiledShader(entryPoint, /**/ null /*/shader.GetEffectCode()/**/); #endif HlslCompiledShaders compiledShaders = new HlslCompiledShaders(); compiledShaders.Add(compiledShader); return(compiledShaders); }
static void EffectParser(string inputfile, string outputfile) { CompilerMacro[] macroArray = null; macroArray = new CompilerMacro[2]; macroArray[0].Name = "XBOX"; macroArray[1].Name = "XBOX360"; CompiledEffect compiledEffect = Microsoft.Xna.Framework.Graphics.Effect.CompileEffectFromFile(inputfile, macroArray, null, CompilerOptions.None, TargetPlatform.Xbox360); if (compiledEffect.Success) { System.IO.File.WriteAllBytes(outputfile, compiledEffect.GetEffectCode()); } else { Console.WriteLine("Error compiling effect:"); } if (compiledEffect.ErrorsAndWarnings != string.Empty) { Console.WriteLine(compiledEffect.ErrorsAndWarnings); } }
void Process(string shaderSourceCode) { if (shaderSourceCode.IndexOf("xvs_3_0") != -1 || shaderSourceCode.IndexOf("xps_3_0") != -1) { shaderSourceCode += "\ncnop"; shaderSourceCode += "\ncnop"; } var preprocessorDefines = new CompilerMacro[2]; preprocessorDefines[0].Name = "XBOX"; preprocessorDefines[1].Name = "XBOX360"; var includeHandler = new NopIncludeHandler(); var options = CompilerOptions.None; var compiledShader = ShaderCompiler.AssembleFromSource( shaderSourceCode, preprocessorDefines, includeHandler, options, Microsoft.Xna.Framework.TargetPlatform.Xbox360); var disassembledSourceCode = compiledShader.ErrorsAndWarnings; disassembledSourceCode = disassembledSourceCode.Replace("\n", Environment.NewLine); if (disassembledSourceCode.IndexOf("// PDB hint 00000000-00000000-00000000") == -1) { UpdateTextBox(outputTextBox, disassembledSourceCode, false); UpdateTextBox(compilerUcodeTextBox, "", false); UpdateTextBox(wordsTextBox, "", false); return; } var prefix = disassembledSourceCode.Substring( 0, disassembledSourceCode.IndexOf( ':', disassembledSourceCode.IndexOf(':') + 1)); disassembledSourceCode = disassembledSourceCode.Replace(prefix + ": ", ""); disassembledSourceCode = disassembledSourceCode.Replace( "// PDB hint 00000000-00000000-00000000" + Environment.NewLine, ""); var firstLine = disassembledSourceCode.IndexOf("//"); var warnings = "// " + disassembledSourceCode.Substring(0, firstLine) .Replace(Environment.NewLine, Environment.NewLine + "// "); disassembledSourceCode = warnings + disassembledSourceCode.Substring(firstLine + 3); disassembledSourceCode = disassembledSourceCode.Trim(); UpdateTextBox(outputTextBox, disassembledSourceCode, true); string shaderType = shaderSourceCode.IndexOf("vs_") == -1 ? "ps" : "vs"; var ucodeWords = ExtractAndDumpWords(shaderType, compiledShader.GetShaderCode()); if (ucodeWords != null) { TryCompiler(shaderType, ucodeWords); } else { UpdateTextBox(compilerUcodeTextBox, "", false); } if (compilerUcodeTextBox.Text.Length > 0) { var sourcePrefix = disassembledSourceCode.Substring(0, disassembledSourceCode.IndexOf("/*")); TryRoundTrip(sourcePrefix, compilerUcodeTextBox.Text, compiledShader.GetShaderCode()); } }