1 /*
2 * Copyright © 2022 Collabora, LTD
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 #include "vk_sampler.h"
25
26 #include "vk_format.h"
27 #include "vk_util.h"
28 #include "vk_ycbcr_conversion.h"
29
30 VkClearColorValue
vk_border_color_value(VkBorderColor color)31 vk_border_color_value(VkBorderColor color)
32 {
33 switch (color) {
34 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
35 return (VkClearColorValue) { .float32 = { 0, 0, 0, 0 } };
36 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
37 return (VkClearColorValue) { .int32 = { 0, 0, 0, 0 } };
38 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
39 return (VkClearColorValue) { .float32 = { 0, 0, 0, 1 } };
40 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
41 return (VkClearColorValue) { .int32 = { 0, 0, 0, 1 } };
42 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
43 return (VkClearColorValue) { .float32 = { 1, 1, 1, 1 } };
44 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
45 return (VkClearColorValue) { .int32 = { 1, 1, 1, 1 } };
46 default:
47 unreachable("Invalid or custom border color enum");
48 }
49 }
50
51 bool
vk_border_color_is_int(VkBorderColor color)52 vk_border_color_is_int(VkBorderColor color)
53 {
54 switch (color) {
55 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
56 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
57 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
58 case VK_BORDER_COLOR_FLOAT_CUSTOM_EXT:
59 return false;
60 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
61 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
62 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
63 case VK_BORDER_COLOR_INT_CUSTOM_EXT:
64 return true;
65 default:
66 unreachable("Invalid border color enum");
67 }
68 }
69
70 VkClearColorValue
vk_sampler_border_color_value(const VkSamplerCreateInfo * pCreateInfo,VkFormat * format_out)71 vk_sampler_border_color_value(const VkSamplerCreateInfo *pCreateInfo,
72 VkFormat *format_out)
73 {
74 if (vk_border_color_is_custom(pCreateInfo->borderColor)) {
75 const VkSamplerCustomBorderColorCreateInfoEXT *border_color_info =
76 vk_find_struct_const(pCreateInfo->pNext,
77 SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT);
78 if (format_out)
79 *format_out = border_color_info->format;
80
81 return border_color_info->customBorderColor;
82 } else {
83 if (format_out)
84 *format_out = VK_FORMAT_UNDEFINED;
85
86 return vk_border_color_value(pCreateInfo->borderColor);
87 }
88 }
89
90 void
vk_sampler_init(const VkSamplerCreateInfo * pCreateInfo,struct vk_sampler * sampler)91 vk_sampler_init(const VkSamplerCreateInfo *pCreateInfo, struct vk_sampler *sampler)
92 {
93 sampler->format = VK_FORMAT_UNDEFINED;
94 sampler->border_color = pCreateInfo->borderColor;
95 sampler->reduction_mode = VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE;
96
97 if (!vk_border_color_is_custom(pCreateInfo->borderColor)) {
98 sampler->border_color_value =
99 vk_border_color_value(pCreateInfo->borderColor);
100 }
101
102 vk_foreach_struct_const(ext, pCreateInfo->pNext) {
103 switch (ext->sType) {
104 case VK_STRUCTURE_TYPE_SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT: {
105 const VkSamplerCustomBorderColorCreateInfoEXT *cbc_info = (void *)ext;
106 if (!vk_border_color_is_custom(pCreateInfo->borderColor))
107 break;
108
109 sampler->border_color_value = cbc_info->customBorderColor;
110 if (cbc_info->format != VK_FORMAT_UNDEFINED)
111 sampler->format = cbc_info->format;
112 break;
113 }
114
115 case VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO: {
116 const VkSamplerReductionModeCreateInfo *rm_info = (void *)ext;
117 sampler->reduction_mode = rm_info->reductionMode;
118 break;
119 }
120
121 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO: {
122 const VkSamplerYcbcrConversionInfo *yc_info = (void *)ext;
123 VK_FROM_HANDLE(vk_ycbcr_conversion, conversion, yc_info->conversion);
124
125 /* From the Vulkan 1.2.259 spec:
126 *
127 * "A VkSamplerYcbcrConversionInfo must be provided for samplers
128 * to be used with image views that access
129 * VK_IMAGE_ASPECT_COLOR_BIT if the format is one of the formats
130 * that require a sampler YCbCr conversion, or if the image view
131 * has an external format."
132 *
133 * This means that on Android we can end up with one of these even if
134 * YCbCr isn't being used at all. Leave sampler->ycbcr_conversion NULL
135 * if it isn't a YCbCr format.
136 */
137 if (vk_format_get_ycbcr_info(conversion->state.format) == NULL)
138 break;
139
140 sampler->ycbcr_conversion = conversion;
141 sampler->format = conversion->state.format;
142 break;
143 }
144 default:
145 break;
146 }
147 }
148 }
149
150 void *
vk_sampler_create(struct vk_device * device,const VkSamplerCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,size_t size)151 vk_sampler_create(struct vk_device *device,
152 const VkSamplerCreateInfo *pCreateInfo,
153 const VkAllocationCallbacks *alloc,
154 size_t size)
155 {
156 struct vk_sampler *sampler;
157
158 sampler = vk_object_zalloc(device, alloc, size, VK_OBJECT_TYPE_SAMPLER);
159 if (!sampler)
160 return NULL;
161
162 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
163 vk_sampler_init(pCreateInfo, sampler);
164
165 return sampler;
166 }
167
168 void
vk_sampler_destroy(struct vk_device * device,const VkAllocationCallbacks * alloc,struct vk_sampler * sampler)169 vk_sampler_destroy(struct vk_device *device,
170 const VkAllocationCallbacks *alloc,
171 struct vk_sampler *sampler)
172 {
173 vk_object_free(device, alloc, sampler);
174 }
175