]> git.tdb.fi Git - libs/gl.git/commitdiff
Expand the PixelFormat conversion API
authorMikko Rasa <tdb@tdb.fi>
Thu, 3 Nov 2016 10:50:35 +0000 (12:50 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 3 Nov 2016 11:00:43 +0000 (13:00 +0200)
Get_sized_pixelformat now takes an explicit size parameter.  The decision
between 16-bit and 32-bit depth component formats has been moved to
Texture::set_internal_format.

Get_unsized_pixelformat was added as an intermediate step for
get_base_pixelformat.  It preserves sRGB information.

source/pixelformat.cpp
source/pixelformat.h
source/texture.cpp

index 320fef1f84dd16e447b106df64f1069275c55b89..d866bcf8094b9f9d677ccea8c00bf36db8842cd7 100644 (file)
@@ -77,6 +77,19 @@ PixelFormat storage_pixelformat_from_graphics(Graphics::PixelFormat pf, bool srg
 }
 
 PixelFormat get_base_pixelformat(PixelFormat pf)
+{
+       PixelFormat unsized = get_unsized_pixelformat(pf);
+       switch(unsized)
+       {
+       case SRGB: return RGB;
+       case SRGB_ALPHA: return RGBA;
+       case SLUMINANCE: return LUMINANCE;
+       case SLUMINANCE_ALPHA: return LUMINANCE_ALPHA;
+       default: return unsized;
+       }
+}
+
+PixelFormat get_unsized_pixelformat(PixelFormat pf)
 {
        switch(pf)
        {
@@ -88,20 +101,16 @@ PixelFormat get_base_pixelformat(PixelFormat pf)
        case RG32F: return RG;
        case RGB8:
        case RGB16F:
-       case RGB32F:
-       case SRGB:
-       case SRGB8: return RGB;
+       case RGB32F: return RGB;
+       case SRGB8: return SRGB;
        case RGBA8:
        case RGBA16F:
-       case RGBA32F:
-       case SRGB_ALPHA:
-       case SRGB8_ALPHA8: return RGBA;
-       case LUMINANCE8:
-       case SLUMINANCE:
-       case SLUMINANCE8: return LUMINANCE;
-       case LUMINANCE_ALPHA8:
-       case SLUMINANCE_ALPHA:
-       case SLUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
+       case RGBA32F: return RGBA;
+       case SRGB8_ALPHA8: return SRGB_ALPHA;
+       case LUMINANCE8: return LUMINANCE;
+       case SLUMINANCE8: return SLUMINANCE;
+       case LUMINANCE_ALPHA8: return LUMINANCE_ALPHA;
+       case SLUMINANCE8_ALPHA8: return SLUMINANCE_ALPHA;
        case DEPTH_COMPONENT16:
        case DEPTH_COMPONENT24:
        case DEPTH_COMPONENT32: return DEPTH_COMPONENT;
@@ -109,26 +118,64 @@ PixelFormat get_base_pixelformat(PixelFormat pf)
        }
 }
 
-PixelFormat get_sized_pixelformat(PixelFormat pf)
+PixelFormat get_sized_pixelformat(PixelFormat pf, unsigned size)
 {
+       if(!size || size>4)
+               throw invalid_argument("get_sized_pixelformat");
+
        switch(pf)
        {
-       case RED: return R8;
-       case RG: return RG8;
-       case RGB: return RGB8;
-       case RGBA: return RGBA8;
-       case SRGB: return SRGB8;
-       case SRGB_ALPHA: return SRGB8_ALPHA8;
-       case LUMINANCE: return LUMINANCE8;
-       case SLUMINANCE: return SLUMINANCE8;
-       case LUMINANCE_ALPHA: return LUMINANCE8_ALPHA8;
-       case SLUMINANCE_ALPHA: return SLUMINANCE8_ALPHA8;
-       case DEPTH_COMPONENT:
-               if(get_gl_api()==OPENGL_ES2)
-                       return DEPTH_COMPONENT16;
+       case SRGB:
+       case SRGB_ALPHA: break;
+       case SRGB8: pf = SRGB; break;
+       case SRGB8_ALPHA8: pf = SRGB_ALPHA; break;
+       default: pf = get_base_pixelformat(pf);
+       }
+
+       switch(size)
+       {
+       case 1:
+               switch(pf)
+               {
+               case RED: return R8;
+               case RG: return RG8;
+               case RGB: return RGB8;
+               case RGBA: return RGBA8;
+               case SRGB: return SRGB8;
+               case SRGB_ALPHA: return SRGB8_ALPHA8;
+               case LUMINANCE: return LUMINANCE8;
+               case SLUMINANCE: return SLUMINANCE8;
+               case LUMINANCE_ALPHA: return LUMINANCE8_ALPHA8;
+               case SLUMINANCE_ALPHA: return SLUMINANCE8_ALPHA8;
+               default: throw invalid_argument("get_sized_pixelformat");
+               }
+       case 2:
+               switch(pf)
+               {
+               case RED: return R16F;
+               case RG: return RG16F;
+               case RGB: return RGB16F;
+               case RGBA: return RGBA16F;
+               case DEPTH_COMPONENT: return DEPTH_COMPONENT16;
+               default: throw invalid_argument("get_sized_pixelformat");
+               }
+       case 3:
+               if(pf==DEPTH_COMPONENT)
+                       return DEPTH_COMPONENT24;
                else
-                       return DEPTH_COMPONENT32;
-       default: return pf;
+                       throw invalid_argument("get_sized_pixelformat");
+       case 4:
+               switch(pf)
+               {
+               case RED: return R32F;
+               case RG: return RG32F;
+               case RGB: return RGB32F;
+               case RGBA: return RGBA32F;
+               case DEPTH_COMPONENT: return DEPTH_COMPONENT32;
+               default: throw invalid_argument("get_sized_pixelformat");
+               }
+       default:
+               throw invalid_argument("get_sized_pixelformat");
        }
 }
 
index 17a90d365781095f650109457ec6673506522ac1..dcbb5948f69ad87a391cc1a6f2af4a26a3f997be 100644 (file)
@@ -63,7 +63,8 @@ PixelFormat pixelformat_from_graphics(Graphics::PixelFormat);
 PixelFormat storage_pixelformat_from_graphics(Graphics::PixelFormat, bool = false);
 
 PixelFormat get_base_pixelformat(PixelFormat);
-PixelFormat get_sized_pixelformat(PixelFormat);
+PixelFormat get_unsized_pixelformat(PixelFormat);
+PixelFormat get_sized_pixelformat(PixelFormat, unsigned = 1);
 PixelFormat get_srgb_pixelformat(PixelFormat);
 unsigned get_component_count(PixelFormat);
 unsigned get_component_size(PixelFormat);
index 21fc7a6ae9bb41711cf079795e752196c4a2e014..b545b16606eaa64a47c2baac3cedd82df39df029 100644 (file)
@@ -87,8 +87,11 @@ DataType Texture::get_alloc_type(PixelFormat fmt)
 
 void Texture::set_internal_format(PixelFormat fmt)
 {
-       if(MSP_sized_internal_formats)
-               fmt = get_sized_pixelformat(fmt);
+       if(!get_component_size(fmt) && MSP_sized_internal_formats)
+       {
+               unsigned size = (fmt==DEPTH_COMPONENT ? get_gl_api()==OPENGL_ES2 ? 2 : 4 : 1);
+               fmt = get_sized_pixelformat(fmt, size);
+       }
 
        require_pixelformat(fmt);
        ifmt = fmt;