Skip to content

Commit

Permalink
Merge pull request #51 from smoogipoo/fix-gl-mre-allocations
Browse files Browse the repository at this point in the history
Replace ManualResetEvent allocations with a single reusable event
  • Loading branch information
smoogipoo authored Apr 30, 2024
2 parents e3936de + b547c52 commit 164a28c
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions src/Veldrid/OpenGL/OpenGLGraphicsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ private class ExecutionThread
{
private readonly OpenGLGraphicsDevice gd;
private readonly BlockingCollection<ExecutionThreadWorkItem> workItems;
private readonly AutoResetEvent executionEvent = new AutoResetEvent(false);
private readonly Action<IntPtr> makeCurrent;
private readonly IntPtr context;
private readonly List<Exception> exceptions = new List<Exception>();
Expand Down Expand Up @@ -1069,12 +1070,11 @@ public MappedResource Map(IMappableResource resource, MapMode mode, uint subreso
MapMode = mode
};

var mre = new ManualResetEventSlim(false);
workItems.Add(new ExecutionThreadWorkItem(resource, &mrp, mre));
mre.Wait();
if (!mrp.Succeeded) throw new VeldridException("Failed to map OpenGL resource.");
workItems.Add(new ExecutionThreadWorkItem(resource, &mrp, executionEvent));
executionEvent.WaitOne();

mre.Dispose();
if (!mrp.Succeeded)
throw new VeldridException("Failed to map OpenGL resource.");

return new MappedResource(resource, mode, mrp.Data, mrp.DataSize, mrp.Subresource, mrp.RowPitch, mrp.DepthPitch);
}
Expand All @@ -1096,10 +1096,8 @@ internal void Unmap(IMappableResource resource, uint subresource)
Subresource = subresource
};

var mre = new ManualResetEventSlim(false);
workItems.Add(new ExecutionThreadWorkItem(resource, &mrp, mre));
mre.Wait();
mre.Dispose();
workItems.Add(new ExecutionThreadWorkItem(resource, &mrp, executionEvent));
executionEvent.WaitOne();
}

internal void UpdateBuffer(DeviceBuffer buffer, uint offsetInBytes, StagingBlock stagingBlock)
Expand Down Expand Up @@ -1132,10 +1130,8 @@ internal void Terminate()

internal void WaitForIdle()
{
var mre = new ManualResetEventSlim();
workItems.Add(new ExecutionThreadWorkItem(mre, false));
mre.Wait();
mre.Dispose();
workItems.Add(new ExecutionThreadWorkItem(executionEvent, false));
executionEvent.WaitOne();

checkExceptions();
}
Expand All @@ -1152,20 +1148,17 @@ internal void SwapBuffers()

internal void FlushAndFinish()
{
var mre = new ManualResetEventSlim();
workItems.Add(new ExecutionThreadWorkItem(mre, true));
mre.Wait();
mre.Dispose();
workItems.Add(new ExecutionThreadWorkItem(executionEvent, true));
executionEvent.WaitOne();

checkExceptions();
}

internal void InitializeResource(IOpenGLDeferredResource deferredResource)
{
var info = new InitializeResourceInfo(deferredResource, new ManualResetEventSlim());
var info = new InitializeResourceInfo(deferredResource, executionEvent);
workItems.Add(new ExecutionThreadWorkItem(info));
info.ResetEvent.Wait();
info.ResetEvent.Dispose();
info.ResetEvent.WaitOne();

if (info.Exception != null) throw info.Exception;
}
Expand Down Expand Up @@ -1205,19 +1198,19 @@ private void executeWorkItem(ExecutionThreadWorkItem workItem)
case WorkItemType.Map:
{
var resourceToMap = (IMappableResource)workItem.Object0;
var mre = (ManualResetEventSlim)workItem.Object1;
var resetEvent = (EventWaitHandle)workItem.Object1;

var resultPtr = (MapParams*)Util.UnpackIntPtr(workItem.UInt0, workItem.UInt1);

if (resultPtr->Map)
{
executeMapResource(
resourceToMap,
mre,
resetEvent,
resultPtr);
}
else
executeUnmapResource(resourceToMap, resultPtr->Subresource, mre);
executeUnmapResource(resourceToMap, resultPtr->Subresource, resetEvent);
}
break;

Expand Down Expand Up @@ -1298,7 +1291,7 @@ private void executeWorkItem(ExecutionThreadWorkItem workItem)
glFinish();
}

((ManualResetEventSlim)workItem.Object0).Set();
((EventWaitHandle)workItem.Object0).Set();
}
break;

Expand Down Expand Up @@ -1333,7 +1326,7 @@ private void executeWorkItem(ExecutionThreadWorkItem workItem)

private void executeMapResource(
IMappableResource resource,
ManualResetEventSlim mre,
EventWaitHandle waitHandle,
MapParams* result)
{
uint subresource = result->Subresource;
Expand Down Expand Up @@ -1590,11 +1583,11 @@ private void executeMapResource(
}
finally
{
mre.Set();
waitHandle.Set();
}
}

private void executeUnmapResource(IMappableResource resource, uint subresource, ManualResetEventSlim mre)
private void executeUnmapResource(IMappableResource resource, uint subresource, EventWaitHandle waitHandle)
{
var key = new MappedResourceCacheKey(resource, subresource);

Expand Down Expand Up @@ -1647,7 +1640,7 @@ private void executeUnmapResource(IMappableResource resource, uint subresource,
}
}

mre.Set();
waitHandle.Set();
}

private void checkExceptions()
Expand Down Expand Up @@ -1697,7 +1690,7 @@ private struct ExecutionThreadWorkItem
public ExecutionThreadWorkItem(
IMappableResource resource,
MapParams* mapResult,
ManualResetEventSlim resetEvent)
EventWaitHandle resetEvent)
{
Type = WorkItemType.Map;
Object0 = resource;
Expand Down Expand Up @@ -1751,10 +1744,10 @@ public ExecutionThreadWorkItem(Texture texture, uint argBlockId, uint dataBlockI
UInt2 = 0;
}

public ExecutionThreadWorkItem(ManualResetEventSlim mre, bool isFullFlush)
public ExecutionThreadWorkItem(EventWaitHandle resetEvent, bool isFullFlush)
{
Type = WorkItemType.WaitForIdle;
Object0 = mre;
Object0 = resetEvent;
Object1 = null;

UInt0 = isFullFlush ? 1u : 0u;
Expand Down Expand Up @@ -1819,13 +1812,13 @@ internal struct MappedResourceInfoWithStaging
private class InitializeResourceInfo
{
public readonly IOpenGLDeferredResource DeferredResource;
public readonly ManualResetEventSlim ResetEvent;
public readonly EventWaitHandle ResetEvent;
public Exception Exception;

public InitializeResourceInfo(IOpenGLDeferredResource deferredResource, ManualResetEventSlim mre)
public InitializeResourceInfo(IOpenGLDeferredResource deferredResource, EventWaitHandle resetEvent)
{
DeferredResource = deferredResource;
ResetEvent = mre;
ResetEvent = resetEvent;
}
}
}
Expand Down

0 comments on commit 164a28c

Please sign in to comment.