Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete ResourceAccess #5626

Merged
merged 2 commits into from Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/Ryujinx.Graphics.GAL/ResourceLayout.cs
Expand Up @@ -15,14 +15,6 @@ public enum ResourceType : byte
BufferImage,
}

public enum ResourceAccess : byte
{
None = 0,
Read = 1,
Write = 2,
ReadWrite = Read | Write,
}

[Flags]
public enum ResourceStages : byte
{
Expand Down Expand Up @@ -81,19 +73,17 @@ public bool Equals(ResourceDescriptor other)
public int Binding { get; }
public ResourceType Type { get; }
public ResourceStages Stages { get; }
public ResourceAccess Access { get; }

public ResourceUsage(int binding, ResourceType type, ResourceStages stages, ResourceAccess access)
public ResourceUsage(int binding, ResourceType type, ResourceStages stages)
{
Binding = binding;
Type = type;
Stages = stages;
Access = access;
}

public override int GetHashCode()
{
return HashCode.Combine(Binding, Type, Stages, Access);
return HashCode.Combine(Binding, Type, Stages);
}

public override bool Equals(object obj)
Expand All @@ -103,7 +93,7 @@ public override bool Equals(object obj)

public bool Equals(ResourceUsage other)
{
return Binding == other.Binding && Type == other.Type && Stages == other.Stages && Access == other.Access;
return Binding == other.Binding && Type == other.Type && Stages == other.Stages;
}

public static bool operator ==(ResourceUsage left, ResourceUsage right)
Expand Down
Expand Up @@ -490,32 +490,35 @@ public ITexture EnsureBufferTexture(int index, Format format)
/// </summary>
/// <param name="offset">Offset of the range</param>
/// <param name="size">Size of the range in bytes</param>
/// <param name="write">Indicates if the buffer contents will be modified</param>
/// <returns>Range</returns>
public BufferRange GetVertexDataBufferRange(int offset, int size)
public BufferRange GetVertexDataBufferRange(int offset, int size, bool write)
{
return new BufferRange(_vertexDataBuffer.Handle, offset, size);
return new BufferRange(_vertexDataBuffer.Handle, offset, size, write);
}

/// <summary>
/// Gets a range of the output geometry shader vertex buffer for binding.
/// </summary>
/// <param name="offset">Offset of the range</param>
/// <param name="size">Size of the range in bytes</param>
/// <param name="write">Indicates if the buffer contents will be modified</param>
/// <returns>Range</returns>
public BufferRange GetGeometryVertexDataBufferRange(int offset, int size)
public BufferRange GetGeometryVertexDataBufferRange(int offset, int size, bool write)
{
return new BufferRange(_geometryVertexDataBuffer.Handle, offset, size);
return new BufferRange(_geometryVertexDataBuffer.Handle, offset, size, write);
}

/// <summary>
/// Gets a range of the output geometry shader index buffer for binding.
/// </summary>
/// <param name="offset">Offset of the range</param>
/// <param name="size">Size of the range in bytes</param>
/// <param name="write">Indicates if the buffer contents will be modified</param>
/// <returns>Range</returns>
public BufferRange GetGeometryIndexDataBufferRange(int offset, int size)
public BufferRange GetGeometryIndexDataBufferRange(int offset, int size, bool write)
{
return new BufferRange(_geometryIndexDataBuffer.Handle, offset, size);
return new BufferRange(_geometryIndexDataBuffer.Handle, offset, size, write);
}

/// <summary>
Expand Down
Expand Up @@ -202,7 +202,7 @@ struct VtgAsComputeState
_context.Renderer.Pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(vertexInfoBinding, vertexInfoRange) });

int vertexDataBinding = _vertexAsCompute.Reservations.VertexOutputStorageBufferBinding;
BufferRange vertexDataRange = _vacContext.GetVertexDataBufferRange(_vertexDataOffset, _vertexDataSize);
BufferRange vertexDataRange = _vacContext.GetVertexDataBufferRange(_vertexDataOffset, _vertexDataSize, write: true);
_context.Renderer.Pipeline.SetStorageBuffers(stackalloc[] { new BufferAssignment(vertexDataBinding, vertexDataRange) });

