clear color works
[demo] / src / vulkan / vkutil.cc
1 #include <gmath/gmath.h>
2 #include <vulkan/vulkan.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <string>
8 #include <vector>
9
10 #include "vkutil.h"
11
12 /* global variables */
13 VkPhysicalDevice vk_physical;
14 VkDevice vk_device;
15 VkCommandPool vk_pool;
16 VkQueue vk_queue;
17 VkSurfaceKHR vk_surface;
18 VkSwapchainKHR vk_swapchain;
19
20 VkPipeline *vkgparent_pipeline;
21 VkFramebuffer *vkfbufs;
22 VkRenderPass vkrpass;
23 VkInstance vkinst;
24 VkCommandBuffer vkcmdbuf;       /* primary command buffer */
25 int vkqfamily;
26
27 VkSemaphore vk_img_avail_sema;
28 VkSemaphore vk_rend_done_sema;
29 VkImage *vkswapchain_images;
30 VkImageView *vkswapchain_views;
31 int vknum_swapchain_images;
32 int vk_curr_swapchain_image;
33
34 /* static functions */
35 static const char *get_device_name_str(VkPhysicalDeviceType type);
36 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags);
37 static const char *get_queue_flags_str(VkQueueFlags flags);
38 static const char *get_mem_size_str(long sz);
39 static int ver_major(uint32_t ver);
40 static int ver_minor(uint32_t ver);
41 static int ver_patch(uint32_t ver);
42
43 /* static variables */
44 static VkPhysicalDevice *phys_devices;
45
46 static int sel_dev;
47 static int sel_qfamily;
48
49 static VkExtensionProperties *vkext, *vkdevext;
50 static uint32_t vkext_count, vkdevext_count;
51
52 bool vku_have_extension(const char *name)
53 {
54         if(!vkext) {
55                 vkext_count = 0;
56                 vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0);
57                 if(vkext_count) {
58                         vkext = new VkExtensionProperties[vkext_count];
59                         vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext);
60
61                         printf("instance extensions:\n");
62                         for(int i=0; i<(int)vkext_count; i++) {
63                                 printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion);
64                         }
65                 }
66         }
67
68         for(int i=0; i<(int)vkext_count; i++) {
69                 if(strcmp(vkext[i].extensionName, name) == 0) {
70                         return true;
71                 }
72         }
73         return false;
74 }
75
76 bool vku_have_device_extension(const char *name)
77 {
78         if(sel_dev < 0) return false;
79
80         if(!vkdevext) {
81                 vkdevext_count = 0;
82                 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0);
83                 if(vkdevext_count) {
84                         vkdevext = new VkExtensionProperties[vkdevext_count];
85                         vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext);
86
87                         printf("selected device extensions:\n");
88                         for(int i=0; i<(int)vkdevext_count; i++) {
89                                 printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion);
90                         }
91                 }
92         }
93
94         for(int i=0; i<(int)vkdevext_count; i++) {
95                 if(strcmp(vkdevext[i].extensionName, name) == 0) {
96                         return true;
97                 }
98         }
99         return false;
100 }
101
102 bool vku_create_device()
103 {
104         VkInstanceCreateInfo inst_info;
105         VkDeviceCreateInfo dev_info;
106         VkDeviceQueueCreateInfo queue_info;
107         VkCommandPoolCreateInfo cmdpool_info;
108         uint32_t num_devices;
109         float qprio = 0.0f;
110
111         static const char *ext_names[] = {
112                 "VK_KHR_xcb_surface",
113                 "VK_KHR_surface"
114         };
115
116         static const char *devext_names[] = {
117                 "VK_KHR_swapchain"
118         };
119
120         sel_dev = -1;
121         sel_qfamily = -1;
122
123         for(unsigned int i=0; i<sizeof ext_names / sizeof *ext_names; i++) {
124                 if(!vku_have_extension(ext_names[i])) {
125                         fprintf(stderr, "required extension (%s) not found\n", ext_names[i]);
126                         return false;
127                 }
128         }
129         memset(&inst_info, 0, sizeof inst_info);
130         inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
131         inst_info.ppEnabledExtensionNames = ext_names;
132         inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
133
134         if(vkCreateInstance(&inst_info, 0, &vkinst) != 0) {
135                 fprintf(stderr, "failed to create vulkan instance\n");
136                 return false;
137         }
138         printf("created vulkan instance\n");
139         if(vkEnumeratePhysicalDevices(vkinst, &num_devices, 0) != 0) {
140                 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
141                 return false;
142         }
143         phys_devices = new VkPhysicalDevice[num_devices];
144         if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) {
145                 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
146                 return false;
147         }
148         printf("found %u physical device(s)\n", (unsigned int)num_devices);
149
150         for(int i=0; i<(int)num_devices; i++) {
151                 VkPhysicalDeviceProperties dev_prop;
152                 VkPhysicalDeviceMemoryProperties mem_prop;
153                 VkQueueFamilyProperties *qprop;
154                 uint32_t qprop_count;
155
156                 vkGetPhysicalDeviceProperties(phys_devices[i], &dev_prop);
157
158                 printf("Device %d: %s\n", i, dev_prop.deviceName);
159                 printf("  type: %s\n", get_device_name_str(dev_prop.deviceType));
160                 printf("  API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
161                        ver_patch(dev_prop.apiVersion));
162                 printf("  driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
163                        ver_patch(dev_prop.driverVersion));
164                 printf("  vendor id: %x  device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
165
166
167                 vkGetPhysicalDeviceMemoryProperties(phys_devices[i], &mem_prop);
168                 printf("  %d memory heaps:\n", mem_prop.memoryHeapCount);
169                 for(unsigned int j=0; j<mem_prop.memoryHeapCount; j++) {
170                         VkMemoryHeap heap = mem_prop.memoryHeaps[j];
171                         printf("    Heap %d - size: %s, flags: %s\n", j, get_mem_size_str(heap.size),
172                                heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
173                 }
174                 printf("  %d memory types:\n", mem_prop.memoryTypeCount);
175                 for(unsigned int j=0; j<mem_prop.memoryTypeCount; j++) {
176                         VkMemoryType type = mem_prop.memoryTypes[j];
177                         printf("    Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
178                                get_memtype_flags_str(type.propertyFlags));
179                 }
180
181                 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, 0);
182                 if(qprop_count <= 0) {
183                         continue;
184                 }
185                 qprop = new VkQueueFamilyProperties[qprop_count];
186                 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop);
187
188                 for(unsigned int j=0; j<qprop_count; j++) {
189                         printf("  Queue family %d:\n", j);
190                         printf("    flags: %s\n", get_queue_flags_str(qprop[j].queueFlags));
191                         printf("    num queues: %u\n", qprop[j].queueCount);
192
193                         if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
194                                 sel_dev = i;
195                                 sel_qfamily = j;
196                         }
197                 }
198                 delete [] qprop;
199         }
200
201         if(sel_dev < 0 || sel_qfamily < 0) {
202                 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
203                 vkDestroyDevice(vk_device, 0);
204                 return false;
205         }
206
207         for(unsigned int i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
208                 if(!vku_have_device_extension(devext_names[i])) {
209                         fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
210                                 ext_names[i], sel_dev);
211                         return false;
212                 }
213         }
214
215         /* create device & command queue */
216         memset(&queue_info, 0, sizeof queue_info);
217         queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
218         queue_info.queueFamilyIndex = sel_qfamily;
219         queue_info.queueCount = 1;
220         queue_info.pQueuePriorities = &qprio;
221
222         memset(&dev_info, 0, sizeof dev_info);
223         dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
224         dev_info.queueCreateInfoCount = 1;
225         dev_info.pQueueCreateInfos = &queue_info;
226         dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
227         dev_info.ppEnabledExtensionNames = devext_names;
228
229         if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vk_device) != 0) {
230                 fprintf(stderr, "failed to create device %d\n", sel_dev);
231                 return false;
232         }
233         printf("created device %d\n", sel_dev);
234
235         vk_physical = phys_devices[sel_dev];
236         vkqfamily = sel_qfamily;
237
238         vkGetDeviceQueue(vk_device, sel_qfamily, 0, &vk_queue);
239
240         /* create command buffer pool */
241         memset(&cmdpool_info, 0, sizeof cmdpool_info);
242         cmdpool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
243         cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
244         cmdpool_info.queueFamilyIndex = sel_qfamily;
245
246         if(vkCreateCommandPool(vk_device, &cmdpool_info, 0, &vk_pool) != 0) {
247                 fprintf(stderr, "failed to get command queue!\n");
248                 return false;
249         }
250
251         /*      if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
252                         fprintf(stderr, "failed to create primary command buffer\n");
253                         return false;
254                 } */
255
256         return true;
257 }
258
259 void vku_cleanup()
260 {
261         if(vkinst) {
262                 vkDeviceWaitIdle(vk_device);
263                 vkDestroyCommandPool(vk_device, vk_pool, 0);
264
265                 vkDestroySemaphore(vk_device, vk_img_avail_sema, 0);
266                 vkDestroySemaphore(vk_device, vk_rend_done_sema, 0);
267
268                 vkDestroyDevice(vk_device, 0);
269                 vkDestroyInstance(vkinst, 0);
270                 vkinst = 0;
271         }
272
273         delete [] phys_devices;
274         phys_devices = 0;
275 }
276
277 bool vku_create_semaphores()
278 {
279         VkSemaphoreCreateInfo sinf;
280         memset(&sinf, 0, sizeof sinf);
281
282         sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
283         if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_img_avail_sema) != VK_SUCCESS) {
284                 fprintf(stderr, "Failed to create semaphore\n");
285                 return false;
286         }
287
288         if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_rend_done_sema) != VK_SUCCESS) {
289                 fprintf(stderr, "Failed to create semaphore\n");
290                 return false;
291         }
292
293         return true;
294 }
295
296 VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
297 {
298         VkCommandBuffer cmdbuf;
299         VkCommandBufferAllocateInfo inf;
300
301         memset(&inf, 0, sizeof inf);
302         inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
303         inf.commandPool = pool;
304         inf.level = level;
305         inf.commandBufferCount = 1;
306
307         if(vkAllocateCommandBuffers(vk_device, &inf, &cmdbuf) != 0) {
308                 return 0;
309         }
310         return cmdbuf;
311 }
312
313 bool vku_alloc_cmdbufs(VkCommandPool pool, VkCommandBufferLevel level, unsigned int count, VkCommandBuffer *buf_array)
314 {
315         VkCommandBufferAllocateInfo cinf;
316         memset(&cinf, 0, sizeof cinf);
317
318         cinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
319         cinf.commandPool = vk_pool;
320         cinf.level = level;
321         cinf.commandBufferCount = count;
322
323         if(vkAllocateCommandBuffers(vk_device, &cinf, buf_array) != VK_SUCCESS) {
324                 fprintf(stderr, "Failed to allocate command buffer\n");
325                 return false;
326         }
327
328         return true;
329 }
330
331 void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf)
332 {
333         vkFreeCommandBuffers(vk_device, pool, 1, &buf);
334 }
335
336 void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
337 {
338         VkCommandBufferBeginInfo inf;
339
340         memset(&inf, 0, sizeof inf);
341         inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
342         inf.flags = flags;
343
344         vkBeginCommandBuffer(buf, &inf);
345 }
346
347
348 void vku_end_cmdbuf(VkCommandBuffer buf)
349 {
350         vkEndCommandBuffer(buf);
351 }
352
353 void vku_reset_cmdbuf(VkCommandBuffer buf)
354 {
355         vkResetCommandBuffer(buf, 0);
356 }
357
358 void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
359 {
360         VkSubmitInfo info;
361
362         memset(&info, 0, sizeof info);
363         info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
364         info.commandBufferCount = 1;
365         info.pCommandBuffers = &buf;
366
367         vkQueueSubmit(q, 1, &info, done_fence);
368 }
369
370 bool vku_get_surface_format(VkPhysicalDevice gpu, VkSurfaceKHR surface, VkSurfaceFormatKHR *sformat)
371 {
372         uint32_t fcount;
373
374         if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical,
375                                                 vk_surface, &fcount, 0) != VK_SUCCESS) {
376                 fprintf(stderr, "Failed to get format count for physical device.\n");
377                 return false;
378         }
379         if(fcount == 0) {
380                 fprintf(stderr, "No color formats were found.\n");
381                 return false;
382         }
383
384         VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount];
385         if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface,
386                                                 &fcount, formats) != VK_SUCCESS) {
387                 delete [] formats;
388                 fprintf(stderr, "Failed to get surface formats.\n");
389                 return false;
390         }
391
392         *sformat = formats[0];
393
394         if((fcount == 1) && (formats[0].format == VK_FORMAT_UNDEFINED)) {
395                 sformat->format = VK_FORMAT_B8G8R8_UNORM;
396         }
397
398         delete [] formats;
399         return true;
400 }
401
402
403 int vku_get_next_image(VkSwapchainKHR sc)
404 {
405         uint32_t next;
406
407         if(vkAcquireNextImageKHR(vk_device, sc, UINT64_MAX, 0, 0, &next) != 0) {
408                 return -1;
409         }
410         return (int)next;
411 }
412
413 void vku_present(VkSwapchainKHR sc, int img_idx)
414 {
415         VkPresentInfoKHR inf;
416         VkResult res;
417         uint32_t index = img_idx;
418
419         memset(&inf, 0, sizeof inf);
420         inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
421         inf.swapchainCount = 1;
422         inf.pSwapchains = &sc;
423         inf.pImageIndices = &index;
424         inf.pResults = &res;
425
426         vkQueuePresentKHR(vk_queue, &inf);
427 }
428
429 struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
430 {
431         struct vku_buffer *buf;
432         VkBufferCreateInfo binfo;
433
434         buf = new vku_buffer;
435
436         memset(&binfo, 0, sizeof binfo);
437         binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
438         binfo.size = sz;
439         binfo.usage = usage;
440         binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
441
442         if(vkCreateBuffer(vk_device, &binfo, 0, &buf->buf) != 0) {
443                 fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
444                 return 0;
445         }
446         // TODO back with memory
447         return buf;
448 }
449
450 void vku_destroy_buffer(struct vku_buffer *buf)
451 {
452         if(buf) {
453                 vkDestroyBuffer(vk_device, buf->buf, 0);
454                 delete buf;
455         }
456 }
457
458 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
459                      VkBuffer src, int soffs, int size)
460 {
461         VkBufferCopy copy;
462         copy.size = size;
463         copy.srcOffset = soffs;
464         copy.dstOffset = doffs;
465
466         vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
467 }
468
469 const char *vku_get_vulkan_error_str(VkResult error)
470 {
471         std::string errmsg;
472         switch(error) {
473         case VK_SUCCESS:
474                 errmsg = std::string("VK_SUCCESS");
475                 break;
476         case VK_NOT_READY:
477                 errmsg = std::string("VK_NOT_READY");
478                 break;
479         case VK_TIMEOUT:
480                 errmsg = std::string("VK_TIMEOUT");
481                 break;
482         case VK_EVENT_SET:
483                 errmsg = std::string("VK_EVENT_SET");
484                 break;
485         case VK_EVENT_RESET:
486                 errmsg = std::string("VK_EVENT_RESET");
487                 break;
488         case VK_INCOMPLETE:
489                 errmsg = std::string("VK_EVENT");
490                 break;
491         case VK_ERROR_OUT_OF_HOST_MEMORY:
492                 errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
493                 break;
494         case VK_ERROR_OUT_OF_DEVICE_MEMORY:
495                 errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
496                 break;
497         case VK_ERROR_INITIALIZATION_FAILED:
498                 errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
499                 break;
500         case VK_ERROR_DEVICE_LOST:
501                 errmsg = std::string("VK_ERROR_DEVICE_LOST");
502                 break;
503         case VK_ERROR_MEMORY_MAP_FAILED:
504                 errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
505                 break;
506         case VK_ERROR_LAYER_NOT_PRESENT:
507                 errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
508                 break;
509         case VK_ERROR_EXTENSION_NOT_PRESENT:
510                 errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
511                 break;
512         case VK_ERROR_FEATURE_NOT_PRESENT:
513                 errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
514         case VK_ERROR_INCOMPATIBLE_DRIVER:
515                 errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
516                 break;
517         case VK_ERROR_TOO_MANY_OBJECTS:
518                 errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
519                 break;
520         case VK_ERROR_FORMAT_NOT_SUPPORTED:
521                 errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
522                 break;
523         case VK_ERROR_FRAGMENTED_POOL:
524                 errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
525                 break;
526         default:
527                 errmsg = std::string("UNKNOWN");
528                 break;
529         }
530
531         return errmsg.c_str();
532 }
533
534
535 static const char *get_device_name_str(VkPhysicalDeviceType type)
536 {
537         switch(type) {
538         case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
539                 return "integrated GPU";
540         case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
541                 return "discrete GPU";
542         case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
543                 return "virtual GPU";
544         case VK_PHYSICAL_DEVICE_TYPE_CPU:
545                 return "CPU";
546         default:
547                 break;
548         }
549         return "unknown";
550 }
551
552 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags)
553 {
554         static char str[128];
555
556         str[0] = 0;
557         if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
558                 strcat(str, "device-local ");
559         }
560         if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
561                 strcat(str, "host-visible ");
562         }
563         if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
564                 strcat(str, "host-coherent ");
565         }
566         if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
567                 strcat(str, "host-cached ");
568         }
569         if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
570                 strcat(str, "lazily-allocated ");
571         }
572
573         if(!*str) {
574                 strcat(str, "-");
575         }
576         return str;
577 }
578
579 static const char *get_queue_flags_str(VkQueueFlags flags)
580 {
581         static char str[128];
582
583         str[0] = 0;
584         if(flags & VK_QUEUE_GRAPHICS_BIT) {
585                 strcat(str, "graphics ");
586         }
587         if(flags & VK_QUEUE_COMPUTE_BIT) {
588                 strcat(str, "compute ");
589         }
590         if(flags & VK_QUEUE_TRANSFER_BIT) {
591                 strcat(str, "transfer ");
592         }
593         if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
594                 strcat(str, "sparse-binding ");
595         }
596         if(!*str) {
597                 strcat(str, "-");
598         }
599         return str;
600 }
601
602 static int ver_major(uint32_t ver)
603 {
604         return (ver >> 22) & 0x3ff;
605 }
606
607 static int ver_minor(uint32_t ver)
608 {
609         return (ver >> 12) & 0x3ff;
610 }
611
612 static int ver_patch(uint32_t ver)
613 {
614         return ver & 0xfff;
615 }
616
617 static const char *get_mem_size_str(long sz)
618 {
619         static char str[64];
620         static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
621         int uidx = 0;
622         sz *= 10;
623
624         while(sz >= 10240 && unitstr[uidx + 1]) {
625                 sz /= 1024;
626                 ++uidx;
627         }
628         sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
629         return str;
630 }