Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / [Java] GPU piggybacking?

Author
Message
C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 20th Aug 2011 00:25
Ok, some of you may have seen this thread where I'm making a voxel engine in Java using raycasts and sprites.

Anyway, what I want to ask the experienced minds here is:

Does anyone know of a way to put some of the processing I'm doing with the giant array and the raycasting for loops onto the GPU instead of cramming it all into the CPU?

Basically the principle of my engine is that there is a 1024x1024x128 array and it draws a scene by casting 100x60 rays for all the blobs that get rendered. Each ray is cast with a for loop which maxes out at 512 range. Each ray tests in a direction whether it's hit something and simply returns the voxel type it hit and some lighting info.

This means that for each frame the engine runs, it is potentially having to do 512x100x60 array accesses on top of 100x60 trigonometry calculations (stored in an array as well to cut down processing slightly)

I would very much like a way to utilize the GPU for some of this processing rather than cramming all that through the CPU(s).

Does anyone know if this is even possible? - And if so, can it be done in Java? ?

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 20th Aug 2011 02:20
For a start, you can use a better data structure. One way is to make a series of lower resolution versions of the voxel data, perhaps 1/4 the size in each dimension for each successive layer. The voxel in a lower res one is solid if any of the higher res pixels inside it are solid. When you cast the ray, cast it through the lowest res array first. If you hit a voxel cast against the higher res voxels inside it, etc. etc.

That way you can very quickly skip over empty space, it doesn't use up very much more memory (each layer is 64 times smaller than the one before it), it's still a simple and intuitive data structure, and it's very easy and fast to update the low-res layers if the high-res layers change at all.

[b]
C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 20th Aug 2011 03:15
Hmm, that's gonna require a complete reworking of how my engine works actually. xD

How exactly would you suggest making such a data type? i.e. of varying size?

I understand the concept but I've never really understood how one would code an octree.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Aug 2011 09:45
Not sure if this could help, but JCuda is a java binding for using CUDA. That's about all I know of it.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 20th Aug 2011 13:51
C0wbox, and octree is actually fairly simple. You could make a very simple version like this: (untested, just to give an example of the theory)



"everyone forgets a semi-colon sometimes." - Phaelax
C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 20th Aug 2011 16:43
Aha, yeah that's what I thought of doing - unfortunately due to the poor quality of the computer science course I'm doing at university they haven't taught us about creating trees using OOP.

! Thanks though, I'm now, as usual, about 2 semesters ahead of everyone else on my course. xD

I'll have a go at making Voxeng3 using an octree when I get home after work.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 21st Aug 2011 20:00 Edited at: 21st Aug 2011 20:01
Although it is basically the same as an octree, the method I explained doesn't require a tree data structure, and isn't limited to 8 children per node.

I assume you're currently using 3-dimensional arrays for the voxel data. All you need is a set of 3-dimensional arrays of difference sizes. If you actually used a tree data structure you would waste a lot of memory storing all the references. Each reference is usually 4 bytes, so if you store the 8 children of each voxel you are wasting 32 bytes per voxel.

With the array version you implicitly know the child nodes. If you have two detail levels, say 4x4x4 and 16x16x16, and you want to find the child nodes of the node at (1, 2, 3) in the low detail level. The next level is 4x bigger in each direction, so you just multiply the coordinate by 4, becoming (4, 8, 12) which is the coordinate of the first child node. You know there are always 4x4x4 child nodes for each parent node, so you know that the 64 child nodes are the ones who's coordinates match:
(4 to 7, 8 to 11, 12 to 15)

