1 /*
2  * Copyright © 2022 Imagination Technologies Ltd.
3  *
4  * based in part on radv driver which is:
5  * Copyright © 2016 Red Hat.
6  * Copyright © 2016 Bas Nieuwenhuizen
7  *
8  * Based on u_format.h which is:
9  * Copyright 2009-2010 VMware, Inc.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  */
30 
31 /* clang-format off */
32 #ifndef VK_FORMAT_H
33 #define VK_FORMAT_H
34 
35 #include <util/format/u_format.h>
36 #include <vulkan/util/vk_format.h>
37 
38 #include <vulkan/vulkan.h>
39 
40 #include "util/u_endian.h"
41 
42 static inline bool
vk_format_is_alpha_on_msb(VkFormat vk_format)43 vk_format_is_alpha_on_msb(VkFormat vk_format)
44 {
45    const struct util_format_description *desc =
46       vk_format_description(vk_format);
47 
48    return (desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
49            desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
50 #if defined(UTIL_ARCH_BIG_ENDIAN)
51            desc->swizzle[3] == PIPE_SWIZZLE_X;
52 #else
53            desc->swizzle[3] == PIPE_SWIZZLE_W;
54 #endif
55 }
56 
57 static inline boolean
vk_format_has_alpha(VkFormat vk_format)58 vk_format_has_alpha(VkFormat vk_format)
59 {
60    return util_format_has_alpha(vk_format_to_pipe_format(vk_format));
61 }
62 
63 static inline boolean
vk_format_is_pure_integer(VkFormat vk_format)64 vk_format_is_pure_integer(VkFormat vk_format)
65 {
66    return util_format_is_pure_integer(vk_format_to_pipe_format(vk_format));
67 }
68 
69 static inline uint
vk_format_get_blocksizebits(VkFormat vk_format)70 vk_format_get_blocksizebits(VkFormat vk_format)
71 {
72    return util_format_get_blocksizebits(vk_format_to_pipe_format(vk_format));
73 }
74 
75 static inline uint
vk_format_get_channel_width(VkFormat vk_format,uint32_t channel)76 vk_format_get_channel_width(VkFormat vk_format, uint32_t channel)
77 {
78    const struct util_format_description *desc =
79       vk_format_description(vk_format);
80 
81    return desc->channel[channel].size;
82 }
83 
84 static inline boolean
vk_format_has_32bit_component(VkFormat vk_format)85 vk_format_has_32bit_component(VkFormat vk_format)
86 {
87    const struct util_format_description *desc =
88       vk_format_description(vk_format);
89 
90    for (uint32_t i = 0; i < desc->nr_channels; i++) {
91       if (desc->channel[i].size == 32U)
92          return true;
93    }
94 
95    return false;
96 }
97 
98 static inline uint
vk_format_get_component_size_in_bits(VkFormat vk_format,enum util_format_colorspace colorspace,uint32_t component)99 vk_format_get_component_size_in_bits(VkFormat vk_format,
100                                      enum util_format_colorspace colorspace,
101                                      uint32_t component)
102 {
103    return util_format_get_component_bits(vk_format_to_pipe_format(vk_format),
104                                          colorspace,
105                                          component);
106 }
107 
108 static inline boolean
vk_format_is_normalized(VkFormat vk_format)109 vk_format_is_normalized(VkFormat vk_format)
110 {
111    const struct util_format_description *desc =
112       vk_format_description(vk_format);
113 
114    for (uint32_t i = 0; i < desc->nr_channels; i++) {
115       if (!desc->channel[i].normalized)
116          return false;
117    }
118 
119    return true;
120 }
121 
122 #endif /* VK_FORMAT_H */
123