Difference between surface and texture (SDL / general)

CGraphicsSdlSdl 2

C Problem Overview


Can anyone explain to me in simple words what is the difference between texture and surface? I saw it used in SDL2 as SDL_Surface and SDL_Texture. SDL_Textureis created from SDL_Surface which in turn is created from image/bitmap. Both are collection of pixels. But I do not see the main difference between them (has to do something with GPU?)

I tried to google it but all explanations I found were too complex to understand them without digging deeper into computer graphics stuff.

C Solutions


Solution 1 - C

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.

Solution 2 - C

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

Solution 3 - C

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

Solution 4 - C

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionps-auxView Question on Stackoverflow
Solution 1 - CPetr AbdulinView Answer on Stackoverflow
Solution 2 - Csaloomi2012View Answer on Stackoverflow
Solution 3 - CEvanderView Answer on Stackoverflow
Solution 4 - ComottoView Answer on Stackoverflow