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 

Textures in RAM (not VRAM)

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



Joined: 01 Jul 2005
Posts: 197

PostPosted: Thu Oct 27, 2005 10:18 pm    Post subject: Textures in RAM (not VRAM) Reply with quote

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
View user's profile Send private message
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Thu Oct 27, 2005 10:43 pm    Post subject: Reply with quote

Just a wild guess, but have you tried memalign instead of malloc?
Back to top
View user's profile Send private message
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Fri Oct 28, 2005 12:12 am    Post subject: Reply with quote

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
View user's profile Send private message
Paco



Joined: 09 Oct 2005
Posts: 54

PostPosted: Fri Oct 28, 2005 6:33 am    Post subject: Reply with quote

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
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Fri Oct 28, 2005 7:12 am    Post subject: Reply with quote

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
View user's profile Send private message
ReKleSS



Joined: 18 Jun 2005
Posts: 73
Location: Melbourne, Australia

PostPosted: Fri Oct 28, 2005 4:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Fri Oct 28, 2005 6:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
patpsp



Joined: 25 Oct 2005
Posts: 31

PostPosted: Fri Oct 28, 2005 7:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Fri Oct 28, 2005 9:49 pm    Post subject: Reply with quote

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