private static byte[] CompileGLSL(ShaderInfo shaderInfo, string shaderFilename, string shaderXML, ContentProcessorContext context)
        {
			return System.IO.File.ReadAllBytes(shaderFilename);
        }
示例#2
0
		static public ShaderInfo PureGLSLFromString (string effectSource, string filePath, Options options, IEffectCompilerOutput output)
		{

			ShaderInfo shaderInfo = new ShaderInfo ();
			shaderInfo.FilePath = options.SourceFile;
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (effectSource);
			XmlElement effectElement;
			XmlNode current = doc.FirstChild;
			while (current != null && current.NodeType != XmlNodeType.Element) {
				current = current.NextSibling;
			}
			effectElement = (XmlElement)current;
			shaderInfo.Techniques = new List<TechniqueInfo> ();
			foreach (XmlElement technique in effectElement.ChildNodes.OfType<XmlElement>()) {
				TechniqueInfo info = new TechniqueInfo ();
				info.name = technique.GetAttribute ("name");
				info.Passes = new List<PassInfo> ();
				foreach (XmlElement pass in technique.ChildNodes.OfType<XmlElement>()) {
					PassInfo pi = new PassInfo ();
					pi.name = pass.GetAttribute ("name");
					foreach (XmlElement sh in pass.ChildNodes) {
						if (sh.Name == "Shader") {
							if (sh.GetAttribute ("type") == "PixelShader") {
								pi.psFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(shaderInfo.FilePath),sh.GetAttribute ("filename"));
								shaderInfo.Dependencies.Add (pi.psFileName);
								pi.psShaderXML = sh.OuterXml;
							} else if (sh.GetAttribute ("type") == "VertexShader") {
								pi.vsFilename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(shaderInfo.FilePath),sh.GetAttribute ("filename"));
								shaderInfo.Dependencies.Add (pi.vsFilename);
								pi.vsShaderXML = sh.OuterXml;
							} else {
								throw new PipelineException ("Unsupported Shader type detected");
							}
						} else if (sh.Name == "BlendState") {
							pi.blendState = ParseBlendState (sh);

						} else if (sh.Name == "DepthStencilState") {
							pi.depthStencilState = ParseDepthStencilState (sh);
						} else if (sh.Name == "RasterizerState") {
							pi.rasterizerState = ParseRasterizerState (sh);
						} else {
							throw new PipelineException ("'" + sh.Name + "' element not recognized");
						}
					}
					info.Passes.Add (pi);
				}
				shaderInfo.Techniques.Add (info);
			}

			shaderInfo.Profile = options.Profile;
			shaderInfo.Debug = options.Debug;

			shaderInfo.OutputFilePath = options.OutputFile;

			return shaderInfo;
		}
示例#3
0
 private static byte[] CompileGLSL(ShaderInfo shaderInfo, string shaderFilename, string shaderXML, ContentProcessorContext context)
 {
     return(System.IO.File.ReadAllBytes(shaderFilename));
 }
        private d3dx_state CreateShader(ShaderInfo shaderInfo, string shaderFilename, string shaderXML, bool isVertexShader, ContentProcessorContext context)
        {
            // Compile the shader.

            byte[] bytecode;
            if (shaderInfo.Profile == ShaderProfile.DirectX_11 || shaderInfo.Profile == ShaderProfile.OpenGL)
            {
                throw new NotImplementedException("HLSL not implemented here");
            }
            else if (shaderInfo.Profile == ShaderProfile.PureGLSL)
            {
                bytecode = CompileGLSL(shaderInfo, shaderFilename, shaderXML, context);
            }
            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)
                    throw new NotImplementedException("HLSL not implemented");//shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug);
                else if (shaderInfo.Profile == ShaderProfile.PureGLSL)
                    shaderData = ShaderData.CreatePureGLSL(bytecode,shaderFilename, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderXML, 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 CompileEffect(ShaderInfo shaderInfo,ContentProcessorContext context)
        {
            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.psFileName))
                    {
                        pass.state_count += 1;
						tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.psFileName, pinfo.psShaderXML, false, context);
                    }
                    if (!string.IsNullOrEmpty(pinfo.vsFilename))
                    {
                        pass.state_count += 1;
                        tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.vsFilename, pinfo.vsShaderXML, true, context);
                    }

                    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;
        }
