Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97aed4e5ca |
+4
-3
@@ -1,9 +1,10 @@
|
|||||||
build/
|
build/
|
||||||
.vscode/
|
.*
|
||||||
|
!.gitignore
|
||||||
|
!.clangd
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
|
CMakeKits.json
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
docs/*
|
docs/*
|
||||||
!docs/CMake*
|
!docs/CMake*
|
||||||
!docs/Doxyfile.in
|
!docs/Doxyfile.in
|
||||||
!.clangd
|
|
||||||
.glsld
|
|
||||||
+16
-31
@@ -28,45 +28,32 @@ function(toAbsolutePath SETVAR PATHS)
|
|||||||
set(${SETVAR} ${RET_VAL} PARENT_SCOPE)
|
set(${SETVAR} ${RET_VAL} PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
if (NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE "Debug")
|
||||||
|
endif()
|
||||||
|
|
||||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
message(STATUS "Targeting Release build")
|
add_compile_definitions(_NB_BUILD_RELEASE)
|
||||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
message(STATUS "Targeting Debug build")
|
|
||||||
set(NB_DEBUG_BUILD ON)
|
set(NB_DEBUG_BUILD ON)
|
||||||
add_compile_definitions(_NB_BUILD_DEBUG)
|
add_compile_definitions(_NB_BUILD_DEBUG)
|
||||||
endif()
|
endif()
|
||||||
|
message(STATUS "Targeting ${CMAKE_BUILD_TYPE} build")
|
||||||
|
|
||||||
|
if (NB_DEBUG_BUILD)
|
||||||
|
set(NB_LOGGING ON)
|
||||||
|
set(NB_BUILD_TESTS ON)
|
||||||
|
set(NB_BUILD_DOCS ON)
|
||||||
|
set(NB_MONITOR_OPENGL_CALLS ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
|
||||||
|
endif()
|
||||||
|
|
||||||
cmake_dependent_option(NB_LOGGING
|
|
||||||
"Creates a default logger and automatically logs with code locations."
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_BUILD_TESTS
|
|
||||||
"Build unit tests"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_BUILD_DOCS
|
|
||||||
"Build documentation"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NBENGINE_INSTALL
|
cmake_dependent_option(NBENGINE_INSTALL
|
||||||
"Install NBEngine"
|
"Install NBEngine"
|
||||||
ON
|
ON
|
||||||
NB_DEBUG_BUILD
|
NB_DEBUG_BUILD
|
||||||
OFF
|
OFF
|
||||||
)
|
)
|
||||||
cmake_dependent_option(NB_MONITOR_OPENGL_CALLS
|
|
||||||
"Monitors every OpenGL call for errors"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||||
message(STATUS "Building for Windows")
|
message(STATUS "Building for Windows")
|
||||||
@@ -76,8 +63,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
|||||||
set(NB_TARGET_LINUX ON)
|
set(NB_TARGET_LINUX ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(NB_GRAPHICS_API_OGL ON)
|
|
||||||
|
|
||||||
if(NB_BUILD_TESTS)
|
if(NB_BUILD_TESTS)
|
||||||
message(STATUS "Building tests")
|
message(STATUS "Building tests")
|
||||||
enable_testing()
|
enable_testing()
|
||||||
@@ -108,13 +93,13 @@ endif()
|
|||||||
|
|
||||||
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
||||||
|
|
||||||
add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
|
add_subdirectory(./engine)
|
||||||
|
|
||||||
# Toggle
|
# Toggle
|
||||||
set(NB_REBUILD_DOCS)
|
set(NB_REBUILD_DOCS)
|
||||||
|
|
||||||
if (NB_BUILD_DOCS)
|
if (NB_BUILD_DOCS)
|
||||||
add_subdirectory(./docs)
|
add_subdirectory(./docs)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ toAbsolutePath(NB_CORE_SOURCE
|
|||||||
)
|
)
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
./include/NBCore/DataSink.hpp
|
./include/NBCore/DataSink.hpp
|
||||||
./include/NBCore/Errors.hpp
|
./include/NBCore/DataTypes.hpp
|
||||||
|
./include/NBCore/Errors.hpp
|
||||||
./include/NBCore/ErrorsImpl.hpp
|
./include/NBCore/ErrorsImpl.hpp
|
||||||
./include/NBCore/Logger.hpp
|
./include/NBCore/Logger.hpp
|
||||||
./include/NBCore/Printer.hpp
|
./include/NBCore/Printer.hpp
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstring>
|
||||||
|
#ifndef _NB_DATA_TYPES
|
||||||
|
#define _NB_DATA_TYPES
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
template<typename T, unsigned int N>
|
||||||
|
class NDArray {
|
||||||
|
protected:
|
||||||
|
std::unique_ptr<T[]> _data;
|
||||||
|
static unsigned int calculateTotalSize(const std::array<unsigned int, N>& dims) {
|
||||||
|
unsigned int ret = 1;
|
||||||
|
std::for_each(dims.begin(), dims.end(), [&](unsigned int n){
|
||||||
|
ret *= n;
|
||||||
|
});
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
const std::array<unsigned int, N> dimensions;
|
||||||
|
const unsigned int totalSize;
|
||||||
|
static constexpr unsigned int NumberDimensions = N;
|
||||||
|
NDArray(
|
||||||
|
const std::array<unsigned int, N>& dimensions_,
|
||||||
|
T* data_=nullptr
|
||||||
|
) :
|
||||||
|
dimensions(dimensions_),
|
||||||
|
totalSize(calculateTotalSize(dimensions_))
|
||||||
|
{
|
||||||
|
if (data_) { data(data_); }
|
||||||
|
}
|
||||||
|
NDArray(
|
||||||
|
const std::array<unsigned int, N>& dimensions_,
|
||||||
|
const T* data_
|
||||||
|
) : NDArray(dimensions_) { data(data_); }
|
||||||
|
|
||||||
|
unsigned int coordinateToIndex(const std::array<T, N>& coord) const {
|
||||||
|
unsigned int blockSize = 1;
|
||||||
|
unsigned int ret = 0;
|
||||||
|
for (auto i = 0; i < dimensions.size(); ++i) {
|
||||||
|
ret += coord[i] * blockSize;
|
||||||
|
blockSize *= dimensions[i];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const T& at(const std::array<T, N>& coord) const {
|
||||||
|
return _data[coordinateToIndex(coord)];
|
||||||
|
}
|
||||||
|
inline std::shared_ptr<T> data() const { return _data; }
|
||||||
|
|
||||||
|
inline T& at(const std::array<T, N>& coord) {
|
||||||
|
return _data[coordinateToIndex(coord)];
|
||||||
|
}
|
||||||
|
inline T* data(const T* data_) {
|
||||||
|
_data = std::make_unique<T[]>(totalSize);
|
||||||
|
std::memcpy(_data.get(), data_, totalSize * sizeof(T));
|
||||||
|
return _data.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}; // namespace nb
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif // _NB_DATA_TYPES
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifndef _NB_CORE_TYPES
|
#ifndef _NB_UTILTIES
|
||||||
#define _NB_CORE_TYPES
|
#define _NB_UTILTIES
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|||||||
@@ -1,24 +1,23 @@
|
|||||||
cmake_minimum_required(VERSION 3.26.0)
|
cmake_minimum_required(VERSION 3.26.0)
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
enable_testing()
|
||||||
enable_testing()
|
include(GoogleTest)
|
||||||
include(GoogleTest)
|
add_executable(TestCore
|
||||||
add_executable(TestCore
|
./testErrors.cpp
|
||||||
./testErrors.cpp
|
#./testProcesses.cpp
|
||||||
#./testProcesses.cpp
|
./testUtils.cpp
|
||||||
./testUtils.cpp
|
)
|
||||||
)
|
target_link_libraries(TestCore
|
||||||
target_link_libraries(TestCore
|
NBCore
|
||||||
NBCore
|
GTest::gtest_main
|
||||||
GTest::gtest_main
|
)
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(LoggerTest
|
add_executable(LoggerTest
|
||||||
./testLogger.cpp
|
./testLogger.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(LoggerTest
|
target_link_libraries(LoggerTest
|
||||||
NBCore
|
NBCore
|
||||||
)
|
)
|
||||||
|
|
||||||
|
gtest_discover_tests(TestCore)
|
||||||
|
|
||||||
gtest_discover_tests(TestCore)
|
|
||||||
endif()
|
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <NBCore/DataTypes.hpp>
|
||||||
#include <NBCore/TypeTraits.hpp>
|
#include <NBCore/TypeTraits.hpp>
|
||||||
#include <NBCore/StringUtils.hpp>
|
#include <NBCore/StringUtils.hpp>
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ TEST(UtilsTest, TestFindAndReplace) {
|
|||||||
|
|
||||||
tmp = find_and_replace("aeiou", "a", "afa");
|
tmp = find_and_replace("aeiou", "a", "afa");
|
||||||
tmp = find_and_replace(tmp, "e", "efe");
|
tmp = find_and_replace(tmp, "e", "efe");
|
||||||
tmp = find_and_replace(tmp, "i", "ifi");
|
tmp = find_and_replace(tmp, "i", "ifi");
|
||||||
tmp = find_and_replace(tmp, "o", "ofo");
|
tmp = find_and_replace(tmp, "o", "ofo");
|
||||||
tmp = find_and_replace(tmp, "u", "ufu");
|
tmp = find_and_replace(tmp, "u", "ufu");
|
||||||
ASSERT_STREQ(
|
ASSERT_STREQ(
|
||||||
@@ -46,7 +47,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) {
|
|||||||
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
||||||
L"Jefeff"
|
L"Jefeff"
|
||||||
);
|
);
|
||||||
|
|
||||||
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
||||||
ASSERT_STREQ(
|
ASSERT_STREQ(
|
||||||
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
||||||
@@ -74,44 +74,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TEST(UtilsTest, TestWstrToStr) {
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"Hi!").c_str(),
|
|
||||||
"Hi!"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"Naif").c_str(),
|
|
||||||
"Naif"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"\r\r\r\n").c_str(),
|
|
||||||
"\r\r\r\n"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
|
|
||||||
"Naif\ttalks\r\ra\t\nlot"
|
|
||||||
);
|
|
||||||
} */
|
|
||||||
|
|
||||||
/* TEST(UtilsTest, TestStrToWstr) {
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("Hi!").c_str(),
|
|
||||||
L"Hi!"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("Naif").c_str(),
|
|
||||||
L"Naif"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("\r\r\r\n").c_str(),
|
|
||||||
L"\r\r\r\n"
|
|
||||||
);
|
|
||||||
ASSERT_STREQ(
|
|
||||||
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
|
|
||||||
L"Naif\ttalks\r\ra\t\nlot"
|
|
||||||
);
|
|
||||||
} */
|
|
||||||
|
|
||||||
struct A { bool x(); };
|
struct A { bool x(); };
|
||||||
struct B { bool y(); };
|
struct B { bool y(); };
|
||||||
struct C { bool x(); bool y; };
|
struct C { bool x(); bool y; };
|
||||||
@@ -145,4 +107,22 @@ TEST(UtilsTest, TestIsDetected) {
|
|||||||
ASSERT_TRUE(ret);
|
ASSERT_TRUE(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(DataTypesTests, TestNDArray) {
|
||||||
|
float* data_f = new float[9] {0,1,2,3,4,5,6,7,8};
|
||||||
|
nb::NDArray<float, 2> arr({3, 3}, data_f);
|
||||||
|
ASSERT_FLOAT_EQ(data_f[4], arr.at({1,1}));
|
||||||
|
arr.at({1,1}) = 0.5;
|
||||||
|
ASSERT_FLOAT_EQ(0.5, arr.at({1,1}));
|
||||||
|
delete[] data_f;
|
||||||
|
|
||||||
|
unsigned int* data_ui = new unsigned int[8] {0,1,2,3,4,5,6,7};
|
||||||
|
nb::NDArray<unsigned int, 3> arr3d({2,2,2}, data_ui);
|
||||||
|
ASSERT_EQ(data_ui[5], arr3d.at({1,0,1}));
|
||||||
|
arr3d.at({0,1,1}) = 23;
|
||||||
|
ASSERT_EQ(23, arr3d.at({0,1,1}));
|
||||||
|
delete[] data_ui;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@@ -56,18 +56,6 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
|||||||
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} PARENT_SCOPE)
|
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} PARENT_SCOPE)
|
||||||
set(NB_GRAPHICS_INCLUDE ${NB_GRAPHICS_INCLUDE} PARENT_SCOPE)
|
set(NB_GRAPHICS_INCLUDE ${NB_GRAPHICS_INCLUDE} PARENT_SCOPE)
|
||||||
|
|
||||||
if (NB_GRAPHICS_API_OGL)
|
|
||||||
toAbsolutePath(NB_GRAPHICS_API_OGL_SOURCE
|
|
||||||
./src/OGL/Buffers.cpp
|
|
||||||
)
|
|
||||||
toAbsolutePath(NB_GRAPHICS_API_OGL_INCLUDE
|
|
||||||
./include/NBGraphics/OGL/Buffers.hpp
|
|
||||||
)
|
|
||||||
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} ${NB_GRAPHICS_API_OGL_SOURCE} PARENT_SCOPE)
|
|
||||||
set(NB_GRAPHICS_INCLUDE ${NB_GRAPHICS_INCLUDE} ${NB_GRAPHICS_API_OGL_SOURCE} PARENT_SCOPE)
|
|
||||||
add_library(NBGraphicsAPI_OGL ${NB_GRAPHICS_API_OGL_SOURCE})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_library(NBGraphics
|
add_library(NBGraphics
|
||||||
${NB_GRAPHICS_SOURCE}
|
${NB_GRAPHICS_SOURCE}
|
||||||
${GLAD_PATH}/src/glad.c
|
${GLAD_PATH}/src/glad.c
|
||||||
@@ -75,11 +63,6 @@ add_library(NBGraphics
|
|||||||
add_library(NBEngine::Graphics ALIAS NBGraphics)
|
add_library(NBEngine::Graphics ALIAS NBGraphics)
|
||||||
add_dependencies(NBGraphics NBCore)
|
add_dependencies(NBGraphics NBCore)
|
||||||
add_dependencies(NBGraphics glfw)
|
add_dependencies(NBGraphics glfw)
|
||||||
|
|
||||||
if(NB_GRAPHICS_API_OGL)
|
|
||||||
add_dependencies(NBGraphics NBGraphicsAPI_OGL)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_link_libraries(NBGraphics
|
target_link_libraries(NBGraphics
|
||||||
PUBLIC glfw
|
PUBLIC glfw
|
||||||
PUBLIC NBCore
|
PUBLIC NBCore
|
||||||
|
|||||||
@@ -1,262 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_BUFFER
|
|
||||||
#define _NB_BUFFER
|
|
||||||
|
|
||||||
#include <NBGraphics/GLLoad.hpp>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
#include <NBCore/Logger.hpp>
|
|
||||||
#include <NBCore/Utils.hpp>
|
|
||||||
|
|
||||||
#include <NBGraphics/OGLObjects.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
extern const ConstantMap <GLenum, std::string> BufferTypes;
|
|
||||||
|
|
||||||
template<GLenum N>
|
|
||||||
struct GLSLEnum;
|
|
||||||
template<typename T>
|
|
||||||
struct GLSLType;
|
|
||||||
|
|
||||||
#define GLSLTypeTraits(type_, size_, value_) \
|
|
||||||
template<>\
|
|
||||||
struct GLSLEnum<value_> {\
|
|
||||||
using type = type_;\
|
|
||||||
static constexpr size_t size = size_;\
|
|
||||||
static constexpr GLenum value=value_;\
|
|
||||||
};\
|
|
||||||
template<>\
|
|
||||||
struct GLSLType<type_> {\
|
|
||||||
using type = type_;\
|
|
||||||
static constexpr size_t size = size_;\
|
|
||||||
static constexpr GLenum value=value_;\
|
|
||||||
};
|
|
||||||
|
|
||||||
#define GLSLTypeEquivalence(original, newtype) \
|
|
||||||
template<>\
|
|
||||||
struct GLSLType<newtype> : public GLSLType<original> {};
|
|
||||||
|
|
||||||
GLSLTypeTraits(uint8_t, 1, GL_UNSIGNED_BYTE);
|
|
||||||
GLSLTypeTraits(uint16_t, 2, GL_UNSIGNED_SHORT);
|
|
||||||
GLSLTypeTraits(uint32_t, 4, GL_UNSIGNED_INT);
|
|
||||||
GLSLTypeTraits(int8_t, 1, GL_BYTE);
|
|
||||||
GLSLTypeTraits(int16_t, 2, GL_SHORT);
|
|
||||||
GLSLTypeTraits(int32_t, 4, GL_INT);
|
|
||||||
GLSLTypeTraits(float, 4, GL_FLOAT);
|
|
||||||
GLSLTypeTraits(double, 8, GL_DOUBLE);
|
|
||||||
|
|
||||||
class BufferError : public Error<BufferError> {
|
|
||||||
protected:
|
|
||||||
using Base = Error<BufferError>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED,
|
|
||||||
DATA_OVERFLOW,
|
|
||||||
DATA_ERROR,
|
|
||||||
BAD_DATA_TYPE
|
|
||||||
};
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Buffer : public OpenGLObject {
|
|
||||||
protected:
|
|
||||||
using Codes = BufferError::Codes;
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _usage = GL_STATIC_DRAW;
|
|
||||||
size_t _size;
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
OPENGL_CALL(glGenBuffers(1, &_id));
|
|
||||||
}
|
|
||||||
this->bind();
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
virtual void remove() override {
|
|
||||||
if (_id) {
|
|
||||||
this->unbind();
|
|
||||||
OPENGL_CALL(glDeleteBuffers(1, &_id));
|
|
||||||
_id = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
const GLenum Target;
|
|
||||||
using OpenGLObject::OpenGLObject;
|
|
||||||
Buffer(GLenum target);
|
|
||||||
Buffer(GLenum target, const ByteVector&, GLenum usage=GL_STATIC_DRAW);
|
|
||||||
Buffer(Buffer&& other);
|
|
||||||
Buffer& operator=(Buffer&& rhs);
|
|
||||||
|
|
||||||
virtual void bind() const override {
|
|
||||||
if (_id) {
|
|
||||||
OPENGL_CALL(glBindBuffer(Target, _id));
|
|
||||||
} else {
|
|
||||||
THROW(OpenGLObjectError(
|
|
||||||
OpenGLObjectError::Codes::INVALID_OBJECT,
|
|
||||||
"w/ BufferType " + BufferTypes[Target]
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual void unbind() const override {
|
|
||||||
OPENGL_CALL(glBindBuffer(Target, 0));
|
|
||||||
}
|
|
||||||
GLenum usage() const;
|
|
||||||
size_t size() const;
|
|
||||||
ByteVector data() const;
|
|
||||||
|
|
||||||
void data(const ByteVector& data, GLenum usage=GL_STATIC_DRAW);
|
|
||||||
void data(const void* data, size_t size, GLenum usage=GL_STATIC_DRAW);
|
|
||||||
void subdata(const void* data, size_t size, GLintptr offset=0);
|
|
||||||
void subdata(const ByteVector& data_, GLintptr offset=0);
|
|
||||||
// virtual void clear() const { glClearBufferData(Target, ) }
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/* template <typename BufferType>
|
|
||||||
class ImmutableBuffer : public virtual Buffer {
|
|
||||||
public:
|
|
||||||
ImmutableBuffer() = default;
|
|
||||||
ImmutableBuffer(
|
|
||||||
size_t size_,
|
|
||||||
GLenum usage_ = GL_STATIC_DRAW,
|
|
||||||
GLbitfield flags_=0x0
|
|
||||||
) : size(size_), Base::usage(usage_) {
|
|
||||||
glBufferStorage(Target, size, nullptr, flags_);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::weak_ptr<void> map(GLbitfield access_) {
|
|
||||||
if (_map && _mapAccess != access_) { unmap(); }
|
|
||||||
_mapAccess = access_;
|
|
||||||
if (!_map) {
|
|
||||||
bind();
|
|
||||||
glMapBuffer(Target, _mapAccess);
|
|
||||||
}
|
|
||||||
return _map;
|
|
||||||
}
|
|
||||||
void unmap() {
|
|
||||||
bind();
|
|
||||||
glUnmapBuffer(Target);
|
|
||||||
_map.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t size;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::shared_ptr<void> _map = nullptr;
|
|
||||||
GLbitfield _mapAccess = 0;
|
|
||||||
|
|
||||||
}; */
|
|
||||||
|
|
||||||
class UniformBuffer : public Buffer {
|
|
||||||
protected:
|
|
||||||
GLuint _index;
|
|
||||||
|
|
||||||
public:
|
|
||||||
UniformBuffer();
|
|
||||||
void bind() const override;
|
|
||||||
GLuint bufferIndex() const;
|
|
||||||
GLuint bufferIndex(GLuint);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct IsValidGLElementIndexType {
|
|
||||||
static constexpr bool value = false;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct ValidGLElementIndexType {
|
|
||||||
static constexpr bool value = true;
|
|
||||||
static constexpr GLenum glsltype = GLSLType<T>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct IsValidGLElementIndexType<uint8_t> : public ValidGLElementIndexType<uint8_t> {};
|
|
||||||
template<>
|
|
||||||
struct IsValidGLElementIndexType<uint16_t> : public ValidGLElementIndexType<uint16_t> {};
|
|
||||||
template<>
|
|
||||||
struct IsValidGLElementIndexType<uint32_t> : public ValidGLElementIndexType<uint32_t> {};
|
|
||||||
|
|
||||||
class ElementBuffer : Buffer {
|
|
||||||
private:
|
|
||||||
template<typename T>
|
|
||||||
inline void checkForElementDataType() {
|
|
||||||
static_assert(IsValidGLElementIndexType<T>::value,
|
|
||||||
"ElementBuffer data type must resolve to one of GL_UNSIGNED_BYTE,"
|
|
||||||
"GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Codes = BufferError::Codes;
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
using Buffer::_usage;
|
|
||||||
using Buffer::_size;
|
|
||||||
using Buffer::declare;
|
|
||||||
using Buffer::remove;
|
|
||||||
GLenum _datatype;
|
|
||||||
template<typename T>
|
|
||||||
inline void checkDatatypeConsistency() {
|
|
||||||
if (_datatype != ValidGLElementIndexType<T>::value) {
|
|
||||||
THROW(BufferError(
|
|
||||||
Codes::BAD_DATA_TYPE,
|
|
||||||
GLenumTable[ValidGLElementIndexType<T>::value] + " not same as buffer "
|
|
||||||
"data type "+GLenumTable[_datatype]
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Buffer::Target;
|
|
||||||
using Buffer::OpenGLObject;
|
|
||||||
using Buffer::bind;
|
|
||||||
using Buffer::unbind;
|
|
||||||
using Buffer::usage;
|
|
||||||
using Buffer::size;
|
|
||||||
template<typename T>
|
|
||||||
ElementBuffer(const std::vector<T>& indices, GLenum usage=GL_STATIC_DRAW) : Buffer(GL_ELEMENT_ARRAY_BUFFER) {
|
|
||||||
data(indices, usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline GLenum datatype() { return _datatype; }
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void data(const std::vector<T>& data, GLenum usage=GL_STATIC_DRAW) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
_datatype = ValidGLElementIndexType<T>::glsltype;
|
|
||||||
Buffer::data(vectorToBytes(data), usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void data(const T* data, size_t size, GLenum usage=GL_STATIC_DRAW) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
_datatype = ValidGLElementIndexType<T>::glsltype;
|
|
||||||
Buffer::data((void*)data, size * sizeof(T), usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void subdata(const T* data, size_t size, GLintptr offset=0) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
checkDatatypeConsistency<T>();
|
|
||||||
Buffer::subdata((void*)data, size * sizeof(T), offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void subdata(const std::vector<T>& data_, GLintptr offset=0) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
checkDatatypeConsistency<T>();
|
|
||||||
Buffer::subdata(vectorToBytes(data_), offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace NB
|
|
||||||
#endif // _NB_BUFFER
|
|
||||||
Reference in New Issue
Block a user