Advertisement
xerpi

ya2d_texture before cleaning shit

Mar 20th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 KB | None | 0 0
  1. #include "ya2d_texture.h"
  2.  
  3. //Simple draw
  4.     //Non swizzled
  5.         //GU_SPRITES: 28.54 FPS
  6.         //GU_TRIANGLE_STRIP: 37.36 FPS
  7.     //Swizzled
  8.         //GU_SPRITES: 89.91 FPS
  9.         //GU_TRIANGLE_STRIP: 110.34 FPS
  10. //Fast draw
  11.     //Non swizzled
  12.         //GU_SPRITES: 148.26 FPS
  13.         //GU_TRIANGLE_STRIP: 142.57 FPS
  14.     //Swizzled
  15.         //GU_SPRITES: 264.74 FPS
  16.         //GU_TRIANGLE_STRIP: 263.21 FPS
  17.  
  18.  
  19.     void ya2d_createTexture(ya2d_Texture *texp, int w, int h, int psm)
  20.     {
  21.         texp->imageWidth  = w;
  22.         texp->imageHeight = h;
  23.         texp->textureWidth  = next_pow2(w);
  24.         texp->textureHeight = next_pow2(h);
  25.         texp->centerX = (int)(w/2);
  26.         texp->centerY = (int)(h/2);
  27.  
  28.         texp->texPSM = psm;
  29.         texp->isSwizzled = 0;
  30.         texp->hasAlpha   = 1;
  31.        
  32.         switch(psm)
  33.         {
  34.         case GU_PSM_8888:
  35.         default:
  36.             texp->rowBytes = texp->textureWidth * 4;
  37.             break;
  38.         case GU_PSM_4444:
  39.         case GU_PSM_5650:
  40.         case GU_PSM_5551:
  41.             texp->rowBytes = texp->textureWidth * 2;
  42.             break;
  43.         }
  44.        
  45.         texp->dataLength = texp->rowBytes * texp->textureHeight;
  46.         texp->data = (uint8_t *)memalign(16, texp->dataLength);
  47.         memset(texp->data, 0, texp->dataLength);
  48.     }
  49.    
  50.     void ya2d_swizzleTexture(ya2d_Texture *texp)
  51.     {
  52.         if(texp->isSwizzled || (texp->textureWidth < 33 && texp->textureHeight < 33)) return;
  53.         uint8_t *swizzledData = (uint8_t *)memalign(16, texp->dataLength);
  54.         swizzle_fast(swizzledData, texp->data, texp->textureWidth * 4, texp->textureHeight);
  55.         memcpy(texp->data, swizzledData, texp->dataLength);
  56.         free(swizzledData);
  57.         texp->isSwizzled = 1;
  58.     }
  59.  
  60.     void ya2d_freeTexture(ya2d_Texture *texp)
  61.     {
  62.         if(texp->data != NULL)
  63.         {
  64.             free(texp->data);
  65.             texp->data = NULL;
  66.         }
  67.     }
  68.  
  69.     void ya2d_drawTexture(ya2d_Texture *texp, int x, int y)
  70.     {
  71.         if(!texp->data) return;
  72.  
  73.         sceGuEnable(GU_TEXTURE_2D);
  74.         sceGumPushMatrix();
  75.         sceGumLoadIdentity();        
  76.  
  77.         if(texp->isSwizzled)
  78.             sceGuTexMode(texp->texPSM, 0, 0, GU_TRUE);
  79.         else
  80.             sceGuTexMode(texp->texPSM, 0, 0, GU_FALSE);
  81.                    
  82.         if(texp->hasAlpha)
  83.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
  84.         else
  85.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
  86.  
  87.         sceGuTexImage(0, texp->textureWidth, texp->textureHeight, texp->textureWidth, texp->data);
  88.  
  89.         /*
  90.         ya2d_TextureVertex *vertices = (ya2d_TextureVertex *)sceGuGetMemory(2 * sizeof(ya2d_TextureVertex));
  91.  
  92.         vertices[0] = (ya2d_TextureVertex){0, 0, x, y, 0};
  93.         vertices[1] = (ya2d_TextureVertex){texp->textureWidth, texp->textureHeight, x+texp->textureWidth, y+texp->textureHeight, 0};
  94.  
  95.         sceGumDrawArray(GU_SPRITES, GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 2, 0, vertices);
  96.        
  97.         sceKernelDcacheWritebackRange(vertices, 2 * sizeof(ya2d_TextureVertex));
  98.         */
  99.  
  100.         /*
  101.         ya2d_FloatTextureVertex *vertices = (ya2d_FloatTextureVertex *)sceGuGetMemory(4 * sizeof(ya2d_FloatTextureVertex));
  102.  
  103.         vertices[0] = (ya2d_FloatTextureVertex){0.0f, 0.0f, x, y, 0.0f};
  104.         vertices[1] = (ya2d_FloatTextureVertex){1.0f, 0.0f, x+texp->textureWidth, y, 0.0f};
  105.         vertices[2] = (ya2d_FloatTextureVertex){0.0f, 1.0f, x, y+texp->textureHeight, 0.0f};
  106.         vertices[3] = (ya2d_FloatTextureVertex){1.0f, 1.0f, x+texp->textureWidth, y+texp->textureHeight, 0.0f};
  107.  
  108.         sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D, 4, 0, vertices);
  109.        
  110.         sceKernelDcacheWritebackRange(vertices, 4 * sizeof(ya2d_FloatTextureVertex));
  111.         */
  112.        
  113.        
  114.         ya2d_TextureVertex *vertices = (ya2d_TextureVertex *)sceGuGetMemory(4 * sizeof(ya2d_TextureVertex));
  115.  
  116.         vertices[0] = (ya2d_TextureVertex){0, 0, x, y, 0};
  117.         vertices[1] = (ya2d_TextureVertex){texp->textureWidth, 0, x+texp->textureWidth, y, 0};
  118.         vertices[2] = (ya2d_TextureVertex){0, texp->textureHeight, x, y+texp->textureHeight, 0};
  119.         vertices[3] = (ya2d_TextureVertex){texp->textureWidth, texp->textureHeight, x+texp->textureWidth, y+texp->textureHeight, 0};
  120.  
  121.         sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 4, 0, vertices);
  122.        
  123.         sceKernelDcacheWritebackRange(vertices, 4 * sizeof(ya2d_TextureVertex));
  124.        
  125.         sceGumPopMatrix();
  126.     }
  127.  
  128.     void ya2d_drawTextureFast(ya2d_Texture *texp, int x, int y)
  129.     {
  130.         if(texp->textureWidth <= YA2D_TEXTURE_SLICE) ya2d_drawTexture(texp, x, y);
  131.         if(!texp->data) return;
  132.  
  133.         sceGuEnable(GU_TEXTURE_2D);
  134.         sceGumPushMatrix();
  135.         sceGumLoadIdentity();        
  136.  
  137.         if(texp->isSwizzled)
  138.             sceGuTexMode(texp->texPSM, 0, 0, GU_TRUE);
  139.         else
  140.             sceGuTexMode(texp->texPSM, 0, 0, GU_FALSE);
  141.                    
  142.         if(texp->hasAlpha)
  143.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
  144.         else
  145.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
  146.  
  147.         sceGuTexImage(0, texp->textureWidth, texp->textureHeight, texp->textureWidth, texp->data);
  148.        
  149.         int i;
  150.         for(i = 0; i < texp->textureWidth; i+= YA2D_TEXTURE_SLICE)
  151.         {
  152.             /*
  153.             ya2d_TextureVertex *vertices = (ya2d_TextureVertex *)sceGuGetMemory(4 * sizeof(ya2d_TextureVertex));
  154.             vertices[0] = (ya2d_TextureVertex){i, 0, x+i, y, 0};
  155.             vertices[1] = (ya2d_TextureVertex){i+YA2D_TEXTURE_SLICE, 0, x+i+YA2D_TEXTURE_SLICE, y, 0};
  156.             vertices[2] = (ya2d_TextureVertex){i, texp->textureHeight, x+i, y+texp->textureHeight, 0};
  157.             vertices[3] = (ya2d_TextureVertex){i+YA2D_TEXTURE_SLICE, texp->textureHeight, x+i+YA2D_TEXTURE_SLICE, y+texp->textureHeight, 0};
  158.             sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 4, 0, vertices);
  159.             sceKernelDcacheWritebackRange(vertices, 4 * sizeof(ya2d_TextureVertex));
  160.             */
  161.            
  162.             ya2d_TextureVertex *vertices = (ya2d_TextureVertex *)sceGuGetMemory(2 * sizeof(ya2d_TextureVertex));
  163.             vertices[0] = (ya2d_TextureVertex){i, 0, x+i, y, 0};
  164.             vertices[1] = (ya2d_TextureVertex){i+YA2D_TEXTURE_SLICE, texp->textureHeight, x+i+YA2D_TEXTURE_SLICE, y+texp->textureHeight, 0};
  165.             sceGumDrawArray(GU_SPRITES, GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 2, 0, vertices);
  166.             sceKernelDcacheWritebackRange(vertices, 2 * sizeof(ya2d_TextureVertex));
  167.         }
  168.                
  169.         sceGumPopMatrix();
  170.     }
  171.  
  172. void ya2d_drawRotateTexture(ya2d_Texture *texp, int x, int y, float angle)
  173. {
  174.     /*sceGuDisable(GU_TEXTURE_2D);
  175.         sceGumPushMatrix();
  176.         sceGumLoadIdentity();
  177.         ScePspFVector3 p = {(float)x, (float)y, 0.0f};
  178.        
  179.         ya2d_FloatVertex __attribute__((aligned(16))) vert[3] =
  180.         {
  181.                {GU_RGB(255,0,0), 0.0f, -50.0f, 0.0f}, // Top, red
  182.                {GU_RGB(0,255,0), 50.0f, 50.0f, 0.0f}, // Right, green
  183.                {GU_RGB(0,0,255), -50.0f, 50.0f, 0.0f}, // Left, blue
  184.         };
  185.         sceGumTranslate(&p);
  186.         sceGumRotateZ(angle);
  187.        
  188.         sceGumDrawArray(GU_TRIANGLES,GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,3,0,vert);
  189.  
  190.  
  191.        
  192.         sceGumLoadIdentity();
  193.         p.x += 51.0f;
  194.         sceGumTranslate(&p);
  195.         sceGumRotateZ(angle);
  196.        
  197.         sceGumDrawArray(GU_TRIANGLES,GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,3,0,vert);
  198.  
  199.         sceGumPopMatrix();
  200.         */
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement