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 

gl texture troubles

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



Joined: 13 Sep 2007
Posts: 19
Location: Gorizia, ITALY

PostPosted: Thu Sep 13, 2007 11:32 pm    Post subject: gl texture troubles Reply with quote

hello, I need some hints with my code, as I got a program that display two textures (two square polygons [3*2]) one with a clock display and one with the pointer. Apart that I still can't have the transparency out of the png alpha channel (this will be next in line), it happens that first texture is rendered correctly, the second appears stretched, messed up, and the code used to display it is the same of the other. What can it be? The two pngs are the same, I'm trying to load the same png one above the other for debugging ease (anyway the topmost one, the pointer, rotates as you move the analog stick, so one can see what there's behind).

here are the code portions:
Code:

Image* img_lancetta;
Image* img_tachimetro;
 



Vertex __attribute__((aligned(16))) lancetta[3*2] = {
 
   { 0, 1, color,-0.18f,-0.3f, 1.0f },   // Front
   { 1, 1, color, 0.18f,-0.3f, 1.0f },
   { 0, 0, color,-0.18f, 1.0f, 1.0f },
 
   { 0, 0, color,-0.18f, 1.0f, 1.0f },
   { 1, 1, color, 0.18f,-0.3f, 1.0f },
   { 1, 0, color, 0.18f, 1.0f, 1.0f }
   
   
};

Vertex __attribute__((aligned(16))) tachimetro[3*2] = {
 
   
        { 0, 1, color,-1.0f,-1.0f, -0.1f },   
   { 1, 1, color, 1.0f,-1.0f, -0.1f },
   { 0, 0, color,-1.0f, 1.0f, -0.1f },
 
   { 0, 0, color,-1.0f, 1.0f, -0.1f },
   { 1, 1, color, 1.0f,-1.0f, -0.1f },
   { 1, 0, color, 1.0f, 1.0f, -0.1f }
   
   
};
[...main()....]

        img_lancetta = loadImage("lancetta.png");
        swizzle(img_lancetta);
       
        img_tachimetro = loadImage("lancetta.png");  //note same png here
        swizzle(img_tachimetro); 

[...]

void InitGU( void )
{
   // Init GU
   
        sceKernelDcacheWritebackInvalidateAll();  //debug
   sceGuInit();
       
       
       
        sceGuStart( GU_DIRECT, dList );
        // Set Buffers
   sceGuDrawBuffer( GU_PSM_8888, fbp0, BUF_WIDTH );
   sceGuDispBuffer( SCR_WIDTH, SCR_HEIGHT, (void*)0x88000, BUF_WIDTH);
   sceGuDepthBuffer( (void*)0x110000, BUF_WIDTH);
 
   sceGuOffset( 2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2));
   sceGuViewport( 2048, 2048, SCR_WIDTH, SCR_HEIGHT);
   sceGuDepthRange( 65535, 0);
        // Set Render States
   sceGuScissor( 0, 0, SCR_WIDTH, SCR_HEIGHT);
   sceGuEnable( GU_SCISSOR_TEST );
        sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
   sceGuDepthFunc( GU_GEQUAL );
   sceGuEnable( GU_DEPTH_TEST );
   sceGuFrontFace( GU_CW );
   sceGuShadeModel( GU_SMOOTH );
   sceGuEnable(GU_CLIP_PLANES);//---
   sceGuEnable(GU_TEXTURE_2D);   
       
        // setup texture (NEW)
   // I set the swizzled parameter to true fixed as my pngs
//are all bigger than 32*32
   sceGuTexMode( GU_PSM_8888, 0, 0, 1);   
        //sceGuTexImage(0,TEXW,TEXH,TEXBLOCKW,pointer_to_tex_data);
   sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA);    // Apply image as a decal (NEW)
   sceGuTexFilter( GU_LINEAR, GU_LINEAR );      // Linear filtering (Good Quality) (NEW)
   sceGuTexScale( 1.0f, 1.0f );                    // No scaling
   sceGuTexOffset( 0.0f, 0.0f );
       
        //---
       
        sceGuFinish();
   sceGuSync(0,0);
 
   sceDisplayWaitVblankStart();
   sceGuDisplay(GU_TRUE);
   // finish
}

[....]

