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 psurf->nr_samples = templ->nr_samples;
149 psurf->u.tex.level = level;
150 psurf->u.tex.first_layer = templ->u.tex.first_layer;
151 psurf->u.tex.last_layer = templ->u.tex.last_layer;
152 }
153
154 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)155 apply_view_usage_for_format(struct zink_screen *screen, struct zink_resource *res, struct zink_surface *surface, enum pipe_format format, VkImageViewCreateInfo *ivci)
156 {
157 VkFormatFeatureFlags feats = res->linear ?
158 screen->format_props[format].linearTilingFeatures :
159 screen->format_props[format].optimalTilingFeatures;
160 VkImageUsageFlags attachment = (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT);
161 surface->usage_info.usage = res->obj->vkusage & ~attachment;
162 if (res->obj->modifier_aspect) {
163 feats = res->obj->vkfeats;
164 /* intersect format features for current modifier */
165 for (unsigned i = 0; i < screen->modifier_props[format].drmFormatModifierCount; i++) {
166 if (res->obj->modifier == screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifier)
167 feats &= screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifierTilingFeatures;
168 }
169 }
170 /* if the format features don't support framebuffer attachment, use VkImageViewUsageCreateInfo to remove it */
171 if ((res->obj->vkusage & attachment) &&
172 !(feats & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) {
173 ivci->pNext = &surface->usage_info;
174 }
175 }
176
177 static struct zink_surface *
create_surface(struct pipe_context * pctx,struct pipe_resource * pres,const struct pipe_surface * templ,VkImageViewCreateInfo * ivci,bool actually)178 create_surface(struct pipe_context *pctx,
179 struct pipe_resource *pres,
180 const struct pipe_surface *templ,
181 VkImageViewCreateInfo *ivci,
182 bool actually)
183 {
184 struct zink_screen *screen = zink_screen(pctx->screen);
185 struct zink_resource *res = zink_resource(pres);
186
187 struct zink_surface *surface = CALLOC_STRUCT(zink_surface);
188 if (!surface)
189 return NULL;
190
191 surface->usage_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO;
192 surface->usage_info.pNext = NULL;
193 apply_view_usage_for_format(screen, res, surface, templ->format, ivci);
194
195 pipe_resource_reference(&surface->base.texture, pres);
196 pipe_reference_init(&surface->base.reference, 1);
197 init_pipe_surface_info(pctx, &surface->base, templ, pres);
198 surface->obj = zink_resource(pres)->obj;
199
200 init_surface_info(screen, surface, res, ivci);
201
202 if (!actually)
203 return surface;
204 assert(ivci->image);
205 VkResult result = VKSCR(CreateImageView)(screen->dev, ivci, NULL,
206 &surface->image_view);
207 if (result != VK_SUCCESS) {
208 mesa_loge("ZINK: vkCreateImageView failed (%s)", vk_Result_to_str(result));
209 FREE(surface);
210 return NULL;
211 }
212
213 return surface;
214 }
215
216 static uint32_t
hash_ivci(const void * key)217 hash_ivci(const void *key)
218 {
219 return _mesa_hash_data((char*)key + offsetof(VkImageViewCreateInfo, flags), sizeof(VkImageViewCreateInfo) - offsetof(VkImageViewCreateInfo, flags));
220 }
221
222 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)223 do_create_surface(struct pipe_context *pctx, struct pipe_resource *pres, const struct pipe_surface *templ, VkImageViewCreateInfo *ivci, uint32_t hash, bool actually)
224 {
225 /* create a new surface */
226 struct zink_surface *surface = create_surface(pctx, pres, templ, ivci, actually);
227 /* only transient surfaces have nr_samples set */
228 surface->base.nr_samples = zink_screen(pctx->screen)->info.have_EXT_multisampled_render_to_single_sampled ? templ->nr_samples : 0;
229 surface->hash = hash;
230 surface->ivci = *ivci;
231 return surface;
232 }
233
234 /* get a cached surface for a shader descriptor */
235 struct zink_surface *
zink_get_surface(struct zink_context * ctx,struct pipe_resource * pres,const struct pipe_surface * templ,VkImageViewCreateInfo * ivci)236 zink_get_surface(struct zink_context *ctx,
237 struct pipe_resource *pres,
238 const struct pipe_surface *templ,
239 VkImageViewCreateInfo *ivci)
240 {
241 struct zink_surface *surface = NULL;
242 struct zink_resource *res = zink_resource(pres);
243 uint32_t hash = hash_ivci(ivci);
244
245 simple_mtx_lock(&res->surface_mtx);
246 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, ivci);
247
248 if (!entry) {
249 /* create a new surface, but don't actually create the imageview if mutable isn't set and the format is different;
250 * mutable will be set later and the imageview will be filled in
251 */
252 bool actually = !zink_format_needs_mutable(pres->format, templ->format) || (pres->bind & ZINK_BIND_MUTABLE);
253 surface = do_create_surface(&ctx->base, pres, templ, ivci, hash, actually);
254 entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, hash, &surface->ivci, surface);
255 if (!entry) {
256 simple_mtx_unlock(&res->surface_mtx);
257 return NULL;
258 }
259
260 surface = entry->data;
261 } else {
262 surface = entry->data;
263 p_atomic_inc(&surface->base.reference.count);
264 }
265 simple_mtx_unlock(&res->surface_mtx);
266
267 return surface;
268 }
269
270 /* wrap a surface for use as a framebuffer attachment
271 * Takes ownership of surface */
272 static struct zink_ctx_surface *
wrap_surface(struct pipe_context * pctx,struct zink_surface * surface,const struct pipe_surface * templ)273 wrap_surface(struct pipe_context *pctx,
274 struct zink_surface *surface,
275 const struct pipe_surface *templ)
276 {
277 struct zink_ctx_surface *csurf = CALLOC_STRUCT(zink_ctx_surface);
278 if (!csurf) {
279 zink_surface_reference (zink_screen(pctx->screen), &surface, NULL);
280 return NULL;
281 }
282
283 csurf->base = *templ;
284 pipe_reference_init(&csurf->base.reference, 1);
285 csurf->surf = surface;
286 csurf->base.context = pctx;
287
288 return csurf;
289 }
290
291 /* this is the context hook, so only zink_ctx_surfaces will reach it */
292 static void
zink_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurface)293 zink_surface_destroy(struct pipe_context *pctx,
294 struct pipe_surface *psurface)
295 {
296 struct zink_ctx_surface *csurf = (struct zink_ctx_surface *)psurface;
297 if (csurf->needs_mutable)
298 /* this has an extra resource ref */
299 pipe_resource_reference(&csurf->base.texture, NULL);
300 zink_surface_reference(zink_screen(pctx->screen), &csurf->surf, NULL);
301 pipe_surface_release(pctx, (struct pipe_surface**)&csurf->transient);
302 FREE(csurf);
303 }
304
305 /* this the context hook that returns a zink_ctx_surface */
306 static struct pipe_surface *
zink_create_surface(struct pipe_context * pctx,struct pipe_resource * pres,const struct pipe_surface * templ)307 zink_create_surface(struct pipe_context *pctx,
308 struct pipe_resource *pres,
309 const struct pipe_surface *templ)
310 {
311 struct zink_resource *res = zink_resource(pres);
312 struct zink_screen *screen = zink_screen(pctx->screen);
313 bool is_array = templ->u.tex.last_layer != templ->u.tex.first_layer;
314 bool needs_mutable = false;
315 enum pipe_texture_target target_2d[] = {PIPE_TEXTURE_2D, PIPE_TEXTURE_2D_ARRAY};
316 if (!res->obj->dt && zink_format_needs_mutable(pres->format, templ->format)) {
317 /* mutable not set by default */
318 needs_mutable = !(res->base.b.bind & ZINK_BIND_MUTABLE);
319 /*
320 VUID-VkImageViewCreateInfo-image-07072
321 If image was created with the VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT flag and
322 format is a non-compressed format, the levelCount and layerCount members of
323 subresourceRange must both be 1
324
325 ...but this is allowed with a maintenance6 property
326 */
327 if (util_format_is_compressed(pres->format) && templ->u.tex.first_layer != templ->u.tex.last_layer &&
328 (!screen->info.have_KHR_maintenance6 || !screen->info.maint6_props.blockTexelViewCompatibleMultipleLayers))
329 return NULL;
330 }
331
332 if (!screen->threaded && needs_mutable) {
333 /* this is fine without tc */
334 needs_mutable = false;
335 zink_resource_object_init_mutable(zink_context(pctx), res);
336 }
337
338 if (!zink_get_format(screen, templ->format))
339 return NULL;
340
341 VkImageViewCreateInfo ivci = create_ivci(screen, res, templ,
342 pres->target == PIPE_TEXTURE_3D ? target_2d[is_array] : pres->target);
343
344 struct zink_surface *surface = NULL;
345 if (res->obj->dt) {
346 /* don't cache swapchain surfaces. that's weird. */
347 surface = do_create_surface(pctx, pres, templ, &ivci, 0, false);
348 if (unlikely(!surface)) {
349 mesa_loge("ZINK: failed do_create_surface!");
350 return NULL;
351 }
352
353 surface->is_swapchain = true;
354 } else if (!needs_mutable) {
355 surface = zink_get_surface(zink_context(pctx), pres, templ, &ivci);
356 if (unlikely(!surface)) {
357 mesa_loge("ZINK: failed to get non-mutable surface!");
358 return NULL;
359 }
360 }
361
362 struct zink_ctx_surface *csurf = wrap_surface(pctx, surface, needs_mutable ? templ : &surface->base); /* move ownership of surface */
363 if (!unlikely (csurf)) {
364 mesa_loge("ZINK: failed to allocate csurf!");
365 return NULL;
366 }
367
368 csurf->needs_mutable = needs_mutable;
369 if (needs_mutable) {
370 pipe_resource_reference(&csurf->base.texture, pres);
371 init_pipe_surface_info(pctx, &csurf->base, templ, pres);
372 }
373
374 if (templ->nr_samples && !screen->info.have_EXT_multisampled_render_to_single_sampled) {
375 /* transient fb attachment: not cached */
376 struct pipe_resource rtempl = *pres;
377 rtempl.nr_samples = templ->nr_samples;
378 rtempl.bind |= ZINK_BIND_TRANSIENT;
379 struct zink_resource *transient = zink_resource(pctx->screen->resource_create(pctx->screen, &rtempl));
380 if (unlikely(!transient)) {
381 mesa_loge("ZINK: failed to create transient resource!");
382 goto fail;
383 }
384
385 ivci.image = transient->obj->image;
386 struct zink_surface *tsurf = create_surface(pctx, &transient->base.b, templ, &ivci, true);
387 pipe_resource_reference((struct pipe_resource**)&transient, NULL);
388 if (unlikely(!tsurf)) {
389 mesa_loge("ZINK: failed to create transient surface!");
390 goto fail;
391 }
392
393 csurf->transient = wrap_surface(pctx, tsurf, &tsurf->base); /* move ownership of tsurf */
394 if (unlikely(!csurf->transient)) {
395 mesa_loge("ZINK: failed to wrap transient surface!");
396 goto fail;
397 }
398 }
399
400 return &csurf->base;
401 fail:
402 zink_surface_destroy(pctx, &csurf->base);
403 return NULL;
404 }
405
406 void
zink_destroy_surface(struct zink_screen * screen,struct pipe_surface * psurface)407 zink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface)
408 {
409 struct zink_surface *surface = zink_surface(psurface);
410 struct zink_resource *res = zink_resource(psurface->texture);
411 if ((!psurface->nr_samples || screen->info.have_EXT_multisampled_render_to_single_sampled) && !surface->is_swapchain) {
412 simple_mtx_lock(&res->surface_mtx);
413 if (psurface->reference.count) {
414 /* a different context got a cache hit during deletion: this surface is alive again */
415 simple_mtx_unlock(&res->surface_mtx);
416 return;
417 }
418 struct hash_entry *he = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci);
419 assert(he);
420 assert(he->data == surface);
421 _mesa_hash_table_remove(&res->surface_cache, he);
422 simple_mtx_unlock(&res->surface_mtx);
423 }
424 /* this surface is dead now */
425 simple_mtx_lock(&res->obj->view_lock);
426 /* imageviews are never destroyed directly to ensure lifetimes for in-use surfaces */
427 if (surface->is_swapchain) {
428 for (unsigned i = 0; i < surface->swapchain_size; i++)
429 util_dynarray_append(&res->obj->views, VkImageView, surface->swapchain[i]);
430 free(surface->swapchain);
431 } else
432 util_dynarray_append(&res->obj->views, VkImageView, surface->image_view);
433 simple_mtx_unlock(&res->obj->view_lock);
434 pipe_resource_reference(&psurface->texture, NULL);
435 FREE(surface);
436 }
437
438 /* this is called when a surface is rebound for mutable/storage use */
439 bool
zink_rebind_surface(struct zink_context * ctx,struct pipe_surface ** psurface)440 zink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface)
441 {
442 struct zink_surface *surface = zink_surface(*psurface);
443 struct zink_resource *res = zink_resource((*psurface)->texture);
444 struct zink_screen *screen = zink_screen(ctx->base.screen);
445 if (surface->obj == res->obj)
446 return false;
447 assert(!res->obj->dt);
448 VkImageViewCreateInfo ivci = surface->ivci;
449 ivci.image = res->obj->image;
450 uint32_t hash = hash_ivci(&ivci);
451
452 simple_mtx_lock(&res->surface_mtx);
453 struct hash_entry *new_entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, &ivci);
454 if (new_entry) {
455 /* reuse existing surface; old one will be cleaned up naturally */
456 struct zink_surface *new_surface = new_entry->data;
457 simple_mtx_unlock(&res->surface_mtx);
458 zink_surface_reference(screen, (struct zink_surface**)psurface, new_surface);
459 return true;
460 }
461 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci);
462 assert(entry);
463 _mesa_hash_table_remove(&res->surface_cache, entry);
464 VkImageView image_view;
465 apply_view_usage_for_format(screen, res, surface, surface->base.format, &ivci);
466 VkResult result = VKSCR(CreateImageView)(screen->dev, &ivci, NULL, &image_view);
467 if (result != VK_SUCCESS) {
468 mesa_loge("ZINK: failed to create new imageview (%s)", vk_Result_to_str(result));
469 simple_mtx_unlock(&res->surface_mtx);
470 return false;
471 }
472 surface->hash = hash;
473 surface->ivci = ivci;
474 entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci, surface);
475 assert(entry);
476 simple_mtx_lock(&res->obj->view_lock);
477 util_dynarray_append(&res->obj->views, VkImageView, surface->image_view);
478 simple_mtx_unlock(&res->obj->view_lock);
479 surface->image_view = image_view;
480 surface->obj = zink_resource(surface->base.texture)->obj;
481 /* update for imageless fb */
482 surface->info.flags = res->obj->vkflags;
483 surface->info.usage = res->obj->vkusage;
484 simple_mtx_unlock(&res->surface_mtx);
485 return true;
486 }
487
488 /* dummy surfaces are used for null framebuffer/descriptors */
489 struct pipe_surface *
zink_surface_create_null(struct zink_context * ctx,enum pipe_texture_target target,unsigned width,unsigned height,unsigned samples)490 zink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples)
491 {
492 struct pipe_surface surf_templ = {0};
493
494 struct pipe_resource *pres;
495 struct pipe_resource templ = {0};
496 templ.width0 = width;
497 templ.height0 = height;
498 templ.depth0 = 1;
499 templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
500 templ.target = target;
501 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
502 if (samples < 2)
503 templ.bind |= PIPE_BIND_SHADER_IMAGE;
504 templ.nr_samples = samples;
505
506 pres = ctx->base.screen->resource_create(ctx->base.screen, &templ);
507 if (!pres)
508 return NULL;
509
510 surf_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
511 surf_templ.nr_samples = 0;
512 struct pipe_surface *psurf = ctx->base.create_surface(&ctx->base, pres, &surf_templ);
513 pipe_resource_reference(&pres, NULL);
514 return psurf;
515 }
516
517 void
zink_context_surface_init(struct pipe_context * context)518 zink_context_surface_init(struct pipe_context *context)
519 {
520 context->create_surface = zink_create_surface;
521 context->surface_destroy = zink_surface_destroy;
522 }
523
524 /* must be called before a swapchain image is used to ensure correct imageview is used */
525 void
zink_surface_swapchain_update(struct zink_context * ctx,struct zink_surface * surface)526 zink_surface_swapchain_update(struct zink_context *ctx, struct zink_surface *surface)
527 {
528 struct zink_screen *screen = zink_screen(ctx->base.screen);
529 struct zink_resource *res = zink_resource(surface->base.texture);
530 struct kopper_displaytarget *cdt = res->obj->dt;
531 if (!cdt)
532 return; //dead swapchain
533 if (cdt->swapchain != surface->dt_swapchain) {
534 /* new swapchain: clear out previous swapchain imageviews/array and setup a new one;
535 * old views will be pruned normally in zink_batch or on object destruction
536 */
537 simple_mtx_lock(&res->obj->view_lock);
538 for (unsigned i = 0; i < surface->swapchain_size; i++)
539 util_dynarray_append(&res->obj->views, VkImageView, surface->swapchain[i]);
540 simple_mtx_unlock(&res->obj->view_lock);
541 free(surface->swapchain);
542 surface->swapchain_size = cdt->swapchain->num_images;
543 surface->swapchain = calloc(surface->swapchain_size, sizeof(VkImageView));
544 if (!surface->swapchain) {
545 mesa_loge("ZINK: failed to allocate surface->swapchain!");
546 return;
547 }
548 surface->base.width = res->base.b.width0;
549 surface->base.height = res->base.b.height0;
550 init_surface_info(screen, surface, res, &surface->ivci);
551 surface->dt_swapchain = cdt->swapchain;
552 }
553 if (!surface->swapchain[res->obj->dt_idx]) {
554 /* no current swapchain imageview exists: create it */
555 assert(res->obj->image && cdt->swapchain->images[res->obj->dt_idx].image == res->obj->image);
556 surface->ivci.image = res->obj->image;
557 assert(surface->ivci.image);
558 VKSCR(CreateImageView)(screen->dev, &surface->ivci, NULL, &surface->swapchain[res->obj->dt_idx]);
559 }
560 /* the current swapchain imageview is now the view for the current swapchain image */
561 surface->image_view = surface->swapchain[res->obj->dt_idx];
562 }
563