Commit Graph

65 Commits

Author SHA1 Message Date
Philip Rideout
2e8df10e6e Quaternion slerp: ensure "short path" when falling back to lerp.
I noticed that our slerp function sometimes produces a jolt in
animation, but only when the time delta is very small, and only when the
two operands have completely opposing signs.

For example, let's say you are slerping from <0.76, 0.39, 0.51, 0.19>
to <-0.72, -0.45, -0.49, -0.17>.

These quats are actually quite near to each other because the total
negation of the second quat is similar to the first quat.

We were already doing the short path check in the proper slerp path, but
not when falling back to lerp due to a small angle.
2021-02-01 14:35:05 -08:00
Mathias Agopian
af82aaa0fc remove unused code that causes warnings 2021-01-15 14:38:01 -08:00
Pixelflinger
bfd1de809a Implement screen space cone tracing (SSCT)
This is a technique from Naughty Dog used in TLOU2, where we compute
the "ambient" shadowing of a dominant light in screen-space.

This is currently integrated to the SSAO pass.
2020-09-28 10:53:30 -07:00
Philip Rideout
450fe214f9 Flip the shading normal when det < 0.
This was tested by replacing the node 0 scale in BusterDrone with
[-1, 1, 1].

For future reference, commit f728776 shows when we switched from
transpose(inverse()) to cof(). This was a good change, but before that
particular change, we had a "two wrongs made a right" situation for
mirrored normals.

Fixes #3001.
2020-09-22 16:54:44 -07:00
Pixelflinger
fb42817304 add float versions of math constants to libmath
e.g. `F_PI` is also available as `d::PI` and `f::PI` as `double` or
`float` respectively.

Most of our uses of those constants are `float` and it was error prone
and cumbersome to have to cast to that each time we used them.

The `F_` constants are intended to mimic `math.h`, however these new
constants are not, and all the "n over" constants are named
`N_OVER_` instead of `N_`, which helps avoiding the confusion that e.g.:
`F_2_PI` is actually `2/pi` and not `2*pi`.

