diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index e1a7d358..6aeb9a5a 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -150,39 +150,26 @@ namespace flutter_filament _scene = _engine->createScene(); - Log("Scene created"); - utils::Entity camera = EntityManager::get().create(); _mainCamera = _engine->createCamera(camera); - Log("Main camera created"); _view = _engine->createView(); - Log("View created"); - setToneMapping(ToneMapping::ACES); - Log("Set tone mapping"); -#ifdef __EMSCRIPTEN__ - Log("Bloom is disabled on WebGL builds as it causes instability with certain drivers"); - decltype(_view->getBloomOptions()) opts; - opts.enabled = false; - _view->setBloomOptions(opts); + // there's a glitch on certain iGPUs where nothing will render when postprocessing is enabled and bloom is disabled + // set bloom to a small value here + setBloom(0.01); _view->setAmbientOcclusionOptions({.enabled = false}); - _view->setDynamicResolutionOptions({.enabled = false}); _view->setDithering(filament::Dithering::NONE); - _view->setAntiAliasing(filament::AntiAliasing::NONE); + setAntiAliasing(false, false, false); _view->setShadowingEnabled(false); _view->setScreenSpaceRefractionEnabled(false); - -#else - setBloom(0.6f); - Log("Set bloom"); -#endif + setPostProcessing(false); _view->setScene(_scene); _view->setCamera(_mainCamera); @@ -197,17 +184,6 @@ namespace flutter_filament const float sens = _mainCamera->getSensitivity(); Log("Camera aperture %f shutter %f sensitivity %f", aperture, shutterSpeed, sens); - - View::DynamicResolutionOptions options; - options.enabled = false; - // options.homogeneousScaling = homogeneousScaling; - // options.minScale = filament::math::float2{ minScale }; - // options.maxScale = filament::math::float2{ maxScale }; - // options.sharpness = sharpness; - // options.quality = View::QualityLevel::ULTRA; - _view->setDynamicResolutionOptions(options); - - setAntiAliasing(false, true, false); EntityManager &em = EntityManager::get(); @@ -292,14 +268,15 @@ namespace flutter_filament void FilamentViewer::setBloom(float strength) { + decltype(_view->getBloomOptions()) opts; #ifdef __EMSCRIPTEN__ + opts.enabled = false; Log("Bloom is disabled on WebGL builds as it causes instability with certain drivers. setBloom will be ignored"); #else - decltype(_view->getBloomOptions()) opts; opts.enabled = true; opts.strength = strength; - _view->setBloomOptions(opts); #endif + _view->setBloomOptions(opts); } void FilamentViewer::setToneMapping(ToneMapping toneMapping) @@ -335,7 +312,7 @@ namespace flutter_filament { _frameInterval = frameInterval; Renderer::FrameRateOptions fro; - fro.interval = frameInterval; + fro.interval = 1; //frameInterval; _renderer->setFrameRateOptions(fro); Log("Set framerate interval to %f", frameInterval); } @@ -1045,7 +1022,7 @@ namespace flutter_filament if (secsSinceLastFpsCheck >= 1) { auto fps = _frameCount / secsSinceLastFpsCheck; - Log("%ffps", fps); + Log("%ffps (%d skipped)", fps, _skippedFrames); _frameCount = 0; _skippedFrames = 0; _fpsCounterStartTime = now; diff --git a/ios/src/SceneManager.cpp b/ios/src/SceneManager.cpp index 01f312c7..74ac4ea8 100644 --- a/ios/src/SceneManager.cpp +++ b/ios/src/SceneManager.cpp @@ -206,8 +206,8 @@ namespace flutter_filament FilamentAsset *asset = nullptr; if(numInstances > 1) { - FilamentInstance* instances[numInstances]; - asset = _assetLoader->createInstancedAsset((const uint8_t *)data, length, instances, numInstances); + std::vector instances(numInstances); + asset = _assetLoader->createInstancedAsset((const uint8_t *)data, length, instances.data(), numInstances); } else { asset = _assetLoader->createAsset( (const uint8_t *)data, length); diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index b7279374..f62f273b 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -298,6 +298,8 @@ class FilamentControllerFFI extends FilamentController { throw Exception("Failed to get resource loader"); } + var renderingSurface = await _createRenderingSurface(); + if (Platform.isWindows && requiresTextureWidget) { _driver = Pointer.fromAddress( await _channel.invokeMethod("getDriverPlatform")); @@ -310,7 +312,6 @@ class FilamentControllerFFI extends FilamentController { var renderCallbackOwner = Pointer.fromAddress(renderCallbackResult[1]); - var renderingSurface = await _createRenderingSurface(); print("Got rendering surface"); diff --git a/lib/widgets/filament_widget.dart b/lib/widgets/filament_widget.dart index b6343df3..e40f498c 100644 --- a/lib/widgets/filament_widget.dart +++ b/lib/widgets/filament_widget.dart @@ -305,5 +305,5 @@ class TransparencyPainter extends CustomPainter { } @override - bool shouldRepaint(covariant CustomPainter oldDelegate) => true; + bool shouldRepaint(covariant CustomPainter oldDelegate) => false; } diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt index 20bb2560..3180cb8c 100644 --- a/windows/CMakeLists.txt +++ b/windows/CMakeLists.txt @@ -29,7 +29,9 @@ if(USE_ANGLE) add_compile_definitions(USE_ANGLE) list(APPEND PLUGIN_SOURCES "flutter_angle_texture.cpp" "egl_context.cpp" ) else() - add_compile_definitions(WGL_USE_BACKING_WINDOW) + if(WGL_USE_BACKING_WINDOW) + add_compile_definitions(WGL_USE_BACKING_WINDOW) + endif() list(APPEND PLUGIN_SOURCES "wgl_context.cpp" "opengl_texture_buffer.cpp" "backing_window.cpp") # if(WGL_USE_BACKING_WINDOW) # list(APPEND PLUGIN_SOURCES ) diff --git a/windows/backing_window.cpp b/windows/backing_window.cpp index bc314c26..e9668489 100644 --- a/windows/backing_window.cpp +++ b/windows/backing_window.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include diff --git a/windows/egl_context.cpp b/windows/egl_context.cpp index 1e7a87e6..342a4d5f 100644 --- a/windows/egl_context.cpp +++ b/windows/egl_context.cpp @@ -1,8 +1,17 @@ + #include "egl_context.h" +#define FILAMENT_USE_EXTERNAL_GLES3 +#include + +#pragma comment(lib, "dwmapi.lib") +#pragma comment(lib, "comctl32.lib") + namespace flutter_filament { -EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar); { +FlutterEGLContext::FlutterEGLContext( + flutter::PluginRegistrarWindows* pluginRegistrar, + flutter::TextureRegistrar* textureRegistrar) : FlutterRenderContext(pluginRegistrar, textureRegistrar) { _platform = new filament::backend::PlatformEGL(); @@ -34,7 +43,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter dxgi->Release(); if (!adapter_) { std::cout << "Failed to locate default D3D adapter" << std::endl; - return false; + return; } DXGI_ADAPTER_DESC adapter_desc_; @@ -49,7 +58,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter if (FAILED(hr)) { std::cout << "Failed to create D3D device" << std::endl; - return false; + return; } Microsoft::WRL::ComPtr dxgi_device = nullptr; @@ -74,7 +83,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter EGLBoolean bindAPI = eglBindAPI(EGL_OPENGL_ES_API); if (UTILS_UNLIKELY(!bindAPI)) { std::cout << "eglBindAPI EGL_OPENGL_ES_API failed" << std::endl; - return false; + return; } _eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); @@ -106,10 +115,10 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter if (UTILS_UNLIKELY(!initialized)) { std::cout << "eglInitialize failed" << std::endl; - return false; + return; } - importGLESExtensionsEntryPoints(); + glext::importGLESExtensionsEntryPoints(); EGLint configsCount; @@ -126,53 +135,66 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter // find an opaque config if (!eglChooseConfig(_eglDisplay, configAttribs, &_eglConfig, 1, &configsCount)) { - return false; + std::cout << "Failed to find EGL config" << std::endl; + return; } - _context = (void *)eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT, - contextAttribs); + auto ctx = eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT,contextAttribs); + _context = (void*)ctx; if (UTILS_UNLIKELY(_context == EGL_NO_CONTEXT)) { - return false; + return; } } -EGLContext::CreateRenderingSurface( +void FlutterEGLContext::CreateRenderingSurface( uint32_t width, uint32_t height, std::unique_ptr> result, uint32_t left, uint32_t top ) { - importGLESExtensionsEntryPoints(); + + glext::importGLESExtensionsEntryPoints(); if(left != 0 || top != 0) { result->Error("ERROR", "Rendering with EGL uses a Texture render target/Flutter widget and does not need a window offset."); - return false; + return; } if (_active.get()) { result->Error("ERROR", "Texture already exists. You must call destroyTexture before " "attempting to create a new one."); - return false; + return; } - _active = std::make_unique( + std::unique_ptr active = std::make_unique( _pluginRegistrar, _textureRegistrar, std::move(result), width, height, _D3D11Device, _D3D11DeviceContext, _eglConfig, _eglDisplay, _context, [=](size_t width, size_t height) { + std::cout << "RESIZE" << std::endl; std::vector list; list.push_back((int64_t)width); list.push_back((int64_t)height); - auto val = std::make_unique(list); - this->_channel->InvokeMethod("resize", std::move(val), nullptr); + // auto val = std::make_unique(list); + // this->_channel->InvokeMethod("resize", std::move(val), nullptr); }); + _active = std::move(active); - return _active->flutterTextureId != -1; } -EGLContext::GetSharedContext() { - return (void*)_eglContext; +void FlutterEGLContext::RenderCallback() { + if(_active.get()) { + ((FlutterAngleTexture*)_active.get())->RenderCallback(); + } +} + +void* FlutterEGLContext::GetSharedContext() { + return (void*)_context; +} + +void* FlutterEGLContext::GetPlatform() { + return (void*)_platform; } } \ No newline at end of file diff --git a/windows/egl_context.h b/windows/egl_context.h index 454cae0f..c77b7216 100644 --- a/windows/egl_context.h +++ b/windows/egl_context.h @@ -1,24 +1,35 @@ #ifndef _EGL_CONTEXT_H #define _EGL_CONTEXT_H +#include + +#include +#include +#include +#include + #include "flutter_angle_texture.h" #include "backend/platforms/PlatformEGL.h" +#include "flutter_render_context.h" namespace flutter_filament { -class EGLContext : public FlutterRenderingContext { +class FlutterEGLContext : public FlutterRenderContext { public: - EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar); + FlutterEGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar); + void* GetSharedContext(); + void RenderCallback(); + void* GetPlatform(); + void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr> result, uint32_t left, uint32_t top ); + private: - EGLContext _context = NULL; + void* _context = nullptr; EGLConfig _eglConfig = NULL; EGLDisplay _eglDisplay = NULL; - std::unique_ptr _active = nullptr; - std::unique_ptr _inactive = nullptr; ID3D11Device* _D3D11Device = nullptr; ID3D11DeviceContext* _D3D11DeviceContext = nullptr; filament::backend::Platform* _platform = nullptr; -} +}; } diff --git a/windows/flutter_angle_texture.cpp b/windows/flutter_angle_texture.cpp index 96e8d413..832e20e6 100644 --- a/windows/flutter_angle_texture.cpp +++ b/windows/flutter_angle_texture.cpp @@ -236,6 +236,7 @@ FlutterAngleTexture::FlutterAngleTexture( resultList.push_back(flutter::EncodableValue(flutterTextureId)); resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); resultList.push_back(flutter::EncodableValue(glTextureId)); + resultList.push_back(flutter::EncodableValue((int64_t) eglContext)); result->Success(resultList); } diff --git a/windows/flutter_angle_texture.h b/windows/flutter_angle_texture.h index 479d0e1b..c207b7bb 100644 --- a/windows/flutter_angle_texture.h +++ b/windows/flutter_angle_texture.h @@ -28,7 +28,7 @@ typedef uint32_t GLuint; namespace flutter_filament { -class FlutterAngleTexture : FlutterTextureBuffer { +class FlutterAngleTexture : public FlutterTextureBuffer { public: FlutterAngleTexture( flutter::PluginRegistrarWindows* pluginRegistrar, diff --git a/windows/flutter_filament_plugin.cpp b/windows/flutter_filament_plugin.cpp index 7c3d4d96..3670da36 100644 --- a/windows/flutter_filament_plugin.cpp +++ b/windows/flutter_filament_plugin.cpp @@ -3,11 +3,7 @@ #include "flutter_filament_plugin.h" -// This must be included before many other Windows headers. -#include - -// For getPlatformVersion; remove unless needed for your plugin implementation. -#include +#include #include #include @@ -18,7 +14,7 @@ #include #include #include -#include + #include #include #include @@ -31,11 +27,6 @@ #include "FlutterFilamentApi.h" -#include -#include -#include -#include - #include "flutter_render_context.h" #if USE_ANGLE @@ -154,10 +145,16 @@ void render_callback(void *owner) { void FlutterFilamentPlugin::RenderCallback() { if (_context) { auto flutterTextureId = _context->GetFlutterTextureId(); + if(flutterTextureId == -1) { + std::cout << "Bad texture" << std::endl; + return; + } #ifdef USE_ANGLE - _active->RenderCallback(); + _context->RenderCallback(); #endif +#if !WGL_USE_BACKING_WINDOW _textureRegistrar->MarkTextureFrameAvailable(flutterTextureId); +#endif } } @@ -183,7 +180,7 @@ void FlutterFilamentPlugin::CreateTexture( // this will be used to create a backing texture and passed to Filament if (!_context) { #ifdef USE_ANGLE - _context = std::make_unique(_pluginRegistrar); + _context = std::make_unique(_pluginRegistrar, _textureRegistrar); #else _context = std::make_unique(_pluginRegistrar, _textureRegistrar); #endif @@ -195,7 +192,6 @@ void FlutterFilamentPlugin::DestroyTexture( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - const auto *flutterTextureId = std::get_if(methodCall.arguments()); if (!flutterTextureId) { @@ -261,7 +257,7 @@ void FlutterFilamentPlugin::HandleMethodCall( result->Success(resultList); } else if (methodCall.method_name() == "getDriverPlatform") { #ifdef USE_ANGLE - result->Success(flutter::EncodableValue((int64_t)_platform)); + result->Success(flutter::EncodableValue((int64_t)_context->GetPlatform())); #else result->Success(flutter::EncodableValue((int64_t) nullptr)); #endif diff --git a/windows/flutter_filament_plugin.h b/windows/flutter_filament_plugin.h index 95502014..3b918fc1 100644 --- a/windows/flutter_filament_plugin.h +++ b/windows/flutter_filament_plugin.h @@ -16,7 +16,7 @@ #include "FlutterFilamentApi.h" -#if ANGLE +#if USE_ANGLE #include "egl_context.h" #else #include "wgl_context.h" @@ -60,7 +60,7 @@ public: private: #ifdef USE_ANGLE - std::unique_ptr _context = nullptr; + std::unique_ptr _context = nullptr; #else std::unique_ptr _context = nullptr; #endif diff --git a/windows/flutter_render_context.h b/windows/flutter_render_context.h index f1ebd238..16092891 100644 --- a/windows/flutter_render_context.h +++ b/windows/flutter_render_context.h @@ -12,6 +12,7 @@ namespace flutter_filament { class FlutterRenderContext { public: + void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr> result, uint32_t left, uint32_t top ); void DestroyTexture(std::unique_ptr> result) { @@ -33,11 +34,15 @@ namespace flutter_filament { }); } int64_t GetFlutterTextureId() { + if(!_active) { + return -1; + } return _active->flutterTextureId; } - void* sharedContext = nullptr; - + protected: + FlutterRenderContext( flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar) : _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar) {}; + flutter::PluginRegistrarWindows* _pluginRegistrar; flutter::TextureRegistrar* _textureRegistrar; std::unique_ptr _active = nullptr; diff --git a/windows/flutter_texture_buffer.h b/windows/flutter_texture_buffer.h index cde6a228..3bf0c342 100644 --- a/windows/flutter_texture_buffer.h +++ b/windows/flutter_texture_buffer.h @@ -11,7 +11,7 @@ namespace flutter_filament { class FlutterTextureBuffer { public: - int64_t flutterTextureId = 0; + int64_t flutterTextureId = -1; }; } diff --git a/windows/opengl_texture_buffer.cpp b/windows/opengl_texture_buffer.cpp index f261abcb..4b501b21 100644 --- a/windows/opengl_texture_buffer.cpp +++ b/windows/opengl_texture_buffer.cpp @@ -117,11 +117,15 @@ OpenGLTextureBuffer::OpenGLTextureBuffer( flutterTextureId = textureRegistrar->RegisterTexture(texture.get()); std::cout << "Registered Flutter texture ID " << flutterTextureId << std::endl; - std::vector resultList; - resultList.push_back(flutter::EncodableValue(flutterTextureId)); - resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); - resultList.push_back(flutter::EncodableValue(glTextureId)); - result->Success(resultList); + + if (flutterTextureId != -1) { + std::vector resultList; + resultList.push_back(flutter::EncodableValue(flutterTextureId)); + resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); + resultList.push_back(flutter::EncodableValue(glTextureId)); + resultList.push_back(flutter::EncodableValue((int64_t)_context)); + result->Success(resultList); + } } OpenGLTextureBuffer::~OpenGLTextureBuffer() { diff --git a/windows/wgl_context.cpp b/windows/wgl_context.cpp index bf377e8b..44e7209e 100644 --- a/windows/wgl_context.cpp +++ b/windows/wgl_context.cpp @@ -10,94 +10,13 @@ namespace flutter_filament { WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar, flutter::TextureRegistrar *textureRegistrar) - : _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar) { - - auto hwnd = pluginRegistrar->GetView()->GetNativeWindow(); - - HDC whdc = GetDC(hwnd); - if (whdc == NULL) { - std::cout << "No device context for temporary window" << std::endl; - return; - } - - std::cout << "No GL context exists, creating" << std::endl; - - PIXELFORMATDESCRIPTOR pfd = { - sizeof(PIXELFORMATDESCRIPTOR), - 1, - PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags - PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette. - 32, // Colordepth of the framebuffer. - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 32, // Number of bits for the depthbuffer - 0, // Number of bits for the stencilbuffer - 0, // Number of Aux buffers in the framebuffer. - PFD_MAIN_PLANE, - 0, - 0, - 0, - 0}; - - int pixelFormat = ChoosePixelFormat(whdc, &pfd); - SetPixelFormat(whdc, pixelFormat, &pfd); - - // We need a tmp context to retrieve and call wglCreateContextAttribsARB. - HGLRC tempContext = wglCreateContext(whdc); - if (!wglMakeCurrent(whdc, tempContext)) { - std::cout << "Failed to acquire temporary context" << std::endl; - return; - } - - GLenum err = glGetError(); - - if (err != GL_NO_ERROR) { - std::cout << "GL Error @ 455 %d" << std::endl; - return; - } - - PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = nullptr; - - wglCreateContextAttribs = - (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress( - "wglCreateContextAttribsARB"); - - if (!wglCreateContextAttribs) { - std::cout << "Failed to resolve wglCreateContextAttribsARB" << std::endl; - return; - } - - for (int minor = 5; minor >= 1; minor--) { - std::vector mAttribs = {WGL_CONTEXT_MAJOR_VERSION_ARB, 4, - WGL_CONTEXT_MINOR_VERSION_ARB, minor, 0}; - _context = wglCreateContextAttribs(whdc, nullptr, mAttribs.data()); - if (_context) { - break; - } - } - - wglMakeCurrent(NULL, NULL); - wglDeleteContext(tempContext); - - if (!_context || !wglMakeCurrent(whdc, _context)) { - std::cout << "Failed to create OpenGL context." << std::endl; - return; - } + : FlutterRenderContext(pluginRegistrar, textureRegistrar) { } void WGLContext::ResizeRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top) { + #if WGL_USE_BACKING_WINDOW _backingWindow->Resize(width, height, left, top); + #endif } void WGLContext::CreateRenderingSurface( @@ -116,7 +35,7 @@ void WGLContext::CreateRenderingSurface( resultList.push_back( flutter::EncodableValue((int64_t)_backingWindow->GetHandle())); resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); - resultList.push_back(flutter::EncodableValue((int64_t)sharedContext)); + resultList.push_back(flutter::EncodableValue((int64_t)_context)); result->Success(resultList); #else if(left != 0 || top != 0) { @@ -128,20 +47,10 @@ void WGLContext::CreateRenderingSurface( "attempting to create a new one."); } else { - _active = std::make_unique( + auto active = std::make_unique( _pluginRegistrar, _textureRegistrar, std::move(result), width, height, _context); - - if (_active->flutterTextureId != -1) { - std::vector resultList; - resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); - resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); - resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); - resultList.push_back(flutter::EncodableValue((int64_t)sharedContext)); - result->Success(resultList); - } else { - result->Error("NO_FLUTTER_TEXTURE", "Unknown error registering texture with Flutter.", nullptr); - } + _active = std::move(active); } #endif } diff --git a/windows/wgl_context.h b/windows/wgl_context.h index e08e5f3d..33406180 100644 --- a/windows/wgl_context.h +++ b/windows/wgl_context.h @@ -20,9 +20,6 @@ namespace flutter_filament { uint32_t width, uint32_t height, uint32_t left, uint32_t top ); private: - - flutter::PluginRegistrarWindows* _pluginRegistrar = nullptr; - flutter::TextureRegistrar* _textureRegistrar = nullptr; HGLRC _context = NULL; #if WGL_USE_BACKING_WINDOW std::unique_ptr _backingWindow = nullptr;