Esempio n. 1
0
        public static void Instantiate(ShaderClassType classType, Dictionary <string, Expression> expressions, Dictionary <string, Identifier> identifiers, bool autoGenericInstances, LoggerResult log)
        {
            var instantiator = new XenkoClassInstantiator(classType, expressions, identifiers, autoGenericInstances, log);

            instantiator.Run();
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the <see cref="ShaderClassType" />.
        /// </summary>
        /// <param name="shaderClassSource">The shader class source.</param>
        /// <param name="shaderMacros">The shader macros.</param>
        /// <param name="log">The log to output error logs.</param>
        /// <param name="autoGenericInstances"></param>
        /// <returns>A ShaderClassType or null if there was some errors.</returns>
        /// <exception cref="System.ArgumentNullException">shaderClassSource</exception>
        public LoadedShaderClassType LoadClassSource(ShaderClassSource shaderClassSource, SiliconStudio.Shaders.Parser.ShaderMacro[] shaderMacros, LoggerResult log, bool autoGenericInstances)
        {
            if (shaderClassSource == null)
            {
                throw new ArgumentNullException("shaderClassSource");
            }

            string generics = null;

            if (shaderClassSource.GenericArguments != null)
            {
                generics = "";
                foreach (var gen in shaderClassSource.GenericArguments)
                {
                    generics += "___" + gen;
                }
            }
            var shaderClassType = LoadShaderClass(shaderClassSource.ClassName, generics, log, shaderMacros);

            if (shaderClassType == null)
            {
                return(null);
            }

            // Instantiate generic class
            if (shaderClassSource.GenericArguments != null || (shaderClassType.Type.ShaderGenerics.Count > 0 && autoGenericInstances))
            {
                if (shaderClassType.IsInstanciated)
                {
                    return(shaderClassType);
                }

                // If we want to automatically generate a generic instance (in case we just want to parse and verify the generic)
                if (autoGenericInstances)
                {
                    shaderClassSource.GenericArguments = new string[shaderClassType.Type.ShaderGenerics.Count];
                    for (int i = 0; i < shaderClassSource.GenericArguments.Length; i++)
                    {
                        var variableGeneric = shaderClassType.Type.ShaderGenerics[i];
                        shaderClassSource.GenericArguments[i] = GetDefaultConstValue(variableGeneric);
                    }
                }

                if (shaderClassSource.GenericArguments.Length != shaderClassType.Type.ShaderGenerics.Count)
                {
                    log.Error(XenkoMessageCode.WrongGenericNumber, shaderClassType.Type.Span, shaderClassSource.ClassName);
                    return(null);
                }

                // check the name of the generics
                foreach (var generic in shaderClassType.Type.ShaderGenerics)
                {
                    foreach (var genericCompare in shaderClassType.Type.ShaderGenerics.Where(x => x != generic))
                    {
                        if (generic.Name.Text == genericCompare.Name.Text)
                        {
                            log.Error(XenkoMessageCode.SameNameGenerics, generic.Span, generic, genericCompare, shaderClassSource.ClassName);
                        }
                    }
                }

                if (log.HasErrors)
                {
                    return(null);
                }

                // When we use an actual generic instance, we replace the name with the name of the class + a hash of the generic parameters
                if (!autoGenericInstances)
                {
                    var className = GenerateGenericClassName(shaderClassSource);
                    shaderClassType.Type.Name = new Identifier(className);
                }

                var genericAssociation = CreateGenericAssociation(shaderClassType.Type.ShaderGenerics, shaderClassSource.GenericArguments);
                var identifierGenerics = GenerateIdentifierFromGenerics(genericAssociation);
                var expressionGenerics = GenerateGenericsExpressionValues(shaderClassType.Type.ShaderGenerics, shaderClassSource.GenericArguments);
                XenkoClassInstantiator.Instantiate(shaderClassType.Type, expressionGenerics, identifierGenerics, autoGenericInstances, log);
                shaderClassType.Type.ShaderGenerics.Clear();
                shaderClassType.IsInstanciated = true;
            }
            return(shaderClassType);
        }
 public static void Instantiate(ShaderClassType classType, Dictionary<string, Expression> expressions, Dictionary<string, Identifier> identifiers, bool autoGenericInstances, LoggerResult log)
 {
     var instantiator = new XenkoClassInstantiator(classType, expressions, identifiers, autoGenericInstances, log);
     instantiator.Run();
 }