Implement texture buffers (#1152)

* Implement texture buffers

* Throw NotSupportedException where appropriate
This commit is contained in:
gdkchan 2020-04-25 10:02:18 -03:00 committed by GitHub
parent a065dc1626
commit 3cb1fa0e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 291 additions and 135 deletions

View file

@ -12,8 +12,6 @@ namespace Ryujinx.Graphics.Gpu.Engine
private const int NsToTicksFractionNumerator = 384;
private const int NsToTicksFractionDenominator = 625;
private ulong _runningCounter;
private readonly CounterCache _counterCache = new CounterCache();
/// <summary>

View file

@ -259,7 +259,7 @@ namespace Ryujinx.Graphics.Gpu.Engine
{
UpdateStorageBuffers();
BufferManager.CommitBindings();
BufferManager.CommitGraphicsBindings();
TextureManager.CommitGraphicsBindings();
}

View file

@ -295,7 +295,9 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary>
public void SynchronizeMemory()
{
if (_sequenceNumber == _context.SequenceNumber && _hasData)
// Texture buffers are not handled here, instead they are invalidated (if modified)
// when the texture is bound. This is handled by the buffer manager.
if ((_sequenceNumber == _context.SequenceNumber && _hasData) || Info.Target == Target.TextureBuffer)
{
return;
}

View file

@ -69,7 +69,6 @@ namespace Ryujinx.Graphics.Gpu.Image
public void SetTextures(int stage, TextureBindingInfo[] bindings)
{
_textureBindings[stage] = bindings;
_textureState[stage] = new TextureStatePerStage[bindings.Length];
}
@ -81,7 +80,6 @@ namespace Ryujinx.Graphics.Gpu.Image
public void SetImages(int stage, TextureBindingInfo[] bindings)
{
_imageBindings[stage] = bindings;
_imageState[stage] = new TextureStatePerStage[bindings.Length];
}
@ -201,7 +199,7 @@ namespace Ryujinx.Graphics.Gpu.Image
}
else
{
packedId = ReadPackedId(stageIndex, binding.Handle);
packedId = ReadPackedId(stageIndex, binding.Handle, _textureBufferIndex);
}
int textureId = UnpackTextureId(packedId);
@ -227,6 +225,14 @@ namespace Ryujinx.Graphics.Gpu.Image
_context.Renderer.Pipeline.SetTexture(index, stage, hostTexture);
}
if (hostTexture != null && texture.Info.Target == Target.TextureBuffer)
{
// Ensure that the buffer texture is using the correct buffer as storage.
// Buffers are frequently re-created to accomodate larger data, so we need to re-bind
// to ensure we're not using a old buffer that was already deleted.
_context.Methods.BufferManager.SetBufferTextureStorage(hostTexture, texture.Address, texture.Size, _isCompute);
}
Sampler sampler = _samplerPool.Get(samplerId);
ISampler hostSampler = sampler?.HostSampler;
@ -258,8 +264,7 @@ namespace Ryujinx.Graphics.Gpu.Image
{
TextureBindingInfo binding = _imageBindings[stageIndex][index];
int packedId = ReadPackedId(stageIndex, binding.Handle);
int packedId = ReadPackedId(stageIndex, binding.Handle, _textureBufferIndex);
int textureId = UnpackTextureId(packedId);
Texture texture = pool.Get(textureId);
@ -284,8 +289,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <returns>The texture descriptor for the specified texture</returns>
public TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle)
{
int packedId = ReadPackedId(stageIndex, handle);
int packedId = ReadPackedId(stageIndex, handle, state.Get<int>(MethodOffset.TextureBufferIndex));
int textureId = UnpackTextureId(packedId);
var poolState = state.Get<PoolState>(MethodOffset.TexturePoolState);
@ -303,8 +307,9 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary>
/// <param name="stageIndex">The number of the shader stage where the texture is bound</param>
/// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
/// <param name="textureBufferIndex">Index of the constant buffer holding the texture handles</param>
/// <returns>The packed texture and sampler ID (the real texture handle)</returns>
private int ReadPackedId(int stageIndex, int wordOffset)
private int ReadPackedId(int stageIndex, int wordOffset, int textureBufferIndex)
{
ulong address;
@ -312,11 +317,11 @@ namespace Ryujinx.Graphics.Gpu.Image
if (_isCompute)
{
address = bufferManager.GetComputeUniformBufferAddress(_textureBufferIndex);
address = bufferManager.GetComputeUniformBufferAddress(textureBufferIndex);
}
else
{
address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, _textureBufferIndex);
address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, textureBufferIndex);
}
address += (uint)wordOffset * 4;

