Example #1
0
        public static ShaderOp GenerateInterfaceStructAndOp(FrontEndTranslator translator, List <ShaderInterfaceField> interfaceFields, TypeName structName, string instanceName, StorageClass storageClass)
        {
            var interfaceStruct = translator.FindType(new TypeKey(structName));

            // Generate the interface struct if it doesn't already exist (uniform buffers already exist)
            if (interfaceStruct == null)
            {
                interfaceStruct = translator.CreateType(new TypeKey(structName), structName, OpType.Struct, null);
                foreach (var interfaceField in interfaceFields)
                {
                    interfaceStruct.mFields.Add(interfaceField.ShaderField);
                }
            }

            var opPointerType = translator.FindOrCreatePointerType(new TypeKey(structName), structName, storageClass);
            var op            = translator.CreateOp(OpInstructionType.OpVariable, opPointerType, null);

            ShaderInterfaceField.InstanceAccessDelegate fieldInstanceGetFunction = (FrontEndTranslator translator, ShaderInterfaceField interfaceField, FrontEndContext context) =>
            {
                return(translator.GenerateAccessChain(op, interfaceField.ShaderField.mMeta.mName, context));
            };

            foreach (var interfaceField in interfaceFields)
            {
                interfaceField.GetInstance = fieldInstanceGetFunction;
            }

            op.DebugInfo.Name = instanceName;
            return(op);
        }
        static void SplitSampledImageValueTypeIntrinsic(FrontEndTranslator translator, FrontEndContext context, ImageIntrinsicAttributeData intrinsicData, ShaderType returnType, List <IShaderIR> arguments)
        {
            var valueOps = new List <IShaderIR>();

            var imageParam   = translator.GetOrGenerateValueTypeFromIR(context.mCurrentBlock, arguments[0]);
            var samplerParam = translator.GetOrGenerateValueTypeFromIR(context.mCurrentBlock, arguments[1]);

            // Try and generate a sampled image type for this image. If we do create a new type that wasn't necessary that's fine because it'll be de-duped in the binary backend.
            var sampledImageTypeStr = new TypeName {
                Name = "GeneratedSampledImage_" + imageParam.mResultType.ToString()
            };
            var sampledImageType = translator.FindType(new TypeKey(sampledImageTypeStr));

            if (sampledImageType == null)
            {
                sampledImageType = translator.CreateType(new TypeKey(sampledImageTypeStr), sampledImageTypeStr, OpType.SampledImage, null);
                sampledImageType.mParameters.Add(imageParam.mResultType.GetDereferenceType());
            }

            // Combine the image and sampler together into a sampled image. This is a bit annoying, but a sampled image
            // can't be stored into a variable so this has to be a temporary. Use this combined sampled image in place of the first two args to the intrinsics.
            var sampledImageOp = translator.CreateOp(context.mCurrentBlock, OpInstructionType.OpSampledImage, sampledImageType, new List <IShaderIR>()
            {
                imageParam, samplerParam
            });

            valueOps.Add(sampledImageOp);
            WriteArguments(translator, context, valueOps, intrinsicData, arguments, 2);
            var op = translator.CreateOp(context.mCurrentBlock, intrinsicData.OpType, returnType, valueOps);

            context.Push(op);
        }
Example #3
0
        public static ShaderType ProcessSamplerType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            var shaderType = translator.CreateType(typeSymbol, OpType.Sampler);

            shaderType.mStorageClass = StorageClass.UniformConstant;
            return(shaderType);
        }
Example #4
0
        public static ShaderType ProcessFloatType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            var width = (UInt32)attribute.ConstructorArguments[0].Value;

            var shaderType = translator.CreateType(typeSymbol, OpType.Float);

            shaderType.mParameters.Add(translator.CreateConstantLiteral(width));

            return(shaderType);
        }
Example #5
0
        public static ShaderType ProcessVectorType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            var componentType  = translator.FindType(new TypeKey(attribute.ConstructorArguments[0].Value as ISymbol));
            var componentCount = (UInt32)attribute.ConstructorArguments[1].Value;

            var shaderType = translator.CreateType(typeSymbol, OpType.Vector);

            shaderType.mParameters.Add(componentType);
            shaderType.mParameters.Add(translator.CreateConstantLiteral(componentCount));

            ProcessVectorDefaultConstructor(translator, typeSymbol, shaderType);

            return(shaderType);
        }
Example #6
0
        public static ShaderType ProcessIntegerType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            var width  = (UInt32)attribute.ConstructorArguments[0].Value;
            var signed = (bool)attribute.ConstructorArguments[1].Value;

            var shaderType = translator.CreateType(typeSymbol, OpType.Int);

            shaderType.mParameters.Add(translator.CreateConstantLiteral(width));
            var signedness = translator.CreateConstantLiteral <uint>(signed ? 1u : 0u);

            shaderType.mParameters.Add(signedness);

            return(shaderType);
        }
Example #7
0
        public static ShaderType ProcessSampledImageType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            var imageType = translator.FindType(new TypeKey(attribute.ConstructorArguments[0].Value as ITypeSymbol));

            // Validate the image type param is an image type
            if (imageType == null || imageType.mBaseType != OpType.Image)
            {
                throw new System.Exception();
            }

            var shaderType = translator.CreateType(typeSymbol, OpType.SampledImage);

            shaderType.mParameters.Add(imageType);
            shaderType.mStorageClass = StorageClass.UniformConstant;
            return(shaderType);
        }