void Drawscene(void){
 rtri=-(angle)*pi/180;
 sceGuStart( GU_DIRECT, dList );                           // Starts the display list
 sceKernelDcacheWritebackInvalidateAll();

      
   // clear screen
   sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);      // Clears the Color and Depth Buffer

        sceGumMatrixMode(GU_MODEL);                               // Selects the Model Matrix

        sceGumLoadIdentity();                                     // And Reset it
 
        {   // Move 1.5 units left and 3 units back
      ScePspFVector3 move = { 0.0f,0.0f, -3.0f };
      
      sceGumTranslate( &move );                         // Apply moving of the matrix
                sceGumRotateZ( rtri );            // rotate from analog stick movement
       }
       
       sceGuTexImage( 0, img_lancetta->textureWidth, img_lancetta->textureHeight, img_lancetta->textureWidth,img_lancetta->data );
       sceGumDrawArray( GU_TRIANGLES, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,3*2, 0, lancetta);
 

        sceGumLoadIdentity();                                     // And Reset it
 
        {   // Move 1.5 units left and 3 units back
      ScePspFVector3 move = { 0.0f,0.0f, -3.1f };
      
      sceGumTranslate( &move );                         // Apply moving of the matrix
                sceGumRotateZ( 0.01f );            // Rotate the triangle on the Y-axis (NEW)
       }
       
       sceGuTexImage( 0, img_tachimetro->textureWidth, img_tachimetro->textureHeight, img_tachimetro->textureWidth,img_tachimetro->data );
       sceGumDrawArray( GU_TRIANGLES, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,3*2, 0, tachimetro);
       
       
       
   sceGuFinish();
   sceGuSync(0,0);



}





I suppose the swizzle code correct, as it works perfectly with the first texture being displayed.
I'm a total newbie with opengl so it may be everything, from some parameters in InitGU or who knows, some code missing in Drawscene, or whatever.. If someone can give me some of his light .. ;D

Thanks in advance
Back to top
View user's profile Send private message
chp



Joined: 23 Jun 2004
Posts: 313

PostPosted: Thu Sep 13, 2007 11:55 pm    Post subject: Reply with quote

Do you flush the cpu caches after swizzling the textures? Otherwise the last texture might still be in the data cache since GU operates on uncached memory when it's writing to the display list.
_________________
GE Dominator
Back to top
View user's profile Send private message
ruspa



Joined: 13 Sep 2007
Posts: 19
Location: Gorizia, ITALY

PostPosted: Fri Sep 14, 2007 12:01 am    Post subject: Reply with quote

you mean I have to put a

sceKernelDcacheWritebackInvalidateAll();

between the two

Code:
sceGumLoadIdentity();                                     // And Reset it
 
        {   // Move 1.5 units left and 3 units back
      ScePspFVector3 move = { 0.0f,0.0f, -3.1f };
      
      sceGumTranslate( &move );                         // Apply moving of the matrix
                sceGumRotateZ( 0.01f );            // Rotate the triangle on the Y-axis (NEW)
       }
       
       sceGuTexImage( 0, img_tachimetro->textureWidth, img_tachimetro->textureHeight, img_tachimetro->textureWidth,img_tachimetro->data );
       sceGumDrawArray( GU_TRIANGLES, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,3*2, 0, tachimetro);
       
??

I'll try right now :)
anyway the output I have is this:
the texture above rotates as you interact with pad, the one behind is the same but fixed, should look only "fat" as is the same red gauge-pointer but stretched. Instead, it's all wobbled.. stirred :O
Time to try your suggestion... I'll let you know
Back to top
View user's profile Send private message
ruspa



Joined: 13 Sep 2007
Posts: 19
Location: Gorizia, ITALY

PostPosted: Fri Sep 14, 2007 12:04 am    Post subject: Reply with quote

