Nugget
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2021 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
31#include "common/hardware/gpu.h"
32#include "shell/dcos.h"
33
34#define ONE 16777216
35
36static inline int32_t dMul(int32_t a, int32_t b) {
37 long long r = a;
38 r *= b;
39 return r >> 24;
40}
41int32_t dDiv(int32_t a, int32_t b);
42
43struct Vertex2D {
44 int32_t x, y;
45};
46
47struct Matrix2D {
48 struct Vertex2D vs[2];
49};
50
51struct Vertex3D {
52 int32_t x, y, z;
53};
54
55struct Matrix3D {
56 struct Vertex3D vs[3];
57};
58
60
61void generateRotationMatrix3D(struct Matrix3D *m, int t, enum Axis a);
62void multiplyMatrix3D(const struct Matrix3D *m1, const struct Matrix3D *m2, struct Matrix3D *out);
63void scaleMatrix3D(struct Matrix3D *m, int32_t s);
64void matrixVertexMul3D(const struct Matrix3D *m, const struct Vertex3D *v, struct Vertex3D *out);
65void matrixVertexMul3Dxy(const struct Matrix3D *m, const struct Vertex3D *v, struct Vertex2D *out);
66int32_t matrixVertexMul3Dz(const struct Matrix3D *m, const struct Vertex3D *v);
67
68static inline void rotationMatrix2D(struct Matrix2D *m, int t) {
69 int32_t c = dCos(t);
70 int32_t s = dSin(t);
71 m->vs[0].x = c;
72 m->vs[0].y = s;
73 m->vs[1].x = -s;
74 m->vs[1].y = c;
75}
76
77static inline void scaleMatrix2D(struct Matrix2D *m, int32_t s) {
78 m->vs[0].x = dMul(m->vs[0].x, s);
79 m->vs[0].y = dMul(m->vs[0].y, s);
80 m->vs[1].x = dMul(m->vs[1].x, s);
81 m->vs[1].y = dMul(m->vs[1].y, s);
82}
83
84static inline void matrixVertexMul2D(struct Matrix2D *m, struct Vertex2D *v) {
85 int32_t x = v->x;
86 int32_t y = v->y;
87 int32_t mx1 = m->vs[0].x;
88 int32_t my1 = m->vs[0].y;
89 int32_t mx2 = m->vs[1].x;
90 int32_t my2 = m->vs[1].y;
91 int32_t nx1 = dMul(x, mx1);
92 int32_t ny1 = dMul(y, my1);
93 int32_t nx2 = dMul(x, mx2);
94 int32_t ny2 = dMul(y, my2);
95 int32_t nx = nx1 + ny1;
96 int32_t ny = nx2 + ny2;
97 v->x = nx;
98 v->y = ny;
99}
100
101// standard lerp function
102// s = source, an arbitrary number up to 2^24
103// d = destination, an arbitrary number up to 2^24
104// p = position, a number between 0 and 256, inclusive
105// p = 0 means output = s
106// p = 256 means output = d
107static inline uint32_t lerpU(uint32_t s, uint32_t d, unsigned p) { return (s * (256 - p) + d * p) >> 8; }
108static inline int32_t lerpS(int32_t s, int32_t d, unsigned p) { return (s * (256 - p) + d * p) >> 8; }
109static inline int32_t lerpD(int32_t s, int32_t d, int32_t p) { return dMul(s, 16777216 - p) + dMul(d, p); }
110static inline union Color lerpC(const union Color s, const union Color d, unsigned p) {
111 union Color r;
112 r.r = lerpU(s.r, d.r, p);
113 r.b = lerpU(s.b, d.b, p);
114 r.g = lerpU(s.g, d.g, p);
115 return r;
116}
uint32_t t
Definition cop0.c:79
uint32_t r
Definition cpu.c:222
uint32_t out
Definition cpu.c:62
void matrixVertexMul3D(const struct Matrix3D *m, const struct Vertex3D *v, struct Vertex3D *out)
Definition math.c:107
int32_t matrixVertexMul3Dz(const struct Matrix3D *m, const struct Vertex3D *v)
Definition math.c:159
void generateRotationMatrix3D(struct Matrix3D *m, int t, enum Axis a)
Definition math.c:33
void multiplyMatrix3D(const struct Matrix3D *m1, const struct Matrix3D *m2, struct Matrix3D *out)
Definition math.c:73
int32_t dDiv(int32_t a, int32_t b)
Definition math.c:202
void scaleMatrix3D(struct Matrix3D *m, int32_t s)
Definition math.c:95
void matrixVertexMul3Dxy(const struct Matrix3D *m, const struct Vertex3D *v, struct Vertex2D *out)
Definition math.c:137
Axis
Definition math.h:59
@ AXIS_X
Definition math.h:59
@ AXIS_Y
Definition math.h:59
@ AXIS_Z
Definition math.h:59
char * s
Definition string.c:48
char b[9]
Definition string.c:47
Definition math.h:47
struct Vertex2D vs[2]
Definition math.h:48
Definition math.h:55
struct Vertex3D vs[3]
Definition math.h:56
Definition math.h:43
int32_t y
Definition math.h:44
int32_t x
Definition math.h:44
Definition math.h:51
int32_t y
Definition math.h:52
int32_t x
Definition math.h:52
int32_t z
Definition math.h:52
static int c
Definition syscalls.h:121
void uint32_t(classId, spec)
Definition gpu.h:109
uint8_t r
Definition gpu.h:111