# Cylinder Raytracing

This topic is 4915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been working through the math for raytracing a cylinder and I've come up with 2 options and need to know which is best. I've got one method where to find the determinant (before the sqrt) I've only done 9 multiplies and 2 adds. However for the full calclation it needs 16 multiplications, 5 adds, 1 division, and 1 sqrt. The other method has 11 multiplications and 4 adds before the determinant, but in total it has 12 multiplications, 5 adds, 1 division, and 1 sqrt. So which is best? Faster early exit or faster overall?

##### Share on other sites
In a brute-force renderer where you check all scene objects for a given ray for intersection, the early-exit version would be the one to prefer, because most of the time there wouldn't be an intersection anyway.

But as you're obviously caring for performance you're most likely using some sort of spatial subdivision to speed up the raytracing process. In this case I'd vote for the second method because after scene graph traversal the probability that the cylinder in question is hit by the ray is high.

##### Share on other sites
Makes sense, and in fact I'm using adaptive-depth octrees, so second method it is. Thanks for the info.

##### Share on other sites
It depends on several things. If you're using SSE the div/sqrt combined will cost just as much as a multiply (because rsqrt is just an approximation accurate to 12-bits), if you're not then the div/sqrt will cost more than twice as much as all the other ops combined.

A mis-predicted branch is also very expensive, but not as bad as div or sqrt. So if you can branch to avoid a div and sqrt, it is definitely worth it with non-SSE. With SSE, it is likely that the non-branching version is faster.

When raytracing, spatial subdivision is important because it's like trivially rejecting half your primitives with one tree traversal step (which is generally faster than the ray/prim test). So, everything in perspective, spatial subdiv algo (k-d tree) is more important than speeding up ray/prim test.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015742
×