public void GenerateDefaultConstructor(ShaderType owningType, StructDeclarationSyntax node)
        {
            if (owningType.mImplicitConstructor == null)
            {
                return;
            }

            var returnType           = FindType(typeof(void));
            var defaultConstructorFn = owningType.mImplicitConstructor;
            var thisType             = owningType.FindPointerType(StorageClass.Function);

            var thisOp = CreateOp(defaultConstructorFn.mParametersBlock, OpInstructionType.OpFunctionParameter, thisType, null);

            thisOp.DebugInfo.Name = "self";

            defaultConstructorFn.mBlocks.Add(new ShaderBlock());
            mContext.mCurrentFunction = defaultConstructorFn;
            mContext.mCurrentBlock    = defaultConstructorFn.mBlocks[0];
            mContext.mThisOp          = thisOp;

            var fnParamOps = new List <IShaderIR>();

            fnParamOps.Add(owningType.mPreConstructor);
            fnParamOps.Add(thisOp);
            CreateOp(mContext.mCurrentBlock, OpInstructionType.OpFunctionCall, returnType, fnParamOps);

            mContext.mThisOp          = null;
            mContext.mCurrentBlock    = null;
            mContext.mCurrentFunction = null;
        }
Esempio n. 2
0
        void GenerateIds(ShaderType shaderType)
        {
            if (mIdMap.ContainsKey(shaderType))
            {
                return;
            }

            // SpirV only allows one instance of a primitive type, but for convenience we allow multiple
            // distinct types that share the same underlying primitive type (e.g. Vector4, Quaternion).
            // To handle this, we dedupe the types to the same underlying id.
            if (shaderType.IsPrimitiveType())
            {
                if (TryDedupePrimitiveType(shaderType))
                {
                    return;
                }
            }

            GetId(shaderType);
            if (shaderType.StorageClassCollection != null)
            {
                GetId(shaderType.FindPointerType(StorageClass.Function));
            }
            foreach (var param in shaderType.mParameters)
            {
                GenerateIds(param);
            }
            foreach (var field in shaderType.mFields)
            {
                GetId(field.mType);
            }
        }
Esempio n. 3
0
        public GlobalShaderField CreateStaticField(ShaderType owningType, ShaderType fieldType, string name, IShaderIR initializerExpression)
        {
            // Static fields are a special storage class. Get the target storage class from the field type. If the field type is a special storage class then use that instead of private.
            var storageClass = StorageClass.Private;

            if (fieldType.mStorageClass == StorageClass.Uniform || fieldType.mStorageClass == StorageClass.UniformConstant)
            {
                storageClass = fieldType.mStorageClass;
            }

            // Also create the pointer type if needed.
            var staticFieldType = fieldType.FindPointerType(storageClass);

            if (staticFieldType == null)
            {
                staticFieldType = CreateType(fieldType, storageClass, true);
            }

            var shaderField = new GlobalShaderField();

            shaderField.mMeta          = new ShaderFieldMeta();
            shaderField.mType          = staticFieldType;
            shaderField.mMeta.mName    = name;
            shaderField.DebugInfo.Name = name;
            owningType.mStaticFields.Add(shaderField);

            shaderField.InstanceOp             = new ShaderOp();
            shaderField.InstanceOp.mOpType     = OpInstructionType.OpVariable;
            shaderField.InstanceOp.mResultType = staticFieldType;
            shaderField.InstanceOp.mParameters.Add(staticFieldType);
            mCurrentLibrary.mStaticGlobals.Add(shaderField, shaderField);
            return(shaderField);
        }
Esempio n. 4
0
        public ShaderOp CreateOpVariable(ShaderType resultType, FrontEndContext context)
        {
            var variableOp = CreateOp(OpInstructionType.OpVariable, resultType.FindPointerType(resultType.mStorageClass), null);

            context.mCurrentFunction.mBlocks[0].mLocalVariables.Add(variableOp);
            return(variableOp);
        }
Esempio n. 5
0
        public void GeneratePreConstructor(INamedTypeSymbol structSymbol, ShaderType owningType)
        {
            // The preconstructor is an instance function that returns void and takes no args
            var preconstructorName = "PreConstructor";
            var returnType         = FindType(typeof(void));
            var thisType           = owningType.FindPointerType(StorageClass.Function);

            owningType.mPreConstructor = mFrontEnd.CreateFunctionAndType(owningType, returnType, preconstructorName, thisType, null);
        }
Esempio n. 6
0
        public ShaderType FindOrCreatePointerType(ShaderType baseType, StorageClass storageClass)
        {
            var pointerType = baseType.FindPointerType(storageClass);

            if (pointerType != null)
            {
                return(pointerType);
            }

            pointerType                = new ShaderType();
            pointerType.mBaseType      = OpType.Pointer;
            pointerType.DebugInfo.Name = "";
            pointerType.mStorageClass  = storageClass;
            baseType.StorageClassCollection.AddPointerType(storageClass, pointerType);
            return(pointerType);
        }
Esempio n. 7
0
        public void GenerateDefaultConstructor(INamedTypeSymbol structSymbol, ShaderType owningType)
        {
            // Only generate a default constructor if needed
            var defaultConstructorSymbol = FindDefaultConstructor(structSymbol);

            if (defaultConstructorSymbol == null)
            {
                return;
            }

            var constructorName = "DefaultConstructor";
            var returnType      = FindType(typeof(void));
            var thisType        = owningType.FindPointerType(StorageClass.Function);

            owningType.mImplicitConstructor = mFrontEnd.CreateFunctionAndType(owningType, returnType, constructorName, thisType, null);
            mFrontEnd.mCurrentLibrary.AddFunction(new FunctionKey(defaultConstructorSymbol), owningType.mImplicitConstructor);
        }
Esempio n. 8
0
        public static ShaderFunction GenerateFunction_ShaderTypeParam(FrontEndTranslator translator, ShaderType shaderType, string functionName, out ShaderOp thisOp)
        {
            var library     = translator.mCurrentLibrary;
            var voidType    = library.FindType(new TypeKey(typeof(void)));
            var thisType    = shaderType.FindPointerType(StorageClass.Function);
            var fnArgsTypes = new List <ShaderType>()
            {
                thisType
            };

            var function = translator.CreateFunctionAndType(shaderType, voidType, functionName, null, fnArgsTypes);

            thisOp = translator.CreateOp(function.mParametersBlock, OpInstructionType.OpFunctionParameter, thisType, null);
            thisOp.DebugInfo.Name = "self";
            function.mBlocks.Add(new ShaderBlock());
            return(function);
        }
        public void GeneratePreConstructor(ShaderType owningType, StructDeclarationSyntax node)
        {
            if (owningType.mPreConstructor == null)
            {
                return;
            }

            var preConstuctorFn = owningType.mPreConstructor;
            var thisType        = owningType.FindPointerType(StorageClass.Function);

            var thisOp = CreateOp(preConstuctorFn.mParametersBlock, OpInstructionType.OpFunctionParameter, thisType, null);

            thisOp.DebugInfo.Name = "self";
            preConstuctorFn.mBlocks.Add(new ShaderBlock());

            mContext.mCurrentFunction = preConstuctorFn;
            mContext.mCurrentBlock    = preConstuctorFn.mBlocks[0];
            mContext.mThisOp          = thisOp;
            base.VisitStructDeclaration(node);
            mContext.mThisOp          = null;
            mContext.mCurrentBlock    = null;
            mContext.mCurrentFunction = null;
        }
Esempio n. 10
0
 static public ShaderType GetPointerType(ShaderType shaderType)
 {
     return(shaderType.FindPointerType(StorageClass.Function));
 }