Example #1
0
        public static void GenerateHardwareBuiltIns(FrontEndTranslator translator, ShaderInterfaceSet interfaceSet, StorageClass storageClass, ShaderBlock declarationBlock, ShaderBlock decorationsBlock)
        {
            foreach (var builtInInterfaceField in interfaceSet)
            {
                var pointerType     = translator.FindOrCreatePointerType(builtInInterfaceField.ShaderField.mType, storageClass);
                var fieldInstanceOp = translator.CreateOp(OpInstructionType.OpVariable, pointerType, null);
                declarationBlock.mLocalVariables.Add(fieldInstanceOp);

                Decorations.AddDecorationBuiltIn(translator, fieldInstanceOp, builtInInterfaceField.BuiltInType, decorationsBlock);

                ShaderInterfaceField.InstanceAccessDelegate fieldInstanceGetFunction = (FrontEndTranslator translator, ShaderInterfaceField interfaceField, FrontEndContext context) =>
                {
                    return(fieldInstanceOp);
                };
                builtInInterfaceField.GetInstance = fieldInstanceGetFunction;
            }
        }
Example #2
0
        public static void AddDecorationLocations(FrontEndTranslator translator, ShaderType shaderType, ShaderInterfaceSet interfaceSet, LocationCallback locationCallback, ShaderBlock decorationsBlock)
        {
            int GetNextLocation(Dictionary <int, ShaderInterfaceField> usedLocations, int currLocation)
            {
                while (usedLocations.ContainsKey(currLocation))
                {
                    ++currLocation;
                }
                return(currLocation);
            };
            var usedLocations = new Dictionary <int, ShaderInterfaceField>();
            List <(ShaderInterfaceField Field, ShaderOp FieldOp)> unknownLocationFields = new List <(ShaderInterfaceField Field, ShaderOp FieldOp)>();

            foreach (var field in interfaceSet)
            {
                var fieldOp = interfaceSet.GetFieldInstance(translator, field, null);

                int location  = -1;
                int component = -1;
                locationCallback(field.ShaderField, out location, out component);
                if (location != -1)
                {
                    Decorations.AddDecorationLocation(translator, fieldOp, location, decorationsBlock);
                    usedLocations.Add(location, field);
                    if (component != -1)
                    {
                        Decorations.AddDecorationComponent(translator, fieldOp, location, decorationsBlock);
                    }
                }
                else
                {
                    unknownLocationFields.Add((field, fieldOp));
                }
            }

            var nextLocation = 0;

            foreach (var pair in unknownLocationFields)
            {
                nextLocation = GetNextLocation(usedLocations, nextLocation);
                Decorations.AddDecorationLocation(translator, pair.FieldOp, nextLocation, decorationsBlock);
                usedLocations.Add(nextLocation, pair.Field);
            }
        }