84 lines
2.9 KiB
C++
84 lines
2.9 KiB
C++
/*
|
|
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
|
* its licensors.
|
|
*
|
|
* For complete copyright and license terms please see the LICENSE at the root of this
|
|
* distribution (the "License"). All use of this software is governed by the License,
|
|
* or, if provided, by the license below or the license accompanying this file. Do not
|
|
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
*
|
|
*/
|
|
#pragma once
|
|
|
|
#include <AzCore/Memory/PoolAllocator.h>
|
|
#include <AzCore/std/containers/map.h>
|
|
#include <AzCore/std/parallel/mutex.h>
|
|
#include <Atom/RHI/DeviceObject.h>
|
|
|
|
namespace AZ
|
|
{
|
|
namespace Vulkan
|
|
{
|
|
class Device;
|
|
|
|
/**
|
|
* It holds Vulkan's native VkDeviceMemory.
|
|
* When it is removed, VkDeviceMemory will be freed.
|
|
* Allows simultaneous mappings of non overlapping ranges.
|
|
*/
|
|
class Memory
|
|
: public RHI::DeviceObject
|
|
{
|
|
using Base = RHI::DeviceObject;
|
|
|
|
public:
|
|
AZ_CLASS_ALLOCATOR(Memory, AZ::ThreadPoolAllocator, 0);
|
|
AZ_RTTI(Memory, "A5D92BC7-2BD8-46BB-85AB-ED549982130E", Base);
|
|
|
|
struct Descriptor
|
|
{
|
|
VkDeviceSize m_sizeInBytes = 0;
|
|
uint32_t m_memoryTypeIndex = 0;
|
|
};
|
|
|
|
~Memory() = default;
|
|
|
|
static RHI::Ptr<Memory> Create();
|
|
RHI::ResultCode Init(Device& device, const Descriptor& descriptor);
|
|
|
|
const VkDeviceMemory GetNativeDeviceMemory();
|
|
const Descriptor& GetDescriptor() const;
|
|
|
|
// It maps a memory and returns its mapped address.
|
|
CpuVirtualAddress Map(size_t offset, size_t size, RHI::HostMemoryAccess hostAccess);
|
|
|
|
// Unmap have to be called after Map is called for memory.
|
|
void Unmap(size_t offset, RHI::HostMemoryAccess hostAccess);
|
|
|
|
private:
|
|
Memory() = default;
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// RHI::Object
|
|
void SetNameInternal(const AZStd::string_view& name) override;
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// RHI::DeviceObject
|
|
void Shutdown() override;
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
Descriptor m_descriptor;
|
|
VkDeviceMemory m_nativeDeviceMemory = VK_NULL_HANDLE;
|
|
CpuVirtualAddress m_mappedMemory = nullptr;
|
|
VkMemoryPropertyFlags m_memoryPropertyFlags = 0;
|
|
|
|
using MappingMap = AZStd::map<size_t, size_t>;
|
|
MappingMap m_mappings;
|
|
|
|
AZStd::mutex m_mutex;
|
|
};
|
|
}
|
|
}
|