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_has_alpha(VkFormat format)72 vk_format_has_alpha(VkFormat format)
73 {
74    return util_format_has_alpha(vk_format_to_pipe_format(format));
75 }
76 
77 static inline bool
vk_format_is_int(VkFormat format)78 vk_format_is_int(VkFormat format)
79 {
80    return util_format_is_pure_integer(vk_format_to_pipe_format(format));
81 }
82 
83 static inline bool
vk_format_is_uint(VkFormat format)84 vk_format_is_uint(VkFormat format)
85 {
86    return util_format_is_pure_uint(vk_format_to_pipe_format(format));
87 }
88 
89 static inline bool
vk_format_is_sint(VkFormat format)90 vk_format_is_sint(VkFormat format)
91 {
92    return util_format_is_pure_sint(vk_format_to_pipe_format(format));
93 }
94 
95 static inline bool
vk_format_is_srgb(VkFormat format)96 vk_format_is_srgb(VkFormat format)
97 {
98    return util_format_is_srgb(vk_format_to_pipe_format(format));
99 }
100 
101 static inline bool
vk_format_is_unorm(VkFormat format)102 vk_format_is_unorm(VkFormat format)
103 {
104    return util_format_is_unorm(vk_format_to_pipe_format(format));
105 }
106 
107 static inline bool
vk_format_is_snorm(VkFormat format)108 vk_format_is_snorm(VkFormat format)
109 {
110    return util_format_is_snorm(vk_format_to_pipe_format(format));
111 }
112 
113 static inline bool
vk_format_is_float(VkFormat format)114 vk_format_is_float(VkFormat format)
115 {
116    return util_format_is_float(vk_format_to_pipe_format(format));
117 }
118 
119 static inline unsigned
vk_format_get_component_bits(VkFormat format,enum util_format_colorspace colorspace,unsigned component)120 vk_format_get_component_bits(VkFormat format,
121                              enum util_format_colorspace colorspace,
122                              unsigned component)
123 {
124    switch (format) {
125    case VK_FORMAT_G8B8G8R8_422_UNORM:
126    case VK_FORMAT_B8G8R8G8_422_UNORM:
127    case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
128    case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
129       /* util_format_get_component_bits doesn't return what we want */
130       return 8;
131    default:
132       break;
133    }
134 
135    return util_format_get_component_bits(vk_format_to_pipe_format(format),
136                                          colorspace, component);
137 }
138 
139 static inline unsigned
vk_format_get_nr_components(VkFormat format)140 vk_format_get_nr_components(VkFormat format)
141 {
142    return util_format_get_nr_components(vk_format_to_pipe_format(format));
143 }
144 
145 #endif /* VK_FORMAT_H */
146