1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * Based on u_format.h which is:
6 * Copyright 2009-2010 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #ifndef VK_FORMAT_H
28 #define VK_FORMAT_H
29
30 #include <assert.h>
31 #include <util/macros.h>
32 #include <util/format/u_format.h>
33 #include <vulkan/util/vk_format.h>
34
35 #include <vulkan/vulkan.h>
36
37 static inline const struct util_format_description *
vk_format_description(VkFormat format)38 vk_format_description(VkFormat format)
39 {
40 return util_format_description(vk_format_to_pipe_format(format));
41 }
42
43 /**
44 * Return bytes per block (not pixel) for the given format.
45 */
46 static inline unsigned
vk_format_get_blocksize(VkFormat format)47 vk_format_get_blocksize(VkFormat format)
48 {
49 return util_format_get_blocksize(vk_format_to_pipe_format(format));
50 }
51
52 static inline unsigned
vk_format_get_blockwidth(VkFormat format)53 vk_format_get_blockwidth(VkFormat format)
54 {
55 return util_format_get_blockwidth(vk_format_to_pipe_format(format));
56 }
57
58 static inline unsigned
vk_format_get_blockheight(VkFormat format)59 vk_format_get_blockheight(VkFormat format)
60 {
61 return util_format_get_blockheight(vk_format_to_pipe_format(format));
62 }
63
64 static inline bool
vk_format_is_compressed(VkFormat format)65 vk_format_is_compressed(VkFormat format)
66 {
67 /* this includes 4:2:2 formats, which are compressed formats for vulkan */
68 return vk_format_get_blockwidth(format) > 1;
69 }
70
71 static inline bool
vk_format_is_depth_or_stencil(VkFormat format)72 vk_format_is_depth_or_stencil(VkFormat format)
73 {
74 const struct util_format_description *desc = vk_format_description(format);
75
76 return util_format_has_depth(desc) || util_format_has_stencil(desc);
77 }
78
79 static inline bool
vk_format_has_alpha(VkFormat format)80 vk_format_has_alpha(VkFormat format)
81 {
82 return util_format_has_alpha(vk_format_to_pipe_format(format));
83 }
84
85 static inline bool
vk_format_is_int(VkFormat format)86 vk_format_is_int(VkFormat format)
87 {
88 return util_format_is_pure_integer(vk_format_to_pipe_format(format));
89 }
90
91 static inline bool
vk_format_is_uint(VkFormat format)92 vk_format_is_uint(VkFormat format)
93 {
94 return util_format_is_pure_uint(vk_format_to_pipe_format(format));
95 }
96
97 static inline bool
vk_format_is_sint(VkFormat format)98 vk_format_is_sint(VkFormat format)
99 {
100 return util_format_is_pure_sint(vk_format_to_pipe_format(format));
101 }
102
103 static inline bool
vk_format_is_srgb(VkFormat format)104 vk_format_is_srgb(VkFormat format)
105 {
106 return util_format_is_srgb(vk_format_to_pipe_format(format));
107 }
108
109 static inline bool
vk_format_is_unorm(VkFormat format)110 vk_format_is_unorm(VkFormat format)
111 {
112 return util_format_is_unorm(vk_format_to_pipe_format(format));
113 }
114
115 static inline bool
vk_format_is_snorm(VkFormat format)116 vk_format_is_snorm(VkFormat format)
117 {
118 return util_format_is_snorm(vk_format_to_pipe_format(format));
119 }
120
121 static inline bool
vk_format_is_float(VkFormat format)122 vk_format_is_float(VkFormat format)
123 {
124 return util_format_is_float(vk_format_to_pipe_format(format));
125 }
126
127 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)128 vk_format_get_component_bits(VkFormat format,
129 enum util_format_colorspace colorspace,
130 unsigned component)
131 {
132 switch (format) {
133 case VK_FORMAT_G8B8G8R8_422_UNORM:
134 case VK_FORMAT_B8G8R8G8_422_UNORM:
135 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
136 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
137 /* util_format_get_component_bits doesn't return what we want */
138 return 8;
139 default:
140 break;
141 }
142
143 return util_format_get_component_bits(vk_format_to_pipe_format(format),
144 colorspace, component);
145 }
146
147 static inline unsigned
vk_format_get_nr_components(VkFormat format)148 vk_format_get_nr_components(VkFormat format)
149 {
150 return util_format_get_nr_components(vk_format_to_pipe_format(format));
151 }
152
153 #endif /* VK_FORMAT_H */
154