Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.87 KB | None | 0 0
  1. #include <3ds.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. #include "screen_dump_1bpp_bin.h"
  8. #include "screen_dump_4bpp_bin.h"
  9. #include "screen_dump_8bpp_bin.h"
  10.  
  11. #include "screen_dump_4bpp_pal_bin.h"
  12. #include "screen_dump_8bpp_pal_bin.h"
  13.  
  14. #define TimesToRun 100
  15.  
  16. u32 Keys_Held = 0;
  17. u32 Keys_Down = 0;
  18. u32 Keys_Up = 0;
  19.  
  20. static u32 FBConvTable[ 256 ][ 8 ];
  21. static u32 ScreenOut[ 512 * 512 * 4 ];
  22.  
  23. static u16 CLUT_reds[ 256 ];
  24. static u16 CLUT_greens[ 256 ];
  25. static u16 CLUT_blues[ 256 ];
  26.  
  27. s32 MainThreadPriority = 0;
  28.  
  29. void LoadCLUT4BPP( void ) {
  30.     int i = 0;
  31.  
  32.     for ( i = 0; i < 16; i++ ) {
  33.         CLUT_reds[ i ] = screen_dump_4bpp_pal_bin[ ( i * 3 ) + 0 ] << 8;
  34.         CLUT_greens[ i ] = screen_dump_4bpp_pal_bin[ ( i * 3 ) + 1 ] << 8;
  35.         CLUT_blues[ i ] = screen_dump_4bpp_pal_bin[ ( i * 3 ) + 2 ] << 8;
  36.     }
  37. }
  38.  
  39. void LoadCLUT8BPP( void ) {
  40.     int i = 0;
  41.  
  42.     for ( i = 0; i < 256; i++ ) {
  43.         CLUT_reds[ i ] = screen_dump_8bpp_pal_bin[ ( i * 3 ) + 0 ] << 8;
  44.         CLUT_greens[ i ] = screen_dump_8bpp_pal_bin[ ( i * 3 ) + 1 ] << 8;
  45.         CLUT_blues[ i ] = screen_dump_8bpp_pal_bin[ ( i * 3 ) + 2 ] << 8;
  46.     }
  47. }
  48.  
  49. void MakeTable1BPP( void ) {
  50.     int i = 0;
  51.     int j = 0;    
  52.  
  53.     for ( i = 0; i < 256; i++ ) {
  54.         for ( j = 0; j <= 7; j++ ) {
  55.             FBConvTable[ i ][ j ] = ( i & BIT( j ) ) ? 0xFF : 0xFFFFFFFF;
  56.         }
  57.     }
  58. }
  59.  
  60. void MakeTable4BPP( void ) {
  61.     int r, g, b = 0;
  62.     int l, h = 0;
  63.     int i = 0;
  64.  
  65.     LoadCLUT4BPP( );
  66.  
  67.     for ( i = 0; i < 256; i++ ) {
  68.         l = ( i >> 4 );
  69.         h = ( i & 0x0F );
  70.  
  71.         r = ( CLUT_reds[ l ] >> 8 ) & 0xFF;
  72.         g = ( CLUT_greens[ l ] >> 8 ) & 0xFF;
  73.         b = ( CLUT_blues[ l ] >> 8 ) & 0xFF;
  74.  
  75.         FBConvTable[ i ][ 0 ] = ( r << 24 ) | ( g << 16 ) | ( b << 8 ) | 0xFF;
  76.  
  77.         r = ( CLUT_reds[ h ] >> 8 ) & 0xFF;
  78.         g = ( CLUT_greens[ h ] >> 8 ) & 0xFF;
  79.         b = ( CLUT_blues[ h ] >> 8 ) & 0xFF;        
  80.  
  81.         FBConvTable[ i ][ 1 ] = ( r << 24 ) | ( g << 16 ) | ( b << 8 ) | 0xFF;
  82.     }
  83. }
  84.  
  85. void MakeTable8BPP( void ) {
  86.     int i = 0;
  87.  
  88.     LoadCLUT8BPP( );
  89.  
  90.     for ( i = 0; i < 256; i++ ) {
  91.         FBConvTable[ i ][ 0 ] = \
  92.         ( ( ( CLUT_reds[ i ] >> 8 ) & 0xFF ) << 24 ) | \
  93.         ( ( ( CLUT_greens[ i ] >> 8 ) & 0xFF ) << 16 ) | \
  94.         ( ( ( CLUT_blues[ i ] >> 8 ) & 0xFF ) << 8 ) | \
  95.         0xFF;
  96.     }
  97. }
  98.  
  99. struct ConvertStruct {
  100.     u8* Src;
  101.     u32* Dest;
  102.     int Size;
  103. };
  104.  
  105. void Convert1BPP_8BPP( u8* Src, u32* Dest, int SrcSizeInBytes ) {
  106.     while ( SrcSizeInBytes-- ) {
  107.         memcpy( Dest, &FBConvTable[ *Src ], 8 * sizeof( u32 ) );
  108.  
  109.         Dest+= 8;
  110.         Src++;
  111.     }
  112. }
  113.  
  114. void ConvertIRGB4_To_RGBA8( u8* Src, u32* Dest, int SrcSizeInBytes ) {
  115.     while ( SrcSizeInBytes-- ) {
  116.         *Dest++ = FBConvTable[ *Src ][ 0 ];
  117.         *Dest++ = FBConvTable[ *Src++ ][ 1 ];
  118.     }
  119. }
  120.  
  121. void Convert4BPPThread( void* Param ) {
  122.     struct ConvertStruct* CS = ( struct ConvertStruct* ) Param;
  123.  
  124.     ConvertIRGB4_To_RGBA8( CS->Src, CS->Dest, CS->Size );
  125. }
  126.  
  127. int IRGB4_To_RGBA8( u8* Src, u32* Dest, int Size ) {
  128.     struct ConvertStruct CS;
  129.     Thread T = NULL;
  130.  
  131.     CS.Src = Src;
  132.     CS.Dest = Dest;
  133.     CS.Size = Size / 2;
  134.  
  135.     T = threadCreate( Convert4BPPThread, ( void* ) &CS, 4096, MainThreadPriority - 2, 1, false );
  136.  
  137.     ConvertIRGB4_To_RGBA8( &Src[ Size / 2 ], &Dest[ Size / 8 ], Size / 2 );
  138.  
  139.     if ( T != NULL ) {
  140.         threadJoin( T, UINT64_MAX );
  141.         threadFree( T );
  142.     } else {
  143.         printf( "threadCreate: Error\n" );
  144.     }
  145.  
  146.     return T == NULL ? 0 : 1;
  147. }
  148.  
  149. void ConvertIRGB8_To_RGBA8( u8* Src, u32* Dest, int SrcSizeInBytes ) {
  150.     while ( SrcSizeInBytes-- ) {
  151.         *Dest++ = FBConvTable[ *Src++ ][ 0 ];
  152.     }
  153. }
  154.  
  155. void Convert8BPPThread( void* Param ) {
  156.     struct ConvertStruct* CS = ( struct ConvertStruct* ) Param;
  157.  
  158.     ConvertIRGB8_To_RGBA8( CS->Src, CS->Dest, CS->Size );  
  159. }
  160.  
  161. /*
  162.  * Converts an indexed RGB888 image to an RGBA888 image and
  163.  * splits the workload between syscore and appcore.
  164.  */
  165. int IRGB8_To_RGBA8( u8* Src, u32* Dest, int Size ) {
  166.     struct ConvertStruct CS;
  167.     Thread T = NULL;
  168.  
  169.     /* Set up the params for the syscore to use the 2nd
  170.      * half of the buffer.
  171.      */
  172.     CS.Src = Src;
  173.     CS.Dest = Dest;
  174.     CS.Size = Size / 2;
  175.  
  176.     /* Create a syscore thread */
  177.     T = threadCreate( Convert8BPPThread, ( void* ) &CS, 4096, MainThreadPriority - 1, 1, false );
  178.  
  179.     /* While syscore is running the thread, run this normally for
  180.      * the first half of the buffer.
  181.      */
  182.     ConvertIRGB8_To_RGBA8( &Src[ Size / 2 ], &Dest[ Size / 8 ], Size / 2 );
  183.  
  184.     if ( T != NULL ) {
  185.         /* Wait for syscore thread to finish */
  186.         threadJoin( T, U64_MAX );
  187.         threadFree( T );
  188.     } else {
  189.         printf( "threadCreate: Error\n" );
  190.     }
  191.  
  192.     return T == NULL ? 0 : 1;
  193. }
  194.  
  195. void Test1BPPSpeed( void ) {
  196.     u64 StartTime = 0;
  197.     u64 EndTime = 0;
  198.     int i = 0;
  199.  
  200.     MakeTable1BPP( );
  201.  
  202.     StartTime = osGetTime( );
  203.  
  204.     for ( i = 0; i < TimesToRun; i++ ) {
  205.         Convert1BPP_8BPP( ( u8* ) screen_dump_1bpp_bin, ScreenOut, screen_dump_1bpp_bin_size );
  206.     }
  207.  
  208.     EndTime = osGetTime( );
  209.  
  210.     printf( "1BPP Conversion took %.1fms\n", ( ( float ) ( EndTime - StartTime ) ) / ( ( float ) TimesToRun ) );
  211. }
  212.  
  213. void Test4BPPSpeed( void ) {
  214.     u64 StartTime = 0;
  215.     u64 EndTime = 0;
  216.     int i = 0;
  217.  
  218.     MakeTable4BPP( );
  219.  
  220.     StartTime = osGetTime( );
  221.  
  222.     for ( i = 0; i < TimesToRun; i++ ) {
  223.         if ( IRGB4_To_RGBA8( ( u8* ) screen_dump_4bpp_bin, ScreenOut, screen_dump_4bpp_bin_size ) == 0 )
  224.             break;
  225.     }
  226.  
  227.     EndTime = osGetTime( );
  228.  
  229.     printf( "4BPP Conversion took %.1fms\n", ( ( float ) ( EndTime - StartTime ) ) / ( ( float ) TimesToRun ) );
  230. }
  231.  
  232. void Test8BPPSpeed( void ) {
  233.     u64 StartTime = 0;
  234.     u64 EndTime = 0;
  235.     int i = 0;
  236.  
  237.     MakeTable8BPP( );
  238.  
  239.     StartTime = osGetTime( );
  240.  
  241.     for ( i = 0; i < TimesToRun; i++ ) {
  242.         if ( IRGB8_To_RGBA8( ( u8* ) screen_dump_8bpp_bin, ScreenOut, screen_dump_8bpp_bin_size ) == 0 )
  243.             break;
  244.     }
  245.  
  246.     EndTime = osGetTime( );
  247.  
  248.     printf( "8BPP Conversion took %.1fms\n", ( ( float ) ( EndTime - StartTime ) ) / ( ( float ) TimesToRun ) );
  249. }
  250.  
  251. int main( void ) {
  252.     gfxInitDefault( );
  253.     consoleInit( GFX_TOP, NULL );
  254.  
  255.     printf( "Ready...\n" );
  256.  
  257.     svcGetThreadPriority( &MainThreadPriority, CUR_THREAD_HANDLE );
  258.     APT_SetAppCpuTimeLimit( 80 );
  259.  
  260.     Test1BPPSpeed( );
  261.     Test4BPPSpeed( );
  262.     Test8BPPSpeed( );
  263.  
  264.     while ( aptMainLoop( ) ) {
  265.         hidScanInput( );
  266.  
  267.         Keys_Held = hidKeysHeld( );
  268.         Keys_Down = hidKeysDown( );
  269.         Keys_Up = hidKeysUp( );
  270.  
  271.         if ( Keys_Down & KEY_START )
  272.             break;
  273.  
  274.         gfxSwapBuffers( );
  275.         gfxFlushBuffers( );
  276.         gspWaitForVBlank( );
  277.     }
  278.  
  279.     gfxExit( );
  280.  
  281.     return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement