• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef VK_FORMAT_H
32 #define VK_FORMAT_H
33 
34 #include <stdbool.h>
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 
vk_format_is_alpha_on_msb(VkFormat vk_format)42 static inline bool vk_format_is_alpha_on_msb(VkFormat vk_format)
43 {
44    const struct util_format_description *desc =
45       vk_format_description(vk_format);
46 
47    return (desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
48            desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
49 #if UTIL_ARCH_BIG_ENDIAN
50           desc->swizzle[3] == PIPE_SWIZZLE_X;
51 #else
52           desc->swizzle[3] == PIPE_SWIZZLE_W;
53 #endif
54 }
55 
vk_format_get_channel_width(VkFormat vk_format,uint32_t channel)56 static inline uint vk_format_get_channel_width(VkFormat vk_format,
57                                                uint32_t channel)
58 {
59    const struct util_format_description *desc =
60       vk_format_description(vk_format);
61 
62    return desc->channel[channel].size;
63 }
64 
vk_format_has_32bit_component(VkFormat vk_format)65 static inline bool vk_format_has_32bit_component(VkFormat vk_format)
66 {
67    const struct util_format_description *desc =
68       vk_format_description(vk_format);
69 
70    for (uint32_t i = 0; i < desc->nr_channels; i++) {
71       if (desc->channel[i].size == 32U)
72          return true;
73    }
74 
75    return false;
76 }
77 
vk_format_is_normalized(VkFormat vk_format)78 static inline bool vk_format_is_normalized(VkFormat vk_format)
79 {
80    const struct util_format_description *desc =
81       vk_format_description(vk_format);
82 
83    for (uint32_t i = 0; i < desc->nr_channels; i++) {
84       if (!desc->channel[i].normalized)
85          return false;
86    }
87 
88    return true;
89 }
90 
91 static inline uint32_t
vk_format_get_common_color_channel_count(VkFormat src_format,VkFormat dst_format)92 vk_format_get_common_color_channel_count(VkFormat src_format,
93                                          VkFormat dst_format)
94 {
95    const struct util_format_description *dst_desc =
96       vk_format_description(dst_format);
97    const struct util_format_description *src_desc =
98       vk_format_description(src_format);
99    uint32_t count = 0;
100 
101    /* Check if destination format is alpha only and source format has alpha
102     * channel.
103     */
104    if (util_format_is_alpha(vk_format_to_pipe_format(dst_format))) {
105       count = 1;
106    } else if (dst_desc->nr_channels <= src_desc->nr_channels) {
107       for (uint32_t i = 0; i < dst_desc->nr_channels; i++) {
108          enum pipe_swizzle swizzle = dst_desc->swizzle[i];
109 
110          if (swizzle > PIPE_SWIZZLE_W)
111             continue;
112 
113          for (uint32_t j = 0; j < src_desc->nr_channels; j++) {
114             if (src_desc->swizzle[j] == swizzle) {
115                count++;
116                break;
117             }
118          }
119       }
120    } else {
121       count = dst_desc->nr_channels;
122    }
123 
124    return count;
125 }
126 
vk_format_is_alpha(VkFormat format)127 static inline bool vk_format_is_alpha(VkFormat format)
128 {
129    return util_format_is_alpha(vk_format_to_pipe_format(format));
130 }
131 
132 #endif /* VK_FORMAT_H */
133