Refactor DrawCall to use VertexSpecification and update related rendering logic
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@@ -176,11 +177,27 @@ namespace Dashboard.Drawing
|
|||||||
public int Divisor { get; init; } = 1;
|
public int Divisor { get; init; } = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record VertexSpecification(
|
||||||
|
ImmutableList<VertexAttribute> Attributes,
|
||||||
|
ImmutableList<IBuffer?> VertexBuffers
|
||||||
|
)
|
||||||
|
{
|
||||||
|
public IBuffer? ElementBuffer { get; init; } = null;
|
||||||
|
}
|
||||||
|
|
||||||
public record struct UniformDescriptor(int Location, UniformType Type, long Offset, long Size);
|
public record struct UniformDescriptor(int Location, UniformType Type, long Offset, long Size);
|
||||||
|
|
||||||
public struct DrawCall(MeshPrimitive primitive, IBuffer vertexBuffer, int first, int count)
|
public struct DrawCall(MeshPrimitive primitive, VertexSpecification vertexSpec, int first, int count)
|
||||||
{
|
{
|
||||||
public IShader? ShaderPipeline { get; init; }
|
public IShader? ShaderPipeline { get; init; }
|
||||||
|
public VertexSpecification VertexSpecification { get; init; } = vertexSpec;
|
||||||
|
|
||||||
|
public MeshPrimitive Primitive { get; init; } = primitive;
|
||||||
|
public int First { get; init; } = first;
|
||||||
|
public int Count { get; init; } = count;
|
||||||
|
public int BaseVertex { get; init; } = 0;
|
||||||
|
public long Offset { get; init; } = 0;
|
||||||
|
|
||||||
public Box2d ViewportRegion { get; init; } = new Box2d(Vector2.Zero, Vector2.PositiveInfinity);
|
public Box2d ViewportRegion { get; init; } = new Box2d(Vector2.Zero, Vector2.PositiveInfinity);
|
||||||
public Box2d ScissorRegion { get; init; } = new Box2d(Vector2.PositiveInfinity, Vector2.PositiveInfinity);
|
public Box2d ScissorRegion { get; init; } = new Box2d(Vector2.PositiveInfinity, Vector2.PositiveInfinity);
|
||||||
public WindingOrder FrontFace { get; init; } = WindingOrder.Counterclockwise;
|
public WindingOrder FrontFace { get; init; } = WindingOrder.Counterclockwise;
|
||||||
@@ -197,19 +214,11 @@ namespace Dashboard.Drawing
|
|||||||
public float PointSize { get; init; }= 1.0f;
|
public float PointSize { get; init; }= 1.0f;
|
||||||
public float LineWidth { get; init; }= 1.0f;
|
public float LineWidth { get; init; }= 1.0f;
|
||||||
|
|
||||||
public MeshPrimitive Primitive { get; init; } = primitive;
|
|
||||||
public List<ITexture?> Textures { get; init; } = [];
|
public List<ITexture?> Textures { get; init; } = [];
|
||||||
public List<VertexAttribute> Attributes { get; init; } = [];
|
|
||||||
public List<UniformDescriptor> Uniforms { get; init; } = [];
|
public List<UniformDescriptor> Uniforms { get; init; } = [];
|
||||||
public IndexType IndexType { get; init; } = IndexType.UnsignedShort;
|
public IndexType IndexType { get; init; } = IndexType.UnsignedShort;
|
||||||
|
|
||||||
public int First { get; init; } = first;
|
|
||||||
public int Count { get; init; } = count;
|
|
||||||
public long Offset { get; init; } = 0;
|
|
||||||
public int BaseVertex { get; init; } = 0;
|
|
||||||
|
|
||||||
public IBuffer VertexBuffer { get; init; } = vertexBuffer;
|
|
||||||
public IBuffer? ElementBuffer { get; init; }
|
|
||||||
public ReadOnlyMemory<byte> UniformData { get; init; } = ReadOnlyMemory<byte>.Empty;
|
public ReadOnlyMemory<byte> UniformData { get; init; } = ReadOnlyMemory<byte>.Empty;
|
||||||
public IBuffer? UniformBuffer { get; init; }
|
public IBuffer? UniformBuffer { get; init; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
Vector2 size = Context.FramebufferSize;
|
Vector2 size = Context.FramebufferSize;
|
||||||
drawCall.SetAll(size, _vao, false);
|
drawCall.SetAll(size, _vao, false);
|
||||||
|
|
||||||
if (drawCall.ElementBuffer is null)
|
if (drawCall.VertexSpecification.ElementBuffer is null)
|
||||||
{
|
{
|
||||||
GL.DrawArrays(drawCall.Primitive.OpenGL, drawCall.First, drawCall.Count);
|
GL.DrawArrays(drawCall.Primitive.OpenGL, drawCall.First, drawCall.Count);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -336,15 +336,21 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
{
|
{
|
||||||
GL.BindVertexArray(vao);
|
GL.BindVertexArray(vao);
|
||||||
|
|
||||||
if (call.ElementBuffer != null)
|
VertexSpecification spec = call.VertexSpecification;
|
||||||
|
|
||||||
|
if (spec.ElementBuffer != null)
|
||||||
{
|
{
|
||||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, (call.ElementBuffer as GLBuffer)?.Handle ?? 0);
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, (spec.ElementBuffer as GLBuffer)?.Handle ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
GL.BindBuffer(BufferTarget.ArrayBuffer, (call.VertexBuffer as GLBuffer)?.Handle ?? 0);
|
|
||||||
|
|
||||||
foreach(VertexAttribute attrib in call.Attributes)
|
for (int i = 0; i < spec.Attributes.Count; i++)
|
||||||
{
|
{
|
||||||
|
VertexAttribute attrib = spec.Attributes[i];
|
||||||
|
IBuffer? buffer = spec.VertexBuffers[i];
|
||||||
|
|
||||||
|
GL.BindBuffer(BufferTarget.ArrayBuffer, (buffer as GLBuffer)?.Handle ?? 0);
|
||||||
|
|
||||||
GL.VertexAttribPointer(
|
GL.VertexAttribPointer(
|
||||||
(uint)attrib.Location,
|
(uint)attrib.Location,
|
||||||
attrib.Components,
|
attrib.Components,
|
||||||
|
|||||||
@@ -113,14 +113,17 @@ IShader shader = direct.CreatePipeline(new GlslShaderCreateInfo()
|
|||||||
""",
|
""",
|
||||||
});
|
});
|
||||||
|
|
||||||
DrawCall call = new DrawCall(MeshPrimitive.Triangle, vertex, 0, 3)
|
VertexSpecification spec = new VertexSpecification(
|
||||||
{
|
|
||||||
ShaderPipeline = shader,
|
|
||||||
Attributes =
|
|
||||||
[
|
[
|
||||||
new VertexAttribute(0, 3, VertexAttributeType.Float, 0, 8 * sizeof(float)),
|
new VertexAttribute(0, 3, VertexAttributeType.Float, 0, 8 * sizeof(float)),
|
||||||
new VertexAttribute(1, 4, VertexAttributeType.Float, 4 * sizeof(float), 8 * sizeof(float))
|
new VertexAttribute(1, 4, VertexAttributeType.Float, 4 * sizeof(float), 8 * sizeof(float))
|
||||||
],
|
],
|
||||||
|
[vertex, vertex]
|
||||||
|
);
|
||||||
|
|
||||||
|
DrawCall call = new DrawCall(MeshPrimitive.Triangle, spec, 0, 3)
|
||||||
|
{
|
||||||
|
ShaderPipeline = shader
|
||||||
};
|
};
|
||||||
|
|
||||||
window.EventRaised += (sender, eventArgs) =>
|
window.EventRaised += (sender, eventArgs) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user