Fix precision errors in pack/unpack depth #12005
Merged
+5
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Current code is introducing precision errors in the pack/unpack depth functions by not using the correct range of values. When the packed_depth is written to R8G8B8A8 texture the maximum value it can store is 255 per channel, not 256. We were introducing errors in the third decimal in a range from 0..1. Seems this code was also wrong in three.js and other libraries, hence the confusion. Coded a godbolt to prove the issue (https://godbolt.org/z/8MrrhPh11).
Link to the original post: https://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
This fix also helps in gl-native terrain occlusion and others.
Tested manually that the problem addresed in this commit is still fixed.
cc: @mapbox/gl-native
Launch Checklist
@mapbox/map-design-team
@mapbox/static-apis
if this PR includes style spec API or visual changes@mapbox/gl-native
if this PR includes shader changes or needs a native portmapbox-gl-js
changelog:<changelog>Fix precision errors in depth pack/unpack</changelog>