Optimizing my Game Engine. Help needed!

Luminary
Posts: 5,143
Joined: 2002.04
Post: #16
If you want real-time help, the iDevGames IRC channel is the place. http://www.idevgames.com/irc
Quote this message in a reply
Member
Posts: 43
Joined: 2009.10
Post: #17
I'm not sure how VBOs would be useful if you're doing batch drawing. I may very well just not be experienced enough, but once you make a VBO its static. If you're doing batch drawing then don't you pretty much need to resubmit the collection of vertices every time one of the sprites in the batch changes position (or rotates, or scales, etc...)? If you're using VBOs, this would mean you'd need to generate a VBO every time this happened in which case you would never gain any performance since you can't just reference a VBO thats sitting in memory waiting for you.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #18
alerus Wrote:I'm not sure how VBOs would be useful if you're doing batch drawing. I may very well just not be experienced enough, but once you make a VBO its static. If you're doing batch drawing then don't you pretty much need to resubmit the collection of vertices every time one of the sprites in the batch changes position (or rotates, or scales, etc...)? If you're using VBOs, this would mean you'd need to generate a VBO every time this happened in which case you would never gain any performance since you can't just reference a VBO thats sitting in memory waiting for you.

That's a valid point ... you don't have to recreate it every time since there are ways to update it ... but the real question is if using VBOs and updating them on every frame will make any difference over custom arrays.

I haven't tried that yet .. I am using custom vert arrays since VBOs make no difference on the original iPhone and in any case, vertex processing is not much of an issue when doing 2d rendering.
Quote this message in a reply
Member
Posts: 43
Joined: 2009.10
Post: #19
warmi Wrote:That's a valid point ... you don't have to recreate it every time since there are ways to update it ... but the real question is if using VBOs and updating them on every frame will make any difference over custom arrays.

I haven't tried that yet .. I am using custom vert arrays since VBOs make no difference on the original iPhone and in any case, vertex processing is not much of an issue when doing 2d rendering.

Ah I didn't realize you could update VBOs. I was under the impression that you generally would modify VBOs just using standard transformation matrices and otherwise would let them be static. Even so, as you say, still needing to update them may make the advantages minor unless you only need to update a small fraction when drawing any given frame.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #20
Unfortunately the weakness of VBOs is that there's no good way to update a small portion; it's all or nothing.

It should still be at least as good as, and potentially better than, no VBO.
Quote this message in a reply
Member
Posts: 43
Joined: 2009.10
Post: #21
I'm now doing a bit more research on that. What about glBufferSubData? Is that available on GL ES? Even if it is, one downside I see is there is still no way to extend the memory used. So if you have a dynamic number of sprites you want to use in a batch, this can be problematic without perhaps preallocating a large chunk. Hmmm....
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #22
Yes glBufferSubData is in ES. Tip: the quickest way to check, if you are unsure, is to hold down the command key and double-click on the symbol (function/constant/etc) in your existing ES code (which means you have to type it or paste it in there first). That should get Xcode to automatically open up and highlight it for you in the relevant header. If the header doesn't pop up, then no, it's not included.

Yes, preallocating a relatively large chunk is one strategy. Remember, once it's there, if you don't have to update it, you save bandwidth, which might even be more significant than some batching techniques.
Quote this message in a reply
Member
Posts: 22
Joined: 2009.08
Post: #23
Okay, so I've got objects, each containing information about the vertices that each instance needs to create. Right now it's just drawing its own vertices, causing OpenGL ES to just perform a draw call for each object, which can get hectic after a lot of calls, so I'd like to batch those into one. What's the best way to pull the vertex information from each instance and plug it into a single array that OpenGL can draw? Am I going to have to deal with linked lists? As Warmi said on his first post, NSMutableArray might be a big slower... Are there faster alternatives?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #24
Loop over each object and add its vertices to your array... what are you asking?
Quote this message in a reply
Member
Posts: 43
Joined: 2009.10
Post: #25
SamBaylus Wrote:Okay, so I've got objects, each containing information about the vertices that each instance needs to create. Right now it's just drawing its own vertices, causing OpenGL ES to just perform a draw call for each object, which can get hectic after a lot of calls, so I'd like to batch those into one. What's the best way to pull the vertex information from each instance and plug it into a single array that OpenGL can draw? Am I going to have to deal with linked lists? As Warmi said on his first post, NSMutableArray might be a big slower... Are there faster alternatives?

Well normally you'd call glDrawArrays with a pointer to the memory of 4 vertices (if you're drawing a quad), and tell it to draw 4 vertices from there such as:

Code:
glVertexPointer(2, GL_SHORT, sizeof(Vertex), vertexPointer);
...
glDrawArrays(GL_TRIANGLES, 0, 4);

Well now instead of stopping at 4 vertices in your vertex pointer, keeping adding vertices for each subsequent quad. If you're doing 2 sprites this means you'll have 8 vertices, or 12 for 3 sprites, etc... You'll just need to make sure that you allocate enough space for how many sprites you'll draw before you fill it up. For example:

Code:
vertexPointer = (Vertex *)malloc(sizeof(Vertex)*4*numSprites)

Note that the 4 is because you have 4 vertices per sprite.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.09
Post: #26
Correct me if I'm wrong, but since you are using GL_TRIANGLES wouldn't that be:
glDrawArrays(GL_TRIANGLES, 0, 6 * numSprites);

vertexPointer = (Vertex *)malloc(sizeof(Vertex) * 6 *numSprites);

If you need the sprite sorted (by z-index) then you would need some kind of list, if not then just use an array.
Every time you update you run through all your sprites and add the vertices to the vertexPointer array. Here you can optimize so you don't add vertices that haven't changed, but you need to update the array if you have added a sprite in the middle of the list. You can for instance update all vertices after the inserted index when this happens.
Did this make any sense at all ? Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Best toolset/engine for iOS pixel art game? eXpiation 3 5,615 Oct 16, 2012 06:22 AM
Last Post: Skorche
  I wrote my engine and this is the 1st Game! papaonn 5 6,167 Mar 20, 2012 03:53 AM
Last Post: papaonn
  iPhone game engine Goliath 7 14,668 Jan 6, 2012 11:54 AM
Last Post: EqwanoX
  [Game Engine]Orx coming to iPhone iarwain 6 11,318 May 17, 2011 02:31 PM
Last Post: iarwain
  Help optimizing an inconsistent framerate Duddd 6 4,755 Apr 4, 2010 09:27 PM
Last Post: Duddd