private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info) { Dictionary <string, AstTextureOperation> images = new Dictionary <string, AstTextureOperation>(); foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle)) { string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize); string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr); if (!images.TryAdd(imageName, texOp)) { // Ensure that all texture operations share the same format. // This avoid errors like mismatched formats. texOp.Format = images[imageName].Format; continue; } string layout = texOp.Format.ToGlslFormat(); if (!string.IsNullOrEmpty(layout)) { layout = "layout(" + layout + ") "; } string imageTypeName = GetImageTypeName(texOp.Type, texOp.Format.GetComponentType()); context.AppendLine("uniform " + layout + imageTypeName + " " + imageName + ";"); } foreach (KeyValuePair <string, AstTextureOperation> kv in images) { string imageName = kv.Key; AstTextureOperation texOp = kv.Value; if ((texOp.Type & SamplerType.Indexed) != 0) { for (int index = 0; index < texOp.ArraySize; index++) { string indexExpr = NumberFormatter.FormatInt(index); string indexedSamplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); var desc = new TextureDescriptor(indexedSamplerName, texOp.Type, texOp.Handle + index * 2); context.TextureDescriptors.Add(desc); } } else { var desc = new TextureDescriptor(imageName, texOp.Type, texOp.Handle); context.ImageDescriptors.Add(desc); } } }
private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info) { HashSet <string> images = new HashSet <string>(); foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle)) { string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize); string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr); if ((texOp.Flags & TextureFlags.Bindless) != 0 || !images.Add(imageName)) { continue; } int firstBinding = -1; if ((texOp.Type & SamplerType.Indexed) != 0) { for (int index = 0; index < texOp.ArraySize; index++) { int binding = context.Config.Counts.IncrementImagesCount(); if (firstBinding < 0) { firstBinding = binding; } var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2); context.ImageDescriptors.Add(desc); } } else { firstBinding = context.Config.Counts.IncrementImagesCount(); var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle); context.ImageDescriptors.Add(desc); } string layout = texOp.Format.ToGlslFormat(); if (!string.IsNullOrEmpty(layout)) { layout = ", " + layout; } string imageTypeName = texOp.Type.ToGlslImageType(texOp.Format.GetComponentType()); context.AppendLine($"layout (binding = {firstBinding}{layout}) uniform {imageTypeName} {imageName};"); } }
private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors) { int arraySize = 0; foreach (var descriptor in descriptors) { if (descriptor.Type.HasFlag(SamplerType.Indexed)) { if (arraySize == 0) { arraySize = ShaderConfig.SamplerArraySize; } else if (--arraySize != 0) { continue; } } string indexExpr = NumberFormatter.FormatInt(arraySize); string imageName = OperandManager.GetImageName( context.Config.Stage, descriptor.CbufSlot, descriptor.HandleIndex, descriptor.Format, descriptor.Type.HasFlag(SamplerType.Indexed), indexExpr); string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType()); if (descriptor.Flags.HasFlag(TextureUsageFlags.ImageCoherent)) { imageTypeName = "coherent " + imageTypeName; } string layout = descriptor.Format.ToGlslFormat(); if (!string.IsNullOrEmpty(layout)) { layout = ", " + layout; } if (context.Config.Options.TargetApi == TargetApi.Vulkan) { bool isBuffer = (descriptor.Type & SamplerType.Mask) == SamplerType.TextureBuffer; int setIndex = isBuffer ? 5 : 3; layout = $", set = {setIndex}{layout}"; } context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {imageTypeName} {imageName};"); } }
private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info) { Dictionary <string, AstTextureOperation> images = new Dictionary <string, AstTextureOperation>(); foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle)) { string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize); string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr); if (!images.TryAdd(imageName, texOp)) { continue; } string imageTypeName = GetImageTypeName(texOp.Type); context.AppendLine("writeonly uniform " + imageTypeName + " " + imageName + ";"); } foreach (KeyValuePair <string, AstTextureOperation> kv in images) { string imageName = kv.Key; AstTextureOperation texOp = kv.Value; if ((texOp.Type & SamplerType.Indexed) != 0) { for (int index = 0; index < texOp.ArraySize; index++) { string indexExpr = NumberFormatter.FormatInt(index); string indexedSamplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); var desc = new TextureDescriptor(indexedSamplerName, texOp.Type, texOp.Handle + index * 2); context.TextureDescriptors.Add(desc); } } else { var desc = new TextureDescriptor(imageName, texOp.Type, texOp.Handle); context.ImageDescriptors.Add(desc); } } }
private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors) { int arraySize = 0; foreach (var descriptor in descriptors) { if (descriptor.Type.HasFlag(SamplerType.Indexed)) { if (arraySize == 0) { arraySize = ShaderConfig.SamplerArraySize; } else if (--arraySize != 0) { continue; } } string indexExpr = NumberFormatter.FormatInt(arraySize); string imageName = OperandManager.GetImageName( context.Config.Stage, descriptor.CbufSlot, descriptor.HandleIndex, descriptor.Format, descriptor.Type.HasFlag(SamplerType.Indexed), indexExpr); string layout = descriptor.Format.ToGlslFormat(); if (!string.IsNullOrEmpty(layout)) { layout = ", " + layout; } string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType()); context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {imageTypeName} {imageName};"); } }