blob: 7b3cebd64319ce7c46d091e413867040d2834699 [file] [log] [blame]
Chia-I Wubdf4c562014-08-07 06:36:33 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wubdf4c562014-08-07 06:36:33 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wubdf4c562014-08-07 06:36:33 +080026 */
27
28#include "kmd/winsys.h"
Chia-I Wu34f45182014-08-19 14:02:59 +080029#include "cmd.h"
Chia-I Wubdf4c562014-08-07 06:36:33 +080030#include "dev.h"
Chia-I Wu41858c82015-04-04 16:39:25 +080031#include "wsi.h"
Chia-I Wubdf4c562014-08-07 06:36:33 +080032#include "fence.h"
33
Chia-I Wu26f0bd02014-08-07 10:38:40 +080034static void fence_destroy(struct intel_obj *obj)
Chia-I Wubdf4c562014-08-07 06:36:33 +080035{
36 struct intel_fence *fence = intel_fence_from_obj(obj);
37
38 intel_fence_destroy(fence);
39}
40
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060041VkResult intel_fence_create(struct intel_dev *dev,
42 const VkFenceCreateInfo *info,
Chia-I Wubdf4c562014-08-07 06:36:33 +080043 struct intel_fence **fence_ret)
44{
45 struct intel_fence *fence;
46
Chia-I Wu545c2e12015-02-22 13:19:54 +080047 fence = (struct intel_fence *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060048 sizeof(*fence), dev->base.dbg, VK_DBG_OBJECT_FENCE, info, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +080049 if (!fence)
Tony Barbour8205d902015-04-16 15:59:00 -060050 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wubdf4c562014-08-07 06:36:33 +080051
Chia-I Wu41858c82015-04-04 16:39:25 +080052 if (dev->exts[INTEL_EXT_WSI_X11]) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060053 VkResult ret = intel_wsi_fence_init(fence);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060054 if (ret != VK_SUCCESS) {
Chia-I Wu41858c82015-04-04 16:39:25 +080055 intel_fence_destroy(fence);
56 return ret;
57 }
58 }
59
Chia-I Wu26f0bd02014-08-07 10:38:40 +080060 fence->obj.destroy = fence_destroy;
Chia-I Wubdf4c562014-08-07 06:36:33 +080061
Chia-I Wubdf4c562014-08-07 06:36:33 +080062 *fence_ret = fence;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060063 fence->signaled = (info->flags & VK_FENCE_CREATE_SIGNALED_BIT);
Chia-I Wubdf4c562014-08-07 06:36:33 +080064
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060065 return VK_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +080066}
67
68void intel_fence_destroy(struct intel_fence *fence)
69{
Chia-I Wu41858c82015-04-04 16:39:25 +080070 if (fence->wsi_data)
71 intel_wsi_fence_cleanup(fence);
72
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070073 intel_bo_unref(fence->seqno_bo);
Chia-I Wu046a7a92015-02-17 14:29:01 -070074
Chia-I Wubbf2c932014-08-07 12:20:08 +080075 intel_base_destroy(&fence->obj.base);
Chia-I Wubdf4c562014-08-07 06:36:33 +080076}
77
Chia-I Wub56f5df2015-04-04 20:21:10 +080078void intel_fence_copy(struct intel_fence *fence,
79 const struct intel_fence *src)
80{
81 intel_wsi_fence_copy(fence, src);
82 intel_fence_set_seqno(fence, src->seqno_bo);
83}
84
Chia-I Wu046a7a92015-02-17 14:29:01 -070085void intel_fence_set_seqno(struct intel_fence *fence,
86 struct intel_bo *seqno_bo)
87{
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070088 intel_bo_unref(fence->seqno_bo);
89 fence->seqno_bo = intel_bo_ref(seqno_bo);
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -050090
91 fence->signaled = false;
Chia-I Wu046a7a92015-02-17 14:29:01 -070092}
93
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060094VkResult intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns)
Chia-I Wubdf4c562014-08-07 06:36:33 +080095{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060096 VkResult ret;
Chia-I Wubda4f622015-02-25 15:06:15 -070097
Chia-I Wu41858c82015-04-04 16:39:25 +080098 ret = intel_wsi_fence_wait(fence, timeout_ns);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060099 if (ret != VK_SUCCESS)
Chia-I Wu41858c82015-04-04 16:39:25 +0800100 return ret;
Chia-I Wubda4f622015-02-25 15:06:15 -0700101
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500102 if (fence->signaled) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600103 return VK_SUCCESS;
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500104 }
105
Chia-I Wu046a7a92015-02-17 14:29:01 -0700106 if (fence->seqno_bo) {
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500107 ret = (intel_bo_wait(fence->seqno_bo, timeout_ns)) ?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600108 VK_NOT_READY : VK_SUCCESS;
109 if (ret == VK_SUCCESS) {
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500110 fence->signaled = true;
111 }
112 return ret;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800113 }
Chia-I Wubdf4c562014-08-07 06:36:33 +0800114
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600115 return VK_ERROR_UNAVAILABLE;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800116}
117
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600118ICD_EXPORT VkResult VKAPI vkCreateFence(
119 VkDevice device,
120 const VkFenceCreateInfo* pCreateInfo,
121 VkFence* pFence)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800122{
123 struct intel_dev *dev = intel_dev(device);
124
125 return intel_fence_create(dev, pCreateInfo,
126 (struct intel_fence **) pFence);
127}
128
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600129ICD_EXPORT VkResult VKAPI vkGetFenceStatus(
130 VkFence fence_)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800131{
132 struct intel_fence *fence = intel_fence(fence_);
133
Chia-I Wuca4b1542014-09-23 15:02:01 +0800134 return intel_fence_wait(fence, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +0800135}
136
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600137ICD_EXPORT VkResult VKAPI vkWaitForFences(
138 VkDevice device,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600139 uint32_t fenceCount,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600140 const VkFence* pFences,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600141 bool32_t waitAll,
142 uint64_t timeout)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800143{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600144 VkResult ret = VK_SUCCESS;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600145 uint32_t i;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800146
147 for (i = 0; i < fenceCount; i++) {
148 struct intel_fence *fence = intel_fence(pFences[i]);
149 int64_t ns;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600150 VkResult r;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800151
Courtney Goeltzenleuchterb29b8e42015-03-25 16:37:00 -0600152 /* timeout in nano seconds */
153 ns = (timeout <= (uint64_t) INT64_MAX) ? ns : -1;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800154 r = intel_fence_wait(fence, ns);
155
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600156 if (!waitAll && r == VK_SUCCESS)
157 return VK_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800158
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600159 if (r != VK_SUCCESS)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800160 ret = r;
161 }
162
163 return ret;
164}
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600165ICD_EXPORT VkResult VKAPI vkResetFences(
166 VkDevice device,
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500167 uint32_t fenceCount,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600168 VkFence* pFences)
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500169{
170 uint32_t i;
171
172 for (i = 0; i < fenceCount; i++) {
173 struct intel_fence *fence = intel_fence(pFences[i]);
174 fence->signaled = false;
175 }
176
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600177 return VK_SUCCESS;
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500178}