mirror of
https://github.com/blender/blender
synced 2026-09-29 04:37:17 +03:00
For now more than 20 years, Blender has been communicating with the host operating system through the GHOST module. At the time of its creation, Blender was still mostly written in C. As GHOST relied on C++ virtual inheritance to define abstract interfaces that are then implemented for each operating system, a C API had to be put in place to allow calling of such C++ functions from C, using opaque void pointer types to hold references to the abstract C++ interface types. Over the years, with Blender transitioning to C++, the C interface has become almost completely obsolete, hard to maintain, and is a common source of confusion and mistakes. With every GHOST method having to be implemented both in the abstract interface, common base class, OS specific implementation *and* in the C-API, to then be called from C++ through opaque handles, maintenance and expansion of this system has become needlessly difficult. In response to this problem, this PR refactors the GHOST module internals and the Blender calling code (in the WM, XR, GPU, and general OS dependent modules) to directly use and call the GHOST C++ class interfaces methods. Making for a simpler, cleaner and less error prone usage of the module. As well as providing better insight, IDE support and understanding of the module functionalities, and removing the need for opaque handle types and void pointers casting. Part of the 2025 Platform & Builds Code Quality Project (#151316) ## Main Changes ### Removal of opaque types and port of GHOST Types to C++ The GHOST opaque type handles (`GHOST_SystemHandle`, `GHOST_WindowHandle`, etc..) were removed in favor of directly accessing using and accessing their parent interface class. In addition to this, the main `GHOST_Types.h` header file was refactored to C++ (and renamed to `.hh`), dropping use of `typedef` for struct, enum and type alias and replacing them with direct C++ type definition and `using` declaration, as well as generally cleaning up the file from now outdated macros. ### Removal of the GHOST C-API interface, and port of calling code to C++ API The GHOST C-API interface (GHOST_C-api.h / GHOST_C-api.cc) and GHOST Path API (GHOST_Path-api.hh / GHOST_Path-api.cc) were removed in favor of directly using the GHOST interface class in the Blender calling code, making for a simpler and clearer API, allowing for clear and limited GHOST header includes, and generally simplifying the code. Some example of the new C++ API include (before/after): In general, throughout the code, number of casting is reduced. For example, in the WM code, a single `GHOST_IWindow *ghost_window` object can be created from `win->runtime->ghostwin` and be reused in a function, rather than casting the handle each time: In addition, certain C++ objects, such as GHOST_Rects, can be directly instantiated instead of being heap allocated: ### Replacement of void pointers in GPU backend code in favor GHOST interface pointer types With GHOST Types now being directly instantiable from within Blender code, the GPU backend code was improved in favor of replacing `void *` types referencing the GHOST graphic context with actual `GHOST_IContext *` interface pointer types, improving both type safety, general readability and removing the need for casting back and forth. See changes in `source/blender/gpu`. ### Conservation of the GHOST XR API As the XR part of the GHOST C-API didn't only provide opaque handle and C++ function calling, but also XR error handling via `GHOST_XR_CAPI_CALL{_RET}` macros, this part of the C-API was preserved in a new sub, XR specific API, exposed in the new `GHOST_Xr-api.hh` header and implemented in the existing `GHOST_Xr.cc` file. Following the rest of the refactor, the functions definition and prototypes were ported from using opaques `GHOST_XrContextHandle` to `GHOST_IXrContext *` pointer types. ### Removal of legacy GHOST OpenGL based tests In the GHOST module source tree, a `test` folder remained containing old tests dating back from the initial Git commit of ~2002. As these files have to be manually compiled and ran as a separate CMake target, are well outside our standard testing framework and code style, have outdated coverage, and mostly rely on pure OpenGL and now unsupported OpenGL code. The decision was made to remove them rather than uselessly port them to the C++ API. A possible future quality task could then be to re-implement proper, modern GHOST tests. *NOTE: See PR for additional API usage before/after examples.* Pull Request: https://projects.blender.org/blender/blender/pulls/151792 |
||
|---|---|---|
| .. | ||
| multitest | ||
| CMakeLists.txt | ||