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