OpenGL v DirectX

BlitzMax Forums/BlitzMax Programming/OpenGL v DirectX

Which one is faster for you, run this code and tell me your FPS for each of the Tests:
' //////////////////////////////
' // OpenGL V DirectX
' ______________________________
Global mapWidth% = 100
Global mapHeight% = 100

Global myMap:Int[,] = New Int[mapWidth, mapHeight]

Global frameCount%, frameTimer%, fps%

Global testImage:TImage

' TESTING RECT DRAWING
' ____________________

' First test DirectX
SetGraphicsDriver(D3D7Max2DDriver())
Graphics 1024, 768, 32, -1

While Not KeyHit(KEY_SPACE)
	Cls
	
	SetColor 0, 100, 0
	For x% = 0 To mapWidth - 1
		For y% = 0 To mapHeight - 1
			DrawRect x * 6, y * 6, 5, 5
		Next
	Next
	
	frameCount:+ 1
	
	If frameTimer < MilliSecs()
		fps = frameCount
		frameTimer = 1000 + MilliSecs()
		frameCount = 0
	EndIf
	
	SetColor 255, 255, 255
	DrawText "FPS By DirectX = " + fps, 10, 10
	DrawText "Press spacebar to test OpenGL", (GraphicsWidth() / 2) - (TextWidth("Press spacebar to test OpenGL") / 2), 750
	
	Flip

        if keyhit(KEY_ESCAPE) then end
Wend

EndGraphics()

' Second test OpenGL
frameCount = 0

SetGraphicsDriver(GLMax2DDriver())
Graphics 1024, 768, 32, -1

While Not KeyHit(KEY_SPACE)
	Cls
	
	SetColor 0, 100, 0
	For x% = 0 To mapWidth - 1
		For y% = 0 To mapHeight - 1
			DrawRect x * 6, y * 6, 5, 5
		Next
	Next
	
	frameCount:+ 1
	
	If frameTimer < MilliSecs()
		fps = frameCount
		frameTimer = 1000 + MilliSecs()
		frameCount = 0
	EndIf
	
	SetColor 255, 255, 255
	DrawText "FPS By OpenGL = " + fps, 10, 10
	DrawText "Press spacebar to test DirectX image drawing", (GraphicsWidth() / 2) - (TextWidth("Press spacebar to test DirectX image drawing") / 2), 750
	
	Flip

        if keyhit(KEY_ESCAPE) then end
Wend

EndGraphics()

' TESTING IMAGE DRAWING
' _____________________

' First test DirectX
frameCount = 0

SetGraphicsDriver(D3D7Max2DDriver())
Graphics 1024, 768, 32, -1

testImage = CreateImage(5, 5)
SetColor 0, 100, 0
DrawRect 0, 0, 10, 10
GrabImage(testImage, 0, 0)

SetColor 255, 255, 255
While Not KeyHit(KEY_SPACE)
	Cls
	For x% = 0 To mapWidth - 1
		For y% = 0 To mapHeight - 1
			DrawImage testImage , x * 6, y * 6
		Next
	Next
	
	frameCount:+ 1
	
	If frameTimer < MilliSecs()
		fps = frameCount
		frameTimer = 1000 + MilliSecs()
		frameCount = 0
	EndIf
	
	DrawText "FPS By DirectX = " + fps, 10, 10
	DrawText "Press spacebar to test OpenGL", (GraphicsWidth() / 2) - (TextWidth("Press spacebar to test OpenGL") / 2), 750
	
	Flip

        if keyhit(KEY_ESCAPE) then end
Wend

EndGraphics()

' Second test OpenGL
frameCount = 0

SetGraphicsDriver(GLMax2DDriver())
Graphics 1024, 768, 32, -1

testImage = CreateImage(5, 5)
SetColor 0, 100, 0
DrawRect 0, 0, 10, 10
GrabImage(testImage, 0, 0)

SetColor 255, 255, 255
While Not KeyHit(KEY_SPACE)
	Cls
	For x% = 0 To mapWidth - 1
		For y% = 0 To mapHeight - 1
			DrawImage testImage , x * 6, y * 6
		Next
	Next
	
	frameCount:+ 1
	
	If frameTimer < MilliSecs()
		fps = frameCount
		frameTimer = 1000 + MilliSecs()
		frameCount = 0
	EndIf
	
	DrawText "FPS By OpenGL = " + fps, 10, 10
	DrawText "Press spacebar to End", (GraphicsWidth() / 2) - (TextWidth("Press spacebar to End") / 2), 750
	
	Flip

        if keyhit(KEY_ESCAPE) then end
