PVR texture color loss??

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
iggydrake
DCEmu Freak
DCEmu Freak
Posts: 58
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sat Sep 17, 2005 12:52 pm
Location: Canada
Has thanked: 0
Been thanked: 0
Contact:

PVR texture color loss??

Post by iggydrake »

Hi,

I've decided to go back to DC programming after some time. i've ported a new PC game engine to Dreamcast, it's much better than my old one I posted here long ago, but I seem to be having a strange problem.

images look like crap on DC even tho they look fine in any paint prog etc.
I've attached a sample image of how an 8 or 16 bit would look on DC compared to original PNG file

Here's the image loading/displaying code I learned here before that i'm using:

Code: Select all



pvr_ptr_t LoadPVRImage(const char* img,uint32 alpha, int w, int h)
{
pvr_ptr_t dst;
dst=pvr_mem_malloc(w*h*2);
png_to_texture(img, dst, alpha);
return dst;
}

void DrawPVRImage(pvr_ptr_t img, int x, int y, int z,uint32 width, uint32 height, int index, float r, float g, float b, float a, uint32 txw, uint32 txh, int sx, int sy) 
{
   
   int cx, cy;
   float inc_x = ((float)width/(float)txw);
   float inc_y = ((float)height/(float)txh);
   float index_x;
   float index_y = 1;
   int tiles_wide = (txw/width);
   if(sx>0) {sx-=width;}
   if(sy>0) {sy-=height;}
   cx=x+width+sx;
   cy=y+height+sy;
   if(cx<0) {return;}
   if(cy<0) {return;}
   if(x>640) {return;}
   if(y>480) {return;}

   while(index >= tiles_wide * index_y)
   {
      index_y++;
   }
   index_y--;
   index_x = index - (tiles_wide*index_y);

    pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;
    pvr_vertex_t vert;
    pvr_poly_cxt_txr(&cxt,PVR_LIST_TR_POLY,PVR_TXRFMT_ARGB4444,txw,txh,img,PVR_FILTER_NONE);
    pvr_poly_compile(&hdr,&cxt);
    pvr_prim(&hdr,sizeof(hdr));
    vert.argb = PVR_PACK_COLOR(a,r,g,b);   
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;
    vert.x = x;
    vert.y = y;
    vert.z = z;
    vert.u = index_x*inc_x;
    vert.v = index_y*inc_y;
    pvr_prim(&vert,sizeof(vert));
    vert.x = x + width+sx;
    vert.y = y;
    vert.z = z;
    vert.u = index_x*inc_x+inc_x;
    vert.v = index_y*inc_y;
    pvr_prim(&vert,sizeof(vert));
    vert.x = x;
    vert.y = y + height+sy;
    vert.z = z;
    vert.u = index_x*inc_x;
    vert.v = index_y*inc_y+inc_y;
    pvr_prim(&vert,sizeof(vert));
    vert.x = x + width+sx;
    vert.y = y + height+sy;
    vert.z = z;
    vert.u = index_x*inc_x+inc_x;
    vert.v = index_y*inc_y+inc_y;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));

}
it seems that for some reason a 16bit, 24bit, 256 color image....they all look as if they've been converted down to some lower depth....I've tried converting the images to every color depth, format, amounts I could but it's always the same result.

does anyone know why this might be happening??
~Mal

if i told you that the next thing I say will be true, but that the last thing I said was a lie, would you believe me?
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has thanked: 0
Been thanked: 0

Re: PVR texture color loss??

Post by OneThirty8 »

I'm not the most knowledgeable, nor am I the most experienced, but since nobody else has offered a suggestion...

I noticed this line:

Code: Select all

pvr_poly_cxt_txr(&cxt,PVR_LIST_TR_POLY,PVR_TXRFMT_ARGB4444,txw,txh,img,PVR_FILTER_NONE);
Are you certain that your textures are all ARGB4444? Is it possible that they're ARGB1555 or RGB565? That would certainly screw them up, if you're expecting one format and getting another.

If that is the case, you could replace the part that says "PVR_TXRFMT_ARGB4444" with a variable of some sort and work some way to keep track of the correct format for each texture (could be a headache), or you could write/borrow some code to convert everything to the format you wish to use (could be easier in the long run?). Instead of using the function that allocates memory on the PVR and loads the PNG image into that space automatically, you could allocate some memory to read the image into, figure out what size your texture needs to be on the PVR and allocate that space, and convert (and possibly twiddle) the image with code you write yourself, which will ensure that you're always dealing with what you expect to be dealing with. And then you just need to remember to free up whatever temporary space you used to read the PNG into.
iggydrake
DCEmu Freak
DCEmu Freak
Posts: 58
Joined: Sat Sep 17, 2005 12:52 pm
Location: Canada
Has thanked: 0
Been thanked: 0
Contact:

