fixed to fix and some more
This commit is contained in:
+30
-43
@@ -16,22 +16,31 @@
|
||||
#ifndef COM_MAT_H
|
||||
#define COM_MAT_H
|
||||
|
||||
#include "fixed.h"
|
||||
#include "def.h"
|
||||
#include "fix.h"
|
||||
#include "vec.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef union {
|
||||
com_fixed_t com_def_alignedas(64) a[4 * 4];
|
||||
com_def_alignedas(64) com_def_vector(com_fix_t, a, 4 * 4);
|
||||
} com_mat_t;
|
||||
|
||||
void com_mat_run_bench(void);
|
||||
|
||||
#define COM_MAT_PROFILE_SINK(m_m) \
|
||||
do { \
|
||||
for (int i = 0; i < 16; ++i) \
|
||||
COM_DEF_PROFILE_SINK((m_m).a[i]); \
|
||||
} while (0)
|
||||
|
||||
static inline com_mat_t com_mat_identity(void) {
|
||||
com_mat_t result = {0};
|
||||
|
||||
result.a[0 * 4 + 0] = COM_FIXED_FRACUNIT;
|
||||
result.a[1 * 4 + 1] = COM_FIXED_FRACUNIT;
|
||||
result.a[2 * 4 + 2] = COM_FIXED_FRACUNIT;
|
||||
result.a[3 * 4 + 3] = COM_FIXED_FRACUNIT;
|
||||
result.a[0 * 4 + 0] = COM_FIX_FRACUNIT;
|
||||
result.a[1 * 4 + 1] = COM_FIX_FRACUNIT;
|
||||
result.a[2 * 4 + 2] = COM_FIX_FRACUNIT;
|
||||
result.a[3 * 4 + 3] = COM_FIX_FRACUNIT;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -43,7 +52,7 @@ static inline com_mat_t com_mat_mul(com_mat_t a, com_mat_t b) {
|
||||
// for (int c = 0; c < 4; ++c) {
|
||||
// for (int k = 0; k < 4; ++k) {
|
||||
// for (int r = 0; r < 4; ++r) {
|
||||
// result.a[r + c * 4] += com_fixed_mul(a.a[r + k * 4], b.a[k + c * 4]);
|
||||
// result.a[r + c * 4] += com_fix_mul(a.a[r + k * 4], b.a[k + c * 4]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -56,7 +65,7 @@ static inline com_mat_t com_mat_mul(com_mat_t a, com_mat_t b) {
|
||||
((int64_t)a.a[r + 1 * 4] * b.a[1 + c * 4]) +
|
||||
((int64_t)a.a[r + 2 * 4] * b.a[2 + c * 4]) +
|
||||
((int64_t)a.a[r + 3 * 4] * b.a[3 + c * 4])) >>
|
||||
COM_FIXED_FRACBITS;
|
||||
COM_FIX_FRACBITS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,34 +85,13 @@ static inline com_mat_t com_mat_mul_reodered(com_mat_t a, com_mat_t b) {
|
||||
((int64_t)a.a[1 + r * 4] * b.a[1 + c * 4]) +
|
||||
((int64_t)a.a[2 + r * 4] * b.a[2 + c * 4]) +
|
||||
((int64_t)a.a[3 + r * 4] * b.a[3 + c * 4])) >>
|
||||
COM_FIXED_FRACBITS;
|
||||
COM_FIX_FRACBITS;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Projects vertex position to a screen via reordered row-major MVP matrix,
|
||||
* which implies division by w in-place */
|
||||
static inline com_vec_t com_mat_vec_project(com_mat_t a, com_vec_t b) {
|
||||
com_fixed_t t[4];
|
||||
com_vec_t result;
|
||||
|
||||
for (int c = 0; c < 4; ++c) {
|
||||
t[c] =
|
||||
(((int64_t)a.a[c * 4 + 0] * b.s.x) + ((int64_t)a.a[c * 4 + 1] * b.s.y) +
|
||||
((int64_t)a.a[c * 4 + 2] * b.s.z) + a.a[c * 4 + 3]) >>
|
||||
COM_FIXED_FRACBITS;
|
||||
}
|
||||
|
||||
/* Creates perspective effect, could be skipped for orthographic */
|
||||
result.a[0] = com_fixed_div(t[0], t[3]);
|
||||
result.a[1] = com_fixed_div(t[1], t[3]);
|
||||
result.a[2] = com_fixed_div(t[2], t[3]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Slightly optimized case of assumed identity scaling, might be useful for MVP
|
||||
* calculations, if model matrix does not scale. View matrix is always
|
||||
* unscaled as well.
|
||||
@@ -116,7 +104,7 @@ static inline com_vec_t com_mat_vec_project(com_mat_t a, com_vec_t b) {
|
||||
// result.a[0 * 4 + 3] = 0;
|
||||
// result.a[1 * 4 + 3] = 0;
|
||||
// result.a[2 * 4 + 3] = 0;
|
||||
// result.a[3 * 4 + 3] = COM_FIXED_FRACUNIT;
|
||||
// result.a[3 * 4 + 3] = COM_FIX_FRACUNIT;
|
||||
|
||||
// return result;
|
||||
// }
|
||||
@@ -158,7 +146,7 @@ static inline com_mat_t com_mat_look_at(com_vec_t pos, com_vec_t up,
|
||||
result.a[12] = -com_vec_dot(r, pos);
|
||||
result.a[13] = -com_vec_dot(u, pos);
|
||||
result.a[14] = com_vec_dot(target, pos);
|
||||
result.a[15] = COM_FIXED_FRACUNIT;
|
||||
result.a[15] = COM_FIX_FRACUNIT;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -166,23 +154,22 @@ static inline com_mat_t com_mat_look_at(com_vec_t pos, com_vec_t up,
|
||||
/* TODO: move to .c file */
|
||||
/* Produces a projection matrix needed for camera work. */
|
||||
static inline com_mat_t com_mat_perspective(uint16_t rwidth, uint16_t rheight,
|
||||
com_fixed_t nearz, com_fixed_t farz,
|
||||
com_fixed_t fov) {
|
||||
com_fix_t nearz, com_fix_t farz,
|
||||
com_fix_t fov) {
|
||||
com_mat_t result = {0};
|
||||
|
||||
com_fixed_t const aspect =
|
||||
com_fixed_div(rwidth * COM_FIXED_FRACUNIT, rheight * COM_FIXED_FRACUNIT);
|
||||
com_fixed_t const f =
|
||||
com_fixed_div(COM_FIXED_FRACUNIT,
|
||||
com_fixed_tan(com_fixed_mul(fov, COM_FIXED_FRACHALF)));
|
||||
com_fixed_t const fn = com_fixed_div(COM_FIXED_FRACUNIT, (nearz - farz));
|
||||
com_fix_t const aspect =
|
||||
com_fix_div(rwidth * COM_FIX_FRACUNIT, rheight * COM_FIX_FRACUNIT);
|
||||
com_fix_t const f = com_fix_div(
|
||||
COM_FIX_FRACUNIT, com_fix_tan(com_fix_mul(fov, COM_FIX_FRACHALF)));
|
||||
com_fix_t const fn = com_fix_div(COM_FIX_FRACUNIT, (nearz - farz));
|
||||
|
||||
result.a[0 * 4 + 0] = com_fixed_div(f, aspect);
|
||||
result.a[0 * 4 + 0] = com_fix_div(f, aspect);
|
||||
result.a[1 * 4 + 1] = f;
|
||||
result.a[2 * 4 + 2] = (nearz + farz) * fn;
|
||||
result.a[2 * 4 + 3] = -COM_FIXED_FRACUNIT;
|
||||
result.a[2 * 4 + 3] = -COM_FIX_FRACUNIT;
|
||||
result.a[3 * 4 + 2] =
|
||||
(COM_FIXED_FRACUNIT * 2) * com_fixed_mul(com_fixed_mul(nearz, farz), fn);
|
||||
(COM_FIX_FRACUNIT * 2) * com_fix_mul(com_fix_mul(nearz, farz), fn);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user