blob: 291e29802293e0aea5517a42ecf145dfd329af70 [file] [log] [blame]
Chia-I Wu714df452015-01-01 07:55:04 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu714df452015-01-01 07:55:04 +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.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28#include "dev.h"
29#include "obj.h"
30#include "buf.h"
31
32static void buf_destroy(struct intel_obj *obj)
33{
34 struct intel_buf *buf = intel_buf_from_obj(obj);
35
36 intel_buf_destroy(buf);
37}
38
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060039static VkResult buf_get_info(struct intel_base *base, int type,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060040 size_t *size, void *data)
Chia-I Wu714df452015-01-01 07:55:04 +080041{
42 struct intel_buf *buf = intel_buf_from_base(base);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060043 VkResult ret = VK_SUCCESS;
Chia-I Wu714df452015-01-01 07:55:04 +080044
45 switch (type) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060046 case VK_INFO_TYPE_MEMORY_REQUIREMENTS:
Chia-I Wu714df452015-01-01 07:55:04 +080047 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060048 VkMemoryRequirements *mem_req = data;
Chia-I Wu714df452015-01-01 07:55:04 +080049
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060050 *size = sizeof(VkMemoryRequirements);
Chia-I Wu714df452015-01-01 07:55:04 +080051 if (data == NULL)
52 return ret;
53
54 /*
55 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
56 *
57 * "For buffers, which have no inherent "height," padding
58 * requirements are different. A buffer must be padded to the
59 * next multiple of 256 array elements, with an additional 16
60 * bytes added beyond that to account for the L1 cache line."
61 */
62 mem_req->size = buf->size;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -060063 if (buf->usage & (VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT |
64 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Chia-I Wu714df452015-01-01 07:55:04 +080065 mem_req->size = u_align(mem_req->size, 256) + 16;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -060066 }
Chia-I Wu714df452015-01-01 07:55:04 +080067
68 mem_req->alignment = 4096;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060069 mem_req->memType = VK_MEMORY_TYPE_BUFFER;
Chia-I Wu714df452015-01-01 07:55:04 +080070
71 }
72 break;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060073 case VK_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS:
Jon Ashburn5567c812015-01-20 08:50:12 -070074 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060075 VkBufferMemoryRequirements *buf_req = data;
Jon Ashburn5567c812015-01-20 08:50:12 -070076
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060077 *size = sizeof(VkBufferMemoryRequirements);
Jon Ashburn5567c812015-01-20 08:50:12 -070078 if (data == NULL)
79 return ret;
80 buf_req->usage = buf->usage;
81 }
82 break;
Chia-I Wu714df452015-01-01 07:55:04 +080083 default:
84 ret = intel_base_get_info(base, type, size, data);
85 break;
86 }
87
88 return ret;
89}
90
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060091VkResult intel_buf_create(struct intel_dev *dev,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -060092 const VkBufferCreateInfo *info,
Chia-I Wu714df452015-01-01 07:55:04 +080093 struct intel_buf **buf_ret)
94{
95 struct intel_buf *buf;
96
Chia-I Wu545c2e12015-02-22 13:19:54 +080097 buf = (struct intel_buf *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060098 sizeof(*buf), dev->base.dbg, VK_DBG_OBJECT_BUFFER, info, 0);
Chia-I Wu714df452015-01-01 07:55:04 +080099 if (!buf)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600100 return VK_ERROR_OUT_OF_MEMORY;
Chia-I Wu714df452015-01-01 07:55:04 +0800101
102 buf->size = info->size;
103 buf->usage = info->usage;
104
105 buf->obj.destroy = buf_destroy;
106 buf->obj.base.get_info = buf_get_info;
107
108 *buf_ret = buf;
109
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600110 return VK_SUCCESS;
Chia-I Wu714df452015-01-01 07:55:04 +0800111}
112
113void intel_buf_destroy(struct intel_buf *buf)
114{
115 intel_base_destroy(&buf->obj.base);
116}
117
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600118ICD_EXPORT VkResult VKAPI vkCreateBuffer(
119 VkDevice device,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600120 const VkBufferCreateInfo* pCreateInfo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600121 VkBuffer* pBuffer)
Chia-I Wu714df452015-01-01 07:55:04 +0800122{
123 struct intel_dev *dev = intel_dev(device);
124
125 return intel_buf_create(dev, pCreateInfo, (struct intel_buf **) pBuffer);
126}