Page 1 of 5

2D drawing slow as hell

Posted: Tue Sep 20, 2011 10:45 am
by xorc1zt
purebasic 2d drawing api seem really outdated, even java who rely on a virtual machine is 4x faster

purebasic

Code: Select all

#screenwidth   = 640
#screenheight  = 480

InitSprite()
OpenWindow(0, 0, 0, #screenwidth, #screenheight, "PB Drawing", #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #screenwidth, #screenheight, 0, 0, 0, #PB_Screen_NoSynchronization)

bufferaddress.i = 0
StartDrawing(ScreenOutput())
bufferaddress = DrawingBuffer()
StopDrawing()

fpscounter.i  = 0
timer.i       = 0
fps.i = 0
frames.i = 0
totalTime.l = 0
curTime.l = ElapsedMilliseconds()
    
Repeat
  
  lastTime = curTime
  curTime = ElapsedMilliseconds()
  totalTime + (curTime - lastTime)
  If( totalTime > 1000 ) 
    totalTime - 1000
    fps = frames
    frames = 0
  EndIf
  frames + 1
        
  ClearScreen(0)
  StartDrawing(ScreenOutput())
  For i = 0 To 20
    Box(Random(320), Random(240), Random(320), Random(240), Random($FFFFFF) )
  Next i
  DrawText(20,430,Str(fps))
  StopDrawing()
  
  FlipBuffers()
  
;   If ElapsedMilliseconds() > timer+1000
;     PrintN("fps: "+Str(fpscounter))
;     fpscounter  = 0
;     timer       = ElapsedMilliseconds()
;   EndIf
;   
;   fpscounter + 1
  
  
Until WindowEvent() = #PB_Event_CloseWindow
java version

Code: Select all

import java.awt.*;
import java.awt.image.*;
import java.util.Enumeration;
import java.util.Random;
import javax.swing.JFrame;

/*
 * This is an example of a simple windowed render loop
 */
public class SimpleWindowedGame extends Canvas {

  public static void main( String[] args ) {
                
    // Create game window...
    JFrame app = new JFrame();
    app.setIgnoreRepaint( true );
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                
    // Create canvas for painting...
    Canvas canvas = new Canvas();
    canvas.setIgnoreRepaint( true );
    canvas.setSize( 640, 480 );
                
    // Add canvas to game window...
    app.add( canvas );
    app.pack();
    app.setVisible( true );
                
    // Create BackBuffer...
    canvas.createBufferStrategy( 2 );
    BufferStrategy buffer = canvas.getBufferStrategy();

    // Get graphics configuration...
    GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    // Create off-screen drawing surface
    BufferedImage bi = gc.createCompatibleImage( 640, 480 );

    // Objects needed for rendering...
    Graphics graphics = null;
    Graphics2D g2d = null;
    Color background = Color.BLACK;
    Random rand = new Random();
                
    // Variables for counting frames per seconds
    int fps = 0;
    int frames = 0;
    long totalTime = 0;
    long curTime = System.currentTimeMillis();
    long lastTime = curTime;
    //int[] pixels;
    
    while( true ) {
      try {
        // count Frames per second...
        lastTime = curTime;
        curTime = System.currentTimeMillis();
        totalTime += curTime - lastTime;
        if( totalTime > 1000 ) {
          totalTime -= 1000;
          fps = frames;
          frames = 0;
        } 
        ++frames;

        // clear back buffer...
        g2d = bi.createGraphics();
        g2d.setColor( background );
        g2d.fillRect( 0, 0, 639, 479 );
                                
        // draw some rectangles...
        for( int i = 0; i < 20; ++i ) {
          int r = rand.nextInt(256);
          int g = rand.nextInt(256);
          int b = rand.nextInt(256);
          g2d.setColor( new Color(r,g,b) );
          int x = rand.nextInt( 640/2 );
          int y = rand.nextInt( 480/2 );
          int w = rand.nextInt( 640/2 );
          int h = rand.nextInt( 480/2 );
          g2d.fillRect( x, y, w, h );
        }
                                     
        // display frames per second...
        g2d.setFont( new Font( "Courier New", Font.PLAIN, 12 ) );
        g2d.setColor( Color.GREEN );
        g2d.drawString( String.format( "FPS: %s", fps ), 20, 20 );
                                
        // Blit image and flip...
        graphics = buffer.getDrawGraphics();
        graphics.drawImage( bi, 0, 0, null );
        if( !buffer.contentsLost() )
          buffer.show();
                                
        // Let the OS have a little time...
        Thread.yield();
      } finally {
        // release resources
        if( graphics != null ) 
          graphics.dispose();
        if( g2d != null ) 
          g2d.dispose();
      }
    }
  }
}
results
pb directx : ~130 fps
pb opengl : ~275 fps
java : ~1000 fps

java executable : http://demo.ovh.net/en/44d4f05017461b9b ... 32569b38d/

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 10:54 am
by Foz
Something is wrong with your system - I'm getting over 1100 fps here with the PB version.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 11:06 am
by eesau
xorc1zt, are you testing with debugger off? It should always be off for these kinds of tests.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 11:43 am
by xorc1zt
yes debugger off

my system:
windows 7 x64
intel e8400
amd 5770 ( catalyst 11.8 )

please could you compile the code and upload the executable here http://demo.ovh.net/en

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 11:55 am
by MachineCode
I get around 640 fps on my PC for the PureBasic executable, but it's an old PC (XP with a dual-core CPU at 2600 MHz each, with an old slow GeForce 8400 GS graphics card).

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:07 pm
by Thorium
On my PC i get only 75 FPS with DirectX9, 60 with DirectX7 and 280 with OpenGL. My system is a Core i7 @ 2,66Ghz with a GeForce GTX 260.

I guess PB uses something new graphics cards do not support but i dont know.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:07 pm
by Foz
Ok, I'm on an old P4 3ghz, with a Radeon X800SE, using XP - and if the mouse is off the window - it reaches up to 1400, and if the mouse is moving around the window, it drops to around 600.

It is possible that it's something to do with the fact that it's Windows 7. But I won't be able to confirm until I get home tonight.

As a thought: you don't have DirectX7 set as the sub system do you? If I set it in XP, it appears that VSync is enabled (I only get 60-63 fps)

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:12 pm
by Thorium
I guess it's the box drawing. An actual 2D game project of me runs with DirectX9 on 900-800 FPS. While scrolling background, displaying animations and playing music.

Btw. i am running Win7 x64.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:28 pm
by MachineCode
Thorium wrote:I guess it's the box drawing
Yep, because if I change "Box" to "Line" then my fps jumps from 640 to 814.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:29 pm
by IceSoft
PB use GDI, Java GDI+ ?

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:42 pm
by Thorium
IceSoft wrote:PB use GDI, Java GDI+ ?
No GDI at all, this is DirectX.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 12:52 pm
by xorc1zt
java use gdi and directdraw
3.1.3 Windows OS: Using Default DirectDraw/GDI Pipeline

The default pipeline on the Windows platform is a mixture of the DirectDraw pipeline and the GDI pipeline, where some operations are performed with the DirectDraw pipeline and others with the GDI pipeline. DirectDraw and GDI APIs are used for rendering to accelerated offscreen and onscreen surfaces.

Starting with the Java SE 6 release, when the application enters full-screen mode, the new Direct3D pipeline can be used, if the drivers satisfy the requirements. Possible issues with the Direct3D pipeline include rendering artifacts, crashes, and performance-related problems.

An additional pipeline, the OpenGL pipeline, might offer greater performance in some configurations.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 1:11 pm
by Thorium
Do you realy need the box drawing?
We could make a alternative procedure for it but you are better with just using sprites.

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 1:20 pm
by xorc1zt
actually i gonna use opengl without pb 2D Drawing library.

here is the same test with my GLFW base for purebasic : http://demo.ovh.net/en/1c9793d0960f7ca3 ... 8a018f41c/

~ 5500 fps

Re: 2D drawing slow as hell

Posted: Tue Sep 20, 2011 6:12 pm
by netmaestro
2DDrawing isn't the ideal way to display objects in Directx. It's really only there for text etc. That said, there's really nothing wrong per se with the Box() command, it just needs to be used in a context where it's more suited. Try this, it should be much faster than the DX:

Code: Select all

#screenwidth   = 640
#screenheight  = 480

OpenWindow(0, 0, 0, #screenwidth, #screenheight, "PB Drawing", #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)

fpscounter.i  = 0
timer.i       = 0
fps.i = 0
frames.i = 0
totalTime.l = 0
curTime.l = ElapsedMilliseconds()
    
Repeat
  
  lastTime = curTime
  curTime = ElapsedMilliseconds()
  totalTime + (curTime - lastTime)
  If( totalTime > 1000 ) 
    totalTime - 1000
    fps = frames
    frames = 0
  EndIf
  frames + 1
        
  StartDrawing(WindowOutput(0))
  Box(0,0,640,480,#Black)
  For i = 0 To 20
    Box(Random(320), Random(240), Random(320), Random(240), Random($FFFFFF) )
  Next i
  SetWindowTitle(0,Str(fps))
  StopDrawing()
    
Until WindowEvent() = #PB_Event_CloseWindow