Compare commits
4 Commits
521483f4e0
...
1efe2a0a7c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1efe2a0a7c | ||
|
|
ea632614b0 | ||
|
|
3048d524b7 | ||
|
|
7648a77e1a |
@ -1,5 +1,5 @@
|
|||||||
toAbsolutePath(NB_CORE_SOURCE
|
toAbsolutePath(NB_CORE_SOURCE
|
||||||
./src/ErrorsImpl.cpp
|
./src/Errors.cpp
|
||||||
./src/Logger.cpp
|
./src/Logger.cpp
|
||||||
./src/Processes.cpp
|
./src/Processes.cpp
|
||||||
./src/StringUtils.cpp
|
./src/StringUtils.cpp
|
||||||
@ -10,7 +10,6 @@ toAbsolutePath(NB_CORE_INCLUDE
|
|||||||
./ANSITerm.hpp
|
./ANSITerm.hpp
|
||||||
./DataSink.hpp
|
./DataSink.hpp
|
||||||
./Errors.hpp
|
./Errors.hpp
|
||||||
./ErrorsImpl.hpp
|
|
||||||
./Logger.hpp
|
./Logger.hpp
|
||||||
./Processes.hpp
|
./Processes.hpp
|
||||||
./StringUtils.hpp
|
./StringUtils.hpp
|
||||||
|
|||||||
@ -76,7 +76,7 @@ class MultithreadedDataProcessor
|
|||||||
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||||
|
|
||||||
bool isRunning() const noexcept override {
|
bool isRunning() const noexcept override {
|
||||||
return this->_running;
|
return this->_running && (_runningThread!=nullptr);
|
||||||
}
|
}
|
||||||
bool run() override {
|
bool run() override {
|
||||||
if (!type_ptr->isRunning()) {
|
if (!type_ptr->isRunning()) {
|
||||||
|
|||||||
@ -2,11 +2,281 @@
|
|||||||
#ifndef _NB_ERROR
|
#ifndef _NB_ERROR
|
||||||
#define _NB_ERROR
|
#define _NB_ERROR
|
||||||
|
|
||||||
#include "ErrorsImpl.hpp"
|
#include <exception>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
#include "StringUtils.hpp"
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool IsValidException_v = std::is_base_of_v<std::exception, T>;
|
||||||
|
template <typename T>
|
||||||
|
using IsValidException = std::enable_if_t<IsValidException_v<T>, bool>;
|
||||||
|
|
||||||
|
template<class ErrorType=NoneType>
|
||||||
|
class ErrorBase;
|
||||||
|
|
||||||
|
template <class ErrorType=NoneType>
|
||||||
|
class Error;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool IsValidNBError_v = std::is_base_of_v<ErrorBase<NoneType>, T>;
|
||||||
|
template <typename T>
|
||||||
|
using IsValidNBError = std::enable_if_t<IsValidNBError_v<T>, bool>;
|
||||||
|
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class ErrorBase<NoneType> {
|
||||||
|
public:
|
||||||
|
const unsigned int code;
|
||||||
|
const std::string msg;
|
||||||
|
const std::string type;
|
||||||
|
const std::shared_ptr<const void> trace;
|
||||||
|
const bool traceIsNBError;
|
||||||
|
|
||||||
|
virtual std::string what() const noexcept = 0;
|
||||||
|
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
template<typename T=NoneType>
|
||||||
|
ErrorBase(const ErrorBase<T>& err) : ErrorBase(std::move(err)) {}
|
||||||
|
|
||||||
|
template<typename T=NoneType>
|
||||||
|
ErrorBase(ErrorBase<T>&& err) : ErrorBase(
|
||||||
|
err.code,
|
||||||
|
err.msg,
|
||||||
|
err.type,
|
||||||
|
(err.traceIsNBError)
|
||||||
|
? static_cast<const ErrorBase*>(err.trace.get())
|
||||||
|
: static_cast<const std::exception*>(err.trace.get())
|
||||||
|
) {}
|
||||||
|
|
||||||
|
ErrorBase(
|
||||||
|
unsigned int code_,
|
||||||
|
std::string msg_,
|
||||||
|
std::string type_,
|
||||||
|
const std::exception* trace_
|
||||||
|
) noexcept :
|
||||||
|
code(code_),
|
||||||
|
msg(msg_),
|
||||||
|
type(type_),
|
||||||
|
trace{ trace_ ?
|
||||||
|
std::static_pointer_cast<const void>(
|
||||||
|
std::make_shared<Error<NoneType>>(*trace_))
|
||||||
|
: nullptr,
|
||||||
|
},
|
||||||
|
traceIsNBError(false)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T, std::enable_if_t<IsValidNBError_v<T>, bool> = true>
|
||||||
|
ErrorBase(
|
||||||
|
unsigned int code_,
|
||||||
|
std::string msg_,
|
||||||
|
std::string type_,
|
||||||
|
const T* trace_
|
||||||
|
) noexcept :
|
||||||
|
code(code_),
|
||||||
|
msg(msg_),
|
||||||
|
type(type_),
|
||||||
|
trace{trace_ ? trace_->make_shared() : nullptr},
|
||||||
|
traceIsNBError(true)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <class ErrorType>
|
||||||
|
class ErrorBase : public ErrorBase<NoneType> {
|
||||||
|
private:
|
||||||
|
using Base = ErrorBase<NoneType>;
|
||||||
|
void inline check_asserts() {
|
||||||
|
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
|
||||||
|
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
|
||||||
|
"a class member."
|
||||||
|
);
|
||||||
|
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
|
||||||
|
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
|
||||||
|
"enum Codes must be of underlying type unsigned int."
|
||||||
|
);
|
||||||
|
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
||||||
|
"const std::string type must be a class member."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <typename T>
|
||||||
|
ErrorBase(const ErrorBase<T>& cpy) : Base(cpy) { check_asserts(); }
|
||||||
|
|
||||||
|
virtual std::shared_ptr<Base> make_shared() const noexcept override {
|
||||||
|
return std::static_pointer_cast<Base>(
|
||||||
|
std::make_shared<ErrorBase>(*this)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual std::string what() const noexcept override {
|
||||||
|
std::string ret = msg;
|
||||||
|
if (trace) {
|
||||||
|
std::string trace_msg;
|
||||||
|
if (traceIsNBError) {
|
||||||
|
trace_msg = std::static_pointer_cast<const Base>(trace)->what();
|
||||||
|
} else {
|
||||||
|
trace_msg = std::string(std::static_pointer_cast<const Base>(trace)->what());
|
||||||
|
}
|
||||||
|
ret += nb::NEWLINE + indent_strblock(
|
||||||
|
trace_msg,
|
||||||
|
nb::TABOVER,
|
||||||
|
nb::TABOVER+"Trace: "
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::traceIsNBError;
|
||||||
|
|
||||||
|
friend ErrorType;
|
||||||
|
friend Error<ErrorType>;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
ErrorBase(
|
||||||
|
unsigned int code_,
|
||||||
|
std::string msg_,
|
||||||
|
std::string type_,
|
||||||
|
const T* trace_
|
||||||
|
) : Base(
|
||||||
|
code_,
|
||||||
|
msg_,
|
||||||
|
type_,
|
||||||
|
trace_
|
||||||
|
) { check_asserts(); }
|
||||||
|
};
|
||||||
|
template <typename ErrorType>
|
||||||
|
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
||||||
|
template <typename ErrorType>
|
||||||
|
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
||||||
|
|
||||||
|
|
||||||
|
template <class ErrorType>
|
||||||
|
class Error : public ErrorBase<ErrorType> {
|
||||||
|
using Base = ErrorBase<ErrorType>;
|
||||||
|
public:
|
||||||
|
template <typename T>
|
||||||
|
Error(
|
||||||
|
unsigned int code_,
|
||||||
|
const T& trace_
|
||||||
|
) noexcept : Base(
|
||||||
|
code_,
|
||||||
|
ErrorType::ErrorMessages.at(code_),
|
||||||
|
ErrorType::type,
|
||||||
|
&trace_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
template<typename ET>
|
||||||
|
Error(
|
||||||
|
std::string msg_,
|
||||||
|
const ErrorBase<ET>& trace_
|
||||||
|
) noexcept : Base(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
ErrorType::type,
|
||||||
|
&trace_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(
|
||||||
|
std::string msg_,
|
||||||
|
const std::exception& trace_
|
||||||
|
) noexcept : Base(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
ErrorType::type,
|
||||||
|
&trace_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(unsigned int code_) noexcept : Base(
|
||||||
|
code_,
|
||||||
|
ErrorType::ErrorMessages.at(code_),
|
||||||
|
ErrorType::type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(std::string msg_) noexcept : Base(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
ErrorType::type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::traceIsNBError;
|
||||||
|
using Base::type;
|
||||||
|
using Base::ErrorMessages;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||||
|
using Base = ErrorBase<Error<NoneType>>;
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
Error(unsigned int code_=1) noexcept : Base(
|
||||||
|
code_,
|
||||||
|
ErrorMessages.at(code_),
|
||||||
|
type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(const std::exception& err) : Base(
|
||||||
|
Codes::STANDARD,
|
||||||
|
std::string(err.what()),
|
||||||
|
"std::exception",
|
||||||
|
nullptr
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(std::string msg_) noexcept : Base(
|
||||||
|
Codes::UNDEFINED,
|
||||||
|
msg_,
|
||||||
|
type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
STANDARD, UNDEFINED
|
||||||
|
};
|
||||||
|
|
||||||
|
using Base::what;
|
||||||
|
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::traceIsNBError;
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
} // namespace nb
|
||||||
|
|
||||||
#ifdef _NB_AUTOLOG
|
#ifdef _NB_AUTOLOG
|
||||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||||
#ifndef LOG
|
#ifndef LOG
|
||||||
@ -33,11 +303,10 @@ namespace nb {
|
|||||||
|
|
||||||
#ifndef THROW
|
#ifndef THROW
|
||||||
#ifdef _NB_AUTOLOG
|
#ifdef _NB_AUTOLOG
|
||||||
#define THROW(args...) ERROR(args); nb::logger.stop(); throw args
|
#define THROW(args...) ERROR(args); throw args
|
||||||
#else
|
#else
|
||||||
#define THROW(args...) throw args
|
#define THROW(args...) throw args
|
||||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||||
#endif // THROW
|
#endif // THROW
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_ERROR
|
#endif // _NB_ERROR
|
||||||
@ -1,175 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_ERRORS_IMPL
|
|
||||||
#define _NB_ERRORS_IMPL
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include "StringUtils.hpp"
|
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
|
|
||||||
|
|
||||||
template <class ErrorType=NoneType>
|
|
||||||
class Error;
|
|
||||||
|
|
||||||
class ErrorBase {
|
|
||||||
protected:
|
|
||||||
|
|
||||||
public:
|
|
||||||
const unsigned int code;
|
|
||||||
const std::string msg;
|
|
||||||
const std::string type;
|
|
||||||
const std::shared_ptr<ErrorBase> trace;
|
|
||||||
|
|
||||||
ErrorBase(const ErrorBase&) = default;
|
|
||||||
ErrorBase(const std::exception&) noexcept;
|
|
||||||
virtual std::string what() const noexcept {
|
|
||||||
std::string ret = msg;
|
|
||||||
if (trace) {
|
|
||||||
std::string trace_msg = msg;
|
|
||||||
ret += nb::NEWLINE + indent_strblock(
|
|
||||||
trace_msg,
|
|
||||||
nb::TABOVER,
|
|
||||||
nb::TABOVER+"Trace: "
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
ErrorBase(
|
|
||||||
unsigned int code_,
|
|
||||||
std::string msg_,
|
|
||||||
std::string type_,
|
|
||||||
std::shared_ptr<ErrorBase> trace_
|
|
||||||
) noexcept :
|
|
||||||
code(code_),
|
|
||||||
msg(msg_),
|
|
||||||
type(type_),
|
|
||||||
trace{trace_}
|
|
||||||
{}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class ErrorType>
|
|
||||||
class Error : public ErrorBase {
|
|
||||||
private:
|
|
||||||
void inline check_asserts() {
|
|
||||||
static_assert(std::is_same<const ErrorCodeMap, decltype(ErrorType::ErrorMessages)>::value,
|
|
||||||
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
|
|
||||||
"a class member."
|
|
||||||
);
|
|
||||||
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
|
|
||||||
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
|
|
||||||
"enum Codes must be of underlying type unsigned int."
|
|
||||||
);
|
|
||||||
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
|
||||||
"const std::string type must be a class member."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using ErrorBase::ErrorBase;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Error(
|
|
||||||
unsigned int code_,
|
|
||||||
const ErrorBase& trace_
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages.at(code_),
|
|
||||||
ErrorType::type,
|
|
||||||
std::make_shared<ErrorBase>(trace_)
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(
|
|
||||||
std::string msg_,
|
|
||||||
const ErrorBase& trace_
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
0,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
std::make_shared<ErrorBase>(trace_)
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(unsigned int code_) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages.at(code_),
|
|
||||||
ErrorType::type,
|
|
||||||
nullptr
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(std::string msg_) noexcept : ErrorBase(
|
|
||||||
0,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
nullptr
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(
|
|
||||||
unsigned int code_,
|
|
||||||
const std::string& note_,
|
|
||||||
const ErrorBase& trace_
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
|
||||||
ErrorType::type,
|
|
||||||
std::make_shared<ErrorBase>(trace_)
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(unsigned int code_, const std::string& note_) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
|
||||||
ErrorType::type,
|
|
||||||
nullptr
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
using ErrorBase::what;
|
|
||||||
using ErrorBase::code;
|
|
||||||
using ErrorBase::msg;
|
|
||||||
using ErrorBase::trace;
|
|
||||||
using ErrorBase::type;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<>
|
|
||||||
class Error<NoneType> : public Error<Error<NoneType>> {
|
|
||||||
using Base = Error<Error<NoneType>>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
Error(unsigned int code_=1) noexcept : Base(code_) {}
|
|
||||||
|
|
||||||
Error(const std::exception& err) : Base(
|
|
||||||
Codes::STANDARD,
|
|
||||||
std::string(err.what()),
|
|
||||||
"std::exception",
|
|
||||||
nullptr
|
|
||||||
) {}
|
|
||||||
|
|
||||||
// fix
|
|
||||||
Error(std::string msg_) noexcept : Base(
|
|
||||||
Codes::UNDEFINED,
|
|
||||||
msg_
|
|
||||||
) {}
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
STANDARD, UNDEFINED
|
|
||||||
};
|
|
||||||
|
|
||||||
using Base::what;
|
|
||||||
using Base::code;
|
|
||||||
using Base::msg;
|
|
||||||
using Base::trace;
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_ERRORS_IMPL
|
|
||||||
@ -9,13 +9,13 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "DataSink.hpp"
|
#include "DataSink.hpp"
|
||||||
#include "ErrorsImpl.hpp"
|
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include "ThreadSafeQueue.hpp"
|
#include "ThreadSafeQueue.hpp"
|
||||||
#include "TypeTraits.hpp"
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
class ErrorBase;
|
class ErrorBase;
|
||||||
|
|
||||||
typedef std::chrono::time_point<
|
typedef std::chrono::time_point<
|
||||||
@ -32,7 +32,6 @@ class LoggerBase
|
|||||||
using StreamType = ST;
|
using StreamType = ST;
|
||||||
using LoggerType = Logger;
|
using LoggerType = Logger;
|
||||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool run() override {
|
bool run() override {
|
||||||
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
||||||
@ -46,8 +45,6 @@ class LoggerBase
|
|||||||
}
|
}
|
||||||
return static_cast<LoggerType*>(this)->isRunning();
|
return static_cast<LoggerType*>(this)->isRunning();
|
||||||
}
|
}
|
||||||
using Base::flush;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
LoggerBase() = default;
|
LoggerBase() = default;
|
||||||
|
|
||||||
@ -76,33 +73,7 @@ public:
|
|||||||
|
|
||||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||||
|
|
||||||
template<typename U>
|
|
||||||
void log(
|
void log(
|
||||||
U val,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) { static_cast<LoggerType*>(this)->write_message(val, 0x00, file, line); }
|
|
||||||
|
|
||||||
template<typename U>
|
|
||||||
void warn(
|
|
||||||
U val,
|
|
||||||
uint8_t lvl=0x01,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) { static_cast<LoggerType*>(this)->write_message(val, lvl, file, line); }
|
|
||||||
|
|
||||||
void error(
|
|
||||||
const ErrorBase& val,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
static_cast<LoggerType*>(this)->write_message(val, 0xFF, file, line);
|
|
||||||
static_cast<LoggerType*>(this)->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::vector<std::ostream*> _ostream;
|
|
||||||
void write_message(
|
|
||||||
std::string msg,
|
std::string msg,
|
||||||
uint8_t lvl=0x00,
|
uint8_t lvl=0x00,
|
||||||
std::string file="",
|
std::string file="",
|
||||||
@ -120,24 +91,56 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
void write_message(
|
void log(
|
||||||
char const(&msg) [N],
|
char const(&msg) [N],
|
||||||
uint8_t lvl=0x00,
|
uint8_t lvl=0x00,
|
||||||
std::string file="",
|
std::string file="",
|
||||||
unsigned int line=0
|
unsigned int line=0
|
||||||
) {
|
) {
|
||||||
static_cast<LoggerType*>(this)->write_message(std::string(msg), lvl, file, line);
|
static_cast<LoggerType*>(this)->log(std::string(msg), lvl, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_message(
|
template <typename T>
|
||||||
const ErrorBase& err,
|
void log(
|
||||||
uint8_t lvl=0x00,
|
const ErrorBase<T>& err,
|
||||||
|
uint8_t lvl=0xFF,
|
||||||
std::string file="",
|
std::string file="",
|
||||||
unsigned int line=0
|
unsigned int line=0
|
||||||
) {
|
) {
|
||||||
static_cast<LoggerType*>(this)->write_message(err.what(), lvl, file, line);
|
static_cast<LoggerType*>(this)->log(err.what(), lvl, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::enable_if_t<std::is_base_of_v<std::exception, T>, void> log(
|
||||||
|
const T& err,
|
||||||
|
uint8_t lvl=0xFF,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->log(std::string(err.what()), lvl, file, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U>
|
||||||
|
void warn(
|
||||||
|
U val,
|
||||||
|
uint8_t lvl=0x01,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) { static_cast<LoggerType*>(this)->log(val, lvl, file, line); }
|
||||||
|
|
||||||
|
template<typename U>
|
||||||
|
void error(
|
||||||
|
U val,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->log(val, 0xFF, file, line);
|
||||||
|
static_cast<LoggerType*>(this)->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<std::ostream*> _ostream;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||||
@ -163,8 +166,6 @@ protected:
|
|||||||
bool process(const LogEvent& msg);
|
bool process(const LogEvent& msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const bool LOGGER_RUNNING;
|
|
||||||
|
|
||||||
#ifndef _NB_NO_LOGGER
|
#ifndef _NB_NO_LOGGER
|
||||||
extern DefaultDebugLogger logger;
|
extern DefaultDebugLogger logger;
|
||||||
#endif // _NB_NO_LOGGER
|
#endif // _NB_NO_LOGGER
|
||||||
|
|||||||
@ -9,12 +9,6 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using SharedVector = std::vector<std::shared_ptr<T>>;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using RValueVector = std::vector<T&&>;
|
|
||||||
|
|
||||||
using ByteVector = std::vector<uint8_t>;
|
using ByteVector = std::vector<uint8_t>;
|
||||||
|
|
||||||
class ObjectManagerError : public Error<ObjectManagerError> {
|
class ObjectManagerError : public Error<ObjectManagerError> {
|
||||||
@ -31,38 +25,6 @@ class ObjectManagerError : public Error<ObjectManagerError> {
|
|||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
ByteVector vectorToBytes(const std::vector<T>& vec) {
|
|
||||||
unsigned int num_bytes = vec.size() * sizeof(T);
|
|
||||||
ByteVector ret(num_bytes);
|
|
||||||
memcpy(ret.data(), vec.data(), num_bytes);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename S>
|
|
||||||
ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
|
||||||
ByteVector vec1_raw = vectorToBytes<T>(vec1);
|
|
||||||
ByteVector vec2_raw = vectorToBytes<S>(vec2);
|
|
||||||
unsigned int vec1_raw_size = vec1_raw.size();
|
|
||||||
unsigned int vec2_raw_size = vec2_raw.size();
|
|
||||||
ByteVector ret(vec1_raw_size + vec2_raw_size);
|
|
||||||
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
|
||||||
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::vector<T> bytesToVector(const ByteVector& vec) {
|
|
||||||
if (vec.size() % sizeof(T) != 0) {
|
|
||||||
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
|
||||||
}
|
|
||||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
|
||||||
std::vector<T> ret(num_elmts);
|
|
||||||
memcpy(ret.data(), vec.data(), vec.size());
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ThreadsafeObjectLock;
|
class ThreadsafeObjectLock;
|
||||||
|
|
||||||
|
|||||||
13
engine/NBCore/src/Errors.cpp
Normal file
13
engine/NBCore/src/Errors.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "Errors.hpp"
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
const std::string Error<>::type = "nb::Error";
|
||||||
|
|
||||||
|
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
||||||
|
{Error::Codes::STANDARD, "std::exception"},
|
||||||
|
{Error::Codes::UNDEFINED, "Error"}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,16 +0,0 @@
|
|||||||
#include "ErrorsImpl.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
const std::string Error<>::type = "nb::Error";
|
|
||||||
const ErrorCodeMap Error<>::ErrorMessages = {
|
|
||||||
{Error::Codes::STANDARD, "std::exception"},
|
|
||||||
{Error::Codes::UNDEFINED, "Error"}
|
|
||||||
};
|
|
||||||
|
|
||||||
//ErrorBase::
|
|
||||||
|
|
||||||
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
|
|
||||||
: ErrorBase(Error<NoneType>(exception_)) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -33,6 +33,6 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
|||||||
return log.run();
|
return log.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
#include <NBCore/Logger.hpp>
|
#include <NBCore/Logger.hpp>
|
||||||
@ -15,20 +16,69 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
extern const std::unordered_map<GLenum, std::string> BufferTypes;
|
static uint8_t GLSLTypeSize(GLenum type) {
|
||||||
|
switch(type) {
|
||||||
|
case GL_SHORT:
|
||||||
|
case GL_UNSIGNED_SHORT:
|
||||||
|
case GL_HALF_FLOAT:
|
||||||
|
return 2;
|
||||||
|
break;
|
||||||
|
case GL_INT:
|
||||||
|
case GL_UNSIGNED_INT:
|
||||||
|
case GL_FLOAT:
|
||||||
|
return 4;
|
||||||
|
break;
|
||||||
|
case GL_DOUBLE:
|
||||||
|
return 8;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
ByteVector vectorToRaw(const std::vector<T>& vec) {
|
||||||
|
unsigned int num_bytes = vec.size() * sizeof(T);
|
||||||
|
ByteVector ret(num_bytes);
|
||||||
|
memcpy(ret.data(), vec.data(), num_bytes);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename S>
|
||||||
|
ByteVector concatVectorsToRaw(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
||||||
|
ByteVector vec1_raw = vectorToRaw<T>(vec1);
|
||||||
|
ByteVector vec2_raw = vectorToRaw<S>(vec2);
|
||||||
|
unsigned int vec1_raw_size = vec1_raw.size();
|
||||||
|
unsigned int vec2_raw_size = vec2_raw.size();
|
||||||
|
ByteVector ret(vec1_raw_size + vec2_raw_size);
|
||||||
|
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
||||||
|
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::vector<T> rawToVector(const ByteVector& vec) {
|
||||||
|
if (vec.size() % sizeof(T) != 0) {
|
||||||
|
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
||||||
|
}
|
||||||
|
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||||
|
std::vector<T> ret(num_elmts);
|
||||||
|
memcpy(ret.data(), vec.data(), vec.size());
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
class BufferError : public Error<BufferError> {
|
class BufferError : public Error<BufferError> {
|
||||||
protected:
|
|
||||||
using Base = Error<BufferError>;
|
using Base = Error<BufferError>;
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
enum Codes : unsigned int {
|
||||||
UNDEFINED,
|
UNDEFINED, DATA_OVERFLOW, INVALID_BUFFER, HANDLE_OVERWRITE
|
||||||
DATA_OVERFLOW,
|
|
||||||
INVALID_BUFFER,
|
|
||||||
HANDLE_OVERWRITE,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
@ -39,25 +89,13 @@ class Buffer : public OpenGLObject {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
using OpenGLObject::OpenGLObject;
|
using OpenGLObject::OpenGLObject;
|
||||||
|
|
||||||
Buffer() = default;
|
Buffer() = default;
|
||||||
Buffer(Buffer&& other) {
|
Buffer(Buffer&& other) {
|
||||||
*this = std::move(other);
|
*this = std::move(other);
|
||||||
}
|
}
|
||||||
Buffer& operator=(Buffer&& rhs) {
|
Buffer& operator=(Buffer&& rhs) {
|
||||||
auto targ_name = BufferTypes.at(Target);
|
if (_id) { THROW(BufferError(Codes::HANDLE_OVERWRITE)); }
|
||||||
if (Target != rhs.Target) {
|
|
||||||
THROW(BufferError(
|
|
||||||
Codes::INVALID_BUFFER,
|
|
||||||
"w/ ("+targ_name+") := ("+BufferTypes.at(rhs.Target)+")"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
//nb::logger.log(std::to_string(_id) + ":=" + std::to_string(rhs._id));
|
|
||||||
if (_id) {
|
|
||||||
THROW(BufferError(
|
|
||||||
Codes::HANDLE_OVERWRITE,
|
|
||||||
"w/ BufferType = " + targ_name
|
|
||||||
));
|
|
||||||
}
|
|
||||||
_id = rhs._id;
|
_id = rhs._id;
|
||||||
_usage = rhs._usage;
|
_usage = rhs._usage;
|
||||||
_size = rhs._size;
|
_size = rhs._size;
|
||||||
@ -71,12 +109,8 @@ class Buffer : public OpenGLObject {
|
|||||||
virtual void bind() const override {
|
virtual void bind() const override {
|
||||||
if (_id) {
|
if (_id) {
|
||||||
glBindBuffer(Target, _id);
|
glBindBuffer(Target, _id);
|
||||||
} else {
|
|
||||||
THROW(BufferError(
|
|
||||||
Codes::INVALID_BUFFER,
|
|
||||||
"w/ BufferType " + BufferTypes.at(Target)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
THROW(BufferError(Codes::INVALID_BUFFER));
|
||||||
}
|
}
|
||||||
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
||||||
virtual GLenum usage() const { return _usage; }
|
virtual GLenum usage() const { return _usage; }
|
||||||
@ -206,14 +240,13 @@ class ArrayBuffer<true>
|
|||||||
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
||||||
using BufferType::BufferType;
|
using BufferType::BufferType;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Base::Target;
|
using Base::Target;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<bool Immutable=false>
|
template<bool Immutable=false>
|
||||||
class ElementBuffer : public Buffer<ElementBuffer<false>> {
|
class ElementBuffer : public virtual Buffer<ElementBuffer<false>> {
|
||||||
using Base = Buffer<ElementBuffer<false>>;
|
using Base = Buffer<ElementBuffer<false>>;
|
||||||
using BufferType = Base;
|
using BufferType = Base;
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,6 @@ set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
|||||||
toAbsolutePath(NB_GRAPHICS_SOURCE
|
toAbsolutePath(NB_GRAPHICS_SOURCE
|
||||||
./src/Buffers.cpp
|
./src/Buffers.cpp
|
||||||
./src/OGLObjects.cpp
|
./src/OGLObjects.cpp
|
||||||
./src/ProgramPipeline.cpp
|
|
||||||
./src/Textures.cpp
|
./src/Textures.cpp
|
||||||
./src/VertexArray.cpp
|
./src/VertexArray.cpp
|
||||||
./src/Window.cpp
|
./src/Window.cpp
|
||||||
@ -22,7 +21,7 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
|||||||
./Draw.hpp
|
./Draw.hpp
|
||||||
./GLLoad.hpp
|
./GLLoad.hpp
|
||||||
./OGLObjects.hpp
|
./OGLObjects.hpp
|
||||||
./ProgramPipeline.hpp
|
./shader.hpp
|
||||||
./Textures.hpp
|
./Textures.hpp
|
||||||
./VertexArray.hpp
|
./VertexArray.hpp
|
||||||
./Window.hpp
|
./Window.hpp
|
||||||
|
|||||||
@ -26,7 +26,7 @@ class OpenGLObject {
|
|||||||
public:
|
public:
|
||||||
OpenGLObject(const OpenGLObject&) = delete;
|
OpenGLObject(const OpenGLObject&) = delete;
|
||||||
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
||||||
virtual ~OpenGLObject() {};
|
~OpenGLObject() { remove(); }
|
||||||
|
|
||||||
virtual void bind() const = 0;
|
virtual void bind() const = 0;
|
||||||
virtual void unbind() const = 0;
|
virtual void unbind() const = 0;
|
||||||
@ -36,9 +36,9 @@ class OpenGLObject {
|
|||||||
OpenGLObject() = default;
|
OpenGLObject() = default;
|
||||||
|
|
||||||
virtual GLuint declare() = 0;
|
virtual GLuint declare() = 0;
|
||||||
virtual void remove() = 0;
|
virtual void remove() { this->remove(); };
|
||||||
|
|
||||||
GLuint _id = 0;
|
GLuint _id;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|||||||
@ -1,123 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_SHADER
|
|
||||||
#define _NB_SHADER
|
|
||||||
|
|
||||||
#include "GLLoad.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
#include <NBCore/Utils.hpp>
|
|
||||||
|
|
||||||
#include "OGLObjects.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
class ShaderError : public Error<ShaderError> {
|
|
||||||
using Base = Error<ShaderError>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ProgramError : public Error<ProgramError> {
|
|
||||||
using Base = Error<ProgramError>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED, LINKING_ERROR
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum OpenGLProfiles {
|
|
||||||
Core,
|
|
||||||
Compatibility,
|
|
||||||
ES
|
|
||||||
};
|
|
||||||
|
|
||||||
class Shader : public OpenGLObject {
|
|
||||||
using Base = OpenGLObject;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
using Base::id;
|
|
||||||
Shader(GLenum, const std::string&);
|
|
||||||
Shader(Shader&&);
|
|
||||||
Shader& operator=(Shader&&) = delete;
|
|
||||||
~Shader() { remove(); }
|
|
||||||
operator bool();
|
|
||||||
virtual void bind() const override { /* TODO: Some warning of some kind perhaps*/ }
|
|
||||||
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
|
|
||||||
GLint status(GLenum) const;
|
|
||||||
std::string log() const;
|
|
||||||
const GLenum target;
|
|
||||||
friend Base;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_id;
|
|
||||||
GLint _success;
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
_id = _id = glCreateShader(target);
|
|
||||||
}
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void remove() override {
|
|
||||||
if (_id) {
|
|
||||||
glDeleteShader(_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
class Program : public OpenGLObject {
|
|
||||||
using Base = OpenGLObject;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
using Base::id;
|
|
||||||
Program(SharedVector<Shader>);
|
|
||||||
Program(Program&&);
|
|
||||||
Program& operator=(Program&&) = delete;
|
|
||||||
operator bool();
|
|
||||||
~Program() { remove(); }
|
|
||||||
virtual void bind() const override {
|
|
||||||
glUseProgram(_id);
|
|
||||||
}
|
|
||||||
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
|
|
||||||
GLint status(GLenum) const;
|
|
||||||
std::string log() const;
|
|
||||||
friend Base;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_id;
|
|
||||||
GLint _success;
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
_id = glCreateProgram();
|
|
||||||
}
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
virtual void remove() override {
|
|
||||||
if (_id) {
|
|
||||||
glDeleteProgram(_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@ -12,27 +12,6 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
static uint8_t GLSLTypeSize(GLenum type) {
|
|
||||||
switch(type) {
|
|
||||||
case GL_SHORT:
|
|
||||||
case GL_UNSIGNED_SHORT:
|
|
||||||
case GL_HALF_FLOAT:
|
|
||||||
return 2;
|
|
||||||
break;
|
|
||||||
case GL_INT:
|
|
||||||
case GL_UNSIGNED_INT:
|
|
||||||
case GL_FLOAT:
|
|
||||||
return 4;
|
|
||||||
break;
|
|
||||||
case GL_DOUBLE:
|
|
||||||
return 8;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct VertexAttributeLayout {
|
struct VertexAttributeLayout {
|
||||||
int32_t offset = 0;
|
int32_t offset = 0;
|
||||||
GLsizei stride = 0;
|
GLsizei stride = 0;
|
||||||
@ -74,6 +53,42 @@ class VAOError : public Error<VAOError> {
|
|||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <bool Immutable=false>
|
||||||
|
class VertexBuffer : public ArrayBuffer<false>{
|
||||||
|
using Base = ArrayBuffer<false>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template<typename... Args>
|
||||||
|
VertexBuffer(const VertexAttributeList& vas={}, Args... args)
|
||||||
|
: attributes(vas) {
|
||||||
|
if (sizeof...(Args)) {
|
||||||
|
data(args...);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VertexBuffer(VertexBuffer&& rhs)
|
||||||
|
: attributes(rhs.attributes), Base(std::move(rhs)){}
|
||||||
|
|
||||||
|
|
||||||
|
using Base::Target;
|
||||||
|
const VertexAttributeList attributes;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::_usage;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class VertexBuffer<true>
|
||||||
|
: public virtual VertexBuffer<false>, public virtual ImmutableBuffer<ArrayBuffer<true>> {
|
||||||
|
using Base = ArrayBuffer<false>;
|
||||||
|
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
||||||
|
using BufferType::BufferType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Target;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
class VAO : public OpenGLObject {
|
class VAO : public OpenGLObject {
|
||||||
using Base = OpenGLObject;
|
using Base = OpenGLObject;
|
||||||
using Codes = VAOError::Codes;
|
using Codes = VAOError::Codes;
|
||||||
@ -124,40 +139,25 @@ class VAO : public OpenGLObject {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using VBO = ArrayBuffer<false>;
|
|
||||||
using VertBufVec = SharedVector<VBO>;
|
|
||||||
using EBO = ElementBuffer<false>;
|
|
||||||
|
|
||||||
struct VertexData {
|
|
||||||
std::shared_ptr<VBO> vbo;
|
|
||||||
VertexAttributeList attrs;
|
|
||||||
};
|
|
||||||
|
|
||||||
using VertexDataVec = std::vector<VertexData>;
|
|
||||||
|
|
||||||
class VertexGroup {
|
class VertexGroup {
|
||||||
public:
|
public:
|
||||||
VertexGroup(VertexGroup&&);
|
VertexGroup(VertexGroup&&);
|
||||||
VertexGroup& operator=(VertexGroup&&);
|
VertexGroup& operator=(VertexGroup&&);
|
||||||
|
|
||||||
VertexGroup(const VertexData&);
|
|
||||||
VertexGroup(const VertexDataVec&);
|
|
||||||
VertexGroup(const ByteVector&, const VertexAttributeList&);
|
|
||||||
|
|
||||||
void bind() const {
|
void bind() const {
|
||||||
_ebo->bind();
|
_elmt_buf.bind();
|
||||||
_vao->bind();
|
_vao.bind();
|
||||||
}
|
}
|
||||||
void unbind() const {
|
void unbind() const {
|
||||||
_vao->unbind();
|
_vao.bind();
|
||||||
_ebo->unbind();
|
_elmt_buf.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexDataVec getBuffers();
|
VertexBuffer<false>& buffer(size_t);
|
||||||
VertexData getBuffers(size_t);
|
size_t buffer(VertexBuffer<false>&&);
|
||||||
size_t setBuffers(const VertexDataVec&);
|
std::vector<std::shared_ptr<VertexBuffer<false>>> buffers();
|
||||||
size_t addBuffer(const VertexData&);
|
size_t buffers(std::vector<VertexBuffer<false>>&&);
|
||||||
VertexData dropBuffer(size_t);
|
VertexBuffer<false>&& dropBuffer(size_t);
|
||||||
|
|
||||||
VertexAttributePointer attribute(size_t) const;
|
VertexAttributePointer attribute(size_t) const;
|
||||||
void enableAttr(GLuint);
|
void enableAttr(GLuint);
|
||||||
@ -168,9 +168,9 @@ class VertexGroup {
|
|||||||
void disable();
|
void disable();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
VertexDataVec _vertex_data;
|
std::vector<std::shared_ptr<VertexBuffer<false>>> _vertex_data;
|
||||||
std::shared_ptr<EBO> _ebo;
|
ElementBuffer<false> _elmt_buf;
|
||||||
std::shared_ptr<VAO> _vao;
|
VAO _vao;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|||||||
@ -14,6 +14,11 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
class GLError : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
GLError(const std::string&);
|
||||||
|
};
|
||||||
|
|
||||||
class OpenGLError : public Error<OpenGLError> {
|
class OpenGLError : public Error<OpenGLError> {
|
||||||
using Base = Error<OpenGLError>;
|
using Base = Error<OpenGLError>;
|
||||||
|
|
||||||
|
|||||||
193
engine/NBGraphics/shader.hpp
Normal file
193
engine/NBGraphics/shader.hpp
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_SHADER
|
||||||
|
#define _NB_SHADER
|
||||||
|
|
||||||
|
#include <GLLoad.h>
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstring>
|
||||||
|
#include <exception>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <regex>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
// #define _PREPROC_FUNC_PARAMS_ (const ShaderPreprocessor&, ShaderUnit&, unsigned int, const std::string&, std::vector<uint8_t>&)
|
||||||
|
|
||||||
|
namespace NB {
|
||||||
|
|
||||||
|
class ShaderPreprocessorError : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
enum Codes : unsigned char {
|
||||||
|
NONE,
|
||||||
|
FILE_NOT_FOUND,
|
||||||
|
BUILTIN_NOT_FOUND,
|
||||||
|
CUSTOM,
|
||||||
|
UNDEFINED
|
||||||
|
};
|
||||||
|
|
||||||
|
const Codes code;
|
||||||
|
|
||||||
|
ShaderPreprocessorError(const std::string&, const std::string& file="", int line=-1);
|
||||||
|
ShaderPreprocessorError(Codes, const std::string& arg="", const std::string& file="", int line=-1);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static std::string errorCodeParser(Codes, const std::string& arg="");
|
||||||
|
};
|
||||||
|
|
||||||
|
class ShaderError : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
ShaderError(const std::string&, const char* shad=nullptr, const std::string& file="", int line=-1);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string formatString(const std::string&, const char* shad=nullptr, const std::string& file="", int line=-1);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct File {
|
||||||
|
// typedef std::tuple<std::string, std::string, std::string> FilePath;
|
||||||
|
struct FilePath {
|
||||||
|
std::string dir;
|
||||||
|
std::string basename;
|
||||||
|
std::string ext;
|
||||||
|
};
|
||||||
|
FilePath path;
|
||||||
|
std::stringstream src;
|
||||||
|
std::map<unsigned int, std::shared_ptr<File>> include_map;
|
||||||
|
|
||||||
|
File() {}
|
||||||
|
File(const File& rhs) { *this = rhs; }
|
||||||
|
|
||||||
|
File& operator=(const File& rhs) {
|
||||||
|
path = rhs.path;
|
||||||
|
src.str("");
|
||||||
|
src << rhs.src.rdbuf();
|
||||||
|
include_map = rhs.include_map;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// File::FilePath get_file_path(std::string);
|
||||||
|
|
||||||
|
class ShaderPreprocessor;
|
||||||
|
|
||||||
|
enum OpenGLProfiles {
|
||||||
|
Core,
|
||||||
|
Compatibility,
|
||||||
|
ES
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ShaderUnit {
|
||||||
|
GLenum type = 0x0;
|
||||||
|
File file;
|
||||||
|
std::string preprocSource;
|
||||||
|
std::map<std::string, std::string> defines;
|
||||||
|
short vMajor=0, vMinor=0;
|
||||||
|
OpenGLProfiles profile = Core;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ShaderPreprocessor;
|
||||||
|
|
||||||
|
class ShaderProgram {
|
||||||
|
friend ShaderPreprocessor;
|
||||||
|
public:
|
||||||
|
ShaderProgram() {}
|
||||||
|
ShaderProgram(const ShaderProgram& rhs) = delete;
|
||||||
|
ShaderProgram(ShaderProgram&& rhs);
|
||||||
|
|
||||||
|
~ShaderProgram();
|
||||||
|
|
||||||
|
ShaderProgram& operator=(const ShaderProgram& rhs) = delete;
|
||||||
|
ShaderProgram& operator=(ShaderProgram&& rhs);
|
||||||
|
|
||||||
|
std::vector<ShaderUnit> getShaders() const;
|
||||||
|
ShaderUnit getShaders(unsigned int) const;
|
||||||
|
void use() const;
|
||||||
|
unsigned int id() const;
|
||||||
|
|
||||||
|
void setBool(const std::string& name, bool value) const;
|
||||||
|
void setInt(const std::string& name, int value) const;
|
||||||
|
void setUnsigned(const std::string& name, int value) const;
|
||||||
|
void setFloat(const std::string& name, float value) const;
|
||||||
|
void setMat4(const std::string& name, glm::mat4& value) const;
|
||||||
|
|
||||||
|
static ShaderProgram CreateShaderProgram(std::vector<ShaderUnit>&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
ShaderProgram(std::vector<ShaderUnit>& shaders) : _shader_units(shaders){}
|
||||||
|
|
||||||
|
std::vector<ShaderUnit> _shader_units;
|
||||||
|
unsigned int _id;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ShaderPreprocessor {
|
||||||
|
public:
|
||||||
|
typedef ShaderPreprocessorError::Codes Codes;
|
||||||
|
|
||||||
|
enum TokenType {
|
||||||
|
TK,
|
||||||
|
DR,
|
||||||
|
WS,
|
||||||
|
NL,
|
||||||
|
LC,
|
||||||
|
BC
|
||||||
|
};
|
||||||
|
static std::string TokenName(const TokenType&);
|
||||||
|
|
||||||
|
std::map<std::string, GLenum> AcceptedExtensions = {
|
||||||
|
{".frag", GL_FRAGMENT_SHADER},
|
||||||
|
{".fs", GL_FRAGMENT_SHADER},
|
||||||
|
{".vert", GL_VERTEX_SHADER},
|
||||||
|
{".vs", GL_VERTEX_SHADER},
|
||||||
|
{".tess", 0x0},
|
||||||
|
{".geom", GL_GEOMETRY_SHADER},
|
||||||
|
{".comp", 0x0},
|
||||||
|
{".shad", 0x0},
|
||||||
|
{".glsl", 0x0}
|
||||||
|
};
|
||||||
|
std::map<std::string, OpenGLProfiles> AcceptedProfiles {
|
||||||
|
{"core", Core},
|
||||||
|
{"compatibility", Compatibility},
|
||||||
|
{"es", ES}
|
||||||
|
};
|
||||||
|
std::vector<std::string> Directories;
|
||||||
|
std::map<std::string, std::string> BuiltIns;
|
||||||
|
|
||||||
|
ShaderPreprocessor();
|
||||||
|
|
||||||
|
|
||||||
|
File load(const std::string, bool builtin_first=false) const;
|
||||||
|
ShaderUnit& preprocess(const std::string&, ShaderUnit&) const;
|
||||||
|
ShaderUnit preprocess(File, GLenum shader_type=0x0) const;
|
||||||
|
ShaderUnit preprocess(const std::string&, GLenum shader_type=0x0) const;
|
||||||
|
ShaderProgram ReloadFromFile(const ShaderProgram& rhs) const;
|
||||||
|
ShaderProgram CreateShaderProgram(std::vector<std::string>) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef std::pair<TokenType, std::string> Token;
|
||||||
|
typedef std::vector<std::string> StringVec;
|
||||||
|
|
||||||
|
inline bool directive_dispatch(ShaderUnit&, const std::string&) const;
|
||||||
|
inline std::vector<Token> tokenize(const std::string&) const;
|
||||||
|
|
||||||
|
inline bool preprocessor_include(ShaderUnit&, const StringVec&, const std::string&) const;
|
||||||
|
inline bool preprocessor_version(ShaderUnit&, const StringVec&, const std::string&) const;
|
||||||
|
inline bool preprocessor_define(ShaderUnit&, const StringVec&, const std::string&) const;
|
||||||
|
// inline bool preprocessor_uniform(ShaderUnit&, const std::string&, const std::string&) const;
|
||||||
|
|
||||||
|
File loadFromBase(const std::string, const std::string base="") const;
|
||||||
|
File loadFromDirectories(const std::string) const;
|
||||||
|
File loadFromBuiltIn(const std::string) const;
|
||||||
|
File load_BuiltInFirst(const std::string) const;
|
||||||
|
File load_FilesFirst(const std::string) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -2,11 +2,6 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
const std::unordered_map<GLenum, std::string> BufferTypes({
|
|
||||||
{GL_ARRAY_BUFFER, "GL_ARRAY_BUFFER"},
|
|
||||||
{GL_ELEMENT_ARRAY_BUFFER, "GL_ELEMENT_BUFFER"}
|
|
||||||
});
|
|
||||||
|
|
||||||
using BufferErrorCodes = BufferError::Codes;
|
using BufferErrorCodes = BufferError::Codes;
|
||||||
const std::string BufferError::type = "nb::BufferError";
|
const std::string BufferError::type = "nb::BufferError";
|
||||||
const ErrorCodeMap BufferError::ErrorMessages = {
|
const ErrorCodeMap BufferError::ErrorMessages = {
|
||||||
|
|||||||
@ -1,75 +0,0 @@
|
|||||||
#include "ProgramPipeline.hpp"
|
|
||||||
|
|
||||||
namespace nb{
|
|
||||||
|
|
||||||
using ShaderErrCodes = ShaderError::Codes;
|
|
||||||
const std::string ShaderError::type = "nb::ShaderError";
|
|
||||||
const ErrorCodeMap ShaderError::ErrorMessages = {
|
|
||||||
{ShaderErrCodes::UNDEFINED, "Error"}
|
|
||||||
};
|
|
||||||
|
|
||||||
using ProgramErrCodes = ProgramError::Codes;
|
|
||||||
const std::string ProgramError::type = "nb::ProgramError";
|
|
||||||
const ErrorCodeMap ProgramError::ErrorMessages = {
|
|
||||||
{ProgramErrCodes::UNDEFINED, "Error"},
|
|
||||||
{ProgramErrCodes::LINKING_ERROR, "Linker error"}
|
|
||||||
};
|
|
||||||
|
|
||||||
Shader::Shader(GLenum target_, const std::string& source_) : target(target_) {
|
|
||||||
declare();
|
|
||||||
_success = status(GL_COMPILE_STATUS);
|
|
||||||
}
|
|
||||||
|
|
||||||
Shader::Shader(Shader&& cpy) : target(cpy.target) {
|
|
||||||
_id = cpy._id;
|
|
||||||
}
|
|
||||||
|
|
||||||
Shader::operator bool() { return _success; }
|
|
||||||
|
|
||||||
GLint Shader::status(GLenum parameter) const {
|
|
||||||
GLint param = 0;
|
|
||||||
glGetShaderiv(_id, parameter, ¶m);
|
|
||||||
return param;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Shader::log() const {
|
|
||||||
GLint logsize = status(GL_INFO_LOG_LENGTH);
|
|
||||||
char* log_ = new char[logsize];
|
|
||||||
glGetShaderInfoLog(_id, logsize, NULL, log_);
|
|
||||||
std::string ret(log_, logsize);
|
|
||||||
delete[] log_;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
Program::Program(SharedVector<Shader> shaders_) {
|
|
||||||
declare();
|
|
||||||
for (auto shad_ptr : shaders_) {
|
|
||||||
glAttachShader(_id, shad_ptr->id());
|
|
||||||
}
|
|
||||||
glLinkProgram(_id);
|
|
||||||
_success = status(GL_LINK_STATUS);
|
|
||||||
if (!_success) {
|
|
||||||
WARN(log(), 0x01);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Program::operator bool() { return _success; }
|
|
||||||
|
|
||||||
GLint Program::status(GLenum parameter) const {
|
|
||||||
GLint param = 0;
|
|
||||||
glGetProgramiv(_id, parameter, ¶m);
|
|
||||||
return param;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Program::log() const {
|
|
||||||
GLint logsize = status(GL_INFO_LOG_LENGTH);
|
|
||||||
char* log_ = new char[logsize];
|
|
||||||
glGetProgramInfoLog(_id, logsize, NULL, log_);
|
|
||||||
std::string ret(log_, logsize);
|
|
||||||
delete[] log_;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -60,7 +60,7 @@ VertexAttributePointer VAO::attr(GLuint idx) const {
|
|||||||
return attr_ptr;
|
return attr_ptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
THROW(VAOError(Codes::INVALID_ATTRIBUTE));
|
THROW(BufferError(Codes::INVALID_ATTRIBUTE));
|
||||||
}
|
}
|
||||||
|
|
||||||
void VAO::enable() const {
|
void VAO::enable() const {
|
||||||
@ -105,127 +105,121 @@ VertexGroup::VertexGroup(VertexGroup&& other) {
|
|||||||
|
|
||||||
VertexGroup& VertexGroup::operator=(VertexGroup&& rhs) {
|
VertexGroup& VertexGroup::operator=(VertexGroup&& rhs) {
|
||||||
_vertex_data = rhs._vertex_data;
|
_vertex_data = rhs._vertex_data;
|
||||||
_ebo = rhs._ebo;
|
_elmt_buf = std::move(rhs._elmt_buf);
|
||||||
_vao = rhs._vao;
|
_vao = std::move(rhs._vao);
|
||||||
|
|
||||||
rhs._vertex_data = {};
|
rhs._vertex_data = {};
|
||||||
rhs._ebo = nullptr;
|
rhs._elmt_buf = ElementBuffer<false>();
|
||||||
rhs._vao = nullptr;
|
rhs._vao = VAO();
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexGroup::VertexGroup(const VertexDataVec& vertex_data_)
|
size_t VertexGroup::buffer(VertexBuffer<false>&& buf_) {
|
||||||
: _vao(std::make_shared<VAO>()), _ebo(std::make_shared<EBO>()) {
|
_vertex_data.emplace_back(
|
||||||
setBuffers(vertex_data_);
|
std::make_shared<VertexBuffer<false>>(std::move(buf_))
|
||||||
}
|
);
|
||||||
|
VertexBuffer<false>* buf_ptr = _vertex_data.back().get();
|
||||||
VertexGroup::VertexGroup(const VertexData& vertex_data_)
|
VertexAttributePointerList attrs = _vao.attributes();
|
||||||
: VertexGroup(VertexDataVec({vertex_data_})) {}
|
|
||||||
|
|
||||||
VertexGroup::VertexGroup(
|
|
||||||
const ByteVector& data_,
|
|
||||||
const VertexAttributeList& attrs_
|
|
||||||
) : VertexGroup({std::make_shared<VBO>(data_), attrs_}) {}
|
|
||||||
|
|
||||||
size_t VertexGroup::addBuffer(const VertexData& vertex_data_) {
|
|
||||||
VertexData data = vertex_data_;
|
|
||||||
_vertex_data.emplace_back(data);
|
|
||||||
VertexAttributePointerList attrs = _vao->attributes();
|
|
||||||
GLuint idx = attrs.size();
|
GLuint idx = attrs.size();
|
||||||
for (auto attr : data.attrs) {
|
for (auto attr : buf_ptr->attributes) {
|
||||||
attrs.emplace_back(
|
attrs.emplace_back(
|
||||||
VertexAttributePointer{attr, data.vbo->id(),idx}
|
VertexAttributePointer{attr, buf_ptr->id(),idx}
|
||||||
);
|
);
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
_vao->attributes(attrs);
|
_vao.attributes(attrs);
|
||||||
return _vertex_data.size();
|
return _vertex_data.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexData VertexGroup::getBuffers(size_t idx) {
|
VertexBuffer<false>& VertexGroup::buffer(size_t idx) {
|
||||||
return _vertex_data[idx];
|
return *_vertex_data[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexDataVec VertexGroup::getBuffers() {
|
std::vector<std::shared_ptr<VertexBuffer<false>>> VertexGroup::buffers() {
|
||||||
return _vertex_data;
|
return _vertex_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t VertexGroup::setBuffers(const VertexDataVec& vertex_data_) {
|
size_t VertexGroup::buffers(std::vector<VertexBuffer<false>>&& bufs_) {
|
||||||
_vertex_data = vertex_data_;
|
_vertex_data.clear();
|
||||||
|
for (auto& buf : bufs_) {
|
||||||
|
_vertex_data.emplace_back(
|
||||||
|
std::make_shared<VertexBuffer<false>>(std::move(buf))
|
||||||
|
);
|
||||||
|
}
|
||||||
VertexAttributePointerList attr_ptrs;
|
VertexAttributePointerList attr_ptrs;
|
||||||
GLuint idx = 0;
|
GLuint idx = 0;
|
||||||
for (auto data : _vertex_data) {
|
for (auto buf : _vertex_data) {
|
||||||
for (auto attr : data.attrs) {
|
for (auto attr : buf->attributes) {
|
||||||
attr_ptrs.emplace_back(
|
attr_ptrs.emplace_back(
|
||||||
VertexAttributePointer{attr, data.vbo->id(), idx}
|
VertexAttributePointer{attr, buf->id(), idx}
|
||||||
);
|
);
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_vao->attributes(attr_ptrs);
|
_vao.attributes(attr_ptrs);
|
||||||
return _vertex_data.size();
|
return _vertex_data.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexData VertexGroup::dropBuffer(size_t idx) {
|
VertexBuffer<false>&& VertexGroup::dropBuffer(size_t idx) {
|
||||||
if (idx >= _vertex_data.size()) {
|
if (idx >= _vertex_data.size()) {
|
||||||
THROW(BufferError(BufferError::Codes::INVALID_BUFFER));
|
THROW(BufferError(BufferError::Codes::INVALID_BUFFER));
|
||||||
}
|
}
|
||||||
VertexDataVec tmp;
|
std::vector<VertexBuffer<false>> tmp;
|
||||||
VertexData ret;
|
std::shared_ptr<VertexBuffer<false>> ret_ptr;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (auto buf : _vertex_data) {
|
for (auto buf : _vertex_data) {
|
||||||
if (i == idx) {
|
if (i == idx) {
|
||||||
ret = tmp[idx];
|
ret_ptr = std::make_shared<VertexBuffer<false>>(std::move(tmp[idx]));
|
||||||
} else {
|
} else {
|
||||||
tmp.emplace_back(buf);
|
tmp.emplace_back(std::move(*buf));
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
setBuffers(tmp);
|
buffers(std::move(tmp));
|
||||||
return ret;
|
return std::move(*ret_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexAttributePointer VertexGroup::attribute(size_t idx_) const {
|
VertexAttributePointer VertexGroup::attribute(size_t idx_) const {
|
||||||
return _vao->attr(idx_);
|
return _vao.attr(idx_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexGroup::enableAttr(GLuint idx) {
|
void VertexGroup::enableAttr(GLuint idx) {
|
||||||
bind();
|
bind();
|
||||||
_vao->enable(idx);
|
_vao.enable(idx);
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexGroup::disableAttr(GLuint idx) {
|
void VertexGroup::disableAttr(GLuint idx) {
|
||||||
bind();
|
bind();
|
||||||
_vao->disable(idx);
|
_vao.disable(idx);
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexGroup::disableBuffer(size_t idx) {
|
void VertexGroup::disableBuffer(size_t idx) {
|
||||||
auto buf_idx = _vertex_data[idx].vbo->id();
|
auto buf_idx = _vertex_data[idx]->id();
|
||||||
bind();
|
bind();
|
||||||
for (auto attr : _vao->attributes()) {
|
for (auto attr : _vao.attributes()) {
|
||||||
if (attr.buffer == buf_idx) {
|
if (attr.buffer == buf_idx) {
|
||||||
_vao->disable(attr.index);
|
_vao.disable(attr.index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexGroup::enableBuffer(size_t idx) {
|
void VertexGroup::enableBuffer(size_t idx) {
|
||||||
auto buf_idx = _vertex_data[idx].vbo->id();
|
auto buf_idx = _vertex_data[idx]->id();
|
||||||
bind();
|
bind();
|
||||||
for (auto attr : _vao->attributes()) {
|
for (auto attr : _vao.attributes()) {
|
||||||
if (attr.buffer == buf_idx) {
|
if (attr.buffer == buf_idx) {
|
||||||
_vao->enable(attr.index);
|
_vao.enable(attr.index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unbind();
|
unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexGroup::enable() { _vao->enable(); }
|
void VertexGroup::enable() { _vao.enable(); }
|
||||||
|
|
||||||
void VertexGroup::disable() { _vao->disable(); }
|
void VertexGroup::disable() { _vao.disable(); }
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|||||||
@ -34,6 +34,8 @@ const ErrorCodeMap WindowError::ErrorMessages = {
|
|||||||
{WindowErrorCodes::INIT_FAILED, "Could not intialized window"}
|
{WindowErrorCodes::INIT_FAILED, "Could not intialized window"}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GLError::GLError(const std::string& msg) : std::runtime_error(msg) {}
|
||||||
|
|
||||||
int Window::getGLFWHint(int hint_key) {
|
int Window::getGLFWHint(int hint_key) {
|
||||||
if (GLFWHints.find(hint_key) == GLFWHints.end()) {
|
if (GLFWHints.find(hint_key) == GLFWHints.end()) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
742
engine/NBGraphics/src/shader.cpp
Normal file
742
engine/NBGraphics/src/shader.cpp
Normal file
@ -0,0 +1,742 @@
|
|||||||
|
#include "Shader.h"
|
||||||
|
|
||||||
|
// #define _PREPROC_FUNC_PARAM_NAMES_ (const ShaderPreprocessor& proc, ShaderUnit& shad, unsigned int linenum, const std::string& line, std::vector<uint8_t>& params)
|
||||||
|
|
||||||
|
namespace NB{
|
||||||
|
|
||||||
|
File::FilePath get_file_path(std::string name) {
|
||||||
|
const std::vector<char> allowed_special_chars = {
|
||||||
|
'.', '!', '#', '$', '%', '&', '\'', '(', ')', '+', ',', '-', ';', '=', '@', '[', ']', '^', '_', '`', '~'
|
||||||
|
};
|
||||||
|
std::vector<std::string> path = {""};
|
||||||
|
for (const char c : name) {
|
||||||
|
if (c == '/' || c == '\\') {
|
||||||
|
path.push_back("");
|
||||||
|
} else if (std::isalnum(c)) {
|
||||||
|
path.back() += c;
|
||||||
|
} else {
|
||||||
|
bool spec_char_found = false;
|
||||||
|
for (const char sc : allowed_special_chars) {
|
||||||
|
if (c == sc) {
|
||||||
|
path.back() += c;
|
||||||
|
spec_char_found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!spec_char_found) {
|
||||||
|
throw std::runtime_error("'" + name + "' is not valid filepath.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
File::FilePath ret;
|
||||||
|
for (const auto& tk : path) {
|
||||||
|
if (tk == path.back()) {
|
||||||
|
size_t period = tk.find(".");
|
||||||
|
if (period == std::string::npos) {
|
||||||
|
ret.basename = tk;
|
||||||
|
ret.ext = "";
|
||||||
|
} else {
|
||||||
|
ret.basename = tk.substr(0, period);
|
||||||
|
ret.ext = tk.substr(period);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret.dir += tk + "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShaderPreprocessorError class
|
||||||
|
ShaderPreprocessorError::ShaderPreprocessorError(
|
||||||
|
const std::string& msg,
|
||||||
|
const std::string& file,
|
||||||
|
int line
|
||||||
|
) : code(Codes::UNDEFINED), std::runtime_error(formatDebugString(msg, file, line)) {}
|
||||||
|
|
||||||
|
ShaderPreprocessorError::ShaderPreprocessorError(
|
||||||
|
Codes err_code,
|
||||||
|
const std::string& arg,
|
||||||
|
const std::string& file,
|
||||||
|
int line
|
||||||
|
) : code(err_code), std::runtime_error(formatDebugString(errorCodeParser(err_code, arg), file, line)) {}
|
||||||
|
|
||||||
|
std::string ShaderPreprocessorError::errorCodeParser(Codes err_code, const std::string& arg) {
|
||||||
|
switch(err_code) {
|
||||||
|
case Codes::FILE_NOT_FOUND:
|
||||||
|
return "File '" + arg + "' not found.";
|
||||||
|
case Codes::BUILTIN_NOT_FOUND:
|
||||||
|
return "Built-in '" + arg + "' not found.";
|
||||||
|
case Codes::CUSTOM:
|
||||||
|
case Codes::UNDEFINED:
|
||||||
|
return arg;
|
||||||
|
case Codes::NONE:
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShaderError class
|
||||||
|
ShaderError::ShaderError(
|
||||||
|
const std::string& msg,
|
||||||
|
const char* shad,
|
||||||
|
const std::string& file,
|
||||||
|
int line
|
||||||
|
) : std::runtime_error(formatString(msg, shad, file, line)) {}
|
||||||
|
|
||||||
|
std::string ShaderError::formatString(
|
||||||
|
const std::string& msg,
|
||||||
|
const char* shad,
|
||||||
|
const std::string& file,
|
||||||
|
int line
|
||||||
|
) {
|
||||||
|
std::stringstream ret;
|
||||||
|
if (file != "") {
|
||||||
|
ret << "In file " << file;
|
||||||
|
if (line >= 0) {
|
||||||
|
ret << " at line " << line;
|
||||||
|
}
|
||||||
|
ret << ":\n\t";
|
||||||
|
}
|
||||||
|
ret << msg;
|
||||||
|
if (shad != nullptr) {
|
||||||
|
ret << " with shader error: " << shad;
|
||||||
|
}
|
||||||
|
return ret.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShaderPreprocessor class
|
||||||
|
std::string ShaderPreprocessor::TokenName(const ShaderPreprocessor::TokenType& x) {
|
||||||
|
switch(x) {
|
||||||
|
case TK:
|
||||||
|
return "Token";
|
||||||
|
break;
|
||||||
|
case DR:
|
||||||
|
return "Directive";
|
||||||
|
break;
|
||||||
|
case WS:
|
||||||
|
return "Whitespace";
|
||||||
|
break;
|
||||||
|
case NL:
|
||||||
|
return "NewLine";
|
||||||
|
break;
|
||||||
|
case LC:
|
||||||
|
return "LineComment";
|
||||||
|
break;
|
||||||
|
case BC:
|
||||||
|
return "BlockComment";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return "Unrecognized";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderPreprocessor::ShaderPreprocessor() {}
|
||||||
|
|
||||||
|
File ShaderPreprocessor::loadFromBase(const std::string path, const std::string base) const {
|
||||||
|
File ret;
|
||||||
|
std::ifstream filestream;
|
||||||
|
filestream.open(base + path);
|
||||||
|
if (filestream.is_open()) {
|
||||||
|
ret.path = get_file_path(base + path);
|
||||||
|
ret.src << filestream.rdbuf();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
filestream.close();
|
||||||
|
std::string ext;
|
||||||
|
for (const auto& kv : AcceptedExtensions) {
|
||||||
|
ext = kv.first;
|
||||||
|
filestream.open(base + path + ext);
|
||||||
|
if (filestream.is_open()) {
|
||||||
|
ret.path = get_file_path(base + path + ext);
|
||||||
|
ret.src << filestream.rdbuf();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
filestream.close();
|
||||||
|
}
|
||||||
|
throw ShaderPreprocessorError(Codes::FILE_NOT_FOUND, base + path);
|
||||||
|
}
|
||||||
|
|
||||||
|
File ShaderPreprocessor::loadFromDirectories(const std::string name) const {
|
||||||
|
File ret;
|
||||||
|
std::ifstream fstream;
|
||||||
|
for (const std::string& path : Directories) {
|
||||||
|
fstream.open(path + name);
|
||||||
|
if (fstream.is_open()) {
|
||||||
|
ret.path = get_file_path(path + name);
|
||||||
|
ret.src << fstream.rdbuf();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
fstream.close();
|
||||||
|
}
|
||||||
|
throw ShaderPreprocessorError(Codes::FILE_NOT_FOUND, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
File ShaderPreprocessor::load(const std::string path, bool builtin_first) const {
|
||||||
|
if(builtin_first) {
|
||||||
|
return load_BuiltInFirst(path);
|
||||||
|
}
|
||||||
|
return load_FilesFirst(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
File ShaderPreprocessor::loadFromBuiltIn(const std::string name) const {
|
||||||
|
File ret;
|
||||||
|
//std::stringstream ret;
|
||||||
|
decltype(BuiltIns)::const_iterator builtin_it = BuiltIns.find(name);
|
||||||
|
if (builtin_it != BuiltIns.end()) {
|
||||||
|
ret.path = File::FilePath{"builtin:", name, ""};
|
||||||
|
ret.src << builtin_it->second;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
throw ShaderPreprocessorError(Codes::BUILTIN_NOT_FOUND, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
File ShaderPreprocessor::load_FilesFirst(const std::string path) const {
|
||||||
|
try {
|
||||||
|
return loadFromBase(path);
|
||||||
|
} catch (ShaderPreprocessorError e) {
|
||||||
|
if (e.code == Codes::FILE_NOT_FOUND) {
|
||||||
|
try {
|
||||||
|
return loadFromDirectories(path);
|
||||||
|
} catch (ShaderPreprocessorError f) {
|
||||||
|
if (f.code == Codes::FILE_NOT_FOUND) {
|
||||||
|
try {
|
||||||
|
return loadFromBuiltIn(path);
|
||||||
|
} catch(ShaderPreprocessorError g) {
|
||||||
|
if (g.code == Codes::BUILTIN_NOT_FOUND) {
|
||||||
|
throw ShaderPreprocessorError(Codes::FILE_NOT_FOUND, path);
|
||||||
|
} else { throw g; }
|
||||||
|
}
|
||||||
|
} else { throw f; }
|
||||||
|
}
|
||||||
|
} else { throw e; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
File ShaderPreprocessor::load_BuiltInFirst(const std::string path) const {
|
||||||
|
try {
|
||||||
|
return loadFromBuiltIn(path);
|
||||||
|
} catch (ShaderPreprocessorError f) {
|
||||||
|
if (f.code == Codes::BUILTIN_NOT_FOUND) {
|
||||||
|
try {
|
||||||
|
return loadFromDirectories(path);
|
||||||
|
} catch (ShaderPreprocessorError e) {
|
||||||
|
if (e.code == Codes::FILE_NOT_FOUND) {
|
||||||
|
return loadFromBase(path);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ShaderPreprocessor::Token> ShaderPreprocessor::tokenize(const std::string& code) const {
|
||||||
|
enum State {
|
||||||
|
FSlash,
|
||||||
|
WhiteSpace,
|
||||||
|
LineComment,
|
||||||
|
BlockComment,
|
||||||
|
BlockCommentEndStar,
|
||||||
|
Directive,
|
||||||
|
Token
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<ShaderPreprocessor::Token> tks;
|
||||||
|
|
||||||
|
std::string token = "";
|
||||||
|
State state = WhiteSpace;
|
||||||
|
for(char c : code) {
|
||||||
|
if (c==13) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
switch(state) {
|
||||||
|
case WhiteSpace:
|
||||||
|
if (c=='/') {
|
||||||
|
if (token != "") { tks.push_back({WS, token}); }
|
||||||
|
token = c;
|
||||||
|
state = FSlash;
|
||||||
|
} else if (c=='#') {
|
||||||
|
if (token != "") { tks.push_back({WS, token}); }
|
||||||
|
token = c;
|
||||||
|
state = Directive;
|
||||||
|
} else if (c=='\n') {
|
||||||
|
if (token != "") { tks.push_back({WS, token}); }
|
||||||
|
tks.push_back({NL, "\n"});
|
||||||
|
token = "";
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else if (std::isblank(c)) {
|
||||||
|
token += c;
|
||||||
|
} else {
|
||||||
|
if (token != "") { tks.push_back({WS, token}); }
|
||||||
|
token = c;
|
||||||
|
state = Token;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case FSlash:
|
||||||
|
if (c=='/') {
|
||||||
|
token += c;
|
||||||
|
state = LineComment;
|
||||||
|
} else if (c=='*') {
|
||||||
|
token += c;
|
||||||
|
state = BlockComment;
|
||||||
|
} else if (c=='\n') {
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
tks.push_back({NL, "\n"});
|
||||||
|
token = "";
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else if (std::isblank(c)) {
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
token = c;
|
||||||
|
}else {
|
||||||
|
token += c;
|
||||||
|
state = Token;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LineComment:
|
||||||
|
if (c=='\n') {
|
||||||
|
tks.push_back({LC, token});
|
||||||
|
tks.push_back({NL, "\n"});
|
||||||
|
token = "";
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else {
|
||||||
|
token += c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case BlockComment:
|
||||||
|
token += c;
|
||||||
|
if (c=='*') {
|
||||||
|
state = BlockCommentEndStar;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case BlockCommentEndStar:
|
||||||
|
token += c;
|
||||||
|
if (c=='/') {
|
||||||
|
tks.push_back({BC, token});
|
||||||
|
token = "";
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else {
|
||||||
|
state = BlockComment;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Directive:
|
||||||
|
if (c=='\n') {
|
||||||
|
tks.push_back({DR, token});
|
||||||
|
tks.push_back({NL, "\n"});
|
||||||
|
token = "";
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else if (c=='/') {
|
||||||
|
tks.push_back({DR, token});
|
||||||
|
token = c;
|
||||||
|
state = FSlash;
|
||||||
|
} else {
|
||||||
|
token += c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Token:
|
||||||
|
if (c=='\n') {
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
tks.push_back({NL, "\n"});
|
||||||
|
token = "";
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else if (std::isblank(c)) {
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
token = c;
|
||||||
|
state = WhiteSpace;
|
||||||
|
} else if (c=='/') {
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
token = c;
|
||||||
|
state = FSlash;
|
||||||
|
}else {
|
||||||
|
token += c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(state) {
|
||||||
|
case WhiteSpace:
|
||||||
|
if (token != "") { tks.push_back({WS, token}); }
|
||||||
|
case FSlash:
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
break;
|
||||||
|
case LineComment:
|
||||||
|
tks.push_back({LC, token});
|
||||||
|
break;
|
||||||
|
case BlockComment:
|
||||||
|
case BlockCommentEndStar:
|
||||||
|
tks.push_back({BC, token});
|
||||||
|
break;
|
||||||
|
case Directive:
|
||||||
|
tks.push_back({DR, token});
|
||||||
|
break;
|
||||||
|
case Token:
|
||||||
|
tks.push_back({TK, token});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tks;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderUnit& ShaderPreprocessor::preprocess(const std::string& code, ShaderUnit& shad) const {
|
||||||
|
typedef ShaderPreprocessor::Token Token;
|
||||||
|
std::vector<Token> tks = tokenize(code);
|
||||||
|
|
||||||
|
for (int i{0}; i < tks.size(); ++i) {
|
||||||
|
switch(tks[i].first) {
|
||||||
|
case DR:
|
||||||
|
directive_dispatch(shad, tks[i].second);
|
||||||
|
break;
|
||||||
|
case TK:
|
||||||
|
case NL:
|
||||||
|
case LC:
|
||||||
|
case BC:
|
||||||
|
case WS:
|
||||||
|
default:
|
||||||
|
shad.preprocSource += tks[i].second;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shad.vMajor == 0 && shad.vMinor == 0) {
|
||||||
|
shad.vMajor = 1;
|
||||||
|
shad.vMinor = 10;
|
||||||
|
}
|
||||||
|
return shad;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderUnit ShaderPreprocessor::preprocess(File file, GLenum shader_type) const {
|
||||||
|
ShaderUnit ret;
|
||||||
|
ret.file = file;
|
||||||
|
if (shader_type) {
|
||||||
|
ret.type = shader_type;
|
||||||
|
} else {
|
||||||
|
decltype(AcceptedExtensions.begin()) find_type = AcceptedExtensions.find(file.path.ext);
|
||||||
|
if (find_type != AcceptedExtensions.end()) {
|
||||||
|
ret.type = find_type->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preprocess(file.src.str(), ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderUnit ShaderPreprocessor::preprocess(const std::string& code, GLenum shader_type) const {
|
||||||
|
File local;
|
||||||
|
local.path = File::FilePath{"live:", "live", ".shad"};
|
||||||
|
return preprocess(local, shader_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderProgram ShaderPreprocessor::CreateShaderProgram(std::vector<std::string> shads) const {
|
||||||
|
std::vector<ShaderUnit> _shader_units;
|
||||||
|
for (const auto& name : shads) {
|
||||||
|
_shader_units.push_back(preprocess(load(name)));
|
||||||
|
}
|
||||||
|
return ShaderProgram::CreateShaderProgram(_shader_units);
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderProgram ShaderPreprocessor::ReloadFromFile(const ShaderProgram& rhs) const {
|
||||||
|
std::vector<std::string> shader_names;
|
||||||
|
shader_names.reserve(rhs._shader_units.size());
|
||||||
|
std::string filename;
|
||||||
|
for (const ShaderUnit& shad : rhs._shader_units) {
|
||||||
|
filename = shad.file.path.dir + shad.file.path.basename + shad.file.path.ext;
|
||||||
|
shader_names.emplace_back(filename);
|
||||||
|
}
|
||||||
|
return CreateShaderProgram(shader_names);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ShaderPreprocessor::directive_dispatch(ShaderUnit& shad, const std::string& line) const {
|
||||||
|
StringVec dir_tks = {""};
|
||||||
|
for (char c : line) {
|
||||||
|
if (std::isblank(c)) {
|
||||||
|
if (dir_tks.back() != "") {
|
||||||
|
dir_tks.push_back("");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dir_tks.back() += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!dir_tks.size()) { return false; }
|
||||||
|
|
||||||
|
if (dir_tks[0][0] != '#') { return false; }
|
||||||
|
|
||||||
|
if (dir_tks[0] == "#define") {
|
||||||
|
return preprocessor_define(shad, dir_tks, line);
|
||||||
|
} else if (dir_tks[0] == "#version") {
|
||||||
|
return preprocessor_version(shad, dir_tks, line);
|
||||||
|
} else if (dir_tks[0] == "#include") {
|
||||||
|
return preprocessor_include(shad, dir_tks, line);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef std::vector<std::string> StringVec;
|
||||||
|
|
||||||
|
bool ShaderPreprocessor::preprocessor_include(
|
||||||
|
ShaderUnit& shad,
|
||||||
|
const StringVec& tokens,
|
||||||
|
const std::string& line
|
||||||
|
) const
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (tokens[0] != "#include") { return false; }
|
||||||
|
} catch (std::out_of_range e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::string path = "";
|
||||||
|
for (const auto& tk : tokens) {
|
||||||
|
if (tk != tokens.front()) {
|
||||||
|
path += tk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add file-inclusion base +
|
||||||
|
// Do path cleanup +
|
||||||
|
// Restructure preprocessing data flow
|
||||||
|
std::string filename = path.substr(1, path.size()-2);
|
||||||
|
try {
|
||||||
|
if (path[0] == '"' && path.back() == '"') {
|
||||||
|
preprocess(load(filename).src.str(), shad);
|
||||||
|
return true;
|
||||||
|
} else if (path[0] == '<' && path.back() == '>') {
|
||||||
|
preprocess(load(filename, true).src.str(), shad);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (ShaderPreprocessorError e) {
|
||||||
|
if (e.code == Codes::FILE_NOT_FOUND) {
|
||||||
|
std::cout << "COULD NOT FIND " << filename << ".\n";
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ShaderPreprocessor::preprocessor_define(ShaderUnit& shad, const StringVec& tokens, const std::string& line) const {
|
||||||
|
shad.preprocSource += line;
|
||||||
|
try {
|
||||||
|
if (tokens[0] != "#define") { return false; }
|
||||||
|
try {
|
||||||
|
shad.defines[tokens[1]] = tokens[2];
|
||||||
|
} catch (std::length_error& f) {
|
||||||
|
shad.defines[tokens[1]] = "";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (std::out_of_range& e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ShaderPreprocessor::preprocessor_version(ShaderUnit& shad, const StringVec& tokens, const std::string& line) const {
|
||||||
|
shad.preprocSource += line;
|
||||||
|
std::cout << "Shader version: ";
|
||||||
|
try {
|
||||||
|
if (tokens[0] != "#version") { return false; }
|
||||||
|
if (tokens[1].size() == 3) {
|
||||||
|
short vMajor = tokens[1][0]-'0';
|
||||||
|
short vMinorA = tokens[1][1]-'0';
|
||||||
|
short vMinorB = tokens[1][2]-'0';
|
||||||
|
if (vMajor > 10 || vMinorA > 10 || vMinorB > 10) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
shad.vMajor = vMajor;
|
||||||
|
shad.vMinor = vMinorA*10 + vMinorB;
|
||||||
|
shad.profile = AcceptedProfiles.at(tokens[2]);
|
||||||
|
std::cout << shad.vMajor << "." << shad.vMinor << "\n";
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (std::out_of_range& e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* bool ShaderPreprocessor::preprocessor_uniform(
|
||||||
|
ShaderUnit& shad,
|
||||||
|
const std::string& type,
|
||||||
|
const std::string& name
|
||||||
|
) const {
|
||||||
|
std::string type_str = "";
|
||||||
|
unsigned int type_str_len = 0;
|
||||||
|
for (const char& c : type) {
|
||||||
|
if (type_str_len) {
|
||||||
|
if (std::isdigit(c)) {
|
||||||
|
type_str += c;
|
||||||
|
continue;
|
||||||
|
} else if (c=='[') {
|
||||||
|
type_str += '_';
|
||||||
|
}else if (c == ']' || std::isblank(c)){
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (std::isalnum(c)) {
|
||||||
|
type_str += c;
|
||||||
|
continue;
|
||||||
|
} else if (c == '[') {
|
||||||
|
type_str_len = type_str.length();
|
||||||
|
type_str += '_';
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string name_str = "";
|
||||||
|
unsigned int name_str_len = 0;
|
||||||
|
for (const char& c : name) {
|
||||||
|
if (c == ';') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (name_str_len) {
|
||||||
|
if (std::isdigit(c)) {
|
||||||
|
type_str.insert(type_str_len, 1, c);
|
||||||
|
type_str_len++;
|
||||||
|
continue;
|
||||||
|
} else if (c=='[') {
|
||||||
|
type_str.insert(type_str_len, "_");
|
||||||
|
type_str_len++;
|
||||||
|
continue;
|
||||||
|
}else if (c == ']' || std::isblank(c)){
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (std::isalnum(c)) {
|
||||||
|
name_str += c;
|
||||||
|
continue;
|
||||||
|
} else if ( c == '[') {
|
||||||
|
name_str_len = name_str.length();
|
||||||
|
type_str.insert(type_str_len, "_");
|
||||||
|
type_str_len++;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shad.uniforms.push_back(UniformHandle{
|
||||||
|
name_str,
|
||||||
|
type_str,
|
||||||
|
0x0,
|
||||||
|
0x0
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} */
|
||||||
|
|
||||||
|
|
||||||
|
// ShaderProgram
|
||||||
|
|
||||||
|
ShaderProgram::ShaderProgram(ShaderProgram&& rhs) {
|
||||||
|
_shader_units = rhs._shader_units;
|
||||||
|
_id = rhs._id;
|
||||||
|
rhs._id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderProgram::~ShaderProgram() {
|
||||||
|
glDeleteProgram(_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderProgram& ShaderProgram::operator=(ShaderProgram&& rhs) {
|
||||||
|
_shader_units = rhs._shader_units;
|
||||||
|
_id = rhs._id;
|
||||||
|
rhs._id = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ShaderProgram ShaderProgram::CreateShaderProgram(std::vector<ShaderUnit>& shaders) {
|
||||||
|
int success;
|
||||||
|
ShaderProgram ret(shaders);
|
||||||
|
char infoLog[512];
|
||||||
|
unsigned int shad_id;
|
||||||
|
std::vector<unsigned int> shad_ids;
|
||||||
|
shad_ids.reserve(shaders.size());
|
||||||
|
for (auto& shad : ret._shader_units) {
|
||||||
|
const char* source = shad.preprocSource.data();
|
||||||
|
shad_id = glCreateShader(shad.type);
|
||||||
|
shad_ids.emplace_back(shad_id);
|
||||||
|
glShaderSource(shad_id, 1, &source, NULL);
|
||||||
|
glCompileShader(shad_id);
|
||||||
|
glGetShaderiv(shad_id, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(shad_id, 512, NULL, infoLog);
|
||||||
|
File::FilePath& fp = shad.file.path;
|
||||||
|
std::string filename = fp.dir + fp.basename + fp.ext;
|
||||||
|
// std::cout << "Could not compile '" + filename + "': " << infoLog << "\n";
|
||||||
|
// return *ret;
|
||||||
|
throw ShaderError("Could not compile '" + filename + "'.", infoLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret._id = glCreateProgram();
|
||||||
|
for(auto& id : shad_ids) {
|
||||||
|
glAttachShader(ret._id, id);
|
||||||
|
}
|
||||||
|
glLinkProgram(ret._id);
|
||||||
|
glGetProgramiv(ret._id, GL_LINK_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetProgramInfoLog(ret._id, 512, NULL, infoLog);
|
||||||
|
throw ShaderError("Could not link shader program.", infoLog);
|
||||||
|
}
|
||||||
|
for (auto& id : shad_ids) {
|
||||||
|
glDeleteShader(id);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int ShaderProgram::id() const { return _id; }
|
||||||
|
|
||||||
|
void ShaderProgram::use() const {
|
||||||
|
glUseProgram(_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ShaderUnit> ShaderProgram::getShaders() const {
|
||||||
|
return _shader_units;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderUnit ShaderProgram::getShaders(unsigned int idx) const {
|
||||||
|
return _shader_units[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::setBool(const std::string& name, bool value) const {
|
||||||
|
glUniform1i(glGetUniformLocation(_id, name.c_str()), (int)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::setInt(const std::string& name, int value) const {
|
||||||
|
glUniform1i(glGetUniformLocation(_id, name.c_str()), (int)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::setFloat(const std::string& name, float value) const {
|
||||||
|
glUniform1f(glGetUniformLocation(_id, name.c_str()), (int)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShaderProgram::setMat4(const std::string& name, glm::mat4& value) const {
|
||||||
|
glUniformMatrix4fv(glGetUniformLocation(_id, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* // Shader class
|
||||||
|
Shader::Shader() { _id = 0x0; }
|
||||||
|
|
||||||
|
Shader::Shader(const Shader& cpy_shader) { _id = cpy_shader._id; }
|
||||||
|
|
||||||
|
Shader& Shader::operator=(const Shader& cpy_shader) { _id = cpy_shader._id; return *this; }
|
||||||
|
|
||||||
|
void Shader::use() const{
|
||||||
|
glUseProgram(_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
||||||
@ -9,6 +9,8 @@ if (NB_BUILD_TESTS)
|
|||||||
target_link_libraries(TestWindow
|
target_link_libraries(TestWindow
|
||||||
NBCore
|
NBCore
|
||||||
NBGraphics
|
NBGraphics
|
||||||
|
# GTest::gtest_main
|
||||||
)
|
)
|
||||||
|
# gtest_discover_tests(TestWindow)
|
||||||
|
|
||||||
endif()
|
endif()
|
||||||
@ -1,8 +1,9 @@
|
|||||||
#include "GLLoad.hpp"
|
#include "GLLoad.hpp"
|
||||||
#include "ProgramPipeline.hpp"
|
// #define _NB_AUTOLOG
|
||||||
#include "VertexArray.hpp"
|
#include "VertexArray.hpp"
|
||||||
#include "Window.hpp"
|
#include "Window.hpp"
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
nb::logger.log("Howdy!");
|
nb::logger.log("Howdy!");
|
||||||
nb::Window window(200, 200, "Hello!");
|
nb::Window window(200, 200, "Hello!");
|
||||||
@ -10,53 +11,8 @@ int main() {
|
|||||||
window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
window.init();
|
window.init();
|
||||||
|
|
||||||
nb::logger.log("Goob");
|
|
||||||
|
|
||||||
auto vert = std::make_shared<nb::Shader>(
|
|
||||||
GL_VERTEX_SHADER,
|
|
||||||
"#version 330 core\n"
|
|
||||||
"layout (location = 0) in vec2 aPos;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);\n"
|
|
||||||
"}\0"
|
|
||||||
);
|
|
||||||
|
|
||||||
LOG("Vertex Shader: " + vert->log());
|
|
||||||
|
|
||||||
auto frag = std::make_shared<nb::Shader>(
|
|
||||||
GL_FRAGMENT_SHADER,
|
|
||||||
"#version 330 core\n"
|
|
||||||
"out vec4 FragColor;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
|
||||||
"}\n\0"
|
|
||||||
);
|
|
||||||
|
|
||||||
LOG("Fragment shader: " + frag->log());
|
|
||||||
|
|
||||||
nb::ByteVector data = nb::vectorToBytes<float>({
|
|
||||||
-0.5, -0.5, 0.5, -0.5, 0.0, 0.5
|
|
||||||
});
|
|
||||||
|
|
||||||
nb::Program prog({vert, frag});
|
|
||||||
prog.bind();
|
|
||||||
|
|
||||||
nb::VertexGroup tri(data, {
|
|
||||||
nb::VertexAttribute{
|
|
||||||
2,
|
|
||||||
GL_FLOAT,
|
|
||||||
false,
|
|
||||||
{0, 8}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
tri.bind();
|
|
||||||
|
|
||||||
GLFWwindow* window_ptr = window.getWindow();
|
GLFWwindow* window_ptr = window.getWindow();
|
||||||
while(!glfwWindowShouldClose(window_ptr)) {
|
while(!glfwWindowShouldClose(window_ptr)) {}
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user