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();
 }
        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
            {
                index     = 0,
                type      = STATE_TYPE.CONSTANT,
                operation = isVertexShader ? 146 : (uint)147,

                parameter = new D3Dx_parameter
                {
                    name     = string.Empty,
                    semantic = string.Empty,
                    class_   = D3DXPARAMETER_CLASS.OBJECT,
                    type     = isVertexShader ? D3DXPARAMETER_TYPE.VERTEXSHADER : D3DXPARAMETER_TYPE.PIXELSHADER,
                    rows     = 0,
                    columns  = 0,
                    data     = shaderData.SharedIndex
                }
            };

            return(state);
        }
Exemple #3
0
        internal override ShaderData CreateShader(
            ShaderResult shaderResult, string shaderFunction, string shaderProfile,
            bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
        {
            // For now GLSL is only supported via translation
            // using MojoShader which works from HLSL bytecode.
            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.CreateGLSL(
                bytecode, isVertexShader, effect.ConstantBuffers,
                effect.Shaders.Count, shaderInfo.SamplerStates, shaderResult.Debug);

            effect.Shaders.Add(shaderData);

            return(shaderData);
        }
        public static byte[] CompileHLSL(
            ShaderResult shaderResult, string shaderFunction, string shaderProfile,
            ref string errorsAndWarnings)
        {
            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();
                }

                var shaderByteCode = result.Bytecode;
                //var source = shaderByteCode.Disassemble();

                // Return a copy of the shader bytecode.
                return(shaderByteCode.Data);
            }
            catch (SharpDX.CompilationException ex)
            {
                errorsAndWarnings += ex.Message;
                throw new ShaderCompilerException();
            }
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
 internal abstract ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings);
Exemple #8
0
        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);
        }