View file

@ -489,7 +489,11 @@ namespace Ryujinx.Graphics.Gpu.Image
// Calculate texture sizes, used to find all overlapping textures.
SizeInfo sizeInfo;
if (info.IsLinear)
if (info.Target == Target.TextureBuffer)
{
sizeInfo = new SizeInfo(info.Width * info.FormatInfo.BytesPerPixel);
}
else if (info.IsLinear)
{
sizeInfo = SizeCalculator.GetLinearTextureSize(
info.Stride,

View file

@ -273,6 +273,19 @@ namespace Ryujinx.Graphics.Gpu.Memory
return 0;
}
CreateBuffer(address, size);
return address;
}
/// <summary>
/// Creates a new buffer for the specified range, if it does not yet exist.
/// This can be used to ensure the existance of a buffer.
/// </summary>
/// <param name="address">Address of the buffer in memory</param>
/// <param name="size">Size of the buffer in bytes</param>
public void CreateBuffer(ulong address, ulong size)
{
ulong endAddress = address + size;
ulong alignedAddress = address & ~BufferAlignmentMask;
@ -285,9 +298,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
alignedEndAddress += BufferAlignmentSize;
}
CreateBuffer(alignedAddress, alignedEndAddress - alignedAddress);
return address;
CreateBufferAligned(alignedAddress, alignedEndAddress - alignedAddress);
}
/// <summary>
@ -297,7 +308,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// </summary>
/// <param name="address">Address of the buffer in guest memory</param>
/// <param name="size">Size in bytes of the buffer</param>
private void CreateBuffer(ulong address, ulong size)
private void CreateBufferAligned(ulong address, ulong size)
{
int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref _bufferOverlaps);
@ -339,6 +350,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
buffer.Dispose();
}
// Existing buffers were modified, we need to rebind everything.
_rebind = true;
}
}
@ -441,7 +453,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// Ensures that the graphics engine bindings are visible to the host GPU.
/// Note: this actually performs the binding using the host graphics API.
/// </summary>
public void CommitBindings()
public void CommitGraphicsBindings()
{
if (_indexBufferDirty || _rebind)
{
@ -606,6 +618,34 @@ namespace Ryujinx.Graphics.Gpu.Memory
}
}
/// <summary>
/// Sets the buffer storage of a buffer texture.
/// </summary>
/// <param name="texture">Buffer texture</param>
/// <param name="address">Address of the buffer in memory</param>
/// <param name="size">Size of the buffer in bytes</param>
/// <param name="compute">Indicates if the buffer texture belongs to the compute or graphics pipeline</param>
public void SetBufferTextureStorage(ITexture texture, ulong address, ulong size, bool compute)
{
CreateBuffer(address, size);
if (_rebind)
{
// We probably had to modify existing buffers to create the texture buffer,
// so rebind everything to ensure we're using the new buffers for all bound resources.
if (compute)
{
CommitComputeBindings();
}
else
{
CommitGraphicsBindings();
}
}
texture.SetStorage(GetBufferRange(address, size));
}
/// <summary>
/// Copy a buffer data from a given address to another.
/// </summary>

View file

