Skip to content

Commit

Permalink
Inline SetVertexAttributeArray (#1600)
Browse files Browse the repository at this point in the history
* private _enabledVertexAttributes

* rename _enabledVertexAttributesSet

* rewrite

* inline SetVertexAttributeArray
  • Loading branch information
nkast authored May 23, 2024
1 parent 59326dd commit 019d5fe
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 53 deletions.
72 changes: 46 additions & 26 deletions MonoGame.Framework/Graphics/.BlazorGL/ConcreteGraphicsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal sealed class ConcreteGraphicsContext : GraphicsContextStrategy
internal BufferBindingInfo[] _bufferBindingInfos;
private int _activeBufferBindingInfosCount;
internal bool[] _newEnabledVertexAttributes;
internal readonly HashSet<int> _enabledVertexAttributes = new HashSet<int>();
private readonly HashSet<int> _enabledVertexAttributesSet = new HashSet<int>();
private bool _attribsDirty;

private DepthStencilState _clearDepthStencilState = new DepthStencilState { StencilEnable = true };
Expand Down Expand Up @@ -373,29 +373,6 @@ public WebGLUniformLocation GetUniformLocation(ShaderProgram shaderProgram, stri
return location;
}

private void SetVertexAttributeArray(bool[] attrs)
{
for (int x = 0; x < attrs.Length; x++)
{
if (attrs[x])
{
if (_enabledVertexAttributes.Add(x))
{
GL.EnableVertexAttribArray(x);
GL.CheckGLError();
}
}
else
{
if (_enabledVertexAttributes.Remove(x))
{
GL.DisableVertexAttribArray(x);
GL.CheckGLError();
}
}
}
}

private void PlatformApplyVertexBuffersAttribs(int baseVertex)
{
ConcreteVertexShader vertexShaderStrategy = ((IPlatformShader)this.VertexShader).Strategy.ToConcrete<ConcreteVertexShader>();
Expand Down Expand Up @@ -461,6 +438,7 @@ private void PlatformApplyVertexBuffersAttribs(int baseVertex)
{
for (int eva = 0; eva < _newEnabledVertexAttributes.Length; eva++)
_newEnabledVertexAttributes[eva] = false;

for (int slot = 0; slot < _vertexBuffers.Count; slot++)
{
for (int e = 0; e < _bufferBindingInfos[slot].AttributeInfo.Elements.Count; e++)
Expand All @@ -472,7 +450,28 @@ private void PlatformApplyVertexBuffersAttribs(int baseVertex)
_activeBufferBindingInfosCount = _vertexBuffers.Count;
}

SetVertexAttributeArray(_newEnabledVertexAttributes);
// SetVertexAttributeArray
{
for (int x = 0; x < _newEnabledVertexAttributes.Length; x++)
{
if (_newEnabledVertexAttributes[x] == true)
{
if (_enabledVertexAttributesSet.Add(x))
{
GL.EnableVertexAttribArray(x);
GL.CheckGLError();
}
}
else // (_newEnabledVertexAttributes[x] == false)
{
if (_enabledVertexAttributesSet.Remove(x))
{
GL.DisableVertexAttribArray(x);
GL.CheckGLError();
}
}
}
}
}

internal void PlatformApplyUserVertexDataAttribs(VertexDeclaration vertexDeclaration, int baseVertex)
Expand Down Expand Up @@ -504,7 +503,28 @@ internal void PlatformApplyUserVertexDataAttribs(VertexDeclaration vertexDeclara
}
}

SetVertexAttributeArray(vertexAttribInfo.EnabledAttributes);
// SetVertexAttributeArray
{
for (int x = 0; x < vertexAttribInfo.EnabledAttributes.Length; x++)
{
if (vertexAttribInfo.EnabledAttributes[x] == true)
{
if (_enabledVertexAttributesSet.Add(x))
{
GL.EnableVertexAttribArray(x);
GL.CheckGLError();
}
}
else // (vertexAttribInfo.EnabledAttributes[x] == false)
{
if (_enabledVertexAttributesSet.Remove(x))
{
GL.DisableVertexAttribArray(x);
GL.CheckGLError();
}
}
}
}
_attribsDirty = true;
}

