Question on how to change the image data of a texture on a continual basis

Member
Posts: 257
Joined: 2004.06
Post: #1
Okay, so I've done a few searches of the forum but can't quite seem to find the right answer for what I'm trying to do so please forgive me if this has been brought up before.

Anyway, I'm using the Texture2D class to convert raw pixel data into something I can show on the screen. However, this raw pixel data is constantly changing so in order to update the image on the screen, I'm continually releasing the old Texture2D object and creating a new one. This strikes me a terribly inefficient. So my question then is it possible to create a texture once and continually update it from this buffer of pixel data? Is this what FBOs and rendering to a texture are for?

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #2
Did you try glTexSubImage2D?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #3
Also, you should be aware that on the iPhone you should avoid changing a texture in the middle of your rendering code. The iPhone doesn't have a stream renderer, so it has to make a copy of your texture internally when you modify if you've already used it for rendering something. The documentation isn't really clear what the rules are for determining if it needs to do this or not, so it might just do it every time regardless.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #4
Malarkey Wrote:Okay, so I've done a few searches of the forum but can't quite seem to find the right answer for what I'm trying to do so please forgive me if this has been brought up before.

Anyway, I'm using the Texture2D class to convert raw pixel data into something I can show on the screen. However, this raw pixel data is constantly changing so in order to update the image on the screen, I'm continually releasing the old Texture2D object and creating a new one. This strikes me a terribly inefficient. So my question then is it possible to create a texture once and continually update it from this buffer of pixel data? Is this what FBOs and rendering to a texture are for?

Rendering to FBO will be much faster but it will only work for textures which are modified using OpenGL ES itself ..in other words you cannot use it to create textures out of some external binary data.

Also the biggest hit when uploading textures comes from the fact that the driver needs to "swizzle" whatever texture data you are submitting into its own internal format and , as far as I know, there is no way to avoid this step without Img Tech own custom extension, which is current not supported on the iphone ( the swizzling part happens not when you are creating the texture but when it is being accessed by the driver during rendering)
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #5
Thanks for the replies, everyone. I figured glTexSubImage2D would be the way that I want to go but I wanted to make sure that I wasn't overlooking some other technique that I could use.

Skorche: You mention not changing the texture in the middle of my rendering code. Can you elaborate on that? What exactly would be the middle of my rendering code? I have a function that gets called on a timer that makes some OpenGL calls (glClear, glMatrixMode, glLoadIdentity, etc.) before finally calling swapBuffers in the EAGLView. In the middle of that function, I'm making a call to glTexSubImage2D to update the texture. Is that a bad idea then?

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
If you use a texture, then modify it, then use it again before swapBuffers, the renderer will need to keep both copies. This can become quite expensive if you keep modifying the texture in a single frame.

See the description of Tile Based Deferred Rendering for an overview.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #7
Ah, okay, got it. That's what I figured but I don't do much OpenGL programming. Again, thanks for the help everyone.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
arekkusu Wrote:If you use a texture, then modify it, then use it again before swapBuffers, the renderer will need to keep both copies. This can become quite expensive if you keep modifying the texture in a single frame.

Ok, so it does track if it's been used or not and needs a second copy. Good to know. It was never quite clear on that. I remember reading the documentation and thinking the way it was worded left it open to be implemented to always make a copy of the texture.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #9
Okay, so I tried using glTexSubImage2D and performance is just terrible on the device. I actually get a better framerate just submitting the whole power of 2 memory buffer again using glTexImage2D. Is glTexSubImage2D really that expensive or am I just doing something horribly wrong?

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #10
It's expensive. There really isn't a high performance path for this on the iPhone.

You'll get the best upload performance with compressed PVRTC textures, since they have the smallest footprint.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #11
Malarkey Wrote:Okay, so I tried using glTexSubImage2D and performance is just terrible on the device. I actually get a better framerate just submitting the whole power of 2 memory buffer again using glTexImage2D. Is glTexSubImage2D really that expensive or am I just doing something horribly wrong?

What you need is an extension called GL_IMG_texture_stream but unfortunately, it is either not exposed or not implemented on the iPhone ( it is available on other devices ).

File a bug , maybe they will include it.
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #12
In the short term, your best bet is to TexSubImage into the entire texture level, and to double or triple buffer the texture: ie, when you are done with an image, throw the texture into a queue, and only TexSubImage the whole level after 2 frames or so. This will ensure that the GPU is really done with any operations on that texture.

For the future, please do file bug reports, especially with sample code to demonstrate exactly the case you would like to be faster.
Quote this message in a reply
Member
Posts: 93
Joined: 2008.11
Post: #13
Hey,

For best performance I had to use FBO which is attached to a texture:

Code:
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, m_unName, 0);

What you will get is simply a framebuffer which you will use just as if it was your backbuffer but the rendered stuff will go into the tuxture attached to the FBO.
In my game I have a generic Quad class which uses this.. for example I generate a static label out of my bitmap fonts engine.

HTH,
Alex

TapMania - iPhone StepMania // Human knowledge belongs to the world!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  stupid question about IB - background image aerospaceman 2 3,380 Apr 29, 2010 07:17 PM
Last Post: aerospaceman
  SOIL image library problem or texture lib problem hippocoder 12 8,982 Mar 22, 2010 08:51 PM
Last Post: Frank C.
  OpenAL wont change the volume (gain) of any sound alerus 2 3,993 Nov 15, 2009 02:19 PM
Last Post: alerus
  How can I change the name of a project? riruilo 4 2,641 Jun 3, 2009 08:04 AM
Last Post: riruilo
  Texture Atlas question kendric 4 2,647 Mar 30, 2009 06:37 PM
Last Post: kendric