Improve msvc codegen for hot-path array LUTs

In some constexpr functions, msvc is building the LUT at runtime
(pushing each element onto the stack) out of an abundance of caution. Moving the
arrays into be file-scoped constexpr's avoids this and turns the functions into
simple look-ups as intended.
This commit is contained in:
heapo 2018-12-04 10:32:00 -08:00
parent 465f486160
commit 7853e6b5d4

View File

@ -125,17 +125,7 @@ enum class SurfaceTarget {
TextureCubeArray, TextureCubeArray,
}; };
/** constexpr std::array<u32, MaxPixelFormat> compression_factor_table = {{
* Gets the compression factor for the specified PixelFormat. This applies to just the
* "compressed width" and "compressed height", not the overall compression factor of a
* compressed image. This is used for maintaining proper surface sizes for compressed
* texture formats.
*/
static constexpr u32 GetCompressionFactor(PixelFormat format) {
if (format == PixelFormat::Invalid)
return 0;
constexpr std::array<u32, MaxPixelFormat> compression_factor_table = {{
1, // ABGR8U 1, // ABGR8U
1, // ABGR8S 1, // ABGR8S
1, // ABGR8UI 1, // ABGR8UI
@ -202,16 +192,23 @@ static constexpr u32 GetCompressionFactor(PixelFormat format) {
1, // Z24S8 1, // Z24S8
1, // S8Z24 1, // S8Z24
1, // Z32FS8 1, // Z32FS8
}}; }};
/**
* Gets the compression factor for the specified PixelFormat. This applies to just the
* "compressed width" and "compressed height", not the overall compression factor of a
* compressed image. This is used for maintaining proper surface sizes for compressed
* texture formats.
*/
static constexpr u32 GetCompressionFactor(PixelFormat format) {
if (format == PixelFormat::Invalid)
return 0;
ASSERT(static_cast<std::size_t>(format) < compression_factor_table.size()); ASSERT(static_cast<std::size_t>(format) < compression_factor_table.size());
return compression_factor_table[static_cast<std::size_t>(format)]; return compression_factor_table[static_cast<std::size_t>(format)];
} }
static constexpr u32 GetDefaultBlockWidth(PixelFormat format) { constexpr std::array<u32, MaxPixelFormat> block_width_table = {{
if (format == PixelFormat::Invalid)
return 0;
constexpr std::array<u32, MaxPixelFormat> block_width_table = {{
1, // ABGR8U 1, // ABGR8U
1, // ABGR8S 1, // ABGR8S
1, // ABGR8UI 1, // ABGR8UI
@ -278,16 +275,17 @@ static constexpr u32 GetDefaultBlockWidth(PixelFormat format) {
1, // Z24S8 1, // Z24S8
1, // S8Z24 1, // S8Z24
1, // Z32FS8 1, // Z32FS8
}}; }};
static constexpr u32 GetDefaultBlockWidth(PixelFormat format) {
if (format == PixelFormat::Invalid)
return 0;
ASSERT(static_cast<std::size_t>(format) < block_width_table.size()); ASSERT(static_cast<std::size_t>(format) < block_width_table.size());
return block_width_table[static_cast<std::size_t>(format)]; return block_width_table[static_cast<std::size_t>(format)];
} }
static constexpr u32 GetDefaultBlockHeight(PixelFormat format) { constexpr std::array<u32, MaxPixelFormat> block_height_table = {{
if (format == PixelFormat::Invalid)
return 0;
constexpr std::array<u32, MaxPixelFormat> block_height_table = {{
1, // ABGR8U 1, // ABGR8U
1, // ABGR8S 1, // ABGR8S
1, // ABGR8UI 1, // ABGR8UI
@ -354,17 +352,17 @@ static constexpr u32 GetDefaultBlockHeight(PixelFormat format) {
1, // Z24S8 1, // Z24S8
1, // S8Z24 1, // S8Z24
1, // Z32FS8 1, // Z32FS8
}}; }};
static constexpr u32 GetDefaultBlockHeight(PixelFormat format) {
if (format == PixelFormat::Invalid)
return 0;
ASSERT(static_cast<std::size_t>(format) < block_height_table.size()); ASSERT(static_cast<std::size_t>(format) < block_height_table.size());
return block_height_table[static_cast<std::size_t>(format)]; return block_height_table[static_cast<std::size_t>(format)];
} }
static constexpr u32 GetFormatBpp(PixelFormat format) { constexpr std::array<u32, MaxPixelFormat> bpp_table = {{
if (format == PixelFormat::Invalid)
return 0;
constexpr std::array<u32, MaxPixelFormat> bpp_table = {{
32, // ABGR8U 32, // ABGR8U
32, // ABGR8S 32, // ABGR8S
32, // ABGR8UI 32, // ABGR8UI
@ -431,7 +429,11 @@ static constexpr u32 GetFormatBpp(PixelFormat format) {
32, // Z24S8 32, // Z24S8
32, // S8Z24 32, // S8Z24
64, // Z32FS8 64, // Z32FS8
}}; }};
static constexpr u32 GetFormatBpp(PixelFormat format) {
if (format == PixelFormat::Invalid)
return 0;
ASSERT(static_cast<std::size_t>(format) < bpp_table.size()); ASSERT(static_cast<std::size_t>(format) < bpp_table.size());
return bpp_table[static_cast<std::size_t>(format)]; return bpp_table[static_cast<std::size_t>(format)];