• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_format.h"
27 #include "zink_resource.h"
28 #include "zink_screen.h"
29 #include "zink_surface.h"
30 #include "zink_kopper.h"
31 
32 #include "util/format/u_format.h"
33 #include "util/u_inlines.h"
34 #include "util/u_memory.h"
35 
36 VkImageViewCreateInfo
create_ivci(struct zink_screen * screen,struct zink_resource * res,const struct pipe_surface * templ,enum pipe_texture_target target)37 create_ivci(struct zink_screen *screen,
38             struct zink_resource *res,
39             const struct pipe_surface *templ,
40             enum pipe_texture_target target)
41 {
42    VkImageViewCreateInfo ivci;
43    /* zero holes since this is hashed */
44    memset(&ivci, 0, sizeof(VkImageViewCreateInfo));
45    ivci.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
46    ivci.image = res->obj->image;
47 
48    switch (target) {
49    case PIPE_TEXTURE_1D:
50       ivci.viewType = res->need_2D ? VK_IMAGE_VIEW_TYPE_2D : VK_IMAGE_VIEW_TYPE_1D;
51       break;
52 
53    case PIPE_TEXTURE_1D_ARRAY:
54       ivci.viewType = res->need_2D ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_1D_ARRAY;
55       break;
56 
57    case PIPE_TEXTURE_2D:
58    case PIPE_TEXTURE_RECT:
59       ivci.viewType = VK_IMAGE_VIEW_TYPE_2D;
60       break;
61 
62    case PIPE_TEXTURE_2D_ARRAY:
63       ivci.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
64       break;
65 
66    case PIPE_TEXTURE_CUBE:
67       ivci.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
68       break;
69 
70    case PIPE_TEXTURE_CUBE_ARRAY:
71       ivci.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
72       break;
73 
74    case PIPE_TEXTURE_3D:
75       ivci.viewType = VK_IMAGE_VIEW_TYPE_3D;
76       break;
77 
78    default:
79       unreachable("unsupported target");
80    }
81 
82    ivci.format = res->base.b.format == PIPE_FORMAT_A8_UNORM ? res->format : zink_get_format(screen, templ->format);
83    assert(ivci.format != VK_FORMAT_UNDEFINED);
84 
85    /* TODO: it's currently illegal to use non-identity swizzles for framebuffer attachments,
86     * but if that ever changes, this will be useful
87    const struct util_format_description *desc = util_format_description(templ->format);
88    ivci.components.r = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_X));
89    ivci.components.g = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_Y));
90    ivci.components.b = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_Z));
91    ivci.components.a = zink_component_mapping(zink_clamp_void_swizzle(desc, PIPE_SWIZZLE_W));
92    */
93    ivci.components.r = VK_COMPONENT_SWIZZLE_R;
94    ivci.components.g = VK_COMPONENT_SWIZZLE_G;
95    ivci.components.b = VK_COMPONENT_SWIZZLE_B;
96    ivci.components.a = VK_COMPONENT_SWIZZLE_A;
97 
98    ivci.subresourceRange.aspectMask = res->aspect;
99    ivci.subresourceRange.baseMipLevel = templ->u.tex.level;
100    ivci.subresourceRange.levelCount = 1;
101    ivci.subresourceRange.baseArrayLayer = templ->u.tex.first_layer;
102    ivci.subresourceRange.layerCount = 1 + templ->u.tex.last_layer - templ->u.tex.first_layer;
103    assert(ivci.viewType != VK_IMAGE_VIEW_TYPE_3D || ivci.subresourceRange.baseArrayLayer == 0);
104    assert(ivci.viewType != VK_IMAGE_VIEW_TYPE_3D || ivci.subresourceRange.layerCount == 1);
105    /* ensure cube image types get clamped to 2D/2D_ARRAY as expected for partial views */
106    ivci.viewType = zink_surface_clamp_viewtype(ivci.viewType, templ->u.tex.first_layer, templ->u.tex.last_layer, res->base.b.array_size);
107 
108    return ivci;
109 }
110 
111 /* this is used for framebuffer attachments to set up imageless framebuffers */
112 static void
init_surface_info(struct zink_screen * screen,struct zink_surface * surface,struct zink_resource * res,VkImageViewCreateInfo * ivci)113 init_surface_info(struct zink_screen *screen, struct zink_surface *surface, struct zink_resource *res, VkImageViewCreateInfo *ivci)
114 {
115    VkImageViewUsageCreateInfo *usage_info = (VkImageViewUsageCreateInfo *)ivci->pNext;
116    surface->info.flags = res->obj->vkflags;
117    surface->info.usage = usage_info ? usage_info->usage : res->obj->vkusage;
118    surface->info.width = surface->base.width;
119    surface->info.height = surface->base.height;
120    surface->info.layerCount = ivci->subresourceRange.layerCount;
121    surface->info.format[0] = ivci->format;
122    if (res->obj->dt) {
123       struct kopper_displaytarget *cdt = res->obj->dt;
124       if (zink_kopper_has_srgb(cdt))
125          surface->info.format[1] = ivci->format == cdt->formats[0] ? cdt->formats[1] : cdt->formats[0];
126    } else {
127       enum pipe_format srgb = util_format_is_srgb(surface->base.format) ? util_format_linear(surface->base.format) : util_format_srgb(surface->base.format);
128       if (srgb == surface->base.format)
129          srgb = PIPE_FORMAT_NONE;
130       if (srgb) {
131          VkFormat format = zink_get_format(screen, srgb);
132          if (format)
133             surface->info.format[1] = format;
134       }
135    }
136 }
137 
138 static void
init_pipe_surface_info(struct pipe_context * pctx,struct pipe_surface * psurf,const struct pipe_surface * templ,const struct pipe_resource * pres)139 init_pipe_surface_info(struct pipe_context *pctx, struct pipe_surface *psurf, const struct pipe_surface *templ, const struct pipe_resource *pres)
140 {
141    unsigned int level = templ->u.tex.level;
142    psurf->context = pctx;
143    psurf->format = templ->format;
144    psurf->width = u_minify(pres->width0, level);
145    assert(psurf->width);
146    psurf->height = u_minify(pres->height0, level);
147    assert(psurf->height);
148 
149    if (util_format_is_compressed(pres->format) &&
150        !util_format_is_compressed(psurf->format)) {
151       assert(util_format_get_blockwidth(psurf->format) == 1);
152       assert(util_format_get_blockheight(psurf->format) == 1);
153 
154       psurf->width = DIV_ROUND_UP(psurf->width,
155                                   util_format_get_blockwidth(pres->format));
156       psurf->height = DIV_ROUND_UP(psurf->height,
157                                    util_format_get_blockheight(pres->format));
158    }
159 
160    psurf->nr_samples = templ->nr_samples;
161    psurf->u.tex.level = level;
162    psurf->u.tex.first_layer = templ->u.tex.first_layer;
163    psurf->u.tex.last_layer = templ->u.tex.last_layer;
164 }
165 
166 static void
apply_view_usage_for_format(struct zink_screen * screen,struct zink_resource * res,struct zink_surface * surface,enum pipe_format format,VkImageViewCreateInfo * ivci)167 apply_view_usage_for_format(struct zink_screen *screen, struct zink_resource *res, struct zink_surface *surface, enum pipe_format format, VkImageViewCreateInfo *ivci)
168 {
169    VkFormatFeatureFlags feats = res->linear ?
170                                 zink_get_format_props(screen, format)->linearTilingFeatures :
171                                 zink_get_format_props(screen, format)->optimalTilingFeatures;
172    VkImageUsageFlags attachment = (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT);
173    surface->usage_info.usage = res->obj->vkusage & ~attachment;
174    if (res->obj->modifier_aspect) {
175       feats = res->obj->vkfeats;
176       /* intersect format features for current modifier */
177       for (unsigned i = 0; i < screen->modifier_props[format].drmFormatModifierCount; i++) {
178          if (res->obj->modifier == screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifier)
179             feats &= screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifierTilingFeatures;
180       }
181    }
182    /* if the format features don't support framebuffer attachment, use VkImageViewUsageCreateInfo to remove it */
183    if ((res->obj->vkusage & attachment) &&
184        !(feats & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) {
185       ivci->pNext = &surface->usage_info;
186    }
187 }
188 
189 static struct zink_surface *
create_surface(struct pipe_context * pctx,struct pipe_resource * pres,const struct pipe_surface * templ,VkImageViewCreateInfo * ivci,bool actually)190 create_surface(struct pipe_context *pctx,
191                struct pipe_resource *pres,
192                const struct pipe_surface *templ,
193                VkImageViewCreateInfo *ivci,
194                bool actually)
195 {
196    struct zink_screen *screen = zink_screen(pctx->screen);
197    struct zink_resource *res = zink_resource(pres);
198 
199    struct zink_surface *surface = CALLOC_STRUCT(zink_surface);
200    if (!surface)
201       return NULL;
202 
203    surface->usage_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO;
204    surface->usage_info.pNext = NULL;
205    apply_view_usage_for_format(screen, res, surface, templ->format, ivci);
206 
207    pipe_resource_reference(&surface->base.texture, pres);
208    pipe_reference_init(&surface->base.reference, 1);
209    init_pipe_surface_info(pctx, &surface->base, templ, pres);
210    surface->obj = zink_resource(pres)->obj;
211 
212    init_surface_info(screen, surface, res, ivci);
213 
214    if (!actually)
215       return surface;
216    assert(ivci->image);
217    VkResult result = VKSCR(CreateImageView)(screen->dev, ivci, NULL,
218                                             &surface->image_view);
219    if (result != VK_SUCCESS) {
220       mesa_loge("ZINK: vkCreateImageView failed (%s)", vk_Result_to_str(result));
221       FREE(surface);
222       return NULL;
223    }
224 
225    return surface;
226 }
227 
228 static uint32_t
hash_ivci(const void * key)229 hash_ivci(const void *key)
230 {
231    return _mesa_hash_data((char*)key + offsetof(VkImageViewCreateInfo, flags), sizeof(VkImageViewCreateInfo) - offsetof(VkImageViewCreateInfo, flags));
232 }
233 
234 static struct zink_surface *
do_create_surface(struct pipe_context * pctx,struct pipe_resource * pres,const struct pipe_surface * templ,VkImageViewCreateInfo * ivci,uint32_t hash,bool actually)235 do_create_surface(struct pipe_context *pctx, struct pipe_resource *pres, const struct pipe_surface *templ, VkImageViewCreateInfo *ivci, uint32_t hash, bool actually)
236 {
237    /* create a new surface */
238    struct zink_surface *surface = create_surface(pctx, pres, templ, ivci, actually);
239    /* only transient surfaces have nr_samples set */
240    surface->base.nr_samples = zink_screen(pctx->screen)->info.have_EXT_multisampled_render_to_single_sampled ? templ->nr_samples : 0;
241    surface->hash = hash;
242    surface->ivci = *ivci;
243    return surface;
244 }
245 
246 /* get a cached surface for a shader descriptor */
247 struct zink_surface *
zink_get_surface(struct zink_context * ctx,struct pipe_resource * pres,const struct pipe_surface * templ,VkImageViewCreateInfo * ivci)248 zink_get_surface(struct zink_context *ctx,
249             struct pipe_resource *pres,
250             const struct pipe_surface *templ,
251             VkImageViewCreateInfo *ivci)
252 {
253    struct zink_surface *surface = NULL;
254    struct zink_resource *res = zink_resource(pres);
255    uint32_t hash = hash_ivci(ivci);
256 
257    simple_mtx_lock(&res->surface_mtx);
258    struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, ivci);
259 
260    if (!entry) {
261       /* create a new surface, but don't actually create the imageview if mutable isn't set and the format is different;
262        * mutable will be set later and the imageview will be filled in
263        */
264       bool actually = !zink_format_needs_mutable(pres->format, templ->format) || (pres->bind & ZINK_BIND_MUTABLE);
265       surface = do_create_surface(&ctx->base, pres, templ, ivci, hash, actually);
266       entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, hash, &surface->ivci, surface);
267       if (!entry) {
268          simple_mtx_unlock(&res->surface_mtx);
269          return NULL;
270       }
271 
272       surface = entry->data;
273    } else {
274       surface = entry->data;
275       p_atomic_inc(&surface->base.reference.count);
276    }
277    simple_mtx_unlock(&res->surface_mtx);
278 
279    return surface;
280 }
281 
282 /* wrap a surface for use as a framebuffer attachment
283  * Takes ownership of surface */
284 static struct zink_ctx_surface *
wrap_surface(struct pipe_context * pctx,struct zink_surface * surface,const struct pipe_surface * templ)285 wrap_surface(struct pipe_context *pctx,
286              struct zink_surface *surface,
287              const struct pipe_surface *templ)
288 {
289    struct zink_ctx_surface *csurf = CALLOC_STRUCT(zink_ctx_surface);
290    if (!csurf) {
291       zink_surface_reference (zink_screen(pctx->screen), &surface, NULL);
292       return NULL;
293    }
294 
295    csurf->base = *templ;
296    pipe_reference_init(&csurf->base.reference, 1);
297    csurf->surf = surface;
298    csurf->base.context = pctx;
299 
300    return csurf;
301 }
302 
303 /* this is the context hook, so only zink_ctx_surfaces will reach it */
304 static void
zink_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurface)305 zink_surface_destroy(struct pipe_context *pctx,
306                      struct pipe_surface *psurface)
307 {
308    struct zink_ctx_surface *csurf = (struct zink_ctx_surface *)psurface;
309    if (csurf->needs_mutable)
310       /* this has an extra resource ref */
311       pipe_resource_reference(&csurf->base.texture, NULL);
312    zink_surface_reference(zink_screen(pctx->screen), &csurf->surf, NULL);
313    pipe_surface_release(pctx, (struct pipe_surface**)&csurf->transient);
314    FREE(csurf);
315 }
316 
317 /* this the context hook that returns a zink_ctx_surface */
318 static struct pipe_surface *
zink_create_surface(struct pipe_context * pctx,struct pipe_resource * pres,const struct pipe_surface * templ)319 zink_create_surface(struct pipe_context *pctx,
320                     struct pipe_resource *pres,
321                     const struct pipe_surface *templ)
322 {
323    struct zink_resource *res = zink_resource(pres);
324    struct zink_screen *screen = zink_screen(pctx->screen);
325    bool is_array = templ->u.tex.last_layer != templ->u.tex.first_layer;
326    bool needs_mutable = false;
327    enum pipe_texture_target target_2d[] = {PIPE_TEXTURE_2D, PIPE_TEXTURE_2D_ARRAY};
328    if (!res->obj->dt && zink_format_needs_mutable(pres->format, templ->format)) {
329       /* mutable not set by default */
330       needs_mutable = !(res->base.b.bind & ZINK_BIND_MUTABLE);
331       /*
332          VUID-VkImageViewCreateInfo-image-07072
333          If image was created with the VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT flag and
334          format is a non-compressed format, the levelCount and layerCount members of
335          subresourceRange must both be 1
336 
337          ...but this is allowed with a maintenance6 property
338        */
339       if (util_format_is_compressed(pres->format) && templ->u.tex.first_layer != templ->u.tex.last_layer &&
340           (!screen->info.have_KHR_maintenance6 || !screen->info.maint6_props.blockTexelViewCompatibleMultipleLayers))
341          return NULL;
342    }
343 
344    if (!screen->threaded && needs_mutable) {
345       /* this is fine without tc */
346       needs_mutable = false;
347       zink_resource_object_init_mutable(zink_context(pctx), res);
348    }
349 
350    if (!zink_get_format(screen, templ->format))
351       return NULL;
352 
353    VkImageViewCreateInfo ivci = create_ivci(screen, res, templ,
354                                             pres->target == PIPE_TEXTURE_3D ? target_2d[is_array] : pres->target);
355 
356    struct zink_surface *surface = NULL;
357    if (res->obj->dt) {
358       /* don't cache swapchain surfaces. that's weird. */
359       surface = do_create_surface(pctx, pres, templ, &ivci, 0, false);
360       if (unlikely(!surface)) {
361          mesa_loge("ZINK: failed do_create_surface!");
362          return NULL;
363       }
364 
365       surface->is_swapchain = true;
366    } else if (!needs_mutable) {
367       surface = zink_get_surface(zink_context(pctx), pres, templ, &ivci);
368       if (unlikely(!surface)) {
369          mesa_loge("ZINK: failed to get non-mutable surface!");
370          return NULL;
371       }
372    }
373 
374    struct zink_ctx_surface *csurf = wrap_surface(pctx, surface, needs_mutable ? templ : &surface->base); /* move ownership of surface */
375    if (!unlikely (csurf)) {
376       mesa_loge("ZINK: failed to allocate csurf!");
377       return NULL;
378    }
379 
380    csurf->needs_mutable = needs_mutable;
381    if (needs_mutable) {
382       pipe_resource_reference(&csurf->base.texture, pres);
383       init_pipe_surface_info(pctx, &csurf->base, templ, pres);
384    }
385 
386    if (templ->nr_samples && !screen->info.have_EXT_multisampled_render_to_single_sampled) {
387       /* transient fb attachment: not cached */
388       struct pipe_resource rtempl = *pres;
389       rtempl.nr_samples = templ->nr_samples;
390       rtempl.bind |= ZINK_BIND_TRANSIENT;
391       struct zink_resource *transient = zink_resource(pctx->screen->resource_create(pctx->screen, &rtempl));
392       if (unlikely(!transient)) {
393          mesa_loge("ZINK: failed to create transient resource!");
394          goto fail;
395       }
396 
397       ivci.image = transient->obj->image;
398       struct zink_surface *tsurf = create_surface(pctx, &transient->base.b, templ, &ivci, true);
399       pipe_resource_reference((struct pipe_resource**)&transient, NULL);
400       if (unlikely(!tsurf)) {
401          mesa_loge("ZINK: failed to create transient surface!");
402          goto fail;
403       }
404 
405       csurf->transient = wrap_surface(pctx, tsurf, &tsurf->base); /* move ownership of tsurf */
406       if (unlikely(!csurf->transient)) {
407          mesa_loge("ZINK: failed to wrap transient surface!");
408          goto fail;
409       }
410    }
411 
412    return &csurf->base;
413 fail:
414    zink_surface_destroy(pctx, &csurf->base);
415    return NULL;
416 }
417 
418 void
zink_destroy_surface(struct zink_screen * screen,struct pipe_surface * psurface)419 zink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface)
420 {
421    struct zink_surface *surface = zink_surface(psurface);
422    struct zink_resource *res = zink_resource(psurface->texture);
423    if ((!psurface->nr_samples || screen->info.have_EXT_multisampled_render_to_single_sampled) && !surface->is_swapchain) {
424       simple_mtx_lock(&res->surface_mtx);
425       if (psurface->reference.count) {
426          /* a different context got a cache hit during deletion: this surface is alive again */
427          simple_mtx_unlock(&res->surface_mtx);
428          return;
429       }
430       struct hash_entry *he = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci);
431       assert(he);
432       assert(he->data == surface);
433       _mesa_hash_table_remove(&res->surface_cache, he);
434       simple_mtx_unlock(&res->surface_mtx);
435    }
436    /* this surface is dead now */
437    simple_mtx_lock(&res->obj->view_lock);
438    /* imageviews are never destroyed directly to ensure lifetimes for in-use surfaces */
439    if (surface->is_swapchain) {
440       for (unsigned i = 0; i < surface->swapchain_size; i++)
441          util_dynarray_append(&res->obj->views, VkImageView, surface->swapchain[i]);
442       free(surface->swapchain);
443    } else
444       util_dynarray_append(&res->obj->views, VkImageView, surface->image_view);
445    simple_mtx_unlock(&res->obj->view_lock);
446    pipe_resource_reference(&psurface->texture, NULL);
447    FREE(surface);
448 }
449 
450 /* this is called when a surface is rebound for mutable/storage use */
451 bool
zink_rebind_surface(struct zink_context * ctx,struct pipe_surface ** psurface)452 zink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface)
453 {
454    struct zink_surface *surface = zink_surface(*psurface);
455    struct zink_resource *res = zink_resource((*psurface)->texture);
456    struct zink_screen *screen = zink_screen(ctx->base.screen);
457    if (surface->obj == res->obj)
458       return false;
459    assert(!res->obj->dt);
460    VkImageViewCreateInfo ivci = surface->ivci;
461    ivci.image = res->obj->image;
462    uint32_t hash = hash_ivci(&ivci);
463 
464    simple_mtx_lock(&res->surface_mtx);
465    struct hash_entry *new_entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, &ivci);
466    if (new_entry) {
467       /* reuse existing surface; old one will be cleaned up naturally */
468       struct zink_surface *new_surface = new_entry->data;
469       simple_mtx_unlock(&res->surface_mtx);
470       zink_surface_reference(screen, (struct zink_surface**)psurface, new_surface);
471       return true;
472    }
473    struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci);
474    assert(entry);
475    _mesa_hash_table_remove(&res->surface_cache, entry);
476    VkImageView image_view;
477    apply_view_usage_for_format(screen, res, surface, surface->base.format, &ivci);
478    VkResult result = VKSCR(CreateImageView)(screen->dev, &ivci, NULL, &image_view);
479    if (result != VK_SUCCESS) {
480       mesa_loge("ZINK: failed to create new imageview (%s)", vk_Result_to_str(result));
481       simple_mtx_unlock(&res->surface_mtx);
482       return false;
483    }
484    surface->hash = hash;
485    surface->ivci = ivci;
486    entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci, surface);
487    assert(entry);
488    simple_mtx_lock(&res->obj->view_lock);
489    util_dynarray_append(&res->obj->views, VkImageView, surface->image_view);
490    simple_mtx_unlock(&res->obj->view_lock);
491    surface->image_view = image_view;
492    surface->obj = zink_resource(surface->base.texture)->obj;
493    /* update for imageless fb */
494    surface->info.flags = res->obj->vkflags;
495    surface->info.usage = res->obj->vkusage;
496    simple_mtx_unlock(&res->surface_mtx);
497    return true;
498 }
499 
500 /* dummy surfaces are used for null framebuffer/descriptors */
501 struct pipe_surface *
zink_surface_create_null(struct zink_context * ctx,enum pipe_texture_target target,unsigned width,unsigned height,unsigned samples)502 zink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples)
503 {
504    struct pipe_surface surf_templ = {0};
505 
506    struct pipe_resource *pres;
507    struct pipe_resource templ = {0};
508    templ.width0 = width;
509    templ.height0 = height;
510    templ.depth0 = 1;
511    templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
512    templ.target = target;
513    templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
514    if (samples < 2)
515       templ.bind |= PIPE_BIND_SHADER_IMAGE;
516    templ.nr_samples = samples;
517 
518    pres = ctx->base.screen->resource_create(ctx->base.screen, &templ);
519    if (!pres)
520       return NULL;
521 
522    surf_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
523    surf_templ.nr_samples = 0;
524    struct pipe_surface *psurf = ctx->base.create_surface(&ctx->base, pres, &surf_templ);
525    pipe_resource_reference(&pres, NULL);
526    return psurf;
527 }
528 
529 void
zink_context_surface_init(struct pipe_context * context)530 zink_context_surface_init(struct pipe_context *context)
531 {
532    context->create_surface = zink_create_surface;
533    context->surface_destroy = zink_surface_destroy;
534 }
535 
536 /* must be called before a swapchain image is used to ensure correct imageview is used */
537 void
zink_surface_swapchain_update(struct zink_context * ctx,struct zink_surface * surface)538 zink_surface_swapchain_update(struct zink_context *ctx, struct zink_surface *surface)
539 {
540    struct zink_screen *screen = zink_screen(ctx->base.screen);
541    struct zink_resource *res = zink_resource(surface->base.texture);
542    struct kopper_displaytarget *cdt = res->obj->dt;
543    if (!cdt)
544       return; //dead swapchain
545    if (cdt->swapchain != surface->dt_swapchain) {
546       /* new swapchain: clear out previous swapchain imageviews/array and setup a new one;
547        * old views will be pruned normally in zink_batch or on object destruction
548        */
549       simple_mtx_lock(&res->obj->view_lock);
550       for (unsigned i = 0; i < surface->swapchain_size; i++)
551          util_dynarray_append(&res->obj->views, VkImageView, surface->swapchain[i]);
552       simple_mtx_unlock(&res->obj->view_lock);
553       free(surface->swapchain);
554       surface->swapchain_size = cdt->swapchain->num_images;
555       surface->swapchain = calloc(surface->swapchain_size, sizeof(VkImageView));
556       if (!surface->swapchain) {
557          mesa_loge("ZINK: failed to allocate surface->swapchain!");
558          return;
559       }
560       surface->base.width = res->base.b.width0;
561       surface->base.height = res->base.b.height0;
562       init_surface_info(screen, surface, res, &surface->ivci);
563       surface->dt_swapchain = cdt->swapchain;
564    }
565    if (!surface->swapchain[res->obj->dt_idx]) {
566       /* no current swapchain imageview exists: create it */
567       assert(res->obj->image && cdt->swapchain->images[res->obj->dt_idx].image == res->obj->image);
568       surface->ivci.image = res->obj->image;
569       assert(surface->ivci.image);
570       VKSCR(CreateImageView)(screen->dev, &surface->ivci, NULL, &surface->swapchain[res->obj->dt_idx]);
571    }
572    /* the current swapchain imageview is now the view for the current swapchain image */
573    surface->image_view = surface->swapchain[res->obj->dt_idx];
574 }
575