[b]
Fallout
21
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 21st Aug 2011 20:57
I had a think about this the other day in light of this unlimited engine thing. I thought a possible technique would be to have each pixel represented as a vector projected away from the eye (much like you're thinking). Translate these vectors by the camera position/rotation matrix.

With the octrees people have described above, it should be possible to quickly move through space and find the correct colour for the pixel. Move into the node/chunk the cam is in and essentially check as Zotoaster described. If the colour returned is "transparent" (i.e. no molecules in that chunk), you can move through to the other side of that entire octree node. The slowest scenario for rendering would be when there are lots of tiny chunks so rays are slowed down in nodes/chunks, forced to scan them, but no collision is made. However, vector to box collision is incredibly fast and easy, so it should be quick.

Having said all that, I don't think this is how voxel engines are actually done. I've not read up on them, but this is pretty much raycasting, and my understanding is that voxels don't use ray casting.

Enjoy experimenting anyway. It'll be cool to play around with your own engine.

C0wbox
17
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 21st Aug 2011 23:38
You've both raised some interesting points here. (As it is, I never started making Voxeng3 - might do it if I get a good idea sorted out in my own head.)

@ Diggsey
What you've said all sounds very logical, however, I was thinking of using an octree to save wasted space as well. - With that multiple resolution array idea you've suggested, I'd be using more data than I was in the 1024x1024x128 array. (I'd be using like, 1024x1024x128 + 512x512x64 + 256x256x32 for example.) This would most likely overflow the Java heap, something I'm trying to avoid getting close to doing.

I'd prefer to use an efficient octree (not even necessarily using the object oriented one Zotoaster suggested) to make sure all raycasts were as short as possible as well as ensuring all data is as small as possible (without compression/reworking the voxel engine).

@ Fallout
Yeah I never do things the way they're supposed to be done. - I don't know how to make Wolfenstein3D, but I came close using another method (1D raycasting). I don't know how to make a proper voxel engine, but I came close using another method (2D raycasting). This is just because I don't know how the 3D maths works behind real games and haven't invested much time attempting to learn.

I think though, that real voxel engines use layered alpha-mapped images which get closer and closer to the camera and end up making it look like filled space. Similar to the way this image looks:

I imagine each layer of that is just a splodge of pixels on a 2D image which is then rendered as a plain infront of and behind other splodges to make a 3D shape.

I could do this (badly) but the difficulty then comes in re-rendering the splodges efficiently and calculating the new angles to render from. (I'd also probably use raycasts to be rendering it anyway so it'd render the whole process fairly pointless. xD)

In conclusion:
I'm actually not that good a computer scientist, I don't know how half of the things I see are done. xD

Fallout
21
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 22nd Aug 2011 10:08
Quote: "Similar to the way this image looks"


You mean that greyscale computer generated poo?!

Quote: "I'm actually not that good a computer scientist, I don't know how half of the things I see are done."


Me neither, to be honest. I learn what I have to learn, and don't really like going much deeper than that (hence why I've not looked into voxels much either). I had the same idea as you though! I wanted to give it a go for fun to see how it may work, but I can't justify the tinking time.

I hope you figure something out though. Definitely a good learning experience. I bet you'll learn some techniques applicable to future games to you make that you didn't even expect to learn.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 23rd Aug 2011 03:06 Edited at: 23rd Aug 2011 03:10
Quote: "(I'd be using like, 1024x1024x128 + 512x512x64 + 256x256x32 for example.) This would most likely overflow the Java heap, something I'm trying to avoid getting close to doing."


As I said, if you go down by at least a factor of 4 in each dimension for each layer, you use a tiny amount of extra data. (Less than 1.6% extra)

Each layer is 4x4x4 times smaller than the previous, ie. 64x smaller. If you sum that geometric progression to infinity (1 + 1/64 + 1/64² + 1/64³ + ...), you get 1/(1 - (1 / 64)) = 1.01587302, which is a size increase of 1.587302% if you generate all the levels you can.

Quote: "I'd prefer to use an efficient octree (not even necessarily using the object oriented one Zotoaster suggested) to make sure all raycasts were as short as possible as well as ensuring all data is as small as possible (without compression/reworking the voxel engine)."


Again, an octree will use far, far more memory than simple 3d arrays, since you still have to store the same amount of voxel data, but then you have to store the octree structure on top of that.

[b]

Login to post a reply

Server time is: 2024-04-26 05:53:43
Your offset time is: 2024-04-26 05:53:43