Memory Changes part 2

* Slightly refactors RangeLists from the last Memory Changes MR, which fixes issue 61.

* Convert as many const size array iterators to span iterators as possible. When iterating over a const size array, every iteration created a Span, now only the first iteration does in most places.

* Now using object pooling for a few object types that were rapidly deleted and recreated.

* Converted a few flag checks to binary operations to save memory allocations.
This commit is contained in:
LotP 2025-08-25 17:44:15 -05:00 committed by KeatonTheBot
parent ff0daa9f35
commit 3deddbd491
89 changed files with 2121 additions and 1154 deletions

View file

@ -422,9 +422,11 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
/// </summary>
private void UpdateTfBufferState()
{
Span<TfBufferState> tfBufferStateSpan = _state.State.TfBufferState.AsSpan();
for (int index = 0; index < Constants.TotalTransformFeedbackBuffers; index++)
{
TfBufferState tfb = _state.State.TfBufferState[index];
TfBufferState tfb = tfBufferStateSpan[index];
if (!tfb.Enable)
{
@ -465,10 +467,10 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
var memoryManager = _channel.MemoryManager;
var rtControl = _state.State.RtControl;
bool useControl = updateFlags.HasFlag(RenderTargetUpdateFlags.UseControl);
bool layered = updateFlags.HasFlag(RenderTargetUpdateFlags.Layered);
bool singleColor = updateFlags.HasFlag(RenderTargetUpdateFlags.SingleColor);
bool discard = updateFlags.HasFlag(RenderTargetUpdateFlags.DiscardClip);
bool useControl = (updateFlags & RenderTargetUpdateFlags.UseControl) == RenderTargetUpdateFlags.UseControl;
bool layered = (updateFlags & RenderTargetUpdateFlags.Layered) == RenderTargetUpdateFlags.Layered;
bool singleColor = (updateFlags & RenderTargetUpdateFlags.SingleColor) == RenderTargetUpdateFlags.SingleColor;
bool discard = (updateFlags & RenderTargetUpdateFlags.DiscardClip) == RenderTargetUpdateFlags.DiscardClip;
int count = useControl ? rtControl.UnpackCount() : Constants.TotalRenderTargets;
@ -486,11 +488,13 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
bool changedScale = false;
uint rtNoAlphaMask = 0;
Span<RtColorState> rtColorStateSpan = _state.State.RtColorState.AsSpan();
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
int rtIndex = useControl ? rtControl.UnpackPermutationIndex(index) : index;
var colorState = _state.State.RtColorState[rtIndex];
var colorState = rtColorStateSpan[rtIndex];
if (index >= count || !IsRtEnabled(colorState) || (singleColor && index != singleUse))
{
@ -538,7 +542,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
Image.Texture depthStencil = null;
if (dsEnable && updateFlags.HasFlag(RenderTargetUpdateFlags.UpdateDepthStencil))
if (dsEnable && (updateFlags & RenderTargetUpdateFlags.UpdateDepthStencil) == RenderTargetUpdateFlags.UpdateDepthStencil)
{
var dsState = _state.State.RtDepthStencilState;
var dsSize = _state.State.RtDepthStencilSize;
@ -598,7 +602,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
/// </summary>
public void UpdateRenderTargetSpecialization()
{
_currentSpecState.SetFragmentOutputTypes(_state.State.RtControl, ref _state.State.RtColorState);
_currentSpecState.SetFragmentOutputTypes(_state.State.RtControl, _state.State.RtColorState.AsSpan());
}
/// <summary>
@ -623,10 +627,11 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
const int MaxH = 0xffff;
Span<Rectangle<int>> regions = stackalloc Rectangle<int>[Constants.TotalViewports];
Span<ScissorState> scissorStateSpan = _state.State.ScissorState.AsSpan();
for (int index = 0; index < Constants.TotalViewports; index++)
{
ScissorState scissor = _state.State.ScissorState[index];
ScissorState scissor = scissorStateSpan[index];
bool enable = scissor.Enable && (scissor.X1 != MinX ||
scissor.Y1 != MinY ||
@ -730,6 +735,8 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
UpdateDepthMode();
Span<Viewport> viewports = stackalloc Viewport[Constants.TotalViewports];
Span<ViewportTransform> viewportTransformSpan = _state.State.ViewportTransform.AsSpan();
Span<ViewportExtents> viewportExtentsSpan = _state.State.ViewportExtents.AsSpan();
for (int index = 0; index < Constants.TotalViewports; index++)
{
@ -744,8 +751,8 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
continue;
}
ref var transform = ref _state.State.ViewportTransform[index];
ref var extents = ref _state.State.ViewportExtents[index];
ref var transform = ref viewportTransformSpan[index];
ref var extents = ref viewportExtentsSpan[index];
float scaleX = MathF.Abs(transform.ScaleX);
float scaleY = transform.ScaleY;
@ -967,10 +974,11 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
uint vbEnableMask = _vbEnableMask;
Span<VertexAttribDescriptor> vertexAttribs = stackalloc VertexAttribDescriptor[Constants.TotalVertexAttribs];
Span<VertexAttribState> vertexAttribStateSpan = _state.State.VertexAttribState.AsSpan();
for (int index = 0; index < Constants.TotalVertexAttribs; index++)
{
var vertexAttrib = _state.State.VertexAttribState[index];
var vertexAttrib = vertexAttribStateSpan[index];
int bufferIndex = vertexAttrib.UnpackBufferIndex();
@ -1014,7 +1022,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
_pipeline.SetVertexAttribs(vertexAttribs);
_context.Renderer.Pipeline.SetVertexAttribs(vertexAttribs);
_currentSpecState.SetAttributeTypes(ref _state.State.VertexAttribState);
_currentSpecState.SetAttributeTypes(_state.State.VertexAttribState.AsSpan());
}
/// <summary>
@ -1105,20 +1113,25 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
int drawFirstVertex = _drawState.DrawFirstVertex;
int drawVertexCount = _drawState.DrawVertexCount;
uint vbEnableMask = 0;
Span<VertexBufferState> vertexBufferStateSpan = _state.State.VertexBufferState.AsSpan();
Span<BufferPipelineDescriptor> vertexBuffersSpan = _pipeline.VertexBuffers.AsSpan();
Span<GpuVa> vertexBufferEndAddressSpan = _state.State.VertexBufferEndAddress.AsSpan();
Span<Boolean32> vertexBufferInstancedSpan = _state.State.VertexBufferInstanced.AsSpan();
for (int index = 0; index < Constants.TotalVertexBuffers; index++)
{
var vertexBuffer = _state.State.VertexBufferState[index];
var vertexBuffer = vertexBufferStateSpan[index];
if (!vertexBuffer.UnpackEnable())
{
_pipeline.VertexBuffers[index] = new BufferPipelineDescriptor(false, 0, 0);
vertexBuffersSpan[index] = new BufferPipelineDescriptor(false, 0, 0);
_channel.BufferManager.SetVertexBuffer(index, 0, 0, 0, 0);
continue;
}
GpuVa endAddress = _state.State.VertexBufferEndAddress[index];
GpuVa endAddress = vertexBufferEndAddressSpan[index];
ulong address = vertexBuffer.Address.Pack();
@ -1129,7 +1142,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
int stride = vertexBuffer.UnpackStride();
bool instanced = _state.State.VertexBufferInstanced[index];
bool instanced = vertexBufferInstancedSpan[index];
int divisor = instanced ? vertexBuffer.Divisor : 0;
@ -1176,7 +1189,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
size = Math.Min(vbSize, (ulong)((firstInstance + drawFirstVertex + drawVertexCount) * stride));
}
_pipeline.VertexBuffers[index] = new BufferPipelineDescriptor(_channel.MemoryManager.IsMapped(address), stride, divisor);
vertexBuffersSpan[index] = new BufferPipelineDescriptor(_channel.MemoryManager.IsMapped(address), stride, divisor);
_channel.BufferManager.SetVertexBuffer(index, address, size, stride, divisor);
}
@ -1229,10 +1242,12 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
bool rtColorMaskShared = _state.State.RtColorMaskShared;
Span<uint> componentMasks = stackalloc uint[Constants.TotalRenderTargets];
Span<RtColorMask> rtColorMaskSpan = _state.State.RtColorMask.AsSpan();
Span<uint> colorWriteMaskSpan = _pipeline.ColorWriteMask.AsSpan();
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
var colorMask = _state.State.RtColorMask[rtColorMaskShared ? 0 : index];
var colorMask = rtColorMaskSpan[rtColorMaskShared ? 0 : index];
uint componentMask;
@ -1242,7 +1257,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
componentMask |= (colorMask.UnpackAlpha() ? 8u : 0u);
componentMasks[index] = componentMask;
_pipeline.ColorWriteMask[index] = componentMask;
colorWriteMaskSpan[index] = componentMask;
}
_context.Renderer.Pipeline.SetRenderTargetColorMasks(componentMasks);
@ -1274,10 +1289,14 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
if (blendIndependent)
{
Span<Boolean32> blendEnableSpan = _state.State.BlendEnable.AsSpan();
Span<BlendState> blendStateSpan = _state.State.BlendState.AsSpan();
Span<BlendDescriptor> blendDescriptorsSpan = _pipeline.BlendDescriptors.AsSpan();
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
bool enable = _state.State.BlendEnable[index];
var blend = _state.State.BlendState[index];
bool enable = blendEnableSpan[index];
var blend = blendStateSpan[index];
var descriptor = new BlendDescriptor(
enable,
@ -1298,7 +1317,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
dualSourceBlendEnabled = true;
}
_pipeline.BlendDescriptors[index] = descriptor;
blendDescriptorsSpan[index] = descriptor;
_context.Renderer.Pipeline.SetBlendState(index, descriptor);
}
}
@ -1325,10 +1344,12 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
{
dualSourceBlendEnabled = true;
}
Span<BlendDescriptor> blendDescriptorsSpan = _pipeline.BlendDescriptors.AsSpan();
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
_pipeline.BlendDescriptors[index] = descriptor;
blendDescriptorsSpan[index] = descriptor;
_context.Renderer.Pipeline.SetBlendState(index, descriptor);
}
}
@ -1414,12 +1435,13 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
ShaderAddresses addresses = new();
Span<ulong> addressesSpan = addresses.AsSpan();
Span<ShaderState> shaderStateSpan = _state.State.ShaderState.AsSpan();
ulong baseAddress = _state.State.ShaderBaseAddress.Pack();
for (int index = 0; index < 6; index++)
{
var shader = _state.State.ShaderState[index];
var shader = shaderStateSpan[index];
if (!shader.UnpackEnable() && index != 1)
{
continue;