Alignment.h 2.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#ifndef __GPU__ALIGNMENT_H__
#define __GPU__ALIGNMENT_H__




#include "GPUdefs.h"
#include <assert.h>


namespace GPU
{
    inline unsigned int ChannelCount( GLenum format )
    {
        switch( format )
        {
25 26 27
            case GL_LUMINANCE:
            case GL_R        :  return 1;
            case GL_RG       :  return 2;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
            case GL_RGB      :  return 3;
            case GL_RGBA     :  return 4;
            default          :  assert(false);
        }
        return 0;
    }

    inline unsigned int TypeSize( GLenum type )
    {
        switch( type )
        {
            case GL_UNSIGNED_BYTE :  return sizeof(GLubyte );
            case GL_BYTE          :  return sizeof(GLbyte  );
            case GL_UNSIGNED_SHORT:  return sizeof(GLushort);
            case GL_SHORT         :  return sizeof(GLshort );
            case GL_UNSIGNED_INT  :  return sizeof(GLuint  );
            case GL_INT           :  return sizeof(GLint   );
            case GL_FLOAT         :  return sizeof(GLfloat );
            case GL_DOUBLE        :  return sizeof(GLdouble);
            default               :  assert(false);
        }
        return 0;
    }

    inline unsigned int PixelSize( GLenum format, GLenum type )
    {
        return ChannelCount(format) * TypeSize(type);
    }

    inline void SetAlignment( GLenum alignParam, unsigned int stride )
    {
        static const int alignment[8] = { 8, 1, 2, 1, 4, 1, 2, 1 };
60
        gpuAssert( glPixelStorei( alignParam, alignment[stride & 7] ) );
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }

    inline void SetPackAlignment( unsigned int stride )
    {
        SetAlignment( GL_PACK_ALIGNMENT, stride );
    }

    inline void SetPackAlignment( unsigned int width,
                                  GLenum format,
                                  GLenum type )
    {
        SetPackAlignment( width * PixelSize(format, type) );
    }

    inline void SetUnpackAlignment( unsigned int stride )
    {
        SetAlignment( GL_UNPACK_ALIGNMENT, stride );
    }

    inline void SetUnpackAlignment( unsigned int width,
                                    GLenum format,
                                    GLenum type )
    {
        SetUnpackAlignment( width * PixelSize(format, type) );
    }
}




#endif //__GPU__ALIGNMENT_H__