_vacContext.VertexInfoBufferUpdater.Commit();
Expand Down Expand Up @@ -245,9 +245,9 @@ struct VtgAsComputeState
int geometryVbBinding = _geometryAsCompute.Reservations.GeometryVertexOutputStorageBufferBinding;
int geometryIbBinding = _geometryAsCompute.Reservations.GeometryIndexOutputStorageBufferBinding;

BufferRange vertexDataRange = _vacContext.GetVertexDataBufferRange(_vertexDataOffset, _vertexDataSize);
BufferRange vertexBuffer = _vacContext.GetGeometryVertexDataBufferRange(_geometryVertexDataOffset, _geometryVertexDataSize);
BufferRange indexBuffer = _vacContext.GetGeometryIndexDataBufferRange(_geometryIndexDataOffset, _geometryIndexDataSize);
BufferRange vertexDataRange = _vacContext.GetVertexDataBufferRange(_vertexDataOffset, _vertexDataSize, write: false);
BufferRange vertexBuffer = _vacContext.GetGeometryVertexDataBufferRange(_geometryVertexDataOffset, _geometryVertexDataSize, write: true);
BufferRange indexBuffer = _vacContext.GetGeometryIndexDataBufferRange(_geometryIndexDataOffset, _geometryIndexDataSize, write: true);