Wend

End


The test are as follows:
Test1 - Tests shape drawing
Test2 - Tests image drawing


my results are:
TEST 1:
DirectX = 41
OpenGL = 102

TEST 2:
DirectX = 41
OpenGL = 62


EDIT: I should say I have a really crapy Graphics Card (sapphire ATI 9600)

I ask because I want to know which one is fastest on a rage of different computers.

BTW if you find anything in the code that looks like it would make it an unfair test, can you please post the fixed code. Thx


Debug

Test 1
DX = 49
GL = 211

Test 2
DX = 44
GL = 123

Non Debug

Test 1
DX = 56
GL = 408

Test 2
DX = 55
GL = 298


Win2K SP4, 2600+, Radeon 9800 Pro, 512 RAM

My results. First in debug mode:

TEST 1:
DirectX = 33
OpenGL = 151

TEST 2:
DirectX = 33
OpenGL = 118

Now, not in debug:

TEST 1:
DirectX = 36
OpenGL = 366

TEST 2:
DirectX = 39
OpenGL = 275

This is with a P4 2.6GHz, 1GB RAM, NVidia GeForce 6800 (Detonator 77.72 drivers)

Muttley

Test 1
DX = 50
GL = 500

Test 2
DX = 50
GL = 380

Test 1
DX = 43
GL = 166

Test 2
DX = 37
GL = 94

WinXP SP2, 2700+, 9800Pro, 1Gig RAM

There's summat up with these tests - this can't be right!
Test 1
DX = 52
GL = 612

Test 2
DX = 52
GL = 420

(running on my winxp box)

woot! I trounced both muttley's 6800 and tonyg's 9800Pro. :D

There's summat up with these tests - this can't be right!


mabey its my coding, I wrote the test while half asleep, but i noticed these huge differences while develop my current project so I thought I'd try and create a test - so far it seems OpenGL is faaaaaaaar faster then DX, which is the reult I got in my project.

if you find somthing wrong with the code please post an update.

On Athlon XP 3000+, Radeon 9600 Pro, Win XP Pro SP2, BlitzMax 1.12, Non-Debug:

Test 1
DX = 54
GL = 496

Test 2
DX = 52
GL = 363

Perhaps you're forgetting flip false?

I tried it with flip 0 (necessary in 1.12) and I agree, even on the ati card, ogl is always twice as fast or more.

That is very strange.

I'm getting 4x the speed on average with OpenGL... what is max doing that is so slow with DX?

Curious, I tried a few things, like using Strict with loop locals, using a function instead of inline code, etc. and couldn't get a significant change.

Changing from DrawRect to DrawOval in Test 1 makes a big change, though. I went from DX = 54 / GL = 496 to:

Test 1
DX = 20
GL = 40

So it looks like GL's DrawRect might be much faster than DX's, while its DrawOval is less so.

Using ovals makes no difference with Test2 since it's DrawImage that counts. In Blitz3D, there's "DrawBlock" which is faster than its DrawImage due to no masking, but turning off masking here didn't help. I tried using DrawImageRect but there was no significant change (both DX and GL were a tad slower).

Yep Just did a test with 16*16 bitmaps

First Test:
Graphics set to 1280*1024*32*0
Drawing 5120 bitmaps to screen
Then Flip False

DirectX Result = 67Fps
Opengl Result=200Fps

SecondTest:
Graphics set to 1280*1024*32*0
Setblend(ALPHABLEND)
Drawing 5120 bitmaps to screen
SetBlend(SHADEBLEND)
Drawing 5120 bitmaps to screen
Then Flip False

DirectX Result = 34Fps
Opengl Result=100Fps


And Results using Diablos Test

Test 1
Directx =56
Opengl=600-602

Test 2
Directx =53
Opengl=405-408

Test 1
65
705

Test 2
66
503

Exact same results 1024x768 and 1600x1200
.

test1
45
410

test2
40
361

WinXp, Geforce 6800 GT

What's going on here? Everyone's Dx seems to be
consistently 5-10x slower than OGL. Is that because
Mark favours OGL/Mac and therefore has
optimized/understood it better when writing?

