Skip to content

Commit d9d8889

Browse files
committed
Always translate to the default swizzle (RGBA) when swizzling is not supported by the graphics API profile
1 parent 776dc6d commit d9d8889

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

gli/core/gl.inl

+10-3
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,11 @@ namespace detail
329329

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

332-
bool const IsExternalBGRA = ((FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_FORMAT_BIT) && !has_swizzle(this->Profile)) || (FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_TYPE_BIT);
333-
334332
gl::format FormatGL;
335333
FormatGL.Internal = FormatDesc.Internal;
336334
FormatGL.External = FormatDesc.External;
337335
FormatGL.Type = FormatDesc.Type;
338-
FormatGL.Swizzles = detail::translate(IsExternalBGRA ? gli::swizzles(Swizzles.b, Swizzles.g, Swizzles.r, Swizzles.a) : Swizzles);
336+
FormatGL.Swizzles = this->compute_swizzle(FormatDesc, Swizzles);
339337
return FormatGL;
340338
}
341339

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

357+
inline gl::swizzles gl::compute_swizzle(format_desc const& FormatDesc, gli::swizzles const& Swizzles) const
358+
{
359+
if (!this->has_swizzle(this->Profile))
360+
return swizzles(gl::SWIZZLE_RED, gl::SWIZZLE_GREEN, gl::SWIZZLE_BLUE, gl::SWIZZLE_ALPHA);
361+
362+
bool const IsExternalBGRA = ((FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_FORMAT_BIT) && !has_swizzle(this->Profile)) || (FormatDesc.Properties & detail::FORMAT_PROPERTY_BGRA_TYPE_BIT);
363+
364+
return detail::translate(IsExternalBGRA ? gli::swizzles(Swizzles.b, Swizzles.g, Swizzles.r, Swizzles.a) : Swizzles);
365+
}
359366
}//namespace gli

gli/gl.hpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,6 @@ namespace gli
350350
gli::format find(internal_format InternalFormat, external_format ExternalFormat, type_format Type);
351351

352352
private:
353-
bool has_swizzle(profile Profile) const
354-
{
355-
return Profile == PROFILE_ES30 || Profile == PROFILE_GL33;
356-
}
357-
358353
struct format_desc
359354
{
360355
internal_format Internal;
@@ -363,6 +358,13 @@ namespace gli
363358
unsigned int Properties;
364359
};
365360

361+
bool has_swizzle(profile Profile) const
362+
{
363+
return Profile == PROFILE_ES30 || Profile == PROFILE_GL33;
364+
}
365+
366+
gl::swizzles compute_swizzle(format_desc const& FormatDesc, gli::swizzles const& Swizzle) const;
367+
366368
std::array<format_desc, FORMAT_COUNT> FormatDesc;
367369
profile Profile;
368370
};

test/core/gl.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ namespace ktx
1515
Error += FormatA.Internal == gli::gl::INTERNAL_RGB8_UNORM ? 0 : 1;
1616
Error += FormatA.External == gli::gl::EXTERNAL_RGB ? 0 : 1;
1717
Error += FormatA.Type == gli::gl::TYPE_U8 ? 0 : 1;
18-
Error += FormatA.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
18+
// Swizzle is not supported by KTX, so we always return the constant default swizzle
19+
Error += FormatA.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;
1920

2021
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));
2122
gli::gl::format FormatB = GL.translate(TextureB.format(), TextureB.swizzles());
2223

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

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

4851
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));
4952
gli::gl::format FormatB = GL.translate(TextureB.format(), TextureB.swizzles());
5053

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

5660
{
5761
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));
@@ -60,7 +64,8 @@ namespace gl32
6064
Error += FormatC.Internal == gli::gl::INTERNAL_R5G6B5 ? 0 : 1;
6165
Error += FormatC.External == gli::gl::EXTERNAL_RGB ? 0 : 1;
6266
Error += FormatC.Type == gli::gl::TYPE_UINT16_R5G6B5_REV ? 0 : 1;
63-
Error += FormatC.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ONE) ? 0 : 1;
67+
// Swizzle is not supported by OpenGL 3.2, so we always return the constant default swizzle
68+
Error += FormatC.Swizzles == gli::gl::swizzles(gli::gl::SWIZZLE_RED, gli::gl::SWIZZLE_GREEN, gli::gl::SWIZZLE_BLUE, gli::gl::SWIZZLE_ALPHA) ? 0 : 1;
6469

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

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

0 commit comments

Comments
 (0)