Argb question

Blitz3D Forums/Blitz3D Beginners Area/Argb question

Stevie G(Posted 2004) [#1]
I'm using this to writepixels at half of their original color. Is there a quicker way to do this with Bit shifting? Any help appreciated.

argb = $FF Shl 24 Or ( red * .5) Shl 16 Or ( green *.5) Shl 8 Or ( blue * .5 )


SoggyP(Posted 2004) [#2]
Hi Folks,

If I remember correctly, some tests were done a while ago that showed there was no real speed difference between normal arithmetic and shifting.

No doubt someone will correct me if I'm wrong.

Later,

Jes


Stevie G(Posted 2004) [#3]
I wouldn't have though much difference either but I'd like to know this for future reference as it may? be quicker on a bigger image.


Who was John Galt?(Posted 2004) [#4]
try
argb= $FF000000 or (red shl 15) or (green shl 7) or (blue shr 1)

Not sure if shifting is faster than multiplying, but this should cut down on the no of operations


jfk EO-11110(Posted 2004) [#5]
try this:
halfbright = (rgb and $FEFEFE) shr 1

you can also (50:50) mix 2 pixles that way:

mix = ((rgb1 and $FEFEFE) shr 1) + ((rgb2 and $FEFEFE) shr 1)

I just tested the speed difference, out of curiosity. Unfortunately the most speed loss happens with the readpixelfast and the writepixelfast commands. THe reason why is their VRam location (machine dependent?) and the bus bottle neck, especially with continous read/write access that prevents good instruction caching on the machine level.

The RGB halfbright code is 40:300 faster with the SHR variant, but when Writepixelfast and Readpixelfast are involved, this speed gain becomes rather irrelevant. well, in the test it was still about 20% faster.

Here's the test code:

Graphics 640,480,32,2
SetBuffer BackBuffer()

img=CreateImage(128,128)

SetBuffer ImageBuffer(img)
LockBuffer
tt=MilliSecs()
For i=0 To 1000000
; rgb=ReadPixelFast(0,0)
 rgb2=(rgb And $FEFEFE) Shr 1
; WritePixelFast 0,1, rgb2
Next
tt2=MilliSecs()
UnlockBuffer
SetBuffer BackBuffer()
Text 0,0, "SHR: "+(tt2-tt)

;------------

SetBuffer ImageBuffer(img)
LockBuffer
tt=MilliSecs()
For i=0 To 1000000
; rgb=ReadPixelFast(0,0) And $FFFFFF
 r=(rgb Shl 16)*.5
 g=((rgb Shl 8) And $FF)*.5
 b=(rgb And $FF)*.5
 rgb2=(r Shl 16)Or(g Shl 8)Or b
; WritePixelFast 0,1, rgb2
Next
tt2=MilliSecs()
UnlockBuffer
SetBuffer BackBuffer()
Text 0,16, "Multiplying: "+(tt2-tt)
WaitKey()


Simply unREM the 4 Pixel Commands to compare the speed. It may be that using a Vram resident Texture may be faster (Texture flag 256), and maybe the process can be optimized, if you first read the image to a bank and then write it back, because switching from read to write for every pixel does slow things down on the cpu-cache level, as I already mentioned.


Stevie G(Posted 2004) [#6]
Thanks folks. I've been storing the values before writepixelfast - was just doing a bit of optimizing and the HalfBright version is perfect - thanks JFK!

One day I'll study how this bit shifting stuff works!!!