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);
        }
        public static 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
                {
                    name         = tinfo.name,
                    pass_count   = (uint)tinfo.Passes.Count,
                    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
                    {
                        name = pinfo.name ?? string.Empty,

                        blendState        = pinfo.blendState,
                        depthStencilState = pinfo.depthStencilState,
                        rasterizerState   = pinfo.rasterizerState,

                        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
                        {
                            class_   = D3DXPARAMETER_CLASS.OBJECT,
                            name     = sampler.parameterName,
                            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);
        }