Exemple #1
0
        public ShaderOp CreateOp(OpInstructionType opType, ShaderType resultType, List <IShaderIR> arguments)
        {
            var resultOp = new ShaderOp();

            InitializeOp(resultOp, opType, resultType, arguments);
            return(resultOp);
        }
Exemple #2
0
        public ShaderOp CreateConstant(ConstantOpKey key)
        {
            ShaderOp op = new ShaderOp();

            mConstantOps.Add(key, op);
            return(op);
        }
Exemple #3
0
        public void DefaultConstructType(INamedTypeSymbol typeSymbol, ShaderOp variableOp)
        {
            var defaultConstructorSymbol = FindDefaultConstructorSymbol(typeSymbol);

            if (defaultConstructorSymbol == null)
            {
                return;
            }

            var constructorKey = new FunctionKey(defaultConstructorSymbol);
            // If this is an intrinsic (defined by the compiler and not user defined) then invoke it.
            var constructorIntrinsic = mFrontEnd.mCurrentLibrary.FindIntrinsicFunction(constructorKey);

            if (constructorIntrinsic != null)
            {
                constructorIntrinsic(mFrontEnd, new List <IShaderIR>(), mContext);
                mFrontEnd.CreateStoreOp(mContext.mCurrentBlock, variableOp, mContext.Pop());
                return;
            }
            // Otherwise see if this is user defined
            var constructorFn = mFrontEnd.mCurrentLibrary.FindFunction(constructorKey);

            if (constructorFn != null)
            {
                mFrontEnd.GenerateFunctionCall(constructorFn, variableOp, null, mContext);
                return;
            }

            var derefType    = variableOp.mResultType.GetDereferenceType();
            var defaultValue = mFrontEnd.DefaultConstructPrimitive(derefType, mContext);

            mFrontEnd.CreateStoreOp(mContext.mCurrentBlock, variableOp, defaultValue);
        }
Exemple #4
0
 public ShaderOp GetOwnerInstance(FrontEndTranslator translator, ShaderOp ownerOp, FrontEndContext context)
 {
     if (GetOwnerDelegate == null)
     {
         return(ownerOp);
     }
     return(GetOwnerDelegate(translator, this, ownerOp, context));
 }
Exemple #5
0
 private bool IsConstantOp(ShaderOp shaderOp)
 {
     return(shaderOp.mOpType == OpInstructionType.OpConstant ||
            shaderOp.mOpType == OpInstructionType.OpConstantComposite ||
            shaderOp.mOpType == OpInstructionType.OpConstantTrue ||
            shaderOp.mOpType == OpInstructionType.OpConstantFalse ||
            shaderOp.mOpType == OpInstructionType.OpConstantSampler ||
            shaderOp.mOpType == OpInstructionType.OpConstantNull);
 }
        void WriteGlobal(ShaderOp constantOp)
        {
            mWriter.WriteInstruction((UInt16)(4), Spv.Op.OpVariable);
            mWriter.Write(GetId(constantOp.mResultType));
            mWriter.Write(GetId(constantOp));
            var spirvStorageClass = ConvertStorageClass(constantOp.mResultType.mStorageClass);

            mWriter.Write((UInt32)spirvStorageClass);
        }
Exemple #7
0
        public static void DecorateUniforms(FrontEndTranslator translator, ShaderOp instanceOp, ShaderBlock decorationsBlock)
        {
            var instanceType = instanceOp.mResultType.GetDereferenceType();

            AddDecorationDescriptorSet(translator, instanceType, 0, decorationsBlock);
            AddDecorationBinding(translator, instanceType, 0, decorationsBlock);
            AddDecorationBlock(translator, instanceType, decorationsBlock);
            DecorateOffsets(translator, instanceType, decorationsBlock);
        }
Exemple #8
0
        public ShaderOp CreateVariable(ShaderType fieldType)
        {
            var op = new ShaderOp();

            op.mOpType     = OpInstructionType.OpVariable;
            op.mResultType = fieldType;
            op.mParameters.Add(fieldType);
            return(op);
        }
Exemple #9
0
 public void InitializeOp(ShaderOp op, OpInstructionType opType, ShaderType resultType, List <IShaderIR> arguments)
 {
     op.mOpType     = opType;
     op.mResultType = resultType;
     if (arguments != null)
     {
         op.mParameters.AddRange(arguments);
     }
 }
