forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help me on blit memory image with GU please!

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
restar



Joined: 01 Jan 2007
Posts: 1

PostPosted: Tue Jan 02, 2007 12:48 am    Post subject: Help me on blit memory image with GU please! Reply with quote

Hello everyone, since i'm a newbie, one thing troubled me very much, i wanted to blit a 280x272 size image to screen,the image is just a line i created it myself, but when i run the code i saw 4 lines on screen,i don't know where the bug is, please help me.
my code is quite simple, it is changed from the GU's sample blit:
Code:
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
//#include <math.h>
#include <string.h>

//#include <pspctrl.h>
#include <pspgu.h>
//#include <psprtc.h>

//#include "../common/callbacks.h"
//#include "../common/vram.h"

PSP_MODULE_INFO("Blit Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

static unsigned int __attribute__((aligned(16))) list[262144];

#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)

static unsigned short __attribute__((aligned(16))) pixels[SCR_WIDTH*SCR_HEIGHT];
//static unsigned short __attribute__((aligned(16))) swizzled_pixels[BUF_WIDTH*SCR_HEIGHT];

struct Vertex
{
   unsigned short u, v;
   unsigned short color;
   short x, y, z;
};

int done = 0;

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
      done = 1;
//     sceKernelExitGame();
      return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
      int cbid;

      cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
      sceKernelRegisterExitCallback(cbid);

      sceKernelSleepThreadCB();

      return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
      int thid = 0;

      thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
      if(thid >= 0) {
            sceKernelStartThread(thid, 0, 0);
      }

      return thid;
}

static unsigned int staticOffset = 0;

static unsigned int getMemorySize(unsigned int width, unsigned int height, unsigned int psm)
{
   switch (psm)
   {
      case GU_PSM_T4:
         return (width * height) >> 1;

      case GU_PSM_T8:
         return width * height;

      case GU_PSM_5650:
      case GU_PSM_5551:
      case GU_PSM_4444:
      case GU_PSM_T16:
         return 2 * width * height;

      case GU_PSM_8888:
      case GU_PSM_T32:
         return 4 * width * height;

      default:
         return 0;
   }
}

void* getStaticVramBuffer(unsigned int width, unsigned int height, unsigned int psm)
{
   unsigned int memSize = getMemorySize(width,height,psm);
   void* result = (void*)staticOffset;
   staticOffset += memSize;

   return result;
}

void simpleBlit(int sx, int sy, int sw, int sh, int dx, int dy)
{
   // simple blit, this just copies A->B, with all the cache-misses that apply

   struct Vertex* vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));

   vertices[0].u = sx; vertices[0].v = sy;
   vertices[0].color = 0;
   vertices[0].x = dx; vertices[0].y = dy; vertices[0].z = 0;

   vertices[1].u = sx+sw; vertices[1].v = sy+sh;
   vertices[1].color = 0;
   vertices[1].x = dx+sw; vertices[1].y = dy+sh; vertices[1].z = 0;

   sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_COLOR_4444|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
}

const char* modes[] =
{
   "normal, linear",
   "optimized, linear",
   "normal, swizzled",
   "optimized, swizzled"
};

int main(int argc, char* argv[])
{
   unsigned int x,y;

   pspDebugScreenInit();
   SetupCallbacks();

   // Setup GU

   void* fbp0 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);
   void* fbp1 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);
//   void* zbp = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);

   sceGuInit();

   sceGuStart(GU_DIRECT,list);
   sceGuDrawBuffer(GU_PSM_4444,fbp0,BUF_WIDTH);
   sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
//   sceGuDepthBuffer(zbp,BUF_WIDTH);
   sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
   sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
//   sceGuDepthRange(65535,0);
   sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
   sceGuEnable(GU_SCISSOR_TEST);
//   sceGuFrontFace(GU_CW);
   sceGuEnable(GU_TEXTURE_2D);
