Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify haze evaluation #10606

Merged
merged 6 commits into from
Apr 22, 2021
Merged

Conversation

rreusser
Copy link
Contributor

@rreusser rreusser commented Apr 22, 2021

This PR greatly simplifies the haze evaluation—both in terms of code and computational expense—with very little change in visual effect.

Preface: I’m well aware of the risk of code-golfing this into oblivion until no one can make sense of it, but I think the following change removes most of the negative aspects @ansis was very legitimately concerned about, while maintaining reasonable motivation and resulting in a much more self-contained and easy to maintain application of this coloring.

Update: I've illustrated the following argument in an interactive notebook

The existing process for computing haze is as follows:

  1. convert the fragment color from srgb to linear
  2. add haze to that color
  3. tonemap to avoid blown out fully saturated rgb values
  4. convert linear back to srgb

The process makes reasonable physical sense but involves a lot of steps and also lead to small frustrations like having to fight against tonemapping subtly darkening colors. It also raises questions like: do we really want ad hoc color space conversion and tonemapping in the middle of a special purpose function?

This PR maintains the above motivation while consolidating the evaluation into a single step.

For varying values of haze h = (0, 0.01, 0.05, 0.25, 0.5), the red curves below illustrate the above four-step process, with input colors on the x-axis and output colors on the y-axis. The tonemapping and color computation functions are given by:

Screen Shot 2021-04-21 at 7 26 23 PM

Screen Shot 2021-04-21 at 7 26 30 PM

(with k = 8.0 in the smooth min tonemapping function). Based on @ansis' observation that we could just scale the curve to pass through (1, 1) exactly, the black lines illustrate the simplified equation (with γ= 2.2):

Screen Shot 2021-04-21 at 7 28 16 PM

https://www.desmos.com/calculator/nhgqknt2w0
gamma-2 2

Finally, we can further fudge the gamma just a bit since exceptional physical accuracy is not needed. If we use γ= 2, pow() calls disappear and we're left with a single square root as the only transcendental function:

Screen Shot 2021-04-21 at 7 28 19 PM

(the x^4 in the denominator is a small touch which prevents the colors from getting too squished together near (1, 1). Without it, they are closer to tangent and visual details is lost.)

gamma-2

Benefits of this approach:

  • calls to exp2, log2, and two calls to pow are reduced to a single sqrt (all repeated 3x for r/g/b)
  • Significantly less shader code
  • avoids the need to pass an extra varying to manage tonemapping
  • Output is strictly lighter than the input, so that we don't have to worry about undesirably darkening colors by tonemapping
  • It follows the original motivation, which I think is reasonable, while eliminating a lot fo the negatives

The difference in appearance is negligible. Darkening of pure whites due to the tonemapping is just about the only difference which can't be compensated for by a very slight change the parameters.

Original:
tonemapped

Simplified:
simplified

(Strictly speaking, I ever so slightly modified the exponent in the cpu-side approximate srgb-linear conversion. The above shows power 2.2. I've PR'd power 2.0. The difference can entirely be compensated for by very slightly changing the input color. The difference does not seem important; I only note it to be precise.)

To be addressed in followup: consolidating and refactoring into a shared vertex/fragment prelude.glsl.

@rreusser rreusser requested review from karimnaaji and ansis April 22, 2021 02:49
@rreusser rreusser changed the title Simplify fog evaluation Simplify haze evaluation Apr 22, 2021
Copy link
Contributor

@karimnaaji karimnaaji left a comment

Choose a reason for hiding this comment

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

Nice simplifications! Did you also do some quick visual checks on styles different than satellite? I think the color tones of satellite is rich enough to cover a lot, but just to confirm.

Left one nit on naming, otherwise 👍

@@ -32,7 +32,7 @@ vec3 fog_position(vec2 pos) {
return fog_position(vec3(pos, 0));
}

