Make EnumClassBitmask operations constexpr

This allows declaring combinations of bits as constants in header.
kReadOnlyBufferUsages is added using this feature.

Also reformat the file.
This commit is contained in:
Corentin Wallez 2018-07-05 16:29:12 +02:00 committed by Corentin Wallez
parent 23bec15f4c
commit 117f2f0ad6
3 changed files with 38 additions and 22 deletions

View File

@ -164,11 +164,8 @@ namespace backend {
}
bool BufferBase::IsUsagePossible(nxt::BufferUsageBit allowedUsage, nxt::BufferUsageBit usage) {
const nxt::BufferUsageBit allReadBits =
nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::TransferSrc |
nxt::BufferUsageBit::Index | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Uniform;
bool allowed = (usage & allowedUsage) == usage;
bool readOnly = (usage & allReadBits) == usage;
bool readOnly = (usage & kReadOnlyBufferUsages) == usage;
bool singleUse = nxt::HasZeroOrOneBits(usage);
return allowed && (readOnly || singleUse);
}

View File

@ -23,6 +23,14 @@
namespace backend {
static constexpr nxt::BufferUsageBit kReadOnlyBufferUsages =
nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::TransferSrc |
nxt::BufferUsageBit::Index | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Uniform;
static constexpr nxt::BufferUsageBit kWritableBufferUsages = nxt::BufferUsageBit::MapWrite |
nxt::BufferUsageBit::TransferDst |
nxt::BufferUsageBit::Storage;
class BufferBase : public RefCounted {
public:
BufferBase(BufferBuilder* builder);

View File

@ -53,16 +53,23 @@ namespace nxt {
struct LowerBitmask<T, typename std::enable_if<IsNXTBitmask<T>::enable>::type> {
static constexpr bool enable = true;
using type = T;
static T Lower(T t) {return t;}
constexpr static T Lower(T t) {
return t;
}
};
template<typename T>
struct BoolConvertible {
using Integral = typename UnderlyingType<T>::type;
BoolConvertible(Integral value) : value(value) {}
operator bool() const {return value != 0;}
operator T() const {return static_cast<T>(value);}
constexpr BoolConvertible(Integral value) : value(value) {
}
constexpr operator bool() const {
return value != 0;
}
constexpr operator T() const {
return static_cast<T>(value);
}
Integral value;
};
@ -71,7 +78,9 @@ namespace nxt {
struct LowerBitmask<BoolConvertible<T>> {
static constexpr bool enable = true;
using type = T;
static type Lower(BoolConvertible<T> t) {return t;}
static constexpr type Lower(BoolConvertible<T> t) {
return t;
}
};
template<typename T>
@ -117,33 +126,35 @@ namespace nxt {
return ~static_cast<Integral>(LowerBitmask<T1>::Lower(t));
}
template<typename T, typename T2, typename = typename std::enable_if<
IsNXTBitmask<T>::enable && LowerBitmask<T2>::enable
>::type>
T& operator &= (T& l, T2 right) {
template <typename T,
typename T2,
typename = typename std::enable_if<IsNXTBitmask<T>::enable &&
LowerBitmask<T2>::enable>::type>
constexpr T& operator&=(T& l, T2 right) {
T r = LowerBitmask<T2>::Lower(right);
l = l & r;
return l;
}
template<typename T, typename T2, typename = typename std::enable_if<
IsNXTBitmask<T>::enable && LowerBitmask<T2>::enable
>::type>
T& operator |= (T& l, T2 right) {
template <typename T,
typename T2,
typename = typename std::enable_if<IsNXTBitmask<T>::enable &&
LowerBitmask<T2>::enable>::type>
constexpr T& operator|=(T& l, T2 right) {
T r = LowerBitmask<T2>::Lower(right);
l = l | r;
return l;
}
template<typename T, typename T2, typename = typename std::enable_if<
IsNXTBitmask<T>::enable && LowerBitmask<T2>::enable
>::type>
T& operator ^= (T& l, T2 right) {
template <typename T,
typename T2,
typename = typename std::enable_if<IsNXTBitmask<T>::enable &&
LowerBitmask<T2>::enable>::type>
constexpr T& operator^=(T& l, T2 right) {
T r = LowerBitmask<T2>::Lower(right);
l = l ^ r;
return l;
}
}
#endif // NXT_ENUM_CLASS_BITMASKS_H_