Exemple #10
0
        public static void AddDecorationBuiltIn(FrontEndTranslator translator, ShaderOp instanceOp, Spv.BuiltIn builtInType, ShaderBlock decorationsBlock)
        {
            var decorationBuiltInLiteral = translator.CreateConstantLiteral((int)Spv.Decoration.DecorationBuiltIn);
            var builtInTypeLiteral       = translator.CreateConstantLiteral((int)builtInType);

            translator.CreateOp(decorationsBlock, OpInstructionType.OpDecorate, null, new List <IShaderIR>()
            {
                instanceOp, decorationBuiltInLiteral, builtInTypeLiteral
            });
        }
Exemple #11
0
        public static void AddDecorationLocation(FrontEndTranslator translator, ShaderOp instanceOp, int location, ShaderBlock decorationsBlock)
        {
            var decorationLocationLiteral = translator.CreateConstantLiteral((int)Spv.Decoration.DecorationLocation);
            var locationLiteral           = translator.CreateConstantLiteral(location);

            translator.CreateOp(decorationsBlock, OpInstructionType.OpDecorate, null, new List <IShaderIR>()
            {
                instanceOp, decorationLocationLiteral, locationLiteral
            });
        }
 void WriteConstantOrGlobal(ShaderOp op)
 {
     if (op.mOpType == OpInstructionType.OpVariable)
     {
         WriteGlobal(op);
     }
     else
     {
         WriteConstant(op);
     }
 }
Exemple #13
0
 void GenerateIds(ShaderOp op)
 {
     if (op.mResultType != null)
     {
         GetId(op.mResultType);
     }
     GetId(op);
     foreach (var arg in op.mParameters)
     {
         GenerateIds(arg);
     }
 }
Exemple #14
0
        public void Visit(ShaderOp shaderOp)
        {
            Visit(shaderOp.mResultType);
            if (IsConstantOp(shaderOp))
            {
                mReferencedTypesConstantsAndGlobals.Add(shaderOp);
            }

            foreach (var param in shaderOp.mParameters)
            {
                Visit(param);
            }
        }
        void WriteBasicOpNoResultId(ShaderOp op, Spv.Op spvOpType)
        {
            UInt16 totalSize = (UInt16)(1 + op.mParameters.Count);

            if (op.mResultType != null)
            {
                ++totalSize;
            }
            mWriter.WriteInstruction(totalSize, spvOpType);
            if (op.mResultType != null)
            {
                mWriter.Write(GetId(op.mResultType));
            }
            WriteArgs(op.mParameters);
        }
Exemple #16
0
        public static EntryPointFunction GenerateEntryPointCopyFunction(FrontEndTranslator translator, ShaderType shaderType, string functionName)
        {
            ShaderOp thisOp         = null;
            var      shaderFunction = GenerateFunction_ShaderTypeParam(translator, shaderType, functionName, out thisOp);
            var      context        = new FrontEndContext();

            context.mCurrentBlock    = shaderFunction.mBlocks[0];
            context.mCurrentFunction = shaderFunction;

            var entryPointFunction = new EntryPointFunction();

            entryPointFunction.Context              = context;
            entryPointFunction.ShaderFunction       = shaderFunction;
            entryPointFunction.ShaderTypeInstanceOp = thisOp;
            return(entryPointFunction);
        }
Exemple #17
0
        public static void CopyInterfaceFields(FrontEndTranslator translator, ShaderOp thisOp, ShaderInterfaceSet interfaceSet, InterfaceFieldCopyMode copyMode, FrontEndContext context)
        {
            foreach (var interfaceField in interfaceSet)
            {
                var interfaceInstance = interfaceSet.GetFieldInstance(translator, interfaceField, context);
                if (interfaceInstance == null)
                {
                    continue;
                }

                var shaderFieldInstance = translator.GenerateAccessChain(thisOp, interfaceField.ShaderField.mMeta.mName, context);
                if (copyMode == InterfaceFieldCopyMode.Input)
                {
                    translator.CreateStoreOp(context.mCurrentBlock, shaderFieldInstance, interfaceInstance);
                }
                else
                {
                    translator.CreateStoreOp(context.mCurrentBlock, interfaceInstance, shaderFieldInstance);
                }
            }
        }
Exemple #18
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);
        }
