PocketMatrix forums

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

Pixel Modulation

i'm currently rewriting my rasterizer. there i came to the problem of effectively modulating two 16-bits RGB565 values.

the fastest solution i have so far is:

Code:

tmp = (c0&REDBLUE;_MASK) * (c1&REDBLUE;_MASK);
dst = ((tmp>>16)&RED;_MASK) |
((((c0&GREEN;_MASK) * (c1&GREEN;_MASK))>>11)&GREEN;_MASK) |
((tmp>>5)&BLUE;_MASK);

any idea on how to improve this or is this pretty much it?
(it looks for me as if the green component could be done more effective...)

thanks,
DANIEL
Dave H · 12 Dec 2003, 18:25 UTC — Pixel Modulation
drgoldie wrote:
i'm currently rewriting my rasterizer. there i came to the problem of effectively modulating two 16-bits RGB565 values.

the fastest solution i have so far is:

Code:

tmp = (c0&REDBLUE_MASK) * (c1&REDBLUE_MASK);
dst = ((tmp>>16)&RED_MASK) |
((((c0&GREEN_MASK) * (c1&GREEN_MASK))>>11)&GREEN_MASK) |
((tmp>>5)&BLUE_MASK);

any idea on how to improve this or is this pretty much it?
(it looks for me as if the green component could be done more effective...)

thanks,
DANIEL


This might not work but I had the idea a while ago of moving the green component out of the middle, multiplying and then shifting back. I only even did it with /2 but to be honest I'm thinking now it could work with multiplictions up to 31.

e.g.
Original pixel value:
0000000000000000 rrrrrggggggbbbbb

Shift out
00000gggggg00000 rrrrr000000bbbbb

This means you can multiply by up to 31 without hitting the others -- or is it only powers of two which work..?

e.g. multiply by 4/32
000GGGGGGgg000RR RRRrr0000BBBBBbb * 4
00000000GGGGGGgg 000RRRRRrr0000BB /32 (>>5)

And there is your RGB
00000000GGG00000 000RR000000000BB
Stick the green back in
0000000000000000 000RR000GGG000BB

Et Voila. Could work quite well on ARM assembler actually couldn't it?

*update* - oops, sorry you needed a colour modulation, sorry, though it was white only!
I believe we discussed this before. It's possible, and I do it all the time. ;)
You can multiply by anything, it doesn't have to be base 2.

There are a few problems with this technique.

Scalar is limited to 5 bit. For uses like gouraud shading the banding is very noticable compared to 8 bit.

You double your texture size, which doubles the amount of cache loads. Memory access is the big problem on a PPC (if you are using only fast integer math).

