glBufferData期间如何处理GL_OUT_OF_MEMORY错误? [英] How to handle GL_OUT_OF_MEMORY error during glBufferData?

查看:620
本文介绍了glBufferData期间如何处理GL_OUT_OF_MEMORY错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenGL参考提到了GL_OUT_OF_MEMORY错误

The OpenGL reference mentions about the GL_OUT_OF_MEMORY error

记录错误后,除错误标志的状态外,GL的状态未定义.

The state of the GL is undefined, except for the state of the error flags, after this error is recorded.

如果函数glBufferData无法消化给定的数据,则可能会生成此错误.但是另一方面,API似乎没有提供任何方法来检查发送特定大小的数据是否成功.

The function glBufferData can generate this error if it can't digest the given data. But on the other hand the API doesn't seem to provide any way to check if sending data of particular size would succeed.

这种情况真的没有希望吗?如果出现此错误,那么剩下的就是重新创建整个OpenGL上下文并重新开始的唯一事情吗?

Is this situation really hopeless? If I get this error, is the only thing left for me to recreate the whole OpenGL context and start over?

推荐答案

如果malloc返回NULL或new引发异常,该怎么办?您是否有恢复这种可能性的路径?

What do you do if malloc returns NULL or new throws an exception? Do you have a recovery path for that possibility?

大多数应用程序不这样做.大多数应用程序高兴地认为malloc永远不会返回NULL和/或new永远不会抛出.如果这些操作失败,它们将很高兴崩溃.

Most applications don't. Most applications happily assume that malloc will never return NULL and/or that new will never throw. And if those operations fail, they will happily crash.

对于OpenGL通常也是如此.您大概有充分的理由询问该特定的内存大小.因为你需要它.而且,无论出于什么原因,如果您无法获得它,通常都没有解决方案.

The same would generally go for OpenGL. You presumably asked for that particular memory size for a good reason; because you needed it. And if you can't get it, for whatever reason, there usually isn't a solution for that.

在某些情况下,您可以从无法分配内存中恢复过来,而OpenGL则以另一种方式使您感到困惑.

While there are cases where you could recover from not being able to allocate memory, OpenGL confounds you in another way.

请参阅,在OUT_OF_MEMORY错误中未定义OpenGL的整个状态的原因是:OOM可能发生在任何地方.没有任何函数的文档声称它可以发出OOM错误,因为每个函数都可以发出此类错误.

See, the reason that the entire state of OpenGL is undefined on an OUT_OF_MEMORY error is this: OOM can happen from anywhere. No function's documentation claims that it can issue an OOM error because every function can issue such an error.

在调用分配函数时,没有(必要)分配内存.驱动程序可以(几乎可以肯定)将分配推迟到以后.因此,在驱动程序检测到OOM条件后,无论调用什么OpenGL函数,都会收到OOM错误.

Memory is not (necessarily) allocated when you call an allocating function. The driver can (and almost certainly will) defer allocation until later. So you get an OOM error from whatever OpenGL function you call after the driver detects the OOM condition.

因此,如果缓冲区分配失败,请在导致失败的glBufferData调用之后长时间,OpenGL规范可以对当前状态说些什么?仅凭OOM错误,就无法准确查明是什么原因造成的.

So if buffer allocation fails, long after the call to glBufferData that provoked the failure, what can the OpenGL specification say about the current state? From the OOM error alone, there is no way to track down exactly what caused it.

因此,如果收到此错误,则实际上不可能恢复.您唯一真正的求助是终止应用程序或对其进行重建.

So if you get this error, recovery is not really possible. Your only real recourse is to terminate the application or rebuild it.

请注意,当您尝试分配内存而不能这样做时,Vulkan或D3D12之类的较低级API会立即OOM.

Note that lower-level APIs like Vulkan or D3D12 will OOM immediately when you try to allocate memory and cannot.

也:

但是另一方面,API似乎没有提供任何方法来检查发送特定大小的数据是否成功.

But on the other hand the API doesn't seem to provide any way to check if sending data of particular size would succeed.

那解决不了什么.为什么?

That would solve nothing. Why?

因为您的应用程序没有拥有 GPU;您的操作系统.多个程序可以同时在GPU上分配内存.操作系统也可以使用内存来调拨内存,并根据需要将内容分页到内存中或从内存中调出.

Because your application doesn't own the GPU; your OS does. Multiple programs can allocate memory on the GPU at the same time. The OS can poke about with memory too, paging things into and outof memory as it sees fit.

因此,如果您询问分配是否成功,并且OpenGL在实际执行分配时返回了yes,答案可能会发生更改.

So if you ask if an allocation would succeed, and OpenGL returned yes, by the time you actually perform that allocation, the answer may have changed.

这也是为什么Vulkan和类似的API没有测试分配是否成功的功能的原因(也没有测试剩余的未分配内存的功能)的原因.您只需分配内存即可;要么它起作用了,您就拥有了记忆,或者它失败了,而您却没有了.

This is also why Vulkan and similar APIs do not have a function to test if an allocation will succeed (nor does it have a function to test how much memory is left unallocated). You just allocate memory; either it works and you get your memory, or it fails and you don't.

这篇关于glBufferData期间如何处理GL_OUT_OF_MEMORY错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