Repository navigation
Expand file tree
/
Copy pathta_ctx.cpp
More file actions
498 lines (406 loc) · 10.1 KB
/
Copy pathta_ctx.cpp
File metadata and controls
498 lines (406 loc) · 10.1 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
This file is part of libswirl
*/
#include "license/bsd"
#include "ta.h"
#include "ta_ctx.h"
#include "hw/sh4/sh4_sched.h"
extern u32 fskip;
extern u32 FrameCount;
int frameskip=0;
bool FrameSkipping=false; // global switch to enable/disable frameskip
TA_context* ta_ctx;
tad_context ta_tad;
TA_context* vd_ctx;
rend_context vd_rc;
#if ANDROID
#include <errno.h>
#include <malloc.h>
int posix_memalign(void** memptr, size_t alignment, size_t size) {
if ((alignment & (alignment - 1)) != 0 || alignment == 0) {
return EINVAL;
}
if (alignment % sizeof(void*) != 0) {
return EINVAL;
}
*memptr = memalign(alignment, size);
if (*memptr == NULL) {
return errno;
}
return 0;
}
#endif
// helper for 32 byte aligned memory allocation
void* OS_aligned_malloc(size_t align, size_t size)
{
void *result;
#ifdef __MINGW32__
return __mingw_aligned_malloc(size, align);
#elif HOST_OS == OS_WINDOWS
result = _aligned_malloc(size, align);
#else
if(posix_memalign(&result, align, size)) result = 0;
#endif
return result;
}
// helper for 32 byte aligned memory de-allocation
void OS_aligned_free(void *ptr)
{
#ifdef __MINGW32__
__mingw_aligned_free(ptr);
#elif HOST_OS == OS_WINDOWS
_aligned_free(ptr);
#else
free(ptr);
#endif
}
void SetCurrentTARC(u32 addr)
{
if (addr != TACTX_NONE)
{
if (ta_ctx)
SetCurrentTARC(TACTX_NONE);
verify(ta_ctx == 0);
//set new context
ta_ctx = tactx_Find(addr,true);
//copy cached params
ta_tad = ta_ctx->tad;
}
else
{
//Flush cache to context
verify(ta_ctx != 0);
ta_ctx->tad=ta_tad;
//clear context
ta_ctx=0;
ta_tad.Reset(0);
}
}
bool TryDecodeTARC()
{
verify(ta_ctx != 0);
if (vd_ctx == 0)
{
vd_ctx = ta_ctx;
vd_ctx->rend.proc_start = vd_ctx->rend.proc_end + 32;
vd_ctx->rend.proc_end = vd_ctx->tad.thd_data;
#if !defined(HOST_NO_THREADS)
vd_ctx->rend_inuse.Lock();
#endif
vd_rc = vd_ctx->rend;
//signal the vdec thread
return true;
}
else
return false;
}
void VDecEnd()
{
verify(vd_ctx != 0);
vd_ctx->rend = vd_rc;
#if !defined(HOST_NO_THREADS)
vd_ctx->rend_inuse.Unlock();
#endif
vd_ctx = 0;
}
TA_context* rqueue;
#if !defined(HOST_NO_THREADS)
cMutex mtx_rqueue;
cResetEvent frame_finished;
#endif
double last_frame = 0;
u64 last_cyces = 0;
bool QueueRender(TA_context* ctx)
{
verify(ctx != 0);
if (FrameSkipping && frameskip) {
frameskip=1-frameskip;
tactx_Recycle(ctx);
fskip++;
return false;
}
//Try to limit speed to a "sane" level
//Speed is also limited via audio, but audio
//is sometimes not accurate enough (android, vista+)
u32 cycle_span = (u32)(sh4_sched_now64() - last_cyces);
last_cyces = sh4_sched_now64();
double time_span = os_GetSeconds() - last_frame;
last_frame = os_GetSeconds();
bool too_fast = (cycle_span / time_span) > (SH4_MAIN_CLOCK * 1.2);
if (rqueue && too_fast && settings.pvr.SynchronousRender) {
//wait for a frame if
// we have another one queue'd and
// sh4 run at > 120% on the last slice
// and SynchronousRendering is enabled
#if !defined(HOST_NO_THREADS)
frame_finished.Wait();
#endif
}
if (rqueue) {
// FIXME if the discarded render is a RTT we'll have a texture missing. But waiting for the current frame to finish kills performance...
tactx_Recycle(ctx);
fskip++;
return false;
}
#if !defined(HOST_NO_THREADS)
frame_finished.Reset();
mtx_rqueue.Lock();
#endif
TA_context* old = rqueue;
rqueue=ctx;
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Unlock();
#endif
verify(!old);
return true;
}
TA_context* DequeueRender()
{
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Lock();
#endif
TA_context* rv = rqueue;
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Unlock();
#endif
if (rv)
FrameCount++;
return rv;
}
bool rend_framePending() {
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Lock();
#endif
TA_context* rv = rqueue;
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Unlock();
#endif
return rv != 0;
}
void FinishRender(TA_context* ctx)
{
verify(ctx == NULL || rqueue == ctx);
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Lock();
#endif
rqueue = 0;
#if !defined(HOST_NO_THREADS)
mtx_rqueue.Unlock();
#endif
if (ctx != NULL)
tactx_Recycle(ctx);
#if !defined(HOST_NO_THREADS)
frame_finished.Set();
#endif
}
#if !defined(HOST_NO_THREADS)
cMutex mtx_pool;
#endif
vector<TA_context*> ctx_pool;
vector<TA_context*> ctx_list;
TA_context* tactx_Alloc()
{
TA_context* rv = 0;
#if !defined(HOST_NO_THREADS)
mtx_pool.Lock();
#endif
if (ctx_pool.size())
{
rv = ctx_pool[ctx_pool.size()-1];
ctx_pool.pop_back();
}
#if !defined(HOST_NO_THREADS)
mtx_pool.Unlock();
#endif
if (!rv)
{
rv = new TA_context();
rv->Alloc();
}
return rv;
}
void tactx_Recycle(TA_context* poped_ctx)
{
#if !defined(HOST_NO_THREADS)
mtx_pool.Lock();
#endif
{
if (ctx_pool.size()>2)
{
poped_ctx->Free();
delete poped_ctx;
}
else
{
poped_ctx->Reset();
ctx_pool.push_back(poped_ctx);
}
}
#if !defined(HOST_NO_THREADS)
mtx_pool.Unlock();
#endif
}
TA_context* tactx_Find(u32 addr, bool allocnew)
{
for (size_t i=0; i<ctx_list.size(); i++)
if (ctx_list[i]->Address==addr)
return ctx_list[i];
}
if (allocnew)
{
TA_context* rv = tactx_Alloc();
rv->Address=addr;
ctx_list.push_back(rv);
return rv;
}
else
{
return 0;
}
}
TA_context* tactx_Pop(u32 addr)
{
for (size_t i=0; i<ctx_list.size(); i++)
{
if (ctx_list[i]->Address==addr)
{
TA_context* rv = ctx_list[i];
if (ta_ctx == rv)
SetCurrentTARC(TACTX_NONE);
ctx_list.erase(ctx_list.begin() + i);
return rv;
}
}
return 0;
}
void tactx_Term()
{
for (size_t i = 0; i < ctx_list.size(); i++)
{
ctx_list[i]->Free();
delete ctx_list[i];
}
ctx_list.clear();
#if !defined(HOST_NO_THREADS)
mtx_pool.Lock();
#endif
{
for (size_t i = 0; i < ctx_pool.size(); i++)
{
ctx_pool[i]->Free();
delete ctx_pool[i];
}
}
ctx_pool.clear();
#if !defined(HOST_NO_THREADS)
mtx_pool.Unlock();
#endif
}
#include "deps/zlib/zlib.h"
#include "hw/pvr/pvr_mem.h"
void tactx_write_frame(const char* file, TA_context* ctx, u8* vram, u8* vram_ref) {
FILE* fw = fopen(file, "wb");
//append to it
fseek(fw, 0, SEEK_END);
u32 bytes = (u32)(ctx->tad.End() - ctx->tad.thd_root);
fwrite("TAFRAME4", 1, 8, fw);
fwrite(&ctx->rend.isRTT, 1, sizeof(ctx->rend.isRTT), fw);
u32 zero = 0;
fwrite(&zero, 1, sizeof(bool), fw); // Was autosort
fwrite(&ctx->rend.fb_X_CLIP.full, 1, sizeof(ctx->rend.fb_X_CLIP.full), fw);
fwrite(&ctx->rend.fb_Y_CLIP.full, 1, sizeof(ctx->rend.fb_Y_CLIP.full), fw);
fwrite(ctx->rend.global_param_op.head(), 1, sizeof(PolyParam), fw);
fwrite(ctx->rend.verts.head(), 1, 4 * sizeof(Vertex), fw);
u32 t = VRAM_SIZE;
fwrite(&t, 1, sizeof(t), fw);
u8* compressed;
uLongf compressed_size;
u8* src_vram = vram;
if (vram_ref) {
src_vram = (u8*)malloc(VRAM_SIZE);
for (int i = 0; i < VRAM_SIZE; i++) {
src_vram[i] = vram[i] ^ vram_ref[i];
}
}
compressed = (u8*)malloc(VRAM_SIZE + 16);
compressed_size = VRAM_SIZE;
verify(compress(compressed, &compressed_size, src_vram, VRAM_SIZE) == Z_OK);
fwrite(&compressed_size, 1, sizeof(compressed_size), fw);
fwrite(compressed, 1, compressed_size, fw);
free(compressed);
if (src_vram != vram)
free(src_vram);
fwrite(&bytes, 1, sizeof(t), fw);
compressed = (u8*)malloc(bytes + 16);
compressed_size = bytes;
verify(compress(compressed, &compressed_size, ctx->tad.thd_root, bytes) == Z_OK);
fwrite(&compressed_size, 1, sizeof(compressed_size), fw);
fwrite(compressed, 1, compressed_size, fw);
free(compressed);
fwrite(&ctx->tad.render_pass_count, 1, sizeof(u32), fw);
for (int i = 0; i < ctx->tad.render_pass_count; i++) {
u32 offset = (u32)(ctx->tad.render_passes[i] - ctx->tad.thd_root);
fwrite(&offset, 1, sizeof(offset), fw);
}
fwrite(pvr_regs, 1, sizeof(pvr_regs), fw);
fclose(fw);
}
TA_context* tactx_read_frame(const char* file, u8* vram, u8* vram_ref) {
FILE* fw = fopen(file, "rb");
if (fw == NULL)
die("Cannot open frame to display");
char id0[8] = { 0 };
u32 t = 0;
fread(id0, 1, 8, fw);
if (memcmp(id0, "TAFRAME", 7) != 0 || (id0[7] != '3' && id0[7] != '4')) {
fclose(fw);
return 0;
}
int sizeofPolyParam = sizeof(PolyParam);
int sizeofVertex = sizeof(Vertex);
if (id0[7] == '3')
{
sizeofPolyParam -= 12;
sizeofVertex -= 16;
}
TA_context* ctx = tactx_Alloc();
ctx->Reset();
ctx->tad.Clear();
fread(&ctx->rend.isRTT, 1, sizeof(ctx->rend.isRTT), fw);
fread(&t, 1, sizeof(bool), fw); // Was autosort
fread(&ctx->rend.fb_X_CLIP.full, 1, sizeof(ctx->rend.fb_X_CLIP.full), fw);
fread(&ctx->rend.fb_Y_CLIP.full, 1, sizeof(ctx->rend.fb_Y_CLIP.full), fw);
fread(ctx->rend.global_param_op.Append(), 1, sizeofPolyParam, fw);
Vertex* vtx = ctx->rend.verts.Append(4);
for (int i = 0; i < 4; i++)
fread(vtx + i, 1, sizeofVertex, fw);
fread(&t, 1, sizeof(t), fw);
verify(t == VRAM_SIZE);
uLongf compressed_size;
fread(&compressed_size, 1, sizeof(compressed_size), fw);
u8* gz_stream = (u8*)malloc(compressed_size);
fread(gz_stream, 1, compressed_size, fw);
uLongf tl = t;
verify(uncompress(vram, &tl, gz_stream, compressed_size) == Z_OK);
free(gz_stream);
fread(&t, 1, sizeof(t), fw);
fread(&compressed_size, 1, sizeof(compressed_size), fw);
gz_stream = (u8*)malloc(compressed_size);
fread(gz_stream, 1, compressed_size, fw);
tl = t;
verify(uncompress(ctx->tad.thd_data, &tl, gz_stream, compressed_size) == Z_OK);
free(gz_stream);
ctx->tad.thd_data += t;
if (fread(&t, 1, sizeof(t), fw) > 0) {
ctx->tad.render_pass_count = t;
for (int i = 0; i < t; i++) {
u32 offset;
fread(&offset, 1, sizeof(offset), fw);
ctx->tad.render_passes[i] = ctx->tad.thd_root + offset;
}
}
fread(pvr_regs, 1, sizeof(pvr_regs), fw);
fclose(fw);
return ctx;
}