Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Rendering Performance Issues  (Read 8412 times)
0 Members and 1 Guest are viewing this topic.
Offline Rorkien
« Reply #30 - Posted 2012-05-06 17:35:25 »

This draws a pixel-array onto another pixel-array. I get around 2200 fps on an i5

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   public void draw(ArrayImage a, int xp, int yp) { //Draw a normal tile
      for (int y = 0; y < a.height; y++) {
         int yPixel = y + yp;
         if (yPixel < 0 || yPixel >= height) continue;

         for (int x = 0; x < a.width; x++) {
            int xPixel = x + xp;
            if (xPixel < 0 || xPixel >= width) continue;  

            int srcPixel = a.pixels[x + y * a.width];
            if (srcPixel < 0 /*if <0 srcPixel is meant to be transparent*/) pixels[xPixel + yPixel * width] = srcPixel;                                      
         }
      }
   }
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #31 - Posted 2012-05-06 17:46:15 »

This draws a pixel-array onto another pixel-array. I get around 2200 fps on an i5
Without knowning the dimensions of those pixel-arrays, it's a meaningless statistic Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Rorkien
« Reply #32 - Posted 2012-05-06 17:55:49 »

Yeah pardon, i forgot it reaches 2200 fps with nothing on the screen, just filling it with zeros every iteration  Tongue

Also its 640x500
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ghostsoldier23

Junior Duke


Medals: 1



« Reply #33 - Posted 2012-05-06 18:29:01 »

I think I might scrap this whole thing and just invoke drawImage on the Graphics2D created by the BufferStrategy.

Does anyone know why BufferStrategy.drawImage is SO much faster than BufferedImage.drawImage?
Offline davedes
« Reply #34 - Posted 2012-05-06 18:32:04 »

Why not just use System.arraycopy to copy a region of your image?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   /**
    * Quickly copies the specified pixels from the source image to the destination image.
    * @param src the source image
    * @param dst the destination image
    * @param srcX the x position of the source image to start copying
    * @param srcY the y position of the source image to start copying
    * @param srcW the width of the section to copy from source
    * @param srcH the height of the section to copy from source
    * @param dstX the x position to place the copied pixels at on the destination image
    * @param dstY the y position to place the copied pixels at on the destination image
    */

   public void copyPixels(BufferedImage src, BufferedImage dst, int srcX, int srcY, int srcW, int srcH, int dstX, int dstY) {
      int[] srcbuf = ((DataBufferInt)src.getRaster().getDataBuffer()).getData();
      int[] dstbuf = ((DataBufferInt)dst.getRaster().getDataBuffer()).getData();
      int srcOff = srcX + srcY * src.getWidth();
      int dstOff = dstX + dstY * dst.getWidth();
      for (int y=0; y<srcH; y++) { //copy every row of src starting at offset
         System.arraycopy(srcbuf, srcOff, dstbuf, dstOff, srcW);
         srcOff += src.getWidth();
         dstOff += dst.getWidth();
      }
   }


EDIT: If you're loading images with ImageIO, they may not be TYPE_INT_ARGB. You can convert them easily like so:
1  
2  
3  
4  
5  
      if (image.getType()!=BufferedImage.TYPE_INT_ARGB) {
         BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
         convertedImage.createGraphics().drawRenderedImage(image, null);
         image = convertedImage;
      }

Offline ghostsoldier23

Junior Duke


Medals: 1



« Reply #35 - Posted 2012-05-06 19:04:40 »

Is that faster than just doing BufferStrategy.drawImage?
Offline jonjava
« Reply #36 - Posted 2012-05-06 19:10:35 »

This draws a pixel-array onto another pixel-array. I get around 2200 fps on an i5
Without knowning the dimensions of those pixel-arrays, it's a meaningless statistic Smiley

Not to mention it has nothing to do with drawing performance, that's just looping through arrays and setting their values.

It's like measuring how long a for loop takes.

1  
2  
3  
4  
5  
int[] array = new array[1920*1080];

for (int i = 0; i < array.length; i++) {
array[i] = (int) System.currentTimeMillis();
}


Doing this loop 60 times takes about ~0.07 seconds. looping through 600 times ~0.6 seconds. looping through 1200 times  ~1.2seconds.

The statistic is meaningless because it doesn't really have anything to do with frames per second or drawing. Am I wrong?

[EDIT]
Also, I made a super awesome illustrative image of what happens to an image when you turn it into an array of ints! (Row major order)

Before: BufferedImage = new BufferedImage(width, height);

After: int[] array = new int[width*height];

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #37 - Posted 2012-05-06 19:13:26 »

In the end, rendering is nothing more or less than moving data around.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline davedes
« Reply #38 - Posted 2012-05-06 19:13:54 »

