2 * Copyright 2018 Collabora Ltd.
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:
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
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.
24 #include "zink_context.h"
25 #include "zink_resource.h"
26 #include "zink_screen.h"
27 #include "zink_surface.h"
29 #include "util/format/u_format.h"
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
33 static struct pipe_surface
*
34 zink_create_surface(struct pipe_context
*pctx
,
35 struct pipe_resource
*pres
,
36 const struct pipe_surface
*templ
)
38 struct zink_screen
*screen
= zink_screen(pctx
->screen
);
39 unsigned int level
= templ
->u
.tex
.level
;
41 struct zink_surface
*surface
= CALLOC_STRUCT(zink_surface
);
45 pipe_resource_reference(&surface
->base
.texture
, pres
);
46 pipe_reference_init(&surface
->base
.reference
, 1);
47 surface
->base
.context
= pctx
;
48 surface
->base
.format
= templ
->format
;
49 surface
->base
.width
= u_minify(pres
->width0
, level
);
50 surface
->base
.height
= u_minify(pres
->height0
, level
);
51 surface
->base
.nr_samples
= templ
->nr_samples
;
52 surface
->base
.u
.tex
.level
= level
;
53 surface
->base
.u
.tex
.first_layer
= templ
->u
.tex
.first_layer
;
54 surface
->base
.u
.tex
.last_layer
= templ
->u
.tex
.last_layer
;
56 struct zink_resource
*res
= zink_resource(pres
);
58 VkImageViewCreateInfo ivci
= {};
59 ivci
.sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
;
60 ivci
.image
= res
->image
;
62 switch (pres
->target
) {
64 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_1D
;
67 case PIPE_TEXTURE_1D_ARRAY
:
68 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_1D_ARRAY
;
72 case PIPE_TEXTURE_RECT
:
73 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_2D
;
76 case PIPE_TEXTURE_2D_ARRAY
:
77 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
80 case PIPE_TEXTURE_CUBE
:
81 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_CUBE
;
84 case PIPE_TEXTURE_CUBE_ARRAY
:
85 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
;
89 ivci
.viewType
= VK_IMAGE_VIEW_TYPE_2D
;
93 unreachable("unsupported target");
96 ivci
.format
= zink_get_format(screen
, templ
->format
);
97 assert(ivci
.format
!= VK_FORMAT_UNDEFINED
);
99 // TODO: format swizzles
100 ivci
.components
.r
= VK_COMPONENT_SWIZZLE_R
;
101 ivci
.components
.g
= VK_COMPONENT_SWIZZLE_G
;
102 ivci
.components
.b
= VK_COMPONENT_SWIZZLE_B
;
103 ivci
.components
.a
= VK_COMPONENT_SWIZZLE_A
;
105 ivci
.subresourceRange
.aspectMask
= res
->aspect
;
106 ivci
.subresourceRange
.baseMipLevel
= templ
->u
.tex
.level
;
107 ivci
.subresourceRange
.levelCount
= 1;
108 ivci
.subresourceRange
.baseArrayLayer
= templ
->u
.tex
.first_layer
;
109 ivci
.subresourceRange
.layerCount
= 1 + templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
;
111 if (pres
->target
== PIPE_TEXTURE_CUBE
||
112 pres
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
113 ivci
.subresourceRange
.layerCount
*= 6;
115 if (vkCreateImageView(screen
->dev
, &ivci
, NULL
,
116 &surface
->image_view
) != VK_SUCCESS
) {
121 return &surface
->base
;
125 zink_surface_destroy(struct pipe_context
*pctx
,
126 struct pipe_surface
*psurface
)
128 struct zink_screen
*screen
= zink_screen(pctx
->screen
);
129 struct zink_surface
*surface
= zink_surface(psurface
);
130 pipe_resource_reference(&psurface
->texture
, NULL
);
131 vkDestroyImageView(screen
->dev
, surface
->image_view
, NULL
);
136 zink_context_surface_init(struct pipe_context
*context
)
138 context
->create_surface
= zink_create_surface
;
139 context
->surface_destroy
= zink_surface_destroy
;