D3DXIntersect mathod performance

Started by
4 comments, last by JiiPee 11 years, 3 months ago

Does anybody know how optimized D3DXIntersect method is? Is there any sense to create own intersection test algorithm?

See my game dev blog: http://gamedev4hobby.blogspot.fi/
Advertisement
The said function is optimized for the general case - it cannot assume anything about the geometry of the intersected mesh. In addition, it needs to interpret the data from a vertex buffer, which causes at least one level of pointer dereferencing (indirect random memory access). Also, being a dll function (as opposed to a native function in the same module as the caller), calling it requires one additional address mapping operation.

The actual functionality of the intersection calculation is fairly well optimized, though.

If you can make assumptions about the spatial partitioning of the mesh, you can probably write a better function yourself. The basic logic (assuming nothing about the geometry) is as follows:

[font=courier new']Let "intersections" be an empty list of triangle indices and distances from the origin of the probe ray [/font]
[font=courier new']Let "mindist" be a float that is initialized to maximum value of float[/font]
[font=courier new']Let "mintri" be an index of the nearest found triangle so far[/font]

[font=courier new']For each triangle in mesh[/font]
[font=courier new'] Check for intersection between ray and current triangle[/font]
[font=courier new'] If (ray intersects triangle)[/font]
[font=courier new'] {[/font]
[font=courier new'] store current triangle index and intersection distance to "intersections" collection[/font]
[font=courier new'] if (mindist is larger than current intersection distance)[/font]
[font=courier new'] {[/font]
[font=courier new'] set mindist = current intersection distance[/font]
[font=courier new'] set mintri = current triangle[/font]
[font=courier new'] }[/font]
[font=courier new'] }[/font]
[font=courier new']return interections, mindist and mintri; each are potentially empty or unchanged, if intesections were not found[/font]

Niko Suni

Is there any sense to create own intersection test algorithm?
Yes. By creating your own you are not tied to the D3DX library.
Reason enough for me.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Good opportunity for optimizing the intersection algorithm itself is to calculate an axis-aligned bounding box of the mesh in advance; it is then easy to determine whether or not it is even possible that the ray would intersect any of the triangles. Ray-AABB intersection is very fast. The D3DX function does not do this, as it may be expensive to calculate the bounding box itself, and it cannot assume that a given mesh is unchanged between the calls to it (but you can, if it is your mesh and you know it is static).

Niko Suni

Is there any sense to create own intersection test algorithm?
Yes. By creating your own you are not tied to the D3DX library.
Reason enough for me.


L. Spiro

Yes, but sometimes using time to unnecessary optimization is bad considering the project schedule. Or if I'm very smart, I could say bad for the ROI ;)

I try to find reasons why to do or not to do my own test algorithm. If common opinion is, that the DX algorithm suxs, then it's probably smart to create own at very beginning because you have to do it anyway. If the common opinion is, that weel it's ok, then I think I'll stick with it and perhaps create it, if the algorithm seems to be bottle neck.

See my game dev blog: http://gamedev4hobby.blogspot.fi/

Nice answers Nik02. I'll have to consider those options.

See my game dev blog: http://gamedev4hobby.blogspot.fi/

This topic is closed to new replies.

Advertisement