void fog_haze(vec3 pos, out float fog_opac, out vec4 haze) {
void fog_haze(vec3 pos, out float fog_opac, out vec3 haze) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticing this now, and not a change from this PR, but did we not name this function appropriately? As the output is both opacity and haze naming it fog_haze seems counterintuitive, it might be an overlook from the optimization when moving things fragment -> vertex.

@rreusser
Copy link
Contributor Author

@karimnaaji I think the only major difference that wouldn't be caught by satellite imagery is that for light styles, this function struggles just a bit more at very bright values since everything gets squeezed to a point at pure white, rather than tonemapping darkening and maintaining a bit more separation between colors as they become brighter. Due to the somewhat lower utility of haze for stylized maps, I think this is an acceptable tradeoff for avoiding frustration with trying to constrain tonemapping to just the fog layer.

@karimnaaji
Copy link
Contributor

@karimnaaji I think the only major difference that wouldn't be caught by satellite imagery is that for light styles, this function struggles just a bit more at very bright values since everything gets squeezed to a point at pure white, rather than tonemapping darkening and maintaining a bit more separation between colors as they become brighter. Due to the somewhat lower utility of haze for stylized maps, I think this is an acceptable tradeoff for avoiding frustration with trying to constrain tonemapping to just the fog layer.

Yes that seems fair, thanks for checking! And I agree that haze really shines with satellite-based imagery.

@rreusser rreusser merged commit 0539f08 into fog-implementation Apr 22, 2021
@rreusser rreusser deleted the ricky/simplify-fog-evaluation branch April 22, 2021 23:23
karimnaaji pushed a commit that referenced this pull request Apr 23, 2021
* Simplify fog evaluation

* Switch to approximate gamma conversion

* Factor out haze application

* Document the haze_apply function

* Remove backticks

* Update src/shaders/_prelude_fog.fragment.glsl
karimnaaji pushed a commit that referenced this pull request May 4, 2021
* Simplify fog evaluation

* Switch to approximate gamma conversion

* Factor out haze application

* Document the haze_apply function

* Remove backticks

* Update src/shaders/_prelude_fog.fragment.glsl
karimnaaji pushed a commit that referenced this pull request May 4, 2021
* Simplify fog evaluation

* Switch to approximate gamma conversion

* Factor out haze application

* Document the haze_apply function

* Remove backticks

* Update src/shaders/_prelude_fog.fragment.glsl
karimnaaji pushed a commit that referenced this pull request May 4, 2021
* Simplify fog evaluation

* Switch to approximate gamma conversion

* Factor out haze application

* Document the haze_apply function

* Remove backticks

* Update src/shaders/_prelude_fog.fragment.glsl
karimnaaji pushed a commit that referenced this pull request May 6, 2021
* Simplify fog evaluation

* Switch to approximate gamma conversion

* Factor out haze application

* Document the haze_apply function

* Remove backticks

* Update src/shaders/_prelude_fog.fragment.glsl
karimnaaji added a commit that referenced this pull request May 7, 2021
* Add fog stylespec

* Add fog type specification

* Add fog stylespec validation

Lint

* Add setFog() and getFog() APIs

* Add debug page

* Add note to validate fog

* Add Fog preludes and inject preprocessor defines when enabled

Fix Lint

Fix Flow

drop: Test Fog define

* Inject shader preludes and connect to globally defined fog uniforms

Fixup

* Apply fog to sky layers

Remove unnecessary shader statement

* Apply fog to terrain render

* Fix fog on sky gradient layers

* Apply fog to fill-extrusion layer

* Apply fog to fill layer

* Apply fog to fill-extrusion-pattern layer

Fixup shader statement

* Apply fog to fill-outline, fill-outline-pattern, fill-pattern layers

* Apply fog to background layer

* rename posMatrix to projMatrix

* Add cameraMatrix

* add separate cameraMatrix

* Calculate worldsize for cameraMatrix based on camera distance distance to sealevel

Fix flow and lint errors

* Simplify shader statement, remove extra division by w
u_cam_matrix is not projective and w is always 1 for points

* Add fading in of fog around 60 deg pitch

Fix lint errors

* Improve fog demo example and add GUI controls

* Fog tile culling in tile cover

Fixup comment

* Remove extraneous mult

Remove unused

* Simplify fog tile culling

Fix lint and flow

* Apply correct fog+blending in shaders (#10496)

* Make an attempt to implement consistent fog logic

* Add comments

* Apply premultiplied fix across shaders

* Clean up gamma correction

* Simply GLSL defines and fix shader compile error

* Fix syntax error and remove unused gamma correction

* Add fog to circles

* Apply fog to background pattern and hillshade

* Clean up a small shader optimization

* Add dithering to fog (#10506)

* Add basic dithering to fog

* Add temporal offset for fog

* Make uniform naming consistent

* Code golf the shaders just a little

* Separate dithering from fog function

* Rework the fog falloff function (#10515)

* Rework the fog falloff function

* Rework the fog function

* Add some spec validation

* Support fog on markers (#10516)

* Apply fog to Markers

* Fix lint flow and cleanup

* Fix opacity not updating for terrain and fog after updating the spec properties

* Add map.getFogOpacity API

* Code style

* Evaluate opacity less often

* Update per rebased changes

* Minor tweak

* Update src/style/fog.js

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/ui/marker.js

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/ui/map.js

Co-authored-by: Ricky Reusser <[email protected]>

* Update variable name per review

* Fixup invalid commit from github

* Address review comments

* Update per review

Co-authored-by: Ricky Reusser <[email protected]>

Fix popup tip not being occluded

* Fog-sky blending (#10527)

* Improve fog blending

* Clean up blending

* Remove fog-sky blending fix

Fix tests and low

* Do not cull tiles crossing over the horizon during fog culling phase
This prevents sudden pop in and out for tiles within the fog culling range

* Use fog color as default context clear color when enabled
This prevents unaffected fragments resulting in empty holes in location
where we do not draw fragments

* Cull at 80% of the distance between fog start end fog end
This leaves a non-noticeable 2% fog opacity change threshold
and reduces the distance before we start culling

* Add style diffing support and unit tests fog setFog and getFog

* Cutoff fog-opacity from the style specification

* Fix a bug using stale tile cover result due missing update to fog ranges
This issue was visible when updating fog properties and an incorrect tile
cover run would potentially leave invalid tiles on the viewport

* Switch fog to use window-scaled units (#10533)

* Switch fog to use window-scaled units

* Update style spec for fog range

* Update fog validation tests to match new limits

* Update fog validations

* Add haziness property to fog (#10537)

* Add haziness to fog

* Fix flow types

* Apply PR feedback

* Update marker opacity to use viewport-scaled height (#10542)

* Update marker opacity to use viewport-scaled height

* add units to variable names to make intent clear

* Code cleanup: Simplify getDistanceToSeaLevel + remove extraneous transform variable

* Add unit tests for furthestTileCorner

* Add unit tests for coveringTiles with fog culling

* Add basic symbol clipping when fog opacity is high (#10541)

* Rename cameraMatrix --> fogMatrix (#10544)

* Offload terrain fog to vertex shader (#10549)

* Offload terrain fog to vertex shader

* Don't set haze uniforms unless haze is present

* Remove haze varying unless haze is used

* Disable fog code path on the 2d case

* Remove redundant uniforms

* Simplify fog render logic

Co-authored-by: Karim Naaji <[email protected]>

* Add documentation of the public facing fog APIs + revise defaults (#10545)

* Add documentation of the public facing APIs

* Fixup missing `}`

* Update fog range docs

* Disable haze by default and fix mixed up haze properties

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/style-spec/reference/v8.json

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/ui/map.js

Co-authored-by: Ricky Reusser <[email protected]>

Co-authored-by: Ricky Reusser <[email protected]>

* Extract getAABBPointSquareDist from fog culling and add unit tests

* Use implicit vec3.transformMat4 division by w instead of explicit one

* Add fog as part of the release testing

* Add fog culling render test

* Add sky-blend render tests

* Add more style diffing unit tests and coverage around setFog/getFog public APIs

* Add unit tests for getFogOpacity

* Add basic terrain render tests

* Add fog marker occlusion unit tests

Remove extraneous note

* Add 2D tests for fog (#10566)

* Add 2D render tests for fog

* Test fog shaders

* Swap raster test

* Add half float heatmap render test image

* Increase test allowance for raster test

* Add more fog style spec render test coverage

* Fix shader preprocessor define leading to invalid shaders on production build variants (#10582)

* Prevent shader stripping issue on production builds
Always use same function signature independently of shader variant

* Use const for the unused param

* Directly use inlined parameter

* Fixup

* Fix render tests
Cannot use const as 'out' or 'inout' parameters.

* Fog style spec changes (#10590)

* Clean up fog style properties

* Present a more reasonable example

* Fix fog tests and validations

* Improve haze wording

* Apply review feedback: getFogOpacity -> queryFogOpacity

* Apply review feedback: Keep seperation of DOM-based and non-DOM based map element updates
Defer marker opacity update in DOM task queue

* Fix fog sky blending (#10578)

* Clean up fog sky blending

* Add fog sky blending fix to cpu side

Fix post rebase flow

* Better consistency in naming conventions

* Apply review feedback: Remove extraneous function call

* Apply review feedback: Rename sky-blend -> horizon-blend

* Apply review feedback: Increase range of pitch fading to be less aggressive

* Simplify haze evaluation (#10606)

* Simplify fog evaluation

* Switch to approximate gamma conversion

* Factor out haze application

* Document the haze_apply function

* Remove backticks

* Update src/shaders/_prelude_fog.fragment.glsl

* Apply review feedback: Consolidate shader code that can be shared in shared prelude

* Remove haze (#10613)

* Remove haze

* Remove density

* Catch a couple more small cleanup items

* Fix broken shader

* Tighten/restore fog culling

* Update src/render/painter.js

* Revert fog opacity regression from #10578 with CPU side evaluation of horizon blend

* Demo: Update default location

* Revise comments and cleanup shader code for readability
- Also removes extraneous branching with min in fog_apply_premultiplied

* Share fog varying globally in new shader prelude

* Revise comments and naming

* Code cleanup for better readability

* Fix validation error on complex style expression of fog color validation

* Fix validation error on complex style expression of fog range validation and add validation tests

* Simplify fog validation checks for expression(s)

* Switch fog from percentage window height to CSS vh units (#10632)

* Switch fog from percentage window height to CSS vh units

* Fix tests

* Fix more tests

* Fix still more tests

* Update src/style-spec/reference/v8.json

* Don't leave alpha of fog color unused (#10633)

* Don't leave alpha of fog color unused

* Apply review comments: Simplify flow, reduce uniforms

* Fix naming

* Update docs

* Fix flow

* Update all render tests to use new units

* Fixup consistent naming

* Add symbol clipping render test

* Sample terrain to estimate de facto camera scale (#10621)

* Continuously adjust the fog scale by raycasting

* Remove unused function

* Fix linter errors

* Name thresholds for clarity

* Hard-code sampling pattern

* Rearrange conditional

* Adjust sampling by horizon line

* Replace arbitrary constant with epsilon

* Remove stray import

* Simplify logic

* Only calc fog matrices on avg elevation change

* Fix cloning of transform

* Fix bug in state management

* Fix linter errors

* Remove debug code

* Move samples points inside function

* Fix accidental call to calcMatrices

* We still love you, flow

* Clarify code

* Add tests for EasedVariable

* Add one more test

* Fix default per latest changes and add render tests

* Fix alpha composing of horizon blend with sky gradient (#10640)

Make sure to un/post multiply alpha before/after applying sky gradient
contribution, as color ramps are premultiplied-alpha colors.  This prevents
washed out colors when using transparency.

* Account for FOV in fog (#10629)

* Account for FOV in fog

* Fix a typo

* Fix flow errors

* Fix a factor of two to make tests pass

* Fix bad merge

* Fix tests

* Shift fog range zero to the map center

* Fix debug

* Fix ranges to account for fov

* Remove unused variable

* Apply review feedback: Directly update marker style opacity

* Update per latest discussion: Use nameless units to prevent confusion
when trying to reason about them. As the units of the range are representing
3d distances on a sphere centered around camera center point, they can
hardly be reasoned about in terms of window height.

* Fixup naming consistency

* Fix race condition in fog terrain sampling (#10642)

* Fix race condition in fog terrain sampling

* Use frame start time stamp

* Update render test expectation

* Remove fog vh units (#10648)

* Remove fog vh units

* Fix floating point errors in test expectations

* Fix geo test

* Add fog demo and transition fixes (#10649)

* Skip unnecessary uniform code path when terrain is rendering to texture
On a typical frame with terrain this was called ~200 times, where only
20 were necessary (~10%)

* Fix invalid transition evaluation
When `delay` was undefined but not `now`, properties.begin and properties.end
were assigned incorrect values, leading to an immediate transition interpolation.
This was noticed when testing transitions on fog.

* Fix style.hasTransition() when fog is transitioning values

* Add a fog demo for release

* Directly use DOM queue rencently introduced by #10530

* Apply review feedback: Directly clear the timer here instead of unsetting it

* Apply review feedback: Fix nits, mark queryFogOpacity private until we
need to expose it publicly

* Update src/util/util.js

Co-authored-by: Ricky Reusser <[email protected]>

* Update src/util/util.js

Co-authored-by: Ricky Reusser <[email protected]>

* Apply review feedback: Fix nits

* Apply review feedback: Simplify matrix initialization

* make private methods private for docs

Co-authored-by: Arindam Bose <[email protected]>
Co-authored-by: Ricky Reusser <[email protected]>
Co-authored-by: Ansis Brammanis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants