예제 #1
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 ShaderClassType 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.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.ShaderGenerics.Count];
                    for (int i = 0; i < shaderClassSource.GenericArguments.Length; i++)
                    {
                        var variableGeneric = shaderClassType.ShaderGenerics[i];
                        shaderClassSource.GenericArguments[i] = GetDefaultConstValue(variableGeneric);
                    }
                }

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

                // check the name of the generics
                foreach (var generic in shaderClassType.ShaderGenerics)
                {
                    foreach (var genericCompare in shaderClassType.ShaderGenerics.Where(x => x != generic))
                    {
                        if (generic.Name.Text == genericCompare.Name.Text)
                        {
                            log.Error(ParadoxMessageCode.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.Name = new Identifier(className);
                }

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

            instantiator.Run();
        }
 public static void Instantiate(ShaderClassType classType, Dictionary<string, Expression> expressions, Dictionary<string, Identifier> identifiers, bool autoGenericInstances, LoggerResult log)
 {
     var instantiator = new ParadoxClassInstantiator(classType, expressions, identifiers, autoGenericInstances, log);
     instantiator.Run();
 }