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_create(struct vk_device * device,const VkSamplerCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,size_t size)91 vk_sampler_create(struct vk_device *device,
92                   const VkSamplerCreateInfo *pCreateInfo,
93                   const VkAllocationCallbacks *alloc,
94                   size_t size)
95 {
96    struct vk_sampler *sampler;
97 
98    sampler = vk_object_zalloc(device, alloc, size, VK_OBJECT_TYPE_SAMPLER);
99    if (!sampler)
100       return NULL;
101 
102    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
103 
104    sampler->format = VK_FORMAT_UNDEFINED;
105    sampler->border_color = pCreateInfo->borderColor;
106    sampler->reduction_mode = VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE;
107 
108    if (!vk_border_color_is_custom(pCreateInfo->borderColor)) {
109       sampler->border_color_value =
110          vk_border_color_value(pCreateInfo->borderColor);
111    }
112 
113    vk_foreach_struct_const(ext, pCreateInfo->pNext) {
114       switch (ext->sType) {
115       case VK_STRUCTURE_TYPE_SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT: {
116          const VkSamplerCustomBorderColorCreateInfoEXT *cbc_info = (void *)ext;
117          if (!vk_border_color_is_custom(pCreateInfo->borderColor))
118             break;
119 
120          sampler->border_color_value = cbc_info->customBorderColor;
121          if (cbc_info->format != VK_FORMAT_UNDEFINED)
122             sampler->format = cbc_info->format;
123          break;
124       }
125 
126       case VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO: {
127          const VkSamplerReductionModeCreateInfo *rm_info = (void *)ext;
128          sampler->reduction_mode = rm_info->reductionMode;
129          break;
130       }
131 
132       case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO: {
133          const VkSamplerYcbcrConversionInfo *yc_info = (void *)ext;
134          VK_FROM_HANDLE(vk_ycbcr_conversion, conversion, yc_info->conversion);
135 
136          /* From the Vulkan 1.2.259 spec:
137           *
138           *    "A VkSamplerYcbcrConversionInfo must be provided for samplers
139           *    to be used with image views that access
140           *    VK_IMAGE_ASPECT_COLOR_BIT if the format is one of the formats
141           *    that require a sampler YCbCr conversion, or if the image view
142           *    has an external format."
143           *
144           * This means that on Android we can end up with one of these even if
145           * YCbCr isn't being used at all. Leave sampler->ycbcr_conversion NULL
146           * if it isn't a YCbCr format.
147           */
148          if (vk_format_get_ycbcr_info(conversion->state.format) == NULL)
149             break;
150 
151          sampler->ycbcr_conversion = conversion;
152          sampler->format = conversion->state.format;
153          break;
154       }
155       default:
156          break;
157       }
158    }
159 
160    return sampler;
161 }
162 
163 void
vk_sampler_destroy(struct vk_device * device,const VkAllocationCallbacks * alloc,struct vk_sampler * sampler)164 vk_sampler_destroy(struct vk_device *device,
165                    const VkAllocationCallbacks *alloc,
166                    struct vk_sampler *sampler)
167 {
168    vk_object_free(device, alloc, sampler);
169 }
170