| 1 |
|
| 2 |
. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "kk_image_view.h" |
| 9 |
|
| 10 |
#include "kk_device.h" |
| 11 |
#include "kk_entrypoints.h" |
| 12 |
#include "kk_format.h" |
| 13 |
#include "kk_image.h" |
| 14 |
#include "kk_physical_device.h" |
| 15 |
|
| 16 |
#include "kosmickrisp/bridge/mtl_bridge.h" |
| 17 |
#include "kosmickrisp/bridge/mtl_format.h" |
| 18 |
|
| 19 |
#include "vk_format.h" |
| 20 |
|
| 21 |
static enum pipe_swizzle |
| 22 |
vk_swizzle_to_pipe(VkComponentSwizzle swizzle) |
| 23 |
{ |
| 24 |
switch (swizzle) { |
| 25 |
case VK_COMPONENT_SWIZZLE_R: |
| 26 |
return PIPE_SWIZZLE_X; |
| 27 |
case VK_COMPONENT_SWIZZLE_G: |
| 28 |
return PIPE_SWIZZLE_Y; |
| 29 |
case VK_COMPONENT_SWIZZLE_B: |
| 30 |
return PIPE_SWIZZLE_Z; |
| 31 |
case VK_COMPONENT_SWIZZLE_A: |
| 32 |
return PIPE_SWIZZLE_W; |
| 33 |
case VK_COMPONENT_SWIZZLE_ONE: |
| 34 |
return PIPE_SWIZZLE_1; |
| 35 |
case VK_COMPONENT_SWIZZLE_ZERO: |
| 36 |
return PIPE_SWIZZLE_0; |
| 37 |
default: |
| 38 |
UNREACHABLE("Invalid component swizzle"); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static enum VkImageViewType |
| 43 |
remove_1d_view_types(enum VkImageViewType type) |
| 44 |
{ |
| 45 |
if (type == VK_IMAGE_VIEW_TYPE_1D) |
| 46 |
return VK_IMAGE_VIEW_TYPE_2D; |
| 47 |
if (type == VK_IMAGE_VIEW_TYPE_1D_ARRAY) |
| 48 |
return VK_IMAGE_VIEW_TYPE_2D_ARRAY; |
| 49 |
return type; |
| 50 |
} |
| 51 |
|
| 52 |
VkResult |
| 53 |
kk_image_view_init(struct kk_device *dev, struct kk_image_view *view, |
| 54 |
const VkImageViewCreateInfo *pCreateInfo) |
| 55 |
{ |
| 56 |
VK_FROM_HANDLE(kk_image, image, pCreateInfo->image); |
| 57 |
|
| 58 |
memset(view, 0, sizeof(*view)); |
| 59 |
|
| 60 |
vk_image_view_init(&dev->vk, &view->vk, pCreateInfo); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if (image->vk.aspects & |
| 67 |
(VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) { |
| 68 |
assert(image->plane_count == 1); |
| 69 |
assert(kk_image_aspects_to_plane(image, view->vk.aspects) == 0); |
| 70 |
view->plane_count = 1; |
| 71 |
view->planes[0].image_plane = 0; |
| 72 |
} else { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
assert(util_bitcount(view->vk.aspects) == |
| 78 |
vk_format_get_plane_count(view->vk.format)); |
| 79 |
view->plane_count = 0; |
| 80 |
u_foreach_bit(aspect_bit, view->vk.aspects) { |
| 81 |
uint8_t image_plane = |
| 82 |
kk_image_aspects_to_plane(image, 1u << aspect_bit); |
| 83 |
view->planes[view->plane_count++].image_plane = image_plane; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
for (unsigned view_plane = 0; view_plane < view->plane_count; view_plane++) { |
| 88 |
const uint8_t image_plane = view->planes[view_plane].image_plane; |
| 89 |
struct kk_image_plane *plane = &image->planes[image_plane]; |
| 90 |
|
| 91 |
const struct vk_format_ycbcr_info *ycbcr_info = |
| 92 |
vk_format_get_ycbcr_info(view->vk.format); |
| 93 |
assert(ycbcr_info || view_plane == 0); |
| 94 |
VkFormat plane_format = |
| 95 |
ycbcr_info ? ycbcr_info->planes[view_plane].format : view->vk.format; |
| 96 |
enum pipe_format p_format = vk_format_to_pipe_format(plane_format); |
| 97 |
if (view->vk.aspects == VK_IMAGE_ASPECT_DEPTH_BIT) |
| 98 |
p_format = vk_format_to_pipe_format(image->vk.format); |
| 99 |
else if (view->vk.aspects == VK_IMAGE_ASPECT_STENCIL_BIT) |
| 100 |
p_format = util_format_stencil_only( |
| 101 |
vk_format_to_pipe_format(image->vk.format)); |
| 102 |
|
| 103 |
view->planes[view_plane].format = p_format; |
| 104 |
const struct kk_va_format *supported_format = kk_get_va_format(p_format); |
| 105 |
assert(supported_format); |
| 106 |
|
| 107 |
struct kk_view_layout view_layout = { |
| 108 |
.view_type = remove_1d_view_types(view->vk.view_type), |
| 109 |
.sample_count_sa = plane->layout.sample_count_sa, |
| 110 |
.format = {.pipe = p_format, |
| 111 |
.mtl = supported_format->mtl_pixel_format}, |
| 112 |
.base_level = view->vk.base_mip_level, |
| 113 |
.num_levels = view->vk.level_count, |
| 114 |
.base_array_layer = view->vk.base_array_layer, |
| 115 |
.array_len = view->vk.layer_count, |
| 116 |
.min_lod_clamp = view->vk.min_lod, |
| 117 |
}; |
| 118 |
uint8_t view_swizzle[4] = {vk_swizzle_to_pipe(view->vk.swizzle.r), |
| 119 |
vk_swizzle_to_pipe(view->vk.swizzle.g), |
| 120 |
vk_swizzle_to_pipe(view->vk.swizzle.b), |
| 121 |
vk_swizzle_to_pipe(view->vk.swizzle.a)}; |
| 122 |
util_format_compose_swizzles(supported_format->unswizzle.channels, |
| 123 |
view_swizzle, view_layout.swizzle.channels); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if (util_format_is_depth_or_stencil(p_format)) { |
| 129 |
if (view_layout.swizzle.red == PIPE_SWIZZLE_Y || |
| 130 |
view_layout.swizzle.red == PIPE_SWIZZLE_Z) |
| 131 |
view_layout.swizzle.red = PIPE_SWIZZLE_0; |
| 132 |
if (view_layout.swizzle.green == PIPE_SWIZZLE_Y || |
| 133 |
view_layout.swizzle.green == PIPE_SWIZZLE_Z) |
| 134 |
view_layout.swizzle.green = PIPE_SWIZZLE_0; |
| 135 |
if (view_layout.swizzle.blue == PIPE_SWIZZLE_Y || |
| 136 |
view_layout.swizzle.blue == PIPE_SWIZZLE_Z) |
| 137 |
view_layout.swizzle.blue = PIPE_SWIZZLE_0; |
| 138 |
if (view_layout.swizzle.alpha == PIPE_SWIZZLE_Y || |
| 139 |
view_layout.swizzle.alpha == PIPE_SWIZZLE_Z) |
| 140 |
view_layout.swizzle.alpha = PIPE_SWIZZLE_0; |
| 141 |
} |
| 142 |
|
| 143 |
mtl_texture *mtl_handle = image->planes[image_plane].mtl_handle; |
| 144 |
if (image->vk.image_type == VK_IMAGE_TYPE_3D && |
| 145 |
view->vk.view_type != VK_IMAGE_VIEW_TYPE_3D) |
| 146 |
mtl_handle = image->planes[image_plane].mtl_handle_array; |
| 147 |
|
| 148 |
if (view->vk.usage & |
| 149 |
(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) { |
| 150 |
view->planes[view_plane].mtl_handle_sampled = |
| 151 |
mtl_new_texture_view_with(mtl_handle, &view_layout); |
| 152 |
view->planes[view_plane].sampled_gpu_resource_id = |
| 153 |
mtl_texture_get_gpu_resource_id( |
| 154 |
view->planes[view_plane].mtl_handle_sampled); |
| 155 |
} |
| 156 |
|
| 157 |
if (view->vk.usage & VK_IMAGE_USAGE_STORAGE_BIT) { |
| 158 |
|
| 159 |
if (view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE || |
| 160 |
view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) |
| 161 |
view_layout.view_type = VK_IMAGE_VIEW_TYPE_2D_ARRAY; |
| 162 |
|
| 163 |
view->planes[view_plane].mtl_handle_storage = |
| 164 |
mtl_new_texture_view_with(mtl_handle, &view_layout); |
| 165 |
view->planes[view_plane].storage_gpu_resource_id = |
| 166 |
mtl_texture_get_gpu_resource_id( |
| 167 |
view->planes[view_plane].mtl_handle_storage); |
| 168 |
} |
| 169 |
|
| 170 |
if (view->vk.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 171 |
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) { |
| 172 |
bool requires_type_change = |
| 173 |
view_layout.view_type != VK_IMAGE_VIEW_TYPE_3D && |
| 174 |
view_layout.view_type != VK_IMAGE_VIEW_TYPE_2D_ARRAY; |
| 175 |
bool requires_format_change = view->vk.format != image->vk.format; |
| 176 |
VkImageViewType original_type = view_layout.view_type; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if (requires_type_change || requires_format_change) { |
| 183 |
view_layout.view_type = requires_type_change |
| 184 |
? VK_IMAGE_VIEW_TYPE_2D_ARRAY |
| 185 |
: original_type; |
| 186 |
view->planes[view_plane].mtl_handle_input = |
| 187 |
mtl_new_texture_view_with(mtl_handle, &view_layout); |
| 188 |
} else |
| 189 |
view->planes[view_plane].mtl_handle_input = mtl_retain(mtl_handle); |
| 190 |
view->planes[view_plane].input_gpu_resource_id = |
| 191 |
mtl_texture_get_gpu_resource_id( |
| 192 |
view->planes[view_plane].mtl_handle_input); |
| 193 |
|
| 194 |
|
| 195 |
if (requires_format_change) { |
| 196 |
view_layout.view_type = original_type; |
| 197 |
view_layout.base_array_layer = 0u; |
| 198 |
view_layout.base_level = 0u; |
| 199 |
view_layout.array_len = image->vk.array_layers; |
| 200 |
view_layout.num_levels = image->vk.mip_levels; |
| 201 |
view->planes[view_plane].mtl_handle_render = |
| 202 |
mtl_new_texture_view_with_no_swizzle(mtl_handle, &view_layout); |
| 203 |
} else |
| 204 |
view->planes[view_plane].mtl_handle_render = mtl_retain(mtl_handle); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return VK_SUCCESS; |
| 209 |
} |
| 210 |
|
| 211 |
void |
| 212 |
kk_image_view_finish(struct kk_device *dev, struct kk_image_view *view) |
| 213 |
{ |
| 214 |
for (uint8_t plane = 0; plane < view->plane_count; plane++) { |
| 215 |
if (view->planes[plane].mtl_handle_sampled) |
| 216 |
mtl_release(view->planes[plane].mtl_handle_sampled); |
| 217 |
|
| 218 |
if (view->planes[plane].mtl_handle_storage) |
| 219 |
mtl_release(view->planes[plane].mtl_handle_storage); |
| 220 |
|
| 221 |
if (view->planes[plane].mtl_handle_input) |
| 222 |
mtl_release(view->planes[plane].mtl_handle_input); |
| 223 |
|
| 224 |
if (view->planes[plane].mtl_handle_render) |
| 225 |
mtl_release(view->planes[plane].mtl_handle_render); |
| 226 |
} |
| 227 |
|
| 228 |
vk_image_view_finish(&view->vk); |
| 229 |
} |
| 230 |
|
| 231 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 232 |
kk_CreateImageView(VkDevice _device, const VkImageViewCreateInfo *pCreateInfo, |
| 233 |
const VkAllocationCallbacks *pAllocator, VkImageView *pView) |
| 234 |
{ |
| 235 |
VK_FROM_HANDLE(kk_device, dev, _device); |
| 236 |
struct kk_image_view *view; |
| 237 |
VkResult result; |
| 238 |
|
| 239 |
view = vk_alloc2(&dev->vk.alloc, pAllocator, sizeof(*view), 8, |
| 240 |
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 241 |
if (!view) |
| 242 |
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 243 |
|
| 244 |
result = kk_image_view_init(dev, view, pCreateInfo); |
| 245 |
if (result != VK_SUCCESS) { |
| 246 |
vk_free2(&dev->vk.alloc, pAllocator, view); |
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
*pView = kk_image_view_to_handle(view); |
| 251 |
|
| 252 |
return VK_SUCCESS; |
| 253 |
} |
| 254 |
|
| 255 |
VKAPI_ATTR void VKAPI_CALL |
| 256 |
kk_DestroyImageView(VkDevice _device, VkImageView imageView, |
| 257 |
const VkAllocationCallbacks *pAllocator) |
| 258 |
{ |
| 259 |
VK_FROM_HANDLE(kk_device, dev, _device); |
| 260 |
VK_FROM_HANDLE(kk_image_view, view, imageView); |
| 261 |
|
| 262 |
if (!view) |
| 263 |
return; |
| 264 |
|
| 265 |
kk_image_view_finish(dev, view); |
| 266 |
vk_free2(&dev->vk.alloc, pAllocator, view); |
| 267 |
} |