Commit 38ff8304 by Alexis Hetu Committed by Alexis Hétu

Buffer, BufferView and DeviceMemory

This cl adds Buffer, BufferView and DeviceMemory. - DeviceMemory contains the appropriate logic to allocate and map device memory. - Buffer simply wraps a DeviceMemory and an offset for now. - BufferView wraps a Buffer with a memory region and a Format. Bug b/118383648 Change-Id: I6d53b9f0728d4cdec2696339cc6aa8ce2e05ca49 Reviewed-on: https://swiftshader-review.googlesource.com/c/21728Tested-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent fde88d96
// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "VkBuffer.hpp"
#include "VkConfig.h"
#include "VkDeviceMemory.hpp"
#include <memory.h>
namespace vk
{
Buffer::Buffer(const VkBufferCreateInfo* pCreateInfo, void* mem) :
flags(pCreateInfo->flags), size(pCreateInfo->size), usage(pCreateInfo->usage),
sharingMode(pCreateInfo->sharingMode), queueFamilyIndexCount(pCreateInfo->queueFamilyIndexCount),
queueFamilyIndices(reinterpret_cast<uint32_t*>(mem))
{
size_t queueFamilyIndicesSize = sizeof(uint32_t) * queueFamilyIndexCount;
memcpy(queueFamilyIndices, pCreateInfo->pQueueFamilyIndices, queueFamilyIndicesSize);
}
void Buffer::destroy(const VkAllocationCallbacks* pAllocator)
{
vk::deallocate(queueFamilyIndices, pAllocator);
}
size_t Buffer::ComputeRequiredAllocationSize(const VkBufferCreateInfo* pCreateInfo)
{
return sizeof(uint32_t) * pCreateInfo->queueFamilyIndexCount;
}
const VkMemoryRequirements Buffer::getMemoryRequirements() const
{
VkMemoryRequirements memoryRequirements = {};
memoryRequirements.alignment = vk::REQUIRED_MEMORY_ALIGNMENT;
memoryRequirements.memoryTypeBits = vk::REQUIRED_MEMORY_TYPE_BITS;
memoryRequirements.size = size; // TODO: also reserve space for a header containing
// the size of the buffer (for robust buffer access)
return memoryRequirements;
}
void Buffer::bind(VkDeviceMemory pDeviceMemory, VkDeviceSize pMemoryOffset)
{
memory = Cast(pDeviceMemory)->getOffsetPointer(pMemoryOffset);
}
} // namespace vk
// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VK_BUFFER_HPP_
#define VK_BUFFER_HPP_
#include "VkObject.hpp"
namespace vk
{
class Buffer : public Object<Buffer, VkBuffer>
{
public:
Buffer(const VkBufferCreateInfo* pCreateInfo, void* mem);
~Buffer() = delete;
void destroy(const VkAllocationCallbacks* pAllocator);
static size_t ComputeRequiredAllocationSize(const VkBufferCreateInfo* pCreateInfo);
const VkMemoryRequirements getMemoryRequirements() const;
void bind(VkDeviceMemory pDeviceMemory, VkDeviceSize pMemoryOffset);
private:
void* memory = nullptr;
VkBufferCreateFlags flags = 0;
VkDeviceSize size = 0;
VkBufferUsageFlags usage = 0;
VkSharingMode sharingMode = VK_SHARING_MODE_EXCLUSIVE;
uint32_t queueFamilyIndexCount = 0;
uint32_t* queueFamilyIndices = nullptr;
};
static inline Buffer* Cast(VkBuffer object)
{
return reinterpret_cast<Buffer*>(object);
}
} // namespace vk
#endif // VK_BUFFER_HPP_
// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VK_BUFFER_VIEW_HPP_
#define VK_BUFFER_VIEW_HPP_
#include "VkObject.hpp"
namespace vk
{
class BufferView : public Object<BufferView, VkBufferView>
{
public:
BufferView(const VkBufferViewCreateInfo* pCreateInfo, void* mem) :
buffer(pCreateInfo->buffer), format(pCreateInfo->format), offset(pCreateInfo->offset), range(pCreateInfo->range)
{
}
~BufferView() = delete;
static size_t ComputeRequiredAllocationSize(const VkBufferViewCreateInfo* pCreateInfo)
{
return 0;
}
private:
VkBuffer buffer;
VkFormat format;
VkDeviceSize offset;
VkDeviceSize range;
};
static inline BufferView* Cast(VkBufferView object)
{
return reinterpret_cast<BufferView*>(object);
}
} // namespace vk
#endif // VK_BUFFER_VIEW_HPP_
...@@ -12,8 +12,11 @@ ...@@ -12,8 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "VkBuffer.hpp"
#include "VkBufferView.hpp"
#include "VkCommandBuffer.hpp" #include "VkCommandBuffer.hpp"
#include "VkDevice.hpp" #include "VkDevice.hpp"
#include "VkDeviceMemory.hpp"
#include "VkEvent.hpp" #include "VkEvent.hpp"
#include "VkFence.hpp" #include "VkFence.hpp"
#include "VkInstance.hpp" #include "VkInstance.hpp"
......
...@@ -45,6 +45,7 @@ public: ...@@ -45,6 +45,7 @@ public:
VkPeerMemoryFeatureFlags* pPeerMemoryFeatures) const; VkPeerMemoryFeatureFlags* pPeerMemoryFeatures) const;
void getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo* pCreateInfo, void getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
VkDescriptorSetLayoutSupport* pSupport) const; VkDescriptorSetLayoutSupport* pSupport) const;
VkPhysicalDevice getPhysicalDevice() const { return physicalDevice; }
private: private:
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
......
// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "VkConfig.h"
#include "VkDeviceMemory.hpp"
namespace vk
{
DeviceMemory::DeviceMemory(const VkMemoryAllocateInfo* pCreateInfo, void* mem) :
size(pCreateInfo->allocationSize), buffer(nullptr), memoryTypeIndex(pCreateInfo->memoryTypeIndex)
{
ASSERT(size);
}
void DeviceMemory::destroy(const VkAllocationCallbacks* pAllocator)
{
vk::deallocate(buffer, DEVICE_MEMORY);
}
size_t DeviceMemory::ComputeRequiredAllocationSize(const VkMemoryAllocateInfo* pCreateInfo)
{
// buffer is "GPU memory", so we use device memory for it
return 0;
}
VkResult DeviceMemory::allocate()
{
if(!buffer)
{
buffer = vk::allocate(size, REQUIRED_MEMORY_ALIGNMENT, DEVICE_MEMORY);
}
if(!buffer)
{
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
return VK_SUCCESS;
}
VkResult DeviceMemory::map(VkDeviceSize pOffset, VkDeviceSize pSize, void** ppData)
{
*ppData = getOffsetPointer(pOffset);
return VK_SUCCESS;
}
VkDeviceSize DeviceMemory::getCommittedMemoryInBytes() const
{
return size;
}
void* DeviceMemory::getOffsetPointer(VkDeviceSize pOffset)
{
ASSERT(buffer);
return reinterpret_cast<char*>(buffer) + pOffset;
}
} // namespace vk
// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VK_DEVICE_MEMORY_HPP_
#define VK_DEVICE_MEMORY_HPP_
#include "VkObject.hpp"
namespace vk
{
class DeviceMemory : public Object<DeviceMemory, VkDeviceMemory>
{
public:
DeviceMemory(const VkMemoryAllocateInfo* pCreateInfo, void* mem);
~DeviceMemory() = delete;
static size_t ComputeRequiredAllocationSize(const VkMemoryAllocateInfo* pCreateInfo);
void destroy(const VkAllocationCallbacks* pAllocator);
VkResult allocate();
VkResult map(VkDeviceSize offset, VkDeviceSize size, void** ppData);
VkDeviceSize getCommittedMemoryInBytes() const;
void* getOffsetPointer(VkDeviceSize pOffset);
uint32_t getMemoryTypeIndex() const { return memoryTypeIndex; }
private:
void* buffer = nullptr;
VkDeviceSize size = 0;
uint32_t memoryTypeIndex = 0;
};
static inline DeviceMemory* Cast(VkDeviceMemory object)
{
return reinterpret_cast<DeviceMemory*>(object);
}
} // namespace vk
#endif // VK_DEVICE_MEMORY_HPP_
...@@ -94,29 +94,29 @@ void PhysicalDevice::getFeatures(VkPhysicalDeviceSamplerYcbcrConversionFeatures* ...@@ -94,29 +94,29 @@ void PhysicalDevice::getFeatures(VkPhysicalDeviceSamplerYcbcrConversionFeatures*
void PhysicalDevice::getFeatures(VkPhysicalDevice16BitStorageFeatures* features) const void PhysicalDevice::getFeatures(VkPhysicalDevice16BitStorageFeatures* features) const
{ {
features->storageBuffer16BitAccess = VK_FALSE; features->storageBuffer16BitAccess = VK_FALSE;
features->storageInputOutput16 = VK_FALSE; features->storageInputOutput16 = VK_FALSE;
features->storagePushConstant16 = VK_FALSE; features->storagePushConstant16 = VK_FALSE;
features->uniformAndStorageBuffer16BitAccess = VK_FALSE; features->uniformAndStorageBuffer16BitAccess = VK_FALSE;
} }
void PhysicalDevice::getFeatures(VkPhysicalDeviceVariablePointerFeatures* features) const void PhysicalDevice::getFeatures(VkPhysicalDeviceVariablePointerFeatures* features) const
{ {
features->variablePointersStorageBuffer = VK_FALSE; features->variablePointersStorageBuffer = VK_FALSE;
features->variablePointers = VK_FALSE; features->variablePointers = VK_FALSE;
} }
void PhysicalDevice::getFeatures(VkPhysicalDevice8BitStorageFeaturesKHR* features) const void PhysicalDevice::getFeatures(VkPhysicalDevice8BitStorageFeaturesKHR* features) const
{ {
features->storageBuffer8BitAccess = VK_FALSE; features->storageBuffer8BitAccess = VK_FALSE;
features->uniformAndStorageBuffer8BitAccess = VK_FALSE; features->uniformAndStorageBuffer8BitAccess = VK_FALSE;
features->storagePushConstant8 = VK_FALSE; features->storagePushConstant8 = VK_FALSE;
} }
void PhysicalDevice::getFeatures(VkPhysicalDeviceMultiviewFeatures* features) const void PhysicalDevice::getFeatures(VkPhysicalDeviceMultiviewFeatures* features) const
{ {
features->multiview = VK_FALSE; features->multiview = VK_FALSE;
features->multiviewGeometryShader = VK_FALSE; features->multiviewGeometryShader = VK_FALSE;
features->multiviewTessellationShader = VK_FALSE; features->multiviewTessellationShader = VK_FALSE;
} }
...@@ -272,24 +272,24 @@ const VkPhysicalDeviceProperties& PhysicalDevice::getProperties() const ...@@ -272,24 +272,24 @@ const VkPhysicalDeviceProperties& PhysicalDevice::getProperties() const
} }
void PhysicalDevice::getProperties(VkPhysicalDeviceIDProperties* properties) const void PhysicalDevice::getProperties(VkPhysicalDeviceIDProperties* properties) const
{ {
memcpy(properties->deviceUUID, SWIFTSHADER_UUID, VK_UUID_SIZE); memcpy(properties->deviceUUID, SWIFTSHADER_UUID, VK_UUID_SIZE);
memset(properties->deviceLUID, 0, VK_LUID_SIZE); memset(properties->deviceLUID, 0, VK_LUID_SIZE);
memset(properties->driverUUID, 0, VK_UUID_SIZE); memset(properties->driverUUID, 0, VK_UUID_SIZE);
*((uint64_t*)properties->driverUUID) = DRIVER_VERSION; *((uint64_t*)properties->driverUUID) = DRIVER_VERSION;
properties->deviceNodeMask = 0; properties->deviceNodeMask = 0;
properties->deviceLUIDValid = VK_FALSE; properties->deviceLUIDValid = VK_FALSE;
} }
void PhysicalDevice::getProperties(VkPhysicalDeviceMaintenance3Properties* properties) const void PhysicalDevice::getProperties(VkPhysicalDeviceMaintenance3Properties* properties) const
{ {
properties->maxMemoryAllocationSize = 1 << 31; properties->maxMemoryAllocationSize = 1 << 31;
properties->maxPerSetDescriptors = 1024; properties->maxPerSetDescriptors = 1024;
} }
void PhysicalDevice::getProperties(VkPhysicalDeviceMultiviewProperties* properties) const void PhysicalDevice::getProperties(VkPhysicalDeviceMultiviewProperties* properties) const
{ {
properties->maxMultiviewInstanceIndex = (1 << 27) - 1; properties->maxMultiviewInstanceIndex = (1 << 27) - 1;
properties->maxMultiviewViewCount = 6; properties->maxMultiviewViewCount = 6;
} }
...@@ -305,9 +305,9 @@ void PhysicalDevice::getProperties(VkPhysicalDeviceProtectedMemoryProperties* pr ...@@ -305,9 +305,9 @@ void PhysicalDevice::getProperties(VkPhysicalDeviceProtectedMemoryProperties* pr
void PhysicalDevice::getProperties(VkPhysicalDeviceSubgroupProperties* properties) const void PhysicalDevice::getProperties(VkPhysicalDeviceSubgroupProperties* properties) const
{ {
properties->subgroupSize = 1; properties->subgroupSize = 1;
properties->supportedStages = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_COMPUTE_BIT; properties->supportedStages = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_COMPUTE_BIT;
properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT; properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT;
properties->quadOperationsInAllStages = VK_FALSE; properties->quadOperationsInAllStages = VK_FALSE;
} }
......
...@@ -98,9 +98,11 @@ copy "$(OutDir)vk_swiftshader.dll" "$(SolutionDir)out\$(Configuration)_$(Platfor ...@@ -98,9 +98,11 @@ copy "$(OutDir)vk_swiftshader.dll" "$(SolutionDir)out\$(Configuration)_$(Platfor
<ItemGroup> <ItemGroup>
<ClCompile Include="libVulkan.cpp" /> <ClCompile Include="libVulkan.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="VkBuffer.cpp" />
<ClCompile Include="VkCommandBuffer.cpp" /> <ClCompile Include="VkCommandBuffer.cpp" />
<ClCompile Include="VkDebug.cpp" /> <ClCompile Include="VkDebug.cpp" />
<ClCompile Include="VkDevice.cpp" /> <ClCompile Include="VkDevice.cpp" />
<ClCompile Include="VkDeviceMemory.cpp" />
<ClCompile Include="VkGetProcAddress.cpp" /> <ClCompile Include="VkGetProcAddress.cpp" />
<ClCompile Include="VkInstance.cpp" /> <ClCompile Include="VkInstance.cpp" />
<ClCompile Include="VkMemory.cpp" /> <ClCompile Include="VkMemory.cpp" />
...@@ -180,11 +182,14 @@ copy "$(OutDir)vk_swiftshader.dll" "$(SolutionDir)out\$(Configuration)_$(Platfor ...@@ -180,11 +182,14 @@ copy "$(OutDir)vk_swiftshader.dll" "$(SolutionDir)out\$(Configuration)_$(Platfor
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="VkBuffer.hpp" />
<ClInclude Include="VkBufferView.hpp" />
<ClInclude Include="VkCommandBuffer.hpp" /> <ClInclude Include="VkCommandBuffer.hpp" />
<ClInclude Include="VkConfig.h" /> <ClInclude Include="VkConfig.h" />
<ClInclude Include="VkDebug.hpp" /> <ClInclude Include="VkDebug.hpp" />
<ClInclude Include="VkDestroy.h" /> <ClInclude Include="VkDestroy.h" />
<ClInclude Include="VkDevice.hpp" /> <ClInclude Include="VkDevice.hpp" />
<ClInclude Include="VkDeviceMemory.hpp" />
<ClInclude Include="VkEvent.hpp" /> <ClInclude Include="VkEvent.hpp" />
<ClInclude Include="VkFence.hpp" /> <ClInclude Include="VkFence.hpp" />
<ClInclude Include="VkGetProcAddress.h" /> <ClInclude Include="VkGetProcAddress.h" />
......
...@@ -207,6 +207,9 @@ ...@@ -207,6 +207,9 @@
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>Source Files\Vulkan</Filter> <Filter>Source Files\Vulkan</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="VkBuffer.cpp">
<Filter>Source Files\Vulkan</Filter>
</ClCompile>
<ClCompile Include="VkCommandBuffer.cpp"> <ClCompile Include="VkCommandBuffer.cpp">
<Filter>Source Files\Vulkan</Filter> <Filter>Source Files\Vulkan</Filter>
</ClCompile> </ClCompile>
...@@ -216,6 +219,9 @@ ...@@ -216,6 +219,9 @@
<ClCompile Include="VkDevice.cpp"> <ClCompile Include="VkDevice.cpp">
<Filter>Source Files\Vulkan</Filter> <Filter>Source Files\Vulkan</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="VkDeviceMemory.cpp">
<Filter>Source Files\Vulkan</Filter>
</ClCompile>
<ClCompile Include="VkGetProcAddress.cpp"> <ClCompile Include="VkGetProcAddress.cpp">
<Filter>Source Files\Vulkan</Filter> <Filter>Source Files\Vulkan</Filter>
</ClCompile> </ClCompile>
...@@ -239,6 +245,12 @@ ...@@ -239,6 +245,12 @@
<ClInclude Include="resource.h"> <ClInclude Include="resource.h">
<Filter>Header Files\Vulkan</Filter> <Filter>Header Files\Vulkan</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="VkBuffer.hpp">
<Filter>Header Files\Vulkan</Filter>
</ClInclude>
<ClInclude Include="VkBufferView.hpp">
<Filter>Header Files\Vulkan</Filter>
</ClInclude>
<ClInclude Include="VkCommandBuffer.hpp"> <ClInclude Include="VkCommandBuffer.hpp">
<Filter>Header Files\Vulkan</Filter> <Filter>Header Files\Vulkan</Filter>
</ClInclude> </ClInclude>
...@@ -248,6 +260,9 @@ ...@@ -248,6 +260,9 @@
<ClInclude Include="VkDevice.hpp"> <ClInclude Include="VkDevice.hpp">
<Filter>Header Files\Vulkan</Filter> <Filter>Header Files\Vulkan</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="VkDeviceMemory.hpp">
<Filter>Header Files\Vulkan</Filter>
</ClInclude>
<ClInclude Include="VkEvent.hpp"> <ClInclude Include="VkEvent.hpp">
<Filter>Header Files\Vulkan</Filter> <Filter>Header Files\Vulkan</Filter>
</ClInclude> </ClInclude>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment