Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Loading textures in another thread & reserving memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 spek   Prime Members   -  Reputation: 997

Like
0Likes
Like

Posted 22 April 2013 - 11:32 AM

About texture / VBO loading,
 
I have a roaming world so it happens quite a lot that textures or object-geometry needs to be loaded or removed on the fly. Although the image loading happens in a background-thread, the main thread is still used to generate the texture and copy the image pixels into it, which halts the main thread.
 
 
I've seen multi-threading with OpenGL is possible by sharing:
So I guess I should look into that first. But maybe OpenGL (3.x) also has special functions for smoothly loading texture data nowadays?
 
 
Second, is it useful to reuse texture/buffer objects? Right new each new texture calls glGenerateTextures() and fills it with pixels. Obsolete textures call glDeleteTextures() again. But instead of throwing them away, I could also put new textures in unused "slots" (if the resolution / format equals of course). Is that useful?
 
The same question for VBO's. Objects are generated and removed all the time, but maybe I can reuse those as well. Although chances are very little that we find a VBO with exact the same vertices of course. Would it hurt to put a (slight) smaller object into a bigger VBO?
 
 
Rick


Sponsor:

#2 RobTheBloke   Crossbones+   -  Reputation: 2349

Like
1Likes
Like

Posted 23 April 2013 - 07:23 AM

But maybe OpenGL (3.x) also has special functions for smoothly loading texture data nowadays?

Nope.

But instead of throwing them away, I could also put new textures in unused "slots" (if the resolution / format equals of course). Is that useful?

Not really. Instead of issuing a glDeleteTextures + glGenTextures + glTexture2d, you could just issue a glTexture2d call to resize the currently bound texture. This may help in some minor edge cases, but in the bigger scheme of things, it's unlikely to make a difference. If anything, keeping the symmetry in your code (i.e. create/destroy) will make the code easier to maintain.

The same question for VBO's.

glBufferData if you need to rezise the buffer, otherwise keep the symmetry to keep the code clean.



#3 spek   Prime Members   -  Reputation: 997

Like
0Likes
Like

Posted 23 April 2013 - 07:45 AM

Ok. Just using the default GL functions sure is easier.

 

But I would think glGenerate and other functions to copy image data into a buffer, involves reserving memory and such. So the idea is that if I need to load a 512x512 texture for example, it will look for an unused 512x512 buffer. If there is none, it will generate a new texture, but otherwise it uses a buffer that has already been reserved in the memory.

 

But maybe OpenGL already does this internally somehow?



#4 L. Spiro   Crossbones+   -  Reputation: 13928

Like
1Likes
Like

Posted 23 April 2013 - 06:45 PM

Again, no, OpenGL does not have similar functionality.  And, again, ::glGenTextures(), ::glDeleteTextures(), etc., are all dwarfed by ::glTexImage2D().

When you upload new texture data the driver (depending on the implementation—for reference here I am using a common implementation known to be used in various devices and hardware) will create a copy of your data which in itself is slow.

 

But the real slow happens the first time you render with that texture and its updated data.

When you actually try to render with a modified texture, the driver makes another copy where it performs swizzling into the GPU’s native format etc.  And no I am not talking about channel swizzling and RGB vs. BGR.  That is only a channel swizzle, not a pixel swizzle, which is another swizzle necessary for many GPU’s.

 

Because this happens when you actually draw with the modified texture, you can’t offload it to another thread or make it transparent.

So:

#1: You will have some kind of jitter in OpenGL no matter what, at least if the most common implementation is used by your drivers.

#2: Even if it is not, all these processes still happen at some point in the pipeline, and they consume so many cycles that anything you save by not calling ::glGenTextures() is completely moot.  It isn’t worth making a sharing system and searching for previous matches etc.  In fact you are just as likely to cause GPU stalls by overwriting a texture that is currently in use by the GPU even though it was already flagged by your CPU side as being unused after its last render.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 spek   Prime Members   -  Reputation: 997

Like
0Likes
Like

Posted 24 April 2013 - 12:45 AM

Hmmm, I see.

Then that leads to the following question; how to smoothly stream a scene then? I'm sure that has been done before with OpenGL.

 

Seems that no matter where and how I load it, the "slap in the face" comes when walking into that room where its textures finally get rendered, right? To spread the workload, would it be a good idea to render new textures, one-by-one, in an invisible background buffer to ensure they have been handled before walking into a new room and freezing?

 

Second I wonder how big the impact really is. In my case, some of the bigger textures are 1024 x 1024 RGBA8 images, or even bigger ("2048 x 1024") but using DXT compression. I assume that even loading just one of those, already hurts. Or is it so small that, if I spread it out with some time intervals, it wouldn't be very noticable? For the info, I'm working on a 2009 laptop videocard, not the fastest thing in the world anymore hehe.

 

Thanks for the info!


Edited by spek, 24 April 2013 - 12:55 AM.


#6 Hodgman   Moderators   -  Reputation: 30882

Like
2Likes
Like

