Skip to content

Commit e7ef7b5

Browse files
committed
Map/Unmap buffers via IGraphicscontext
1 parent 043c0b7 commit e7ef7b5

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

src/EngineKit/Extensions/ToGLExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ public static int ToGL(this Swizzle swizzle)
4444
};
4545
}
4646

47-
public static GL.BufferStorageMask ToGL(this StorageAllocationFlags storageAllocationFlags)
47+
public static uint ToGL(this StorageAllocationFlags storageAllocationFlags)
4848
{
49-
GL.BufferStorageMask result = 0u;
50-
result |= (storageAllocationFlags & StorageAllocationFlags.Dynamic) == StorageAllocationFlags.Dynamic ? GL.BufferStorageMask.DynamicStorageBit : 0;
51-
result |= (storageAllocationFlags & StorageAllocationFlags.Client) == StorageAllocationFlags.Client ? GL.BufferStorageMask.ClientStorageBit : 0;
49+
var result = 0u;
50+
result |= (storageAllocationFlags & StorageAllocationFlags.Dynamic) == StorageAllocationFlags.Dynamic ? (uint)GL.BufferStorageFlags.DynamicStorageBit : 0;
51+
result |= (storageAllocationFlags & StorageAllocationFlags.Client) == StorageAllocationFlags.Client ? (uint)GL.BufferStorageFlags.ClientStorageBit : 0;
52+
result |= (storageAllocationFlags & StorageAllocationFlags.Mappable) == StorageAllocationFlags.Mappable
53+
? (uint)(GL.MapFlags.Persistent | GL.MapFlags.Coherent)
54+
: 0;
5255
return result;
5356
}
5457

src/EngineKit/Graphics/Buffer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ protected Buffer()
2020
public int Count { get; protected set; }
2121

2222
public int SizeInBytes { get; private set; }
23+
24+
public bool IsMappable { get; private set; }
2325

2426
public void Dispose()
2527
{
@@ -31,6 +33,7 @@ public void AllocateStorage(int sizeInBytes, StorageAllocationFlags storageAlloc
3133
GL.NamedBufferStorage(Id, sizeInBytes, nint.Zero, storageAllocationFlags.ToGL());
3234
SizeInBytes = sizeInBytes;
3335
Count = SizeInBytes / Stride;
36+
IsMappable = storageAllocationFlags.HasFlag(StorageAllocationFlags.Mappable);
3437
}
3538

3639
public void AllocateStorage<TElement>(TElement element, StorageAllocationFlags storageAllocationFlags)
@@ -39,6 +42,7 @@ public void AllocateStorage<TElement>(TElement element, StorageAllocationFlags s
3942
GL.NamedBufferStorage(Id, element, storageAllocationFlags.ToGL());
4043
Count = 1;
4144
SizeInBytes = Marshal.SizeOf<TElement>();
45+
IsMappable = storageAllocationFlags.HasFlag(StorageAllocationFlags.Mappable);
4246
}
4347

4448
public void AllocateStorage<TElement>(TElement[] elements, StorageAllocationFlags storageAllocationFlags)
@@ -47,6 +51,7 @@ public void AllocateStorage<TElement>(TElement[] elements, StorageAllocationFlag
4751
GL.NamedBufferStorage(Id, elements, storageAllocationFlags.ToGL());
4852
Count = elements.Length;
4953
SizeInBytes = Marshal.SizeOf<TElement>() * Count;
54+
IsMappable = storageAllocationFlags.HasFlag(StorageAllocationFlags.Mappable);
5055
}
5156

5257
public unsafe void Update(nint dataPtr, int offsetInBytes, int sizeInBytes)

src/EngineKit/Graphics/GraphicsContext.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ public Result<IGraphicsPipeline> CreateGraphicsPipeline(GraphicsPipelineDescript
159159
return Result.Success<IGraphicsPipeline>(graphicsPipeline);
160160
}
161161

162+
public unsafe bool TryMapBuffer(IBuffer buffer, MemoryAccess memoryAccess, out nint bufferPtr)
163+
{
164+
if (!buffer.IsMappable)
165+
{
166+
bufferPtr = IntPtr.Zero;
167+
return false;
168+
}
169+
170+
bufferPtr = (nint)GL.MapBuffer(buffer.Id, memoryAccess.ToGL());
171+
return true;
172+
}
173+
174+
public void UnmapBuffer(IBuffer buffer)
175+
{
176+
GL.UnmapBuffer(buffer.Id);
177+
}
178+
162179
public IIndexBuffer CreateIndexBuffer(Label label, MeshPrimitive[] meshPrimitives)
163180
{
164181
var indices = meshPrimitives

src/EngineKit/Graphics/IBuffer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public interface IBuffer : IDisposable
99
int Stride { get; }
1010

1111
int Count { get; }
12+
13+
bool IsMappable { get; }
1214

1315
void AllocateStorage(int sizeInBytes, StorageAllocationFlags storageAllocationFlags);
1416

src/EngineKit/Graphics/IGraphicsContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ void BlitFramebufferToSwapchain(
1818
int targetWidth,
1919
int targetHeight);
2020

21+
bool TryMapBuffer(IBuffer buffer, MemoryAccess memoryAccess, out nint bufferPtr);
22+
23+
void UnmapBuffer(IBuffer buffer);
24+
2125
IIndexBuffer CreateIndexBuffer<TIndex>(Label label)
2226
where TIndex : unmanaged;
2327

src/EngineKit/Graphics/StorageAllocationFlags.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public enum StorageAllocationFlags
77
{
88
None = 0,
99
Dynamic = 1,
10-
Client = 2
10+
Client = 2,
11+
Mappable = 4
1112
}

0 commit comments

Comments
 (0)