| View previous topic :: View next topic |
| Author |
Message |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Thu Oct 27, 2005 10:18 pm Post subject: Textures in RAM (not VRAM) |
|
|
To load textures, I've currently been using this code from the spharm sample:
| Code: | #define VRAM_OFFSET ((512*280*4)*3)
// 0x198000
static unsigned int vramaddr = 0;
unsigned char *convertimage(unsigned char *inptr,int size)
{
// convert our raw image
// saved as raw. no header .. interleaved and order RGB
int x;
unsigned char *input = inptr;
unsigned char *output,*outptr;
int tsize = size*size;
if (vramaddr == 0)
vramaddr = (0x40000000 | 0x04000000) + VRAM_OFFSET;
outptr = output = (unsigned char *)vramaddr;
for (x=0;x<tsize;x++) {
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = 0xff;
}
vramaddr += tsize * 4;
if ((vramaddr & 0xff) != 0)
vramaddr = (vramaddr & 0xffffff00) + 0x100;
return output;
} |
But this funcition loads into VRAM, and I've run out of space, so I'd like to load them to RAM instead. I think that I need to use malloc(), but it returns a void*, and I need a static unsigned char to return. I tried changing the code to:
| Code: | unsigned int ramaddr = 0;
unsigned char *convertimageRAM(unsigned char *inptr,int size)
{
// convert our raw image
// saved as raw. no header .. interleaved and order RGB
//this function loads into RAM, not VRAM
int x;
unsigned char *input = inptr;
unsigned char *output,*outptr;
size_t sizediff;
sizediff = size;
int tsize = size*size;
if (ramaddr == 0)
ramaddr = (unsigned int)malloc(sizediff);
outptr = output = (unsigned char *)ramaddr;
for (x=0;x<tsize;x++) {
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = 0xff;
}
ramaddr += tsize * 4;
if ((ramaddr & 0xff) != 0)
ramaddr = (ramaddr & 0xffffff00) + 0x100;
return output;
} | But this only displays half the image, the rest speckles of colour. What am I doing wrong, and how can I use the malloc() function like this?
Thanks.
EDIT:
Corrected Code:
| Code: | unsigned int ramaddr = 0;
unsigned char *convertimageRAM(unsigned char *inptr,int size)
{
// convert our raw image
// saved as raw. no header .. interleaved and order RGB
//this function loads into RAM, not VRAM
int x;
unsigned char *input = inptr;
unsigned char *output,*outptr;
size_t sizediff;
sizediff = size*size*4;
int tsize = size*size;
ramaddr = (unsigned int)memalign(16,sizediff);
outptr = output = (unsigned char *)ramaddr;
for (x=0;x<tsize;x++) {
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = 0xff;
}
return output;
} |
Last edited by AnonymousTipster on Sat Oct 29, 2005 3:07 am; edited 2 times in total |
|
| Back to top |
|
 |
urchin
Joined: 02 Jun 2005 Posts: 121
|
Posted: Thu Oct 27, 2005 10:43 pm Post subject: |
|
|
| Just a wild guess, but have you tried memalign instead of malloc? |
|
| Back to top |
|
 |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Fri Oct 28, 2005 12:12 am Post subject: |
|
|
I tried changing the code to the following:
| Code: | unsigned int ramaddr = 0;
unsigned char *convertimageRAM(unsigned char *inptr,int size)
{
// convert our raw image
// saved as raw. no header .. interleaved and order RGB
//this function loads into RAM, not VRAM
int x;
unsigned char *input = inptr;
unsigned char *output,*outptr;
size_t sizediff;
sizediff = size;
int tsize = size*size;
if (ramaddr == 0)
//ramaddr = (unsigned int)malloc(sizediff);//(0x40000000 | 0x04000000) + VRAM_OFFSET;
ramaddr = (unsigned int)memalign(RoundUpPow2(sizediff),sizediff);
outptr = output = (unsigned char *)ramaddr;
for (x=0;x<tsize;x++) {
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = *(input++);
*(outptr++) = 0xff;
}
ramaddr += tsize * 4;
if ((ramaddr & 0xff) != 0)
ramaddr = (ramaddr & 0xffffff00) + 0x100;
return output;
} | (IE using memalign) and the results are the same. Also, my textures are already power's of 2, so memalign shouldn't change anything. I think the problem is changing void* into unsigned int, but i'm not very sure. |
|
| Back to top |
|
 |
Paco
Joined: 09 Oct 2005 Posts: 54
|
Posted: Fri Oct 28, 2005 6:33 am Post subject: |
|
|
| Code: |
// Choppped up
sizediff = size;
int tsize = size*size;
ramaddr = (unsigned int)malloc(sizediff);
for (x=0;x<tsize;x++) {
|
You are allocating "size" bytes and then filling "size*size*4" bytes of memory. That's very wrong. _________________ Paco |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Fri Oct 28, 2005 7:12 am Post subject: |
|
|
| AnonymousTipster wrote: | | Also, my textures are already power's of 2, so memalign shouldn't change anything. |
memalign has nothing to do with the size of your texture, but assures that the pointer is memory aligned, which is returned. |
|
| Back to top |
|
 |
ReKleSS

Joined: 18 Jun 2005 Posts: 73 Location: Melbourne, Australia
|
Posted: Fri Oct 28, 2005 4:23 pm Post subject: |
|
|
To follow up on what Shine said, your use of memalign is weird. You're aligning the textures to the size of the texture... for the GU textures need to be 16 byte aligned (i.e. memalign(16, blah)).
-ReK |
|
| Back to top |
|
 |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Fri Oct 28, 2005 6:54 pm Post subject: |
|
|
Ok, i've got it, the problem was what Paco mentioned, that I was allocating size, but using size*size*4, so once I change it to alloc size*size*4 bytes, everything works fine.
Thanks. |
|
| Back to top |
|
 |
patpsp
Joined: 25 Oct 2005 Posts: 31
|
Posted: Fri Oct 28, 2005 7:19 pm Post subject: |
|
|
It will be nice if you edit the first post to put the corrected code.
So if someone else has the same problem, he could have a look at the bad code and its correction.
Thanks |
|
| Back to top |
|
 |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Fri Oct 28, 2005 9:49 pm Post subject: |
|
|
| patpsp wrote: | It will be nice if you edit the first post to put the corrected code.
So if someone else has the same problem, he could have a look at the bad code and its correction.
Thanks | Done. ^_^ |
|
| Back to top |
|
 |
|