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

            try
            {
                Stride.Core.Shaders.Parser.ShaderMacro[] macros;

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

                var parsingResult = StrideShaderParser.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());
        }
Example #2
0
        /// <summary>
        /// Generates the csharp code from a sdfx file.
        /// </summary>
        /// <param name="sdfxShaderCode">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 sdfxShaderCode, string filePath)
        {
            // In .sdfx, shader has been renamed to effect, in order to avoid ambiguities with HLSL and .sdsl
            var macros = new []
            {
                new Stride.Core.Shaders.Parser.ShaderMacro("shader", "effect")
            };

            // Compile
            var shader = StrideShaderParser.PreProcessAndParse(sdfxShaderCode, 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());
        }