Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between surface and texture (SDL / general)

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.

like image 268
ps-aux Avatar asked Jan 27 '14 22:01

ps-aux


People also ask

What is a SDL texture?

A structure that contains an efficient, driver-specific representation of pixel data.

What is an SDL surface?

An SDL surface is just an image data type that contains the pixels of an image along with all data needed to render it. SDL surfaces use software rendering which means it uses the CPU to render. It is possible to render hardware images but it's a bit more difficult so we're going to learn it the easy way first.

What is SDL renderer?

SDL_Renderer is a struct that handles all rendering. It is tied to a SDL_Window so it can only render within that SDL_Window . It also keeps track the settings related to the rendering. There are several important functions tied to the SDL_Renderer.


2 Answers

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.

like image 84
Petr Abdulin Avatar answered Sep 22 '22 10:09

Petr Abdulin


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

like image 34
saloomi2012 Avatar answered Sep 19 '22 10:09

saloomi2012