Implement vertex instancing (#381)
This commit is contained in:
parent
da7e702751
commit
a42ab2e40c
7 changed files with 82 additions and 20 deletions
|
@ -7,6 +7,8 @@
|
|||
public bool Enabled;
|
||||
public int Stride;
|
||||
public long VboKey;
|
||||
public bool Instanced;
|
||||
public int Divisor;
|
||||
public GalVertexAttrib[] Attribs;
|
||||
}
|
||||
|
||||
|
@ -22,6 +24,8 @@
|
|||
public float FlipX;
|
||||
public float FlipY;
|
||||
|
||||
public int Instance;
|
||||
|
||||
public GalFrontFace FrontFace;
|
||||
|
||||
public bool CullFaceEnabled;
|
||||
|
|
|
@ -126,9 +126,9 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
|
||||
BindVertexLayout(New);
|
||||
|
||||
if (New.FlipX != Old.FlipX || New.FlipY != Old.FlipY)
|
||||
if (New.FlipX != Old.FlipX || New.FlipY != Old.FlipY || New.Instance != Old.Instance)
|
||||
{
|
||||
Shader.SetFlip(New.FlipX, New.FlipY);
|
||||
Shader.SetExtraData(New.FlipX, New.FlipY, New.Instance);
|
||||
}
|
||||
|
||||
//Note: Uncomment SetFrontFace and SetCullFace when flipping issues are solved
|
||||
|
@ -290,8 +290,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
|
||||
private void BindConstBuffers(GalPipelineState New)
|
||||
{
|
||||
//Index 0 is reserved
|
||||
int FreeBinding = 1;
|
||||
int FreeBinding = OGLShader.ReservedCbufCount;
|
||||
|
||||
void BindIfNotNull(OGLShaderStage Stage)
|
||||
{
|
||||
|
@ -385,6 +384,15 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
{
|
||||
GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
|
||||
}
|
||||
|
||||
if (Binding.Instanced && Binding.Divisor != 0)
|
||||
{
|
||||
GL.VertexAttribDivisor(Attrib.Index, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
GL.VertexAttribDivisor(Attrib.Index, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,10 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
{
|
||||
class OGLShader : IGalShader
|
||||
{
|
||||
public const int ReservedCbufCount = 1;
|
||||
|
||||
private const int ExtraDataSize = 4;
|
||||
|
||||
public OGLShaderProgram Current;
|
||||
|
||||
private ConcurrentDictionary<long, OGLShaderStage> Stages;
|
||||
|
@ -96,7 +100,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
return Enumerable.Empty<ShaderDeclInfo>();
|
||||
}
|
||||
|
||||
public unsafe void SetFlip(float X, float Y)
|
||||
public unsafe void SetExtraData(float FlipX, float FlipY, int Instance)
|
||||
{
|
||||
BindProgram();
|
||||
|
||||
|
@ -104,14 +108,15 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
|
||||
GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
|
||||
|
||||
float* Data = stackalloc float[4];
|
||||
Data[0] = X;
|
||||
Data[1] = Y;
|
||||
float* Data = stackalloc float[ExtraDataSize];
|
||||
Data[0] = FlipX;
|
||||
Data[1] = FlipY;
|
||||
Data[2] = BitConverter.Int32BitsToSingle(Instance);
|
||||
|
||||
//Invalidate buffer
|
||||
GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
||||
GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
||||
|
||||
GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, 4 * sizeof(float), (IntPtr)Data);
|
||||
GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, ExtraDataSize * sizeof(float), (IntPtr)Data);
|
||||
}
|
||||
|
||||
public void Bind(long Key)
|
||||
|
@ -197,7 +202,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
|
||||
GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
|
||||
|
||||
GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
||||
GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
||||
|
||||
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ExtraUboHandle);
|
||||
}
|
||||
|
@ -219,8 +224,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL
|
|||
|
||||
GL.UniformBlockBinding(ProgramHandle, ExtraBlockindex, 0);
|
||||
|
||||
//First index is reserved
|
||||
int FreeBinding = 1;
|
||||
int FreeBinding = ReservedCbufCount;
|
||||
|
||||
void BindUniformBlocksIfNotNull(OGLShaderStage Stage)
|
||||
{
|
||||
|
|
|
@ -41,6 +41,7 @@ namespace Ryujinx.Graphics.Gal.Shader
|
|||
|
||||
public const string ExtraUniformBlockName = "Extra";
|
||||
public const string FlipUniformName = "flip";
|
||||
public const string InstanceUniformName = "instance";
|
||||
|
||||
public const string ProgramName = "program";
|
||||
public const string ProgramAName = ProgramName + "_a";
|
||||
|
|
|
@ -241,10 +241,15 @@ namespace Ryujinx.Graphics.Gal.Shader
|
|||
{
|
||||
if (Decl.ShaderType == GalShaderType.Vertex)
|
||||
{
|
||||
SB.AppendLine("layout (std140) uniform " + GlslDecl.ExtraUniformBlockName + "{");
|
||||
//Memory layout here is [flip_x, flip_y, instance, unused]
|
||||
//It's using 4 bytes, not 8
|
||||
|
||||
SB.AppendLine("layout (std140) uniform " + GlslDecl.ExtraUniformBlockName + " {");
|
||||
|
||||
SB.AppendLine(IdentationStr + "vec2 " + GlslDecl.FlipUniformName + ";");
|
||||
|
||||
SB.AppendLine(IdentationStr + "int " + GlslDecl.InstanceUniformName + ";");
|
||||
|
||||
SB.AppendLine("};");
|
||||
}
|
||||
|
||||
|
@ -816,7 +821,7 @@ namespace Ryujinx.Graphics.Gal.Shader
|
|||
switch (Abuf.Offs)
|
||||
{
|
||||
case GlslDecl.VertexIdAttr: return "gl_VertexID";
|
||||
case GlslDecl.InstanceIdAttr: return "gl_InstanceID";
|
||||
case GlslDecl.InstanceIdAttr: return GlslDecl.InstanceUniformName;
|
||||
}
|
||||
}
|
||||
else if (Decl.ShaderType == GalShaderType.TessEvaluation)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue