106

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.

ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • 8
    Take a look at my answer here: http://stackoverflow.com/questions/21007329/what-is-a-sdl-renderer/21007477#21007477 It will explain `SDL_Texture` and `SDL_Surface` as well as `SDL_Renderer`. In general, `SDL_Texture` is what is used for rendering, but when you load texture information using `SDL_Image` or `SDL_ttf`, you'll get the data as an `SDL_Surface` – olevegard Jan 27 '14 at 22:15
  • @olevegard That answer is pretty much a copy paste, I would really like an answer with some knowledge. – this Jan 27 '14 at 22:26
  • 5
    Well as you assume, `SDL_Surface` is just a collection of pixels while `SDL_Texture` is `an efficient, driver-specific representation of pixel data` meaning it can be used by the GPU http://wiki.libsdl.org/SDL_Texture. Did that answer your question? You really should dig deeper into computer graphics to understand more – olevegard Jan 27 '14 at 22:54
  • 2
    @olevegard Too bad you didn't write that in you answer. Here is your chance. – this Jan 28 '14 at 11:27

4 Answers4

102

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.

Community
  • 1
  • 1
Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
  • 5
    If hardware acceleration is available, is there any advantage to use SDL_Surface? E.g. for small rendering operations, the overhead for going to the hardware is too big? – Timmos Jul 08 '15 at 08:30
  • 4
    @Timmos Hardware rendering is always going to be faster than software. However, there is one advantage to a software surface: since it is stored in CPU-accessible main memory, you can directly write data to it. This can be useful if you want to generate a texture in real-time, for example as procedurally generated content in video games. (Mind, though, that nowadays many types of procedural generation can also be done on the GPU using shaders.) – Colin Emonds Apr 07 '16 at 20:08
  • 2
    @Timmos You have full control over the way that surfaces are worked on, generated, full read write control, I mean really...control is what a surface gives you. OpenGL and such API's are required to work with a GPU because GPU manufacturers don't allow direct access to the GPU, which is why you will oftentimes hear people complain about inefficiencies with OpenGL or DX or just dumb things that those API's do, because they could write something faster or more efficient, but they can't, because they have no access to the GPU. – Trevor Hart Oct 09 '16 at 20:21
  • May advice why `SDL_FillRect` is faster then `SDL_RenderFillRect`? Q: https://stackoverflow.com/q/65450100/4632019 – Eugen Konkov Dec 26 '20 at 10:34
11

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

saloomi2012
  • 337
  • 2
  • 12
3

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

Community
  • 1
  • 1
Evander
  • 76
  • 6
2

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!

omotto
  • 1,721
  • 19
  • 20