Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (789)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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 VM flag degrade performance on Java2D???  (Read 5891 times)
0 Members and 1 Guest are viewing this topic.
Offline alesky

Junior Devvie

Medals: 3
Exp: 15 years


« Posted 2013-01-07 09:51:20 »

And without the "OpenGL" VM flag?

my situation was to have a game implemented without the flag for the OpenGL.
my framework (done in java2D) is completely based on the BufferedImageOp to draw on the fly effect!

how my frameworks works is this just create to any cycle the next immage and then the the drawable:

dbImage = JPanel.createImage(pWidth, pHeight); // create the double buffer immage
dbg = (Graphics2D) dbImage.getGraphics();

after that it is pass to the rendering that are 2D sprite based on the bufferedImmage

any sprite before to be render on the dbg ( that i s the Graphics2D) is passed to a chain of effect that execute specific effect implemented as BufferedImageOp brightness, and transparence or affine trasfomration for scale and rotation

//filtersToApplyare all the filter ot use: can be any, one of this or all -->brightness, transparence,scale and rotation
BufferedImage filtered = EffectManager.getInstance().chain(filtersToApply,image, position, size, color);
dbg.drawImage(filtered, (int)(position.getPosX() - filtered.getWidth()/2), (int)(position.getPosY() - size.getHeigh()/2), null);

in the end finally draw the final immage with all the sprite and background painted over
g = (Graphics2D) gamePanel.getGraphics();
g.drawImage(doFinalEffect(dbImage), 0, 0,pWidth,pHeight, null);

but before is possible to pass the final image (that is the immage with the background and all the sprites) again to a chain of effect (that is implemented has before as BufferedImageOp  brightness to make obscuration of all the immage)

ALL this story to explane that it is very easy nothing complex, and when i have activated the "OpenGL" VM flag -Dsun.java2d.opengl=True, my application is start to be very slow!!! it become a disaster, lost 30% of the frames!!!!
i would like to understand why, or at list to understand how to analyze
Offline kappa
« League of Dukes »

JGO Kernel

Medals: 123
Projects: 15


« Reply #1 - Posted 2013-01-07 09:58:47 »

Its likely the Direct3d backend is just faster on your pc (that is if you are on Windows).

Also "-Dsun.java2d.opengl=True" is a bit of a debug mode and will give you verbose console output, highly unlikely but "-Dsun.java2d.opengl=true" maybe a tiny bit faster.

btw which graphics card are you on?
Offline alesky

Junior Devvie

Medals: 3
Exp: 15 years


« Reply #2 - Posted 2013-01-07 10:30:37 »

the output in the console is almost nothing, so modifying it din't recognize any effect

i'm working on my laptop and the graphics card is integrated on it:
Intel(R) HD Graphics Family

testing this card with the OpenGl Extensions viewer 3.0 i'm able to support with this card operation
openGL 2.2
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline The-Next

Senior Devvie

Medals: 7
Projects: 2

« Reply #3 - Posted 2013-01-07 17:07:06 »

Hi alesky!

I've been working a little bit with an intel HD3000 with Java2D in my laptop (JDK 7 and Windows 7) and:

I tried both flags: for using direct3D and openGL pipeline with:

    System.setProperty("sun.java2d.opengl", "True"); or    System.setProperty("sun.java2d.d3d", "True");

And I get much better performance with direct3D pipeline.

Also, I have to say that, for some reason, the global performance of intelHD with java2D is quite bad, maybe is a driver issue. I have also a Nvidia GT520 in the laptop. In theory Nvidia GPU is not much better than the intel HD, but the performance in java2D is much better, maybe 5 times faster. Also, sometimes, the intel behaves weirdly. The performance suddently drops when I add some extra pictures.


About the code you show. Several months ago a did many microbenchs in order to get the best performance with java2D, but I don't remember all the details.

You use a jPanel for drawing the stuff?. Did you tried to use a BufferStrategy over a canvas instead? As far I remember it gives better performance.

Ok, then you apply a set of affineTransforms, right?, so you create an intermediate BufferedImage called "filtered" with all the changes. Do you tried directly to draw the image to dbg without creating that "filtered" image?

something like this:


also, are you creating a new image each cycle?

"dbImage = JPanel.createImage(pWidth, pHeight); // create the double buffer immage"

Why don't you reuse it, I found that the command clearRect is really efficient for clearing an image, is even more efficient than fillRect.


I would not mind to take a look at your full code, I enjoy optimizing Tongue


Offline davedes
« Reply #4 - Posted 2013-01-07 17:11:26 »

Personally I wouldn't rely on that flag - some systems won't support it, others will perform better, and some may even perform worse.

General consensus is: if you need reliable performance, Java2D is the wrong choice. Wink

Offline The-Next

Senior Devvie

Medals: 7
Projects: 2

« Reply #5 - Posted 2013-01-07 17:18:53 »

Oh, yes! davedes post remind me an important thing. Sometimes Java2D cannot recognize the hardware, so the direct3D pipeline cannot be activated. This happens for example in laptops with nvidia optimus.

You can solve this with the environment variable J2D_D3D_NO_HWCHECK=true, so you deactivate any hardware checking and pipeline can be enabled.

I would love to try java2D in more computers. But yes, it seems that if you go outside standard configurations, weird things can happen. That makes development more thrilling  Grin

Offline BoBear2681

JGO Coder

Medals: 19

« Reply #6 - Posted 2013-01-07 21:08:06 »

Setting flags like that always screams to me "I can determine what Java2D pipeline will run faster on this machine better than Java2D itself can."  Do you think the JVM is picking the wrong pipeline for some reason?
Offline alesky

Junior Devvie

Medals: 3
Exp: 15 years


« Reply #7 - Posted 2013-01-07 23:55:20 »

Next thanks to you
regarding the optimization i want to answer to all your idea just to share our knowledge

firs of all
The chose of the JPanel happen for one specific reason the method JPanel.createImage(pWidth, pHeight)
hire the official link to java doc
this method must be called each time in the cycle, because it manage automatically for you the double buffering strategy
increasing the performance, so for this reason this object is used by double buffering i can not reuse the same object clearing the immage

never tried to do it by canvas, is there a specific method to do it automatically? the old AWT expose something to do it? if exsist let me know i want to try it

Ok, then you apply a set of affineTransforms, right?, so you create an intermediate BufferedImage called "filtered" with all the changes. Do you tried directly to draw the image to dbg without creating that "filtered" image?

this part of the code is already optimized, the solution applied is this:
every image transformed in the chain of effect is stored in a repository  keeping with it information of the kind of transformation performed over it, so every time that  on a image is required to execute same effect, first of all I look in the repository if that image with that kind of transformation already exists in this case this image is get and reused, other ways a new image with effect is created and saved in the repository.
this make in my case a huge increasing of the performance and aloud to me to work on image modification on the fly
but the back side of this solution is the memory used to store images in the repository

instead regarding this fact (davedes please don't take it personally, but i have to write this)
General consensus is: if you need reliable performance, Java2D is the wrong choice
the fact that exist technologies that are better of the Java2D is something that also the kids know,
so if i use this specific section of the forum called Java2D, and not OpenGL Development may be ther is a reason what u think?

if i can get 1 euro for all the guys that write in this forum,
General consensus is: if you need reliable performance, Java2D is the wrong choice
i will become one of the most reach person in the word (ok i will open a topic for this  Grin)
regarding the real discussion of this topic,
Setting flags like that always screams to me
me too!!!

i want to try to set the variable J2D_D3D_NO_HWCHECK=true and try to activate the direct3D. i will let to know how it works

Offline The-Next

Senior Devvie

Medals: 7
Projects: 2

« Reply #8 - Posted 2013-01-08 05:11:05 »

alesky, I show you a skeleton of a canvas ready for drawing anything you want. I use a VolatileImage as a intermediate buffer. I set the same name you used. If you try it, it would be great some result comparison  Cheesy

public class Zoom_canvas extends Canvas {

    protected BufferStrategy buffer;  //BufferStratrgy
    protected VolatileImage dbImage ; //VolatileImage as intermediate buffer

    public Zoom_canvas() {   //constructor
        this.setIgnoreRepaint(true); //we repaint manually

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
        dbImage = gc.createCompatibleVolatileImage(this.getWidth, this.getHeight, Transparency.TRANSLUCENT); //initialize the VolatileImage

    public void dispose() {

    public void Init() { //call this before starting game loop, it initializes the bufferStrategy
        createBufferStrategy(2);  //double buffering
        buffer = getBufferStrategy();

    public void PerformDraw() {  //public drawing method, call this from your game loop for update image

        Graphics g;
        try {
            g = buffer.getDrawGraphics();
        } catch (Exception e) {
        this.paint((Graphics2D) g);

        if (!buffer.contentsLost()) {

    private void paint(Graphics2D g) { //real drawing method


        Graphics2D ga = (Graphics2D) dbImage.getGraphics();
        ga.setBackground(new Color(0, 0, 0, 0));
        ga.clearRect(0, 0, dbImage.getWidth(), dbImage.getHeight()); // we clear the intermediate buffer

//... TO DO, ADD dome drawing here using "ga"!!!

        g.drawImage(dbImage, 0, 0, null);

Pages: [1]
  ignore  |  Print  

hadezbladez (2614 views)
2018-11-16 13:46:03

hadezbladez (940 views)
2018-11-16 13:41:33

hadezbladez (2567 views)
2018-11-16 13:35:35

hadezbladez (506 views)
2018-11-16 13:32:03

EgonOlsen (3724 views)
2018-06-10 19:43:48

EgonOlsen (4122 views)
2018-06-10 19:43:44

EgonOlsen (2483 views)
2018-06-10 19:43:20

DesertCoockie (3288 views)
2018-05-13 18:23:11

nelsongames (3384 views)
2018-04-24 18:15:36

nelsongames (4363 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 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‑
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!