mirror of
https://github.com/baldurk/renderdoc.git
synced 2026-05-04 17:10:47 +00:00
Move GPUBuffer helper class out of VulkanDebugManager
This commit is contained in:
@@ -126,6 +126,116 @@ void VkMarkerRegion::End(VkCommandBuffer cmd)
|
||||
ObjDisp(scope.cmd)->CmdDebugMarkerEndEXT(Unwrap(scope.cmd));
|
||||
}
|
||||
|
||||
void GPUBuffer::Create(WrappedVulkan *driver, VkDevice dev, VkDeviceSize size, uint32_t ringSize,
|
||||
uint32_t flags)
|
||||
{
|
||||
m_pDriver = driver;
|
||||
device = dev;
|
||||
|
||||
align = (VkDeviceSize)driver->GetDeviceProps().limits.minUniformBufferOffsetAlignment;
|
||||
|
||||
sz = size;
|
||||
// offset must be aligned, so ensure we have at least ringSize
|
||||
// copies accounting for that
|
||||
totalsize = ringSize == 1 ? size : AlignUp(size, align) * ringSize;
|
||||
curoffset = 0;
|
||||
|
||||
ringCount = ringSize;
|
||||
|
||||
VkBufferCreateInfo bufInfo = {
|
||||
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, totalsize, 0,
|
||||
};
|
||||
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||||
|
||||
if(flags & eGPUBufferVBuffer)
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
||||
|
||||
if(flags & eGPUBufferIBuffer)
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
||||
|
||||
if(flags & eGPUBufferSSBO)
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
||||
|
||||
VkResult vkr = driver->vkCreateBuffer(dev, &bufInfo, NULL, &buf);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
|
||||
VkMemoryRequirements mrq = {};
|
||||
driver->vkGetBufferMemoryRequirements(dev, buf, &mrq);
|
||||
|
||||
VkMemoryAllocateInfo allocInfo = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, NULL, mrq.size, 0};
|
||||
|
||||
if(flags & eGPUBufferReadback)
|
||||
allocInfo.memoryTypeIndex = driver->GetReadbackMemoryIndex(mrq.memoryTypeBits);
|
||||
else if(flags & eGPUBufferGPULocal)
|
||||
allocInfo.memoryTypeIndex = driver->GetGPULocalMemoryIndex(mrq.memoryTypeBits);
|
||||
else
|
||||
allocInfo.memoryTypeIndex = driver->GetUploadMemoryIndex(mrq.memoryTypeBits);
|
||||
|
||||
vkr = driver->vkAllocateMemory(dev, &allocInfo, NULL, &mem);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
|
||||
vkr = driver->vkBindBufferMemory(dev, buf, mem, 0);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
}
|
||||
|
||||
void GPUBuffer::FillDescriptor(VkDescriptorBufferInfo &desc)
|
||||
{
|
||||
desc.buffer = Unwrap(buf);
|
||||
desc.offset = 0;
|
||||
desc.range = sz;
|
||||
}
|
||||
|
||||
void GPUBuffer::Destroy()
|
||||
{
|
||||
if(device != VK_NULL_HANDLE)
|
||||
{
|
||||
m_pDriver->vkDestroyBuffer(device, buf, NULL);
|
||||
m_pDriver->vkFreeMemory(device, mem, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void *GPUBuffer::Map(uint32_t *bindoffset, VkDeviceSize usedsize)
|
||||
{
|
||||
VkDeviceSize offset = bindoffset ? curoffset : 0;
|
||||
VkDeviceSize size = usedsize > 0 ? usedsize : sz;
|
||||
|
||||
// wrap around the ring, assuming the ring is large enough
|
||||
// that this memory is now free
|
||||
if(offset + sz > totalsize)
|
||||
offset = 0;
|
||||
RDCASSERT(offset + sz <= totalsize);
|
||||
|
||||
// offset must be aligned
|
||||
curoffset = AlignUp(offset + size, align);
|
||||
|
||||
if(bindoffset)
|
||||
*bindoffset = (uint32_t)offset;
|
||||
|
||||
void *ptr = NULL;
|
||||
VkResult vkr = m_pDriver->vkMapMemory(device, mem, offset, size, 0, (void **)&ptr);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *GPUBuffer::Map(VkDeviceSize &bindoffset, VkDeviceSize usedsize)
|
||||
{
|
||||
uint32_t offs = 0;
|
||||
|
||||
void *ret = Map(&offs, usedsize);
|
||||
|
||||
bindoffset = offs;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void GPUBuffer::Unmap()
|
||||
{
|
||||
m_pDriver->vkUnmapMemory(device, mem);
|
||||
}
|
||||
|
||||
bool VkInitParams::IsSupportedVersion(uint64_t ver)
|
||||
{
|
||||
if(ver == CurrentVersion)
|
||||
|
||||
@@ -136,6 +136,45 @@ struct VkMarkerRegion
|
||||
static WrappedVulkan *vk;
|
||||
};
|
||||
|
||||
struct GPUBuffer
|
||||
{
|
||||
enum CreateFlags
|
||||
{
|
||||
eGPUBufferReadback = 0x1,
|
||||
eGPUBufferVBuffer = 0x2,
|
||||
eGPUBufferIBuffer = 0x4,
|
||||
eGPUBufferSSBO = 0x8,
|
||||
eGPUBufferGPULocal = 0x10,
|
||||
};
|
||||
|
||||
void Create(WrappedVulkan *driver, VkDevice dev, VkDeviceSize size, uint32_t ringSize,
|
||||
uint32_t flags);
|
||||
void Destroy();
|
||||
|
||||
void FillDescriptor(VkDescriptorBufferInfo &desc);
|
||||
|
||||
size_t GetRingCount() { return size_t(ringCount); }
|
||||
void *Map(VkDeviceSize &bindoffset, VkDeviceSize usedsize = 0);
|
||||
void *Map(uint32_t *bindoffset = NULL, VkDeviceSize usedsize = 0);
|
||||
void Unmap();
|
||||
|
||||
VkDeviceSize sz = 0;
|
||||
VkBuffer buf = VK_NULL_HANDLE;
|
||||
VkDeviceMemory mem = VK_NULL_HANDLE;
|
||||
|
||||
// uniform buffer alignment requirement
|
||||
VkDeviceSize align = 0;
|
||||
|
||||
// for handling ring allocations
|
||||
VkDeviceSize totalsize = 0;
|
||||
VkDeviceSize curoffset = 0;
|
||||
|
||||
uint32_t ringCount = 0;
|
||||
|
||||
WrappedVulkan *m_pDriver = NULL;
|
||||
VkDevice device = VK_NULL_HANDLE;
|
||||
};
|
||||
|
||||
// in vk_<platform>.cpp
|
||||
extern const char *VulkanLibraryName;
|
||||
|
||||
|
||||
@@ -397,17 +397,11 @@ private:
|
||||
vector<VkEvent> m_CleanupEvents;
|
||||
vector<VkEvent> m_PersistentEvents;
|
||||
|
||||
const VkPhysicalDeviceProperties &GetDeviceProps() { return m_PhysicalDeviceData.props; }
|
||||
VkDriverInfo GetDriverVersion() { return VkDriverInfo(m_PhysicalDeviceData.props); }
|
||||
const VkFormatProperties &GetFormatProperties(VkFormat f)
|
||||
{
|
||||
return m_PhysicalDeviceData.fmtprops[f];
|
||||
}
|
||||
|
||||
uint32_t GetReadbackMemoryIndex(uint32_t resourceRequiredBitmask);
|
||||
uint32_t GetUploadMemoryIndex(uint32_t resourceRequiredBitmask);
|
||||
uint32_t GetGPULocalMemoryIndex(uint32_t resourceRequiredBitmask);
|
||||
|
||||
struct BakedCmdBufferInfo
|
||||
{
|
||||
BakedCmdBufferInfo()
|
||||
@@ -776,6 +770,10 @@ public:
|
||||
return m_DescriptorSetState[descSet].layout;
|
||||
}
|
||||
|
||||
uint32_t GetReadbackMemoryIndex(uint32_t resourceRequiredBitmask);
|
||||
uint32_t GetUploadMemoryIndex(uint32_t resourceRequiredBitmask);
|
||||
uint32_t GetGPULocalMemoryIndex(uint32_t resourceRequiredBitmask);
|
||||
|
||||
vector<EventUsage> GetUsage(ResourceId id) { return m_ResourceUses[id]; }
|
||||
// return the pre-selected device and queue
|
||||
VkDevice GetDev()
|
||||
@@ -821,6 +819,8 @@ public:
|
||||
VkExtensionProperties *pProperties);
|
||||
|
||||
const VkPhysicalDeviceFeatures &GetDeviceFeatures() { return m_PhysicalDeviceData.features; }
|
||||
const VkPhysicalDeviceProperties &GetDeviceProps() { return m_PhysicalDeviceData.props; }
|
||||
VkDriverInfo GetDriverVersion() { return VkDriverInfo(m_PhysicalDeviceData.props); }
|
||||
// Device initialization
|
||||
|
||||
IMPLEMENT_FUNCTION_SERIALISED(VkResult, vkCreateInstance, const VkInstanceCreateInfo *pCreateInfo,
|
||||
|
||||
@@ -40,116 +40,6 @@
|
||||
|
||||
const VkDeviceSize STAGE_BUFFER_BYTE_SIZE = 16 * 1024 * 1024ULL;
|
||||
|
||||
void VulkanDebugManager::GPUBuffer::Create(WrappedVulkan *driver, VkDevice dev, VkDeviceSize size,
|
||||
uint32_t ringSize, uint32_t flags)
|
||||
{
|
||||
m_pDriver = driver;
|
||||
device = dev;
|
||||
|
||||
align = (VkDeviceSize)driver->GetDeviceProps().limits.minUniformBufferOffsetAlignment;
|
||||
|
||||
sz = size;
|
||||
// offset must be aligned, so ensure we have at least ringSize
|
||||
// copies accounting for that
|
||||
totalsize = ringSize == 1 ? size : AlignUp(size, align) * ringSize;
|
||||
curoffset = 0;
|
||||
|
||||
ringCount = ringSize;
|
||||
|
||||
VkBufferCreateInfo bufInfo = {
|
||||
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, totalsize, 0,
|
||||
};
|
||||
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||||
|
||||
if(flags & eGPUBufferVBuffer)
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
||||
|
||||
if(flags & eGPUBufferIBuffer)
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
||||
|
||||
if(flags & eGPUBufferSSBO)
|
||||
bufInfo.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
||||
|
||||
VkResult vkr = driver->vkCreateBuffer(dev, &bufInfo, NULL, &buf);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
|
||||
VkMemoryRequirements mrq = {};
|
||||
driver->vkGetBufferMemoryRequirements(dev, buf, &mrq);
|
||||
|
||||
VkMemoryAllocateInfo allocInfo = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, NULL, mrq.size, 0};
|
||||
|
||||
if(flags & eGPUBufferReadback)
|
||||
allocInfo.memoryTypeIndex = driver->GetReadbackMemoryIndex(mrq.memoryTypeBits);
|
||||
else if(flags & eGPUBufferGPULocal)
|
||||
allocInfo.memoryTypeIndex = driver->GetGPULocalMemoryIndex(mrq.memoryTypeBits);
|
||||
else
|
||||
allocInfo.memoryTypeIndex = driver->GetUploadMemoryIndex(mrq.memoryTypeBits);
|
||||
|
||||
vkr = driver->vkAllocateMemory(dev, &allocInfo, NULL, &mem);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
|
||||
vkr = driver->vkBindBufferMemory(dev, buf, mem, 0);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
}
|
||||
|
||||
void VulkanDebugManager::GPUBuffer::FillDescriptor(VkDescriptorBufferInfo &desc)
|
||||
{
|
||||
desc.buffer = Unwrap(buf);
|
||||
desc.offset = 0;
|
||||
desc.range = sz;
|
||||
}
|
||||
|
||||
void VulkanDebugManager::GPUBuffer::Destroy()
|
||||
{
|
||||
if(device != VK_NULL_HANDLE)
|
||||
{
|
||||
m_pDriver->vkDestroyBuffer(device, buf, NULL);
|
||||
m_pDriver->vkFreeMemory(device, mem, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void *VulkanDebugManager::GPUBuffer::Map(uint32_t *bindoffset, VkDeviceSize usedsize)
|
||||
{
|
||||
VkDeviceSize offset = bindoffset ? curoffset : 0;
|
||||
VkDeviceSize size = usedsize > 0 ? usedsize : sz;
|
||||
|
||||
// wrap around the ring, assuming the ring is large enough
|
||||
// that this memory is now free
|
||||
if(offset + sz > totalsize)
|
||||
offset = 0;
|
||||
RDCASSERT(offset + sz <= totalsize);
|
||||
|
||||
// offset must be aligned
|
||||
curoffset = AlignUp(offset + size, align);
|
||||
|
||||
if(bindoffset)
|
||||
*bindoffset = (uint32_t)offset;
|
||||
|
||||
void *ptr = NULL;
|
||||
VkResult vkr = m_pDriver->vkMapMemory(device, mem, offset, size, 0, (void **)&ptr);
|
||||
RDCASSERTEQUAL(vkr, VK_SUCCESS);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *VulkanDebugManager::GPUBuffer::Map(VkDeviceSize &bindoffset, VkDeviceSize usedsize)
|
||||
{
|
||||
uint32_t offs = 0;
|
||||
|
||||
void *ret = Map(&offs, usedsize);
|
||||
|
||||
bindoffset = offs;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void VulkanDebugManager::GPUBuffer::Unmap()
|
||||
{
|
||||
m_pDriver->vkUnmapMemory(device, mem);
|
||||
}
|
||||
|
||||
struct VulkanBlobShaderCallbacks
|
||||
{
|
||||
bool Create(uint32_t size, byte *data, vector<uint32_t> **ret) const
|
||||
|
||||
@@ -138,56 +138,6 @@ public:
|
||||
void ReplaceResource(ResourceId from, ResourceId to);
|
||||
void RemoveReplacement(ResourceId id);
|
||||
|
||||
struct GPUBuffer
|
||||
{
|
||||
enum CreateFlags
|
||||
{
|
||||
eGPUBufferReadback = 0x1,
|
||||
eGPUBufferVBuffer = 0x2,
|
||||
eGPUBufferIBuffer = 0x4,
|
||||
eGPUBufferSSBO = 0x8,
|
||||
eGPUBufferGPULocal = 0x10,
|
||||
};
|
||||
GPUBuffer()
|
||||
: sz(0),
|
||||
buf(VK_NULL_HANDLE),
|
||||
mem(VK_NULL_HANDLE),
|
||||
align(0),
|
||||
totalsize(0),
|
||||
curoffset(0),
|
||||
ringCount(0),
|
||||
m_pDriver(NULL),
|
||||
device(VK_NULL_HANDLE)
|
||||
{
|
||||
}
|
||||
void Create(WrappedVulkan *driver, VkDevice dev, VkDeviceSize size, uint32_t ringSize,
|
||||
uint32_t flags);
|
||||
void Destroy();
|
||||
|
||||
void FillDescriptor(VkDescriptorBufferInfo &desc);
|
||||
|
||||
size_t GetRingCount() { return size_t(ringCount); }
|
||||
void *Map(VkDeviceSize &bindoffset, VkDeviceSize usedsize = 0);
|
||||
void *Map(uint32_t *bindoffset = NULL, VkDeviceSize usedsize = 0);
|
||||
void Unmap();
|
||||
|
||||
VkDeviceSize sz;
|
||||
VkBuffer buf;
|
||||
VkDeviceMemory mem;
|
||||
|
||||
// uniform buffer alignment requirement
|
||||
VkDeviceSize align;
|
||||
|
||||
// for handling ring allocations
|
||||
VkDeviceSize totalsize;
|
||||
VkDeviceSize curoffset;
|
||||
|
||||
uint32_t ringCount;
|
||||
|
||||
WrappedVulkan *m_pDriver;
|
||||
VkDevice device;
|
||||
};
|
||||
|
||||
VkDescriptorPool m_DescriptorPool;
|
||||
VkSampler m_LinearSampler, m_PointSampler;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user