private static void WriteParameter(BinaryWriter 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;
                }
            }
        }
示例#2
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);
        }
 private static void WriteAnnotations(BinaryWriter writer, d3dx_parameter[] annotations)
 {
     var count = annotations == null ? 0 : annotations.Length;
     writer.Write((byte)count);
     for (var i = 0; i < count; i++)
         WriteParameter(writer, annotations[i]);
 }
        private static void WriteParameter(BinaryWriter 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;
                }
            }
        }
 private static void WriteParameters(BinaryWriter writer, d3dx_parameter[] parameters, int count)
 {
     writer.Write((byte)count);
     for (var i = 0; i < count; i++)
         WriteParameter(writer, parameters[i]);
 }
        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;
        }
示例#7
0
        static public DXEffectObject FromShaderInfo(TwoMGFX.ShaderInfo shaderInfo)
        {
            var effect = new DXEffectObject();

            // These are filled out as we process stuff.
            effect.ConstantBuffers = new List <DXConstantBufferData>();
            effect.Shaders         = new List <DXShaderData>();

            // 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.state_count = 0;
                    var tempstate = new d3dx_state[2];

                    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)
                    {
                        shader._samplers[s].parameter = parameters.Count;

                        var param = new d3dx_parameter();
                        param.class_   = D3DXPARAMETER_CLASS.OBJECT;
                        param.type     = D3DXPARAMETER_TYPE.TEXTURE2D; // TODO: Fix this right!
                        param.name     = sampler.parameterName;
                        param.semantic = string.Empty;

                        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.

            /*
             * // Now find the things we could not in the constant
             * // buffer reflection interface...  semantics and annotations.
             * for (var i = 0; i < parameters.Count; i++)
             * {
             *  var vdesc = dxeffect.GetVariableByName(parameters[i].name);
             *  if (!vdesc.IsValid)
             *      continue;
             *
             *  parameters[i].semantic = vdesc.Description.Semantic ?? string.Empty;
             *
             *  // TODO: Annotations!
             * }
             */

            effect.Parameters = parameters.ToArray();

            return(effect);
        }