Also added `F_TAU` which is 2 * PI.
2020-09-10 15:39:16 -07:00
Romain Guy
2fcca9677d Add Ushimura tone mapping operator
Also fix potential negative values when applying white balance
during color grading.
2020-06-04 15:17:08 -07:00
Romain Guy
23b62e2d12 Add ASC CDL (Color decision list) to color grading (#2635)
A CDL is made of slope/offset/power, and is roughly equivalent
to lift/gamma/gain.
2020-06-03 19:01:22 -07:00
Romain Guy
1aa57fc425 Color grading: add saturation and contrast (#2627)
* Add transforms for ACEScct and enabling/disabling color grading in sample

* Add saturation and contrast adjustments

* Rename ACES to ACES_LEGACY and introduce ACES

ACES_LEGACY is ACES with a brightness boost to match our old
tone mapping operator.
2020-06-02 22:54:00 -07:00
Romain Guy
50adc1f661 Add per-view color grading LUT (#2615)
* Add the ability to set a ColorGrading LUT per View

In this change, material_sandbox allows to change the tone mapping
operator at runtime.

* Add Mathematica notebook used to explore white balance

The implementation is meant to reproduce the temperature/tint sliders
founds in Adobe Lightroom. The temperature can be offset from 2,000K
to 50,000K using a slider between -100 and +100 (-1.0 to 1.0 in our
API). The range of tint was modelled after the range used for the
temperature.

* Fix various issues

- A refactoring wrongly remamed the color grading pass
- Setting a View's ColorGrading to null selects the
  default color grading options

* Implement white balance in ColorGrading
2020-05-31 19:24:42 -07:00
Pixelflinger
68f0ab6312 Implement ACES tone mapping
this leverages the new LUT based tone mapping
2020-05-26 23:46:37 -07:00
Romain Guy
8094955fc1 Add new tool cso-lut (#2394)
* Add new tool cso-lut

This tool is for internal usage only. It computes a 3D lookup table
for cone/sphere occlusion computations. These can be used to implement
specular occlusion, directional ambient occlusion, etc.

* Improve generated comment

* Fix Windows build
2020-04-14 18:22:56 -07:00
Mathias Agopian
4842b1ca13 rework stream operators for libmath
libmath itself doesn't expose any stream operators anymore. However,
libutils is able to automatically print libmath types into its
io::ostream -- however matrices are not formatted nicely.

Added a new optional library, libmathio, that provides std::ostream
operators for all libmath types. libmathio does a better job at
formating matrices.

Also removed apply() and map() from libmath because there were not used
anywhere and they forced us to depend on <functional> in public headers.
2020-03-05 22:50:49 -08:00
Ben Doherty
0990550f45 Fix, mathfwd does not work correctly with MSVC (#2214) 2020-03-05 17:46:21 -08:00
Mathias Agopian
651ccb183e New mathfwd.h header
math/mathwfd.h forward declares all {mat|vec}{2|3|4}<> classes,
which allows us to remove their respective #include in a lot of
our public headers.
Our math headers are full of templates, so this should help build times
a bit.

Also we want to keep the public headers as minimalist as possible.
2020-02-12 15:30:13 -08:00
Philip Rideout
898ed70aa0 Remove <iostream> from math headers.
This speeds up our build time by using <iosfwd> and un-inlining the
vector print functions.
2020-01-06 15:09:06 -08:00
Romain Guy
8e00df1de5 Add Color::absorptionAtDistance() to compute absorption for refraction. (#1964)
Specifying the transmittance color at a specific distance is more
user-friendly than specifying absorption coefficients directly.
2019-12-10 18:20:23 -08:00
Ben Doherty
1061d5c5fa Fix MSVC 2019 16.4 build (#1943) 2019-12-10 13:34:48 -08:00
Mathias Agopian
faa82ff35a implement diag() in TMatSquareFunctions
this requires using 'auto' as the return type.
2019-10-21 14:27:40 -07:00
Mathias Agopian
c9fcedb40b some optimizations to the math code (#1779)
- we use by-value parameters in some places, which helps the compiler
  (not sure why, but assuming references can be aliased and the
   compiler is cautious about that).
  This helps matrix multiplies (which is almost never inlined) and
  transpose (but that's almost always inlined and merged with
  something else, so the gain here is not completely real)

- remove support for matrices-of-matrices because this wasn't complete
  anyways.

- improve matrix * scalar by inlining the loop manually instead of
  going through *=, this saves some extra writes.
2019-10-15 17:22:54 -07:00
Ben Doherty
e8641ce257 Additional fixes for MSVC (#1765) 2019-10-10 16:45:06 -07:00
Philip Rideout
da13067b57 libmath: prevent NaN's in slerp due to acos.
Fixes #1749.
2019-10-08 15:37:48 -07:00
Mathias Agopian
03c2a90206 noexcept-ize libmath a bit move
this gets rid of a few ide-warnings about static initialization of
arrays with math types.
2019-10-03 17:12:35 -07:00
Romain Guy
a4a1bf07aa Use our own constants instead of M_PI, M_*, etc. (#1733)
These constants are not part of the standard. We instead use our own
constexpr definitions in the filament::math namespace, as part of
the scalar.h include.
2019-10-02 15:31:38 -07:00
Philip Rideout
16a5adcfde Fix quat slerp so it takes shortest path.
Fixes #1716.

This regressed in c65e561c59.
2019-10-01 16:22:00 -07:00
Gregory Popovitch
d5c0d11404 Final changes for building with msvc 2019 on Windows (#1681) 2019-09-30 14:18:06 -07:00
Mathias Agopian
16d307e543 enforce that quaternions are made of arithmetic types
I believe this might fix some msvc compiler issues too.
2019-09-26 19:05:33 -07:00
Mathias Agopian
5ed07639af workaround an MSVC bug with brace initialization
the C++ standard says:

   if T is a class type with a default constructor that is neither
   user-provided nor deleted (that is, it may be a class with an
   implicitly-defined or defaulted default constructor), the object
   is zero-initialized and then it is default-initialized if it has a
   non-trivial default constructor

Unfortunately, MSVC always calls the default constructor, even if it
is trivial, which breaks constexpr-ness.
To workaround this, we're always zero-initializing TVecN<>

Also removed constexpr from default constructors, since they never can
be constexpr as they're not initializing the vector.
2019-09-26 19:05:33 -07:00
Mathias Agopian
b509ef4dad handle scalar op vector directly to help some compilers 2019-09-25 22:39:13 -07:00
Mathias Agopian
f0ad12ce2e Fix matrix operations when using mixed precision
This is a similar fix to the previous vector fix. Commutative 
operations now always return the same type and the operations are
carried out in the precision resulting from following traditional
C++ rules.
2019-09-18 18:22:05 -07:00
Mathias Agopian
21acf53d3f improve vector operations when using implicit conversion
It used to be that operations e.g. like:

 float3{} + double{} would be computed as
 float3{} + float3{double{}} instead of
 float3{} + double3{double{}}

I other words, when an implicit conversion was involved on the right
it would be converted to the left side’s type, possibly losing 
precision.

Another problem was that swiping the operands could produce different
Results, e.g.:

   float3{1} * 5.0 -> float3{5.0f}
   5.0 * float3{1} -> double3{5.0}


This is no longer the case, now both expressions would return a double3. 

Note:

float3 r{};
r *= 5;

Is now equivalent to:

r[0] *= 5;
r[1] *= 5;
r[2] *= 5;

Instead of before:

r[0] *= 5.0f;
r[1] *= 5.0f;
r[2] *= 5.0f;
2019-09-18 18:22:05 -07:00
Mathias Agopian
2f5927d531 Minor code clean-up 2019-09-18 18:22:05 -07:00
Mathias Agopian
cf950a8d6f improve mixed-precision vector operations
We now follow the same rules than for basic types, when performing
mixed-precision operations, e.g.:

  float3 + double3 -> double3
  double3 + float3 -> double3
  dot(float3, double3) -> double

In particular, e.g. swapping the arguments to arithmetic operations
now always yields to the same result type (before, float3 + double3
would not return the same type than double3 + float3).
2019-09-18 18:22:05 -07:00
Mathias Agopian
9c8d0ed5a1 handle basis inverting transforms (e.g. planar symmetries)
We now inverse the face winding order based on the determinant of
each render primitive world's transform. This prevents front/back faces
to be reversed when using a left-handed basis, after transform.

Fix #1520
2019-09-17 18:38:52 -07:00
Mathias Agopian
c981b52832 use enable_if<> instead of static_assert()
This is nicer because this way the methods that don't match don't
even exist. Also make it better when editing code with a C++ aware
ide.

Also use the less verbose std::enable_if_t<>
2019-09-16 18:13:21 -07:00
Mathias Agopian
67f75f0ecf reformatting 2019-08-09 15:07:12 -07:00
Mathias Agopian
f728776714 better normal transforms
We use the cofactors to transform normals, which preserves its
direction, unlike of the inverse-transpose method.
2019-08-09 15:07:12 -07:00
Mathias Agopian
10259f80f4 Add support for det() and cof()
Respectively computing the determinant and cofactors of a matrix.
2019-08-09 15:07:12 -07:00
Mathias Agopian
58b7084c8b make libmath much more constexpr friendly
It turns out that most of libmath couldn't be used in constexpr
expression due to our use of union{}. The C++ standard requires that
all accesses to a union{} in a constexpr expression be the same
element.

Also because libm and cmath are not constexpr some functions such
as length() or normalize() can't be constexpr. The same is true for
anything needing things like sqrt, cos, sin, ceil, floor.

This change mainly does the following:
- replace all accesses to vector elements by operator[]
  (this ensure all of libmath uses the same union element)

- avoid use of std::min / std::max / std::abs

- avoid uninitialized variables, which can't be constexpr

- remove 'constexpr' keyword on functions that can never be

It is now possible to write things like:

    constexpr mat4f I = inverse(
            transpose(mat4f::translation(float3{ 1, 2, 3 }) 
                 * mat4f::scaling(4)));
2019-07-18 16:48:37 -07:00
Mathias Agopian
2e4a825fec clamp correctly when converting to RGB_11_11_10
This avoids artifacts with unclipped HDR environments.
2019-07-11 22:45:49 -07:00
Ben Doherty
80f3afaac6 Manually define PI to fix MSVC issue (#1388) 2019-07-10 16:23:26 -07:00
Mathias Agopian
801c5b57a5 math::half is an arithmetic type even on msvc 2019-07-09 15:45:18 -07:00
Mathias Agopian
8cd44fb2e8 rework math::half to be more generic
We introduce a templated fp<> class that can represent any float
format and convert to/from float32.
2019-06-19 12:22:01 -07:00
Mathias Agopian
22be5f17b4 conversion between vec<T> and vec<U> are now implicit
This change allows for e.g. to call a method that takes double4 with
float4 parameters -- just like it would work with double and floats.
2019-04-19 17:31:37 -07:00
Mathias Agopian
e63282c9a2 removed unnecessary arithmetic check 2019-04-19 17:31:37 -07:00
Mathias Agopian
7795de4ac2 added vec{2|3|4}<T>
We can now use for e.g. vec4<float> instead of float4, this is
useful for templated code. The existing solution was to reach
out to the math::detail::TVec<> class.
2019-04-19 17:31:37 -07:00
Mathias Agopian
beb2f74bdc Better frustum/box intersection, make code more robust
- switched the frustum/box intersection code to double. With large 
scenes, we were getting very wrong intersection points (up to 1m off).
This doesn't seem to happen with doubles. 

- reject points that end-up outside the frustum

- made LiSPSM code more robust against the problem above. When points
were falsely placed behind the near plane (due to bad intersections),
we could end-up with NaN (sqrt of negative numbers).
2019-04-17 18:12:31 -07:00
Mathias Agopian
45fdece973 Fix documentation typos 2019-04-08 16:42:21 -07:00
Philip Rideout
10251821c6 filagui: Introduce widget for manipulating vectors.
This is our first ImGui extension, it draws a draggable 3D arrow for
manipulating a unit vector. This is especially useful for tweaking the
light direction.
2019-04-04 10:59:38 -07:00
Mathias Agopian
2f34f9f857 remove vector comparison operators
they're dangerous as they will make things like
std::min() work, but probably won't do the
right thing.
2019-03-14 13:13:57 -07:00
Romain Guy
6b39569c14 Defend against Windows' near/far defines (#985) 2019-03-14 11:49:43 -07:00