mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-06 22:53:35 +00:00
Format: src/{common, utils, wire}
This commit is contained in:
parent
a351ce9618
commit
9d01c6c26d
@ -16,7 +16,11 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void HandleAssertionFailure(const char* file, const char* function, int line, const char* condition) {
|
||||
std::cerr << "Assertion failure at " << file << ":" << line << " (" << function << "): " << condition << std::endl;
|
||||
void HandleAssertionFailure(const char* file,
|
||||
const char* function,
|
||||
int line,
|
||||
const char* condition) {
|
||||
std::cerr << "Assertion failure at " << file << ":" << line << " (" << function
|
||||
<< "): " << condition << std::endl;
|
||||
NXT_BREAKPOINT();
|
||||
}
|
||||
|
@ -17,63 +17,64 @@
|
||||
|
||||
#include "common/Compiler.h"
|
||||
|
||||
void HandleAssertionFailure(const char* file, const char* function, int line, const char* condition);
|
||||
// NXT asserts to be used instead of the regular C stdlib assert function (if you don't use assert
|
||||
// yet, you should start now!). In debug ASSERT(condition) will trigger an error, otherwise in
|
||||
// release it does nothing at runtime.
|
||||
//
|
||||
// In case of name clashes (with for example a testing library), you can define the
|
||||
// NXT_SKIP_ASSERT_SHORTHANDS to only define the NXT_ prefixed macros.
|
||||
//
|
||||
// These asserts feature:
|
||||
// - Logging of the error with file, line and function information.
|
||||
// - Breaking in the debugger when an assert is triggered and a debugger is attached.
|
||||
// - Use the assert information to help the compiler optimizer in release builds.
|
||||
|
||||
/*
|
||||
* NXT asserts to be used instead of the regular C stdlib assert function (if you don't
|
||||
* use assert yet, you should start now!). In debug ASSERT(condition) will trigger an error,
|
||||
* otherwise in release it does nothing at runtime.
|
||||
*
|
||||
* In case of name clashes (with for example a testing library), you can define the
|
||||
* NXT_SKIP_ASSERT_SHORTHANDS to only define the NXT_ prefixed macros.
|
||||
*
|
||||
* These asserts feature:
|
||||
* - Logging of the error with file, line and function information.
|
||||
* - Breaking in the debugger when an assert is triggered and a debugger is attached.
|
||||
* - Use the assert information to help the compiler optimizer in release builds.
|
||||
*/
|
||||
|
||||
// MSVC triggers a warning in /W4 for do {} while(0). SDL worked around this by using
|
||||
// // (0,0) and points out that it looks like an owl face.
|
||||
// MSVC triggers a warning in /W4 for do {} while(0). SDL worked around this by using (0,0) and
|
||||
// points out that it looks like an owl face.
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
#define NXT_ASSERT_LOOP_CONDITION (0,0)
|
||||
# define NXT_ASSERT_LOOP_CONDITION (0, 0)
|
||||
#else
|
||||
#define NXT_ASSERT_LOOP_CONDITION (0)
|
||||
# define NXT_ASSERT_LOOP_CONDITION (0)
|
||||
#endif
|
||||
|
||||
// NXT_ASSERT_CALLSITE_HELPER generates the actual assert code. In Debug it does what you would
|
||||
// expect of an assert and in release it tries to give hints to make the compiler generate better code.
|
||||
// expect of an assert and in release it tries to give hints to make the compiler generate better
|
||||
// code.
|
||||
#if defined(NXT_ENABLE_ASSERTS)
|
||||
#define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
|
||||
# define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
HandleAssertionFailure(file, func, line, #condition); \
|
||||
} \
|
||||
} while(NXT_ASSERT_LOOP_CONDITION)
|
||||
} while (NXT_ASSERT_LOOP_CONDITION)
|
||||
#else
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
#define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
|
||||
__assume(condition)
|
||||
#elif defined(NXT_COMPILER_CLANG) && defined(__builtin_assume)
|
||||
#define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
|
||||
__builtin_assume(condition)
|
||||
#else
|
||||
#define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
|
||||
# if defined(NXT_COMPILER_MSVC)
|
||||
# define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) __assume(condition)
|
||||
# elif defined(NXT_COMPILER_CLANG) && defined(__builtin_assume)
|
||||
# define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) __builtin_assume(condition)
|
||||
# else
|
||||
# define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
|
||||
do { \
|
||||
(void) sizeof(condition); \
|
||||
} while(NXT_ASSERT_LOOP_CONDITION)
|
||||
#endif
|
||||
(void)sizeof(condition); \
|
||||
} while (NXT_ASSERT_LOOP_CONDITION)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define NXT_ASSERT(condition) NXT_ASSERT_CALLSITE_HELPER(__FILE__, __func__, __LINE__, condition)
|
||||
#define NXT_UNREACHABLE() \
|
||||
do { \
|
||||
NXT_ASSERT(NXT_ASSERT_LOOP_CONDITION && "Unreachable code hit"); NXT_BUILTIN_UNREACHABLE(); \
|
||||
} while(NXT_ASSERT_LOOP_CONDITION)
|
||||
NXT_ASSERT(NXT_ASSERT_LOOP_CONDITION && "Unreachable code hit"); \
|
||||
NXT_BUILTIN_UNREACHABLE(); \
|
||||
} while (NXT_ASSERT_LOOP_CONDITION)
|
||||
|
||||
#if !defined(NXT_SKIP_ASSERT_SHORTHANDS)
|
||||
#define ASSERT NXT_ASSERT
|
||||
#define UNREACHABLE NXT_UNREACHABLE
|
||||
# define ASSERT NXT_ASSERT
|
||||
# define UNREACHABLE NXT_UNREACHABLE
|
||||
#endif
|
||||
|
||||
void HandleAssertionFailure(const char* file,
|
||||
const char* function,
|
||||
int line,
|
||||
const char* condition);
|
||||
|
||||
#endif // COMMON_ASSERT_H_
|
||||
|
@ -21,7 +21,6 @@
|
||||
#include <bitset>
|
||||
#include <limits>
|
||||
|
||||
|
||||
// This is ANGLE's BitSetIterator class with a customizable return type
|
||||
// TODO(cwallez@chromium.org): it could be optimized, in particular when N <= 64
|
||||
|
||||
@ -36,7 +35,7 @@ class BitSetIterator final {
|
||||
public:
|
||||
BitSetIterator(const std::bitset<N>& bitset);
|
||||
BitSetIterator(const BitSetIterator& other);
|
||||
BitSetIterator &operator=(const BitSetIterator& other);
|
||||
BitSetIterator& operator=(const BitSetIterator& other);
|
||||
|
||||
class Iterator final {
|
||||
public:
|
||||
@ -45,7 +44,9 @@ class BitSetIterator final {
|
||||
|
||||
bool operator==(const Iterator& other) const;
|
||||
bool operator!=(const Iterator& other) const;
|
||||
T operator*() const { return static_cast<T>(mCurrentBit); }
|
||||
T operator*() const {
|
||||
return static_cast<T>(mCurrentBit);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned long getNextBit();
|
||||
@ -56,21 +57,23 @@ class BitSetIterator final {
|
||||
unsigned long mOffset;
|
||||
};
|
||||
|
||||
Iterator begin() const { return Iterator(mBits); }
|
||||
Iterator end() const { return Iterator(std::bitset<N>(0)); }
|
||||
Iterator begin() const {
|
||||
return Iterator(mBits);
|
||||
}
|
||||
Iterator end() const {
|
||||
return Iterator(std::bitset<N>(0));
|
||||
}
|
||||
|
||||
private:
|
||||
const std::bitset<N> mBits;
|
||||
};
|
||||
|
||||
template <size_t N, typename T>
|
||||
BitSetIterator<N, T>::BitSetIterator(const std::bitset<N>& bitset)
|
||||
: mBits(bitset) {
|
||||
BitSetIterator<N, T>::BitSetIterator(const std::bitset<N>& bitset) : mBits(bitset) {
|
||||
}
|
||||
|
||||
template <size_t N, typename T>
|
||||
BitSetIterator<N, T>::BitSetIterator(const BitSetIterator& other)
|
||||
: mBits(other.mBits) {
|
||||
BitSetIterator<N, T>::BitSetIterator(const BitSetIterator& other) : mBits(other.mBits) {
|
||||
}
|
||||
|
||||
template <size_t N, typename T>
|
||||
|
@ -21,32 +21,32 @@
|
||||
// - NXT_BUILTIN_UNREACHABLE(): Hints the compiler that a code path is unreachable
|
||||
|
||||
// Clang and GCC
|
||||
#ifdef __GNUC__
|
||||
#if defined(__clang__)
|
||||
#define NXT_COMPILER_CLANG
|
||||
#else
|
||||
#define NXT_COMPILER_GCC
|
||||
#endif
|
||||
#if defined(__GNUC__)
|
||||
# if defined(__clang__)
|
||||
# define NXT_COMPILER_CLANG
|
||||
# else
|
||||
# define NXT_COMPILER_GCC
|
||||
# endif
|
||||
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
#define NXT_BREAKPOINT() __asm__ __volatile__("int $3\n\t")
|
||||
#else
|
||||
#error "Implement BREAKPOINT on your platform"
|
||||
#endif
|
||||
# if defined(__i386__) || defined(__x86_64__)
|
||||
# define NXT_BREAKPOINT() __asm__ __volatile__("int $3\n\t")
|
||||
# else
|
||||
# error "Implement BREAKPOINT on your platform"
|
||||
# endif
|
||||
|
||||
#define NXT_BUILTIN_UNREACHABLE() __builtin_unreachable()
|
||||
# define NXT_BUILTIN_UNREACHABLE() __builtin_unreachable()
|
||||
|
||||
// MSVC
|
||||
#elif defined(_MSC_VER)
|
||||
#define NXT_COMPILER_MSVC
|
||||
# define NXT_COMPILER_MSVC
|
||||
|
||||
extern void __cdecl __debugbreak(void);
|
||||
#define NXT_BREAKPOINT() __debugbreak()
|
||||
extern void __cdecl __debugbreak(void);
|
||||
# define NXT_BREAKPOINT() __debugbreak()
|
||||
|
||||
#define NXT_BUILTIN_UNREACHABLE() __assume(false)
|
||||
# define NXT_BUILTIN_UNREACHABLE() __assume(false)
|
||||
|
||||
#else
|
||||
#error "Unsupported compiler"
|
||||
# error "Unsupported compiler"
|
||||
#endif
|
||||
|
||||
#endif // COMMON_COMPILER_H_
|
||||
|
@ -19,7 +19,8 @@
|
||||
|
||||
static constexpr uint32_t kMaxPushConstants = 32u;
|
||||
static constexpr uint32_t kMaxBindGroups = 4u;
|
||||
static constexpr uint32_t kMaxBindingsPerGroup = 16u; // TODO(cwallez@chromium.org): investigate bindgroup limits
|
||||
// TODO(cwallez@chromium.org): investigate bindgroup limits
|
||||
static constexpr uint32_t kMaxBindingsPerGroup = 16u;
|
||||
static constexpr uint32_t kMaxVertexAttributes = 16u;
|
||||
static constexpr uint32_t kMaxVertexInputs = 16u;
|
||||
static constexpr uint32_t kNumStages = 3;
|
||||
|
@ -17,11 +17,11 @@
|
||||
#include "common/Platform.h"
|
||||
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
#include <windows.h>
|
||||
# include <windows.h>
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
#include <dlfcn.h>
|
||||
# include <dlfcn.h>
|
||||
#else
|
||||
#error "Unsupported platform for DynamicLib"
|
||||
# error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
|
||||
DynamicLib::~DynamicLib() {
|
||||
@ -42,21 +42,21 @@ bool DynamicLib::Valid() const {
|
||||
}
|
||||
|
||||
bool DynamicLib::Open(const std::string& filename, std::string* error) {
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
mHandle = LoadLibraryA(filename.c_str());
|
||||
|
||||
if (mHandle == nullptr && error != nullptr) {
|
||||
*error = "Windows Error: " + std::to_string(GetLastError());
|
||||
}
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
mHandle = dlopen(filename.c_str(), RTLD_NOW);
|
||||
|
||||
if (mHandle == nullptr && error != nullptr) {
|
||||
*error = dlerror();
|
||||
}
|
||||
#else
|
||||
#error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
#else
|
||||
# error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
|
||||
return mHandle != nullptr;
|
||||
}
|
||||
@ -66,13 +66,13 @@ void DynamicLib::Close() {
|
||||
return;
|
||||
}
|
||||
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
FreeLibrary(static_cast<HMODULE>(mHandle));
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
dlclose(mHandle);
|
||||
#else
|
||||
#error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
#else
|
||||
# error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
|
||||
mHandle = nullptr;
|
||||
}
|
||||
@ -80,21 +80,21 @@ void DynamicLib::Close() {
|
||||
void* DynamicLib::GetProc(const std::string& procName, std::string* error) const {
|
||||
void* proc = nullptr;
|
||||
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
#if NXT_PLATFORM_WINDOWS
|
||||
proc = reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(mHandle), procName.c_str()));
|
||||
|
||||
if (proc == nullptr && error != nullptr) {
|
||||
*error = "Windows Error: " + std::to_string(GetLastError());
|
||||
}
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
#elif NXT_PLATFORM_POSIX
|
||||
proc = reinterpret_cast<void*>(dlsym(mHandle, procName.c_str()));
|
||||
|
||||
if (proc == nullptr && error != nullptr) {
|
||||
*error = dlerror();
|
||||
}
|
||||
#else
|
||||
#error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
#else
|
||||
# error "Unsupported platform for DynamicLib"
|
||||
#endif
|
||||
|
||||
return proc;
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ class DynamicLib {
|
||||
|
||||
void* GetProc(const std::string& procName, std::string* error = nullptr) const;
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
bool GetProc(T** proc, const std::string& procName, std::string* error = nullptr) const {
|
||||
ASSERT(proc != nullptr);
|
||||
static_assert(std::is_function<T>::value, "");
|
||||
|
@ -17,31 +17,31 @@
|
||||
#include "common/Assert.h"
|
||||
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
#include <intrin.h>
|
||||
# include <intrin.h>
|
||||
#endif
|
||||
|
||||
uint32_t ScanForward(uint32_t bits) {
|
||||
ASSERT(bits != 0);
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
unsigned long firstBitIndex = 0ul;
|
||||
unsigned char ret = _BitScanForward(&firstBitIndex, bits);
|
||||
ASSERT(ret != 0);
|
||||
return firstBitIndex;
|
||||
#else
|
||||
#else
|
||||
return static_cast<uint32_t>(__builtin_ctz(bits));
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t Log2(uint32_t value) {
|
||||
ASSERT(value != 0);
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
#if defined(NXT_COMPILER_MSVC)
|
||||
unsigned long firstBitIndex = 0ul;
|
||||
unsigned char ret = _BitScanReverse(&firstBitIndex, value);
|
||||
ASSERT(ret != 0);
|
||||
return firstBitIndex;
|
||||
#else
|
||||
#else
|
||||
return 31 - static_cast<uint32_t>(__builtin_clz(value));
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool IsPowerOfTwo(size_t n) {
|
||||
@ -58,7 +58,8 @@ bool IsPtrAligned(const void* ptr, size_t alignment) {
|
||||
void* AlignVoidPtr(void* ptr, size_t alignment) {
|
||||
ASSERT(IsPowerOfTwo(alignment));
|
||||
ASSERT(alignment != 0);
|
||||
return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) & ~(alignment - 1));
|
||||
return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
|
||||
~(alignment - 1));
|
||||
}
|
||||
|
||||
bool IsAligned(uint32_t value, size_t alignment) {
|
||||
|
@ -28,12 +28,12 @@ void* AlignVoidPtr(void* ptr, size_t alignment);
|
||||
bool IsAligned(uint32_t value, size_t alignment);
|
||||
uint32_t Align(uint32_t value, size_t alignment);
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T* AlignPtr(T* ptr, size_t alignment) {
|
||||
return reinterpret_cast<T*>(AlignVoidPtr(ptr, alignment));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
const T* AlignPtr(const T* ptr, size_t alignment) {
|
||||
return reinterpret_cast<const T*>(AlignVoidPtr(const_cast<T*>(ptr), alignment));
|
||||
}
|
||||
|
@ -16,15 +16,15 @@
|
||||
#define COMMON_PLATFORM_H_
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#define NXT_PLATFORM_WINDOWS 1
|
||||
# define NXT_PLATFORM_WINDOWS 1
|
||||
#elif defined(__linux__)
|
||||
#define NXT_PLATFORM_LINUX 1
|
||||
#define NXT_PLATFORM_POSIX 1
|
||||
# define NXT_PLATFORM_LINUX 1
|
||||
# define NXT_PLATFORM_POSIX 1
|
||||
#elif defined(__APPLE__)
|
||||
#define NXT_PLATFORM_APPLE 1
|
||||
#define NXT_PLATFORM_POSIX 1
|
||||
# define NXT_PLATFORM_APPLE 1
|
||||
# define NXT_PLATFORM_POSIX 1
|
||||
#else
|
||||
#error "Unsupported platform."
|
||||
# error "Unsupported platform."
|
||||
#endif
|
||||
|
||||
#endif // COMMON_PLATFORM_H_
|
||||
|
@ -21,7 +21,7 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
class SerialQueue {
|
||||
private:
|
||||
using SerialPair = std::pair<Serial, std::vector<T>>;
|
||||
@ -115,7 +115,7 @@ class SerialQueue {
|
||||
|
||||
// SerialQueue
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void SerialQueue<T>::Enqueue(const T& value, Serial serial) {
|
||||
NXT_ASSERT(Empty() || mStorage.back().first <= serial);
|
||||
|
||||
@ -125,7 +125,7 @@ void SerialQueue<T>::Enqueue(const T& value, Serial serial) {
|
||||
mStorage.back().second.emplace_back(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void SerialQueue<T>::Enqueue(T&& value, Serial serial) {
|
||||
NXT_ASSERT(Empty() || mStorage.back().first <= serial);
|
||||
|
||||
@ -135,132 +135,133 @@ void SerialQueue<T>::Enqueue(T&& value, Serial serial) {
|
||||
mStorage.back().second.emplace_back(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void SerialQueue<T>::Enqueue(const std::vector<T>& values, Serial serial) {
|
||||
NXT_ASSERT(values.size() > 0);
|
||||
NXT_ASSERT(Empty() || mStorage.back().first <= serial);
|
||||
mStorage.emplace_back(SerialPair(serial, {values}));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void SerialQueue<T>::Enqueue(std::vector<T>&& values, Serial serial) {
|
||||
NXT_ASSERT(values.size() > 0);
|
||||
NXT_ASSERT(Empty() || mStorage.back().first <= serial);
|
||||
mStorage.emplace_back(SerialPair(serial, {values}));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
bool SerialQueue<T>::Empty() const {
|
||||
return mStorage.empty();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::ConstBeginEnd SerialQueue<T>::IterateAll() const {
|
||||
return {mStorage.begin(), mStorage.end()};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::ConstBeginEnd SerialQueue<T>::IterateUpTo(Serial serial) const {
|
||||
return {mStorage.begin(), FindUpTo(serial)};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::BeginEnd SerialQueue<T>::IterateAll() {
|
||||
return {mStorage.begin(), mStorage.end()};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::BeginEnd SerialQueue<T>::IterateUpTo(Serial serial) {
|
||||
return {mStorage.begin(), FindUpTo(serial)};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void SerialQueue<T>::Clear() {
|
||||
mStorage.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void SerialQueue<T>::ClearUpTo(Serial serial) {
|
||||
mStorage.erase(mStorage.begin(), FindUpTo(serial));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
Serial SerialQueue<T>::FirstSerial() const {
|
||||
NXT_ASSERT(!Empty());
|
||||
return mStorage.front().first;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::ConstStorageIterator SerialQueue<T>::FindUpTo(Serial serial) const {
|
||||
auto it = mStorage.begin();
|
||||
while (it != mStorage.end() && it->first <= serial) {
|
||||
it ++;
|
||||
it++;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::StorageIterator SerialQueue<T>::FindUpTo(Serial serial) {
|
||||
auto it = mStorage.begin();
|
||||
while (it != mStorage.end() && it->first <= serial) {
|
||||
it ++;
|
||||
it++;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
// SerialQueue::BeginEnd
|
||||
|
||||
template<typename T>
|
||||
SerialQueue<T>::BeginEnd::BeginEnd(typename SerialQueue<T>::StorageIterator start, typename SerialQueue<T>::StorageIterator end)
|
||||
template <typename T>
|
||||
SerialQueue<T>::BeginEnd::BeginEnd(typename SerialQueue<T>::StorageIterator start,
|
||||
typename SerialQueue<T>::StorageIterator end)
|
||||
: mStartIt(start), mEndIt(end) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::Iterator SerialQueue<T>::BeginEnd::begin() const {
|
||||
return {mStartIt};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::Iterator SerialQueue<T>::BeginEnd::end() const {
|
||||
return {mEndIt};
|
||||
}
|
||||
|
||||
// SerialQueue::Iterator
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
SerialQueue<T>::Iterator::Iterator(typename SerialQueue<T>::StorageIterator start)
|
||||
: mStorageIterator(start), mSerialIterator(nullptr) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::Iterator& SerialQueue<T>::Iterator::operator++() {
|
||||
T* vectorData = mStorageIterator->second.data();
|
||||
|
||||
if (mSerialIterator == nullptr) {
|
||||
mSerialIterator = vectorData + 1;
|
||||
} else {
|
||||
mSerialIterator ++;
|
||||
mSerialIterator++;
|
||||
}
|
||||
|
||||
if (mSerialIterator >= vectorData + mStorageIterator->second.size()) {
|
||||
mSerialIterator = nullptr;
|
||||
mStorageIterator ++;
|
||||
mStorageIterator++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
bool SerialQueue<T>::Iterator::operator==(const typename SerialQueue<T>::Iterator& other) const {
|
||||
return other.mStorageIterator == mStorageIterator && other.mSerialIterator == mSerialIterator;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
bool SerialQueue<T>::Iterator::operator!=(const typename SerialQueue<T>::Iterator& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T& SerialQueue<T>::Iterator::operator*() const {
|
||||
if (mSerialIterator == nullptr) {
|
||||
return *mStorageIterator->second.begin();
|
||||
@ -270,57 +271,60 @@ T& SerialQueue<T>::Iterator::operator*() const {
|
||||
|
||||
// SerialQueue::ConstBeginEnd
|
||||
|
||||
template<typename T>
|
||||
SerialQueue<T>::ConstBeginEnd::ConstBeginEnd(typename SerialQueue<T>::ConstStorageIterator start, typename SerialQueue<T>::ConstStorageIterator end)
|
||||
template <typename T>
|
||||
SerialQueue<T>::ConstBeginEnd::ConstBeginEnd(typename SerialQueue<T>::ConstStorageIterator start,
|
||||
typename SerialQueue<T>::ConstStorageIterator end)
|
||||
: mStartIt(start), mEndIt(end) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::ConstIterator SerialQueue<T>::ConstBeginEnd::begin() const {
|
||||
return {mStartIt};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::ConstIterator SerialQueue<T>::ConstBeginEnd::end() const {
|
||||
return {mEndIt};
|
||||
}
|
||||
|
||||
// SerialQueue::ConstIterator
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
SerialQueue<T>::ConstIterator::ConstIterator(typename SerialQueue<T>::ConstStorageIterator start)
|
||||
: mStorageIterator(start), mSerialIterator(nullptr) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
typename SerialQueue<T>::ConstIterator& SerialQueue<T>::ConstIterator::operator++() {
|
||||
const T* vectorData = mStorageIterator->second.data();
|
||||
|
||||
if (mSerialIterator == nullptr) {
|
||||
mSerialIterator = vectorData + 1;
|
||||
} else {
|
||||
mSerialIterator ++;
|
||||
mSerialIterator++;
|
||||
}
|
||||
|
||||
if (mSerialIterator >= vectorData + mStorageIterator->second.size()) {
|
||||
mSerialIterator = nullptr;
|
||||
mStorageIterator ++;
|
||||
mStorageIterator++;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SerialQueue<T>::ConstIterator::operator==(const typename SerialQueue<T>::ConstIterator& other) const {
|
||||
template <typename T>
|
||||
bool SerialQueue<T>::ConstIterator::operator==(
|
||||
const typename SerialQueue<T>::ConstIterator& other) const {
|
||||
return other.mStorageIterator == mStorageIterator && other.mSerialIterator == mSerialIterator;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SerialQueue<T>::ConstIterator::operator!=(const typename SerialQueue<T>::ConstIterator& other) const {
|
||||
template <typename T>
|
||||
bool SerialQueue<T>::ConstIterator::operator!=(
|
||||
const typename SerialQueue<T>::ConstIterator& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
const T& SerialQueue<T>::ConstIterator::operator*() const {
|
||||
if (mSerialIterator == nullptr) {
|
||||
return *mStorageIterator->second.begin();
|
||||
|
@ -18,21 +18,21 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
#if defined(NXT_ENABLE_BACKEND_D3D12)
|
||||
#if defined(NXT_ENABLE_BACKEND_D3D12)
|
||||
BackendBinding* CreateD3D12Binding();
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_METAL)
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_METAL)
|
||||
BackendBinding* CreateMetalBinding();
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_NULL)
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_NULL)
|
||||
BackendBinding* CreateNullBinding();
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_OPENGL)
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_OPENGL)
|
||||
BackendBinding* CreateOpenGLBinding();
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_VULKAN)
|
||||
#endif
|
||||
#if defined(NXT_ENABLE_BACKEND_VULKAN)
|
||||
BackendBinding* CreateVulkanBinding();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void BackendBinding::SetWindow(GLFWwindow* window) {
|
||||
mWindow = window;
|
||||
@ -40,34 +40,34 @@ namespace utils {
|
||||
|
||||
BackendBinding* CreateBinding(BackendType type) {
|
||||
switch (type) {
|
||||
#if defined(NXT_ENABLE_BACKEND_D3D12)
|
||||
#if defined(NXT_ENABLE_BACKEND_D3D12)
|
||||
case BackendType::D3D12:
|
||||
return CreateD3D12Binding();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(NXT_ENABLE_BACKEND_METAL)
|
||||
#if defined(NXT_ENABLE_BACKEND_METAL)
|
||||
case BackendType::Metal:
|
||||
return CreateMetalBinding();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(NXT_ENABLE_BACKEND_NULL)
|
||||
#if defined(NXT_ENABLE_BACKEND_NULL)
|
||||
case BackendType::Null:
|
||||
return CreateNullBinding();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(NXT_ENABLE_BACKEND_OPENGL)
|
||||
#if defined(NXT_ENABLE_BACKEND_OPENGL)
|
||||
case BackendType::OpenGL:
|
||||
return CreateOpenGLBinding();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(NXT_ENABLE_BACKEND_VULKAN)
|
||||
#if defined(NXT_ENABLE_BACKEND_VULKAN)
|
||||
case BackendType::Vulkan:
|
||||
return CreateVulkanBinding();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -47,6 +47,7 @@ namespace utils {
|
||||
};
|
||||
|
||||
BackendBinding* CreateBinding(BackendType type);
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
||||
#endif // UTILS_BACKENDBINDING_H_
|
||||
|
@ -22,27 +22,26 @@
|
||||
#include "GLFW/glfw3.h"
|
||||
#include "GLFW/glfw3native.h"
|
||||
|
||||
#include <initializer_list>
|
||||
#include <wrl.h>
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_4.h>
|
||||
#include <wrl.h>
|
||||
#include <initializer_list>
|
||||
#ifdef _DEBUG
|
||||
#include <dxgidebug.h>
|
||||
#endif
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace backend {
|
||||
namespace d3d12 {
|
||||
namespace backend { namespace d3d12 {
|
||||
void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device);
|
||||
ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device);
|
||||
uint64_t GetSerial(const nxtDevice device);
|
||||
void NextSerial(nxtDevice device);
|
||||
void ExecuteCommandLists(nxtDevice device, std::initializer_list<ID3D12CommandList*> commandLists);
|
||||
void ExecuteCommandLists(nxtDevice device,
|
||||
std::initializer_list<ID3D12CommandList*> commandLists);
|
||||
void WaitForSerial(nxtDevice device, uint64_t serial);
|
||||
void OpenCommandList(nxtDevice device, ComPtr<ID3D12GraphicsCommandList>* commandList);
|
||||
}
|
||||
}
|
||||
}} // namespace backend::d3d12
|
||||
|
||||
namespace utils {
|
||||
namespace {
|
||||
@ -56,7 +55,8 @@ namespace utils {
|
||||
uint32_t dxgiFactoryFlags = 0;
|
||||
#ifdef _DEBUG
|
||||
// Enable the debug layer (requires the Graphics Tools "optional feature").
|
||||
// NOTE: Enabling the debug layer after device creation will invalidate the active device.
|
||||
// NOTE: Enabling the debug layer after device creation will invalidate the active
|
||||
// device.
|
||||
{
|
||||
ComPtr<ID3D12Debug> debugController;
|
||||
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
|
||||
@ -68,7 +68,8 @@ namespace utils {
|
||||
|
||||
ComPtr<IDXGIDebug1> dxgiDebug;
|
||||
if (SUCCEEDED(DXGIGetDebugInterface1(0, IID_PPV_ARGS(&dxgiDebug)))) {
|
||||
dxgiDebug->ReportLiveObjects(DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_FLAGS(DXGI_DEBUG_RLO_ALL));
|
||||
dxgiDebug->ReportLiveObjects(DXGI_DEBUG_ALL,
|
||||
DXGI_DEBUG_RLO_FLAGS(DXGI_DEBUG_RLO_ALL));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -101,7 +102,8 @@ namespace utils {
|
||||
resourceState |= D3D12_RESOURCE_STATE_COPY_DEST;
|
||||
}
|
||||
if (usage & NXT_TEXTURE_USAGE_BIT_SAMPLED) {
|
||||
resourceState |= (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
|
||||
resourceState |= (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE |
|
||||
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
|
||||
}
|
||||
if (usage & NXT_TEXTURE_USAGE_BIT_STORAGE) {
|
||||
resourceState |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
|
||||
@ -112,7 +114,7 @@ namespace utils {
|
||||
|
||||
return resourceState;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class SwapChainImplD3D12 : SwapChainImpl {
|
||||
public:
|
||||
@ -156,8 +158,10 @@ namespace utils {
|
||||
mCommandQueue = backend::d3d12::GetCommandQueue(mBackendDevice);
|
||||
}
|
||||
|
||||
nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit allowedUsage,
|
||||
uint32_t width, uint32_t height) {
|
||||
nxtSwapChainError Configure(nxtTextureFormat format,
|
||||
nxtTextureUsageBit allowedUsage,
|
||||
uint32_t width,
|
||||
uint32_t height) {
|
||||
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
|
||||
return "unsupported format";
|
||||
}
|
||||
@ -176,21 +180,17 @@ namespace utils {
|
||||
|
||||
ComPtr<IDXGISwapChain1> swapChain1;
|
||||
ASSERT_SUCCESS(mFactory->CreateSwapChainForHwnd(
|
||||
mCommandQueue.Get(),
|
||||
mWindow,
|
||||
&swapChainDesc,
|
||||
nullptr,
|
||||
nullptr,
|
||||
&swapChain1
|
||||
));
|
||||
mCommandQueue.Get(), mWindow, &swapChainDesc, nullptr, nullptr, &swapChain1));
|
||||
ASSERT_SUCCESS(swapChain1.As(&mSwapChain));
|
||||
|
||||
for (uint32_t n = 0; n < kFrameCount; ++n) {
|
||||
ASSERT_SUCCESS(mSwapChain->GetBuffer(n, IID_PPV_ARGS(&mRenderTargetResources[n])));
|
||||
}
|
||||
|
||||
// Get the initial render target and arbitrarily choose a "previous" render target that's different
|
||||
mPreviousRenderTargetIndex = mRenderTargetIndex = mSwapChain->GetCurrentBackBufferIndex();
|
||||
// Get the initial render target and arbitrarily choose a "previous" render target
|
||||
// that's different
|
||||
mPreviousRenderTargetIndex = mRenderTargetIndex =
|
||||
mSwapChain->GetCurrentBackBufferIndex();
|
||||
mPreviousRenderTargetIndex = mRenderTargetIndex == 0 ? 1 : 0;
|
||||
|
||||
// Initial the serial for all render targets
|
||||
@ -220,7 +220,8 @@ namespace utils {
|
||||
backend::d3d12::OpenCommandList(mBackendDevice, &commandList);
|
||||
|
||||
D3D12_RESOURCE_BARRIER resourceBarrier;
|
||||
resourceBarrier.Transition.pResource = mRenderTargetResources[mPreviousRenderTargetIndex].Get();
|
||||
resourceBarrier.Transition.pResource =
|
||||
mRenderTargetResources[mPreviousRenderTargetIndex].Get();
|
||||
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
|
||||
resourceBarrier.Transition.StateAfter = mRenderTargetResourceState;
|
||||
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
||||
@ -228,7 +229,7 @@ namespace utils {
|
||||
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
|
||||
commandList->ResourceBarrier(1, &resourceBarrier);
|
||||
ASSERT_SUCCESS(commandList->Close());
|
||||
backend::d3d12::ExecuteCommandLists(mBackendDevice, { commandList.Get() });
|
||||
backend::d3d12::ExecuteCommandLists(mBackendDevice, {commandList.Get()});
|
||||
}
|
||||
|
||||
backend::d3d12::NextSerial(mBackendDevice);
|
||||
@ -237,11 +238,14 @@ namespace utils {
|
||||
mRenderTargetIndex = mSwapChain->GetCurrentBackBufferIndex();
|
||||
|
||||
// If the next render target is not ready to be rendered yet, wait until it is ready.
|
||||
// If the last completed serial is less than the last requested serial for this render target,
|
||||
// then the commands previously executed on this render target have not yet completed
|
||||
backend::d3d12::WaitForSerial(mBackendDevice, mLastSerialRenderTargetWasUsed[mRenderTargetIndex]);
|
||||
// If the last completed serial is less than the last requested serial for this render
|
||||
// target, then the commands previously executed on this render target have not yet
|
||||
// completed
|
||||
backend::d3d12::WaitForSerial(mBackendDevice,
|
||||
mLastSerialRenderTargetWasUsed[mRenderTargetIndex]);
|
||||
|
||||
mLastSerialRenderTargetWasUsed[mRenderTargetIndex] = backend::d3d12::GetSerial(mBackendDevice);
|
||||
mLastSerialRenderTargetWasUsed[mRenderTargetIndex] =
|
||||
backend::d3d12::GetSerial(mBackendDevice);
|
||||
|
||||
return NXT_SWAP_CHAIN_NO_ERROR;
|
||||
}
|
||||
@ -256,11 +260,8 @@ namespace utils {
|
||||
void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override {
|
||||
mFactory = CreateFactory();
|
||||
ASSERT(GetHardwareAdapter(mFactory.Get(), &mHardwareAdapter));
|
||||
ASSERT_SUCCESS(D3D12CreateDevice(
|
||||
mHardwareAdapter.Get(),
|
||||
D3D_FEATURE_LEVEL_11_0,
|
||||
IID_PPV_ARGS(&mD3d12Device)
|
||||
));
|
||||
ASSERT_SUCCESS(D3D12CreateDevice(mHardwareAdapter.Get(), D3D_FEATURE_LEVEL_11_0,
|
||||
IID_PPV_ARGS(&mD3d12Device)));
|
||||
|
||||
backend::d3d12::Init(mD3d12Device, procs, device);
|
||||
mBackendDevice = *device;
|
||||
@ -291,14 +292,16 @@ namespace utils {
|
||||
|
||||
static bool GetHardwareAdapter(IDXGIFactory4* factory, IDXGIAdapter1** hardwareAdapter) {
|
||||
*hardwareAdapter = nullptr;
|
||||
for (uint32_t adapterIndex = 0; ; ++adapterIndex) {
|
||||
for (uint32_t adapterIndex = 0;; ++adapterIndex) {
|
||||
IDXGIAdapter1* adapter = nullptr;
|
||||
if (factory->EnumAdapters1(adapterIndex, &adapter) == DXGI_ERROR_NOT_FOUND) {
|
||||
break; // No more adapters to enumerate.
|
||||
}
|
||||
|
||||
// Check to see if the adapter supports Direct3D 12, but don't create the actual device yet.
|
||||
if (SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr))) {
|
||||
// Check to see if the adapter supports Direct3D 12, but don't create the actual
|
||||
// device yet.
|
||||
if (SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0,
|
||||
_uuidof(ID3D12Device), nullptr))) {
|
||||
*hardwareAdapter = adapter;
|
||||
return true;
|
||||
}
|
||||
@ -312,4 +315,4 @@ namespace utils {
|
||||
return new D3D12Binding;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -22,16 +22,14 @@
|
||||
#include "GLFW/glfw3.h"
|
||||
#include "GLFW/glfw3native.h"
|
||||
|
||||
#import <QuartzCore/CAMetalLayer.h>
|
||||
#import <Metal/Metal.h>
|
||||
#import <QuartzCore/CAMetalLayer.h>
|
||||
|
||||
namespace backend {
|
||||
namespace metal {
|
||||
namespace backend { namespace metal {
|
||||
void Init(id<MTLDevice> metalDevice, nxtProcTable* procs, nxtDevice* device);
|
||||
void SetNextDrawable(nxtDevice device, id<CAMetalDrawable> drawable);
|
||||
void Present(nxtDevice device);
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
namespace utils {
|
||||
class SwapChainImplMTL : SwapChainImpl {
|
||||
@ -51,8 +49,7 @@ namespace utils {
|
||||
id<CAMetalDrawable> mCurrentDrawable = nil;
|
||||
id<MTLTexture> mCurrentTexture = nil;
|
||||
|
||||
SwapChainImplMTL(id nsWindow)
|
||||
: mNsWindow(nsWindow) {
|
||||
SwapChainImplMTL(id nsWindow) : mNsWindow(nsWindow) {
|
||||
}
|
||||
|
||||
~SwapChainImplMTL() {
|
||||
@ -68,8 +65,10 @@ namespace utils {
|
||||
mCommandQueue = [mMtlDevice newCommandQueue];
|
||||
}
|
||||
|
||||
nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit,
|
||||
uint32_t width, uint32_t height) {
|
||||
nxtSwapChainError Configure(nxtTextureFormat format,
|
||||
nxtTextureUsageBit,
|
||||
uint32_t width,
|
||||
uint32_t height) {
|
||||
if (format != NXT_TEXTURE_FORMAT_B8_G8_R8_A8_UNORM) {
|
||||
return "unsupported format";
|
||||
}
|
||||
@ -77,19 +76,19 @@ namespace utils {
|
||||
ASSERT(height > 0);
|
||||
|
||||
NSView* contentView = [mNsWindow contentView];
|
||||
[contentView setWantsLayer: YES];
|
||||
[contentView setWantsLayer:YES];
|
||||
|
||||
CGSize size = {};
|
||||
size.width = width;
|
||||
size.height = height;
|
||||
|
||||
mLayer = [CAMetalLayer layer];
|
||||
[mLayer setDevice: mMtlDevice];
|
||||
[mLayer setPixelFormat: MTLPixelFormatBGRA8Unorm];
|
||||
[mLayer setFramebufferOnly: YES];
|
||||
[mLayer setDrawableSize: size];
|
||||
[mLayer setDevice:mMtlDevice];
|
||||
[mLayer setPixelFormat:MTLPixelFormatBGRA8Unorm];
|
||||
[mLayer setFramebufferOnly:YES];
|
||||
[mLayer setDrawableSize:size];
|
||||
|
||||
[contentView setLayer: mLayer];
|
||||
[contentView setLayer:mLayer];
|
||||
|
||||
return NXT_SWAP_CHAIN_NO_ERROR;
|
||||
}
|
||||
@ -110,7 +109,7 @@ namespace utils {
|
||||
|
||||
nxtSwapChainError Present() {
|
||||
id<MTLCommandBuffer> commandBuffer = [mCommandQueue commandBuffer];
|
||||
[commandBuffer presentDrawable: mCurrentDrawable];
|
||||
[commandBuffer presentDrawable:mCurrentDrawable];
|
||||
[commandBuffer commit];
|
||||
|
||||
return NXT_SWAP_CHAIN_NO_ERROR;
|
||||
@ -149,5 +148,4 @@ namespace utils {
|
||||
BackendBinding* CreateMetalBinding() {
|
||||
return new MetalBinding;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,7 +25,9 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
void FillShaderModuleBuilder(const nxt::ShaderModuleBuilder& builder, nxt::ShaderStage stage, const char* source) {
|
||||
void FillShaderModuleBuilder(const nxt::ShaderModuleBuilder& builder,
|
||||
nxt::ShaderStage stage,
|
||||
const char* source) {
|
||||
shaderc::Compiler compiler;
|
||||
shaderc::CompileOptions options;
|
||||
|
||||
@ -60,7 +62,8 @@ namespace utils {
|
||||
|
||||
#ifdef DUMP_SPIRV_ASSEMBLY
|
||||
{
|
||||
auto resultAsm = compiler.CompileGlslToSpvAssembly(source, strlen(source), kind, "myshader?", options);
|
||||
auto resultAsm = compiler.CompileGlslToSpvAssembly(source, strlen(source), kind,
|
||||
"myshader?", options);
|
||||
size_t sizeAsm = (resultAsm.cend() - resultAsm.cbegin());
|
||||
|
||||
char* buffer = reinterpret_cast<char*>(malloc(sizeAsm + 1));
|
||||
@ -86,13 +89,18 @@ namespace utils {
|
||||
#endif
|
||||
}
|
||||
|
||||
nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source) {
|
||||
nxt::ShaderModule CreateShaderModule(const nxt::Device& device,
|
||||
nxt::ShaderStage stage,
|
||||
const char* source) {
|
||||
nxt::ShaderModuleBuilder builder = device.CreateShaderModuleBuilder();
|
||||
FillShaderModuleBuilder(builder, stage, source);
|
||||
return builder.GetResult();
|
||||
}
|
||||
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage) {
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device,
|
||||
const void* data,
|
||||
uint32_t size,
|
||||
nxt::BufferUsageBit usage) {
|
||||
nxt::Buffer buffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | usage)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
|
||||
@ -103,4 +111,4 @@ namespace utils {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -18,12 +18,23 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
void FillShaderModuleBuilder(const nxt::ShaderModuleBuilder& builder, nxt::ShaderStage stage, const char* source);
|
||||
nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source);
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage);
|
||||
void FillShaderModuleBuilder(const nxt::ShaderModuleBuilder& builder,
|
||||
nxt::ShaderStage stage,
|
||||
const char* source);
|
||||
nxt::ShaderModule CreateShaderModule(const nxt::Device& device,
|
||||
nxt::ShaderStage stage,
|
||||
const char* source);
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device,
|
||||
const void* data,
|
||||
uint32_t size,
|
||||
nxt::BufferUsageBit usage);
|
||||
|
||||
template<typename T>
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, nxt::BufferUsageBit usage, std::initializer_list<T> data) {
|
||||
return CreateFrozenBufferFromData(device, data.begin(), uint32_t(sizeof(T) * data.size()), usage);
|
||||
template <typename T>
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device,
|
||||
nxt::BufferUsageBit usage,
|
||||
std::initializer_list<T> data) {
|
||||
return CreateFrozenBufferFromData(device, data.begin(), uint32_t(sizeof(T) * data.size()),
|
||||
usage);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
@ -14,11 +14,9 @@
|
||||
|
||||
#include "utils/BackendBinding.h"
|
||||
|
||||
namespace backend {
|
||||
namespace null {
|
||||
namespace backend { namespace null {
|
||||
void Init(nxtProcTable* procs, nxtDevice* device);
|
||||
}
|
||||
}
|
||||
}} // namespace backend::null
|
||||
|
||||
namespace utils {
|
||||
|
||||
@ -37,9 +35,8 @@ namespace utils {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
BackendBinding* CreateNullBinding() {
|
||||
return new NullBinding;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -19,15 +19,15 @@
|
||||
#include "nxt/nxt_wsi.h"
|
||||
#include "utils/SwapChainImpl.h"
|
||||
|
||||
#include <cstdio>
|
||||
// Glad needs to be included before GLFW otherwise it complain that GL.h was already included
|
||||
#include "glad/glad.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
namespace backend {
|
||||
namespace opengl {
|
||||
namespace backend { namespace opengl {
|
||||
void Init(void* (*getProc)(const char*), nxtProcTable* procs, nxtDevice* device);
|
||||
}
|
||||
}
|
||||
}} // namespace backend::opengl
|
||||
|
||||
namespace utils {
|
||||
class SwapChainImplGL : SwapChainImpl {
|
||||
@ -45,8 +45,7 @@ namespace utils {
|
||||
GLuint mBackFBO = 0;
|
||||
GLuint mBackTexture = 0;
|
||||
|
||||
SwapChainImplGL(GLFWwindow* window)
|
||||
: mWindow(window) {
|
||||
SwapChainImplGL(GLFWwindow* window) : mWindow(window) {
|
||||
}
|
||||
|
||||
~SwapChainImplGL() {
|
||||
@ -60,17 +59,18 @@ namespace utils {
|
||||
void Init(nxtWSIContextGL*) {
|
||||
glGenTextures(1, &mBackTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mBackTexture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
|
||||
glGenFramebuffers(1, &mBackFBO);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
|
||||
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||
GL_TEXTURE_2D, mBackTexture, 0);
|
||||
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
||||
mBackTexture, 0);
|
||||
}
|
||||
|
||||
nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit,
|
||||
uint32_t width, uint32_t height) {
|
||||
nxtSwapChainError Configure(nxtTextureFormat format,
|
||||
nxtTextureUsageBit,
|
||||
uint32_t width,
|
||||
uint32_t height) {
|
||||
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
|
||||
return "unsupported format";
|
||||
}
|
||||
@ -81,8 +81,8 @@ namespace utils {
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, mBackTexture);
|
||||
// Reallocate the texture
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
nullptr);
|
||||
|
||||
return NXT_SWAP_CHAIN_NO_ERROR;
|
||||
}
|
||||
@ -95,8 +95,8 @@ namespace utils {
|
||||
nxtSwapChainError Present() {
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glBlitFramebuffer(0, 0, mWidth, mHeight, 0, 0, mWidth, mHeight,
|
||||
GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
glBlitFramebuffer(0, 0, mWidth, mHeight, 0, 0, mWidth, mHeight, GL_COLOR_BUFFER_BIT,
|
||||
GL_NEAREST);
|
||||
glfwSwapBuffers(mWindow);
|
||||
|
||||
return NXT_SWAP_CHAIN_NO_ERROR;
|
||||
@ -106,21 +106,22 @@ namespace utils {
|
||||
class OpenGLBinding : public BackendBinding {
|
||||
public:
|
||||
void SetupGLFWWindowHints() override {
|
||||
#if defined(NXT_PLATFORM_APPLE)
|
||||
#if defined(NXT_PLATFORM_APPLE)
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
#else
|
||||
#else
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override {
|
||||
glfwMakeContextCurrent(mWindow);
|
||||
backend::opengl::Init(reinterpret_cast<void*(*)(const char*)>(glfwGetProcAddress), procs, device);
|
||||
backend::opengl::Init(reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress),
|
||||
procs, device);
|
||||
|
||||
mBackendDevice = *device;
|
||||
}
|
||||
@ -145,4 +146,4 @@ namespace utils {
|
||||
return new OpenGLBinding;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -18,22 +18,21 @@
|
||||
namespace utils {
|
||||
class SwapChainImpl {
|
||||
protected:
|
||||
template<class TImpl, typename TWSIContext>
|
||||
template <class TImpl, typename TWSIContext>
|
||||
static nxtSwapChainImplementation GenerateSwapChainImplementation() {
|
||||
nxtSwapChainImplementation impl = {};
|
||||
impl.Init = [](void* userData, void* wsiContext) {
|
||||
auto* ctx = reinterpret_cast<TWSIContext*>(wsiContext);
|
||||
reinterpret_cast<TImpl*>(userData)->Init(ctx);
|
||||
};
|
||||
impl.Destroy = [](void* userData) {
|
||||
delete reinterpret_cast<TImpl*>(userData);
|
||||
};
|
||||
impl.Configure = [](void* userData, nxtTextureFormat format, nxtTextureUsageBit allowedUsage, uint32_t width, uint32_t height) {
|
||||
return reinterpret_cast<TImpl*>(userData)->Configure(format, allowedUsage, width, height);
|
||||
impl.Destroy = [](void* userData) { delete reinterpret_cast<TImpl*>(userData); };
|
||||
impl.Configure = [](void* userData, nxtTextureFormat format,
|
||||
nxtTextureUsageBit allowedUsage, uint32_t width, uint32_t height) {
|
||||
return reinterpret_cast<TImpl*>(userData)->Configure(format, allowedUsage, width,
|
||||
height);
|
||||
};
|
||||
impl.GetNextTexture = [](void* userData, nxtSwapChainNextTexture* nextTexture) {
|
||||
return reinterpret_cast<TImpl*>(userData)->GetNextTexture(
|
||||
nextTexture);
|
||||
return reinterpret_cast<TImpl*>(userData)->GetNextTexture(nextTexture);
|
||||
};
|
||||
impl.Present = [](void* userData) {
|
||||
return reinterpret_cast<TImpl*>(userData)->Present();
|
||||
@ -41,6 +40,6 @@ namespace utils {
|
||||
return impl;
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace utils
|
||||
|
||||
#endif // UTILS_SWAPCHAINIMPL_H_
|
||||
|
@ -15,25 +15,25 @@
|
||||
#include "common/Platform.h"
|
||||
|
||||
#if defined(NXT_PLATFORM_WINDOWS)
|
||||
#include <Windows.h>
|
||||
# include <Windows.h>
|
||||
#elif defined(NXT_PLATFORM_POSIX)
|
||||
#include <unistd.h>
|
||||
# include <unistd.h>
|
||||
#else
|
||||
#error "Unsupported platform."
|
||||
# error "Unsupported platform."
|
||||
#endif
|
||||
|
||||
namespace utils {
|
||||
|
||||
#if defined(NXT_PLATFORM_WINDOWS)
|
||||
#if defined(NXT_PLATFORM_WINDOWS)
|
||||
void USleep(unsigned int usecs) {
|
||||
Sleep(static_cast<DWORD>(usecs / 1000));
|
||||
}
|
||||
#elif defined(NXT_PLATFORM_POSIX)
|
||||
#elif defined(NXT_PLATFORM_POSIX)
|
||||
void USleep(unsigned int usecs) {
|
||||
usleep(usecs);
|
||||
}
|
||||
#else
|
||||
#error "Implement USleep for your platform."
|
||||
#endif
|
||||
#else
|
||||
# error "Implement USleep for your platform."
|
||||
#endif
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -15,5 +15,4 @@
|
||||
namespace utils {
|
||||
|
||||
void USleep(unsigned int usecs);
|
||||
|
||||
}
|
||||
|
@ -17,11 +17,9 @@
|
||||
#include "nxt/nxt_wsi.h"
|
||||
#include "utils/SwapChainImpl.h"
|
||||
|
||||
namespace backend {
|
||||
namespace vulkan {
|
||||
namespace backend { namespace vulkan {
|
||||
void Init(nxtProcTable* procs, nxtDevice* device);
|
||||
}
|
||||
}
|
||||
}} // namespace backend::vulkan
|
||||
|
||||
namespace utils {
|
||||
|
||||
@ -36,8 +34,7 @@ namespace utils {
|
||||
private:
|
||||
GLFWwindow* window = nullptr;
|
||||
|
||||
SwapChainImplVulkan(GLFWwindow* window)
|
||||
: window(window) {
|
||||
SwapChainImplVulkan(GLFWwindow* window) : window(window) {
|
||||
}
|
||||
|
||||
~SwapChainImplVulkan() {
|
||||
@ -83,9 +80,8 @@ namespace utils {
|
||||
nxtSwapChainImplementation mSwapchainImpl = {};
|
||||
};
|
||||
|
||||
|
||||
BackendBinding* CreateVulkanBinding() {
|
||||
return new VulkanBinding;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace utils
|
||||
|
@ -14,8 +14,7 @@
|
||||
|
||||
#include "wire/TerribleCommandBuffer.h"
|
||||
|
||||
namespace nxt {
|
||||
namespace wire {
|
||||
namespace nxt { namespace wire {
|
||||
|
||||
TerribleCommandBuffer::TerribleCommandBuffer() {
|
||||
}
|
||||
@ -48,5 +47,4 @@ namespace wire {
|
||||
mOffset = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace nxt::wire
|
||||
|
@ -19,10 +19,9 @@
|
||||
|
||||
#include "wire/Wire.h"
|
||||
|
||||
namespace nxt {
|
||||
namespace wire {
|
||||
namespace nxt { namespace wire {
|
||||
|
||||
class TerribleCommandBuffer : public CommandSerializer {
|
||||
class TerribleCommandBuffer : public CommandSerializer {
|
||||
public:
|
||||
TerribleCommandBuffer();
|
||||
TerribleCommandBuffer(CommandHandler* handler);
|
||||
@ -36,9 +35,8 @@ class TerribleCommandBuffer : public CommandSerializer {
|
||||
CommandHandler* mHandler = nullptr;
|
||||
size_t mOffset = 0;
|
||||
uint8_t mBuffer[10000000];
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace nxt::wire
|
||||
|
||||
#endif // WIRE_TERRIBLE_COMMAND_BUFFER_H_
|
||||
|
@ -19,8 +19,7 @@
|
||||
|
||||
#include "nxt/nxt.h"
|
||||
|
||||
namespace nxt {
|
||||
namespace wire {
|
||||
namespace nxt { namespace wire {
|
||||
|
||||
class CommandSerializer {
|
||||
public:
|
||||
@ -35,10 +34,13 @@ namespace wire {
|
||||
virtual const uint8_t* HandleCommands(const uint8_t* commands, size_t size) = 0;
|
||||
};
|
||||
|
||||
CommandHandler* NewClientDevice(nxtProcTable* procs, nxtDevice* device, CommandSerializer* serializer);
|
||||
CommandHandler* NewServerCommandHandler(nxtDevice device, const nxtProcTable& procs, CommandSerializer* serializer);
|
||||
CommandHandler* NewClientDevice(nxtProcTable* procs,
|
||||
nxtDevice* device,
|
||||
CommandSerializer* serializer);
|
||||
CommandHandler* NewServerCommandHandler(nxtDevice device,
|
||||
const nxtProcTable& procs,
|
||||
CommandSerializer* serializer);
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace nxt::wire
|
||||
|
||||
#endif // WIRE_WIRE_H_
|
||||
|
@ -14,8 +14,7 @@
|
||||
|
||||
#include "wire/WireCmd.h"
|
||||
|
||||
namespace nxt {
|
||||
namespace wire {
|
||||
namespace nxt { namespace wire {
|
||||
|
||||
size_t ReturnDeviceErrorCallbackCmd::GetRequiredSize() const {
|
||||
return sizeof(*this) + messageStrlen + 1;
|
||||
@ -45,5 +44,4 @@ namespace wire {
|
||||
return this + 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace nxt::wire
|
||||
|
@ -17,8 +17,7 @@
|
||||
|
||||
#include "wire/WireCmd_autogen.h"
|
||||
|
||||
namespace nxt {
|
||||
namespace wire {
|
||||
namespace nxt { namespace wire {
|
||||
|
||||
struct ReturnDeviceErrorCallbackCmd {
|
||||
wire::ReturnWireCmd commandId = ReturnWireCmd::DeviceErrorCallback;
|
||||
@ -55,7 +54,6 @@ namespace wire {
|
||||
const void* GetData() const;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}} // namespace nxt::wire
|
||||
|
||||
#endif // WIRE_WIRECMD_H_
|
||||
|
Loading…
x
Reference in New Issue
Block a user