gl texture troubles

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

Moderators: cheriff, TyRaNiD

Post Reply
ruspa
Posts: 19
Joined: Thu Sep 13, 2007 11:17 pm
Location: Gorizia, ITALY

gl texture troubles

Post by ruspa »

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

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
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

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
ruspa
Posts: 19
Joined: Thu Sep 13, 2007 11:17 pm
Location: Gorizia, ITALY

Post by ruspa »

you mean I have to put a

sceKernelDcacheWritebackInvalidateAll();

between the two

Code: Select all

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: Image
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
ruspa
Posts: 19
Joined: Thu Sep 13, 2007 11:17 pm
Location: Gorizia, ITALY

Post by ruspa »

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

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 &#40;blocky = 0; blocky < height_blocks; ++blocky&#41;
   &#123;
      const u8* xsrc = ysrc;
      for &#40;blockx = 0; blockx < width_blocks; ++blockx&#41;
      &#123;
         const u32* src = &#40;u32*&#41;xsrc;
         for &#40;j = 0; j < 8; ++j&#41;
         &#123;
            *&#40;dst++&#41; = *&#40;src++&#41;;
            *&#40;dst++&#41; = *&#40;src++&#41;;
            *&#40;dst++&#41; = *&#40;src++&#41;;
            *&#40;dst++&#41; = *&#40;src++&#41;;
            src += src_pitch;
         &#125;
         xsrc += 16;
     &#125;
     ysrc += src_row;
   &#125;
&#125;
and I call the two functions with


Code: Select all

        img_tachimetro = loadImage&#40;"texture_rpm.png"&#41;;
        swizzle&#40;img_tachimetro&#41;;        
        sceKernelDcacheWritebackAll&#40;&#41;;
        
        img_lancetta = loadImage&#40;"lancetta.png"&#41;;
        swizzle&#40;img_lancetta&#41;;
        sceKernelDcacheWritebackAll&#40;&#41;;
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

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
ruspa
Posts: 19
Joined: Thu Sep 13, 2007 11:17 pm
Location: Gorizia, ITALY

Post by ruspa »

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
ruspa
Posts: 19
Joined: Thu Sep 13, 2007 11:17 pm
Location: Gorizia, ITALY

Post by ruspa »

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!

Image

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!
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

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
Post Reply