Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/spirv/vk-host.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / spirv / vk-host.c` 512 lines ยท 18951 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // INTERIM verification host: runs one vkspirv GLCompute entry point on one 3 // build-pinned Vulkan driver. It never generates IR or SPIR-V and is not 4 // part of the translator. Successor: seed/spirv/vkhost.om, Omega's own Vulkan 5 // loader binding through the native ABI (vkCreateInstance .. vkQueueSubmit 6 // called from seeded Omega); until that exists this file is a foreign oracle 7 // recorded as such, never an Omega implementation. 8 // 9 // usage: vk-host MODULE.spv ENTRY NPARAMS < ROWS 10 // ROWS: one line per invocation, NPARAMS canonical u64 decimals separated by 11 // single spaces (an empty line when NPARAMS is 0). At most `1048576` rows. 12 // stdout: "VALUE STATUS\n" per row, read back from outputs[2g] and outputs[2g+1]. 13 // Exit 0 only when every Vulkan call succeeded and every row was written; any 14 // usage, environment, API or runtime failure exits nonzero with a named stderr 15 // line and prints nothing on stdout. Device and driver identity go to stderr. 16 // 17 // Driver isolation: the build pins one target and one absolute driver path. 18 // VKSPIRV_TARGET=0 lavapipe: VKSPIRV_ICD is the lavapipe ICD manifest; the only 19 // accepted device is VK_PHYSICAL_DEVICE_TYPE_CPU "llvmpipe*", MESA_LLVMPIPE. 20 // VKSPIRV_TARGET=1 MoltenVK: the binary links libMoltenVK directly (no loader); 21 // VKSPIRV_ICD names that dylib; accepted driverID is MOLTENVK. This is a 22 // Vulkan-on-Metal translation layer, not a native Vulkan driver. 23 // VKSPIRV_TARGET=2 NVIDIA: VKSPIRV_ICD is the nvidia ICD manifest; accepted 24 // device is a GPU with driverID NVIDIA_PROPRIETARY. Build/run it only inside 25 // an authorized GPU window. 26 // Loader discovery, driver selection and layer variables are overwritten or 27 // removed before the first Vulkan call and implicit layers are disabled, so an 28 // inherited environment can never select a different driver. 29 30 #define _POSIX_C_SOURCE 200809L 31 #include 32 33 #include 34 #include 35 #include 36 #include 37 #include 38 39 enum { 40 EXIT_USAGE = 2, 41 EXIT_ENVIRONMENT = 3, 42 EXIT_MODULE = 4, 43 EXIT_DEVICE = 5, 44 EXIT_PIPELINE = 6, 45 EXIT_INPUT = 7, 46 EXIT_RUN = 8, 47 }; 48 49 #define MAX_PARAMS 6 50 #define MAX_ROWS 1048576u 51 #define LOCAL_SIZE 64u 52 #define POISON UINT64_C(0xa5a5a5a5a5a5a5a5) 53 #ifndef VKSPIRV_ICD 54 #define VKSPIRV_ICD "" 55 #endif 56 #ifndef VKSPIRV_TARGET 57 #define VKSPIRV_TARGET 0 58 #endif 59 #if VKSPIRV_TARGET == 0 60 #define TARGET_NAME "lavapipe" 61 #elif VKSPIRV_TARGET == 1 62 #define TARGET_NAME "moltenvk" 63 #elif VKSPIRV_TARGET == 2 64 #define TARGET_NAME "nvidia" 65 #else 66 #error "VKSPIRV_TARGET must be 0 (lavapipe), 1 (MoltenVK) or 2 (NVIDIA)" 67 #endif 68 69 static int refuse(int code, const char *what) 70 { 71 fprintf(stderr, "vk-host: %s\n", what); 72 return code; 73 } 74 75 static int vkfail(int code, const char *what, VkResult r) 76 { 77 fprintf(stderr, "vk-host: %s failed (VkResult %d)\n", what, (int)r); 78 return code; 79 } 80 81 static int parse_u64(const char *text, size_t length, uint64_t *out) 82 { 83 if (length == 0 || length > 20 || (text[0] == '0' && length > 1)) 84 return -1; 85 uint64_t v = 0; 86 for (size_t i = 0; i < length; i++) { 87 if (text[i] < '0' || text[i] > '9') 88 return -1; 89 unsigned d = (unsigned)(text[i] - '0'); 90 if (v > (UINT64_MAX - d) / 10) 91 return -1; 92 v = v * 10 + d; 93 } 94 *out = v; 95 return 0; 96 } 97 98 static int pin_loader(void) 99 { 100 const char *icd = VKSPIRV_ICD; 101 if (icd[0] != '/') 102 return refuse(EXIT_ENVIRONMENT, "build requires an absolute VKSPIRV_ICD driver path"); 103 static const char *drop[] = { 104 "VK_ADD_DRIVER_FILES", "VK_LAYER_PATH", "VK_ADD_LAYER_PATH", 105 "VK_IMPLICIT_LAYER_PATH", "VK_ADD_IMPLICIT_LAYER_PATH", 106 "VK_INSTANCE_LAYERS", "VK_LOADER_LAYERS_ENABLE", 107 "VK_LOADER_DRIVERS_SELECT", "VK_LOADER_DRIVERS_DISABLE", 108 "VK_LOADER_DEVICE_SELECT", "MESA_VK_DEVICE_SELECT", 109 }; 110 for (size_t i = 0; i < sizeof drop / sizeof drop[0]; i++) 111 if (unsetenv(drop[i]) != 0) 112 return refuse(EXIT_ENVIRONMENT, "cannot clear loader variable"); 113 if (setenv("VK_DRIVER_FILES", icd, 1) != 0 || setenv("VK_ICD_FILENAMES", icd, 1) != 0 || 114 setenv("VK_LOADER_LAYERS_DISABLE", "~all~", 1) != 0) 115 return refuse(EXIT_ENVIRONMENT, "cannot pin loader variables"); 116 fprintf(stderr, "vk-host: target=%s VK_ICD_FILENAMES=%s VK_DRIVER_FILES=%s layers=disabled%s\n", 117 TARGET_NAME, icd, icd, VKSPIRV_TARGET == 1 ? " linked=direct-no-loader" : ""); 118 return 0; 119 } 120 121 static void *read_file(const char *path, size_t *size) 122 { 123 FILE *f = fopen(path, "rb"); 124 if (!f) 125 return NULL; 126 void *buf = NULL; 127 if (fseek(f, 0, SEEK_END) == 0) { 128 long n = ftell(f); 129 if (n > 0 && n % 4 == 0 && n <= (1L << 26) && fseek(f, 0, SEEK_SET) == 0) { 130 buf = malloc((size_t)n); 131 if (buf && fread(buf, 1, (size_t)n, f) != (size_t)n) { 132 free(buf); 133 buf = NULL; 134 } 135 *size = (size_t)n; 136 } 137 } 138 fclose(f); 139 return buf; 140 } 141 142 // Reads rows into a malloc'd array of rows*nparams words; returns row count or 0. 143 static uint32_t read_rows(unsigned nparams, uint64_t **out) 144 { 145 size_t cap = 1024, rows = 0; 146 uint64_t *v = malloc(cap * (nparams ? nparams : 1) * sizeof *v); 147 char line[512]; 148 if (!v) 149 return 0; 150 while (fgets(line, sizeof line, stdin)) { 151 size_t n = strlen(line); 152 if (n == 0 || line[n - 1] != '\n' || rows == MAX_ROWS) 153 goto bad; 154 line[--n] = 0; 155 if (rows == cap) { 156 cap *= 2; 157 uint64_t *g = realloc(v, cap * (nparams ? nparams : 1) * sizeof *v); 158 if (!g) 159 goto bad; 160 v = g; 161 } 162 size_t p = 0; 163 for (unsigned j = 0; j < nparams; j++) { 164 size_t q = p; 165 while (line[q] && line[q] != ' ') 166 q++; 167 if (parse_u64(line + p, q - p, &v[rows * nparams + j]) != 0) 168 goto bad; 169 if (j + 1 < nparams) { 170 if (line[q] != ' ') 171 goto bad; 172 q++; 173 } 174 p = q; 175 } 176 if (line[p] != 0) 177 goto bad; 178 rows++; 179 } 180 if (ferror(stdin) || rows == 0) 181 goto bad; 182 *out = v; 183 return (uint32_t)rows; 184 bad: 185 free(v); 186 return 0; 187 } 188 189 static int find_memory(VkPhysicalDevice pd, uint32_t bits, uint32_t *index) 190 { 191 VkPhysicalDeviceMemoryProperties mp; 192 vkGetPhysicalDeviceMemoryProperties(pd, &mp); 193 VkMemoryPropertyFlags want = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; 194 for (uint32_t i = 0; i < mp.memoryTypeCount; i++) 195 if ((bits & (1u << i)) && (mp.memoryTypes[i].propertyFlags & want) == want) { 196 *index = i; 197 return 0; 198 } 199 return -1; 200 } 201 202 static int make_buffer(VkPhysicalDevice pd, VkDevice dev, VkDeviceSize size, VkBuffer *buf, 203 VkDeviceMemory *mem, void **map) 204 { 205 VkBufferCreateInfo bi = { 206 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, 207 .size = size, 208 .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 209 .sharingMode = VK_SHARING_MODE_EXCLUSIVE, 210 }; 211 VkResult r = vkCreateBuffer(dev, &bi, NULL, buf); 212 if (r != VK_SUCCESS) 213 return vkfail(EXIT_DEVICE, "vkCreateBuffer", r); 214 VkMemoryRequirements req; 215 vkGetBufferMemoryRequirements(dev, *buf, &req); 216 uint32_t type; 217 if (find_memory(pd, req.memoryTypeBits, &type) != 0) 218 return refuse(EXIT_DEVICE, "no host-visible coherent memory type"); 219 VkMemoryAllocateInfo ai = { 220 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 221 .allocationSize = req.size, 222 .memoryTypeIndex = type, 223 }; 224 if ((r = vkAllocateMemory(dev, &ai, NULL, mem)) != VK_SUCCESS) 225 return vkfail(EXIT_DEVICE, "vkAllocateMemory", r); 226 if ((r = vkBindBufferMemory(dev, *buf, *mem, 0)) != VK_SUCCESS) 227 return vkfail(EXIT_DEVICE, "vkBindBufferMemory", r); 228 if ((r = vkMapMemory(dev, *mem, 0, VK_WHOLE_SIZE, 0, map)) != VK_SUCCESS) 229 return vkfail(EXIT_DEVICE, "vkMapMemory", r); 230 return 0; 231 } 232 233 static int pick_device(VkInstance inst, VkPhysicalDevice *out) 234 { 235 uint32_t n = 0; 236 VkResult r = vkEnumeratePhysicalDevices(inst, &n, NULL); 237 if (r != VK_SUCCESS) 238 return vkfail(EXIT_DEVICE, "vkEnumeratePhysicalDevices", r); 239 if (n != 1) 240 return refuse(EXIT_DEVICE, "pinned ICD must expose exactly one physical device"); 241 if ((r = vkEnumeratePhysicalDevices(inst, &n, out)) != VK_SUCCESS) 242 return vkfail(EXIT_DEVICE, "vkEnumeratePhysicalDevices", r); 243 VkPhysicalDeviceDriverProperties drv = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES}; 244 VkPhysicalDeviceProperties2 p2 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &drv}; 245 vkGetPhysicalDeviceProperties2(*out, &p2); 246 const VkPhysicalDeviceProperties *p = &p2.properties; 247 fprintf(stderr, "vk-host: device=\"%s\" type=%d driver=\"%s\" info=\"%s\" api=%u.%u.%u vendor=0x%x\n", 248 p->deviceName, (int)p->deviceType, drv.driverName, drv.driverInfo, 249 VK_API_VERSION_MAJOR(p->apiVersion), VK_API_VERSION_MINOR(p->apiVersion), 250 VK_API_VERSION_PATCH(p->apiVersion), p->vendorID); 251 #if VKSPIRV_TARGET == 0 252 if (p->deviceType != VK_PHYSICAL_DEVICE_TYPE_CPU || strncmp(p->deviceName, "llvmpipe", 8) != 0 || 253 drv.driverID != VK_DRIVER_ID_MESA_LLVMPIPE) 254 return refuse(EXIT_DEVICE, "physical device is not lavapipe (llvmpipe CPU)"); 255 #elif VKSPIRV_TARGET == 1 256 if (drv.driverID != VK_DRIVER_ID_MOLTENVK) 257 return refuse(EXIT_DEVICE, "physical device is not MoltenVK"); 258 #else 259 if ((p->deviceType != VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU && 260 p->deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) || 261 drv.driverID != VK_DRIVER_ID_NVIDIA_PROPRIETARY) 262 return refuse(EXIT_DEVICE, "physical device is not an NVIDIA proprietary-driver GPU"); 263 #endif 264 VkPhysicalDeviceFeatures f; 265 vkGetPhysicalDeviceFeatures(*out, &f); 266 if (!f.shaderInt64) 267 return refuse(EXIT_DEVICE, "device lacks shaderInt64"); 268 return 0; 269 } 270 271 int main(int argc, char **argv) 272 { 273 if (argc != 4) 274 return refuse(EXIT_USAGE, "usage: vk-host MODULE.spv ENTRY NPARAMS < ROWS"); 275 uint64_t np64; 276 if (parse_u64(argv[3], strlen(argv[3]), &np64) != 0 || np64 > MAX_PARAMS) 277 return refuse(EXIT_USAGE, "NPARAMS must be 0..6"); 278 unsigned nparams = (unsigned)np64; 279 int rc = pin_loader(); 280 if (rc) 281 return rc; 282 size_t codesize = 0; 283 uint32_t *code = read_file(argv[1], &codesize); 284 if (!code) 285 return refuse(EXIT_MODULE, "cannot read module (nonempty, multiple of 4 bytes)"); 286 uint64_t *in = NULL; 287 uint32_t rows = read_rows(nparams, &in); 288 if (rows == 0) 289 return refuse(EXIT_INPUT, "malformed or empty ROWS on stdin"); 290 291 VkApplicationInfo app = { 292 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, 293 .pApplicationName = "vkspirv-host", 294 .apiVersion = VK_API_VERSION_1_3, 295 }; 296 VkInstanceCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app}; 297 VkInstance inst; 298 VkResult r = vkCreateInstance(&ii, NULL, &inst); 299 if (r != VK_SUCCESS) 300 return vkfail(EXIT_DEVICE, "vkCreateInstance", r); 301 VkPhysicalDevice pd; 302 if ((rc = pick_device(inst, &pd))) 303 return rc; 304 305 uint32_t nq = 0, qf = UINT32_MAX; 306 vkGetPhysicalDeviceQueueFamilyProperties(pd, &nq, NULL); 307 VkQueueFamilyProperties qp[16]; 308 if (nq > 16) 309 nq = 16; 310 vkGetPhysicalDeviceQueueFamilyProperties(pd, &nq, qp); 311 for (uint32_t i = 0; i < nq && qf == UINT32_MAX; i++) 312 if (qp[i].queueFlags & VK_QUEUE_COMPUTE_BIT) 313 qf = i; 314 if (qf == UINT32_MAX) 315 return refuse(EXIT_DEVICE, "no compute queue"); 316 float prio = 1.0f; 317 VkDeviceQueueCreateInfo qi = { 318 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, 319 .queueFamilyIndex = qf, 320 .queueCount = 1, 321 .pQueuePriorities = &prio, 322 }; 323 VkPhysicalDeviceFeatures want = {.shaderInt64 = VK_TRUE}; 324 // A portability (MoltenVK) device must have VK_KHR_portability_subset enabled. 325 const char *ext[1]; 326 uint32_t next = 0, nprop = 0; 327 VkExtensionProperties props[512]; 328 if ((r = vkEnumerateDeviceExtensionProperties(pd, NULL, &nprop, NULL)) != VK_SUCCESS) 329 return vkfail(EXIT_DEVICE, "vkEnumerateDeviceExtensionProperties", r); 330 if (nprop > 512) 331 nprop = 512; 332 if ((r = vkEnumerateDeviceExtensionProperties(pd, NULL, &nprop, props)) != VK_SUCCESS && 333 r != VK_INCOMPLETE) 334 return vkfail(EXIT_DEVICE, "vkEnumerateDeviceExtensionProperties", r); 335 for (uint32_t i = 0; i < nprop; i++) 336 if (strcmp(props[i].extensionName, "VK_KHR_portability_subset") == 0) 337 ext[next++] = "VK_KHR_portability_subset"; 338 VkDeviceCreateInfo di = { 339 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, 340 .queueCreateInfoCount = 1, 341 .pQueueCreateInfos = &qi, 342 .enabledExtensionCount = next, 343 .ppEnabledExtensionNames = next ? ext : NULL, 344 .pEnabledFeatures = &want, 345 }; 346 VkDevice dev; 347 if ((r = vkCreateDevice(pd, &di, NULL, &dev)) != VK_SUCCESS) 348 return vkfail(EXIT_DEVICE, "vkCreateDevice", r); 349 VkQueue queue; 350 vkGetDeviceQueue(dev, qf, 0, &queue); 351 352 VkDeviceSize insize = (VkDeviceSize)rows * nparams * 8; 353 if (insize == 0) 354 insize = 8; 355 VkDeviceSize outsize = (VkDeviceSize)rows * 16; 356 VkBuffer bin, bout; 357 VkDeviceMemory min, mout; 358 void *pin, *pout; 359 if ((rc = make_buffer(pd, dev, insize, &bin, &min, &pin)) || 360 (rc = make_buffer(pd, dev, outsize, &bout, &mout, &pout))) 361 return rc; 362 memset(pin, 0, (size_t)insize); 363 if (nparams) 364 memcpy(pin, in, (size_t)rows * nparams * 8); 365 uint64_t *o = pout; 366 for (uint64_t i = 0; i < (uint64_t)rows * 2; i++) 367 o[i] = POISON; 368 369 VkShaderModuleCreateInfo si = { 370 .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, 371 .codeSize = codesize, 372 .pCode = code, 373 }; 374 VkShaderModule sm; 375 if ((r = vkCreateShaderModule(dev, &si, NULL, &sm)) != VK_SUCCESS) 376 return vkfail(EXIT_PIPELINE, "vkCreateShaderModule", r); 377 VkDescriptorSetLayoutBinding lb[2] = { 378 {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = 1, 379 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}, 380 {.binding = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = 1, 381 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}, 382 }; 383 VkDescriptorSetLayoutCreateInfo li = { 384 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, 385 .bindingCount = 2, 386 .pBindings = lb, 387 }; 388 VkDescriptorSetLayout dsl; 389 if ((r = vkCreateDescriptorSetLayout(dev, &li, NULL, &dsl)) != VK_SUCCESS) 390 return vkfail(EXIT_PIPELINE, "vkCreateDescriptorSetLayout", r); 391 VkPipelineLayoutCreateInfo pli = { 392 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, 393 .setLayoutCount = 1, 394 .pSetLayouts = &dsl, 395 }; 396 VkPipelineLayout pl; 397 if ((r = vkCreatePipelineLayout(dev, &pli, NULL, &pl)) != VK_SUCCESS) 398 return vkfail(EXIT_PIPELINE, "vkCreatePipelineLayout", r); 399 VkComputePipelineCreateInfo ci = { 400 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, 401 .stage = { 402 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, 403 .stage = VK_SHADER_STAGE_COMPUTE_BIT, 404 .module = sm, 405 .pName = argv[2], 406 }, 407 .layout = pl, 408 }; 409 VkPipeline pipe; 410 if ((r = vkCreateComputePipelines(dev, VK_NULL_HANDLE, 1, &ci, NULL, &pipe)) != VK_SUCCESS) 411 return vkfail(EXIT_PIPELINE, "vkCreateComputePipelines", r); 412 413 VkDescriptorPoolSize ps = {.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = 2}; 414 VkDescriptorPoolCreateInfo dpi = { 415 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, 416 .maxSets = 1, 417 .poolSizeCount = 1, 418 .pPoolSizes = &ps, 419 }; 420 VkDescriptorPool pool; 421 if ((r = vkCreateDescriptorPool(dev, &dpi, NULL, &pool)) != VK_SUCCESS) 422 return vkfail(EXIT_PIPELINE, "vkCreateDescriptorPool", r); 423 VkDescriptorSetAllocateInfo dai = { 424 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, 425 .descriptorPool = pool, 426 .descriptorSetCount = 1, 427 .pSetLayouts = &dsl, 428 }; 429 VkDescriptorSet set; 430 if ((r = vkAllocateDescriptorSets(dev, &dai, &set)) != VK_SUCCESS) 431 return vkfail(EXIT_PIPELINE, "vkAllocateDescriptorSets", r); 432 VkDescriptorBufferInfo bi[2] = { 433 {.buffer = bin, .offset = 0, .range = insize}, 434 {.buffer = bout, .offset = 0, .range = outsize}, 435 }; 436 VkWriteDescriptorSet w[2] = { 437 {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, .dstBinding = 0, 438 .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .pBufferInfo = &bi[0]}, 439 {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, .dstBinding = 1, 440 .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .pBufferInfo = &bi[1]}, 441 }; 442 vkUpdateDescriptorSets(dev, 2, w, 0, NULL); 443 444 VkCommandPoolCreateInfo cpi = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .queueFamilyIndex = qf}; 445 VkCommandPool cp; 446 if ((r = vkCreateCommandPool(dev, &cpi, NULL, &cp)) != VK_SUCCESS) 447 return vkfail(EXIT_RUN, "vkCreateCommandPool", r); 448 VkCommandBufferAllocateInfo cai = { 449 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, 450 .commandPool = cp, 451 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, 452 .commandBufferCount = 1, 453 }; 454 VkCommandBuffer cb; 455 if ((r = vkAllocateCommandBuffers(dev, &cai, &cb)) != VK_SUCCESS) 456 return vkfail(EXIT_RUN, "vkAllocateCommandBuffers", r); 457 VkCommandBufferBeginInfo cbi = { 458 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 459 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, 460 }; 461 if ((r = vkBeginCommandBuffer(cb, &cbi)) != VK_SUCCESS) 462 return vkfail(EXIT_RUN, "vkBeginCommandBuffer", r); 463 vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); 464 vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0, 1, &set, 0, NULL); 465 vkCmdDispatch(cb, (rows + LOCAL_SIZE - 1) / LOCAL_SIZE, 1, 1); 466 VkMemoryBarrier mb = { 467 .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, 468 .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, 469 .dstAccessMask = VK_ACCESS_HOST_READ_BIT, 470 }; 471 vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &mb, 472 0, NULL, 0, NULL); 473 if ((r = vkEndCommandBuffer(cb)) != VK_SUCCESS) 474 return vkfail(EXIT_RUN, "vkEndCommandBuffer", r); 475 VkFenceCreateInfo fi = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; 476 VkFence fence; 477 if ((r = vkCreateFence(dev, &fi, NULL, &fence)) != VK_SUCCESS) 478 return vkfail(EXIT_RUN, "vkCreateFence", r); 479 VkSubmitInfo sub = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &cb}; 480 if ((r = vkQueueSubmit(queue, 1, &sub, fence)) != VK_SUCCESS) 481 return vkfail(EXIT_RUN, "vkQueueSubmit", r); 482 if ((r = vkWaitForFences(dev, 1, &fence, VK_TRUE, UINT64_C(`600000000000`))) != VK_SUCCESS) 483 return vkfail(EXIT_RUN, "vkWaitForFences", r); 484 for (uint32_t g = 0; g < rows; g++) 485 if (o[2 * g + 1] == POISON) 486 return refuse(EXIT_RUN, "an invocation did not write its status word"); 487 for (uint32_t g = 0; g < rows; g++) 488 if (printf("%llu %llu\n", (unsigned long long)o[2 * g], (unsigned long long)o[2 * g + 1]) < 0) 489 return refuse(EXIT_RUN, "stdout write failed"); 490 if (fflush(stdout) != 0) 491 return refuse(EXIT_RUN, "stdout flush failed"); 492 fprintf(stderr, "vk-host: rows=%u nparams=%u workgroups=%u local_size=%u entry=%s\n", rows, nparams, 493 (rows + LOCAL_SIZE - 1) / LOCAL_SIZE, LOCAL_SIZE, argv[2]); 494 vkDestroyFence(dev, fence, NULL); 495 vkDestroyCommandPool(dev, cp, NULL); 496 vkDestroyDescriptorPool(dev, pool, NULL); 497 vkDestroyPipeline(dev, pipe, NULL); 498 vkDestroyPipelineLayout(dev, pl, NULL); 499 vkDestroyDescriptorSetLayout(dev, dsl, NULL); 500 vkDestroyShaderModule(dev, sm, NULL); 501 vkUnmapMemory(dev, min); 502 vkUnmapMemory(dev, mout); 503 vkDestroyBuffer(dev, bin, NULL); 504 vkDestroyBuffer(dev, bout, NULL); 505 vkFreeMemory(dev, min, NULL); 506 vkFreeMemory(dev, mout, NULL); 507 vkDestroyDevice(dev, NULL); 508 vkDestroyInstance(inst, NULL); 509 free(in); 510 free(code); 511 return 0; 512 }