1 // 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 #ifndef VK_BUFFER_HPP_
16 #define VK_BUFFER_HPP_
17
18 #include "VkObject.hpp"
19
20 namespace vk {
21
22 class DeviceMemory;
23
24 class Buffer : public Object<Buffer, VkBuffer>
25 {
26 public:
27 Buffer(const VkBufferCreateInfo *pCreateInfo, void *mem);
28 void destroy(const VkAllocationCallbacks *pAllocator);
29
30 static size_t ComputeRequiredAllocationSize(const VkBufferCreateInfo *pCreateInfo);
31 static const VkMemoryRequirements GetMemoryRequirements(VkDeviceSize size, VkBufferUsageFlags usage);
32
33 const VkMemoryRequirements getMemoryRequirements() const;
34 void bind(DeviceMemory *pDeviceMemory, VkDeviceSize pMemoryOffset);
35 void copyFrom(const void *srcMemory, VkDeviceSize size, VkDeviceSize offset);
36 void copyTo(void *dstMemory, VkDeviceSize size, VkDeviceSize offset) const;
37 void copyTo(Buffer *dstBuffer, const VkBufferCopy2KHR &pRegion) const;
38 void fill(VkDeviceSize dstOffset, VkDeviceSize fillSize, uint32_t data);
39 void update(VkDeviceSize dstOffset, VkDeviceSize dataSize, const void *pData);
40 void *getOffsetPointer(VkDeviceSize offset) const;
41 uint64_t getOpaqueCaptureAddress() const;
getSize() const42 inline VkDeviceSize getSize() const { return size; }
43 uint8_t *end() const;
44 bool canBindToMemory(DeviceMemory *pDeviceMemory) const;
45
getUsage() const46 VkBufferUsageFlags getUsage() const { return usage; }
getFlags() const47 VkBufferCreateFlags getFlags() const { return flags; }
48
49 private:
50 void *memory = nullptr;
51 VkBufferCreateFlags flags = 0;
52 VkDeviceSize size = 0;
53 VkBufferUsageFlags usage = 0;
54 VkSharingMode sharingMode = VK_SHARING_MODE_EXCLUSIVE;
55 uint32_t queueFamilyIndexCount = 0;
56 uint32_t *queueFamilyIndices = nullptr;
57 uint64_t opaqueCaptureAddress = 0;
58
59 VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleTypes = (VkExternalMemoryHandleTypeFlags)0;
60 };
61
Cast(VkBuffer object)62 static inline Buffer *Cast(VkBuffer object)
63 {
64 return Buffer::Cast(object);
65 }
66
67 } // namespace vk
68
69 #endif // VK_BUFFER_HPP_
70