PocketMatrix forums

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

Notes on built-in clipping performance

There's been some comment lately on the performance hit of using the built-in clipper in GD 2.04. Just thought I'd drop in my $.0000002 ...

My current project involves a 200x200 area of the screen that is used for the actual game action, the rest of the screen rarely changes (i.e., scores and some incidental graphics, etc.). The 200x200 area has a 240x240 border around it, and as you might expect, I needed to clip all my graphics against that 200x200 area.

I wrote my own clipper for it (the one posted in the sticky thread here), and it worked pretty well. However, I prefer my code be a concise as possible, and the extra RECT setups and function calls didn't thrill me.

So, based on a suggestion I read here, what I did was to create a 400x400 surface, and then blit all the game elements to that. Then, at the end of ProcessNextFrame, I blit the center 200x200 RECT of that onto backbuffer, and I basically get clipping "for free" (as in no additional code required) and I keep my code less verbose than before.

On my stock MIPS-based Casio E-125 (150MHz that is), I was getting a fairly solid 40FPS (which is what it's coded to) all the time. There were some parts that would dip down to 37-39. That seemed pretty good to me, certainly sufficient for this game.

However, with the addition of GD 2.04's clipping, I decided to do away with the extra surface-to-backbuffer blit, if for no other reason than to have some extra speed overhead, assuming it was faster than the double-blit approach.

I can report that the frame rate now stays solidly at 40 at all times with only one exception (which is due to a bit of unoptimized code on my part).

So, while I don't have any comment on the built-in clipper vs. my own clipper, since I didn't make that comparison, I can say that if you are using the extra surface approach that I was, you can safely do away with it and actually gain a little bit of performance, not to mention a little bit of memory, since you won't have that extra surface floating around.

Take that for whatever it's worth, which probably isn't much!, but there it is anyway. :)
warmi · 04 Aug 2003, 20:24 UTC — warmi
It almost never makes sense to avoid clipping using oversized surfaces.
After all , a clipping routine is only dozen or so instructions which is significantly faster than blitting hundreds of additional pixels.
Clipping is often considered performance enhancing technique ( 3d etc ) so unless you are blitting 2x2 sprites, it always makes sense to clip.
Performance-wise, yes, no argument. But sometimes it's not the only consideration. I'll take less verbose code over top-notch performing code (assuming the slower performance is still acceptable of course) any day.
I live by the statement, "make it work right, then make it work fast." A clever optimization is almost never worth the complexity of the code, and when running a program through a profilier the bottlenecks are almost always some place unexpected.
"make it work right, then make it work fast."

That's precisely why it is much simpler and much less error prone to create a generic clipping routine that will work in every situation than to worry about solutions like oversized surfaces and similar stuff that changes with every game and can lead to memory overrun errors as soon as your sprite strays even only 1 pixels beyond the limits of the surface.
I disagree with part of what you said warmi...

Since GD just doesn't draw anything that strays off the edge of a surface, and since we all trust Johan to have implemented that in such a way that a memory overrun wouldn't occur, that solution is no more error-prone than a generic clipper in my mind.

In addition, since you are basically creating a viewport by virtue of the fact that you are only blitting to backbuffer a portion of the oversized surface, it's not truly as inefficient as it could be. Worse than simply clipping, true (that was the point of my post), but not terribly so.

Creating a generic clipper that works in every case isn't quite as trivial as it seems (I think even Johan had some trouble with regard to clipping and rotations if I remember correctly). It's arguably not an efficient use of my time to code that, even if it only takes an hour let's say, when I can do the oversized surface thing in 1 minute and spend the other 59 minutes coding my game.

I do agree that such a solution might well change from game to game, and that is undesireable. But I think it's not what I would call a big deal: "hmm, lemme see... my oversized surface was 400x400 in the last game, but this is a full-screen game, so I guess it should be something like 480x640 this time around". See, not a big deal to change it.

That all being said, this is all really more a matter of code aesthetics, since performance is pretty close with either solution. My original point was simply that when I had to have extra lines in my own code to do clipping myself, I didn't like it and preferred the "cleaner" approach, relatively speaking, of the oversized surface approach. With it built into GD now, that concern has been rendered null and void and I don't advocate the oversized surface approach at all. Just offering a comparison if anyone else is considering it for some reason.

In the end, I suspect we're all going to use the built-in clipper and be happy doing so. The rest of this discussion is just interesting chatter now.
I wrote all the clippig code myself and just punted on the rotation near the edge. For those tiles I just unrotated and then clipped them. That is why I was happy to hear real clipping was put it. It let me rip out my clipper and allowed the items near the edge to rotate again.

I should have thought about the over-sized buffer solution. It is a much easier solution when you have rotated tiles you want to clip.