高效的算法" 2D内存管理和QUOT;高效、算法、内存管理、QUOT

2023-09-07 14:29:21 作者:給我壹槍看我痛得多漂亮

我工作的一个基于OpenGL的应用程序,需要能够产生的纹理上飞了一套改变。

I am working on an OpenGL-based app that needs to be able to generate a changing set of textures on the fly.

这是我最初的阅读,它听起来就像这将是一个非常糟糕的主意,用一个单独的纹理为每个我需要绘制的结合纹理操作图像,以便它可以用于渲染实在是太慢了。

From my initial reading it sounds like it would be a really bad idea to use a separate texture for each image that I need to draw as the operation of binding a texture so that is can be used for rendering is really slow.

在preferred的方法是很多这些需要的图像合并成一个单一的大图像,并结合纹理一次,而它(精灵)绘制只节选。

The preferred approach would be to combine many of these needed images into a single bigger image and binding that texture once and drawing only excerpts from it (sprites).

不幸的是我的图片可以有非常不同的尺寸,成为有些未predictable方式需要的和过时的。

Unfortunately my images can have very different sizes and become needed and obsolete in somewhat unpredictable ways.

所以,我需要的是一种内存管理器,用于维护使用和自由区的名单在我的纹理(S)和可分配/处理领域新/过时的图片。

So, what I need is a sort of memory manager that maintains a list of used and free regions in my texture(s) and can allocate/dispose areas for new/obsolete images.

这基本上是每个程序需要做内存管理(分配和处理不同尺寸的堆变量),但它是一个二维的问题,因为我里面的一个大广场,而不是短期的线路分配矩形更长的

It's basically what every program needs to do for memory management (allocate and dispose variables of different size in the heap), except it's a 2D problem, since I'm allocating rectangles inside a big square rather than short "lines" in a longer one.

中是否有问题,我试图解决,一个名字有一个(或多个)标准/最佳实践方法(ES)?

Is there a name for the problem I am trying to solve and is there a (or multiple) standard/best-practice approach(es)?

(快速澄清:我不是在寻找各种方法来处理引用计数和这种弄清楚哪些图像不再使用我正在寻找一种方式来保留新矩形的跳线设置。将现有的和差距的工作,最大限度地减少(消除?)任何重新安排其他矩形。)的

更新:的

我想出了一个想法不同的方法,但决定将它张贴作为new问题调查。的

推荐答案

这类似于二维装箱问题 - 这是一个NP难,但是你可以使用断头台切割启发(见的这里一个解释和的此处某些源$ C ​​$ C),其中约解决在多项式时间的问题。

This looks similar to a 2D bin-packing problem - this is NP-hard, however you can use a guillotine cut heuristic (see here for an explanation and here for some source code) which approximately solves the problem in polynomial time.

标准的2D装箱算法不包括删除对象。我的建议是保持自由空间搜索树并使用最先匹配或最佳匹配,那么当一切都变得充分分散的(如分配新的对象,以自由空间,当你不能够找到足够的自由空间来分配一个对象)你重新运行完整的装箱算法。如果对象的全重排是极不可取的,那么你可以不是分割屏幕空间分成几个季度(或十六分或其他),只有重新运行在屏幕的部分(S)最自由空间装箱算法。

The standard 2D bin-packing algorithm doesn't include the deletion of objects. My recommendation is to maintain a search tree of free spaces and assign new objects to free space using first-fit or best-fit, then when everything becomes sufficiently fragmented (e.g. when you're not able to find enough free space to allocate an object) you re-run the full bin-packing algorithm. If a full rearrangement of objects is highly undesirable then you can instead split the screen space into quarters (or sixteenths or whatever) and only re-run the bin-packing algorithm on the portion(s) of the screen with the most free space.