Textures in RAM (not VRAM)

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

Moderators: cheriff, TyRaNiD

Post Reply
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Textures in RAM (not VRAM)

Post by AnonymousTipster »

To load textures, I've currently been using this code from the spharm sample:

Code: Select all

#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 &#40;x=0;x<tsize;x++&#41; &#123;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = 0xff;
  &#125;
  vramaddr += tsize * 4;
  if &#40;&#40;vramaddr & 0xff&#41; != 0&#41;
    vramaddr = &#40;vramaddr & 0xffffff00&#41; + 0x100;
  return output;
&#125;
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: Select all

unsigned int ramaddr = 0;
unsigned char *convertimageRAM&#40;unsigned char *inptr,int size&#41;
&#123;
  // 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 &#40;ramaddr == 0&#41;
    ramaddr = &#40;unsigned int&#41;malloc&#40;sizediff&#41;;
  outptr = output = &#40;unsigned char *&#41;ramaddr;
  for &#40;x=0;x<tsize;x++&#41; &#123;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = 0xff;
  &#125;
  ramaddr += tsize * 4;
  if &#40;&#40;ramaddr & 0xff&#41; != 0&#41;
    ramaddr = &#40;ramaddr & 0xffffff00&#41; + 0x100;
  return output;
&#125;
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: Select all

unsigned int ramaddr = 0;
unsigned char *convertimageRAM&#40;unsigned char *inptr,int size&#41;
&#123;
  // 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 = &#40;unsigned int&#41;memalign&#40;16,sizediff&#41;;
  outptr = output = &#40;unsigned char *&#41;ramaddr;
  for &#40;x=0;x<tsize;x++&#41; &#123;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = 0xff;
  &#125;
  return output;
&#125;
Last edited by AnonymousTipster on Sat Oct 29, 2005 3:07 am, edited 2 times in total.
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

Just a wild guess, but have you tried memalign instead of malloc?
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

I tried changing the code to the following:

Code: Select all

unsigned int ramaddr = 0;
unsigned char *convertimageRAM&#40;unsigned char *inptr,int size&#41;
&#123;
  // 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 &#40;ramaddr == 0&#41;
    //ramaddr = &#40;unsigned int&#41;malloc&#40;sizediff&#41;;//&#40;0x40000000 | 0x04000000&#41; + VRAM_OFFSET;
	ramaddr = &#40;unsigned int&#41;memalign&#40;RoundUpPow2&#40;sizediff&#41;,sizediff&#41;;
  outptr = output = &#40;unsigned char *&#41;ramaddr;
  for &#40;x=0;x<tsize;x++&#41; &#123;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = *&#40;input++&#41;;
    *&#40;outptr++&#41; = 0xff;
  &#125;
  ramaddr += tsize * 4;
  if &#40;&#40;ramaddr & 0xff&#41; != 0&#41;
    ramaddr = &#40;ramaddr & 0xffffff00&#41; + 0x100;
  return output;
&#125;
(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.
Paco
Posts: 54
Joined: Sun Oct 09, 2005 6:53 pm

Post by Paco »

Code: Select all

  // Choppped up
  sizediff = size;
  int tsize = size*size;
  ramaddr = &#40;unsigned int&#41;malloc&#40;sizediff&#41;;
  for &#40;x=0;x<tsize;x++&#41; &#123; 
You are allocating "size" bytes and then filling "size*size*4" bytes of memory. That's very wrong.
Paco
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

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.
User avatar
ReKleSS
Posts: 73
Joined: Sat Jun 18, 2005 12:57 pm
Location: Melbourne, Australia

Post by ReKleSS »

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
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

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.
patpsp
Posts: 31
Joined: Tue Oct 25, 2005 5:24 pm

Post by patpsp »

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
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

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. ^_^
Post Reply