Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 1 | #ifndef COMMON_H_
|
| 2 | #define COMMON_H_
|
| 3 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4 | #include "VmaUsage.h"
|
| 5 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 6 | #ifdef _WIN32
|
| 7 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 8 | #include <iostream>
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 9 | #include <fstream>
|
| 10 | #include <vector>
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 11 | #include <memory>
|
| 12 | #include <algorithm>
|
| 13 | #include <numeric>
|
| 14 | #include <array>
|
| 15 | #include <type_traits>
|
| 16 | #include <utility>
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 17 | #include <chrono>
|
| 18 | #include <string>
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 19 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 20 | #include <cassert>
|
| 21 | #include <cstdlib>
|
| 22 | #include <cstdio>
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 23 | #include <cstdarg>
|
| 24 |
|
| 25 | typedef std::chrono::high_resolution_clock::time_point time_point;
|
| 26 | typedef std::chrono::high_resolution_clock::duration duration;
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 27 |
|
| 28 | #define ERR_GUARD_VULKAN(Expr) do { VkResult res__ = (Expr); if (res__ < 0) assert(0); } while(0)
|
| 29 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 30 | extern VkPhysicalDevice g_hPhysicalDevice;
|
| 31 | extern VkDevice g_hDevice;
|
| 32 | extern VmaAllocator g_hAllocator;
|
| 33 | extern bool g_MemoryAliasingWarningEnabled;
|
| 34 |
|
| 35 | inline float ToFloatSeconds(duration d)
|
| 36 | {
|
| 37 | return std::chrono::duration_cast<std::chrono::duration<float>>(d).count();
|
| 38 | }
|
| 39 |
|
| 40 | template <typename T>
|
| 41 | inline T ceil_div(T x, T y)
|
| 42 | {
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame^] | 43 | return (x+y-1) / y;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 44 | }
|
| 45 |
|
| 46 | template <typename T>
|
| 47 | static inline T align_up(T val, T align)
|
| 48 | {
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame^] | 49 | return (val + align - 1) / align * align;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 50 | }
|
| 51 |
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame^] | 52 | static const float PI = 3.14159265358979323846264338327950288419716939937510582f;
|
| 53 |
|
| 54 | struct vec3
|
| 55 | {
|
| 56 | float x, y, z;
|
| 57 |
|
| 58 | vec3() { }
|
| 59 | vec3(float x, float y, float z) : x(x), y(y), z(z) { }
|
| 60 |
|
| 61 | float& operator[](uint32_t index) { return *(&x + index); }
|
| 62 | const float& operator[](uint32_t index) const { return *(&x + index); }
|
| 63 |
|
| 64 | vec3 operator+(const vec3& rhs) const { return vec3(x + rhs.x, y + rhs.y, z + rhs.z); }
|
| 65 | vec3 operator-(const vec3& rhs) const { return vec3(x - rhs.x, y - rhs.y, z - rhs.z); }
|
| 66 | vec3 operator*(float s) const { return vec3(x * s, y * s, z * s); }
|
| 67 |
|
| 68 | vec3 Normalized() const
|
| 69 | {
|
| 70 | return (*this) * (1.f / sqrt(x * x + y * y + z * z));
|
| 71 | }
|
| 72 | };
|
| 73 |
|
| 74 | inline float Dot(const vec3& lhs, const vec3& rhs)
|
| 75 | {
|
| 76 | return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
|
| 77 | }
|
| 78 | inline vec3 Cross(const vec3& lhs, const vec3& rhs)
|
| 79 | {
|
| 80 | return vec3(
|
| 81 | lhs.y * rhs.z - lhs.z * rhs.y,
|
| 82 | lhs.z * rhs.x - lhs.x * rhs.z,
|
| 83 | lhs.x * rhs.y - lhs.y * rhs.x);
|
| 84 | }
|
| 85 |
|
| 86 | struct vec4
|
| 87 | {
|
| 88 | float x, y, z, w;
|
| 89 |
|
| 90 | vec4() { }
|
| 91 | vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) { }
|
| 92 | vec4(const vec3& v, float w) : x(v.x), y(v.y), z(v.z), w(w) { }
|
| 93 |
|
| 94 | float& operator[](uint32_t index) { return *(&x + index); }
|
| 95 | const float& operator[](uint32_t index) const { return *(&x + index); }
|
| 96 | };
|
| 97 |
|
| 98 | struct mat4
|
| 99 | {
|
| 100 | union
|
| 101 | {
|
| 102 | struct
|
| 103 | {
|
| 104 | float _11, _12, _13, _14;
|
| 105 | float _21, _22, _23, _24;
|
| 106 | float _31, _32, _33, _34;
|
| 107 | float _41, _42, _43, _44;
|
| 108 | };
|
| 109 | float m[4][4]; // [row][column]
|
| 110 | };
|
| 111 |
|
| 112 | mat4() { }
|
| 113 |
|
| 114 | mat4(
|
| 115 | float _11, float _12, float _13, float _14,
|
| 116 | float _21, float _22, float _23, float _24,
|
| 117 | float _31, float _32, float _33, float _34,
|
| 118 | float _41, float _42, float _43, float _44) :
|
| 119 | _11(_11), _12(_12), _13(_13), _14(_14),
|
| 120 | _21(_21), _22(_22), _23(_23), _24(_24),
|
| 121 | _31(_31), _32(_32), _33(_33), _34(_34),
|
| 122 | _41(_41), _42(_42), _43(_43), _44(_44)
|
| 123 | {
|
| 124 | }
|
| 125 |
|
| 126 | mat4(
|
| 127 | const vec4& row1,
|
| 128 | const vec4& row2,
|
| 129 | const vec4& row3,
|
| 130 | const vec4& row4) :
|
| 131 | _11(row1.x), _12(row1.y), _13(row1.z), _14(row1.w),
|
| 132 | _21(row2.x), _22(row2.y), _23(row2.z), _24(row2.w),
|
| 133 | _31(row3.x), _32(row3.y), _33(row3.z), _34(row3.w),
|
| 134 | _41(row4.x), _42(row4.y), _43(row4.z), _44(row4.w)
|
| 135 | {
|
| 136 | }
|
| 137 |
|
| 138 | mat4 operator*(const mat4 &rhs) const
|
| 139 | {
|
| 140 | return mat4(
|
| 141 | _11 * rhs._11 + _12 * rhs._21 + _13 * rhs._31 + _14 * rhs._41,
|
| 142 | _11 * rhs._12 + _12 * rhs._22 + _13 * rhs._32 + _14 * rhs._42,
|
| 143 | _11 * rhs._13 + _12 * rhs._23 + _13 * rhs._33 + _14 * rhs._43,
|
| 144 | _11 * rhs._14 + _12 * rhs._24 + _13 * rhs._34 + _14 * rhs._44,
|
| 145 |
|
| 146 | _21 * rhs._11 + _22 * rhs._21 + _23 * rhs._31 + _24 * rhs._41,
|
| 147 | _21 * rhs._12 + _22 * rhs._22 + _23 * rhs._32 + _24 * rhs._42,
|
| 148 | _21 * rhs._13 + _22 * rhs._23 + _23 * rhs._33 + _24 * rhs._43,
|
| 149 | _21 * rhs._14 + _22 * rhs._24 + _23 * rhs._34 + _24 * rhs._44,
|
| 150 |
|
| 151 | _31 * rhs._11 + _32 * rhs._21 + _33 * rhs._31 + _34 * rhs._41,
|
| 152 | _31 * rhs._12 + _32 * rhs._22 + _33 * rhs._32 + _34 * rhs._42,
|
| 153 | _31 * rhs._13 + _32 * rhs._23 + _33 * rhs._33 + _34 * rhs._43,
|
| 154 | _31 * rhs._14 + _32 * rhs._24 + _33 * rhs._34 + _34 * rhs._44,
|
| 155 |
|
| 156 | _41 * rhs._11 + _42 * rhs._21 + _43 * rhs._31 + _44 * rhs._41,
|
| 157 | _41 * rhs._12 + _42 * rhs._22 + _43 * rhs._32 + _44 * rhs._42,
|
| 158 | _41 * rhs._13 + _42 * rhs._23 + _43 * rhs._33 + _44 * rhs._43,
|
| 159 | _41 * rhs._14 + _42 * rhs._24 + _43 * rhs._34 + _44 * rhs._44);
|
| 160 | }
|
| 161 |
|
| 162 | static mat4 RotationY(float angle)
|
| 163 | {
|
| 164 | const float s = sin(angle), c = cos(angle);
|
| 165 | return mat4(
|
| 166 | c, 0.f, -s, 0.f,
|
| 167 | 0.f, 1.f, 0.f, 0.f,
|
| 168 | s, 0.f, c, 0.f,
|
| 169 | 0.f, 0.f, 0.f, 1.f);
|
| 170 | }
|
| 171 |
|
| 172 | static mat4 Perspective(float fovY, float aspectRatio, float zNear, float zFar)
|
| 173 | {
|
| 174 | float yScale = 1.0f / tan(fovY * 0.5f);
|
| 175 | float xScale = yScale / aspectRatio;
|
| 176 | return mat4(
|
| 177 | xScale, 0.0f, 0.0f, 0.0f,
|
| 178 | 0.0f, yScale, 0.0f, 0.0f,
|
| 179 | 0.0f, 0.0f, zFar / (zFar - zNear), 1.0f,
|
| 180 | 0.0f, 0.0f, -zNear * zFar / (zFar - zNear), 0.0f);
|
| 181 | }
|
| 182 |
|
| 183 | static mat4 LookAt(vec3 at, vec3 eye, vec3 up)
|
| 184 | {
|
| 185 | vec3 zAxis = (at - eye).Normalized();
|
| 186 | vec3 xAxis = Cross(up, zAxis).Normalized();
|
| 187 | vec3 yAxis = Cross(zAxis, xAxis);
|
| 188 | return mat4(
|
| 189 | xAxis.x, yAxis.x, zAxis.x, 0.0f,
|
| 190 | xAxis.y, yAxis.y, zAxis.y, 0.0f,
|
| 191 | xAxis.z, yAxis.z, zAxis.z, 0.0f,
|
| 192 | -Dot(xAxis, eye), -Dot(yAxis, eye), -Dot(zAxis, eye), 1.0f);
|
| 193 | }
|
| 194 | };
|
| 195 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 196 | class RandomNumberGenerator
|
| 197 | {
|
| 198 | public:
|
| 199 | RandomNumberGenerator() : m_Value{GetTickCount()} {}
|
| 200 | RandomNumberGenerator(uint32_t seed) : m_Value{seed} { }
|
| 201 | void Seed(uint32_t seed) { m_Value = seed; }
|
| 202 | uint32_t Generate() { return GenerateFast() ^ (GenerateFast() >> 7); }
|
| 203 |
|
| 204 | private:
|
| 205 | uint32_t m_Value;
|
| 206 | uint32_t GenerateFast() { return m_Value = (m_Value * 196314165 + 907633515); }
|
| 207 | };
|
| 208 |
|
| 209 | void ReadFile(std::vector<char>& out, const char* fileName);
|
| 210 |
|
| 211 | enum class CONSOLE_COLOR
|
| 212 | {
|
| 213 | INFO,
|
| 214 | NORMAL,
|
| 215 | WARNING,
|
| 216 | ERROR_,
|
| 217 | COUNT
|
| 218 | };
|
| 219 |
|
| 220 | void SetConsoleColor(CONSOLE_COLOR color);
|
| 221 |
|
| 222 | void PrintMessage(CONSOLE_COLOR color, const char* msg);
|
| 223 | void PrintMessage(CONSOLE_COLOR color, const wchar_t* msg);
|
| 224 |
|
| 225 | inline void Print(const char* msg) { PrintMessage(CONSOLE_COLOR::NORMAL, msg); }
|
| 226 | inline void Print(const wchar_t* msg) { PrintMessage(CONSOLE_COLOR::NORMAL, msg); }
|
| 227 | inline void PrintWarning(const char* msg) { PrintMessage(CONSOLE_COLOR::WARNING, msg); }
|
| 228 | inline void PrintWarning(const wchar_t* msg) { PrintMessage(CONSOLE_COLOR::WARNING, msg); }
|
| 229 | inline void PrintError(const char* msg) { PrintMessage(CONSOLE_COLOR::ERROR_, msg); }
|
| 230 | inline void PrintError(const wchar_t* msg) { PrintMessage(CONSOLE_COLOR::ERROR_, msg); }
|
| 231 |
|
| 232 | void PrintMessageV(CONSOLE_COLOR color, const char* format, va_list argList);
|
| 233 | void PrintMessageV(CONSOLE_COLOR color, const wchar_t* format, va_list argList);
|
| 234 | void PrintMessageF(CONSOLE_COLOR color, const char* format, ...);
|
| 235 | void PrintMessageF(CONSOLE_COLOR color, const wchar_t* format, ...);
|
| 236 | void PrintWarningF(const char* format, ...);
|
| 237 | void PrintWarningF(const wchar_t* format, ...);
|
| 238 | void PrintErrorF(const char* format, ...);
|
| 239 | void PrintErrorF(const wchar_t* format, ...);
|
| 240 |
|
| 241 | void SaveFile(const wchar_t* filePath, const void* data, size_t dataSize);
|
| 242 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 243 | #endif // #ifdef _WIN32
|
| 244 |
|
| 245 | #endif
|