_context.Renderer.Pipeline.SetStorageBuffers(stackalloc[]
{
Expand Down Expand Up @@ -293,8 +293,8 @@ struct VtgAsComputeState

if (_geometryAsCompute != null)
{
BufferRange vertexBuffer = _vacContext.GetGeometryVertexDataBufferRange(_geometryVertexDataOffset, _geometryVertexDataSize);
BufferRange indexBuffer = _vacContext.GetGeometryIndexDataBufferRange(_geometryIndexDataOffset, _geometryIndexDataSize);
BufferRange vertexBuffer = _vacContext.GetGeometryVertexDataBufferRange(_geometryVertexDataOffset, _geometryVertexDataSize, write: false);
BufferRange indexBuffer = _vacContext.GetGeometryIndexDataBufferRange(_geometryIndexDataOffset, _geometryIndexDataSize, write: false);

_context.Renderer.Pipeline.SetProgram(_vertexPassthroughProgram);
_context.Renderer.Pipeline.SetIndexBuffer(indexBuffer, IndexType.UInt);
Expand All @@ -310,7 +310,7 @@ struct VtgAsComputeState
}
else
{
BufferRange vertexDataRange = _vacContext.GetVertexDataBufferRange(_vertexDataOffset, _vertexDataSize);
BufferRange vertexDataRange = _vacContext.GetVertexDataBufferRange(_vertexDataOffset, _vertexDataSize, write: false);

_context.Renderer.Pipeline.SetProgram(_vertexPassthroughProgram);
_context.Renderer.Pipeline.SetStorageBuffers(stackalloc[] { new BufferAssignment(vertexDataBinding, vertexDataRange) });
Expand Down
25 changes: 11 additions & 14 deletions src/Ryujinx.Graphics.Gpu/Shader/ShaderInfoBuilder.cs
Expand Up @@ -61,7 +61,7 @@ public ShaderInfoBuilder(GpuContext context, bool tfEnabled, bool vertexAsComput
}

AddDescriptor(SupportBufferStages, ResourceType.UniformBuffer, UniformSetIndex, 0, 1);
AddUsage(SupportBufferStages, ResourceType.UniformBuffer, ResourceAccess.Read, UniformSetIndex, 0, 1);
AddUsage(SupportBufferStages, ResourceType.UniformBuffer, UniformSetIndex, 0, 1);

ResourceReservationCounts rrc = new(!context.Capabilities.SupportsTransformFeedback && tfEnabled, vertexAsCompute);

Expand All @@ -73,16 +73,16 @@ public ShaderInfoBuilder(GpuContext context, bool tfEnabled, bool vertexAsComput
// TODO: Handle that better? Maybe we should only set the binding that are really needed on each shader.
ResourceStages stages = vertexAsCompute ? ResourceStages.Compute | ResourceStages.Vertex : VtgStages;

PopulateDescriptorAndUsages(stages, ResourceType.UniformBuffer, ResourceAccess.Read, UniformSetIndex, 1, rrc.ReservedConstantBuffers - 1);
PopulateDescriptorAndUsages(stages, ResourceType.StorageBuffer, ResourceAccess.ReadWrite, StorageSetIndex, 0, rrc.ReservedStorageBuffers);
PopulateDescriptorAndUsages(stages, ResourceType.BufferTexture, ResourceAccess.Read, TextureSetIndex, 0, rrc.ReservedTextures);
PopulateDescriptorAndUsages(stages, ResourceType.BufferImage, ResourceAccess.ReadWrite, ImageSetIndex, 0, rrc.ReservedImages);
PopulateDescriptorAndUsages(stages, ResourceType.UniformBuffer, UniformSetIndex, 1, rrc.ReservedConstantBuffers - 1);
PopulateDescriptorAndUsages(stages, ResourceType.StorageBuffer, StorageSetIndex, 0, rrc.ReservedStorageBuffers);
PopulateDescriptorAndUsages(stages, ResourceType.BufferTexture, TextureSetIndex, 0, rrc.ReservedTextures);
PopulateDescriptorAndUsages(stages, ResourceType.BufferImage, ImageSetIndex, 0, rrc.ReservedImages);
}

private void PopulateDescriptorAndUsages(ResourceStages stages, ResourceType type, ResourceAccess access, int setIndex, int start, int count)
private void PopulateDescriptorAndUsages(ResourceStages stages, ResourceType type, int setIndex, int start, int count)
{
AddDescriptor(stages, type, setIndex, start, count);
AddUsage(stages, type, access, setIndex, start, count);
AddUsage(stages, type, setIndex, start, count);
}

/// <summary>
Expand Down Expand Up @@ -174,15 +174,14 @@ private void AddDualDescriptor(ResourceStages stages, ResourceType type, Resourc
/// </summary>
/// <param name="stages">Shader stages where the resource is used</param>
/// <param name="type">Type of the resource</param>
/// <param name="access">How the resource is accessed by the shader stages where it is used</param>
/// <param name="setIndex">Descriptor set number where the resource will be bound</param>
/// <param name="binding">Binding number where the resource will be bound</param>
/// <param name="count">Number of resources bound at the binding location</param>
private void AddUsage(ResourceStages stages, ResourceType type, ResourceAccess access, int setIndex, int binding, int count)
private void AddUsage(ResourceStages stages, ResourceType type, int setIndex, int binding, int count)
{
for (int index = 0; index < count; index++)
{
_resourceUsages[setIndex].Add(new ResourceUsage(binding + index, type, stages, access));
_resourceUsages[setIndex].Add(new ResourceUsage(binding + index, type, stages));
}
}

Expand All @@ -200,8 +199,7 @@ private void AddUsage(IEnumerable<BufferDescriptor> buffers, ResourceStages stag
_resourceUsages[setIndex].Add(new ResourceUsage(
buffer.Binding,
isStorage ? ResourceType.StorageBuffer : ResourceType.UniformBuffer,
stages,
buffer.Flags.HasFlag(BufferUsageFlags.Write) ? ResourceAccess.ReadWrite : ResourceAccess.Read));
stages));
}
}

Expand All @@ -225,8 +223,7 @@ private void AddUsage(IEnumerable<TextureDescriptor> textures, ResourceStages st
_resourceUsages[setIndex].Add(new ResourceUsage(
texture.Binding,
type,
stages,
texture.Flags.HasFlag(TextureUsageFlags.ImageStore) ? ResourceAccess.ReadWrite : ResourceAccess.Read));
stages));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/Ryujinx.Graphics.Vulkan/ResourceBindingSegment.cs
Expand Up @@ -8,15 +8,13 @@ namespace Ryujinx.Graphics.Vulkan
public readonly int Count;
public readonly ResourceType Type;
public readonly ResourceStages Stages;
public readonly ResourceAccess Access;

