Skip to content

Commit

Permalink
backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Meakk committed Sep 26, 2024
1 parent 64b1b6c commit 718b9e0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 12 deletions.
1 change: 1 addition & 0 deletions application/F3DOptionsTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ static inline const std::array<CLIGroup, 8> CLIOptions = {{
{ "config", "", "Specify the configuration file to use. absolute/relative path or filename/filestem to search in configuration file locations", "<filePath/filename/fileStem>", "" },
{ "dry-run", "", "Do not read the configuration file", "<bool>", "1" },
{ "no-render", "", "Do not read the configuration file", "<bool>", "1" },
{ "offscreen-backend", "", "Backend to use when rendering offscreen (native|egl|osmesa)", "<string>", "native" },
{ "max-size", "", "Maximum size in Mib of a file to load, negative value means unlimited", "<size in Mib>", "" },
{ "watch", "", "Watch current file and automatically reload it whenever it is modified on disk", "<bool>", "1" },
{ "load-plugins", "", "List of plugins to load separated with a comma", "<paths or names>", "" },
Expand Down
1 change: 1 addition & 0 deletions application/F3DOptionsTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static inline const OptionsDict DefaultAppOptions = {
{ "config", "" },
{ "dry-run", "false" },
{ "no-render", "false" },
{ "offscreen-backend", "native" },
{ "max-size", "-1.0" },
{ "watch", "false" },
{ "load-plugins", "" },
Expand Down
28 changes: 27 additions & 1 deletion application/F3DStarter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class F3DStarter::F3DInternals
std::string Output;
bool NoBackground;
bool NoRender;
std::string OffscreenBackend;
double MaxSize;
bool Watch;
std::vector<std::string> Plugins;
Expand Down Expand Up @@ -515,6 +516,8 @@ class F3DStarter::F3DInternals
this->AppOptions.Output = f3d::options::parse<std::string>(appOptions.at("output"));
this->AppOptions.NoBackground = f3d::options::parse<bool>(appOptions.at("no-background"));
this->AppOptions.NoRender = f3d::options::parse<bool>(appOptions.at("no-render"));
this->AppOptions.OffscreenBackend =
f3d::options::parse<std::string>(appOptions.at("offscreen-backend"));
this->AppOptions.MaxSize = f3d::options::parse<double>(appOptions.at("max-size"));
this->AppOptions.Watch = f3d::options::parse<bool>(appOptions.at("watch"));
this->AppOptions.Plugins = { f3d::options::parse<std::vector<std::string>>(
Expand Down Expand Up @@ -707,7 +710,30 @@ int F3DStarter::Start(int argc, char** argv)
else
{
bool offscreen = !reference.empty() || !output.empty();
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createNative(offscreen));

if (!offscreen)
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createNative(offscreen));
}
else
{
if (this->Internals->AppOptions.OffscreenBackend == "egl")
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createEGL());
}
else if (this->Internals->AppOptions.OffscreenBackend == "osmesa")
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createOSMesa());
}
else
{
if (this->Internals->AppOptions.OffscreenBackend != "native")
{
f3d::log::warn("--offscreen-backend value is invalid, falling back to \"native\"");
}
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createNative(true));
}
}

f3d::window& window = this->Internals->Engine->getWindow();
window.setWindowName(F3D::AppTitle).setIcon(F3DIcon, sizeof(F3DIcon));
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/python/tkinter/minimal_tkinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self):

