private static byte[] CompileHLSL(ShaderInfo shaderInfo, 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 (shaderInfo.Profile == ShaderProfile.DirectX_11) { shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility; } if (shaderInfo.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( shaderInfo.FileContent, shaderFunction, shaderProfile, shaderFlags, 0, null, null, shaderInfo.FilePath); // Store all the errors and warnings to log out later. errorsAndWarnings += result.Message; if (result.HasErrors) { 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()); }
private static byte[] CompileHLSL(ShaderInfo shaderInfo, 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 (shaderInfo.Profile == ShaderProfile.DirectX_11) shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility; if (shaderInfo.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( shaderInfo.FileContent, shaderFunction, shaderProfile, shaderFlags, 0, null, null, shaderInfo.FilePath); // Store all the errors and warnings to log out later. errorsAndWarnings += result.Message; if (result.HasErrors) 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(); }
protected virtual object EvalStart(ParseTree tree, params object[] paramlist) { var shader = new ShaderInfo(); foreach (var node in Nodes) { node.Eval(tree, shader); } return(shader); }
protected virtual object EvalStart(ParseTree tree, params object[] paramlist) { ShaderInfo shader = new ShaderInfo(); foreach (ParseNode node in Nodes) { var technique = node.Eval(tree, shader) as TechniqueInfo; if (technique != null) { shader.Techniques.Add(technique); } } return(shader); }
internal override ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings) { var bytecode = EffectObject.CompileHLSL(shaderInfo, 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 shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); effect.Shaders.Add(shaderData); return(shaderData); }
private d3dx_state CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, ref string errorsAndWarnings) { // Compile and create the shader. var shaderData = shaderInfo.Profile.CreateShader(shaderInfo, 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); }
public static int Main(string[] args) { var options = new Options(); var parser = new Utilities.CommandLineParser(options); parser.Title = "2MGFX - Converts Microsoft FX files to a compiled MonoGame Effect."; 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. ShaderInfo shaderInfo; try { shaderInfo = ShaderInfo.FromFile(options.SourceFile, options); } 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; try { effect = EffectObject.FromShaderInfo(shaderInfo); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); Console.Error.WriteLine("Failed to compile '{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 d3dx_state CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader) { // Compile the shader. byte[] bytecode; if (shaderInfo.Profile == ShaderProfile.DirectX_11 || shaderInfo.Profile == ShaderProfile.OpenGL) { // For now GLSL is only supported via translation // using MojoShader which works from HLSL bytecode. bytecode = CompileHLSL(shaderInfo, shaderFunction, shaderProfile); } else if (shaderInfo.Profile == ShaderProfile.PlayStation4) bytecode = CompilePSSL(shaderInfo, shaderFunction, shaderProfile); else throw new NotSupportedException("Unknown shader profile!"); // First look to see if we already created this same shader. ShaderData shaderData = null; foreach (var shader in Shaders) { if (bytecode.SequenceEqual(shader.Bytecode)) { shaderData = shader; break; } } // Create a new shader. if (shaderData == null) { if (shaderInfo.Profile == ShaderProfile.DirectX_11) shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); else if (shaderInfo.Profile == ShaderProfile.OpenGL) shaderData = ShaderData.CreateGLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); else if (shaderInfo.Profile == ShaderProfile.PlayStation4) shaderData = ShaderData.CreatePSSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); else throw new NotSupportedException("Unknown shader profile!"); Shaders.Add(shaderData); } 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 EffectObject FromShaderInfo(ShaderInfo shaderInfo) { var effect = new EffectObject(); // 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. 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]; pinfo.ValidateShaderModels(shaderInfo.Profile); if (!string.IsNullOrEmpty(pinfo.psFunction)) { pass.state_count += 1; tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.psFunction, pinfo.psModel, false); } if (!string.IsNullOrEmpty(pinfo.vsFunction)) { pass.state_count += 1; tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.vsFunction, pinfo.vsModel, true); } 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; }
private static byte[] CompilePSSL(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile) { // This is only part of the private PS4 repository. throw new NotImplementedException(); }
private d3dx_state CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader) { // Compile the shader. byte[] bytecode; if (shaderInfo.Profile == ShaderProfile.DirectX_11 || shaderInfo.Profile == ShaderProfile.OpenGL) { // For now GLSL is only supported via translation // using MojoShader which works from HLSL bytecode. bytecode = CompileHLSL(shaderInfo, shaderFunction, shaderProfile); } else if (shaderInfo.Profile == ShaderProfile.PlayStation4) { bytecode = CompilePSSL(shaderInfo, shaderFunction, shaderProfile); } else { throw new NotSupportedException("Unknown shader profile!"); } // First look to see if we already created this same shader. ShaderData shaderData = null; foreach (var shader in Shaders) { if (bytecode.SequenceEqual(shader.Bytecode)) { shaderData = shader; break; } } // Create a new shader. if (shaderData == null) { if (shaderInfo.Profile == ShaderProfile.DirectX_11) { shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); } else if (shaderInfo.Profile == ShaderProfile.OpenGL) { shaderData = ShaderData.CreateGLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); } else if (shaderInfo.Profile == ShaderProfile.PlayStation4) { shaderData = ShaderData.CreatePSSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug); } else { throw new NotSupportedException("Unknown shader profile!"); } Shaders.Add(shaderData); } 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 EffectObject FromShaderInfo(ShaderInfo shaderInfo) { var effect = new EffectObject(); // 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. 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]; pinfo.ValidateShaderModels(shaderInfo.Profile); if (!string.IsNullOrEmpty(pinfo.psFunction)) { pass.state_count += 1; tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.psFunction, pinfo.psModel, false); } if (!string.IsNullOrEmpty(pinfo.vsFunction)) { pass.state_count += 1; tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.vsFunction, pinfo.vsModel, true); } 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(); var parser = new Utilities.CommandLineParser(options); parser.Title = "2MGFX - Converts Microsoft FX files to a compiled MonoGame Effect."; 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. ShaderInfo shaderInfo; try { shaderInfo = ShaderInfo.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(shaderInfo, 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); }
internal abstract ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings);