PocketMatrix forums

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

Tutorial 7 online

Folks,

Tutorial 7 is now online. This time I talk a bit about c-buffers, and optimizations, some specific for c-buffers, others are more generally applicable. C-Buffers can be used for other things than sprites too, check out the tutorial for some ideas. :)

url: http://www.pocketmatrix.com/phantom/tech.htm

Oh yeah, I added the sources for the bouncer demo, of course.

And another thing: If I click on 'developer' on the top bar, I do see a link to a certain game that is called 'Argentum', and some ancient 'Quake' thing, but nothing about the wealth of information on my tech pages. :)

(boy am I starting a burst of off-topic posts)

Well anyway, spread the word.

- Jacco.
That's a good article.  I enjoyed reading it.  I think it's a good thing that you're taking time to do this for the dev community, with all the other stuff you've got going on.

I didn't see anything about changing how the sprite is stored.  Do you think it would help to store the sprite as a list of spans instead of a rectangular bitmap where you need to traverse the pixels one at a time?  Even some sort of crude RLE might help here.  I haven't looked at your code so maybe you do this already.

Something that isn't mentioned in your article, but most think of sprites as being drawn in the order of submission.  That is, if you draw a red sprite first, then draw a green one in the same position, the green sprite will normally show up.  With this technique, that won't happen since the spans are already taken by the underlying red sprite.  I suppose the technical term for this is that you are drawing them in a front-to-back order.

I'm curious to know just how bad operator new and delete would do if you didn't use your span pool.  I know it's probably going to be hideous compared to setting a few pointers, but you might want to do it as a test just to bolster your argument for in-game memory management.  

In fact, in-game memory management might be a good topic for a future tutorial.  I can tell you from first hand experience that the heap manager in CE is pretty abysmal - much worse than anything on the PC.

p.s. I had trouble finding your tutorial pages too.  I meant to say something to Chris about it last week but I forgot.  Chris, get with it buddy!

am I the only one, that can't find it?
I see tutorials 1..6, then a shot from tut6, a demo to download and then a list of source code downloads and info about nutcracker/easyce at the bottom... where is the 7th tutorial?!?
/me is blind at the age of 20... gosh
Man...I just wrote a ton of stuff and erased it by accident.

Anyway, nice job Phantom it's a great article.

I also think Dan is on to something about a memory management article.  I am curious about heap management on the PPC, having not dealt with it directly yet.  So, Phantom, if you can throw an article about that together for, say, Friday, that'd be great.~~~~
Mirek,
Now I can't seem to find the seventh tutorial listed either!  This site is going through some funky voodoo lately.  On one of my machines, I try to click on a forum thread and it takes me back to the forum home page.  On another machine it works just fine. Kooky.

Jacco,
I'm wondering how much slower it would be to use a stencil buffer instead of spans?  It's incredibly easy to implement and could give you some cool transition effects by animating the contents of the stencil buffer instead of clearing it at the beginning of each frame.  Just a thought.

RI,
I think it was me who mentioned memory management in my earlier post in this thread, not Dan. :)

Digby:using stencil buffer would be much slower, because with c-buffer you can write one value for n-pixels, while with stencil you need to write one value per pixel... you're quite much missing the whole idea then... (what's the difference between drawing a pixel and checking stencil if the pixel should be drawn and then drawing it or not?) The c-buffer takes a totally different approax cause it allows you to check if you should write or not a whole line of pixels with a few reads... and btw, what do you need the stencil buffer for? or maybe you're looking for something like s-buffering? (s-buffering is equal to z-buffering but you also save data as spans (and the difference between s-buffer and c-buffer is, that you save the z values in s-buffer))

and yeah... the pocketmatrix server seems to have some problems...
and ohh.. digby.. would you mind emailing me the tutorial/source code/exec(for PC)? I would like to read it and waiting till they repair the whole thing isn't the best idea I guess cause it might take few days...
ohh and maybe one more question.. is zlib free? Can I use it in a commercial product without any payment?(credits are ok)
Thanks for info...
Pocket Matrix just switched servers. Jacco must have uploaded his files to the old pocketmatrix.com, so it's not available on the new host.

Dan East
From zlib.h:

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.


Dan East
ohhh, thnx a lot Dan:)))))
Hey guys, I can finally get to the forums again. I'm glad you liked the tutorial, apparently I can get away with quite complex stuff here. I was worried a bit that just Dan would appreciate tutorial 7. :) You guys answered all questions already, so I will now just go and get up-to-date on the other forums. :)
Hey,

first of all, i really liked the 7th tutorial. It is very well written and explains the stuff in a very clear and understandable way. Keep up the good work!

Now i dont know what most of you guys have been doing in the last years, but i have been an active member of the demo scene ever since. During the last years i have seen *tons* of optimization rutines, new algorithms and coding tutorials. So if you are really interested in coding examples/tutorials, there is a 99,99% chance you will find what you need in some demo-scene archives somewhere.

For this reason i think that talented ppl like Phantom should keep doing tutorials and sample code and stuff, but not necessarily reinvent the wheel. What i would like to see are pocketpc specific things. One more ultra-fast sprite blitting algo. is nice to see, but thats really nothing new (except if it is done in ARM asm). And yes, please get away with more complex stuff Phantom :-)

M.E.




Marcelo,

You're right about reinventing the wheel, but I found that many tutorials are not that well written, and when you're experimenting with new stuff (most of the time a reason for looking up a tutorial) it's always nice that the examples are ready to be compiled on your platform. That's why I usually supply a sample source.

- Jacco.
A great deal of the demos written for x86 are in (or contain) assembly. Especially if it is more than a couple years old when the average home PC wasn't 1 gigahertz. Believe me, I've looked through a huge number of demos, and very few were of use to me for that reason.

Dan East
Mirek,

Relax, I think YOU missed my point.  I fully understand the c-buffer approach.  My question was that I would like to know the performance difference between it and using a stencil buffer.  Your answer of "much slower" isn't meaningful to me.  I would prefer to see data to back that up.  Is it 25% slower?  is it 50% slower?  is it 200% slower?  That's the sort of  information someone would need to choose one algorithm over another.

The c-buffer's performance is highly data dependent.  It works nice for large opaque objects that don't contain a lot of transparency changes across a horizontal span.  It doesn't work for alpha-blending and the application must submit its drawing in explicit front-to-back order (or at least be aware that it will be drawn as such).  If you get a sprite that has a number of transparent sections across a horizontal span, then span management for these sprites can turn into a nightmare.  Jacco chose to use an opaque ball shape for a reason.  Using a c-buffer approach for rendering a sprite of any aribrary shape is more difficult and can end up being slower than alternate methods.

I think the c-buffer approach is great for large opaque compositing.  Things like parallax images for side-scrollers could benefit from reduced overdraw (Jacco hinted at this in his tutorial).  I'm not so sure about its worth as a general sprite solution though.

Captain_Chickenpants · 27 Sep 2001, 12:43 UTC
Okey dokey, what I need is a modification to the C-Buffer approach that will alpha blend between sprites.
I think the way I will have to do it is to allocate an area of memory for where the sprites overlap, and blend into this then passing this as the address for that span, unless anyone else has a better idea?

Paul.
digby those resoluts you're asking are highly dependend on entry data. On phantom's ball example the difference would be huge, up to 100% slower I would say... and with a bitmap created of pixels transparent/non-transparent/transparent/.... c-buffer would be slower then stencil buffer. But in usual games, most/all sprites have got few spans per horizontal line... It all depends where and how you will use it.
On the other hand.. the best way to find out is to make a little test...*hint* *hint*
Mirek,

Yes, I wholeheartedly agree that to answer my question I should probably write some code and measure the results.

We should be able to come up with an idea of the overhead of managing multiple spans vs. testing a stencil buffer though.  One thing about the stencil buffer that I didn't mention before was that all you need is 1 bpp to perform the stencil test.  Therefore you can read 32 pixels worth of stencil data in one DWORD read.

