Exemplo n.º 1
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);
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 4
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));
            }