Merge branch 'feature/instancing' of github.com:nmfisher/polyvox_filament into feature/instancing

This commit is contained in:
Nick Fisher
2024-03-08 18:02:14 +08:00
17 changed files with 115 additions and 191 deletions

View File

@@ -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;

View File

@@ -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<FilamentInstance*> instances(numInstances);
asset = _assetLoader->createInstancedAsset((const uint8_t *)data, length, instances.data(), numInstances);
} else {
asset = _assetLoader->createAsset(
(const uint8_t *)data, length);

View File

@@ -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<Void>.fromAddress(
await _channel.invokeMethod("getDriverPlatform"));
@@ -310,7 +312,6 @@ class FilamentControllerFFI extends FilamentController {
var renderCallbackOwner =
Pointer<Void>.fromAddress(renderCallbackResult[1]);
var renderingSurface = await _createRenderingSurface();
print("Got rendering surface");

View File

@@ -305,5 +305,5 @@ class TransparencyPainter extends CustomPainter {
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}

View File

@@ -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 )

View File

@@ -5,7 +5,6 @@
#include <chrono>
#include <thread>
#include <Commctrl.h>
#include <Windows.h>
#include <dwmapi.h>
#include <ShObjIdl.h>

View File

@@ -1,8 +1,17 @@
#include "egl_context.h"
#define FILAMENT_USE_EXTERNAL_GLES3
#include <gl_headers.h>
#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<IDXGIDevice> 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<flutter::MethodResult<flutter::EncodableValue>> 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<FlutterAngleTexture>(
std::unique_ptr<FlutterTextureBuffer> active = std::make_unique<FlutterAngleTexture>(
_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<int64_t> list;
list.push_back((int64_t)width);
list.push_back((int64_t)height);
auto val = std::make_unique<flutter::EncodableValue>(list);
this->_channel->InvokeMethod("resize", std::move(val), nullptr);
// auto val = std::make_unique<flutter::EncodableValue>(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;
}
}

View File

@@ -1,24 +1,35 @@
#ifndef _EGL_CONTEXT_H
#define _EGL_CONTEXT_H
#include <Windows.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/texture_registrar.h>
#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<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top );
private:
EGLContext _context = NULL;
void* _context = nullptr;
EGLConfig _eglConfig = NULL;
EGLDisplay _eglDisplay = NULL;
std::unique_ptr<FlutterAngleTexture> _active = nullptr;
std::unique_ptr<FlutterAngleTexture> _inactive = nullptr;
ID3D11Device* _D3D11Device = nullptr;
ID3D11DeviceContext* _D3D11DeviceContext = nullptr;
filament::backend::Platform* _platform = nullptr;
}
};
}

View File

@@ -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);
}

View File

@@ -28,7 +28,7 @@ typedef uint32_t GLuint;
namespace flutter_filament {
class FlutterAngleTexture : FlutterTextureBuffer {
class FlutterAngleTexture : public FlutterTextureBuffer {
public:
FlutterAngleTexture(
flutter::PluginRegistrarWindows* pluginRegistrar,

View File

@@ -3,11 +3,7 @@
#include "flutter_filament_plugin.h"
// This must be included before many other Windows headers.
#include <windows.h>
// For getPlatformVersion; remove unless needed for your plugin implementation.
#include <VersionHelpers.h>
#include <Windows.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
@@ -18,7 +14,7 @@
#include <cstring>
#include <filesystem>
#include <fstream>
#include <future>
#include <iostream>
#include <locale>
#include <map>
@@ -31,11 +27,6 @@
#include "FlutterFilamentApi.h"
#include <Commctrl.h>
#include <Windows.h>
#include <dwmapi.h>
#include <wrl.h>
#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<EGLContext>(_pluginRegistrar);
_context = std::make_unique<FlutterEGLContext>(_pluginRegistrar, _textureRegistrar);
#else
_context = std::make_unique<WGLContext>(_pluginRegistrar, _textureRegistrar);
#endif
@@ -195,7 +192,6 @@ void FlutterFilamentPlugin::DestroyTexture(
const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
const auto *flutterTextureId = std::get_if<int64_t>(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

View File

@@ -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<EGLContext> _context = nullptr;
std::unique_ptr<FlutterEGLContext> _context = nullptr;
#else
std::unique_ptr<WGLContext> _context = nullptr;
#endif

View File

@@ -12,6 +12,7 @@ namespace flutter_filament {
class FlutterRenderContext {
public:
void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top );
void DestroyTexture(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> 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<FlutterTextureBuffer> _active = nullptr;

View File

@@ -11,7 +11,7 @@ namespace flutter_filament {
class FlutterTextureBuffer {
public:
int64_t flutterTextureId = 0;
int64_t flutterTextureId = -1;
};
}

View File

@@ -117,11 +117,15 @@ OpenGLTextureBuffer::OpenGLTextureBuffer(
flutterTextureId = textureRegistrar->RegisterTexture(texture.get());
std::cout << "Registered Flutter texture ID " << flutterTextureId
<< std::endl;
std::vector<flutter::EncodableValue> 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<flutter::EncodableValue> 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() {

View File

@@ -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<int> 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<OpenGLTextureBuffer>(
auto active = std::make_unique<OpenGLTextureBuffer>(
_pluginRegistrar, _textureRegistrar, std::move(result), width, height,
_context);
if (_active->flutterTextureId != -1) {
std::vector<flutter::EncodableValue> 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
}

View File

@@ -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> _backingWindow = nullptr;