• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "zink_batch.h"
25 #include "zink_fence.h"
26 
27 #include "zink_query.h"
28 #include "zink_resource.h"
29 #include "zink_screen.h"
30 
31 #include "util/set.h"
32 #include "util/u_memory.h"
33 
34 static void
destroy_fence(struct zink_screen * screen,struct zink_fence * fence)35 destroy_fence(struct zink_screen *screen, struct zink_fence *fence)
36 {
37    if (fence->fence)
38       vkDestroyFence(screen->dev, fence->fence, NULL);
39    util_dynarray_fini(&fence->resources);
40    FREE(fence);
41 }
42 
43 struct zink_fence *
zink_create_fence(struct pipe_screen * pscreen,struct zink_batch * batch)44 zink_create_fence(struct pipe_screen *pscreen, struct zink_batch *batch)
45 {
46    struct zink_screen *screen = zink_screen(pscreen);
47 
48    VkFenceCreateInfo fci = {};
49    fci.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
50 
51    struct zink_fence *ret = CALLOC_STRUCT(zink_fence);
52    if (!ret) {
53       debug_printf("CALLOC_STRUCT failed\n");
54       return NULL;
55    }
56 
57    if (vkCreateFence(screen->dev, &fci, NULL, &ret->fence) != VK_SUCCESS) {
58       debug_printf("vkCreateFence failed\n");
59       goto fail;
60    }
61    ret->active_queries = batch->active_queries;
62    batch->active_queries = NULL;
63 
64    ret->batch_id = batch->batch_id;
65    util_dynarray_init(&ret->resources, NULL);
66    set_foreach(batch->resources, entry) {
67       /* the fence needs its own reference to ensure it can safely access lifetime-dependent
68        * resource members
69        */
70       struct pipe_resource *r = NULL, *pres = (struct pipe_resource *)entry->key;
71       pipe_resource_reference(&r, pres);
72       util_dynarray_append(&ret->resources, struct pipe_resource*, pres);
73    }
74 
75    pipe_reference_init(&ret->reference, 1);
76    return ret;
77 
78 fail:
79    destroy_fence(screen, ret);
80    return NULL;
81 }
82 
83 void
zink_fence_reference(struct zink_screen * screen,struct zink_fence ** ptr,struct zink_fence * fence)84 zink_fence_reference(struct zink_screen *screen,
85                      struct zink_fence **ptr,
86                      struct zink_fence *fence)
87 {
88    if (pipe_reference(&(*ptr)->reference, &fence->reference))
89       destroy_fence(screen, *ptr);
90 
91    *ptr = fence;
92 }
93 
94 static void
fence_reference(struct pipe_screen * pscreen,struct pipe_fence_handle ** pptr,struct pipe_fence_handle * pfence)95 fence_reference(struct pipe_screen *pscreen,
96                 struct pipe_fence_handle **pptr,
97                 struct pipe_fence_handle *pfence)
98 {
99    zink_fence_reference(zink_screen(pscreen), (struct zink_fence **)pptr,
100                         zink_fence(pfence));
101 }
102 
103 static inline void
fence_remove_resource_access(struct zink_fence * fence,struct zink_resource * res)104 fence_remove_resource_access(struct zink_fence *fence, struct zink_resource *res)
105 {
106    p_atomic_set(&res->batch_uses[fence->batch_id], 0);
107 }
108 
109 bool
zink_fence_finish(struct zink_screen * screen,struct zink_fence * fence,uint64_t timeout_ns)110 zink_fence_finish(struct zink_screen *screen, struct zink_fence *fence,
111                   uint64_t timeout_ns)
112 {
113    bool success = vkWaitForFences(screen->dev, 1, &fence->fence, VK_TRUE,
114                                   timeout_ns) == VK_SUCCESS;
115    if (success) {
116       if (fence->active_queries)
117          zink_prune_queries(screen, fence);
118 
119       /* unref all used resources */
120       util_dynarray_foreach(&fence->resources, struct pipe_resource*, pres) {
121          struct zink_resource *stencil, *res = zink_resource(*pres);
122          fence_remove_resource_access(fence, res);
123 
124          /* we still hold a ref, so this doesn't need to be atomic */
125          zink_get_depth_stencil_resources((struct pipe_resource*)res, NULL, &stencil);
126          if (stencil)
127             fence_remove_resource_access(fence, stencil);
128          pipe_resource_reference(pres, NULL);
129       }
130       util_dynarray_clear(&fence->resources);
131    }
132    return success;
133 }
134 
135 static bool
fence_finish(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_fence_handle * pfence,uint64_t timeout_ns)136 fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,
137                   struct pipe_fence_handle *pfence, uint64_t timeout_ns)
138 {
139    return zink_fence_finish(zink_screen(pscreen), zink_fence(pfence),
140                             timeout_ns);
141 }
142 
143 void
zink_screen_fence_init(struct pipe_screen * pscreen)144 zink_screen_fence_init(struct pipe_screen *pscreen)
145 {
146    pscreen->fence_reference = fence_reference;
147    pscreen->fence_finish = fence_finish;
148 }
149