Exemple #19
0
 public ShaderType GetOpResultType(ShaderOp op)
 {
     return(op.mResultType);
 }
 void WriteConstant(ShaderOp constantOp)
 {
     WriteOp(constantOp);
 }
        void WriteOp(ShaderOp op)
        {
            switch (op.mOpType)
            {
            case OpInstructionType.OpStore:
                WriteBasicOpNoResultId(op, Spv.Op.OpStore);
                break;

            case OpInstructionType.OpVariable:
                mWriter.WriteInstruction((UInt16)(4 + op.mParameters.Count), Spv.Op.OpVariable);
                mWriter.Write(GetId(op.mResultType));
                mWriter.Write(GetId(op));
                mWriter.Write((UInt32)Spv.StorageClass.StorageClassFunction);
                break;

            case OpInstructionType.OpReturn:
                mWriter.WriteInstruction(1, Spv.Op.OpReturn);
                break;

            case OpInstructionType.OpReturnValue:
                mWriter.WriteInstruction(2, Spv.Op.OpReturnValue, GetId(op.mParameters[0]));
                break;

            case OpInstructionType.OpUnreachable:
                mWriter.WriteInstruction(1, Spv.Op.OpUnreachable);
                break;

            case OpInstructionType.OpExecutionMode:
                WriteBasicOpNoResultId(op, Spv.Op.OpExecutionMode);
                break;

            case OpInstructionType.OpDecorate:
                WriteBasicOpNoResultId(op, Spv.Op.OpDecorate);
                break;

            case OpInstructionType.OpMemberDecorate:
                WriteBasicOpNoResultId(op, Spv.Op.OpMemberDecorate);
                break;

            case OpInstructionType.OpConstant:
                mWriter.WriteInstruction(4, Spv.Op.OpConstant);
                var constantLiteral = op.mParameters[0] as ShaderConstantLiteral;
                mWriter.Write(GetId(op.mResultType));
                mWriter.Write(GetId(op));
                if (constantLiteral.mValue is bool boolVal)
                {
                    mWriter.Write(boolVal ? 1 : 0);
                }
                else if (constantLiteral.mValue is int intVal)
                {
                    mWriter.Write(intVal);
                }
                else if (constantLiteral.mValue is uint uintVal)
                {
                    mWriter.Write(uintVal);
                }
                else if (constantLiteral.mValue is float floatVal)
                {
                    byte[] raw        = BitConverter.GetBytes(floatVal);
                    int    floatAsInt = BitConverter.ToInt32(raw, 0);
                    mWriter.Write(floatAsInt);
                }
                else
                {
                    throw new Exception();
                }

                break;

            case OpInstructionType.OpFunctionCall:
            {
                UInt16 instructionCount = (UInt16)(3 + op.mParameters.Count);
                mWriter.WriteInstruction(instructionCount, Spv.Op.OpFunctionCall);
                mWriter.Write(GetId(op.mResultType));
                mWriter.Write(GetId(op));
                WriteArgs(op.mParameters);
                break;
            }

            case OpInstructionType.OpBranch:
                mWriter.WriteInstruction((UInt16)2, Spv.Op.OpBranch);
                WriteArgs(op.mParameters);
                break;

            case OpInstructionType.OpBranchConditional:
            {
                UInt16 instructionCount = (UInt16)(1 + op.mParameters.Count);
                mWriter.WriteInstruction(instructionCount, Spv.Op.OpBranchConditional);
                WriteArgs(op.mParameters);
                break;
            }

            case OpInstructionType.OpLoopMerge:
            {
                UInt16 instructionCount = (UInt16)(1 + op.mParameters.Count);
                mWriter.WriteInstruction(instructionCount, Spv.Op.OpLoopMerge);
                WriteArgs(op.mParameters);
                break;
            }

            case OpInstructionType.OpSelectionMerge:
            {
                UInt16 instructionCount = (UInt16)(1 + op.mParameters.Count);
                mWriter.WriteInstruction(instructionCount, Spv.Op.OpSelectionMerge);
                WriteArgs(op.mParameters);
                break;
            }

            default:
                Spv.Op spvOp;
                if (!SimpleInstructions.TryGetValue(op.mOpType, out spvOp))
                {
                    throw new Exception();
                }
                WriteBasicOp(op, spvOp);
                break;
            }
        }
Exemple #22
0
 //---------------------------------------------------------------Constants
 public bool AddConstant(ConstantOpKey key, ShaderOp shaderOp)
 {
     return(mConstantOps.TryAdd(key, shaderOp));
 }