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;
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    VkImageView image_view;
48    VkImageView simage_view;//old iview after storage replacement/rebind
49    void *obj; //backing resource object
50    uint32_t hash;
51    struct zink_batch_usage *batch_uses;
52    struct util_dynarray framebuffer_refs;
53    struct zink_descriptor_refs desc_set_refs;
54 };
55 
56 /* wrapper object that preserves the gallium expectation of having
57  * pipe_surface::context match the context used to create the surface
58  */
59 struct zink_ctx_surface {
60    struct pipe_surface base;
61    struct zink_surface *surf;
62    struct zink_ctx_surface *transient; //zink_ctx_surface
63    /* TODO: need replicate EXT */
64    bool transient_init;
65 };
66 
67 /* use this cast for framebuffer surfaces */
68 static inline struct zink_surface *
zink_csurface(struct pipe_surface * psurface)69 zink_csurface(struct pipe_surface *psurface)
70 {
71    return psurface ? ((struct zink_ctx_surface *)psurface)->surf : NULL;
72 }
73 
74 /* use this cast for checking transient framebuffer surfaces */
75 static inline struct zink_surface *
zink_transient_surface(struct pipe_surface * psurface)76 zink_transient_surface(struct pipe_surface *psurface)
77 {
78    return psurface ? ((struct zink_ctx_surface *)psurface)->transient ? ((struct zink_ctx_surface *)psurface)->transient->surf : NULL : NULL;
79 }
80 
81 /* use this cast for internal surfaces */
82 static inline struct zink_surface *
zink_surface(struct pipe_surface * psurface)83 zink_surface(struct pipe_surface *psurface)
84 {
85    return (struct zink_surface *)psurface;
86 }
87 
88 void
89 zink_destroy_surface(struct zink_screen *screen, struct pipe_surface *psurface);
90 
91 static inline void
zink_surface_reference(struct zink_screen * screen,struct zink_surface ** dst,struct zink_surface * src)92 zink_surface_reference(struct zink_screen *screen, struct zink_surface **dst, struct zink_surface *src)
93 {
94    struct zink_surface *old_dst = *dst;
95 
96    if (pipe_reference_described(old_dst ? &old_dst->base.reference : NULL,
97                                 src ? &src->base.reference : NULL,
98                                 (debug_reference_descriptor)
99                                 debug_describe_surface))
100       zink_destroy_surface(screen, &old_dst->base);
101    *dst = src;
102 }
103 
104 void
105 zink_context_surface_init(struct pipe_context *context);
106 
107 VkImageViewCreateInfo
108 create_ivci(struct zink_screen *screen,
109             struct zink_resource *res,
110             const struct pipe_surface *templ,
111             enum pipe_texture_target target);
112 
113 struct pipe_surface *
114 zink_get_surface(struct zink_context *ctx,
115             struct pipe_resource *pres,
116             const struct pipe_surface *templ,
117             VkImageViewCreateInfo *ivci);
118 
119 static inline VkImageViewType
zink_surface_clamp_viewtype(VkImageViewType viewType,unsigned first_layer,unsigned last_layer,unsigned array_size)120 zink_surface_clamp_viewtype(VkImageViewType viewType, unsigned first_layer, unsigned last_layer, unsigned array_size)
121 {
122    unsigned layerCount = 1 + last_layer - first_layer;
123    if (viewType == VK_IMAGE_VIEW_TYPE_CUBE || viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
124       if (first_layer == last_layer)
125          return VK_IMAGE_VIEW_TYPE_2D;
126       if (layerCount % 6 != 0 && (first_layer || layerCount != array_size))
127          return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
128    }
129    return viewType;
130 }
131 
132 bool
133 zink_rebind_surface(struct zink_context *ctx, struct pipe_surface **psurface);
134 
135 static inline bool
zink_rebind_ctx_surface(struct zink_context * ctx,struct pipe_surface ** psurface)136 zink_rebind_ctx_surface(struct zink_context *ctx, struct pipe_surface **psurface)
137 {
138    struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)*psurface;
139    return zink_rebind_surface(ctx, (struct pipe_surface**)&csurf->surf);
140 }
141 
142 struct pipe_surface *
143 zink_surface_create_null(struct zink_context *ctx, enum pipe_texture_target target, unsigned width, unsigned height, unsigned samples);
144 #endif
145