Re: PVR texture color loss??

Post by iggydrake »

I tried that just to be sure, using every format it allowed. It's definaly not a format issue, as it seems that if you
use the wrong one, all the colors go crazy as black turns to red and it's just pixel noise of wierd colors...lol

basically what i'm doing is this... my engine has a background, then 2 parrallax layers, 3 tile layers and objects (which include the player)

all of the other ones draw perfectly, except the parallax layer. the background which was opaque poly had a similar problem
before but all I did was convert the image from a 24bit color one to 16bit and it worked fine.
this particular layer is of a city scape that goes over the background and can scroll..it doesnt use many colors at all,
but the gradient at the bottom looks horrible when seen running (the example I showed is the part in question)

I wonder why it only seems to affect a specific image... o.O


I dont ever recall having this problem before. i think i used a snowy background for the demo I posted here a long time back, but then....it didnt use overlays like that ether....

!!

....I just loaded a different map....which uses a different graphic for a layer....they're all like 24-32bit images and they show up fine! It's just that one image no matter what format I save it in....and the troubled part is that black to dk grey gradient...

I just got no clue what could be going on.... x_x
~Mal

if i told you that the next thing I say will be true, but that the last thing I said was a lie, would you believe me?
Phantom
DC Developer
DC Developer
Posts: 1753
Joined: Thu Jan 16, 2003 4:01 am
Location: The Netherlands
Has thanked: 0
Been thanked: 0
Contact:

Re: PVR texture color loss??

Post by Phantom »

If you're using ARGB4444, that only gives you 16 true shades of gray. So what you're seeing is quite normal (this especially goes for gradients). Basically what happens is that a whole range of colors (in 24 bit, or even 16 bit), ends up as a single color in ARGB4444, simply because ARGB4444 has only 12 bits of color information. This leaves you with a "banded" gradient like in that image.

You may wonder why the image looks fine on your PC even with just 256 colors. That is because those 256 colors are taken from a true-color palette, and can be chosen to be all grays if needed, but with ARGB4444 you only have 16 shades of gray and that's it.

One way to fix the problem is to dither the images beforehand. For DreamChess we used ImageMagick to accomplish this, using the following script: http://svn.berlios.de/svnroot/repos/dre ... -reduce.sh

I could "fix" the image for you if you want, feel free to PM me in that case.

EDIT: I've attached an image so you can see what the script does. The left part is the original gradient, and the right part has been processed by the script. The left part will result in color bands on the DC, while the right part will render exactly as you see it here.

Basically the script takes an image and color reduces it, so that it only uses colors from the ARGB4444 range (it also supports RGB565 and ARGB1555), while using dithering to keep it close to the original.
"Nothing works" - Catweazle
iggydrake
DCEmu Freak
DCEmu Freak
Posts: 58
Joined: Sat Sep 17, 2005 12:52 pm
Location: Canada
Has thanked: 0
Been thanked: 0
Contact:

Re: PVR texture color loss??

Post by iggydrake »

I see! so this only affects shades of grey?
makes sense.

Yeah, if ya could fix my image, that'd be great. :)
I'll PM ya with info etc.
~Mal

if i told you that the next thing I say will be true, but that the last thing I said was a lie, would you believe me?
Ex-Cyber
DCEmu User with No Life
DCEmu User with No Life
Posts: 3641
Joined: Sat Feb 16, 2002 1:55 pm
Has thanked: 0
Been thanked: 0

Re: PVR texture color loss??

Post by Ex-Cyber »

iggydrake wrote:I see! so this only affects shades of grey?
If I've understood the problem correctly, it affects anything where you try to create a gradient that's too "slow" for the underlying texture format. In particular I'm pretty sure you'd see an identical banding effect if you did the same gradient with only the red, green, or blue channel.
"You know, I have a great, wonderful, really original method of teaching antitrust law, and it kept 80 percent of the students awake. They learned things. It was fabulous." -- Justice Stephen Breyer
Phantom
DC Developer
DC Developer
Posts: 1753
Joined: Thu Jan 16, 2003 4:01 am
Location: The Netherlands
Has thanked: 0
Been thanked: 0
Contact:

Re: PVR texture color loss??

Post by Phantom »

Ex-Cyber wrote:
iggydrake wrote:I see! so this only affects shades of grey?
If I've understood the problem correctly, it affects anything where you try to create a gradient that's too "slow" for the underlying texture format. In particular I'm pretty sure you'd see an identical banding effect if you did the same gradient with only the red, green, or blue channel.
Exactly. In general there are not enough colors available in ARGB4444 to have fluid transitions from one color to another. This can also be a problem with things like pre-rendered images that frequently contain subtle color changes. The DreamChess title screen was particularly problematic.
"Nothing works" - Catweazle
Post Reply