Skip to main content
gamedev.net
🔒 Locked

[java] VolatileImage question

Started by leif_dragon Dec 12, 2008 at 12:26 PM 7 replies 2.1k views
Original Post
leif_dragon
leif_dragon
I have been reading a lot about the use of volatile image's and how they enable graphics card acceleration/support. I still don't fully understand one thing however. My question is, if I am performing rendering functions on an image in main memory vs a volatile image in graphics card memory, do the rendering operations get performed by different possessors? For example, will the image in main memory use the cpu to perform its rendering operations, while the volatile image in vram (graphics card memory) use the gpu to perform its rendering operations? My main reason for asking is because I am performing a number of lengthy or some what graphically intense rendering operations on certain game images, and I would like to figure out the best way to optimize performance by getting hardware acceleration/support. Throwing them all at the cpu is not good practice. Thanks for any replies. Leif
anemian
anemian
It depends on OS. In Linux just about anything and everything is graphically accelerated even normal images, in Windows only drawing lines that are completely horizontal/vertical and drawing images are graphically accelerated, in Apple's OSs I have no clue.

Because volatile images should only exist in VRAM it'd be interesting to see what the Windows JVM does with it.

But you will notice massive FPS gains if drawing was what was slowing you down IE from 60 FPS up to 100+. That's from someone else's mouth though from http://www.javagaming.org (no I don't remember the post it was in).
The only valid measurement of code quality: WTFs/minute
leif_dragon
leif_dragon
My problem is that I need to modify this particular Image at a pixel level. That is the main reason why I decided to use BufferedImage's in the first place. Is there any way to get hardware support when rendering an image at a pixel level?
anemian
anemian
If you don't do anything to Buffered Images that would require the processor to work on them AND they are a compatible format (GraphicsEnvironment.createCompatibleGraphics or similar, not using new or File.IO). They should eventually end up on the video card especially if you're just drawing the image on the screen.


Granted I can't say I know that for sure or have tested it myself, it's just from reading books and random forums. It's annoying for me to say that but I've focused almost entirely on the business side and am just recently seriously looking at games.
The only valid measurement of code quality: WTFs/minute
leif_dragon
leif_dragon
Thanks for the input anemian. If it is any help to display my situation more fully I will post some source code.

The following code draws a translucent masking image (BufferedImage of type ARGB), using a particular 2-dimensional array that holds the data of the images height-map. The code renders a light value to each pixel using BufferedImage.setRGB() in the masking (BufferedImage img) bufferedImage.
This masking image is then drawn over the actual image, producing a normal mapped surface.

for (int x = 0; x < bumpMap.length; x++){  for (int y = 0; y < bumpMap[x].length; y++)  {    if(x > 0 & x < bumpMap.length - 1)      nX = bumpMap[x+1][y] - bumpMap[x-1][y];    else if(x == 0)      nX = bumpMap[x+1][y];    else      nX = bumpMap[x-1][y];					    if(y > 0 & y < bumpMap[x].length - 1)	      nY = bumpMap[x][y+1] - bumpMap[x][y-1];    else if(y == 0)      nX = bumpMap[x][y+1];    else      nX = bumpMap[x][y-1];						    nX -= x - lightX - radius + offset;    nY -= y - lightY - radius + offset;					    if(nX < 0 || nX > environmentMap.length - 1) 	nX = 0;    if(nY < 0 || nY > environmentMap[nX].length - 1)	nY = 0;					    if(missingP[x][y])    {	if(lightX < 0 || lightX >= missingP.length || lightY < 0 || lightY >= missingP[lightX].length || !missingP[lightX][lightY])	   img.setRGB(x, y, environmentMap[nX][nY]);	else	   img.setRGB(x, y, shadowMap[nX][nY]);    }  }}


The effect produced, is as such.

Photobucket

So I would really like to know if there is any way of producing this effect with hardware acceleration/support. My main goal is to get it so that the graphics card handles all heavy rendering operations such as this.

Please help!
KulSeran
KulSeran
Sounds like what you are looking for is shaders.
Look up GLSL, HLSL, or CG shaders. You can do all sorts of heavy texture manipulation on the pixel level inside a shader. And on the plus side, it only happens for visible pixels (which don't need to 1:1 correspond with texture pixels.)
I know there is a OpenGL API in java, but depending on your application target, you'd have to check if it is available(sorry I don't do much Java stuff, was just trolling the active topics)
Momoko_Fan
Momoko_Fan
I suggest you check out Slick2D:
http://slick.cokeandcode.com/index.php

It uses OpenGL as it's back-end, though it doesn't support shaders now you could request it as a feature :)
Glass_Knife
Glass_Knife
I wish this stuff was easier to understand. Here are links to all the stuff I've read when I was trying to figure this out. There is a lot of information.

BufferedImage as Good as Butter
BufferedImage as Good as Butter, Part II
VolatileImage: Now you See it, Now you Don't
VolatileImage Q&A
ToolkitBufferedVolatileManagedImage Strategies

Another thing you could try is not setting the pixel using setRGB(). If you have an image of 256x256, that's 65536 function calls, which is slow.

Use the following code to get the pixel data...

DataBufferInt buf = (DataBufferInt) bufferedImage.getRaster().getDataBuffer();int[] asdf = buf.getData();


Enjoy!
leif_dragon
leif_dragon
Thanks for the awesome replies guys. I really feel like I'm beginning to know were I am going now.

thanks for the insight, Momoko_Fan and KulSeran,
For some reason I was under the impression that I would be able to simply add some code that would allow my java code to be run by a GPU. I realize now that GPU's support their own language's.. kind of a lame revelation on my part.
Anyways thanks for helping me realize this.

to Glass_Knife,
I have to say that you have been pretty helpful in the number of replies you have contributed to my threads, so thanks for that. Also thanks for the nifty code for RGB retrieval. I actually had already started searching for a more efficient way of pixel manipulation, through RGB integer manipulation of rasters. It's kind of silly how inefficient getRGB() and setRGB() really are.

Thanks all.. and hopefully next time I post, Ill be putting up an announcement about how I have been able to use this to add to my game project. If your interested, check it out.

http://www.gamedev.net/community/forums/topic.asp?topic_id=516788

Merry Christmas!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.