Your inner loop can become so tight that is stalls out waiting to access the next texel. This is exactly what happened with my tests. Thus the extra cycles wasted during the stall can be used to do the proper 8 bit math for higher quality output, if your asm is properly arranged (ie don't use the data immediately after you fetch it)

The last two issues combined negated performance in my case.

Dan East
Actually technically you can multiply by a number from
[64..0] where 64 is 1.0 and 0 is 0, being assured that there will be no conflict in bits. Reason being if you extract the green bits first, you will have 6 bits for the result of the b*cont to over flow into. (Please note this will only work if you are processing 1, 16 bit pixel at a time)

rrrrrggggggbbbbb =>
rrrrr000000bbbbb | 00000gggggg00000

so if we multiplied the RB factor by some arbitrary const [0..64] we would have

RRRRRrrrrrrBBBBBbbbbbb compared to the orginal rb
......rrrrr000000bbbbb

I'm using code very similar to this in my 3D engine

Code:

; multiply a 565 pixel by a 7 bit const in the
; closed interval [64..0]

ldh rgb, [pixel]
bic rb, rgb, #$07E0 ; clear the green
and g, rgb, #$07E0 ; clear the rb
mul rb_result, rb, const
mul g_result, g, const
mov rb_result,rb_result LSR #6 ; shift back into place
mov g_result,g_result LSR #6
bic g_result,g_result, #$1F ; clear lower order bits
bic rb_result,rb_result, #$07E0 ; clear bits in green
orr final_rgb, g_result, rb_result
well, it seems as if the modulation can not be reduced to one multiplication. but at least i can use the green-component's mul for modulating alpha too.
so that's four color channels with two mul operations. that's quite ok i'd say...

thx,
DANIEL
Dan East wrote:
You can multiply by anything, it doesn't have to be base 2.

There are a few problems with this technique.

Scalar is limited to 5 bit. For uses like gouraud shading the banding is very noticable compared to 8 bit.

You double your texture size, which doubles the amount of cache loads. Memory access is the big problem on a PPC (if you are using only fast integer math).

Your inner loop can become so tight that is stalls out waiting to access the next texel. This is exactly what happened with my tests. Thus the extra cycles wasted during the stall can be used to do the proper 8 bit math for higher quality output, if your asm is properly arranged (ie don't use the data immediately after you fetch it)

The last two issues combined negated performance in my case.

Dan East


Is 5-bit (or 6-bit) too limiting even though the output is only 565?
Yeah. It's really noticable. I'll try to do a screenshot sometime to contrast.

Dan East
Dan East wrote:
Yeah. It's really noticable. I'll try to do a screenshot sometime to contrast.

Dan East


Would it be possible to use some kind of error diffusion stippling to reduce the banding?
when i wrote the method to modulate RGBA with two multiplications, i had to choose whether to use only 5 bits for green or for alpha (should not be possible to do both with 6 bits precision in one mul op).

first i thought it would be wise to modulate green with only 5 bits (since red and blue are done the same way) but it looked really awful. i thought i had an error in my code but checking the resulting image in photoshop showed me that 5 bits is just not enough for green while it looks ok for red and blue.

so i went for 5 bits for the alpha channel which is not that apparent...

DANIEL
drgoldie wrote:
when i wrote the method to modulate RGBA with two multiplications, i had to choose whether to use only 5 bits for green or for alpha (should not be possible to do both with 6 bits precision in one mul op).

first i thought it would be wise to modulate green with only 5 bits (since red and blue are done the same way) but it looked really awful. i thought i had an error in my code but checking the resulting image in photoshop showed me that 5 bits is just not enough for green while it looks ok for red and blue.

It's kind of unfortunate that the eye is most sensitive to differences in the green component, which is also why it's green getting the 6th bit in RGB-5-6-5 format.

- Martin
drgoldie wrote:well, it seems as if the modulation can not be reduced to one multiplication. but at least i can use the green-component's mul for modulating alpha too.
so that's four color channels with two mul operations. that's quite ok i'd say...

thx,
DANIEL


Sorry for replying to an old thread but I've been optimising my 3D software renderer when I came across this.

You can do color modulation with one multiplication if you use the long type.

Code:


u32 color = 0x0f4f
color = (color|(color<<20))&0xf000f0f;
u64 temp = (u64)color*0xc000a0f;
temp &= 0xf0000000f000f0;
color = ((temp>>4))|((temp>>12))|((temp>>48));


The color is in 444 but can be made for 565. I hope this can be useful to somebody.
Hi all, been a Long long time since I've posted....

I'm looking into creating transparency masks for my objects and im wondering whats the best way to go about it?

atm I'm using gzipped tga files because there easy to use, with a mask color for full transparency. Has anyone written any png librarys? as they have blending transparently etc?

thanks in advance.
I had a play with png but found it a bit complicated for me - I'm simple. Plus I think it added a few hundred kb to the exe but that could have been because I built the entire thing instead of just support for what I needed. If you get png working well with a small footprint I would like to hear how you did it.

Instead, I ended up building a little win32 command line utility that converted png files with alpha channel into my own encoded format that included the alpha channel and with pixel format in 16bit565 already so no conversion was required at the ppc side. Works really well. The encoder/decoder only has about a 2kb footprint and is really fast, with the compromise being images are larger than they could have been if png, but hasn't been an issue so far. If you had a lot of rich images that need to be alpha blended then png would probably be a good option.

Another option could be to store the alpha as a separate resource in your own custom way and merge it with the pixel data when the image is loaded. I would suspect the alpha data would compress well using a simple run length encoding.

In any case you will need to build additional rendering capabilities to account for the alpha channel (assuming you are not using some library that has it already). I chose to store the alpha value with each pixel as opposed to in a separate block of memory but I don't know if that's better or worse.

There was another thread where Torus??? (I think - going back a bit now) posted how to do alpha blending with a few bitwise and a single mul which to this day I'm thankful for. If you can't find it I can post some code. Correct me if I'm wrong but the option of doing a single mul with 64bit is really bad - the Torus way (I hope it wasn't someone else :)) did it with a single mul and all 32bit - lightning fast. In fact the app I'm working on at the moment I originally stayed away from anything alpha blended but performance is so good that the entire screen pretty much is now a product of heaps of alpha blended images.
it depends what kind of alpha you want, if you just need 1 bit of alpha then just use a colorkey, if you need 8 bits then either use a separate 8 bit image (bmp or tga), or tga supports color and alpha and is by far the easiest to use. just use whatever is easiest, dont worry about the size, with storage cards over 4GB these days worring about the size is a waste of time.