Difference between glTexSubImage and glTexImage function in OpenGL
Asked Answered
J

2

13

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

Thanks..

Jeniferjeniffer answered 9/3, 2010 at 0:9 Comment(0)
O
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.)

Orinasal answered 9/3, 2010 at 0:26 Comment(8)
+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.Korten
@Korten 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.Orinasal
I thought you created textures with glTexImage, not glTexSubImage.Hairbreadth
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.Korten
@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.Korten
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 glTexImageJeniferjeniffer
@Jeniferjeniffer 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 songho.ca/opengl/gl_pbo.html that is worth investigating.Orinasal
@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 ?Korten
M
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.

Myosin answered 9/3, 2010 at 0:29 Comment(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.Korten
glTexImage2D can support non-power-of-2 on drivers that support NPOT textures, and is required in order to define such textures.Acromion

© 2022 - 2024 — McMap. All rights reserved.