-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] support half precision uniforms and half precision samplers #40590
Conversation
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
Requires no changes in the solid_fill shader, on metal we implicitly convert to a HalfVector4. On gles, we don't do the conversion - so it should just work. |
@@ -5,11 +5,11 @@ | |||
#include <impeller/types.glsl> | |||
|
|||
uniform FragInfo { | |||
vec4 color; | |||
f16vec4 color; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this require #extension GL_EXT_shader_16bit_storage : require
up top?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not, I'm not actually using a f16vec4 on glsl, here is the compiled shader:
#version 100
precision mediump float;
precision highp int;
struct FragInfo
{
vec4 color;
};
uniform FragInfo frag_info;
void main()
{
gl_FragData[0] = frag_info.color;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh - but this is all in types.glsl too
impeller/geometry/scalar.h
Outdated
/// @brief Convert a scalar to a half precision float. | ||
/// | ||
/// Adapted from | ||
/// https://developer.android.com/games/optimize/vertex-data-management . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should document the behavior of this and how it handles precision loss, and add some tests that assert it.
Specifically:
- What happens when the
f
is greater than the max finite value or less than the lowest finite value of a half float? - What happens when
f
is infinity/-infinity/nan? - What happens to very tiny values of
f
?
impeller/geometry/vector.h
Outdated
@@ -288,6 +288,43 @@ struct Vector4 { | |||
static_assert(sizeof(Vector3) == 3 * sizeof(Scalar)); | |||
static_assert(sizeof(Vector4) == 4 * sizeof(Scalar)); | |||
|
|||
struct HalfVector4 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could convert Vector3 and others to be TVector3 with template types for floats or half-floats. Then using Vector3 = TVector<Scalar>;
and using HalfVector3 = TVector<Half>
. Will significantly dry up this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one argument against doing this is that we don't really want to use half precision anywhere except for in the shaders.
impeller/geometry/scalar.h
Outdated
@@ -13,6 +14,7 @@ | |||
namespace impeller { | |||
|
|||
using Scalar = float; | |||
using Half = uint16_t; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this different from the _Float16 C11 extension? I also see __fp16
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL
impeller/geometry/vector.h
Outdated
@@ -14,27 +15,46 @@ | |||
|
|||
namespace impeller { | |||
|
|||
struct Vector3 { | |||
template <class T, class U> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it exists this is a really dangerous implementation because TVector a = TVector.SomeMethod() will truncate to zero. We need to replace all of the static cast calls everywhere with Cast
I've removed the TVector specializations. In practice, you can't really do any arithmetic operations on the CPU with half types. And you can't actually use them since I'm exploiting implicit conversions to deal with metal and gles headers being different. |
I really need to test the half conversion. I think I'm going to see if there is some table somewhere I can reference for smoke testing. |
Need to enable #40616 for some shaders to get |
Updated to use hardware conversion and a _Float16 type, which nicely enough has a literal too. Found in https://clang.llvm.org/docs/LanguageExtensions.html and seems to be supported on all relevant platforms |
oh windows. Thankfully we only need this on apple platforms for now |
From some investigation the loss of precision on the solid fill shader is visible in the wonderous app. need to dig deeper. |
nvm typo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, modulo dan being happy too =)
impeller/geometry/half.cc
Outdated
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "half.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(this should be based from the project root too)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops!
Friendly ping:) @dnfield @chinmaygarde |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some nits and I genuinely didn't understand the member width. Its got to be right if the headers are right. But otherwise lgtm.
@@ -2090,5 +2092,38 @@ TEST(GeometryTest, Gradient) { | |||
} | |||
} | |||
|
|||
TEST(GeometryTest, HalfConversions) { | |||
#ifdef FML_OS_WIN | |||
GTEST_SKIP() << "The reason this doesn't work on Windows"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps "Half-precision floats (IEEE 754) are not portable and unavailable on Windows."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could write or use our own library (or a header rather) for doing this portably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, one exists in an earlier commit. however we have as of today no use for it as this is only used on metal backends.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment fixed
}; | ||
|
||
/// @brief A storage only class for half precision floating point vector 4. | ||
struct HalfVector4 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did templating the existing Scalar type vectors not work? This is fine too but seems like we can dry this up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this above and backed it out. The big caveat is that 1) We need implicit constructor conversion to make the headers work 2) we generally don't want to ever use these in the backend besides that as it implies we're dropping precision before we need to. There is no performance improvement to be gained from this. In addition, if we end up needing to emulate _Float16 support for windows we'll likely end up needing to using uint16_t as a storage type and then we'd have to define all of the operators to make that work.
Given all that, I'd really rather just leave them as storage only conversions.
impeller/geometry/half.cc
Outdated
|
||
namespace impeller { | ||
|
||
InternalHalf ScalarToHalf(Scalar f) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this just be inline constexpr in the header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh, this means I can't LOG if you try to use this on windows. Instead I'll make it always return 0 so that it is obviously wrong.
impeller/compiler/reflector.cc
Outdated
@@ -767,6 +773,75 @@ std::vector<StructMember> Reflector::ReadStructMembers( | |||
continue; | |||
} | |||
|
|||
// Tightly packed half Point (vec2). | |||
if (member.basetype == spirv_cross::SPIRType::BaseType::Half && // | |||
member.width == sizeof(float) * 4 && // |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you double check this? The member width doesn't make sense for half base types. I may be misunderstanding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, this was a hack I left in. Should be sizeof(Half) * 8
GetArrayStride<sizeof(HalfVector2)>(struct_type, member, i); | ||
uint32_t element_padding = stride - sizeof(HalfVector2); | ||
result.emplace_back(StructMember{ | ||
"HalfVector2", // type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a function call, but with C++20, you can use aggregate initialization and do this without comments: {.type = "HalfVector2"}
.
…123664) * 1d506dfd7 [Impeller] Allow metal shaders to compile through SPIR-V with openGL semantics. (flutter/engine#40616) * 3287e2b36 [Impeller] support half precision uniforms and half precision samplers (flutter/engine#40590) * 5c1c07fba Revert "Revert "Reland "Default the CanvasKit base URL to local artifacts. (#40293)" (#40470)" (#40700)" (flutter/engine#40717) * 9e4cedc81 Forward stdout and stderr from dart2wasm when verbose. (flutter/engine#40731) * 6941c1820 [Impeller] Allow toggling vulkan validation using a command line test flag. (flutter/engine#40728) * 6ef595829 Roll buildroot to build CanvasKit for speed instead of code size (flutter/engine#40737) * 5b8e02479 [Impeller] Gaussian blur: Add alpha mask specialization (flutter/engine#40707) * 6852bea71 Roll Skia from 7311e9220faf to e3eeabb14e9c (4 revisions) (flutter/engine#40745) * 235e42bea Add an option to malioc_diff.py to print a unified diff (flutter/engine#40732) * 38e6d772f Roll Dart SDK from 1acd1e649fb7 to 69867ba60bb7 (2 revisions) (flutter/engine#40738) * d81c4f2c7 [Impeller] migrate texture fill shaders to half precision. (flutter/engine#40735)
Work towards flutter/flutter#115044
Exploits implicit conversions to use different uniform types on gles vs metal