Set texture/image bindings in place rather than allocating and passing an array (#2647)

* Remove allocations for texture bindings and state

* Rent rather than stackalloc + copy

A bit faster.
This commit is contained in:
riperiperi 2021-09-19 13:03:05 +01:00 committed by GitHub
parent 32c09af71a
commit b0af010247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 47 deletions

View File

@ -191,7 +191,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Compute
_channel.BufferManager.SetComputeStorageBufferBindings(info.SBuffers); _channel.BufferManager.SetComputeStorageBufferBindings(info.SBuffers);
_channel.BufferManager.SetComputeUniformBufferBindings(info.CBuffers); _channel.BufferManager.SetComputeUniformBufferBindings(info.CBuffers);
var textureBindings = new TextureBindingInfo[info.Textures.Count]; TextureBindingInfo[] textureBindings = _channel.TextureManager.RentComputeTextureBindings(info.Textures.Count);
for (int index = 0; index < info.Textures.Count; index++) for (int index = 0; index < info.Textures.Count; index++)
{ {
@ -207,9 +207,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Compute
descriptor.Flags); descriptor.Flags);
} }
_channel.TextureManager.SetComputeTextures(textureBindings); TextureBindingInfo[] imageBindings = _channel.TextureManager.RentComputeImageBindings(info.Images.Count);
var imageBindings = new TextureBindingInfo[info.Images.Count];
for (int index = 0; index < info.Images.Count; index++) for (int index = 0; index < info.Images.Count; index++)
{ {
@ -227,8 +225,6 @@ namespace Ryujinx.Graphics.Gpu.Engine.Compute
descriptor.Flags); descriptor.Flags);
} }
_channel.TextureManager.SetComputeImages(imageBindings);
_channel.TextureManager.CommitComputeBindings(); _channel.TextureManager.CommitComputeBindings();
_channel.BufferManager.CommitComputeBindings(); _channel.BufferManager.CommitComputeBindings();

View File

@ -970,14 +970,14 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
if (info == null) if (info == null)
{ {
_channel.TextureManager.SetGraphicsTextures(stage, Array.Empty<TextureBindingInfo>()); _channel.TextureManager.RentGraphicsTextureBindings(stage, 0);
_channel.TextureManager.SetGraphicsImages(stage, Array.Empty<TextureBindingInfo>()); _channel.TextureManager.RentGraphicsImageBindings(stage, 0);
_channel.BufferManager.SetGraphicsStorageBufferBindings(stage, null); _channel.BufferManager.SetGraphicsStorageBufferBindings(stage, null);
_channel.BufferManager.SetGraphicsUniformBufferBindings(stage, null); _channel.BufferManager.SetGraphicsUniformBufferBindings(stage, null);
continue; continue;
} }
var textureBindings = new TextureBindingInfo[info.Textures.Count]; Span<TextureBindingInfo> textureBindings = _channel.TextureManager.RentGraphicsTextureBindings(stage, info.Textures.Count);
for (int index = 0; index < info.Textures.Count; index++) for (int index = 0; index < info.Textures.Count; index++)
{ {
@ -993,9 +993,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
descriptor.Flags); descriptor.Flags);
} }
_channel.TextureManager.SetGraphicsTextures(stage, textureBindings); TextureBindingInfo[] imageBindings = _channel.TextureManager.RentGraphicsImageBindings(stage, info.Images.Count);
var imageBindings = new TextureBindingInfo[info.Images.Count];
for (int index = 0; index < info.Images.Count; index++) for (int index = 0; index < info.Images.Count; index++)
{ {
@ -1013,8 +1011,6 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
descriptor.Flags); descriptor.Flags);
} }
_channel.TextureManager.SetGraphicsImages(stage, imageBindings);
_channel.BufferManager.SetGraphicsStorageBufferBindings(stage, info.SBuffers); _channel.BufferManager.SetGraphicsStorageBufferBindings(stage, info.SBuffers);
_channel.BufferManager.SetGraphicsUniformBufferBindings(stage, info.CBuffers); _channel.BufferManager.SetGraphicsUniformBufferBindings(stage, info.CBuffers);
} }

View File

