/// <summary>
        /// Runs the code generation. Results is accessible from <see cref="ShaderWriter.Text"/> property.
        /// </summary>
        public override bool Run()
        {
            // If there are any errors, report them in the file as well
            // but return immediately as we can't really process the shader object
            if (logging.HasErrors)
            {
                LogErrors();
                return false;
            }

            // Add namespace for shader class type
            FixShaderClassTypeWithNoNameSpace();

            var blockVisitor = new ShaderBlockVisitor(this, logging);
            blockVisitor.Run(shader);

            // If there are any errors, generated by the visitor report them immediately
            if (logging.HasErrors)
            {
                LogErrors();
                return false;
            }

            WriteLine("// <auto-generated>");
            WriteLine("// Do not edit this file yourself!");
            WriteLine("//");
            WriteLine("// This code was generated by Xenko Shader Mixin Code Generator.");
            WriteLine("// To generate it yourself, please install SiliconStudio.Xenko.VisualStudio.Package .vsix");
            WriteLine("// and re-save the associated .xkfx.");
            WriteLine("// </auto-generated>");
            WriteLine();

            // No mixin found, just return
            if (!blockVisitor.HasMixin && !blockVisitor.HasShaderClassType)
            {
                WriteLine("// Nothing to generate");
                return true;
            }

            IsXkfx = blockVisitor.HasMixin;

            // Header of usings declaration
            // TODO: Should probably be better to use fully qualified name of types to avoid conflicts.

            WriteLine("using System;");
            WriteLine("using SiliconStudio.Core;");
            WriteLine("using SiliconStudio.Xenko.Rendering;");
            WriteLine("using SiliconStudio.Xenko.Graphics;");
            WriteLine("using SiliconStudio.Xenko.Shaders;");
            WriteLine("using SiliconStudio.Core.Mathematics;");
            WriteLine("using Buffer = SiliconStudio.Xenko.Graphics.Buffer;");
            WriteLine();

            // Visit the shader and generate the code
            VisitDynamic(shader);

            // If there are any errors log them into the shader
            if (logging.HasErrors)
            {
                LogErrors();
                return false;
            }

            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Runs the code generation. Results is accessible from <see cref="ShaderWriter.Text"/> property.
        /// </summary>
        public override bool Run()
        {
            // If there are any errors, report them in the file as well
            // but return immediately as we can't really process the shader object
            if (logging.HasErrors)
            {
                LogErrors();
                return(false);
            }

            // Add namespace for shader class type
            FixShaderClassTypeWithNoNameSpace();

            var blockVisitor = new ShaderBlockVisitor(this, logging);

            blockVisitor.Run(shader);

            // If there are any errors, generated by the visitor report them immediately
            if (logging.HasErrors)
            {
                LogErrors();
                return(false);
            }

            WriteLine("// <auto-generated>");
            WriteLine("// Do not edit this file yourself!");
            WriteLine("//");
            WriteLine("// This code was generated by Stride Shader Mixin Code Generator.");
            WriteLine("// To generate it yourself, please install Stride.VisualStudio.Package .vsix");
            WriteLine("// and re-save the associated .sdfx.");
            WriteLine("// </auto-generated>");
            WriteLine();

            // No mixin found, just return
            if (!blockVisitor.HasMixin && !blockVisitor.HasShaderClassType)
            {
                WriteLine("// Nothing to generate");
                return(true);
            }

            IsXkfx = blockVisitor.HasMixin;

            // Header of usings declaration
            // TODO: Should probably be better to use fully qualified name of types to avoid conflicts.

            WriteLine("using System;");
            WriteLine("using Stride.Core;");
            WriteLine("using Stride.Rendering;");
            WriteLine("using Stride.Graphics;");
            WriteLine("using Stride.Shaders;");
            WriteLine("using Stride.Core.Mathematics;");
            WriteLine("using Buffer = Stride.Graphics.Buffer;");
            WriteLine();

            // Visit the shader and generate the code
            VisitDynamic(shader);

            // If there are any errors log them into the shader
            if (logging.HasErrors)
            {
                LogErrors();
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        private async Task UpdateBuffersAsync(Document document, CancellationToken cancellationToken)
        {
            var shaderLabSyntaxTree = (SyntaxTree)await document.GetSyntaxTreeAsync(cancellationToken);

            var shaderBlockVisitor = new ShaderBlockVisitor(shaderLabSyntaxTree);

            shaderBlockVisitor.Visit((SyntaxNode)shaderLabSyntaxTree.Root);
            var shaderBlockSpans = shaderBlockVisitor.CgBlockSpans;

            var snapshot = document.SourceText.FindCorrespondingEditorTextSnapshot();

            var dataBufferSpans = new List <object>();

            var primaryIndex = 0;

            foreach (var shaderBlockSpan in shaderBlockSpans)
            {
                var primarySpan = Span.FromBounds(primaryIndex, shaderBlockSpan.Start);
                if (!primarySpan.IsEmpty)
                {
                    dataBufferSpans.Add(snapshot.CreateTrackingSpan(
                                            primarySpan,
                                            SpanTrackingMode.EdgeExclusive));
                }

                var elisionBuffer = _projectionBufferFactoryService.CreateElisionBuffer(
                    null,
                    new NormalizedSnapshotSpanCollection(new SnapshotSpan(snapshot, shaderBlockSpan)),
                    ElisionBufferOptions.None,
                    _hlslContentType);

                dataBufferSpans.Add(elisionBuffer.CurrentSnapshot.CreateTrackingSpan(
                                        0,
                                        elisionBuffer.CurrentSnapshot.Length,
                                        SpanTrackingMode.EdgeInclusive));

                primaryIndex = shaderBlockSpan.End;
            }

            // Last span.
            {
                var primarySpan = Span.FromBounds(primaryIndex, snapshot.Length);
                if (!primarySpan.IsEmpty)
                {
                    dataBufferSpans.Add(snapshot.CreateTrackingSpan(primarySpan, SpanTrackingMode.EdgeExclusive));
                }
            }

            // TODO: Make this a bit more type-safe.
            var viewBuffer = snapshot.TextBuffer.Properties.GetProperty <IProjectionBuffer>(typeof(IProjectionBuffer));

            _foregroundNotificationService.RegisterNotification(
                () =>
            {
                viewBuffer.ReplaceSpans(
                    0,
                    viewBuffer.CurrentSnapshot.SpanCount,
                    dataBufferSpans,
                    EditOptions.None,
                    null);
            },
                _listener.BeginAsyncOperation("ReplaceProjectionBufferSpans"),
                cancellationToken);
        }