Expand Down
74 changes: 47 additions & 27 deletions MonoGame.Framework/Graphics/.GL/ConcreteGraphicsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal abstract class ConcreteGraphicsContextGL : GraphicsContextStrategy
internal BufferBindingInfo[] _bufferBindingInfos;
private int _activeBufferBindingInfosCount;
internal bool[] _newEnabledVertexAttributes;
internal readonly HashSet<int> _enabledVertexAttributes = new HashSet<int>();
private readonly HashSet<int> _enabledVertexAttributesSet = new HashSet<int>();
private bool _attribsDirty;

// Keeps track of last applied state to avoid redundant OpenGL calls
Expand Down Expand Up @@ -419,29 +419,6 @@ internal int GetUniformLocation(ShaderProgram shaderProgram, string name)
return location;
}

private void SetVertexAttributeArray(bool[] attrs)
{
for (int x = 0; x < attrs.Length; x++)
{
if (attrs[x])
{
if (_enabledVertexAttributes.Add(x))
{
GL.EnableVertexAttribArray(x);
GL.CheckGLError();
}
}
else
{
if (_enabledVertexAttributes.Remove(x))
{
GL.DisableVertexAttribArray(x);
GL.CheckGLError();
}
}
}
}

private void PlatformApplyVertexBuffersAttribs(int baseVertex)
{
ConcreteVertexShader vertexShaderStrategy = ((IPlatformShader)this.VertexShader).Strategy.ToConcrete<ConcreteVertexShader>();
Expand Down Expand Up @@ -507,6 +484,7 @@ private void PlatformApplyVertexBuffersAttribs(int baseVertex)
{
for (int eva = 0; eva < _newEnabledVertexAttributes.Length; eva++)
_newEnabledVertexAttributes[eva] = false;

for (int slot = 0; slot < _vertexBuffers.Count; slot++)
{
for (int e = 0; e < _bufferBindingInfos[slot].AttributeInfo.Elements.Count; e++)
Expand All @@ -518,7 +496,28 @@ private void PlatformApplyVertexBuffersAttribs(int baseVertex)
_activeBufferBindingInfosCount = _vertexBuffers.Count;
}

SetVertexAttributeArray(_newEnabledVertexAttributes);
// SetVertexAttributeArray
{
for (int x = 0; x < _newEnabledVertexAttributes.Length; x++)
{
if (_newEnabledVertexAttributes[x] == true)
{
if (_enabledVertexAttributesSet.Add(x))
{
GL.EnableVertexAttribArray(x);
GL.CheckGLError();
}
}
else // (_newEnabledVertexAttributes[x] == false)
{
if (_enabledVertexAttributesSet.Remove(x))
{
GL.DisableVertexAttribArray(x);
GL.CheckGLError();
}
}
}
}
}

internal void PlatformApplyUserVertexDataAttribs(VertexDeclaration vertexDeclaration, IntPtr baseVertex)
Expand Down Expand Up @@ -551,7 +550,28 @@ internal void PlatformApplyUserVertexDataAttribs(VertexDeclaration vertexDeclara
#endif
}

SetVertexAttributeArray(vertexAttribInfo.EnabledAttributes);
// SetVertexAttributeArray
{
for (int x = 0; x < vertexAttribInfo.EnabledAttributes.Length; x++)
{
if (vertexAttribInfo.EnabledAttributes[x] == true)
{
if (_enabledVertexAttributesSet.Add(x))
{
GL.EnableVertexAttribArray(x);
GL.CheckGLError();
}
}
else // (vertexAttribInfo.EnabledAttributes[x] == false)
{
if (_enabledVertexAttributesSet.Remove(x))
{
GL.DisableVertexAttribArray(x);
GL.CheckGLError();
}
}
}
}
_attribsDirty = true;
}

Expand Down Expand Up @@ -1184,7 +1204,7 @@ internal void Cardboard_ResetContext()
_shaderProgram = null;

//invalidate VertexAttributes
_enabledVertexAttributes.Clear();
_enabledVertexAttributesSet.Clear();

//invalidate textures
((IPlatformTextureCollection)this.Textures).Strategy.Clear();
Expand Down

0 comments on commit 019d5fe

Please sign in to comment.