blob: a6a3c768c494a537a59a463451edf4385bd24a9b [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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060039static VK_RESULT 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060043 VK_RESULT 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060048 VK_MEMORY_REQUIREMENTS *mem_req = data;
Chia-I Wu714df452015-01-01 07:55:04 +080049
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060050 *size = sizeof(VK_MEMORY_REQUIREMENTS);
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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060063 if (buf->usage & VK_BUFFER_USAGE_SHADER_ACCESS_READ_BIT)
Chia-I Wu714df452015-01-01 07:55:04 +080064 mem_req->size = u_align(mem_req->size, 256) + 16;
65
66 mem_req->alignment = 4096;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060067 mem_req->memType = VK_MEMORY_TYPE_BUFFER;
Chia-I Wu714df452015-01-01 07:55:04 +080068
69 }
70 break;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060071 case VK_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS:
Jon Ashburn5567c812015-01-20 08:50:12 -070072 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060073 VK_BUFFER_MEMORY_REQUIREMENTS *buf_req = data;
Jon Ashburn5567c812015-01-20 08:50:12 -070074
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060075 *size = sizeof(VK_BUFFER_MEMORY_REQUIREMENTS);
Jon Ashburn5567c812015-01-20 08:50:12 -070076 if (data == NULL)
77 return ret;
78 buf_req->usage = buf->usage;
79 }
80 break;
Chia-I Wu714df452015-01-01 07:55:04 +080081 default:
82 ret = intel_base_get_info(base, type, size, data);
83 break;
84 }
85
86 return ret;
87}
88
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060089VK_RESULT intel_buf_create(struct intel_dev *dev,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -060090 const VkBufferCreateInfo *info,
Chia-I Wu714df452015-01-01 07:55:04 +080091 struct intel_buf **buf_ret)
92{
93 struct intel_buf *buf;
94
Chia-I Wu545c2e12015-02-22 13:19:54 +080095 buf = (struct intel_buf *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060096 sizeof(*buf), dev->base.dbg, VK_DBG_OBJECT_BUFFER, info, 0);
Chia-I Wu714df452015-01-01 07:55:04 +080097 if (!buf)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060098 return VK_ERROR_OUT_OF_MEMORY;
Chia-I Wu714df452015-01-01 07:55:04 +080099
100 buf->size = info->size;
101 buf->usage = info->usage;
102
103 buf->obj.destroy = buf_destroy;
104 buf->obj.base.get_info = buf_get_info;
105
106 *buf_ret = buf;
107
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600108 return VK_SUCCESS;
Chia-I Wu714df452015-01-01 07:55:04 +0800109}
110
111void intel_buf_destroy(struct intel_buf *buf)
112{
113 intel_base_destroy(&buf->obj.base);
114}
115
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600116ICD_EXPORT VK_RESULT VKAPI vkCreateBuffer(
117 VK_DEVICE device,
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600118 const VkBufferCreateInfo* pCreateInfo,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600119 VK_BUFFER* pBuffer)
Chia-I Wu714df452015-01-01 07:55:04 +0800120{
121 struct intel_dev *dev = intel_dev(device);
122
123 return intel_buf_create(dev, pCreateInfo, (struct intel_buf **) pBuffer);
124}