1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef VK_FORMAT_INFO_H
25 #define VK_FORMAT_INFO_H
26
27 #include <stdbool.h>
28 #include <vulkan/vulkan.h>
29
30 #include "util/format/u_format.h"
31 #include "vulkan/util/vk_format.h"
32
33 static inline VkImageAspectFlags
vk_format_aspects(VkFormat format)34 vk_format_aspects(VkFormat format)
35 {
36 switch (format) {
37 case VK_FORMAT_UNDEFINED:
38 return 0;
39
40 case VK_FORMAT_S8_UINT:
41 return VK_IMAGE_ASPECT_STENCIL_BIT;
42
43 case VK_FORMAT_D16_UNORM_S8_UINT:
44 case VK_FORMAT_D24_UNORM_S8_UINT:
45 case VK_FORMAT_D32_SFLOAT_S8_UINT:
46 return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
47
48 case VK_FORMAT_D16_UNORM:
49 case VK_FORMAT_X8_D24_UNORM_PACK32:
50 case VK_FORMAT_D32_SFLOAT:
51 return VK_IMAGE_ASPECT_DEPTH_BIT;
52
53 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
54 case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
55 case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
56 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
57 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
58 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
59 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
60 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
61 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
62 case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
63 case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
64 case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
65 return (VK_IMAGE_ASPECT_PLANE_0_BIT |
66 VK_IMAGE_ASPECT_PLANE_1_BIT |
67 VK_IMAGE_ASPECT_PLANE_2_BIT);
68
69 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
70 case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
71 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
72 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
73 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
74 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
75 case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
76 case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
77 return (VK_IMAGE_ASPECT_PLANE_0_BIT |
78 VK_IMAGE_ASPECT_PLANE_1_BIT);
79
80 default:
81 return VK_IMAGE_ASPECT_COLOR_BIT;
82 }
83 }
84
85 static inline bool
vk_format_is_color(VkFormat format)86 vk_format_is_color(VkFormat format)
87 {
88 return vk_format_aspects(format) == VK_IMAGE_ASPECT_COLOR_BIT;
89 }
90
91 /* FIXME: from freedreno vk_format.h, common place?*/
92 static inline bool
vk_format_is_int(VkFormat format)93 vk_format_is_int(VkFormat format)
94 {
95 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
96 }
97
98 static inline bool
vk_format_is_sint(VkFormat format)99 vk_format_is_sint(VkFormat format)
100 {
101 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
102 }
103
104 static inline bool
vk_format_is_uint(VkFormat format)105 vk_format_is_uint(VkFormat format)
106 {
107 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
108 }
109
110 static inline bool
vk_format_is_srgb(VkFormat format)111 vk_format_is_srgb(VkFormat format)
112 {
113 return util_format_is_srgb(vk_format_to_pipe_format(format));
114 }
115
116 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)117 vk_format_is_depth_or_stencil(VkFormat format)
118 {
119 const VkImageAspectFlags aspects = vk_format_aspects(format);
120 return aspects & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
121 }
122
123 static inline bool
vk_format_has_depth(VkFormat format)124 vk_format_has_depth(VkFormat format)
125 {
126 const VkImageAspectFlags aspects = vk_format_aspects(format);
127 return aspects & VK_IMAGE_ASPECT_DEPTH_BIT;
128 }
129
130 static inline bool
vk_format_has_stencil(VkFormat format)131 vk_format_has_stencil(VkFormat format)
132 {
133 const VkImageAspectFlags aspects = vk_format_aspects(format);
134 return aspects & VK_IMAGE_ASPECT_STENCIL_BIT;
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 return util_format_is_compressed(vk_format_to_pipe_format(format));
159 }
160
161 static inline const struct util_format_description *
vk_format_description(VkFormat format)162 vk_format_description(VkFormat format)
163 {
164 return util_format_description(vk_format_to_pipe_format(format));
165 }
166
167 #endif /* VK_FORMAT_INFO_H */
168