private static void WriteParameter(BinaryWriterEx writer, D3Dx_parameter param)
        {
            var class_ = ToXNAParameterClass(param.class_);
            var type   = ToXNAParameterType(param.type);

            writer.Write((byte)class_);
            writer.Write((byte)type);

            writer.Write(param.name);
            writer.Write(param.semantic);
            WriteAnnotations(writer, param.annotation_handles);

            writer.Write((byte)param.rows);
            writer.Write((byte)param.columns);

            // Write the elements or struct members.
            WriteParameters(writer, param.member_handles, (int)param.element_count);
            WriteParameters(writer, param.member_handles, (int)param.member_count);

            if (param.element_count == 0 && param.member_count == 0)
            {
                switch (type)
                {
                case EffectParameterType.Bool:
                case EffectParameterType.Int32:
                case EffectParameterType.Single:
                    writer.Write((byte[])param.data);
                    break;
                }
            }
        }
        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);
        }