Skip to content

Commit

Permalink
Added GraphicsDevice.DrawIndexedInstanced for GPU Instancing
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsakharov committed Apr 19, 2024
1 parent a83a077 commit ff1cc68
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
6 changes: 4 additions & 2 deletions Prowl.Runtime/Rendering/GraphicsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ public abstract class GraphicsDevice

public void Draw(Topology primitiveType, uint count) => Draw(primitiveType, 0, count);
public abstract void Draw(Topology primitiveType, int v, uint count);
public unsafe void DrawIndexed(Topology triangles, uint indexCount, bool index32bit) => DrawIndexed(triangles, indexCount, index32bit, null);
public abstract unsafe void DrawIndexed(Topology triangles, uint indexCount, bool index32bit, void* value);
public unsafe void DrawIndexed(Topology primitiveType, uint indexCount, bool index32bit) => DrawIndexed(primitiveType, indexCount, index32bit, null);
public abstract unsafe void DrawIndexed(Topology primitiveType, uint indexCount, bool index32bit, void* value);
public abstract unsafe void DrawIndexed(Topology primitiveType, uint indexCount, int startIndex, int baseVertex, bool index32bit);
public abstract unsafe void DrawIndexedInstanced(Topology primitiveType, uint indexCount, uint instanceCount, bool index32bit);

public abstract void Dispose();
}
Expand Down
57 changes: 36 additions & 21 deletions Prowl.Runtime/Rendering/OpenGL/GLDevice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Prowl.Runtime.Rendering.Primitives;
using BepuPhysics.Collidables;
using Prowl.Runtime.Rendering.Primitives;
using Silk.NET.Core.Native;
using Silk.NET.OpenGL;
using System;
Expand Down Expand Up @@ -390,21 +391,43 @@ public override unsafe void TexSubImage3D(GraphicsTexture texture, int mip, int

public override void Draw(Topology primitiveType, int v, uint count)
{
var mode = primitiveType switch {
Topology.Points => PrimitiveType.Points,
Topology.Lines => PrimitiveType.Lines,
Topology.LineLoop => PrimitiveType.LineLoop,
Topology.LineStrip => PrimitiveType.LineStrip,
Topology.Triangles => PrimitiveType.Triangles,
Topology.TriangleStrip => PrimitiveType.TriangleStrip,
Topology.TriangleFan => PrimitiveType.TriangleFan,
_ => throw new ArgumentOutOfRangeException(nameof(primitiveType), primitiveType, null),
};
PrimitiveType mode = TopologyToGL(primitiveType);
GL.DrawArrays(mode, v, count);
}
public override unsafe void DrawIndexed(Topology triangles, uint indexCount, bool index32bit, void* value)
public override unsafe void DrawIndexed(Topology primitiveType, uint indexCount, bool index32bit, void* value)
{
PrimitiveType mode = TopologyToGL(primitiveType);
GL.DrawElements(mode, indexCount, index32bit ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort, value);
}

public override unsafe void DrawIndexed(Topology primitiveType, uint indexCount, int startIndex, int baseVertex, bool index32bit)
{
PrimitiveType mode = TopologyToGL(primitiveType);

var format = index32bit ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort;
var formatSize = index32bit ? sizeof(uint) : sizeof(ushort);
GL.DrawElementsBaseVertex(mode, indexCount, format, (void*)(startIndex * formatSize), baseVertex);
}

public override unsafe void DrawIndexedInstanced(Topology primitiveType, uint indexCount, uint instanceCount, bool index32bit)
{
PrimitiveType mode = TopologyToGL(primitiveType);

var format = index32bit ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort;
GL.DrawElementsInstanced(mode, indexCount, format, null, instanceCount);
}

public override void Dispose()
{
GL.Dispose();
}

#region Private


private static PrimitiveType TopologyToGL(Topology triangles)
{
var mode = triangles switch {
return triangles switch {
Topology.Points => PrimitiveType.Points,
Topology.Lines => PrimitiveType.Lines,
Topology.LineLoop => PrimitiveType.LineLoop,
Expand All @@ -414,16 +437,8 @@ public override unsafe void DrawIndexed(Topology triangles, uint indexCount, boo
Topology.TriangleFan => PrimitiveType.TriangleFan,
_ => throw new ArgumentOutOfRangeException(nameof(triangles), triangles, null),
};
GL.DrawElements(mode, indexCount, index32bit ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort, value);
}

public override void Dispose()
{
GL.Dispose();
}

#region Private

private DepthFunction DepthModeToGL(RasterizerState.DepthMode depthMode)
{
return depthMode switch {
Expand Down

0 comments on commit ff1cc68

Please sign in to comment.