Slowdown with read/writepixelfast..

BlitzPlus Forums/BlitzPlus Programming/Slowdown with read/writepixelfast..

ErikT(Posted 2004) [#1]
For my game I wrote this function which is called every time a portion of a background image is loaded. It takes a background image, does it's thing and spit out a coloured version of the same image. I'm real proud of it :D I feel it proves once again that *anyone* can learn to code, even a guy who's practically retarded when it comes to math (like I am). One problem though: whenever I draw the modified images, fps are cut in half! But the read/writepixel operations are already done with when I draw them. The modified images shouldn't draw any slower than ordinary ones, right?

I posted the function below if anyone wants to take a look at it and see if there's anything wrong.

[CODE]
Function Scenery_Light(f,r_value3D,g_value3D,b_value3D)

Local r# = 0
Local g# = 0
Local b# = 0
Local r_value
Local g_value
Local b_value
Local R_Modifier#
Local G_Modifier#
Local B_Modifier#
Local PixelR#
Local PixelR_Modifier#
Local width = 160
Local height = 400

r_value = 255-r_value3D
g_value = 255-g_value3D
b_value = 255-b_value3D

deltax = 0
deltay = 0

LightedBackgr = CreateImageBB(width,height)

LockBufferReadingBB(Layer3[f])
LockBufferWritingBB(LightedBackgr,64000)

For x = 0 To width-1
For y = 0 To height-1
If BitDepth = 8
PixelR = ReadPixelFastBB(x,y)

PixelR_Modifier = (r_value+g_value+b_value)/3
PixelR = PixelR - (PixelR_Modifier*(PixelR/255))

If PixelR > 255
PixelR = 255
ElseIf PixelR < 0
PixelR = 0
EndIf

ElseIf BitDepth = 16
r = 8*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) Shr 11 And %11111) ;pos 11-15
g = 4*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) Shr 5 And %111111) ;pos 5-10
b = 8*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) And %11111);pos 0-4

ElseIf BitDepth = 32
rgba = PeekInt(pixelReadBank,4*x+y*ReadPixelPitch) And %111111111111111111111111

r = rgba Shr 16 And %11111111 ;pos 16-23
g = rgba Shr 8 And %11111111 ;pos 8-15
b = rgba And %11111111 ;pos 0-7
EndIf

R_Modifier = (r_value*(r/255))
G_Modifier = (g_value*(g/255))
B_Modifier = (b_value*(b/255))

r = r - R_Modifier
g = g - G_Modifier
b = b - B_Modifier

If r > 255
r = 255
ElseIf r < 0
r = 0
EndIf
If g > 255
g = 255
ElseIf g < 0
g = 0
EndIf
If b > 255
b = 255
ElseIf b < 0
b = 0
EndIf

If BitDepth = 8
WritePixelFastBB(x+deltax,y+deltay,PixelR)
Else
WritePixelFastBB(x+deltax,y+deltay,r,g,b)
EndIf
Next
Next

UnlockBufferWritingBB(LightedBackgr)
UnlockBufferWritingBB(Layer3[f])
FreeImageBB(Layer3[f])
Layer3[f] = CreateImageBB(width,height)
Layer3[f] = CopyImageBB(LightedBackgr,1)
FreeImageBB(LightedBackgr)

End Function
[/CODE]


ford escort(Posted 2004) [#2]
look at the createimage command

CreateImage( width,height[,frames,flags] )

and here the description of the flags

1 : managed (image is frequently drawn but seldom modified - this is the default)
2 : dynamic (image is frequently drawn and modified)
4 : scratch (image is seldom drawn or modified)

try to set the flag to 2 maybe this can speed up things ...


ErikT(Posted 2004) [#3]
Thanks for the heads up. Didn't make any difference, tho', so the problem's somewhere else obviously. Rrr, why won't it just behave? :-=


ford escort(Posted 2004) [#4]
it look like you're using some customized commands i don't know them

CreateImageBB

maybe the problem is there

as b+ use the new flags if this command is for bb and b3d maybe it's not implemented and b+ uses these image created as default managed that is a bit more slow than dynamic.


ErikT(Posted 2004) [#5]
I'm using the Extended Blitz lib by Turtle1776. Yep, the bug could be there too. Just thought I'd post here in case I did an obvious bummer in my own code.


Mr Brine(Posted 2004) [#6]
Ive found the 'createimage' uses a lot of processor power for what it does, I'd try creating the image before you enter your programs main loop and passing that image handle to your function instead of creating the image inside the function.

I know your using the 'createimagebb' so I dont know if this info is valid.

Mr Brine


ErikT(Posted 2004) [#7]
The whole function is run just after images are loaded and before the game starts. What stumps me is why the modified image is drawn so slow. It's already been altered before the action starts and afterwards just sits in memory like any other image.


Mr Brine(Posted 2004) [#8]
Do you use these bb... functions to draw the actual graphics in the program loop?

Are you doing applying additional effects that only these bb functions provide?


ErikT(Posted 2004) [#9]
Yep, the custom commands replace all the native ones. In the cases of Create/Load/Drawimage it should work the same, except you're allowed to put images in and draw them from system mem.