Example #8
0
        public static ShaderType ProcessMatrixType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            ShaderType columnType  = null;
            UInt32     columnCount = 0;

            if (!ValidateMatrixPrimitive(translator, typeSymbol, attribute, out columnType, out columnCount))
            {
                return(null);
            }

            var shaderType = translator.CreateType(typeSymbol, OpType.Matrix);

            shaderType.mParameters.Add(columnType);
            shaderType.mParameters.Add(translator.CreateConstantLiteral(columnCount));

            RegisterMatrixConstructors(translator, typeSymbol, shaderType, columnType, columnCount);

            return(shaderType);
        }
Example #9
0
        public static ShaderType ProcessImageType(FrontEndTranslator translator, INamedTypeSymbol typeSymbol, AttributeData attribute)
        {
            ShaderType sampledType = null;

            Shader.ImageDimension        dimension        = Shader.ImageDimension.Dim2D;
            Shader.ImageDepthMode        depthMode        = Shader.ImageDepthMode.None;
            Shader.ImageArrayedMode      arrayedMode      = Shader.ImageArrayedMode.None;
            Shader.ImageMultiSampledMode multiSampledMode = Shader.ImageMultiSampledMode.SingleSampled;
            Shader.ImageSampledMode      sampledMode      = Shader.ImageSampledMode.Sampling;
            Shader.ImageFormat           imageFormat      = Shader.ImageFormat.Unknown;

            // Load all constructor args by type.
            foreach (var argument in attribute.ConstructorArguments)
            {
                var argName = TypeAliases.GetTypeName(argument.Type);
                if (argName == TypeAliases.GetTypeName <Type>())
                {
                    sampledType = translator.FindType(new TypeKey(argument.Value as ITypeSymbol));
                }
                else if (argName == TypeAliases.GetTypeName <Shader.ImageDimension>())
                {
                    dimension = (Shader.ImageDimension)argument.Value;
                }
                else if (argName == TypeAliases.GetTypeName <Shader.ImageDepthMode>())
                {
                    depthMode = (Shader.ImageDepthMode)argument.Value;
                }
                else if (argName == TypeAliases.GetTypeName <Shader.ImageArrayedMode>())
                {
                    arrayedMode = (Shader.ImageArrayedMode)argument.Value;
                }
                else if (argName == TypeAliases.GetTypeName <Shader.ImageMultiSampledMode>())
                {
                    multiSampledMode = (Shader.ImageMultiSampledMode)argument.Value;
                }
                else if (argName == TypeAliases.GetTypeName <Shader.ImageSampledMode>())
                {
                    sampledMode = (Shader.ImageSampledMode)argument.Value;
                }
                else if (argName == TypeAliases.GetTypeName <Shader.ImageFormat>())
                {
                    imageFormat = (Shader.ImageFormat)argument.Value;
                }
            }
            // Handle named arguments
            foreach (var pair in attribute.NamedArguments)
            {
                if (pair.Key == "SampledType")
                {
                    sampledType = translator.FindType(new TypeKey(pair.Value.Value as ITypeSymbol));
                }
                else if (pair.Key == "Dimension")
                {
                    dimension = (Shader.ImageDimension)pair.Value.Value;
                }
                else if (pair.Key == "DepthMode")
                {
                    depthMode = (Shader.ImageDepthMode)pair.Value.Value;
                }
                else if (pair.Key == "ArrayedMode")
                {
                    arrayedMode = (Shader.ImageArrayedMode)pair.Value.Value;
                }
                else if (pair.Key == "MultiSampledMode")
                {
                    multiSampledMode = (Shader.ImageMultiSampledMode)pair.Value.Value;
                }
                else if (pair.Key == "SampledMode")
                {
                    sampledMode = (Shader.ImageSampledMode)pair.Value.Value;
                }
                else if (pair.Key == "ImageFormat")
                {
                    imageFormat = (Shader.ImageFormat)pair.Value.Value;
                }
            }

            // Validate input type. SpirV spec says: "Sampled Type is the type of the components that result
            // from sampling or reading from this image type. Must be ascalar numerical type or OpTypeVoid"
            if (sampledType == null || (sampledType.mBaseType != OpType.Bool && sampledType.mBaseType != OpType.Int && sampledType.mBaseType != OpType.Float && sampledType.mBaseType != OpType.Void))
            {
                throw new Exception();
            }

            var shaderType = translator.CreateType(typeSymbol, OpType.Image);

            shaderType.mParameters.Add(sampledType);
            shaderType.mParameters.Add(translator.CreateConstantLiteral((UInt32)dimension));
            shaderType.mParameters.Add(translator.CreateConstantLiteral((UInt32)depthMode));
            shaderType.mParameters.Add(translator.CreateConstantLiteral((UInt32)arrayedMode));
            shaderType.mParameters.Add(translator.CreateConstantLiteral((UInt32)multiSampledMode));
            shaderType.mParameters.Add(translator.CreateConstantLiteral((UInt32)sampledMode));
            shaderType.mParameters.Add(translator.CreateConstantLiteral((UInt32)imageFormat));
            shaderType.mStorageClass = StorageClass.UniformConstant;

            return(shaderType);
        }
Example #10
0
 public ShaderType CreateType(ISymbol symbol, OpType opType)
 {
     return(mFrontEnd.CreateType(symbol, opType));
 }