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 pipe_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->base;
268 }
269
270 /* wrap a surface for use as a framebuffer attachment */
271 static struct pipe_surface *
wrap_surface(struct pipe_context * pctx,const struct pipe_surface * psurf)272 wrap_surface(struct pipe_context *pctx, const struct pipe_surface *psurf)
273 {
274 struct zink_ctx_surface *csurf = CALLOC_STRUCT(zink_ctx_surface);
275 if (!csurf) {
276 mesa_loge("ZINK: failed to allocate csurf!");
277 return NULL;
278 }
279
280 csurf->base = *psurf;
281 pipe_reference_init(&csurf->base.reference, 1);
282 csurf->surf = (struct zink_surface*)psurf;
283 csurf->base.context = pctx;
284
285 return &csurf->base;
286 }
287
288 /* this the context hook that returns a zink_ctx_surface */
289 static struct pipe_surface *
zink_create_surface(struct pipe_context * pctx,struct pipe_resource * pres,const struct pipe_surface * templ)290 zink_create_surface(struct pipe_context *pctx,
291 struct pipe_resource *pres,
292 const struct pipe_surface *templ)
293 {
294 struct zink_resource *res = zink_resource(pres);
295 struct zink_screen *screen = zink_screen(pctx->screen);
296 bool is_array = templ->u.tex.last_layer != templ->u.tex.first_layer;
297 bool needs_mutable = false;
298 enum pipe_texture_target target_2d[] = {PIPE_TEXTURE_2D, PIPE_TEXTURE_2D_ARRAY};
299 if (!res->obj->dt && zink_format_needs_mutable(pres->format, templ->format)) {
300 /* mutable not set by default */
301 needs_mutable = !(res->base.b.bind & ZINK_BIND_MUTABLE);
302 /*
303 VUID-VkImageViewCreateInfo-image-07072
304 If image was created with the VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT flag and
305 format is a non-compressed format, the levelCount and layerCount members of
306 subresourceRange must both be 1
307
308 ...but this is allowed with a maintenance6 property
309 */
310 if (util_format_is_compressed(pres->format) && templ->u.tex.first_layer != templ->u.tex.last_layer &&
311 (!screen->info.have_KHR_maintenance6 || !screen->info.maint6_props.blockTexelViewCompatibleMultipleLayers))
312 return NULL;
313 }
314
315 if (!screen->threaded && needs_mutable) {
316 /* this is fine without tc */
317 needs_mutable = false;
318 zink_resource_object_init_mutable(zink_context(pctx), res);
319 }
320
321 if (!zink_get_format(screen, templ->format))
322 return NULL;
323
324 VkImageViewCreateInfo ivci = create_ivci(screen, res, templ,
325 pres->target == PIPE_TEXTURE_3D ? target_2d[is_array] : pres->target);
326
327 struct pipe_surface *psurf = NULL;
328 if (res->obj->dt) {
329 /* don't cache swapchain surfaces. that's weird. */
330 struct zink_surface *surface = do_create_surface(pctx, pres, templ, &ivci, 0, false);
331 if (surface) {
332 surface->is_swapchain = true;
333 psurf = &surface->base;
334 }
335 } else if (!needs_mutable) {
336 psurf = zink_get_surface(zink_context(pctx), pres, templ, &ivci);
337 }
338 if (!psurf && !needs_mutable)
339 return NULL;
340
341 struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)wrap_surface(pctx, needs_mutable ? templ : psurf);
342 csurf->needs_mutable = needs_mutable;
343 if (needs_mutable) {
344 csurf->surf = NULL;
345 pipe_resource_reference(&csurf->base.texture, pres);
346 init_pipe_surface_info(pctx, &csurf->base, templ, pres);
347 }
348
349 if (templ->nr_samples && !screen->info.have_EXT_multisampled_render_to_single_sampled) {
350 /* transient fb attachment: not cached */
351 struct pipe_resource rtempl = *pres;
352 rtempl.nr_samples = templ->nr_samples;
353 rtempl.bind |= ZINK_BIND_TRANSIENT;
354 struct zink_resource *transient = zink_resource(pctx->screen->resource_create(pctx->screen, &rtempl));
355 if (!transient)
356 return NULL;
357 ivci.image = transient->obj->image;
358 csurf->transient = (struct zink_ctx_surface*)wrap_surface(pctx, (struct pipe_surface*)create_surface(pctx, &transient->base.b, templ, &ivci, true));
359 if (!csurf->transient) {
360 pipe_resource_reference((struct pipe_resource**)&transient, NULL);
361 pipe_surface_release(pctx, &psurf);
362 return NULL;
363 }
364 pipe_resource_reference((struct pipe_resource**)&transient, NULL);
365 }
366
367 return &csurf->base;
368 }
369
370 void
zink_destroy_surface(struct zink_screen * screen,struct pipe_surface * psurface)371 zink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface)
372 {
373 struct zink_surface *surface = zink_surface(psurface);
374 struct zink_resource *res = zink_resource(psurface->texture);
375 if ((!psurface->nr_samples || screen->info.have_EXT_multisampled_render_to_single_sampled) && !surface->is_swapchain) {
376 simple_mtx_lock(&res->surface_mtx);
377 if (psurface->reference.count) {
378 /* a different context got a cache hit during deletion: this surface is alive again */
379 simple_mtx_unlock(&res->surface_mtx);
380 return;
381 }
382 struct hash_entry *he = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci);
383 assert(he);
384 assert(he->data == surface);
385 _mesa_hash_table_remove(&res->surface_cache, he);
386 simple_mtx_unlock(&res->surface_mtx);
387 }
388 /* this surface is dead now */
389 simple_mtx_lock(&res->obj->view_lock);
390 /* imageviews are never destroyed directly to ensure lifetimes for in-use surfaces */
391 if (surface->is_swapchain) {
392 for (unsigned i = 0; i < surface->swapchain_size; i++)
393 util_dynarray_append(&res->obj->views, VkImageView, surface->swapchain[i]);
394 free(surface->swapchain);
395 } else
396 util_dynarray_append(&res->obj->views, VkImageView, surface->image_view);
397 simple_mtx_unlock(&res->obj->view_lock);
398 pipe_resource_reference(&psurface->texture, NULL);
399 FREE(surface);
400 }
401
402 /* this is the context hook, so only zink_ctx_surfaces will reach it */
403 static void
zink_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurface)404 zink_surface_destroy(struct pipe_context *pctx,
405 struct pipe_surface *psurface)
406 {
407 struct zink_ctx_surface *csurf = (struct zink_ctx_surface *)psurface;
408 if (csurf->needs_mutable)
409 /* this has an extra resource ref */
410 pipe_resource_reference(&csurf->base.texture, NULL);
411 zink_surface_reference(zink_screen(pctx->screen), &csurf->surf, NULL);
412 pipe_surface_release(pctx, (struct pipe_surface**)&csurf->transient);
413 FREE(csurf);
414 }
415
416 /* this is called when a surface is rebound for mutable/storage use */
417 bool
zink_rebind_surface(struct zink_context * ctx,struct pipe_surface ** psurface)418 zink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface)
419 {
420 struct zink_surface *surface = zink_surface(*psurface);
421 struct zink_resource *res = zink_resource((*psurface)->texture);
422 struct zink_screen *screen = zink_screen(ctx->base.screen);
423 if (surface->obj == res->obj)
424 return false;
425 assert(!res->obj->dt);
426 VkImageViewCreateInfo ivci = surface->ivci;
427 ivci.image = res->obj->image;
428 uint32_t hash = hash_ivci(&ivci);
429
430 simple_mtx_lock(&res->surface_mtx);
431 struct hash_entry *new_entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, hash, &ivci);
432 if (new_entry) {
433 /* reuse existing surface; old one will be cleaned up naturally */
434 struct zink_surface *new_surface = new_entry->data;
435 simple_mtx_unlock(&res->surface_mtx);
436 zink_surface_reference(screen, (struct zink_surface**)psurface, new_surface);
437 return true;
438 }
439 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci);
440 assert(entry);
441 _mesa_hash_table_remove(&res->surface_cache, entry);
442 VkImageView image_view;
443 apply_view_usage_for_format(screen, res, surface, surface->base.format, &ivci);
444 VkResult result = VKSCR(CreateImageView)(screen->dev, &ivci, NULL, &image_view);
445 if (result != VK_SUCCESS) {
446 mesa_loge("ZINK: failed to create new imageview (%s)", vk_Result_to_str(result));
447 simple_mtx_unlock(&res->surface_mtx);
448 return false;
449 }
450 surface->hash = hash;
451 surface->ivci = ivci;
452 entry = _mesa_hash_table_insert_pre_hashed(&res->surface_cache, surface->hash, &surface->ivci, surface);
453 assert(entry);
454 simple_mtx_lock(&res->obj->view_lock);
455 util_dynarray_append(&res->obj->views, VkImageView, surface->image_view);
456 simple_mtx_unlock(&res->obj->view_lock);
457 surface->image_view = image_view;
458 surface->obj = zink_resource(surface->base.texture)->obj;
459 /* update for imageless fb */
460 surface->info.flags = res->obj->vkflags;
461 surface->info.usage = res->obj->vkusage;
462 simple_mtx_unlock(&res->surface_mtx);
463 return true;
464 }
465
466 /* dummy surfaces are used for null framebuffer/descriptors */
467 struct pipe_surface *
zink_surface_create_null(struct zink_context * ctx,enum pipe_texture_target target,unsigned width,unsigned height,unsigned samples)468 zink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples)
469 {
470 struct pipe_surface surf_templ = {0};
471
472 struct pipe_resource *pres;
473 struct pipe_resource templ = {0};
474 templ.width0 = width;
475 templ.height0 = height;
476 templ.depth0 = 1;
477 templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
478 templ.target = target;
479 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
480 if (samples < 2)
481 templ.bind |= PIPE_BIND_SHADER_IMAGE;
482 templ.nr_samples = samples;
483
484 pres = ctx->base.screen->resource_create(ctx->base.screen, &templ);
485 if (!pres)
486 return NULL;
487
488 surf_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
489 surf_templ.nr_samples = 0;
490 struct pipe_surface *psurf = ctx->base.create_surface(&ctx->base, pres, &surf_templ);
491 pipe_resource_reference(&pres, NULL);
492 return psurf;
493 }
494
495 void
zink_context_surface_init(struct pipe_context * context)496 zink_context_surface_init(struct pipe_context *context)
497 {
498 context->create_surface = zink_create_surface;
499 context->surface_destroy = zink_surface_destroy;
500 }
501
502 /* must be called before a swapchain image is used to ensure correct imageview is used */
503 void
zink_surface_swapchain_update(struct zink_context * ctx,struct zink_surface * surface)504 zink_surface_swapchain_update(struct zink_context *ctx, struct zink_surface *surface)
505 {
506 struct zink_screen *screen = zink_screen(ctx->base.screen);
507 struct zink_resource *res = zink_resource(surface->base.texture);
508 struct kopper_displaytarget *cdt = res->obj->dt;
509 if (!cdt)
510 return; //dead swapchain
511 if (cdt->swapchain != surface->dt_swapchain) {
512 /* new swapchain: clear out previous swapchain imageviews/array and setup a new one;
513 * old views will be pruned normally in zink_batch or on object destruction
514 */
515 simple_mtx_lock(&res->obj->view_lock);
516 for (unsigned i = 0; i < surface->swapchain_size; i++)
517 util_dynarray_append(&res->obj->views, VkImageView, surface->swapchain[i]);
518 simple_mtx_unlock(&res->obj->view_lock);
519 free(surface->swapchain);
520 surface->swapchain_size = cdt->swapchain->num_images;
521 surface->swapchain = calloc(surface->swapchain_size, sizeof(VkImageView));
522 if (!surface->swapchain) {
523 mesa_loge("ZINK: failed to allocate surface->swapchain!");
524 return;
525 }
526 surface->base.width = res->base.b.width0;
527 surface->base.height = res->base.b.height0;
528 init_surface_info(screen, surface, res, &surface->ivci);
529 surface->dt_swapchain = cdt->swapchain;
530 }
531 if (!surface->swapchain[res->obj->dt_idx]) {
532 /* no current swapchain imageview exists: create it */
533 assert(res->obj->image && cdt->swapchain->images[res->obj->dt_idx].image == res->obj->image);
534 surface->ivci.image = res->obj->image;
535 assert(surface->ivci.image);
536 VKSCR(CreateImageView)(screen->dev, &surface->ivci, NULL, &surface->swapchain[res->obj->dt_idx]);
537 }
538 /* the current swapchain imageview is now the view for the current swapchain image */
539 surface->image_view = surface->swapchain[res->obj->dt_idx];
540 }
541