Is that faster than just doing BufferStrategy.drawImage?
I would guess yes... Unless you're copying huge sections, in which case drawImage might take advantage of OpenGL acceleration (assuming it's enabled).

If this kind of performance is necessary, just benchmark it yourself.

Offline jonjava
« Reply #39 - Posted 2012-05-06 19:19:40 »

In the end, rendering is nothing more or less than moving data around.

Well my point is that if it takes less than a second to loop and alter 2 073 600 000 pixels ( 1000 * 1920 * 1080 ), performance is a non issue. Shocked

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

Junior Duke


Medals: 1



« Reply #40 - Posted 2012-05-06 19:24:25 »

Is that faster than just doing BufferStrategy.drawImage?
I would guess yes... Unless you're copying huge sections, in which case drawImage might take advantage of OpenGL acceleration (assuming it's enabled).

If this kind of performance is necessary, just benchmark it yourself.

My fps dropped by around 250 frames when using your method as compared to just drawImage.
Offline nsigma
« Reply #41 - Posted 2012-05-06 19:44:35 »

I think I might scrap this whole thing and just invoke drawImage on the Graphics2D created by the BufferStrategy.

Does anyone know why BufferStrategy.drawImage is SO much faster than BufferedImage.drawImage?

I pretty much answered this in my previous post on this thread (bearing in mind that BufferStrategy is most likely using VolatileImage)

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline ghostsoldier23

Junior Duke


Medals: 1



« Reply #42 - Posted 2012-05-06 19:52:18 »

I think I might scrap this whole thing and just invoke drawImage on the Graphics2D created by the BufferStrategy.

Does anyone know why BufferStrategy.drawImage is SO much faster than BufferedImage.drawImage?

I pretty much answered this in my previous post on this thread (bearing in mind that BufferStrategy is most likely using VolatileImage)

Now you have.  Before you didn't mention that BufferStrategy most likely uses a VolatileImage.
Offline davedes
« Reply #43 - Posted 2012-05-06 20:03:25 »

Is that faster than just doing BufferStrategy.drawImage?
I would guess yes... Unless you're copying huge sections, in which case drawImage might take advantage of OpenGL acceleration (assuming it's enabled).

If this kind of performance is necessary, just benchmark it yourself.

My fps dropped by around 250 frames when using your method as compared to just drawImage.
My method wasn't intended to be used every frame. It's just a quick way to copy pixels from one image to another, without dealing with volatile images / video memory / etc.

You've changed your questions so many times that I'm no longer really sure what your current problem is or what you're trying to accomplish...

Offline jonjava
« Reply #44 - Posted 2012-05-06 20:10:16 »

What seems to be the problem here??

Quote
I'm trying to do pixel-by-pixel rendering to an image using for loops.  I have an int array where pixel data is written to, and then, once per frame, that array is rendered to the BufferedImage.

1  
2  
3  
4  
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
         BufferedImage.TYPE_INT_RGB);
   int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer())
         .getData();


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
BufferStrategy s = getBufferStrategy();
      if (s == null) {
         createBufferStrategy(2);
      }

      for (int i = 0; i < pixels.length; i++) {
         pixels[i] = i + (int)System.currentTimeMilllis();
      }

                drawImageAt(10, 20, someImage);

      Graphics g = bs.getDrawGraphics();
      g.drawImage(image, 0, 0, null);
      g.dispose();
      bs.show();


1  
2  
3  
4  
5  
6  
7  
8  
9  
public void drawImageAt(int x, int y, BufferedImage img) {
    int[] pixies = img.getRGB(0,0, img.getWidth(), img.getHeight(), null, 0, img.getWidth() );
   
    for (int yy = 0; yy < img.getHeight(); yy++) {
       for (int xx = 0 ; xx < img.getWidth(); xx++) {
           pixels[ (y+yy) * image.getWidth() + x + xx ] = pixies[y*img.getWidth() + x];
}
}
}

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #45 - Posted 2012-05-06 20:11:02 »

My fps dropped by around 250 frames when using your method as compared to just drawImage.
This statement doesn't mean anything else than 'it got slower'.

If the framerate dropped from 2500fps to 2250fps, that means it took 0.04ms
If the framerate dropped from 300fps to 50fps, that means it took 16.67ms

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: 1 [2]
  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.

Longarmx (45 views)
2014-10-17 03:59:02

Norakomi (36 views)
2014-10-16 15:22:06

Norakomi (27 views)
2014-10-16 15:20:20

lcass (31 views)
2014-10-15 16:18:58

TehJavaDev (61 views)
2014-10-14 00:39:48

TehJavaDev (61 views)
2014-10-14 00:35:47

TehJavaDev (51 views)
2014-10-14 00:32:37

BurntPizza (69 views)
2014-10-11 23:24:42

BurntPizza (39 views)
2014-10-11 23:10:45

BurntPizza (81 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!