| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190 |
- using Ryujinx.Common.Memory;
- using Ryujinx.Graphics.GAL;
- using Ryujinx.Graphics.Shader;
- using Silk.NET.Vulkan;
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using Buffer = Silk.NET.Vulkan.Buffer;
- using CompareOp = Ryujinx.Graphics.GAL.CompareOp;
- using Format = Ryujinx.Graphics.GAL.Format;
- using SamplerCreateInfo = Ryujinx.Graphics.GAL.SamplerCreateInfo;
- namespace Ryujinx.Graphics.Vulkan
- {
- class DescriptorSetUpdater
- {
- private const ulong StorageBufferMaxMirrorable = 0x2000;
- private const int ArrayGrowthSize = 16;
- private record struct BufferRef
- {
- public Auto<DisposableBuffer> Buffer;
- public int Offset;
- public bool Write;
- public BufferRef(Auto<DisposableBuffer> buffer)
- {
- Buffer = buffer;
- Offset = 0;
- Write = true;
- }
- public BufferRef(Auto<DisposableBuffer> buffer, ref BufferRange range)
- {
- Buffer = buffer;
- Offset = range.Offset;
- Write = range.Write;
- }
- }
- private record struct TextureRef
- {
- public ShaderStage Stage;
- public TextureView View;
- public Auto<DisposableImageView> ImageView;
- public Auto<DisposableSampler> Sampler;
- public TextureRef(ShaderStage stage, TextureView view, Auto<DisposableImageView> imageView, Auto<DisposableSampler> sampler)
- {
- Stage = stage;
- View = view;
- ImageView = imageView;
- Sampler = sampler;
- }
- }
- private record struct ImageRef
- {
- public ShaderStage Stage;
- public TextureView View;
- public Auto<DisposableImageView> ImageView;
- public ImageRef(ShaderStage stage, TextureView view, Auto<DisposableImageView> imageView)
- {
- Stage = stage;
- View = view;
- ImageView = imageView;
- }
- }
- private readonly record struct ArrayRef<T>(ShaderStage Stage, T Array);
- private readonly VulkanRenderer _gd;
- private readonly Device _device;
- private ShaderCollection _program;
- private readonly BufferRef[] _uniformBufferRefs;
- private readonly BufferRef[] _storageBufferRefs;
- private readonly TextureRef[] _textureRefs;
- private readonly ImageRef[] _imageRefs;
- private readonly TextureBuffer[] _bufferTextureRefs;
- private readonly TextureBuffer[] _bufferImageRefs;
- private ArrayRef<TextureArray>[] _textureArrayRefs;
- private ArrayRef<ImageArray>[] _imageArrayRefs;
- private ArrayRef<TextureArray>[] _textureArrayExtraRefs;
- private ArrayRef<ImageArray>[] _imageArrayExtraRefs;
- private readonly DescriptorBufferInfo[] _uniformBuffers;
- private readonly DescriptorBufferInfo[] _storageBuffers;
- private readonly DescriptorImageInfo[] _textures;
- private readonly DescriptorImageInfo[] _images;
- private readonly BufferView[] _bufferTextures;
- private readonly BufferView[] _bufferImages;
- private readonly DescriptorSetTemplateUpdater _templateUpdater;
- private BitMapStruct<Array2<long>> _uniformSet;
- private BitMapStruct<Array2<long>> _storageSet;
- private BitMapStruct<Array2<long>> _uniformMirrored;
- private BitMapStruct<Array2<long>> _storageMirrored;
- private readonly int[] _uniformSetPd;
- private int _pdSequence = 1;
- private bool _updateDescriptorCacheCbIndex;
- [Flags]
- private enum DirtyFlags
- {
- None = 0,
- Uniform = 1 << 0,
- Storage = 1 << 1,
- Texture = 1 << 2,
- Image = 1 << 3,
- All = Uniform | Storage | Texture | Image,
- }
- private DirtyFlags _dirty;
- private readonly BufferHolder _dummyBuffer;
- private readonly TextureView _dummyTexture;
- private readonly SamplerHolder _dummySampler;
- public List<TextureView> FeedbackLoopHazards { get; private set; }
- public DescriptorSetUpdater(VulkanRenderer gd, Device device)
- {
- _gd = gd;
- _device = device;
- // Some of the bindings counts needs to be multiplied by 2 because we have buffer and
- // regular textures/images interleaved on the same descriptor set.
- _uniformBufferRefs = new BufferRef[Constants.MaxUniformBufferBindings];
- _storageBufferRefs = new BufferRef[Constants.MaxStorageBufferBindings];
- _textureRefs = new TextureRef[Constants.MaxTextureBindings * 2];
- _imageRefs = new ImageRef[Constants.MaxImageBindings * 2];
- _bufferTextureRefs = new TextureBuffer[Constants.MaxTextureBindings * 2];
- _bufferImageRefs = new TextureBuffer[Constants.MaxImageBindings * 2];
- _textureArrayRefs = [];
- _imageArrayRefs = [];
- _textureArrayExtraRefs = [];
- _imageArrayExtraRefs = [];
- _uniformBuffers = new DescriptorBufferInfo[Constants.MaxUniformBufferBindings];
- _storageBuffers = new DescriptorBufferInfo[Constants.MaxStorageBufferBindings];
- _textures = new DescriptorImageInfo[Constants.MaxTexturesPerStage];
- _images = new DescriptorImageInfo[Constants.MaxImagesPerStage];
- _bufferTextures = new BufferView[Constants.MaxTexturesPerStage];
- _bufferImages = new BufferView[Constants.MaxImagesPerStage];
- _uniformSetPd = new int[Constants.MaxUniformBufferBindings];
- DescriptorImageInfo initialImageInfo = new()
- {
- ImageLayout = ImageLayout.General,
- };
- _textures.AsSpan().Fill(initialImageInfo);
- _images.AsSpan().Fill(initialImageInfo);
- if (gd.Capabilities.SupportsNullDescriptors)
- {
- // If null descriptors are supported, we can pass null as the handle.
- _dummyBuffer = null;
- }
- else
- {
- // If null descriptors are not supported, we need to pass the handle of a dummy buffer on unused bindings.
- _dummyBuffer = gd.BufferManager.Create(gd, 0x10000, forConditionalRendering: false, baseType: BufferAllocationType.DeviceLocal);
- }
- _dummyTexture = gd.CreateTextureView(new TextureCreateInfo(
- 1,
- 1,
- 1,
- 1,
- 1,
- 1,
- 1,
- 4,
- Format.R8G8B8A8Unorm,
- DepthStencilMode.Depth,
- Target.Texture2D,
- SwizzleComponent.Red,
- SwizzleComponent.Green,
- SwizzleComponent.Blue,
- SwizzleComponent.Alpha));
- _dummySampler = (SamplerHolder)gd.CreateSampler(new SamplerCreateInfo(
- MinFilter.Nearest,
- MagFilter.Nearest,
- false,
- AddressMode.Repeat,
- AddressMode.Repeat,
- AddressMode.Repeat,
- CompareMode.None,
- CompareOp.Always,
- new ColorF(0, 0, 0, 0),
- 0,
- 0,
- 0,
- 1f));
- _templateUpdater = new();
- }
- public void Initialize(bool isMainPipeline)
- {
- MemoryOwner<byte> dummyTextureData = MemoryOwner<byte>.RentCleared(4);
- _dummyTexture.SetData(dummyTextureData);
- if (isMainPipeline)
- {
- FeedbackLoopHazards = [];
- }
- }
- private static bool BindingOverlaps(ref DescriptorBufferInfo info, int bindingOffset, int offset, int size)
- {
- return offset < bindingOffset + (int)info.Range && (offset + size) > bindingOffset;
- }
- internal void Rebind(Auto<DisposableBuffer> buffer, int offset, int size)
- {
- if (_program == null)
- {
- return;
- }
- // Check stage bindings
- _uniformMirrored.Union(_uniformSet).SignalSet((int binding, int count) =>
- {
- for (int i = 0; i < count; i++)
- {
- ref BufferRef bufferRef = ref _uniformBufferRefs[binding];
- if (bufferRef.Buffer == buffer)
- {
- ref DescriptorBufferInfo info = ref _uniformBuffers[binding];
- int bindingOffset = bufferRef.Offset;
- if (BindingOverlaps(ref info, bindingOffset, offset, size))
- {
- _uniformSet.Clear(binding);
- _uniformSetPd[binding] = 0;
- SignalDirty(DirtyFlags.Uniform);
- }
- }
- binding++;
- }
- });
- _storageMirrored.Union(_storageSet).SignalSet((int binding, int count) =>
- {
- for (int i = 0; i < count; i++)
- {
- ref BufferRef bufferRef = ref _storageBufferRefs[binding];
- if (bufferRef.Buffer == buffer)
- {
- ref DescriptorBufferInfo info = ref _storageBuffers[binding];
- int bindingOffset = bufferRef.Offset;
- if (BindingOverlaps(ref info, bindingOffset, offset, size))
- {
- _storageSet.Clear(binding);
- SignalDirty(DirtyFlags.Storage);
- }
- }
- binding++;
- }
- });
- }
- public void InsertBindingBarriers(CommandBufferScoped cbs)
- {
- if ((FeedbackLoopHazards?.Count ?? 0) > 0)
- {
- // Clear existing hazards - they will be rebuilt.
- foreach (TextureView hazard in FeedbackLoopHazards)
- {
- hazard.DecrementHazardUses();
- }
- FeedbackLoopHazards.Clear();
- }
- foreach (ResourceBindingSegment segment in _program.BindingSegments[PipelineBase.TextureSetIndex])
- {
- if (segment.Type == ResourceType.TextureAndSampler)
- {
- if (!segment.IsArray)
- {
- for (int i = 0; i < segment.Count; i++)
- {
- ref TextureRef texture = ref _textureRefs[segment.Binding + i];
- texture.View?.PrepareForUsage(cbs, texture.Stage.ConvertToPipelineStageFlags(), FeedbackLoopHazards);
- }
- }
- else
- {
- ref ArrayRef<TextureArray> arrayRef = ref _textureArrayRefs[segment.Binding];
- PipelineStageFlags stageFlags = arrayRef.Stage.ConvertToPipelineStageFlags();
- arrayRef.Array?.QueueWriteToReadBarriers(cbs, stageFlags);
- }
- }
- }
- foreach (ResourceBindingSegment segment in _program.BindingSegments[PipelineBase.ImageSetIndex])
- {
- if (segment.Type == ResourceType.Image)
- {
- if (!segment.IsArray)
- {
- for (int i = 0; i < segment.Count; i++)
- {
- ref ImageRef image = ref _imageRefs[segment.Binding + i];
- image.View?.PrepareForUsage(cbs, image.Stage.ConvertToPipelineStageFlags(), FeedbackLoopHazards);
- }
- }
- else
- {
- ref ArrayRef<ImageArray> arrayRef = ref _imageArrayRefs[segment.Binding];
- PipelineStageFlags stageFlags = arrayRef.Stage.ConvertToPipelineStageFlags();
- arrayRef.Array?.QueueWriteToReadBarriers(cbs, stageFlags);
- }
- }
- }
- for (int setIndex = PipelineBase.DescriptorSetLayouts; setIndex < _program.BindingSegments.Length; setIndex++)
- {
- ResourceBindingSegment[] bindingSegments = _program.BindingSegments[setIndex];
- if (bindingSegments.Length == 0)
- {
- continue;
- }
- ResourceBindingSegment segment = bindingSegments[0];
- if (segment.IsArray)
- {
- if (segment.Type == ResourceType.Texture ||
- segment.Type == ResourceType.Sampler ||
- segment.Type == ResourceType.TextureAndSampler ||
- segment.Type == ResourceType.BufferTexture)
- {
- ref ArrayRef<TextureArray> arrayRef = ref _textureArrayExtraRefs[setIndex - PipelineBase.DescriptorSetLayouts];
- PipelineStageFlags stageFlags = arrayRef.Stage.ConvertToPipelineStageFlags();
- arrayRef.Array?.QueueWriteToReadBarriers(cbs, stageFlags);
- }
- else if (segment.Type == ResourceType.Image || segment.Type == ResourceType.BufferImage)
- {
- ref ArrayRef<ImageArray> arrayRef = ref _imageArrayExtraRefs[setIndex - PipelineBase.DescriptorSetLayouts];
- PipelineStageFlags stageFlags = arrayRef.Stage.ConvertToPipelineStageFlags();
- arrayRef.Array?.QueueWriteToReadBarriers(cbs, stageFlags);
- }
- }
- }
- }
- public void AdvancePdSequence()
- {
- if (++_pdSequence == 0)
- {
- _pdSequence = 1;
- }
- }
- public void SetProgram(CommandBufferScoped cbs, ShaderCollection program, bool isBound)
- {
- if (!program.HasSameLayout(_program))
- {
- // When the pipeline layout changes, push descriptor bindings are invalidated.
- AdvancePdSequence();
- }
- _program = program;
- _updateDescriptorCacheCbIndex = true;
- _dirty = DirtyFlags.All;
- }
- public void SetImage(CommandBufferScoped cbs, ShaderStage stage, int binding, ITexture image)
- {
- if (image is TextureBuffer imageBuffer)
- {
- _bufferImageRefs[binding] = imageBuffer;
- }
- else if (image is TextureView view)
- {
- ref ImageRef iRef = ref _imageRefs[binding];
- iRef.View?.ClearUsage(FeedbackLoopHazards);
- view?.PrepareForUsage(cbs, stage.ConvertToPipelineStageFlags(), FeedbackLoopHazards);
- iRef = new(stage, view, view.GetIdentityImageView());
- }
- else
- {
- _imageRefs[binding] = default;
- _bufferImageRefs[binding] = null;
- }
- SignalDirty(DirtyFlags.Image);
- }
- public void SetImage(int binding, Auto<DisposableImageView> image)
- {
- _imageRefs[binding] = new(ShaderStage.Compute, null, image);
- SignalDirty(DirtyFlags.Image);
- }
- public void SetStorageBuffers(CommandBuffer commandBuffer, ReadOnlySpan<BufferAssignment> buffers)
- {
- for (int i = 0; i < buffers.Length; i++)
- {
- BufferAssignment assignment = buffers[i];
- BufferRange buffer = assignment.Range;
- int index = assignment.Binding;
- Auto<DisposableBuffer> vkBuffer = buffer.Handle == BufferHandle.Null
- ? null
- : _gd.BufferManager.GetBuffer(commandBuffer, buffer.Handle, buffer.Write, isSSBO: true);
- ref BufferRef currentBufferRef = ref _storageBufferRefs[index];
- DescriptorBufferInfo info = new()
- {
- Offset = (ulong)buffer.Offset,
- Range = (ulong)buffer.Size,
- };
- BufferRef newRef = new(vkBuffer, ref buffer);
- ref DescriptorBufferInfo currentInfo = ref _storageBuffers[index];
- if (!currentBufferRef.Equals(newRef) || currentInfo.Range != info.Range)
- {
- _storageSet.Clear(index);
- currentInfo = info;
- currentBufferRef = newRef;
- }
- }
- SignalDirty(DirtyFlags.Storage);
- }
- public void SetStorageBuffers(CommandBuffer commandBuffer, int first, ReadOnlySpan<Auto<DisposableBuffer>> buffers)
- {
- for (int i = 0; i < buffers.Length; i++)
- {
- Auto<DisposableBuffer> vkBuffer = buffers[i];
- int index = first + i;
- ref BufferRef currentBufferRef = ref _storageBufferRefs[index];
- DescriptorBufferInfo info = new()
- {
- Offset = 0,
- Range = Vk.WholeSize,
- };
- BufferRef newRef = new(vkBuffer);
- ref DescriptorBufferInfo currentInfo = ref _storageBuffers[index];
- if (!currentBufferRef.Equals(newRef) || currentInfo.Range != info.Range)
- {
- _storageSet.Clear(index);
- currentInfo = info;
- currentBufferRef = newRef;
- }
- }
- SignalDirty(DirtyFlags.Storage);
- }
- public void SetTextureAndSampler(
- CommandBufferScoped cbs,
- ShaderStage stage,
- int binding,
- ITexture texture,
- ISampler sampler)
- {
- if (texture is TextureBuffer textureBuffer)
- {
- _bufferTextureRefs[binding] = textureBuffer;
- }
- else if (texture is TextureView view)
- {
- ref TextureRef iRef = ref _textureRefs[binding];
- iRef.View?.ClearUsage(FeedbackLoopHazards);
- view?.PrepareForUsage(cbs, stage.ConvertToPipelineStageFlags(), FeedbackLoopHazards);
- iRef = new(stage, view, view.GetImageView(), ((SamplerHolder)sampler)?.GetSampler());
- }
- else
- {
- _textureRefs[binding] = default;
- _bufferTextureRefs[binding] = null;
- }
- SignalDirty(DirtyFlags.Texture);
- }
- public void SetTextureAndSamplerIdentitySwizzle(
- CommandBufferScoped cbs,
- ShaderStage stage,
- int binding,
- ITexture texture,
- ISampler sampler)
- {
- if (texture is TextureView view)
- {
- view.Storage.QueueWriteToReadBarrier(cbs, AccessFlags.ShaderReadBit, stage.ConvertToPipelineStageFlags());
- _textureRefs[binding] = new(stage, view, view.GetIdentityImageView(), ((SamplerHolder)sampler)?.GetSampler());
- SignalDirty(DirtyFlags.Texture);
- }
- else
- {
- SetTextureAndSampler(cbs, stage, binding, texture, sampler);
- }
- }
- public void SetTextureArray(CommandBufferScoped cbs, ShaderStage stage, int binding, ITextureArray array)
- {
- ref ArrayRef<TextureArray> arrayRef = ref GetArrayRef(ref _textureArrayRefs, binding, ArrayGrowthSize);
- if (arrayRef.Stage != stage || arrayRef.Array != array)
- {
- arrayRef.Array?.DecrementBindCount();
- if (array is TextureArray textureArray)
- {
- textureArray.IncrementBindCount();
- textureArray.QueueWriteToReadBarriers(cbs, stage.ConvertToPipelineStageFlags());
- }
- arrayRef = new ArrayRef<TextureArray>(stage, array as TextureArray);
- SignalDirty(DirtyFlags.Texture);
- }
- }
- public void SetTextureArraySeparate(CommandBufferScoped cbs, ShaderStage stage, int setIndex, ITextureArray array)
- {
- ref ArrayRef<TextureArray> arrayRef = ref GetArrayRef(ref _textureArrayExtraRefs, setIndex - PipelineBase.DescriptorSetLayouts);
- if (arrayRef.Stage != stage || arrayRef.Array != array)
- {
- arrayRef.Array?.DecrementBindCount();
- if (array is TextureArray textureArray)
- {
- textureArray.IncrementBindCount();
- textureArray.QueueWriteToReadBarriers(cbs, stage.ConvertToPipelineStageFlags());
- }
- arrayRef = new ArrayRef<TextureArray>(stage, array as TextureArray);
- SignalDirty(DirtyFlags.Texture);
- }
- }
- public void SetImageArray(CommandBufferScoped cbs, ShaderStage stage, int binding, IImageArray array)
- {
- ref ArrayRef<ImageArray> arrayRef = ref GetArrayRef(ref _imageArrayRefs, binding, ArrayGrowthSize);
- if (arrayRef.Stage != stage || arrayRef.Array != array)
- {
- arrayRef.Array?.DecrementBindCount();
- if (array is ImageArray imageArray)
- {
- imageArray.IncrementBindCount();
- imageArray.QueueWriteToReadBarriers(cbs, stage.ConvertToPipelineStageFlags());
- }
- arrayRef = new ArrayRef<ImageArray>(stage, array as ImageArray);
- SignalDirty(DirtyFlags.Image);
- }
- }
- public void SetImageArraySeparate(CommandBufferScoped cbs, ShaderStage stage, int setIndex, IImageArray array)
- {
- ref ArrayRef<ImageArray> arrayRef = ref GetArrayRef(ref _imageArrayExtraRefs, setIndex - PipelineBase.DescriptorSetLayouts);
- if (arrayRef.Stage != stage || arrayRef.Array != array)
- {
- arrayRef.Array?.DecrementBindCount();
- if (array is ImageArray imageArray)
- {
- imageArray.IncrementBindCount();
- imageArray.QueueWriteToReadBarriers(cbs, stage.ConvertToPipelineStageFlags());
- }
- arrayRef = new ArrayRef<ImageArray>(stage, array as ImageArray);
- SignalDirty(DirtyFlags.Image);
- }
- }
- private static ref ArrayRef<T> GetArrayRef<T>(ref ArrayRef<T>[] array, int index, int growthSize = 1)
- {
- ArgumentOutOfRangeException.ThrowIfNegative(index);
- if (array.Length <= index)
- {
- Array.Resize(ref array, index + growthSize);
- }
- return ref array[index];
- }
- public void SetUniformBuffers(CommandBuffer commandBuffer, ReadOnlySpan<BufferAssignment> buffers)
- {
- for (int i = 0; i < buffers.Length; i++)
- {
- BufferAssignment assignment = buffers[i];
- BufferRange buffer = assignment.Range;
- int index = assignment.Binding;
- Auto<DisposableBuffer> vkBuffer = buffer.Handle == BufferHandle.Null
- ? null
- : _gd.BufferManager.GetBuffer(commandBuffer, buffer.Handle, false);
- ref BufferRef currentBufferRef = ref _uniformBufferRefs[index];
- DescriptorBufferInfo info = new()
- {
- Offset = (ulong)buffer.Offset,
- Range = (ulong)buffer.Size,
- };
- BufferRef newRef = new(vkBuffer, ref buffer);
- ref DescriptorBufferInfo currentInfo = ref _uniformBuffers[index];
- if (!currentBufferRef.Equals(newRef) || currentInfo.Range != info.Range)
- {
- _uniformSet.Clear(index);
- _uniformSetPd[index] = 0;
- currentInfo = info;
- currentBufferRef = newRef;
- }
- }
- SignalDirty(DirtyFlags.Uniform);
- }
- private void SignalDirty(DirtyFlags flag)
- {
- _dirty |= flag;
- }
- public void UpdateAndBindDescriptorSets(CommandBufferScoped cbs, PipelineBindPoint pbp)
- {
- if ((_dirty & DirtyFlags.All) == 0)
- {
- return;
- }
- ShaderCollection program = _program;
- if (_dirty.HasFlag(DirtyFlags.Uniform))
- {
- if (program.UsePushDescriptors)
- {
- UpdateAndBindUniformBufferPd(cbs);
- }
- else
- {
- UpdateAndBind(cbs, program, PipelineBase.UniformSetIndex, pbp);
- }
- }
- if (_dirty.HasFlag(DirtyFlags.Storage))
- {
- UpdateAndBind(cbs, program, PipelineBase.StorageSetIndex, pbp);
- }
- if (_dirty.HasFlag(DirtyFlags.Texture))
- {
- if (program.UpdateTexturesWithoutTemplate)
- {
- UpdateAndBindTexturesWithoutTemplate(cbs, program, pbp);
- }
- else
- {
- UpdateAndBind(cbs, program, PipelineBase.TextureSetIndex, pbp);
- }
- }
- if (_dirty.HasFlag(DirtyFlags.Image))
- {
- UpdateAndBind(cbs, program, PipelineBase.ImageSetIndex, pbp);
- }
- if (program.BindingSegments.Length > PipelineBase.DescriptorSetLayouts)
- {
- // Program is using extra sets, we need to bind those too.
- BindExtraSets(cbs, program, pbp);
- }
- _dirty = DirtyFlags.None;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static bool UpdateBuffer(
- CommandBufferScoped cbs,
- ref DescriptorBufferInfo info,
- ref BufferRef buffer,
- Auto<DisposableBuffer> dummyBuffer,
- bool mirrorable)
- {
- int offset = buffer.Offset;
- bool mirrored = false;
- if (mirrorable)
- {
- info.Buffer = buffer.Buffer?.GetMirrorable(cbs, ref offset, (int)info.Range, out mirrored).Value ?? default;
- }
- else
- {
- info.Buffer = buffer.Buffer?.Get(cbs, offset, (int)info.Range, buffer.Write).Value ?? default;
- }
- info.Offset = (ulong)offset;
- // The spec requires that buffers with null handle have offset as 0 and range as VK_WHOLE_SIZE.
- if (info.Buffer.Handle == 0)
- {
- info.Buffer = dummyBuffer?.Get(cbs).Value ?? default;
- info.Offset = 0;
- info.Range = Vk.WholeSize;
- }
- return mirrored;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void UpdateAndBind(CommandBufferScoped cbs, ShaderCollection program, int setIndex, PipelineBindPoint pbp)
- {
- ResourceBindingSegment[] bindingSegments = program.BindingSegments[setIndex];
- if (bindingSegments.Length == 0)
- {
- return;
- }
- Auto<DisposableBuffer> dummyBuffer = _dummyBuffer?.GetBuffer();
- if (_updateDescriptorCacheCbIndex)
- {
- _updateDescriptorCacheCbIndex = false;
- program.UpdateDescriptorCacheCommandBufferIndex(cbs.CommandBufferIndex);
- }
- DescriptorSetCollection dsc = program.GetNewDescriptorSetCollection(setIndex, out bool isNew).Get(cbs);
- if (!program.HasMinimalLayout)
- {
- if (isNew)
- {
- Initialize(cbs, setIndex, dsc);
- }
- }
- DescriptorSetTemplate template = program.Templates[setIndex];
- DescriptorSetTemplateWriter tu = _templateUpdater.Begin(template);
- foreach (ResourceBindingSegment segment in bindingSegments)
- {
- int binding = segment.Binding;
- int count = segment.Count;
- if (setIndex == PipelineBase.UniformSetIndex)
- {
- for (int i = 0; i < count; i++)
- {
- int index = binding + i;
- if (_uniformSet.Set(index))
- {
- ref BufferRef buffer = ref _uniformBufferRefs[index];
- bool mirrored = UpdateBuffer(cbs, ref _uniformBuffers[index], ref buffer, dummyBuffer, true);
- _uniformMirrored.Set(index, mirrored);
- }
- }
- ReadOnlySpan<DescriptorBufferInfo> uniformBuffers = _uniformBuffers;
- tu.Push(uniformBuffers.Slice(binding, count));
- }
- else if (setIndex == PipelineBase.StorageSetIndex)
- {
- for (int i = 0; i < count; i++)
- {
- int index = binding + i;
- ref BufferRef buffer = ref _storageBufferRefs[index];
- if (_storageSet.Set(index))
- {
- ref DescriptorBufferInfo info = ref _storageBuffers[index];
- bool mirrored = UpdateBuffer(cbs,
- ref info,
- ref _storageBufferRefs[index],
- dummyBuffer,
- !buffer.Write && info.Range <= StorageBufferMaxMirrorable);
- _storageMirrored.Set(index, mirrored);
- }
- }
- ReadOnlySpan<DescriptorBufferInfo> storageBuffers = _storageBuffers;
- tu.Push(storageBuffers.Slice(binding, count));
- }
- else if (setIndex == PipelineBase.TextureSetIndex)
- {
- if (!segment.IsArray)
- {
- if (segment.Type != ResourceType.BufferTexture)
- {
- Span<DescriptorImageInfo> textures = _textures;
- for (int i = 0; i < count; i++)
- {
- ref DescriptorImageInfo texture = ref textures[i];
- ref TextureRef refs = ref _textureRefs[binding + i];
- texture.ImageView = refs.ImageView?.Get(cbs).Value ?? default;
- texture.Sampler = refs.Sampler?.Get(cbs).Value ?? default;
- if (texture.ImageView.Handle == 0)
- {
- texture.ImageView = _dummyTexture.GetImageView().Get(cbs).Value;
- }
- if (texture.Sampler.Handle == 0)
- {
- texture.Sampler = _dummySampler.GetSampler().Get(cbs).Value;
- }
- }
- tu.Push<DescriptorImageInfo>(textures[..count]);
- }
- else
- {
- Span<BufferView> bufferTextures = _bufferTextures;
- for (int i = 0; i < count; i++)
- {
- bufferTextures[i] = _bufferTextureRefs[binding + i]?.GetBufferView(cbs, false) ?? default;
- }
- tu.Push<BufferView>(bufferTextures[..count]);
- }
- }
- else
- {
- if (segment.Type != ResourceType.BufferTexture)
- {
- tu.Push(_textureArrayRefs[binding].Array.GetImageInfos(_gd, cbs, _dummyTexture, _dummySampler));
- }
- else
- {
- tu.Push(_textureArrayRefs[binding].Array.GetBufferViews(cbs));
- }
- }
- }
- else if (setIndex == PipelineBase.ImageSetIndex)
- {
- if (!segment.IsArray)
- {
- if (segment.Type != ResourceType.BufferImage)
- {
- Span<DescriptorImageInfo> images = _images;
- for (int i = 0; i < count; i++)
- {
- images[i].ImageView = _imageRefs[binding + i].ImageView?.Get(cbs).Value ?? default;
- }
- tu.Push<DescriptorImageInfo>(images[..count]);
- }
- else
- {
- Span<BufferView> bufferImages = _bufferImages;
- for (int i = 0; i < count; i++)
- {
- bufferImages[i] = _bufferImageRefs[binding + i]?.GetBufferView(cbs, true) ?? default;
- }
- tu.Push<BufferView>(bufferImages[..count]);
- }
- }
- else
- {
- if (segment.Type != ResourceType.BufferTexture)
- {
- tu.Push(_imageArrayRefs[binding].Array.GetImageInfos(_gd, cbs, _dummyTexture));
- }
- else
- {
- tu.Push(_imageArrayRefs[binding].Array.GetBufferViews(cbs));
- }
- }
- }
- }
- DescriptorSet[] sets = dsc.GetSets();
- _templateUpdater.Commit(_gd, _device, sets[0]);
- _gd.Api.CmdBindDescriptorSets(cbs.CommandBuffer, pbp, _program.PipelineLayout, (uint)setIndex, 1, sets, 0, ReadOnlySpan<uint>.Empty);
- }
- private void UpdateAndBindTexturesWithoutTemplate(CommandBufferScoped cbs, ShaderCollection program, PipelineBindPoint pbp)
- {
- int setIndex = PipelineBase.TextureSetIndex;
- ResourceBindingSegment[] bindingSegments = program.BindingSegments[setIndex];
- if (bindingSegments.Length == 0)
- {
- return;
- }
- if (_updateDescriptorCacheCbIndex)
- {
- _updateDescriptorCacheCbIndex = false;
- program.UpdateDescriptorCacheCommandBufferIndex(cbs.CommandBufferIndex);
- }
- DescriptorSetCollection dsc = program.GetNewDescriptorSetCollection(setIndex, out _).Get(cbs);
- foreach (ResourceBindingSegment segment in bindingSegments)
- {
- int binding = segment.Binding;
- int count = segment.Count;
- if (!segment.IsArray)
- {
- if (segment.Type != ResourceType.BufferTexture)
- {
- Span<DescriptorImageInfo> textures = _textures;
- for (int i = 0; i < count; i++)
- {
- ref DescriptorImageInfo texture = ref textures[i];
- ref TextureRef refs = ref _textureRefs[binding + i];
- texture.ImageView = refs.ImageView?.Get(cbs).Value ?? default;
- texture.Sampler = refs.Sampler?.Get(cbs).Value ?? default;
- if (texture.ImageView.Handle == 0)
- {
- texture.ImageView = _dummyTexture.GetImageView().Get(cbs).Value;
- }
- if (texture.Sampler.Handle == 0)
- {
- texture.Sampler = _dummySampler.GetSampler().Get(cbs).Value;
- }
- }
- dsc.UpdateImages(0, binding, textures[..count], DescriptorType.CombinedImageSampler);
- }
- else
- {
- Span<BufferView> bufferTextures = _bufferTextures;
- for (int i = 0; i < count; i++)
- {
- bufferTextures[i] = _bufferTextureRefs[binding + i]?.GetBufferView(cbs, false) ?? default;
- }
- dsc.UpdateBufferImages(0, binding, bufferTextures[..count], DescriptorType.UniformTexelBuffer);
- }
- }
- else
- {
- if (segment.Type != ResourceType.BufferTexture)
- {
- dsc.UpdateImages(0, binding, _textureArrayRefs[binding].Array.GetImageInfos(_gd, cbs, _dummyTexture, _dummySampler), DescriptorType.CombinedImageSampler);
- }
- else
- {
- dsc.UpdateBufferImages(0, binding, _textureArrayRefs[binding].Array.GetBufferViews(cbs), DescriptorType.UniformTexelBuffer);
- }
- }
- }
- DescriptorSet[] sets = dsc.GetSets();
- _gd.Api.CmdBindDescriptorSets(cbs.CommandBuffer, pbp, _program.PipelineLayout, (uint)setIndex, 1, sets, 0, ReadOnlySpan<uint>.Empty);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void UpdateAndBindUniformBufferPd(CommandBufferScoped cbs)
- {
- int sequence = _pdSequence;
- ResourceBindingSegment[] bindingSegments = _program.BindingSegments[PipelineBase.UniformSetIndex];
- Auto<DisposableBuffer> dummyBuffer = _dummyBuffer?.GetBuffer();
- long updatedBindings = 0;
- DescriptorSetTemplateWriter writer = _templateUpdater.Begin(32 * Unsafe.SizeOf<DescriptorBufferInfo>());
- foreach (ResourceBindingSegment segment in bindingSegments)
- {
- int binding = segment.Binding;
- int count = segment.Count;
- ReadOnlySpan<DescriptorBufferInfo> uniformBuffers = _uniformBuffers;
- for (int i = 0; i < count; i++)
- {
- int index = binding + i;
- if (_uniformSet.Set(index))
- {
- ref BufferRef buffer = ref _uniformBufferRefs[index];
- bool mirrored = UpdateBuffer(cbs, ref _uniformBuffers[index], ref buffer, dummyBuffer, true);
- _uniformMirrored.Set(index, mirrored);
- }
- if (_uniformSetPd[index] != sequence)
- {
- // Need to set this push descriptor (even if the buffer binding has not changed)
- _uniformSetPd[index] = sequence;
- updatedBindings |= 1L << index;
- writer.Push(MemoryMarshal.CreateReadOnlySpan(ref _uniformBuffers[index], 1));
- }
- }
- }
- if (updatedBindings > 0)
- {
- DescriptorSetTemplate template = _program.GetPushDescriptorTemplate(updatedBindings);
- _templateUpdater.CommitPushDescriptor(_gd, cbs, template, _program.PipelineLayout);
- }
- }
- private void Initialize(CommandBufferScoped cbs, int setIndex, DescriptorSetCollection dsc)
- {
- // We don't support clearing texture descriptors currently.
- if (setIndex != PipelineBase.UniformSetIndex && setIndex != PipelineBase.StorageSetIndex)
- {
- return;
- }
- Buffer dummyBuffer = _dummyBuffer?.GetBuffer().Get(cbs).Value ?? default;
- foreach (ResourceBindingSegment segment in _program.ClearSegments[setIndex])
- {
- dsc.InitializeBuffers(0, segment.Binding, segment.Count, segment.Type.Convert(), dummyBuffer);
- }
- }
- private void BindExtraSets(CommandBufferScoped cbs, ShaderCollection program, PipelineBindPoint pbp)
- {
- for (int setIndex = PipelineBase.DescriptorSetLayouts; setIndex < program.BindingSegments.Length; setIndex++)
- {
- ResourceBindingSegment[] bindingSegments = program.BindingSegments[setIndex];
- if (bindingSegments.Length == 0)
- {
- continue;
- }
- ResourceBindingSegment segment = bindingSegments[0];
- if (segment.IsArray)
- {
- DescriptorSet[] sets = null;
- if (segment.Type == ResourceType.Texture ||
- segment.Type == ResourceType.Sampler ||
- segment.Type == ResourceType.TextureAndSampler ||
- segment.Type == ResourceType.BufferTexture)
- {
- sets = _textureArrayExtraRefs[setIndex - PipelineBase.DescriptorSetLayouts].Array.GetDescriptorSets(
- _device,
- cbs,
- _templateUpdater,
- program,
- setIndex,
- _dummyTexture,
- _dummySampler);
- }
- else if (segment.Type == ResourceType.Image || segment.Type == ResourceType.BufferImage)
- {
- sets = _imageArrayExtraRefs[setIndex - PipelineBase.DescriptorSetLayouts].Array.GetDescriptorSets(
- _device,
- cbs,
- _templateUpdater,
- program,
- setIndex,
- _dummyTexture);
- }
- if (sets != null)
- {
- _gd.Api.CmdBindDescriptorSets(cbs.CommandBuffer, pbp, _program.PipelineLayout, (uint)setIndex, 1, sets, 0, ReadOnlySpan<uint>.Empty);
- }
- }
- }
- }
- public void SignalCommandBufferChange()
- {
- _updateDescriptorCacheCbIndex = true;
- _dirty = DirtyFlags.All;
- _uniformSet.Clear();
- _storageSet.Clear();
- AdvancePdSequence();
- }
- public void ForceTextureDirty()
- {
- SignalDirty(DirtyFlags.Texture);
- }
- public void ForceImageDirty()
- {
- SignalDirty(DirtyFlags.Image);
- }
- private static void SwapBuffer(BufferRef[] list, Auto<DisposableBuffer> from, Auto<DisposableBuffer> to)
- {
- for (int i = 0; i < list.Length; i++)
- {
- if (list[i].Buffer == from)
- {
- list[i].Buffer = to;
- }
- }
- }
- public void SwapBuffer(Auto<DisposableBuffer> from, Auto<DisposableBuffer> to)
- {
- SwapBuffer(_uniformBufferRefs, from, to);
- SwapBuffer(_storageBufferRefs, from, to);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- _dummyTexture.Dispose();
- _dummySampler.Dispose();
- _templateUpdater.Dispose();
- }
- }
- public void Dispose()
- {
- Dispose(true);
- }
- }
- }
|