:( same result (If I got what you meant, at least)

If can help, I reversed and adapted the neheport lesson n°5 (the solid, textured cube), I used just a face and rotate it with a texture on it, then another single textured polygon is drawn later, and behind since it's more distant. I tell this just to help tracing my error, maybe it's a thing dealing with 3d/2d thingies..

===============================
What I discovered:

if I set sceGuTexMode( GU_PSM_8888, 0, 0, 0); in the initGU I disable the swizzle thing (and I expected to see two ruined textures, as far I learned if you want to use textures exceding 32x32 pixels you've got to swizzle them and to set in the initGU the last parameter to true) instead it happened that the first texture got wobbled and the second appears correct. In other words my code swizzle only the first texture, not the second. That's why setting the swizzle parameter to false, the unswizzled texture appears correct. But... this is confusing me because those textures are bigger than 32x32... yesterday before learning swizzle I was getting panned tilted textures... O__O what have I misunderstood?

I use a swizzling routine found on a dev site, here is it:
Code:
void swizzle( Image* img )
{
   if( img->swizzled)
   {
      //pspDebugScreenPrintf("Already swizzled Image\n");
   }
   else
   {
      int type = 4;      // 32 - bit pixel format so 4 bytes
      long size = img->textureWidth * img->textureHeight * type;
      u8* temp = (u8*)malloc(size);

      swizzle_fast( temp, (u8*)img->data, (img->textureWidth * type), img->textureHeight );
      
        free(img->data);
        img->data = (u32*)temp;
        img->swizzled = 1;
                //return img;
   }
        //img->swizzled = 0;
        //return img;
}

void swizzle_fast(u8* out, const u8* in, unsigned int width, unsigned int height)
{
   unsigned int blockx, blocky;
   unsigned int i,j;
 
   unsigned int width_blocks = (width / 16);
   unsigned int height_blocks = (height / 8);
 
   unsigned int src_pitch = (width-16)/4;
   unsigned int src_row = width * 8;
 
   const u8* ysrc = in;
   u32* dst = (u32*)out;
 
   for (blocky = 0; blocky < height_blocks; ++blocky)
   {
      const u8* xsrc = ysrc;
      for (blockx = 0; blockx < width_blocks; ++blockx)
      {
         const u32* src = (u32*)xsrc;
         for (j = 0; j < 8; ++j)
         {
            *(dst++) = *(src++);
            *(dst++) = *(src++);
            *(dst++) = *(src++);
            *(dst++) = *(src++);
            src += src_pitch;
         }
         xsrc += 16;
     }
     ysrc += src_row;
   }
}


and I call the two functions with



Code:
        img_tachimetro = loadImage("texture_rpm.png");
        swizzle(img_tachimetro);       
        sceKernelDcacheWritebackAll();
       
        img_lancetta = loadImage("lancetta.png");
        swizzle(img_lancetta);
        sceKernelDcacheWritebackAll();
Back to top
View user's profile Send private message
chp



Joined: 23 Jun 2004
Posts: 313

PostPosted: Fri Sep 14, 2007 2:49 am    Post subject: Reply with quote

swizzle_fast() should be ok, considering that I wrote that code back in the day.. :)

But you don't need to swizzle textures if you don't want to, but performance will suffer if you don't (and preferably you should use sceGuCopyImage() to copy the texture to VRAM before you use it, but that's only if you get all of this working...). sceGuTexMode(GU_PSM_8888, 0, 0, texture->swizzled ? GU_TRUE : GU_FALSE) before each draw would select swizzled/unswizzled mode for the texture.

Is Image::swizzled cleared properly when you instance it? From the code I can see in swizzle() you have a condition that might skip the swizzle altogether, and you might swizzle the first but not the second.
_________________
GE Dominator
Back to top
View user's profile Send private message
ruspa



Joined: 13 Sep 2007
Posts: 19
Location: Gorizia, ITALY

PostPosted: Fri Sep 14, 2007 2:56 am    Post subject: Reply with quote

chp wrote:
Is Image::swizzled cleared properly when you instance it? From the code I can see in swizzle() you have a condition that might skip the swizzle altogether, and you might swizzle the first but not the second.



is the same thing I think, but I can't get a lock on the idea, too newbie in opengl and rusty in C (I did the mistake to became a actionscript pro, did 3d engines in flash but.. I lost the touch with raw good lowlevel coding). I'll give it some more tries on the swizzle method, but I won't cry if you explain me the thing of "cleared properly" as you would do with a child :D :D :D
Furthermore, as soon as I resolve this one, I'll piss you off (I promise) with missing alpha transparency hehe
Back to top
View user's profile Send private message
ruspa



Joined: 13 Sep 2007
Posts: 19
Location: Gorizia, ITALY

PostPosted: Fri Sep 14, 2007 3:14 am    Post subject: Reply with quote

IT WORKS!!!!!!!!!!!!!! IT WORKS!!!! EVEN WITH TRANSPARENCY!!!
(sorry for shouting) I read some docs on initialization instructions (sceGuenable stuff) and I was missing the alpha test, and to resolve the swizzling, I simply removed the "if already swizzled" test, this just because we don't need to re-swizzle textures after initializations, so every texture need to be swizzle once and not any more. This way every texture is forced to be swizzled and the problem is resolved :D
Thanks man, I feel really very happy!



just got to understand why I get this
"h@#@#[/game150/LANC2/lance+ùèùà" sht string if there's no printing functions in my code O__o Anyway I'm having big fun, now that things starts working :D tnx again chp!
Back to top
View user's profile Send private message
chp



Joined: 23 Jun 2004
Posts: 313

PostPosted: Fri Sep 14, 2007 5:35 am    Post subject: Reply with quote

Good to see that you fixed it! And I don't get pissed off as long as it's proper development stuff which this was, so no worries there. :)
_________________
GE Dominator
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