C# SharpDX device E_OUTOFMEMORY exception when loading a large scene

0

I am writing a SharpDX (v4.0.1) application that displays assemblies composed of many (possibly very big) separate parts. Each part has its own vertex and index buffer. When I try to load a very big assembly, I eventually run into E_OUTOFMEMORY exception when trying to create a vertex buffer for a part:

int size = _meshVertices.Length * Utilities.SizeOf<MeshVertex>();
BufferDescription descr = new BufferDescription(size, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
_vertexBuffer = Buffer.Create(_device.Device, _meshVertices, descr);

$exception  {"HRESULT: [0x8007000E], Module: [General], ApiCode: [E_OUTOFMEMORY/Out of memory], Message: Not enough storage is available to complete this operation.\r\n"}  SharpDX.SharpDXException

When I was googling a possible solution, I found out that there is a size limit on a resource - but this doesn't seem to be the case as it happens even for smaller parts.

Another solution I tried was using the MemoryFailPoint class to try to reserve the memory before creating the buffer. But I suppose this checks for available RAM, while I believe the exception is caused by having not enough GPU memory. Anyway, this seems to alleviate the problem a bit but it doesn't fix it completely. It just takes longer before it fails.

int size = _meshVertices.Length * Utilities.SizeOf<MeshVertex>();
using (System.Runtime.MemoryFailPoint memFailPoint = new System.Runtime.MemoryFailPoint(size / 1024 / 0124))
{
    BufferDescription descr = new BufferDescription(size, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
    _vertexBuffer = Buffer.Create(_device.Device, _meshVertices, descr);
}

My problem is that after I get the exception, the device is lost. I assume that the resources such as vertex buffers are bound to the device, so to recreate the device after it is lost would mean to recreate all the resources which would be very time consuming.

So preferably, I am looking for a way to know that the device does not have enough memory before I try to create the buffer, so that I could just discard the single part if needed. Is it possible to do something like that?

Any help would be very much appreciated. I am new to SharpDX so if you think I misunderstood the problem and something else is causing it, please let me know as well. Thanks in advance for any input on this.

c#
out-of-memory
sharpdx
asked on Stack Overflow Jan 17, 2018 by kammie

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0