Fast Pixels?

BlitzPlus Forums/BlitzPlus Programming/Fast Pixels?

JoshK(Posted 2003) [#1]
How is Simon's example in the Newsletter so fast? This is SLIGHTLY faster than Blitz commands, but nowhere near as fast as his. As far as I can tell, he isn't doing anything different from this: Write your data to a bank, then copy the bank to a buffer.

Actually, My ReadPixel is twice as fast, and my WritePixel is much slower.

Graphics 640,480,32,2
buffer=BackBuffer();ImageBuffer(i)

w=GraphicsWidth();ImageWidth(i)
h=GraphicsHeight();ImageHeight(i)

start=MilliSecs()
LockBuffer buffer
For x=0 To w-1
	For y=0 To h-1 
		ReadPixelFast(x,y,buffer)
		WritePixelFast x,y,RGB(0,0,255),buffer
		Next
	Next
UnlockBuffer buffer
time1=MilliSecs()-start
Flip

start=MilliSecs()
LockBuffer buffer
pixels=LockedPixels(buffer)
pitch=LockedPitch(buffer)
bits=LockedFormat(buffer)
fakepixels=CreateBank(w*h*bits)
CopyBank pixels,0,fakepixels,0,w*h*bits
For x=0 To w-1
	For y=0 To h-1
		readpixelfaster x,y,fakepixels,pitch,bits
		WritePixelFaster x,y,RGB(255,0,255),fakepixels,pitch,bits
		Next
	Next
CopyBank fakepixels,0,pixels,0,w*h*bits
FreeBank fakepixels
UnlockBuffer buffer
time2=MilliSecs()-start
Flip

Notify "Blitz: "+time1+Chr(10)+"Faster?: "+time2

End

Function ReadPixelFaster(x,y,pixels,pitch,bits)
offset=y*pitch+bits*x
Select bits
	Case 4
		Return PeekInt(pixels,offset)
	Case 2
		Return PeekShort(pixels,offset)
	End Select
End Function

Function WritePixelFaster(x,y,ARGB,pixels,pitch,bits)
offset=y*pitch+bits*x
Select bits
	Case 4
		PokeInt pixels,offset,ARGB
	Case 2
		PokeShort pixels,offset,ARGB
	End Select
End Function



Eikon(Posted 2003) [#2]
pixels=LockedPixels(buffer)
pitch=LockedPitch(buffer)
bits=LockedFormat(buffer)


JoshK(Posted 2003) [#3]
Okay, his method isn't actually that fast. I didn't realize BlitzPlus was so much faster than Blitz 3D. Here are three different methods. With this, I get 70/110/170 FPS:

Const DWIDTH=640 
Const DHEIGHT=480 
Global tick

Function UpdateBank(bank) 
argb=tick 
tick=tick+1 
o=0
Select GraphicsDepth()
	Case 16
		For y=1 To DHEIGHT 
			For x=1 To DWIDTH 
				argb=x+y+argb 
				PokeShort bank,o,argb
				o=o+2 
				Next 
			Next
	Case 32
		For y=1 To DHEIGHT 
			For x=1 To DWIDTH 
				argb=x+y+argb 
				PokeInt bank,o,argb 
				o=o+4 
				Next
			Next
	End Select
End Function

Function CopyBankToBuffer(bank,buffer)
LockBuffer buffer
imagebank=LockedPixels(buffer) 
pitch=LockedPitch(buffer)
bits=LockedFormat(buffer)
CopyBank bank,0,imagebank,0,BankSize(bank)
UnlockBuffer buffer 
End Function 

Function Updatescreen() 
argb=tick 
tick=tick+1
LockBuffer BackBuffer()
For y=1 To DHEIGHT 
	For x=1 To DWIDTH 
		argb=x+y+argb 
		WritePixelFast x,y,argb,BackBuffer()
		Next
	Next
UnlockBuffer BackBuffer()
End Function

Function Directbuffer()
LockBuffer BackBuffer()
bank=LockedPixels(BackBuffer())
argb=tick 
tick=tick+1 
o=0
Select GraphicsDepth()
	Case 16
		For y=1 To DHEIGHT 
			For x=1 To DWIDTH 
				argb=x+y+argb 
				PokeShort bank,o,argb
				o=o+2 
				Next 
			Next
	Case 32
		For y=1 To DHEIGHT 
			For x=1 To DWIDTH 
				argb=x+y+argb 
				PokeInt bank,o,argb 
				o=o+4 
				Next
			Next
	End Select
UnlockBuffer BackBuffer()
End Function

Graphics DWIDTH,DHEIGHT,32,2
bank=CreateBank(DWIDTH*DHEIGHT*GraphicsDepth()/8) 

While Not KeyHit(1)
	;calc fps 
	fcount=(fcount+1) 
	If fcount=20 
		t=MilliSecs() 
		FPS#=20000.0/(t-ftime) 
		ftime=t 
		fcount=0 
		EndIf 
	;update screen
	Select mode
		Case 0
			updatescreen
		Case 1
			UpdateBank bank
			CopyBankToBuffer bank,BackBuffer()
		Case 2
			directbuffer
		End Select
	Color 0,0,0
	Rect 0,0,GraphicsWidth(),20
	Color 255,255,255
	Text 0,0,"fps="+FPS
	
	If KeyHit(57)
		mode=mode+1
		If mode=4 mode=0
		EndIf
	
	Select mode
		Case 0
			Text 120,0,"Blitz commands."
		Case 1
			Text 120,0,"Poke values to a bank, then copy to graphics buffer.  (Original)"
		Case 2
			Text 120,0,"Poke values directly to graphics buffer."
		End Select
	Flip False
Wend 

End



skidracer(Posted 2003) [#4]
You can't do

CopyBank fakepixels,0,pixels,0,w*h*bits

as it ignores the pitch of the pixel buffer which on some graphics cards will not be relative to the width.

The first listing also doesn't have the variable bits as global so was effectively broken.


JoshK(Posted 2003) [#5]
"bits" gets passed into the Read and Write functions.

w*bits=pitch, right?

The functions work, they just aren't that fast.