Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions runtime/src/vulkan_device_markers.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ struct MarkerTarget {
explicit operator bool() const { return buffer != VK_NULL_HANDLE; }

void started(VkCommandBuffer command_buffer, std::uint64_t seq) const {
if (!*this || (!write2 && !write)) return;
if (write2)
write2(command_buffer, VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT, buffer, offset, std::uint32_t(seq));
else
write(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, buffer, offset, std::uint32_t(seq));
}
void finished(VkCommandBuffer command_buffer, std::uint64_t seq) const {
if (!*this || (!write2 && !write)) return;
if (write2)
write2(command_buffer, VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT, buffer, offset + 4, std::uint32_t(seq));
else
Expand All @@ -140,14 +142,15 @@ class DeviceMarkers {
DeviceMarkers(const DeviceMarkers&) = delete;
DeviceMarkers& operator=(const DeviceMarkers&) = delete;
~DeviceMarkers() {
if (memory_ && unmap_) unmap_(device_, memory_);
if (words_ && memory_ && unmap_) unmap_(device_, memory_);
if (buffer_ && destroy_buffer_) destroy_buffer_(device_, buffer_, nullptr);
if (memory_ && free_memory_) free_memory_(device_, memory_, nullptr);
}

// Returns the VkResult of the step that failed, or VK_SUCCESS.
VkResult create(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceMemoryProperties& memory,
bool synchronization2) {
if (!get) return VK_ERROR_EXTENSION_NOT_PRESENT;
device_ = device;
#define LEONIDA_MARKER_LOAD(type, name) const auto name = reinterpret_cast<type>(get(device, #name))
LEONIDA_MARKER_LOAD(PFN_vkCreateBuffer, vkCreateBuffer);
Expand Down Expand Up @@ -219,7 +222,9 @@ class DeviceMarkers {
static std::uint32_t memory_type(const VkPhysicalDeviceMemoryProperties& memory, std::uint32_t allowed) {
constexpr VkMemoryPropertyFlags need = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
std::uint32_t fallback = UINT32_MAX;
for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i) {
const std::uint32_t count =
memory.memoryTypeCount < VK_MAX_MEMORY_TYPES ? memory.memoryTypeCount : VK_MAX_MEMORY_TYPES;
for (std::uint32_t i = 0; i < count; ++i) {
const VkMemoryPropertyFlags flags = memory.memoryTypes[i].propertyFlags;
if (!(allowed & (1u << i)) || (flags & need) != need) continue;
if (!(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) return i;
Expand Down
30 changes: 29 additions & 1 deletion runtime/test/vulkan_device_markers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Write {
};
std::vector<Write> writes;
std::uint32_t words[2 * DeviceMarkers::max_queues];
bool mapped = false, unmapped = false, freed = false;
bool mapped = false, unmapped = false, freed = false, fail_map = false;

VKAPI_ATTR VkResult VKAPI_CALL create_buffer(VkDevice, const VkBufferCreateInfo* info, const VkAllocationCallbacks*,
VkBuffer* buffer) {
Expand All @@ -52,6 +52,7 @@ VKAPI_ATTR VkResult VKAPI_CALL allocate(VkDevice, const VkMemoryAllocateInfo* in
}
VKAPI_ATTR VkResult VKAPI_CALL bind(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) { return VK_SUCCESS; }
VKAPI_ATTR VkResult VKAPI_CALL map(VkDevice, VkDeviceMemory, VkDeviceSize, VkDeviceSize, VkMemoryMapFlags, void** out) {
if (fail_map) return VK_ERROR_MEMORY_MAP_FAILED;
std::memset(words, 0xcd, sizeof(words));
*out = words;
mapped = true;
Expand Down Expand Up @@ -295,6 +296,33 @@ int main() {
if (int failed = recorded(true)) return failed;
if (int failed = recorded(false)) return failed;
if (int failed = extension_request()) return failed;
{
// Regression: memoryTypeCount > VK_MAX_MEMORY_TYPES must not read OOB or shift >= 32.
VkPhysicalDeviceMemoryProperties oversized{};
oversized.memoryTypeCount = VK_MAX_MEMORY_TYPES + 8;
EXPECT(DeviceMarkers::memory_type(oversized, 0xffffffffu) == UINT32_MAX);

// Regression: null get_proc_addr must return an error rather than dereferencing null.
DeviceMarkers null_loader;
EXPECT(null_loader.create(nullptr, VK_NULL_HANDLE, memory_types(), false) == VK_ERROR_EXTENSION_NOT_PRESENT);

// Regression: empty MarkerTarget must ignore started/finished safely.
MarkerTarget empty_target{};
empty_target.started(VK_NULL_HANDLE, 1);
empty_target.finished(VK_NULL_HANDLE, 1);

// Regression: failed vkMapMemory must not call vkUnmapMemory in ~DeviceMarkers.
mapped = unmapped = freed = false;
fail_map = true;
{
DeviceMarkers failed_map;
EXPECT(failed_map.create(get_proc, fake_handle<VkDevice>(1), memory_types(), false) ==
VK_ERROR_MEMORY_MAP_FAILED);
EXPECT(!failed_map.ready());
}
fail_map = false;
EXPECT(!mapped && !unmapped && freed);
}
std::puts("vulkan_device_markers: ok");
return 0;
}
Loading