public ResourceBindingSegment(int binding, int count, ResourceType type, ResourceStages stages, ResourceAccess access)
public ResourceBindingSegment(int binding, int count, ResourceType type, ResourceStages stages)
{
Binding = binding;
Count = count;
Type = type;
Stages = stages;
Access = access;
}
}
}
12 changes: 1 addition & 11 deletions src/Ryujinx.Graphics.Vulkan/ResourceLayoutBuilder.cs
Expand Up @@ -34,22 +34,12 @@ public ResourceLayoutBuilder Add(ResourceStages stages, ResourceType type, int b
_ => throw new ArgumentException($"Invalid resource type \"{type}\"."),
};

ResourceAccess access = IsReadOnlyType(type) ? ResourceAccess.Read : ResourceAccess.ReadWrite;

_resourceDescriptors[setIndex].Add(new ResourceDescriptor(binding, 1, type, stages));
_resourceUsages[setIndex].Add(new ResourceUsage(binding, type, stages, access));
_resourceUsages[setIndex].Add(new ResourceUsage(binding, type, stages));

return this;
}

private static bool IsReadOnlyType(ResourceType type)
{
return type == ResourceType.UniformBuffer ||
type == ResourceType.Sampler ||
type == ResourceType.TextureAndSampler ||
type == ResourceType.BufferTexture;
}

public ResourceLayout Build()
{
var descriptors = new ResourceDescriptorCollection[TotalSets];
Expand Down
21 changes: 5 additions & 16 deletions src/Ryujinx.Graphics.Vulkan/ShaderCollection.cs
Expand Up @@ -162,8 +162,7 @@ private static ResourceBindingSegment[][] BuildClearSegments(ReadOnlyCollection<
currentDescriptor.Binding,
currentCount,
currentDescriptor.Type,
currentDescriptor.Stages,
ResourceAccess.ReadWrite));
currentDescriptor.Stages));
}

currentDescriptor = descriptor;
Expand All @@ -181,8 +180,7 @@ private static ResourceBindingSegment[][] BuildClearSegments(ReadOnlyCollection<
currentDescriptor.Binding,
currentCount,
currentDescriptor.Type,
currentDescriptor.Stages,
ResourceAccess.ReadWrite));
currentDescriptor.Stages));
}

segments[setIndex] = currentSegments.ToArray();
Expand All @@ -206,25 +204,17 @@ private static ResourceBindingSegment[][] BuildBindingSegments(ReadOnlyCollectio
{
ResourceUsage usage = setUsages[setIndex].Usages[index];

// If the resource is not accessed, we don't need to update it.
if (usage.Access == ResourceAccess.None)
{
continue;
}

if (currentUsage.Binding + currentCount != usage.Binding ||
currentUsage.Type != usage.Type ||
currentUsage.Stages != usage.Stages ||
currentUsage.Access != usage.Access)
currentUsage.Stages != usage.Stages)
{
if (currentCount != 0)
{
currentSegments.Add(new ResourceBindingSegment(
currentUsage.Binding,
currentCount,
currentUsage.Type,
currentUsage.Stages,
currentUsage.Access));
currentUsage.Stages));
}

currentUsage = usage;
Expand All @@ -242,8 +232,7 @@ private static ResourceBindingSegment[][] BuildBindingSegments(ReadOnlyCollectio
currentUsage.Binding,
currentCount,
currentUsage.Type,
currentUsage.Stages,
currentUsage.Access));
currentUsage.Stages));
}

segments[setIndex] = currentSegments.ToArray();
Expand Down