We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › P3D faster than OpenGL, any reason
Page Index Toggle Pages: 1
P3D faster than OpenGL, any reason ? (Read 2117 times)
P3D faster than OpenGL, any reason ?
Feb 19th, 2009, 4:15pm
 
Hello,

I'm currently using Processing for fast prototyping, and a recent change in computer (from medium to very high-speed) and in Processing version (0.156 to 1.0.1) made my sketches going down in framerate in OpenGL (dropping from 60fps to 30 as soon as more than a few hundred triangles are displayed - ~250) and going back to P3D make a steady 60fps even when zooming out and displaying everything (>1k triangles)

I tried with/without VSync, removing smooth made a small improvement (45 fps instead of 30 with a few hundred triangles/texts drawn), going back to 0.156 but now P3D is a lot faster.

On my old Pc, the OpenGL mode was 60fps steady while P3D was around 10fps Smiley.

Anyone can explain a reason why this is not working faster in OpenGL ?
Thank you very much for your help if you have any tips about this.
(i'm not very good at hardware, should there be an opengl setup I had done on the old PC that I should do on the new one ?)
Re: P3D faster than OpenGL, any reason ?
Reply #1 - Feb 20th, 2009, 10:50am
 
I had a flash thought this morning about using P3D instructions in OpenGL Mode, and I was right.

Testing with this code, I think that an automatic flush is called when you use P3D instruction in OpenGL rendering mode, thus making it slow quite fast if you have a lot of primitives.

Learnt : either use P3D with processing graphic function, or OPENGL with OpenGL context functions, but not a mix if you want it to be fast Wink


Code:

import javax.media.opengl.*;
import com.sun.opengl.util.*;
import processing.opengl.*;

String Mode = OPENGL;
String RMode = P3D; // Rendering mode, mean do we use processing function to display or GL context functions
PFont fontA;
boolean bFlush = false;

// ____________________
void setup()
{
if ( RMode == OPENGL )
Mode = OPENGL;
size(640, 448, Mode);

if ( Mode == OPENGL )
{
// enable VSync to avoid framerate problems ?
PGraphicsOpenGL pgl;
GL gl;
pgl = (PGraphicsOpenGL)g; //processing graphics object
gl = pgl.beginGL(); //begin opengl
gl.setSwapInterval(1); //set vertical sync on, screen refresh is 60 Hz so framerate will be 60
pgl.endGL(); //end opengl
}
else
{
frameRate(60); // a little more than 60 because 60hz screen and vsync activated ?
}

background(10);
fontA = loadFont("ank-48.vlw");
}

// ____________________
void draw()
{
background(10);

// display framerate
color FillColor = color(204,204,204,255);
fill(FillColor);
textFont(fontA, 14);
text("fps "+frameRate, 15, 15);
text("Viewport Mode = "+Mode,15,30);
text("Instruction Mode (R) = "+RMode, 15, 45);
text("force flush (F) ? "+bFlush, 15, 60);

// send some triangles to display
// unoptimized to test
int psize = 10;
noStroke();
if ( RMode == P3D )
{
for (int i=0; i<2000; i++)
{
pushMatrix();
translate(random(width/3.0)+width/3.0, random(height/3.0)+height/3.0, random(80)+80); // reverse order, translate before rotate
rotateZ(random(2*PI));
beginShape();
fill(color(random(255), random(255), random(255), random(255)));
vertex(-psize/2.0, -psize/2.0);
vertex(psize/2.0, -psize/2.0);
vertex(psize/2.0, psize/2.0);
vertex(-psize/2.0, psize/2.0);
endShape();
popMatrix();
if ( bFlush )
flush();
}
}
else // RMode == OPENGL
{
for (int i=0; i<2000; i++)
{
PGraphicsOpenGL pgl;
GL gl;
pgl = (PGraphicsOpenGL)g; //processing graphics object
gl = pgl.beginGL(); //begin opengl
gl.glPushMatrix();
gl.glTranslatef(random(width/3.0)+width/3.0, random(height/3.0)+height/3.0, random(80)+80);
gl.glRotatef( random(2*PI), 0,0,1 );
gl.glBegin(gl.GL_POLYGON);
gl.glColor3f(random(255)/255.0,random(255)/255.0,random(255)/255.0);
gl.glVertex3f(-psize/2.0, -psize/2.0, 0.0f);
gl.glVertex3f(psize/2.0, -psize/2.0, 0.0f);
gl.glVertex3f(psize/2.0, psize/2.0, 0.0f);
gl.glVertex3f(-psize/2.0, psize/2.0, 0.0f);
gl.glEnd();
gl.glPopMatrix();
if ( bFlush )
gl.glFlush();
pgl.endGL(); //end opengl
}
}
}

// ____________________
void keyPressed()
{
if ( key == 'f' || key == 'F' )
bFlush = !bFlush;

if ( key == 'r' || key == 'R' )
{
if ( Mode == OPENGL )
{
if (RMode == OPENGL )
{
RMode = P3D;
println("going to P3D Rmode");
}
else if ( RMode == P3D )
{
RMode = OPENGL;
println("going to OPENGL Rmode");
}
}
else
println("Can't switch to OpenGL instructions in P3D render mode");
}
}
Re: P3D faster than OpenGL, any reason ?
Reply #2 - Feb 21st, 2009, 7:15pm
 
Quote:
Testing with this code, I think that an automatic flush is called when you use P3D instruction in OpenGL rendering mode, thus making it slow quite fast if you have a lot of primitives.

Learnt : either use P3D with processing graphic function, or OPENGL with OpenGL context functions, but not a mix if you want it to be fast Wink

Can a knowledgeable hacker please confirm or deny this potentially very important insight I had wondered why the OpenGL version of my Patchy teapot demo was so much slower than the P3D version. If the OP is correct, and if I want Patchy to be useful in OpenGL, that means I have to write a separate OpenGL renderer.
Re: P3D faster than OpenGL, any reason ?
Reply #3 - Feb 22nd, 2009, 12:09am
 
I'm not sure what your benchmark was intended to test, but the OpenGL portion can be easily improved:  try putting beginGL/endGL/glFlush outside your geometry loop -- you're calling those 2000 times per frame, yikes!!!  (and glFlush probably isn't even necessary)
Re: P3D faster than OpenGL, any reason ?
Reply #4 - Feb 22nd, 2009, 1:49pm
 
davbol wrote on Feb 22nd, 2009, 12:09am:
I'm not sure what your benchmark was intended to test, but the OpenGL portion can be easily improved:  try putting beginGL/endGL/glFlush outside your geometry loop -- you're calling those 2000 times per frame, yikes!!!  (and glFlush probably isn't even necessary)


I wanted to find why the OpenGL rendering mode was slower when using Processing functions instead of gl.glxxx functions. I had to put the flush in the loop to have an (almost) equal framerate results, thus leading to some though it could be the reason. Of course you must not do that in a real project !

...
...

Now I tested at home, where I do have the almost same PC as at work (quad core 9450 @ 2.66 Ghz, 4GbRam, GTX 260 GeForce, only difference is Work PC have a little faster ram setup), and the results are not the same, everything run fine should I use OpenGL or P3D instructions... and the only slowdown I found was the OpenGL forced flush as your comment was suggesting.

Now I'm really confused about what reason can make this skecth run slowly on some PC and not some others...

This must be a PC-reason, but what (I do really need some fast results at work, as I said I'm making lots of prototypes in P5 for real projects, but I don't think they are ready to switch my Work and Home PCs Wink. At least I switched to P3D mode, but not a lot of people have powerfull PCs to demo the prototypes if not in OpenGL. And switching all the prototypes to gl.glxxx instructions might be a real time consuming task).

Thanks for any tips on this !

Edited:
Just a random though after reading this again, another difference between Home/Work PCs is that I have a dual screen at work, So I'll test by deactivating this feature tomorrow. I know Dual Screen setup can make some OpenGL problems as I experimented some, so wait and see...
Re: P3D faster than OpenGL, any reason ?
Reply #5 - Feb 22nd, 2009, 7:11pm
 
Regarding performance, see my post in this thread:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1217367002

And the documentation for PGraphicsOpenGL:
http://dev.processing.org/reference/everything/javadoc/processing/opengl/PGraphicsOpenGL.html

I'll be working on OpenGL optimizations in the coming months. There's lots that can be improved, but I just haven't had time to do it.
Re: P3D faster than OpenGL, any reason ?
Reply #6 - Feb 23rd, 2009, 9:54am
 
fry wrote on Feb 22nd, 2009, 7:11pm:
Regarding performance, see my post in this thread:
http://processing.org/discourse/yabb_beta/YaBB.cgi?num=1217367002

And the documentation for PGraphicsOpenGL:
http://dev.processing.org/reference/everything/javadoc/processing/opengl/PGraphicsOpenGL.html

I'll be working on OpenGL optimizations in the coming months. There's lots that can be improved, but I just haven't had time to do it.



Thank you for the links and information !


Now I tested at work on the dual screen config, and disabling the second screen fix the problem.
In fact, when using the 2 screen setup, after some time the framerate stabilize to 30 fps, 1/2 of the real refresh.
(my first screen is 1600x1200, the second is 1680x1050, both 60 Hz)
Changing the screen refresh rate change the framerate a bit but don't fix it, enabling/disabling the VSync or FrameRate function don't change anything.


Should I contribute by reporting the bug
Page Index Toggle Pages: 1