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 *
8 * SPDX-License-Identifier: MIT
9 */
10
11 #ifndef RADV_FORMATS_H
12 #define RADV_FORMATS_H
13
14 #include <assert.h>
15 #include <util/macros.h>
16 #include <vulkan/vulkan.h>
17
18 #include "amd_family.h"
19
20 #include "vk_format.h"
21
22 static inline enum pipe_format
radv_format_to_pipe_format(VkFormat vkformat)23 radv_format_to_pipe_format(VkFormat vkformat)
24 {
25 switch (vkformat) {
26 case VK_FORMAT_R10X6_UNORM_PACK16:
27 case VK_FORMAT_R12X4_UNORM_PACK16:
28 return PIPE_FORMAT_R16_UNORM;
29 case VK_FORMAT_R10X6G10X6_UNORM_2PACK16:
30 case VK_FORMAT_R12X4G12X4_UNORM_2PACK16:
31 return PIPE_FORMAT_R16G16_UNORM;
32 default:
33 return vk_format_to_pipe_format(vkformat);
34 }
35 }
36
37 /**
38 * Return the index of the first non-void channel
39 * -1 if no non-void channels
40 */
41 static inline int
vk_format_get_first_non_void_channel(VkFormat format)42 vk_format_get_first_non_void_channel(VkFormat format)
43 {
44 return util_format_get_first_non_void_channel(radv_format_to_pipe_format(format));
45 }
46
47 static inline enum pipe_swizzle
radv_swizzle_conv(VkComponentSwizzle component,const unsigned char chan[4],VkComponentSwizzle vk_swiz)48 radv_swizzle_conv(VkComponentSwizzle component, const unsigned char chan[4], VkComponentSwizzle vk_swiz)
49 {
50 if (vk_swiz == VK_COMPONENT_SWIZZLE_IDENTITY)
51 vk_swiz = component;
52 switch (vk_swiz) {
53 case VK_COMPONENT_SWIZZLE_ZERO:
54 return PIPE_SWIZZLE_0;
55 case VK_COMPONENT_SWIZZLE_ONE:
56 return PIPE_SWIZZLE_1;
57 case VK_COMPONENT_SWIZZLE_R:
58 case VK_COMPONENT_SWIZZLE_G:
59 case VK_COMPONENT_SWIZZLE_B:
60 case VK_COMPONENT_SWIZZLE_A:
61 return (enum pipe_swizzle)chan[vk_swiz - VK_COMPONENT_SWIZZLE_R];
62 default:
63 unreachable("Illegal swizzle");
64 }
65 }
66
67 static inline void
vk_format_compose_swizzles(const VkComponentMapping * mapping,const unsigned char swz[4],enum pipe_swizzle dst[4])68 vk_format_compose_swizzles(const VkComponentMapping *mapping, const unsigned char swz[4], 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
76 static inline bool
vk_format_is_subsampled(VkFormat format)77 vk_format_is_subsampled(VkFormat format)
78 {
79 return util_format_is_subsampled_422(radv_format_to_pipe_format(format));
80 }
81
82 static inline VkFormat
vk_format_no_srgb(VkFormat format)83 vk_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
122 struct radv_physical_device;
123
124 uint32_t radv_translate_buffer_numformat(const struct util_format_description *desc, int first_non_void);
125
126 uint32_t radv_translate_tex_dataformat(const struct radv_physical_device *pdev,
127 const struct util_format_description *desc, int first_non_void);
128
129 uint32_t radv_translate_tex_numformat(const struct util_format_description *desc, int first_non_void);
130
131 bool radv_is_atomic_format_supported(VkFormat format);
132
133 bool radv_is_storage_image_format_supported(const struct radv_physical_device *dev, VkFormat format);
134
135 bool radv_is_buffer_format_supported(VkFormat format, bool *scaled);
136
137 bool radv_is_colorbuffer_format_supported(const struct radv_physical_device *pdev, VkFormat format);
138
139 bool radv_is_format_emulated(const struct radv_physical_device *pdev, VkFormat format);
140
141 bool radv_format_pack_clear_color(VkFormat format, uint32_t clear_vals[2], VkClearColorValue *value);
142
143 bool radv_dcc_formats_compatible(enum amd_gfx_level gfx_level, VkFormat format1, VkFormat format2,
144 bool *sign_reinterpret);
145
146 #endif /* RADV_FORMATS_H */
147