Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4b6c55645 | ||
|
|
416446206f |
@@ -76,6 +76,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
set(NB_TARGET_LINUX ON)
|
||||
endif()
|
||||
|
||||
set(NB_GRAPHICS_API_OGL ON)
|
||||
|
||||
if(NB_BUILD_TESTS)
|
||||
message(STATUS "Building tests")
|
||||
enable_testing()
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#define _NB_TYPE_TRAITS
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace nb {
|
||||
|
||||
@@ -29,6 +30,9 @@ namespace detail {
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template<typename K, typename V>
|
||||
std::pair<K, V> KeyVal(K key, V val) { return std::pair(key, val); }
|
||||
|
||||
enum TypeCodes {
|
||||
UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32, INT64,
|
||||
FLOAT16, FLOAT32, DOUBLE64, BOOL, NONE
|
||||
|
||||
@@ -56,6 +56,18 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
||||
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} 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
|
||||
${NB_GRAPHICS_SOURCE}
|
||||
${GLAD_PATH}/src/glad.c
|
||||
@@ -63,6 +75,11 @@ add_library(NBGraphics
|
||||
add_library(NBEngine::Graphics ALIAS NBGraphics)
|
||||
add_dependencies(NBGraphics NBCore)
|
||||
add_dependencies(NBGraphics glfw)
|
||||
|
||||
if(NB_GRAPHICS_API_OGL)
|
||||
add_dependencies(NBGraphics NBGraphicsAPI_OGL)
|
||||
endif()
|
||||
|
||||
target_link_libraries(NBGraphics
|
||||
PUBLIC glfw
|
||||
PUBLIC NBCore
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
#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
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
@@ -126,21 +128,26 @@ class OGLWindow {
|
||||
static thread_local GLFWwindow* _currWindow;
|
||||
static thread_local const GLFWwindow* _currArena;
|
||||
|
||||
static void SetHintInteger(const std::array<int, 2>&);
|
||||
static void SetHintString(const std::tuple<int, std::string>&);
|
||||
template<typename T>
|
||||
static void SetHint(std::pair<int, T> hint) {
|
||||
if constexpr ( std::is_same_v<T, std::string> ) {
|
||||
glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str());
|
||||
} else if constexpr ( std::is_same_v<T, int> ) {
|
||||
glfwWindowHint(std::get<0>(hint), std::get<1>(hint));
|
||||
}
|
||||
}
|
||||
|
||||
std::string _title;
|
||||
GLFWwindow* _window_ptr;
|
||||
const GLFWwindow* _arena;
|
||||
|
||||
OGLWindow(
|
||||
unsigned int,
|
||||
unsigned int,
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string&,
|
||||
const std::vector<std::array<int, 2>>& integerWindowHints,
|
||||
const std::vector<std::tuple<int, std::string>>& stringWindowHints={},
|
||||
GLFWwindow* sharingContext = NULL
|
||||
);
|
||||
|
||||
GLFWwindow* makeCurrent() const;
|
||||
void detachWindow();
|
||||
|
||||
@@ -154,21 +161,37 @@ class OGLWindow {
|
||||
static GLFWwindow* MakeCurrent(const std::shared_ptr<OGLWindow>&);
|
||||
static inline GLFWwindow* DropCurrent() { return MakeCurrent(nullptr); }
|
||||
static void DestroyWindow(std::shared_ptr<OGLWindow>&);
|
||||
template <typename... T>
|
||||
static std::shared_ptr<OGLWindow> CreateWindow(
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string& title,
|
||||
const std::vector<std::array<int, 2>>& integerHints={},
|
||||
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||
);
|
||||
const std::pair<int, T>&... windowHints
|
||||
) {
|
||||
(SetHint(std::pair(windowHints)),... );
|
||||
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
||||
x,
|
||||
y,
|
||||
title,
|
||||
nullptr
|
||||
) );
|
||||
}
|
||||
template<typename... T>
|
||||
static std::shared_ptr<OGLWindow> CreateSharedWindow(
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string& title,
|
||||
std::shared_ptr<OGLWindow> sharingWindow,
|
||||
const std::vector<std::array<int, 2>>& integerHints={},
|
||||
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||
);
|
||||
std::shared_ptr<OGLWindow> sharingWindow = NULL,
|
||||
const std::pair<int, T>&... windowHints
|
||||
) {
|
||||
(SetHint(std::pair(windowHints)),... );
|
||||
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
||||
x,
|
||||
y,
|
||||
title,
|
||||
sharingWindow->_window_ptr
|
||||
) );
|
||||
}
|
||||
|
||||
OGLWindow(OGLWindow&&) = delete;
|
||||
OGLWindow(const OGLWindow&) = delete;
|
||||
|
||||
@@ -48,16 +48,6 @@ bool OGLWindow::TerminateGLFW() {
|
||||
return !IsGLFWInitialized();
|
||||
}
|
||||
|
||||
void OGLWindow::SetHintInteger(const std::array<int, 2>& hint) {
|
||||
CheckForMainThread();
|
||||
glfwWindowHint(hint[0], hint[1]);
|
||||
}
|
||||
|
||||
void OGLWindow::SetHintString(const std::tuple<int, std::string>& hint) {
|
||||
CheckForMainThread();
|
||||
glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str());
|
||||
}
|
||||
|
||||
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
||||
auto tmp = _currWindow;
|
||||
if (window) {
|
||||
@@ -71,56 +61,14 @@ GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
std::shared_ptr<OGLWindow> OGLWindow::CreateWindow(
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string& title,
|
||||
const std::vector<std::array<int, 2>>& integerHints,
|
||||
const std::vector<std::tuple<int, std::string>>& stringHints
|
||||
) {
|
||||
return std::shared_ptr<OGLWindow>( new OGLWindow(
|
||||
x,
|
||||
y,
|
||||
title,
|
||||
integerHints,
|
||||
stringHints
|
||||
) );
|
||||
}
|
||||
|
||||
std::shared_ptr<OGLWindow> OGLWindow::CreateSharedWindow(
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string& title,
|
||||
std::shared_ptr<OGLWindow> sharingWindow,
|
||||
const std::vector<std::array<int, 2>>& integerHints,
|
||||
const std::vector<std::tuple<int, std::string>>& stringHints
|
||||
) {
|
||||
return std::shared_ptr<OGLWindow>( new OGLWindow(
|
||||
x,
|
||||
y,
|
||||
title,
|
||||
integerHints,
|
||||
stringHints,
|
||||
sharingWindow->getWindowPtr()
|
||||
) );
|
||||
}
|
||||
|
||||
OGLWindow::OGLWindow(
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string& title,
|
||||
const std::vector<std::array<int, 2>>& integerHints,
|
||||
const std::vector<std::tuple<int, std::string>>& stringHints,
|
||||
const std::string&,
|
||||
GLFWwindow* sharingContext
|
||||
) : _title(title) {
|
||||
) {
|
||||
CheckForMainThread();
|
||||
InitGLFW();
|
||||
for (const auto& hint : integerHints) {
|
||||
SetHintInteger(hint);
|
||||
}
|
||||
for (const auto& hint : stringHints) {
|
||||
SetHintString(hint);
|
||||
}
|
||||
_window_ptr = glfwCreateWindow(
|
||||
x,
|
||||
y,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "stb_image.h"
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
#include <NBCore/TypeTraits.hpp>
|
||||
#include <NBGraphics/Image.hpp>
|
||||
#include <NBGraphics/Mesh.hpp>
|
||||
#include <NBGraphics/ProgramPipeline.hpp>
|
||||
@@ -17,8 +18,8 @@ int main() {
|
||||
400,
|
||||
400,
|
||||
"Howdy!",
|
||||
{{GLFW_CONTEXT_VERSION_MAJOR, 3},
|
||||
{GLFW_CONTEXT_VERSION_MINOR, 3}}
|
||||
nb::KeyVal(GLFW_CONTEXT_VERSION_MAJOR, 3),
|
||||
nb::KeyVal(GLFW_CONTEXT_VERSION_MINOR, 3)
|
||||
);
|
||||
nb::OGLWindow::MakeCurrent(window);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user