Spanify Graphics Abstraction Layer (#1226)

* Spanify Graphics Abstraction Layer

* Be explicit about BufferHandle size
This commit is contained in:
gdkchan 2020-05-23 06:46:09 -03:00 committed by GitHub
parent cc8dbdd3fb
commit 5011640b30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 208 additions and 134 deletions

View file

@ -10,12 +10,18 @@ namespace Ryujinx.Graphics.OpenGL
private bool _needsAttribsUpdate;
private VertexBufferDescriptor[] _vertexBuffers;
private VertexAttribDescriptor[] _vertexAttribs;
private readonly VertexAttribDescriptor[] _vertexAttribs;
private readonly VertexBufferDescriptor[] _vertexBuffers;
private int _vertexAttribsCount;
private int _vertexBuffersCount;
public VertexArray()
{
Handle = GL.GenVertexArray();
_vertexAttribs = new VertexAttribDescriptor[Constants.MaxVertexAttribs];
_vertexBuffers = new VertexBufferDescriptor[Constants.MaxVertexBuffers];
}
public void Bind()
@ -23,17 +29,17 @@ namespace Ryujinx.Graphics.OpenGL
GL.BindVertexArray(Handle);
}
public void SetVertexBuffers(VertexBufferDescriptor[] vertexBuffers)
public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
{
int bindingIndex = 0;
foreach (VertexBufferDescriptor vb in vertexBuffers)
for (int index = 0; index < vertexBuffers.Length; index++)
{
if (vb.Buffer.Buffer != null)
{
int bufferHandle = ((Buffer)vb.Buffer.Buffer).Handle;
VertexBufferDescriptor vb = vertexBuffers[index];
GL.BindVertexBuffer(bindingIndex, bufferHandle, (IntPtr)vb.Buffer.Offset, vb.Stride);
if (vb.Buffer.Handle != null)
{
GL.BindVertexBuffer(bindingIndex, vb.Buffer.Handle.ToInt32(), (IntPtr)vb.Buffer.Offset, vb.Stride);
GL.VertexBindingDivisor(bindingIndex, vb.Divisor);
}
@ -42,31 +48,35 @@ namespace Ryujinx.Graphics.OpenGL
GL.BindVertexBuffer(bindingIndex, 0, IntPtr.Zero, 0);
}
_vertexBuffers[index] = vb;
bindingIndex++;
}
_vertexBuffers = vertexBuffers;
_vertexBuffersCount = bindingIndex;
_needsAttribsUpdate = true;
}
public void SetVertexAttributes(VertexAttribDescriptor[] vertexAttribs)
public void SetVertexAttributes(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
{
int attribIndex = 0;
int index = 0;
foreach (VertexAttribDescriptor attrib in vertexAttribs)
for (; index < vertexAttribs.Length; index++)
{
VertexAttribDescriptor attrib = vertexAttribs[index];
FormatInfo fmtInfo = FormatTable.GetFormatInfo(attrib.Format);
if (attrib.IsZero)
{
// Disabling the attribute causes the shader to read a constant value.
// The value is configurable, but by default is a vector of (0, 0, 0, 1).
GL.DisableVertexAttribArray(attribIndex);
GL.DisableVertexAttribArray(index);
}
else
{
GL.EnableVertexAttribArray(attribIndex);
GL.EnableVertexAttribArray(index);
}
int offset = attrib.Offset;
@ -79,47 +89,47 @@ namespace Ryujinx.Graphics.OpenGL
{
VertexAttribType type = (VertexAttribType)fmtInfo.PixelType;
GL.VertexAttribFormat(attribIndex, size, type, fmtInfo.Normalized, offset);
GL.VertexAttribFormat(index, size, type, fmtInfo.Normalized, offset);
}
else
{
VertexAttribIntegerType type = (VertexAttribIntegerType)fmtInfo.PixelType;
GL.VertexAttribIFormat(attribIndex, size, type, offset);
GL.VertexAttribIFormat(index, size, type, offset);
}
GL.VertexAttribBinding(attribIndex, attrib.BufferIndex);
GL.VertexAttribBinding(index, attrib.BufferIndex);
attribIndex++;
_vertexAttribs[index] = attrib;
}
for (; attribIndex < 16; attribIndex++)
_vertexAttribsCount = index;
for (; index < Constants.MaxVertexAttribs; index++)
{
GL.DisableVertexAttribArray(attribIndex);
GL.DisableVertexAttribArray(index);
}
_vertexAttribs = vertexAttribs;
}
public void SetIndexBuffer(Buffer indexBuffer)
public void SetIndexBuffer(BufferHandle buffer)
{
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer?.Handle ?? 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, buffer.ToInt32());
}
public void Validate()
{
for (int attribIndex = 0; attribIndex < _vertexAttribs.Length; attribIndex++)
for (int attribIndex = 0; attribIndex < _vertexAttribsCount; attribIndex++)
{
VertexAttribDescriptor attrib = _vertexAttribs[attribIndex];
if ((uint)attrib.BufferIndex >= _vertexBuffers.Length)
if ((uint)attrib.BufferIndex >= _vertexBuffersCount)
{
GL.DisableVertexAttribArray(attribIndex);
continue;
}
if (_vertexBuffers[attrib.BufferIndex].Buffer.Buffer == null)
if (_vertexBuffers[attrib.BufferIndex].Buffer.Handle == null)
{
GL.DisableVertexAttribArray(attribIndex);