-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
gpu.c
201 lines (166 loc) · 3.87 KB
/
gpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
gpu.c _ Advanced GPU commands.
*/
#include <stdlib.h>
#include <string.h>
#include <3ds/types.h>
#include <3ds/svc.h>
#include <3ds/gpu/gpu.h>
#include <3ds/gpu/gx.h>
#include <3ds/gpu/shbin.h>
u32* gpuCmdBuf;
u32 gpuCmdBufSize;
u32 gpuCmdBufOffset;
void GPUCMD_AddRawCommands(const u32* cmd, u32 size)
{
if(!cmd || !size)return;
memcpy(&gpuCmdBuf[gpuCmdBufOffset], cmd, size*4);
gpuCmdBufOffset+=size;
}
static void GPUCMD_AddInternal(u32 header, const u32* param, u32 paramlength)
{
if(!gpuCmdBuf || gpuCmdBufOffset+paramlength+1>gpuCmdBufSize)
svcBreak(USERBREAK_PANIC); // Shouldn't happen.
paramlength--;
header|=(paramlength&0xff)<<20;
gpuCmdBuf[gpuCmdBufOffset]=param ? param[0] : 0;
gpuCmdBuf[gpuCmdBufOffset+1]=header;
if(paramlength)
{
if(param)memcpy(&gpuCmdBuf[gpuCmdBufOffset+2], ¶m[1], paramlength*4);
else memset(&gpuCmdBuf[gpuCmdBufOffset+2], 0, paramlength*4);
}
gpuCmdBufOffset+=paramlength+2;
if(paramlength&1)gpuCmdBuf[gpuCmdBufOffset++]=0x00000000; //alignment
}
void GPUCMD_Add(u32 header, const u32* param, u32 paramlength)
{
if(!paramlength)paramlength=1;
while(paramlength)
{
u32 remaining = paramlength > 0x100 ? 0x100 : paramlength;
GPUCMD_AddInternal(header, param, remaining);
param += remaining;
paramlength -= remaining;
if(header & BIT(31)) header += remaining;
}
}
void GPUCMD_Split(u32** addr, u32* size)
{
GPUCMD_AddWrite(GPUREG_FINALIZE, 0x12345678);
if (gpuCmdBufOffset & 3)
GPUCMD_AddWrite(GPUREG_FINALIZE, 0x12345678); // 16-byte align the buffer
if (addr) *addr = gpuCmdBuf;
if (size) *size = gpuCmdBufOffset;
gpuCmdBuf += gpuCmdBufOffset;
gpuCmdBufSize -= gpuCmdBufOffset;
gpuCmdBufOffset = 0;
}
static inline u32 floatrawbits(float f)
{
union { float f; u32 i; } s;
s.f = f;
return s.i;
}
// f16 has:
// - 1 sign bit
// - 5 exponent bits
// - 10 mantissa bits
u32 f32tof16(float f)
{
u32 i = floatrawbits(f);
u32 mantissa = (i << 9) >> 9;
s32 exponent = (i << 1) >> 24;
u32 sign = (i << 0) >> 31;
// Truncate mantissa
mantissa >>= 13;
// Re-bias exponent
exponent = exponent - 127 + 15;
if (exponent < 0)
{
// Underflow: flush to zero
return sign << 15;
}
else if (exponent > 0x1F)
{
// Overflow: saturate to infinity
return sign << 15 | 0x1F << 10;
}
return sign << 15 | exponent << 10 | mantissa;
}
// f20 has:
// - 1 sign bit
// - 7 exponent bits
// - 12 mantissa bits
u32 f32tof20(float f)
{
u32 i = floatrawbits(f);
u32 mantissa = (i << 9) >> 9;
s32 exponent = (i << 1) >> 24;
u32 sign = (i << 0) >> 31;
// Truncate mantissa
mantissa >>= 11;
// Re-bias exponent
exponent = exponent - 127 + 63;
if (exponent < 0)
{
// Underflow: flush to zero
return sign << 19;
}
else if (exponent > 0x7F)
{
// Overflow: saturate to infinity
return sign << 19 | 0x7F << 12;
}
return sign << 19 | exponent << 12 | mantissa;
}
// f24 has:
// - 1 sign bit
// - 7 exponent bits
// - 16 mantissa bits
u32 f32tof24(float f)
{
u32 i = floatrawbits(f);
u32 mantissa = (i << 9) >> 9;
s32 exponent = (i << 1) >> 24;
u32 sign = (i << 0) >> 31;
// Truncate mantissa
mantissa >>= 7;
// Re-bias exponent
exponent = exponent - 127 + 63;
if (exponent < 0)
{
// Underflow: flush to zero
return sign << 23;
}
else if (exponent > 0x7F)
{
// Overflow: saturate to infinity
return sign << 23 | 0x7F << 16;
}
return sign << 23 | exponent << 16 | mantissa;
}
// f31 has:
// - 1 sign bit
// - 7 exponent bits
// - 23 mantissa bits
u32 f32tof31(float f)
{
u32 i = floatrawbits(f);
u32 mantissa = (i << 9) >> 9;
s32 exponent = (i << 1) >> 24;
u32 sign = (i << 0) >> 31;
// Re-bias exponent
exponent = exponent - 127 + 63;
if (exponent < 0)
{
// Underflow: flush to zero
return sign << 30;
}
else if (exponent > 0x7F)
{
// Overflow: saturate to infinity
return sign << 30 | 0x7F << 23;
}
return sign << 30 | exponent << 23 | mantissa;
}