Beispiel #1
0
        public override void VisitVariableDeclaration(VariableDeclarationSyntax node)
        {
            if (node.Variables.Count != 1)
            {
                throw new ShaderGenerationException("Cannot declare multiple variables together.");
            }

            VariableDeclaratorSyntax vds = node.Variables[0];

            string             resourceName = vds.Identifier.Text;
            TypeInfo           typeInfo     = GetModel(node).GetTypeInfo(node.Type);
            string             fullTypeName = GetModel(node).GetFullTypeName(node.Type);
            TypeReference      valueType    = new TypeReference(fullTypeName, typeInfo.Type);
            ShaderResourceKind kind         = ClassifyResourceKind(fullTypeName);

            var structure = _backends.Select(b => b.GetContext(_shaderSet.Name).Structures
                                             .FirstOrDefault(s => SymbolEqualityComparer.Default.Equals(s.Type, typeInfo.Type)))
                            .FirstOrDefault();

            if (structure != null && structure.Fields.Any(f => f.IsBuiltIn))
            {
                kind = ShaderResourceKind.BuiltIn;
            }

            ShaderBuiltin builtin = default;

            if (kind == ShaderResourceKind.Uniform)
            {
                if (node.Parent is FieldDeclarationSyntax field)
                {
                    if (field.Modifiers.Any(f => f.IsKind(SyntaxKind.PrivateKeyword)))
                    {
                        kind = ShaderResourceKind.Local;
                    }
                    else if (field.AttributeLists.Any(l => l.Attributes.Any(a => a.Name.ToString().Contains("Semantic"))))
                    {
                        kind = ShaderResourceKind.BuiltIn;
                        SemanticType semanticType = GetSemanticType(vds);
                        if (semanticType == SemanticType.None)
                        {
                            var geometrySemantic = GetGeometrySemantic(vds);
                            if (geometrySemantic != GeometrySemantic.None)
                            {
                                builtin = new ShaderBuiltin(geometrySemantic);
                            }
                        }
                        else
                        {
                            builtin = new ShaderBuiltin(semanticType);
                        }
                    }
                }
                else if (typeInfo.Type.GetAttributes().Any(a => a.AttributeClass.Name.Contains("Semantic")))
                {
                    kind = ShaderResourceKind.BuiltIn;
                }
            }

            if (kind == ShaderResourceKind.StructuredBuffer ||
                kind == ShaderResourceKind.RWStructuredBuffer ||
                kind == ShaderResourceKind.RWTexture2D ||
                valueType.Name.Contains(nameof(UniformBuffer <int>)))
            {
                valueType = ParseElementType(vds);
            }

            if (node.Parent is FieldDeclarationSyntax)
            {
                var arraySize = node.Parent.DescendantNodes().OfType <AttributeSyntax>().FirstOrDefault(
                    attrSyntax => attrSyntax.Name.ToString().EndsWith("ArraySize"));
                if (arraySize != null)
                {
                    var fixedSize = (int)GetModel(node).GetConstantValue(arraySize.ArgumentList.Arguments.First().Expression).Value;
                    valueType = new TypeReference(valueType.Name, valueType.TypeInfo, fixedSize);
                }

                var emitAttribute = node.Parent.DescendantNodes().OfType <AttributeSyntax>().FirstOrDefault(
                    attrSyntax => attrSyntax.Name.ToString().EndsWith("EmitVertex"));
                if (emitAttribute != null)
                {
                    kind = ShaderResourceKind.Emit;
                }
            }

            ResourceDefinition rd;

            if (kind == ShaderResourceKind.Local)
            {
                rd = new ResourceDefinition(resourceName, 0, 0, valueType, kind);
            }
            else
            {
                int set = 0; // Default value if not otherwise specified.
                if (GetResourceDecl(node, out AttributeSyntax resourceSetDecl))
                {
                    set = GetAttributeArgumentIntValue(resourceSetDecl, 0, GetModel(node));
                }

                int resourceBinding = GetAndIncrementBinding(set);

                if (kind == ShaderResourceKind.Uniform)
                {
                    ValidateUniformType(typeInfo);
                }
                rd = new ResourceDefinition(resourceName, set, resourceBinding, valueType, kind);
            }

            rd.Semantic = builtin.Semantic;

            foreach (LanguageBackend b in _backends)
            {
                b.AddResource(_shaderSet.Name, rd);
            }
        }
Beispiel #2
0
 protected virtual string SemanticIdentifier(ShaderBuiltin builtin)
 {
     return(null);
 }