Example #1
0
 protected override void OnTranslate(ShaderTranslationContext sc, FieldDeclarationSyntax syntax, ScopeInfo scope)
 {
     // Does the language allow instanced constant buffers and does the field use a constant buffer struct type?
     if (sc.ConstantBuffers.ContainsKey(syntax.Declaration.Type.ToString()))
     {
         if (!sc.Language.InstancedConstantBuffers)
         {
             sc.CompleteSelfAndChildren(syntax);
         }
     }
     else
     {
         sc.CompleteSelfAndChildren(syntax.AttributeLists);
     }
 }
Example #2
0
        protected override void OnTranslate(ShaderTranslationContext sc, MethodDeclarationSyntax syntax, ScopeInfo scope)
        {
            MethodInfo info = sc.GetMethodInfo(syntax);

            if (info != null)
            {
                MappedEntryPoint ep = null;
                sc.EntryPointsByMethod.TryGetValue(info, out ep);

                sc.Source.AppendLineBreak();
                ScopeInfo mScope = sc.Source.OpenScope(ScopeType.Method, null, ep);
                mScope.Method = info;

                ShaderType returnType = ShaderType.TranslateType(sc, syntax.ReturnType.ToString());

                ep?.Translator?.TranslatePrefix(sc, info, syntax, ep);
                sc.Source.Append($"{returnType.Translation} {syntax.Identifier.ValueText}");

                sc.Complete(syntax.ReturnType);
                sc.Complete(syntax.ConstraintClauses);
                sc.Complete(syntax.AttributeLists);

                if (syntax.TypeParameterList != null)
                {
                    sc.CompleteSelfAndChildren(syntax.TypeParameterList);
                }

                // Translate parameters before method body.
                sc.Runner.Translate(sc, syntax.ParameterList, 0);
                ep?.Translator?.TranslatePostfix(sc, info, syntax, ep);
            }
        }
        protected override void OnTranslate(ShaderTranslationContext sc, StructDeclarationSyntax syntax, ScopeInfo scope)
        {
            string          typeName          = syntax.Identifier.ToString();
            string          headerTranslation = "";
            StructScopeType scopeType         = StructScopeType.Struct;
            Type            sType             = null;

            if (sc.ConstantBuffers.TryGetValue(typeName, out MappedConstantBuffer cMap))
            {
                sType = cMap.TypeInfo;
                IEnumerable <Attribute> cBufferAttributes = sType.GetCustomAttributes(false).Cast <Attribute>();
                sc.Language.TranslateConstBufferHeader(sc, syntax, cMap, cBufferAttributes);
                scopeType = StructScopeType.ConstantBuffer;
            }
            else if (sc.Structures.TryGetValue(typeName, out sType))
            {
                //IEnumerable<Attribute> cBufferAttributes = structInfo.GetCustomAttributes(false).Cast<Attribute>();
                // TODO Add translation of struct attributes (e.g. [InputStructure] or [OutputStructure]).

                sc.Source.AppendLineBreak();
                sc.Source.Append($"struct {syntax.Identifier}");
                scopeType = StructScopeType.Struct;
            }

            sc.Source.Append(headerTranslation);
            ScopeInfo structScope = sc.Source.OpenScope(ScopeType.Struct, sType);

            structScope.StructType = scopeType;

            sc.CompleteSelfAndChildren(syntax.AttributeLists);
        }
        protected override void OnTranslate(ShaderTranslationContext sc, MemberAccessExpressionSyntax syntax, ScopeInfo scope)
        {
            switch (syntax.Expression)
            {
            case IdentifierNameSyntax idSyntax:
                // Is this a static class identifier?
                // Static classes are abstract and sealed at IL level.
                Type targetType = ShaderType.Resolve(sc, idSyntax.Identifier.ValueText);
                if (targetType != null && targetType.IsClass && targetType.IsAbstract && targetType.IsSealed)
                {
                    // Is the member a constant value? If so, we can take it's value directly.
                    FieldInfo fInfo = targetType.GetField(syntax.Name.Identifier.ValueText);
                    if (fInfo != null && (fInfo.Attributes & FieldAttributes.Literal) == FieldAttributes.Literal)
                    {
                        object val = fInfo.GetValue(null);
                        if (val != null)
                        {
                            sc.Source.Append(val.ToString());
                            sc.CompleteChildren(syntax);
                            return;
                        }
                    }
                }
                else
                {
                    ScopeInfo cScope = scope.FindOfType(ScopeType.Class);
                    if (cScope == sc.Source.RootScope)
                    {
                        FieldInfo fInfo = cScope.TypeInfo.OriginalType.GetField(idSyntax.Identifier.ValueText);
                        if (fInfo != null && sc.ConstantBuffers.Values.Any(x => x.TypeInfo == fInfo.FieldType))
                        {
                            if (!sc.Language.InstancedConstantBuffers)
                            {
                                sc.CompleteSelfAndChildren(syntax.Expression);
                                sc.Runner.Translate(sc, syntax.Name);
                                return;
                            }
                        }
                    }
                }
                break;

            case ThisExpressionSyntax thisSyntax:
            case BaseExpressionSyntax baseSyntax:
                ScopeInfo pScope = scope.FindOfType(ScopeType.Class);
                if (pScope.TypeInfo.OriginalType == sc.ShaderType)
                {
                    sc.Complete(syntax.Expression);

                    // Are we translating a shader intrinsic method/function?
                    if (syntax.Name is IdentifierNameSyntax idSyntax && syntax.Parent is InvocationExpressionSyntax)
                    {
                        string translatedIntrinsic = ShaderType.GetIntrinsicTranslation(sc, idSyntax.Identifier.ValueText);
                        sc.Source.Append(translatedIntrinsic);
                        sc.Complete(syntax.Name);
                    }
                    return;
                }
                break;
            }

            sc.Runner.Translate(sc, syntax.Expression);
            sc.Source.Append(syntax.OperatorToken);
            sc.Runner.Translate(sc, syntax.Name);
        }
 protected override void OnTranslate(ShaderTranslationContext sc, TypeParameterConstraintClauseSyntax syntax, ScopeInfo scope)
 {
     sc.CompleteSelfAndChildren(syntax);
 }