public static byte[] CompileHLSL(ShaderResult shaderResult, string shaderFunction, string shaderProfile, ref string errorsAndWarnings) { SharpDX.D3DCompiler.ShaderBytecode shaderByteCode; try { SharpDX.D3DCompiler.ShaderFlags shaderFlags = 0; // While we never allow preshaders, this flag is invalid for // the DX11 shader compiler which doesn't allow preshaders // in the first place. //shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.NoPreshader; if (shaderResult.Profile == ShaderProfile.DirectX_11) { shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility; } if (shaderResult.Debug) { shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization; shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.Debug; } else { shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel3; } // Compile the shader into bytecode. var result = SharpDX.D3DCompiler.ShaderBytecode.Compile( shaderResult.FileContent, shaderFunction, shaderProfile, shaderFlags, 0, null, null, shaderResult.FilePath); // Store all the errors and warnings to log out later. errorsAndWarnings += result.Message; if (result.Bytecode == null) { throw new ShaderCompilerException(); } shaderByteCode = result.Bytecode; //var source = shaderByteCode.Disassemble(); } catch (SharpDX.CompilationException ex) { errorsAndWarnings += ex.Message; throw new ShaderCompilerException(); } // Return a copy of the shader bytecode. return(shaderByteCode.Data.ToArray()); }
internal override ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings) { var bytecode = EffectObject.CompileHLSL(shaderResult, shaderFunction, shaderProfile, ref errorsAndWarnings); // First look to see if we already created this same shader. foreach (var shader in effect.Shaders) { if (bytecode.SequenceEqual(shader.Bytecode)) { return(shader); } } var shaderInfo = shaderResult.ShaderInfo; var shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderResult.Debug); effect.Shaders.Add(shaderData); return(shaderData); }
private d3dx_state CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, ref string errorsAndWarnings) { // Compile and create the shader. var shaderData = shaderResult.Profile.CreateShader(shaderResult, shaderFunction, shaderProfile, isVertexShader, this, ref errorsAndWarnings); var state = new d3dx_state(); state.index = 0; state.type = STATE_TYPE.CONSTANT; state.operation = isVertexShader ? (uint)146 : (uint)147; state.parameter = new d3dx_parameter(); state.parameter.name = string.Empty; state.parameter.semantic = string.Empty; state.parameter.class_ = D3DXPARAMETER_CLASS.OBJECT; state.parameter.type = isVertexShader ? D3DXPARAMETER_TYPE.VERTEXSHADER : D3DXPARAMETER_TYPE.PIXELSHADER; state.parameter.rows = 0; state.parameter.columns = 0; state.parameter.data = shaderData.SharedIndex; return(state); }
static public ShaderResult FromString(string effectSource, string filePath, Options options, IEffectCompilerOutput output) { var macros = new Dictionary <string, string>(); macros.Add("MGFX", "1"); options.Profile.AddMacros(macros); // If we're building shaders for debug set that flag too. if (options.Debug) { macros.Add("DEBUG", "1"); } if (!string.IsNullOrEmpty(options.Defines)) { var defines = options.Defines.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var define in defines) { var name = define; var value = "1"; if (define.Contains("=")) { var parts = define.Split('='); if (parts.Length > 0) { name = parts[0].Trim(); } if (parts.Length > 1) { value = parts[1].Trim(); } } macros.Add(name, value); } } // Use the D3DCompiler to pre-process the file resolving // all #includes and macros.... this even works for GLSL. string newFile; var fullPath = Path.GetFullPath(filePath); var dependencies = new List <string>(); newFile = Preprocessor.Preprocess(effectSource, fullPath, macros, dependencies, output); // Parse the resulting file for techniques and passes. var tree = new Parser(new Scanner()).Parse(newFile, fullPath); if (tree.Errors.Count > 0) { var errors = String.Empty; foreach (var error in tree.Errors) { errors += string.Format("{0}({1},{2}) : {3}\r\n", error.File, error.Line, error.Column, error.Message); } throw new Exception(errors); } // Evaluate the results of the parse tree. var shaderInfo = tree.Eval() as ShaderInfo; // Remove the samplers and techniques so that the shader compiler // gets a clean file without any FX file syntax in it. var cleanFile = newFile; WhitespaceNodes(TokenType.Technique_Declaration, tree.Nodes, ref cleanFile); WhitespaceNodes(TokenType.Sampler_Declaration_States, tree.Nodes, ref cleanFile); // Setup the rest of the shader info. ShaderResult result = new ShaderResult(); result.ShaderInfo = shaderInfo; result.Dependencies = dependencies; result.FilePath = fullPath; result.FileContent = cleanFile; if (!string.IsNullOrEmpty(options.OutputFile)) { result.OutputFilePath = Path.GetFullPath(options.OutputFile); } result.AdditionalOutputFiles = new List <string>(); // Remove empty techniques. for (var i = 0; i < shaderInfo.Techniques.Count; i++) { var tech = shaderInfo.Techniques[i]; if (tech.Passes.Count <= 0) { shaderInfo.Techniques.RemoveAt(i); i--; } } // We must have at least one technique. if (shaderInfo.Techniques.Count <= 0) { throw new Exception("The effect must contain at least one technique and pass!"); } result.Profile = options.Profile; result.Debug = options.Debug; return(result); }
static public EffectObject CompileEffect(ShaderResult shaderResult, out string errorsAndWarnings) { var effect = new EffectObject(); errorsAndWarnings = string.Empty; // These are filled out as we process stuff. effect.ConstantBuffers = new List <ConstantBufferData>(); effect.Shaders = new List <ShaderData>(); // Go thru the techniques and that will find all the // shaders and constant buffers. var shaderInfo = shaderResult.ShaderInfo; effect.Techniques = new d3dx_technique[shaderInfo.Techniques.Count]; for (var t = 0; t < shaderInfo.Techniques.Count; t++) { var tinfo = shaderInfo.Techniques[t];; var technique = new d3dx_technique(); technique.name = tinfo.name; technique.pass_count = (uint)tinfo.Passes.Count; technique.pass_handles = new d3dx_pass[tinfo.Passes.Count]; for (var p = 0; p < tinfo.Passes.Count; p++) { var pinfo = tinfo.Passes[p]; var pass = new d3dx_pass(); pass.name = pinfo.name ?? string.Empty; pass.blendState = pinfo.blendState; pass.depthStencilState = pinfo.depthStencilState; pass.rasterizerState = pinfo.rasterizerState; pass.state_count = 0; var tempstate = new d3dx_state[2]; shaderResult.Profile.ValidateShaderModels(pinfo); if (!string.IsNullOrEmpty(pinfo.psFunction)) { pass.state_count += 1; tempstate[pass.state_count - 1] = effect.CreateShader(shaderResult, pinfo.psFunction, pinfo.psModel, false, ref errorsAndWarnings); } if (!string.IsNullOrEmpty(pinfo.vsFunction)) { pass.state_count += 1; tempstate[pass.state_count - 1] = effect.CreateShader(shaderResult, pinfo.vsFunction, pinfo.vsModel, true, ref errorsAndWarnings); } pass.states = new d3dx_state[pass.state_count]; for (var s = 0; s < pass.state_count; s++) { pass.states[s] = tempstate[s]; } technique.pass_handles[p] = pass; } effect.Techniques[t] = technique; } // Make the list of parameters by combining all the // constant buffers ignoring the buffer offsets. var parameters = new List <d3dx_parameter>(); for (var c = 0; c < effect.ConstantBuffers.Count; c++) { var cb = effect.ConstantBuffers[c]; for (var i = 0; i < cb.Parameters.Count; i++) { var param = cb.Parameters[i]; var match = parameters.FindIndex(e => e.name == param.name); if (match == -1) { cb.ParameterIndex.Add(parameters.Count); parameters.Add(param); } else { // TODO: Make sure the type and size of // the parameter match up! cb.ParameterIndex.Add(match); } } } // Add the texture parameters from the samplers. foreach (var shader in effect.Shaders) { for (var s = 0; s < shader._samplers.Length; s++) { var sampler = shader._samplers[s]; var match = parameters.FindIndex(e => e.name == sampler.parameterName); if (match == -1) { // Store the index for runtime lookup. shader._samplers[s].parameter = parameters.Count; var param = new d3dx_parameter(); param.class_ = D3DXPARAMETER_CLASS.OBJECT; param.name = sampler.parameterName; param.semantic = string.Empty; switch (sampler.type) { case MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_1D: param.type = D3DXPARAMETER_TYPE.TEXTURE1D; break; case MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D: param.type = D3DXPARAMETER_TYPE.TEXTURE2D; break; case MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_VOLUME: param.type = D3DXPARAMETER_TYPE.TEXTURE3D; break; case MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_CUBE: param.type = D3DXPARAMETER_TYPE.TEXTURECUBE; break; } parameters.Add(param); } else { // TODO: Make sure the type and size of // the parameter match up! shader._samplers[s].parameter = match; } } } // TODO: Annotations are part of the .FX format and // not a part of shaders... we need to implement them // in our mgfx parser if we want them back. effect.Parameters = parameters.ToArray(); return(effect); }
public static int Main(string[] args) { if (!Environment.Is64BitProcess && Environment.OSVersion.Platform != PlatformID.Unix) { Console.Error.WriteLine("The MonoGame content tools only work on a 64bit OS."); return(-1); } var options = new Options(); Options.CurrentOptions = options; var parser = new CommandLineParser(options); parser.Title = "2MGFX - The MonoGame Effect compiler."; if (!parser.ParseCommandLine(args)) { return(1); } // Validate the input file exits. if (!File.Exists(options.SourceFile)) { Console.Error.WriteLine("The input file '{0}' was not found!", options.SourceFile); return(1); } // TODO: This would be where we would decide the user // is trying to convert an FX file to a MGFX glsl file. // // For now we assume we're going right to a compiled MGFXO file. // Parse the MGFX file expanding includes, macros, and returning the techniques. ShaderResult shaderResult; try { shaderResult = ShaderResult.FromFile(options.SourceFile, options, new ConsoleEffectCompilerOutput()); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); Console.Error.WriteLine("Failed to parse '{0}'!", options.SourceFile); return(1); } // Create the effect object. EffectObject effect; var shaderErrorsAndWarnings = string.Empty; try { effect = EffectObject.CompileEffect(shaderResult, out shaderErrorsAndWarnings); if (!string.IsNullOrEmpty(shaderErrorsAndWarnings)) { Console.Error.WriteLine(shaderErrorsAndWarnings); } } catch (ShaderCompilerException) { // Write the compiler errors and warnings and let the user know what happened. Console.Error.WriteLine(shaderErrorsAndWarnings); Console.Error.WriteLine("Failed to compile '{0}'!", options.SourceFile); return(1); } catch (Exception ex) { // First write all the compiler errors and warnings. if (!string.IsNullOrEmpty(shaderErrorsAndWarnings)) { Console.Error.WriteLine(shaderErrorsAndWarnings); } // If we have an exception message then write that. if (!string.IsNullOrEmpty(ex.Message)) { Console.Error.WriteLine(ex.Message); } // Let the user know what happened. Console.Error.WriteLine("Unexpected error compiling '{0}'!", options.SourceFile); return(1); } // Get the output file path. if (options.OutputFile == string.Empty) { options.OutputFile = Path.GetFileNameWithoutExtension(options.SourceFile) + ".mgfxo"; } // Write out the effect to a runtime format. try { using (var stream = new FileStream(options.OutputFile, FileMode.Create, FileAccess.Write)) using (var writer = new BinaryWriter(stream)) effect.Write(writer, options); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); Console.Error.WriteLine("Failed to write '{0}'!", options.OutputFile); return(1); } // We finished succesfully. Console.WriteLine("Compiled '{0}' to '{1}'.", options.SourceFile, options.OutputFile); return(0); }
private static byte[] CompilePSSL(ShaderResult shaderResult, string shaderFunction, string shaderProfile, ref string errorsAndWarnings) { // This is only part of the private PS4 repository. throw new NotImplementedException(); }
internal abstract ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings);