Skip to content

Commit

Permalink
[Vulkan] Change VkMemoryBlock into a struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
mellinoe committed Apr 8, 2018
1 parent 66b9326 commit 4b908fa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/Veldrid/Vk/VkDeviceMemoryManager.cs
Expand Up @@ -214,7 +214,7 @@ public bool Allocate(ulong size, ulong alignment, out VkMemoryBlock block)
}
}

block = null;
block = default(VkMemoryBlock);
return false;
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ internal IntPtr Map(VkMemoryBlock memoryBlock)
}

[DebuggerDisplay("[Mem:{DeviceMemory.Handle}] Off:{Offset}, Size:{Size}")]
internal unsafe class VkMemoryBlock
internal unsafe struct VkMemoryBlock : IEquatable<VkMemoryBlock>
{
public readonly uint MemoryTypeIndex;
public readonly VkDeviceMemory DeviceMemory;
Expand All @@ -308,5 +308,12 @@ public VkMemoryBlock(VkDeviceMemory memory, ulong offset, ulong size, uint memor
MemoryTypeIndex = memoryTypeIndex;
BaseMappedPointer = mappedPtr;
}

public bool Equals(VkMemoryBlock other)
{
return DeviceMemory.Equals(other.DeviceMemory)
&& Offset.Equals(other.Offset)
&& Size.Equals(other.Size);
}
}
}
8 changes: 4 additions & 4 deletions src/Veldrid/Vk/VkGraphicsDevice.cs
Expand Up @@ -722,7 +722,7 @@ private void CreateGraphicsCommandPool()

protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource)
{
VkMemoryBlock memoryBlock = null;
VkMemoryBlock memoryBlock = default(VkMemoryBlock);
IntPtr mappedPtr = IntPtr.Zero;
uint sizeInBytes;
uint offset = 0;
Expand All @@ -744,7 +744,7 @@ protected override MappedResource MapCore(MappableResource resource, MapMode mod
depthPitch = (uint)layout.depthPitch;
}

if (memoryBlock != null)
if (memoryBlock.DeviceMemory.Handle != 0)
{
if (memoryBlock.IsPersistentMapped)
{
Expand All @@ -769,7 +769,7 @@ protected override MappedResource MapCore(MappableResource resource, MapMode mod

protected override void UnmapCore(MappableResource resource, uint subresource)
{
VkMemoryBlock memoryBlock = null;
VkMemoryBlock memoryBlock = default(VkMemoryBlock);
if (resource is VkBuffer buffer)
{
memoryBlock = buffer.Memory;
Expand All @@ -780,7 +780,7 @@ protected override void UnmapCore(MappableResource resource, uint subresource)
memoryBlock = tex.Memory;
}

if (memoryBlock != null && !memoryBlock.IsPersistentMapped)
if (memoryBlock.DeviceMemory.Handle != 0 && !memoryBlock.IsPersistentMapped)
{
vkUnmapMemory(_device, memoryBlock.DeviceMemory);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Veldrid/Vk/VkTexture.cs
Expand Up @@ -353,7 +353,7 @@ public override void Dispose()
vkDestroyImage(_gd.Device, _optimalImage, null);
}

if (_memoryBlock != null)
if (_memoryBlock.DeviceMemory.Handle != 0)
{
_gd.MemoryManager.Free(_memoryBlock);
}
Expand Down

0 comments on commit 4b908fa

Please sign in to comment.