Help me on blit memory image with GU please!

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
restar
Posts: 1
Joined: Mon Jan 01, 2007 6:22 pm

Help me on blit memory image with GU please!

Post by restar »

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: Select all

#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&#40;"Blit Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;

static unsigned short __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; pixels&#91;SCR_WIDTH*SCR_HEIGHT&#93;;
//static unsigned short __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; swizzled_pixels&#91;BUF_WIDTH*SCR_HEIGHT&#93;;

struct Vertex
&#123;
	unsigned short u, v;
	unsigned short color;
	short x, y, z;
&#125;;

int done = 0;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
      done = 1;
//	  sceKernelExitGame&#40;&#41;;
      return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
      int cbid;

      cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
      sceKernelRegisterExitCallback&#40;cbid&#41;;

      sceKernelSleepThreadCB&#40;&#41;;

      return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
      int thid = 0;

      thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
      if&#40;thid >= 0&#41; &#123;
            sceKernelStartThread&#40;thid, 0, 0&#41;;
      &#125;

      return thid;
&#125; 

static unsigned int staticOffset = 0;

static unsigned int getMemorySize&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	switch &#40;psm&#41;
	&#123;
		case GU_PSM_T4&#58;
			return &#40;width * height&#41; >> 1;

		case GU_PSM_T8&#58;
			return width * height;

		case GU_PSM_5650&#58;
		case GU_PSM_5551&#58;
		case GU_PSM_4444&#58;
		case GU_PSM_T16&#58;
			return 2 * width * height;

		case GU_PSM_8888&#58;
		case GU_PSM_T32&#58;
			return 4 * width * height;

		default&#58;
			return 0;
	&#125;
&#125;

void* getStaticVramBuffer&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	unsigned int memSize = getMemorySize&#40;width,height,psm&#41;;
	void* result = &#40;void*&#41;staticOffset;
	staticOffset += memSize;

	return result;
&#125;

void simpleBlit&#40;int sx, int sy, int sw, int sh, int dx, int dy&#41;
&#123;
	// simple blit, this just copies A->B, with all the cache-misses that apply

	struct Vertex* vertices = &#40;struct Vertex*&#41;sceGuGetMemory&#40;2 * sizeof&#40;struct Vertex&#41;&#41;;

	vertices&#91;0&#93;.u = sx; vertices&#91;0&#93;.v = sy;
	vertices&#91;0&#93;.color = 0;
	vertices&#91;0&#93;.x = dx; vertices&#91;0&#93;.y = dy; vertices&#91;0&#93;.z = 0;

	vertices&#91;1&#93;.u = sx+sw; vertices&#91;1&#93;.v = sy+sh;
	vertices&#91;1&#93;.color = 0;
	vertices&#91;1&#93;.x = dx+sw; vertices&#91;1&#93;.y = dy+sh; vertices&#91;1&#93;.z = 0;

	sceGuDrawArray&#40;GU_SPRITES,GU_TEXTURE_16BIT|GU_COLOR_4444|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices&#41;;
&#125;

const char* modes&#91;&#93; =
&#123;
	"normal, linear",
	"optimized, linear",
	"normal, swizzled",
	"optimized, swizzled"
&#125;;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	unsigned int x,y;

	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;

	// Setup GU

	void* fbp0 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;;
	void* fbp1 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;;
//	void* zbp = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;;

	sceGuInit&#40;&#41;;

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

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;1&#41;;

	// generate dummy image to blit

/*	for &#40;y = 0; y < SCR_HEIGHT; ++y&#41;
	&#123;
		unsigned short* row = &pixels&#91;y * SCR_WIDTH&#93;;
		for &#40;x = 0; x < SCR_WIDTH; ++x&#41;
		&#123;
			row&#91;x&#93; = x * y;
		&#125;
	&#125;
	
*/	int i;
	for&#40;i = SCR_WIDTH; i<SCR_WIDTH+100;i++&#41;
	pixels&#91;i&#93; = &#40;unsigned short&#41;0x00f0;
//	swizzle_fast&#40;&#40;u8*&#41;swizzled_pixels,&#40;const u8*&#41;pixels,BUF_WIDTH*2,SCR_HEIGHT&#41;; // 512*2 because swizzle operates in bytes, and each pixel in a 16-bit texture is 2 bytes

	sceKernelDcacheWritebackAll&#40;&#41;;

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

//	sceCtrlSetSamplingCycle&#40;0&#41;;
//	sceCtrlSetSamplingMode&#40;0&#41;;

//	u64 last_tick;
//	sceRtcGetCurrentTick&#40;&last_tick&#41;;
//	u32 tick_frequency = sceRtcGetTickResolution&#40;&#41;;
//	int frame_count = 0;

	while&#40;!done&#41;
	&#123;
//		SceCtrlData pad;

		sceGuStart&#40;GU_DIRECT,list&#41;;

		// switch methods if requested

//		if&#40;sceCtrlPeekBufferPositive&#40;&pad, 1&#41;&#41;
//		&#123;
//			if &#40;pad.Buttons != oldPad.Buttons&#41;
//			&#123;
//				if&#40;pad.Buttons & PSP_CTRL_CROSS&#41;
//					blit_method ^= 1;
//				if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;
//					swizzle ^= 1;
//			&#125;
//			oldPad = pad;
//		&#125;

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

//		if &#40;blit_method&#41;
//			advancedBlit&#40;0,0,SCR_WIDTH,SCR_HEIGHT,0,0,32&#41;;
//		else
			simpleBlit&#40;0,0,SCR_WIDTH,SCR_HEIGHT,0,0&#41;;

		sceGuFinish&#40;&#41;;
		sceGuSync&#40;0,0&#41;;

//		float curr_fps = 1.0f / curr_ms;

//		pspDebugScreenSetOffset&#40;&#40;int&#41;fbp0&#41;;
//		pspDebugScreenSetXY&#40;0,0&#41;;
//		pspDebugScreenPrintf&#40;"fps&#58; %d.%03d &#40;%dMB/s&#41; &#40;X = mode, O = swizzle&#41; %s",&#40;int&#41;curr_fps,&#40;int&#41;&#40;&#40;curr_fps-&#40;int&#41;curr_fps&#41; * 1000.0f&#41;,&#40;&#40;&#40;int&#41;curr_fps * SCR_WIDTH * SCR_HEIGHT * 2&#41;/&#40;1024*1024&#41;&#41;,modes&#91;blit_method + swizzle * 2&#93;&#41;;

//		sceDisplayWaitVblankStart&#40;&#41;;
		fbp0 = sceGuSwapBuffers&#40;&#41;;

		// simple frame rate counter

//		++frame_count;
//		u64 curr_tick;
//		sceRtcGetCurrentTick&#40;&curr_tick&#41;;
//		if &#40;&#40;curr_tick-last_tick&#41; >= tick_frequency&#41;
//		&#123;
//			float time_span = &#40;&#40;int&#41;&#40;curr_tick-last_tick&#41;&#41; / &#40;float&#41;tick_frequency;
//			curr_ms = time_span / frame_count;

//			frame_count = 0;
//			sceRtcGetCurrentTick&#40;&last_tick&#41;;
//		&#125;
	&#125;

	sceGuTerm&#40;&#41;;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;
and the screen shot is here
Image
Image
any imformation will be helpful to me, thank you
Post Reply