Пример #1
0
 public NodeCompiler(RegisterState registers)
 {
     _registers                    = registers;
     _nodeGrouper                  = new NodeGrouper(registers);
     _constantCompiler             = new ConstantCompiler(_nodeGrouper);
     _matrixMultiplicationCompiler = new MatrixMultiplicationCompiler(this);
 }
Пример #2
0
        public HlslTreeNode[] TryGetContext(IList <HlslTreeNode> components)
        {
            var firstComponent = components[0];

            if (!(firstComponent is DivisionOperation firstDivision))
            {
                return(null);
            }

            var firstLengthContext = _nodeGrouper.LengthGrouper.TryGetLengthContext(firstDivision.Divisor);

            if (firstLengthContext == null)
            {
                return(null);
            }

            int dimension = firstLengthContext.Length;

            if (firstLengthContext.Any(c => NodeGrouper.AreNodesEquivalent(firstDivision.Dividend, c)) == false)
            {
                return(null);
            }

            for (int i = 1; i < dimension; i++)
            {
                if (i >= components.Count)
                {
                    return(null);
                }

                var nextComponent = components[i];
                if (!(nextComponent is DivisionOperation nextDivision))
                {
                    return(null);
                }

                if (NodeGrouper.AreNodesEquivalent(nextDivision.Divisor, firstDivision.Divisor) == false)
                {
                    return(null);
                }

                if (firstLengthContext.Any(c => NodeGrouper.AreNodesEquivalent(nextDivision.Dividend, c)) == false)
                {
                    return(null);
                }
            }

            return(components
                   .Take(dimension)
                   .Cast <DivisionOperation>()
                   .Select(c => c.Dividend)
                   .ToArray());
        }
Пример #3
0
        public string Compile(ConstantNode[] group)
        {
            ConstantNode first = group[0];

            int count = group.Length;

            if (count == 1)
            {
                return(CompileConstant(first));
            }

            if (group.All(c => NodeGrouper.AreNodesEquivalent(c, first)))
            {
                return(CompileConstant(first));
            }

            string components = string.Join(", ", group.Select(CompileConstant));

            return($"float{count}({components})");
        }
Пример #4
0
        public MatrixMultiplicationContext TryGetMultiplicationGroup(IList <HlslTreeNode> components)
        {
            const bool allowMatrix = true;

            var first = components[0];
            var firstDotProductNode = _nodeGrouper.DotProductGrouper.TryGetDotProductGroup(first, allowMatrix);

            if (firstDotProductNode == null)
            {
                return(null);
            }

            int dimension = firstDotProductNode.Dimension;

            if (components.Count < dimension)
            {
                return(null);
            }

            HlslTreeNode[] firstMatrixRow = TryGetMatrixRow(firstDotProductNode);
            if (firstMatrixRow == null)
            {
                return(null);
            }

            HlslTreeNode[] vector = firstDotProductNode.Value1 == firstMatrixRow
                                        ? firstDotProductNode.Value2
                                        : firstDotProductNode.Value1;

            var matrixRows = new HlslTreeNode[dimension][];

            matrixRows[0] = firstMatrixRow;
            for (int i = 1; i < dimension; i++)
            {
                var next           = components[i];
                var dotProductNode = _nodeGrouper.DotProductGrouper.TryGetDotProductGroup(next, dimension, allowMatrix);
                if (dotProductNode == null)
                {
                    return(null);
                }

                HlslTreeNode[] matrixRow = TryGetMatrixRow(dotProductNode);
                if (matrixRow == null)
                {
                    return(null);
                }
                matrixRows[i] = matrixRow;

                HlslTreeNode[] nextVector = dotProductNode.Value1 == matrixRow
                                        ? dotProductNode.Value2
                                        : dotProductNode.Value1;
                if (NodeGrouper.IsVectorEquivalent(vector, nextVector) == false)
                {
                    return(null);
                }
            }

            ConstantDeclaration matrix = TryGetMatrixDeclaration(matrixRows);

            if (matrix == null)
            {
                return(null);
            }

            bool matrixByVector = firstMatrixRow
                                  .Cast <RegisterInputNode>()
                                  .All(row => row.ComponentIndex == 0);

            SwizzleVector(vector, firstMatrixRow, matrixByVector);

            return(new MatrixMultiplicationContext(vector, matrix, matrixByVector));
        }
Пример #5
0
 public MatrixMultiplicationGrouper(NodeGrouper nodeGrouper, RegisterState registers)
 {
     _nodeGrouper = nodeGrouper;
     _registers   = registers;
 }
Пример #6
0
 public NormalizeGrouper(NodeGrouper nodeGrouper)
 {
     _nodeGrouper = nodeGrouper;
 }
Пример #7
0
 public DotProductGrouper(NodeGrouper nodeGrouper)
 {
     _nodeGrouper = nodeGrouper;
 }
Пример #8
0
 public ConstantCompiler(NodeGrouper nodeGrouper)
 {
     _nodeGrouper = nodeGrouper;
 }