Skip to content

Commit

Permalink
[NOX-76] add GLContext unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rafal Maziejuk <[email protected]>
  • Loading branch information
rafalmaziejuk committed May 16, 2024
1 parent 616652c commit b3851fe
Show file tree
Hide file tree
Showing 31 changed files with 441 additions and 84 deletions.
27 changes: 25 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
branches: [ "dev" ]

env:
DISPLAY: ":99"
MESA_GL_VERSION_OVERRIDE: "4.6"

defaults:
run:
Expand Down Expand Up @@ -127,13 +131,32 @@ jobs:

- name: Install dependencies
run: |
set -e
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get update && sudo apt-get install libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libxext-dev libgl1-mesa-dev gcovr
sudo apt-get update && sudo apt-get install xorg-dev libxrandr-dev libxcursor-dev libgl1-mesa-dev libegl1-mesa-dev libdrm-dev mesa-utils xvfb fluxbox gcovr
fi
if [ "$RUNNER_OS" == "Windows" ]; then
pip install gcovr
fi
- name: Create Display Linux
if: runner.os == 'Linux'
run: |
set -e
Xvfb $DISPLAY -screen 0 800x600x24 &
sleep 5
fluxbox > /dev/null 2>&1 &
sleep 5
- name: Install Windows Mesa3D
uses: ssciwr/setup-mesa-dist-win@v2
if: runner.os == 'Windows'
with:
build-type: 'release-msvc'

- name: Configure
run: cmake -S . -B build ${{ matrix.platform.flags }}

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tools/__pycache__/
# Files
include/nox/export.h
src/opengl/nox_opengl_export.h
tests/config.h
tests/helpers/config.h
cmake/nox_config.cmake
cmake/nox_config_version.cmake
scripts/custom_config.cmd
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "examples/third_party/glfw"]
path = examples/base/third_party/glfw
[submodule "third_party/glfw"]
path = third_party/glfw
url = https://github.com/glfw/glfw
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ endif()
# --------------------------------------------------
# Add subdirectories
# --------------------------------------------------
add_subdirectory(third_party)
add_subdirectory(src)

if(NOX_BUILD_EXAMPLES)
Expand Down
26 changes: 0 additions & 26 deletions examples/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
set(NOX_EXAMPLES_BASE_LIB_NAME "base")

set(NOX_EXAMPLES_BASE_DIR "${PROJECT_SOURCE_DIR}/examples/base")
set(NOX_EXAMPLES_BASE_THIRD_PARTY_DIR "${NOX_EXAMPLES_BASE_DIR}/third_party")

set(NOX_EXAMPLES_BASE_FOLDER_NAME "${NOX_EXAMPLES_FOLDER_NAME}/base")
set(NOX_EXAMPLES_BASE_THIRD_PARTY_FOLDER_NAME "${NOX_EXAMPLES_BASE_FOLDER_NAME}/third_party")

