Jump to content
Posted 22 April 2013 - 11:32 AM
Posted 23 April 2013 - 07:23 AM
But maybe OpenGL (3.x) also has special functions for smoothly loading texture data nowadays?
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.
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?
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.
#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.
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.
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 ;)
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
Edited by spek, 24 April 2013 - 11:06 AM.
Posted 24 April 2013 - 11:52 AM
Edited by cr88192, 24 April 2013 - 12:00 PM.
Current Status / Downloads: http://cr88192.mooo.com:8080/wiki/index.php/BGB_Current_Status
YouTube Channel: http://www.youtube.com/user/BGBTech
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.
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.
Edited by L. Spiro, 25 April 2013 - 01:20 AM.