digby:yeah, you can, but...
1.without using asm, checking for single bit might be not that fast afterall (still much faster then reading mem)
2.often you would have to read much more then one pack of 32bits
3.So It all comes to the question where you want to use it... if you have got bitmaps with average width of 50 and one-few spans, you would be probably better handled with a c-buffer, else a stencil buffer might be the way to go... (this is an "example" and I can't give you exact numbers when it's better to use c-buffer and when stencil buffer.. You would need to test that...
PS. With a small overdraw (<2.0) I guess you could be better served without any additional buffor at all - cause you're doing just a simple blit which is quite fast... things would look much different if you were doing additional calculations perpixel (like bumpmapping mentioned earlier by phantom)
(putting 20k more pixels on screen might be less expensive then handling the c-buffer and hungryds if not thousands of spans...)
Therefore for small overdraw (especially when you're using a lot of transparent images) compressed sprites (rle encoded/whatever the name is) might be the best solution (and you can use alpha-blending easily then...)
Ehm,

Some thoughts:
- A stencil buffer implemented in software could be faster than just blitting. I've never thought of that, but instead of writing 32 pixels, you test 32bits in a mask, wich does not require any memory access at all (theoretically; if the mask is in a register).
- Of course this testing takes time, so at zero overdraw, this algorithm is obviously slower than a simple blit.
- Each implementation of the stencil buffer algorithm has it's own more or less exact percentage of overdraw at wich it is faster to use than plain blitting. Authors should calculate this figure and publish it with the source code. :)
- The c-buffer can't handle alpha blending. Alpha stuff is fundamentally bad for c-buffers, since it REQUIRES overdraw. You could theoretically draw alpha spans without filling the c-buffer, though. That means however that subsequent spans that get behind the alpha spans should be mixed AT THAT STAGE. So while you don't fill the c-buffer, you still have to keep track of the fact that there is an alpha-span there. That's not funny anymore. :)
- The performance of a c-buffer algorithm could be expressed in 'minimal efficient spanlength'. When you insert two 240 pixel span per scanline, ANY implementation of the c-buffer algo is faster than plain blitting. However, imagine an asm implementation of a c-buffer algo using trees instead of linked lists and a non-recursive insertion function (mine is still recursive). Given the simplicity of the actually executed code (the insertion function is huge, but the path is quickly narrowed down), a very good implementation could already be efficient at an average span length of 4 or 5 pixels. This would mean that almsot ANY situation would benefit from using it.
- Final thought: In the above optimal algo there would be a huge performance difference between an optimzied 'scene' and a scene that peforms OK just because the c-buffer code is so mighty efficient. :) While high-speed is good, huge fluctuations in speed are NOT good.

- Jacco.
O yeah, I haven't tried this yet, but I bet the balls demo would be a lot faster if I would sort the balls using their x-coordinate. :) Far less clipping of spans in that case, all spans would simply grow from left to right. :)

Ehm... That wouldn't work. The drawing order of the balls would change per frame, causing bad popping. Never mind. :)
Phantom:*ROTFL*, your last post is great:)))
and about the stencil.. with complicated sprites such situation might be "rare" and you also didn't take one fact into account... you have to fill the stencil buffer first anyway...:)
And another reason why c/stencil isn't the greatest - no alpha-blending for both...
I still believe a good usual drawing can be often faster or as fast as c-buffer/stencil buffer... and a lot less trouble:)
Of course. :) But sticking to the most ancient solution is not satisfying, is it? :) But I think you're right.
Phantom:sure it's not... the use of c-buffer or stencil buffer can bring a lot in some cases... It all depends pretty much on the situation. (I guess most engines that don't use alpha-blending would profit heavily from them..., but a lot of ppl is using alpha-blending...)
So to sum it all up, it's a good idea and might appear here or there to create effects "never seen before":) (for ex. it's perfect for a Mortal Kombat like background with 7 layers or so... a lot of side-scrollers could profit from it:)