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
|
#include "stdafx.h"
#include <stdlib.h>
#include "EdgeZLib.h"
#include "edge/zlib/edgezlib_ppu.h"
static CellSpurs* s_pSpurs = nullptr;
//Set this to 5 if you want deflate/inflate to run in parallel on 5 SPUs.
const uint32_t kNumDeflateTasks = 1;
const uint32_t kMaxNumDeflateQueueEntries = 64;
static CellSpursEventFlag s_eventFlagDeflate; //Cannot be on stack
static CellSpursTaskset s_taskSetDeflate; //Cannot be on stack
static EdgeZlibDeflateQHandle s_deflateQueue;
static void* s_pDeflateQueueBuffer = nullptr;
static void* s_pDeflateTaskContext[kNumDeflateTasks];
static uint32_t s_numElementsToCompress; //Cannot be on stack
const uint32_t kNumInflateTasks = 1;
const uint32_t kMaxNumInflateQueueEntries = 64;
static CellSpursEventFlag s_eventFlagInflate; //Cannot be on stack
static CellSpursTaskset s_taskSetInflate; //Cannot be on stack
static EdgeZlibInflateQHandle s_inflateQueue;
static void* s_pInflateQueueBuffer = nullptr;
static void* s_pInflateTaskContext[kNumInflateTasks];
static uint32_t s_numElementsToDecompress; //Cannot be on stack
static CRITICAL_SECTION s_critSect;
static const bool sc_verbose = false;
#define printf_verbose(...) { if(sc_verbose){ printf(__VA_ARGS__);} }
void EdgeZLib::Init(CellSpurs* pSpurs)
{
s_pSpurs = pSpurs;
InitializeCriticalSection(&s_critSect);
//////////////////////////////////////////////////////////////////////////
//
// Initialize Deflate Queue.
// This will hold the queue of work that the Deflate Task(s) on SPU will
// pull work from.
//
//////////////////////////////////////////////////////////////////////////
{
// register taskSet
CellSpursTasksetAttribute taskSetAttribute;
uint8_t prios[8] = {15, 15, 15, 15, 15, 15, 0, 0};
int ret = cellSpursTasksetAttributeInitialize( &taskSetAttribute, 0, prios, 8 );
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursTasksetAttributeSetName(&taskSetAttribute, "edgeZlibDeflateTaskSet");
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursCreateTasksetWithAttribute( pSpurs, &s_taskSetDeflate, &taskSetAttribute );
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursEventFlagInitializeIWL( pSpurs,
&s_eventFlagDeflate,
CELL_SPURS_EVENT_FLAG_CLEAR_AUTO,
CELL_SPURS_EVENT_FLAG_SPU2PPU );
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursEventFlagAttachLv2EventQueue( &s_eventFlagDeflate );
PS3_ASSERT_CELL_ERROR(ret);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Event flag created\n" );
#endif
uint32_t deflateQueueBuffSize = edgeZlibGetDeflateQueueSize( kMaxNumDeflateQueueEntries );
s_pDeflateQueueBuffer = memalign( EDGE_ZLIB_DEFLATE_QUEUE_ALIGN, deflateQueueBuffSize );
s_deflateQueue = edgeZlibCreateDeflateQueue(
s_pSpurs,
kMaxNumDeflateQueueEntries,
s_pDeflateQueueBuffer,
deflateQueueBuffSize );
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Deflate Queue created\n" );
#endif
//////////////////////////////////////////////////////////////////////////
//
// Build Deflate Tasks.
// We want the compression to be able to run in parallel on multiple
// SPUs so we create as many tasks as SPUs that we want it to run
// on (kNumDeflateTasks).
//
//////////////////////////////////////////////////////////////////////////
s_pDeflateTaskContext[kNumDeflateTasks];
for( uint32_t taskNum = 0 ; taskNum < kNumDeflateTasks ; taskNum++ )
{
uint32_t contextSaveSize = edgeZlibGetDeflateTaskContextSaveSize();
s_pDeflateTaskContext[taskNum] = memalign( CELL_SPURS_TASK_CONTEXT_ALIGN, contextSaveSize );
edgeZlibCreateDeflateTask( &s_taskSetDeflate, s_pDeflateTaskContext[taskNum], s_deflateQueue );
}
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: %d Deflate Task(s) started\n", kNumDeflateTasks );
#endif
}
{
//////////////////////////////////////////////////////////////////////////
//
// Initialize taskset and event flag
//
//////////////////////////////////////////////////////////////////////////
// register taskSet
CellSpursTasksetAttribute taskSetAttribute;
uint8_t prios[8] = {15, 15, 15, 15, 15, 15, 0, 0};
int ret = cellSpursTasksetAttributeInitialize( &taskSetAttribute, 0, prios, 8 );
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursTasksetAttributeSetName(&taskSetAttribute, "edgeZlibInflateTaskSet");
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursCreateTasksetWithAttribute( s_pSpurs, &s_taskSetInflate, &taskSetAttribute );
PS3_ASSERT_CELL_ERROR(ret);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Inflate Taskset created\n" );
#endif
ret = cellSpursEventFlagInitializeIWL( s_pSpurs,
&s_eventFlagInflate,
CELL_SPURS_EVENT_FLAG_CLEAR_AUTO,
CELL_SPURS_EVENT_FLAG_SPU2PPU );
PS3_ASSERT_CELL_ERROR(ret);
ret = cellSpursEventFlagAttachLv2EventQueue( &s_eventFlagInflate );
PS3_ASSERT_CELL_ERROR(ret);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Event flag created\n" );
#endif
//////////////////////////////////////////////////////////////////////////
//
// Initialize Inflate Queue.
// This will hold the queue of work that the Inflate Task(s) on SPU will
// pull work from.
//
//////////////////////////////////////////////////////////////////////////
uint32_t inflateQueueBuffSize = edgeZlibGetInflateQueueSize( kMaxNumInflateQueueEntries );
s_pInflateQueueBuffer = memalign( EDGE_ZLIB_INFLATE_QUEUE_ALIGN, inflateQueueBuffSize );
s_inflateQueue = edgeZlibCreateInflateQueue(
s_pSpurs,
kMaxNumInflateQueueEntries,
s_pInflateQueueBuffer,
inflateQueueBuffSize );
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Inflate Queue created\n" );
#endif
//////////////////////////////////////////////////////////////////////////
//
// Build Inflate Tasks.
// We want the compression to be able to run in parallel on multiple
// SPUs so we create as many tasks as SPUs that we want it to run
// on (kNumInflateTasks).
//
//////////////////////////////////////////////////////////////////////////
for( uint32_t taskNum = 0 ; taskNum < kNumInflateTasks ; taskNum++ )
{
uint32_t contextSaveSize = edgeZlibGetInflateTaskContextSaveSize();
s_pInflateTaskContext[taskNum] = memalign( CELL_SPURS_TASK_CONTEXT_ALIGN, contextSaveSize );
edgeZlibCreateInflateTask( &s_taskSetInflate, s_pInflateTaskContext[taskNum], s_inflateQueue );
}
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: %d Inflate Task(s) started\n", kNumInflateTasks );
#endif
}
}
bool EdgeZLib::Compress(void* pDestination, uint32_t* pDestSize, const void* pSource, uint32_t SrcSize)
{
EnterCriticalSection(&s_critSect);
//////////////////////////////////////////////////////////////////////////
//
// Add one item to the Deflate Queue.
// The Deflate Task will wake up and process this work.
//
//////////////////////////////////////////////////////////////////////////
uint32_t* pDst = nullptr;
bool findingSizeOnly = false;
if(pDestination == nullptr && *pDestSize == 0)
{
pDst = (uint32_t*)malloc(SrcSize);
findingSizeOnly = true;
*pDestSize = SrcSize;
}
else
{
pDst = ((uint32_t*)pDestination);
}
pDst[0] = SrcSize; // 4 byte header added for source size
s_numElementsToCompress = 1; //Must be set correctly before any elements are added
uint16_t eventFlagBits = 1;
uint32_t compressionLevel = 9;
static uint32_t s_compressedSize; //Cannot be on stack
// create one task queue entry (max 64K deflate per entry)
edgeZlibAddDeflateQueueElement( s_deflateQueue,
pSource, SrcSize,
&pDst[1], *pDestSize,
&s_compressedSize,
&s_numElementsToCompress, //This will be decremented by 1 when this element is compressed
&s_eventFlagDeflate, //When s_numElementsToCompress decrements to zero, this event will be triggered
eventFlagBits,
compressionLevel,
kEdgeZlibDeflateTask_DeflateStoreCompressed
);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Deflate element(s) added to queue\n" );
printf_verbose( "PPU: Wait for event flag acknowledgment\n" );
#endif
//////////////////////////////////////////////////////////////////////////
//
// Wait for the event flag to be acknowledged in order to tell us the
// work is done.
//
//////////////////////////////////////////////////////////////////////////
int ret = cellSpursEventFlagWait( &s_eventFlagDeflate, &eventFlagBits, CELL_SPURS_EVENT_FLAG_AND );
PS3_ASSERT_CELL_ERROR(ret );
assert( s_numElementsToCompress == 0 ); //Should have reached zero by now
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Edge Zlib event acknowledged\n" );
#endif
// ensure Deflate task told us it stored the compressed data
assert( (s_compressedSize & kEdgeZlibDeflateTask_HowStoredMask) == kEdgeZlibDeflateTask_CompressedWasStored );
// remove bit from s_compressedSize
s_compressedSize &= ~kEdgeZlibDeflateTask_HowStoredMask;
//////////////////////////////////////////////////////////////////////////
//
// Print stats
//
//////////////////////////////////////////////////////////////////////////
#ifndef _CONTENT_PACKAGE
printf_verbose( "\n uncompSiz compSiz compress%% numTasks\n" );
printf_verbose( "___________________________________________\n" );
#endif
float compressionPercent = (1.f - (float)s_compressedSize / (float)SrcSize) * 100.f;
#ifndef _CONTENT_PACKAGE
printf_verbose( " 0x%05x 0x%05x %8.2f%% %d\n\n",
(int)SrcSize, (int)s_compressedSize, compressionPercent, kNumDeflateTasks );
#endif
*pDestSize = s_compressedSize + 4; // 4 byte header for size
if(findingSizeOnly)
free(pDst);
LeaveCriticalSection(&s_critSect);
return true;
}
void EdgeZLib::Shutdown()
{
//////////////////////////////////////////////////////////////////////////
//
// Shutdown Deflate Queue, event flag and taskset.
//
//////////////////////////////////////////////////////////////////////////
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Shutdown Deflate Queue...\n" );
#endif
edgeZlibShutdownDeflateQueue( s_deflateQueue );
int ret = cellSpursEventFlagDetachLv2EventQueue( &s_eventFlagDeflate );
PS3_ASSERT_CELL_ERROR(ret);
// shutdown taskSet
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Wait for taskset shutdown...\n" );
#endif
ret = cellSpursShutdownTaskset( &s_taskSetDeflate );
PS3_ASSERT_CELL_ERROR(ret);
// wait for all tasks to finish
ret = cellSpursJoinTaskset( &s_taskSetDeflate );
PS3_ASSERT_CELL_ERROR(ret);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Shutdown taskset completed.\n" );
#endif
// free alloc'd buffers
for( uint32_t taskNum = 0 ; taskNum < kNumDeflateTasks ; taskNum++ )
{
free( s_pDeflateTaskContext[taskNum] );
}
free( s_pDeflateQueueBuffer );
//////////////////////////////////////////////////////////////////////////
//
// Shutdown Inflate Queue, event flag and taskset.
//
//////////////////////////////////////////////////////////////////////////
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Shutdown Inflate Queue...\n" );
#endif
edgeZlibShutdownInflateQueue( s_inflateQueue );
ret = cellSpursEventFlagDetachLv2EventQueue( &s_eventFlagInflate );
PS3_ASSERT_CELL_ERROR(ret);
// shutdown taskSet
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Wait for taskset shutdown...\n" );
#endif
ret = cellSpursShutdownTaskset( &s_taskSetInflate );
PS3_ASSERT_CELL_ERROR(ret);
// wait for all tasks to finish
ret = cellSpursJoinTaskset( &s_taskSetInflate );
PS3_ASSERT_CELL_ERROR(ret);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Shutdown taskset completed.\n" );
#endif
// free alloc'd buffers
for( uint32_t taskNum = 0 ; taskNum < kNumInflateTasks ; taskNum++ )
{
free( s_pInflateTaskContext[taskNum] );
}
free( s_pInflateQueueBuffer );
}
bool EdgeZLib::Decompress(void* pDestination, uint32_t* pDestSize, const void* pSource, uint32_t SrcSize)
{
#if 1
EnterCriticalSection(&s_critSect);
const uint32_t* pSrc = (uint32_t*)pSource;
uint32_t uncompressedSize = pSrc[0];
assert(uncompressedSize <= (*pDestSize));
//////////////////////////////////////////////////////////////////////////
//
// Add one item to the Inflate Queue.
// The Inflate Task will wake up and process this work.
//
//////////////////////////////////////////////////////////////////////////
s_numElementsToDecompress = 1; //Must be set correctly before any elements are added
uint16_t eventFlagBits = 1;
// create one task queue entry (max 64K inflate per entry)
edgeZlibAddInflateQueueElement( s_inflateQueue,
&pSrc[1], SrcSize,
pDestination, uncompressedSize,
&s_numElementsToDecompress, //This will be decremented by 1 when this element is compressed
&s_eventFlagInflate, //When s_numElementsToDecompress decrements to zero, this event will be triggered
eventFlagBits,
kEdgeZlibInflateTask_Inflate
);
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Inflate element(s) added to queue\n" );
printf_verbose( "PPU: Wait for event flag acknowledgment\n" );
#endif
//////////////////////////////////////////////////////////////////////////
//
// Wait for the event flag to be acknowledged in order to tell us the
// work is done.
//
//////////////////////////////////////////////////////////////////////////
int ret = cellSpursEventFlagWait( &s_eventFlagInflate, &eventFlagBits, CELL_SPURS_EVENT_FLAG_AND );
PS3_ASSERT_CELL_ERROR(ret);
assert( s_numElementsToDecompress == 0 ); //Should have reached zero by now
#ifndef _CONTENT_PACKAGE
printf_verbose( "PPU: Edge Zlib event acknowledged\n" );
#endif
*pDestSize = uncompressedSize;
LeaveCriticalSection(&s_critSect);
return true;
#endif // 0
}
|