Compare commits

...

1 Commits

Author SHA1 Message Date
ParantezTech 220b808076 [GPU] Host cached guest buffer 2026-07-26 04:22:11 +03:00
@@ -9445,7 +9445,8 @@ internal static unsafe class VulkanVideoPresenter
size, size,
BufferUsageFlags.StorageBufferBit, BufferUsageFlags.StorageBufferBit,
MemoryPropertyFlags.HostVisibleBit | MemoryPropertyFlags.HostCoherentBit, MemoryPropertyFlags.HostVisibleBit | MemoryPropertyFlags.HostCoherentBit,
out var memory); out var memory,
preferredMemoryFlags: MemoryPropertyFlags.HostCachedBit);
void* mapped; void* mapped;
Check(_vk.MapMemory(_device, memory, 0, size, 0, &mapped), "vkMapMemory(guest buffer)"); Check(_vk.MapMemory(_device, memory, 0, size, 0, &mapped), "vkMapMemory(guest buffer)");
var shadow = new byte[checked((int)size)]; var shadow = new byte[checked((int)size)];
@@ -10254,7 +10255,8 @@ internal static unsafe class VulkanVideoPresenter
ulong size, ulong size,
BufferUsageFlags usage, BufferUsageFlags usage,
MemoryPropertyFlags memoryFlags, MemoryPropertyFlags memoryFlags,
out DeviceMemory memory) out DeviceMemory memory,
MemoryPropertyFlags preferredMemoryFlags = 0)
{ {
var bufferInfo = new BufferCreateInfo var bufferInfo = new BufferCreateInfo
{ {
@@ -10270,7 +10272,10 @@ internal static unsafe class VulkanVideoPresenter
{ {
SType = StructureType.MemoryAllocateInfo, SType = StructureType.MemoryAllocateInfo,
AllocationSize = requirements.Size, AllocationSize = requirements.Size,
MemoryTypeIndex = FindMemoryType(requirements.MemoryTypeBits, memoryFlags), MemoryTypeIndex = FindMemoryType(
requirements.MemoryTypeBits,
memoryFlags,
preferredMemoryFlags),
}; };
Check(_vk.AllocateMemory(_device, &memoryInfo, null, out memory), "vkAllocateMemory"); Check(_vk.AllocateMemory(_device, &memoryInfo, null, out memory), "vkAllocateMemory");
Check(_vk.BindBufferMemory(_device, buffer, memory, 0), "vkBindBufferMemory"); Check(_vk.BindBufferMemory(_device, buffer, memory, 0), "vkBindBufferMemory");
@@ -10307,18 +10312,26 @@ internal static unsafe class VulkanVideoPresenter
} }
} }
private uint FindMemoryType(uint typeBits, MemoryPropertyFlags requiredFlags) private uint FindMemoryType(
uint typeBits,
MemoryPropertyFlags requiredFlags,
MemoryPropertyFlags preferredFlags = 0)
{ {
_vk.GetPhysicalDeviceMemoryProperties(_physicalDevice, out var properties); _vk.GetPhysicalDeviceMemoryProperties(_physicalDevice, out var properties);
var memoryTypes = &properties.MemoryTypes.Element0; var memoryTypes = &properties.MemoryTypes.Element0;
for (var pass = preferredFlags != 0 ? 0 : 1; pass < 2; pass++)
{
var wanted = pass == 0 ? requiredFlags | preferredFlags : requiredFlags;
for (uint index = 0; index < properties.MemoryTypeCount; index++) for (uint index = 0; index < properties.MemoryTypeCount; index++)
{ {
if ((typeBits & (1u << (int)index)) != 0 && if ((typeBits & (1u << (int)index)) != 0 &&
(memoryTypes[index].PropertyFlags & requiredFlags) == requiredFlags) (memoryTypes[index].PropertyFlags & wanted) == wanted)
{ {
return index; return index;
} }
} }
}
throw new InvalidOperationException("No compatible Vulkan host-visible memory type was found."); throw new InvalidOperationException("No compatible Vulkan host-visible memory type was found.");
} }