BUffer Locking - Emulation Of How Buffer unlocking works in graphics commands

Started by kevin, February 13, 2023, 05:59:31 AM

Previous topic - Next topic

kevin

  Emulation Of How Buffer lock and unlocking works in graphics commands


  This is not really a practical source code example rather it's a an emulation of showing the integration of buffer lock/ and unlocking and how managing those yourself can reduce remove a bottle neck when rendering pixels to a surface.  


PlayBASIC Code: [Select]
      loadfont "Courier new",1,50

INIT()

Frames_To_TEst=50

do

Test1(Frames_To_TEST)
Test2(Frames_To_TEST)

Sync
loop scancode()<>0




Function Test1(Frames)
do
cls

for lp=0 to 250
DrawDot(frames+lp,lp,$ffffff)
next

print "test #1"
print fps()

sync
Frames--
loop Frames<0

EndFunction



Function Test2(Frames)
do
cls

lockscreen()
for lp=0 to 250
DrawDot(frames+lp,lp,$ffffff)
next
unlockscreen()

print "test #2"
print fps()

sync
Frames--
loop Frames<0

EndFunction





Type tLockState
Count
Ptr
Pitch
Depth
EndType
Dim LockState as tLockState

Function INIT()
Dim LockState as tLockState
LockState = new tLockState
EndFunction


Function LockScreen()
State=LockState.Count

if State=0
LockBuffer
NULLPixel =point(0,0)
lockState.ptr = GetImagePtr(0)
lockState.pitch = GetImagePitch(0)
lockState.depth = GetImageDepth(0)
endif
LockState.Count=State+1
EndFunction



Function UnLockScreen()
State=LockState.Count
if State>0
State--
LockState.Count=State
endif
if State=0
unLockBuffer
endif
EndFunction


// Command set mock up


Function DrawDot(X,Y,ThisRGB)

LockScreen()

// clip it
if x>=0 and X<getsurfaceWidth()
if y>=0 and y<getsurfaceHeight()

// write it to surface
ptr=lockState.ptr
if LockState.Depth=32
pokeint ptr+(y*LockState.Pitch)+(x*4),ThisRGB
endif
endif
endif

UnlockScreen()
Endfunction







Related Links)

     - PlayBASIC Documentation - LOCKBUFFER - UNLOCKBUFFER
     - A Crash Course In BASIC program Optimization