1 // Copyright 2021 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 "Buffer.hpp"
16
Buffer(vk::Device device,vk::DeviceSize size,vk::BufferUsageFlags usage)17 Buffer::Buffer(vk::Device device, vk::DeviceSize size, vk::BufferUsageFlags usage)
18 : device(device)
19 , size(size)
20 {
21 vk::BufferCreateInfo bufferInfo{};
22 bufferInfo.size = size;
23 bufferInfo.usage = usage;
24 bufferInfo.sharingMode = vk::SharingMode::eExclusive;
25
26 buffer = device.createBuffer(bufferInfo);
27
28 auto memRequirements = device.getBufferMemoryRequirements(buffer);
29
30 vk::MemoryAllocateInfo allocInfo{};
31 allocInfo.allocationSize = memRequirements.size;
32 allocInfo.memoryTypeIndex = 0; //TODO: getMemoryTypeIndex(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
33
34 bufferMemory = device.allocateMemory(allocInfo);
35 device.bindBufferMemory(buffer, bufferMemory, 0);
36 }
37
~Buffer()38 Buffer::~Buffer()
39 {
40 device.freeMemory(bufferMemory);
41 device.destroyBuffer(buffer);
42 }
43