-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-1bcm.c
454 lines (395 loc) · 13.4 KB
/
main-1bcm.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* APoV Project
* pspgu 1 bit color mapping version
* By m-c/d in 2020
*/
#include <pspgu.h>
#include <pspgum.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <psprtc.h>
#include <psppower.h>
#include <pspdisplay.h>
#define HEADER_BYTES_COUNT 80
#define TEXTURE_BLOCK_SIZE 256
#define BUFFER_WIDTH 512
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272
PSP_MODULE_INFO("APoV", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(-1024);
void sceDmacMemcpy(void *dst, const void *src, int size);
typedef struct Cached {
u32 mask, moff, midx;
float fa, fb, fc;
u32 hcka, hckb, vcka, vckb;
u32 hoa, hob, voa, vob;
} Cached __attribute__((aligned(16)));
typedef struct Vertex {
u16 u, v;
u16 x, y, z;
} Vertex;
typedef struct Options {
u32 SPACE_BLOCK_SIZE;
u32 HORIZONTAL_POV_COUNT;
u32 VERTICAL_POV_COUNT;
u32 RAY_STEP;
u32 WIDTH_BLOCK_COUNT;
u32 DEPTH_BLOCK_COUNT;
u32 COLOR_MAP_SIZE;
u32 TRACE_EDGES;
} Options;
#define S TEXTURE_BLOCK_SIZE
#define P (S / 16)
#define T (S / 16)
static u16 VERTICES_COUNT;
static u16 TEXTURE_WIDTH;
static Vertex* surface;
static Options options;
void generateRenderSurface() {
const u8 VERTICES_BY_BLOCK = 2;
VERTICES_COUNT = VERTICES_BY_BLOCK * (TEXTURE_BLOCK_SIZE / P) * (TEXTURE_WIDTH / P);
surface = memalign(16, sizeof(Vertex) * VERTICES_COUNT);
const u16 X = (SCREEN_WIDTH - TEXTURE_WIDTH) / 2;
const u16 Y = (SCREEN_HEIGHT - TEXTURE_BLOCK_SIZE) / 2;
u16 x = 0;
u16 offset = 0;
while(x < TEXTURE_WIDTH) {
u16 y = 0;
while(y < TEXTURE_BLOCK_SIZE) {
const Vertex a = {x, y, X+x, Y+y, 0};
const Vertex b = {x+T, y+T, X+x+P, Y+y+P, 0};
surface[offset + 0] = a;
surface[offset + 1] = b;
offset += VERTICES_BY_BLOCK;
y += P;
}
x += P;
}
}
static u8 MODE = 0;
static u16 WIN_WIDTH;
static u16 WIN_HEIGHT;
static u32 WIN_PIXELS_COUNT;
static u32 SPACE_VOXELS_COUNT;
static u32 WIN_BYTES_COUNT;
static u32 SPACE_BYTES_COUNT;
static u32 BASE_BYTES_COUNT;
static u16 MAP_WIDTH;
static u16 MAP_HEIGHT;
static u16 MAP_WIDTH_SCALE;
static u16 MAP_HEIGHT_SCALE;
static u32 MAP_PIXELS_COUNT;
static u32 MAP_BYTES_COUNT;
static u32 MAP_VOXELS_COUNT;
static u32 MAP_VOLUME_BYTES_COUNT;
static void initGuContext(void* list) {
sceGuStart(GU_DIRECT, list);
sceGuDrawBuffer(GU_PSM_8888, (void*)0, BUFFER_WIDTH);
sceGuDispBuffer(SCREEN_WIDTH, SCREEN_HEIGHT, (void*)(sizeof(u32) *
BUFFER_WIDTH * SCREEN_HEIGHT) , BUFFER_WIDTH);
sceGuClearColor(0xFF000000);
sceGuDisable(GU_SCISSOR_TEST);
sceGuEnable(GU_CULL_FACE);
sceGuFrontFace(GU_CW);
sceGuEnable(GU_CLIP_PLANES);
sceGuTexWrap(GU_CLAMP, GU_CLAMP);
sceGuTexMode(GU_PSM_8888, 0, 1, 0);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
sceGuTexFilter(GU_NEAREST, GU_NEAREST);
sceGuDisplay(GU_TRUE);
sceGuFinish();
sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE);
}
static SceUID f;
static void openData() {
f = sceIoOpen("atoms.apov", PSP_O_RDONLY, 0777);
}
static void closeData() {
sceIoClose(f);
}
static u64 loffset = -1;
static u8 readData(u8* const frame, const u64 offset) {
if(offset != loffset) {
const u64 nbytes = WIN_BYTES_COUNT + MAP_BYTES_COUNT;
sceIoLseek(f, offset + HEADER_BYTES_COUNT, SEEK_SET);
if(nbytes != sceIoRead(f, frame, nbytes)) {
openData();
return 0;
}
loffset = offset;
return 1;
}
return 0;
}
static Cached* cached = NULL;
static int __attribute__((aligned(16))) em[16] = {0};
void cache() {
Cached c;
cached = memalign(16, WIN_PIXELS_COUNT * sizeof(Cached));
u16 x = 0;
while(x < WIN_WIDTH) {
u16 y = 0;
while(y < WIN_HEIGHT) {
const u32 i = x + y * WIN_WIDTH;
const float fx = ((float)x) / MAP_WIDTH_SCALE;
const float fy = ((float)y) / MAP_HEIGHT_SCALE;
const u32 ux = fx;
const u32 uy = fy;
const u32 uyb = uy * MAP_WIDTH;
const float hc = fx - ux - 0.5f;
const float vc = fy - uy - 0.5f;
c.mask = (0b1 << (i % 8));
c.moff = (i / 8);
c.midx = (ux + uyb);
c.fb = (hc < 0.0f ? -hc : hc);
c.fc = (vc < 0.0f ? -vc : vc);
c.fa = 1.0f - (c.fb + c.fc);
c.hoa = ux - 1 + uyb;
c.hob = ux + 1 + uyb;
c.voa = ux + uyb - MAP_WIDTH;
c.vob = ux + uyb + MAP_WIDTH;
c.hcka = hc < 0.0f && ux > 0 ? 1 : 0;
c.hckb = hc >= 0.0f && ux < (MAP_WIDTH - 1) ? 1 : 0;
c.vcka = vc < 0.0f && uy > 0 ? 1 : 0;
c.vckb = vc >= 0.0f && uy < (MAP_HEIGHT - 1) ? 1:0;
cached[i] = c;
y++;
}
x++;
}
if(options.TRACE_EDGES) {
const u16 WIN_WIDTH_X2 = WIN_WIDTH * 2;
// Edges matrix
em[0] = -1;
em[1] = +1;
em[2] = -WIN_WIDTH;
em[3] = +WIN_WIDTH;
em[4] = -1-WIN_WIDTH;
em[5] = +1+WIN_WIDTH;
em[6] = -1+WIN_WIDTH;
em[7] = +1-WIN_WIDTH;
em[8] = -2;
em[9] = +2;
em[10] = -WIN_WIDTH_X2;
em[11] = +WIN_WIDTH_X2;
em[12] = -2-WIN_WIDTH_X2;
em[13] = +2+WIN_WIDTH_X2;
em[14] = -2+WIN_WIDTH_X2;
em[15] = +2-WIN_WIDTH_X2;
}
}
void updateView(u8* const frame, u32* const map, u32* const base) {
if(MODE == 0) {
u32 i = 0;
while(i < WIN_PIXELS_COUNT) {
Cached* const cache = &(cached[i]);
if(frame[cache->moff] & cache->mask) {
base[i] = map[cache->midx] | 0xFF << 24;
} else base[i] = 0x00;
i++;
}
} else if(MODE == 1) {
u32 i = 0;
while(i < WIN_PIXELS_COUNT) {
Cached* const cache = &(cached[i]);
if(frame[cache->moff] & cache->mask) {
u32 b, c;
const u32 a = map[cache->midx];
if(cache->hcka) {
b = map[cache->hoa];
} else if(cache->hckb) {
b = map[cache->hob];
} else b = 0;
if(cache->vcka) {
c = map[cache->voa];
} else if(cache->vckb) {
c = map[cache->vob];
} else c = 0;
const u8 R = (u8)(
((a & 0xFF) * cache->fa) +
((b & 0xFF) * cache->fb) +
((c & 0xFF) * cache->fc));
const u8 G = (u8)(
(((a >> 8) & 0xFF) * cache->fa) +
(((b >> 8) & 0xFF) * cache->fb) +
(((c >> 8) & 0xFF) * cache->fc));
const u8 B = (u8)(
(((a >> 16) & 0xFF) * cache->fa) +
(((b >> 16) & 0xFF) * cache->fb) +
(((c >> 16) & 0xFF) * cache->fc));
base[i] = R | G << 8 | B << 16 | 0xFF << 24;
} else {
if(options.TRACE_EDGES) {
const u16 x = i % WIN_WIDTH;
const u16 y = i / WIN_WIDTH;
if(x >= 2 && x < (WIN_WIDTH - 2) &&
y >= 2 && y < (WIN_HEIGHT - 2)) {
u8 n = 0;
while(n < 16) {
Cached* const ca = &(cached[i + em[n]]);
Cached* const cb = &(cached[i + em[n + 1]]);
const u8 count =
((frame[ca->moff] & ca->mask) ? 1 : 0) +
((frame[cb->moff] & cb->mask) ? 1 : 0);
if(count == 2) {
const u32 a = map[ca->midx];
const u32 b = map[cb->midx];
const u8 R = ((a & 0xFF) + (b & 0xFF)) / 2.5f;
const u8 G = (((a >> 8) & 0xFF) + ((b >> 8) & 0xFF)) / 2.3f;
const u8 B = (((a >> 16) & 0xFF) + ((b >> 16) & 0xFF)) / 2.3f;
base[i] = R | G << 8 | B << 16 | 0xFF << 24;
goto _continue;
}
n+=2;
}
}
}
base[i] = 0x00;
}
_continue:
i++;
}
}
}
static int ajustCursor(const int value, const u8 mode) {
if(!mode) {
u16 max;
if(value < 0) {
return 0;
} else if(value >= (max = (options.SPACE_BLOCK_SIZE *
options.DEPTH_BLOCK_COUNT) / options.RAY_STEP)) {
return max - 1;
}
} else if(mode == 1) {
if(value < 0) {
return options.HORIZONTAL_POV_COUNT - 1;
} else if(value >= options.HORIZONTAL_POV_COUNT) {
return 0;
}
} else if(mode == 2) {
if(value < 0) {
return options.VERTICAL_POV_COUNT - 1;
} else if(value >= options.VERTICAL_POV_COUNT) {
return 0;
}
}
return value;
}
static u64 getOffset(const int move, const int hrotate, const int vrotate) {
const u32 pov = (hrotate * options.VERTICAL_POV_COUNT + vrotate);
return (WIN_BYTES_COUNT + MAP_BYTES_COUNT) * move +
pov * (SPACE_BYTES_COUNT + MAP_VOLUME_BYTES_COUNT);
}
SceCtrlData pad;
static u64 controls() {
static int move = 0;
static int hrotate = 0;
static int vrotate = 0;
static SceCtrlData lpad;
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_TRIANGLE) { move++; }
if(pad.Buttons & PSP_CTRL_CROSS) { move--; }
if(pad.Buttons & PSP_CTRL_RIGHT) { hrotate--; }
if(pad.Buttons & PSP_CTRL_LEFT) { hrotate++; }
if(pad.Buttons & PSP_CTRL_UP) { vrotate--; }
if(pad.Buttons & PSP_CTRL_DOWN) { vrotate++; }
move = ajustCursor(move, 0);
hrotate = ajustCursor(hrotate, 1);
vrotate = ajustCursor(vrotate, 2);
if((pad.Buttons & PSP_CTRL_SQUARE) &&
!(lpad.Buttons & PSP_CTRL_SQUARE)) {
MODE = (MODE + 1) % 2;
loffset = -1;
}
lpad = pad;
return getOffset(move, hrotate, vrotate);
}
u8 getPower(u16 value) {
u8 power = 0;
while(value > 1) {
if((value & 1) != 0) {
return 0;
}
power++;
value >>= 1;
}
return power;
}
void getOptions() {
FILE* f = fopen("atoms.apov", "rb");
if(f != NULL) {
fread(&options, sizeof(Options), 1, f);
fclose(f);
}
}
int main() {
scePowerSetClockFrequency(333, 333, 166);
getOptions();
const u16 DEPTH_FRAME_COUNT = ((options.DEPTH_BLOCK_COUNT *
options.SPACE_BLOCK_SIZE) / options.RAY_STEP);
WIN_WIDTH = options.SPACE_BLOCK_SIZE * options.WIDTH_BLOCK_COUNT;
WIN_HEIGHT = options.SPACE_BLOCK_SIZE;
WIN_PIXELS_COUNT = WIN_WIDTH * WIN_HEIGHT;
SPACE_VOXELS_COUNT = DEPTH_FRAME_COUNT * WIN_PIXELS_COUNT;
BASE_BYTES_COUNT = WIN_PIXELS_COUNT * 8;
WIN_BYTES_COUNT = WIN_PIXELS_COUNT / 8;
SPACE_BYTES_COUNT = SPACE_VOXELS_COUNT / 8;
MAP_WIDTH = options.COLOR_MAP_SIZE * options.WIDTH_BLOCK_COUNT;
MAP_HEIGHT = options.COLOR_MAP_SIZE;
MAP_PIXELS_COUNT = MAP_WIDTH * MAP_HEIGHT;
MAP_BYTES_COUNT = MAP_PIXELS_COUNT * sizeof(u32);
MAP_VOXELS_COUNT = DEPTH_FRAME_COUNT * MAP_PIXELS_COUNT;
MAP_VOLUME_BYTES_COUNT = MAP_VOXELS_COUNT * sizeof(u32);
MAP_WIDTH_SCALE = WIN_WIDTH / MAP_WIDTH;
MAP_HEIGHT_SCALE = WIN_HEIGHT / MAP_HEIGHT;
TEXTURE_WIDTH = TEXTURE_BLOCK_SIZE * options.WIDTH_BLOCK_COUNT;
cache();
u32* base = memalign(16, BASE_BYTES_COUNT);
u8* frame = memalign(16, WIN_BYTES_COUNT + MAP_BYTES_COUNT);
void* list = memalign(16, 256);
generateRenderSurface();
sceGuInit();
initGuContext(list);
pspDebugScreenInitEx(NULL, PSP_DISPLAY_PIXEL_FORMAT_8888, 0);
pspDebugScreenEnableBackColor(0);
openData();
int dbuff = 0;
u64 prev, now, fps = 0;
const u64 tickResolution = sceRtcGetTickResolution();
do {
sceRtcGetCurrentTick(&prev);
sceGuStart(GU_DIRECT, list);
sceGuClear(GU_COLOR_BUFFER_BIT);
if(readData(frame, controls())) {
updateView(frame, (u32*)&frame[WIN_BYTES_COUNT], base);
}
sceGuTexImage(0, TEXTURE_WIDTH, TEXTURE_BLOCK_SIZE, TEXTURE_WIDTH, base);
sceGumDrawArray(GU_SPRITES, GU_TEXTURE_16BIT|GU_TRANSFORM_2D|GU_VERTEX_16BIT,
VERTICES_COUNT, 0, surface);
sceGuFinish();
sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE);
pspDebugScreenSetOffset(dbuff);
pspDebugScreenSetXY(0, 0);
pspDebugScreenSetTextColor(0xFF00A0FF);
pspDebugScreenPrintf("Fps: %llu\n", fps);
pspDebugScreenPrintf("Press [ ] to %s smoothing\n", MODE ? "disable" : "enable");
sceDisplayWaitVblankStart();
dbuff = (int)sceGuSwapBuffers();
sceRtcGetCurrentTick(&now);
fps = tickResolution / (now - prev);
} while(!(pad.Buttons & PSP_CTRL_SELECT));
sceGuTerm();
free(surface);
free(list);
free(base);
free(frame);
free(cached);
closeData();
sceKernelExitGame();
return 0;
}