Skip to content

Instantly share code, notes, and snippets.

@dw817
Last active May 5, 2016 17:42
Show Gist options
  • Save dw817/e75ebd49c9a3ab822a0d to your computer and use it in GitHub Desktop.
Save dw817/e75ebd49c9a3ab822a0d to your computer and use it in GitHub Desktop.
So I sat down and wrote a pretty comprehensive Benchmark program to determine which is the quickest way to fill a screen with random pixels. Now, because you are using GLGraphicsDriver(), no normal plotting commands are available, so you can't see the frames per second in realtime. Yet I am calculating it. If you hit [ESC] you can see the final …
' __________________________________
' // //
' // The Fastest Pixel Benchmark //
' // Version, "Paintbox" //
' // Written by David W - 02/03/16 //
'//_______________________________//
' Who gets the prize for the fastest pixel ?
Strict ' DO NOT RUN THIS IN DEBUG MODE SO IT IS BEST SPEED
crndseed(MilliSecs()) ' very fast random (see below)
Global scrnx=DesktopWidth()
Global scrny=DesktopHeight()
Local i,j,timetotal,timecount,timeslice,timewait=50,r,g,b
Local pixels[scrnx*scrny] ' used for the GlBufferSubData() method
Local pointer:Byte Ptr,pbo,a
Local pic:TPixmap=CreatePixmap(scrnx,scrny,pf_rgb888) ' for writepixel() and array.pixel() method, note no alpha
Local v:Int Ptr=Varptr(pixels[0]) ' no need to compute each time
Local typ1=1 ' in all cases, higher number is better
' typ1 = 0 Use Flip() 27.0270 fps (for array.pixels)
' typ1 = 1 Use GlFlush() 27.7777 fps (for array.pixels)
Local typ2=2 ' GlFlush Flip()
' typ2 = 0 Use SetColor & Plot 09.8039 / 09.7087 fps
' typ2 = 1 Use WritePixel() 22.2222 / 21.7391 fps
' typ2 = 2 Use array.Pixels 27.7777 / 27.0270 fps
' typ2 = 3 Use GlBufferSubData() 28.5714 / 28.5714 fps
If typ2=3
SetGraphicsDriver GLGraphicsDriver(),0 ' NOTE UNIQUE GRAPHIC MODE REQUIRED FOR THIS METHOD !
ElseIf typ1
SetGraphicsDriver GLMax2DDriver(),0 ' zero forces front buffer
Else
SetGraphicsDriver GLMax2DDriver() ' needed for intensive pixel work
EndIf
Graphics scrnx,scrny
If typ2=3
glewinit ' does anyone know what this command does ?
glGenBuffers 1,Varptr pbo
glbindbuffer GL_PIXEL_UNPACK_BUFFER,pbo
glBufferData GL_PIXEL_UNPACK_BUFFER,scrnx*scrny*4,Null,GL_DYNAMIC_DRAW
EndIf
Repeat ' {* MAIN *}
If typ2=3
glbindbuffer GL_PIXEL_UNPACK_BUFFER,pbo
gldrawPixels scrnx,scrny,GL_BGRA,GL_UNSIGNED_BYTE,Null
EndIf
For i=0 Until scrny
For j=0 Until scrnx
r=fnr(256) ; g=fnr(256) ; b=fnr(256) ' get a good random color for plotting
If typ2=0
SetColor r,g,b
Plot j,i
ElseIf typ2=1
WritePixel pic,j,i,r+g Shl 8+b Shl 16
ElseIf typ2=2
a=j*3+i*scrnx*3
pic.pixels[a]=r
pic.pixels[a+1]=g
pic.pixels[a+2]=b
ElseIf typ2=3
pixels[j+i*scrnx]=r+g Shl 8+b Shl 16
EndIf
Next
Next
If typ2=1 Or typ2=2 Then DrawPixmap pic,0,0
If typ2=3
glbuffersubdata GL_PIXEL_UNPACK_BUFFER,0,scrnx*scrny*4,v
glbindbuffer GL_PIXEL_UNPACK_BUFFER,0
EndIf
If typ2=0 Then SetColor 255,255,255 ' since you changed it above
If typ2<3 Then SetScale 3,3 ' note if typ2=3 then no normal graphics are available
If timewait
timewait:-1
Else
If timecount Then timeslice=MilliSecs()-timeslice ' you will have to hit ESC to see the elapsed milliseconds
timetotal:+timeslice ' wait 50 cycles before starting to clock
If typ2<3 And timecount>0 And timewait=0
SetColor 0,0,0
DrawText 1000.0/(timetotal/timecount)+"fps",13,13
SetColor 255,255,255
DrawText 1000.0/(timetotal/timecount)+"fps",10,10 ' show how many milliseconds have elapsed between frames
EndIf
timecount:+1 ' NOTE ! Speed is averaged over time so it's very accurate
timeslice=MilliSecs() ' back to back, this gets the most accurate reading
If typ2<3 Then SetScale 1,1
EndIf
If typ1=0
Flip
Else
glflush
EndIf
Until KeyDown(key_escape) ' {* END OF MAIN *}
Print 1000.0/(timetotal/timecount) ' in case you were using GLGraphics, now you can see the frames per second
' >> RETURN VERY FAST RANDOM NUMBER & SEED GENERATOR
' THANK YOU MANEL IBÁÑEZ
Function fnr(a)
Return _crand()Mod a
EndFunction
Extern "C"
Function crndseed:Int(val:Int) = "srand"
Function _crand:Int () = "rand"
End Extern
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment