Пример #1
0
        public static ShaderClassType ParseSource(string shaderSource, LoggerResult log)
        {
            var parsingResult = XenkoShaderParser.TryParse(shaderSource, null);

            parsingResult.CopyTo(log);

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

            var shader      = parsingResult.Shader;
            var shaderClass = shader.Declarations.OfType <ShaderClassType>().SingleOrDefault();

            if (shaderClass == null)
            {
                var queue = new Queue <NamespaceBlock>(shader.Declarations.OfType <NamespaceBlock>());
                while (queue.Count > 0 && shaderClass == null)
                {
                    var namespaceNode = queue.Dequeue();
                    shaderClass = namespaceNode.Body.OfType <ShaderClassType>().SingleOrDefault();
                    foreach (var subNamespace in namespaceNode.Body.OfType <NamespaceBlock>())
                    {
                        queue.Enqueue(subNamespace);
                    }
                }
            }
            return(shaderClass);
        }
Пример #2
0
        public static ShaderClassType ParseSource(string shaderSource, LoggerResult log)
        {
            var parsingResult = XenkoShaderParser.TryParse(shaderSource, null);

            parsingResult.CopyTo(log);

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

            var shader      = parsingResult.Shader;
            var shaderClass = shader.Declarations.OfType <ShaderClassType>().Single();

            return(shaderClass);
        }
Пример #3
0
        public static ShaderClassType ParseSource(string shaderSource, LoggerResult log)
        {
            var parsingResult = XenkoShaderParser.TryParse(shaderSource, null);

            parsingResult.CopyTo(log);

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

            var shader      = parsingResult.Shader;
            var shaderClass = shader.Declarations.OfType <ShaderClassType>().Single();

            shaderClass.SourcePath             = null;           // shaderSource.Path;
            shaderClass.SourceHash             = ObjectId.Empty; // shaderSource.Hash;
            shaderClass.PreprocessedSourceHash = ObjectId.Empty; // hashPreprocessSource;
            shaderClass.IsInstanciated         = false;

            return(shaderClass);
        }
Пример #4
0
        private LoadedShaderClassType LoadShaderClass(ShaderClassSource classSource, string generics, LoggerResult log, SiliconStudio.Shaders.Parser.ShaderMacro[] macros = null)
        {
            var type = classSource.ClassName;

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var shaderSourceKey = new ShaderSourceKey(type, generics, macros);

            lock (loadedShaders)
            {
                // Already instantiated
                LoadedShaderClassType shaderClass;

                if (loadedShaders.TryGetValue(shaderSourceKey, out shaderClass))
                {
                    return(shaderClass);
                }

                // Load file
                var    shaderSource = SourceManager.LoadShaderSource(type);
                string preprocessedSource;
                try
                {
                    preprocessedSource = PreProcessor.Run(shaderSource.Source, shaderSource.Path, macros);
                }
                catch (Exception ex)
                {
                    log.Error(MessageCode.ErrorUnexpectedException, new SourceSpan(new SourceLocation(shaderSource.Path, 0, 1, 1), 1), ex);
                    return(null);
                }

                byte[] byteArray            = Encoding.UTF8.GetBytes(preprocessedSource);
                var    hashPreprocessSource = ObjectId.FromBytes(byteArray);

                // Compile
                var parsingResult = XenkoShaderParser.TryParse(preprocessedSource, shaderSource.Path);
                parsingResult.CopyTo(log);

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

                var shader = parsingResult.Shader;

                // As shaders can be embedded in namespaces, get only the shader class and make sure there is only one in a xksl.
                var shaderClassTypes = XenkoShaderParser.GetShaderClassTypes(shader.Declarations).ToList();
                if (shaderClassTypes.Count != 1)
                {
                    var sourceSpan = new SourceSpan(new SourceLocation(shaderSource.Path, 0, 0, 0), 1);
                    if (shaderClassTypes.Count > 1)
                    {
                        sourceSpan = shaderClassTypes[1].Span;
                    }
                    log.Error(XenkoMessageCode.ShaderMustContainSingleClassDeclaration, sourceSpan, type);
                    return(null);
                }

                shaderClass                        = new LoadedShaderClassType();
                shaderClass.Type                   = shaderClassTypes.First();
                shaderClass.SourcePath             = shaderSource.Path;
                shaderClass.SourceHash             = shaderSource.Hash;
                shaderClass.PreprocessedSourceHash = hashPreprocessSource;
                shaderClass.IsInstanciated         = false;

                // TODO: We should not use Console. Change the way we log things here
                // Console.WriteLine("Loading Shader {0}{1}", type, macros != null && macros.Length > 0 ? String.Format("<{0}>", string.Join(", ", macros)) : string.Empty);

                // If the file name is not matching the class name, provide an error
                if (shaderClass.Type.Name.Text != type)
                {
                    log.Error(XenkoMessageCode.FileNameNotMatchingClassName, shaderClass.Type.Name.Span, type, shaderClass.Type.Name.Text);
                    return(null);
                }

                loadedShaders.Add(shaderSourceKey, shaderClass);

                return(shaderClass);
            }
        }