Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm building a 3d object's triangles. If I can write them to the buffer in the order they are calculated it will simplify the CPU code. The vertices for the triangles will not be contiguous.

Is there any performance penalty for writing them out of order?

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

There are (at least) two factors at play here. GPUs can utilize a post-transform vertex cache when indexed primitives are rendered. The size of this cache varies, and it's utilized during an individual draw call (not across multiple calls, as far as I'm aware, so it doesn't matter how frequently you change the buffer data in that context).

If you organize your data to take advantage of this cache (an example algorithm is here), you can see performance improvements. However, this organization has more to do with ordering your indices so that each index triple reuses as many recently-seen vertices as possible. It probably has little bearing on your specific scenario, unless your triangles, via your index buffer, are also randomly scattered about in the buffer... in which case you're probably blowing the cache often. It wasn't clear to me in your question whether or not this was the case, so I thought it worth mentioning.

What's more likely to be a problem is that GPUs also cache memory accesses to vertex data during a draw. The size of that cache is also fairly unreliably sized and it's conceivable that you could get a high miss frequency in that cache on the individual cores processing those indices into very poorly localized vertex data.

As to wether or not that's going to cause enough of a performance issue to be a red flag in your application, and particular (it sounds like) to re-engineer your algorithm to better organize the data at the expense of the readability of the algorithm... that's not something I can answer, you'll have to profile some scenarios and see.

I would personally err on the side of readable, maintainable code, though, as I think any cache missing you are going to cause is not going to be significant enough for users to notice.

share|improve this answer
add comment

This is a good question.

I believe what's happening behind the scenes is that the indices are used to dereference the vertex data you pass in, and that data will be sent to the vertex shaders. That said, I'll refer you to Josh's answer from here.

What you definitely do not want to do is recreate your vertex buffer every frame. Make sure that you are updating the same buffer (buffers, if you count the index buffer) each frame.

I should note that your method does prevent you from using triangle strips, which would require less index memory and should be faster to process for OpenGL. Granted, it is usually difficult to properly assemble an entire object using one triangle strip, especially procedurally.

share|improve this answer
1  
what about vertex caching? random access should practically remove any chance of it. –  Ali.S Oct 22 '12 at 4:29
    
The asker seems to be procedurally generating vertices, so he will be providing indices anyway, and new data every frame, making caching irrelevant. Ordering vs. not ordering using indices in both cases should have identical performance. –  ktodisco Oct 22 '12 at 5:25
2  
Post-T&L caching doesn't render indexed data "useless" (in fact, indexed data is required for it). Ordering vertices for maximum spatial locality over a mesh does have a benefit, as does ensuring locality of reference in your buffers, because GPUs do cache memory accesses. It's not often that these areas become serious bottlenecks, but I wouldn't say it's correct to assert there's no difference. –  Josh Petrie Oct 22 '12 at 5:30
1  
Also the "caching" in question occurs within one frame, indeed, within one draw call, so w.r.t to the benefit of the cache, it's irrelevant that the data is regenerated every frame. Your point about minimizing resource recreation between successive frames is, however, valid. –  Josh Petrie Oct 22 '12 at 5:32
    
Thanks, the triangle strip info is something to consider too. –  Jay Oct 22 '12 at 12:28
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.