Ensure that vertex attribute buffer index is valid on GPU (#3942)

* Ensure that vertex attribute buffer index is valid on GPU

* Remove vertex buffer validation code from OpenGL

* Remove some fields that are no longer necessary
This commit is contained in:
gdkchan 2022-11-30 18:06:40 -03:00 committed by GitHub
parent af01100050
commit 8750b90a7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 41 deletions

View file

@ -37,6 +37,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
private bool _vsUsesDrawParameters; private bool _vsUsesDrawParameters;
private bool _vtgWritesRtLayer; private bool _vtgWritesRtLayer;
private byte _vsClipDistancesWritten; private byte _vsClipDistancesWritten;
private uint _vbEnableMask;
private bool _prevDrawIndexed; private bool _prevDrawIndexed;
private bool _prevDrawIndirect; private bool _prevDrawIndirect;
@ -76,6 +77,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
nameof(ThreedClassState.VertexBufferState), nameof(ThreedClassState.VertexBufferState),
nameof(ThreedClassState.VertexBufferEndAddress)), nameof(ThreedClassState.VertexBufferEndAddress)),
// Must be done after vertex buffer updates.
new StateUpdateCallbackEntry(UpdateVertexAttribState, nameof(ThreedClassState.VertexAttribState)), new StateUpdateCallbackEntry(UpdateVertexAttribState, nameof(ThreedClassState.VertexAttribState)),
new StateUpdateCallbackEntry(UpdateBlendState, new StateUpdateCallbackEntry(UpdateBlendState,
@ -852,12 +854,23 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
/// </summary> /// </summary>
private void UpdateVertexAttribState() private void UpdateVertexAttribState()
{ {
uint vbEnableMask = _vbEnableMask;
Span<VertexAttribDescriptor> vertexAttribs = stackalloc VertexAttribDescriptor[Constants.TotalVertexAttribs]; Span<VertexAttribDescriptor> vertexAttribs = stackalloc VertexAttribDescriptor[Constants.TotalVertexAttribs];
for (int index = 0; index < Constants.TotalVertexAttribs; index++) for (int index = 0; index < Constants.TotalVertexAttribs; index++)
{ {
var vertexAttrib = _state.State.VertexAttribState[index]; var vertexAttrib = _state.State.VertexAttribState[index];
int bufferIndex = vertexAttrib.UnpackBufferIndex();
if ((vbEnableMask & (1u << bufferIndex)) == 0)
{
// Using a vertex buffer that doesn't exist is invalid, so let's use a dummy attribute for those cases.
vertexAttribs[index] = new VertexAttribDescriptor(0, 0, true, Format.R32G32B32A32Float);
continue;
}
if (!FormatTable.TryGetAttribFormat(vertexAttrib.UnpackFormat(), out Format format)) if (!FormatTable.TryGetAttribFormat(vertexAttrib.UnpackFormat(), out Format format))
{ {
Logger.Debug?.Print(LogClass.Gpu, $"Invalid attribute format 0x{vertexAttrib.UnpackFormat():X}."); Logger.Debug?.Print(LogClass.Gpu, $"Invalid attribute format 0x{vertexAttrib.UnpackFormat():X}.");
@ -866,7 +879,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
} }
vertexAttribs[index] = new VertexAttribDescriptor( vertexAttribs[index] = new VertexAttribDescriptor(
vertexAttrib.UnpackBufferIndex(), bufferIndex,
vertexAttrib.UnpackOffset(), vertexAttrib.UnpackOffset(),
vertexAttrib.UnpackIsConstant(), vertexAttrib.UnpackIsConstant(),
format); format);
@ -954,6 +967,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
bool drawIndexed = _drawState.DrawIndexed; bool drawIndexed = _drawState.DrawIndexed;
bool drawIndirect = _drawState.DrawIndirect; bool drawIndirect = _drawState.DrawIndirect;
uint vbEnableMask = 0;
for (int index = 0; index < Constants.TotalVertexBuffers; index++) for (int index = 0; index < Constants.TotalVertexBuffers; index++)
{ {
@ -971,6 +985,11 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
ulong address = vertexBuffer.Address.Pack(); ulong address = vertexBuffer.Address.Pack();
if (_channel.MemoryManager.IsMapped(address))
{
vbEnableMask |= 1u << index;
}
int stride = vertexBuffer.UnpackStride(); int stride = vertexBuffer.UnpackStride();
bool instanced = _state.State.VertexBufferInstanced[index]; bool instanced = _state.State.VertexBufferInstanced[index];
@ -1017,6 +1036,12 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
_pipeline.VertexBuffers[index] = new BufferPipelineDescriptor(_channel.MemoryManager.IsMapped(address), stride, divisor); _pipeline.VertexBuffers[index] = new BufferPipelineDescriptor(_channel.MemoryManager.IsMapped(address), stride, divisor);
_channel.BufferManager.SetVertexBuffer(index, address, size, stride, divisor); _channel.BufferManager.SetVertexBuffer(index, address, size, stride, divisor);
} }
if (_vbEnableMask != vbEnableMask)
{
_vbEnableMask = vbEnableMask;
UpdateVertexAttribState();
}
} }
/// <summary> /// <summary>

View file

@ -10,13 +10,9 @@ namespace Ryujinx.Graphics.OpenGL
{ {
public int Handle { get; private set; } public int Handle { get; private set; }
private bool _needsAttribsUpdate;
private readonly VertexAttribDescriptor[] _vertexAttribs; private readonly VertexAttribDescriptor[] _vertexAttribs;
private readonly VertexBufferDescriptor[] _vertexBuffers; private readonly VertexBufferDescriptor[] _vertexBuffers;
private int _vertexAttribsCount;
private int _vertexBuffersCount;
private int _minVertexCount; private int _minVertexCount;
private uint _vertexAttribsInUse; private uint _vertexAttribsInUse;
@ -76,9 +72,7 @@ namespace Ryujinx.Graphics.OpenGL
_vertexBuffers[bindingIndex] = vb; _vertexBuffers[bindingIndex] = vb;
} }
_vertexBuffersCount = bindingIndex;
_minVertexCount = minVertexCount; _minVertexCount = minVertexCount;
_needsAttribsUpdate = true;
} }
public void SetVertexAttributes(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs) public void SetVertexAttributes(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
@ -131,8 +125,6 @@ namespace Ryujinx.Graphics.OpenGL
_vertexAttribs[index] = attrib; _vertexAttribs[index] = attrib;
} }
_vertexAttribsCount = index;
for (; index < Constants.MaxVertexAttribs; index++) for (; index < Constants.MaxVertexAttribs; index++)
{ {
DisableVertexAttrib(index); DisableVertexAttrib(index);
@ -160,13 +152,11 @@ namespace Ryujinx.Graphics.OpenGL
public void PreDraw(int vertexCount) public void PreDraw(int vertexCount)
{ {
LimitVertexBuffers(vertexCount); LimitVertexBuffers(vertexCount);
Validate();
} }
public void PreDrawVbUnbounded() public void PreDrawVbUnbounded()
{ {
UnlimitVertexBuffers(); UnlimitVertexBuffers();
Validate();
} }
public void LimitVertexBuffers(int vertexCount) public void LimitVertexBuffers(int vertexCount)
@ -252,36 +242,6 @@ namespace Ryujinx.Graphics.OpenGL
_vertexBuffersLimited = 0; _vertexBuffersLimited = 0;
} }
public void Validate()
{
for (int attribIndex = 0; attribIndex < _vertexAttribsCount; attribIndex++)
{
VertexAttribDescriptor attrib = _vertexAttribs[attribIndex];
if (!attrib.IsZero)
{
if ((uint)attrib.BufferIndex >= _vertexBuffersCount)
{
DisableVertexAttrib(attribIndex);
continue;
}
if (_vertexBuffers[attrib.BufferIndex].Buffer.Handle == BufferHandle.Null)
{
DisableVertexAttrib(attribIndex);
continue;
}
if (_needsAttribsUpdate)
{
EnableVertexAttrib(attribIndex);
}
}
}
_needsAttribsUpdate = false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnableVertexAttrib(int index) private void EnableVertexAttrib(int index)
{ {