blob: 5b8b37f42a0d1e169251b8d48edd2956ac7bf9ea [file] [log] [blame]
Chia-I Wuf9911eb2014-08-06 13:50:31 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wuf9911eb2014-08-06 13:50:31 +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 Wuf9911eb2014-08-06 13:50:31 +080026 */
27
Chia-I Wuf9911eb2014-08-06 13:50:31 +080028#include "dev.h"
29#include "mem.h"
30
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060031VkResult intel_mem_alloc(struct intel_dev *dev,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -060032 const VkMemoryAllocInfo *info,
Chia-I Wuf9911eb2014-08-06 13:50:31 +080033 struct intel_mem **mem_ret)
34{
35 struct intel_mem *mem;
36
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070037 /* ignore any IMAGE_INFO and BUFFER_INFO usage: they don't alter allocations */
Chia-I Wuf9911eb2014-08-06 13:50:31 +080038
Chia-I Wu545c2e12015-02-22 13:19:54 +080039 mem = (struct intel_mem *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060040 sizeof(*mem), dev->base.dbg, VK_DBG_OBJECT_GPU_MEMORY, info, 0);
Chia-I Wuf9911eb2014-08-06 13:50:31 +080041 if (!mem)
Tony Barbour8205d902015-04-16 15:59:00 -060042 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wuf9911eb2014-08-06 13:50:31 +080043
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070044 mem->bo = intel_winsys_alloc_bo(dev->winsys,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060045 "vk-gpu-memory", info->allocationSize, 0);
Chia-I Wuf9911eb2014-08-06 13:50:31 +080046 if (!mem->bo) {
47 intel_mem_free(mem);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060048 return VK_ERROR_UNKNOWN;
Chia-I Wuf9911eb2014-08-06 13:50:31 +080049 }
50
Chia-I Wu000747d2014-08-20 15:39:36 +080051 mem->size = info->allocationSize;
52
Courtney Goeltzenleuchterc35ab462014-08-06 17:10:04 -060053 *mem_ret = mem;
54
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060055 return VK_SUCCESS;
Chia-I Wuf9911eb2014-08-06 13:50:31 +080056}
57
58void intel_mem_free(struct intel_mem *mem)
59{
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070060 intel_bo_unref(mem->bo);
Chia-I Wuf9911eb2014-08-06 13:50:31 +080061
Chia-I Wubbf2c932014-08-07 12:20:08 +080062 intel_base_destroy(&mem->base);
Chia-I Wuf9911eb2014-08-06 13:50:31 +080063}
64
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060065VkResult intel_mem_import_userptr(struct intel_dev *dev,
Chia-I Wu2cd1e072015-03-06 12:10:13 -070066 const void *userptr,
67 size_t size,
68 struct intel_mem **mem_ret)
69{
70 const size_t alignment = 4096;
71 struct intel_mem *mem;
72
73 if ((uintptr_t) userptr % alignment || size % alignment)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060074 return VK_ERROR_INVALID_ALIGNMENT;
Chia-I Wu2cd1e072015-03-06 12:10:13 -070075
76 mem = (struct intel_mem *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060077 sizeof(*mem), dev->base.dbg, VK_DBG_OBJECT_GPU_MEMORY, NULL, 0);
Chia-I Wu2cd1e072015-03-06 12:10:13 -070078 if (!mem)
Tony Barbour8205d902015-04-16 15:59:00 -060079 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu2cd1e072015-03-06 12:10:13 -070080
81 mem->bo = intel_winsys_import_userptr(dev->winsys,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060082 "vk-gpu-memory-userptr", (void *) userptr, size, 0);
Chia-I Wu2cd1e072015-03-06 12:10:13 -070083 if (!mem->bo) {
84 intel_mem_free(mem);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060085 return VK_ERROR_UNKNOWN;
Chia-I Wu2cd1e072015-03-06 12:10:13 -070086 }
87
88 mem->size = size;
89
90 *mem_ret = mem;
91
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060092 return VK_SUCCESS;
Chia-I Wu2cd1e072015-03-06 12:10:13 -070093}
94
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060095VkResult intel_mem_set_priority(struct intel_mem *mem,
96 VkMemoryPriority priority)
Chia-I Wuf9911eb2014-08-06 13:50:31 +080097{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060098 /* pin the bo when VK_MEMORY_PRIORITY_VERY_HIGH? */
99 return VK_SUCCESS;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800100}
101
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600102ICD_EXPORT VkResult VKAPI vkAllocMemory(
Mike Stroyan230e6252015-04-17 12:36:38 -0600103 VkDevice device,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600104 const VkMemoryAllocInfo* pAllocInfo,
Mike Stroyan230e6252015-04-17 12:36:38 -0600105 VkDeviceMemory* pMem)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800106{
107 struct intel_dev *dev = intel_dev(device);
108
109 return intel_mem_alloc(dev, pAllocInfo, (struct intel_mem **) pMem);
110}
111
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600112ICD_EXPORT VkResult VKAPI vkFreeMemory(
Mike Stroyan230e6252015-04-17 12:36:38 -0600113 VkDevice device,
114 VkDeviceMemory mem_)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800115{
116 struct intel_mem *mem = intel_mem(mem_);
117
118 intel_mem_free(mem);
119
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600120 return VK_SUCCESS;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800121}
122
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600123ICD_EXPORT VkResult VKAPI vkSetMemoryPriority(
Mike Stroyan230e6252015-04-17 12:36:38 -0600124 VkDevice device,
125 VkDeviceMemory mem_,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600126 VkMemoryPriority priority)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800127{
128 struct intel_mem *mem = intel_mem(mem_);
129
130 return intel_mem_set_priority(mem, priority);
131}
132
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600133ICD_EXPORT VkResult VKAPI vkMapMemory(
Mike Stroyan230e6252015-04-17 12:36:38 -0600134 VkDevice device,
Tony Barbour3e3420a2015-04-16 19:09:28 -0600135 VkDeviceMemory mem_,
136 VkDeviceSize offset,
137 VkDeviceSize size,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600138 VkFlags flags,
Tony Barbour3e3420a2015-04-16 19:09:28 -0600139 void** ppData)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800140{
141 struct intel_mem *mem = intel_mem(mem_);
142 void *ptr = intel_mem_map(mem, flags);
143
Tony Barbour3e3420a2015-04-16 19:09:28 -0600144 *ppData = (void *)((size_t)ptr + offset);
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800145
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600146 return (ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800147}
148
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600149ICD_EXPORT VkResult VKAPI vkUnmapMemory(
Mike Stroyan230e6252015-04-17 12:36:38 -0600150 VkDevice device,
Tony Barbour8205d902015-04-16 15:59:00 -0600151 VkDeviceMemory mem_)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800152{
153 struct intel_mem *mem = intel_mem(mem_);
154
155 intel_mem_unmap(mem);
156
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600157 return VK_SUCCESS;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800158}
Chia-I Wu251e7d92014-08-19 13:35:42 +0800159
Tony Barbour859ceab2015-04-16 19:23:13 -0600160ICD_EXPORT VkResult VKAPI vkFlushMappedMemory(
Mike Stroyan230e6252015-04-17 12:36:38 -0600161 VkDevice device,
Tony Barbour859ceab2015-04-16 19:23:13 -0600162 VkDeviceMemory mem_,
163 VkDeviceSize offset,
164 VkDeviceSize size)
165{
166 return VK_SUCCESS;
167}
168
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600169ICD_EXPORT VkResult VKAPI vkPinSystemMemory(
170 VkDevice device,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600171 const void* pSysMem,
172 size_t memSize,
Tony Barbour8205d902015-04-16 15:59:00 -0600173 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800174{
Chia-I Wu2cd1e072015-03-06 12:10:13 -0700175 struct intel_dev *dev = intel_dev(device);
176
177 return intel_mem_import_userptr(dev, pSysMem, memSize,
178 (struct intel_mem **) pMem);
Chia-I Wu251e7d92014-08-19 13:35:42 +0800179}
180
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600181ICD_EXPORT VkResult VKAPI vkOpenSharedMemory(
182 VkDevice device,
183 const VkMemoryOpenInfo* pOpenInfo,
Tony Barbour8205d902015-04-16 15:59:00 -0600184 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800185{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600186 return VK_ERROR_UNAVAILABLE;
Chia-I Wu251e7d92014-08-19 13:35:42 +0800187}
188
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600189ICD_EXPORT VkResult VKAPI vkOpenPeerMemory(
190 VkDevice device,
191 const VkPeerMemoryOpenInfo* pOpenInfo,
Tony Barbour8205d902015-04-16 15:59:00 -0600192 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800193{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600194 return VK_ERROR_UNAVAILABLE;
Chia-I Wu251e7d92014-08-19 13:35:42 +0800195}