views:

331

answers:

2

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

Thanks..

+1  A: 

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.)

gavinb
+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
@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
I thought you created textures with glTexImage, not glTexSubImage.
genpfault
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
@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
Thanks for the answer all.@Bahbar: I'm confused for a bit too when gavinb said to use glTexSubImage to create the initial texture. I bet he meant to say glTexImage
Edwin
@Edwin Yes indeed, I definitely meant to say `glTexImage` - sorry for the confusion! Fixed the typo now. @Bahbar: thanks for the extra info, will look into it. I found an interesting article on streaming texture updates via PBOs http://www.songho.ca/opengl/gl_pbo.html that is worth investigating.
gavinb
@gavinb: good find. At the end of the day, whether the DMA is implemented depends on what the platform you're targeting is. Nothing like measuring it, eh ?
Bahbar
A: 

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.

Ben Voigt
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