1/* 2 * Copyright 2018 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 * on the rights to use, copy, modify, merge, publish, distribute, sub 8 * license, and/or sell copies of the Software, and to permit persons to whom 9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21 * USE OR OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24#include "zink_context.h" 25#include "zink_framebuffer.h" 26#include "zink_resource.h" 27#include "zink_screen.h" 28#include "zink_surface.h" 29#include "zink_kopper.h" 30 31#include "util/format/u_format.h" 32#include "util/u_inlines.h" 33#include "util/u_memory.h" 34 35VkImageViewCreateInfo 36create_ivci(struct zink_screen *screen, 37 struct zink_resource *res, 38 const struct pipe_surface *templ, 39 enum pipe_texture_target target) 40{ 41 VkImageViewCreateInfo ivci; 42 /* zero holes since this is hashed */ 43 memset(&ivci, 0, sizeof(VkImageViewCreateInfo)); 44 ivci.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; 45 ivci.image = res->obj->image; 46 47 switch (target) { 48 case PIPE_TEXTURE_1D: 49 ivci.viewType = res->need_2D ? VK_IMAGE_VIEW_TYPE_2D : VK_IMAGE_VIEW_TYPE_1D; 50 break; 51 52 case PIPE_TEXTURE_1D_ARRAY: 53 ivci.viewType = res->need_2D ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_1D_ARRAY; 54 break; 55 56 case PIPE_TEXTURE_2D: 57 case PIPE_TEXTURE_RECT: 58 ivci.viewType = VK_IMAGE_VIEW_TYPE_2D; 59 break; 60 61 case PIPE_TEXTURE_2D_ARRAY: 62 ivci.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY; 63 break; 64 65 case PIPE_TEXTURE_CUBE: 66 ivci.viewType = VK_IMAGE_VIEW_TYPE_CUBE; 67 break; 68 69 case PIPE_TEXTURE_CUBE_ARRAY: 70 ivci.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY; 71 break; 72 73 case PIPE_TEXTURE_3D: 74 ivci.viewType = VK_IMAGE_VIEW_TYPE_3D; 75 break; 76 77 default: 78 unreachable("unsupported target"); 79 } 80 81 ivci.format = zink_get_format(screen, templ->format); 82 assert(ivci.format != VK_FORMAT_UNDEFINED); 83 84 /* TODO: it's currently illegal to use non-identity swizzles for framebuffer attachments, 85 * but if that ever changes, this will be useful 86 const struct util_format_description *desc = util_format_description(templ->format); 87 ivci.components.r = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_X)); 88 ivci.components.g = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_Y)); 89 ivci.components.b = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_Z)); 90 ivci.components.a = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_W)); 91 */ 92 ivci.components.r = VK_COMPONENT_SWIZZLE_R; 93 ivci.components.g = VK_COMPONENT_SWIZZLE_G; 94 ivci.components.b = VK_COMPONENT_SWIZZLE_B; 95 ivci.components.a = VK_COMPONENT_SWIZZLE_A; 96 97 ivci.subresourceRange.aspectMask = res->aspect; 98 ivci.subresourceRange.baseMipLevel = templ->u.tex.level; 99 ivci.subresourceRange.levelCount = 1; 100 ivci.subresourceRange.baseArrayLayer = templ->u.tex.first_layer; 101 ivci.subresourceRange.layerCount = 1 + templ->u.tex.last_layer - templ->u.tex.first_layer; 102 assert(ivci.viewType != VK_IMAGE_VIEW_TYPE_3D || ivci.subresourceRange.baseArrayLayer == 0); 103 assert(ivci.viewType != VK_IMAGE_VIEW_TYPE_3D || ivci.subresourceRange.layerCount == 1); 104 ivci.viewType = zink_surface_clamp_viewtype(ivci.viewType, templ->u.tex.first_layer, templ->u.tex.last_layer, res->base.b.array_size); 105 106 return ivci; 107} 108 109static void 110init_surface_info(struct zink_surface *surface, struct zink_resource *res, VkImageViewCreateInfo *ivci) 111{ 112 VkImageViewUsageCreateInfo *usage_info = (VkImageViewUsageCreateInfo *)ivci->pNext; 113 surface->info.flags = res->obj->vkflags; 114 surface->info.usage = usage_info ? usage_info->usage : res->obj->vkusage; 115 surface->info.width = surface->base.width; 116 surface->info.height = surface->base.height; 117 surface->info.layerCount = ivci->subresourceRange.layerCount; 118 surface->info.format[0] = ivci->format; 119 if (res->obj->dt) { 120 struct kopper_displaytarget *cdt = res->obj->dt; 121 if (zink_kopper_has_srgb(cdt)) 122 surface->info.format[1] = ivci->format == cdt->formats[0] ? cdt->formats[1] : cdt->formats[0]; 123 } 124 surface->info_hash = _mesa_hash_data(&surface->info, sizeof(surface->info)); 125} 126 127static struct zink_surface * 128create_surface(struct pipe_context *pctx, 129 struct pipe_resource *pres, 130 const struct pipe_surface *templ, 131 VkImageViewCreateInfo *ivci, 132 bool actually) 133{ 134 struct zink_screen *screen = zink_screen(pctx->screen); 135 struct zink_resource *res = zink_resource(pres); 136 unsigned int level = templ->u.tex.level; 137 138 struct zink_surface *surface = CALLOC_STRUCT(zink_surface); 139 if (!surface) 140 return NULL; 141 142 VkImageViewUsageCreateInfo usage_info; 143 usage_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO; 144 usage_info.pNext = NULL; 145 VkFormatFeatureFlags feats = res->optimal_tiling ? 146 screen->format_props[templ->format].optimalTilingFeatures : 147 screen->format_props[templ->format].linearTilingFeatures; 148 VkImageUsageFlags attachment = (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT); 149 usage_info.usage = res->obj->vkusage & ~attachment; 150 if (res->obj->modifier_aspect) { 151 feats = res->obj->vkfeats; 152 /* intersect format features for current modifier */ 153 for (unsigned i = 0; i < screen->modifier_props[templ->format].drmFormatModifierCount; i++) { 154 if (res->obj->modifier == screen->modifier_props[templ->format].pDrmFormatModifierProperties[i].drmFormatModifier) 155 feats &= screen->modifier_props[templ->format].pDrmFormatModifierProperties[i].drmFormatModifierTilingFeatures; 156 } 157 } 158 if ((res->obj->vkusage & attachment) && 159 !(feats & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) { 160 ivci->pNext = &usage_info; 161 } 162 163 pipe_resource_reference(&surface->base.texture, pres); 164 pipe_reference_init(&surface->base.reference, 1); 165 surface->base.context = pctx; 166 surface->base.format = templ->format; 167 surface->base.width = u_minify(pres->width0, level); 168 assert(surface->base.width); 169 surface->base.height = u_minify(pres->height0, level); 170 assert(surface->base.height); 171 surface->base.nr_samples = templ->nr_samples; 172 surface->base.u.tex.level = level; 173 surface->base.u.tex.first_layer = templ->u.tex.first_layer; 174 surface->base.u.tex.last_layer = templ->u.tex.last_layer; 175 surface->obj = zink_resource(pres)->obj; 176 util_dynarray_init(&surface->desc_set_refs.refs, NULL); 177 178 init_surface_info(surface, res, ivci); 179 180 if (!actually) 181 return surface; 182 assert(ivci->image); 183 VkResult result = VKSCR(CreateImageView)(screen->dev, ivci, NULL, 184 &surface->image_view); 185 if (result != VK_SUCCESS) { 186 mesa_loge("ZINK: vkCreateImageView failed (%s)", vk_Result_to_str(result)); 187 FREE(surface); 188 return NULL; 189 } 190 191 return surface; 192} 193 194static uint32_t 195hash_ivci(const void *key) 196{ 197 return _mesa_hash_data((char*)key + offsetof(VkImageViewCreateInfo, flags), sizeof(VkImageViewCreateInfo) - offsetof(VkImageViewCreateInfo, flags)); 198} 199 200static struct zink_surface * 201do_create_surface(struct pipe_context *pctx, struct pipe_resource *pres, const struct pipe_surface *templ, VkImageViewCreateInfo *ivci, uint32_t hash, bool actually) 202{ 203 /* create a new surface */ 204 struct zink_surface *surface = create_surface(pctx, pres, templ, ivci, actually); 205 surface->base.nr_samples = 0; 206 surface->hash = hash; 207 surface->ivci = *ivci; 208 return surface; 209} 210 211struct pipe_surface * 212zink_get_surface(struct zink_context *ctx, 213 struct pipe_resource *pres, 214 const struct pipe_surface *templ, 215 VkImageViewCreateInfo *ivci) 216{ 217 struct zink_surface *surface = NULL; 218 struct zink_resource *res = zink_resource(pres); 219 uint32_t hash = hash_ivci(ivci); 220 221 simple_mtx_lock(&res->surface_mtx); 222 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, ivci); 223 224 if (!entry) { 225 /* create a new surface */ 226 surface = do_create_surface(&ctx->base, pres, templ, ivci, hash, true); 227 entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, hash, &surface->ivci, surface); 228 if (!entry) { 229 simple_mtx_unlock(&res->surface_mtx); 230 return NULL; 231 } 232 233 surface = entry->data; 234 } else { 235 surface = entry->data; 236 p_atomic_inc(&surface->base.reference.count); 237 } 238 simple_mtx_unlock(&res->surface_mtx); 239 240 return &surface->base; 241} 242 243static struct pipe_surface * 244wrap_surface(struct pipe_context *pctx, struct pipe_surface *psurf) 245{ 246 struct zink_ctx_surface *csurf = CALLOC_STRUCT(zink_ctx_surface); 247 csurf->base = *psurf; 248 pipe_reference_init(&csurf->base.reference, 1); 249 csurf->surf = (struct zink_surface*)psurf; 250 csurf->base.context = pctx; 251 252 return &csurf->base; 253} 254 255static struct pipe_surface * 256zink_create_surface(struct pipe_context *pctx, 257 struct pipe_resource *pres, 258 const struct pipe_surface *templ) 259{ 260 struct zink_resource *res = zink_resource(pres); 261 bool is_array = templ->u.tex.last_layer != templ->u.tex.first_layer; 262 enum pipe_texture_target target_2d[] = {PIPE_TEXTURE_2D, PIPE_TEXTURE_2D_ARRAY}; 263 VkImageViewCreateInfo ivci = create_ivci(zink_screen(pctx->screen), res, templ, 264 pres->target == PIPE_TEXTURE_3D ? target_2d[is_array] : pres->target); 265 266 struct pipe_surface *psurf = NULL; 267 if (res->obj->dt) { 268 /* don't cache swapchain surfaces. that's weird. */ 269 struct zink_surface *surface = do_create_surface(pctx, pres, templ, &ivci, 0, false); 270 if (surface) { 271 surface->is_swapchain = true; 272 psurf = &surface->base; 273 } 274 } else 275 psurf = zink_get_surface(zink_context(pctx), pres, templ, &ivci); 276 if (!psurf) 277 return NULL; 278 279 struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)wrap_surface(pctx, psurf); 280 281 if (templ->nr_samples) { 282 /* transient fb attachment: not cached */ 283 struct pipe_resource rtempl = *pres; 284 rtempl.nr_samples = templ->nr_samples; 285 rtempl.bind |= ZINK_BIND_TRANSIENT; 286 struct zink_resource *transient = zink_resource(pctx->screen->resource_create(pctx->screen, &rtempl)); 287 if (!transient) 288 return NULL; 289 ivci.image = transient->obj->image; 290 csurf->transient = (struct zink_ctx_surface*)wrap_surface(pctx, (struct pipe_surface*)create_surface(pctx, &transient->base.b, templ, &ivci, true)); 291 if (!csurf->transient) { 292 pipe_resource_reference((struct pipe_resource**)&transient, NULL); 293 pipe_surface_release(pctx, &psurf); 294 return NULL; 295 } 296 pipe_resource_reference((struct pipe_resource**)&transient, NULL); 297 } 298 299 return &csurf->base; 300} 301 302void 303zink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface) 304{ 305 struct zink_surface *surface = zink_surface(psurface); 306 struct zink_resource *res = zink_resource(psurface->texture); 307 if (!psurface->nr_samples && !surface->is_swapchain) { 308 simple_mtx_lock(&res->surface_mtx); 309 if (psurface->reference.count) { 310 /* got a cache hit during deletion */ 311 simple_mtx_unlock(&res->surface_mtx); 312 return; 313 } 314 struct hash_entry *he = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci); 315 assert(he); 316 assert(he->data == surface); 317 _mesa_hash_table_remove(&res->surface_cache, he); 318 simple_mtx_unlock(&res->surface_mtx); 319 } 320 zink_descriptor_set_refs_clear(&surface->desc_set_refs, surface); 321 if (surface->simage_view) 322 VKSCR(DestroyImageView)(screen->dev, surface->simage_view, NULL); 323 if (surface->is_swapchain) { 324 for (unsigned i = 0; i < surface->old_swapchain_size; i++) 325 VKSCR(DestroyImageView)(screen->dev, surface->old_swapchain[i], NULL); 326 for (unsigned i = 0; i < surface->swapchain_size; i++) 327 VKSCR(DestroyImageView)(screen->dev, surface->swapchain[i], NULL); 328 free(surface->swapchain); 329 } else 330 VKSCR(DestroyImageView)(screen->dev, surface->image_view, NULL); 331 pipe_resource_reference(&psurface->texture, NULL); 332 FREE(surface); 333} 334 335static void 336zink_surface_destroy(struct pipe_context *pctx, 337 struct pipe_surface *psurface) 338{ 339 struct zink_ctx_surface *csurf = (struct zink_ctx_surface *)psurface; 340 zink_surface_reference(zink_screen(pctx->screen), &csurf->surf, NULL); 341 pipe_surface_release(pctx, (struct pipe_surface**)&csurf->transient); 342 FREE(csurf); 343} 344 345bool 346zink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface) 347{ 348 struct zink_surface *surface = zink_surface(*psurface); 349 struct zink_resource *res = zink_resource((*psurface)->texture); 350 struct zink_screen *screen = zink_screen(ctx->base.screen); 351 if (surface->simage_view) 352 return false; 353 assert(!res->obj->dt); 354 VkImageViewCreateInfo ivci = surface->ivci; 355 ivci.image = res->obj->image; 356 uint32_t hash = hash_ivci(&ivci); 357 358 simple_mtx_lock(&res->surface_mtx); 359 struct hash_entry *new_entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, &ivci); 360 if (zink_batch_usage_exists(surface->batch_uses)) 361 zink_batch_reference_surface(&ctx->batch, surface); 362 zink_descriptor_set_refs_clear(&surface->desc_set_refs, surface); 363 if (new_entry) { 364 /* reuse existing surface; old one will be cleaned up naturally */ 365 struct zink_surface *new_surface = new_entry->data; 366 simple_mtx_unlock(&res->surface_mtx); 367 zink_batch_usage_set(&new_surface->batch_uses, ctx->batch.state); 368 zink_surface_reference(screen, (struct zink_surface**)psurface, new_surface); 369 return true; 370 } 371 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci); 372 assert(entry); 373 _mesa_hash_table_remove(&res->surface_cache, entry); 374 VkImageView image_view; 375 VkResult result = VKSCR(CreateImageView)(screen->dev, &ivci, NULL, &image_view); 376 if (result != VK_SUCCESS) { 377 mesa_loge("ZINK: failed to create new imageview (%s)", vk_Result_to_str(result)); 378 simple_mtx_unlock(&res->surface_mtx); 379 return false; 380 } 381 surface->hash = hash; 382 surface->ivci = ivci; 383 entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci, surface); 384 assert(entry); 385 surface->simage_view = surface->image_view; 386 surface->image_view = image_view; 387 surface->obj = zink_resource(surface->base.texture)->obj; 388 /* update for imageless fb */ 389 surface->info.flags = res->obj->vkflags; 390 surface->info.usage = res->obj->vkusage; 391 surface->info_hash = _mesa_hash_data(&surface->info, sizeof(surface->info)); 392 zink_batch_usage_set(&surface->batch_uses, ctx->batch.state); 393 simple_mtx_unlock(&res->surface_mtx); 394 return true; 395} 396 397struct pipe_surface * 398zink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples) 399{ 400 struct pipe_surface surf_templ = {0}; 401 402 struct pipe_resource *pres; 403 struct pipe_resource templ = {0}; 404 templ.width0 = width; 405 templ.height0 = height; 406 templ.depth0 = 1; 407 templ.format = PIPE_FORMAT_R8G8B8A8_UNORM; 408 templ.target = target; 409 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW; 410 if (samples < 2) 411 templ.bind |= PIPE_BIND_SHADER_IMAGE; 412 templ.nr_samples = samples; 413 414 pres = ctx->base.screen->resource_create(ctx->base.screen, &templ); 415 if (!pres) 416 return NULL; 417 418 surf_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM; 419 surf_templ.nr_samples = 0; 420 struct pipe_surface *psurf = ctx->base.create_surface(&ctx->base, pres, &surf_templ); 421 pipe_resource_reference(&pres, NULL); 422 return psurf; 423} 424 425void 426zink_context_surface_init(struct pipe_context *context) 427{ 428 context->create_surface = zink_create_surface; 429 context->surface_destroy = zink_surface_destroy; 430} 431 432void 433zink_surface_swapchain_update(struct zink_context *ctx, struct zink_surface *surface) 434{ 435 struct zink_screen *screen = zink_screen(ctx->base.screen); 436 struct zink_resource *res = zink_resource(surface->base.texture); 437 struct kopper_displaytarget *cdt = res->obj->dt; 438 if (!cdt) 439 return; //dead swapchain 440 if (res->obj->dt != surface->dt) { 441 /* new swapchain: clear out previous old_swapchain and move current swapchain there */ 442 for (unsigned i = 0; i < surface->old_swapchain_size; i++) 443 util_dynarray_append(&ctx->batch.state->dead_swapchains, VkImageView, surface->old_swapchain[i]); 444 free(surface->old_swapchain); 445 surface->old_swapchain = surface->swapchain; 446 surface->old_swapchain_size = surface->swapchain_size; 447 surface->swapchain_size = cdt->swapchain->num_images; 448 surface->swapchain = calloc(surface->swapchain_size, sizeof(VkImageView)); 449 surface->base.width = res->base.b.width0; 450 surface->base.height = res->base.b.height0; 451 init_surface_info(surface, res, &surface->ivci); 452 } 453 if (!surface->swapchain[res->obj->dt_idx]) { 454 assert(res->obj->image && cdt->swapchain->images[res->obj->dt_idx].image == res->obj->image); 455 surface->ivci.image = res->obj->image; 456 assert(surface->ivci.image); 457 VKSCR(CreateImageView)(screen->dev, &surface->ivci, NULL, &surface->swapchain[res->obj->dt_idx]); 458 } 459 surface->image_view = surface->swapchain[res->obj->dt_idx]; 460} 461