Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Turnip runs on HakiosGPU: vk_test fills a buffer on the GPU (PASS)
yehorkaa83af5e748b92
diff --git a/tools/mesa/hakios-drm/hakios.drm.c b/tools/mesa/hakios-drm/hakios.drm.cindex 2672652..add92af 100644--- a/tools/mesa/hakios-drm/hakios.drm.c+++ b/tools/mesa/hakios-drm/hakios.drm.c@@ -562,3 +562,8 @@ submit_with_syncobjs(struct drm_msm_gem_submit *submit, unsigned long size)/* Linux's device nodes, which Turnip's lists of devices ask about: one GPU, a render node 128 */int drmGetNodeTypeFromFd(int fd) { (void)fd; return DRM_NODE_RENDER; }++int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices) { (void)flags; (void)devices; (void)max_devices; return 0; }+void drmFreeDevices(drmDevicePtr devices[], int count) { (void)devices; (void)count; }+int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) { (void)fd; (void)flags; *device = NULL; return -ENODEV; }+void drmFreeDevice(drmDevicePtr *device) { (void)device; }diff --git a/tools/mesa/hakios-drm/xf86drm.h b/tools/mesa/hakios-drm/xf86drm.hindex 6feb777..d8ca982 100644--- a/tools/mesa/hakios-drm/xf86drm.h+++ b/tools/mesa/hakios-drm/xf86drm.h@@ -62,6 +62,24 @@ int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points, uint32_t count)int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points, uint32_t count, uint32_t flags);int drmSyncobjTransfer(int fd, uint32_t dst_handle, uint64_t dst_point, uint32_t src_handle, uint64_t src_point, uint32_t flags);int drmGetNodeTypeFromFd(int fd);++/* Linux's list of DRM devices, which the Vulkan runtime walks: none here (Turnip finds the one GPU itself, tu_knl.cc) */+#define DRM_NODE_MAX 3+#define DRM_BUS_PCI 0+#define DRM_BUS_USB 1+#define DRM_BUS_PLATFORM 2+#define DRM_BUS_HOST1X 3+typedef struct _drmDevice {+ char **nodes;+ int available_nodes;+ int bustype;+ union { void *pci, *usb, *platform, *host1x; } businfo;+ union { void *pci, *usb, *platform, *host1x; } deviceinfo;+} drmDevice, *drmDevicePtr;+int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices);+void drmFreeDevices(drmDevicePtr devices[], int count);+int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device);+void drmFreeDevice(drmDevicePtr *device);int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle);int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd);int drmSyncobjImportSyncFile(int fd, uint32_t handle, int sync_file_fd);diff --git a/tools/mesa/turnip-darwin.patch b/tools/mesa/turnip-darwin.patchindex 7209b0c..4b709a7 100644--- a/tools/mesa/turnip-darwin.patch+++ b/tools/mesa/turnip-darwin.patch@@ -58,6 +58,144 @@ index 62ebfd0..72aac15 100644if (pthread_cond_init(&device->timeline_cond, &condattr) != 0) {pthread_condattr_destroy(&condattr);result = vk_startup_errorf(physical_device->instance,+diff --git a/src/freedreno/vulkan/tu_knl.cc b/src/freedreno/vulkan/tu_knl.cc+index 80e3900..3f00525 100644+--- a/src/freedreno/vulkan/tu_knl.cc++++ b/src/freedreno/vulkan/tu_knl.cc+@@ -24,6 +24,9 @@++ #include "tu_device.h"+ #include "tu_knl.h"++#ifdef __APPLE__++#include <sys/sysctl.h>++#endif+ #include "tu_queue.h"+ #include "tu_rmv.h"++@@ -124,7 +127,11 @@ tu_bo_unmap(struct tu_device *dev, struct tu_bo *bo, bool reserve)+ return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED,+ "Failed to replace mapping with reserved memory");+ } else {++#if defined(__APPLE__) && defined(TU_HAS_MSM)++ hakios_drm_unmap(dev->fd, bo->gem_handle, bo->map);++#else+ munmap(bo->map, bo->size);++#endif+ }++ bo->map = NULL;+@@ -194,7 +201,14 @@ tu_get_l1_dcache_size()+ if (!(DETECT_ARCH_AARCH64 || DETECT_ARCH_X86 || DETECT_ARCH_X86_64))+ return 0;++-#if DETECT_ARCH_AARCH64 && \++#if defined(__APPLE__)++ /* CTR_EL0 is not readable from user space on Darwin: */++ int64_t line = 0;++ size_t size = sizeof(line);++ if (sysctlbyname("hw.cachelinesize", &line, &size, NULL, 0) != 0)++ return 64;++ return line;++#elif DETECT_ARCH_AARCH64 && \+ (!defined(_SC_LEVEL1_DCACHE_LINESIZE) || DETECT_OS_ANDROID)+ /* Bionic does not implement _SC_LEVEL1_DCACHE_LINESIZE properly: */+ uint64_t ctr_el0;+@@ -352,6 +366,41 @@ tu_enumerate_devices(struct vk_instance *vk_instance)+ mesa_logi("Found compatible device '%s'.", path);++ return result;++#elif defined(__APPLE__) && defined(TU_HAS_MSM)++ /* Darwin: no DRM nodes to list. The one GPU is the kernel's service HakiosGPU, which speaks msm's requests through++ * the stand-in for libdrm (src/hakios-drm); its file descriptor means nothing but "the connection". */++ struct tu_instance *instance =++ container_of(vk_instance, struct tu_instance, vk);++ struct tu_physical_device *device = NULL;++ int fd = drmOpenWithType("msm", NULL, DRM_NODE_RENDER);++ if (fd < 0)++ return VK_ERROR_INCOMPATIBLE_DRIVER;++ drmVersionPtr version = drmGetVersion(fd);++ if (!version) {++ close(fd);++ return VK_ERROR_INCOMPATIBLE_DRIVER;++ }++ VkResult result = tu_knl_drm_msm_load(instance, fd, version, &device);++ drmFreeVersion(version);++ if (result != VK_SUCCESS) {++ close(fd);++ return result;++ }++ device->master_fd = -1;++ device->kgsl_dma_fd = -1;++ snprintf(device->fd_path, ARRAY_SIZE(device->fd_path), "%s", "HakiosGPU");++ device->has_master = false;++ device->has_local = false;++ result = tu_physical_device_init(device, instance);++ if (result != VK_SUCCESS) {++ vk_free(&instance->vk.alloc, device);++ close(fd);++ return result;++ }++ list_addtail(&device->vk.link, &instance->vk.physical_devices.list);++ if (TU_DEBUG(STARTUP))++ mesa_logi("Found compatible device HakiosGPU (msm).");++ return VK_SUCCESS;+ #else+ return VK_ERROR_INCOMPATIBLE_DRIVER;+ #endif+diff --git a/src/freedreno/vulkan/tu_knl_drm.cc b/src/freedreno/vulkan/tu_knl_drm.cc+index 27916d0..569b1d6 100644+--- a/src/freedreno/vulkan/tu_knl_drm.cc++++ b/src/freedreno/vulkan/tu_knl_drm.cc+@@ -72,7 +72,11 @@ tu_drm_bo_finish(struct tu_device *dev, struct tu_bo *bo)++ if (bo->map) {+ TU_RMV(bo_unmap, dev, bo);++#ifdef __APPLE__++ hakios_drm_unmap(dev->fd, bo->gem_handle, bo->map);++#else+ munmap(bo->map, bo->size);++#endif+ }++ TU_RMV(bo_destroy, dev, bo);+diff --git a/src/freedreno/vulkan/tu_knl_drm_msm.cc b/src/freedreno/vulkan/tu_knl_drm_msm.cc+index 08831e7..7472ea6 100644+--- a/src/freedreno/vulkan/tu_knl_drm_msm.cc++++ b/src/freedreno/vulkan/tu_knl_drm_msm.cc+@@ -191,7 +191,12 @@ tu_drm_is_memory_type_supported(int fd, uint32_t flags)+ static VkResult+ msm_device_init(struct tu_device *dev)+ {++#ifdef __APPLE__++ /* no device node: a descriptor of the one connection to HakiosGPU (src/hakios-drm) */++ int fd = drmOpenWithType("msm", NULL, DRM_NODE_RENDER);++#else+ int fd = open(dev->physical_device->fd_path, O_RDWR | O_CLOEXEC);++#endif+ if (fd < 0) {+ return vk_startup_errorf(+ dev->physical_device->instance, VK_ERROR_INITIALIZATION_FAILED,+@@ -735,6 +740,12 @@ msm_bo_init_dmabuf(struct tu_device *dev,+ static VkResult+ msm_bo_map(struct tu_device *dev, struct tu_bo *bo, void *placed_addr)+ {++#ifdef __APPLE__++ /* no mmap() on a device here: the kernel's driver maps the buffer (src/hakios-drm) */++ if (placed_addr != NULL)++ return vk_error(dev, VK_ERROR_MEMORY_MAP_FAILED);++ void *map = hakios_drm_map(dev->fd, bo->gem_handle, bo->size);++#else+ uint64_t offset = tu_gem_info(dev, bo->gem_handle, MSM_INFO_GET_OFFSET);+ if (!offset)+ return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);+@@ -743,6 +754,7 @@ msm_bo_map(struct tu_device *dev, struct tu_bo *bo, void *placed_addr)+ void *map = mmap(placed_addr, bo->size, PROT_READ | PROT_WRITE,+ MAP_SHARED | (placed_addr != NULL ? MAP_FIXED : 0),+ dev->fd, offset);++#endif+ if (map == MAP_FAILED)+ return vk_error(dev, VK_ERROR_MEMORY_MAP_FAILED);+diff --git a/src/freedreno/vulkan/tu_wsi.cc b/src/freedreno/vulkan/tu_wsi.ccindex 57cf904..3e0a1b1 100644--- a/src/freedreno/vulkan/tu_wsi.cc@@ -86,6 +224,20 @@ index 57cf904..3e0a1b1 100644#elsereturn true;#endif+diff --git a/src/util/libdrm.h b/src/util/libdrm.h+index 4bfe1d2..0062655 100644+--- a/src/util/libdrm.h++++ b/src/util/libdrm.h+@@ -27,7 +27,8 @@+ * the proliferation of #ifdef'ery to support environments without libdrm.+ */++-#ifdef HAVE_LIBDRM++#if defined(HAVE_LIBDRM) || (defined(__APPLE__) && __has_include(<xf86drm.h>))++/* (Darwin: the stand-in for libdrm, src/hakios-drm, where a target has it on its include path) */+ #include <xf86drm.h>+ #else+diff --git a/src/vulkan/wsi/meson.build b/src/vulkan/wsi/meson.buildindex 6933eb7..b7ea38d 100644--- a/src/vulkan/wsi/meson.builddiff --git a/userland/build.sh b/userland/build.shindex 489c297..6802708 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -47,6 +47,9 @@# and nothing changes (src/inject.c, opengl_es()).# GL=test: only the library and bin/gl_test (a triangle, src/gl_test.c;# RC_JOBS=rc.jobs.gltest runs it); the window server stays as it is.+# TURNIP=1 with Turnip, Mesa's Vulkan driver for the Adreno (tools/mk-turnip.sh), as+# lib/libvulkan_freedreno.dylib (11 MB), and bin/vk_test, which runs it once round+# (src/vk_test.c; RC_JOBS=rc.jobs.vktest): the first piece of a Metal on Vulkan# ADSP=1 with the audio DSP's firmware from ../fw-audio in fw/ (20 MB; for the# kernel's adsp_step=N, drivers/audio/hakios.adsp.c, which runs before the root is# mounted). Not beside GL=1: the two do not fit the 64 MB the loader takes with@@ -314,6 +317,13 @@ if [ -d $DARWIN/rootfs/usr/lib ]; then# it, and it is copied in when it is there.[ -f $M/air_run ] && cp $M/air_run obj/air_runfi+ if [ -n "${TURNIP:-}" ]; then+ # Turnip and its smallest whole round (src/vk_test.c): built by tools/mk-turnip.sh into the Mesa tree's out/+ M=../xnu-deps/build/mesa/out+ [ -f $M/libvulkan_freedreno.dylib ] || ../tools/mk-turnip.sh+ $DYN -I../xnu-deps/build/mesa/mesa-25.0.7/include -o obj/vk_test src/vk_test.c+ cp $M/libvulkan_freedreno.dylib obj/+ fificp obj/hello_dyn obj/svc_test obj/surf_test obj/rwtouch obj/mg_test root/bin/[ -f obj/cpu_test ] && cp obj/cpu_test root/bin/ # (a prebuilt/ from before this program has none)@@ -355,6 +365,10 @@ if [ -d $DARWIN/rootfs/usr/lib ]; thencp obj/HakiosMetal root/lib/HakiosMetal.bundle/cp src/HakiosMetal.plist root/lib/HakiosMetal.bundle/Info.plistfi+ if [ -n "${TURNIP:-}" ]; then+ cp obj/libvulkan_freedreno.dylib root/lib/+ cp obj/vk_test root/bin/+ fiif [ -n "$GL" ]; thencp obj/libOSMesa.8.dylib root/lib/cp obj/gl_test root/bin/diff --git a/userland/rc.jobs.interactive b/userland/rc.jobs.interactivenew file mode 100644index 0000000..6260ef2--- /dev/null+++ b/userland/rc.jobs.interactive@@ -0,0 +1,3 @@+# Commands run by the launchd job com.hakios.demo (see com.hakios.demo.plist) for a session a person drives:+# nothing but the load monitor, so that the phone is all theirs (RC_JOBS=rc.jobs.interactive)+/redwood/bin/cputop 10 &diff --git a/userland/rc.jobs.vktest b/userland/rc.jobs.vktestnew file mode 100644index 0000000..bca9825--- /dev/null+++ b/userland/rc.jobs.vktest@@ -0,0 +1,4 @@+# Commands run by the launchd job com.hakios.demo (see com.hakios.demo.plist): Turnip once round (src/vk_test.c),+# in an image built with TURNIP=1+/redwood/bin/vk_test+/redwood/bin/cputop 10 &diff --git a/userland/src/vk_test.c b/userland/src/vk_test.cnew file mode 100644index 0000000..622ca30--- /dev/null+++ b/userland/src/vk_test.c@@ -0,0 +1,231 @@+/*+ * Turnip on the phone, the smallest whole round: Mesa's Vulkan driver for the Adreno (tools/mk-turnip.sh) loaded as a+ * Vulkan loader would load it (vk_icdGetInstanceProcAddr), an instance, the GPU as a physical device, a device and its+ * queue, a buffer in host-visible memory filled by the GPU (vkCmdFillBuffer), the submission waited for by a fence+ * (a syncobj of tools/mesa/hakios-drm), and every word of the buffer checked. Then the same fill a hundred times, timed:+ * what one submission and its wait cost. RC_JOBS=rc.jobs.vktest runs it (an image built with TURNIP=1).+ */+#include <dlfcn.h>+#include <signal.h>+#include <sys/ucontext.h>+#include <unistd.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>++#define VK_NO_PROTOTYPES+#include <vulkan/vulkan.h>++/* (vk_icd.h's: the loader-to-driver handshake) */+typedef VkResult (VKAPI_PTR *PFN_vkNegotiateLoaderICDInterfaceVersion)(uint32_t *pVersion);++#define DRIVER "/redwood/lib/libvulkan_freedreno.dylib"+#define WORDS (1u << 20) /* a 4 MB buffer */++static PFN_vkGetInstanceProcAddr gipa;+static VkInstance instance;+static VkDevice device;++#define I(name) PFN_##name name = (PFN_##name)gipa(instance, #name)+#define CHECK(what, call) do { VkResult r_ = (call); if (r_ != VK_SUCCESS) { \+ printf("vk_test: %s failed: VkResult %d\n", what, r_); return 1; } } while (0)++/* a crash says where: the pc and lr, as offsets into the image each falls in */+static void+where(const char *what, uintptr_t a)+{+ Dl_info info;++ if (dladdr((void *)a, &info) && info.dli_fname != NULL)+ printf("vk_test: %s %#lx = %s+%#lx (%s+%#lx)\n", what, (unsigned long)a, info.dli_fname,+ (unsigned long)(a - (uintptr_t)info.dli_fbase), info.dli_sname ? info.dli_sname : "?",+ (unsigned long)(a - (uintptr_t)info.dli_saddr));+ else+ printf("vk_test: %s %#lx\n", what, (unsigned long)a);+}++static void+crashed(int sig, siginfo_t *si, void *context)+{+ ucontext_t *uc = context;++ printf("vk_test: signal %d at address %p\n", sig, si->si_addr);+ where("pc", (uintptr_t)uc->uc_mcontext->__ss.__pc);+ where("lr", (uintptr_t)uc->uc_mcontext->__ss.__lr);+ uintptr_t fp = (uintptr_t)uc->uc_mcontext->__ss.__fp;+ for (int i = 0; i < 16 && fp != 0 && (fp & 7) == 0; i++) {+ where("frame", ((uintptr_t *)fp)[1]);+ fp = ((uintptr_t *)fp)[0];+ }+ fflush(stdout);+ _exit(128 + sig);+}++static double+now_ms(void)+{+ struct timespec t;++ clock_gettime(CLOCK_MONOTONIC, &t);+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;+}++int+main(void)+{+ struct sigaction sa = { .sa_sigaction = crashed, .sa_flags = SA_SIGINFO };+ sigaction(SIGSEGV, &sa, NULL);+ sigaction(SIGBUS, &sa, NULL);+ sigaction(SIGILL, &sa, NULL);+ sigaction(SIGABRT, &sa, NULL);+ sigaction(SIGTRAP, &sa, NULL);+ setvbuf(stdout, NULL, _IONBF, 0);+ dup2(1, 2); /* Mesa's own messages (stderr) beside ours */+ setenv("TU_DEBUG", "startup", 0);+ void *driver = dlopen(DRIVER, RTLD_NOW | RTLD_LOCAL);++ if (driver == NULL) {+ printf("vk_test: %s does not load: %s\n", DRIVER, dlerror());+ return 1;+ }+ gipa = (PFN_vkGetInstanceProcAddr)dlsym(driver, "vk_icdGetInstanceProcAddr");+ PFN_vkNegotiateLoaderICDInterfaceVersion negotiate =+ (PFN_vkNegotiateLoaderICDInterfaceVersion)dlsym(driver, "vk_icdNegotiateLoaderICDInterfaceVersion");+ if (gipa == NULL) {+ printf("vk_test: no vk_icdGetInstanceProcAddr in the driver\n");+ return 1;+ }+ if (negotiate != NULL) {+ uint32_t version = 5;+ negotiate(&version);+ printf("vk_test: loader interface version %u\n", version);+ }++ I(vkCreateInstance);+ VkApplicationInfo app = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "vk_test",+ .apiVersion = VK_API_VERSION_1_1 };+ VkInstanceCreateInfo ici = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app };+ CHECK("vkCreateInstance", vkCreateInstance(&ici, NULL, &instance));+ printf("vk_test: instance made\n");++ I(vkEnumeratePhysicalDevices);+ I(vkGetPhysicalDeviceProperties);+ I(vkGetPhysicalDeviceMemoryProperties);+ I(vkGetPhysicalDeviceQueueFamilyProperties);+ uint32_t count = 1;+ VkPhysicalDevice gpu = VK_NULL_HANDLE;+ VkResult er = vkEnumeratePhysicalDevices(instance, &count, &gpu);+ if ((er != VK_SUCCESS && er != VK_INCOMPLETE) || count == 0) {+ printf("vk_test: no physical device (VkResult %d, %u devices)\n", er, count);+ return 1;+ }+ VkPhysicalDeviceProperties props;+ vkGetPhysicalDeviceProperties(gpu, &props);+ printf("vk_test: device \"%s\", Vulkan %u.%u.%u, driver %#x, vendor %#x device %#x\n", props.deviceName,+ VK_API_VERSION_MAJOR(props.apiVersion), VK_API_VERSION_MINOR(props.apiVersion), VK_API_VERSION_PATCH(props.apiVersion),+ props.driverVersion, props.vendorID, props.deviceID);+ uint32_t families = 0;+ vkGetPhysicalDeviceQueueFamilyProperties(gpu, &families, NULL);+ printf("vk_test: %u queue families\n", families);++ float priority = 1.0f;+ VkDeviceQueueCreateInfo qci = { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueFamilyIndex = 0,+ .queueCount = 1, .pQueuePriorities = &priority };+ VkDeviceCreateInfo dci = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .queueCreateInfoCount = 1,+ .pQueueCreateInfos = &qci };+ I(vkCreateDevice);+ CHECK("vkCreateDevice", vkCreateDevice(gpu, &dci, NULL, &device));+ printf("vk_test: device made\n");++ I(vkGetDeviceQueue);+ I(vkCreateBuffer);+ I(vkGetBufferMemoryRequirements);+ I(vkAllocateMemory);+ I(vkBindBufferMemory);+ I(vkMapMemory);+ I(vkCreateCommandPool);+ I(vkAllocateCommandBuffers);+ I(vkBeginCommandBuffer);+ I(vkCmdFillBuffer);+ I(vkEndCommandBuffer);+ I(vkCreateFence);+ I(vkQueueSubmit);+ I(vkWaitForFences);+ I(vkResetFences);+ VkQueue queue;+ vkGetDeviceQueue(device, 0, 0, &queue);++ VkBufferCreateInfo bci = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = WORDS * 4,+ .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT };+ VkBuffer buffer;+ CHECK("vkCreateBuffer", vkCreateBuffer(device, &bci, NULL, &buffer));+ VkMemoryRequirements req;+ vkGetBufferMemoryRequirements(device, buffer, &req);+ VkPhysicalDeviceMemoryProperties mp;+ vkGetPhysicalDeviceMemoryProperties(gpu, &mp);+ uint32_t type = UINT32_MAX;+ for (uint32_t i = 0; i < mp.memoryTypeCount; i++) {+ const VkMemoryPropertyFlags want = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;+ if ((req.memoryTypeBits & (1u << i)) && (mp.memoryTypes[i].propertyFlags & want) == want) {+ type = i;+ break;+ }+ }+ if (type == UINT32_MAX) {+ printf("vk_test: no host-visible coherent memory type\n");+ return 1;+ }+ VkMemoryAllocateInfo mai = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = req.size,+ .memoryTypeIndex = type };+ VkDeviceMemory memory;+ CHECK("vkAllocateMemory", vkAllocateMemory(device, &mai, NULL, &memory));+ CHECK("vkBindBufferMemory", vkBindBufferMemory(device, buffer, memory, 0));+ uint32_t *words;+ CHECK("vkMapMemory", vkMapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, (void **)&words));+ memset(words, 0, WORDS * 4);++ VkCommandPoolCreateInfo pci = { .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .queueFamilyIndex = 0 };+ VkCommandPool pool;+ CHECK("vkCreateCommandPool", vkCreateCommandPool(device, &pci, NULL, &pool));+ VkCommandBufferAllocateInfo cai = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = pool,+ .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 };+ VkCommandBuffer cb;+ CHECK("vkAllocateCommandBuffers", vkAllocateCommandBuffers(device, &cai, &cb));+ VkCommandBufferBeginInfo begin = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };+ CHECK("vkBeginCommandBuffer", vkBeginCommandBuffer(cb, &begin));+ vkCmdFillBuffer(cb, buffer, 0, VK_WHOLE_SIZE, 0x600dc0de);+ CHECK("vkEndCommandBuffer", vkEndCommandBuffer(cb));+ VkFenceCreateInfo fci = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };+ VkFence fence;+ CHECK("vkCreateFence", vkCreateFence(device, &fci, NULL, &fence));+ VkSubmitInfo si = { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &cb };+ double t0 = now_ms();+ CHECK("vkQueueSubmit", vkQueueSubmit(queue, 1, &si, fence));+ CHECK("vkWaitForFences", vkWaitForFences(device, 1, &fence, VK_TRUE, 2000000000ull));+ double t1 = now_ms();+ uint32_t right = 0, first_wrong = UINT32_MAX;+ for (uint32_t i = 0; i < WORDS; i++) {+ if (words[i] == 0x600dc0de) right++;+ else if (first_wrong == UINT32_MAX) first_wrong = i;+ }+ printf("vk_test: the GPU filled %u of %u words with 0x600dc0de in %.2f ms%s\n", right, WORDS, t1 - t0,+ right == WORDS ? "" : " -- WRONG");+ if (first_wrong != UINT32_MAX) printf("vk_test: word %u is %#x\n", first_wrong, words[first_wrong]);++ /* a hundred more submissions of the same, each waited for */+ double best = 1e9, sum = 0;+ for (int i = 0; i < 100; i++) {+ CHECK("vkResetFences", vkResetFences(device, 1, &fence));+ double a = now_ms();+ CHECK("vkQueueSubmit", vkQueueSubmit(queue, 1, &si, fence));+ CHECK("vkWaitForFences", vkWaitForFences(device, 1, &fence, VK_TRUE, 2000000000ull));+ double d = now_ms() - a;+ sum += d;+ if (d < best) best = d;+ }+ printf("vk_test: 100 fills of 4 MB, each submitted and waited for: %.3f ms on average, %.3f at best\n", sum / 100, best);+ printf("vk_test: %s\n", right == WORDS ? "PASS" : "FAIL");+ return right == WORDS ? 0 : 1;+}