blob: 35ef65db4a9fb6ccecd9dc1f0cd298ce5b721de9 [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"
Courtney Goeltzenleuchter9ecf6852015-06-09 08:22:48 -060033#include "instance.h"
Chia-I Wubdf4c562014-08-07 06:36:33 +080034
Chia-I Wu26f0bd02014-08-07 10:38:40 +080035static void fence_destroy(struct intel_obj *obj)
Chia-I Wubdf4c562014-08-07 06:36:33 +080036{
37 struct intel_fence *fence = intel_fence_from_obj(obj);
38
39 intel_fence_destroy(fence);
40}
41
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060042VkResult intel_fence_create(struct intel_dev *dev,
43 const VkFenceCreateInfo *info,
Chia-I Wubdf4c562014-08-07 06:36:33 +080044 struct intel_fence **fence_ret)
45{
46 struct intel_fence *fence;
47
Chia-I Wu545c2e12015-02-22 13:19:54 +080048 fence = (struct intel_fence *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060049 sizeof(*fence), dev->base.dbg, VK_OBJECT_TYPE_FENCE, info, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +080050 if (!fence)
Tony Barbour8205d902015-04-16 15:59:00 -060051 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wubdf4c562014-08-07 06:36:33 +080052
Courtney Goeltzenleuchter9ecf6852015-06-09 08:22:48 -060053 if (dev->base.handle.instance->global_exts[INTEL_GLOBAL_EXT_WSI_LUNARG]) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060054 VkResult ret = intel_wsi_fence_init(fence);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060055 if (ret != VK_SUCCESS) {
Chia-I Wu41858c82015-04-04 16:39:25 +080056 intel_fence_destroy(fence);
57 return ret;
58 }
59 }
60
Chia-I Wu26f0bd02014-08-07 10:38:40 +080061 fence->obj.destroy = fence_destroy;
Chia-I Wubdf4c562014-08-07 06:36:33 +080062
Chia-I Wubdf4c562014-08-07 06:36:33 +080063 *fence_ret = fence;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060064 fence->signaled = (info->flags & VK_FENCE_CREATE_SIGNALED_BIT);
Chia-I Wubdf4c562014-08-07 06:36:33 +080065
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060066 return VK_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +080067}
68
69void intel_fence_destroy(struct intel_fence *fence)
70{
Chia-I Wu41858c82015-04-04 16:39:25 +080071 if (fence->wsi_data)
72 intel_wsi_fence_cleanup(fence);
73
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070074 intel_bo_unref(fence->seqno_bo);
Chia-I Wu046a7a92015-02-17 14:29:01 -070075
Chia-I Wubbf2c932014-08-07 12:20:08 +080076 intel_base_destroy(&fence->obj.base);
Chia-I Wubdf4c562014-08-07 06:36:33 +080077}
78
Chia-I Wub56f5df2015-04-04 20:21:10 +080079void intel_fence_copy(struct intel_fence *fence,
80 const struct intel_fence *src)
81{
82 intel_wsi_fence_copy(fence, src);
83 intel_fence_set_seqno(fence, src->seqno_bo);
84}
85
Chia-I Wu046a7a92015-02-17 14:29:01 -070086void intel_fence_set_seqno(struct intel_fence *fence,
87 struct intel_bo *seqno_bo)
88{
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070089 intel_bo_unref(fence->seqno_bo);
90 fence->seqno_bo = intel_bo_ref(seqno_bo);
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -050091
92 fence->signaled = false;
Chia-I Wu046a7a92015-02-17 14:29:01 -070093}
94
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060095VkResult intel_fence_wait(struct intel_fence *fence, int64_t timeout_ns)
Chia-I Wubdf4c562014-08-07 06:36:33 +080096{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060097 VkResult ret;
Chia-I Wubda4f622015-02-25 15:06:15 -070098
Chia-I Wu41858c82015-04-04 16:39:25 +080099 ret = intel_wsi_fence_wait(fence, timeout_ns);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600100 if (ret != VK_SUCCESS)
Chia-I Wu41858c82015-04-04 16:39:25 +0800101 return ret;
Chia-I Wubda4f622015-02-25 15:06:15 -0700102
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500103 if (fence->signaled) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600104 return VK_SUCCESS;
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500105 }
106
Chia-I Wu046a7a92015-02-17 14:29:01 -0700107 if (fence->seqno_bo) {
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500108 ret = (intel_bo_wait(fence->seqno_bo, timeout_ns)) ?
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600109 VK_NOT_READY : VK_SUCCESS;
110 if (ret == VK_SUCCESS) {
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500111 fence->signaled = true;
112 }
113 return ret;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800114 }
Chia-I Wubdf4c562014-08-07 06:36:33 +0800115
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600116 return VK_ERROR_UNAVAILABLE;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800117}
118
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600119ICD_EXPORT VkResult VKAPI vkCreateFence(
120 VkDevice device,
121 const VkFenceCreateInfo* pCreateInfo,
122 VkFence* pFence)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800123{
124 struct intel_dev *dev = intel_dev(device);
125
126 return intel_fence_create(dev, pCreateInfo,
127 (struct intel_fence **) pFence);
128}
129
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600130ICD_EXPORT VkResult VKAPI vkGetFenceStatus(
Mike Stroyan230e6252015-04-17 12:36:38 -0600131 VkDevice device,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600132 VkFence fence_)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800133{
134 struct intel_fence *fence = intel_fence(fence_);
135
Chia-I Wuca4b1542014-09-23 15:02:01 +0800136 return intel_fence_wait(fence, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +0800137}
138
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600139ICD_EXPORT VkResult VKAPI vkWaitForFences(
140 VkDevice device,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600141 uint32_t fenceCount,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600142 const VkFence* pFences,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600143 bool32_t waitAll,
144 uint64_t timeout)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800145{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600146 VkResult ret = VK_SUCCESS;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600147 uint32_t i;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800148
149 for (i = 0; i < fenceCount; i++) {
150 struct intel_fence *fence = intel_fence(pFences[i]);
151 int64_t ns;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600152 VkResult r;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800153
Courtney Goeltzenleuchterb29b8e42015-03-25 16:37:00 -0600154 /* timeout in nano seconds */
155 ns = (timeout <= (uint64_t) INT64_MAX) ? ns : -1;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800156 r = intel_fence_wait(fence, ns);
157
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600158 if (!waitAll && r == VK_SUCCESS)
159 return VK_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800160
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600161 if (r != VK_SUCCESS)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800162 ret = r;
163 }
164
165 return ret;
166}
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600167ICD_EXPORT VkResult VKAPI vkResetFences(
168 VkDevice device,
Courtney Goeltzenleuchterf2e33ad2015-06-18 17:28:20 -0600169 uint32_t fenceCount,
170 const VkFence* pFences)
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500171{
172 uint32_t i;
173
174 for (i = 0; i < fenceCount; i++) {
175 struct intel_fence *fence = intel_fence(pFences[i]);
176 fence->signaled = false;
177 }
178
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600179 return VK_SUCCESS;
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500180}