-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add instanced rendering #81
Merged
+347
−230
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Bearded.Graphics.Vertices; | ||
|
||
namespace Bearded.Graphics.Rendering; | ||
|
||
public static class BufferExtensions | ||
{ | ||
public static IVertexBuffer AsVertexBuffer<TVertex>(this Buffer<TVertex> buffer) | ||
where TVertex : struct, IVertexData | ||
{ | ||
return VertexBuffer.From(buffer); | ||
} | ||
|
||
public static IVertexBuffer AsVertexBuffer<TVertex>(this BufferStream<TVertex> buffer) | ||
where TVertex : struct, IVertexData | ||
{ | ||
return VertexBuffer.From(buffer); | ||
} | ||
|
||
public static IIndexBuffer AsIndexBuffer<TIndex>(this Buffer<TIndex> buffer) | ||
where TIndex : struct | ||
{ | ||
return IndexBuffer.From(buffer); | ||
} | ||
|
||
public static IIndexBuffer AsIndexBuffer<TIndex>(this BufferStream<TIndex> buffer) | ||
where TIndex : struct | ||
{ | ||
return IndexBuffer.From(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Bearded.Graphics.Rendering; | ||
|
||
public interface IFlushableBuffer | ||
{ | ||
void FlushIfNeeded(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using OpenTK.Graphics.OpenGL; | ||
|
||
namespace Bearded.Graphics.Rendering; | ||
|
||
public interface IIndexBuffer | ||
{ | ||
int Count { get; } | ||
DrawElementsType ElementType { get; } | ||
void ConfigureBoundVertexArray(); | ||
} | ||
|
||
public static class IndexBuffer | ||
{ | ||
public static IIndexBuffer From<TIndex>(Buffer<TIndex> buffer) | ||
where TIndex : struct | ||
{ | ||
return new Static<TIndex>(buffer); | ||
} | ||
|
||
public static IIndexBuffer From<TIndex>(BufferStream<TIndex> stream) | ||
where TIndex : struct | ||
{ | ||
return new Streaming<TIndex>(stream); | ||
} | ||
|
||
private sealed class Static<TIndex>(Buffer<TIndex> buffer) : IIndexBuffer | ||
where TIndex : struct | ||
{ | ||
public int Count => buffer.Count; | ||
|
||
public DrawElementsType ElementType { get; } = elementType<TIndex>(); | ||
|
||
public void ConfigureBoundVertexArray() => buffer.Bind(BufferTarget.ElementArrayBuffer); | ||
} | ||
|
||
private sealed class Streaming<TIndex>(BufferStream<TIndex> stream) : IIndexBuffer, IFlushableBuffer | ||
where TIndex : struct | ||
{ | ||
public int Count => stream.Count; | ||
|
||
public DrawElementsType ElementType { get; } = elementType<TIndex>(); | ||
|
||
public void ConfigureBoundVertexArray() => stream.Buffer.Bind(BufferTarget.ElementArrayBuffer); | ||
|
||
public void FlushIfNeeded() => stream.FlushIfDirty(); | ||
} | ||
|
||
private static DrawElementsType elementType<TIndex>() | ||
{ | ||
return default(TIndex) switch | ||
{ | ||
byte => DrawElementsType.UnsignedByte, | ||
ushort => DrawElementsType.UnsignedShort, | ||
uint => DrawElementsType.UnsignedInt, | ||
_ => throw new NotSupportedException("Index type must be one of [byte, ushort, uint].") | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Bearded.Graphics.Shading; | ||
using OpenTK.Graphics.OpenGL; | ||
|
||
namespace Bearded.Graphics.Rendering; | ||
|
||
public static partial class Renderable | ||
{ | ||
public static IRenderable Build(PrimitiveType primitiveType, Action<Builder> configure) | ||
{ | ||
var builder = new Builder(primitiveType); | ||
configure(builder); | ||
return builder.Build(); | ||
} | ||
|
||
public sealed class Builder(PrimitiveType primitiveType) | ||
{ | ||
private readonly List<IVertexBuffer> vertexBuffers = []; | ||
private IIndexBuffer? indexBuffer; | ||
private Func<int>? instanceCount; | ||
|
||
public Builder With(IVertexBuffer buffer) | ||
{ | ||
vertexBuffers.Add(buffer); | ||
return this; | ||
} | ||
|
||
public Builder With(params IVertexBuffer[] buffers) | ||
{ | ||
vertexBuffers.AddRange(buffers); | ||
return this; | ||
} | ||
|
||
public Builder With(ReadOnlySpan<IVertexBuffer> buffers) | ||
{ | ||
vertexBuffers.AddRange(buffers); | ||
return this; | ||
} | ||
|
||
public Builder With(IEnumerable<IVertexBuffer> buffers) | ||
{ | ||
vertexBuffers.AddRange(buffers); | ||
return this; | ||
} | ||
|
||
public Builder With(IIndexBuffer buffer) | ||
{ | ||
indexBuffer = buffer; | ||
return this; | ||
} | ||
|
||
public Builder InstancedWith(Func<int> getInstanceCount) | ||
{ | ||
instanceCount = getInstanceCount; | ||
return this; | ||
} | ||
|
||
public IRenderable Build() | ||
{ | ||
if (vertexBuffers.Count == 0) | ||
throw new InvalidOperationException("Renderable must have at least one vertex buffer."); | ||
|
||
return build(primitiveType, [..vertexBuffers], indexBuffer, instanceCount); | ||
} | ||
|
||
private static IRenderable build( | ||
PrimitiveType type, | ||
ImmutableArray<IVertexBuffer> vertices, | ||
IIndexBuffer? indices, | ||
Func<int>? instanceCount) | ||
{ | ||
var flushables = listFlushableBuffers(vertices, indices); | ||
|
||
Action draw = (indices, instanceCount) switch | ||
{ | ||
(null, null) => () => GL.DrawArrays(type, 0, vertices[0].Count), | ||
(null, not null) => () => GL.DrawArraysInstanced(type, 0, vertices[0].Count, instanceCount()), | ||
(not null, null) => () => GL.DrawElements(type, indices.Count, indices.ElementType, 0), | ||
(not null, not null) => () => GL.DrawElementsInstanced(type, indices.Count, indices.ElementType, 0, instanceCount()), | ||
}; | ||
|
||
return new Implementation(configure, flushables.IsDefaultOrEmpty ? draw : flushAndDraw); | ||
|
||
void configure(ShaderProgram program) | ||
{ | ||
foreach (var buffer in vertices) | ||
{ | ||
buffer.ConfigureBoundVertexArray(program); | ||
} | ||
|
||
indices?.ConfigureBoundVertexArray(); | ||
} | ||
|
||
void flushAndDraw() | ||
{ | ||
foreach (var flushable in flushables) | ||
{ | ||
flushable.FlushIfNeeded(); | ||
} | ||
|
||
draw(); | ||
} | ||
|
||
} | ||
|
||
private static ImmutableArray<IFlushableBuffer> listFlushableBuffers( | ||
ImmutableArray<IVertexBuffer> vertices, IIndexBuffer? indices) | ||
{ | ||
var flushableCount = vertices.Count(b => b is IFlushableBuffer); | ||
flushableCount += indices is IFlushableBuffer ? 1 : 0; | ||
|
||
if (flushableCount == 0) | ||
return ImmutableArray<IFlushableBuffer>.Empty; | ||
|
||
var builder = ImmutableArray.CreateBuilder<IFlushableBuffer>(flushableCount); | ||
|
||
foreach (var buffer in vertices) | ||
{ | ||
if (buffer is IFlushableBuffer flushable) | ||
{ | ||
builder.Add(flushable); | ||
} | ||
} | ||
if (indices is IFlushableBuffer indexFlushable) | ||
{ | ||
builder.Add(indexFlushable); | ||
} | ||
|
||
return builder.MoveToImmutable(); | ||
} | ||
} | ||
|
||
private sealed class Implementation(Action<ShaderProgram> configureBoundVertexArray, Action render) : IRenderable | ||
{ | ||
public DrawCall MakeDrawCallFor(ShaderProgram program) | ||
{ | ||
return DrawCall.With(() => configureBoundVertexArray(program), render); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably be more explicit here.
With
for records replaces, here it appends. This gets especially confusing since the index buffer, which also usesWith
, indeed does replace. So I would consider calling thisAddBuffer
(or evenAddVertexBuffer
) orAppendBuffer
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't disagree. But all the name changes I tried I ended up not being happy with. Perhaps the builder itself isn't quite ideal. I'll leave it like it is for now, and will see if I can come up with a better interface in the future.