Closed 10110111 closed 5 years ago
This is on purpose because:
To solve this we use different units on CPU and GPU. You need to do the conversion between the two when setting GPU variables from CPU, or in the shader. See for instance https://github.com/ebruneton/precomputed_atmospheric_scattering/blob/2bc8e4124e99bea7026fdd0a3933dd5c35ce6b5c/atmosphere/demo/demo.cc#L342 or https://github.com/ebruneton/precomputed_atmospheric_scattering/blob/2bc8e4124e99bea7026fdd0a3933dd5c35ce6b5c/atmosphere/demo/demo.glsl#L61
The problem is not in different units, but rather in mismatch between the units and the value of the m
constant. It is equivalent to 1 km for the GPU code and to 1 m for the CPU code, although I'd expect m
to stand for "meter".
This doesn't break your original code, but behaves inconsistently when one tries to add a dimensionful constant using m
to denote meters.
For me "m" still means 1 meter in the GPU code. But there is no automatic, transparent unit conversion in this code. This means that you must manually convert values in meters to values in the GPU length unit. In practice, this means dividing by kLengthUnitInMeters.
For instance https://github.com/ebruneton/precomputed_atmospheric_scattering/blob/2bc8e4124e99bea7026fdd0a3933dd5c35ce6b5c/atmosphere/demo/demo.glsl#L61 sets the sphere center at 1 km, and it could have been written with an explicit "m":
const vec3 kSphereCenter = vec3(0.0, 0.0, 1000.0 * m) / kLengthUnitInMeters;
I was trying to add a dimensionful quantity into
functions.glsl
(Earth-Moon distance to experiment with solar eclipse in subsolar point), and it appeared that there's a mismatch between the units in which the GPU model works (supplies the values into the shader) and the units of the CPU model.The particular variables which appear to be out of sync are:
const Length m = 1.0;
insidedefinitions.glsl
(used by GPU model)constexpr Length kLengthUnit = 1.0 * km;
insidereference/model_test.cc
, which is supplied toModel::Model
, which, in turn, does things likestd::to_string(bottom_radius / length_unit_in_meters)
when constructingAtmosphereParameters
for the shader.The result is that the shader code used by the GPU model works in kilometers, but
m
is defined as1.0
, so that e.g. using3*m
in the shader will give you three kilometers instead of three meters.A trivial fix would be to redefine
m
to1e-3
, but this seems to be a fragile solution, since any change tokLengthUnit
will bring this out of sync again.