Difference between surface and texture (SDL / general)

ps-aux picture ps-aux · Jan 27, 2014 · Viewed 32.4k times · Source

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.

Answer

Petr Abdulin picture Petr Abdulin · Sep 30, 2014

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.