@ -280,8 +280,12 @@ namespace Ryujinx.Graphics.Gpu.Shader
=> localMemorySize,
QueryInfoName.ComputeSharedMemorySize
=> sharedMemorySize,
QueryInfoName.IsTextureBuffer
=> Convert.ToInt32(QueryIsTextureBuffer(state, 0, index, compute: true)),
QueryInfoName.IsTextureRectangle
=> Convert.ToInt32(QueryIsTextureRectangle(state, 0, index, compute: true)),
QueryInfoName.TextureFormat
=> (int)QueryComputeTextureFormat(state, index),
=> (int)QueryTextureFormat(state, 0, index, compute: true),
_
=> QueryInfoCommon(info)
};
@ -331,13 +335,13 @@ namespace Ryujinx.Graphics.Gpu.Shader
return info switch
{
QueryInfoName.IsTextureBuffer
=> Convert.ToInt32(QueryIsTextureBuffer(state, (int)stage - 1, index)),
=> Convert.ToInt32(QueryIsTextureBuffer(state, (int)stage - 1, index, compute: false)),
QueryInfoName.IsTextureRectangle
=> Convert.ToInt32(QueryIsTextureRectangle(state, (int)stage - 1, index)),
=> Convert.ToInt32(QueryIsTextureRectangle(state, (int)stage - 1, index, compute: false)),
QueryInfoName.PrimitiveTopology
=> (int)QueryPrimitiveTopology(),
QueryInfoName.TextureFormat
=> (int)QueryGraphicsTextureFormat(state, (int)stage - 1, index),
=> (int)QueryTextureFormat(state, (int)stage - 1, index, compute: false),
_
=> QueryInfoCommon(info)
};
@ -429,11 +433,12 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="stageIndex">Index of the shader stage</param>
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
/// <returns>True if the texture is a buffer texture, false otherwise</returns>
private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int index)
private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int handle, bool compute)
{
return GetGraphicsTextureDescriptor(state, stageIndex, index).UnpackTextureTarget() == TextureTarget.TextureBuffer;
return GetTextureDescriptor(state, stageIndex, handle, compute).UnpackTextureTarget() == TextureTarget.TextureBuffer;
}
/// <summary>
@ -443,11 +448,12 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="stageIndex">Index of the shader stage</param>
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
/// <returns>True if the texture is a rectangle texture, false otherwise</returns>
private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int index)
private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int handle, bool compute)
{
var descriptor = GetGraphicsTextureDescriptor(state, stageIndex, index);
var descriptor = GetTextureDescriptor(state, stageIndex, handle, compute);
TextureTarget target = descriptor.UnpackTextureTarget();
@ -461,23 +467,13 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// Queries the format of a given texture.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="stageIndex">Index of the shader stage. This is ignored if <paramref name="compute"/> is true</param>
/// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
/// <returns>The texture format</returns>
private TextureFormat QueryComputeTextureFormat(GpuState state, int index)
private TextureFormat QueryTextureFormat(GpuState state, int stageIndex, int handle, bool compute)
{
return QueryTextureFormat(GetComputeTextureDescriptor(state, index));
}
/// <summary>
/// Queries the format of a given texture.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="stageIndex">Index of the shader stage</param>
/// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
/// <returns>The texture format</returns>
private TextureFormat QueryGraphicsTextureFormat(GpuState state, int stageIndex, int index)
{
return QueryTextureFormat(GetGraphicsTextureDescriptor(state, stageIndex, index));
return QueryTextureFormat(GetTextureDescriptor(state, stageIndex, handle, compute));
}
/// <summary>
@ -541,23 +537,20 @@ namespace Ryujinx.Graphics.Gpu.Shader
/// Gets the texture descriptor for a given texture on the pool.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="stageIndex">Index of the shader stage. This is ignored if <paramref name="compute"/> is true</param>
/// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
/// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
/// <returns>Texture descriptor</returns>
private TextureDescriptor GetComputeTextureDescriptor(GpuState state, int handle)
private TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle, bool compute)
{
return _context.Methods.TextureManager.GetComputeTextureDescriptor(state, handle);
}
/// <summary>
/// Gets the texture descriptor for a given texture on the pool.
/// </summary>
/// <param name="state">Current GPU state</param>
/// <param name="stageIndex">Index of the shader stage</param>
/// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
/// <returns>Texture descriptor</returns>
private TextureDescriptor GetGraphicsTextureDescriptor(GpuState state, int stageIndex, int handle)
{
return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, handle);
if (compute)
{
return _context.Methods.TextureManager.GetComputeTextureDescriptor(state, handle);
}
else
{
return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, handle);
}
}
/// <summary>