示例#6
0
        static public ShaderInfo PureGLSLFromString(string effectSource, string filePath, Options options, IEffectCompilerOutput output)
        {
            ShaderInfo shaderInfo = new ShaderInfo();

            shaderInfo.FilePath = options.SourceFile;
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(effectSource);
            XmlElement effectElement;
            XmlNode    current = doc.FirstChild;

            while (current != null && current.NodeType != XmlNodeType.Element)
            {
                current = current.NextSibling;
            }
            effectElement         = (XmlElement)current;
            shaderInfo.Techniques = new List <TechniqueInfo> ();
            foreach (XmlElement technique in effectElement.ChildNodes.OfType <XmlElement>())
            {
                TechniqueInfo info = new TechniqueInfo();
                info.name   = technique.GetAttribute("name");
                info.Passes = new List <PassInfo> ();
                foreach (XmlElement pass in technique.ChildNodes.OfType <XmlElement>())
                {
                    PassInfo pi = new PassInfo();
                    pi.name = pass.GetAttribute("name");
                    foreach (XmlElement sh in pass.ChildNodes)
                    {
                        if (sh.Name == "Shader")
                        {
                            if (sh.GetAttribute("type") == "PixelShader")
                            {
                                pi.psFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(shaderInfo.FilePath), sh.GetAttribute("filename"));
                                shaderInfo.Dependencies.Add(pi.psFileName);
                                pi.psShaderXML = sh.OuterXml;
                            }
                            else if (sh.GetAttribute("type") == "VertexShader")
                            {
                                pi.vsFilename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(shaderInfo.FilePath), sh.GetAttribute("filename"));
                                shaderInfo.Dependencies.Add(pi.vsFilename);
                                pi.vsShaderXML = sh.OuterXml;
                            }
                            else
                            {
                                throw new PipelineException("Unsupported Shader type detected");
                            }
                        }
                        else if (sh.Name == "BlendState")
                        {
                            pi.blendState = ParseBlendState(sh);
                        }
                        else if (sh.Name == "DepthStencilState")
                        {
                            pi.depthStencilState = ParseDepthStencilState(sh);
                        }
                        else if (sh.Name == "RasterizerState")
                        {
                            pi.rasterizerState = ParseRasterizerState(sh);
                        }
                        else
                        {
                            throw new PipelineException("'" + sh.Name + "' element not recognized");
                        }
                    }
                    info.Passes.Add(pi);
                }
                shaderInfo.Techniques.Add(info);
            }

            shaderInfo.Profile = options.Profile;
            shaderInfo.Debug   = options.Debug;

            shaderInfo.OutputFilePath = options.OutputFile;

            return(shaderInfo);
        }
示例#7
0
        private d3dx_state CreateShader(ShaderInfo shaderInfo, string shaderFilename, string shaderXML, bool isVertexShader, ContentProcessorContext context)
        {
            // Compile the shader.

            byte[] bytecode;
            if (shaderInfo.Profile == ShaderProfile.DirectX_11 || shaderInfo.Profile == ShaderProfile.OpenGL)
            {
                throw new NotImplementedException("HLSL not implemented here");
            }
            else if (shaderInfo.Profile == ShaderProfile.PureGLSL)
            {
                bytecode = CompileGLSL(shaderInfo, shaderFilename, shaderXML, context);
            }
            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)
                {
                    throw new NotImplementedException("HLSL not implemented");//shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug);
                }
                else if (shaderInfo.Profile == ShaderProfile.PureGLSL)
                {
                    shaderData = ShaderData.CreatePureGLSL(bytecode, shaderFilename, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.SamplerStates, shaderXML, 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);
        }
示例#8
0
        static public EffectObject CompileEffect(ShaderInfo shaderInfo, ContentProcessorContext context)
        {
            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.psFileName))
                    {
                        pass.state_count += 1;
                        tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.psFileName, pinfo.psShaderXML, false, context);
                    }
                    if (!string.IsNullOrEmpty(pinfo.vsFilename))
                    {
                        pass.state_count += 1;
                        tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.vsFilename, pinfo.vsShaderXML, true, context);
                    }

                    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);
        }