blob: a0561a59904d03392468a9d44e6ad4767d22b256 [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(
103 VkDevice device,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600104 const VkMemoryAllocInfo* pAllocInfo,
Tony Barbour8205d902015-04-16 15:59:00 -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(
Tony Barbour8205d902015-04-16 15:59:00 -0600113 VkDeviceMemory mem_)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800114{
115 struct intel_mem *mem = intel_mem(mem_);
116
117 intel_mem_free(mem);
118
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600119 return VK_SUCCESS;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800120}
121
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600122ICD_EXPORT VkResult VKAPI vkSetMemoryPriority(
Tony Barbour8205d902015-04-16 15:59:00 -0600123 VkDeviceMemory mem_,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600124 VkMemoryPriority priority)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800125{
126 struct intel_mem *mem = intel_mem(mem_);
127
128 return intel_mem_set_priority(mem, priority);
129}
130
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600131ICD_EXPORT VkResult VKAPI vkMapMemory(
Tony Barbour3e3420a2015-04-16 19:09:28 -0600132 VkDeviceMemory mem_,
133 VkDeviceSize offset,
134 VkDeviceSize size,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600135 VkFlags flags,
Tony Barbour3e3420a2015-04-16 19:09:28 -0600136 void** ppData)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800137{
138 struct intel_mem *mem = intel_mem(mem_);
139 void *ptr = intel_mem_map(mem, flags);
140
Tony Barbour3e3420a2015-04-16 19:09:28 -0600141 *ppData = (void *)((size_t)ptr + offset);
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800142
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600143 return (ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800144}
145
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600146ICD_EXPORT VkResult VKAPI vkUnmapMemory(
Tony Barbour8205d902015-04-16 15:59:00 -0600147 VkDeviceMemory mem_)
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800148{
149 struct intel_mem *mem = intel_mem(mem_);
150
151 intel_mem_unmap(mem);
152
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600153 return VK_SUCCESS;
Chia-I Wuf9911eb2014-08-06 13:50:31 +0800154}
Chia-I Wu251e7d92014-08-19 13:35:42 +0800155
Tony Barbour859ceab2015-04-16 19:23:13 -0600156ICD_EXPORT VkResult VKAPI vkFlushMappedMemory(
157 VkDeviceMemory mem_,
158 VkDeviceSize offset,
159 VkDeviceSize size)
160{
161 return VK_SUCCESS;
162}
163
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600164ICD_EXPORT VkResult VKAPI vkPinSystemMemory(
165 VkDevice device,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600166 const void* pSysMem,
167 size_t memSize,
Tony Barbour8205d902015-04-16 15:59:00 -0600168 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800169{
Chia-I Wu2cd1e072015-03-06 12:10:13 -0700170 struct intel_dev *dev = intel_dev(device);
171
172 return intel_mem_import_userptr(dev, pSysMem, memSize,
173 (struct intel_mem **) pMem);
Chia-I Wu251e7d92014-08-19 13:35:42 +0800174}
175
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600176ICD_EXPORT VkResult VKAPI vkOpenSharedMemory(
177 VkDevice device,
178 const VkMemoryOpenInfo* pOpenInfo,
Tony Barbour8205d902015-04-16 15:59:00 -0600179 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800180{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600181 return VK_ERROR_UNAVAILABLE;
Chia-I Wu251e7d92014-08-19 13:35:42 +0800182}
183
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600184ICD_EXPORT VkResult VKAPI vkOpenPeerMemory(
185 VkDevice device,
186 const VkPeerMemoryOpenInfo* pOpenInfo,
Tony Barbour8205d902015-04-16 15:59:00 -0600187 VkDeviceMemory* pMem)
Chia-I Wu251e7d92014-08-19 13:35:42 +0800188{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600189 return VK_ERROR_UNAVAILABLE;
Chia-I Wu251e7d92014-08-19 13:35:42 +0800190}