xref: /third_party/mesa3d/src/amd/vulkan/vk_format.h (revision bf215546)
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 DEALINGS
24 * 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 <vulkan/util/vk_format.h>
33#include <vulkan/vulkan.h>
34
35/**
36 * Return the index of the first non-void channel
37 * -1 if no non-void channels
38 */
39static inline int
40vk_format_get_first_non_void_channel(VkFormat format)
41{
42   return util_format_get_first_non_void_channel(vk_format_to_pipe_format(format));
43}
44
45static inline enum pipe_swizzle
46radv_swizzle_conv(VkComponentSwizzle component, const unsigned char chan[4],
47                  VkComponentSwizzle vk_swiz)
48{
49   if (vk_swiz == VK_COMPONENT_SWIZZLE_IDENTITY)
50      vk_swiz = component;
51   switch (vk_swiz) {
52   case VK_COMPONENT_SWIZZLE_ZERO:
53      return PIPE_SWIZZLE_0;
54   case VK_COMPONENT_SWIZZLE_ONE:
55      return PIPE_SWIZZLE_1;
56   case VK_COMPONENT_SWIZZLE_R:
57   case VK_COMPONENT_SWIZZLE_G:
58   case VK_COMPONENT_SWIZZLE_B:
59   case VK_COMPONENT_SWIZZLE_A:
60      return (enum pipe_swizzle)chan[vk_swiz - VK_COMPONENT_SWIZZLE_R];
61   default:
62      unreachable("Illegal swizzle");
63   }
64}
65
66static inline void
67vk_format_compose_swizzles(const VkComponentMapping *mapping, const unsigned char swz[4],
68                           enum pipe_swizzle dst[4])
69{
70   dst[0] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_R, swz, mapping->r);
71   dst[1] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_G, swz, mapping->g);
72   dst[2] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_B, swz, mapping->b);
73   dst[3] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_A, swz, mapping->a);
74}
75
76static inline bool
77vk_format_is_subsampled(VkFormat format)
78{
79   return util_format_is_subsampled_422(vk_format_to_pipe_format(format));
80}
81
82static inline VkFormat
83vk_format_no_srgb(VkFormat format)
84{
85   switch (format) {
86   case VK_FORMAT_R8_SRGB:
87      return VK_FORMAT_R8_UNORM;
88   case VK_FORMAT_R8G8_SRGB:
89      return VK_FORMAT_R8G8_UNORM;
90   case VK_FORMAT_R8G8B8_SRGB:
91      return VK_FORMAT_R8G8B8_UNORM;
92   case VK_FORMAT_B8G8R8_SRGB:
93      return VK_FORMAT_B8G8R8_UNORM;
94   case VK_FORMAT_R8G8B8A8_SRGB:
95      return VK_FORMAT_R8G8B8A8_UNORM;
96   case VK_FORMAT_B8G8R8A8_SRGB:
97      return VK_FORMAT_B8G8R8A8_UNORM;
98   case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
99      return VK_FORMAT_A8B8G8R8_UNORM_PACK32;
100   case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
101      return VK_FORMAT_BC1_RGB_UNORM_BLOCK;
102   case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
103      return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
104   case VK_FORMAT_BC2_SRGB_BLOCK:
105      return VK_FORMAT_BC2_UNORM_BLOCK;
106   case VK_FORMAT_BC3_SRGB_BLOCK:
107      return VK_FORMAT_BC3_UNORM_BLOCK;
108   case VK_FORMAT_BC7_SRGB_BLOCK:
109      return VK_FORMAT_BC7_UNORM_BLOCK;
110   case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
111      return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
112   case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
113      return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
114   case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
115      return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
116   default:
117      assert(!vk_format_is_srgb(format));
118      return format;
119   }
120}
121
122static inline VkFormat
123vk_to_non_srgb_format(VkFormat format)
124{
125   switch (format) {
126   case VK_FORMAT_R8_SRGB:
127      return VK_FORMAT_R8_UNORM;
128   case VK_FORMAT_R8G8_SRGB:
129      return VK_FORMAT_R8G8_UNORM;
130   case VK_FORMAT_R8G8B8_SRGB:
131      return VK_FORMAT_R8G8B8_UNORM;
132   case VK_FORMAT_B8G8R8_SRGB:
133      return VK_FORMAT_B8G8R8_UNORM;
134   case VK_FORMAT_R8G8B8A8_SRGB:
135      return VK_FORMAT_R8G8B8A8_UNORM;
136   case VK_FORMAT_B8G8R8A8_SRGB:
137      return VK_FORMAT_B8G8R8A8_UNORM;
138   case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
139      return VK_FORMAT_A8B8G8R8_UNORM_PACK32;
140   default:
141      return format;
142   }
143}
144
145static inline unsigned
146vk_format_get_plane_width(VkFormat format, unsigned plane, unsigned width)
147{
148   return util_format_get_plane_width(vk_format_to_pipe_format(format), plane, width);
149}
150
151static inline unsigned
152vk_format_get_plane_height(VkFormat format, unsigned plane, unsigned height)
153{
154   return util_format_get_plane_height(vk_format_to_pipe_format(format), plane, height);
155}
156
157#endif /* VK_FORMAT_H */
158