更新时间:2021-12-29 01:37:42
一种对我有用的异步加载资源的方法是将不可避免的文件读取操作推入指定的线程中.它可以轻松地与惰性资源管理相结合,如下所示:
A way to load resources asynchronously that worked for me is to push the inevitable read-from-file operations into designated threads. It can be easily combined with lazy resource management like so:
unitialized
并存储文件路径以供以后使用.uninitialized
的资源,请启动一个线程,该线程将文件中的数据异步加载到 CPU 缓冲区中并存储元信息(例如,纹理的宽度和高度,数字网格等的顶点).现在,将请求的资源替换为默认值或简单地不执行任何操作"(假设渲染器可以处理空"资源).initialized
.存储的元数据可帮助您确定生成的缓冲区的必要大小.您现在可以按预期使用资源.unitialized
and store the file paths for later use.uninitialized
, start a thread that asynchronously loads the data from file into a CPU buffer and store meta info (e.g. width and height of textures, number of vertices for meshes etc.) as well. For now, replace the requested resource by some default or simply "do nothing" (assuming your renderer can handle "empty" resources).initialized
. The stored metadata helps you to figure out the necessary size of the resulting buffers. You can now use the resource as intended.以这种方式处理资源可避免在多个线程之间共享OpenGL上下文,因为您只能异步处理(可能很繁重)CPU绑定的从文件加载"操作.当然,您将不得不应对相互排斥的问题,以安全地检查加载线程是否已完成.此外,您可能会考虑定义和维护上传预算,以限制每帧从CPU传输到GPU的数据量,以避免帧率下降.
Handling resources this way avoids sharing OpenGL contexts between multiple threads since you only handle the (presumably very heavy) CPU-bound load-from-file operations asynchronously. Of course, you will have to cope with mutual exclusion to safely check whether or not a loading thread has finished. Furthermore, you might consider defining and maintaining an upload budget for limiting the amount of data transferred from CPU to GPU per frame in order to avoid framerate drops.