17ec681f3Smrg/*
27ec681f3Smrg * Copyright 2018 Collabora Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
87ec681f3Smrg * license, and/or sell copies of the Software, and to permit persons to whom
97ec681f3Smrg * the Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
197ec681f3Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
207ec681f3Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
217ec681f3Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#ifndef ZINK_BATCH_H
257ec681f3Smrg#define ZINK_BATCH_H
267ec681f3Smrg
277ec681f3Smrg#include <vulkan/vulkan.h>
287ec681f3Smrg
297ec681f3Smrg#include "util/list.h"
307ec681f3Smrg#include "util/set.h"
317ec681f3Smrg#include "util/u_dynarray.h"
327ec681f3Smrg
337ec681f3Smrg#include "zink_fence.h"
347ec681f3Smrg
357ec681f3Smrg#ifdef __cplusplus
367ec681f3Smrgextern "C" {
377ec681f3Smrg#endif
387ec681f3Smrg
397ec681f3Smrgstruct pipe_reference;
407ec681f3Smrg
417ec681f3Smrgstruct zink_buffer_view;
427ec681f3Smrgstruct zink_context;
437ec681f3Smrgstruct zink_descriptor_set;
447ec681f3Smrgstruct zink_image_view;
457ec681f3Smrgstruct zink_program;
467ec681f3Smrgstruct zink_render_pass;
477ec681f3Smrgstruct zink_resource;
487ec681f3Smrgstruct zink_sampler_view;
497ec681f3Smrgstruct zink_surface;
507ec681f3Smrg
517ec681f3Smrgstruct zink_batch_usage {
527ec681f3Smrg   uint32_t usage;
537ec681f3Smrg   cnd_t flush;
547ec681f3Smrg   mtx_t mtx;
557ec681f3Smrg   bool unflushed;
567ec681f3Smrg};
577ec681f3Smrg
587ec681f3Smrg/* not real api don't use */
597ec681f3Smrgbool
607ec681f3Smrgbatch_ptr_add_usage(struct zink_batch *batch, struct set *s, void *ptr);
617ec681f3Smrg
627ec681f3Smrgstruct zink_batch_state {
637ec681f3Smrg   struct zink_fence fence;
647ec681f3Smrg   struct zink_batch_state *next;
657ec681f3Smrg
667ec681f3Smrg   struct zink_batch_usage usage;
677ec681f3Smrg   struct zink_context *ctx;
687ec681f3Smrg   VkCommandPool cmdpool;
697ec681f3Smrg   VkCommandBuffer cmdbuf;
707ec681f3Smrg   VkCommandBuffer barrier_cmdbuf;
717ec681f3Smrg
727ec681f3Smrg   VkQueue queue; //duplicated from batch for threading
737ec681f3Smrg   VkSemaphore sem;
747ec681f3Smrg
757ec681f3Smrg   struct util_queue_fence flush_completed;
767ec681f3Smrg
777ec681f3Smrg   struct pipe_resource *flush_res;
787ec681f3Smrg
797ec681f3Smrg   struct set *programs;
807ec681f3Smrg
817ec681f3Smrg   struct set *resources;
827ec681f3Smrg   struct set *surfaces;
837ec681f3Smrg   struct set *bufferviews;
847ec681f3Smrg
857ec681f3Smrg   struct util_dynarray unref_resources;
867ec681f3Smrg   struct util_dynarray bindless_releases[2];
877ec681f3Smrg
887ec681f3Smrg   struct util_dynarray persistent_resources;
897ec681f3Smrg   struct util_dynarray zombie_samplers;
907ec681f3Smrg   struct util_dynarray dead_framebuffers;
917ec681f3Smrg
927ec681f3Smrg   struct set *active_queries; /* zink_query objects which were active at some point in this batch */
937ec681f3Smrg
947ec681f3Smrg   struct zink_batch_descriptor_data *dd;
957ec681f3Smrg
967ec681f3Smrg   VkDeviceSize resource_size;
977ec681f3Smrg
987ec681f3Smrg    /* this is a monotonic int used to disambiguate internal fences from their tc fence references */
997ec681f3Smrg   unsigned submit_count;
1007ec681f3Smrg
1017ec681f3Smrg   bool is_device_lost;
1027ec681f3Smrg   bool have_timelines;
1037ec681f3Smrg   bool has_barriers;
1047ec681f3Smrg   bool scanout_flush;
1057ec681f3Smrg};
1067ec681f3Smrg
1077ec681f3Smrgstruct zink_batch {
1087ec681f3Smrg   struct zink_batch_state *state;
1097ec681f3Smrg
1107ec681f3Smrg   struct zink_batch_usage *last_batch_usage;
1117ec681f3Smrg
1127ec681f3Smrg   unsigned work_count;
1137ec681f3Smrg
1147ec681f3Smrg   bool has_work;
1157ec681f3Smrg   bool last_was_compute;
1167ec681f3Smrg   bool in_rp; //renderpass is currently active
1177ec681f3Smrg};
1187ec681f3Smrg
1197ec681f3Smrg
1207ec681f3Smrgstatic inline struct zink_batch_state *
1217ec681f3Smrgzink_batch_state(struct zink_fence *fence)
1227ec681f3Smrg{
1237ec681f3Smrg   return (struct zink_batch_state *)fence;
1247ec681f3Smrg}
1257ec681f3Smrg
1267ec681f3Smrgvoid
1277ec681f3Smrgzink_reset_batch_state(struct zink_context *ctx, struct zink_batch_state *bs);
1287ec681f3Smrg
1297ec681f3Smrgvoid
1307ec681f3Smrgzink_clear_batch_state(struct zink_context *ctx, struct zink_batch_state *bs);
1317ec681f3Smrg
1327ec681f3Smrgvoid
1337ec681f3Smrgzink_batch_reset_all(struct zink_context *ctx);
1347ec681f3Smrg
1357ec681f3Smrgvoid
1367ec681f3Smrgzink_batch_state_destroy(struct zink_screen *screen, struct zink_batch_state *bs);
1377ec681f3Smrg
1387ec681f3Smrgvoid
1397ec681f3Smrgzink_batch_state_clear_resources(struct zink_screen *screen, struct zink_batch_state *bs);
1407ec681f3Smrg
1417ec681f3Smrgvoid
1427ec681f3Smrgzink_reset_batch(struct zink_context *ctx, struct zink_batch *batch);
1437ec681f3Smrgvoid
1447ec681f3Smrgzink_start_batch(struct zink_context *ctx, struct zink_batch *batch);
1457ec681f3Smrg
1467ec681f3Smrgvoid
1477ec681f3Smrgzink_end_batch(struct zink_context *ctx, struct zink_batch *batch);
1487ec681f3Smrg
1497ec681f3Smrgvoid
1507ec681f3Smrgzink_batch_resource_usage_set(struct zink_batch *batch, struct zink_resource *res, bool write);
1517ec681f3Smrg
1527ec681f3Smrgvoid
1537ec681f3Smrgzink_batch_reference_resource_rw(struct zink_batch *batch,
1547ec681f3Smrg                                 struct zink_resource *res,
1557ec681f3Smrg                                 bool write);
1567ec681f3Smrgvoid
1577ec681f3Smrgzink_batch_reference_resource(struct zink_batch *batch, struct zink_resource *res);
1587ec681f3Smrg
1597ec681f3Smrgvoid
1607ec681f3Smrgzink_batch_reference_resource_move(struct zink_batch *batch, struct zink_resource *res);
1617ec681f3Smrg
1627ec681f3Smrgvoid
1637ec681f3Smrgzink_batch_reference_sampler_view(struct zink_batch *batch,
1647ec681f3Smrg                                  struct zink_sampler_view *sv);
1657ec681f3Smrg
1667ec681f3Smrgvoid
1677ec681f3Smrgzink_batch_reference_program(struct zink_batch *batch,
1687ec681f3Smrg                             struct zink_program *pg);
1697ec681f3Smrg
1707ec681f3Smrgvoid
1717ec681f3Smrgzink_batch_reference_image_view(struct zink_batch *batch,
1727ec681f3Smrg                                struct zink_image_view *image_view);
1737ec681f3Smrg
1747ec681f3Smrgvoid
1757ec681f3Smrgzink_batch_reference_bufferview(struct zink_batch *batch, struct zink_buffer_view *buffer_view);
1767ec681f3Smrgvoid
1777ec681f3Smrgzink_batch_reference_surface(struct zink_batch *batch, struct zink_surface *surface);
1787ec681f3Smrg
1797ec681f3Smrgvoid
1807ec681f3Smrgdebug_describe_zink_batch_state(char *buf, const struct zink_batch_state *ptr);
1817ec681f3Smrg
1827ec681f3Smrgstatic inline bool
1837ec681f3Smrgzink_batch_usage_is_unflushed(const struct zink_batch_usage *u)
1847ec681f3Smrg{
1857ec681f3Smrg   return u && u->unflushed;
1867ec681f3Smrg}
1877ec681f3Smrg
1887ec681f3Smrgstatic inline void
1897ec681f3Smrgzink_batch_usage_unset(struct zink_batch_usage **u, struct zink_batch_state *bs)
1907ec681f3Smrg{
1917ec681f3Smrg   (void)p_atomic_cmpxchg((uintptr_t *)u, (uintptr_t)&bs->usage, (uintptr_t)NULL);
1927ec681f3Smrg}
1937ec681f3Smrg
1947ec681f3Smrgstatic inline void
1957ec681f3Smrgzink_batch_usage_set(struct zink_batch_usage **u, struct zink_batch_state *bs)
1967ec681f3Smrg{
1977ec681f3Smrg   *u = &bs->usage;
1987ec681f3Smrg}
1997ec681f3Smrg
2007ec681f3Smrgstatic inline bool
2017ec681f3Smrgzink_batch_usage_matches(const struct zink_batch_usage *u, const struct zink_batch_state *bs)
2027ec681f3Smrg{
2037ec681f3Smrg   return u == &bs->usage;
2047ec681f3Smrg}
2057ec681f3Smrg
2067ec681f3Smrgstatic inline bool
2077ec681f3Smrgzink_batch_usage_exists(const struct zink_batch_usage *u)
2087ec681f3Smrg{
2097ec681f3Smrg   return u && (u->usage || u->unflushed);
2107ec681f3Smrg}
2117ec681f3Smrg
2127ec681f3Smrgbool
2137ec681f3Smrgzink_screen_usage_check_completion(struct zink_screen *screen, const struct zink_batch_usage *u);
2147ec681f3Smrg
2157ec681f3Smrgbool
2167ec681f3Smrgzink_batch_usage_check_completion(struct zink_context *ctx, const struct zink_batch_usage *u);
2177ec681f3Smrg
2187ec681f3Smrgvoid
2197ec681f3Smrgzink_batch_usage_wait(struct zink_context *ctx, struct zink_batch_usage *u);
2207ec681f3Smrg
2217ec681f3Smrg#ifdef __cplusplus
2227ec681f3Smrg}
2237ec681f3Smrg#endif
2247ec681f3Smrg
2257ec681f3Smrg#endif
226