PocketMatrix forums

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

Best Way to plot Pixel/Draw Lines (Code Snippets plz :))

Hello... Sorry for disturbing again :)
I wanted to ask for your fastest pixel plotting / Line Drawing algos, cause my Engine momentanli is really slow... So I want to speed it up. I Have A Function Plot_Pixel16, but its painfull slow... I would be very pleased for ideas which could help. thx alot, and sorry for my bad english

greetz

Lowtraxx
lowtraxx wrote:So I want to speed it up. I Have A Function Plot_Pixel16, but its painfull slow... I would be very pleased for ideas which could help. thx alot, and sorry for my bad english

Can you show us your current code? What exactly is your line drawing function doing? Just rastering a constant color line? Or more sophisticated stuff (blending, smooth color, etc..)?

Inlining your Plot_Pixel16 function into the line drawing code is a good start (check the assembly output of your compiler; maybe you need to turn it into a macro); use of suitable fixed point math helps, too, (you can download fixed.h/fixed.cpp from the ogl-es project on sourceforge) otherwise good ol' Bresenheim should get you pretty far already.

Cheers,
HM
lowtraxx · 06 Jun 2004, 00:27 UTC — Function Code
Code:

inline int TSLib3D::Plot_Pixel16(unsigned char *VidMem, int X, int Y, COLOR color)
{
unsigned short usColor;
int adress;


adress = (X * GetXPitch()) + (Y * GetYPitch());


usColor = (unsigned short)
(((color.Red & 0xf8) << 8) |
((color.Green & 0xfc) << 3) |
((color.Blue & 0xf8) >> 3) );


*(unsigned short *)(VidMem + adress) = usColor;
return 0;
}


Line Plotting is Bresenham´s Algo
The COLOR -> u16 conversion strikes me as a bad idea.
Code:
adress = (X * GetXPitch()) + (Y * GetYPitch());


Lets review this line. It consist of 2 function calls and 2 multiplications.

1. Since the function call return constant values pull it out of PlotPixel.
2. If you draw horizontal lines, you dont need a complete address calculation evey pixel

If you wanto to speed it up even more, inline the function.

Depending on the "cleverness" of te compiler, it may be a good idea to avoid temporary storage (ie. adress and usColor). Of course a clever compiler never assignes any stack space for those 2 variables, but you never know :)

Tala
I didn't think there was any overhead for those functions, presuming they're inlined and simply fetching a private member.
Tala wrote:
Code:
adress = (X * GetXPitch()) + (Y * GetYPitch());


Lets review this line. It consist of 2 function calls and 2 multiplications.

1. Since the function call return constant values pull it out of PlotPixel.
2. If you draw horizontal lines, you dont need a complete address calculation evey pixel

So we got to the following proposals so far:

1. Convert the color to U16 color565 value upfront before you enter the line drawing loop.
2. Bresenham basically increments x and conditionally increments/decrements y (or other way around); instead of having x, y and vidmem ptr calculate x * GetXPitch(), y * GetYPitch upfront; set vidmem to address of first pixel to draw; use these value to increment your pointer variable directly within the drawing loop (i.e. while(...) { vidmem += xoff; if (need to step) {... vidmem += yoff; } } ).
3. Instead of calling into the function just do a *vidmem = color565;

- HM
joshbu [MSFT] · 06 Jun 2004, 18:31 UTC
So, I wnated to add a few general performance guidelines.

1. Always identify a reproducable scenario. In Quake, for example, the feature that records game sessions is perfect.

2. Conditional compliation of instrumentation is very useful as well. A simple FPS counter as well as hooks for more sophisticated profilers, for example.

3. Don't change code until you have a baseline. That's how you know you're getting better.

4. Measure twice, cut once.

5. If you're curious about what's getting inlined and what is not, use the compiler to output the generated assembler file (with the same code generation switches) and take a look. Even eithout knowing a lot about assembler, you probably can figure out if you're getting the inlining/constants you think you are.

Now, in this case, things look pretty straight forward. A PlotPixel routine is pretty useful from a code readibility point of view, but is almost certainly a bad idea in performance critical code.
lowtraxx · 09 Jun 2004, 23:50 UTC — Thx so much // Need more Input :)
Hello,

First off all, thank you so much for all your help! Your Hints are great! I thought about your postings and came to the point to rewrite everything... I will use André LaMothe´s Tricks of the Windows Game Programming Gurus and Tricks of the 3D Game Programming Gurus in Combination with J. Harbours Pocket PC Game Programming. But there are still a Lot of Proiblems *g*. First, where are the f-functions? (fmodf...) I only found fmod. Second, André has a function like this:

Code:

// Precompute first pixel Adress in video buffer
vb_start = vb_start + x0 + y0*lpitch;


Could someone explaint this to me? and how to port this to the PPC?
What is the lpitch component on the PPC is it cbxpitch?

Then he draws the Pixel with
Code:

*vb_start = color;


Can i do the same on a PPC Display, and how?
Then do i need to clip the client area on a PPC?

I know these are many Questions, but i am completly new to PPC Development :oops: (as i mentioned in an earlier post :) ) And everyone started as a noob sometime ago :)

So thanx alot, and hope to hear a lot feedback *g*

Greetz

Oli
Could someone explaint this to me? and how to port this to the PPC?
What is the lpitch component on the PPC is it cbxpitch?


That is exactly what you already doing in your code.

Code:

adress = (X * GetXPitch()) + (Y * GetYPitch());
*(VidMem + adress) = usColor;


is the same as:

Code:

adress = VidMem + X*GetXPitch() + Y*GetYpitch();
*adress = usColor


Since XPitch is usually 1, you dont need the first multiply.

Tala

ps:
lpitch = linepitch = ypitch

pps: As already noted, this address calculation is only necessary for the first pixel. Dont do it every pixel, rather use the algorithm hm posted above.