Jump to content
  • Advertisement
Sign in to follow this  
chameleon_789

OpenGL Most effecient method for modifying textures in memory?

This topic is 3897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Part of an app I'm making centers around some textures which I plan to modify fairly often. At the moment, I'm using glTexImage2D and creating the textures from an array of bytes (data) with the following code :
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1, &generic_texture);
glBindTexture(GL_TEXTURE_2D, generic_texture);
// etc...
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE,data);
I'm not sure of the underlying mechanics of glTexImage2D but I assume that part of it is that it copies the (processed?) array into video memory. The problem is, I'm planning to modify the texture fairly frequently : writing the changes into the array, and then creating a new texture and copying the whole thing to the video memory seems a little redundant and slow - especially if I'm modifying a large texture or a large amount of textures. Really, I'm looking for a way of directly modifying the textures in memory - or at least, something a little less redundant the the method I'm planning to use. I'm not really familiar with OpenGl though, I'm pretty much a full blown beginner - I've been working off code examples :) - so I don't know if this kind of thing is impossible, if there's a function which caters specifically to this kind of thing, if I assumed wrong, etc.. can anyone help me out here, give me some pointers as to what I should do / be doing differently?

Share this post


Link to post
Share on other sites
Advertisement
Yeah I don't think there's any 'quick' way of reading a texture, modifying it and sending it back.
How are you modifying it? Might the modifications be possible to compute in a fragment shader?

Share this post


Link to post
Share on other sites
Pixel buffer object (PBO) is what you are looking for. You use it exactly like VBO, except that you glBindBuffer(GL_PIXEL_UNPACK_BUFFER). You can then either glMapBuffer the object or glSubBufferData into it and use glTexSubImage2D as usual.

Things to note:
1. Buffer objects work asynchronously, so you get better performance out of them
2. If you use 2 (or 3) buffer objects to upload your textures, you make the driver's life easier, which is good for performance. If you only use one buffer, the driver sometimes has to copy around data internally (if you overwrite a buffer while it's still being read from). Also, several concurrent uploads make better use of the available bus bandwidth.
3. Only ever write, don't read back (unless you really MUST), and make sure you provide the correct usage hint (in your case, GL_STREAM_DRAW).
4. Opinions differ on whether glMapBuffer or glSubBufferData is "better". Either will work, use what fits best for your need.
5. Any "sub" (glSubBufferData, glTexSubImage2D) function is usually preferrable to the "non-sub" counterparts, even if you want to replace the whole thing. "sub" is always at least as fast, but often faster, since it will use memory that's already there, while the "non-sub" functions usually free/allocate something.
6. Even reading back a texture, modifying and re-uploading it can be acceptable with buffer objects, if you place something else in between (since the expensive transfers will run asynchronously).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!