Sign in to follow this  
Vertex333

Mouse selection in 3D scene offloaded to GPU

Recommended Posts

Well, would it a) be possible and B) useful to use the vertex data and calculate the mouse point intersection on GPU (direct compute or pixel shader,...) (the intention is to save CPU Power and don't need to calculate vertex stuff a second time on the CPU for mouse selection if everything gets usually done on the vertex shader).

I think it may be a problem that there is a large delay (but maybe this can be reduced if after every frame the calculation gets done) + not only a single point gets calculated but a field around the last mouse position (lets say 100x100) gets calcluated, the next mouse position simply exists until the next frame updates the 100x100 matrix or we get out of the 100x100 field (maybe performance is large enough that we could calculate the whole screen resolution and simply only upload a small area to the CPU/RAM).

What are your thoughts?

If it is possible and useful, how would you implement it?

Thx,
Vertex

Share this post


Link to post
Share on other sites
Highly unlikely that it would be useful, unless it's a very special case. The easiest way to use the GPU for checking what the mouse points at, is to draw everything to an offscreen render target where each object gets its own color. Then you simply read the pixel under the mouse and the color will tell you which object you clicked. This is much faster than trying to make the GPU calculate intersections with actual geometry.

Share this post


Link to post
Share on other sites
So, it should cost more CPU + GPU to do this additional pass than using only CPU (I mean using instancing and stuff like this it is much work to do everything a second time on the CPU, from a programmers view)? I for myself think that it offloads work to GPU + you don't need to change 2 code bases if you change vertex calculation/creation. In my case I have simple quads (only few vertices) and lots of instances (matrices via buffer). calculating that should cost more than doing a second pass on the gpu (not tested or implemented, just my thoughts).

I am really asking myself why it is not common to do this on the GPU in an additional pass.

Vertex

Share this post


Link to post
Share on other sites
Sounds like a perfect fit for drawing the quads in different colors and reading back the color. It is a simple method with excellent results when per-triangle or per-object picking is desired. It also requires almost no extra code, no intersection-tests whatsoever, and is very efficient as the GPU gets to do what the GPU does best.

What you are suggesting is using the GPU to instead do an intersection calculation for each primitive. This is certainly possible, but it's unlikely to be faster than drawing that primitive, and there's still the matter of where to put the results. The GPU processes things in parallel, so if you get an intersection with 5 triangles, you need to store all those 5 true-results and then somehow choose which one to pick. You probably could do something like that without too much difficulty, for example in the geometry shader per primitive, and draw the result to a 1-pixel render-target and let the depth-testing discard the wrong results, but I doubt it would be faster than rendering the whole scene with single-colored triangles.

[quote name='Vertex333' timestamp='1298156735' post='4776469']
I am really asking myself why [b]it is not common to do this on the GPU[/b] in an additional pass.
[/quote]

If you've established it's not common, then the reason is probably that there is no known simple or efficient way of accomplishing it. It can certainly be done, but again it's unlikely to be an optimal solution.

Share this post


Link to post
Share on other sites
Getting the raw data to the GPU is only half the battle. Reading back the result (so that you can do something useful and/or interesting with it) is the other half, and that is what will trip you up. Even though readback speeds are very fast on modern hardware, you will still need to stall the pipeline in order to do so, and this will wreck your rendering performance. Unless you factor in an allowance for some lag (e.g. it may be acceptable to wait a few frames before doing the readback) the GPU is just not a suitable tool for what you want to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this