Esempio n. 1
0
        /// <summary>
        /// Called when the effect technique and parameter bindings should be initialized.
        /// </summary>
        /// <param name="graphicsService">The graphics service.</param>
        /// <param name="opaqueData">The opaque data. Can be <see langword="null"/>.</param>
        /// <remarks>
        /// <para>
        /// <strong>Notes to Inheritors:</strong> Derived classes can override this method to create
        /// custom parameter bindings. If the derived class does not initialize all parameter bindings
        /// then it should call the base implementation of <see cref="OnInitializeBindings"/> to
        /// initialize the remaining bindings.
        /// </para>
        /// <para>
        /// The method is called by the constructor of the base class. This means that derived classes
        /// may not be initialized yet!
        /// </para>
        /// </remarks>
        protected virtual void OnInitializeBindings(IGraphicsService graphicsService, IDictionary <string, object> opaqueData)
        {
            if (TechniqueBinding == null)
            {
                TechniqueBinding = EffectEx.TechniqueBinding.Clone();
            }

            EffectHelper.InitializeParameterBindings(graphicsService, EffectEx, opaqueData, ParameterBindings);
        }
        /// <inheritdoc/>
        public virtual EffectParameterDescription GetDescription(Effect effect, EffectParameter parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            EffectParameterDescription description = null;

            // First, try to get usage from annotation string.
#if !MONOGAME
            var annotation = parameter.Annotations["Semantic"];
            if (annotation != null && annotation.ParameterType == EffectParameterType.String)
            {
                description = GetDescriptionFromString(parameter, annotation.GetValueString());
            }
#endif

            if (description == null)
            {
                // No annotation.
                // --> Try to get usage from semantic.
                description = GetDescriptionFromString(parameter, parameter.Semantic);
            }

            if (description == null)
            {
                // No annotation, no semantic.
                // --> Try to get usage from parameter name.
                // Check whether string matches entry in dictionary.
                description = GetDescriptionFromString(parameter, parameter.Name);
            }

            if (description == null)
            {
                // Too bad, better luck next time.
                return(null);
            }

            // Get the effect parameter hint from annotations.
            var hint = EffectHelper.GetHintFromAnnotations(parameter);
            if (hint.HasValue)
            {
                // User-defined hint found in effect file.
                // --> Override default.
                description.Hint = hint.Value;
            }

            return(description);
        }
        /// <summary>
        /// Interprets the specified effect parameter and adds an
        /// <see cref="EffectParameterDescription"/> to the collection.
        /// </summary>
        /// <param name="effect">The effect.</param>
        /// <param name="parameter">The effect parameter.</param>
        /// <param name="interpreters">The effect interpreters.</param>
        private void InterpretParameter(Effect effect, EffectParameter parameter, EffectInterpreterCollection interpreters)
        {
            Debug.Assert(!Contains(parameter), "Effect binding already contains a description for the given effect parameter.");

            // Try all interpreters to find a description.
            foreach (var interpreter in interpreters)
            {
                var description = interpreter.GetDescription(effect, parameter);
                if (description != null)
                {
                    Add(description);
                    return;
                }
            }

            // No description found. If the parameter is a struct or array of structs
            // we check the structure members.
            if (parameter.ParameterClass == EffectParameterClass.Struct)
            {
                if (parameter.Elements.Count > 0)
                {
                    // Effect parameter is an array of structs.
                    foreach (EffectParameter element in parameter.Elements)
                    {
                        foreach (EffectParameter member in element.StructureMembers)
                        {
                            InterpretParameter(effect, member, interpreters);
                        }
                    }
                }
                else
                {
                    // Effect parameter is a struct.
                    foreach (EffectParameter member in parameter.StructureMembers)
                    {
                        InterpretParameter(effect, member, interpreters);
                    }
                }
            }
            else
            {
                // No description found and this parameter is no struct.
                // Try to get hint from annotations and create default description (= "Unknown usage").
                var hint = EffectHelper.GetHintFromAnnotations(parameter) ?? EffectParameterHint.Material;
                Add(new EffectParameterDescription(parameter, null, 0, hint));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes the <see cref="EffectEx"/>.
        /// </summary>
        /// <param name="graphicsService">The graphics service.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="graphicsService"/> is <see langword="null"/>.
        /// </exception>
        protected override void Initialize(IGraphicsService graphicsService)
        {
            if (graphicsService == null)
            {
                throw new ArgumentNullException("graphicsService");
            }

            var effect = Resource;

            // When an Effect is used the original values of the effect parameters, as
            // specified in the .fx file, are lost. --> Store values in dictionary.
            OriginalParameterValues = EffectHelper.GetParameterValues(effect);

            TechniqueDescriptions = new EffectTechniqueDescriptionCollection(graphicsService, effect);
            TechniqueBinding      = EffectHelper.CreateTechniqueBinding(graphicsService, effect);

            ParameterDescriptions = new EffectParameterDescriptionCollection(graphicsService, effect);
            ParameterBindings     = new EffectParameterBindingCollection(EffectParameterHint.Any);
            EffectHelper.InitializeParameterBindings(graphicsService, this, null, ParameterBindings);
        }