#15098: Add headless mode with software OpenGL rendering

This commit is contained in:
Magne Sjaastad committed 2026-11-03 15:23:31 +01:00
1 parent 7b5a977401
commit 278248b71d
10 files changed
+280 -35

No files matched your search

+16
View File
@@ -239,6 +239,14 @@ if(UNIX AND NOT APPLE)
target_link_libraries(ResInsight PRIVATE xcb)
endif()
if(MSVC)
# Delay-load opengl32.dll so that the statically linked OpenGL calls can be
# redirected to the software rasterizer opengl32sw.dll in headless mode, see
# riaDelayLoadHook in RiaMainTools.cpp
target_link_libraries(ResInsight PRIVATE delayimp)
target_link_options(ResInsight PRIVATE /DELAYLOAD:opengl32.dll)
endif()
# ##############################################################################
# Unity builds
# ##############################################################################
@@ -421,6 +429,14 @@ if(RESINSIGHT_PRIVATE_INSTALL)
RUNTIME_DEPENDENCIES PRE_EXCLUDE_REGEXES "api-ms-" "ext-ms-"
POST_EXCLUDE_REGEXES ".*system32/.*\\.dll"
)
# The software OpenGL rasterizer is loaded dynamically (used by --headless),
# so it is not picked up by the RUNTIME_DEPENDENCIES scan above
install(
FILES $<TARGET_FILE_DIR:ResInsight>/opengl32sw.dll
DESTINATION ${RESINSIGHT_INSTALL_FOLDER}
OPTIONAL
)
endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+12
View File
@@ -86,6 +86,7 @@ void riaFilteredMessageHandler( QtMsgType type, const QMessageLogContext& contex
RiaApplication* createApplication( int& argc, char* argv[] )
{
bool isHeadless = false;
for ( int i = 1; i < argc; ++i )
{
if ( !qstrcmp( argv[i], "--console" ) || !qstrcmp( argv[i], "--unittest" ) )
@@ -96,7 +97,18 @@ RiaApplication* createApplication( int& argc, char* argv[] )
return new RiaConsoleApplication( argc, argv );
#endif
}
if ( !qstrcmp( argv[i], "--headless" ) )
{
isHeadless = true;
}
}
if ( isHeadless )
{
RiaMainTools::enableHeadlessRendering();
}
#ifdef ENABLE_GRPC
return new RiaGrpcGuiApplication( argc, argv );
#else
+90
View File
@@ -19,6 +19,7 @@
#include "RiaMainTools.h"
#include "RiaBaseDefs.h"
#include "RiaFileLogger.h"
#include "RiaGuiApplication.h"
#include "RiaLogging.h"
#include "RiaOpenTelemetryManager.h"
#include "RiaPreferencesOpenTelemetry.h"
@@ -41,6 +42,7 @@
#include <cstdlib>
#include <deque>
#include <exception>
#include <iostream>
#include <map>
#include <sstream>
#include <thread>
@@ -73,6 +75,74 @@
#endif
#endif
#ifdef WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <delayimp.h>
#endif
#ifdef WIN32
//--------------------------------------------------------------------------------------------------
/// In headless mode Qt creates its OpenGL context using the software rasterizer opengl32sw.dll
/// (Qt::AA_UseSoftwareOpenGL), while the statically linked OpenGL calls in the visualization
/// framework would go to the system opengl32.dll where no context is current, causing an endless
/// loop in glGetError(). opengl32.dll is therefore delay-loaded (see CMakeLists.txt) and redirected
/// here to the same software rasterizer.
//--------------------------------------------------------------------------------------------------
FARPROC WINAPI riaDelayLoadHook( unsigned dliNotify, PDelayLoadInfo pdli )
{
if ( dliNotify == dliNotePreLoadLibrary && RiaGuiApplication::isHeadless() &&
_stricmp( pdli->szDll, "opengl32.dll" ) == 0 )
{
if ( HMODULE softwareOpenGl = LoadLibraryW( L"opengl32sw.dll" ) )
{
return reinterpret_cast<FARPROC>( softwareOpenGl );
}
}
return nullptr;
}
extern "C" const PfnDliHook __pfnDliNotifyHook2 = riaDelayLoadHook;
//--------------------------------------------------------------------------------------------------
/// ResInsight is built as a GUI application on Windows, so stdout is only available when redirected to a file or
/// pipe. Attach to the console of the launching process (e.g. PowerShell or cmd) so headless log output is visible.
//--------------------------------------------------------------------------------------------------
void riaAttachToParentConsole()
{
// Only files and pipes are real redirections. A GUI process launched from a console (e.g. Windows Terminal) can
// inherit the console handles of the parent without being attached to the console, and writing to those blocks
auto isRedirected = []( DWORD stdHandle )
{
HANDLE handle = GetStdHandle( stdHandle );
if ( handle == nullptr || handle == INVALID_HANDLE_VALUE ) return false;
const DWORD fileType = GetFileType( handle );
return fileType == FILE_TYPE_DISK || fileType == FILE_TYPE_PIPE;
};
const bool stdOutRedirected = isRedirected( STD_OUTPUT_HANDLE );
const bool stdErrRedirected = isRedirected( STD_ERROR_HANDLE );
if ( stdOutRedirected && stdErrRedirected ) return;
if ( !AttachConsole( ATTACH_PARENT_PROCESS ) ) return;
FILE* stream = nullptr;
if ( !stdOutRedirected ) freopen_s( &stream, "CONOUT$", "w", stdout );
if ( !stdErrRedirected ) freopen_s( &stream, "CONOUT$", "w", stderr );
std::cout.clear();
std::cerr.clear();
}
#endif
namespace internal
{
#if RIA_HAS_STD_STACKTRACE
@@ -516,4 +586,24 @@ void deleteStaleSettingsLockFiles()
}
}
//--------------------------------------------------------------------------------------------------
/// Configure software OpenGL rendering for headless mode, allowing 3D views to be rendered and
/// grabbed without a GPU and without showing any windows. Must be called before the QApplication
/// object is created.
//--------------------------------------------------------------------------------------------------
void enableHeadlessRendering()
{
// Use software OpenGL rendering so 3D views can be rendered without a GPU. On Windows this
// makes Qt load opengl32sw.dll (Mesa llvmpipe). Must be set before the QApplication exists.
QApplication::setAttribute( Qt::AA_UseSoftwareOpenGL );
#ifndef WIN32
// On Linux the attribute has no effect, force Mesa software rendering instead
qputenv( "LIBGL_ALWAYS_SOFTWARE", "1" );
#else
riaAttachToParentConsole();
#endif
RiaGuiApplication::enableHeadlessMode();
}
} // namespace RiaMainTools
+1
View File
@@ -26,6 +26,7 @@ namespace RiaMainTools
void initializeSingletons();
void releaseSingletonAndFactoryObjects();
void deleteStaleSettingsLockFiles();
void enableHeadlessRendering();
// True once crash logging has started. The crash handler pumps the Qt event loop while flushing
// telemetry, so timer-driven application logic must check this flag and stay idle to avoid