diff --git a/.github/workflows/smoke_render.yml b/.github/workflows/smoke_render.yml index 2e6ce81..f823faa 100644 --- a/.github/workflows/smoke_render.yml +++ b/.github/workflows/smoke_render.yml @@ -11,10 +11,15 @@ on: - cron: "0 7 * * *" # daily, 07:00 UTC jobs: - macos: - name: macOS (Flutter master) - runs-on: macos-latest - timeout-minutes: 12 + linux: + name: linux (Flutter master) + runs-on: ubuntu-latest + timeout-minutes: 20 + env: + # No GPU on the runner; force Mesa's software rasterizers (GL llvmpipe, + # Vulkan lavapipe) so Impeller has a backend under Xvfb. + LIBGL_ALWAYS_SOFTWARE: "1" + GALLIUM_DRIVER: llvmpipe steps: - uses: actions/checkout@v4 - uses: subosito/flutter-action@v2 @@ -22,15 +27,22 @@ jobs: channel: master - name: Flutter and engine revision run: flutter --version - - run: flutter config --enable-native-assets + - name: Install Linux desktop build deps + software GL/Vulkan + Xvfb + run: | + sudo apt-get update + sudo apt-get install -y \ + clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev \ + xvfb libgl1-mesa-dri libegl-mesa0 libgles2 mesa-vulkan-drivers + - run: flutter config --enable-linux-desktop --enable-native-assets - run: flutter pub get - name: Render smoke scenes working-directory: examples/smoke_render run: > + xvfb-run -a -s "-screen 0 1280x1024x24" flutter drive --driver=test_driver/integration_test.dart --target=integration_test/smoke_test.dart - -d macos --enable-impeller --enable-flutter-gpu + -d linux --enable-impeller --enable-flutter-gpu - name: Upload to Argos id: argos if: ${{ !cancelled() }} @@ -38,7 +50,7 @@ jobs: env: ARGOS_TOKEN: ${{ secrets.ARGOS_TOKEN }} run: | - out=$(npx --yes @argos-ci/cli upload build/smoke --build-name macos 2>&1) || true + out=$(npx --yes @argos-ci/cli upload build/smoke --build-name linux 2>&1) || true echo "$out" url=$(printf '%s' "$out" | grep -oE 'https://app\.argos-ci\.com/[^[:space:]]+' | head -1) echo "build_url=$url" >> "$GITHUB_OUTPUT" @@ -50,7 +62,7 @@ jobs: run: | [ -z "$DISCORD_WEBHOOK_URL" ] && exit 0 RUN_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" - CONTENT="Smoke render failed (macOS, Flutter master). Run: $RUN_URL" + CONTENT="Smoke render failed (linux, Flutter master). Run: $RUN_URL" [ -n "$ARGOS_URL" ] && CONTENT="$CONTENT | Argos: $ARGOS_URL" curl -sf -H "Content-Type: application/json" \ -d "{\"content\": \"$CONTENT\"}" "$DISCORD_WEBHOOK_URL" || true diff --git a/examples/smoke_render/.metadata b/examples/smoke_render/.metadata index 5017a28..d3ade79 100644 --- a/examples/smoke_render/.metadata +++ b/examples/smoke_render/.metadata @@ -4,7 +4,7 @@ # This file should be version controlled and should not be manually edited. version: - revision: "b5cf21f90aece859cd4f33812b2b75538bbde9b4" + revision: "72d04ce778634d7e6c2d383174c9a086f2b8bfba" channel: "[user-branch]" project_type: app @@ -13,11 +13,11 @@ project_type: app migration: platforms: - platform: root - create_revision: b5cf21f90aece859cd4f33812b2b75538bbde9b4 - base_revision: b5cf21f90aece859cd4f33812b2b75538bbde9b4 - - platform: macos - create_revision: b5cf21f90aece859cd4f33812b2b75538bbde9b4 - base_revision: b5cf21f90aece859cd4f33812b2b75538bbde9b4 + create_revision: 72d04ce778634d7e6c2d383174c9a086f2b8bfba + base_revision: 72d04ce778634d7e6c2d383174c9a086f2b8bfba + - platform: linux + create_revision: 72d04ce778634d7e6c2d383174c9a086f2b8bfba + base_revision: 72d04ce778634d7e6c2d383174c9a086f2b8bfba # User provided section diff --git a/examples/smoke_render/integration_test/smoke_test.dart b/examples/smoke_render/integration_test/smoke_test.dart index f6aceee..0347d3b 100644 --- a/examples/smoke_render/integration_test/smoke_test.dart +++ b/examples/smoke_render/integration_test/smoke_test.dart @@ -20,16 +20,6 @@ void main() { // pumpWidget) touches baseShaderLibrary, which throws on web if touched // before initialization completes. await Scene.initializeStaticResources(); - // ignore: avoid_print - print('SMOKEDBG ${smoke.id}: init done'); - - // Bare frame with no flutter_scene content: isolates whether the - // headless runner pumps frames at all (vsync) from scene rendering. - await tester.pumpWidget( - const MaterialApp(home: ColoredBox(color: Color(0xFF202020))), - ); - // ignore: avoid_print - print('SMOKEDBG ${smoke.id}: bare pumped'); await tester.pumpWidget( MaterialApp( @@ -40,23 +30,17 @@ void main() { ), ), ); - // ignore: avoid_print - print('SMOKEDBG ${smoke.id}: pumped'); // Let the post-ready repaint and GPU frames settle. for (var i = 0; i < 20; i++) { await tester.pump(const Duration(milliseconds: 50)); await Future.delayed(const Duration(milliseconds: 50)); } - // ignore: avoid_print - print('SMOKEDBG ${smoke.id}: settled'); final boundary = smokeSceneKey.currentContext!.findRenderObject() as RenderRepaintBoundary; final ui.Image image = await boundary.toImage(pixelRatio: 1.0); - // ignore: avoid_print - print('SMOKEDBG ${smoke.id}: toImage done'); final png = (await image.toByteData(format: ui.ImageByteFormat.png))!; final rgba = (await image.toByteData(format: ui.ImageByteFormat.rawRgba))!; @@ -92,9 +76,15 @@ void main() { greaterThan(20), reason: 'foreground is ~black; lighting or textures may have broken', ); + // A loose backstop against a flat/uniform fill; coverage and foreground + // luma above are the primary blank detectors. Kept low because this + // metric is noisy (dominated by the anti-aliased clear/geometry edge) + // and software rasterizers (e.g. llvmpipe on the Linux CI) produce far + // fewer distinct values than hardware, especially for a low-roughness + // metallic surface reflecting a smooth environment. expect( stats.distinctColors, - greaterThan(100), + greaterThan(24), reason: 'frame looks uniform; possible blank render', ); }); diff --git a/examples/smoke_render/lib/smoke_scenes.dart b/examples/smoke_render/lib/smoke_scenes.dart index 24814a3..e4a6702 100644 --- a/examples/smoke_render/lib/smoke_scenes.dart +++ b/examples/smoke_render/lib/smoke_scenes.dart @@ -127,13 +127,9 @@ class _SmokeSceneViewState extends State { @override void initState() { super.initState(); - // ignore: avoid_print - print('SMOKEDBG ${widget.scene.id}: initState building scene'); final setup = widget.scene.setup(); _scene = setup.scene; _camera = setup.camera; - // ignore: avoid_print - print('SMOKEDBG ${widget.scene.id}: initState scene built'); // The first paint happens before flutter_scene's static resources finish // loading and is skipped; this view is otherwise static, so trigger one @@ -170,11 +166,7 @@ class _SmokePainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { - // ignore: avoid_print - print('SMOKEDBG paint: before scene.render'); scene.render(camera, canvas, viewport: Offset.zero & size); - // ignore: avoid_print - print('SMOKEDBG paint: after scene.render'); } @override diff --git a/examples/smoke_render/linux/.gitignore b/examples/smoke_render/linux/.gitignore new file mode 100644 index 0000000..d3896c9 --- /dev/null +++ b/examples/smoke_render/linux/.gitignore @@ -0,0 +1 @@ +flutter/ephemeral diff --git a/examples/smoke_render/linux/CMakeLists.txt b/examples/smoke_render/linux/CMakeLists.txt new file mode 100644 index 0000000..2d82193 --- /dev/null +++ b/examples/smoke_render/linux/CMakeLists.txt @@ -0,0 +1,128 @@ +# Project-level configuration. +cmake_minimum_required(VERSION 3.13) +project(runner LANGUAGES CXX) + +# The name of the executable created for the application. Change this to change +# the on-disk name of your application. +set(BINARY_NAME "smoke_render") +# The unique GTK application identifier for this application. See: +# https://wiki.gnome.org/HowDoI/ChooseApplicationID +set(APPLICATION_ID "com.example.smoke_render") + +# Explicitly opt in to modern CMake behaviors to avoid warnings with recent +# versions of CMake. +cmake_policy(SET CMP0063 NEW) + +# Load bundled libraries from the lib/ directory relative to the binary. +set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") + +# Root filesystem for cross-building. +if(FLUTTER_TARGET_PLATFORM_SYSROOT) + set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT}) + set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +endif() + +# Define build configuration options. +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + set(CMAKE_BUILD_TYPE "Debug" CACHE + STRING "Flutter build mode" FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Profile" "Release") +endif() + +# Compilation settings that should be applied to most targets. +# +# Be cautious about adding new options here, as plugins use this function by +# default. In most cases, you should add new options to specific targets instead +# of modifying this function. +function(APPLY_STANDARD_SETTINGS TARGET) + target_compile_features(${TARGET} PUBLIC cxx_std_14) + target_compile_options(${TARGET} PRIVATE -Wall -Werror) + target_compile_options(${TARGET} PRIVATE "$<$>:-O3>") + target_compile_definitions(${TARGET} PRIVATE "$<$>:NDEBUG>") +endfunction() + +# Flutter library and tool build rules. +set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") +add_subdirectory(${FLUTTER_MANAGED_DIR}) + +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) + +# Application build; see runner/CMakeLists.txt. +add_subdirectory("runner") + +# Run the Flutter tool portions of the build. This must not be removed. +add_dependencies(${BINARY_NAME} flutter_assemble) + +# Only the install-generated bundle's copy of the executable will launch +# correctly, since the resources must in the right relative locations. To avoid +# people trying to run the unbundled copy, put it in a subdirectory instead of +# the default top-level location. +set_target_properties(${BINARY_NAME} + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run" +) + + +# Generated plugin build rules, which manage building the plugins and adding +# them to the application. +include(flutter/generated_plugins.cmake) + + +# === Installation === +# By default, "installing" just makes a relocatable bundle in the build +# directory. +set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) +endif() + +# Start with a clean build bundle directory every time. +install(CODE " + file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") + " COMPONENT Runtime) + +set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") +set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") + +install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES}) + install(FILES "${bundled_library}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endforeach(bundled_library) + +# Copy the native assets provided by the build.dart from all packages. +set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/") +install(DIRECTORY "${NATIVE_ASSETS_DIR}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +# Fully re-copy the assets directory on each build to avoid having stale files +# from a previous install. +set(FLUTTER_ASSET_DIR_NAME "flutter_assets") +install(CODE " + file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") + " COMPONENT Runtime) +install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" + DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) + +# Install the AOT library on non-Debug builds only. +if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") + install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endif() diff --git a/examples/smoke_render/linux/flutter/CMakeLists.txt b/examples/smoke_render/linux/flutter/CMakeLists.txt new file mode 100644 index 0000000..d5bd016 --- /dev/null +++ b/examples/smoke_render/linux/flutter/CMakeLists.txt @@ -0,0 +1,88 @@ +# This file controls Flutter-level build steps. It should not be edited. +cmake_minimum_required(VERSION 3.10) + +set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") + +# Configuration provided via flutter tool. +include(${EPHEMERAL_DIR}/generated_config.cmake) + +# TODO: Move the rest of this into files in ephemeral. See +# https://github.com/flutter/flutter/issues/57146. + +# Serves the same purpose as list(TRANSFORM ... PREPEND ...), +# which isn't available in 3.10. +function(list_prepend LIST_NAME PREFIX) + set(NEW_LIST "") + foreach(element ${${LIST_NAME}}) + list(APPEND NEW_LIST "${PREFIX}${element}") + endforeach(element) + set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE) +endfunction() + +# === Flutter Library === +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) +pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) +pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) + +set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so") + +# Published to parent scope for install step. +set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) +set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) +set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) +set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE) + +list(APPEND FLUTTER_LIBRARY_HEADERS + "fl_basic_message_channel.h" + "fl_binary_codec.h" + "fl_binary_messenger.h" + "fl_dart_project.h" + "fl_engine.h" + "fl_json_message_codec.h" + "fl_json_method_codec.h" + "fl_message_codec.h" + "fl_method_call.h" + "fl_method_channel.h" + "fl_method_codec.h" + "fl_method_response.h" + "fl_plugin_registrar.h" + "fl_plugin_registry.h" + "fl_standard_message_codec.h" + "fl_standard_method_codec.h" + "fl_string_codec.h" + "fl_value.h" + "fl_view.h" + "flutter_linux.h" +) +list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/") +add_library(flutter INTERFACE) +target_include_directories(flutter INTERFACE + "${EPHEMERAL_DIR}" +) +target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}") +target_link_libraries(flutter INTERFACE + PkgConfig::GTK + PkgConfig::GLIB + PkgConfig::GIO +) +add_dependencies(flutter flutter_assemble) + +# === Flutter tool backend === +# _phony_ is a non-existent file to force this command to run every time, +# since currently there's no way to get a full input/output list from the +# flutter tool. +add_custom_command( + OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} + ${CMAKE_CURRENT_BINARY_DIR}/_phony_ + COMMAND ${CMAKE_COMMAND} -E env + ${FLUTTER_TOOL_ENVIRONMENT} + "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" + ${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE} + VERBATIM +) +add_custom_target(flutter_assemble DEPENDS + "${FLUTTER_LIBRARY}" + ${FLUTTER_LIBRARY_HEADERS} +) diff --git a/examples/smoke_render/linux/flutter/generated_plugin_registrant.cc b/examples/smoke_render/linux/flutter/generated_plugin_registrant.cc new file mode 100644 index 0000000..e71a16d --- /dev/null +++ b/examples/smoke_render/linux/flutter/generated_plugin_registrant.cc @@ -0,0 +1,11 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + + +void fl_register_plugins(FlPluginRegistry* registry) { +} diff --git a/examples/smoke_render/linux/flutter/generated_plugin_registrant.h b/examples/smoke_render/linux/flutter/generated_plugin_registrant.h new file mode 100644 index 0000000..e0f0a47 --- /dev/null +++ b/examples/smoke_render/linux/flutter/generated_plugin_registrant.h @@ -0,0 +1,15 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void fl_register_plugins(FlPluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/smoke_render/linux/flutter/generated_plugins.cmake b/examples/smoke_render/linux/flutter/generated_plugins.cmake new file mode 100644 index 0000000..2e1de87 --- /dev/null +++ b/examples/smoke_render/linux/flutter/generated_plugins.cmake @@ -0,0 +1,23 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +list(APPEND FLUTTER_FFI_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin}) + target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin) + list(APPEND PLUGIN_BUNDLED_LIBRARIES $) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) +endforeach(plugin) + +foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/linux plugins/${ffi_plugin}) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries}) +endforeach(ffi_plugin) diff --git a/examples/smoke_render/linux/runner/CMakeLists.txt b/examples/smoke_render/linux/runner/CMakeLists.txt new file mode 100644 index 0000000..e97dabc --- /dev/null +++ b/examples/smoke_render/linux/runner/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.13) +project(runner LANGUAGES CXX) + +# Define the application target. To change its name, change BINARY_NAME in the +# top-level CMakeLists.txt, not the value here, or `flutter run` will no longer +# work. +# +# Any new source files that you add to the application should be added here. +add_executable(${BINARY_NAME} + "main.cc" + "my_application.cc" + "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" +) + +# Apply the standard set of build settings. This can be removed for applications +# that need different build settings. +apply_standard_settings(${BINARY_NAME}) + +# Add preprocessor definitions for the application ID. +add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") + +# Add dependency libraries. Add any application-specific dependencies here. +target_link_libraries(${BINARY_NAME} PRIVATE flutter) +target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) + +target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") diff --git a/examples/smoke_render/linux/runner/main.cc b/examples/smoke_render/linux/runner/main.cc new file mode 100644 index 0000000..e7c5c54 --- /dev/null +++ b/examples/smoke_render/linux/runner/main.cc @@ -0,0 +1,6 @@ +#include "my_application.h" + +int main(int argc, char** argv) { + g_autoptr(MyApplication) app = my_application_new(); + return g_application_run(G_APPLICATION(app), argc, argv); +} diff --git a/examples/smoke_render/linux/runner/my_application.cc b/examples/smoke_render/linux/runner/my_application.cc new file mode 100644 index 0000000..4bdf446 --- /dev/null +++ b/examples/smoke_render/linux/runner/my_application.cc @@ -0,0 +1,148 @@ +#include "my_application.h" + +#include +#ifdef GDK_WINDOWING_X11 +#include +#endif + +#include "flutter/generated_plugin_registrant.h" + +struct _MyApplication { + GtkApplication parent_instance; + char** dart_entrypoint_arguments; +}; + +G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) + +// Called when first Flutter frame received. +static void first_frame_cb(MyApplication* self, FlView* view) { + gtk_widget_show(gtk_widget_get_toplevel(GTK_WIDGET(view))); +} + +// Implements GApplication::activate. +static void my_application_activate(GApplication* application) { + MyApplication* self = MY_APPLICATION(application); + GtkWindow* window = + GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); + + // Use a header bar when running in GNOME as this is the common style used + // by applications and is the setup most users will be using (e.g. Ubuntu + // desktop). + // If running on X and not using GNOME then just use a traditional title bar + // in case the window manager does more exotic layout, e.g. tiling. + // If running on Wayland assume the header bar will work (may need changing + // if future cases occur). + gboolean use_header_bar = TRUE; +#ifdef GDK_WINDOWING_X11 + GdkScreen* screen = gtk_window_get_screen(window); + if (GDK_IS_X11_SCREEN(screen)) { + const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); + if (g_strcmp0(wm_name, "GNOME Shell") != 0) { + use_header_bar = FALSE; + } + } +#endif + if (use_header_bar) { + GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); + gtk_widget_show(GTK_WIDGET(header_bar)); + gtk_header_bar_set_title(header_bar, "smoke_render"); + gtk_header_bar_set_show_close_button(header_bar, TRUE); + gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); + } else { + gtk_window_set_title(window, "smoke_render"); + } + + gtk_window_set_default_size(window, 1280, 720); + + g_autoptr(FlDartProject) project = fl_dart_project_new(); + fl_dart_project_set_dart_entrypoint_arguments( + project, self->dart_entrypoint_arguments); + + FlView* view = fl_view_new(project); + GdkRGBA background_color; + // Background defaults to black, override it here if necessary, e.g. #00000000 + // for transparent. + gdk_rgba_parse(&background_color, "#000000"); + fl_view_set_background_color(view, &background_color); + gtk_widget_show(GTK_WIDGET(view)); + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); + + // Show the window when Flutter renders. + // Requires the view to be realized so we can start rendering. + g_signal_connect_swapped(view, "first-frame", G_CALLBACK(first_frame_cb), + self); + gtk_widget_realize(GTK_WIDGET(view)); + + fl_register_plugins(FL_PLUGIN_REGISTRY(view)); + + gtk_widget_grab_focus(GTK_WIDGET(view)); +} + +// Implements GApplication::local_command_line. +static gboolean my_application_local_command_line(GApplication* application, + gchar*** arguments, + int* exit_status) { + MyApplication* self = MY_APPLICATION(application); + // Strip out the first argument as it is the binary name. + self->dart_entrypoint_arguments = g_strdupv(*arguments + 1); + + g_autoptr(GError) error = nullptr; + if (!g_application_register(application, nullptr, &error)) { + g_warning("Failed to register: %s", error->message); + *exit_status = 1; + return TRUE; + } + + g_application_activate(application); + *exit_status = 0; + + return TRUE; +} + +// Implements GApplication::startup. +static void my_application_startup(GApplication* application) { + // MyApplication* self = MY_APPLICATION(object); + + // Perform any actions required at application startup. + + G_APPLICATION_CLASS(my_application_parent_class)->startup(application); +} + +// Implements GApplication::shutdown. +static void my_application_shutdown(GApplication* application) { + // MyApplication* self = MY_APPLICATION(object); + + // Perform any actions required at application shutdown. + + G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application); +} + +// Implements GObject::dispose. +static void my_application_dispose(GObject* object) { + MyApplication* self = MY_APPLICATION(object); + g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); + G_OBJECT_CLASS(my_application_parent_class)->dispose(object); +} + +static void my_application_class_init(MyApplicationClass* klass) { + G_APPLICATION_CLASS(klass)->activate = my_application_activate; + G_APPLICATION_CLASS(klass)->local_command_line = + my_application_local_command_line; + G_APPLICATION_CLASS(klass)->startup = my_application_startup; + G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown; + G_OBJECT_CLASS(klass)->dispose = my_application_dispose; +} + +static void my_application_init(MyApplication* self) {} + +MyApplication* my_application_new() { + // Set the program name to the application ID, which helps various systems + // like GTK and desktop environments map this running application to its + // corresponding .desktop file. This ensures better integration by allowing + // the application to be recognized beyond its binary name. + g_set_prgname(APPLICATION_ID); + + return MY_APPLICATION(g_object_new(my_application_get_type(), + "application-id", APPLICATION_ID, "flags", + G_APPLICATION_NON_UNIQUE, nullptr)); +} diff --git a/examples/smoke_render/linux/runner/my_application.h b/examples/smoke_render/linux/runner/my_application.h new file mode 100644 index 0000000..db16367 --- /dev/null +++ b/examples/smoke_render/linux/runner/my_application.h @@ -0,0 +1,21 @@ +#ifndef FLUTTER_MY_APPLICATION_H_ +#define FLUTTER_MY_APPLICATION_H_ + +#include + +G_DECLARE_FINAL_TYPE(MyApplication, + my_application, + MY, + APPLICATION, + GtkApplication) + +/** + * my_application_new: + * + * Creates a new Flutter-based application. + * + * Returns: a new #MyApplication. + */ +MyApplication* my_application_new(); + +#endif // FLUTTER_MY_APPLICATION_H_ diff --git a/examples/smoke_render/macos/Runner/MainFlutterWindow.swift b/examples/smoke_render/macos/Runner/MainFlutterWindow.swift index d358f8d..3cc05eb 100644 --- a/examples/smoke_render/macos/Runner/MainFlutterWindow.swift +++ b/examples/smoke_render/macos/Runner/MainFlutterWindow.swift @@ -11,14 +11,5 @@ class MainFlutterWindow: NSWindow { RegisterGeneratedPlugins(registry: flutterViewController) super.awakeFromNib() - - // On a headless CI runner the app launches in the background. macOS then - // marks the window occluded and Flutter's embedder pauses the display - // link, so no frames are produced and the integration test hangs waiting - // on tester.pump()/toImage(). Force the app active and the window - // frontmost so its occlusion state stays visible and frames keep flowing. - NSApp.setActivationPolicy(.regular) - NSApp.activate(ignoringOtherApps: true) - self.orderFrontRegardless() } } diff --git a/packages/flutter_scene/lib/src/material/material.dart b/packages/flutter_scene/lib/src/material/material.dart index b9d3c54..f79ae59 100644 --- a/packages/flutter_scene/lib/src/material/material.dart +++ b/packages/flutter_scene/lib/src/material/material.dart @@ -8,6 +8,7 @@ import 'package:flutter_scene/src/material/environment.dart'; import 'package:flutter_scene/src/material/physically_based_material.dart'; import 'package:flutter_scene/src/material/unlit_material.dart'; import 'package:flutter_scene/src/importer/flatbuffer.dart' as fb; +import 'package:flutter_scene/src/render/y_flip.dart'; /// Base class for shading a [MeshPrimitive]. /// @@ -199,7 +200,9 @@ abstract class Material { Lighting lighting, ) { pass.setCullMode(doubleSided ? gpu.CullMode.none : gpu.CullMode.backFace); - pass.setWindingOrder(gpu.WindingOrder.counterClockwise); + // backendWinding wraps the default winding for the GLES render-target + // Y-flip workaround (see y_flip.dart); identity on Metal/Vulkan/web. + pass.setWindingOrder(backendWinding(gpu.WindingOrder.counterClockwise)); } /// Whether geometry rendered with this material is fully opaque. diff --git a/packages/flutter_scene/lib/src/material/physically_based_material.dart b/packages/flutter_scene/lib/src/material/physically_based_material.dart index f866966..30d0935 100644 --- a/packages/flutter_scene/lib/src/material/physically_based_material.dart +++ b/packages/flutter_scene/lib/src/material/physically_based_material.dart @@ -293,11 +293,12 @@ class PhysicallyBasedMaterial extends Material { fragInfo[129] = light?.shadowDepthBias ?? 0.0; fragInfo[130] = light?.shadowNormalBias ?? 0.0; fragInfo[131] = light == null ? 0.0 : 1.0 / light.shadowMapResolution; - // Render-to-texture targets (the shadow map, the prefiltered-radiance - // atlas) sample top-down on Metal/Vulkan and bottom-up on OpenGL ES. - // Flutter GPU has no backend query; offscreen-MSAA support is a proxy - // (true on Metal/Vulkan, false on OpenGL ES). - fragInfo[132] = gpu.gpuContext.doesSupportOffscreenMSAA ? 1.0 : 0.0; + // render_target_flip_y: flips V when sampling render-to-texture targets + // (the shadow map, the prefiltered-radiance atlas). flutter_scene now + // stores those top-down on every backend (Metal/Vulkan natively; OpenGL + // ES via the vertex-stage Y-flip workaround, see y_flip.dart), so the + // top-down sampling value (1.0) is correct everywhere. + fragInfo[132] = 1.0; fragInfo[133] = alphaMode.index.toDouble(); fragInfo[134] = alphaCutoff; fragInfo[135] = light?.shadowFadeRange ?? 0.0; diff --git a/packages/flutter_scene/lib/src/material/shader_material.dart b/packages/flutter_scene/lib/src/material/shader_material.dart index ab7bb99..99b7e5a 100644 --- a/packages/flutter_scene/lib/src/material/shader_material.dart +++ b/packages/flutter_scene/lib/src/material/shader_material.dart @@ -4,6 +4,7 @@ import 'package:flutter_scene/src/gpu/gpu.dart' as gpu; import 'package:flutter_scene/src/light.dart'; import 'package:flutter_scene/src/material/material.dart'; +import 'package:flutter_scene/src/render/y_flip.dart'; /// A [Material] backed by a caller-supplied fragment shader. /// @@ -176,7 +177,9 @@ class ShaderMaterial extends Material { Lighting lighting, ) { pass.setCullMode(cullingMode); - pass.setWindingOrder(windingOrder); + // backendWinding wraps the winding for the GLES render-target Y-flip + // workaround (see y_flip.dart); identity on Metal/Vulkan/web. + pass.setWindingOrder(backendWinding(windingOrder)); for (final entry in _uniformBlocks.entries) { pass.bindUniform( diff --git a/packages/flutter_scene/lib/src/render/env_prefilter.dart b/packages/flutter_scene/lib/src/render/env_prefilter.dart index 44cf6eb..ae8b2c3 100644 --- a/packages/flutter_scene/lib/src/render/env_prefilter.dart +++ b/packages/flutter_scene/lib/src/render/env_prefilter.dart @@ -3,6 +3,7 @@ import 'dart:typed_data'; import 'package:flutter_scene/src/gpu/gpu.dart' as gpu; import 'package:vector_math/vector_math.dart'; +import 'package:flutter_scene/src/render/y_flip.dart'; import 'package:flutter_scene/src/shaders.dart'; /// Number of roughness bands in a prefiltered-radiance atlas (band 0 = @@ -80,6 +81,7 @@ gpu.Texture prefilterEquirectRadiance( coordinateSystem: gpu.TextureCoordinateSystem.renderToTexture, ); + final vertexShader = baseShaderLibrary['FullscreenVertex']!; final fragmentShader = baseShaderLibrary['PrefilterEnvFragment']!; final commandBuffer = gpu.gpuContext.createCommandBuffer(); final renderPass = commandBuffer.createRenderPass( @@ -88,12 +90,16 @@ gpu.Texture prefilterEquirectRadiance( ), ); renderPass.bindPipeline( - gpu.gpuContext.createRenderPipeline( - baseShaderLibrary['FullscreenVertex']!, - fragmentShader, - ), + gpu.gpuContext.createRenderPipeline(vertexShader, fragmentShader), ); renderPass.bindVertexBuffer(_fullscreenQuadView, 6); + // Vertex-stage Y-flip so the atlas is stored top-down on backends that + // need it (the OpenGL ES workaround; see y_flip.dart). + final flipInfo = Float32List(4)..[0] = backendYFlipSign; + renderPass.bindUniform( + vertexShader.getUniformSlot('FlipInfo'), + gpu.gpuContext.createHostBuffer().emplace(ByteData.sublistView(flipInfo)), + ); renderPass.bindTexture( fragmentShader.getUniformSlot('source_equirect'), sourceEquirect, diff --git a/packages/flutter_scene/lib/src/render/shadow_encoder.dart b/packages/flutter_scene/lib/src/render/shadow_encoder.dart index aaab6e2..8f6541a 100644 --- a/packages/flutter_scene/lib/src/render/shadow_encoder.dart +++ b/packages/flutter_scene/lib/src/render/shadow_encoder.dart @@ -2,6 +2,7 @@ import 'package:flutter_scene/src/gpu/gpu.dart' as gpu; import 'package:vector_math/vector_math.dart'; import 'package:flutter_scene/src/render/render_scene.dart'; +import 'package:flutter_scene/src/render/y_flip.dart'; import 'package:flutter_scene/src/shaders.dart'; /// Process-lifetime cache of depth-pass render pipelines, keyed by vertex @@ -26,6 +27,9 @@ class ShadowEncoder { this._lightSpaceMatrix, ) { frustum = Frustum.matrix(_lightSpaceMatrix); + // Matrix sent to the vertex shader; carries the GLES render-to-texture + // Y-flip (see y_flip.dart). Frustum culling keeps the unflipped one. + _shaderLightSpaceMatrix = applyBackendYFlip(_lightSpaceMatrix); _renderPass.setDepthWriteEnable(true); _renderPass.setColorBlendEnable(false); _renderPass.setDepthCompareOperation(gpu.CompareFunction.lessEqual); @@ -33,12 +37,15 @@ class ShadowEncoder { // that are visible cast shadows; a depth bias on the receiver handles // self-shadow acne. _renderPass.setCullMode(gpu.CullMode.backFace); - _renderPass.setWindingOrder(gpu.WindingOrder.counterClockwise); + _renderPass.setWindingOrder( + backendWinding(gpu.WindingOrder.counterClockwise), + ); } final gpu.RenderPass _renderPass; final gpu.HostBuffer _transientsBuffer; final Matrix4 _lightSpaceMatrix; + late final Matrix4 _shaderLightSpaceMatrix; static final gpu.Shader _depthShader = baseShaderLibrary['DepthOnlyFragment']!; @@ -87,13 +94,17 @@ class ShadowEncoder { _renderPass, _transientsBuffer, item.worldTransform * instanceTransform, - _lightSpaceMatrix, + _shaderLightSpaceMatrix, _cameraPositionPlaceholder, ); final flip = item.windingFlipped != (instanceTransform.determinant() < 0); _renderPass.setWindingOrder( - flip ? gpu.WindingOrder.clockwise : gpu.WindingOrder.counterClockwise, + backendWinding( + flip + ? gpu.WindingOrder.clockwise + : gpu.WindingOrder.counterClockwise, + ), ); _renderPass.draw(); } @@ -104,15 +115,17 @@ class ShadowEncoder { _renderPass, _transientsBuffer, item.worldTransform, - _lightSpaceMatrix, + _shaderLightSpaceMatrix, _cameraPositionPlaceholder, ); // Mirrored casters reverse winding; flip the cull order so the same faces // that are visible also cast shadows. _renderPass.setWindingOrder( - item.windingFlipped - ? gpu.WindingOrder.clockwise - : gpu.WindingOrder.counterClockwise, + backendWinding( + item.windingFlipped + ? gpu.WindingOrder.clockwise + : gpu.WindingOrder.counterClockwise, + ), ); _renderPass.draw(); } diff --git a/packages/flutter_scene/lib/src/render/tonemap_pass.dart b/packages/flutter_scene/lib/src/render/tonemap_pass.dart index 04d50f2..7d5344f 100644 --- a/packages/flutter_scene/lib/src/render/tonemap_pass.dart +++ b/packages/flutter_scene/lib/src/render/tonemap_pass.dart @@ -4,6 +4,7 @@ import 'package:flutter_scene/src/gpu/gpu.dart' as gpu; import 'package:flutter_scene/src/render/render_graph.dart'; import 'package:flutter_scene/src/render/scene_pass.dart'; +import 'package:flutter_scene/src/render/y_flip.dart'; import 'package:flutter_scene/src/shaders.dart'; import 'package:flutter_scene/src/tone_mapping.dart'; @@ -63,16 +64,24 @@ class TonemapPass extends RenderGraphPass { renderPass.bindPipeline(pipeline); renderPass.bindVertexBuffer(_quadView, 6); + // Vertex-stage Y-flip: stores this pass's output top-down on backends + // that need it (the OpenGL ES workaround; see y_flip.dart). + final flipInfo = Float32List(4); + flipInfo[0] = backendYFlipSign; + renderPass.bindUniform( + _vertexShader.getUniformSlot('FlipInfo'), + context.transientsBuffer.emplace(ByteData.sublistView(flipInfo)), + ); + // TonemapInfo std140: { float exposure; float tone_mapping_mode; - // float flip_y; float pad; }. flip_y compensates for the render-to- - // texture Y orientation of the HDR target, which differs by backend - // (the OpenGL ES FBO is bottom-up). Flutter GPU has no backend query; - // we use offscreen-MSAA support as a proxy (true on Metal/Vulkan, - // false on OpenGL ES). + // float flip_y; float pad; }. flip_y flips the V coordinate when + // sampling the HDR target. With the vertex-stage flip above, every + // backend's HDR target is now stored top-down, so no sampling flip is + // needed (0.0). Kept as a uniform for the shader contract. final info = Float32List(4); info[0] = _exposure; info[1] = _toneMappingMode.index.toDouble(); - info[2] = gpu.gpuContext.doesSupportOffscreenMSAA ? 0.0 : 1.0; + info[2] = 0.0; renderPass.bindUniform( _fragmentShader.getUniformSlot('TonemapInfo'), context.transientsBuffer.emplace(ByteData.sublistView(info)), diff --git a/packages/flutter_scene/lib/src/render/y_flip.dart b/packages/flutter_scene/lib/src/render/y_flip.dart new file mode 100644 index 0000000..14e2c4f --- /dev/null +++ b/packages/flutter_scene/lib/src/render/y_flip.dart @@ -0,0 +1,130 @@ +import 'dart:typed_data'; +import 'dart:ui' as ui; + +import 'package:flutter_scene/src/gpu/gpu.dart' as gpu; +import 'package:flutter_scene/src/shaders.dart'; +import 'package:vector_math/vector_math.dart'; + +// TEMPORARY render-to-texture Y-flip workaround for the OpenGL ES backend. +// +// flutter_scene (like Impeller's Metal and Vulkan backends) assumes +// render-to-texture content is stored top-down. Impeller's OpenGL ES backend +// stores it bottom-up, and the upstream fix that makes GLES match +// (flutter/flutter#186556) is not yet in the engines we build against. So on +// GLES, flutter_scene must absorb the difference itself: negate gl_Position.y +// in every offscreen pass (storing top-down) and invert cull winding to +// compensate (the Y negation reverses screen-space winding). +// +// Flutter GPU exposes no backend query, and capability flags don't reliably +// separate GLES from Vulkan (both use standard formats; offscreen-MSAA support +// is true on GLES3/llvmpipe). So instead of guessing, [probeBackendYFlip] +// measures the orientation directly: it renders a known top/bottom pattern to +// an offscreen texture and reads it back. The web (WebGL2) shim does its own +// equivalent flip at the shim layer, so the probe measures top-down there too +// and this workaround stays off for it. +// +// TODO(flutter_scene): remove this once the GLES render-to-texture top-down +// fix (flutter/flutter#186556 or equivalent) is in the supported engines. +// See: https://github.com/bdero/flutter_scene/issues/145. + +bool _flipsRenderTargetY = false; +bool _probed = false; + +/// Whether this backend stores render-to-texture content bottom-up and so +/// needs flutter_scene's Y-flip. Defaults to `false` until [probeBackendYFlip] +/// has measured it; the renderers read this each frame. +bool get backendFlipsRenderTargetY => _flipsRenderTargetY; + +/// Sign to multiply `gl_Position.y` by in the full-screen passes' vertex +/// shader: -1 to flip when [backendFlipsRenderTargetY], +1 otherwise. +/// Matrix-based passes premultiply [applyBackendYFlip] instead. +double get backendYFlipSign => _flipsRenderTargetY ? -1.0 : 1.0; + +/// Clip-space Y-flip premultiplied into the camera/light matrix sent to the +/// vertex shaders when [backendFlipsRenderTargetY], so `gl_Position.y` is +/// negated. Identity otherwise. The returned matrix is for the shader only; +/// frustum culling must keep using the unflipped transform. +Matrix4 applyBackendYFlip(Matrix4 cameraTransform) { + if (!_flipsRenderTargetY) return cameraTransform; + return Matrix4.diagonal3Values(1.0, -1.0, 1.0) * cameraTransform; +} + +/// Inverts [w] when [backendFlipsRenderTargetY]: the vertex Y negation +/// reverses screen-space winding, so the cull winding must flip to keep the +/// same faces visible. +gpu.WindingOrder backendWinding(gpu.WindingOrder w) { + if (!_flipsRenderTargetY) return w; + return w == gpu.WindingOrder.clockwise + ? gpu.WindingOrder.counterClockwise + : gpu.WindingOrder.clockwise; +} + +// A small full-screen quad (6 vec2 NDC positions) for the probe pass. +gpu.DeviceBuffer? _probeQuad; +gpu.BufferView _probeQuadView() { + final buffer = + _probeQuad ??= gpu.gpuContext.createDeviceBufferWithCopy( + ByteData.sublistView( + Float32List.fromList([ + -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, // + -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, // + ]), + ), + ); + return gpu.BufferView(buffer, offsetInBytes: 0, lengthInBytes: 6 * 2 * 4); +} + +/// Renders a top/bottom pattern to an offscreen texture and reads it back to +/// determine [backendFlipsRenderTargetY]. Idempotent and cheap (runs once). +/// +/// Triggered from the first [Scene.render] rather than initialization: the +/// OpenGL ES backend brings its GPU context up lazily on the raster thread +/// only after the first frame, so the probe's render pass must run during a +/// frame. The render and submit happen synchronously here; the read-back +/// completes asynchronously and updates [backendFlipsRenderTargetY] for +/// subsequent frames (the first frame uses the default, no flip). +void probeBackendYFlip() { + if (_probed) return; + _probed = true; + + const size = 4; + final texture = gpu.gpuContext.createTexture( + gpu.StorageMode.devicePrivate, + size, + size, + format: gpu.PixelFormat.r8g8b8a8UNormInt, + enableRenderTargetUsage: true, + enableShaderReadUsage: true, + coordinateSystem: gpu.TextureCoordinateSystem.renderToTexture, + ); + + final vertexShader = baseShaderLibrary['FullscreenVertex']!; + final fragmentShader = baseShaderLibrary['YFlipProbeFragment']!; + final commandBuffer = gpu.gpuContext.createCommandBuffer(); + final renderPass = commandBuffer.createRenderPass( + gpu.RenderTarget.singleColor( + gpu.ColorAttachment(texture: texture, clearValue: Vector4.zero()), + ), + ); + renderPass.bindPipeline( + gpu.gpuContext.createRenderPipeline(vertexShader, fragmentShader), + ); + renderPass.bindVertexBuffer(_probeQuadView(), 6); + // FlipInfo +1: measure the raw backend orientation, without this workaround. + final flipInfo = Float32List(4)..[0] = 1.0; + renderPass.bindUniform( + vertexShader.getUniformSlot('FlipInfo'), + gpu.gpuContext.createHostBuffer().emplace(ByteData.sublistView(flipInfo)), + ); + renderPass.draw(); + commandBuffer.submit(); + + // Read back asynchronously: top row red means the backend stored the + // top-of-NDC fragment at row 0 (top-down, no flip); otherwise it stored it + // bottom-up and we must flip. + final image = texture.asImage(); + image.toByteData(format: ui.ImageByteFormat.rawRgba).then((bytes) { + if (bytes == null) return; + _flipsRenderTargetY = bytes.getUint8(0) < 128; + }); +} diff --git a/packages/flutter_scene/lib/src/scene.dart b/packages/flutter_scene/lib/src/scene.dart index 9c6b502..326d390 100644 --- a/packages/flutter_scene/lib/src/scene.dart +++ b/packages/flutter_scene/lib/src/scene.dart @@ -16,6 +16,7 @@ import 'render/render_scene.dart'; import 'render/scene_pass.dart'; import 'render/shadow_pass.dart'; import 'render/tonemap_pass.dart'; +import 'render/y_flip.dart'; import 'shaders.dart'; import 'surface.dart'; import 'tone_mapping.dart'; @@ -280,6 +281,10 @@ base class Scene implements SceneGraph { return; } + // Measure the backend's render-to-texture Y orientation once, on the + // first frame (the OpenGL ES context is only up after the first frame). + probeBackendYFlip(); + final drawArea = viewport ?? canvas.getLocalClipBounds(); if (drawArea.isEmpty) { return; @@ -379,11 +384,20 @@ base class Scene implements SceneGraph { ); final image = swapchainColor.asImage(); - canvas.drawImageRect( - image, - ui.Rect.fromLTWH(0, 0, pixelSize.width, pixelSize.height), - drawArea, - ui.Paint()..filterQuality = ui.FilterQuality.medium, - ); + final srcRect = ui.Rect.fromLTWH(0, 0, pixelSize.width, pixelSize.height); + final paint = ui.Paint()..filterQuality = ui.FilterQuality.medium; + if (backendFlipsRenderTargetY) { + // The Y-flip workaround stores the scene top-down via the vertex stage + // (see y_flip.dart). The web shim pairs that with a present-time blit + // flip; on native there is no such flip (asImage presents as-is), so + // flip the blit vertically here to land the image right-side up. + canvas.save(); + canvas.translate(0, drawArea.top + drawArea.bottom); + canvas.scale(1, -1); + canvas.drawImageRect(image, srcRect, drawArea, paint); + canvas.restore(); + } else { + canvas.drawImageRect(image, srcRect, drawArea, paint); + } } } diff --git a/packages/flutter_scene/lib/src/scene_encoder.dart b/packages/flutter_scene/lib/src/scene_encoder.dart index 456846b..ac965e4 100644 --- a/packages/flutter_scene/lib/src/scene_encoder.dart +++ b/packages/flutter_scene/lib/src/scene_encoder.dart @@ -8,6 +8,7 @@ import 'package:flutter_scene/src/geometry/geometry.dart'; import 'package:flutter_scene/src/light.dart'; import 'package:flutter_scene/src/material/material.dart'; import 'package:flutter_scene/src/render/render_scene.dart'; +import 'package:flutter_scene/src/render/y_flip.dart'; /// A deferred opaque draw. Holds the [RenderItem] (instanced or not), its /// resolved pipeline, a per-pipeline grouping key, and the camera @@ -93,6 +94,9 @@ base class SceneEncoder { _transientsBuffer = transientsBuffer { _cameraTransform = _camera.getViewTransform(dimensions); frustum = Frustum.matrix(_cameraTransform); + // Matrix sent to the vertex shader; carries the GLES render-to-texture + // Y-flip (see y_flip.dart). Frustum culling keeps the unflipped one. + _shaderCameraTransform = applyBackendYFlip(_cameraTransform); // Begin the opaque phase. _renderPass.setDepthWriteEnable(true); @@ -105,6 +109,7 @@ base class SceneEncoder { final gpu.RenderPass _renderPass; final gpu.HostBuffer _transientsBuffer; late final Matrix4 _cameraTransform; + late final Matrix4 _shaderCameraTransform; final List<_OpaqueRecord> _opaqueRecords = []; final List<_TranslucentRecord> _translucentRecords = []; @@ -214,15 +219,16 @@ base class SceneEncoder { _renderPass, _transientsBuffer, worldTransform, - _cameraTransform, + _shaderCameraTransform, _camera.position, ); material.bind(_renderPass, _transientsBuffer, _lighting); if (windingFlipped) { // A mirrored (negative-determinant) transform reverses triangle // winding; flip the cull order so front faces aren't culled. Material - // .bind set the default counter-clockwise winding. - _renderPass.setWindingOrder(gpu.WindingOrder.clockwise); + // .bind set the default counter-clockwise winding. backendWinding wraps + // it for the GLES render-target Y-flip (see y_flip.dart). + _renderPass.setWindingOrder(backendWinding(gpu.WindingOrder.clockwise)); } _renderPass.setPrimitiveType(geometry.primitiveType); _renderPass.draw(); @@ -248,13 +254,15 @@ base class SceneEncoder { _renderPass, _transientsBuffer, nodeTransform * instanceTransform, - _cameraTransform, + _shaderCameraTransform, _camera.position, ); // Each instance can itself mirror; combine with the node's parity. final flip = windingFlipped != (instanceTransform.determinant() < 0); _renderPass.setWindingOrder( - flip ? gpu.WindingOrder.clockwise : gpu.WindingOrder.counterClockwise, + backendWinding( + flip ? gpu.WindingOrder.clockwise : gpu.WindingOrder.counterClockwise, + ), ); _renderPass.draw(); } diff --git a/packages/flutter_scene/shaders/base.shaderbundle.json b/packages/flutter_scene/shaders/base.shaderbundle.json index 7012a62..c0aec43 100644 --- a/packages/flutter_scene/shaders/base.shaderbundle.json +++ b/packages/flutter_scene/shaders/base.shaderbundle.json @@ -30,5 +30,9 @@ "PrefilterEnvFragment": { "type": "fragment", "file": "shaders/flutter_scene_prefilter_env.frag" + }, + "YFlipProbeFragment": { + "type": "fragment", + "file": "shaders/flutter_scene_yflip_probe.frag" } } diff --git a/packages/flutter_scene/shaders/flutter_scene_fullscreen.vert b/packages/flutter_scene/shaders/flutter_scene_fullscreen.vert index 2d9b0a1..e831406 100644 --- a/packages/flutter_scene/shaders/flutter_scene_fullscreen.vert +++ b/packages/flutter_scene/shaders/flutter_scene_fullscreen.vert @@ -6,11 +6,20 @@ // top), matching the standard texture-sampling convention. Passes that // sample a render-to-texture input account for that target's per-backend // Y orientation themselves (see flutter_scene_tonemap.frag's flip_y). +// flip_y is -1 on backends where flutter_scene flips render-to-texture in +// the vertex stage (the OpenGL ES Y-flip workaround; see y_flip.dart), +1 +// otherwise. It negates gl_Position.y so this pass's offscreen target is +// stored top-down, leaving v_uv (the input sampling coords) untouched. +uniform FlipInfo { + float flip_y; +} +flip_info; + in vec2 position; out vec2 v_uv; void main() { v_uv = vec2(position.x * 0.5 + 0.5, 0.5 - position.y * 0.5); - gl_Position = vec4(position, 0.0, 1.0); + gl_Position = vec4(position.x, position.y * flip_info.flip_y, 0.0, 1.0); } diff --git a/packages/flutter_scene/shaders/flutter_scene_standard.frag b/packages/flutter_scene/shaders/flutter_scene_standard.frag index 888d55c..414ef96 100644 --- a/packages/flutter_scene/shaders/flutter_scene_standard.frag +++ b/packages/flutter_scene/shaders/flutter_scene_standard.frag @@ -100,16 +100,26 @@ vec3 EvaluateDiffuseSH(vec3 n) { frag_info.diffuse_sh8.xyz * (0.546274 * (n.x * n.x - n.y * n.y)); } -// A 16-tap Poisson disk, sampled by the soft-shadow PCF kernel. -const vec2 kPoissonDisk[16] = vec2[]( - vec2(-0.94201624, -0.39906216), vec2(0.94558609, -0.76890725), - vec2(-0.09418410, -0.92938870), vec2(0.34495938, 0.29387760), - vec2(-0.91588581, 0.45771432), vec2(-0.81544232, -0.87912464), - vec2(-0.38277543, 0.27676845), vec2(0.97484398, 0.75648379), - vec2(0.44323325, -0.97511554), vec2(0.53742981, -0.47373420), - vec2(-0.26496911, -0.41893023), vec2(0.79197514, 0.19090188), - vec2(-0.24188840, 0.99706507), vec2(-0.81409955, 0.91437590), - vec2(0.19984126, 0.78641367), vec2(0.14383161, -0.14100790)); +// One rotated Poisson-disk PCF tap into a cascade's atlas tile. Factored out +// so the kernel can be unrolled with inline literals at the call site (see the +// note in SampleCascade); the compiler inlines this. +float ShadowTap(vec2 p, float ca, float sa, float radius, vec2 uv, int cascade, + float inv_count, float receiver_depth) { + vec2 offset = vec2(p.x * ca - p.y * sa, p.x * sa + p.y * ca) * radius; + // Keep samples a texel inside this cascade's tile, so bilinear filtering of + // the atlas never reaches across the tile boundary into a neighbouring + // cascade's depths. + vec2 cuv = clamp(uv + offset, vec2(frag_info.shadow_texel_size), + vec2(1.0 - frag_info.shadow_texel_size)); + vec2 atlas_uv = vec2((float(cascade) + cuv.x) * inv_count, cuv.y); + // The atlas is a render-to-texture target; flip V to match its sampled Y + // orientation (see render_target_flip_y). + if (frag_info.render_target_flip_y > 0.5) { + atlas_uv.y = 1.0 - atlas_uv.y; + } + float caster_depth = texture(shadow_map, atlas_uv).r; + return receiver_depth <= caster_depth ? 1.0 : 0.0; +} // Samples one cascade's tile of the shadow atlas strip with a rotated // 16-tap Poisson-disk PCF. `world_pos` and `n` are world-space. @@ -153,24 +163,41 @@ float SampleCascade(int cascade, vec3 world_pos, vec3 n, int count) { float angle = noise * 6.28318530718; float ca = cos(angle); float sa = sin(angle); + + // 16-tap Poisson-disk PCF, unrolled with the kernel as inline literals. + // + // TODO(flutter_scene): this would naturally loop over a file-scope + // `const vec2 kPoissonDisk[16] = vec2[](...)`, but *any* const array (even + // one filled element by element, which the SPIR-V optimizer folds back into + // a constant) makes impellerc/SPIRV-Cross emit a GLSL array constructor + // (`vec2[](...)`) in its `#version 100` GLES output. That is invalid + // ES 1.00, so the shader fails to compile on conformant ES drivers + // (e.g. Mesa/llvmpipe under headless CI); lenient drivers accept it. Flutter + // GPU shaders should compile anywhere Flutter runs, so the real fix belongs + // upstream (impellerc should emit valid ES 1.00, or the bundle's GLES stage + // should target ES 3.00). Restore the const-array loop once that lands. + // See: . +#define _SHADOW_TAP(px, py) \ + ShadowTap(vec2(px, py), ca, sa, radius, uv, cascade, inv_count, \ + receiver_depth) float lit = 0.0; - for (int i = 0; i < 16; i++) { - vec2 p = kPoissonDisk[i]; - vec2 offset = vec2(p.x * ca - p.y * sa, p.x * sa + p.y * ca) * radius; - // Keep samples a texel inside this cascade's tile, so bilinear - // filtering of the atlas never reaches across the tile boundary - // into a neighbouring cascade's depths. - vec2 cuv = clamp(uv + offset, vec2(frag_info.shadow_texel_size), - vec2(1.0 - frag_info.shadow_texel_size)); - vec2 atlas_uv = vec2((float(cascade) + cuv.x) * inv_count, cuv.y); - // The atlas is a render-to-texture target; flip V to match its - // sampled Y orientation (see render_target_flip_y). - if (frag_info.render_target_flip_y > 0.5) { - atlas_uv.y = 1.0 - atlas_uv.y; - } - float caster_depth = texture(shadow_map, atlas_uv).r; - lit += receiver_depth <= caster_depth ? 1.0 : 0.0; - } + lit += _SHADOW_TAP(-0.94201624, -0.39906216); + lit += _SHADOW_TAP(0.94558609, -0.76890725); + lit += _SHADOW_TAP(-0.09418410, -0.92938870); + lit += _SHADOW_TAP(0.34495938, 0.29387760); + lit += _SHADOW_TAP(-0.91588581, 0.45771432); + lit += _SHADOW_TAP(-0.81544232, -0.87912464); + lit += _SHADOW_TAP(-0.38277543, 0.27676845); + lit += _SHADOW_TAP(0.97484398, 0.75648379); + lit += _SHADOW_TAP(0.44323325, -0.97511554); + lit += _SHADOW_TAP(0.53742981, -0.47373420); + lit += _SHADOW_TAP(-0.26496911, -0.41893023); + lit += _SHADOW_TAP(0.79197514, 0.19090188); + lit += _SHADOW_TAP(-0.24188840, 0.99706507); + lit += _SHADOW_TAP(-0.81409955, 0.91437590); + lit += _SHADOW_TAP(0.19984126, 0.78641367); + lit += _SHADOW_TAP(0.14383161, -0.14100790); +#undef _SHADOW_TAP float shadow = lit / 16.0; // Only the last cascade has a real outer edge (inner cascades hand diff --git a/packages/flutter_scene/shaders/flutter_scene_yflip_probe.frag b/packages/flutter_scene/shaders/flutter_scene_yflip_probe.frag new file mode 100644 index 0000000..7656b91 --- /dev/null +++ b/packages/flutter_scene/shaders/flutter_scene_yflip_probe.frag @@ -0,0 +1,14 @@ +// Render-to-texture Y-orientation probe; see y_flip.dart. +// +// Paired with flutter_scene_fullscreen.vert (FlipInfo bound to +1, i.e. no +// flip), which sets v_uv.y = 0.5 - ndc_y * 0.5, so v_uv.y < 0.5 is the top +// half of NDC. Emit red there and black below. Reading the rendered texture +// back reveals how the backend stored it: red at the top row means top-down +// (Metal/Vulkan), red at the bottom row means bottom-up (OpenGL ES). +in vec2 v_uv; + +out vec4 frag_color; + +void main() { + frag_color = vec4(v_uv.y < 0.5 ? 1.0 : 0.0, 0.0, 0.0, 1.0); +}