Performance with transparency

Hi there

Some time ago I made a little test. I wanted to develop a simple 2d game, birds eye perspective, easy tiles. But I wanted to have fog so that distant tiles would fade to gray, or black in night. So i had a tile for grass and a tile in gray, and I wanted to first draw the tile and then draw the gray over that one with Alpha / Transparency of 20%, 40%, etc.
It was slow as …

As alternative i tried to precomputed all fog tiles and so I had several versions of each tile, with 20%, 40%, … of fog overlay. These images were allwithout transparency and they were fast.

I researched a little and ended up with all the best I could find, like the createCompatibleImage to get a matching image type, which addes some speed. Bitmask transparency was no problem, its just show pixel or dont show pixel. But all Alpha alternatives I tried were slow.
I tried AlphaComposite, I tried PNG files with Alpha, I tried ARGB BufferedImage. Always slow.

For some reason I want to stick to 2d right now. I am not able to learn 3d stuff in the moment, but I will do so somewhen, there seems to be no other way.

My question is, when I develop a game with Java 2d, is there a way to get fast transparency? Else I will have to use opaque and bitmasked images and precomputed / pregenerated transparencies.

-JAW

Pre-buffering the images is a good idea.
Hardware acceleration for translucent alpha blending is disabled on windows by default i think.

Try the windows option System.setProperty(“sun.java2d.translaccel”, “true”); or its command line form -Dsun.java2d.translaccel=true

Also try it with -Dsun.java2d.ddforcevram=true.

As a second option, try the openGL pipeline: -Dsun.java2d.opengl=True

My problem is, that I am between 2 extremes. I have access to 2 computers.
a) 450 MHz very old, 16 MB graphics card
b) 3 GHz modern, 256 MB graphics card

Naturally, on a nothing works, on b everything works. Measuerment results from b are of no use, because anything in my tests is just fast.

As far as I can judge by now, my old pc cannot get Javas opengl or hardware accelleration and that is the reason why it gets so slow.
I just need to check it again, when I get a better pc :slight_smile:

So probably this topic is done with that. I just had in mind, that, when I develop a little game like a jump n run, I want it to run on 1GHz, so I had
an eye on performance.

-JAW

Hi.

I had a similar problem as the one you describe a long time ago (Java 1.4.0 or so?). Translucent images were godawful slow. I came up with a solution that by all logic should be slower (due to the heavy computing), but ended up being much much faster. What I did was I took a snapshot from my backbuffer image, and embedded anything translucent onto that image, and then drew the backbuffer image as an opaque image. If I still had the code for it, I would gladly share it (I put a lot of effort into it, and the end result was pretty optimized), but sadly that disk crashed ages ago :frowning:

But the concept was to unify all the images you need to draw on top of each other to one (or several for that matter) opaque images, and then only draw those images onto the screen.

I just looked up the J2D docs and found this http://java.sun.com/javase/6/docs/technotes/guides/2d/new_features.html:

On Microsoft Windows systems, images created with createCompatibleVolatileImage in J2SE 5.0 are hardware accelerated only if the hardware supports acceleration and one of the following is true:

* The transparency value is OPAQUE.
* The transparency value is TRANSLUCENT and translucency acceleration has been specifically enabled at runtime (sun.java2d.translaccel=true). 

This is the case for Java 5 & the upcoming 6, Mustang.

For my game Mustang (java 6) performance with bitmasked images is twice as good as Tiger (Java 5) on my computer. This could be because Direct3D is used rather than directdraw. You should give Mustang a go.