xref: /third_party/mesa3d/src/vulkan/util/vk_format.h (revision bf215546)
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
33extern "C" {
34#endif
35
36enum pipe_format
37vk_format_to_pipe_format(enum VkFormat vkformat);
38
39VkImageAspectFlags
40vk_format_aspects(VkFormat format);
41
42static inline bool
43vk_format_is_color(VkFormat format)
44{
45   return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
46}
47
48static inline bool
49vk_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
55static inline bool
56vk_format_has_depth(VkFormat format)
57{
58   const VkImageAspectFlags aspects = vk_format_aspects(format);
59   return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
60}
61
62static inline bool
63vk_format_has_stencil(VkFormat format)
64{
65   const VkImageAspectFlags aspects = vk_format_aspects(format);
66   return aspects & VK_IMAGE_ASPECT_STENCIL_BIT;
67}
68
69static inline VkFormat
70vk_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
85static inline VkFormat
86vk_format_stencil_only(VkFormat format)
87{
88   assert(vk_format_has_stencil(format));
89   return VK_FORMAT_S8_UINT;
90}
91
92void vk_component_mapping_to_pipe_swizzle(VkComponentMapping mapping,
93                                          unsigned char out_swizzle[4]);
94
95static inline bool
96vk_format_is_int(VkFormat format)
97{
98   return util_format_is_pure_integer(vk_format_to_pipe_format(format));
99}
100
101static inline bool
102vk_format_is_sint(VkFormat format)
103{
104   return util_format_is_pure_sint(vk_format_to_pipe_format(format));
105}
106
107static inline bool
108vk_format_is_uint(VkFormat format)
109{
110   return util_format_is_pure_uint(vk_format_to_pipe_format(format));
111}
112
113static inline bool
114vk_format_is_unorm(VkFormat format)
115{
116   return util_format_is_unorm(vk_format_to_pipe_format(format));
117}
118
119static inline bool
120vk_format_is_snorm(VkFormat format)
121{
122   return util_format_is_snorm(vk_format_to_pipe_format(format));
123}
124
125static inline bool
126vk_format_is_float(VkFormat format)
127{
128   return util_format_is_float(vk_format_to_pipe_format(format));
129}
130
131static inline bool
132vk_format_is_srgb(VkFormat format)
133{
134   return util_format_is_srgb(vk_format_to_pipe_format(format));
135}
136
137static inline unsigned
138vk_format_get_blocksize(VkFormat format)
139{
140   return util_format_get_blocksize(vk_format_to_pipe_format(format));
141}
142
143static inline unsigned
144vk_format_get_blockwidth(VkFormat format)
145{
146   return util_format_get_blockwidth(vk_format_to_pipe_format(format));
147}
148
149static inline unsigned
150vk_format_get_blockheight(VkFormat format)
151{
152   return util_format_get_blockheight(vk_format_to_pipe_format(format));
153}
154
155static inline bool
156vk_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
162static inline const struct util_format_description *
163vk_format_description(VkFormat format)
164{
165   return util_format_description(vk_format_to_pipe_format(format));
166}
167
168static inline unsigned
169vk_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
177static inline unsigned
178vk_format_get_nr_components(VkFormat format)
179{
180   return util_format_get_nr_components(vk_format_to_pipe_format(format));
181}
182
183static inline bool
184vk_format_has_alpha(VkFormat format)
185{
186   return util_format_has_alpha(vk_format_to_pipe_format(format));
187}
188
189static inline unsigned
190vk_format_get_blocksizebits(VkFormat format)
191{
192   return util_format_get_blocksizebits(vk_format_to_pipe_format(format));
193}
194
195static inline unsigned
196vk_format_get_plane_count(VkFormat format)
197{
198   return util_format_get_num_planes(vk_format_to_pipe_format(format));
199}
200
201VkFormat
202vk_format_get_plane_format(VkFormat format, unsigned plane_id);
203
204VkFormat
205vk_format_get_aspect_format(VkFormat format, const VkImageAspectFlags aspect);
206
207#ifdef __cplusplus
208}
209#endif
210
211#endif
212