blob: f5954c56807ab17e115f75f2ce57ad7f97276f5b [file] [log] [blame]
Adam Sawickiae5c4662019-01-02 10:23:35 +01001//
2// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21//
22
Adam Sawickif1a793c2018-03-13 15:42:22 +010023#ifndef COMMON_H_
24#define COMMON_H_
25
Adam Sawickib8333fb2018-03-13 16:15:53 +010026#include "VmaUsage.h"
27
Adam Sawickif1a793c2018-03-13 15:42:22 +010028#ifdef _WIN32
29
Adam Sawickib8333fb2018-03-13 16:15:53 +010030#include <iostream>
Adam Sawickif1a793c2018-03-13 15:42:22 +010031#include <fstream>
32#include <vector>
Adam Sawickif1a793c2018-03-13 15:42:22 +010033#include <memory>
34#include <algorithm>
35#include <numeric>
36#include <array>
37#include <type_traits>
38#include <utility>
Adam Sawickib8333fb2018-03-13 16:15:53 +010039#include <chrono>
40#include <string>
Adam Sawickib8d34d52018-10-03 17:41:20 +020041#include <exception>
Adam Sawickif1a793c2018-03-13 15:42:22 +010042
Adam Sawickif1a793c2018-03-13 15:42:22 +010043#include <cassert>
44#include <cstdlib>
45#include <cstdio>
Adam Sawickib8333fb2018-03-13 16:15:53 +010046#include <cstdarg>
47
48typedef std::chrono::high_resolution_clock::time_point time_point;
49typedef std::chrono::high_resolution_clock::duration duration;
Adam Sawickif1a793c2018-03-13 15:42:22 +010050
Adam Sawickib8d34d52018-10-03 17:41:20 +020051#ifdef _DEBUG
52 #define TEST(expr) do { \
53 if(!(expr)) { \
54 assert(0 && #expr); \
55 } \
56 } while(0)
57#else
58 #define TEST(expr) do { \
59 if(!(expr)) { \
60 throw std::runtime_error("TEST FAILED: " #expr); \
61 } \
62 } while(0)
63#endif
64
65#define ERR_GUARD_VULKAN(expr) TEST((expr) >= 0)
Adam Sawickif1a793c2018-03-13 15:42:22 +010066
Adam Sawickib8333fb2018-03-13 16:15:53 +010067extern VkPhysicalDevice g_hPhysicalDevice;
68extern VkDevice g_hDevice;
69extern VmaAllocator g_hAllocator;
70extern bool g_MemoryAliasingWarningEnabled;
71
72inline float ToFloatSeconds(duration d)
73{
74 return std::chrono::duration_cast<std::chrono::duration<float>>(d).count();
75}
76
77template <typename T>
78inline T ceil_div(T x, T y)
79{
Adam Sawicki82c3f332018-06-11 15:27:33 +020080 return (x+y-1) / y;
Adam Sawickib8333fb2018-03-13 16:15:53 +010081}
Adam Sawickiff0f7b82018-10-18 14:44:05 +020082template <typename T>
83inline T round_div(T x, T y)
84{
85 return (x+y/(T)2) / y;
86}
Adam Sawickib8333fb2018-03-13 16:15:53 +010087
88template <typename T>
89static inline T align_up(T val, T align)
90{
Adam Sawicki82c3f332018-06-11 15:27:33 +020091 return (val + align - 1) / align * align;
Adam Sawickib8333fb2018-03-13 16:15:53 +010092}
93
Adam Sawicki82c3f332018-06-11 15:27:33 +020094static const float PI = 3.14159265358979323846264338327950288419716939937510582f;
95
96struct vec3
97{
98 float x, y, z;
99
100 vec3() { }
101 vec3(float x, float y, float z) : x(x), y(y), z(z) { }
102
103 float& operator[](uint32_t index) { return *(&x + index); }
104 const float& operator[](uint32_t index) const { return *(&x + index); }
105
106 vec3 operator+(const vec3& rhs) const { return vec3(x + rhs.x, y + rhs.y, z + rhs.z); }
107 vec3 operator-(const vec3& rhs) const { return vec3(x - rhs.x, y - rhs.y, z - rhs.z); }
108 vec3 operator*(float s) const { return vec3(x * s, y * s, z * s); }
109
110 vec3 Normalized() const
111 {
112 return (*this) * (1.f / sqrt(x * x + y * y + z * z));
113 }
114};
115
116inline float Dot(const vec3& lhs, const vec3& rhs)
117{
118 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
119}
120inline vec3 Cross(const vec3& lhs, const vec3& rhs)
121{
122 return vec3(
123 lhs.y * rhs.z - lhs.z * rhs.y,
124 lhs.z * rhs.x - lhs.x * rhs.z,
125 lhs.x * rhs.y - lhs.y * rhs.x);
126}
127
128struct vec4
129{
130 float x, y, z, w;
131
132 vec4() { }
133 vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) { }
134 vec4(const vec3& v, float w) : x(v.x), y(v.y), z(v.z), w(w) { }
135
136 float& operator[](uint32_t index) { return *(&x + index); }
137 const float& operator[](uint32_t index) const { return *(&x + index); }
138};
139
140struct mat4
141{
142 union
143 {
144 struct
145 {
146 float _11, _12, _13, _14;
147 float _21, _22, _23, _24;
148 float _31, _32, _33, _34;
149 float _41, _42, _43, _44;
150 };
151 float m[4][4]; // [row][column]
152 };
153
154 mat4() { }
155
156 mat4(
157 float _11, float _12, float _13, float _14,
158 float _21, float _22, float _23, float _24,
159 float _31, float _32, float _33, float _34,
160 float _41, float _42, float _43, float _44) :
161 _11(_11), _12(_12), _13(_13), _14(_14),
162 _21(_21), _22(_22), _23(_23), _24(_24),
163 _31(_31), _32(_32), _33(_33), _34(_34),
164 _41(_41), _42(_42), _43(_43), _44(_44)
165 {
166 }
167
168 mat4(
169 const vec4& row1,
170 const vec4& row2,
171 const vec4& row3,
172 const vec4& row4) :
173 _11(row1.x), _12(row1.y), _13(row1.z), _14(row1.w),
174 _21(row2.x), _22(row2.y), _23(row2.z), _24(row2.w),
175 _31(row3.x), _32(row3.y), _33(row3.z), _34(row3.w),
176 _41(row4.x), _42(row4.y), _43(row4.z), _44(row4.w)
177 {
178 }
179
180 mat4 operator*(const mat4 &rhs) const
181 {
182 return mat4(
183 _11 * rhs._11 + _12 * rhs._21 + _13 * rhs._31 + _14 * rhs._41,
184 _11 * rhs._12 + _12 * rhs._22 + _13 * rhs._32 + _14 * rhs._42,
185 _11 * rhs._13 + _12 * rhs._23 + _13 * rhs._33 + _14 * rhs._43,
186 _11 * rhs._14 + _12 * rhs._24 + _13 * rhs._34 + _14 * rhs._44,
187
188 _21 * rhs._11 + _22 * rhs._21 + _23 * rhs._31 + _24 * rhs._41,
189 _21 * rhs._12 + _22 * rhs._22 + _23 * rhs._32 + _24 * rhs._42,
190 _21 * rhs._13 + _22 * rhs._23 + _23 * rhs._33 + _24 * rhs._43,
191 _21 * rhs._14 + _22 * rhs._24 + _23 * rhs._34 + _24 * rhs._44,
192
193 _31 * rhs._11 + _32 * rhs._21 + _33 * rhs._31 + _34 * rhs._41,
194 _31 * rhs._12 + _32 * rhs._22 + _33 * rhs._32 + _34 * rhs._42,
195 _31 * rhs._13 + _32 * rhs._23 + _33 * rhs._33 + _34 * rhs._43,
196 _31 * rhs._14 + _32 * rhs._24 + _33 * rhs._34 + _34 * rhs._44,
197
198 _41 * rhs._11 + _42 * rhs._21 + _43 * rhs._31 + _44 * rhs._41,
199 _41 * rhs._12 + _42 * rhs._22 + _43 * rhs._32 + _44 * rhs._42,
200 _41 * rhs._13 + _42 * rhs._23 + _43 * rhs._33 + _44 * rhs._43,
201 _41 * rhs._14 + _42 * rhs._24 + _43 * rhs._34 + _44 * rhs._44);
202 }
203
204 static mat4 RotationY(float angle)
205 {
206 const float s = sin(angle), c = cos(angle);
207 return mat4(
208 c, 0.f, -s, 0.f,
209 0.f, 1.f, 0.f, 0.f,
210 s, 0.f, c, 0.f,
211 0.f, 0.f, 0.f, 1.f);
212 }
213
214 static mat4 Perspective(float fovY, float aspectRatio, float zNear, float zFar)
215 {
216 float yScale = 1.0f / tan(fovY * 0.5f);
217 float xScale = yScale / aspectRatio;
218 return mat4(
219 xScale, 0.0f, 0.0f, 0.0f,
220 0.0f, yScale, 0.0f, 0.0f,
221 0.0f, 0.0f, zFar / (zFar - zNear), 1.0f,
222 0.0f, 0.0f, -zNear * zFar / (zFar - zNear), 0.0f);
223 }
224
225 static mat4 LookAt(vec3 at, vec3 eye, vec3 up)
226 {
227 vec3 zAxis = (at - eye).Normalized();
228 vec3 xAxis = Cross(up, zAxis).Normalized();
229 vec3 yAxis = Cross(zAxis, xAxis);
230 return mat4(
231 xAxis.x, yAxis.x, zAxis.x, 0.0f,
232 xAxis.y, yAxis.y, zAxis.y, 0.0f,
233 xAxis.z, yAxis.z, zAxis.z, 0.0f,
234 -Dot(xAxis, eye), -Dot(yAxis, eye), -Dot(zAxis, eye), 1.0f);
235 }
236};
237
Adam Sawickib8333fb2018-03-13 16:15:53 +0100238class RandomNumberGenerator
239{
240public:
241 RandomNumberGenerator() : m_Value{GetTickCount()} {}
242 RandomNumberGenerator(uint32_t seed) : m_Value{seed} { }
243 void Seed(uint32_t seed) { m_Value = seed; }
244 uint32_t Generate() { return GenerateFast() ^ (GenerateFast() >> 7); }
245
246private:
247 uint32_t m_Value;
248 uint32_t GenerateFast() { return m_Value = (m_Value * 196314165 + 907633515); }
249};
250
Adam Sawicki0a607132018-08-24 11:18:41 +0200251// Wrapper for RandomNumberGenerator compatible with STL "UniformRandomNumberGenerator" idea.
252struct MyUniformRandomNumberGenerator
253{
254 typedef uint32_t result_type;
255 MyUniformRandomNumberGenerator(RandomNumberGenerator& gen) : m_Gen(gen) { }
256 static uint32_t min() { return 0; }
257 static uint32_t max() { return UINT32_MAX; }
258 uint32_t operator()() { return m_Gen.Generate(); }
259
260private:
261 RandomNumberGenerator& m_Gen;
262};
263
Adam Sawickib8333fb2018-03-13 16:15:53 +0100264void ReadFile(std::vector<char>& out, const char* fileName);
265
266enum class CONSOLE_COLOR
267{
268 INFO,
269 NORMAL,
270 WARNING,
271 ERROR_,
272 COUNT
273};
274
275void SetConsoleColor(CONSOLE_COLOR color);
276
277void PrintMessage(CONSOLE_COLOR color, const char* msg);
278void PrintMessage(CONSOLE_COLOR color, const wchar_t* msg);
279
280inline void Print(const char* msg) { PrintMessage(CONSOLE_COLOR::NORMAL, msg); }
281inline void Print(const wchar_t* msg) { PrintMessage(CONSOLE_COLOR::NORMAL, msg); }
282inline void PrintWarning(const char* msg) { PrintMessage(CONSOLE_COLOR::WARNING, msg); }
283inline void PrintWarning(const wchar_t* msg) { PrintMessage(CONSOLE_COLOR::WARNING, msg); }
284inline void PrintError(const char* msg) { PrintMessage(CONSOLE_COLOR::ERROR_, msg); }
285inline void PrintError(const wchar_t* msg) { PrintMessage(CONSOLE_COLOR::ERROR_, msg); }
286
287void PrintMessageV(CONSOLE_COLOR color, const char* format, va_list argList);
288void PrintMessageV(CONSOLE_COLOR color, const wchar_t* format, va_list argList);
289void PrintMessageF(CONSOLE_COLOR color, const char* format, ...);
290void PrintMessageF(CONSOLE_COLOR color, const wchar_t* format, ...);
291void PrintWarningF(const char* format, ...);
292void PrintWarningF(const wchar_t* format, ...);
293void PrintErrorF(const char* format, ...);
294void PrintErrorF(const wchar_t* format, ...);
295
296void SaveFile(const wchar_t* filePath, const void* data, size_t dataSize);
297
Adam Sawickif1a793c2018-03-13 15:42:22 +0100298#endif // #ifdef _WIN32
299
300#endif