Skip to content

Factory.rive: KTX2 (ASTC) images are drawn at the wrong position because the recorded image size is 0×0 #674

Backlog

Description

@xcc3641

Submission checklist

  • I have confirmed the issue is present in the latest version of the rive Flutter package
  • I have searched the documentation and forums and could not find an answer
  • I have searched existing issues and this is not a duplicate

Reproduced on rive 0.15.0-dev.4 / rive_native 0.2.0-dev.4. For 0.15.0-dev.5 / 0.2.0-dev.5 I only checked the source: sniffImageSize is unchanged there, so I expect the same result, but I have not run it.

Description

With Factory.rive, a .riv whose embedded images are KTX2 (ASTC) uploads and samples correctly, and GPU memory drops as expected, but every image that is not a mesh is drawn at the wrong position. Each one is shifted right and down by half its own size, so large parts move further than small ones. Mesh images are placed correctly.

Expected: KTX2 images are positioned the same as the PNG images they replace.

Cause

DeferredFactory::decodeImage (renderer/include/rive/renderer/cmd/deferred_render_factory.hpp) needs the image size at record time. It tries Bitmap::decode, then falls back to sniffImageSize (renderer/src/deferred_cmd.cpp). sniffImageSize knows PNG, GIF, WebP and JPEG headers but not KTX2, so a KTX2 image is recorded as 0×0.

The replay side decodes the same bytes through RenderContext::decodeImage, which does recognise KTX2, so the texture itself is fine. On the record side Image::draw translates by -width * originX, -height * originY using the 0×0 size, so the image is drawn from its origin instead of around it. Meshes do not depend on the image size, which is why they look right.

In a release build nothing is logged, because the "image dims unknown at record time" warning is inside #ifndef NDEBUG.

Since Factory.rive.decodeImage and FileAsset.decode reach the same function, I expect KTX2 bytes supplied through an asset loader to behave the same way. I only tested embedded images.

Reproduction steps

  1. Take a .riv with at least one plain (non-mesh) image node with an origin of 0.5, 0.5.
  2. Replace the embedded PNG bytes with a KTX2 file of the same pixel size, for example one made with KTX-Software from a premultiplied PNG:
    ktx create --format ASTC_4x4_SRGB_BLOCK --assign-tf srgb --generate-mipmap in.png out.ktx2
  3. Load it with File.decode(bytes, riveFactory: Factory.rive) and show it in a RiveWidget on an iOS device.

The image appears shifted by half its width and height.

I cannot attach the files I used because the artwork is not mine to share. I can build a small synthetic .riv if that would help.

Suggested fix

Reading the size from the KTX2 header in sniffImageSize fixes it for me. I rebuilt only deferred_cmd.cpp with this change, replaced that object in the prebuilt librive_pls_renderer.a for iOS, and the same files then render in the right place.

--- a/renderer/src/deferred_cmd.cpp
+++ b/renderer/src/deferred_cmd.cpp
@@ -24,6 +24,21 @@
     h = static_cast<int>(be32(20));
     return true;
   }
+  // KTX2: 12-byte identifier, then vkFormat, typeSize, pixelWidth and
+  // pixelHeight as little-endian u32 at 12/16/20/24.
+  if (n >= 28 && d[0] == 0xAB && d[1] == 'K' && d[2] == 'T' && d[3] == 'X' &&
+      d[4] == ' ' && d[5] == '2' && d[6] == '0' && d[7] == 0xBB)
+  {
+    auto le32 = [&](size_t i) {
+      return static_cast<uint32_t>(d[i]) |
+             (static_cast<uint32_t>(d[i + 1]) << 8) |
+             (static_cast<uint32_t>(d[i + 2]) << 16) |
+             (static_cast<uint32_t>(d[i + 3]) << 24);
+    };
+    w = static_cast<int>(le32(20));
+    h = static_cast<int>(le32(24));
+    return w > 0 && h > 0;
+  }
   // GIF: "GIF87a"/"GIF89a", then width/height little-endian u16 at 6/8.
   if (n >= 10 && d[0] == 'G' && d[1] == 'I' && d[2] == 'F')
   {

Rive Flutter package version

rive 0.15.0-dev.4 with rive_native 0.2.0-dev.4

Flutter version

Flutter 3.47.8 (stable)

Device

iPhone 13 Pro, iOS 27.2.1, Xcode 27. Not tested on Android.

Activity

  1. xcc3641 commented on Nov 26, 2026

    @xcc3641
    Author

    In case it helps anyone else before a fix ships, this is how we applied the change on iOS without rebuilding all of rive_native. Only one object file differs from the published prebuilt libraries.

    1. Copy the rive_native package out of the Pub cache and point the app at the copy, so the shared cache stays untouched:

      # pubspec_overrides.yaml
      dependency_overrides:
        rive_native:
          path: /path/to/rive_native-0.2.0-dev.4-copy

      Keep ios/rive_marker_ios_setup_complete in the copy, otherwise the pod script downloads the prebuilt libraries again and overwrites the change.

    2. Apply the diff above to runtime/renderer/src/deferred_cmd.cpp.

    3. Generate the iOS build files. In the copy:

      mv runtime/renderer/src/shaders/Makefile.rive runtime/renderer/src/shaders/Makefile
      cd native && ./build.sh release ios flutter-runtime

      The rename is what dart run rive_native:setup --build does before it calls build.sh. We did not need the build to finish, only the makefiles under native/out/release/ios/universal/.

    4. Compile just the one file. Print its command and run it:

      cd native/out/release/ios/universal
      make -n -f rive_pls_renderer.make obj/default/rive_pls_renderer/deferred_cmd.o verbose=1

      We ran that command with -flto=full and -fembed-bitcode removed, because the objects in the published librive_pls_renderer.a are plain Mach-O rather than bitcode.

    5. Replace the object in the prebuilt library:

      ar r native/build/iphoneos/bin/release/librive_pls_renderer.a deferred_cmd.o
      ranlib native/build/iphoneos/bin/release/librive_pls_renderer.a

    With that, the same KTX2 files render in the right place on an iPhone 13 Pro.

    Two things got in the way of a full source build on Xcode 27, unrelated to this issue but worth knowing:

    • Project generation runs the shader build, which needs the Metal Toolchain component (xcodebuild -downloadComponent MetalToolchain). The shaders are not needed for this one object, so we stubbed the metal and metallib tools instead of installing it.
    • With the iOS 27.2 SDK, every C++ file fails on "The selected platform is no longer supported by libc++" because the build targets iOS 13 with -Werror. We added -Wno-error for the single file.
  2. alois-rive commented on Nov 26, 2026

    @alois-rive

    Thanks a lot for the report, @xcc3641, and for the detailed investigation and suggested patch. It pointed us straight at the cause.

    You were right: KTX2 images were recorded as 0×0 because the image-size sniffing didn't recognise the KTX2 header, so Image::draw skipped the origin offset. We've fixed it along the lines you suggested, with a few extra checks on the header, and added tests. The fix should ship in an upcoming release soon. We'll update this issue when it's out.

  3. added
    known
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    known

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions