Reading buffer content

Blitz3D Forums/Blitz3D Programming/Reading buffer content

alain(Posted 2011) [#1]
Hi,

I am trying to optimize my function to save a buffer to the disk.

I checked the forum and found some useful work from "Serpent" and "MickailV" about accessing the buffer directly.

Here:
http://www.blitzbasic.com/Community/posts.php?topic=90333

and

http://www.blitzbasic.com/Community/posts.php?topic=92849#1060106

So I wrote a test program to try their direct access to the buffer:

;---------------------------------------------------------------
; Rendering a basic test scene
;---------------------------------------------------------------
Graphics3D 1024,1024,32,2
SetBuffer BackBuffer()

camera=CreateCamera()
CameraClsColor camera,255,128,0
light=CreateLight()
RotateEntity light,40,30,130

; Create cube
cube=CreateCube()

PositionEntity cube,0,0,3
TurnEntity cube,10,40,80
RenderWorld
Flip
;---------------------------------------------------------------
; Getting front buffer in a bank
;---------------------------------------------------------------
bu = FrontBuffer()
LockBuffer bu
LocBnk = CreateBank(76)
MoveMemoryObjInt(LocBnk,bu,76)
Loc = PeekInt(LocBnk,72)
FreeBank LocBnk
bmp = CreateBank(GraphicsWidth()*GraphicsHeight()*4)

t1 = MilliSecs()
MoveMemoryObjInt(bmp,Loc,BankSize(bmp))
t2 = MilliSecs()

UnlockBuffer bu 
;---------------------------------------------------------------
; Display results
;---------------------------------------------------------------

RuntimeError "time for moving memory: "+(t2-t1)+" [ms]"
End



Decls:
.lib "kernel32.dll"
MoveMemoryObjInt(Destination*,Source%,Length%) : "RtlMoveMemory"


The problem I have is the "MoveMemoryObjInt takes on my computer 650 [ms]... I tried this method to go faster than using the ReadPixelFast method, but it is a slow as simply looping all the pixels and getting the value... Why?

Moving all the memory in one step should be a lot faster than moving pixels one by one..Am I missing something?

Last edited 2011


alain(Posted 2011) [#2]
Update:

It seems moving memory from texture to bank is slow, but moving memory from bank to texture is very fast.

MoveMemoryObjInt(bmp,Loc,BankSize(bmp)) takes 650 milliseconds on my computer, but:

(moving back the bank to the texture)
MoveMemoryIntObj(Loc, bmp, BankSize(bmp))
takes only 1 milliseconds....

Last edited 2011


Rroff(Posted 2011) [#3]
Don't read the front/back buffers directly, this will be very slow even on the best PC. Instead use copyrect to move the data from the buffer into a texture or image buffer and read/move from there.

Except on old PCs i.e. AGP, it takes about 14ms to copyrect and read every pixel with readpixelfast from an average resolution, directly reading from the front/back buffer can easily be 20-40x slower.

Last edited 2011