From 7b788af259a5806cee2c85c96459d345eaa60ba5 Mon Sep 17 00:00:00 2001 From: Thomas Lindemeier Date: Tue, 11 Jun 2024 15:39:47 +0200 Subject: [PATCH] fix compiler issues (#18) * fix compiler issues * default clang * Update CMakeLists.txt * Update CMakeLists.txt * Update VertexArrayObject.cxx * Update bazel.yml * Update .bazelrc * Delete BUILD.bazel * Delete .bazelrc * Delete .github/workflows/bazel.yml * Delete WORKSPACE.bazel --- .bazelrc | 1 - .github/workflows/bazel.yml | 47 ------------------------ .github/workflows/cmake.yml | 68 +++++++++++++++++------------------ BUILD.bazel | 32 ----------------- CMakeLists.txt | 40 ++++++++++----------- WORKSPACE.bazel | 1 - prgl/FrameBufferObject.hxx | 2 ++ prgl/VertexArrayObject.hxx | 7 +++- prgl/VertexBufferObject.hxx | 4 ++- src/ContextImplementation.cxx | 8 +++++ src/FrameBufferObject.cxx | 42 +++++++++++++++++----- src/Texture2d.cxx | 4 +-- src/VertexArrayObject.cxx | 33 +++++++++++++++-- src/VertexBufferObject.cxx | 22 ++++++++++++ 14 files changed, 160 insertions(+), 151 deletions(-) delete mode 100644 .bazelrc delete mode 100644 .github/workflows/bazel.yml delete mode 100644 BUILD.bazel delete mode 100644 WORKSPACE.bazel diff --git a/.bazelrc b/.bazelrc deleted file mode 100644 index 5a77247..0000000 --- a/.bazelrc +++ /dev/null @@ -1 +0,0 @@ -build --cxxopt='-std=c++17' --repo_env=CC=clang-10 --repo_env=CXX=clang++-10 diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml deleted file mode 100644 index a690fe2..0000000 --- a/.github/workflows/bazel.yml +++ /dev/null @@ -1,47 +0,0 @@ -# This is a basic workflow that is manually triggered - -name: bazel-build - -on: - # Trigger the workflow on push or pull request, - # but only for the master branch - push: - branches: - - develop - pull_request: - branches: - - develop - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Install clang - run: | - sudo apt-get update - sudo apt install clang-10 libglfw3-dev libglew-dev libglfw3 libglew2.0 - - - name: Checkout - uses: actions/checkout@v1 - - - name: Mount bazel cache - uses: actions/cache@v1 - with: - path: "/home/runner/.cache/bazel" - key: bazel - - - name: Install bazelisk - run: | - curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.1.0/bazelisk-linux-amd64" - mkdir -p "${GITHUB_WORKSPACE}/bin/" - mv bazelisk-linux-amd64 "${GITHUB_WORKSPACE}/bin/bazel" - chmod +x "${GITHUB_WORKSPACE}/bin/bazel" - - - name: Build - run: | - "${GITHUB_WORKSPACE}/bin/bazel" build --compilation_mode=opt //... - - # - name: Test - # run: | - # "${GITHUB_WORKSPACE}/bin/bazel" test --test_output=errors //... diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index b9e0e54..47977e0 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -23,37 +23,37 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install libglfw3-dev clang-10 ninja-build libglew-dev - - - name: Create Build Environment - # Some projects don't allow in-source building, so create a separate build directory - # We'll use this as our working directory for all subsequent commands - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Configure CMake - # Use a bash shell so we can use the same syntax for environment variable - # access regardless of the host operating system - shell: bash - working-directory: ${{runner.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. - # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 - run: cmake $GITHUB_WORKSPACE -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_COMPILER=/usr/bin/clang-10 -DCMAKE_CXX_COMPILER=/usr/bin/clang++-10 -DCMAKE_BUILD_TYPE=$BUILD_TYPE -GNinja - - - name: Build - working-directory: ${{runner.workspace}}/build - shell: bash - # Execute the build. You can specify a specific target with "--target " - run: cmake --build . --config $BUILD_TYPE - - - name: Test - working-directory: ${{runner.workspace}}/build - shell: bash - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C $BUILD_TYPE + - uses: actions/checkout@v2 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install libglfw3-dev clang ninja-build libglew-dev + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{runner.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{runner.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_BUILD_TYPE=$BUILD_TYPE -GNinja + + - name: Build + working-directory: ${{runner.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE + + - name: Test + working-directory: ${{runner.workspace}}/build + shell: bash + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C $BUILD_TYPE diff --git a/BUILD.bazel b/BUILD.bazel deleted file mode 100644 index ef97ead..0000000 --- a/BUILD.bazel +++ /dev/null @@ -1,32 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_library") - -cc_library( - name = "prgl", - srcs = glob(["src/*.cxx"]), - hdrs = glob(["prgl/*.hxx"]), - copts = [ - "-Wall", - "-Werror", - "-Weverything", - "-Wno-c++98-compat", - "-Wno-padded", - "-Wno-documentation", - "-Wno-global-constructors", - "-Wno-exit-time-destructors", - ], - linkopts = [ - "-lGL", - "-lGLEW", - "-lGLU", - "-ldl", - "-lm", - "-lpthread", - "/usr/lib/x86_64-linux-gnu/libGLX.so", - "/usr/lib/x86_64-linux-gnu/libOpenGL.so", - "/usr/lib/x86_64-linux-gnu/libX11.so", - "/usr/lib/x86_64-linux-gnu/librt.so", - "/usr/lib/x86_64-linux-gnu/libglfw.so", - ], - visibility = ["//visibility:public"], - deps = [], -) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51cf440..e8f0282 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,18 +4,18 @@ project(prgl) find_package(glfw3 REQUIRED) find_package(OpenGL REQUIRED) -find_package (Threads REQUIRED) +find_package(Threads REQUIRED) # glew -if (UNIX) +if(UNIX) find_package(PkgConfig REQUIRED) pkg_search_module(GLEW REQUIRED glew) -else () +else() file(TO_CMAKE_PATH "$ENV{GLEW_HOME}" GLEW_HOME) set(GLEW_INCLUDE_DIR "${GLEW_HOME}/include") find_library(GLEW_LIBRARY NAMES GLEW glew32 glew glew32s PATHS "${GLEW_HOME}/lib" NO_DEFAULT_PATH) set(GLEW_LIBRARIES ${GLEW_LIBRARY}) -endif (UNIX) +endif(UNIX) add_library(${PROJECT_NAME} STATIC src/ContextImplementation.cxx @@ -28,24 +28,23 @@ add_library(${PROJECT_NAME} STATIC src/GlslComputeShader.cxx src/VertexBufferObject.cxx src/VertexArrayObject.cxx - ) set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON) set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "d") -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") # using Clang target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Weverything -Wno-c++98-compat -Wno-padded -Wno-documentation -Werror -Wno-global-constructors -Wno-exit-time-destructors) -elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # using GCC target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Werror) -elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # using Visual Studio C++ target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX) endif() - target_include_directories(${PROJECT_NAME} PUBLIC $ PUBLIC $ @@ -54,7 +53,6 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${GLEW_INCLUDE_DIRS} ) - target_link_libraries(${PROJECT_NAME} OpenGL::GL glfw @@ -62,8 +60,7 @@ target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT} ) - -## testing +# # testing # executable add_executable(${PROJECT_NAME}_test @@ -79,14 +76,15 @@ set_property(TARGET ${PROJECT_NAME}_test PROPERTY CXX_STANDARD_REQUIRED ON) include(FetchContent) option(RUN_TESTS "Build and run the tests" ON) + if(RUN_TESTS) - # get google test - FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.10.0 - ) - FetchContent_MakeAvailable(googletest) - enable_testing() - add_subdirectory(test) + # get google test + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.14.0 + ) + FetchContent_MakeAvailable(googletest) + enable_testing() + add_subdirectory(test) endif() diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel deleted file mode 100644 index 00bb9da..0000000 --- a/WORKSPACE.bazel +++ /dev/null @@ -1 +0,0 @@ -workspace(name = "prgl") diff --git a/prgl/FrameBufferObject.hxx b/prgl/FrameBufferObject.hxx index 340c2c5..1d67c07 100644 --- a/prgl/FrameBufferObject.hxx +++ b/prgl/FrameBufferObject.hxx @@ -31,6 +31,8 @@ class FrameBufferObject final { const std::shared_ptr& getTarget() const; const std::shared_ptr& getDepth() const; + void clear(const std::array& clearColor, double clearDepth); + private: FrameBufferObject(const FrameBufferObject&) = delete; FrameBufferObject& operator=(const FrameBufferObject&) = delete; diff --git a/prgl/VertexArrayObject.hxx b/prgl/VertexArrayObject.hxx index e96edfe..98e1275 100644 --- a/prgl/VertexArrayObject.hxx +++ b/prgl/VertexArrayObject.hxx @@ -52,15 +52,20 @@ class VertexArrayObject final { const std::shared_ptr& vbo); void render(const DrawMode& mode, uint32_t first, uint32_t count); + void renderAll(const DrawMode& mode); private: - VertexArrayObject(const VertexArrayObject&) = delete; + VertexArrayObject(const VertexArrayObject&) = delete; VertexArrayObject& operator=(const VertexArrayObject&) = delete; + protected: uint32_t mVao; + std::size_t mIndicesCount; + std::map> mVboMap; }; + } // namespace prgl #endif // PRGL_VERTEX_ARRAY_OBJECT_H diff --git a/prgl/VertexBufferObject.hxx b/prgl/VertexBufferObject.hxx index 1e1a471..55a849c 100644 --- a/prgl/VertexBufferObject.hxx +++ b/prgl/VertexBufferObject.hxx @@ -132,7 +132,9 @@ class VertexBufferObject final { DataType getVertexComponentDataType() const; uint32_t getVertexComponentDataColumns() const; - size_t getVerticesCount() const; + std::size_t getVerticesCount() const; + + std::size_t getSize() const; private: VertexBufferObject(const VertexBufferObject&) = delete; diff --git a/src/ContextImplementation.cxx b/src/ContextImplementation.cxx index a155fb8..b003ea5 100644 --- a/src/ContextImplementation.cxx +++ b/src/ContextImplementation.cxx @@ -36,6 +36,8 @@ void checkGLError(const char* file, const char* function, int line) { error = "GL_INVALID_FRAMEBUFFER_OPERATION"; break; // case GL_CONTEXT_LOST: error = "GL_CONTEXT_LOST"; break; + default: + break; } std::cerr << "ERROR::" << error.c_str() << "\n\tfile:\t" << file @@ -82,6 +84,9 @@ static void APIENTRY openGlDebugCallback(GLenum /*unused*/, GLenum type, case GL_DEBUG_TYPE_OTHER: std::cerr << "OTHER"; break; + default: + std::cerr << "UNKNOWN"; + break; } std::cerr << std::endl; @@ -97,6 +102,9 @@ static void APIENTRY openGlDebugCallback(GLenum /*unused*/, GLenum type, case GL_DEBUG_SEVERITY_HIGH: std::cerr << "HIGH"; break; + default: + std::cerr << "UNKNOWN"; + break; } std::cerr << std::endl; std::cerr << "---------------------opengl-callback-end--------------" diff --git a/src/FrameBufferObject.cxx b/src/FrameBufferObject.cxx index a874850..0fe353d 100644 --- a/src/FrameBufferObject.cxx +++ b/src/FrameBufferObject.cxx @@ -69,13 +69,30 @@ const std::shared_ptr& FrameBufferObject::getDepth() const { return mDepth; } +void FrameBufferObject::clear(const std::array& clearColor, + double clearDepth) { + if (mTarget != nullptr) { + mTarget->bind(true); + glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); + glClear(GL_COLOR_BUFFER_BIT); + mTarget->bind(false); + } + + if (mDepth != nullptr) { + mDepth->bind(true); + glClearDepth(clearDepth); + glClear(GL_DEPTH_BUFFER_BIT); + mDepth->bind(false); + } +} + bool FrameBufferObject::checkStatus() const { bind(true); // glReadBuffer(GL_NONE); const auto status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); - bool result = false; + bool result = false; switch (status) { case GL_FRAMEBUFFER_COMPLETE_EXT: @@ -83,37 +100,46 @@ bool FrameBufferObject::checkStatus() const { result = true; break; case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: - std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Attachment is NOT complete" + std::cerr << "[ERROR]FRAMEBUFFER::incomplete: " + "Attachment is NOT complete" << std::endl; result = false; break; case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: - std::cerr << "[ERROR]FRAMEBUFFER::incomplete: No image is attached to FBO" + std::cerr << "[ERROR]FRAMEBUFFER::incomplete: No " + "image is attached to FBO" << std::endl; result = false; break; case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: - std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Attached images have " + std::cerr << "[ERROR]FRAMEBUFFER::incomplete: " + "Attached images have " "different dimensions" << std::endl; result = false; break; case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: - std::cout << "[ERROR]FRAMEBUFFER::incomplete: Color attached images have " + std::cout << "[ERROR]FRAMEBUFFER::incomplete: " + "Color attached images have " "different internal formats" << std::endl; result = false; break; case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: - std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Draw buffer" << std::endl; + std::cerr << "[ERROR]FRAMEBUFFER::" + "incomplete: Draw buffer" + << std::endl; result = false; break; case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: - std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Read buffer" << std::endl; + std::cerr << "[ERROR]FRAMEBUFFER::" + "incomplete: Read buffer" + << std::endl; result = false; break; case GL_FRAMEBUFFER_UNSUPPORTED_EXT: - std::cerr << "[ERROR]FRAMEBUFFER::Unsupported by FBO implementation" + std::cerr << "[ERROR]FRAMEBUFFER::Unsupported by " + "FBO implementation" << std::endl; result = false; break; diff --git a/src/Texture2d.cxx b/src/Texture2d.cxx index b9a2623..4255058 100644 --- a/src/Texture2d.cxx +++ b/src/Texture2d.cxx @@ -220,8 +220,8 @@ void Texture2d::render(float posX, float posY, float width, float height, glDisable(GL_DEPTH_TEST); - int32_t iViewport[4]; - glGetIntegerv(GL_VIEWPORT, iViewport); + std::array iViewport; + glGetIntegerv(GL_VIEWPORT, iViewport.data()); glMatrixMode(GL_PROJECTION); glPushMatrix(); diff --git a/src/VertexArrayObject.cxx b/src/VertexArrayObject.cxx index 59e1b5b..ee3a2f2 100644 --- a/src/VertexArrayObject.cxx +++ b/src/VertexArrayObject.cxx @@ -18,13 +18,17 @@ std::shared_ptr VertexArrayObject::Create() { return std::make_shared(); } -VertexArrayObject::VertexArrayObject() : mVao(INVALID_HANDLE) { +VertexArrayObject::VertexArrayObject() + : mVao(INVALID_HANDLE), + mIndicesCount(0) { glGenVertexArrays(1, &mVao); } VertexArrayObject::~VertexArrayObject() { - glDeleteVertexArrays(1, &mVao); - mVao = INVALID_HANDLE; + if (mVao != INVALID_HANDLE) { + glDeleteVertexArrays(1, &mVao); + mVao = INVALID_HANDLE; + } } /** @@ -54,6 +58,29 @@ void VertexArrayObject::render(const DrawMode& mode, const uint32_t first, static_cast(count)); } +/** + * @brief + * https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml + * + * @param mode Specifies what kind of primitives to render. + * @param first Specifies the starting index in the enabled arrays. + * @param count Specifies the number of indices to be rendered. + */ +void VertexArrayObject::renderAll(const DrawMode& mode) { + auto numEl = 1U; + if (mode == DrawMode::Triangles) { + numEl = 3U; + } else if (mode == DrawMode::Points) { + numEl = 1U; + } else { + throw std::runtime_error("not implemented"); + } + + glDrawArrays( + static_cast(mode), static_cast(0), + static_cast(mVboMap.begin()->second->getVerticesCount() / numEl)); +} + void VertexArrayObject::addVertexBufferObject( uint32_t location, const std::shared_ptr& vbo) { // add to the map to keep the reference (consider move) diff --git a/src/VertexBufferObject.cxx b/src/VertexBufferObject.cxx index bb23663..83f4e1d 100644 --- a/src/VertexBufferObject.cxx +++ b/src/VertexBufferObject.cxx @@ -58,4 +58,26 @@ size_t VertexBufferObject::getVerticesCount() const { return mVerticesCount; } +std::size_t VertexBufferObject::getSize() const { + auto depth = 1U; + if (mDataType == DataType::Float) { + depth = sizeof(float); + } else if (mDataType == DataType::Double) { + depth = sizeof(double); + } else if (mDataType == DataType::Int) { + depth = sizeof(int32_t); + } else if (mDataType == DataType::UnsignedInt) { + depth = sizeof(uint32_t); + } else if (mDataType == DataType::Short) { + depth = sizeof(int16_t); + } else if (mDataType == DataType::UnsignedShort) { + depth = sizeof(uint16_t); + } else if (mDataType == DataType::Byte) { + depth = sizeof(int8_t); + } else if (mDataType == DataType::UnsignedByte) { + depth = sizeof(uint8_t); + } + return (mVerticesCount * depth) * mDataColumns; +} + } // namespace prgl