Posted 24 April 2013 - 10:34 AM

For starters don't upload more than one texture per frame to GL, to minimize performance hitches, and also make sure you issue a fake draw call for each texture to force to GPU to make it 'ready' as L.Spiro mentioned. Maybe even performing this readying step on a different frame to the uploading step...

 

I'd also recommend using DXT compression as much as possible, because not only will it reduce the amount of data that has to be copied around, but it will likely reduce the costs of the 'readying' steps in the driver too (they're less likely to swizzle a DXT texture than a plain old uncompressed one).

 

I'd make sure that all my disk loading code (outside of GL) ran fine in the background first, so that you can stream in the assets to RAM without any performance issues. Then as each texture is loaded into RAM, put them into a queue for copying over to GL one by one.

 

The only way to find out is to try it and see what kind of performance you get ;)



#7 spek   Prime Members   -  Reputation: 997

Like
0Likes
Like

Posted 24 April 2013 - 10:50 AM

Allright, shizzle swizzle

 

The image loading already happens in a background thread, and one texture per cycle is made. But I didn't do the fake drawing call, so that explains why the program still chokes.

 

DXT compression is also used, though I found the results often a bit too crappy for normalMaps or specular stored in alpha channels. So there are quite some textures still uncompressed. Maybe I'm not using the right compression settings (using the ATI libraryand compressing to either DXT1, DXT3 or DXT5).

 

 

Oh btw, what exactly would be a cheap way to do a fake drawing? Do I really need to draw a quad or something, or is binding enough to trigger the process?

 

Well, thanks, I know what to do now biggrin.png

 

Rick


Edited by spek, 24 April 2013 - 11:06 AM.


#8 BGB   Crossbones+   -  Reputation: 1554

Like
0Likes
Like

Posted 24 April 2013 - 11:52 AM

doing things like loading in a separate thread can help performance IME, but isn't so much related to GPU related reasons (like uploading the texture) as much as more CPU-related tasks: decoding the textures and converting them to DXT. also things like parsing 3D models, ...

(for example, decoding PNG / JPEG / 3D models / ... while not that expensive, isn't free either...).

(though, I later ended up having to move some of this stuff back into the main thread for now, as my engine falls a little short of being thread-safe... and worse performance was preferable to data trampling / crashes / ...).


the GPU stalls in question aren't usually the big obvious stalls (like, enter a room and wait 1/2 second...), but are usually much smaller (microseconds?), so while they may kill the framerate if done regularly mid-render, they can still be used for "reasonably transparent" loading.

it is in-fact possible to stream things like video and similar to the GPU without hurting things too badly (though you might want to make sure any DXTn encoding/... is done before passing them off to OpenGL).


ADD: I don't know about fake drawing... I had just been calling 'glFinish();' after uploading which seems to do something at least...

granted, I can't really claim to be an 'expert' on all this...

Edited by cr88192, 24 April 2013 - 12:00 PM.


#9 spek   Prime Members   -  Reputation: 997

Like
0Likes
Like

Posted 24 April 2013 - 02:34 PM

Just added the fake draw step (the background image loading and pushing textures one by one was already implemented). Not sure if it's done 100% correctly, I simply binded the texture and drawed a triangle very, very far away from the camera (is it a problem if its outside the view frustum?).

 

It's still noticable when a texture gets loaded, though its more equal spread all over now. It may also be the hardware though, I get the feeling that this laptop is going to a digital heaven soon... My desktop loads the entire thing way faster, even though that videocard is slowly starting to date as well. Not only the loading goes slow, the entire game runs slow so its add up.

 

 

 

This would bring me to the conclusion that, like Spiro said, truly "smooth" streaming isn't possible. Then again on a more modern card it would most likely be less of a pain.

 

I still wonder though, old games like GTA on a PS2 did the same thing (I think). Of course those textures were really low-res, but so was that hardware 10 years ago. Though you could clearly see the scene getting loaded step by step when traveling too fast sometimes, it didn't noticably hurt the performance. And more recent games such as GTA IV are doing it with more (and more detailed) textures on "dating" hardware such as the XBox or PS3. Are they doing something really different, am I just fooled by low-res textures, or is their whole memory architecture different anyway? Just curious.

 

Cheers



#10 L. Spiro   Crossbones+   -  Reputation: 13928

Like
0Likes
Like

Posted 25 April 2013 - 01:19 AM

Are they doing something really different, am I just fooled by low-res textures, or is their whole memory architecture different anyway? Just curious.

Both. Basically this problem is specific to most versions of OpenGL (any that do not allow sharing resources between contexts). PC and Xbox * have always used DirectX and so resources can be fully processed on a second thread, and other modern architectures may implement wrappers for OpenGL (well, the really modern ones are just DirectX 11) but still have an underlying architecture that does not enforce a state-machine-driven way of handling things. Mobile devices don’t have this problem either because they can share resources across contexts which means it can be fully loaded on one thread and then used on the other.


L. Spiro


Edited by L. Spiro, 25 April 2013 - 01:20 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS