Example #1
0
        public static string Preprocess(
            string effectCode, string filePath, IDictionary<string, string> defines, List<string> dependencies,
            IEffectCompilerOutput output)
        {
            var fullPath = Path.GetFullPath(filePath);

            var pp = new CppNet.Preprocessor();

            pp.EmitExtraLineInfo = false;
            pp.addFeature(Feature.LINEMARKERS);
            pp.setListener(new MGErrorListener(output));
            pp.setFileSystem(new MGFileSystem(dependencies));
            pp.setQuoteIncludePath(new List<string> { Path.GetDirectoryName(fullPath) });

            foreach (var define in defines)
                pp.addMacro(define.Key, define.Value);

            pp.addInput(new MGStringLexerSource(effectCode, true, fullPath));

            var result = new StringBuilder();

            var endOfStream = false;
            while (!endOfStream)
            {
                var token = pp.token();
                switch (token.getType())
                {
                    case CppNet.Token.EOF:
                        endOfStream = true;
                        break;
                    case CppNet.Token.CPPCOMMENT:
                        break;
                    case CppNet.Token.CCOMMENT:
                    {
                        var tokenText = token.getText();
                        if (tokenText != null)
                        {
                            // Need to preserve line breaks so that line numbers are correct.
                            foreach (var c in tokenText)
                                if (c == '\n')
                                    result.Append(c);
                        }
                        break;
                    }
                    default:
                    {
                        var tokenText = token.getText();
                        if (tokenText != null)
                            result.Append(tokenText);
                        break;
                    }
                }
            }

            return result.ToString();
        }
Example #2
0
        public static string Preprocess(
            string effectCode, string filePath, IDictionary <string, string> defines, List <string> dependencies,
            IEffectCompilerOutput output)
        {
            var fullPath = Path.GetFullPath(filePath);

            var pp = new CppNet.Preprocessor();

            pp.EmitExtraLineInfo = false;
            pp.addFeature(Feature.LINEMARKERS);
            pp.setListener(new MGErrorListener(fullPath, output));
            pp.setFileSystem(new MGFileSystem(dependencies));
            pp.setQuoteIncludePath(new List <string> {
                Path.GetDirectoryName(fullPath)
            });

            foreach (var define in defines)
            {
                pp.addMacro(define.Key, define.Value);
            }

            pp.addInput(new StringLexerSource(effectCode, true, fullPath));

            var result = new StringBuilder();

            var endOfStream = false;

            while (!endOfStream)
            {
                var token = pp.token();
                switch (token.getType())
                {
                case CppNet.Token.EOF:
                    endOfStream = true;
                    break;

                case CppNet.Token.CCOMMENT:
                case CppNet.Token.CPPCOMMENT:
                    break;

                default:
                    var tokenText = token.getText();
                    if (tokenText != null)
                    {
                        result.Append(tokenText);
                    }
                    break;
                }
            }

            return(result.ToString());
        }
Example #3
0
        public static string Preprocess(
            string effectCode, string filePath, IDictionary <string, string> defines, List <string> dependencies,
            IEffectCompilerOutput output)
        {
            var fullPath = Path.GetFullPath(filePath);

            var pp = new CppNet.Preprocessor();

            pp.EmitExtraLineInfo = false;
            pp.addFeature(Feature.LINEMARKERS);
            pp.setListener(new MGErrorListener(output));
            pp.setFileSystem(new MGFileSystem(dependencies));
            pp.setQuoteIncludePath(new List <string> {
                Path.GetDirectoryName(fullPath)
            });

            foreach (var define in defines)
            {
                pp.addMacro(define.Key, define.Value);
            }

            effectCode = effectCode.Replace("#line", "//--WORKAROUND#line");

            pp.addInput(new MGStringLexerSource(effectCode, true, fullPath));

            var result = new StringBuilder();

            var endOfStream = false;

            while (!endOfStream)
            {
                var token = pp.token();
                switch (token.getType())
                {
                case CppNet.Token.EOF:
                    endOfStream = true;
                    break;

                case CppNet.Token.CPPCOMMENT:
                    if (token.getText().StartsWith("//--WORKAROUND#line"))
                    {
                        result.Append(token.getText().Replace("//--WORKAROUND#line", "#line"));
                    }
                    break;

                case CppNet.Token.CCOMMENT:
                {
                    var tokenText = token.getText();
                    if (tokenText != null)
                    {
                        // Need to preserve line breaks so that line numbers are correct.
                        foreach (var c in tokenText)
                        {
                            if (c == '\n')
                            {
                                result.Append(c);
                            }
                        }
                    }
                    break;
                }

                default:
                {
                    var tokenText = token.getText();
                    if (tokenText != null)
                    {
                        result.Append(tokenText);
                    }
                    break;
                }
                }
            }

            return(result.ToString());
        }
Example #4
0
        /// <summary>
        /// Preprocesses the provided shader or effect source.
        /// </summary>
        /// <param name="shaderSource">An array of bytes containing the raw source of the shader or effect to preprocess.</param>
        /// <param name="sourceFileName">Name of the source file.</param>
        /// <param name="defines">A set of macros to define during preprocessing.</param>
        /// <param name="includeDirectories">The include directories used by the preprocessor.</param>
        /// <returns>
        /// The preprocessed shader source.
        /// </returns>
        public static string Run(string shaderSource, string sourceFileName, ShaderMacro[] defines = null, params string[] includeDirectories)
        {
            var cpp = new Preprocessor();
            cpp.addFeature(Feature.DIGRAPHS);
            cpp.addWarning(Warning.IMPORT);
            cpp.addFeature(Feature.INCLUDENEXT);
            cpp.addFeature(Feature.LINEMARKERS);
            
            // TODO: Handle warning and errors properly instead of relying only on exception
            // Don't setup a listener and get any errors via exceptions
            cpp.setListener(new ErrorListener());

            // Pass defines
            if (defines != null)
            {
                foreach (var define in defines)
                {
                    if (!string.IsNullOrWhiteSpace(define.Name))
                    {
                        cpp.addMacro(define.Name, define.Definition ?? string.Empty);
                    }
                }
            }

            // Setup input directories.
            var tempDirectories = new List<string>() { Path.GetDirectoryName(sourceFileName) };
            tempDirectories.AddRange(includeDirectories);
            cpp.setQuoteIncludePath(tempDirectories);

            var inputSource = new StringLexerSource(shaderSource, true, sourceFileName);

            cpp.addInput(inputSource);

            var textBuilder = new StringBuilder();

            var isEndOfStream = false;
            while (!isEndOfStream)
            {
                Token tok = cpp.token();
                switch (tok.getType())
                {
                    case Token.EOF:
                        isEndOfStream = true;
                        break;
                    case Token.CCOMMENT:
                        var strComment = tok.getText() ?? string.Empty;
                        foreach (var commentChar in strComment)
                        {
                            textBuilder.Append(commentChar == '\n' ? '\n' : ' ');
                        }
                        break;
                    case Token.CPPCOMMENT:
                        break;
                    default:
                        var tokenText = tok.getText();
                        if (tokenText != null)
                        {
                            textBuilder.Append(tokenText);
                        }
                        break;
                }
            }

            return textBuilder.ToString();
        }