the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at master 419 lines 13 kB view raw
1#include "stdafx.h" 2#include <stdlib.h> 3#include "EdgeZLib.h" 4#include "edge/zlib/edgezlib_ppu.h" 5 6static CellSpurs* s_pSpurs = NULL; 7 8//Set this to 5 if you want deflate/inflate to run in parallel on 5 SPUs. 9 10const uint32_t kNumDeflateTasks = 1; 11const uint32_t kMaxNumDeflateQueueEntries = 64; 12static CellSpursEventFlag s_eventFlagDeflate; //Cannot be on stack 13static CellSpursTaskset s_taskSetDeflate; //Cannot be on stack 14static EdgeZlibDeflateQHandle s_deflateQueue; 15static void* s_pDeflateQueueBuffer = NULL; 16static void* s_pDeflateTaskContext[kNumDeflateTasks]; 17static uint32_t s_numElementsToCompress; //Cannot be on stack 18 19 20const uint32_t kNumInflateTasks = 1; 21const uint32_t kMaxNumInflateQueueEntries = 64; 22static CellSpursEventFlag s_eventFlagInflate; //Cannot be on stack 23static CellSpursTaskset s_taskSetInflate; //Cannot be on stack 24static EdgeZlibInflateQHandle s_inflateQueue; 25static void* s_pInflateQueueBuffer = NULL; 26static void* s_pInflateTaskContext[kNumInflateTasks]; 27static uint32_t s_numElementsToDecompress; //Cannot be on stack 28 29 30static CRITICAL_SECTION s_critSect; 31 32static const bool sc_verbose = false; 33 34#define printf_verbose(...) { if(sc_verbose){ printf(__VA_ARGS__);} } 35 36 37 38void EdgeZLib::Init(CellSpurs* pSpurs) 39{ 40 s_pSpurs = pSpurs; 41 InitializeCriticalSection(&s_critSect); 42 43 ////////////////////////////////////////////////////////////////////////// 44 // 45 // Initialize Deflate Queue. 46 // This will hold the queue of work that the Deflate Task(s) on SPU will 47 // pull work from. 48 // 49 ////////////////////////////////////////////////////////////////////////// 50 { 51 52 // register taskSet 53 CellSpursTasksetAttribute taskSetAttribute; 54 uint8_t prios[8] = {15, 15, 15, 15, 15, 15, 0, 0}; 55 int ret = cellSpursTasksetAttributeInitialize( &taskSetAttribute, 0, prios, 8 ); 56 PS3_ASSERT_CELL_ERROR(ret); 57 ret = cellSpursTasksetAttributeSetName(&taskSetAttribute, "edgeZlibDeflateTaskSet"); 58 PS3_ASSERT_CELL_ERROR(ret); 59 ret = cellSpursCreateTasksetWithAttribute( pSpurs, &s_taskSetDeflate, &taskSetAttribute ); 60 PS3_ASSERT_CELL_ERROR(ret); 61 62 ret = cellSpursEventFlagInitializeIWL( pSpurs, 63 &s_eventFlagDeflate, 64 CELL_SPURS_EVENT_FLAG_CLEAR_AUTO, 65 CELL_SPURS_EVENT_FLAG_SPU2PPU ); 66 PS3_ASSERT_CELL_ERROR(ret); 67 68 ret = cellSpursEventFlagAttachLv2EventQueue( &s_eventFlagDeflate ); 69 PS3_ASSERT_CELL_ERROR(ret); 70#ifndef _CONTENT_PACKAGE 71 printf_verbose( "PPU: Event flag created\n" ); 72#endif 73 74 uint32_t deflateQueueBuffSize = edgeZlibGetDeflateQueueSize( kMaxNumDeflateQueueEntries ); 75 s_pDeflateQueueBuffer = memalign( EDGE_ZLIB_DEFLATE_QUEUE_ALIGN, deflateQueueBuffSize ); 76 s_deflateQueue = edgeZlibCreateDeflateQueue( 77 s_pSpurs, 78 kMaxNumDeflateQueueEntries, 79 s_pDeflateQueueBuffer, 80 deflateQueueBuffSize ); 81#ifndef _CONTENT_PACKAGE 82 printf_verbose( "PPU: Deflate Queue created\n" ); 83#endif 84 85 ////////////////////////////////////////////////////////////////////////// 86 // 87 // Build Deflate Tasks. 88 // We want the compression to be able to run in parallel on multiple 89 // SPUs so we create as many tasks as SPUs that we want it to run 90 // on (kNumDeflateTasks). 91 // 92 ////////////////////////////////////////////////////////////////////////// 93 94 s_pDeflateTaskContext[kNumDeflateTasks]; 95 for( uint32_t taskNum = 0 ; taskNum < kNumDeflateTasks ; taskNum++ ) 96 { 97 uint32_t contextSaveSize = edgeZlibGetDeflateTaskContextSaveSize(); 98 s_pDeflateTaskContext[taskNum] = memalign( CELL_SPURS_TASK_CONTEXT_ALIGN, contextSaveSize ); 99 edgeZlibCreateDeflateTask( &s_taskSetDeflate, s_pDeflateTaskContext[taskNum], s_deflateQueue ); 100 } 101#ifndef _CONTENT_PACKAGE 102 printf_verbose( "PPU: %d Deflate Task(s) started\n", kNumDeflateTasks ); 103#endif 104 } 105 106 107 108 { 109 110 111 112 ////////////////////////////////////////////////////////////////////////// 113 // 114 // Initialize taskset and event flag 115 // 116 ////////////////////////////////////////////////////////////////////////// 117 118 // register taskSet 119 CellSpursTasksetAttribute taskSetAttribute; 120 uint8_t prios[8] = {15, 15, 15, 15, 15, 15, 0, 0}; 121 int ret = cellSpursTasksetAttributeInitialize( &taskSetAttribute, 0, prios, 8 ); 122 PS3_ASSERT_CELL_ERROR(ret); 123 ret = cellSpursTasksetAttributeSetName(&taskSetAttribute, "edgeZlibInflateTaskSet"); 124 PS3_ASSERT_CELL_ERROR(ret); 125 ret = cellSpursCreateTasksetWithAttribute( s_pSpurs, &s_taskSetInflate, &taskSetAttribute ); 126 PS3_ASSERT_CELL_ERROR(ret); 127#ifndef _CONTENT_PACKAGE 128 printf_verbose( "PPU: Inflate Taskset created\n" ); 129#endif 130 131 ret = cellSpursEventFlagInitializeIWL( s_pSpurs, 132 &s_eventFlagInflate, 133 CELL_SPURS_EVENT_FLAG_CLEAR_AUTO, 134 CELL_SPURS_EVENT_FLAG_SPU2PPU ); 135 PS3_ASSERT_CELL_ERROR(ret); 136 137 ret = cellSpursEventFlagAttachLv2EventQueue( &s_eventFlagInflate ); 138 PS3_ASSERT_CELL_ERROR(ret); 139#ifndef _CONTENT_PACKAGE 140 printf_verbose( "PPU: Event flag created\n" ); 141#endif 142 143 ////////////////////////////////////////////////////////////////////////// 144 // 145 // Initialize Inflate Queue. 146 // This will hold the queue of work that the Inflate Task(s) on SPU will 147 // pull work from. 148 // 149 ////////////////////////////////////////////////////////////////////////// 150 151 uint32_t inflateQueueBuffSize = edgeZlibGetInflateQueueSize( kMaxNumInflateQueueEntries ); 152 s_pInflateQueueBuffer = memalign( EDGE_ZLIB_INFLATE_QUEUE_ALIGN, inflateQueueBuffSize ); 153 s_inflateQueue = edgeZlibCreateInflateQueue( 154 s_pSpurs, 155 kMaxNumInflateQueueEntries, 156 s_pInflateQueueBuffer, 157 inflateQueueBuffSize ); 158#ifndef _CONTENT_PACKAGE 159 printf_verbose( "PPU: Inflate Queue created\n" ); 160#endif 161 ////////////////////////////////////////////////////////////////////////// 162 // 163 // Build Inflate Tasks. 164 // We want the compression to be able to run in parallel on multiple 165 // SPUs so we create as many tasks as SPUs that we want it to run 166 // on (kNumInflateTasks). 167 // 168 ////////////////////////////////////////////////////////////////////////// 169 170 for( uint32_t taskNum = 0 ; taskNum < kNumInflateTasks ; taskNum++ ) 171 { 172 uint32_t contextSaveSize = edgeZlibGetInflateTaskContextSaveSize(); 173 s_pInflateTaskContext[taskNum] = memalign( CELL_SPURS_TASK_CONTEXT_ALIGN, contextSaveSize ); 174 edgeZlibCreateInflateTask( &s_taskSetInflate, s_pInflateTaskContext[taskNum], s_inflateQueue ); 175 } 176#ifndef _CONTENT_PACKAGE 177 printf_verbose( "PPU: %d Inflate Task(s) started\n", kNumInflateTasks ); 178#endif 179 } 180 181} 182 183 184 185 186 187 188bool EdgeZLib::Compress(void* pDestination, uint32_t* pDestSize, const void* pSource, uint32_t SrcSize) 189{ 190 EnterCriticalSection(&s_critSect); 191 192 ////////////////////////////////////////////////////////////////////////// 193 // 194 // Add one item to the Deflate Queue. 195 // The Deflate Task will wake up and process this work. 196 // 197 ////////////////////////////////////////////////////////////////////////// 198 uint32_t* pDst = NULL; 199 bool findingSizeOnly = false; 200 if(pDestination == NULL && *pDestSize == 0) 201 { 202 pDst = (uint32_t*)malloc(SrcSize); 203 findingSizeOnly = true; 204 *pDestSize = SrcSize; 205 } 206 else 207 { 208 pDst = ((uint32_t*)pDestination); 209 } 210 211 pDst[0] = SrcSize; // 4 byte header added for source size 212 213 214 s_numElementsToCompress = 1; //Must be set correctly before any elements are added 215 216 uint16_t eventFlagBits = 1; 217 218 uint32_t compressionLevel = 9; 219 static uint32_t s_compressedSize; //Cannot be on stack 220 221 // create one task queue entry (max 64K deflate per entry) 222 edgeZlibAddDeflateQueueElement( s_deflateQueue, 223 pSource, SrcSize, 224 &pDst[1], *pDestSize, 225 &s_compressedSize, 226 &s_numElementsToCompress, //This will be decremented by 1 when this element is compressed 227 &s_eventFlagDeflate, //When s_numElementsToCompress decrements to zero, this event will be triggered 228 eventFlagBits, 229 compressionLevel, 230 kEdgeZlibDeflateTask_DeflateStoreCompressed 231 ); 232 233#ifndef _CONTENT_PACKAGE 234 printf_verbose( "PPU: Deflate element(s) added to queue\n" ); 235 236 printf_verbose( "PPU: Wait for event flag acknowledgment\n" ); 237#endif 238 239 ////////////////////////////////////////////////////////////////////////// 240 // 241 // Wait for the event flag to be acknowledged in order to tell us the 242 // work is done. 243 // 244 ////////////////////////////////////////////////////////////////////////// 245 246 int ret = cellSpursEventFlagWait( &s_eventFlagDeflate, &eventFlagBits, CELL_SPURS_EVENT_FLAG_AND ); 247 PS3_ASSERT_CELL_ERROR(ret ); 248 assert( s_numElementsToCompress == 0 ); //Should have reached zero by now 249 250#ifndef _CONTENT_PACKAGE 251 printf_verbose( "PPU: Edge Zlib event acknowledged\n" ); 252#endif 253 // ensure Deflate task told us it stored the compressed data 254 assert( (s_compressedSize & kEdgeZlibDeflateTask_HowStoredMask) == kEdgeZlibDeflateTask_CompressedWasStored ); 255 // remove bit from s_compressedSize 256 s_compressedSize &= ~kEdgeZlibDeflateTask_HowStoredMask; 257 258 259 260 261 ////////////////////////////////////////////////////////////////////////// 262 // 263 // Print stats 264 // 265 ////////////////////////////////////////////////////////////////////////// 266 267#ifndef _CONTENT_PACKAGE 268 printf_verbose( "\n uncompSiz compSiz compress%% numTasks\n" ); 269 printf_verbose( "___________________________________________\n" ); 270#endif 271 float compressionPercent = (1.f - (float)s_compressedSize / (float)SrcSize) * 100.f; 272 273#ifndef _CONTENT_PACKAGE 274 printf_verbose( " 0x%05x 0x%05x %8.2f%% %d\n\n", 275 (int)SrcSize, (int)s_compressedSize, compressionPercent, kNumDeflateTasks ); 276#endif 277 278 *pDestSize = s_compressedSize + 4; // 4 byte header for size 279 if(findingSizeOnly) 280 free(pDst); 281 LeaveCriticalSection(&s_critSect); 282 283 return true; 284} 285 286void EdgeZLib::Shutdown() 287{ 288 ////////////////////////////////////////////////////////////////////////// 289 // 290 // Shutdown Deflate Queue, event flag and taskset. 291 // 292 ////////////////////////////////////////////////////////////////////////// 293 294#ifndef _CONTENT_PACKAGE 295 printf_verbose( "PPU: Shutdown Deflate Queue...\n" ); 296#endif 297 edgeZlibShutdownDeflateQueue( s_deflateQueue ); 298 299 int ret = cellSpursEventFlagDetachLv2EventQueue( &s_eventFlagDeflate ); 300 PS3_ASSERT_CELL_ERROR(ret); 301 302 // shutdown taskSet 303#ifndef _CONTENT_PACKAGE 304 printf_verbose( "PPU: Wait for taskset shutdown...\n" ); 305#endif 306 ret = cellSpursShutdownTaskset( &s_taskSetDeflate ); 307 PS3_ASSERT_CELL_ERROR(ret); 308 // wait for all tasks to finish 309 ret = cellSpursJoinTaskset( &s_taskSetDeflate ); 310 PS3_ASSERT_CELL_ERROR(ret); 311#ifndef _CONTENT_PACKAGE 312 printf_verbose( "PPU: Shutdown taskset completed.\n" ); 313#endif 314 // free alloc'd buffers 315 for( uint32_t taskNum = 0 ; taskNum < kNumDeflateTasks ; taskNum++ ) 316 { 317 free( s_pDeflateTaskContext[taskNum] ); 318 } 319 free( s_pDeflateQueueBuffer ); 320 321 322 ////////////////////////////////////////////////////////////////////////// 323 // 324 // Shutdown Inflate Queue, event flag and taskset. 325 // 326 ////////////////////////////////////////////////////////////////////////// 327 328#ifndef _CONTENT_PACKAGE 329 printf_verbose( "PPU: Shutdown Inflate Queue...\n" ); 330#endif 331 edgeZlibShutdownInflateQueue( s_inflateQueue ); 332 333 ret = cellSpursEventFlagDetachLv2EventQueue( &s_eventFlagInflate ); 334 PS3_ASSERT_CELL_ERROR(ret); 335 336 // shutdown taskSet 337#ifndef _CONTENT_PACKAGE 338 printf_verbose( "PPU: Wait for taskset shutdown...\n" ); 339#endif 340 ret = cellSpursShutdownTaskset( &s_taskSetInflate ); 341 PS3_ASSERT_CELL_ERROR(ret); 342 // wait for all tasks to finish 343 ret = cellSpursJoinTaskset( &s_taskSetInflate ); 344 PS3_ASSERT_CELL_ERROR(ret); 345#ifndef _CONTENT_PACKAGE 346 printf_verbose( "PPU: Shutdown taskset completed.\n" ); 347#endif 348 349 // free alloc'd buffers 350 for( uint32_t taskNum = 0 ; taskNum < kNumInflateTasks ; taskNum++ ) 351 { 352 free( s_pInflateTaskContext[taskNum] ); 353 } 354 free( s_pInflateQueueBuffer ); 355 356 357} 358 359bool EdgeZLib::Decompress(void* pDestination, uint32_t* pDestSize, const void* pSource, uint32_t SrcSize) 360{ 361 362#if 1 363 EnterCriticalSection(&s_critSect); 364 365 const uint32_t* pSrc = (uint32_t*)pSource; 366 uint32_t uncompressedSize = pSrc[0]; 367 assert(uncompressedSize <= (*pDestSize)); 368 369 370 371 ////////////////////////////////////////////////////////////////////////// 372 // 373 // Add one item to the Inflate Queue. 374 // The Inflate Task will wake up and process this work. 375 // 376 ////////////////////////////////////////////////////////////////////////// 377 378 s_numElementsToDecompress = 1; //Must be set correctly before any elements are added 379 380 uint16_t eventFlagBits = 1; 381 382 // create one task queue entry (max 64K inflate per entry) 383 edgeZlibAddInflateQueueElement( s_inflateQueue, 384 &pSrc[1], SrcSize, 385 pDestination, uncompressedSize, 386 &s_numElementsToDecompress, //This will be decremented by 1 when this element is compressed 387 &s_eventFlagInflate, //When s_numElementsToDecompress decrements to zero, this event will be triggered 388 eventFlagBits, 389 kEdgeZlibInflateTask_Inflate 390 ); 391 392#ifndef _CONTENT_PACKAGE 393 printf_verbose( "PPU: Inflate element(s) added to queue\n" ); 394 395 printf_verbose( "PPU: Wait for event flag acknowledgment\n" ); 396#endif 397 398 ////////////////////////////////////////////////////////////////////////// 399 // 400 // Wait for the event flag to be acknowledged in order to tell us the 401 // work is done. 402 // 403 ////////////////////////////////////////////////////////////////////////// 404 405 int ret = cellSpursEventFlagWait( &s_eventFlagInflate, &eventFlagBits, CELL_SPURS_EVENT_FLAG_AND ); 406 PS3_ASSERT_CELL_ERROR(ret); 407 assert( s_numElementsToDecompress == 0 ); //Should have reached zero by now 408 409#ifndef _CONTENT_PACKAGE 410 printf_verbose( "PPU: Edge Zlib event acknowledged\n" ); 411#endif 412 413 *pDestSize = uncompressedSize; 414 LeaveCriticalSection(&s_critSect); 415 416 return true; 417 418#endif // 0 419}