表面和纹理之间的差异(SDL/通用)

有人能简单地解释一下纹理和表面的区别吗?我看到它在 SDL2中作为 SDL_SurfaceSDL_Texture使用。SDL_Texture是由 SDL_Surface创建的,而 SDL_Surface又是由图像/位图创建的。两者都是像素的集合。但是我看不出它们之间的主要区别(必须用 GPU 做点什么?)

我试图在谷歌上搜索,但我发现的所有解释都太复杂,如果不深入挖掘计算机图形学内容,就无法理解。

41099 次浏览

SDL_Texture is loaded in your video card's VRAM instead of regular RAM.

Basically your assumption "has to do something with GPU?" is right.

SDL_Surface is used in software rendering. With software rendering, as saloomi2012 correctly noticed, you are using regular RAM to store image data. Thus, in most cases you can access data buffer associated with surface directly, modifying its content, i.e. it is using CPU, hence the software name.

SDL_Texture on the other hand, is used in a hardware rendering, textures are stored in VRAM and you don't have access to it directly as with SDL_Surface. The rendering operations are accelerated by GPU, using, internally, either OpenGL or DirectX (available only on Windows) API, which in turn are using your video hardware, hence hardware rendering name.

Needless to say that hardware rendering is by orders of magnitude faster than software rendering and should be always be considered as primary option.

Surfaces use your RAM and Textures use your video card that is more fast than the surfaces.

There is more information about that in:

https://thenumbat.github.io/cpp-course/sdl2/05/05.html

As mentioned in the last lesson, textures are the GPU rendering equivalent of surfaces. Hence, textures are almost always created from surfaces, using the function SDL_CreateTextureFromSurface(). This function more or less does what you'd expect—the parameters are the rendering context and a surface to create the texture from. As with other creation functions, it will return NULL on failure.

I hope it helps you!