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 #ifndef ZINK_SURFACE_H
25 #define ZINK_SURFACE_H
26
27 #include "pipe/p_state.h"
28 #include "zink_batch.h"
29 #include <vulkan/vulkan.h>
30
31 struct pipe_context;
32
33 struct zink_surface_info {
34 VkImageCreateFlags flags;
35 VkImageUsageFlags usage;
36 uint32_t width;
37 uint32_t height;
38 uint32_t layerCount;
39 VkFormat format[2];
40 };
41
42 struct zink_surface {
43 struct pipe_surface base;
44 VkImageViewCreateInfo ivci;
45 struct zink_surface_info info; //TODO: union with fb refs
46 uint32_t info_hash;
47 bool is_swapchain;
48 VkImageView image_view;
49 void *dt;
50 VkImageView *swapchain;
51 unsigned swapchain_size;
52 VkImageView *old_swapchain;
53 unsigned old_swapchain_size;
54 VkImageView simage_view;//old iview after storage replacement/rebind
55 void *obj; //backing resource object
56 uint32_t hash;
57 struct zink_batch_usage *batch_uses;
58 struct zink_descriptor_refs desc_set_refs;
59 };
60
61 /* wrapper object that preserves the gallium expectation of having
62 * pipe_surface::context match the context used to create the surface
63 */
64 struct zink_ctx_surface {
65 struct pipe_surface base;
66 struct zink_surface *surf;
67 struct zink_ctx_surface *transient; //zink_ctx_surface
68 /* TODO: need replicate EXT */
69 bool transient_init;
70 };
71
72 /* use this cast for framebuffer surfaces */
73 static inline struct zink_surface *
zink_csurface(struct pipe_surface * psurface)74 zink_csurface(struct pipe_surface *psurface)
75 {
76 return psurface ? ((struct zink_ctx_surface *)psurface)->surf : NULL;
77 }
78
79 /* use this cast for checking transient framebuffer surfaces */
80 static inline struct zink_surface *
zink_transient_surface(struct pipe_surface * psurface)81 zink_transient_surface(struct pipe_surface *psurface)
82 {
83 return psurface ? ((struct zink_ctx_surface *)psurface)->transient ? ((struct zink_ctx_surface *)psurface)->transient->surf : NULL : NULL;
84 }
85
86 /* use this cast for internal surfaces */
87 static inline struct zink_surface *
zink_surface(struct pipe_surface * psurface)88 zink_surface(struct pipe_surface *psurface)
89 {
90 return (struct zink_surface *)psurface;
91 }
92
93 void
94 zink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface);
95
96 static inline void
zink_surface_reference(struct zink_screen * screen,struct zink_surface ** dst,struct zink_surface * src)97 zink_surface_reference(struct zink_screen *screen, struct zink_surface **dst, struct zink_surface *src)
98 {
99 struct zink_surface *old_dst = *dst;
100
101 if (pipe_reference_described(old_dst ? &old_dst->base.reference : NULL,
102 src ? &src->base.reference : NULL,
103 (debug_reference_descriptor)
104 debug_describe_surface))
105 zink_destroy_surface(screen, &old_dst->base);
106 *dst = src;
107 }
108
109 void
110 zink_context_surface_init(struct pipe_context *context);
111
112 VkImageViewCreateInfo
113 create_ivci(struct zink_screen *screen,
114 struct zink_resource *res,
115 const struct pipe_surface *templ,
116 enum pipe_texture_target target);
117
118 struct pipe_surface *
119 zink_get_surface(struct zink_context *ctx,
120 struct pipe_resource *pres,
121 const struct pipe_surface *templ,
122 VkImageViewCreateInfo *ivci);
123
124 static inline VkImageViewType
zink_surface_clamp_viewtype(VkImageViewType viewType,unsigned first_layer,unsigned last_layer,unsigned array_size)125 zink_surface_clamp_viewtype(VkImageViewType viewType, unsigned first_layer, unsigned last_layer, unsigned array_size)
126 {
127 unsigned layerCount = 1 + last_layer - first_layer;
128 if (viewType == VK_IMAGE_VIEW_TYPE_CUBE || viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
129 if (first_layer == last_layer)
130 return VK_IMAGE_VIEW_TYPE_2D;
131 if (layerCount % 6 != 0 && (first_layer || layerCount != array_size))
132 return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
133 }
134 return viewType;
135 }
136
137 bool
138 zink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface);
139
140 static inline bool
zink_rebind_ctx_surface(struct zink_context * ctx,struct pipe_surface ** psurface)141 zink_rebind_ctx_surface(struct zink_context *ctx, struct pipe_surface **psurface)
142 {
143 struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)*psurface;
144 return zink_rebind_surface(ctx, (struct pipe_surface**)&csurf->surf);
145 }
146
147 struct pipe_surface *
148 zink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples);
149
150 void
151 zink_surface_swapchain_update(struct zink_context *ctx, struct zink_surface *surface);
152 #endif
153