Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows cmake debug build #976

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ set(CMAKE_CXX_STANDARD 14)
option(NATRON_SYSTEM_LIBS "use system versions of dependencies instead of bundled ones" OFF)
option(NATRON_BUILD_TESTS "build the Natron test suite" ON)

set(IS_DEBUG_BUILD OFF)
if(CMAKE_BUILD_TYPE MATCHES "^(debug|Debug|DEBUG)$")
set(IS_DEBUG_BUILD ON)
add_definitions(-DDEBUG)

if(WIN32)
# Debug builds need minimal optimizations to avoid excessive link times and link errors related to Eigen.
add_compile_options(-O)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option is set in the qmake builds used to build the installer. That is where I got the idea to do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it only useful on WIN32?

Copy link
Collaborator Author

@acolwell acolwell Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't appear to be required on Linux. The Eigen linking errors I'm seeing on Windows do not appear to occur on Linux. I'm not entirely sure why. My guess is that Windows is not inlining the Eigen code for some reason but Linux is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no linking issues on Linux with CMake.

endif()
else()
add_definitions(-DQT_NO_DEBUG_OUTPUT)
endif()
Expand All @@ -49,7 +56,21 @@ if(WIN32)
endif()
find_package(Python3 COMPONENTS Interpreter Development)
find_package(Qt5 5.15 CONFIG REQUIRED COMPONENTS Core Gui Network Widgets Concurrent)

if(IS_DEBUG_BUILD AND WIN32)
# Explicitly setting SHIBOKEN_PYTHON_LIBRARIES variable to avoid PYTHON_DEBUG_LIBRARY-NOTFOUND
# link errors on Windows debug builds.
set(SHIBOKEN_PYTHON_LIBRARIES ${Python3_LIBRARIES})
endif()
find_package(Shiboken2 5.15 CONFIG REQUIRED COMPONENTS libshiboken2 shiboken2)

if(IS_DEBUG_BUILD AND WIN32)
# Remove NDEBUG from Shiboken2 INTERFACE_COMPILE_DEFINITIONS so it is not inherited in debug builds.
get_property(ShibokenInterfaceDefs TARGET Shiboken2::libshiboken PROPERTY INTERFACE_COMPILE_DEFINITIONS)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because /mingw64/lib/cmake/Shiboken2-5.15.13/Shiboken2Targets.cmake has code that unconditionally sets NDEBUG. :(

list(REMOVE_ITEM ShibokenInterfaceDefs NDEBUG)
set_property(TARGET Shiboken2::libshiboken PROPERTY INTERFACE_COMPILE_DEFINITIONS ShibokenInterfaceDefs)
endif()

find_package(PySide2 5.15 CONFIG REQUIRED COMPONENTS pyside2)
set(QT_VERSION_MAJOR 5)
get_target_property(PYSIDE_INCLUDE_DIRS PySide2::pyside2 INTERFACE_INCLUDE_DIRECTORIES)
Expand Down
33 changes: 25 additions & 8 deletions Engine/OSGLContext_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;

NATRON_NAMESPACE_ENTER

namespace {

bool makeDCAndContextCurrent(HDC dc, HGLRC context)
{
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();

assert(wglInfo);
return wglInfo->MakeCurrent(dc, context);
}

} // namespace

bool
OSGLContext_win::extensionSupported(const char* extension,
OSGLContext_wgl_data* data)
Expand Down Expand Up @@ -96,6 +108,7 @@ OSGLContext_win::initWGLData(OSGLContext_wgl_data* wglInfo)
wglInfo->CreateContext = (WGLCREATECONTEXT_T)GetProcAddress(wglInfo->instance, "wglCreateContext");
wglInfo->DeleteContext = (WGLDELETECONTEXT_T)GetProcAddress(wglInfo->instance, "wglDeleteContext");
wglInfo->GetCurrentContext = (WGLGETCURRENTCONTEXT_T)GetProcAddress(wglInfo->instance, "wglGetCurrentContext");
wglInfo->GetCurrentDC = (WGLGETCURRENTDC_T)GetProcAddress(wglInfo->instance, "wglGetCurrentDC");
wglInfo->GetProcAddress = (WGLGETPROCADDRESS_T)GetProcAddress(wglInfo->instance, "wglGetProcAddress");
wglInfo->MakeCurrent = (WGLMAKECURRENT_T)GetProcAddress(wglInfo->instance, "wglMakeCurrent");
wglInfo->ShareLists = (WGLSHARELISTS_T)GetProcAddress(wglInfo->instance, "wglShareLists");
Expand Down Expand Up @@ -606,18 +619,16 @@ OSGLContext_win::~OSGLContext_win()
bool
OSGLContext_win::makeContextCurrent(const OSGLContext_win* context)
{
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();

assert(wglInfo);
if (context) {
return wglInfo->MakeCurrent(context->_dc, context->_handle);
return makeDCAndContextCurrent(context->_dc, context->_handle);
} else {
return wglInfo->MakeCurrent(0, 0);
return makeDCAndContextCurrent(0, 0);
}
}

bool
OSGLContext_win::threadHasACurrentContext() {
OSGLContext_win::threadHasACurrentContext()
{
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();
assert(wglInfo);
return wglInfo->GetCurrentContext() != nullptr;
Expand Down Expand Up @@ -722,7 +733,11 @@ namespace {
class ScopedGLContext {
public:
ScopedGLContext(const GLRendererID& gid) {
assert(!OSGLContext_win::threadHasACurrentContext());
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();
assert(wglInfo);
_oldDc = wglInfo->GetCurrentDC();
_oldContext = wglInfo->GetCurrentContext();

try {
_context = std::make_unique<OSGLContext_win>(FramebufferConfig(), GLVersion.major, GLVersion.minor, false, gid, nullptr);
} catch (const std::exception& e) {
Expand All @@ -737,14 +752,16 @@ class ScopedGLContext {

~ScopedGLContext() {
if (_context) {
OSGLContext_win::makeContextCurrent(nullptr);
makeDCAndContextCurrent(_oldDc, _oldContext);
}
}

explicit operator bool() const { return (bool)_context; }

private:
std::unique_ptr<OSGLContext_win> _context;
HDC _oldDc = nullptr;
HGLRC _oldContext = nullptr;
};
} // namespace

Expand Down
2 changes: 2 additions & 0 deletions Engine/OSGLContext_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ typedef PROC (WINAPI * WGLGETPROCADDRESS_T)(LPCSTR);
typedef BOOL (WINAPI * WGLMAKECURRENT_T)(HDC, HGLRC);
typedef BOOL (WINAPI * WGLSHARELISTS_T)(HGLRC, HGLRC);
typedef HGLRC (WINAPI * WGLGETCURRENTCONTEXT_T)();
typedef HDC (WINAPI * WGLGETCURRENTDC_T)();

////////// https://www.opengl.org/registry/specs/NV/gpu_affinity.txt

Expand Down Expand Up @@ -157,6 +158,7 @@ struct OSGLContext_wgl_data
WGLCREATECONTEXT_T CreateContext;
WGLDELETECONTEXT_T DeleteContext;
WGLGETCURRENTCONTEXT_T GetCurrentContext;
WGLGETCURRENTDC_T GetCurrentDC;
WGLGETPROCADDRESS_T GetProcAddress;
WGLMAKECURRENT_T MakeCurrent;
WGLSHARELISTS_T ShareLists;
Expand Down