#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

+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