Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] support half precision uniforms and half precision samplers #40590

Merged
merged 28 commits into from
Mar 28, 2023

Conversation

jonahwilliams
Copy link
Member

Work towards flutter/flutter#115044

Exploits implicit conversions to use different uniform types on gles vs metal

@flutter-dashboard
Copy link

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.

@jonahwilliams
Copy link
Member Author

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;
Copy link
Contributor

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?

Copy link
Member Author

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;
}

Copy link
Member Author

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

/// @brief Convert a scalar to a half precision float.
///
/// Adapted from
/// https://developer.android.com/games/optimize/vertex-data-management .
Copy link
Contributor

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?

@@ -288,6 +288,43 @@ struct Vector4 {
static_assert(sizeof(Vector3) == 3 * sizeof(Scalar));
static_assert(sizeof(Vector4) == 4 * sizeof(Scalar));

struct HalfVector4 {
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh good idea

Copy link
Member Author

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.

@@ -13,6 +14,7 @@
namespace impeller {

using Scalar = float;
using Half = uint16_t;
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

TIL

@@ -14,27 +15,46 @@

namespace impeller {

struct Vector3 {
template <class T, class U>
Copy link
Member Author

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

@jonahwilliams
Copy link
Member Author

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.

@jonahwilliams
Copy link
Member Author

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.

@jonahwilliams
Copy link
Member Author

Need to enable #40616 for some shaders to get float16sampler2d working.

@jonahwilliams
Copy link
Member Author

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

@jonahwilliams
Copy link
Member Author

oh windows. Thankfully we only need this on apple platforms for now

@jonahwilliams jonahwilliams changed the title [Impeller] support f16vec4 in shaders and use in solid color uniform [Impeller] support half precision uniforms and half precision samplers Mar 25, 2023
@jonahwilliams
Copy link
Member Author

From some investigation the loss of precision on the solid fill shader is visible in the wonderous app. need to dig deeper.

@jonahwilliams
Copy link
Member Author

nvm typo

Copy link
Member

@gaaclarke gaaclarke left a 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 =)

// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "half.h"
Copy link
Member

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)

Copy link
Member Author

Choose a reason for hiding this comment

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

oops!

@jonahwilliams jonahwilliams requested a review from dnfield March 28, 2023 03:14
@jonahwilliams
Copy link
Member Author

Friendly ping:) @dnfield @chinmaygarde

Copy link
Member

@chinmaygarde chinmaygarde left a 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";
Copy link
Member

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."

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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 {
Copy link
Member

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.

Copy link
Member Author

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.


namespace impeller {

InternalHalf ScalarToHalf(Scalar f) {
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member Author

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.

@@ -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 && //
Copy link
Member

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.

Copy link
Member Author

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
Copy link
Member

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"}.

@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 28, 2023
@auto-submit auto-submit bot merged commit 3287e2b into flutter:main Mar 28, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 28, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 29, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 29, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 29, 2023
zanderso pushed a commit to flutter/flutter that referenced this pull request Mar 29, 2023
…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)
CaseyHillers added a commit that referenced this pull request Mar 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App e: impeller needs tests
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants