Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (474)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  opengl 2D performance  (Read 4592 times)
0 Members and 1 Guest are viewing this topic.
Offline userek

Junior Member


Projects: 1



« Posted 2008-09-12 23:42:49 »

Hello,
Basicly im using jogl to write a 2D game. The problem is that its much slower than i expected. Curently im working on some old computer with some 1,6Ghz cpu and some acient Geforce card, still i remember pretty decent fps when i played years ago some quake 3 engine based games Wink.
In my engine(well maybe engine is too big word for now Wink) i get 30-35, and that is after some basic optimalzations i've made: culling - without it was horrible.
Needless to say im disspointed since i expected simple 2d game to be much less resource demanding than quake iii.
I have about 300 textured quads on my screen, most of them are ground tiles. Without the ground i get twice as much fps, still i expected some crazy number like 400 or something.
There might be something wrong in my code(its a mess so i'll post only the opengl part):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
   public void init(GLAutoDrawable glDrawable) {   
      GL gl = glDrawable.getGL();
      gl. glEnable( GL.GL_BLEND );
      gl. glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA );
      gl.glEnable(GL.GL_TEXTURE_2D);
      gl.glClearColor(255, 255, 255, 0);
      gl.glViewport(0, 0, width, height);
      gl.glDisable(GL.GL_DEPTH_TEST);      
   

      initGame();
     
      animator = new Animator(glDrawable);      
      animator.start();
   }

   public void display(GLAutoDrawable glDrawable) {      
      GL gl=glDrawable.getGL();
     
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();  
      ...//drawing sprites
     gl.glFlush();
     
   }


and the method i use to draw the sprite:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
   public void draw(GL gl , boolean bind) {
      Texture texture = getTexture();
      if(bind){
         texture.enable();
         texture.bind();
      }
       TextureCoords coords = texture.getImageTexCoords();

      gl.glPushMatrix();
      gl.glTranslatef(x, y, 0);      
      gl.glColor3f(1,1,1);
        gl.glBegin(GL.GL_QUADS);
        {
            gl.glTexCoord2f(coords.left(), coords.top());
            gl.glVertex2f(0, 0);            
            gl.glTexCoord2f(coords.left(), coords.bottom());
            gl.glVertex2f(0, height);            
            gl.glTexCoord2f(coords.right(), coords.bottom());
            gl.glVertex2f(width, height);            
            gl.glTexCoord2f(coords.right(), coords.top());
            gl.glVertex2f(width, 0);
        }
      gl.glEnd();
      //texture.disable();
     gl.glPopMatrix();
   }


Any ideas?
thanks

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 729
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-09-12 23:57:24 »

Ofcourse we could all write up pages upon pages of optimalisations, but you could also Google it yourself.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline userek

Junior Member


Projects: 1



« Reply #2 - Posted 2008-09-13 00:17:01 »

Ofcourse we could all write up pages upon pages of optimalisations, but you could also Google it yourself.

Of course, you could just close this subforum since there is google and anyone can use it. Come on man? what is the point of your post? You just simply had 2004 posts and wanted to have 2005? or is there something deeper behind it?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 729
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2008-09-13 00:42:38 »

The point is that Googling this stuff yourself is way faster than waiting for an answer on this forum, on which this topic has been beaten to death anyway, so there are more than enough threads for you to dig that info up.

Just show you invest some time into solving a problem, and people are much more likely to help. Your posting feels like: here's my code, figure out my problem and point out the obvious. I mean... come on put some effort in it.





Oh, and I don't care about my postcount.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline userek

Junior Member


Projects: 1



« Reply #4 - Posted 2008-09-13 01:54:44 »

The point is that Googling this stuff yourself is way faster than waiting for an answer on this forum, on which this topic has been beaten to death anyway, so there are more than enough threads for you to dig that info up.

Just show you invest some time into solving a problem, and people are much more likely to help. Your posting feels like: here's my code, figure out my problem and point out the obvious. I mean... come on put some effort in it.
Listen, instead of posting just to say that you are not going to help, just don't post at all and  everyone will be happy. You post to say nothing smarter than 'google for it nub' and even worse you discourage other people form helping.
I didn't ask exacly you to help me, so please don't post in any of my threads.
thank you.
Offline princec

JGO Kernel


Medals: 334
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2008-09-13 10:14:18 »

Oh go on then I'll help.

1. You're using immediate mode rendering. That's the very slowest way to render things to the screen there is.

2. You push/translate/pop for every sprite; again, pretty much the slowest possible way to draw, as it flushes the graphics pipeline every time.

Oh, and possibly best to ask OpenGL performance questions over on the OpenGL.org beginner boards too, there are a lot more people there who know what they're doing than in here!

Cas Smiley

Offline userek

Junior Member


Projects: 1



« Reply #6 - Posted 2008-09-13 22:03:06 »

Oh go on then I'll help.

1. You're using immediate mode rendering. That's the very slowest way to render things to the screen there is.
Thank you for your reply. Actually i thought there must be something wrong with my code but its seems that opengl is a bit slower than i expected Smiley. I expected a simple 2d game to be blazing fast without any optimalizations.
I'll investigate retained mode rendering, i think this is something i need.

