SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
VulkanInternal.hpp
Go to the documentation of this file.
1#ifndef _VULKANINTERNAL_HPP_
2#define _VULKANINTERNAL_HPP_
3
4#include <cmath>
5#include <algorithm>
6
7namespace Sleak {
8namespace RenderEngine {
9
10/// Halton low-discrepancy sequence value for TAA sub-pixel jitter.
11inline float HaltonSeq(int index, int base) {
12 float result = 0.0f;
13 float f = 1.0f;
14 for (int i = index; i > 0; i /= base) {
15 f /= static_cast<float>(base);
16 result += f * static_cast<float>(i % base);
17 }
18 return result;
19}
20
21/// Row-major mat4 multiply: C = A * B.
22template <typename T>
23void MatMul4(const T A[16], const T B[16], T C[16]) {
24 for (int r = 0; r < 4; ++r)
25 for (int c = 0; c < 4; ++c) {
26 T s = T(0);
27 for (int k = 0; k < 4; ++k) s += A[r * 4 + k] * B[k * 4 + c];
28 C[r * 4 + c] = s;
29 }
30}
31
32/// Row-major 4x4 inverse via Gauss-Jordan elimination.
33template <typename T>
34bool InvertMat4(const T M[16], T out[16]) {
35 constexpr T kSingularEpsilon = T(1e-7);
36 T m[4][8];
37 for (int r = 0; r < 4; ++r) {
38 for (int c = 0; c < 4; ++c) m[r][c] = M[r * 4 + c];
39 for (int c = 0; c < 4; ++c) m[r][4 + c] = (r == c) ? T(1) : T(0);
40 }
41 for (int col = 0; col < 4; ++col) {
42 int pivot = -1;
43 T maxv = T(0);
44 for (int row = col; row < 4; ++row) {
45 T v = std::abs(m[row][col]);
46 if (v > maxv) { maxv = v; pivot = row; }
47 }
48 if (pivot < 0 || maxv < kSingularEpsilon) return false;
49 if (pivot != col) std::swap(m[col], m[pivot]);
50 T inv = T(1) / m[col][col];
51 for (int c = 0; c < 8; ++c) m[col][c] *= inv;
52 for (int row = 0; row < 4; ++row) {
53 if (row == col) continue;
54 T f = m[row][col];
55 for (int c = 0; c < 8; ++c) m[row][c] -= f * m[col][c];
56 }
57 }
58 for (int r = 0; r < 4; ++r)
59 for (int c = 0; c < 4; ++c)
60 out[r * 4 + c] = m[r][4 + c];
61 return true;
62}
63
64} // namespace RenderEngine
65} // namespace Sleak
66
67#endif
Backend-facing rendering layer shared by the four graphics backends.
bool InvertMat4(const T M[16], T out[16])
Row-major 4x4 inverse via Gauss-Jordan elimination.
float HaltonSeq(int index, int base)
Halton low-discrepancy sequence value for TAA sub-pixel jitter.
void MatMul4(const T A[16], const T B[16], T C[16])
Row-major mat4 multiply: C = A * B.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10