/// <inheritdoc/>
        protected override void CloneCore(EffectParameterBinding source)
        {
            // Clone EffectParameterBinding properties.
            base.CloneCore(source);

            Initialize();
        }
Exemple #2
0
    internal static Vector4 GetColor4(EffectParameterBinding binding)
    {
      var binding4 = binding as EffectParameterBinding<Vector4>;
      if (binding4 != null)
        return binding4.Value;

      var binding4F = binding as EffectParameterBinding<Vector4>;
      if (binding4F != null)
        return (Vector4)binding4F.Value;

      var binding3 = binding as EffectParameterBinding<Vector3>;
      if (binding3 != null)
        return new Vector4(binding3.Value, 1);

      var binding3F = binding as EffectParameterBinding<Vector3>;
      if (binding3F != null)
        return new Vector4((Vector3)binding3F.Value, 1);

      var bindingQ = binding as EffectParameterBinding<Quaternion>;
      if (bindingQ != null)
        return ToVector(bindingQ.Value);

      var bindingQF = binding as EffectParameterBinding<Quaternion>;
      if (bindingQF != null)
        return ToVector(bindingQF.Value);

      return new Vector4(1, 1, 1, 1);
    }
        /// <inheritdoc/>
        protected override void CloneCore(EffectParameterBinding source)
        {
            // Clone EffectParameterBinding<T> properties.
            base.CloneCore(source);

            // Clone ConstParameterBinding<T> properties.
            var sourceTyped = (ConstParameterBinding <T>)source;

            Value = sourceTyped.Value;
        }
        /// <inheritdoc/>
        protected override void CloneCore(EffectParameterBinding source)
        {
            // Clone EffectParameterBinding properties.
            base.CloneCore(source);

            // Clone EffectParameterArrayBinding<T> properties.
            var sourceTyped = (EffectParameterArrayBinding <T>)source;

            Values = (T[])sourceTyped.Values.Clone();
        }
Exemple #5
0
        /// <inheritdoc/>
        protected override void CloneCore(EffectParameterBinding source)
        {
            // Clone EffectParameterBinding properties.
            base.CloneCore(source);

            // Clone SkeletonPoseParameterBinding properties.
            var sourceTyped = (SkeletonPoseParameterBinding)source;

            Value = sourceTyped.Value;
        }
        /// <inheritdoc/>
        protected override void CloneCore(EffectParameterBinding source)
        {
            // Clone EffectParameterBinding<T> properties.
            base.CloneCore(source);

            // Clone DelegateParameterBinding<T> properties.
            var sourceTyped = (DelegateParameterBinding <T>)source;

            ComputeParameter = sourceTyped.ComputeParameter;
        }
 // ReSharper disable UnusedParameter.Local
 private void CheckHint(EffectParameterBinding binding)
 {
     if ((Hints & binding.Description.Hint) == 0)
     {
         string message = string.Format(
             CultureInfo.InvariantCulture,
             "Cannot set binding for effect parameter. The effect binding does not supported parameter that have the sort hint \"{0}\".",
             binding.Description.Hint);
         throw new ArgumentException(message);
     }
 }
Exemple #8
0
    internal static Vector3 GetColor3(EffectParameterBinding binding)
    {
      var binding3 = binding as EffectParameterBinding<Vector3>;
      if (binding3 != null)
        return binding3.Value;

      var binding3F = binding as EffectParameterBinding<Vector3>;
      if (binding3F != null)
        return (Vector3)binding3F.Value;

      return new Vector3(1, 1, 1);
    }
    private static bool SetAutomaticBinding(Effect effect, EffectParameter parameter, IList<IEffectBinder> binders, IDictionary<string, object> opaqueData, EffectParameterBindingCollection bindings)
    {
      Debug.Assert(!bindings.Contains(parameter), "Effect binding already contains a binding for the given effect parameter.");

      // Loop through all IEffectParameterBinders and try to setup a valid binding.
      int numberOfBinders = binders.Count;
      for (int i = 0; i < numberOfBinders; i++)
      {
        EffectParameterBinding binding = binders[i].GetBinding(effect, parameter, opaqueData);
        if (binding != null)
        {
          bindings.Add(binding);
          return true;
        }
      }

      return false;
    }