It's probably just an easier API to understand in the first place. But those results are disturbing.

Here's my results:

Test 1 DX: 59
Test 1 GL: 569
Test 2 DX: 58
Test 2 DX: 411

Looks like OpenGL kicks ass! But as Toby says the results are pretty scary. Anyone tested on Max 1.10?

this can't be right!
The reason is, quite simply, because OpenGL is faster (and thus better) than DirectX.

A difference as extreme as shown here points to a problem somewhere with nothing to do with DX or OGL, though.

I tested on max 110

The DirectX results seem to be withing 10 fps of each other on different machine specs while the OpenGL results have a huge variation. Perhaps the DX frame rate is being limited/manipulated somehow or its the result of using an outdated API?

I get about the same results.

But I didn't expect anything else to tell the truth, because DX is used in a "unmeant" way within Max2D (making it work like OpenGL which BM Max2D commands focus on while DX is meant and optimized to work quite different) ...

would be like programming procedural with a mainloop within a event based environment ...

You should, at least at the moment, only use DX as fall back ...
Thats the price of the procedural "1 look & feel" implementation of Max2D

I tried it with flip 0 (necessary in 1.12) and I agree, even on the ati card, ogl is always twice as fast or more.

Hu, I didn't even now about this - [EDIT that is flip 0 :P].

Looks like your right Dreamora, openGL is the way to go in blitzMax, thx everyone for testing :)

..EDIT: I just got some screen shots together of my current project and its kinda worring considering the above:

This first on is DirectX drawing a bunch of images (including the HUD):

FPS = 157

This second one is DirectX trying to draw rects(minimap) and images:

FPS = 30

Now openGL rendering images:

FPS = 134 - [ THATS SLOWER THEN DIRECTX ? ]

And openGL rendering rects:

FPS = 70 [FASTER THEN DIRECTX]

This is the first reason why I created these test :P

This whole thing is just freeking me out.

DX: 52
OGL: 454

DX: 49
OGL: 300

I think it might have something to do with how DX is being set up or something. I believe when it's fixed (this should be considered a minor bug) it should be within 50% of GL speed.

Perhaps someone would like to dig deeply in the modules to see where the problem lies. I'm just not experienced enough. Or post in bug reports. I gave it a look but it doesn't seem apparent to me.

edit: confirmed the speed gap in own game.

Try knocking up a dx.exe gl.exe for us to test and see if it your graphics card that causes the slower opengl in your screenies

Is it as simple as the DX driver being forced to wait for a vblank somewhere? Not convinced, as the numbers posted for DX (and certainly for me) aren't entirely consistent with normal refresh rates...

@Duckstab
OK, I have just uploaded a zip containg a two binarys, DX.exe & GL.exe. Get them here (while you still can) GLvDX_EngineTest.zip

Click the 'N' icon to change the view to minimap(DrawRect) & the target one to change to the ISO view(DrawImage).

Just tested the Exes

DirectX Results
Drawimage 322 fps
DrawRect 35 Fps

Opengl Results
Drawimage 632 Fps "note This Had Glitches what bit is it set to 16 or 32
DrawRect 245 Fps

Its set to 16 bit but if ur getting problems (and if anyone else is) I will put this upto 32bit.

Does OpenGl utilise hardware T&L if it's present? Does OGL automatically cache vertices in VidMem? Does OGL automatically use shared vertices and buffers?

If you implement Hardware T&L for DirectX and use my VertexBuffer code then there is little difference between the speeds. Granted it's not standard BMax stuff and it's not automatic but it helps.

I do not think OpenGL does T&L. I think there is an underlying major problem with DX at the moment.

None of this things is done in VRAM in the OpenGL 1.2 implementation of BM.
The main difference is that BM D3D gives DX the control over where to put the texture while OpenGL forces the VRAM for it ...

But as said, DX works differently ... switching states xy times per frame or recreating the same stupid quad over and over again instead of repositioning it is not really what D3D was made for ...
It does not have its native model format for fun ;-)

I think that it will be as fast as OGL, or within 10% of the speed when it is fixed.

It should be faster than OpenGL on Windows Machines. Guarenteed its done right.

No, OGL has always run faster on my machine than DX. But DX should not be so far behind as this.

Test 1:
DX=70
OpenGL=622

Test 2:
DX=64
OpenGL=420