Skip to content

Commit

Permalink
Always translate to the default swizzle (RGBA) when swizzling is not …
Browse files Browse the repository at this point in the history
…supported by the graphics API profile
  • Loading branch information
Groovounet committed Mar 16, 2016
1 parent 776dc6d commit d9d8889
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
13 changes: 10 additions & 3 deletions gli/core/gl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,11 @@ namespace detail

gl::format_desc const& FormatDesc = this->FormatDesc[Format - FORMAT_FIRST];

bool const IsExternalBGRA = ((FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_FORMAT_BIT) && !has_swizzle(this->Profile)) || (FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_TYPE_BIT);

gl::format FormatGL;
FormatGL.Internal = FormatDesc.Internal;
FormatGL.External = FormatDesc.External;
FormatGL.Type = FormatDesc.Type;
FormatGL.Swizzles = detail::translate(IsExternalBGRA ? gli::swizzles(Swizzles.b, Swizzles.g, Swizzles.r, Swizzles.a) : Swizzles);
FormatGL.Swizzles = this->compute_swizzle(FormatDesc, Swizzles);
return FormatGL;
}

Expand All @@ -356,4 +354,13 @@ namespace detail
return static_cast<gli::format>(FORMAT_INVALID);
}

inline gl::swizzles gl::compute_swizzle(format_desc const& FormatDesc, gli::swizzles const& Swizzles) const
{
if (!this->has_swizzle(this->Profile))
return swizzles(gl::SWIZZLE_RED, gl::SWIZZLE_GREEN, gl::SWIZZLE_BLUE, gl::SWIZZLE_ALPHA);

bool const IsExternalBGRA = ((FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_FORMAT_BIT) && !has_swizzle(this->Profile)) || (FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_TYPE_BIT);

return detail::translate(IsExternalBGRA ? gli::swizzles(Swizzles.b, Swizzles.g, Swizzles.r, Swizzles.a) : Swizzles);
}
}//namespace gli
12 changes: 7 additions & 5 deletions gli/gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,6 @@ namespace gli
gli::format find(internal_format InternalFormat, external_format ExternalFormat, type_format Type);

private:
bool has_swizzle(profile Profile) const
{
return Profile == PROFILE_ES30 || Profile == PROFILE_GL33;
}

struct format_desc
{
internal_format Internal;
Expand All @@ -363,6 +358,13 @@ namespace gli
unsigned int Properties;
};

bool has_swizzle(profile Profile) const
{
return Profile == PROFILE_ES30 || Profile == PROFILE_GL33;
}

gl::swizzles compute_swizzle(format_desc const& FormatDesc, gli::swizzles const& Swizzle) const;

std::array<format_desc, FORMAT_COUNT> FormatDesc;
profile Profile;
};
Expand Down
18 changes: 12 additions & 6 deletions test/core/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ namespace ktx
Error += FormatA.Internal == gli::gl::INTERNAL_RGB8_UNORM ? 0 : 1;
Error += FormatA.External == gli::gl::EXTERNAL_RGB ? 0 : 1;
Error += FormatA.Type == gli::gl::TYPE_U8 ? 0 : 1;
Error += FormatA.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
// Swizzle is not supported by KTX, so we always return the constant default swizzle
Error += FormatA.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;

gli::texture2d TextureB(gli::FORMAT_BGR8_UNORM_PACK8, gli::texture2d::extent_type(2), 1, gli::texture2d::swizzles_type(gli::SWIZZLE_RED, gli::SWIZZLE_GREEN, gli::SWIZZLE_BLUE, gli::SWIZZLE_ALPHA));
gli::gl::format FormatB = GL.translate(TextureB.format(), TextureB.swizzles());

Error += FormatB.Internal == gli::gl::INTERNAL_RGB8_UNORM ? 0 : 1;
Error += FormatB.External == gli::gl::EXTERNAL_BGR ? 0 : 1;
Error += FormatB.Type == gli::gl::TYPE_U8 ? 0 : 1;
Error += FormatB.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
// Swizzle is not supported by KTX, so we always return the constant default swizzle
Error += FormatB.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;

return Error;
}
Expand All @@ -43,15 +45,17 @@ namespace gl32
Error += FormatA.Internal == gli::gl::INTERNAL_RGB8_UNORM ? 0 : 1;
Error += FormatA.External == gli::gl::EXTERNAL_RGB ? 0 : 1;
Error += FormatA.Type == gli::gl::TYPE_U8 ? 0 : 1;
Error += FormatA.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
// Swizzle is not supported by OpenGL 3.2, so we always return the constant default swizzle
Error += FormatA.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;

gli::texture2d TextureB(gli::FORMAT_BGR8_UNORM_PACK8, gli::texture2d::extent_type(2), 1, gli::texture2d::swizzles_type(gli::SWIZZLE_RED, gli::SWIZZLE_GREEN, gli::SWIZZLE_BLUE, gli::SWIZZLE_ALPHA));
gli::gl::format FormatB = GL.translate(TextureB.format(), TextureB.swizzles());

Error += FormatB.Internal == gli::gl::INTERNAL_RGB8_UNORM ? 0 : 1;
Error += FormatB.External == gli::gl::EXTERNAL_BGR ? 0 : 1;
Error += FormatB.Type == gli::gl::TYPE_U8 ? 0 : 1;
Error += FormatB.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
// Swizzle is not supported by OpenGL 3.2, so we always return the constant default swizzle
Error += FormatB.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;

{
gli::texture2d TextureC(gli::FORMAT_R5G6B5_UNORM_PACK16, gli::texture2d::extent_type(2), 1, gli::texture2d::swizzles_type(gli::SWIZZLE_RED, gli::SWIZZLE_GREEN, gli::SWIZZLE_BLUE, gli::SWIZZLE_ALPHA));
Expand All @@ -60,7 +64,8 @@ namespace gl32
Error += FormatC.Internal == gli::gl::INTERNAL_R5G6B5 ? 0 : 1;
Error += FormatC.External == gli::gl::EXTERNAL_RGB ? 0 : 1;
Error += FormatC.Type == gli::gl::TYPE_UINT16_R5G6B5_REV ? 0 : 1;
Error += FormatC.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
// Swizzle is not supported by OpenGL 3.2, so we always return the constant default swizzle
Error += FormatC.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;

gli::format FormatD2 = GL.find(FormatC.Internal, FormatC.External, FormatC.Type);
Error += FormatD2 == TextureC.format() ? 0 : 1;
Expand All @@ -73,7 +78,8 @@ namespace gl32
Error += FormatD.Internal == gli::gl::INTERNAL_R5G6B5 ? 0 : 1;
Error += FormatD.External == gli::gl::EXTERNAL_RGB ? 0 : 1;
Error += FormatD.Type == gli::gl::TYPE_UINT16_R5G6B5 ? 0 : 1;
Error += FormatD.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
// Swizzle is not supported by OpenGL 3.2, so we always return the constant default swizzle
Error += FormatD.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;

gli::format FormatD2 = GL.find(FormatD.Internal, FormatD.External, FormatD.Type);
Error += FormatD2 == TextureD.format() ? 0 : 1;
Expand Down

0 comments on commit d9d8889

Please sign in to comment.