1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2018 Collabora Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef ZINK_BATCH_H
25bf215546Sopenharmony_ci#define ZINK_BATCH_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <vulkan/vulkan.h>
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "util/list.h"
30bf215546Sopenharmony_ci#include "util/set.h"
31bf215546Sopenharmony_ci#include "util/u_dynarray.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "zink_fence.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#ifdef __cplusplus
36bf215546Sopenharmony_ciextern "C" {
37bf215546Sopenharmony_ci#endif
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistruct pipe_reference;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistruct zink_buffer_view;
42bf215546Sopenharmony_cistruct zink_context;
43bf215546Sopenharmony_cistruct zink_descriptor_set;
44bf215546Sopenharmony_cistruct zink_image_view;
45bf215546Sopenharmony_cistruct zink_program;
46bf215546Sopenharmony_cistruct zink_render_pass;
47bf215546Sopenharmony_cistruct zink_resource;
48bf215546Sopenharmony_cistruct zink_sampler_view;
49bf215546Sopenharmony_cistruct zink_surface;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/* zink_batch_usage concepts:
52bf215546Sopenharmony_ci * - batch "usage" is an indicator of when and how a BO was accessed
53bf215546Sopenharmony_ci * - batch "tracking" is the batch state(s) containing an extra ref for a BO
54bf215546Sopenharmony_ci *
55bf215546Sopenharmony_ci * - usage prevents a BO from being mapped while it has pending+conflicting access
56bf215546Sopenharmony_ci * - usage affects pipeline barrier generation for synchronizing reads and writes
57bf215546Sopenharmony_ci * - usage MUST be removed before context destruction to avoid crashing during BO
58bf215546Sopenharmony_ci *   reclaiming in suballocator
59bf215546Sopenharmony_ci *
60bf215546Sopenharmony_ci * - tracking prevents a BO from being destroyed early
61bf215546Sopenharmony_ci * - tracking enables usage to be pruned
62bf215546Sopenharmony_ci *
63bf215546Sopenharmony_ci *
64bf215546Sopenharmony_ci * tracking is added:
65bf215546Sopenharmony_ci * - any time a BO is used in a "one-off" operation (e.g., blit, index buffer, indirect buffer)
66bf215546Sopenharmony_ci * - any time a descriptor is unbound
67bf215546Sopenharmony_ci * - when a buffer is replaced (IFF: resource is bound as a descriptor or usage previously existed)
68bf215546Sopenharmony_ci *
69bf215546Sopenharmony_ci * tracking is removed:
70bf215546Sopenharmony_ci * - in zink_reset_batch_state()
71bf215546Sopenharmony_ci *
72bf215546Sopenharmony_ci * usage is added:
73bf215546Sopenharmony_ci * - any time a BO is used in a "one-off" operation (e.g., blit, index buffer, indirect buffer)
74bf215546Sopenharmony_ci * - any time a descriptor is bound
75bf215546Sopenharmony_ci * - any time a descriptor is unbound (IFF: usage previously existed)
76bf215546Sopenharmony_ci * - for all bound descriptors on the first draw/dispatch after a flush (zink_update_descriptor_refs)
77bf215546Sopenharmony_ci *
78bf215546Sopenharmony_ci * usage is removed:
79bf215546Sopenharmony_ci * - when tracking is removed (IFF: BO usage == tracking, i.e., this is the last batch that a BO was active on)
80bf215546Sopenharmony_ci */
81bf215546Sopenharmony_cistruct zink_batch_usage {
82bf215546Sopenharmony_ci   uint32_t usage;
83bf215546Sopenharmony_ci   cnd_t flush;
84bf215546Sopenharmony_ci   mtx_t mtx;
85bf215546Sopenharmony_ci   bool unflushed;
86bf215546Sopenharmony_ci};
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci/* not real api don't use */
89bf215546Sopenharmony_cibool
90bf215546Sopenharmony_cibatch_ptr_add_usage(struct zink_batch *batch, struct set *s, void *ptr);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistruct zink_batch_state {
93bf215546Sopenharmony_ci   struct zink_fence fence;
94bf215546Sopenharmony_ci   struct zink_batch_state *next;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   struct zink_batch_usage usage;
97bf215546Sopenharmony_ci   struct zink_context *ctx;
98bf215546Sopenharmony_ci   VkCommandPool cmdpool;
99bf215546Sopenharmony_ci   VkCommandBuffer cmdbuf;
100bf215546Sopenharmony_ci   VkCommandBuffer barrier_cmdbuf;
101bf215546Sopenharmony_ci   VkSemaphore signal_semaphore; //external signal semaphore
102bf215546Sopenharmony_ci   struct util_dynarray wait_semaphores; //external wait semaphores
103bf215546Sopenharmony_ci   struct util_dynarray wait_semaphore_stages; //external wait semaphores
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   VkSemaphore present;
106bf215546Sopenharmony_ci   struct zink_resource *swapchain;
107bf215546Sopenharmony_ci   struct util_dynarray acquires;
108bf215546Sopenharmony_ci   struct util_dynarray acquire_flags;
109bf215546Sopenharmony_ci   struct util_dynarray dead_swapchains;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   struct util_queue_fence flush_completed;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   struct set *programs;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   struct set *resources;
116bf215546Sopenharmony_ci   struct set *surfaces;
117bf215546Sopenharmony_ci   struct set *bufferviews;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   struct util_dynarray unref_resources;
120bf215546Sopenharmony_ci   struct util_dynarray bindless_releases[2];
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   struct util_dynarray persistent_resources;
123bf215546Sopenharmony_ci   struct util_dynarray zombie_samplers;
124bf215546Sopenharmony_ci   struct util_dynarray dead_framebuffers;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   struct set *active_queries; /* zink_query objects which were active at some point in this batch */
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   struct zink_batch_descriptor_data *dd;
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   VkDeviceSize resource_size;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci    /* this is a monotonic int used to disambiguate internal fences from their tc fence references */
133bf215546Sopenharmony_ci   unsigned submit_count;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   bool is_device_lost;
136bf215546Sopenharmony_ci   bool has_barriers;
137bf215546Sopenharmony_ci};
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_cistruct zink_batch {
140bf215546Sopenharmony_ci   struct zink_batch_state *state;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   struct zink_batch_usage *last_batch_usage;
143bf215546Sopenharmony_ci   struct zink_resource *swapchain;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   unsigned work_count;
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   bool has_work;
148bf215546Sopenharmony_ci   bool last_was_compute;
149bf215546Sopenharmony_ci   bool in_rp; //renderpass is currently active
150bf215546Sopenharmony_ci};
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistatic inline struct zink_batch_state *
154bf215546Sopenharmony_cizink_batch_state(struct zink_fence *fence)
155bf215546Sopenharmony_ci{
156bf215546Sopenharmony_ci   return (struct zink_batch_state *)fence;
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_civoid
160bf215546Sopenharmony_cizink_reset_batch_state(struct zink_context *ctx, struct zink_batch_state *bs);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_civoid
163bf215546Sopenharmony_cizink_clear_batch_state(struct zink_context *ctx, struct zink_batch_state *bs);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_civoid
166bf215546Sopenharmony_cizink_batch_reset_all(struct zink_context *ctx);
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_civoid
169bf215546Sopenharmony_cizink_batch_state_destroy(struct zink_screen *screen, struct zink_batch_state *bs);
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_civoid
172bf215546Sopenharmony_cizink_batch_state_clear_resources(struct zink_screen *screen, struct zink_batch_state *bs);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_civoid
175bf215546Sopenharmony_cizink_reset_batch(struct zink_context *ctx, struct zink_batch *batch);
176bf215546Sopenharmony_civoid
177bf215546Sopenharmony_cizink_start_batch(struct zink_context *ctx, struct zink_batch *batch);
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_civoid
180bf215546Sopenharmony_cizink_end_batch(struct zink_context *ctx, struct zink_batch *batch);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_civoid
183bf215546Sopenharmony_cizink_batch_add_wait_semaphore(struct zink_batch *batch, VkSemaphore sem);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_civoid
186bf215546Sopenharmony_cizink_batch_resource_usage_set(struct zink_batch *batch, struct zink_resource *res, bool write);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_civoid
189bf215546Sopenharmony_cizink_batch_reference_resource_rw(struct zink_batch *batch,
190bf215546Sopenharmony_ci                                 struct zink_resource *res,
191bf215546Sopenharmony_ci                                 bool write);
192bf215546Sopenharmony_civoid
193bf215546Sopenharmony_cizink_batch_reference_resource(struct zink_batch *batch, struct zink_resource *res);
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_civoid
196bf215546Sopenharmony_cizink_batch_reference_resource_move(struct zink_batch *batch, struct zink_resource *res);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_civoid
199bf215546Sopenharmony_cizink_batch_reference_sampler_view(struct zink_batch *batch,
200bf215546Sopenharmony_ci                                  struct zink_sampler_view *sv);
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_civoid
203bf215546Sopenharmony_cizink_batch_reference_program(struct zink_batch *batch,
204bf215546Sopenharmony_ci                             struct zink_program *pg);
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_civoid
207bf215546Sopenharmony_cizink_batch_reference_image_view(struct zink_batch *batch,
208bf215546Sopenharmony_ci                                struct zink_image_view *image_view);
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_civoid
211bf215546Sopenharmony_cizink_batch_reference_bufferview(struct zink_batch *batch, struct zink_buffer_view *buffer_view);
212bf215546Sopenharmony_civoid
213bf215546Sopenharmony_cizink_batch_reference_surface(struct zink_batch *batch, struct zink_surface *surface);
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_civoid
216bf215546Sopenharmony_cidebug_describe_zink_batch_state(char *buf, const struct zink_batch_state *ptr);
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_cistatic inline bool
219bf215546Sopenharmony_cizink_batch_usage_is_unflushed(const struct zink_batch_usage *u)
220bf215546Sopenharmony_ci{
221bf215546Sopenharmony_ci   return u && u->unflushed;
222bf215546Sopenharmony_ci}
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_cistatic inline void
225bf215546Sopenharmony_cizink_batch_usage_unset(struct zink_batch_usage **u, struct zink_batch_state *bs)
226bf215546Sopenharmony_ci{
227bf215546Sopenharmony_ci   (void)p_atomic_cmpxchg((uintptr_t *)u, (uintptr_t)&bs->usage, (uintptr_t)NULL);
228bf215546Sopenharmony_ci}
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_cistatic inline void
231bf215546Sopenharmony_cizink_batch_usage_set(struct zink_batch_usage **u, struct zink_batch_state *bs)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   *u = &bs->usage;
234bf215546Sopenharmony_ci}
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_cistatic inline bool
237bf215546Sopenharmony_cizink_batch_usage_matches(const struct zink_batch_usage *u, const struct zink_batch_state *bs)
238bf215546Sopenharmony_ci{
239bf215546Sopenharmony_ci   return u == &bs->usage;
240bf215546Sopenharmony_ci}
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_cistatic inline bool
243bf215546Sopenharmony_cizink_batch_usage_exists(const struct zink_batch_usage *u)
244bf215546Sopenharmony_ci{
245bf215546Sopenharmony_ci   return u && (u->usage || u->unflushed);
246bf215546Sopenharmony_ci}
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_cibool
249bf215546Sopenharmony_cizink_screen_usage_check_completion(struct zink_screen *screen, const struct zink_batch_usage *u);
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_cibool
252bf215546Sopenharmony_cizink_batch_usage_check_completion(struct zink_context *ctx, const struct zink_batch_usage *u);
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_civoid
255bf215546Sopenharmony_cizink_batch_usage_wait(struct zink_context *ctx, struct zink_batch_usage *u);
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci#ifdef __cplusplus
258bf215546Sopenharmony_ci}
259bf215546Sopenharmony_ci#endif
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci#endif
262