lordmauve / wasabi2d

Cutting-edge 2D game framework for Python
https://wasabi2d.readthedocs.io/
GNU Lesser General Public License v3.0
156 stars 24 forks source link

Texture array for sprites and text #24

Closed lordmauve closed 4 years ago

lordmauve commented 4 years ago

A current limitation in text and sprite support is that the Atlas class builds multiple textures. This forces multiple draw calls in order to render sprites that got packed into different textures. It may also prevent sprites being moved between textures. In the case of text, it currently causes the layout to crash - because a text primitive expects to generate only one pass.

The Atlas class can instead pack to a TextureArray, and return not just just texture coordinates but the index of the texture that was packed to.

This means all sprites/text will always be drawn in a single draw call, which will be more efficient, and also eliminate the current limitations.

Note that at times the Atlas will need to add new layers to the texture; it isn't clear how to do this from the ModernGL documentation. In the worst case the TextureArray could be grown by reallocating and reuploading all data.

lordmauve commented 4 years ago

It looks like it will be a better idea to resize the texture rather than switch to a texture array. I have already swapped from UVs as float32s in (0, 1) to uint16 pixel coordinates to enable this (as these will not change when the texture is enlarged).

lordmauve commented 4 years ago

As of 2c8975576f722db819761e56d3204d07a973cfdb, we now always grow a single texture atlas.