Exemple #10
0
 protected virtual void CloneCore(EffectParameterBinding source)
 {
     Description = source.Description;
 }
    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);
    }
    private static bool SetOpaqueDataBinding(Effect effect, EffectParameter parameter, IDictionary<string, object> opaqueData, EffectParameterBindingCollection bindings)
    {
      Debug.Assert(!bindings.Contains(parameter), "Effect binding already contains a binding for the given effect parameter.");

      if (opaqueData == null || opaqueData.Count == 0)
      {
        // No opaque data.
        return false;
      }

      if (parameter.ParameterClass == EffectParameterClass.Struct)
      {
        // Structs cannot be set from opaque data.
        return false;
      }

      // Get value from opaque data.
      object value;
      bool valueFound = opaqueData.TryGetValue(parameter.Name, out value);
      if (!valueFound)
        return false;

      EffectParameterBinding binding = null;
      if (parameter.Elements.Count == 0)
      {
        // ----- Parameter is not an array.
        if (parameter.ParameterClass == EffectParameterClass.Scalar)
        {
          // Scalar value bindings.
          if (parameter.ParameterType == EffectParameterType.Bool && ObjectHelper.IsConvertible(value))
            binding = new ConstParameterBinding<bool>(effect, parameter,  ObjectHelper.ConvertTo<bool>(value, CultureInfo.InvariantCulture));
          else if (parameter.ParameterType == EffectParameterType.Int32 && ObjectHelper.IsConvertible(value))
            binding = new ConstParameterBinding<int>(effect, parameter, ObjectHelper.ConvertTo<int>(value, CultureInfo.InvariantCulture));
          else if (parameter.ParameterType == EffectParameterType.Single && ObjectHelper.IsConvertible(value))
            binding = new ConstParameterBinding<float>(effect, parameter, ObjectHelper.ConvertTo<float>(value, CultureInfo.InvariantCulture));
        }
        else if (parameter.ParameterClass == EffectParameterClass.Vector && parameter.ParameterType == EffectParameterType.Single)
        {
          if (parameter.ColumnCount == 2 || parameter.RowCount == 2)
          {
            if (value is Vector2)
              binding = new ConstParameterBinding<Vector2>(effect, parameter, (Vector2)value);
            else if (value is Vector2F)
              binding = new ConstParameterBinding<Vector2F>(effect, parameter, (Vector2F)value);
          }
          else if (parameter.ColumnCount == 3 || parameter.RowCount == 3)
          {
            if (value is Vector3)
              binding = new ConstParameterBinding<Vector3>(effect, parameter, (Vector3)value);
            else if (value is Vector3)
              binding = new ConstParameterBinding<Vector3>(effect, parameter, (Vector3)value);
          }
          else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
          {
            if (value is Vector4)
              binding = new ConstParameterBinding<Vector4>(effect, parameter, (Vector4)value);
            else if (value is Vector4)
              binding = new ConstParameterBinding<Vector4>(effect, parameter, (Vector4)value);
          }
        }
        else if (parameter.ParameterClass == EffectParameterClass.Matrix && parameter.ParameterType == EffectParameterType.Single)
        {
          if (parameter.ColumnCount == 2 && parameter.RowCount == 2)
          {
            if (value is Matrix22F)
              binding = new ConstParameterBinding<Matrix22F>(effect, parameter, (Matrix22F)value);
          }
          else if (parameter.ColumnCount == 3 && parameter.RowCount == 3)
          {
            if (value is Matrix)
              binding = new ConstParameterBinding<Matrix>(effect, parameter, (Matrix)value);
          }
          else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
          {
            if (value is Matrix)
              binding = new ConstParameterBinding<Matrix>(effect, parameter, (Matrix)value);
            else if (value is Matrix)
              binding = new ConstParameterBinding<Matrix>(effect, parameter, (Matrix)value);
          }
        }
        else if (parameter.ParameterClass == EffectParameterClass.Object)
        {
          if (parameter.ParameterType == EffectParameterType.String && value is string)
            binding = new ConstParameterBinding<string>(effect, parameter, (string)value);
          else if (parameter.ParameterType == EffectParameterType.Texture && value is Texture)
            binding = new ConstParameterBinding<Texture>(effect, parameter, (Texture)value);
          else if (parameter.ParameterType == EffectParameterType.Texture1D && value is Texture)
            binding = null; // 1D textures are not supported in XNA.
          else if (parameter.ParameterType == EffectParameterType.Texture2D && value is Texture2D)
            binding = new ConstParameterBinding<Texture2D>(effect, parameter, (Texture2D)value);
          else if (parameter.ParameterType == EffectParameterType.Texture3D && value is Texture3D)
            binding = new ConstParameterBinding<Texture3D>(effect, parameter, (Texture3D)value);
          else if (parameter.ParameterType == EffectParameterType.TextureCube && value is TextureCube)
            binding = new ConstParameterBinding<TextureCube>(effect, parameter, (TextureCube)value);
        }
      }
      else
      {
        // ----- Parameter is array.
        // TODO: We could also check whether the length of the arrays match.
        if (parameter.ParameterClass == EffectParameterClass.Scalar)
        {
          // Scalar value bindings.
          if (parameter.ParameterType == EffectParameterType.Bool && IsArray<bool>(value))
            binding = new ConstParameterArrayBinding<bool>(effect, parameter, ToBooleanArray(value));
          else if (parameter.ParameterType == EffectParameterType.Int32 && IsArray<int>(value))
            binding = new ConstParameterArrayBinding<int>(effect, parameter, ToInt32Array(value));
          else if (parameter.ParameterType == EffectParameterType.Single && IsArray<float>(value))
            binding = new ConstParameterArrayBinding<float>(effect, parameter, ToSingleArray(value));
        }
        else if (parameter.ParameterClass == EffectParameterClass.Vector &&
                 parameter.ParameterType == EffectParameterType.Single)
        {
          if (parameter.ColumnCount == 2 || parameter.RowCount == 2)
          {
            if (value is Vector2[])
              binding = new ConstParameterArrayBinding<Vector2>(effect, parameter, (Vector2[])value);
            else if (value is Vector2F[])
              binding = new ConstParameterArrayBinding<Vector2F>(effect, parameter, (Vector2F[])value);
          }
          else if (parameter.ColumnCount == 3 || parameter.RowCount == 3)
          {
            if (value is Vector3[])
              binding = new ConstParameterArrayBinding<Vector3>(effect, parameter, (Vector3[])value);
            else if (value is Vector3[])
              binding = new ConstParameterArrayBinding<Vector3>(effect, parameter, (Vector3[])value);
          }
          else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
          {
            if (value is Vector4[])
              binding = new ConstParameterArrayBinding<Vector4>(effect, parameter, (Vector4[])value);
            else if (value is Vector4[])
              binding = new ConstParameterArrayBinding<Vector4>(effect, parameter, (Vector4[])value);
          }
        }
        else if (parameter.ParameterClass == EffectParameterClass.Matrix &&
                 parameter.ParameterType == EffectParameterType.Single)
        {
          //#if !XBOX
          // The type Matrix22F[], Matrix[], Matrix[] caused a MissingMethodException at runtime on Xbox 360 in XNA 3.1.
          // See also: EffectParameterBinding<T>.SetValueMethods!
          if (parameter.ColumnCount == 2 && parameter.RowCount == 2)
          {
            if (value is Matrix22F[])
              binding = new ConstParameterArrayBinding<Matrix22F>(effect, parameter, (Matrix22F[])value);
          }
          else if (parameter.ColumnCount == 3 && parameter.RowCount == 3)
          {
            if (value is Matrix[])
              binding = new ConstParameterArrayBinding<Matrix>(effect, parameter, (Matrix[])value);
          }
          else
            //#endif
            if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
            {
              if (value is Matrix[])
                binding = new ConstParameterArrayBinding<Matrix>(effect, parameter, (Matrix[])value);
              //#if !XBOX
              else if (value is Matrix[])
                binding = new ConstParameterArrayBinding<Matrix>(effect, parameter, (Matrix[])value);
              //#endif
            }
        }
        else if (parameter.ParameterClass == EffectParameterClass.Object)
        {
          if (parameter.ParameterType == EffectParameterType.String && value is string[])
            binding = new ConstParameterArrayBinding<string>(effect, parameter, (string[])value);

          // Note: Arrays of textures are not supported in DirectX 9.
        }
      }

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