Ejemplo n.º 1
0
        private static void RebuildFile(string filePath)
        {
            try
            {
                var source  = File.ReadAllText(filePath);
                var content = ShaderMixinCodeGen.GenerateCsharp(source, filePath);

                // Sometimes, we have a collision with the .cs file, so generated filename might be postfixed with 1
                var destPath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "1.cs");
                if (!File.Exists(destPath))
                {
                    destPath = Path.ChangeExtension(filePath, ".cs");
                }
                if (!File.Exists(destPath))
                {
                    Console.WriteLine("Target file {0} doesn't exist", destPath);
                    return;
                }
                File.WriteAllText(destPath, content, Encoding.UTF8);
                Console.WriteLine("File generated {0}", filePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unexpected error {0}: {1}", filePath, ex);
            }
        }
Ejemplo n.º 2
0
        public override void SaveGeneratedAsset(AssetItem assetItem)
        {
            //generate the .cs files
            // Always output a result into the file
            string result;

            try
            {
                var parsingResult = StrideShaderParser.TryPreProcessAndParse(Text, assetItem.FullPath);

                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);

            File.WriteAllBytes(assetItem.GetGeneratedAbsolutePath(), data);
        }
Ejemplo n.º 3
0
        private static void ConvertXkfxToCSharp(string sdfxfile)
        {
            var sdfileContent = File.ReadAllText(sdfxfile);
            var result        = ShaderMixinCodeGen.GenerateCsharp(sdfileContent, sdfxfile);

            File.WriteAllText(Path.ChangeExtension(sdfxfile, ".cs"), result, Encoding.UTF8);
        }
Ejemplo n.º 4
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());
        }
        public static byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            // Always output a result into the file
            string result;

            try
            {
                SiliconStudio.Shaders.Parser.ShaderMacro[] macros;

                // Changed some keywords to avoid ambiguities with HLSL and improve consistency
                if (inputFileName != null && Path.GetExtension(inputFileName).ToLowerInvariant() == ".xkfx")
                {
                    // XKFX
                    macros = new[]
                    {
                        new SiliconStudio.Shaders.Parser.ShaderMacro("shader", "effect")
                    };
                }
                else
                {
                    // XKSL
                    macros = new[]
                    {
                        new SiliconStudio.Shaders.Parser.ShaderMacro("class", "shader")
                    };
                }

                var parsingResult = XenkoShaderParser.TryPreProcessAndParse(inputFileContent, inputFileName, macros);

                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.º 6
0
 private static void RebuildFile(string filePath)
 {
     try
     {
         var source   = File.ReadAllText(filePath);
         var content  = ShaderMixinCodeGen.GenerateCsharp(source, filePath.Replace("C:", "D:"));
         var destPath = Path.ChangeExtension(filePath, ".cs");
         File.WriteAllText(destPath, content, Encoding.UTF8);
         Console.WriteLine("File generated {0}", filePath);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Unexpected error {0}: {1}", filePath, ex);
     }
 }
Ejemplo n.º 7
0
        public override void Save(Stream stream)
        {
            //regex the class name if it has changed
            var className = new UFile(AbsoluteSourceLocation).GetFileName();

            Text = Regex.Replace(Text, $"class {className}");

            var buffer = Encoding.UTF8.GetBytes(Text);

            stream.Write(buffer, 0, buffer.Length);

            //generate the .cs files
            // Always output a result into the file
            string result;

            try
            {
                var parsingResult = XenkoShaderParser.TryPreProcessAndParse(Text, AbsoluteSourceLocation);

                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);

            File.WriteAllBytes(GeneratedAbsolutePath, data);
        }
            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.º 9
0
 //[Fact]
 public void Test()
 {
     var filePath = @"D:\Code\Xenko\sources\engine\Xenko.Shaders.Tests\GameAssets\Mixins\A.xksl";
     var source   = File.ReadAllText(filePath);
     var content  = ShaderMixinCodeGen.GenerateCsharp(source, filePath.Replace("C:", "D:"));
 }
Ejemplo n.º 10
0
 //[Test]
 public void Test()
 {
     var filePath = @"D:\Code\Paradox\sources\engine\SiliconStudio.Paradox.Shaders.Tests\GameAssets\Mixins\A.pdxsl";
     var source   = File.ReadAllText(filePath);
     var content  = ShaderMixinCodeGen.GenerateCsharp(source, filePath.Replace("C:", "D:"));
 }