diff --git a/include/QF/Vulkan/image.h b/include/QF/Vulkan/image.h index 71ca57389..9942562b2 100644 --- a/include/QF/Vulkan/image.h +++ b/include/QF/Vulkan/image.h @@ -82,5 +82,6 @@ VkImageView QFV_CreateImageView (struct qfv_device_s *device, void QFV_GenerateMipMaps (struct qfv_device_s *device, VkCommandBuffer cmd, VkImage image, unsigned mips, unsigned width, unsigned height, unsigned layers); +int QFV_MipLevels (int width, int height) __attribute__((const)); #endif//__QF_Vulkan_image_h diff --git a/libs/video/renderer/vulkan/image.c b/libs/video/renderer/vulkan/image.c index 92d584f80..41b4586f0 100644 --- a/libs/video/renderer/vulkan/image.c +++ b/libs/video/renderer/vulkan/image.c @@ -275,3 +275,33 @@ QFV_GenerateMipMaps (qfv_device_t *device, VkCommandBuffer cmd, 0, 0, 0, 0, 1, &final_barrier); } + +static int +ilog2 (unsigned x) +{ + unsigned o = x; + if (x > 0x7fffffff) { + // avoid overflow + return 31; + } + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x++; + int y = 0; + y |= ((x & 0xffff0000) != 0) << 4; + y |= ((x & 0xff00ff00) != 0) << 3; + y |= ((x & 0xf0f0f0f0) != 0) << 2; + y |= ((x & 0xcccccccc) != 0) << 1; + y |= ((x & 0xaaaaaaaa) != 0) << 0; + return y - ((o & (x - 1)) != 0); +} + +int +QFV_MipLevels (int width, int height) +{ + return ilog2 (max (width, height)) + 1; +} diff --git a/libs/video/renderer/vulkan/vulkan_texture.c b/libs/video/renderer/vulkan/vulkan_texture.c index 4a1e2d2ab..7d8b13155 100644 --- a/libs/video/renderer/vulkan/vulkan_texture.c +++ b/libs/video/renderer/vulkan/vulkan_texture.c @@ -64,30 +64,6 @@ #include "r_scrap.h" #include "vid_vulkan.h" -static int -ilog2 (unsigned x) -{ - unsigned o = x; - if (x > 0x7fffffff) { - // avoid overflow - return 31; - } - x--; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - x++; - int y = 0; - y |= ((x & 0xffff0000) != 0) << 4; - y |= ((x & 0xff00ff00) != 0) << 3; - y |= ((x & 0xf0f0f0f0) != 0) << 2; - y |= ((x & 0xcccccccc) != 0) << 1; - y |= ((x & 0xaaaaaaaa) != 0) << 0; - return y - ((o & (x - 1)) != 0); -} - void Vulkan_ExpandPalette (byte *dst, const byte *src, const byte *palette, int alpha, int count) @@ -164,7 +140,7 @@ Vulkan_LoadTex (vulkan_ctx_t *ctx, tex_t *tex, int mip, const char *name) } if (mip) { - mip = ilog2 (max (tex->width, tex->height)) + 1; + mip = QFV_MipLevels (tex->width, tex->height); } else { mip = 1; }