Ejemplo n.º 1
0
 /// <summary>
 /// Initializes the bindings of the effect by calling the <see cref="IEffectBinder"/>s.
 /// </summary>
 /// <param name="graphicsService">The graphics service.</param>
 /// <param name="effectEx">The effect wrapper.</param>
 /// <param name="opaqueData">The opaque data.</param>
 /// <param name="bindings">
 /// The collection that stores the resulting effect parameter bindings.
 /// </param>
 /// <remarks>
 /// If the <see cref="IEffectBinder"/>s do not return a binding, then this method creates
 /// default bindings using opaque data or the default values from the effect.
 /// </remarks>
 internal static void InitializeParameterBindings(IGraphicsService graphicsService, EffectEx effectEx, IDictionary <string, object> opaqueData, EffectParameterBindingCollection bindings)
 {
     foreach (var parameter in effectEx.Resource.Parameters)
     {
         SetBinding(graphicsService, effectEx, opaqueData, bindings, parameter);
     }
 }
Ejemplo n.º 2
0
        private static void SetDefaultBinding(IGraphicsService graphicsService, EffectEx effectEx, EffectParameter parameter, EffectParameterDescription usage, EffectParameterBindingCollection bindings)
        {
            Debug.Assert(!bindings.Contains(parameter), "Effect binding already contains a binding for the given effect parameter.");

            if (parameter.ParameterClass == EffectParameterClass.Struct)
            {
                if (parameter.Elements.Count > 0)
                {
                    // ----- Effect parameter is an array of structs. --> Recursively process elements of array.
                    foreach (EffectParameter element in parameter.Elements)
                    {
                        SetDefaultBinding(graphicsService, effectEx, element, usage, bindings);
                    }
                }
                else
                {
                    // ----- Effect parameter is a struct. --> Recursively process members of struct.
                    foreach (EffectParameter member in parameter.StructureMembers)
                    {
                        SetDefaultBinding(graphicsService, effectEx, member, usage, bindings);
                    }
                }

                return;
            }

            // Set ConstParameterBinding using the default value stored in .fx file.
            var    effect = effectEx.Resource;
            object originalValue;

            effectEx.OriginalParameterValues.TryGetValue(parameter, out originalValue);
            EffectParameterBinding binding = null;

            if (parameter.Elements.Count == 0)
            {
                // ----- Parameter is not an array.

                if (parameter.ParameterClass == EffectParameterClass.Scalar)
                {
                    // Scalar values.
                    if (parameter.ParameterType == EffectParameterType.Bool)
                    {
                        binding = new ConstParameterBinding <bool>(effect, parameter, (bool)originalValue);
                    }
                    else if (parameter.ParameterType == EffectParameterType.Int32)
                    {
                        binding = new ConstParameterBinding <int>(effect, parameter, (int)originalValue);
                    }
                    else if (parameter.ParameterType == EffectParameterType.Single)
                    {
                        binding = new ConstParameterBinding <float>(effect, parameter, (float)originalValue);
                    }
                }
                else if (parameter.ParameterClass == EffectParameterClass.Vector &&
                         parameter.ParameterType == EffectParameterType.Single)
                {
                    // Vector values.
                    if (parameter.ColumnCount == 2 || parameter.RowCount == 2)
                    {
                        binding = new ConstParameterBinding <Vector2>(effect, parameter, (Vector2)originalValue);
                    }
                    else if (parameter.ColumnCount == 3 || parameter.RowCount == 3)
                    {
                        binding = new ConstParameterBinding <Vector3>(effect, parameter, (Vector3)originalValue);
                    }
                    else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
                    {
                        binding = new ConstParameterBinding <Vector4>(effect, parameter, (Vector4)originalValue);
                    }
                }
                else if (parameter.ParameterClass == EffectParameterClass.Matrix &&
                         parameter.ParameterType == EffectParameterType.Single)
                {
                    // Matrix value.
                    binding = new ConstParameterBinding <Matrix>(effect, parameter, (Matrix)originalValue);
                }
                else if (parameter.ParameterClass == EffectParameterClass.Object)
                {
                    // Object values.
                    if (parameter.ParameterType == EffectParameterType.String)
                    {
                        binding = new ConstParameterBinding <string>(effect, parameter, (string)originalValue);
                    }
                    else if (parameter.ParameterType == EffectParameterType.Texture)
                    {
                        // A texture type but we are not sure which exact type. --> Try different types.
                        try
                        {
                            binding = new ConstParameterBinding <Texture2D>(effect, parameter, graphicsService.GetDefaultTexture2DWhite());
                        }
                        catch (Exception)
                        {
                            try
                            {
                                binding = new ConstParameterBinding <Texture3D>(effect, parameter, graphicsService.GetDefaultTexture3DWhite());
                            }
                            catch (Exception)
                            {
                                try
                                {
                                    binding = new ConstParameterBinding <TextureCube>(effect, parameter, graphicsService.GetDefaultTextureCubeWhite());
                                }
                                catch (Exception)
                                {
                                    // Default value for a parameter of type Texture could not be read from Effect.
                                }
                            }
                        }
                    }
                    else if (parameter.ParameterType == EffectParameterType.Texture1D)
                    {
                        // NOTE: 1D textures are not supported in XNA.
                    }
                    else if (parameter.ParameterType == EffectParameterType.Texture2D)
                    {
                        binding = new ConstParameterBinding <Texture2D>(effect, parameter, graphicsService.GetDefaultTexture2DWhite());
                    }
                    else if (parameter.ParameterType == EffectParameterType.Texture3D)
                    {
                        binding = new ConstParameterBinding <Texture3D>(effect, parameter, graphicsService.GetDefaultTexture3DWhite());
                    }
                    else if (parameter.ParameterType == EffectParameterType.TextureCube)
                    {
                        binding = new ConstParameterBinding <TextureCube>(effect, parameter, graphicsService.GetDefaultTextureCubeWhite());
                    }
                }
            }
            else
            {
                // ----- Parameter is array.
                int length = parameter.Elements.Count;
                Debug.Assert(length > 0, "Effect parameter should be an array.");

                // Note: In XNA originalValue is valid. In MonoGame originalValue is null and we have to
                // create a new array!

                if (parameter.ParameterClass == EffectParameterClass.Scalar)
                {
                    // Scalar value bindings.
                    if (parameter.ParameterType == EffectParameterType.Bool)
                    {
                        binding = new ConstParameterArrayBinding <bool>(effect, parameter, (bool[])originalValue ?? new bool[parameter.Elements.Count]);
                    }
                    else if (parameter.ParameterType == EffectParameterType.Int32)
                    {
                        binding = new ConstParameterArrayBinding <int>(effect, parameter, (int[])originalValue ?? new int[parameter.Elements.Count]);
                    }
                    else if (parameter.ParameterType == EffectParameterType.Single)
                    {
                        binding = new ConstParameterArrayBinding <float>(effect, parameter, (float[])originalValue ?? new float[parameter.Elements.Count]);
                    }
                }
                else if (parameter.ParameterClass == EffectParameterClass.Vector && parameter.ParameterType == EffectParameterType.Single)
                {
                    if (parameter.ColumnCount == 2 || parameter.RowCount == 2)
                    {
                        binding = new ConstParameterArrayBinding <Vector2>(effect, parameter, (Vector2[])originalValue ?? new Vector2[parameter.Elements.Count]);
                    }
                    else if (parameter.ColumnCount == 3 || parameter.RowCount == 3)
                    {
                        binding = new ConstParameterArrayBinding <Vector3>(effect, parameter, (Vector3[])originalValue ?? new Vector3[parameter.Elements.Count]);
                    }
                    else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
                    {
                        binding = new ConstParameterArrayBinding <Vector4>(effect, parameter, (Vector4[])originalValue ?? new Vector4[parameter.Elements.Count]);
                    }
                }
                else if (parameter.ParameterClass == EffectParameterClass.Matrix && parameter.ParameterType == EffectParameterType.Single)
                {
                    binding = new ConstParameterArrayBinding <Matrix>(effect, parameter, (Matrix[])originalValue ?? new Matrix[parameter.Elements.Count]);
                }
                else if (parameter.ParameterClass == EffectParameterClass.Object)
                {
                    // Note: Arrays of strings or textures are not supported in XNA.
                }
            }

            if (binding != null)
            {
                bindings.Add(binding);
            }
        }