Jump to content
  • Advertisement
Sign in to follow this  
Leo_E_49

Structure Packing

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't understand structure packing, I need to pack a set of structures tightly
#pragma pack(push, packing)
#pragma pack(1)

#pragma pack(pop, packing)
in my MS3D model loader, what does this mean? What is #pragma? How do I pack structures in gcc for Linux? I'm trying to port my model loader to the PS2 and I can't seem to figure it out. Thanks for your help. :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Leo_E_49
How do I pack structures in gcc for Linux? I'm trying to port my model loader to the PS2 and I can't seem to figure it out.
Packing the structure tightly almost certainly results in misaligned structure members, and x86s are virtually the only modern processors capable of handling unaligned accesses.

You'll have to load it manually member-by-member or translate it into a format better suited for the PS2.

The pragmas directive itself is just a standard prefix for non-standard compiler extensions. GCC has it's own method of packing structure (probably through a __declspec) however it apparently supports Microsoft's syntax too.

Share this post


Link to post
Share on other sites
Ok thanks. :)

Looks like I've got a lot of work ahead of me. Any tips on manually loading these? I know the basic principle.

Share this post


Link to post
Share on other sites
Quote:
Original post by Leo_E_49
Any tips on manually loading these? I know the basic principle.
Not really, I generally just write a bunch of functions for reading specific datatype (bytes/words/longs/floats) and do it manually.
If performance is an issue (which seems considering that the PS2 is involved) then I suggest translating the models into a format as close to the internal representation as possible.

Share this post


Link to post
Share on other sites
Yeah well, I'm doing this for a university coursework and it's due in a week so performance is the least of my worries. :(

Thanks again for your help, you've given me hope that I can actually finish this on time. :)

Share this post


Link to post
Share on other sites
Okay, I think I don't understand what structure packing actually is.

What is structure packing and what does #pragma pack actually do?

How does a packed structure differ from a normal structure. Is it a memory issue?

Share this post


Link to post
Share on other sites
Packing is a way of specifying to the compiler how much it's allowed to pad out datamembers of a structure in order to align them better for the platform you're compiling to. Most modern architectures will either take a hit from reading misaligned data (that is not on the boundaries of the bus width of that arhitecture), or even crash from it.

Now, on the gcc compiler that is used with the ps2 linux kit (I assume this is what you're using for you coursework), you can use

__attribute__((packed))

after a structure member to specify that members as packed, that is, it will not use any more space in the structure than the size of its data type.

for instance, here's a structure I used for a TGA loader on the PS2 linux kit.
(where uint# are previously defined sized integer types)


define PACKED __attribute__((packed))
struct TGAHeader
{
uint8 identsize PACKED; // size of ID field that follows 18 uint8 header (0 usually)
uint8 colourmaptype PACKED; // type of colour map 0=none, 1=has palette
uint8 imagetype PACKED; // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed

uint16 colourmapstart PACKED; // first colour map entry in palette
uint16 colourmaplength PACKED; // number of colours in palette
uint8 colourmapbits PACKED; // number of bits per palette entry 15,16,24,32

uint16 xstart PACKED; // image x origin
uint16 ystart PACKED; // image y origin
uint16 width PACKED; // image width in pixels
uint16 height PACKED; // image height in pixels
uint8 bits PACKED; // image bits per pixel 8,16,24,32
uint8 descriptor PACKED; // image descriptor bits (vh flip bits)
};




As far as I can remember, the PS2 can handle misaligned data just fine (correct me if I'm wrong). If it couldn't (or can't) though, it would still be up to the compiler to ensure that no such reads happen even when using a packed structure. The compiler would have to generate the necessary code to read in aligned parts of the data members, and shift them around in the registers.. nothing you would need to worry about (unless ofcourse you're passing around pointers to members).

Share this post


Link to post
Share on other sites
Quote:
Original post by c2_0
As far as I can remember, the PS2 can handle misaligned data just fine (correct me if I'm wrong). If it couldn't (or can't) though, it would still be up to the compiler to ensure that no such reads happen even when using a packed structure. The compiler would have to generate the necessary code to read in aligned parts of the data members, and shift them around in the registers.. nothing you would need to worry about (unless ofcourse you're passing around pointers to members).
Doesn't the PS2 use a fairly standard MIPS processor? I'm almost certain they don't support unaligned accesses (at least I've experienced crashes from it on a R4400). But you seem to be right about GCC generating special code to to access unaligned data, I just assumed it was your own responsibility to handle such things.

Sorry for causing extra work..

Share this post


Link to post
Share on other sites
Yes, it uses a MIPS processor, and you might be right, I'm honestly not entirely sure if it handles unalligned access. It shouldn't be a problem in this case anyway.

@Leo
On another note, if you're reading a binary file for loading, remember the PS2 is little endian as opposed to big endian as you might expect from most MIPS base architectures.

Share this post


Link to post
Share on other sites
Well I assume that since I prototyped the model loader on the PC, it should be in a little endian format anyway. Shouldn't it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!