PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

A question of allocation

Hello all,
First time poster here, and this looks like a great forum to be a member of! Anyhow, I am currently working on a side project for the PocketPC. I am trying to build a 3D Library that is a subset of OpenGL. I love the architecture because its like being back on the old machines with fixed point math and old school coding tricks ;). So on with the problem.

I've got a flat/gourad version of my 3d-engine displaying 3DStudio mesh files. I've tried loading in a teapot from 3DS which is about 1000 polygons. To my amazement (well, not really, hadn't even optimized!) the thing was running at like 6fps on my Audiovox Maestro.

After a few weeks of depression, I finally began trying to find out why this could be so slow. My first inclination was the math. Its fixed point 24.8, and is not in assembly. So I decided to skip this until I'm ready for a refresher course in ARM assembly. My next inclination was to check out the preprocessing of my polygons (before the actual fills).

To make a long story short, I noticed that dynamic memory allocation was my culprit. Each frame, I would render all of the object's vertices and then create some new POLY* structs to be filled with the transformed verts (3 per poly here) which would get thrown into an array of POLY pointers "scene".

Through the powers of commenting and FPS counters, I noticed that if I were to allocate a bunch of POLY structs ONCE at the beginning, and constantly change their members, my FPS would jump up by 25FPS (before rendering). This annoyed me because I would have to have a set number of POLY objects allocated, leading to limits of the number of polygons.

So my question is, is PocketPC dynamic allocation (news, deletes) really this slow!? I've tried some HeapCreate/Alloc stuff hoping that fragmentation was the problem, but didn't notice much of a change. It seems like "new" and "delete" of many objects per frame is quite tedious to this little toy. Of course, I may be doing something COMPLETELY wrong. I'd love to hear if any of you have any similar issues, or any tricks I can use? Thanks!

Chris
So my question is, is PocketPC dynamic allocation (news, deletes) really this slow!?


Memory allocation like that is invariably slow, no matter what platform you're on. Think about what the new operator does. It has to go hunting through the heap to find you an "efficient" block of memory to fit what you want. I don't know the memory allocation strategy of the PocketPC OS, but if its aggressive about memory usage it might move stuff about a bit to accomodate you and take a while (comparatively) to get you your area. If it's assigning new pages of memory for you it'll be happily causing page-faults and nastiness behind the scenes to allocate stuff for you.

Delete should be fast, compared to new, but then it still has to re-organise its memory-usage linked lists/whatever to mark the block as having been freed.

Allocating a block of (say) 64 POLY structs at a time is far more efficient because you're not having to make the memory allocation routines go looking for a piece of memory for you as often. When that 64 is used, make another block of 64. To be clear, that is to say you ask for a block of size 64*sizeof(POLY) and use pointers to access the POLYs within, and hold a set of pointers to your blocks of 64.

(64 was a figure off the top of my head).
refractor,
Thanks for the response. I guess I've just taken for granted what the last 6 or 7 years of desktop computing has given us. I will definitely look into a better way of managing my heap. Thanks!

Chris
boost::pool is pretty nice for fast memory allocation tasks -
http://www.boost.org/libs/pool/doc/index.html

In combination with Refractor's suggestions, you should be able to get some pretty decent performance out of this while avoliding the limitation of a fixed-size heap.

However, avoiding any form of allocation or deallocation while in the game loop is always preferable ;)
I'm curious as to why you're even using a second buffer to hold all of the transformed vertices. Why wouldn't you take a triangle through the entire pipeline? You could still have a post-TnL vertex cache of a dozen or so vertices to help with things like strips and fans.
Well, hey, I'm definitely not a pro and am open to all suggestions ;). I am basically attempting something similar to OpenGL Vertex Arrays (set a vertex array pointer, set a pointer to indices to make each triangle). So I render all vertices, and use my array of indices to grab the appropriate transformed vertices and use them in my POLY structs, which later get backface culled, depth sorted (via a bucketsort alg.) and shaded.

This is faster than calls to glVertex(...) for each polygon where shared vertices in a mesh would get rendered more than once, and definitely more function call overhead.

I am unclear on your suggestion though. Could you elaborate? Thanks for the reply!

Chris
I'll have to dig out my "red book" and refresh my memory on OpenGL programming. I haven't wrote any OpenGL code in the last 7 years or so.

If I understand you correctly, you have a buffer full of pre-transformed/pre-lit vertices and another buffer containing the connectivity information (index buffer). What I think you're saying is that you are transforming and lighting every vertex in your vertex buffer, then storing the result (post-transformed/post-lit vertices) in another buffer. You then use the index buffer to grab the particular vertices from this post-transformed vertex buffer and send them to your rasterizer to draw the primitive.

Are these assumptions correct?