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 

Silly texture problem

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



Joined: 02 Nov 2005
Posts: 2

PostPosted: Wed Nov 02, 2005 10:45 pm    Post subject: Silly texture problem Reply with quote

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:
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;
}
Back to top
View user's profile Send private message
jsgf



Joined: 12 Jul 2005
Posts: 254

PostPosted: Fri Nov 04, 2005 9:24 am    Post subject: Reply with quote

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).
Back to top
View user's profile Send private message Visit poster's website
71M



Joined: 21 Jun 2005
Posts: 122
Location: London

PostPosted: Fri Nov 04, 2005 11:33 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
jsgf



Joined: 12 Jul 2005
Posts: 254

PostPosted: Fri Nov 04, 2005 11:49 am    Post subject: Reply with quote

Oh, OK, I didn't notice that.
Back to top
View user's profile Send private message Visit poster's website
Chucky



Joined: 02 Nov 2005
Posts: 2

PostPosted: Sat Nov 12, 2005 11:06 pm    Post subject: Reply with quote

Quote:
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:
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?

Quote:
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.
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