Provide packed VkFormat to uint8_t mapping There are only 241 unique format enums at this moment, so they can fit in an 8-bit variable instead of the 32-bit enum. Bug: b/148016460 Change-Id: I00d47a87ac0d053aee44a5d18f3c96d78c0f7cb0 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/42528 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Tested-by: Nicolas Capens <nicolascapens@google.com> Reviewed-by: Alexis Hétu <sugoi@google.com> Reviewed-by: Chris Forbes <chrisforbes@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Vulkan/VkFormat.cpp b/src/Vulkan/VkFormat.cpp index 2638439..c3cc876 100644 --- a/src/Vulkan/VkFormat.cpp +++ b/src/Vulkan/VkFormat.cpp
@@ -2567,4 +2567,55 @@ return false; } +static constexpr uint8_t pack(VkFormat format) +{ + if(format > VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM) + { + return 0; + } + + // 0 - 184 direct mapping + if(format >= 0 && format <= VK_FORMAT_ASTC_12x12_SRGB_BLOCK) + { + return uint8_t(format); + } + + // 10001560xx -> 185 - 218 + if(format >= VK_FORMAT_G8B8G8R8_422_UNORM && format <= VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM) + { + return uint8_t(format - VK_FORMAT_G8B8G8R8_422_UNORM + 185); + } + + // 100005400x -> 219 - 226 + if(format >= VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG && format <= VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG) + { + return uint8_t(format - VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG + 219); + } + + // 10000660xx -> 227 - 240 + if(format >= VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT && format <= VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK_EXT) + { + return uint8_t(format - VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT + 227); + } + + return 0; +} + +static_assert(VK_HEADER_VERSION == 128, "Update VkFormat to uint8_t mapping if needed"); +static_assert(pack(VK_FORMAT_UNDEFINED) == 0, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_ASTC_12x12_SRGB_BLOCK) == 184, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_G8B8G8R8_422_UNORM) == 185, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM) == 218, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG) == 219, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG) == 226, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT) == 227, "Incorrect VkFormat packed value"); +static_assert(pack(VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK_EXT) == 240, "Incorrect VkFormat packed value"); + +uint8_t Format::mapTo8bit(VkFormat format) +{ + ASSERT(format <= VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM); + + return pack(format); +} + } // namespace vk
diff --git a/src/Vulkan/VkFormat.h b/src/Vulkan/VkFormat.h index 9e4b1f8..06752e1 100644 --- a/src/Vulkan/VkFormat.h +++ b/src/Vulkan/VkFormat.h
@@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef VK_FORMAT_UTILS_HPP_ -#define VK_FORMAT_UTILS_HPP_ +#ifndef VK_FORMAT_HPP_ +#define VK_FORMAT_HPP_ #include "System/Types.hpp" @@ -25,7 +25,9 @@ { public: Format() {} - Format(VkFormat format) : format(format) {} + Format(VkFormat format) + : format(format) + {} inline operator VkFormat() const { return format; } bool isUnsignedNormalized() const; @@ -42,7 +44,7 @@ bool isFloatFormat() const; bool isYcbcrFormat() const; - bool isCompatible(const Format& other) const; + bool isCompatible(const Format &other) const; bool isCompressed() const; VkFormat getDecompressedFormat() const; int blockWidth() const; @@ -65,6 +67,8 @@ bool has32bitIntegerTextureComponents() const; bool isRGBComponent(int component) const; + static uint8_t mapTo8bit(VkFormat format); + private: VkFormat compatibleFormat() const; int sliceBUnpadded(int width, int height, int border, bool target) const; @@ -74,4 +78,4 @@ } // namespace vk -#endif // VK_FORMAT_UTILS_HPP_ \ No newline at end of file +#endif // VK_FORMAT_HPP_ \ No newline at end of file