Skip to content

Conversation

@jonahwilliams
Copy link
Contributor

@jonahwilliams jonahwilliams commented Feb 18, 2025

Fixes #163315
Fixes #163521
Fixes #142641

OpenGL has an inverted coordinate system (bottom left is zero) compared to Metal/Vulkan (top left is zero). We handle this by rendering things upside down on OpenGL. Unfortunately this can leak out of the renderer via readback (toImage), so we need to make sure to undo the inversion.

This is not performed for the "TextureCoordinateSystem::kUploadFromHost" state as that indicates the texture is already "right side up".

@github-actions github-actions bot added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests labels Feb 18, 2025
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.

A suggestion to use just one call to use ReadPixels with an inplace swap to avoid potential driver shenanigans. Perhaps its pinning memory weirdly.

OTOH, feel free to ignore. ReadPixels is slow either way.

case TextureCoordinateSystem::kRenderToTexture:
// The texture is upside down, and must be inverted when copying
// byte data out.
size_t offset = destination_offset;
Copy link
Member

Choose a reason for hiding this comment

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

I have nothing to provide as evidence for this other than perhaps an irrational fear that multiple calls to ReadPixels will make some driver do the most insane thing possible. But can we read in one shot and then flip the image in place after instead?

Iterate over the rows picking two from each end and std::swap_ranges on each perhaps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a reasonable fear, but I also doubt my ability to correctly use std::swap_ranges so it might be 6 of 1 and half a dozen of another...

Copy link
Member

Choose a reason for hiding this comment

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

static void FlipImage(uint8_t* buffer,
                      size_t width,
                      size_t height,
                      size_t stride) {
  if (buffer == nullptr || stride == 0) {
    return;
  }

  const auto byte_width = width * stride;

  for (size_t top = 0; top < height; top++) {
    size_t bottom = height - top - 1;
    if (top >= bottom) {
      break;
    }
    auto* top_row = buffer + byte_width * top;
    auto* bottom_row = buffer + byte_width * bottom;
    std::swap_ranges(top_row, top_row + byte_width, bottom_row);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will update the PR to use this.

case TextureCoordinateSystem::kRenderToTexture:
// The texture is upside down, and must be inverted when copying
// byte data out.
FlipImage(data + destination_offset, 4, source_region.GetHeight(),
Copy link
Member

@chinmaygarde chinmaygarde Feb 18, 2025

Choose a reason for hiding this comment

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

These arguments aren't right. The stride goes last. It's the root buffer address, image width, image height, and the pixel stride.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, lol. The order doesn't matter since its its multiplied. Anyway, not how I meant the arguments would be interpreted 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops...

But thanks to associativity it is mathematically equivalent 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@flutter-dashboard
Copy link

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #163501 at sha ceafbaa

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label Feb 19, 2025
@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 19, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Feb 19, 2025
Merged via the queue into flutter:master with commit 408cbaf Feb 19, 2025
175 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Feb 19, 2025
jonahwilliams pushed a commit to jonahwilliams/flutter that referenced this pull request Feb 19, 2025
…ure readback. (flutter#163501)

Fixes flutter#163315
Fixes flutter#163521
Fixes flutter#142641

OpenGL has an inverted coordinate system (bottom left is zero) compared
to Metal/Vulkan (top left is zero). We handle this by rendering things
upside down on OpenGL. Unfortunately this can leak out of the renderer
via readback (toImage), so we need to make sure to undo the inversion.

This is not performed for the "TextureCoordinateSystem::kUploadFromHost"
state as that indicates the texture is already "right side up".
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 21, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 21, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 21, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 21, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 21, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 22, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 22, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 22, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 22, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 23, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 23, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 24, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 24, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 24, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 24, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 24, 2025
auto-submit bot pushed a commit that referenced this pull request Feb 26, 2025
…texture readback. (#163501) (#163667)

Fixes #163315 Fixes #163521 Fixes #142641

OpenGL has an inverted coordinate system (bottom left is zero) compared to Metal/Vulkan (top left is zero). We handle this by rendering things upside down on OpenGL. Unfortunately this can leak out of the renderer via readback (toImage), so we need to make sure to undo the inversion.

This is not performed for the "TextureCoordinateSystem::kUploadFromHost" state as that indicates the texture is already "right side up".
@reidbaker reidbaker mentioned this pull request Apr 4, 2025
9 tasks
Fintasys pushed a commit to Fintasys/flutter that referenced this pull request May 14, 2025
…texture readback. (flutter#163501) (flutter#163667)

Fixes flutter#163315 Fixes flutter#163521 Fixes flutter#142641

OpenGL has an inverted coordinate system (bottom left is zero) compared to Metal/Vulkan (top left is zero). We handle this by rendering things upside down on OpenGL. Unfortunately this can leak out of the renderer via readback (toImage), so we need to make sure to undo the inversion.

This is not performed for the "TextureCoordinateSystem::kUploadFromHost" state as that indicates the texture is already "right side up".
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels. will affect goldens Changes to golden files

Projects

None yet

2 participants