News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Direct 2D Buffer Writting at 256 x 192

Started by baggey, May 29, 2018, 12:31:17 PM

Previous topic - Next topic

baggey

Finally a Respectable 60 FPS a second! That's 49152 pixels Being Drawn in realtime.

      Now can we gain anymore from Pallette Mapping!??!

Example Code:
PlayBASIC Code: [Select]
 PROJECT : 2dDirectBufferWriting
; CREATED : 29/05/2018
; EDITED : 29/05/2018
; Made By Lee Chatt @ "DWARFWARE"
; ---------------------------------------------------------------------

openscreen 800,600,16,2
Cls RGB(0,0,0)

scalex=2.8
scaley=2.6
width=256
height=192
CreateFXImageEx 1,width,height,16

do

Xpos=13
Ypos=10
lockbuffer
rendertoscreen
fillimage(1)
DrawrotatedImage 1,Xpos,Ypos,0,scalex,scaley,1,1,true

xx=xpos+30
yy=ypos+15

Text Xx,Yy, " Address:"+Str$(GetImagePtr(1))
Text Xx,Yy+20," Pitch:"+Str$(GetImagePitch(1))
Text Xx,Yy+40," Depth:"+Str$(GetImageDepth(1))
text xx,yy+60," Fps:" +str$( FPS())

unlockbuffer
Sync
loop

WaitKey





Function FillImage(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

For xlp=0 To imgwidth-1

pokeword Address+(ylp*pitch)+(xlp*2), rgb(rnd(255),rnd(255),rnd(255))

Next

Next
UnLockBuffer
RenderToScreen

EndFunction




Please don't point me anywhere! Im on a very steep learning curve and miss the obvious! Im struggling as it is!


There is slight tearing on the edge's thou.


Been playing a little more tonight.

Managed to get another 2 FPS from this version! Thats 62 FPS 49152 pixels on the fly.

Example Code:
PlayBASIC Code: [Select]
; PROJECT : 2dDirectBufferWriting2
; CREATED : 29/05/2018
; EDITED : 29/05/2018
; Made By Lee Chatt @ "DWARFWARE"
; ---------------------------------------------------------------------

openscreen 800,600,16,2
Cls RGB(0,0,0)

scalex=2.8
scaley=2.6
width=256
height=192
CreateFXImageEx 1,width,height,16

do

Xpos=13
Ypos=10
lockbuffer

fillimage(1)

Scalex=float (800/getimageWidth(1))
Scaley=float (600/getimageHeight(1))

lockbuffer

drawrotatedimage 1,xpos,ypos,0,scalex,scaley,0,0,false

xx=xpos+30
yy=ypos+15

Text Xx,Yy, " Address:"+Str$(GetImagePtr(1))
Text Xx,Yy+20," Pitch:"+Str$(GetImagePitch(1))
Text Xx,Yy+40," Depth:"+Str$(GetImageDepth(1))
text xx,yy+60," Fps:" +str$( FPS())

unlockbuffer
Sync
loop

WaitKey


Function FillImage(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

For xlp=0 To imgwidth-1

pokeword Address+(ylp*pitch)+(xlp*2), rgb(rnd(255),rnd(255),rnd(255))

Next

Next
UnLockBuffer
RenderToScreen

EndFunction

 

Still tearing on the edges thou?

FINALLY Tonight ive added a ZX Spectrum ink index and got 94 FPS! on the Fly 49152 pixels! However.
Distortion on the edges and colour is off now!?

I Know it's something to do with rgb to index??? and surfaces with different pitches! But my head hurts now!

All i need to todo now is tidy up the edges sort colour and revisit My Emulator.

94 FPS Thou!  :o

Any MODS Greatly appriciated.

Example Code:
PlayBASIC Code: [Select]
; PROJECT : 2dDirectBufferWriting2_PLUS_inkindex
; CREATED : 29/05/2018
; EDITED : 29/05/2018
; ---------------------------------------------------------------------

; Made By Lee Chatt @ "DWARFWARE"
; ---------------------------------------------------------------------

openscreen 800,600,16,2
Cls RGB(0,0,0)

dim inkindex(15)

scalex=2.8
scaley=2.6
width=256
height=192
CreateFXImageEx 1,width,height,16

InitColorArrays()

do

Xpos=13
Ypos=10
lockbuffer

fillimage(1)

Scalex=float (800/getimageWidth(1))
Scaley=float (600/getimageHeight(1))

lockbuffer

drawrotatedimage 1,xpos,ypos,0,scalex,scaley,0,0,false

xx=xpos+30
yy=ypos+15

Text Xx,Yy, " Address:"+Str$(GetImagePtr(1))
Text Xx,Yy+20," Pitch:"+Str$(GetImagePitch(1))
Text Xx,Yy+40," Depth:"+Str$(GetImageDepth(1))
text xx,yy+60," Fps:" +str$( FPS())

unlockbuffer
Sync
loop

WaitKey


Function FillImage(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

For xlp=0 To imgwidth-1

pokeword Address+(ylp*pitch)+(xlp*2),inkindex(rnd(15))

Next

Next
UnLockBuffer
RenderToScreen

EndFunction

PSub InitColorArrays()

// Ive set the colors up here in RGB but using the palletindex(0)
// We have standard colors so we can make our own pallets!
// select color

// Standard Spectrum colors

// Normal
inkindex(0) = rgb(0, 0, 0); Black
inkindex(1) = rgb(0,0,192); Blue
inkindex(2) = rgb(192,0,0); Red
inkindex(3) = rgb(192,0,192); Magenta
inkindex(4) = rgb(0,192,0); Green
inkindex(5) = rgb(0,192,192); Cyan
inkindex(6) = rgb(192,192,0); Yellow
inkindex(7) = rgb(192,192,192); White

// Bright
inkindex(8) = rgb(0, 0, 0); Black
inkindex(9) = rgb(0,0,255); Blue
inkindex(10) = rgb(255,0,0); Red
inkindex(11) = rgb(255,0,255); Magenta
inkindex(12) = rgb(0,255,0); Green
inkindex(13) = rgb(0,255,255); Cyan
inkindex(14) = rgb(255,255,0); Yellow
inkindex(15) = rgb(255,255,255); White

EndpSub




Now all i need is to be able to use that Dll tool thingy! Maybe this will go faster?? ::)

Kind Regards Baggey
Jesus was only famous because of his dad

baggey

#1
So Tonight been reading Playbasic Reference Help etc and managed to sort out the colour problem!

Still running at a respectable 94 FPS Here!

Example Code:
PlayBASIC Code: [Select]
; ---------------------------------------------------------------------
; PROJECT : 2dDirectBufferWriting2_PLUS_inkindex
; CREATED : 31/05/2018
; EDITED : 31/05/2018
; ---------------------------------------------------------------------

; Made By Lee Chatt @ "DWARFWARE"
; ---------------------------------------------------------------------

openscreen 800,600,16,2
Cls RGB(0,0,0)

dim inkindex(15)

scalex=2.8
scaley=2.6
width=256
height=192
CreateFXImageEx 1,width,height,16

InitColorArrays()

do

Xpos=13
Ypos=10
lockbuffer

fillimage(1)

Scalex=float (800/getimageWidth(1))
Scaley=float (600/getimageHeight(1))

lockbuffer

drawrotatedimage 1,xpos,ypos,0,scalex,scaley,0,0,false

xx=xpos+30
yy=ypos+15

Text Xx,Yy, " Address:"+Str$(GetImagePtr(1))
Text Xx,Yy+20," Pitch:"+Str$(GetImagePitch(1))
Text Xx,Yy+40," Depth:"+Str$(GetImageDepth(1))
text xx,yy+60," Fps:" +str$( FPS())

unlockbuffer
Sync
loop

WaitKey


Function FillImage(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

For xlp=0 To imgwidth-1

pokeword Address+(ylp*pitch)+(xlp*2),inkindex(rnd(15))

Next

Next
UnLockBuffer
RenderToScreen

EndFunction

PSub InitColorArrays()

// Ive set the colors up here in RGB but using the palletindex(0)
// We have standard colors so we can make our own pallets!
// select color

// Standard Spectrum colors

// Normal
inkindex(0) = RGB24ToRGB16(rgb(0, 0, 0)); Black
inkindex(1) = RGB24ToRGB16(rgb(0,0,192)); Blue
inkindex(2) = RGB24ToRGB16(rgb(192,0,0)); Red
inkindex(3) =RGB24ToRGB16(rgb(192,0,192)); Magenta
inkindex(4) = RGB24ToRGB16(rgb(0,192,0)); Green
inkindex(5) = RGB24ToRGB16(rgb(0,192,192)); Cyan
inkindex(6) = RGB24ToRGB16(rgb(192,192,0)); Yellow
inkindex(7) = RGB24ToRGB16(rgb(192,192,192)); White

// Bright
inkindex(8) =RGB24ToRGB16( rgb(0, 0, 0)); Black
inkindex(9) = RGB24ToRGB16(rgb(0,0,255)); Blue
inkindex(10) = RGB24ToRGB16(rgb(255,0,0)); Red
inkindex(11) = RGB24ToRGB16(rgb(255,0,255)); Magenta
inkindex(12) = RGB24ToRGB16(rgb(0,255,0)); Green
inkindex(13) = RGB24ToRGB16(rgb(0,255,255)); Cyan
inkindex(14) = RGB24ToRGB16(rgb(255,255,0)); Yellow
inkindex(15) = RGB24ToRGB16(rgb(255,255,255)); White

EndpSub





Hit a wall with the distortion / Tearing of image edges! I have a gut feeling it's something to do with scalling being a Floating point and not a nice round interger?

Anybody got the RGB pallette values for the C64? If not ill do a search on another night.
Jesus was only famous because of his dad

kevin

#2

PlayBASIC Code: [Select]
openscreen 800,600,16,2
Cls RGB(0,0,0)

dim inkindex(15)

scalex=2.8
scaley=2.6
width=256
height=192
CreateFXImageEx 1,width,height,16

InitColorArrays()

do

Xpos=13
Ypos=10
lockbuffer

Name$="fillimageV"+digits$(FillImageIndex+1,3)
if FunctionExist(Name$)
callfunction Name$,1
endif

Scalex=float (800/getimageWidth(1))
Scaley=float (600/getimageHeight(1))


drawrotatedimage 1,xpos,ypos,0,scalex,scaley,0,0,false

unlockbuffer

xx=xpos+30
yy=ypos+15

Text Xx,Yy, " Address:"+Str$(GetImagePtr(1))
Text Xx,Yy+20," Pitch:"+Str$(GetImagePitch(1))
Text Xx,Yy+40," Depth:"+Str$(GetImageDepth(1))
text xx,yy+60," Fps:" +str$( FPS())
text xx,yy+80," Method:" +str$( FillImageIndex)



if rightmousebutton()
FillImageIndex=(FillImageIndex+1)&3
flushmouse
endif

Sync
loop

WaitKey


Function FillImageV001(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

For xlp=0 To imgwidth-1

pokeword Address+(ylp*pitch)+(xlp*2),inkindex(rnd(15))

Next

Next
UnLockBuffer
RenderToScreen

EndFunction




Function FillImageV002(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

RowAddress = Address+ ylp*Pitch
For xlp=0 To imgwidth-1
pokeword RowAddress+(xlp*2),inkindex(rnd(15))
Next

Next
UnLockBuffer
RenderToScreen

EndFunction




Function FillImageV003(ThisImage)

RenderToImage ThisImage

LockBuffer

Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
Depth =GetImageDepth(ThisImage)

imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

For ylp=0 To imgheight-1

RowAddress = Address+ ylp*Pitch
EndRowAddress = RowAddress + (imgwidth-1) *2
For PixelAddress=RowAddress To EndRowAddress step 2
pokeword PixelAddress,inkindex(rnd(15))
Next

Next
UnLockBuffer
RenderToScreen

EndFunction





Function FillImageV004(ThisImage)

RenderToImage ThisImage
Login required to view complete source code



(login required)

PlayBASIC Code: [Select]
$RGBCOLOR0=000000
$RGBCOLOR1=ffffff
$RGBCOLOR2=882000
$RGBCOLOR3=68d0a8
$RGBCOLOR4=a838a0
$RGBCOLOR5=50b818
$RGBCOLOR6=181090
$RGBCOLOR7=f0e858
$RGBCOLOR8=a04800
$RGBCOLOR9=472b1b
$RGBCOLOR10=c87870
$RGBCOLOR11=484848
$RGBCOLOR12=808080
$RGBCOLOR13=98ff98
$RGBCOLOR14=5090d0
$RGBCOLOR15=b8b8b8



stevmjon

i am also researching memory access at the moment.
i want to do a similar thing in my 3D engine with the z buffer.

i noticed that in this demo, in the main do - loop there is lockbuffer & unlockbuffer, and at the same time in the functions there also another lockbuffer & unlockbuffer.
it seems the first lockbuffer is the screen, and the second lockbuffer is the image.
can you have 2 buffers locked independantly, or does command unlockbuffer unlock all of them?

  stevmjon


It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

baggey

#4
WoW 135 FPS here for method 4!  :o

So having read the code has kinda got me thinking. My emulator writes a line by line which is what we have here. But the Zx Spectrum has a very strange layout indeed.
I see the last line is commented out?

Ive played with it and see that 508/2=254 Pixels. What always confuses me is the 0 counts as well so this would then be +2 giving 255 Pixels.

In my emulator i used a 255 Byte Look up pixel table as only Paper and Ink can be Displayed. Im thinking now of Using this method with 32 Look ups per Line Scan.

Will make one Big Speed difference! Its the Dreaded Z80 core that put a stop on it for me as the sheer amount of time was Exponetial.

Ive seen Many cores rewritten in C,Delphi and Java Since! Im thinking of writing a program to strip the code out of a Free Z80 core, into a usable layout for my PlayBasic Emulator.
Hopefully with sucess in getting the "Flags Register" to work! That seems the crutial part for Logic to work.

I see PlayBasic has come a long way  ;D It's my favfrouite Language. Apart from Zx Spectrum basic of course. Which was very slow!

Id even like to write a program that emulates just the Basic running at "80's Speed Machine Code" that way we could write retro games for our old computer's in Basic. As if it were running on the same machine.  I wrote a few games back in the day more than 6 things happening on the screen killed It! "Speed wise i mean".


Shall have a go at the C64 Screen 320 x 200 latter. Could setup a Mode 0 and a Mode 1 depending which Screen Mode or Machine is reqired! From an Emulation point of view.


Sorry if a little off topic!

Kind Regards Baggey
Jesus was only famous because of his dad

kevin



PlayBASIC Code: [Select]
 dim inkindex(15)

scalex=2.8
scaley=2.6
width=256
height=192
CreateFXImageEx 1,width,height,32

InitColorArrays()

FillImageIndex=4

do

Xpos=13
Ypos=10



lockbuffer

Name$="fillimageV"+digits$(FillImageIndex+1,3)
if FunctionExist(Name$)
callfunction Name$,1
endif

Scalex=float (800/getimageWidth(1))
Scaley=float (600/getimageHeight(1))


drawrotatedimage 1,xpos,ypos,0,scalex,scaley,0,0,false

unlockbuffer

xx=xpos+30
yy=ypos+15

Text Xx,Yy, " Address:"+Str$(GetImagePtr(1))
Text Xx,Yy+20," Pitch:"+Str$(GetImagePitch(1))
Text Xx,Yy+40," Depth:"+Str$(GetImageDepth(1))
text xx,yy+60," Fps:" +str$( FPS())
text xx,yy+80," Method:" +str$( FillImageIndex)


if mousebutton()
FillImageIndex=cliprange(FillImageIndex+1,4,5)
flushmouse
endif


Sync
loop

WaitKey





Type tRow
Row(255)
EndType



Function FillImageV005(ThisImage)

RenderToImage ThisImage

LockBuffer

Depth =GetImageDepth(ThisImage)
if depth = 32
Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)


dim Row as tRow pointer

For ylp=0 To imgheight-1


// Compute what 32 pixel chunk
for xlp=0 to imgwidth-1 step 32

Row = (Address+ ylp*Pitch) +(Xlp*4)


row.Row(0)=inkindex(rnd(15))
row.Row(0)=inkindex(rnd(15))
row.Row(1)=inkindex(rnd(15))
row.Row(2)=inkindex(rnd(15))
row.Row(3)=inkindex(rnd(15))
row.Row(4)=inkindex(rnd(15))
row.Row(5)=inkindex(rnd(15))
row.Row(6)=inkindex(rnd(15))
row.Row(7)=inkindex(rnd(15))
row.Row(8)=inkindex(rnd(15))
row.Row(9)=inkindex(rnd(15))
row.Row(10)=inkindex(rnd(15))
row.Row(11)=inkindex(rnd(15))
row.Row(12)=inkindex(rnd(15))
row.Row(13)=inkindex(rnd(15))
row.Row(14)=inkindex(rnd(15))
row.Row(15)=inkindex(rnd(15))
row.Row(16)=inkindex(rnd(15))
row.Row(17)=inkindex(rnd(15))
row.Row(18)=inkindex(rnd(15))
row.Row(19)=inkindex(rnd(15))
row.Row(20)=inkindex(rnd(15))
row.Row(21)=inkindex(rnd(15))
row.Row(22)=inkindex(rnd(15))
row.Row(23)=inkindex(rnd(15))
row.Row(24)=inkindex(rnd(15))
row.Row(25)=inkindex(rnd(15))
row.Row(26)=inkindex(rnd(15))
row.Row(27)=inkindex(rnd(15))
row.Row(28)=inkindex(rnd(15))
row.Row(29)=inkindex(rnd(15))
row.Row(30)=inkindex(rnd(15))
row.Row(31)=inkindex(rnd(15))

next

Next
endif

UnLockBuffer
RenderToScreen

EndFunction


Function FillImageV006(ThisImage)

RenderToImage ThisImage

LockBuffer

Depth =GetImageDepth(ThisImage)
if depth = 32
Address =GetImagePtr(ThisImage)
Pitch =GetImagePitch(ThisIMage)
imgheight=getimageheight(thisimage)
imgwidth=getimagewidth(thisimage)

Login required to view complete source code

kevin

#6
Quote
t seems the first lockbuffer is the screen, and the second lockbuffer is the image.
can you have 2 buffers locked independantly, or does command unlockbuffer unlock all of them?


Lock/Unlock relate to only the current surface, but you can get way with this on FX/AFX surfaces but I suspect it would run into problems with video surfaces.  



Quote

Id even like to write a program that emulates just the Basic running at "80's Speed Machine Code" that way we could write retro games for our old computer's in Basic. As if it were running on the same machine.  I wrote a few games back in the day more than 6 things happening on the screen killed It! "Speed wise i mean".


  most 8bit systems only produce around 1 million cycles per second.   At 50fps that's 20K cycles per frame which isn't much espoicially when opcodes tend to execute on average in 2->4 cycles.