From b3f66b0e01e3d9b6579e8e040789ef3cd512ac84 Mon Sep 17 00:00:00 2001 From: Per Mathisen Date: Mon, 28 Oct 2024 21:39:38 +0800 Subject: [PATCH] Start of a VK_TRACETOOLS_trace_helpers unit test --- CMakeLists.txt | 8 +++ tests/tracing7.cpp | 144 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 tests/tracing7.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bdc89a1..a6206c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -340,6 +340,14 @@ add_dependencies(tracing6 sync_stuff lava-replay) add_test(NAME trace_test_6 COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tracing6) add_test(NAME trace_test_6_replay COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lava-replay tracing_6.vk) +add_executable(tracing7 tests/tracing7.cpp ${VULKAN_TESTS_SRC}) +target_include_directories(tracing7 ${COMMON_INCLUDE}) +target_link_libraries(tracing7 ${COMMON_LIBRARIES} lavatube) +target_compile_options(tracing7 PRIVATE ${COMMON_FLAGS}) +add_dependencies(tracing7 sync_stuff lava-replay) +add_test(NAME trace_test_7 COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tracing7) +add_test(NAME trace_test_7_replay COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lava-replay tracing_7.vk) + add_executable(write1 tests/write1.cpp) target_include_directories(write1 ${COMMON_INCLUDE}) target_link_libraries(write1 ${COMMON_LIBRARIES} lavatube) diff --git a/tests/tracing7.cpp b/tests/tracing7.cpp new file mode 100644 index 0000000..150374b --- /dev/null +++ b/tests/tracing7.cpp @@ -0,0 +1,144 @@ +// Test of VK_TRACETOOLS_trace_helpers support + +#include "tests/common.h" +#include +#include "util_auto.h" +#include "external/tracetooltests/include/vulkan_ext.h" + +typedef void (VKAPI_PTR *PFN_vkSyncBufferTRACETOOLTEST)(VkDevice device, VkBuffer buffer); + +#define TEST_NAME "tracing_7" +#define NUM_BUFFERS 3 + +static void trace_3() +{ + vulkan_setup_t vulkan = test_init(TEST_NAME); + VkResult result; + + PFN_vkAssertBufferTRACETOOLTEST vkAssertBuffer = (PFN_vkAssertBufferTRACETOOLTEST)trace_vkGetDeviceProcAddr(vulkan.device, "vkAssertBufferTRACETOOLTEST"); + assert(vkAssertBuffer != nullptr); + PFN_vkSyncBufferTRACETOOLTEST vkSyncBuffer = (PFN_vkSyncBufferTRACETOOLTEST)trace_vkGetDeviceProcAddr(vulkan.device, "vkSyncBufferTRACETOOLTEST"); + assert(vkSyncBuffer != nullptr); + PFN_vkUpdateBufferTRACETOOLTEST vkUpdateBuffer = (PFN_vkUpdateBufferTRACETOOLTEST)trace_vkGetDeviceProcAddr(vulkan.device, "vkUpdateBufferTRACETOOLTEST"); + assert(vkUpdateBuffer); + PFN_vkUpdateImageTRACETOOLTEST vkUpdateImage = (PFN_vkUpdateImageTRACETOOLTEST)trace_vkGetDeviceProcAddr(vulkan.device, "vkUpdateImageTRACETOOLTEST"); + assert(vkUpdateImage); + PFN_vkThreadBarrierTRACETOOLTEST vkThreadBarrier = (PFN_vkThreadBarrierTRACETOOLTEST)trace_vkGetDeviceProcAddr(vulkan.device, "vkThreadBarrierTRACETOOLTEST"); + assert(vkThreadBarrier); + + VkBuffer buffer[NUM_BUFFERS]; + VkBufferCreateInfo bufferCreateInfo = {}; + bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; + bufferCreateInfo.size = 99; + bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; + bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + for (unsigned i = 0; i < NUM_BUFFERS; i++) + { + result = trace_vkCreateBuffer(vulkan.device, &bufferCreateInfo, nullptr, &buffer[i]); + check(result); + } + + VkMemoryRequirements req; + trace_vkGetBufferMemoryRequirements(vulkan.device, buffer[0], &req); + uint32_t memoryTypeIndex = get_device_memory_type(req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + + VkMemoryAllocateInfo pAllocateMemInfo = {}; + pAllocateMemInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + pAllocateMemInfo.memoryTypeIndex = memoryTypeIndex; + pAllocateMemInfo.allocationSize = req.size * NUM_BUFFERS; + VkDeviceMemory memory = 0; + result = trace_vkAllocateMemory(vulkan.device, &pAllocateMemInfo, nullptr, &memory); + check(result); + assert(memory != 0); + + uint32_t checksum = 0; + char* ptr = (char*)malloc(pAllocateMemInfo.allocationSize); + for (unsigned b = 0; b < NUM_BUFFERS; b++) + { + for (unsigned ii = 0; ii < bufferCreateInfo.size; ii++) ptr[ii + b * req.size] = ii % 255; // just fill it with a pattern + checksum = adler32((unsigned char*)&ptr[b * req.size], bufferCreateInfo.size); + printf("Buffer %d checksum=%u\n", (int)b, (unsigned)checksum); + } + + VkDeviceSize offset = 0; + for (unsigned i = 0; i < NUM_BUFFERS; i++) + { + trace_vkBindBufferMemory(vulkan.device, buffer[i], memory, offset); + + VkUpdateMemoryInfoTRACETOOLTEST updateInfo = { VK_STRUCTURE_TYPE_UPDATE_MEMORY_INFO_TRACETOOLTEST, nullptr }; + updateInfo.dstOffset = 0; // relative to start of buffer + updateInfo.dataSize = bufferCreateInfo.size; + updateInfo.pData = ptr; + trace_vkUpdateBufferTRACETOOLTEST(vulkan.device, buffer[i], &updateInfo); + + offset += req.size; + } + free(ptr); + ptr = nullptr; + + vkThreadBarrier(0, nullptr); + + for (unsigned i = 0; i < NUM_BUFFERS; i++) + { + trace_vkSyncBufferTRACETOOLTEST(vulkan.device, buffer[i]); + uint32_t checksum2 = trace_vkAssertBufferTRACETOOLTEST(vulkan.device, buffer[i]); + assert(checksum == checksum2); + } + + // Cleanup... + for (unsigned i = 0; i < NUM_BUFFERS; i++) + { + trace_vkDestroyBuffer(vulkan.device, buffer[i], nullptr); + } + trace_vkFreeMemory(vulkan.device, memory, nullptr); + test_done(vulkan); +} + +static bool getnext(lava_file_reader& t) +{ + bool done = false; + const uint8_t instrtype = t.read_uint8_t(); + if (instrtype == PACKET_API_CALL) + { + const uint16_t apicall = t.read_uint16_t(); + (void)t.read_int32_t(); + DLOG("[t%02d %06d] %s", t.thread_index(), (int)t.parent->thread_call_numbers->at(t.thread_index()).load(std::memory_order_relaxed) + 1, get_function_name(apicall)); + lava_replay_func api = retrace_getcall(apicall); + api(t); + t.parent->thread_call_numbers->at(t.thread_index()).fetch_add(1, std::memory_order_relaxed); + suballoc_internal_test(); + t.pool.reset(); + if (apicall == 1) done = true; // is vkDestroyInstance + } + else if (instrtype == PACKET_THREAD_BARRIER) + { + t.read_barrier(); + } + else if (instrtype == PACKET_BUFFER_UPDATE) + { + const uint32_t device_index = t.read_handle(); + const uint32_t buffer_index = t.read_handle(); + buffer_update(t, device_index, buffer_index); + } + else assert(false); + suballoc_internal_test(); + return !done; +} + +static void retrace_3() +{ + lava_reader r(TEST_NAME ".vk"); + lava_file_reader& t = r.file_reader(0); + int remaining = suballoc_internal_test(); + assert(remaining == 0); // there should be nothing now + while (getnext(t)) {} + remaining = suballoc_internal_test(); + assert(remaining == 0); // everything should be destroyed now +} + +int main() +{ + trace_3(); + retrace_3(); + return 0; +}