//   sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
   sceGuClear(GU_COLOR_BUFFER_BIT);
   sceGuFinish();
   sceGuSync(0,0);

   sceDisplayWaitVblankStart();
   sceGuDisplay(1);

   // generate dummy image to blit

/*   for (y = 0; y < SCR_HEIGHT; ++y)
   {
      unsigned short* row = &pixels[y * SCR_WIDTH];
      for (x = 0; x < SCR_WIDTH; ++x)
      {
         row[x] = x * y;
      }
   }
   
*/   int i;
   for(i = SCR_WIDTH; i<SCR_WIDTH+100;i++)
   pixels[i] = (unsigned short)0x00f0;
//   swizzle_fast((u8*)swizzled_pixels,(const u8*)pixels,BUF_WIDTH*2,SCR_HEIGHT); // 512*2 because swizzle operates in bytes, and each pixel in a 16-bit texture is 2 bytes

   sceKernelDcacheWritebackAll();

//   float curr_ms = 1.0f;
   int blit_method = 0;
   int swizzle = 0;
//   SceCtrlData oldPad;
//   oldPad.Buttons = 0;

//   sceCtrlSetSamplingCycle(0);
//   sceCtrlSetSamplingMode(0);

//   u64 last_tick;
//   sceRtcGetCurrentTick(&last_tick);
//   u32 tick_frequency = sceRtcGetTickResolution();
//   int frame_count = 0;

   while(!done)
   {
//      SceCtrlData pad;

      sceGuStart(GU_DIRECT,list);

      // switch methods if requested

//      if(sceCtrlPeekBufferPositive(&pad, 1))
//      {
//         if (pad.Buttons != oldPad.Buttons)
//         {
//            if(pad.Buttons & PSP_CTRL_CROSS)
//               blit_method ^= 1;
//            if(pad.Buttons & PSP_CTRL_CIRCLE)
//               swizzle ^= 1;
//         }
//         oldPad = pad;
//      }

      sceGuTexMode(GU_PSM_4444,0,0,swizzle); // 16-bit RGBA
      sceGuTexImage(0,SCR_WIDTH,SCR_HEIGHT,SCR_WIDTH, pixels); // setup texture as a 512x512 texture, even though the buffer is only 512x272 (480 visible)
      sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA); // don't get influenced by any vertex colors
//      sceGuTexFilter(GU_NEAREST,GU_NEAREST); // point-filtered sampling

//      if (blit_method)
//         advancedBlit(0,0,SCR_WIDTH,SCR_HEIGHT,0,0,32);
//      else
         simpleBlit(0,0,SCR_WIDTH,SCR_HEIGHT,0,0);

      sceGuFinish();
      sceGuSync(0,0);

//      float curr_fps = 1.0f / curr_ms;

//      pspDebugScreenSetOffset((int)fbp0);
//      pspDebugScreenSetXY(0,0);
//      pspDebugScreenPrintf("fps: %d.%03d (%dMB/s) (X = mode, O = swizzle) %s",(int)curr_fps,(int)((curr_fps-(int)curr_fps) * 1000.0f),(((int)curr_fps * SCR_WIDTH * SCR_HEIGHT * 2)/(1024*1024)),modes[blit_method + swizzle * 2]);

//      sceDisplayWaitVblankStart();
      fbp0 = sceGuSwapBuffers();

      // simple frame rate counter

//      ++frame_count;
//      u64 curr_tick;
//      sceRtcGetCurrentTick(&curr_tick);
//      if ((curr_tick-last_tick) >= tick_frequency)
//      {
//         float time_span = ((int)(curr_tick-last_tick)) / (float)tick_frequency;
//         curr_ms = time_span / frame_count;

//         frame_count = 0;
//         sceRtcGetCurrentTick(&last_tick);
//      }
   }

   sceGuTerm();

   sceKernelExitGame();
   return 0;
}

and the screen shot is here
[img]http://www.psp-programming.com/forums/index.php?action=dlattach;topic=1606.0;attach=145;image[/img]

any imformation will be helpful to me, thank you
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group