Esempio n. 1
0
        static void GenerateDrawCalls(Compiler compiler, Method initMethod, Method freeMethod, DrawBlock drawBlock, HashSet <Scope> drawScopes)
        {
            foreach (var drawable in drawBlock.Drawables)
            {
                new ShaderGenerator(compiler, drawable, initMethod.Body, freeMethod.Body, drawBlock.DrawScope).Generate();
                FixedArrayProcessor.Process(compiler.Pass, drawable.DrawState.VertexShader);
                FixedArrayProcessor.Process(compiler.Pass, drawable.DrawState.PixelShader);
                VariableInliner.Process(compiler.Pass, drawable.DrawState.VertexShader);
                VariableInliner.Process(compiler.Pass, drawable.DrawState.PixelShader);
            }

            foreach (var drawable in drawBlock.Drawables)
            {
                drawBlock.DrawScope.Statements.Add(new Draw(drawable.Source, drawable.DrawState));
                drawScopes.Add(drawBlock.DrawScope);
            }
        }
Esempio n. 2
0
        public CodeActionEdit GetEdit(CancellationToken cancellationToken)
        {
            SyntaxNode     root  = (SyntaxNode)this.document.GetSyntaxRoot(cancellationToken);
            ISemanticModel model = this.document.GetSemanticModel(cancellationToken);

            ISymbol declaredSymbol = (Symbol)model.GetDeclaredSymbol(this.declaratorNode);

            // Get entire declaration
            VariableDeclarationSyntax variableDeclaration = (VariableDeclarationSyntax)this.declaratorNode.Parent;

            // Get declaration statement
            LocalDeclarationStatementSyntax declarationStatement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;

            // Get associated BlockSyntax the variable is declared inside
            BlockSyntax block = this.declaratorNode.FirstAncestorOrSelf <BlockSyntax>();

            // Get the declared type of expression
            TypeSyntax variableTypeNode = variableDeclaration.Type;

            bool           castInlinedExpression = false;
            CommonTypeInfo typeInfo       = model.GetTypeInfo(this.declaratorNode.Initializer.Value, cancellationToken);
            ITypeSymbol    expressionType = typeInfo.Type;
            ITypeSymbol    declaredType   = typeInfo.ConvertedType;

            // If the variable is declared with `var' keyword, there is no need to cast the initializer expression within replacement later on
            if (!variableTypeNode.IsVar)
            {
                // If expression's resultant type is different than declared one, cast is obligatory so as not to change type context
                // Considers:
                // float f = 5;
                // Expression type: System.Int32
                // Declared type: System.Single
                if (!expressionType.Equals(declaredType))
                {
                    castInlinedExpression = true;
                }
            }

            VariableInliner visitor  = new VariableInliner(model, declaredSymbol, this.declaratorNode, castInlinedExpression, declarationStatement);
            SyntaxNode      newBlock = visitor.Visit(block);

            SyntaxNode newRoot = root.ReplaceNode(block, newBlock);

            return(new CodeActionEdit(document.UpdateSyntaxRoot(newRoot)));
        }
Esempio n. 3
0
        public static void GenerateDrawCalls(Compiler compiler, DataType dt)
        {
            var initMethod = new Method(
                dt.Source, dt, null, Modifiers.Private | Modifiers.Generated, "init_DrawCalls",
                DataType.Void, ParameterList.Empty, new Scope(dt.Source));

            dt.Methods.Add(initMethod);

            foreach (var ctor in dt.Constructors)
            {
                if (ctor.Body == null)
                {
                    compiler.Log.Warning(ctor.Source, ErrorCode.I0000, "Can't call 'init_DrawCalls()' from bodyless constructor");
                }
                else
                {
                    ctor.Body.Statements.Add(new CallMethod(initMethod.Source, new This(initMethod.Source, ctor.DeclaringType), initMethod));
                }
            }

            var freeMethod = new Method(
                dt.Source, dt, null, Modifiers.Private | Modifiers.Generated, "free_DrawCalls",
                DataType.Void, ParameterList.Empty, new Scope(dt.Source));

            dt.Methods.Add(freeMethod);

            var drawScopes = new HashSet <Scope>();

            for (int m = 0, l = dt.Methods.Count; m < l; m++)
            {
                var drawMethod = dt.Methods[m];

                for (int i = 0; i < drawMethod.DrawBlocks.Count; i++)
                {
                    var drawBlock = drawMethod.DrawBlocks[i];

                    if (!DrawableFinder.VerifyCircularReferences(compiler, drawBlock, new List <Block>()))
                    {
                        continue;
                    }

                    int drawBlockIndex = i;
                    if (TrySplitVirtualAppliesInDrawBlockRecursive(compiler, drawBlock, ref drawBlockIndex, drawScopes))
                    {
                        var foundDrawables = false;

                        for (int j = i; j <= drawBlockIndex; j++)
                        {
                            var virtualBlock = drawMethod.DrawBlocks[j];
                            DrawableFinder.FindDrawables(compiler, virtualBlock, false);

                            if (!foundDrawables)
                            {
                                foundDrawables = virtualBlock.Drawables.Length > 0;
                            }
                        }

                        for (int j = i; j <= drawBlockIndex; j++)
                        {
                            var virtualBlock = drawMethod.DrawBlocks[j];

                            if (!foundDrawables)
                            {
                                DrawableFinder.FindDrawables(compiler, virtualBlock, true);
                            }

                            GenerateDrawCalls(compiler, initMethod, freeMethod, virtualBlock, drawScopes);
                        }

                        //compiler.Log.Message(drawBlock.Source, ErrorCode.M0000, "Created " + (drawBlockIndex + i + 1) + " draw blocks from virtual " + drawBlock.Quote());
                        i += drawBlockIndex - i;
                    }
                    else
                    {
                        DrawableFinder.FindDrawables(compiler, drawBlock, true);
                        GenerateDrawCalls(compiler, initMethod, freeMethod, drawBlock, drawScopes);
                    }
                }
            }

            FixedArrayProcessor.Process(compiler.Pass, dt, initMethod.Body, drawScopes);
            VariableInliner.Process(compiler.Pass, initMethod.Body);

            foreach (var s in drawScopes)
            {
                VariableInliner.Process(compiler.Pass, s);
            }

            foreach (var m in dt.Methods)
            {
                if (m.DrawBlocks.Count > 0)
                {
                    ScopeInliner.Process(compiler.Pass, m.Body, drawScopes);
                }
            }
        }