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 

Aligning memory to 16 bytes with new operator

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



Joined: 20 Sep 2005
Posts: 11

PostPosted: Mon Feb 27, 2006 6:28 pm    Post subject: Aligning memory to 16 bytes with new operator Reply with quote

Hello again, I’m having a bit of trouble with my images looking slightly ‘corrupted’ when displayed on the PSP. I had a look in pspgu.h and noticed this:

Code:

/**
  * Set current texturemap
  *
  * Textures may reside in main RAM, but it has a huge speed-penalty. Swizzle textures
  * to get maximum speed.
  *
  * @note Data must be aligned to 1 quad word (16 bytes)
  *
  * @param mipmap - Mipmap level
  * @param width - Width of texture (must be a power of 2)
  * @param height - Height of texture (must be a power of 2)
  * @param tbw - Texture Buffer Width (block-aligned)
  * @param tbp - Texture buffer pointer (16 byte aligned)
**/
void sceGuTexImage(int mipmap, int width, int height, int tbw, const void* tbp);



It seams my texture buffer needs to be 16-byte aligned, which it’s currently not and I’m assuming that’s my problem.

I create a texture with something similar to this:

Code:

unsigned int height = 64;
unsigned int width = 64;
unsigned int* pixels = new unsigned int[height*width];
   
for (u32 y = 0;  y < height;  y++) {
    for (u32 x = 0;  x< width;  x++) {
        pixels[y*_width + x] = 0xFF0000FF;
    }
}
   
pixels = (u32*)((u32)pixels | 0x40000000);


But how do I align memory created with the new operator to 16 bytes? I know there is memalign() but that seams to be old C style and I prefer the use of ‘new’, there’s also __attribute__((aligned(16))) but that doesn’t seam to work for memory allocated at runtime.

Thanks for any help.
Back to top
View user's profile Send private message
chp



Joined: 23 Jun 2004
Posts: 313

PostPosted: Mon Feb 27, 2006 10:41 pm    Post subject: Reply with quote

There are two solutions to this:

a) Write your own operator new & delete that aligns memory-allocations.

You usually just have to write something like:
Code:

void* operator new(size_t size)
{
 return memalign(16,size);
}

void operator free(void* p)
{
 free(p);
}

And the additional for new[], etc.

b) Use a custom allocator for allocations that need to be aligned.

Example:
Code:
class Allocator
{
public:
 virtual void* allocate(size_t size) { return malloc(size); }
 static Allocator instance;
}

class AlignedAllocator : public Allocator
{
public:
 void* allocate(size_t size) { return memalign(16,size); }
 static AlignedAllocator instance;
}

void* operator new(size_t size, Allocator& allocator)
{
 return allocator.allocate(size);
}
Then you can use do something like this:
Code:

char* texture = new (AlignedAllocator::instance) char[256*256];


And it will allocate aligned memory. Remember to overload global deletes as well if you start customizing new.
_________________
GE Dominator
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