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_BATCH_H
25 #define ZINK_BATCH_H
26 
27 #include <vulkan/vulkan.h>
28 
29 #include "util/list.h"
30 #include "util/set.h"
31 #include "util/u_dynarray.h"
32 
33 #include "zink_fence.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 struct pipe_reference;
40 
41 struct zink_buffer_view;
42 struct zink_context;
43 struct zink_descriptor_set;
44 struct zink_image_view;
45 struct zink_program;
46 struct zink_render_pass;
47 struct zink_resource;
48 struct zink_sampler_view;
49 struct zink_surface;
50 
51 struct zink_batch_usage {
52    uint32_t usage;
53    cnd_t flush;
54    mtx_t mtx;
55    bool unflushed;
56 };
57 
58 /* not real api don't use */
59 bool
60 batch_ptr_add_usage(struct zink_batch *batch, struct set *s, void *ptr);
61 
62 struct zink_batch_state {
63    struct zink_fence fence;
64    struct zink_batch_state *next;
65 
66    struct zink_batch_usage usage;
67    struct zink_context *ctx;
68    VkCommandPool cmdpool;
69    VkCommandBuffer cmdbuf;
70    VkCommandBuffer barrier_cmdbuf;
71 
72    VkQueue queue; //duplicated from batch for threading
73    VkSemaphore sem;
74 
75    struct util_queue_fence flush_completed;
76 
77    struct pipe_resource *flush_res;
78 
79    struct set *programs;
80 
81    struct set *resources;
82    struct set *surfaces;
83    struct set *bufferviews;
84 
85    struct util_dynarray unref_resources;
86    struct util_dynarray bindless_releases[2];
87 
88    struct util_dynarray persistent_resources;
89    struct util_dynarray zombie_samplers;
90    struct util_dynarray dead_framebuffers;
91 
92    struct set *active_queries; /* zink_query objects which were active at some point in this batch */
93 
94    struct zink_batch_descriptor_data *dd;
95 
96    VkDeviceSize resource_size;
97 
98     /* this is a monotonic int used to disambiguate internal fences from their tc fence references */
99    unsigned submit_count;
100 
101    bool is_device_lost;
102    bool have_timelines;
103    bool has_barriers;
104    bool scanout_flush;
105 };
106 
107 struct zink_batch {
108    struct zink_batch_state *state;
109 
110    struct zink_batch_usage *last_batch_usage;
111 
112    unsigned work_count;
113 
114    bool has_work;
115    bool last_was_compute;
116    bool in_rp; //renderpass is currently active
117 };
118 
119 
120 static inline struct zink_batch_state *
zink_batch_state(struct zink_fence * fence)121 zink_batch_state(struct zink_fence *fence)
122 {
123    return (struct zink_batch_state *)fence;
124 }
125 
126 void
127 zink_reset_batch_state(struct zink_context *ctx, struct zink_batch_state *bs);
128 
129 void
130 zink_clear_batch_state(struct zink_context *ctx, struct zink_batch_state *bs);
131 
132 void
133 zink_batch_reset_all(struct zink_context *ctx);
134 
135 void
136 zink_batch_state_destroy(struct zink_screen *screen, struct zink_batch_state *bs);
137 
138 void
139 zink_batch_state_clear_resources(struct zink_screen *screen, struct zink_batch_state *bs);
140 
141 void
142 zink_reset_batch(struct zink_context *ctx, struct zink_batch *batch);
143 void
144 zink_start_batch(struct zink_context *ctx, struct zink_batch *batch);
145 
146 void
147 zink_end_batch(struct zink_context *ctx, struct zink_batch *batch);
148 
149 void
150 zink_batch_resource_usage_set(struct zink_batch *batch, struct zink_resource *res, bool write);
151 
152 void
153 zink_batch_reference_resource_rw(struct zink_batch *batch,
154                                  struct zink_resource *res,
155                                  bool write);
156 void
157 zink_batch_reference_resource(struct zink_batch *batch, struct zink_resource *res);
158 
159 void
160 zink_batch_reference_resource_move(struct zink_batch *batch, struct zink_resource *res);
161 
162 void
163 zink_batch_reference_sampler_view(struct zink_batch *batch,
164                                   struct zink_sampler_view *sv);
165 
166 void
167 zink_batch_reference_program(struct zink_batch *batch,
168                              struct zink_program *pg);
169 
170 void
171 zink_batch_reference_image_view(struct zink_batch *batch,
172                                 struct zink_image_view *image_view);
173 
174 void
175 zink_batch_reference_bufferview(struct zink_batch *batch, struct zink_buffer_view *buffer_view);
176 void
177 zink_batch_reference_surface(struct zink_batch *batch, struct zink_surface *surface);
178 
179 void
180 debug_describe_zink_batch_state(char *buf, const struct zink_batch_state *ptr);
181 
182 static inline bool
zink_batch_usage_is_unflushed(const struct zink_batch_usage * u)183 zink_batch_usage_is_unflushed(const struct zink_batch_usage *u)
184 {
185    return u && u->unflushed;
186 }
187 
188 static inline void
zink_batch_usage_unset(struct zink_batch_usage ** u,struct zink_batch_state * bs)189 zink_batch_usage_unset(struct zink_batch_usage **u, struct zink_batch_state *bs)
190 {
191    (void)p_atomic_cmpxchg((uintptr_t *)u, (uintptr_t)&bs->usage, (uintptr_t)NULL);
192 }
193 
194 static inline void
zink_batch_usage_set(struct zink_batch_usage ** u,struct zink_batch_state * bs)195 zink_batch_usage_set(struct zink_batch_usage **u, struct zink_batch_state *bs)
196 {
197    *u = &bs->usage;
198 }
199 
200 static inline bool
zink_batch_usage_matches(const struct zink_batch_usage * u,const struct zink_batch_state * bs)201 zink_batch_usage_matches(const struct zink_batch_usage *u, const struct zink_batch_state *bs)
202 {
203    return u == &bs->usage;
204 }
205 
206 static inline bool
zink_batch_usage_exists(const struct zink_batch_usage * u)207 zink_batch_usage_exists(const struct zink_batch_usage *u)
208 {
209    return u && (u->usage || u->unflushed);
210 }
211 
212 bool
213 zink_screen_usage_check_completion(struct zink_screen *screen, const struct zink_batch_usage *u);
214 
215 bool
216 zink_batch_usage_check_completion(struct zink_context *ctx, const struct zink_batch_usage *u);
217 
218 void
219 zink_batch_usage_wait(struct zink_context *ctx, struct zink_batch_usage *u);
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 
225 #endif
226