From 4ceeaee1d6b5e1eb2cd044ee1db4c94f549de35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 13 Sep 2020 16:11:26 +0200 Subject: [PATCH] Trade: shuffle code around to avoid unnecessary assertion checks. It does the same, but better. --- .../PbrMetallicRoughnessMaterialData.cpp | 22 ++++++++++--------- .../PbrSpecularGlossinessMaterialData.cpp | 22 ++++++++++--------- src/Magnum/Trade/PhongMaterialData.cpp | 11 +++++----- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/Magnum/Trade/PbrMetallicRoughnessMaterialData.cpp b/src/Magnum/Trade/PbrMetallicRoughnessMaterialData.cpp index 08898f3b3..69c2f30a9 100644 --- a/src/Magnum/Trade/PbrMetallicRoughnessMaterialData.cpp +++ b/src/Magnum/Trade/PbrMetallicRoughnessMaterialData.cpp @@ -222,13 +222,14 @@ Float PbrMetallicRoughnessMaterialData::metalness() const { } UnsignedInt PbrMetallicRoughnessMaterialData::metalnessTexture() const { - /* Explicit assertion because printing that MetalnessTexture isn't found - would be misleading as it can be also MetallicRoughnessTexture */ - CORRADE_ASSERT(hasMetalnessTexture(), - "Trade::PbrMetallicRoughnessMaterialData::metalnessTexture(): the material doesn't have a metalness texture", {}); if(Containers::Optional value = tryAttribute(MaterialAttribute::NoneRoughnessMetallicTexture)) return *value; - return attribute(MaterialAttribute::MetalnessTexture); + if(Containers::Optional value = tryAttribute(MaterialAttribute::MetalnessTexture)) + return *value; + + /* Explicit assertion because printing that MetalnessTexture isn't found + would be misleading as it can be also MetallicRoughnessTexture */ + CORRADE_ASSERT_UNREACHABLE("Trade::PbrMetallicRoughnessMaterialData::metalnessTexture(): the material doesn't have a metalness texture", {}); } MaterialTextureSwizzle PbrMetallicRoughnessMaterialData::metalnessTextureSwizzle() const { @@ -260,13 +261,14 @@ Float PbrMetallicRoughnessMaterialData::roughness() const { } UnsignedInt PbrMetallicRoughnessMaterialData::roughnessTexture() const { - /* Explicit assertion because printing that RoughnessTexture isn't found - would be misleading as it can be also MetallicRoughnessTexture */ - CORRADE_ASSERT(hasRoughnessTexture(), - "Trade::PbrMetallicRoughnessMaterialData::roughnessTexture(): the material doesn't have a roughness texture", {}); if(Containers::Optional value = tryAttribute(MaterialAttribute::NoneRoughnessMetallicTexture)) return *value; - return attribute(MaterialAttribute::RoughnessTexture); + if(Containers::Optional value = tryAttribute(MaterialAttribute::RoughnessTexture)) + return *value; + + /* Explicit assertion because printing that RoughnessTexture isn't found + would be misleading as it can be also MetallicRoughnessTexture */ + CORRADE_ASSERT_UNREACHABLE("Trade::PbrMetallicRoughnessMaterialData::roughnessTexture(): the material doesn't have a roughness texture", {}); } MaterialTextureSwizzle PbrMetallicRoughnessMaterialData::roughnessTextureSwizzle() const { diff --git a/src/Magnum/Trade/PbrSpecularGlossinessMaterialData.cpp b/src/Magnum/Trade/PbrSpecularGlossinessMaterialData.cpp index 11fd16b71..56c6fca65 100644 --- a/src/Magnum/Trade/PbrSpecularGlossinessMaterialData.cpp +++ b/src/Magnum/Trade/PbrSpecularGlossinessMaterialData.cpp @@ -156,13 +156,14 @@ Color4 PbrSpecularGlossinessMaterialData::specularColor() const { } UnsignedInt PbrSpecularGlossinessMaterialData::specularTexture() const { - /* Explicit assertion because printing that SpecularTexture isn't found - would be misleading as it can be also SpecularGlossinessTexture */ - CORRADE_ASSERT(hasSpecularTexture(), - "Trade::PbrSpecularGlossinessMaterialData::specularTexture(): the material doesn't have a specular texture", {}); if(Containers::Optional value = tryAttribute(MaterialAttribute::SpecularGlossinessTexture)) return *value; - return attribute(MaterialAttribute::SpecularTexture); + if(Containers::Optional value = tryAttribute(MaterialAttribute::SpecularTexture)) + return *value; + + /* Explicit assertion because printing that SpecularTexture isn't found + would be misleading as it can be also SpecularGlossinessTexture */ + CORRADE_ASSERT_UNREACHABLE("Trade::PbrSpecularGlossinessMaterialData::specularTexture(): the material doesn't have a specular texture", {}); } MaterialTextureSwizzle PbrSpecularGlossinessMaterialData::specularTextureSwizzle() const { @@ -194,13 +195,14 @@ Float PbrSpecularGlossinessMaterialData::glossiness() const { } UnsignedInt PbrSpecularGlossinessMaterialData::glossinessTexture() const { - /* Explicit assertion because printing that GlossinessTexture isn't found - would be misleading as it can be also SpecularGlossinessTexture */ - CORRADE_ASSERT(hasGlossinessTexture(), - "Trade::PbrSpecularGlossinessMaterialData::glossinessTexture(): the material doesn't have a glossiness texture", {}); if(Containers::Optional value = tryAttribute(MaterialAttribute::SpecularGlossinessTexture)) return *value; - return attribute(MaterialAttribute::GlossinessTexture); + if(Containers::Optional value = tryAttribute(MaterialAttribute::GlossinessTexture)) + return *value; + + /* Explicit assertion because printing that GlossinessTexture isn't found + would be misleading as it can be also SpecularGlossinessTexture */ + CORRADE_ASSERT_UNREACHABLE("Trade::PbrSpecularGlossinessMaterialData::glossinessTexture(): the material doesn't have a glossiness texture", {}); } MaterialTextureSwizzle PbrSpecularGlossinessMaterialData::glossinessTextureSwizzle() const { diff --git a/src/Magnum/Trade/PhongMaterialData.cpp b/src/Magnum/Trade/PhongMaterialData.cpp index 1a8b9150e..632d96196 100644 --- a/src/Magnum/Trade/PhongMaterialData.cpp +++ b/src/Magnum/Trade/PhongMaterialData.cpp @@ -250,13 +250,14 @@ Color4 PhongMaterialData::specularColor() const { } UnsignedInt PhongMaterialData::specularTexture() const { - /* Explicit assertion because printing that SpecularTexture isn't found - would be misleading as it can be also SpecularGlossinessTexture */ - CORRADE_ASSERT(hasSpecularTexture(), - "Trade::PhongMaterialData::specularTexture(): the material doesn't have a specular texture", {}); if(Containers::Optional value = tryAttribute(MaterialAttribute::SpecularGlossinessTexture)) return *value; - return attribute(MaterialAttribute::SpecularTexture); + if(Containers::Optional value = tryAttribute(MaterialAttribute::SpecularTexture)) + return *value; + + /* Explicit assertion because printing that SpecularTexture isn't found + would be misleading as it can be also SpecularGlossinessTexture */ + CORRADE_ASSERT_UNREACHABLE("Trade::PhongMaterialData::specularTexture(): the material doesn't have a specular texture", {}); } MaterialTextureSwizzle PhongMaterialData::specularTextureSwizzle() const {