2. You push/translate/pop for every sprite; again, pretty much the slowest possible way to draw, as it flushes the graphics pipeline every time.
I tryied to change that, and to do push and pop only when the texture is changed but i didn't notice the fps boost. Eariler i implemented sprite sorting by texture, to avoid unnesesery texture binding but it only gave me something like 2 fps and i was a bit disppointed Wink.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 729
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2008-09-13 22:13:25 »

One of the problems is that you use one glBegin/glEnd per quad.

So if you have the code around to sort by texture, you should also group all those quads in the same glBegin/glEnd to boost your performance significantly.

This will most likely boost your performance quite a bit, after which you can move to vertex arrays, or vertex buffer objects.

Or merging a few textures into 1 big texture to avoid bindings, and allowing you to group more quads in your glBegin/glEnd.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2008-09-13 22:50:54 »

Don't bother with the matrix stack for sprite positioning, just calculate the coords yourself manually and it'll be much faster - especially if you've only got basic translation going on.

The exception to that is camera positioning, where the matrix stack is much more useful and is only done a handful of times a frame so it's not an issue.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline gouessej
« Reply #9 - Posted 2008-09-13 22:54:07 »

Maybe you could use my algorithm of cells-and-portals subdivision (the implementation is under GPL license), I think it may be used in a 2D game, it might allow you to pass a smaller part of the whole geometry, otherwise you could use a BSP tree. If you need some examples of Java 2D games, go to the FGF players' portal, we have a lot of Java open source 2D games available, the only problem is that this current version of the portal is only on Facebook, the web version is not ready.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline userek

Junior Member


Projects: 1



« Reply #10 - Posted 2008-09-14 14:50:35 »

Thank you for your anwsers. Removig the matrix stack and  using glBegin/glEnd only when the texture is changed gave me 15-20 fps boost, which will probably drop once i put more objects on the screen Wink.
And no, I'm not interested in using any GPL code, since i don't want to make an opensource game.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2008-09-14 15:55:18 »

Your next step should probably be to switch from glBegin/End to using vertex arrays (in your case this probably means using glDrawArrays). A quick google found this which should point you in the right direction.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #12 - Posted 2008-09-14 16:43:48 »

gl.glDisable(GL.GL_DEPTH_TEST);
[...]
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

Very funny. If you don't use it don't clear it. If you overdraw everything either way don't bother with clearing. It's pointless.

Don't enable/disable textures over and over again. Enable it and draw your stuff. If you need to disable it for a few things disable it there, draw that stuff, and enable it again.

Before you bind a texture check if it's already bound (keep track of it with some variable).

Reduce the number of state changes where possible.

Well, even with immediate mode rendering you can render approximately 2000 sprites with a gf1ddr (or gf2mx classic or gf2mx 400) on a 500mhz machine - if the same texture is used for all of em (and no rebinding happens). Even with individual per-sprite begin-quad and end blocks.

弾幕 ☆ @mahonnaiseblog
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 729
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2008-09-14 18:38:06 »

Before you bind a texture check if it's already bound (keep track of it with some variable).

Actually, that's not true. The drivers do that already - for textures, VBOs and FBOs.

Try it, and you'll see checking it yourself is *slightly* slower, due to the double check.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #14 - Posted 2008-09-14 18:59:46 »

Checking it myself gave me a speed boost of about 10% on my gf2mx. It's sensible to do that stuff on the driver side, but it seems that they'd forgotten to do it here n there.

弾幕 ☆ @mahonnaiseblog
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 729
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2008-09-14 19:13:33 »

Seems like it depends on the driver version (or whether it is nVidia, Intel or ATi/AMD)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline userek

Junior Member


Projects: 1



« Reply #16 - Posted 2008-09-14 22:57:56 »

gl.glDisable(GL.GL_DEPTH_TEST);
[...]
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

Very funny. If you don't use it don't clear it. If you overdraw everything either way don't bother with clearing. It's pointless.


Thanks for pointing that out! This was really slowing thigns up. Now i get ~20 fps more Smiley.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 729
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2008-09-14 23:37:53 »

WTF! That's weird... Everybody and their dog clears the depth buffer in a 3D game, and it certainly doesn't/shouldn't affect framerate that much.

In case you need the depth buffer later on, also clear the stencil buffer. clearing both is much faster than clearing one of them, as their memory is interleaved.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline princec

JGO Kernel


Medals: 334
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #18 - Posted 2008-09-14 23:51:50 »

Some cards have fast-erase for the depth buffer and it costs almost nothing; other cards don't. Assume it's one of the ones that don't, and don't clear it. Especially if you're doing 2D - no need for a depth buffer in the first place!

Cas Smiley

Offline userek

Junior Member


Projects: 1



« Reply #19 - Posted 2008-09-15 00:08:30 »

Acutally now i don't clear neither color buffer nor the depth buffer. Maybe my graphics card is broken or something but clearing any of these buffers slows everything by 20-30 fps.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (27 views)
2014-07-10 20:20:18

CopyableCougar4 (25 views)
2014-07-10 02:26:14

CopyableCougar4 (31 views)
2014-07-09 02:55:38

Code Mage (32 views)
2014-07-08 23:57:00

Code Mage (19 views)
2014-07-08 23:49:08

AppleSauce (27 views)
2014-07-08 19:25:32

CopyableCougar4 (27 views)
2014-07-06 01:51:26

ipe369 (33 views)
2014-07-05 14:18:25

vastrolorde (43 views)
2014-07-04 18:45:44

theagentd (60 views)
2014-07-02 22:19:37
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!