Silly texture problem

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

Moderators: cheriff, TyRaNiD

Post Reply
Chucky
Posts: 2
Joined: Wed Nov 02, 2005 10:28 pm

Silly texture problem

Post by Chucky »

I've just started playing around with my PSP, and want to understand how texture rendering works. I've assembled a simple test program which is suppose to show a 4x4 texture scaled to 32x32. However, the colors are all wrong when displayed. I would realy appreciate if someone could take a look at the code, even though it's a noob question.

Code: Select all

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


int main(int argc, char* argv[])
{
	pspDebugScreenInit();
	SetupCallbacks();

	sceGuInit();

	// setup
	sceGuStart(GU_DIRECT,list);
	sceGuDrawBuffer(GU_PSM_5551,(void*)0,512);
	sceGuDispBuffer(480,272,(void*)0x88000,512);
	sceGuDepthBuffer((void*)0x110000,512);
	sceGuOffset(2048 - (480/2),2048 - (272/2));
	sceGuViewport(2048,2048,480,272);
	sceGuDepthRange(0xc350,0x2710);
	sceGuScissor(0,0,480,272);
	sceGuEnable(GU_SCISSOR_TEST);
	sceGuFrontFace(GU_CW);
	sceGuEnable(GU_TEXTURE_2D);
	sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
	sceGuFinish();
	sceGuSync(0,0);

	sceDisplayWaitVblankStart();
	sceGuDisplay(GU_TRUE);
	
	struct Vertex* vertices;
	
	vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
	vertices[0].u = 0;
	vertices[0].v = 0;
	vertices[0].color = 0;
	vertices[0].x = 0;
	vertices[0].y = 0;
	vertices[0].z = 0;
	vertices[1].u = 2;
	vertices[1].v = 2;
	vertices[1].color = 0;
	vertices[1].x = 32;
	vertices[1].y = 32;
	vertices[1].z = 0;
	
	unsigned short __attribute__((aligned(16))) pixels[4];
	//Setting the pixels of the texture
	pixels[0] = 0xff00;
	pixels[1] = 0xff00;
	pixels[2] = 0xffff;
	pixels[3] = 0xffff;
	
	while (!done)
	{
		
		sceGuStart(GU_DIRECT,list);

		sceGuTexMode(GU_PSM_5551,0,0,0);
		sceGuTexImage(0,2,2,2,pixels);
		sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
		sceGuTexFilter(GU_NEAREST,GU_NEAREST);
		sceGuTexScale(1.0f/2.0f,1.0f/2.0f);
		sceGuTexOffset(0.0f,0.0f);
		sceGuAmbientColor(0xffffffff);
		
		sceGuClearColor(0x0);
		sceGuClear(GU_COLOR_BUFFER_BIT);
		
		sceGuDrawArray( GU_SPRITES,
						GU_TEXTURE_16BIT|GU_COLOR_5551|GU_VERTEX_16BIT|GU_TRANSFORM_2D,
						2,
						0,
						vertices);

		sceGuFinish();
		sceGuSync(0,0);

		sceGuSwapBuffers();
	}

	sceGuTerm();

	sceKernelExitGame();
	return 0;
}
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

What colours are you expecting to see?

One obvious problem is that you're not flushing the CPU cache after setting your texture data; see http://goop.org/psp/cache-howto.html for details.

The other is that you're using a 5551 texture format, but apparently expecting to use a 4444 format by the pixel data you're setting in the texture. But without knowing what colours you're expecting to see, its hard to tell.

Also, you probably don't need to have per-vertex colour attributes, since you're not using them. Or set them to white (0xffff) rather than black to make sure they don't affect you in unexpected ways.

Oh, and texture coords are always 0..1, regardless of the texture size (unless you play with the texture offset/scale settings).
71M
Posts: 122
Joined: Tue Jun 21, 2005 5:28 am
Location: London

Post by 71M »

Unfortunately texture coordinates aren't 0..1 when using GU_TRANSFORM_2D they're set in literal pixel values like Chucky has already. This caught me out before and had me scratching my head for hours on end!

Cheers,
71M
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

Oh, OK, I didn't notice that.
Chucky
Posts: 2
Joined: Wed Nov 02, 2005 10:28 pm

Post by Chucky »

One obvious problem is that you're not flushing the CPU cache after setting your texture data; see http://goop.org/psp/cache-howto.html for details.
This sounds intresting, since the the colors which are displayed sometimes changes from time to time, even if I have not changed them in the source code. As I understand you I should pu a sceKernelDcacheWritebackAll() call right after sceGuTexImage. Like this:

Code: Select all

sceGuTexMode(GU_PSM_5551,0,0,0);
		sceGuTexImage(0,2,2,2,pixels);
		sceKernelDcacheWritebackAll();// Here's where I place it
		sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
		sceGuTexFilter(GU_NEAREST,GU_NEAREST);
		sceGuTexScale(1.0f/2.0f,1.0f/2.0f);
		sceGuTexOffset(0.0f,0.0f);
		sceGuAmbientColor(0xffffffff);
But this doesen't make any differance. Where should I put it?
The other is that you're using a 5551 texture format, but apparently expecting to use a 4444 format by the pixel data you're setting in the texture. But without knowing what colours you're expecting to see, its hard to tell.
Can't see how you can tell the differance between 5551 and 4444, since they're the same amounth of bits. I haven't realy woried about what excact colors are displayed yet. My first goal is that the two first pixels should be in one color and the other two in another ;-)

Sorry for the late reply, but I'm doing my national military service and I dont't have a lot of time home.
Post Reply