# --------------------------------------------------
# Create target
Expand Down Expand Up @@ -35,27 +30,6 @@ set_target_properties(
# --------------------------------------------------
# Link libraries
# --------------------------------------------------
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(${NOX_EXAMPLES_BASE_THIRD_PARTY_DIR}/glfw glfw)
set_target_properties(
glfw
PROPERTIES
FOLDER "${NOX_EXAMPLES_BASE_THIRD_PARTY_FOLDER_NAME}/glfw"
)
set_target_properties(
update_mappings
PROPERTIES
FOLDER "${NOX_EXAMPLES_BASE_THIRD_PARTY_FOLDER_NAME}/glfw"
)

add_subdirectory(${NOX_EXAMPLES_BASE_THIRD_PARTY_DIR}/stb_image stb_image)
set_target_properties(
stb_image
PROPERTIES
FOLDER "${NOX_EXAMPLES_BASE_THIRD_PARTY_FOLDER_NAME}/stb_image"
)

target_link_libraries(
${NOX_EXAMPLES_BASE_LIB_NAME}
PUBLIC
Expand Down
1 change: 0 additions & 1 deletion examples/base/third_party/glfw
Submodule glfw deleted from a87acd
15 changes: 14 additions & 1 deletion src/asserts.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
#pragma once

namespace nox {

inline bool assertEnabled = true;

[[nodiscard]] inline bool &getAssertEnabled() {
return assertEnabled;
}

} // namespace nox

// clang-format off

#if defined(NOX_DISABLE_ASSERTS) || !defined(NOX_DEBUG)
#define NOX_ASSERT(condition) (void)(condition)
#else
#include <cassert>
#define NOX_ASSERT(condition) assert(condition)
#define NOX_ASSERT(condition) \
if (nox::getAssertEnabled()) { \
assert(condition); \
}
#endif

#define NOX_ASSERT_MSG(condition, message) NOX_ASSERT((condition) && (message))
Expand Down
7 changes: 0 additions & 7 deletions src/opengl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ generate_export_header(
# --------------------------------------------------
# Link libraries
# --------------------------------------------------
add_subdirectory(${NOX_THIRD_PARTY_DIR}/glad glad)
set_target_properties(
glad
PROPERTIES
FOLDER "${NOX_THIRD_PARTY_FOLDER_NAME}"
)

target_link_libraries(
${NOX_OPENGL_LIB_NAME}
PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion src/opengl/gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GLContext : public Surface {
[[nodiscard]] static std::unique_ptr<GLContext> create(const SurfaceDescriptor &descriptor);

virtual void swapBuffers() const = 0;
virtual void setSwapInterval(bool value) const = 0;
virtual void setSwapInterval(bool interval) const = 0;

public:
GLContext() = default;
Expand Down
4 changes: 2 additions & 2 deletions src/opengl/linux/linux_gl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ void LinuxGLContext::swapBuffers() const {
eglSwapBuffers(m_handleDisplay, m_handleSurface);
}

void LinuxGLContext::setSwapInterval(bool value) const {
eglSwapInterval(m_handleDisplay, static_cast<EGLint>(value));
void LinuxGLContext::setSwapInterval(bool interval) const {
eglSwapInterval(m_handleDisplay, static_cast<EGLint>(interval));
}

} // namespace nox
2 changes: 1 addition & 1 deletion src/opengl/linux/linux_gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LinuxGLContext : public GLContext {
[[nodiscard]] bool initialize(const OpenGLSurfaceAttributesDescriptor &descriptor);

void swapBuffers() const override;
void setSwapInterval(bool value) const override;
void setSwapInterval(bool interval) const override;

protected:
[[nodiscard]] virtual bool setDisplayHandle() = 0;
Expand Down
4 changes: 0 additions & 4 deletions src/opengl/linux/x11_gl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ X11GLContext::X11GLContext(const X11SurfaceBackendDescriptor &descriptor) {
}

X11GLContext::~X11GLContext() {
[[maybe_unused]] XWindowAttributes windowAttributes; // NOLINT
NOX_ASSERT_MSG(XGetWindowAttributes(m_handleDisplayX11, m_handleWindowX11, &windowAttributes),
"The window handle associated with OpenGL context is invalid");

m_handleDisplayX11 = nullptr;
m_handleWindowX11 = 0u;
}
Expand Down
13 changes: 5 additions & 8 deletions src/opengl/windows/windows_gl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ bool GLContext::validateInput(const SurfaceDescriptor &descriptor) {
}

std::unique_ptr<GLContext> GLContext::create(const SurfaceDescriptor &descriptor) {
if (!loadOpenGL()) {
return nullptr;
}
NOX_ENSURE_RETURN_NULLPTR_MSG(loadOpenGL(), "Couldn't preload WGL");

const auto *surfaceBackendDescriptor = std::get_if<WindowsSurfaceBackendDescriptor>(&descriptor.surfaceBackendDescriptor);
const auto *surfaceAttributesDescriptor = std::get_if<OpenGLSurfaceAttributesDescriptor>(&descriptor.surfaceAttributesDescriptor);
Expand All @@ -101,12 +99,9 @@ std::unique_ptr<GLContext> GLContext::create(const SurfaceDescriptor &descriptor
WindowsGLContext::WindowsGLContext(const WindowsSurfaceBackendDescriptor &descriptor) {
m_handleWindow = static_cast<HWND>(descriptor.windowHandle);
m_handleDeviceContext = GetDC(m_handleWindow);
NOX_ASSERT_MSG(m_handleDeviceContext != nullptr, "Couldn't get device context for given window handle");
}

WindowsGLContext::~WindowsGLContext() {
NOX_ASSERT_MSG(IsWindow(m_handleWindow), "The window handle associated with OpenGL context is invalid");

wglMakeCurrent(nullptr, nullptr);
wglDeleteContext(m_handleRenderingContext);
ReleaseDC(m_handleWindow, m_handleDeviceContext);
Expand All @@ -119,6 +114,8 @@ WindowsGLContext::~WindowsGLContext() {
}

bool WindowsGLContext::initialize(const OpenGLSurfaceAttributesDescriptor &descriptor) {
NOX_ENSURE_RETURN_FALSE_MSG(m_handleDeviceContext != nullptr, "Couldn't get device context");

std::array<int32_t, 19> pixelFormatAttributes{WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
Expand Down Expand Up @@ -159,8 +156,8 @@ void WindowsGLContext::swapBuffers() const {
SwapBuffers(m_handleDeviceContext);
}

void WindowsGLContext::setSwapInterval(bool value) const {
wglSwapIntervalEXT(static_cast<int32_t>(value));
void WindowsGLContext::setSwapInterval(bool interval) const {
wglSwapIntervalEXT(static_cast<int32_t>(interval));
}

} // namespace nox
2 changes: 1 addition & 1 deletion src/opengl/windows/windows_gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WindowsGLContext final : public GLContext {
[[nodiscard]] bool initialize(const OpenGLSurfaceAttributesDescriptor &descriptor);

void swapBuffers() const override;
void setSwapInterval(bool value) const override;
void setSwapInterval(bool interval) const override;

private:
HWND m_handleWindow{nullptr};
Expand Down
6 changes: 3 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set(NOX_TESTS_UTILITIES_LIB_NAME "utilities")

# --------------------------------------------------
# Setup googletest
# --------------------------------------------------
Expand All @@ -19,10 +21,8 @@ set_target_properties(gtest_main PROPERTIES FOLDER "${NOX_THIRD_PARTY_FOLDER_NAM

enable_testing()

get_filename_component(NOX_TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/data" ABSOLUTE)
configure_file(config.h.in "${CMAKE_CURRENT_SOURCE_DIR}/config.h")

# --------------------------------------------------
# Add subdirectories
# --------------------------------------------------
add_subdirectory(utilities)
add_subdirectory(unit_tests)
21 changes: 12 additions & 9 deletions tests/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ add_executable(${NOX_UNIT_TESTS_EXE_NAME})
target_include_directories(
${NOX_UNIT_TESTS_EXE_NAME}
PRIVATE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/tests>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
)

target_compile_features(${NOX_UNIT_TESTS_EXE_NAME} PUBLIC cxx_std_17)
Expand Down Expand Up @@ -37,20 +36,14 @@ gtest_discover_tests(${NOX_UNIT_TESTS_EXE_NAME})
# --------------------------------------------------
set_target_properties(${NOX_UNIT_TESTS_EXE_NAME} PROPERTIES FOLDER ${NOX_TESTS_FOLDER_NAME})

# --------------------------------------------------
# Add dependencies
# --------------------------------------------------
if(NOX_BUILD_RENDERER_OPENGL)
add_dependencies(${NOX_UNIT_TESTS_EXE_NAME} ${NOX_OPENGL_LIB_NAME})
endif()

# --------------------------------------------------
# Link libraries
# --------------------------------------------------
target_link_libraries(
${NOX_UNIT_TESTS_EXE_NAME}
PRIVATE
${NOX_LIB_NAME}
${NOX_TESTS_UTILITIES_LIB_NAME}
GTest::gtest_main
)

Expand All @@ -66,8 +59,18 @@ set(NOX_UNIT_TESTS_SOURCES

target_sources(${NOX_UNIT_TESTS_EXE_NAME} PRIVATE ${NOX_UNIT_TESTS_SOURCES})

# --------------------------------------------------
# Add subdirectories
# --------------------------------------------------
if(NOX_BUILD_RENDERER_OPENGL)
add_subdirectory(opengl)
endif()

nox_create_project_source_tree(${NOX_UNIT_TESTS_EXE_NAME})

# --------------------------------------------------
# Create custom for running unit tests
# --------------------------------------------------
add_custom_target(
run-${NOX_UNIT_TESTS_EXE_NAME}
COMMAND ${NOX_UNIT_TESTS_EXE_NAME}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/format_descriptor_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "format_descriptor.h"
#include "src/format_descriptor.h"

#include <gtest/gtest.h>

Expand Down
Loading

0 comments on commit b3851fe

Please sign in to comment.