1 /*
2 * Copyright © 2016 Intel Corporation
3 * Copyright © 2019 Google LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef U_FORMAT_VK_H
26 #define U_FORMAT_VK_H
27
28 #include <vulkan/vulkan_core.h>
29 #include "util/format/u_format.h"
30 #include "util/u_math.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 enum pipe_format
37 vk_format_to_pipe_format(enum VkFormat vkformat);
38
39 VkImageAspectFlags
40 vk_format_aspects(VkFormat format);
41
42 static inline bool
vk_format_is_color(VkFormat format)43 vk_format_is_color(VkFormat format)
44 {
45 return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
46 }
47
48 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)49 vk_format_is_depth_or_stencil(VkFormat format)
50 {
51 const VkImageAspectFlags aspects = vk_format_aspects(format);
52 return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
53 }
54
55 static inline bool
vk_format_has_depth(VkFormat format)56 vk_format_has_depth(VkFormat format)
57 {
58 const VkImageAspectFlags aspects = vk_format_aspects(format);
59 return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
60 }
61
62 static inline bool
vk_format_has_stencil(VkFormat format)63 vk_format_has_stencil(VkFormat format)
64 {
65 const VkImageAspectFlags aspects = vk_format_aspects(format);
66 return aspects & VK_IMAGE_ASPECT_STENCIL_BIT;
67 }
68
69 static inline VkFormat
vk_format_depth_only(VkFormat format)70 vk_format_depth_only(VkFormat format)
71 {
72 assert(vk_format_has_depth(format));
73 switch (format) {
74 case VK_FORMAT_D16_UNORM_S8_UINT:
75 return VK_FORMAT_D16_UNORM;
76 case VK_FORMAT_D24_UNORM_S8_UINT:
77 return VK_FORMAT_X8_D24_UNORM_PACK32;
78 case VK_FORMAT_D32_SFLOAT_S8_UINT:
79 return VK_FORMAT_D32_SFLOAT;
80 default:
81 return format;
82 }
83 }
84
85 static inline VkFormat
vk_format_stencil_only(VkFormat format)86 vk_format_stencil_only(VkFormat format)
87 {
88 assert(vk_format_has_stencil(format));
89 return VK_FORMAT_S8_UINT;
90 }
91
92 void vk_component_mapping_to_pipe_swizzle(VkComponentMapping mapping,
93 unsigned char out_swizzle[4]);
94
95 static inline bool
vk_format_is_int(VkFormat format)96 vk_format_is_int(VkFormat format)
97 {
98 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
99 }
100
101 static inline bool
vk_format_is_sint(VkFormat format)102 vk_format_is_sint(VkFormat format)
103 {
104 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
105 }
106
107 static inline bool
vk_format_is_uint(VkFormat format)108 vk_format_is_uint(VkFormat format)
109 {
110 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
111 }
112
113 static inline bool
vk_format_is_unorm(VkFormat format)114 vk_format_is_unorm(VkFormat format)
115 {
116 return util_format_is_unorm(vk_format_to_pipe_format(format));
117 }
118
119 static inline bool
vk_format_is_snorm(VkFormat format)120 vk_format_is_snorm(VkFormat format)
121 {
122 return util_format_is_snorm(vk_format_to_pipe_format(format));
123 }
124
125 static inline bool
vk_format_is_float(VkFormat format)126 vk_format_is_float(VkFormat format)
127 {
128 return util_format_is_float(vk_format_to_pipe_format(format));
129 }
130
131 static inline bool
vk_format_is_srgb(VkFormat format)132 vk_format_is_srgb(VkFormat format)
133 {
134 return util_format_is_srgb(vk_format_to_pipe_format(format));
135 }
136
137 static inline unsigned
vk_format_get_blocksize(VkFormat format)138 vk_format_get_blocksize(VkFormat format)
139 {
140 return util_format_get_blocksize(vk_format_to_pipe_format(format));
141 }
142
143 static inline unsigned
vk_format_get_blockwidth(VkFormat format)144 vk_format_get_blockwidth(VkFormat format)
145 {
146 return util_format_get_blockwidth(vk_format_to_pipe_format(format));
147 }
148
149 static inline unsigned
vk_format_get_blockheight(VkFormat format)150 vk_format_get_blockheight(VkFormat format)
151 {
152 return util_format_get_blockheight(vk_format_to_pipe_format(format));
153 }
154
155 static inline bool
vk_format_is_compressed(VkFormat format)156 vk_format_is_compressed(VkFormat format)
157 {
158 /* this includes 4:2:2 formats, which are compressed formats for vulkan */
159 return vk_format_get_blockwidth(format) > 1;
160 }
161
162 static inline const struct util_format_description *
vk_format_description(VkFormat format)163 vk_format_description(VkFormat format)
164 {
165 return util_format_description(vk_format_to_pipe_format(format));
166 }
167
168 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)169 vk_format_get_component_bits(VkFormat format, enum util_format_colorspace colorspace,
170 unsigned component)
171 {
172 return util_format_get_component_bits(vk_format_to_pipe_format(format),
173 colorspace,
174 component);
175 }
176
177 static inline unsigned
vk_format_get_nr_components(VkFormat format)178 vk_format_get_nr_components(VkFormat format)
179 {
180 return util_format_get_nr_components(vk_format_to_pipe_format(format));
181 }
182
183 static inline bool
vk_format_has_alpha(VkFormat format)184 vk_format_has_alpha(VkFormat format)
185 {
186 return util_format_has_alpha(vk_format_to_pipe_format(format));
187 }
188
189 static inline unsigned
vk_format_get_blocksizebits(VkFormat format)190 vk_format_get_blocksizebits(VkFormat format)
191 {
192 return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
193 }
194
195 static inline unsigned
vk_format_get_plane_count(VkFormat format)196 vk_format_get_plane_count(VkFormat format)
197 {
198 return util_format_get_num_planes(vk_format_to_pipe_format(format));
199 }
200
201 VkFormat
202 vk_format_get_plane_format(VkFormat format, unsigned plane_id);
203
204 VkFormat
205 vk_format_get_aspect_format(VkFormat format, const VkImageAspectFlags aspect);
206
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif
212