Skip to content

Commit 4ea5121

Browse files
committed
[Vk] Align mapping to nonCoherentAtomSize
1 parent 83f24ed commit 4ea5121

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/Veldrid/MapMode.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,30 @@ public enum MapMode : byte
99
/// <summary>
1010
/// A read-only resource mapping. The mapped data region is not writable, and cannot be used to transfer data into the
1111
/// graphics resource.
12-
/// NOTE: This mode can only be used on resources created with the Staging usage flag.
1312
/// </summary>
13+
/// <remarks>
14+
/// This mode can only be used on resources created with the Staging usage flag.
15+
/// </remarks>
1416
Read = 1 << 0,
1517

1618
/// <summary>
1719
/// A write-only resource mapping. The mapped data region is writable, and will be transferred into the graphics resource
1820
/// when <see cref="GraphicsDevice.Unmap(MappableResource, uint)"/> is called.
19-
/// NOTE: upon mapping a buffer with this
21+
/// </summary>
22+
/// <remarks>
23+
/// Upon mapping a buffer with this
2024
/// mode, the previous contents of the resource will be erased. This mode can only be used to entirely replace the
2125
/// contents of a resource.
22-
/// </summary>
26+
/// </remarks>
2327
Write = 1 << 1,
2428

2529
/// <summary>
2630
/// A read-write resource mapping. The mapped data region is both readable and writable.
27-
/// NOTE: this mode can only be used
28-
/// on resources created with the Staging usage flag.
2931
/// </summary>
32+
/// <remarks>
33+
/// This mode can only be used
34+
/// on resources created with the Staging usage flag.
35+
/// </remarks>
3036
ReadWrite = Write | Read,
3137
}
3238
}

src/Veldrid/Vk/VkGraphicsDevice.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,6 @@ private protected override MappedResource MapCore(
10051005
if (resource is VkBuffer buffer)
10061006
{
10071007
memoryBlock = buffer.Memory;
1008-
sizeInBytes = buffer.SizeInBytes;
10091008
}
10101009
else
10111010
{
@@ -1020,15 +1019,20 @@ private protected override MappedResource MapCore(
10201019

10211020
if (memoryBlock.DeviceMemory != VkDeviceMemory.NULL)
10221021
{
1022+
ulong atomSize = _physicalDeviceProperties.limits.nonCoherentAtomSize;
1023+
ulong mapOffset = memoryBlock.Offset + offsetInBytes;
1024+
ulong bindOffset = ((mapOffset / atomSize) * atomSize);
1025+
ulong bindSize = ((sizeInBytes + atomSize - 1) / atomSize) * atomSize;
1026+
10231027
if (memoryBlock.IsPersistentMapped)
10241028
{
1025-
mappedPtr = (IntPtr)memoryBlock.BlockMappedPointer;
1029+
mappedPtr = (IntPtr)((byte*)memoryBlock.BaseMappedPointer + mapOffset);
10261030
}
10271031
else
10281032
{
10291033
void* ret;
10301034
VkResult result = vkMapMemory(
1031-
_device, memoryBlock.DeviceMemory, memoryBlock.Offset, memoryBlock.Size, 0, &ret);
1035+
_device, memoryBlock.DeviceMemory, bindOffset, bindSize, 0, &ret);
10321036
if (result != VkResult.VK_ERROR_MEMORY_MAP_FAILED)
10331037
{
10341038
CheckResult(result);
@@ -1037,15 +1041,14 @@ private protected override MappedResource MapCore(
10371041
{
10381042
ThrowMapFailedException(resource, subresource);
10391043
}
1040-
mappedPtr = (IntPtr)ret;
1044+
mappedPtr = (IntPtr)((byte*)ret + (mapOffset - bindOffset));
10411045
}
10421046
}
10431047

1044-
byte* dataPtr = (byte*)mappedPtr.ToPointer() + offsetInBytes;
10451048
return new MappedResource(
10461049
resource,
10471050
mode,
1048-
(IntPtr)dataPtr,
1051+
mappedPtr,
10491052
offsetInBytes,
10501053
sizeInBytes,
10511054
subresource,
@@ -1280,7 +1283,7 @@ internal VkCommandList GetAndBeginCommandList()
12801283
{
12811284
Transient = true
12821285
};
1283-
sharedList = (VkCommandList) ResourceFactory.CreateCommandList(desc);
1286+
sharedList = (VkCommandList)ResourceFactory.CreateCommandList(desc);
12841287
}
12851288

12861289
sharedList.Begin();

0 commit comments

Comments
 (0)