Skip to content

Commit

Permalink
Modify Buffer so that it can be bound as anything
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Dec 7, 2023
1 parent 2e1ced6 commit bfb70a1
Show file tree
Hide file tree
Showing 33 changed files with 143 additions and 251 deletions.
26 changes: 13 additions & 13 deletions examples/ComplexExample/ComplexExample/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal sealed class Renderer : IRenderer
private IMaterialPool? _materialPool;

//private readonly IList<GpuObjectData> _objectData;
private IShaderStorageBuffer? _geometryInstanceBuffer;
private IIndirectBuffer? _geometryIndirectBuffer;
private IBuffer? _geometryInstanceBuffer;
private IBuffer? _geometryDrawIndirectBuffer;
private int _objectDataIndex;
private bool _isLoaded;

Expand All @@ -38,7 +38,7 @@ internal sealed class Renderer : IRenderer
private IGraphicsPipeline? _geometryGraphicsPipeline;

private CameraInformation _cameraInformation;
private IUniformBuffer? _cameraInformationBuffer;
private IBuffer? _cameraInformationBuffer;

public Renderer(
ILogger logger,
Expand Down Expand Up @@ -81,7 +81,7 @@ public PooledMaterial AddMaterial(Material material)

public void AddToRenderQueue(PooledMesh pooledMesh, PooledMaterial pooledMaterial, Matrix4x4 worldMatrix)
{
if (!_isLoaded || _geometryInstanceBuffer == null || _geometryIndirectBuffer == null)
if (!_isLoaded || _geometryInstanceBuffer == null || _geometryDrawIndirectBuffer == null)
{
return;
}
Expand All @@ -92,7 +92,7 @@ public void AddToRenderQueue(PooledMesh pooledMesh, PooledMaterial pooledMateria
MaterialId = new Int4(pooledMaterial.Index, 0, 0, 0)
}, _objectDataIndex);

_geometryIndirectBuffer.Update(new GpuIndirectElementData
_geometryDrawIndirectBuffer.Update(new GpuIndirectElementData
{
FirstIndex = pooledMesh.IndexOffset,
IndexCount = pooledMesh.IndexCount,
Expand Down Expand Up @@ -134,14 +134,14 @@ public void RenderWorld(ICamera camera)

_graphicsContext.BindGraphicsPipeline(_geometryGraphicsPipeline);
_graphicsContext.BeginRenderPass(_geometryFramebuffer.Value);
_geometryGraphicsPipeline.BindVertexBuffer(_meshPool.VertexBuffer, 0, 0);
_geometryGraphicsPipeline.BindIndexBuffer(_meshPool.IndexBuffer);
_geometryGraphicsPipeline.BindUniformBuffer(_cameraInformationBuffer, 0);
_geometryGraphicsPipeline.BindShaderStorageBuffer(_geometryInstanceBuffer, 1);
_geometryGraphicsPipeline.BindShaderStorageBuffer(_materialPool.MaterialBuffer, 2);
_geometryGraphicsPipeline.BindAsVertexBuffer(_meshPool.VertexBuffer, 0, 0);
_geometryGraphicsPipeline.BindAsIndexBuffer(_meshPool.IndexBuffer);
_geometryGraphicsPipeline.BindAsUniformBuffer(_cameraInformationBuffer, 0);
_geometryGraphicsPipeline.BindAsShaderStorageBuffer(_geometryInstanceBuffer, 1);
_geometryGraphicsPipeline.BindAsShaderStorageBuffer(_materialPool.MaterialBuffer, 2);
if (_objectDataIndex > 0)
{
_geometryGraphicsPipeline.MultiDrawElementsIndirect(_geometryIndirectBuffer, _objectDataIndex);
_geometryGraphicsPipeline.MultiDrawElementsIndirect(_geometryDrawIndirectBuffer, _objectDataIndex);
}
_graphicsContext.EndRender();

Expand Down Expand Up @@ -227,8 +227,8 @@ public bool Load()
_geometryInstanceBuffer = _graphicsContext.CreateShaderStorageBuffer<GpuMeshInstance>("Instances");
_geometryInstanceBuffer.AllocateStorage(Marshal.SizeOf<GpuMeshInstance>() * 4_096, StorageAllocationFlags.Dynamic);

_geometryIndirectBuffer = _graphicsContext.CreateIndirectBuffer("SceneIndirects");
_geometryIndirectBuffer.AllocateStorage(4096 * Marshal.SizeOf<GpuIndirectElementData>(), StorageAllocationFlags.Dynamic);
_geometryDrawIndirectBuffer = _graphicsContext.CreateDrawIndirectBuffer("SceneIndirects");
_geometryDrawIndirectBuffer.AllocateStorage(4096 * Marshal.SizeOf<GpuIndirectElementData>(), StorageAllocationFlags.Dynamic);

_meshPool = _graphicsContext.CreateMeshPool("Vertices", 1_024 * MegaByte, 768 * MegaByte);
_materialPool = _graphicsContext.CreateMaterialPool("Materials", 16 * MegaByte, _samplerLibrary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ internal sealed class DeferredRenderingApplication : GraphicsApplication

private Model _deccerCubesModel;

private IVertexBuffer? _gpuVertexBuffer;
private IIndexBuffer? _gpuIndexBuffer;
private IBuffer? _gpuVertexBuffer;
private IBuffer? _gpuIndexBuffer;

private SwapchainDescriptor _swapchainDescriptor;

Expand All @@ -45,15 +45,15 @@ internal sealed class DeferredRenderingApplication : GraphicsApplication
private ITexture? _finalTexture;

private GpuCameraConstants _gpuCameraConstants;
private IUniformBuffer? _gpuCameraConstantsBuffer;
private IBuffer? _gpuCameraConstantsBuffer;

private IList<GpuModelMeshInstance> _gpuModelMeshInstances;
private IShaderStorageBuffer? _gpuModelMeshInstanceBuffer;
private IBuffer? _gpuModelMeshInstanceBuffer;
private IList<DrawCommand> _drawCommands;

private IList<GpuMaterial> _gpuMaterials;
private IList<string> _gpuMaterialsInUse;
private IShaderStorageBuffer _gpuMaterialBuffer;
private IBuffer _gpuMaterialBuffer;

private IDictionary<string, ITexture> _textures;

Expand Down Expand Up @@ -156,11 +156,11 @@ protected override void Render(float deltaTime)

GraphicsContext.BeginRenderPass(_gBufferFramebufferDescriptor);
GraphicsContext.BindGraphicsPipeline(_gBufferGraphicsPipeline);
_gBufferGraphicsPipeline.BindUniformBuffer(_gpuCameraConstantsBuffer, 0);
_gBufferGraphicsPipeline.BindShaderStorageBuffer(_gpuModelMeshInstanceBuffer, 1);
_gBufferGraphicsPipeline.BindShaderStorageBuffer(_gpuMaterialBuffer, 2);
_gBufferGraphicsPipeline.BindVertexBuffer(_gpuVertexBuffer, 0, Offset.Zero);
_gBufferGraphicsPipeline.BindIndexBuffer(_gpuIndexBuffer);
_gBufferGraphicsPipeline.BindAsUniformBuffer(_gpuCameraConstantsBuffer, 0);
_gBufferGraphicsPipeline.BindAsShaderStorageBuffer(_gpuModelMeshInstanceBuffer, 1);
_gBufferGraphicsPipeline.BindAsShaderStorageBuffer(_gpuMaterialBuffer, 2);
_gBufferGraphicsPipeline.BindAsVertexBuffer(_gpuVertexBuffer, 0, Offset.Zero);
_gBufferGraphicsPipeline.BindAsIndexBuffer(_gpuIndexBuffer);

for (var i = 0; i < _drawCommands.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ internal sealed class ForwardRendererApplication : GraphicsApplication
private IGraphicsPipeline? _sceneGraphicsPipeline;

private GpuConstants _gpuConstants;
private IUniformBuffer? _gpuConstantsBuffer;
private IShaderStorageBuffer? _gpuModelMeshInstanceBuffer;
private IShaderStorageBuffer? _gpuMaterialBuffer;
private IBuffer? _gpuConstantsBuffer;
private IBuffer? _gpuModelMeshInstanceBuffer;
private IBuffer? _gpuMaterialBuffer;

private IVertexBuffer? _skullVertexBuffer;
private IIndexBuffer? _skullIndexBuffer;
private IBuffer? _skullVertexBuffer;
private IBuffer? _skullIndexBuffer;

private ISampler? _linearMipmapNearestSampler;
private ISampler? _linearMipmapLinear;

private readonly IList<ModelMesh> _modelMeshes;
private IList<GpuModelMeshInstance> _gpuModelMeshInstances;
private readonly List<GpuIndirectElementData> _gpuIndirectElements;
private IIndirectBuffer _gpuIndirectElementDataBuffer;
private IBuffer? _gpuIndirectElementDataBuffer;

public ForwardRendererApplication(
ILogger logger,
Expand Down Expand Up @@ -152,7 +152,7 @@ protected override bool Load()

_gpuModelMeshInstanceBuffer = GraphicsContext.CreateShaderStorageBuffer<GpuModelMeshInstance>("ModelMeshInstances");
_gpuModelMeshInstanceBuffer.AllocateStorage(3 * Marshal.SizeOf<GpuModelMeshInstance>(), StorageAllocationFlags.Dynamic);
_gpuIndirectElementDataBuffer = GraphicsContext.CreateIndirectBuffer("MeshIndirectDrawElement");
_gpuIndirectElementDataBuffer = GraphicsContext.CreateDrawIndirectBuffer("MeshIndirectDrawElement");
_gpuIndirectElementDataBuffer.AllocateStorage(3 * Marshal.SizeOf<GpuIndirectElementData>(), StorageAllocationFlags.Dynamic);

_gpuMaterials.Add(new GpuMaterial
Expand Down Expand Up @@ -203,12 +203,12 @@ protected override void Render(float deltaTime)

GraphicsContext.BeginRenderPass(_swapchainDescriptor);
GraphicsContext.BindGraphicsPipeline(_sceneGraphicsPipeline!);
_sceneGraphicsPipeline!.BindVertexBuffer(_skullVertexBuffer!, 0, 0);
_sceneGraphicsPipeline.BindIndexBuffer(_skullIndexBuffer!);
_sceneGraphicsPipeline!.BindAsVertexBuffer(_skullVertexBuffer!, 0, 0);
_sceneGraphicsPipeline.BindAsIndexBuffer(_skullIndexBuffer!);

_sceneGraphicsPipeline.BindUniformBuffer(_gpuConstantsBuffer, 0);
_sceneGraphicsPipeline.BindShaderStorageBuffer(_gpuModelMeshInstanceBuffer!, 1);
_sceneGraphicsPipeline.BindShaderStorageBuffer(_gpuMaterialBuffer!, 2);
_sceneGraphicsPipeline.BindAsUniformBuffer(_gpuConstantsBuffer, 0);
_sceneGraphicsPipeline.BindAsShaderStorageBuffer(_gpuModelMeshInstanceBuffer!, 1);
_sceneGraphicsPipeline.BindAsShaderStorageBuffer(_gpuMaterialBuffer!, 2);
_sceneGraphicsPipeline.BindSampledTexture(_linearMipmapLinear!, _skullBaseColorTexture!, 0);

_sceneGraphicsPipeline.MultiDrawElementsIndirect(_gpuIndirectElementDataBuffer, _gpuIndirectElements.Count);
Expand Down
4 changes: 2 additions & 2 deletions src/EngineKit.UnitTests/Buffers/IndexBufferShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public IndexBufferShould(GlfwOpenGLDummyWindow glfwOpenGLDummyWindow)
public void BeInstantiable()
{
// Arrange & Act
var indexBuffer = new IndexBuffer<uint>("Label");
var indexBuffer = new Buffer<uint>(BufferTarget.IndexBuffer, "Label");
indexBuffer.AllocateStorage(100, StorageAllocationFlags.None);

// Assert
Expand All @@ -36,7 +36,7 @@ public void BeInstantiable()
public void BeAbleToUpdateDynamicBuffer()
{
// Arrange
var indexBuffer = new IndexBuffer<uint>("Label");
var indexBuffer = new Buffer<uint>(BufferTarget.IndexBuffer, "Label");
indexBuffer.AllocateStorage(100, StorageAllocationFlags.Dynamic);

// Act
Expand Down
6 changes: 3 additions & 3 deletions src/EngineKit.UnitTests/Buffers/UniformBufferShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public UniformBufferShould(GlfwOpenGLDummyWindow glfwOpenGLDummyWindow)
public void BeInstantiable()
{
// Arrange & Act
var uniformBuffer = new UniformBuffer<GpuMaterial>("Label");
var uniformBuffer = new Buffer<GpuMaterial>(BufferTarget.UniformBuffer, "Label");

// Assert
uint bufferId = uniformBuffer;
Expand All @@ -36,7 +36,7 @@ public void BeInstantiable()
public void BeAbleToUpdateDynamicBufferWhenInitializedWithZeroSize()
{
// Arrange
var uniformBuffer = new UniformBuffer<GpuMaterial>("Label");
var uniformBuffer = new Buffer<GpuMaterial>(BufferTarget.UniformBuffer, "Label");
uniformBuffer.AllocateStorage(Marshal.SizeOf<GpuMaterial>(), StorageAllocationFlags.None);

// Act
Expand Down Expand Up @@ -66,7 +66,7 @@ public void BeAbleToUpdateDynamicBufferWhenInitializedWithZeroSize()
public void BeAbleToUpdateDynamicBuffer(int initialElementCount)
{
// Arrange
var uniformBuffer = new UniformBuffer<GpuMaterial>("Label");
var uniformBuffer = new Buffer<GpuMaterial>(BufferTarget.UniformBuffer, "Label");
uniformBuffer.AllocateStorage(initialElementCount * Marshal.SizeOf<GpuMaterial>(), StorageAllocationFlags.Dynamic);

// Act
Expand Down
4 changes: 2 additions & 2 deletions src/EngineKit.UnitTests/Buffers/VertexBufferShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public VertexBufferShould(GlfwOpenGLDummyWindow glfwOpenGLDummyWindow)
public void BeInstantiable()
{
// Arrange
var vertexBuffer = new VertexBuffer<VertexPositionNormalUvTangent>("Label");
var vertexBuffer = new Buffer<VertexPositionNormalUvTangent>(BufferTarget.VertexBuffer, "Label");

// Act
vertexBuffer.AllocateStorage(100, StorageAllocationFlags.None);
Expand All @@ -39,7 +39,7 @@ public void BeInstantiable()
public void BeAbleToUpdateDynamicBuffer()
{
// Arrange
var vertexBuffer = new VertexBuffer<VertexPositionNormalUvTangent>("Label");
var vertexBuffer = new Buffer<VertexPositionNormalUvTangent>(BufferTarget.VertexBuffer, "Label");
vertexBuffer.AllocateStorage(100, StorageAllocationFlags.Dynamic);

// Act
Expand Down
3 changes: 2 additions & 1 deletion src/EngineKit/Extensions/ToGLExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public static GL.BufferTarget ToGL(this BufferTarget bufferTarget)
BufferTarget.IndexBuffer => GL.BufferTarget.ElementArrayBuffer,
BufferTarget.ShaderStorageBuffer => GL.BufferTarget.ShaderStorageBuffer,
BufferTarget.UniformBuffer => GL.BufferTarget.UniformBuffer,
BufferTarget.IndirectDrawBuffer => GL.BufferTarget.DrawIndirectBuffer,
BufferTarget.DrawIndirectBuffer => GL.BufferTarget.DrawIndirectBuffer,
BufferTarget.DispatchIndirectBuffer => GL.BufferTarget.DispatchIndirectBuffer,
_ => throw new ArgumentOutOfRangeException(nameof(bufferTarget), bufferTarget, null)
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/EngineKit/Graphics/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static implicit operator uint(Buffer buffer)
internal class Buffer<TElement> : Buffer
where TElement : unmanaged
{
protected Buffer(BufferTarget bufferTarget, Label? label = null)
internal Buffer(BufferTarget bufferTarget, Label? label = null)
{
var innerLabel = $"{GetBufferNamePrefix(bufferTarget)}-{typeof(TElement).Name}";
if (!string.IsNullOrEmpty(label))
Expand All @@ -112,7 +112,7 @@ private static string GetBufferNamePrefix(BufferTarget bufferTarget)
BufferTarget.IndexBuffer => "Buffer-Indices",
BufferTarget.ShaderStorageBuffer => "Buffer-ShaderStorage",
BufferTarget.UniformBuffer => "Buffer-Uniforms",
BufferTarget.IndirectDrawBuffer => "Buffer-Indirect",
BufferTarget.DrawIndirectBuffer => "Buffer-Indirect",
_ => throw new ArgumentOutOfRangeException(nameof(bufferTarget), bufferTarget, null)
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/EngineKit/Graphics/BufferTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public enum BufferTarget
IndexBuffer,
ShaderStorageBuffer,
UniformBuffer,
IndirectDrawBuffer
DrawIndirectBuffer,
DispatchIndirectBuffer
}
7 changes: 4 additions & 3 deletions src/EngineKit/Graphics/ComputePipeline.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using EngineKit.Extensions;
using EngineKit.Graphics.Shaders;
using EngineKit.Native.OpenGL;

Expand All @@ -19,10 +20,10 @@ public void Dispatch(uint numGroupX, uint numGroupY, uint numGroupZ)
GL.Dispatch(numGroupX, numGroupY, numGroupZ);
}

public void DispatchIndirect(IIndirectBuffer indirectBuffer, int indirectElementIndex)
public void DispatchIndirect(IBuffer dispatchIndirectBuffer, int indirectElementIndex)
{
indirectBuffer.Bind();
GL.DispatchIndirect(new nint(indirectElementIndex * indirectBuffer.Stride));
GL.BindBuffer(BufferTarget.DispatchIndirectBuffer.ToGL(), dispatchIndirectBuffer.Id);
GL.DispatchIndirect(new nint(indirectElementIndex * dispatchIndirectBuffer.Stride));
}

public void Uniform(int location, float value)
Expand Down
10 changes: 10 additions & 0 deletions src/EngineKit/Graphics/GpuIndirectDispatchData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EngineKit.Graphics;

public struct GpuIndirectDispatchData
{
public uint NumGroupsX;

public uint NumGroupsY;

public uint NumGroupsZ;
}
35 changes: 20 additions & 15 deletions src/EngineKit/Graphics/GraphicsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,46 +134,51 @@ public void UnmapBuffer(IBuffer buffer)
GL.UnmapBuffer(buffer.Id);
}

public IIndexBuffer CreateIndexBuffer(Label label, MeshPrimitive[] meshPrimitives)
public IBuffer CreateIndexBuffer(Label label, MeshPrimitive[] meshPrimitives)
{
var indices = meshPrimitives
.SelectMany(meshPrimitive => meshPrimitive.Indices)
.ToArray();
var indexBuffer = new IndexBuffer<uint>(label);
var indexBuffer = new Buffer<uint>(BufferTarget.IndexBuffer, label);
indexBuffer.AllocateStorage(indices, StorageAllocationFlags.None);
return indexBuffer;
}

public IIndexBuffer CreateIndexBuffer<TIndex>(Label label)
public IBuffer CreateIndexBuffer<TIndex>(Label label)
where TIndex : unmanaged
{
return new IndexBuffer<TIndex>(label);
return new Buffer<TIndex>(BufferTarget.IndexBuffer, label);
}

public IIndirectBuffer CreateIndirectBuffer(Label label)
public IBuffer CreateDrawIndirectBuffer(Label label)
{
return new IndirectBuffer(label);
return new Buffer<GpuIndirectElementData>(BufferTarget.DrawIndirectBuffer, label);
}

public IBuffer CreateDispatchIndirectBuffer(Label label)
{
return new Buffer<GpuIndirectDispatchData>(BufferTarget.DrawIndirectBuffer, label);
}

public IShaderStorageBuffer CreateShaderStorageBuffer<TShaderStorageData>(Label label)
public IBuffer CreateShaderStorageBuffer<TShaderStorageData>(Label label)
where TShaderStorageData : unmanaged
{
return new ShaderStorageBuffer<TShaderStorageData>(label);
return new Buffer<TShaderStorageData>(BufferTarget.ShaderStorageBuffer, label);
}

public IUniformBuffer CreateUniformBuffer<TUniformData>(Label label)
public IBuffer CreateUniformBuffer<TUniformData>(Label label)
where TUniformData: unmanaged
{
return new UniformBuffer<TUniformData>(label);
return new Buffer<TUniformData>(BufferTarget.UniformBuffer, label);
}

public IVertexBuffer CreateVertexBuffer<TVertex>(Label label)
public IBuffer CreateVertexBuffer<TVertex>(Label label)
where TVertex : unmanaged
{
return new VertexBuffer<TVertex>(label);
return new Buffer<TVertex>(BufferTarget.VertexBuffer, label);
}

public IVertexBuffer CreateVertexBuffer(
public IBuffer CreateVertexBuffer(
Label label,
MeshPrimitive[] meshPrimitives,
VertexType targetVertexType)
Expand All @@ -197,11 +202,11 @@ public IVertexBuffer CreateVertexBuffer(
}
}

IVertexBuffer vertexBuffer;
IBuffer vertexBuffer;
switch (targetVertexType)
{
case VertexType.PositionNormalUvTangent:
vertexBuffer = new VertexBuffer<VertexPositionNormalUvTangent>(label);
vertexBuffer = new Buffer<VertexPositionNormalUvTangent>(BufferTarget.VertexBuffer, label);
break;
default:
throw new InvalidEnumArgumentException(nameof(targetVertexType));
Expand Down
Loading

0 comments on commit bfb70a1

Please sign in to comment.