PocketMatrix forums

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

Rasterizer thoughts

Hi Thierry,

I just browsed through your design a bit and have a few questions:

(1) Rasterizers

I understand that from a design point of view, rasterizers are more "expandable" than adding drawing functions to the surface class. I had a similar design in GapiTools where the developer always had to define a "target surface/buffer" for all operations.

In practice, we found that it was too easy to forget what surface was the "active" surface for the GapiTool rasterizer. Since PocketFrog no longer have the Surface and Rasterizer connected (other than the display->backbuffer and the surface link set with "SetRenderTarget"), I am curious on your comments on this. Of course it is possible to always create one rasterizer for each surface, but then the advantages of separation for the end user (games developer) is not that clear.

(2) STL

How has the response been so far as to integrating STL into the code base?

Best regards,
Johan
1) Rasterizers: it was indeed a though call. I don't like to deal with a separate entity for simple rendering, yet I feel it was the best way to provide alpha blending and custom pixel shaders easily.

Because I didn't want people to deal with the SerRenderTarget() thing if they didnt feel confortable with it, I've derived DisplayDevice directly from Rasterizer and linked it to the backbuffer. In practice, this means you can simple ignore the fact that rasterizers are a separate entity until you are ready to render to a different surface (say directly to the frontbuffer or something else). I would suggest instantating a new rasterizer for this though instead of retargetting the one in DisplayDevice.

I found that in practice you rarely change the render target anyways... And with this design the user has the three options available: 1) Ignore it completely, 2) Change the target or 3) Create a new rasterizer object (quite a lightweight object BTW).

My main concern right now is that adding a new primitive (say arcs) is not as simple as I would like it to be (you have to understand the Internal::GraphicBuffer class and how it relates to the Surface class). It is always possible to use direct access (Lock/Unlock), but performances may not be acceptable.


2) This STL thingy is an experiment... You don't actually need it to use PocketFrog (except for the cassert.h header, but I'll fix that soon). The STL is required if you compile PocketFrog though. I had a std::set<> in DisplayDevice to hold a reference to all existing surfaces at one point. Really helpfull to dump diagnostic messages to the debug window on shutdown, but I didn't want to impose STL on a PocketFrog user just for that container. I did consider using a void* member in DisplayDevice (and cast it to std::set<>* in the implementation feel, but I couln't live with such a gross thing.

Nowaday, I am having a really hard time programming without the STL and was quite happy to find that STLport was working perfectly (ok, no C++ exceptions) on the PocketPC.

Thanks for your feedback Johan, it is appreciated!
Hi again!

:D

I thought the rasterizer idea was kind of cool so I looked into your implementation a bit... Man. You are one serious OO developer.. ! :)

I am currently chasing cycles with GD and am pretty content with the current feature set. However, in order to optimize speed I had to change all:

Code:
some blit loop {
for (height) {
for (width) {
if (key sourced) adjust for key source
if (colorfill) adjust for colorfill
if (opacity) adjust for opacity
// pixel copy
}
}
}


into:

Code:
if (key sourced)
if (colorfill)
if (opacity)
// full inner loop (key, cf, opacity)
else
// full inner loop (key, cf)
else
if (opacity)
// full inner loop (key, opacity)
else
// full inner loop (key)
else
... four more of these


Basically it resulted in a very nice performance boost. But maintaining it is.. Well.. Not the most fun I guess...

So I checked your rasterizer. My main question is how you will solve the problem of having more than one operator on each pixel.

Basically you start with a simple blit. The key sourcing operator was solved by using two blit calls (blit and blitmask). Then alpha is used as a pixel rasterizer. If you were to add a colorization for example. How would you do that? I was thinking of a solution of chaining pixel rasterizers. It would be a nice design, but it could probably limit the performance too much.

Also. I didn't look that much into your implementation, but are the pixel rasterizer called as a function? It looked like it, but I can be seriously wrong (it looked like you called the embedded operation of the rasterizer struct). Wouldn't that cause the overhead of performing a function call on each pixel?

To sum up. My implementation is very fast but results in (at least) eight copies of each blit operation. Your solution is really good looking, but I am currently unsure of how it will perform (compared to expanding the operations as I did). Also I am not sure how expandable it is if you have the need for multiple operations on each pixel. Please do not take this as critique in any way, because your oo design is really impressive, and it is very clear that you have put a lot of thoughts into the design of PocketFrog. If anything should be critizized it is my implementation with 8 copies of every blit operation...

Also. Just spotted. The transform_if could be improved somewhat. It now reads the source value from memory twice. Basically it would improve by taking a temporary DWORD as a parameter, store "*__first1" in it and then reuse it. Of course it could add a DWORD on stack as in the alpha, but this would cause additional overhead compared to storing it in a parameter as it is used inline. I guess I would change it to:

Code:
transform_if( _InputIter1 __first1, _InputIter1 __last1,
_InputIter2 __first2,
_OutputIter __result,
_Predicate __pred, _BinaryOperation __binary_op,
DWORD dwTemp )
{
_STLP_DEBUG_CHECK(__check_range(__first1, __last1))
for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
{
dwTemp = *__first1;
if (__pred(dwTemp)) *__result = __binary_op(dwTemp, *__first2);
}
return __result;
}


I think your alpha code could benefit from using temporary variables passed as parameters as well.