@ -11,6 +11,9 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
class TextureBindingsManager : IDisposable class TextureBindingsManager : IDisposable
{ {
private const int InitialTextureStateSize = 32;
private const int InitialImageStateSize = 8;
private const int HandleHigh = 16; private const int HandleHigh = 16;
private const int HandleMask = (1 << HandleHigh) - 1; private const int HandleMask = (1 << HandleHigh) - 1;
@ -43,6 +46,9 @@ namespace Ryujinx.Graphics.Gpu.Image
private readonly TextureStatePerStage[][] _textureState; private readonly TextureStatePerStage[][] _textureState;
private readonly TextureStatePerStage[][] _imageState; private readonly TextureStatePerStage[][] _imageState;
private int[] _textureBindingsCount;
private int[] _imageBindingsCount;
private int _textureBufferIndex; private int _textureBufferIndex;
private bool _rebind; private bool _rebind;
@ -72,6 +78,18 @@ namespace Ryujinx.Graphics.Gpu.Image
_textureState = new TextureStatePerStage[stages][]; _textureState = new TextureStatePerStage[stages][];
_imageState = new TextureStatePerStage[stages][]; _imageState = new TextureStatePerStage[stages][];
_textureBindingsCount = new int[stages];
_imageBindingsCount = new int[stages];
for (int stage = 0; stage < stages; stage++)
{
_textureBindings[stage] = new TextureBindingInfo[InitialTextureStateSize];
_imageBindings[stage] = new TextureBindingInfo[InitialImageStateSize];
_textureState[stage] = new TextureStatePerStage[InitialTextureStateSize];
_imageState[stage] = new TextureStatePerStage[InitialImageStateSize];
}
_scales = new float[64]; _scales = new float[64];
for (int i = 0; i < 64; i++) for (int i = 0; i < 64; i++)
@ -81,25 +99,57 @@ namespace Ryujinx.Graphics.Gpu.Image
} }
/// <summary> /// <summary>
/// Binds textures for a given shader stage. /// Rents the texture bindings array for a given stage, so that they can be modified.
/// </summary> /// </summary>
/// <param name="stage">Shader stage number, or 0 for compute shaders</param> /// <param name="stage">Shader stage number, or 0 for compute shaders</param>
/// <param name="bindings">Texture bindings</param> /// <param name="count">The number of bindings needed</param>
public void SetTextures(int stage, TextureBindingInfo[] bindings) /// <returns>The texture bindings array</returns>
public TextureBindingInfo[] RentTextureBindings(int stage, int count)
{ {
_textureBindings[stage] = bindings; if (count > _textureBindings[stage].Length)
_textureState[stage] = new TextureStatePerStage[bindings.Length]; {
Array.Resize(ref _textureBindings[stage], count);
Array.Resize(ref _textureState[stage], count);
}
int toClear = Math.Max(_textureBindingsCount[stage], count);
TextureStatePerStage[] state = _textureState[stage];
for (int i = 0; i < toClear; i++)
{
state[i] = new TextureStatePerStage();
}
_textureBindingsCount[stage] = count;
return _textureBindings[stage];
} }
/// <summary> /// <summary>
/// Binds images for a given shader stage. /// Rents the image bindings array for a given stage, so that they can be modified.
/// </summary> /// </summary>
/// <param name="stage">Shader stage number, or 0 for compute shaders</param> /// <param name="stage">Shader stage number, or 0 for compute shaders</param>
/// <param name="bindings">Image bindings</param> /// <param name="count">The number of bindings needed</param>
public void SetImages(int stage, TextureBindingInfo[] bindings) /// <returns>The image bindings array</returns>
public TextureBindingInfo[] RentImageBindings(int stage, int count)
{ {
_imageBindings[stage] = bindings; if (count > _imageBindings[stage].Length)
_imageState[stage] = new TextureStatePerStage[bindings.Length]; {
Array.Resize(ref _imageBindings[stage], count);
Array.Resize(ref _imageState[stage], count);
}
int toClear = Math.Max(_imageBindingsCount[stage], count);
TextureStatePerStage[] state = _imageState[stage];
for (int i = 0; i < toClear; i++)
{
state[i] = new TextureStatePerStage();
}
_imageBindingsCount[stage] = count;
return _imageBindings[stage];
} }
/// <summary> /// <summary>
@ -235,7 +285,7 @@ namespace Ryujinx.Graphics.Gpu.Image
{ {
if (_scaleChanged) if (_scaleChanged)
{ {
_context.Renderer.Pipeline.UpdateRenderScale(stage, _scales, _textureBindings[stageIndex]?.Length ?? 0, _imageBindings[stageIndex]?.Length ?? 0); _context.Renderer.Pipeline.UpdateRenderScale(stage, _scales, _textureBindingsCount[stageIndex], _imageBindingsCount[stageIndex]);
_scaleChanged = false; _scaleChanged = false;
} }
@ -285,7 +335,8 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="stageIndex">The stage number of the specified shader stage</param> /// <param name="stageIndex">The stage number of the specified shader stage</param>
private void CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex) private void CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex)
{ {
if (_textureBindings[stageIndex] == null || _textureBindings[stageIndex].Length == 0) int textureCount = _textureBindingsCount[stageIndex];
if (textureCount == 0)
{ {
return; return;
} }
@ -304,7 +355,7 @@ namespace Ryujinx.Graphics.Gpu.Image
return; return;
} }
for (int index = 0; index < _textureBindings[stageIndex].Length; index++) for (int index = 0; index < textureCount; index++)
{ {
TextureBindingInfo bindingInfo = _textureBindings[stageIndex][index]; TextureBindingInfo bindingInfo = _textureBindings[stageIndex][index];
@ -381,21 +432,22 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="stageIndex">The stage number of the specified shader stage</param> /// <param name="stageIndex">The stage number of the specified shader stage</param>
private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex) private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex)
{ {
if (_imageBindings[stageIndex] == null) int imageCount = _imageBindingsCount[stageIndex];
if (imageCount == 0)
{ {
return; return;
} }
if (pool == null && _imageBindings[stageIndex].Length != 0) if (pool == null)
{ {
Logger.Error?.Print(LogClass.Gpu, $"Shader stage \"{stage}\" uses images, but texture pool was not set."); Logger.Error?.Print(LogClass.Gpu, $"Shader stage \"{stage}\" uses images, but texture pool was not set.");
return; return;
} }
// Scales for images appear after the texture ones. // Scales for images appear after the texture ones.
int baseScaleIndex = _textureBindings[stageIndex]?.Length ?? 0; int baseScaleIndex = _textureBindingsCount[stageIndex];
for (int index = 0; index < _imageBindings[stageIndex].Length; index++) for (int index = 0; index < imageCount; index++)
{ {
TextureBindingInfo bindingInfo = _imageBindings[stageIndex][index]; TextureBindingInfo bindingInfo = _imageBindings[stageIndex][index];

View File

@ -43,41 +43,45 @@ namespace Ryujinx.Graphics.Gpu.Image
} }
/// <summary> /// <summary>
/// Sets texture bindings on the compute pipeline. /// Rents the texture bindings array of the compute pipeline.
/// </summary> /// </summary>
/// <param name="bindings">The texture bindings</param> /// <param name="count">The number of bindings needed</param>
public void SetComputeTextures(TextureBindingInfo[] bindings) /// <returns>The texture bindings array</returns>
public TextureBindingInfo[] RentComputeTextureBindings(int count)
{ {
_cpBindingsManager.SetTextures(0, bindings); return _cpBindingsManager.RentTextureBindings(0, count);
} }
/// <summary> /// <summary>
/// Sets texture bindings on the graphics pipeline. /// Rents the texture bindings array for a given stage on the graphics pipeline.
/// </summary> /// </summary>
/// <param name="stage">The index of the shader stage to bind the textures</param> /// <param name="stage">The index of the shader stage to bind the textures</param>
/// <param name="bindings">The texture bindings</param> /// <param name="count">The number of bindings needed</param>
public void SetGraphicsTextures(int stage, TextureBindingInfo[] bindings) /// <returns>The texture bindings array</returns>
public TextureBindingInfo[] RentGraphicsTextureBindings(int stage, int count)
{ {
_gpBindingsManager.SetTextures(stage, bindings); return _gpBindingsManager.RentTextureBindings(stage, count);
} }
/// <summary> /// <summary>
/// Sets image bindings on the compute pipeline. /// Rents the image bindings array of the compute pipeline.
/// </summary> /// </summary>
/// <param name="bindings">The image bindings</param> /// <param name="count">The number of bindings needed</param>
public void SetComputeImages(TextureBindingInfo[] bindings) /// <returns>The image bindings array</returns>
public TextureBindingInfo[] RentComputeImageBindings(int count)
{ {
_cpBindingsManager.SetImages(0, bindings); return _cpBindingsManager.RentImageBindings(0, count);
} }
/// <summary> /// <summary>
/// Sets image bindings on the graphics pipeline. /// Rents the image bindings array for a given stage on the graphics pipeline.
/// </summary> /// </summary>
/// <param name="stage">The index of the shader stage to bind the images</param> /// <param name="stage">The index of the shader stage to bind the images</param>
/// <param name="bindings">The image bindings</param> /// <param name="count">The number of bindings needed</param>
public void SetGraphicsImages(int stage, TextureBindingInfo[] bindings) /// <returns>The image bindings array</returns>
public TextureBindingInfo[] RentGraphicsImageBindings(int stage, int count)
{ {
_gpBindingsManager.SetImages(stage, bindings); return _gpBindingsManager.RentImageBindings(stage, count);
} }
/// <summary> /// <summary>