ryujinx-mirror/src/Ryujinx.Graphics.Vulkan/PipelineLayoutCacheEntry.cs
gdkchan 5626f2ca1c
Replace ShaderBindings with new ResourceLayout structure for Vulkan (#5025)
* Introduce ResourceLayout

* Part 1: Use new ResourceSegments array on UpdateAndBind

* Part 2: Use ResourceLayout to build PipelineLayout

* Delete old code

* XML docs

* Fix shader cache load NRE

* Fix typo
2023-05-21 14:04:21 -03:00

110 lines
3.4 KiB
C#

using Ryujinx.Graphics.GAL;
using Silk.NET.Vulkan;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Ryujinx.Graphics.Vulkan
{
class PipelineLayoutCacheEntry
{
private readonly VulkanRenderer _gd;
private readonly Device _device;
public DescriptorSetLayout[] DescriptorSetLayouts { get; }
public PipelineLayout PipelineLayout { get; }
private readonly List<Auto<DescriptorSetCollection>>[][] _dsCache;
private readonly int[] _dsCacheCursor;
private int _dsLastCbIndex;
private PipelineLayoutCacheEntry(VulkanRenderer gd, Device device, int setsCount)
{
_gd = gd;
_device = device;
_dsCache = new List<Auto<DescriptorSetCollection>>[CommandBufferPool.MaxCommandBuffers][];
for (int i = 0; i < CommandBufferPool.MaxCommandBuffers; i++)
{
_dsCache[i] = new List<Auto<DescriptorSetCollection>>[setsCount];
for (int j = 0; j < _dsCache[i].Length; j++)
{
_dsCache[i][j] = new List<Auto<DescriptorSetCollection>>();
}
}
_dsCacheCursor = new int[setsCount];
}
public PipelineLayoutCacheEntry(
VulkanRenderer gd,
Device device,
ReadOnlyCollection<ResourceDescriptorCollection> setDescriptors,
bool usePushDescriptors) : this(gd, device, setDescriptors.Count)
{
(DescriptorSetLayouts, PipelineLayout) = PipelineLayoutFactory.Create(gd, device, setDescriptors, usePushDescriptors);
}
public Auto<DescriptorSetCollection> GetNewDescriptorSetCollection(
VulkanRenderer gd,
int commandBufferIndex,
int setIndex,
out bool isNew)
{
if (_dsLastCbIndex != commandBufferIndex)
{
_dsLastCbIndex = commandBufferIndex;
for (int i = 0; i < _dsCacheCursor.Length; i++)
{
_dsCacheCursor[i] = 0;
}
}
var list = _dsCache[commandBufferIndex][setIndex];
int index = _dsCacheCursor[setIndex]++;
if (index == list.Count)
{
var dsc = gd.DescriptorSetManager.AllocateDescriptorSet(gd.Api, DescriptorSetLayouts[setIndex]);
list.Add(dsc);
isNew = true;
return dsc;
}
isNew = false;
return list[index];
}
protected virtual unsafe void Dispose(bool disposing)
{
if (disposing)
{
for (int i = 0; i < _dsCache.Length; i++)
{
for (int j = 0; j < _dsCache[i].Length; j++)
{
for (int k = 0; k < _dsCache[i][j].Count; k++)
{
_dsCache[i][j][k].Dispose();
}
_dsCache[i][j].Clear();
}
}
_gd.Api.DestroyPipelineLayout(_device, PipelineLayout, null);
for (int i = 0; i < DescriptorSetLayouts.Length; i++)
{
_gd.Api.DestroyDescriptorSetLayout(_device, DescriptorSetLayouts[i], null);
}
}
}
public void Dispose()
{
Dispose(true);
}
}
}