Faster Screen Clearing?

BlitzMax Forums/BlitzMax Beginners Area/Faster Screen Clearing?

MGE(Posted 2007) [#1]
I might be up too long coding.....again...but I keep getting wildly faster times clearing the screen using DrawRect() instead of Cls(). If there's a mistake in my code causing inaccurate results, I apologize. But I've looked at this for an hour before uploading it and it appears fine to me? It will print out the results in the output section of the ide. DrawRect() is first, followed by Cls(). Seems faster regardless of g driver used. Could someone please test this on your end? Thanks!

SuperStrict
Global start_time:Int
Global end_time:Int
Global l:Int
'
HideMouse()
'
SetGraphicsDriver D3D7Max2DDriver()
'SetGraphicsDriver GLMax2DDriver()
Graphics(800,600,32)
'
'
' Test 1: using DrawRect() to clear the screen
'
'
start_time=MilliSecs()
For l=1 To 2000
 SetColor(0,0,0)
 SetAlpha(1)
 SetScale(1,1)
 SetRotation(0)
 SetBlend(SOLIDBLEND)
 DrawRect(0,0,800,600)
Next
end_time=MilliSecs()
Print end_time-start_time
'
'
' Test 2: using Cls() to clear the screen
'
'
start_time=MilliSecs()
SetClsColor(0,0,0)
For l=1 To 2000
 Cls()
Next
end_time=MilliSecs()
Print end_time-start_time
'
ShowMouse()



mothmanbr(Posted 2007) [#2]
I also got a WAY better time using the drawrect... I'll change it in my game and see how that affects the FPS, if it does.


mothmanbr(Posted 2007) [#3]
If anything, the FPS had a minor loss when the screen is full of objects. With CLS it never went under 65, with the drawrect it reached 60. I tested each one a couple of times, but obviously don't rely on my tests too much, cuz my "screen full of objects" thing means "shooting in every direction at once" and it's never the same from one run to the other.


MGE(Posted 2007) [#4]
Hi moth, thanks for checking it out. If you're rendering a bunch of objects you won't see a difference probably because the overhead is already there with all of the objects being drawn. But as far as drawrect() vs cls() in an isolated test, it seems like drawrect() is indeed alot faster! Every cycle gained helps. ;)


Grey Alien(Posted 2007) [#5]
Perhaps CLS is really called 2000 times and the 3D card figures out that DrawRect covers the same area and doesn't duplicate?


MGE(Posted 2007) [#6]
Even with random drawing and page flipping, it still seems faster to use DrawRect() instead of Cls(). ?
SuperStrict
Global start_time:Int
Global end_time:Int
Global l:Int
'
HideMouse()
'
SetGraphicsDriver D3D7Max2DDriver()
'SetGraphicsDriver GLMax2DDriver()
Graphics(800,600,32)
'
'
' Test 1: using DrawRect() to clear the screen
'
'
start_time=MilliSecs()
For l=1 To 2000
 SetColor(0,0,0)
 SetAlpha(1)
 SetScale(1,1)
 SetRotation(0)
 SetBlend(SOLIDBLEND)
 DrawRect(0,0,800,600)
 drawbox()
 Flip(0)
Next
end_time=MilliSecs()
Print end_time-start_time
'
'
' Test 2: using Cls() to clear the screen
'
'
start_time=MilliSecs()
SetClsColor(0,0,0)
For l=1 To 2000
 Cls()
 drawbox()
 Flip(0)
Next
end_time=MilliSecs()
Print end_time-start_time
'
ShowMouse()
'
Function drawbox()
 SetColor(Rand(255),Rand(255),Rand(255))
 SetAlpha(1)
 SetScale(1,1)
 SetRotation(0)
 SetBlend(SOLIDBLEND)
 DrawRect Rand(600),Rand(400),200,200
End Function



dmaz(Posted 2007) [#7]
Cls() is always faster on my machine... your first example had cls 5 times faster in opengl. With this last example I get about 30% faster with Cls from both opengl and directx


you sure do like the parenthesizes! ;)


Perturbatio(Posted 2007) [#8]
Cls is faster for me too, I got:
557
225

on that code for D3D and:

358
323

for OGL


MGE(Posted 2007) [#9]
"you sure do like the parenthesizes! ;)" !#$%^ Based on comments here in the forum, that's the "Blitzmax" preffered way of coding. :) lol...

hmm.. so Cls is faster on your puters. Well, shoot I thought I was on to to something. I hear the regulars shouting "noob, noob, kill the noob!" lol...


ImaginaryHuman(Posted 2007) [#10]
Grey - the graphics card is not intelligent to figure out such optimizations, it just does what you tell it.

Cls is supposed to be optimized on the card but depending on the mfg of the card they might implement it differently. It's not predictable.


xlsior(Posted 2007) [#11]
Weird.

The first piece of code, in Direct3D:
35
477

OGL:
1
2
(?????)

and the second piece of code gave me this in D3D
447
425

and this in OpenGL:
785
771

so... Pretty much all over the place. :-?

Core 2 Duo E6600, Radeon X1650XT


TaskMaster(Posted 2007) [#12]
You are not Flipping in the first example. I wonder if Flipping would have any impact on one or the other?

Flipping a screen that has been CLS'ed might be faster than Flipping one that has been DrawRECT'ed.


TaskMaster(Posted 2007) [#13]
Flipping had an effect, but it does not change the fcat that drawrect was faster than cls on my machine as well.

Strange.

Someone should track the CLS command in the source and see what it does...


MGE(Posted 2007) [#14]
DX7:
device.Clear 1,vp_rect,D3DCLEAR_TARGET,clscolor,0,0

OpenGL:
glClear GL_COLOR_BUFFER_BIT

This is obviously a GPU/Driver related phenomenon if DrawRect() is faster or slower compared to Cls(). This really isn't going to change your fps in a real world scenario, especially if you're already drawing a bunch of objects. But, in a situation where every cycle counts, you may want to add a "performance test" section to your game options and auto configure your render loop based on the results.


dmaz(Posted 2007) [#15]
"you sure do like the parenthesizes! ;)" !#$%^ Based on comments here in the forum, that's the "Blitzmax" preffered way of coding. :) lol...


Based on the comments, really...I hadn't really noticed it in most code examples. for me, it's just too distracting. I only use them I need the returned data and in some cases where it helps readability. but that's me. :)


Dreamora(Posted 2007) [#16]
Just as a sidenote:
If you have a tilemap in the background, you wouldn't need CLS at all or if you have a background image ...

Quite some user tends to forget about that and clears a screen thats overdrawn anyway ...


Grey Alien(Posted 2007) [#17]
Grey - the graphics card is not intelligent to figure out such optimizations, it just does what you tell it.
Oh OK. Shame.


TaskMaster(Posted 2007) [#18]
Figured I would check this out again.

So, when I first learned BlitzMax a couple months ago I write an Asteroids game. WHen the game first starts up, it creates 8 asteroids that float around the screen with the words "Game Over" in the middle of the screen.

I can start and stop the game many times over and get 375 (plus or minus 1) FPS every single time.

Then I removed the CLS command and put the DrawRect stuff in, and I get 347 FPS. So, even though the sample code at the top of this thread is faster with DrawRect on my PC (here at work), in use in an actual program I wrote, the DrawRect stuff is slower.

Another funny thing:

Run as is, I get something like:

4
2191

or

7
2193

But, if I move the CLS portion first, then I get:

1036
125

or

1038
126

This results is very strange. Even though the DrawRect is still faster, it is 15 times slower than when it is first and the CLS is twice as fast as when it is second. WTF?!?!

So, I am inclined to believe that the CLS is faster (since it was faster in my app) and in the sample code, something funny is going on.

Another funny thing, when I siwtch to OpenGL, I see:

3
1973

but if I switch the order and put CLS first, I get:

9
2

Something very fishy going on here.


ImaginaryHuman(Posted 2007) [#19]
Under the hood there are probably some differences between what parts of the graphics pipeline are needed for drawing a rect and for doing a clearscreen. When you do a clearscreen it probably only needs to know to write the clear-screen-color into all pixels in the backbuffer. It probably does not deal with the geometry part of the pipeline, generating fragments and all that. Then when you do your Rect, it has to process the pixels in the rect as geometry and convert it to fragments and then apply the color and do whatever method of interpolation is defined in the `shade model` (smooth shaded or flat shaded etc) and think about a number of other factors depending on which features are switched on, including detecting if blending is operational and various tests etc. Then it spits out the pixels. Presumably there is some sharing going on of the pipeline and the cls is using part of the pipeling causing the rect to stall a bit if called after it, while the other way around perhaps becomes more efficient based on which parts of the pipeline can run concurrently or whatever.


amonite(Posted 2007) [#20]
Hi,

DX debug ON
2808
1951

DX debug OFF
2218
2004

OGL debug ON
2024
1111

OGL debug OFF
2890
1107



CLS faster here.
OpenGL appears to be faster with debug build ??


dmaz(Posted 2007) [#21]
This results is very strange. Even though the DrawRect is still faster, it is 15 times slower than when it is first and the CLS is twice as fast as when it is second.
the one thing I didn't notice before was the prints.... when benchmarking the time should always be saved and printed for all the tests at the end. do not test then print then test then print. we had this issue a while back with another benchmark.


Grey Alien(Posted 2007) [#22]
yep that's absolutely true, print can screen up tests. It screwed some tests I was making for jitter correct, but then I was doing a print every frame...