blob: af62df01c8ac25828507b230eeeb870befeffe77 [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
Ian Elliott4f299362015-07-06 14:45:11 -060053 if (dev->base.handle.instance->global_exts[INTEL_GLOBAL_EXT_WSI_SWAPCHAIN]) {
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 Goeltzenleuchterac544f32015-09-14 18:01:17 -0600116 assert(0 && "Invalid fence status");
117 return VK_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800118}
119
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600120ICD_EXPORT VkResult VKAPI vkCreateFence(
121 VkDevice device,
122 const VkFenceCreateInfo* pCreateInfo,
123 VkFence* pFence)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800124{
125 struct intel_dev *dev = intel_dev(device);
126
127 return intel_fence_create(dev, pCreateInfo,
128 (struct intel_fence **) pFence);
129}
130
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600131ICD_EXPORT void VKAPI vkDestroyFence(
Tony Barbourde4124d2015-07-03 10:33:54 -0600132 VkDevice device,
133 VkFence fence)
134
135 {
136 struct intel_obj *obj = intel_obj(fence.handle);
137
138 obj->destroy(obj);
Tony Barbourde4124d2015-07-03 10:33:54 -0600139 }
140
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600141ICD_EXPORT VkResult VKAPI vkGetFenceStatus(
Mike Stroyan230e6252015-04-17 12:36:38 -0600142 VkDevice device,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600143 VkFence fence_)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800144{
145 struct intel_fence *fence = intel_fence(fence_);
146
Chia-I Wuca4b1542014-09-23 15:02:01 +0800147 return intel_fence_wait(fence, 0);
Chia-I Wubdf4c562014-08-07 06:36:33 +0800148}
149
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600150ICD_EXPORT VkResult VKAPI vkWaitForFences(
151 VkDevice device,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600152 uint32_t fenceCount,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600153 const VkFence* pFences,
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -0600154 VkBool32 waitAll,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600155 uint64_t timeout)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800156{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600157 VkResult ret = VK_SUCCESS;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600158 uint32_t i;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800159
160 for (i = 0; i < fenceCount; i++) {
161 struct intel_fence *fence = intel_fence(pFences[i]);
162 int64_t ns;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600163 VkResult r;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800164
Courtney Goeltzenleuchterb29b8e42015-03-25 16:37:00 -0600165 /* timeout in nano seconds */
166 ns = (timeout <= (uint64_t) INT64_MAX) ? ns : -1;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800167 r = intel_fence_wait(fence, ns);
168
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600169 if (!waitAll && r == VK_SUCCESS)
170 return VK_SUCCESS;
Chia-I Wubdf4c562014-08-07 06:36:33 +0800171
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600172 if (r != VK_SUCCESS)
Chia-I Wubdf4c562014-08-07 06:36:33 +0800173 ret = r;
174 }
175
176 return ret;
177}
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600178ICD_EXPORT VkResult VKAPI vkResetFences(
179 VkDevice device,
Courtney Goeltzenleuchterf2e33ad2015-06-18 17:28:20 -0600180 uint32_t fenceCount,
181 const VkFence* pFences)
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500182{
183 uint32_t i;
184
185 for (i = 0; i < fenceCount; i++) {
186 struct intel_fence *fence = intel_fence(pFences[i]);
187 fence->signaled = false;
188 }
189
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600190 return VK_SUCCESS;
Mark Lobodzinskiebe814d2015-04-07 16:07:57 -0500191}