blob: f19785e84d711a3c875d1b40748ba5326b18dbf3 [file] [log] [blame]
Alexis Hetu38ff8302018-10-18 15:08:13 -04001// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "VkBuffer.hpp"
Antonio Maiorano42fd1592020-04-27 11:30:40 -040016#include "VkConfig.hpp"
Alexis Hetu38ff8302018-10-18 15:08:13 -040017#include "VkDeviceMemory.hpp"
Nicolas Capens318d5e02018-11-16 14:28:36 -050018
19#include <cstring>
Alexis Hetu38ff8302018-10-18 15:08:13 -040020
Nicolas Capens157ba262019-12-10 17:49:14 -050021namespace vk {
Alexis Hetu38ff8302018-10-18 15:08:13 -040022
Ben Clayton2ed93ab2019-12-17 20:38:03 +000023Buffer::Buffer(const VkBufferCreateInfo *pCreateInfo, void *mem)
24 : flags(pCreateInfo->flags)
25 , size(pCreateInfo->size)
26 , usage(pCreateInfo->usage)
27 , sharingMode(pCreateInfo->sharingMode)
Alexis Hetu38ff8302018-10-18 15:08:13 -040028{
Alexis Hetu3c5cb9c2019-04-02 17:12:26 -040029 if(pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT)
30 {
31 queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000032 queueFamilyIndices = reinterpret_cast<uint32_t *>(mem);
Alexis Hetu3c5cb9c2019-04-02 17:12:26 -040033 memcpy(queueFamilyIndices, pCreateInfo->pQueueFamilyIndices, sizeof(uint32_t) * queueFamilyIndexCount);
34 }
David 'Digit' Turner359bc802019-08-14 17:46:07 +020035
Ben Clayton2ed93ab2019-12-17 20:38:03 +000036 const auto *nextInfo = reinterpret_cast<const VkBaseInStructure *>(pCreateInfo->pNext);
Nicolas Capens81bc9d92019-12-16 15:05:57 -050037 for(; nextInfo != nullptr; nextInfo = nextInfo->pNext)
David 'Digit' Turner359bc802019-08-14 17:46:07 +020038 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -050039 if(nextInfo->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO)
David 'Digit' Turner359bc802019-08-14 17:46:07 +020040 {
Ben Clayton2ed93ab2019-12-17 20:38:03 +000041 const auto *externalInfo = reinterpret_cast<const VkExternalMemoryBufferCreateInfo *>(nextInfo);
David 'Digit' Turner359bc802019-08-14 17:46:07 +020042 supportedExternalMemoryHandleTypes = externalInfo->handleTypes;
43 }
44 }
Alexis Hetu38ff8302018-10-18 15:08:13 -040045}
46
Ben Clayton2ed93ab2019-12-17 20:38:03 +000047void Buffer::destroy(const VkAllocationCallbacks *pAllocator)
Alexis Hetu38ff8302018-10-18 15:08:13 -040048{
49 vk::deallocate(queueFamilyIndices, pAllocator);
50}
51
Ben Clayton2ed93ab2019-12-17 20:38:03 +000052size_t Buffer::ComputeRequiredAllocationSize(const VkBufferCreateInfo *pCreateInfo)
Alexis Hetu38ff8302018-10-18 15:08:13 -040053{
Alexis Hetu3c5cb9c2019-04-02 17:12:26 -040054 return (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) ? sizeof(uint32_t) * pCreateInfo->queueFamilyIndexCount : 0;
Alexis Hetu38ff8302018-10-18 15:08:13 -040055}
56
57const VkMemoryRequirements Buffer::getMemoryRequirements() const
58{
59 VkMemoryRequirements memoryRequirements = {};
Ben Claytonefec1b92019-03-05 17:38:16 +000060 if(usage & (VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT))
61 {
62 memoryRequirements.alignment = vk::MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT;
63 }
64 else if(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)
65 {
66 memoryRequirements.alignment = vk::MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT;
67 }
68 else if(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
69 {
70 memoryRequirements.alignment = vk::MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT;
71 }
72 else
73 {
74 memoryRequirements.alignment = REQUIRED_MEMORY_ALIGNMENT;
Alexis Hetu61234552019-01-21 17:22:51 -050075 }
Nicolas Capensc3f79602018-11-13 14:06:37 -050076 memoryRequirements.memoryTypeBits = vk::MEMORY_TYPE_GENERIC_BIT;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000077 memoryRequirements.size = size; // TODO: also reserve space for a header containing
78 // the size of the buffer (for robust buffer access)
Alexis Hetu38ff8302018-10-18 15:08:13 -040079 return memoryRequirements;
80}
81
Ben Clayton2ed93ab2019-12-17 20:38:03 +000082bool Buffer::canBindToMemory(DeviceMemory *pDeviceMemory) const
David 'Digit' Turner359bc802019-08-14 17:46:07 +020083{
84 return pDeviceMemory->checkExternalMemoryHandleType(supportedExternalMemoryHandleTypes);
85}
86
Ben Clayton2ed93ab2019-12-17 20:38:03 +000087void Buffer::bind(DeviceMemory *pDeviceMemory, VkDeviceSize pMemoryOffset)
Alexis Hetu38ff8302018-10-18 15:08:13 -040088{
Alexis Hetu7d96f512019-06-13 18:23:56 -040089 memory = pDeviceMemory->getOffsetPointer(pMemoryOffset);
Alexis Hetu38ff8302018-10-18 15:08:13 -040090}
91
Ben Clayton2ed93ab2019-12-17 20:38:03 +000092void Buffer::copyFrom(const void *srcMemory, VkDeviceSize pSize, VkDeviceSize pOffset)
Alexis Hetu2661e842018-12-07 11:36:34 -050093{
94 ASSERT((pSize + pOffset) <= size);
95
Alexis Hetue0b5a4b2018-12-14 12:11:02 -050096 memcpy(getOffsetPointer(pOffset), srcMemory, pSize);
Alexis Hetu2661e842018-12-07 11:36:34 -050097}
98
Ben Clayton2ed93ab2019-12-17 20:38:03 +000099void Buffer::copyTo(void *dstMemory, VkDeviceSize pSize, VkDeviceSize pOffset) const
Alexis Hetu2661e842018-12-07 11:36:34 -0500100{
101 ASSERT((pSize + pOffset) <= size);
102
Alexis Hetue0b5a4b2018-12-14 12:11:02 -0500103 memcpy(dstMemory, getOffsetPointer(pOffset), pSize);
Alexis Hetu2661e842018-12-07 11:36:34 -0500104}
105
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000106void Buffer::copyTo(Buffer *dstBuffer, const VkBufferCopy &pRegion) const
Alexis Hetubb0a7f02018-12-14 16:50:43 -0500107{
108 copyTo(dstBuffer->getOffsetPointer(pRegion.dstOffset), pRegion.size, pRegion.srcOffset);
109}
110
Alexis Hetu7fb0b732019-02-07 13:50:10 -0500111void Buffer::fill(VkDeviceSize dstOffset, VkDeviceSize fillSize, uint32_t data)
112{
Alexis Hetufb6639f2019-03-25 10:24:23 -0400113 size_t bytes = (fillSize == VK_WHOLE_SIZE) ? (size - dstOffset) : fillSize;
Alexis Hetu7fb0b732019-02-07 13:50:10 -0500114
Alexis Hetufb6639f2019-03-25 10:24:23 -0400115 ASSERT((bytes + dstOffset) <= size);
116
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000117 uint32_t *memToWrite = static_cast<uint32_t *>(getOffsetPointer(dstOffset));
Alexis Hetufb6639f2019-03-25 10:24:23 -0400118
119 // Vulkan 1.1 spec: "If VK_WHOLE_SIZE is used and the remaining size of the buffer is
120 // not a multiple of 4, then the nearest smaller multiple is used."
121 for(; bytes >= 4; bytes -= 4, memToWrite++)
122 {
123 *memToWrite = data;
124 }
Alexis Hetu7fb0b732019-02-07 13:50:10 -0500125}
126
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000127void Buffer::update(VkDeviceSize dstOffset, VkDeviceSize dataSize, const void *pData)
Alexis Hetu7fb0b732019-02-07 13:50:10 -0500128{
129 ASSERT((dataSize + dstOffset) <= size);
130
131 memcpy(getOffsetPointer(dstOffset), pData, dataSize);
132}
133
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000134void *Buffer::getOffsetPointer(VkDeviceSize offset) const
Alexis Hetu2661e842018-12-07 11:36:34 -0500135{
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000136 return reinterpret_cast<uint8_t *>(memory) + offset;
Alexis Hetu377077a2019-03-14 15:10:51 -0400137}
138
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000139uint8_t *Buffer::end() const
Alexis Hetu377077a2019-03-14 15:10:51 -0400140{
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000141 return reinterpret_cast<uint8_t *>(getOffsetPointer(size + 1));
Alexis Hetu2661e842018-12-07 11:36:34 -0500142}
143
Nicolas Capens157ba262019-12-10 17:49:14 -0500144} // namespace vk