public static byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            // Always output a result into the file
            string result;
            try
            {
                var parsingResult = XenkoShaderParser.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 override void SaveGeneratedAsset(AssetItem assetItem)
        {
            //generate the .cs files
            // Always output a result into the file
            string result;
            try
            {
                var parsingResult = XenkoShaderParser.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);
        }
        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();
        }
Exemple #4
0
        /// <summary>
        /// Generates the csharp code from a xkfx file.
        /// </summary>
        /// <param name="xkfxShaderCode">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 xkfxShaderCode, string filePath)
        {
            // Compile
            var shader = XenkoShaderParser.PreProcessAndParse(xkfxShaderCode, 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());
        }
        /// <summary>
        /// Generates the csharp code from a xkfx file.
        /// </summary>
        /// <param name="xkfxShaderCode">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 xkfxShaderCode, string filePath)
        {
            // Compile
            var shader = XenkoShaderParser.PreProcessAndParse(xkfxShaderCode, 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());
        }
Exemple #6
0
        /// <summary>
        /// Generates the csharp code from a xkfx file.
        /// </summary>
        /// <param name="xkfxShaderCode">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 xkfxShaderCode, string filePath)
        {
            // In .xkfx, shader has been renamed to effect, in order to avoid ambiguities with HLSL and .xksl
            var macros = new []
            {
                new SiliconStudio.Shaders.Parser.ShaderMacro("shader", "effect")
            };

            // Compile
            var shader = XenkoShaderParser.PreProcessAndParse(xkfxShaderCode, filePath, macros);

            // 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());
        }
        /// <summary>
        /// Generates the csharp code from a xkfx file.
        /// </summary>
        /// <param name="xkfxShaderCode">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 xkfxShaderCode, string filePath)
        {
            // In .xkfx, shader has been renamed to effect, in order to avoid ambiguities with HLSL and .xksl
            var macros = new []
            {
                new SiliconStudio.Shaders.Parser.ShaderMacro("shader", "effect")
            };

            // Compile
            var shader = XenkoShaderParser.PreProcessAndParse(xkfxShaderCode, filePath, macros);

            // 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());
        }