I have basically looked into most options of using inline functions and templates, and generally came to the conclusion that what's really lacking is a C++ language feature (expand this loop depending on the IF's contained within into as many optimized loops as necessary to avoid the internal IF's). My guess is that NVidia and the other chipmakers have such tools, which could explain the insane amount of transistors being used on those chips.

Regards,
Johan
I see we are struggling with the same problems ;)

The main thing about PocketFrog's rasterizer is that it allows you to define pixel operations for any primitive (line, circle, blit, etc).


So I checked your rasterizer. My main question is how you will solve the problem of having more than one operator on each pixel.


I don't think there is a problem here. You could chain pixel shaders, but then performance would be unacceptable. What you should do instead is create your own custom pixel shader (that does all you need) and plug it in the generic rasterizer. There will be an overhead of a virtual function call every pixel (not very nice!) but it will work. In any cases, the two most useful rasterizers (SrcCopy and AlphaBlending) are expended to provide optimal performance. I intend to provide a way for the user to expand the templates in ALGO2D.H with their custom pixel shader if performance is critical.


Also. I didn't look that much into your implementation, but are the pixel rasterizer called as a function? It looked like it, but I can be seriously wrong (it looked like you called the embedded operation of the rasterizer struct).


Yes, that's right.


Wouldn't that cause the overhead of performing a function call on each pixel?


There you are wrong. Every templated functions get expanded inline. In the end, there is no function call whatsoever. I did verify this for the SrcCopy shader, I will have to check the AlphaBlending shader. In any case, the intend here is to have everything inlined. I haven't yet started optimizing the algos, I will probably plug in what I had in PocketFrog 0.4.5b for the SrcCopy shader.


Also. Just spotted. The transform_if could be improved somewhat. It now reads the source value


Not really... As I said above, everything gets inlined and it doesn't get better then this. That's the whole point of using templated algorithms.


I think your alpha code could benefit from using temporary variables passed as parameters as well.

I don't follow you here. How would temporary variables improve performance? I was thinking more along the line of blending two pixels at once using 32 bits accesses...


To sum up. My implementation is very fast but results in (at least) eight copies of each blit operation.


So does mine... I simply don't have to write them by hand. Let the compiler do the work.


I have basically looked into most options of using inline functions and templates, and generally came to the conclusion that what's really lacking is a C++ language feature (expand this loop depending on the IF's contained within into as many optimized loops as necessary to avoid the internal IF's).


It's already in there... It's called templates.
Hi Thierry,

I guess I kind of got mixed up a bit here. 8)

(1) Function calls

I missed that the Algo2d::blit was inlined. I thought it was an ordinary function taking a struct ref as parameter. As it is now you are entirely correct, and it's kind of a neat solution.

(2) transform_if

"everything gets inlined and it doesn't get better then this"

I don't agree. :) My comment was not on the inlining, but on the use of two memory accesses ("*__first1"). Of course one would hit the cache, but it's still two memory accesses. Register access will always be faster.

(3) Loop Expanding

Yep. Figured out a solution myself.. I think I will use the expanded way I listed above (the "if (bKeySourced) { if (bOpacity) { ... " one). In each loop I can then call a template with another template as pixel shader. I'll post my solution on the web for comments later today.

I have also read several articles on template expansion and performance, and as I see it it's is not clear that template expansion will provide the same performance as complete loop unrolling. Of course the code will be 1000 times more maintainable, but in the end it's only performance that matters (for the end user).

Thanks for your time!

/Johan
Hi Johan,


I don't agree. My comment was not on the inlining, but on the use of two memory accesses ("*__first1"). Of course one would hit the cache, but it's still two memory accesses. Register access will always be faster.


What makes you think there will be two memory accesses? This is a trivial optimization for any compiler.



Of course the code will be 1000 times more maintainable, but in the end it's only performance that matters (for the end user).


Here I don't agree. Performance is important, but it's not the only thing. Features, maintainability (customizations), ease of use, memory footprint, etc. are all important factors.


Thanks for your time!


Hey, no problem!
Hi! I'm back again. :)

I have now checked multiple cases of C to ASM code (using both EVC and VSNET) and came to the conclusion that the compiler is pretty smart.

Both my suggestions for improval above were totally irrelevant. The compiler will always output optimized code in these cases.

Also, I actually managed to get templates to work in my own implementation (despite all the different options in every blit function). It was a really smart move you did by passing a struct containing the operator() as a parameter. If I haven't seen your solution with structs I would never have finished my own solution this fast. Right now I have three operators: SourceColor (source bitmap/colorfill), PixelVerifications (check mask or always copy) and PixelOperations (copy/alpha/alphafast). I also made a bunch of others such as coordinate transforms (fixed point etc). Basically I was able to cut back all code to only one loop for each operation, which at compile time expands to up to 12 different combinations. Studying the ASM output the compiler actually does a 1:1 job of compiling the expanded templates as compared to expanding all loops in code.

I'll post my template findings in a new post when I have cleaned up the design a bit. I will also look into double pixel processing (something which is currently not done and should improve performance considerably in all operations).

By the way. Check out this screen shot. With the new optimized "templated" code I managed to squeeze over 140 alpha blended sprites in my demo (170 sprites without opacity). :) Kind of cool, and not really possible to achieve without using templates (without templates the only choice I guess is to (1) add IF statements to the inner loop resulting in less performance, or (2) expand all possible loops, which in my case were 12 different ones)...

<img src="http://www.gapidraw.com/demo_alpha.jpg">

/Johan
Glad you could learn a few things from my design =)

I am looking forward to read about your findings using templates.