Skip to content

Batched raycast#10634

Closed
arindam1993 wants to merge 100 commits intomainfrom
batched-raycast
Closed

Batched raycast#10634
arindam1993 wants to merge 100 commits intomainfrom
batched-raycast

Conversation

@arindam1993
Copy link
Contributor

Provides an optimized raycast method that can speed up terrain raycasting for multiple rays per frame.
It does this by combining all the rays into a single frustum, and then uses that frustum to determine which tiles need to be considered.

This results in dropping a few tiles before it enters into a O(num_tiles * num_rays) loop.

for (const tile of this._visibleDemTiles) {
if (tile.dem) {
const id = tile.tileID;
const tiles = Math.pow(2.0, id.overscaledZ);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 << id.overscaledZ instead of function call?

return null;
}

raycastBatch(points: Point[]): vec4[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we replace raycast() with raycastBatch(1) defined here or would this one eventually be slower for just one ray?

const yRadius = map.transform.height * 0.33;

// Phyllotaxis: https://www.desmos.com/calculator/duq27u6vof
const n = 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can make n larger if the goal is to stress-test it a bit more.

this._construct(mips, 0, 0, maxLvl, 0);
}

get rootBounds(): [number, number] { return [-aabbSkirtPadding, this.maximums[0]]; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be this.minimums[0] - aabbSkirtPadding in case the terrain is below sea level? Or maybe Math.min(this.minimums[0], 0) - aabbSkirtPadding if the goal is to get it to intersect with elevation = 0?

const treeBounds = tree.rootBounds;

//Z in meters
const minz = treeBounds[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need exaggeration as well, in case it's below sea level?

@karimnaaji karimnaaji force-pushed the fog-implementation branch 3 times, most recently from 69bfce9 to 798728e Compare May 4, 2021 00:31
@astojilj
Copy link
Contributor

astojilj commented May 5, 2021

Where is it going to be used? What is the size of improvement in typical scenario?

@karimnaaji
Copy link
Contributor

@astojilj this gave fairly minimal performance improvement for the use case we had and ended up only using 5 rays, we were imagining using more but the difference in computing average elevation wasn't worth it (refer to. #10621 at https://github.com/mapbox/mapbox-gl-js/pull/10621/files#diff-a2b3330768267b8c7996b8bf292fcd864e8b768f538436813b0d7e19f7202176R336).

So we're not going to go ahead with merging this but will leave it open in case this is needed for other purposes later, as this can be useful whenever we need to do a lot of raycasts at once.

@karimnaaji karimnaaji marked this pull request as draft May 5, 2021 16:01
@arindam1993
Copy link
Contributor Author

Some more stats on what the perf benefit for this specific usecase looked like.
The raw data, logging time for 5 rays cast into the same scene at the same timestamp
Screen Shot 2021-05-03 at 5 35 41 PM
Here y-axis is time in ms, and x axis is each entry in the log.

Since this was super noisy, ran some more tests by scaling up the number of rays and calculating the mean & median of time required
Screen Shot 2021-05-03 at 7 08 10 PM

There isn't much benefit to doing this for what we're doing right now ( 5 rays sampled at 2Hz) , if we ever need to increase the number of rays we can use this to prevent a performance regression.

@karimnaaji karimnaaji force-pushed the fog-implementation branch from 29f0846 to a0d6705 Compare May 6, 2021 15:09
Base automatically changed from fog-implementation to main May 7, 2021 17:44
@ryanhamley
Copy link
Contributor

Closing as stale

@ryanhamley ryanhamley closed this Jan 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants