13

What is the difference between the two functions? Any performance difference?

Thanks..

2 Answers 2

25

You create a texture using glTexImage, and then update its contents with glTexSubImage. When you update the texture, you can update the entire texture, or just a sub-rectangle of it.

It is far more efficient to create the one texture and update it than to create it and delete it repeatedly, so in that sense if you have a texture you want to update, always use glTexSubImage (after the initial creation).

Other techniques may be applicable for texture updates. For example, see this article on texture streaming for further information.

(Originally, this post suggested using glMapBuffer for texture updates - see discussion below.)

8
  • 1
    +1, but I don't agree with your last sentence. You generally can't map textures. They tend to be stored in non-linear format GPU-side anyways. so... If you do the upload (to a Buffer Object) yourself before calling SubTexture, you force the GPU to copy/swizzle on the card. Not necessarily a net win.
    – Bahbar
    Mar 9, 2010 at 8:05
  • @Bahbar That's very interesting. So what is the fastest way of updating a texture from image data in main memory? Just call glTexSubImage2D directly? IIRC it was one of my books that recommended glMapBuffer to do a DMA transfer.
    – gavinb
    Mar 9, 2010 at 12:37
  • I thought you created textures with glTexImage, not glTexSubImage.
    – genpfault
    Mar 9, 2010 at 16:49
  • 2
    Well, if it's already in main memory, then there is absolutely nothing you can do that the GL can't inside SubTexImage. If it's not, and you generate the data dynamically, the picture is a bit more blurred, as you could try to avoid the host memory copy by writing directly to a mapped buffer. My bet is that for many implementations, this would however result in the GL allocating a temporary host buffer anyways, and at that point, you're likely paying for an extra copy (host->gpu on unmap, gpu->gpu on TexSubImage). Either way, TexSubImage would be my recommendation, KISS principle.
    – Bahbar
    Mar 9, 2010 at 16:56
  • 1
    @genpfault: wow, my brain refused to see that until you pointed it out. Yes, I assume gavinb wanted to say glTexImage in the first part of the sentence.
    – Bahbar
    Mar 9, 2010 at 16:57
0

The gl functions with "sub" in the name aren't limited to power-of-2 dimensions. As gavinb points out, you need to use the non-sub variant once to set the overall dimensions, but I don't agree that calling the non-sub variant repeatedly is any slower than using "sub" for updates -- the GPU is still free to overwrite the existing texture in place as long as you are using the same texture id.

2
  • well, it can, but will it ? At the very least, it needs to verify that all the parameters to TexImage are still compatible with the previous ones, to know if the allocation is still proper. Point it, this is not in question for SubTexture. Also, power of two is not hugely relevant, since it's a requirement to support NPOT textures in GL now.
    – Bahbar
    Mar 9, 2010 at 8:08
  • glTexImage2D can support non-power-of-2 on drivers that support NPOT textures, and is required in order to define such textures.
    – William
    Mar 11, 2022 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.