From e2d11014d0b4a87f17c700776fc0031b63b708ec Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sat, 12 Oct 2024 02:14:37 +1100 Subject: [PATCH] fix Windows build.dart to avoid native_assets fork; add implementations for ThermionFlutterWindows --- docs/android.mdx | 14 ++ thermion_dart/hook/build.dart | 31 ++-- .../native/include/APIBoundaryTypes.h | 44 ++--- .../include/ThermionDartRenderThreadApi.h | 78 ++++----- .../material/{gizmo.c => gizmo_material.c} | 0 thermion_dart/native/src/FilamentViewer.cpp | 10 +- thermion_dart/native/src/GridOverlay.cpp | 5 + thermion_dart/native/src/ThermionDartApi.cpp | 51 +++--- .../src/ThermionDartRenderThreadApi.cpp | 78 ++++----- thermion_dart/native/src/ThermionWin32.h | 36 ++++ .../native/src/camutils/Manipulator.cpp | 2 + thermion_dart/pubspec.yaml | 7 +- .../lib/src/widgets/src/thermion_widget.dart | 2 +- .../widgets/src/thermion_widget_windows.dart | 156 +++++++++++++++++- .../thermion_flutter/pubspec.yaml | 8 +- .../windows/backing_window.cpp | 18 +- .../windows/thermion_flutter_plugin.cpp | 12 +- .../thermion_flutter/windows/wgl_context.cpp | 4 +- ...rmion_flutter_texture_backed_platform.dart | 12 +- .../lib/thermion_flutter_windows.dart | 140 ++++++++++------ .../thermion_flutter_ffi/pubspec.yaml | 6 + .../thermion_flutter_platform_interface.dart | 9 +- .../lib/thermion_flutter_window.dart | 16 ++ 23 files changed, 524 insertions(+), 215 deletions(-) rename thermion_dart/native/include/material/{gizmo.c => gizmo_material.c} (100%) create mode 100644 thermion_dart/native/src/ThermionWin32.h create mode 100644 thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_window.dart diff --git a/docs/android.mdx b/docs/android.mdx index 4020b554..1279b9dc 100644 --- a/docs/android.mdx +++ b/docs/android.mdx @@ -1,5 +1,19 @@ ## Android +### Min SDK version + +Thermion requires Android SDK version 22, so change your `app/android/build.gradle` to match this version or higher: + +```groovy + defaultConfig { + ... + minSdk = 22 + ... + } +``` + +### Shrink/Minify Resources + In release mode, you must add the following to your `app/build.gradle`: ``` diff --git a/thermion_dart/hook/build.dart b/thermion_dart/hook/build.dart index 9b561f39..2be9edaa 100644 --- a/thermion_dart/hook/build.dart +++ b/thermion_dart/hook/build.dart @@ -32,8 +32,8 @@ void main(List args) async { final name = "thermion_dart.dart"; final libUri = config.outputDirectory .resolve(config.targetOS.libraryFileName(name, linkMode)); - output.addAssets( - [ + output.addAsset( + NativeCodeAsset( package: config.packageName, name: name, @@ -42,8 +42,7 @@ void main(List args) async { os: config.targetOS, architecture: config.dryRun ? null : config.targetArchitecture, ) - ], - linkInPackage: null, + ); return; } @@ -58,7 +57,7 @@ void main(List args) async { .map((f) => f.path) .toList(); sources.addAll([ - "${config.packageRoot.toFilePath()}/native/include/material/gizmo.c", + "${config.packageRoot.toFilePath()}/native/include/material/gizmo_material.c", "${config.packageRoot.toFilePath()}/native/include/material/image.c", "${config.packageRoot.toFilePath()}/native/include/material/grid.c", "${config.packageRoot.toFilePath()}/native/include/material/unlit.c", @@ -118,7 +117,7 @@ void main(List args) async { defines["WIN32"] = "1"; defines["_DEBUG"] = "1"; defines["_DLL"] = "1"; - flags.addAll(["/std:c++20", "/MDd"]); + flags.addAll(["/std:c++20", "/MDd", "/VERBOSE", ...defines.keys.map((k) => "/D$k=${defines[k]}").toList()]); } if (platform == "ios") { @@ -149,16 +148,28 @@ void main(List args) async { name: packageName, language: Language.cpp, assetName: 'thermion_dart.dart', - sources: sources, - includes: ['native/include', 'native/include/filament'], - defines: defines, + sources: platform == "windows" ? [] : +sources, + includes:[], // ['native/include', 'native/include/filament'], + defines: {}, //defines, flags: [ if (platform == "macos") '-mmacosx-version-min=13.0', if (platform == "ios") '-mios-version-min=13.0', ...flags, ...frameworks, + if (platform != "windows") ...libs.map((lib) => "-l$lib"), + if (platform != "windows") "-L$libDir", + if(platform == "windows") + ...[ + "/IF:\\Projects\\thermion\\thermion_dart\\native\\include", "/IF:\\Projects\\thermion\\thermion_dart\\native\\include\\filament", + ...sources, + '/link', + "/LIBPATH:$libDir", + '/DLL', + // '/out:foo.dll', + ] ], dartBuildFiles: ['hook/build.dart'], ); @@ -201,7 +212,7 @@ void main(List args) async { if (config.targetOS == "windows") { output.addAsset( NativeCodeAsset( - package: "thermion_dart", + package: config.packageName, name: "thermion_dart.dll", linkMode: DynamicLoadingBundled(), os: config.targetOS, diff --git a/thermion_dart/native/include/APIBoundaryTypes.h b/thermion_dart/native/include/APIBoundaryTypes.h index fae05ee8..cf022b63 100644 --- a/thermion_dart/native/include/APIBoundaryTypes.h +++ b/thermion_dart/native/include/APIBoundaryTypes.h @@ -21,14 +21,14 @@ extern "C" typedef struct TScene TScene; struct TMaterialKey { - bool doubleSided = 1; - bool unlit = 1; - bool hasVertexColors = 1; - bool hasBaseColorTexture = 1; - bool hasNormalTexture = 1; - bool hasOcclusionTexture = 1; - bool hasEmissiveTexture = 1; - bool useSpecularGlossiness = 1; + bool doubleSided = true; + bool unlit = true; + bool hasVertexColors = true; + bool hasBaseColorTexture = true; + bool hasNormalTexture = true; + bool hasOcclusionTexture = true; + bool hasEmissiveTexture = true; + bool useSpecularGlossiness = true; int alphaMode = 4; bool enableDiagnostics = 4; union { @@ -43,42 +43,42 @@ extern "C" }; #else struct { - bool hasMetallicRoughnessTexture = 1; + bool hasMetallicRoughnessTexture = true; uint8_t metallicRoughnessUV = 7; }; struct { - bool hasSpecularGlossinessTexture = 1; + bool hasSpecularGlossinessTexture = true; uint8_t specularGlossinessUV = 7; }; #endif }; uint8_t baseColorUV; // -- 32 bit boundary -- - bool hasClearCoatTexture = 1; + bool hasClearCoatTexture = true; uint8_t clearCoatUV = 7; - bool hasClearCoatRoughnessTexture = 1; + bool hasClearCoatRoughnessTexture = true; uint8_t clearCoatRoughnessUV = 7; - bool hasClearCoatNormalTexture = 1; + bool hasClearCoatNormalTexture = true; uint8_t clearCoatNormalUV = 7; - bool hasClearCoat = 1; - bool hasTransmission = 1; + bool hasClearCoat = true; + bool hasTransmission = true; bool hasTextureTransforms = 6; // -- 32 bit boundary -- uint8_t emissiveUV; uint8_t aoUV; uint8_t normalUV; - bool hasTransmissionTexture = 1; + bool hasTransmissionTexture = true; uint8_t transmissionUV = 7; // -- 32 bit boundary -- - bool hasSheenColorTexture = 1; + bool hasSheenColorTexture = true; uint8_t sheenColorUV = 7; - bool hasSheenRoughnessTexture = 1; + bool hasSheenRoughnessTexture = true; uint8_t sheenRoughnessUV = 7; - bool hasVolumeThicknessTexture = 1; + bool hasVolumeThicknessTexture = true; uint8_t volumeThicknessUV = 7; - bool hasSheen = 1; - bool hasIOR = 1; - bool hasVolume = 1; + bool hasSheen = true; + bool hasIOR = true; + bool hasVolume = true; } ; typedef struct TMaterialKey TMaterialKey; diff --git a/thermion_dart/native/include/ThermionDartRenderThreadApi.h b/thermion_dart/native/include/ThermionDartRenderThreadApi.h index e2e31eea..4ae034b0 100644 --- a/thermion_dart/native/include/ThermionDartRenderThreadApi.h +++ b/thermion_dart/native/include/ThermionDartRenderThreadApi.h @@ -19,7 +19,7 @@ extern "C" typedef int32_t EntityId; typedef void (*FilamentRenderCallback)(void *const owner); - void Viewer_createOnRenderThread( + EMSCRIPTEN_KEEPALIVE void Viewer_createOnRenderThread( void *const context, void *const platform, const char *uberArchivePath, @@ -27,68 +27,68 @@ extern "C" void (*renderCallback)(void *const renderCallbackOwner), void *const renderCallbackOwner, void (*callback)(TViewer *viewer)); - void Viewer_createSwapChainRenderThread(TViewer *viewer, void *const surface, void (*onComplete)(TSwapChain*)); - void Viewer_createHeadlessSwapChainRenderThread(TViewer *viewer, uint32_t width, uint32_t height, void (*onComplete)(TSwapChain*)); - void Viewer_destroySwapChainRenderThread(TViewer *viewer, TSwapChain* swapChain, void (*onComplete)()); - void Viewer_renderRenderThread(TViewer *viewer, TView* view, TSwapChain* swapChain); - void Viewer_captureRenderThread(TViewer *viewer, TView* view, TSwapChain* swapChain, uint8_t* out, void (*onComplete)()); - void Viewer_captureRenderTargetRenderThread(TViewer *viewer, TView* view, TSwapChain* swapChain, TRenderTarget* renderTarget, uint8_t* out, void (*onComplete)()); - void Viewer_requestFrameRenderThread(TViewer *viewer, void(*onComplete)()); + EMSCRIPTEN_KEEPALIVE void Viewer_createSwapChainRenderThread(TViewer *viewer, void *const surface, void (*onComplete)(TSwapChain*)); + EMSCRIPTEN_KEEPALIVE void Viewer_createHeadlessSwapChainRenderThread(TViewer *viewer, uint32_t width, uint32_t height, void (*onComplete)(TSwapChain*)); + EMSCRIPTEN_KEEPALIVE void Viewer_destroySwapChainRenderThread(TViewer *viewer, TSwapChain* swapChain, void (*onComplete)()); + EMSCRIPTEN_KEEPALIVE void Viewer_renderRenderThread(TViewer *viewer, TView* view, TSwapChain* swapChain); + EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderThread(TViewer *viewer, TView* view, TSwapChain* swapChain, uint8_t* out, void (*onComplete)()); + EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderTargetRenderThread(TViewer *viewer, TView* view, TSwapChain* swapChain, TRenderTarget* renderTarget, uint8_t* out, void (*onComplete)()); + EMSCRIPTEN_KEEPALIVE void Viewer_requestFrameRenderThread(TViewer *viewer, void(*onComplete)()); - void View_setToneMappingRenderThread(TView *tView, TEngine *tEngine, thermion::ToneMapping toneMapping); - void View_setBloomRenderThread(TView *tView, double bloom); + EMSCRIPTEN_KEEPALIVE void View_setToneMappingRenderThread(TView *tView, TEngine *tEngine, thermion::ToneMapping toneMapping); + EMSCRIPTEN_KEEPALIVE void View_setBloomRenderThread(TView *tView, double bloom); - void destroy_filament_viewer_render_thread(TViewer *viewer); + EMSCRIPTEN_KEEPALIVE void destroy_filament_viewer_render_thread(TViewer *viewer); FilamentRenderCallback make_render_callback_fn_pointer(FilamentRenderCallback); - void set_rendering_render_thread(TViewer *viewer, bool rendering, void(*onComplete)()); + EMSCRIPTEN_KEEPALIVE void set_rendering_render_thread(TViewer *viewer, bool rendering, void(*onComplete)()); - void set_frame_interval_render_thread(TViewer *viewer, float frameInterval); - void set_background_color_render_thread(TViewer *viewer, const float r, const float g, const float b, const float a); - void clear_background_image_render_thread(TViewer *viewer); - void set_background_image_render_thread(TViewer *viewer, const char *path, bool fillHeight, void (*onComplete)()); - void set_background_image_position_render_thread(TViewer *viewer, float x, float y, bool clamp); - void load_skybox_render_thread(TViewer *viewer, const char *skyboxPath, void (*onComplete)()); - void remove_skybox_render_thread(TViewer *viewer); + EMSCRIPTEN_KEEPALIVE void set_frame_interval_render_thread(TViewer *viewer, float frameInterval); + EMSCRIPTEN_KEEPALIVE void set_background_color_render_thread(TViewer *viewer, const float r, const float g, const float b, const float a); + EMSCRIPTEN_KEEPALIVE void clear_background_image_render_thread(TViewer *viewer); + EMSCRIPTEN_KEEPALIVE void set_background_image_render_thread(TViewer *viewer, const char *path, bool fillHeight, void (*onComplete)()); + EMSCRIPTEN_KEEPALIVE void set_background_image_position_render_thread(TViewer *viewer, float x, float y, bool clamp); + EMSCRIPTEN_KEEPALIVE void load_skybox_render_thread(TViewer *viewer, const char *skyboxPath, void (*onComplete)()); + EMSCRIPTEN_KEEPALIVE void remove_skybox_render_thread(TViewer *viewer); - void SceneManager_loadGlbFromBufferRenderThread(TSceneManager *sceneManager, const uint8_t *const data, size_t length, int numInstances, bool keepData, int priority, int layer, bool loadResourcesAsync, void (*callback)(EntityId)); - void load_glb_render_thread(TSceneManager *sceneManager, const char *assetPath, int numInstances, bool keepData, void (*callback)(EntityId)); - void load_gltf_render_thread(TSceneManager *sceneManager, const char *assetPath, const char *relativePath, bool keepData, void (*callback)(EntityId)); - void create_instance_render_thread(TSceneManager *sceneManager, EntityId entityId, void (*callback)(EntityId)); - void remove_entity_render_thread(TViewer *viewer, EntityId asset, void (*callback)()); - void clear_entities_render_thread(TViewer *viewer, void (*callback)()); + EMSCRIPTEN_KEEPALIVE void SceneManager_loadGlbFromBufferRenderThread(TSceneManager *sceneManager, const uint8_t *const data, size_t length, int numInstances, bool keepData, int priority, int layer, bool loadResourcesAsync, void (*callback)(EntityId)); + EMSCRIPTEN_KEEPALIVE void load_glb_render_thread(TSceneManager *sceneManager, const char *assetPath, int numInstances, bool keepData, void (*callback)(EntityId)); + EMSCRIPTEN_KEEPALIVE void load_gltf_render_thread(TSceneManager *sceneManager, const char *assetPath, const char *relativePath, bool keepData, void (*callback)(EntityId)); + EMSCRIPTEN_KEEPALIVE void create_instance_render_thread(TSceneManager *sceneManager, EntityId entityId, void (*callback)(EntityId)); + EMSCRIPTEN_KEEPALIVE void remove_entity_render_thread(TViewer *viewer, EntityId asset, void (*callback)()); + EMSCRIPTEN_KEEPALIVE void clear_entities_render_thread(TViewer *viewer, void (*callback)()); - void apply_weights_render_thread( + EMSCRIPTEN_KEEPALIVE void apply_weights_render_thread( TSceneManager *sceneManager, EntityId asset, const char *const entityName, float *const weights, int count); - void set_animation_frame_render_thread(TSceneManager *sceneManager, EntityId asset, int animationIndex, int animationFrame); - void stop_animation_render_thread(TSceneManager *sceneManager, EntityId asset, int index); - void get_animation_count_render_thread(TSceneManager *sceneManager, EntityId asset, void (*callback)(int)); - void get_animation_name_render_thread(TSceneManager *sceneManager, EntityId asset, char *const outPtr, int index, void (*callback)()); - void get_morph_target_name_render_thread(TSceneManager *sceneManager, EntityId assetEntity, EntityId childEntity, char *const outPtr, int index, void (*callback)()); - void get_morph_target_name_count_render_thread(TSceneManager *sceneManager, EntityId asset, EntityId childEntity, void (*callback)(int32_t)); - void set_morph_target_weights_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void set_animation_frame_render_thread(TSceneManager *sceneManager, EntityId asset, int animationIndex, int animationFrame); + EMSCRIPTEN_KEEPALIVE void stop_animation_render_thread(TSceneManager *sceneManager, EntityId asset, int index); + EMSCRIPTEN_KEEPALIVE void get_animation_count_render_thread(TSceneManager *sceneManager, EntityId asset, void (*callback)(int)); + EMSCRIPTEN_KEEPALIVE void get_animation_name_render_thread(TSceneManager *sceneManager, EntityId asset, char *const outPtr, int index, void (*callback)()); + EMSCRIPTEN_KEEPALIVE void get_morph_target_name_render_thread(TSceneManager *sceneManager, EntityId assetEntity, EntityId childEntity, char *const outPtr, int index, void (*callback)()); + EMSCRIPTEN_KEEPALIVE void get_morph_target_name_count_render_thread(TSceneManager *sceneManager, EntityId asset, EntityId childEntity, void (*callback)(int32_t)); + EMSCRIPTEN_KEEPALIVE void set_morph_target_weights_render_thread(TSceneManager *sceneManager, EntityId asset, const float *const morphData, int numWeights, void (*callback)(bool)); - void update_bone_matrices_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void update_bone_matrices_render_thread(TSceneManager *sceneManager, EntityId asset, void(*callback)(bool)); - void set_bone_transform_render_thread( + EMSCRIPTEN_KEEPALIVE void set_bone_transform_render_thread( TSceneManager *sceneManager, EntityId asset, int skinIndex, int boneIndex, const float *const transform, void (*callback)(bool)); - void set_post_processing_render_thread(TViewer *viewer, bool enabled); - void reset_to_rest_pose_render_thread(TSceneManager *sceneManager, EntityId entityId, void(*callback)()); - void create_geometry_render_thread( + EMSCRIPTEN_KEEPALIVE void set_post_processing_render_thread(TViewer *viewer, bool enabled); + EMSCRIPTEN_KEEPALIVE void reset_to_rest_pose_render_thread(TSceneManager *sceneManager, EntityId entityId, void(*callback)()); + EMSCRIPTEN_KEEPALIVE void create_geometry_render_thread( TSceneManager *sceneManager, float *vertices, int numVertices, @@ -102,7 +102,7 @@ extern "C" TMaterialInstance *materialInstance, bool keepData, void (*callback)(EntityId)); - void unproject_texture_render_thread(TViewer* viewer, EntityId entity, uint8_t* input, uint32_t inputWidth, uint32_t inputHeight, uint8_t* out, uint32_t outWidth, uint32_t outHeight, void(*callback)()); + EMSCRIPTEN_KEEPALIVE void unproject_texture_render_thread(TViewer* viewer, EntityId entity, uint8_t* input, uint32_t inputWidth, uint32_t inputHeight, uint8_t* out, uint32_t outWidth, uint32_t outHeight, void(*callback)()); #ifdef __cplusplus diff --git a/thermion_dart/native/include/material/gizmo.c b/thermion_dart/native/include/material/gizmo_material.c similarity index 100% rename from thermion_dart/native/include/material/gizmo.c rename to thermion_dart/native/include/material/gizmo_material.c diff --git a/thermion_dart/native/src/FilamentViewer.cpp b/thermion_dart/native/src/FilamentViewer.cpp index c7ba9715..adb6a785 100644 --- a/thermion_dart/native/src/FilamentViewer.cpp +++ b/thermion_dart/native/src/FilamentViewer.cpp @@ -669,6 +669,10 @@ namespace thermion { std::lock_guard lock(_renderMutex); SwapChain *swapChain = _engine->createSwapChain((void *)window, filament::backend::SWAP_CHAIN_CONFIG_TRANSPARENT | filament::backend::SWAP_CHAIN_CONFIG_READABLE | filament::SwapChain::CONFIG_HAS_STENCIL_BUFFER); + + if(!_engine->isValid(swapChain)) { + Log("SWAPCHAIN NOT VALID!!"); + } _swapChains.push_back(swapChain); return swapChain; } @@ -767,7 +771,7 @@ namespace thermion // bloom can be a bit glitchy (some Intel iGPUs won't render when postprocessing is enabled and bloom is disabled, // and render targets on MacOS flicker when bloom is disabled. // Here, we enable bloom, but set to 0 strength - view->setBloomOptions({.enabled=true, .strength = 0}); + view->setBloomOptions({.strength = 0, .enabled=true }); view->setShadowingEnabled(false); view->setScreenSpaceRefractionEnabled(false); view->setPostProcessingEnabled(false); @@ -1045,6 +1049,10 @@ namespace thermion for(auto swapChain : _swapChains) { auto views = _renderable[swapChain]; if(views.size() > 0) { + if(!_engine->isValid(swapChain)) { + Log("SWAPCHAIN NOT VALID!!"); + continue; + } bool beginFrame = _renderer->beginFrame(swapChain, frameTimeInNanos); if (beginFrame) { for(auto view : views) { diff --git a/thermion_dart/native/src/GridOverlay.cpp b/thermion_dart/native/src/GridOverlay.cpp index 05fdf616..7b967a3f 100644 --- a/thermion_dart/native/src/GridOverlay.cpp +++ b/thermion_dart/native/src/GridOverlay.cpp @@ -1,3 +1,8 @@ +#ifdef _WIN32 +#define _USE_MATH_DEFINES +#include +#endif + #include "GridOverlay.hpp" #include diff --git a/thermion_dart/native/src/ThermionDartApi.cpp b/thermion_dart/native/src/ThermionDartApi.cpp index 282804fe..5232b1d3 100644 --- a/thermion_dart/native/src/ThermionDartApi.cpp +++ b/thermion_dart/native/src/ThermionDartApi.cpp @@ -1,14 +1,7 @@ #ifdef _WIN32 -#pragma comment(lib, "Shlwapi.lib") -#pragma comment(lib, "opengl32.lib") +#include "ThermionWin32.h" #endif -#include "ResourceBuffer.hpp" -#include "FilamentViewer.hpp" -#include "filament/LightManager.h" -#include "Log.hpp" -#include "ThreadPool.hpp" - #include #include @@ -16,6 +9,12 @@ #include #endif +#include "filament/LightManager.h" +#include "ResourceBuffer.hpp" +#include "FilamentViewer.hpp" +#include "Log.hpp" +#include "ThreadPool.hpp" + using namespace thermion; extern "C" @@ -230,62 +229,62 @@ extern "C" return reinterpret_cast(filamentCamera); } - double4x4 get_camera_model_matrix(TCamera *camera) + EMSCRIPTEN_KEEPALIVE double4x4 get_camera_model_matrix(TCamera *camera) { const auto &mat = reinterpret_cast(camera)->getModelMatrix(); return convert_mat4_to_double4x4(mat); } - double4x4 get_camera_view_matrix(TCamera *camera) + EMSCRIPTEN_KEEPALIVE double4x4 get_camera_view_matrix(TCamera *camera) { const auto &mat = reinterpret_cast(camera)->getViewMatrix(); return convert_mat4_to_double4x4(mat); } - double4x4 get_camera_projection_matrix(TCamera *camera) + EMSCRIPTEN_KEEPALIVE double4x4 get_camera_projection_matrix(TCamera *camera) { const auto &mat = reinterpret_cast(camera)->getProjectionMatrix(); return convert_mat4_to_double4x4(mat); } - double4x4 get_camera_culling_projection_matrix(TCamera *camera) + EMSCRIPTEN_KEEPALIVE double4x4 get_camera_culling_projection_matrix(TCamera *camera) { const auto &mat = reinterpret_cast(camera)->getCullingProjectionMatrix(); return convert_mat4_to_double4x4(mat); } - void set_camera_projection_matrix(TCamera *camera, double4x4 matrix, double near, double far) + EMSCRIPTEN_KEEPALIVE void set_camera_projection_matrix(TCamera *camera, double4x4 matrix, double near, double far) { auto cam = reinterpret_cast(camera); const auto &mat = convert_double4x4_to_mat4(matrix); cam->setCustomProjection(mat, near, far); } - void Camera_setLensProjection(TCamera *camera, double near, double far, double aspect, double focalLength) + EMSCRIPTEN_KEEPALIVE void Camera_setLensProjection(TCamera *camera, double near, double far, double aspect, double focalLength) { auto cam = reinterpret_cast(camera); cam->setLensProjection(focalLength, aspect, near, far); } - void Camera_setModelMatrix(TCamera *camera, double4x4 matrix) + EMSCRIPTEN_KEEPALIVE void Camera_setModelMatrix(TCamera *camera, double4x4 matrix) { auto cam = reinterpret_cast(camera); cam->setModelMatrix(convert_double4x4_to_mat4(matrix)); } - double get_camera_near(TCamera *camera) + EMSCRIPTEN_KEEPALIVE double get_camera_near(TCamera *camera) { auto cam = reinterpret_cast(camera); return cam->getNear(); } - double get_camera_culling_far(TCamera *camera) + EMSCRIPTEN_KEEPALIVE double get_camera_culling_far(TCamera *camera) { auto cam = reinterpret_cast(camera); return cam->getCullingFar(); } - const double *const get_camera_frustum(TCamera *camera) + EMSCRIPTEN_KEEPALIVE const double *const get_camera_frustum(TCamera *camera) { const auto frustum = reinterpret_cast(camera)->getFrustum(); @@ -304,7 +303,6 @@ extern "C" return array; } - EMSCRIPTEN_KEEPALIVE void set_camera_focus_distance(TCamera *camera, float distance) { @@ -703,8 +701,9 @@ extern "C" EMSCRIPTEN_KEEPALIVE void SceneManager_queueTransformUpdates(TSceneManager *tSceneManager, EntityId *entities, const double *const transforms, int numEntities) { auto *sceneManager = reinterpret_cast(tSceneManager); - math::mat4 matrices[ - numEntities]; + + std::vector matrices( + numEntities); for (int i = 0; i < numEntities; i++) { matrices[i] = math::mat4( @@ -723,7 +722,7 @@ extern "C" transforms[i * 16 + 14], transforms[i * 16 + 15]); } - sceneManager->queueTransformUpdates(entities, matrices, numEntities); + sceneManager->queueTransformUpdates(entities, matrices.data(), numEntities); } EMSCRIPTEN_KEEPALIVE bool update_bone_matrices(TSceneManager *sceneManager, EntityId entityId) @@ -955,10 +954,10 @@ extern "C" EMSCRIPTEN_KEEPALIVE void set_material_property_float4(TSceneManager *sceneManager, EntityId entity, int materialIndex, const char *property, double4 value) { filament::math::float4 filamentValue; - filamentValue.x = static_cast(value.x); - filamentValue.y = static_cast(value.y); - filamentValue.z = static_cast(value.z); - filamentValue.w = static_cast(value.w); + filamentValue.x = static_cast(value.x); + filamentValue.y = static_cast(value.y); + filamentValue.z = static_cast(value.z); + filamentValue.w = static_cast(value.w); ((SceneManager *)sceneManager)->setMaterialProperty(entity, materialIndex, property, filamentValue); } diff --git a/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp b/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp index b5dd4550..0b9ef701 100644 --- a/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp +++ b/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp @@ -184,7 +184,7 @@ extern "C" static RenderLoop *_rl; - void Viewer_createOnRenderThread( + EMSCRIPTEN_KEEPALIVE void Viewer_createOnRenderThread( void *const context, void *const platform, const char *uberArchivePath, const void *const loader, void (*renderCallback)(void *const renderCallbackOwner), @@ -200,14 +200,14 @@ extern "C" renderCallback, renderCallbackOwner, callback); } - void destroy_filament_viewer_render_thread(TViewer *viewer) + EMSCRIPTEN_KEEPALIVE void destroy_filament_viewer_render_thread(TViewer *viewer) { _rl->destroyViewer((FilamentViewer *)viewer); delete _rl; _rl = nullptr; } - void Viewer_createHeadlessSwapChainRenderThread(TViewer *viewer, + EMSCRIPTEN_KEEPALIVE void Viewer_createHeadlessSwapChainRenderThread(TViewer *viewer, uint32_t width, uint32_t height, void (*onComplete)(TSwapChain*)) @@ -221,7 +221,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void Viewer_createSwapChainRenderThread(TViewer *viewer, + EMSCRIPTEN_KEEPALIVE void Viewer_createSwapChainRenderThread(TViewer *viewer, void *const surface, void (*onComplete)(TSwapChain*)) { @@ -234,7 +234,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void Viewer_destroySwapChainRenderThread(TViewer *viewer, TSwapChain *swapChain, void (*onComplete)()) + EMSCRIPTEN_KEEPALIVE void Viewer_destroySwapChainRenderThread(TViewer *viewer, TSwapChain *swapChain, void (*onComplete)()) { std::packaged_task lambda( [=]() mutable @@ -246,7 +246,7 @@ extern "C" } - void Viewer_requestFrameRenderThread(TViewer *viewer, void(*onComplete)()) + EMSCRIPTEN_KEEPALIVE void Viewer_requestFrameRenderThread(TViewer *viewer, void(*onComplete)()) { if (!_rl) { @@ -258,7 +258,7 @@ extern "C" } } - void + EMSCRIPTEN_KEEPALIVE void set_frame_interval_render_thread(TViewer *viewer, float frameIntervalInMilliseconds) { _rl->setFrameIntervalInMilliseconds(frameIntervalInMilliseconds); @@ -267,7 +267,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void Viewer_renderRenderThread(TViewer *viewer, TView *tView, TSwapChain *tSwapChain) + EMSCRIPTEN_KEEPALIVE void Viewer_renderRenderThread(TViewer *viewer, TView *tView, TSwapChain *tSwapChain) { std::packaged_task lambda([=]() mutable { @@ -276,21 +276,21 @@ extern "C" auto fut = _rl->add_task(lambda); } - void Viewer_captureRenderThread(TViewer *viewer, TView *view, TSwapChain *tSwapChain, uint8_t *pixelBuffer, void (*onComplete)()) + EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderThread(TViewer *viewer, TView *view, TSwapChain *tSwapChain, uint8_t *pixelBuffer, void (*onComplete)()) { std::packaged_task lambda([=]() mutable { Viewer_capture(viewer, view, tSwapChain, pixelBuffer, onComplete); }); auto fut = _rl->add_task(lambda); } - void Viewer_captureRenderTargetRenderThread(TViewer *viewer, TView *view, TSwapChain *tSwapChain, TRenderTarget* tRenderTarget, uint8_t *pixelBuffer, void (*onComplete)()) + EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderTargetRenderThread(TViewer *viewer, TView *view, TSwapChain *tSwapChain, TRenderTarget* tRenderTarget, uint8_t *pixelBuffer, void (*onComplete)()) { std::packaged_task lambda([=]() mutable { Viewer_captureRenderTarget(viewer, view, tSwapChain, tRenderTarget, pixelBuffer, onComplete); }); auto fut = _rl->add_task(lambda); } - void + EMSCRIPTEN_KEEPALIVE void set_background_color_render_thread(TViewer *viewer, const float r, const float g, const float b, const float a) { @@ -300,7 +300,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void load_gltf_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void load_gltf_render_thread(TSceneManager *sceneManager, const char *path, const char *relativeResourcePath, bool keepData, @@ -314,7 +314,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void load_glb_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void load_glb_render_thread(TSceneManager *sceneManager, const char *path, int numInstances, bool keepData, @@ -330,7 +330,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void SceneManager_loadGlbFromBufferRenderThread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void SceneManager_loadGlbFromBufferRenderThread(TSceneManager *sceneManager, const uint8_t *const data, size_t length, int numInstances, @@ -350,14 +350,14 @@ extern "C" auto fut = _rl->add_task(lambda); } - void clear_background_image_render_thread(TViewer *viewer) + EMSCRIPTEN_KEEPALIVE void clear_background_image_render_thread(TViewer *viewer) { std::packaged_task lambda([=] { clear_background_image(viewer); }); auto fut = _rl->add_task(lambda); } - void set_background_image_render_thread(TViewer *viewer, + EMSCRIPTEN_KEEPALIVE void set_background_image_render_thread(TViewer *viewer, const char *path, bool fillHeight, void (*callback)()) { @@ -369,7 +369,8 @@ extern "C" }); auto fut = _rl->add_task(lambda); } - void set_background_image_position_render_thread(TViewer *viewer, + + EMSCRIPTEN_KEEPALIVE void set_background_image_position_render_thread(TViewer *viewer, float x, float y, bool clamp) { @@ -379,7 +380,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void load_skybox_render_thread(TViewer *viewer, + EMSCRIPTEN_KEEPALIVE void load_skybox_render_thread(TViewer *viewer, const char *skyboxPath, void (*onComplete)()) { @@ -391,7 +392,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void load_ibl_render_thread(TViewer *viewer, const char *iblPath, + EMSCRIPTEN_KEEPALIVE void load_ibl_render_thread(TViewer *viewer, const char *iblPath, float intensity) { std::packaged_task lambda( @@ -399,21 +400,22 @@ extern "C" { load_ibl(viewer, iblPath, intensity); }); auto fut = _rl->add_task(lambda); } - void remove_skybox_render_thread(TViewer *viewer) + + EMSCRIPTEN_KEEPALIVE void remove_skybox_render_thread(TViewer *viewer) { std::packaged_task lambda([=] { remove_skybox(viewer); }); auto fut = _rl->add_task(lambda); } - void remove_ibl_render_thread(TViewer *viewer) + EMSCRIPTEN_KEEPALIVE void remove_ibl_render_thread(TViewer *viewer) { std::packaged_task lambda([=] { remove_ibl(viewer); }); auto fut = _rl->add_task(lambda); } - void remove_entity_render_thread(TViewer *viewer, + EMSCRIPTEN_KEEPALIVE void remove_entity_render_thread(TViewer *viewer, EntityId asset, void (*callback)()) { std::packaged_task lambda([=] @@ -424,7 +426,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void clear_entities_render_thread(TViewer *viewer, void (*callback)()) + EMSCRIPTEN_KEEPALIVE void clear_entities_render_thread(TViewer *viewer, void (*callback)()) { std::packaged_task lambda([=] { @@ -435,7 +437,7 @@ extern "C" } - void + EMSCRIPTEN_KEEPALIVE void get_morph_target_name_render_thread(TSceneManager *sceneManager, EntityId assetEntity, EntityId childEntity, char *const outPtr, int index, void (*callback)()) { @@ -447,7 +449,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void + EMSCRIPTEN_KEEPALIVE void get_morph_target_name_count_render_thread(TSceneManager *sceneManager, EntityId assetEntity, EntityId childEntity, void (*callback)(int)) { @@ -459,7 +461,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void set_animation_frame_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void set_animation_frame_render_thread(TSceneManager *sceneManager, EntityId asset, int animationIndex, int animationFrame) @@ -469,7 +471,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void stop_animation_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void stop_animation_render_thread(TSceneManager *sceneManager, EntityId asset, int index) { std::packaged_task lambda( @@ -478,7 +480,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void get_animation_count_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void get_animation_count_render_thread(TSceneManager *sceneManager, EntityId asset, void (*callback)(int)) { @@ -492,7 +494,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void get_animation_name_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void get_animation_name_render_thread(TSceneManager *sceneManager, EntityId asset, char *const outPtr, int index, @@ -507,7 +509,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void + EMSCRIPTEN_KEEPALIVE void get_name_for_entity_render_thread(TSceneManager *sceneManager, const EntityId entityId, void (*callback)(const char *)) { std::packaged_task lambda( @@ -520,7 +522,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void set_morph_target_weights_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void set_morph_target_weights_render_thread(TSceneManager *sceneManager, EntityId asset, const float *const morphData, int numWeights, @@ -535,7 +537,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void set_bone_transform_render_thread( + EMSCRIPTEN_KEEPALIVE void set_bone_transform_render_thread( TSceneManager *sceneManager, EntityId asset, int skinIndex, @@ -553,7 +555,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void update_bone_matrices_render_thread(TSceneManager *sceneManager, + EMSCRIPTEN_KEEPALIVE void update_bone_matrices_render_thread(TSceneManager *sceneManager, EntityId entity, void (*callback)(bool)) { std::packaged_task lambda( @@ -565,7 +567,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void View_setToneMappingRenderThread(TView *tView, TEngine *tEngine, thermion::ToneMapping toneMapping) { + EMSCRIPTEN_KEEPALIVE void View_setToneMappingRenderThread(TView *tView, TEngine *tEngine, thermion::ToneMapping toneMapping) { std::packaged_task lambda( [=] { @@ -574,7 +576,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void View_setBloomRenderThread(TView *tView, double bloom) { + EMSCRIPTEN_KEEPALIVE void View_setBloomRenderThread(TView *tView, double bloom) { std::packaged_task lambda( [=] { @@ -583,7 +585,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void reset_to_rest_pose_render_thread(TSceneManager *sceneManager, EntityId entityId, void (*callback)()) + EMSCRIPTEN_KEEPALIVE void reset_to_rest_pose_render_thread(TSceneManager *sceneManager, EntityId entityId, void (*callback)()) { std::packaged_task lambda( [=] @@ -594,7 +596,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void create_geometry_render_thread( + EMSCRIPTEN_KEEPALIVE void create_geometry_render_thread( TSceneManager *sceneManager, float *vertices, int numVertices, @@ -619,7 +621,7 @@ extern "C" auto fut = _rl->add_task(lambda); } - void unproject_texture_render_thread(TViewer* viewer, EntityId entity, uint8_t *input, uint32_t inputWidth, uint32_t inputHeight, uint8_t *out, uint32_t outWidth, uint32_t outHeight, void (*callback)()) + EMSCRIPTEN_KEEPALIVE void unproject_texture_render_thread(TViewer* viewer, EntityId entity, uint8_t *input, uint32_t inputWidth, uint32_t inputHeight, uint8_t *out, uint32_t outWidth, uint32_t outHeight, void (*callback)()) { std::packaged_task lambda( [=] diff --git a/thermion_dart/native/src/ThermionWin32.h b/thermion_dart/native/src/ThermionWin32.h new file mode 100644 index 00000000..4b65119c --- /dev/null +++ b/thermion_dart/native/src/ThermionWin32.h @@ -0,0 +1,36 @@ +#pragma once + +#pragma comment(lib, "Shlwapi.lib") +#pragma comment(lib, "opengl32.lib") +#pragma comment(lib, "gdi32.lib") +#pragma comment(lib, "user32.lib") +#pragma comment(lib, "shell32.lib") +#pragma comment(lib, "dwmapi.lib") +#pragma comment(lib, "comctl32.lib") +#pragma comment(lib, "filament.lib") +#pragma comment(lib, "bluevk.lib") +#pragma comment(lib, "bluegl.lib") +#pragma comment(lib, "backend.lib") +#pragma comment(lib, "filameshio.lib") +#pragma comment(lib, "viewer.lib") +#pragma comment(lib, "filamat.lib") +#pragma comment(lib, "geometry.lib") +#pragma comment(lib, "utils.lib") +#pragma comment(lib, "filabridge.lib") +#pragma comment(lib, "gltfio_core.lib") +#pragma comment(lib, "filament-iblprefilter.lib") +#pragma comment(lib, "image.lib") +#pragma comment(lib, "imageio.lib") +#pragma comment(lib, "tinyexr.lib") +#pragma comment(lib, "filaflat.lib") +#pragma comment(lib, "dracodec.lib") +#pragma comment(lib, "ibl.lib") +#pragma comment(lib, "ktxreader.lib") +#pragma comment(lib, "png.lib") +#pragma comment(lib, "z.lib") +#pragma comment(lib, "stb.lib") +#pragma comment(lib, "uberzlib.lib") +#pragma comment(lib, "smol-v.lib") +#pragma comment(lib, "uberarchive.lib") +#pragma comment(lib, "zstd.lib") +#pragma comment(lib, "basis_transcoder.lib") \ No newline at end of file diff --git a/thermion_dart/native/src/camutils/Manipulator.cpp b/thermion_dart/native/src/camutils/Manipulator.cpp index 5b675a5f..7f92bcfb 100644 --- a/thermion_dart/native/src/camutils/Manipulator.cpp +++ b/thermion_dart/native/src/camutils/Manipulator.cpp @@ -177,6 +177,8 @@ namespace filament case Mode::ORBIT: return new OrbitManipulator(mode, details); } + // just to make MSVC happy + return new OrbitManipulator(mode, details); } template diff --git a/thermion_dart/pubspec.yaml b/thermion_dart/pubspec.yaml index ef2cca73..133e0dac 100644 --- a/thermion_dart/pubspec.yaml +++ b/thermion_dart/pubspec.yaml @@ -13,11 +13,16 @@ dependencies: ffi: ^2.1.2 animation_tools_dart: ^0.1.0 native_toolchain_c: ^0.4.2 + # native_toolchain_c: + # path: F:\Projects\native\pkgs\native_toolchain_c + native_assets_cli: ^0.6.1 + # native_assets_cli: + # path: F:\Projects\native\pkgs\native_assets_cli archive: ^3.6.1 web: ^1.0.0 logging: ^1.2.0 http: ^1.2.2 - native_assets_cli: ^0.6.1 + dev_dependencies: ffigen: ^13.0.0 diff --git a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart index 18e0c083..b3246d30 100644 --- a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart +++ b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget.dart @@ -95,7 +95,7 @@ class _ThermionWidgetState extends State { } if (Platform.isWindows) { - return ThermionWidgetWindows(viewer: widget.viewer); + return ThermionWidgetWindows(viewer: widget.viewer, view: view!, initial: widget.initial, onResize: widget.onResize); } return ThermionTextureWidget( diff --git a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_windows.dart b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_windows.dart index c5040b15..211ceede 100644 --- a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_windows.dart +++ b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_widget_windows.dart @@ -1,14 +1,158 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; -import 'package:thermion_flutter/thermion_flutter.dart'; +import 'package:thermion_flutter/src/widgets/src/resize_observer.dart'; +import 'package:thermion_flutter/src/widgets/src/transparent_filament_widget.dart'; +import 'package:thermion_flutter/thermion_flutter.dart' as t; +import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart'; -class ThermionWidgetWindows extends StatelessWidget { +class ThermionWidgetWindows extends StatefulWidget { - final ThermionViewer viewer; + final t.ThermionViewer viewer; + + final t.View view; + + /// + /// + /// + final Widget? initial; + + /// + /// A callback that will be invoked whenever this widget (and the underlying texture is resized). + /// + final Future Function(Size size, t.View view, double pixelRatio)? onResize; + + const ThermionWidgetWindows({super.key, required this.viewer, this.initial, this.onResize, required this.view}); + + + @override + State createState() => _ThermionWidgetWindowsState(); +} + +class _ThermionWidgetWindowsState extends State { + + ThermionFlutterWindow? _window; + + @override + void initState() { + super.initState(); + + WidgetsBinding.instance.addPostFrameCallback((timeStamp) async { + await widget.viewer.initialized; + + var dpr = MediaQuery.of(context).devicePixelRatio; + + var size = ((context.findRenderObject()) as RenderBox).size; + var width = (size.width * dpr).ceil(); + var height = (size.height * dpr).ceil(); + + _window = await t.ThermionFlutterPlatform.instance.createWindow(width, height, 0, 0); + + await widget.view.updateViewport(_window!.width, _window!.height); + + try { + await widget.onResize?.call( + Size(_window!.width.toDouble(), _window!.height.toDouble()), + widget.view, + dpr); + } catch (err, st) { + print(err); + print(st); + } + + if (mounted) { + setState(() {}); + } + + _requestFrame(); + + widget.viewer.onDispose(() async { + var window = _window; + if (mounted) { + setState(() {}); + } + await window?.destroy(); + }); + }); + } + + bool _rendering = false; + + void _requestFrame() { + WidgetsBinding.instance.scheduleFrameCallback((d) async { + if (widget.viewer.rendering && !_rendering) { + _rendering = true; + await widget.viewer.requestFrame(); + _rendering = false; + } + _requestFrame(); + }); + } + + final _resizing = []; + + Timer? _resizeTimer; + + Future _resize(Size oldSize, Size newSize) async { + await Future.wait(_resizing); + + _resizeTimer?.cancel(); + + _resizeTimer = Timer(const Duration(milliseconds: 100), () async { + await Future.wait(_resizing); + if (!mounted) { + return; + } + + if (newSize.width == _window?.width && + newSize.height == _window?.height) { + return; + } + + final completer = Completer(); + + _resizing.add(completer.future); + + final dpr = MediaQuery.of(context).devicePixelRatio; + + newSize *= dpr; + + var newWidth = newSize.width.ceil(); + var newHeight = newSize.height.ceil(); + + await _window?.resize( + newWidth, + newHeight, + 0, + 0, + ); + + await widget.view.updateViewport(_window!.width, _window!.height); + + await widget.onResize?.call( + Size(_window!.width.toDouble(), _window!.height.toDouble()), + widget.view, + dpr); + + if (!mounted) { + return; + } + setState(() {}); + completer.complete(); + _resizing.remove(completer.future); + }); + } - const ThermionWidgetWindows({super.key, required this.viewer}); @override Widget build(BuildContext context) { - // TODO: implement build - throw UnimplementedError(); + if (_window == null) { + return widget.initial ?? Container(color: Colors.red); + } + + return ResizeObserver( + onResized: _resize, + child: CustomPaint(painter:TransparencyPainter())); } + } diff --git a/thermion_flutter/thermion_flutter/pubspec.yaml b/thermion_flutter/thermion_flutter/pubspec.yaml index 1ead0ff3..4d466adb 100644 --- a/thermion_flutter/thermion_flutter/pubspec.yaml +++ b/thermion_flutter/thermion_flutter/pubspec.yaml @@ -23,7 +23,13 @@ dependencies: thermion_flutter_web: ^0.1.0+7 logging: ^1.2.0 web: ^1.0.0 - +dependency_overrides: + thermion_dart: + path: ../../thermion_dart + thermion_flutter_platform_interface: + path: ../thermion_flutter_platform_interface + thermion_flutter_ffi: + path: ../thermion_flutter_ffi dev_dependencies: flutter_test: sdk: flutter diff --git a/thermion_flutter/thermion_flutter/windows/backing_window.cpp b/thermion_flutter/thermion_flutter/windows/backing_window.cpp index b98daed9..573e70e4 100644 --- a/thermion_flutter/thermion_flutter/windows/backing_window.cpp +++ b/thermion_flutter/thermion_flutter/windows/backing_window.cpp @@ -152,8 +152,20 @@ LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message, break; } case WM_ERASEBKGND: { - // Prevent erasing of |window| when it is unfocused and minimized or - // moved out of screen etc. + HDC hdc = (HDC)wparam; + RECT rect; + GetClientRect(window, &rect); + + // Get the BackingWindow instance associated with this window + BackingWindow* backing_window = reinterpret_cast( + GetWindowLongPtr(window, GWLP_USERDATA)); + + if (backing_window) { + HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0)); + FillRect(hdc, &rect, brush); + DeleteObject(brush); + } + break; } case WM_SIZE: @@ -346,8 +358,6 @@ void BackingWindow::Resize(int width, int height, int left, int top) { _top = top; RECT flutterViewRect; ::GetWindowRect(_flutterViewWindow, &flutterViewRect); - std::cout << "Resizing to " << _width << " x " << _height << " with LT" << _left << " " << _top << " flutter view rect" << flutterViewRect.left << " " << flutterViewRect.top << " " << flutterViewRect.right << " " << flutterViewRect.bottom << std::endl; - ::SetWindowPos(_windowHandle, _flutterRootWindow, flutterViewRect.left + _left, flutterViewRect.top + _top, _width, _height, SWP_NOACTIVATE); diff --git a/thermion_flutter/thermion_flutter/windows/thermion_flutter_plugin.cpp b/thermion_flutter/thermion_flutter/windows/thermion_flutter_plugin.cpp index f02c3e9b..afb248a3 100644 --- a/thermion_flutter/thermion_flutter/windows/thermion_flutter_plugin.cpp +++ b/thermion_flutter/thermion_flutter/windows/thermion_flutter_plugin.cpp @@ -172,8 +172,6 @@ void ThermionFlutterPlugin::CreateTexture( auto height = (uint32_t)round(dHeight ); auto left = (uint32_t)round(dLeft ); auto top = (uint32_t)round(dTop ); - - std::cout << "Using " << width << "x" << height << std::endl; // create a single shared context for the life of the application // this will be used to create a backing texture and passed to Filament @@ -182,8 +180,10 @@ void ThermionFlutterPlugin::CreateTexture( _context = std::make_unique(_pluginRegistrar, _textureRegistrar); #else _context = std::make_unique(_pluginRegistrar, _textureRegistrar); + std::cout << "Created WGL context" << std::endl; #endif } + _context->CreateRenderingSurface(width, height, std::move(result), left, top); } @@ -210,7 +210,7 @@ void ThermionFlutterPlugin::DestroyTexture( void ThermionFlutterPlugin::HandleMethodCall( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - + std::cout << methodCall.method_name().c_str() << std::endl; if (methodCall.method_name() == "usesBackingWindow") { result->Success(flutter::EncodableValue( #ifdef WGL_USE_BACKING_WINDOW @@ -243,14 +243,16 @@ void ThermionFlutterPlugin::HandleMethodCall( auto height = (uint32_t)round(dHeight ); auto left = (uint32_t)round(dLeft ); auto top = (uint32_t)round(dTop ); + _context->ResizeRenderingSurface(width, height, left, top); + std::cout << "resized window to " << width << "x" << height << " at " << left << "," << top << std::endl; result->Success(); #else result->Error("ERROR", "resizeWindow is only available when using a backing window"); #endif - } else if (methodCall.method_name() == "createTexture") { + } else if (methodCall.method_name() == "createWindow") { CreateTexture(methodCall, std::move(result)); - } else if (methodCall.method_name() == "destroyTexture") { + } else if (methodCall.method_name() == "destroyWindow") { DestroyTexture(methodCall, std::move(result)); } else if (methodCall.method_name() == "getRenderCallback") { flutter::EncodableList resultList; diff --git a/thermion_flutter/thermion_flutter/windows/wgl_context.cpp b/thermion_flutter/thermion_flutter/windows/wgl_context.cpp index 68bbaebd..7cb5d9c6 100644 --- a/thermion_flutter/thermion_flutter/windows/wgl_context.cpp +++ b/thermion_flutter/thermion_flutter/windows/wgl_context.cpp @@ -45,7 +45,7 @@ WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar, 0, 0, 0, - 16, // Number of bits for the depthbuffer + 24, // Number of bits for the depthbuffer 0, // Number of bits for the stencilbuffer 0, // Number of Aux buffers in the framebuffer. PFD_MAIN_PLANE, @@ -117,6 +117,8 @@ void WGLContext::CreateRenderingSurface( } else { ResizeRenderingSurface(width, height, left, top); } + + std::cout << "created window size " << width << "x" << height << " at " << left << "," << top << " with backing handle" << _backingWindow->GetHandle() << std::endl; std::vector resultList; resultList.push_back(flutter::EncodableValue()); // return null for Flutter texture ID resultList.push_back(flutter::EncodableValue()); // return null for hardware texture ID diff --git a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart index a1b4a898..17a4ca93 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart +++ b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_texture_backed_platform.dart @@ -7,16 +7,19 @@ import 'package:thermion_flutter_ffi/thermion_flutter_method_channel_interface.d import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart'; import 'package:logging/logging.dart'; import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart'; +import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart'; import 'platform_texture.dart'; /// /// An implementation of [ThermionFlutterPlatform] that uses /// Flutter platform channels to create a rendering context, -/// resource loaders, and surface/render target(s). +/// resource loaders, and a texture that will be used as a render target +/// for a headless swapchain. /// class ThermionFlutterTextureBackedPlatform extends ThermionFlutterMethodChannelInterface { + final _logger = Logger("ThermionFlutterTextureBackedPlatform"); static SwapChain? _swapChain; @@ -52,11 +55,10 @@ class ThermionFlutterTextureBackedPlatform await texture.resize(width, height, 0, 0); return texture; } - - // On MacOS, we currently use textures/render targets, so there's no window to resize + @override - Future resizeWindow( - int width, int height, int offsetTop, int offsetRight) { + Future createWindow(int width, int height, int offsetLeft, int offsetTop) { + // TODO: implement createWindow throw UnimplementedError(); } } diff --git a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_windows.dart b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_windows.dart index e05be422..3e4608e0 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_windows.dart +++ b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_windows.dart @@ -1,34 +1,45 @@ import 'dart:async'; import 'package:flutter/services.dart'; -import 'dart:ffi'; import 'package:thermion_dart/thermion_dart.dart'; import 'package:thermion_dart/src/viewer/src/ffi/thermion_viewer_ffi.dart'; import 'package:thermion_flutter_ffi/thermion_flutter_method_channel_interface.dart'; import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart'; import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart'; import 'package:logging/logging.dart'; +import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart'; /// -/// An implementation of [ThermionFlutterPlatform] that uses -/// Flutter platform channels to create a rendering context, -/// resource loaders, and surface/render target(s). +/// A Windows-only implementation of [ThermionFlutterPlatform] that uses +/// a Flutter platform channel to create a rendering context, +/// resource loader and a native HWND that will be sit behind the running +/// Flutter application. /// class ThermionFlutterWindows extends ThermionFlutterMethodChannelInterface { + final _channel = const MethodChannel("dev.thermion.flutter/event"); final _logger = Logger("ThermionFlutterWindows"); - ThermionViewerFFI? _viewer; - - ThermionFlutterWindows._() {} + ThermionViewer? _viewer; SwapChain? _swapChain; + ThermionFlutterWindows._() {} + static void registerWith() { ThermionFlutterPlatform.instance = ThermionFlutterWindows._(); } + @override + Future createViewer({ThermionFlutterOptions? options}) async { + if(_viewer != null) { + throw Exception("Only one viewer should be instantiated over the life of the app"); + } + _viewer = await super.createViewer(options: options); + return _viewer!; + } + /// /// Not supported on Windows. Throws an exception. /// @@ -37,64 +48,85 @@ class ThermionFlutterWindows throw UnimplementedError(); } - bool _resizing = false; + + @override + Future createWindow(int width, int height, int offsetLeft, int offsetTop) async { + + var result = await _channel + .invokeMethod("createWindow", [width, height, offsetLeft, offsetLeft]); + + if (result == null || result[2] == -1) { + throw Exception("Failed to create window"); + } + + var window = + ThermionFlutterWindowImpl(result[2], _channel, viewer!); + await window.resize(width, height, offsetLeft, offsetTop); + throw Exception(); + // var view = await _viewer!.getViewAt(0); + // await view.updateViewport(width, height); + // print("Set viewport dimensions to ${width} ${height}"); + // _swapChain = await _viewer!.createSwapChain(window.handle); + // return window; + } + + +} + +class ThermionFlutterWindowImpl extends ThermionFlutterWindow { + + final ThermionViewer viewer; + final int handle; + int height = 0; + int width = 0; + int offsetLeft = 0; + int offsetTop = 0; + final MethodChannel _channel; + + + + ThermionFlutterWindowImpl(this.handle, this._channel, this.viewer); + + @override + Future destroy() async { + await _channel + .invokeMethod("destroyWindow", [width, height, offsetLeft, offsetLeft]); + } + + @override + Future markFrameAvailable() { + // TODO: implement markFrameAvailable + throw UnimplementedError(); + } + + bool _resizing = false; + /// - /// Called by [ThermionWidget] to resize a texture. Don't call this yourself. + /// Called by [ThermionWidget] to resize the window. Don't call this yourself. /// @override - Future resizeWindow( + Future resize( int width, int height, int offsetLeft, int offsetTop) async { if (_resizing) { throw Exception("Resize underway"); } - throw Exception("TODO"); + if (width == this.width && height == this.height) { + return; + } - // final view = await this._viewer!.getViewAt(0); - // final viewport = await view.getViewport(); - // final swapChain = await this._viewer.getSwapChainAt(0); + this.width = width; + this.height = height; + this.offsetLeft = offsetLeft; + this.offsetTop = offsetTop; - // if (width == viewport.width && height - viewport.height == 0) { - // return; - // } + _resizing = true; - // _resizing = true; - // bool wasRendering = _viewer!.rendering; - // await _viewer!.setRendering(false); - // await _swapChain?.destroy(); - - // var result = await _channel - // .invokeMethod("createTexture", [width, height, offsetLeft, offsetLeft]); - - // if (result == null || result[0] == -1) { - // throw Exception("Failed to create texture"); - // } - - // var newTexture = - // ThermionFlutterTexture(result[0], result[1], width, height, result[2]); - - // await _viewer!.createSwapChain(width, height, - // surface: newTexture.surfaceAddress == null - // ? nullptr - // : Pointer.fromAddress(newTexture.surfaceAddress!)); - - // if (newTexture.hardwareTextureId != null) { - // // ignore: unused_local_variable - // var renderTarget = await _viewer! - // .createRenderTarget(width, height, newTexture.hardwareTextureId!); - // } - - // await _viewer! - // .updateViewportAndCameraProjection(width.toDouble(), height.toDouble()); - - // if (wasRendering) { - // await _viewer!.setRendering(true); - // } - // _textures.add(newTexture); - // _resizing = false; - // return newTexture; + await _channel + .invokeMethod("resizeWindow", [width, height, offsetLeft, offsetLeft]); + _resizing = false; } + - -} +} \ No newline at end of file diff --git a/thermion_flutter/thermion_flutter_ffi/pubspec.yaml b/thermion_flutter/thermion_flutter_ffi/pubspec.yaml index a1f1d922..fddaec73 100644 --- a/thermion_flutter/thermion_flutter_ffi/pubspec.yaml +++ b/thermion_flutter/thermion_flutter_ffi/pubspec.yaml @@ -26,7 +26,13 @@ dependencies: thermion_dart: ^0.2.1-dev.0.0.6 logging: ^1.2.0 +dependency_overrides: + thermion_dart: + path: ../../thermion_dart + thermion_flutter_platform_interface: + path: ../thermion_flutter_platform_interface dev_dependencies: + flutter_test: sdk: flutter mockito: ^5.0.0 diff --git a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart index d3e5e39a..4c8a6fd6 100644 --- a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart +++ b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart @@ -4,6 +4,7 @@ import 'package:thermion_dart/thermion_dart.dart' as t; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'package:thermion_dart/thermion_dart.dart'; import 'thermion_flutter_texture.dart'; +import 'thermion_flutter_window.dart'; class ThermionFlutterOptions { final String? uberarchivePath; @@ -41,7 +42,13 @@ abstract class ThermionFlutterPlatform extends PlatformInterface { t.View view, int width, int height); /// + /// Create a rendering window. /// + /// This is internal; unless you are [thermion_*] package developer, don't + /// call this yourself. May not be supported on all platforms. /// - Future resizeWindow(int width, int height, int offsetTop, int offsetRight); + Future createWindow( + int width, int height, int offsetLeft, int offsetTop); + + } diff --git a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_window.dart b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_window.dart new file mode 100644 index 00000000..6a96ba07 --- /dev/null +++ b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_window.dart @@ -0,0 +1,16 @@ +abstract class ThermionFlutterWindow { + + int get width; + int get height; + + int get handle; + + /// + /// Destroy a texture and clean up the texture cache (if applicable). + /// + Future destroy(); + + Future resize(int width, int height, int left, int top); + + Future markFrameAvailable(); +}