Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  2D performance problems  (Read 2027 times)
0 Members and 1 Guest are viewing this topic.
Offline bison

Senior Newbie





« Posted 2009-03-29 02:27:21 »

Hello

Im having troubles getting decent performance out of a simple 2d tilescroller. Java alphablending with  translucent transparency is god awful slow, as expected.
What I want is simple colorkeying, eg defining a transparent color in the source. Couldn't find any direct way of doing that. Instead I read up on bitmask transparency.

Im loading image data from RGBA png files. So to get them ready for blitting I converted them like this:

1  
2  
3  
4  
5  
6  
7  
8  
            BufferedImage original = ImageIO.read(in);
            java.awt.GraphicsEnvironment ge = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
            java.awt.GraphicsDevice gd = ge.getDefaultScreenDevice();
            java.awt.GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage bim = gc.createCompatibleImage(original.getWidth(), original.getHeight(), Transparency.BITMASK);
            Graphics2D g2d = (Graphics2D) bim.getGraphics();
            g2d.setComposite(AlphaComposite.Src);
            g2d.drawImage(original, 0, 0, null);


To my horror this is for some reason just as slow as Transparency.TRANSLUCENT Sad
I tried reading img data from GIF instead in case Transparency.BITMASK only works for indexed colormodel, with no noticeable speed difference.
Besides i'd still like to able to use 24 color instead of 8 bit, and I don't think thats whats slowing it down either. I get flawless performance on 24 RGB with Transparency.OPAQUE.

What am I doing wrong? seems like java2d is doing per pixel RGBA blitting when I want it to use  BITMASK (colorkeying).

oh and is there anyway of using colorkeying specifying your own color key as a 24 bit RGB value? And can I even assume that the above code auto converts RGBA data from the png to
RGB + transparency bitmask?
Offline CommanderKeith
« Reply #1 - Posted 2009-03-29 03:14:04 »

Try replacing this:
BufferedImage bim = gc.createCompatibleImage(original.getWidth(), original.getHeight(), Transparency.BITMASK);

with this

VolatileImage bim = gc.createCompatibleVolatileImage Image(original.getWidth(), original.getHeight(), Transparency.TRANSLUCENT);

Also, you don't want to create a new image every frame, do it once, then just draw on top of the image every frame.

When using VolatileImages there are some other checks you need to do when displaying them, just google it.

Hope that helps!
Keith

Offline bison

Senior Newbie





« Reply #2 - Posted 2009-03-29 03:52:36 »

Thank you for your reply

I've read about volatile images, but I thought they didn't make difference without acceleration, especially under linux where you don't have the directdraw render path.
I don't want to rely on hardware acceleration to archieve a playable framerate, then i'd gone opengl from the beginning.
Just thought java2d was fast enough to blit a tilemap with colorkey transparency to a 800x600 window in software. Anyway i've begun blitting the first layer of tiles as OPAQUE and the rest
as BITMASK. Helps tremendously.
I still can't understand why simple on/off colorkey transparency isn't faster than per pixel alphablending.
An underlying RLE implementation should result in even faster colorkeyed blits than a straight copy.

Quote
Also, you don't want to create a new image every frame, do it once, then just draw on top of the image every frame.
Don't insult my intelligence good sir Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #3 - Posted 2009-03-29 04:08:31 »

What's the size of the tiles? you using bufferstrategy?

In my game I'm not having any trouble, my bottom tiles are all bitmask then I have a overlay over the whole game window for night which is translucent. I thought I would get slow downs, but I tested in Java 5 and 6 without and slow downs running about 6% cpu 30fps with intel graphics. I guess the game window is smaller, but still...


Current project - Rename and Sort
Offline bison

Senior Newbie





« Reply #4 - Posted 2009-03-29 04:31:39 »

yes im using bufferstrategy(2). On a canvas inside a JFrame under linux. Framesize is 800x600 and I render 27x20 = 540 32x32 tiles minimum each frame.
I've got a simple 3 layer setup: draw layer 1 opaque, 2 bitmask, entities (players and bullet) translucent, layer 3 bitmask. This gives me an acceptable framerate (60-80).
When I first posted this I didn't render layer 1 opaque but with bitmask (colorkey) transparency.

I know per pixel alpha is very slow in software but colorkeying should be just as fast or faster even (since you don't have to draw as many pixel as with opaque), thus I cannot understand
why there is such a dramatic difference between OPAQUE and BITMASK.. and such a small one between BITMASK and TRANSLUCENT.
I blit my tiles from one sheet image (an opaque for layer 1 and a bitmask'ed for layers 2-3) using drawImage(dst, 8 ints etc) (and im not unintentionally scaling, since rendering all tiles as opaque nets a 166+ FPS)
Offline DzzD
« Reply #5 - Posted 2009-03-29 10:56:53 »

Quote
yes im using bufferstrategy(2). On a canvas inside a JFrame under linux. Framesize is 800x600 and I render 27x20 = 540 32x32 tiles minimum each frame.

ps: you should really think about CommanderKeith advice, you will have then more free CPU for doing others stuff

...Also, you don't want to create a new image every frame, do it once, then just draw on top of the image every frame....

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 75
Projects: 15


★★★★★


« Reply #6 - Posted 2009-03-29 11:22:23 »

if you want performance its recommended you use a java opengl binding, it'll be much faster than java2d. A good library to try is Slick found at http://slick.cokeandcode.com it has similar api to java2d so porting your game should be pretty quick.
Offline bison

Senior Newbie





« Reply #7 - Posted 2009-03-29 14:40:31 »

thanks Ill look further into Volatile images. The reason why I was a bit dismissive of the idea was after reading stuff like this:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4498974

Eg. Graphics2D will only be accelerated on linux using the OpenGL pipeline.
I went with java2d on purpose, because I want my game to be playable without a 3d card. I have experience using OpenGL for 2d (and 3d for that matter)
using lwjgl as well as using the native library directly from C.
I don't want to rely on acceleration but of course I won't actively hinder it either. Bitmask transparency not being noticeable quicker than translucent transparency just
had me a bit flabbergasted. Guess its because the underlying implementation supports X bit alpha channels and have no optimization for the 1bit alpha channel case.
Figured I might be doing it the wrong way hence this topic.

I apologize if my remark about creating the image every frame came off as arrogant, what I meant was that of course I don't convert it every frame Smiley

Kapta: I've been checking out Slick as well as the rest of Kevin's site. Great stuff and ill love to try it out for a future project but in this case it was a design goal
to stay with software rendering.

I'm used to doing 2d with libSDL and was expecting bitmask transparency to be as fast as opaque blitting (since there is no technical reason it couldn't be).



As of now I'm getting acceptable performance blitting the ground layer opaque and the other layers with bitmask transparency, but as you can see there aren't
that many transparent tiles in the screenshot.
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.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!