# Initialize F3D
def initgl(self):
self.engine = f3d.Engine.create_external_glx() # may use EGL context
self.engine = f3d.Engine.create_external_native() # may use EGL context
self.engine.loader.load_geometry(
f3d.Mesh(
points=[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0],
Expand Down
2 changes: 1 addition & 1 deletion library/public/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class F3D_EXPORT engine
/**
* Create an engine with an EGL window.
* If several GPU are available, the environment variable
* `VTK_DEFAULT_EGL_DEVICE_INDEX` allows its selection.
* `VTK_DEFAULT_EGL_DEVICE_INDEX` allows its selection.
*/
static engine createEGL();

Expand Down
3 changes: 2 additions & 1 deletion library/public/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class F3D_EXPORT window
* NONE: A mock window without rendering capabilities.
* NATIVE: A window using the native graphical stack.
* NATIVE_OFFSCREEN: A native window rendering to an offscreen buffer, not visible on screen.
* EXTERNAL: An external window that assume the OpenGL context would have been created by another framework
* EXTERNAL: An external window that assume the OpenGL context would have been created by another
* framework.
* EGL: An offscreen window using hardware acceleration that can run headless.
* OSMESA: An offscreen window using software rendering that can run headless.
*/
Expand Down
3 changes: 1 addition & 2 deletions library/src/context.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ context::function getSymbol(const std::string& lib, const std::string& func)

using symbol = context::fptr (*)(const char*);

symbol address =
reinterpret_cast<symbol>(vtksys::DynamicLoader::GetSymbolAddress(handle, func));
symbol address = reinterpret_cast<symbol>(vtksys::DynamicLoader::GetSymbolAddress(handle, func));

if (!address)
{
Expand Down
13 changes: 12 additions & 1 deletion library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <vtkCallbackCommand.h>
#include <vtkCellPicker.h>
#include <vtkGenericRenderWindowInteractor.h>
#include <vtkMath.h>
#include <vtkMatrix3x3.h>
#include <vtkNew.h>
Expand Down Expand Up @@ -40,6 +41,16 @@ class interactor_impl::internals
, Window(window)
, Loader(loader)
{
if (window.getType() == window::Type::NATIVE ||
window.getType() == window::Type::NATIVE_OFFSCREEN)
{
this->VTKInteractor = vtkRenderWindowInteractor::New();
}
else
{
this->VTKInteractor = vtkGenericRenderWindowInteractor::New();
}

#ifdef __EMSCRIPTEN__
vtkRenderWindowInteractor::InteractorManagesTheEventLoop = false;
#endif
Expand Down Expand Up @@ -556,7 +567,7 @@ class interactor_impl::internals
loader_impl& Loader;
animationManager* AnimationManager;

vtkNew<vtkRenderWindowInteractor> VTKInteractor;
vtkSmartPointer<vtkRenderWindowInteractor> VTKInteractor;
vtkNew<vtkF3DInteractorStyle> Style;
vtkSmartPointer<vtkF3DInteractorEventRecorder> Recorder;
std::map<unsigned long, std::pair<int, std::function<void()>>> TimerCallBacks;
Expand Down
21 changes: 17 additions & 4 deletions library/src/window_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "vtkF3DNoRenderWindow.h"
#include "vtkF3DRendererWithColoring.h"

#include <vtkRenderingOpenGLConfigure.h>
#include <vtkCamera.h>
#include <vtkImageData.h>
#include <vtkImageExport.h>
Expand All @@ -20,6 +19,7 @@
#include <vtkPointGaussianMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRendererCollection.h>
#include <vtkRenderingOpenGLConfigure.h>
#include <vtkVersion.h>
#include <vtkWindowToImageFilter.h>
#include <vtksys/SystemTools.hxx>
Expand All @@ -28,6 +28,11 @@
#include <vtkEGLRenderWindow.h>
#endif

#if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 3, 20240914)
#include "vtkglad/include/glad/egl.h"
#include <vtkOSOpenGLRenderWindow.h>
#endif

#if F3D_MODULE_EXTERNAL_RENDERING
#include <vtkExternalOpenGLRenderWindow.h>
#endif
Expand Down Expand Up @@ -194,16 +199,24 @@ window_impl::window_impl(const options& options, Type type, const context::funct
}
else if (type == Type::EGL)
{
#ifdef VTK_OPENGL_HAS_EGL
#if defined(VTK_OPENGL_HAS_EGL) && VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 3, 20240914)
if (!gladLoaderLoadEGL(EGL_NO_DISPLAY))
{
throw engine::no_window_exception("Cannot load EGL library");
}
this->Internals->RenWin = vtkSmartPointer<vtkEGLRenderWindow>::New();
#else
throw engine::no_window_exception(
"Window type is EGL but VTK EGL support is not enabled");
throw engine::no_window_exception("Window type is EGL but VTK EGL support is not enabled");
#endif
}
else if (type == Type::OSMESA)
{
#if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 3, 20240914)
this->Internals->RenWin = vtkSmartPointer<vtkOSOpenGLRenderWindow>::New();
#else
throw engine::no_window_exception(
"Window type is OSMesa but VTK OSMesa support is not enabled");
#endif
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ PYBIND11_MODULE(pyf3d, module)
py::arg("force_std_err") = false)
.def_static("set_use_coloring", &f3d::log::setUseColoring)
.def_static("print",
[](f3d::log::VerboseLevel& level, const std::string& message)
[](f3d::log::VerboseLevel& level, const std::string& message)
{ f3d::log::print(level, message); });

py::enum_<f3d::log::VerboseLevel>(log, "VerboseLevel")
Expand Down

0 comments on commit 718b9e0

Please sign in to comment.