Ejemplo n.º 1
0
        public static byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            // Always output a result into the file
            string result;

            try
            {
                var parsingResult = ParadoxShaderParser.TryPreProcessAndParse(inputFileContent, inputFileName);

                if (parsingResult.HasErrors)
                {
                    result = "// Failed to parse the shader:\n" + parsingResult;
                }
                else
                {
                    // Try to generate a mixin code.
                    var shaderKeyGenerator = new ShaderMixinCodeGen(parsingResult.Shader, parsingResult);

                    shaderKeyGenerator.Run();
                    result = shaderKeyGenerator.Text ?? string.Empty;
                }
            }
            catch (Exception ex)
            {
                result = "// Unexpected exceptions occurred while generating the file\n" + ex;
            }

            // We force the UTF8 to include the BOM to match VS default
            var data = Encoding.UTF8.GetBytes(result);

            return(Encoding.UTF8.GetPreamble().Concat(data).ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the csharp code from a pdxfx file.
        /// </summary>
        /// <param name="pdxfxShaderCode">The PDXFX shader code.</param>
        /// <param name="filePath">The file path.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static string GenerateCsharp(string pdxfxShaderCode, string filePath)
        {
            // Compile
            var shader = ParadoxShaderParser.PreProcessAndParse(pdxfxShaderCode, filePath);

            // Try to generate a mixin code.
            var loggerResult       = new LoggerResult();
            var shaderKeyGenerator = new ShaderMixinCodeGen(shader, loggerResult);

            if (shaderKeyGenerator.Run())
            {
                return(shaderKeyGenerator.Text);
            }
            throw new InvalidOperationException(loggerResult.ToString());
        }
Ejemplo n.º 3
0
        public ShaderClassType ParseSource(string shaderSource, LoggerResult log)
        {
            var parsingResult = ParadoxShaderParser.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);
        }
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                var logger = commandContext.Logger;

                var status = ResultStatus.Successful;

                try
                {
                    var parsingResults = ParadoxShaderParser.TryPreProcessAndParse(asset.Text, sourceLocationOnDisk);
                    if (parsingResults.HasErrors)
                    {
                        foreach (var message in parsingResults.Messages)
                        {
                            if (message.Level == ReportMessageLevel.Error)
                            {
                                logger.Error(message.ToString());
                            }
                        }
                        return(Task.FromResult(ResultStatus.Failed));
                    }

                    var shader       = parsingResults.Shader;
                    var loggerResult = new LoggerResult();

                    // Run shader codegen mixin in order to check that everything is well defined and compiled
                    var shaderMixinCodeGen = new ShaderMixinCodeGen(shader, loggerResult);
                    shaderMixinCodeGen.Run();
                }
                catch (Exception ex)
                {
                    commandContext.Logger.Error("Error while processing pdxfx [{0}]", ex, sourceLocationOnDisk);
                    status = ResultStatus.Failed;
                }

                return(Task.FromResult(status));
            }
Ejemplo n.º 5
0
        private ShaderClassType LoadShaderClass(string type, string generics, LoggerResult log, SiliconStudio.Shaders.Parser.ShaderMacro[] macros = null, HashSet <string> modifiedShaders = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var shaderSourceKey = new ShaderSourceKey(type, generics, macros);

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

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

                // Load file
                var shaderSource = SourceManager.LoadShaderSource(type, modifiedShaders);

                // TODO USE ORIGINAL SOURCE PATH and not to object database path
                var preprocessedSource = PreProcessor.Run(shaderSource.Source, shaderSource.Path, macros);

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

                // Compile
                var parsingResult = ParadoxShaderParser.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 pdxsl.
                var shaderClassTypes = GetShaderClassTypes(shader.Declarations).ToList();
                if (shaderClassTypes.Count != 1)
                {
                    throw new InvalidOperationException(string.Format("Shader [{0}] must contain only a single Shader class type intead of [{1}]", type, shaderClassTypes.Count));
                }

                shaderClass                        = 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 (shaderClass.Name.Text != type)
                {
                    throw new InvalidOperationException(string.Format("Unable to load shader [{0}] not maching class name [{1}]", type, shaderClass.Name.Text));
                }

                // Only full version are stored
                loadedShaders.Add(shaderSourceKey, shaderClass);

                return(shaderClass);
            }
        }
Ejemplo n.º 6
0
 public void Initialize()
 {
     ParadoxShaderParser.Initialize();
 }
Ejemplo n.º 7
0
        private ShaderClassType 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
                ShaderClassType 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.ASCII.GetBytes(preprocessedSource);
                var    hashPreprocessSource = ObjectId.FromBytes(byteArray);

                // Compile
                var parsingResult = ParadoxShaderParser.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 pdxsl.
                var shaderClassTypes = ParadoxShaderParser.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(ParadoxMessageCode.ShaderMustContainSingleClassDeclaration, sourceSpan, type);
                    return(null);
                }

                shaderClass                        = 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.Name.Text != type)
                {
                    log.Error(ParadoxMessageCode.FileNameNotMatchingClassName, shaderClass.Name.Span, type, shaderClass.Name.Text);
                    return(null);
                }

                loadedShaders.Add(shaderSourceKey, shaderClass);

                return(shaderClass);
            }
        }
Ejemplo n.º 8
0
 public void Initialize(string paradoxSdkDir)
 {
     DirectoryHelper.packageDirectoryOverride = paradoxSdkDir;
     ParadoxShaderParser.Initialize();
 }