简体   繁体   English

OpenGL + SDL_TTF:纹理索引回收

[英]OpenGL + SDL_TTF: texture index recycling

I am currently in the process of migrating a bunch of applications from SDL2 rendering (internally using OpenGL) to "pure" OpenGL rendering. 我目前正在将一堆应用程序从SDL2渲染(内部使用OpenGL)迁移到“纯” OpenGL渲染。 During this work I have figured out how to upload textures to the GPU and learned that every texture has an index. 在这项工作中,我弄清楚了如何将纹理上传到GPU,并了解到每个纹理都有索引。

To render text I use the SDL_TTF functions that allow you to, basically, upload a texture to the GPU depicting some text. 为了渲染文本,我使用了SDL_TTF函数,该函数基本上允许您将纹理上载到GPU来描述一些文本。 Most applications I am migrating aren't text heavy but I noticed a possible problem with them. 我要迁移的大多数应用程序都不会增加文本,但是我注意到它们可能存在问题。

Imagine an application that accepts user text input and puts in on the screen: each keystroke would generate a new texture with all the text entered (any other previous texture related to the same functionality would be deleted). 想象一个应用程序接受用户文本输入并输入到屏幕上:每次击键都会生成一个带有所有输入文本的新纹理(与相同功能相关的任何其他先前纹理都将被删除)。 So, to write "Hello" we would get five textures: "H", "He", "Hel", "Hell" and finally "Hello". 因此,编写“ Hello”将得到五个纹理:“ H”,“ He”,“ Hel”,“ Hell”,最后是“ Hello”。 As said, every "previous" texture (in this case from "H" to "Hell") would be deleted and only the memory for "Hello" would remain. 如前所述,每个“上一个”纹理(在这种情况下,从“ H”到“ Hell”)将被删除,仅保留“ Hello”的内存。

Still, in text heavy applications (for example, using this method to put the score of a game, doing it badly enough that you generate a new texture for each frame) lots of textures would be uploaded and deleted and the texture index would keep on growing. 不过,在文本繁重的应用程序中(例如,使用这种方法来计算游戏的得分,这样做非常糟糕,以至于无法为每一帧生成新的纹理),许多纹理将被上载和删除,并且纹理索引将保持不变生长。

My main question here is: will OpenGL recycle those indexes or will it just run out of numbers and crash?. 我的主要问题是:OpenGL是否会回收这些索引,还是会用完数字并崩溃?

PS: I am aware of the possibility of packing all the TTF characters into an atlas and just get the letters I need. PS:我知道可以将所有TTF字符打包到地图集中,然后得到我需要的字母。 I wouldn't like looking into that now unless my current approach is dangerous (this atlas thing sort of feels like old bitmap fonts). 除非我当前的方法很危险,否则我现在不想研究它(这种图集的感觉有点像旧的位图字体)。

PS2: I am also aware that changing textures constantly isn't exactly performant (GPU wise) but these applications aren't graphically challenging and have good performance in older systems. PS2:我也意识到,不断变化的纹理并不是完全有效(GPU方面),但是这些应用程序在图形上并不具有挑战性,并且在较旧的系统中具有良好的性能。

OpenGL objects are referred to by what the OpenGL specification calls "names" (other often used terms in programming are "handle" or "id"/"identifier"). OpenGL对象由OpenGL规范称为“名称”的名称引用(编程中其他常用的术语是“句柄”或“ id” /“标识符”)。 When a new name is first bound (with glBind… ) a corresponding OpenGL object (texture, buffer, etc.) is created; 当第一个新名称绑定(使用glBind… )时,将创建一个对应的OpenGL对象(纹理,缓冲区等)。 some OpenGL functions create objects and return the name to it ( glCreate… ). 一些OpenGL函数会创建对象并返回其名称( glCreate… )。 After a (valid) name is deleted (with glDelete… ) the corresponding OpenGL object will be deallocated once the last internal reference to it gets released. 删除(有效)名称后(使用glDelete… ),一旦释放了对它的最后一个内部引用,便会释放相应的OpenGL对象。 Names may be validly reused (by OpenGL) after deleting them, thereafter referring to a different object. 删除名称后,名称可以被有效地重用(由OpenGL),此后引用另一个对象。


Don't reinvent the wheel. 不要重新发明轮子。 Text rendering with OpenGL is ridiculously convoluted and there does not (yet¹) exist the one perfect method. 使用OpenGL进行文本渲染令人费解,而且还没有一种完美的方法。 There are however several libraries that address the texture generation and resource management problems. 但是,有几个库可以解决纹理生成和资源管理问题。 Do yourself a favour and use one of those. 帮自己一个忙,并使用其中之一。 In your case freetype-gl would be the best option (IMHO): https://github.com/rougier/freetype-gl 在您的情况下,freetype-gl是最好的选择(IMHO): https : //github.com/rougier/freetype-gl


1: I'm currently working on a new shader based glyph rasterizer that addresses most of the known problems, but it's not done yet. 1:我目前正在开发一个基于着色器的新字形光栅化器,该光栅化器可解决大多数已知问题,但尚未完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM