コード例 #1
0
        public string Compile(List <HlslTreeNode> components, int promoteToVectorSize = PromoteToAnyVectorSize)
        {
            if (components.Count == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(components));
            }

            if (components.Count > 1)
            {
                IList <IList <HlslTreeNode> > componentGroups = _nodeGrouper.GroupComponents(components);
                if (componentGroups.Count > 1)
                {
                    return(CompileVectorConstructor(components, componentGroups));
                }

                var multiplication = _nodeGrouper.MatrixMultiplicationGrouper.TryGetMultiplicationGroup(components);
                if (multiplication != null)
                {
                    return(_matrixMultiplicationCompiler.Compile(multiplication));
                }

                var normalize = _nodeGrouper.NormalizeGrouper.TryGetContext(components);
                if (normalize != null)
                {
                    var vector = Compile(normalize);
                    return($"normalize({vector})");
                }
            }

            var first = components[0];

            if (first is ConstantNode)
            {
                return(CompileConstant(components, promoteToVectorSize));
            }

            if (first is Operation operation)
            {
                return(CompileOperation(operation, components, promoteToVectorSize));
            }

            if (first is IHasComponentIndex)
            {
                return(CompileNodesWithComponents(components, first, promoteToVectorSize));
            }

            if (first is GroupNode group)
            {
                return(Compile(group.Inputs));
            }

            throw new NotImplementedException();
        }
コード例 #2
0
        public string Compile(List <HlslTreeNode> components, int promoteToVectorSize = PromoteToAnyVectorSize)
        {
            if (components.Count == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(components));
            }

            if (components.Count == 1)
            {
                HlslTreeNode   singleComponent = components[0];
                HlslTreeNode[] vector          = _nodeGrouper.LengthGrouper.TryGetLengthContext(singleComponent);
                if (vector != null)
                {
                    string value = Compile(vector);
                    return($"length({value})");
                }

                DotProductContext dotProduct = _nodeGrouper.DotProductGrouper.TryGetDotProductGroup(singleComponent);
                if (dotProduct != null)
                {
                    string value1 = Compile(dotProduct.Value1);
                    string value2 = Compile(dotProduct.Value2);
                    return($"dot({value1}, {value2})");
                }
            }
            else
            {
                IList <IList <HlslTreeNode> > componentGroups = _nodeGrouper.GroupComponents(components);
                if (componentGroups.Count > 1)
                {
                    return(CompileVectorConstructor(components, componentGroups));
                }

                var multiplication = _nodeGrouper.MatrixMultiplicationGrouper.TryGetMultiplicationGroup(components);
                if (multiplication != null)
                {
                    return(_matrixMultiplicationCompiler.Compile(multiplication));
                }

                var normalize = _nodeGrouper.NormalizeGrouper.TryGetContext(components);
                if (normalize != null)
                {
                    var vector = Compile(normalize);
                    return($"normalize({vector})");
                }
            }

            var first = components[0];

            if (first is ConstantNode constant)
            {
                return(CompileConstant(components, promoteToVectorSize));
            }

            if (first is Operation operation)
            {
                return(CompileOperation(operation, components, promoteToVectorSize));
            }

            if (first is IHasComponentIndex component)
            {
                return(CompileNodesWithComponents(components, first, promoteToVectorSize));
            }

            throw new NotImplementedException();
        }