Home | History | Annotate | Download | only in demos

Lines Matching full:demo

51 #define APP_LONG_NAME "The Vulkan Cube Demo Program"
80 demo->fp##entrypoint = \
82 if (demo->fp##entrypoint == NULL) { \
94 demo->inst, "vkGetDeviceProcAddr"); \
95 demo->fp##entrypoint = \
97 if (demo->fp##entrypoint == NULL) { \
326 struct demo {
435 static void demo_resize(struct demo *demo);
437 static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
444 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
456 static void demo_flush_init_cmd(struct demo *demo) {
459 if (demo->cmd == VK_NULL_HANDLE)
462 err = vkEndCommandBuffer(demo->cmd);
465 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
477 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
480 err = vkQueueWaitIdle(demo->queue);
483 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
484 demo->cmd = VK_NULL_HANDLE;
487 static void demo_set_image_layout(struct demo *demo, VkImage image,
494 if (demo->cmd == VK_NULL_HANDLE) {
498 .commandPool = demo->cmd_pool,
503 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
522 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
562 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
566 static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
590 .renderPass = demo->render_pass,
591 .framebuffer = demo->framebuffers[demo->current_buffer],
594 .renderArea.extent.width = demo->width,
595 .renderArea.extent.height = demo->height,
606 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
608 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
613 viewport.height = (float)demo->height;
614 viewport.width = (float)demo->width;
621 scissor.extent.width = demo->width;
622 scissor.extent.height = demo->height;
641 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
651 void demo_update_data_buffer(struct demo *demo) {
657 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
660 mat4x4_dup(Model, demo->model_matrix);
661 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
662 (float)degreesToRadians(demo->spin_angle));
663 mat4x4_mul(MVP, VP, demo->model_matrix);
665 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
666 demo->uniform_data.mem_alloc.allocationSize, 0,
672 vkUnmapMemory(demo->device, demo->uniform_data.mem);
675 static void demo_draw(struct demo *demo) {
685 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
690 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
693 &demo->current_buffer);
695 // demo->swapchain is out of date (e.g. the window was resized) and
697 demo_resize(demo);
698 demo_draw(demo);
699 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
702 // demo->swapchain is not as optimal as it could be, but the platform's
710 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
715 demo_flush_init_cmd(demo);
732 &demo->buffers[demo->current_buffer].cmd,
736 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
743 .pSwapchains = &demo->swapchain,
744 .pImageIndices = &demo->current_buffer,
748 err = demo->fpQueuePresentKHR(demo->queue, &present);
750 // demo->swapchain is out of date (e.g. the window was resized) and
752 demo_resize(demo);
754 // demo->swapchain is not as optimal as it could be, but the platform's
760 err = vkQueueWaitIdle(demo->queue);
763 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
766 static void demo_prepare_buffers(struct demo *demo) {
768 VkSwapchainKHR oldSwapchain = demo->swapchain;
772 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
773 demo->gpu, demo->surface, &surfCapabilities);
777 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
778 demo->gpu, demo->surface, &presentModeCount, NULL);
783 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
784 demo->gpu, demo->surface, &presentModeCount, presentModes);
792 swapchainExtent.width = demo->width;
793 swapchainExtent.height = demo->height;
797 demo->width = surfCapabilities.currentExtent.width;
798 demo->height = surfCapabilities.currentExtent.height;
839 .surface = demo->surface,
841 .imageFormat = demo->format,
842 .imageColorSpace = demo->color_space,
860 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
861 &demo->swapchain);
869 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
872 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
873 &demo->swapchainImageCount, NULL);
877 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
879 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
880 &demo->swapchainImageCount,
884 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
885 demo->swapchainImageCount);
886 assert(demo->buffers);
888 for (i = 0; i < demo->swapchainImageCount; i++) {
892 .format = demo->format,
909 demo->buffers[i].image = swapchainImages[i];
916 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
920 color_image_view.image = demo->buffers[i].image;
922 err = vkCreateImageView(demo->device, &color_image_view, NULL,
923 &demo->buffers[i].view);
932 static void demo_prepare_depth(struct demo *demo) {
939 .extent = {demo->width, demo
966 demo->depth.format = depth_format;
969 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
972 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
975 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
976 demo->depth.mem_alloc.pNext = NULL;
977 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
978 demo->depth.mem_alloc.memoryTypeIndex = 0;
980 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
982 &demo->depth.mem_alloc.memoryTypeIndex);
986 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
987 &demo->depth.mem);
992 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
995 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1001 view.image = demo->depth.image;
1002 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
1055 static void demo_prepare_texture_image(struct demo *demo, const char *filename,
1093 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
1096 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
1103 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1109 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
1114 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
1126 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1129 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1137 vkUnmapMemory(demo->device, tex_obj->mem);
1141 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
1148 static void demo_destroy_texture_image(struct demo *demo,
1151 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1152 vkDestroyImage(demo->device, tex_objs->image, NULL);
1155 static void demo_prepare_textures(struct demo *demo) {
1160 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
1167 !demo->use_staging_buffer) {
1169 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1179 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
1185 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1189 demo_set_image_layout(demo, staging_texture.image,
1195 demo_set_image_layout(demo, demo->textures[i].image,
1197 demo->textures[i].imageLayout,
1210 demo->cmd, staging_texture.image,
1211 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1214 demo_set_image_layout(demo, demo->textures[i].image,
1217 demo->textures[i].imageLayout,
1220 demo_flush_init_cmd(demo);
1222 demo_destroy_texture_image(demo, &staging_texture);
1263 err = vkCreateSampler(demo->device, &sampler, NULL,
1264 &demo->textures[i].sampler);
1268 view.image = demo->textures[i].image;
1269 err = vkCreateImageView(demo->device, &view, NULL,
1270 &demo->textures[i].view);
1275 void demo_prepare_cube_data_buffer(struct demo *demo) {
1285 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
1286 mat4x4_mul(MVP, VP, demo->model_matrix);
1306 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
1309 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1312 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1313 demo->uniform_data.mem_alloc.pNext = NULL;
1314 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1315 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1318 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1319 &demo->uniform_data.mem_alloc.memoryTypeIndex);
1322 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1323 &(demo->uniform_data.mem));
1326 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1327 demo->uniform_data.mem_alloc.allocationSize, 0,
1333 vkUnmapMemory(demo->device, demo->uniform_data.mem);
1335 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1336 demo->uniform_data.mem, 0);
1339 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1340 demo->uniform_data.buffer_info.offset = 0;
1341 demo->uniform_data.buffer_info.range = sizeof(data);
1344 static void demo_prepare_descriptor_layout(struct demo *demo) {
1371 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1372 &demo->desc_layout);
1379 .pSetLayouts = &demo->desc_layout,
1382 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
1383 &demo->pipeline_layout);
1387 demo *demo) {
1391 .format = demo->format,
1402 .format = demo->depth.format,
1445 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
1450 demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
1461 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1491 static VkShaderModule demo_prepare_vs(struct demo *demo) {
1497 demo->vert_shader_module =
1498 demo_prepare_shader_module(demo, vertShaderCode, size);
1502 return demo->vert_shader_module;
1505 static VkShaderModule demo_prepare_fs(struct demo *demo) {
1511 demo->frag_shader_module =
1512 demo_prepare_shader_module(demo, fragShaderCode, size);
1516 return demo->frag_shader_module;
1519 static void demo_prepare_pipeline(struct demo *demo) {
1540 pipeline.layout = demo->pipeline_layout;
1600 shaderStages[0].module = demo_prepare_vs(demo);
1605 shaderStages[1].module = demo_prepare_fs(demo);
1611 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1612 &demo->pipelineCache);
1623 pipeline.renderPass = demo->render_pass;
1626 pipeline.renderPass = demo->render_pass;
1628 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1629 &pipeline, NULL, &demo->pipeline);
1632 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1633 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
1636 static void demo_prepare_descriptor_pool(struct demo *demo) {
1658 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1659 &demo->desc_pool);
1663 static void demo_prepare_descriptor_set(struct demo *demo) {
1672 .descriptorPool = demo->desc_pool,
1674 .pSetLayouts = &demo->desc_layout};
1675 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
1680 tex_descs[i].sampler = demo->textures[i].sampler;
1681 tex_descs[i].imageView = demo->textures[i].view;
1688 writes[0].dstSet = demo->desc_set;
1691 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
1694 writes[1].dstSet = demo->desc_set;
1700 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1703 static void demo_prepare_framebuffers(struct demo *demo) {
1705 attachments[1] = demo->depth.view;
1710 .renderPass = demo->render_pass,
1713 .width = demo->width,
1714 .height = demo->height,
1720 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1722 assert(demo->framebuffers);
1724 for (i = 0; i < demo->swapchainImageCount; i++) {
1725 attachments[0] = demo->buffers[i].view;
1726 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1727 &demo->framebuffers[i]);
1732 static void demo_prepare(struct demo *demo) {
1738 .queueFamilyIndex = demo->graphics_queue_node_index,
1741 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1742 &demo->cmd_pool);
1748 .commandPool = demo->cmd_pool,
1753 demo_prepare_buffers(demo);
1754 demo_prepare_depth(demo);
1755 demo_prepare_textures(demo);
1756 demo_prepare_cube_data_buffer(demo);
1758 demo_prepare_descriptor_layout(demo);
1759 demo_prepare_render_pass(demo);
1760 demo_prepare_pipeline(demo);
1762 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1764 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
1768 demo_prepare_descriptor_pool(demo);
1769 demo_prepare_descriptor_set(demo);
1771 demo_prepare_framebuffers(demo);
1773 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1774 demo->current_buffer = i;
1775 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1782 demo_flush_init_cmd(demo);
1784 demo->current_buffer = 0;
1785 demo->prepared = true;
1788 static void demo_cleanup(struct demo *demo) {
1791 demo->prepared = false;
1793 for (i = 0; i < demo->swapchainImageCount; i++) {
1794 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
1796 free(demo->framebuffers);
1797 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
1799 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1800 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1801 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1802 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1803 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
1806 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1807 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1808 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1809 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
1811 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
1813 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1814 vkDestroyImage(demo->device, demo->depth.image, NULL);
1815 vkFreeMemory(demo->device, demo->depth.mem, NULL);
1817 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1818 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
1820 for (i = 0; i < demo->swapchainImageCount; i++) {
1821 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
1822 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1823 &demo->buffers[i].cmd);
1825 free(demo->buffers);
1827 free(demo->queue_props);
1829 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1830 vkDestroyDevice(demo->device, NULL);
1831 if (demo->validate) {
1832 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
1834 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1835 vkDestroyInstance(demo->inst, NULL);
1838 xcb_destroy_window(demo->connection, demo->window);
1839 xcb_disconnect(demo->connection);
1840 free(demo->atom_wm_delete_window);
1844 static void demo_resize(struct demo *demo) {
1848 if (!demo->prepared) {
1855 demo->prepared = false;
1857 for (i = 0; i < demo->swapchainImageCount; i++) {
1858 vkDestroyFramebuffer(demo->device, demo
1860 free(demo->framebuffers);
1861 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
1863 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1864 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1865 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1866 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1867 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
1870 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1871 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1872 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1873 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
1876 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1877 vkDestroyImage(demo->device, demo->depth.image, NULL);
1878 vkFreeMemory(demo->device, demo->depth.mem, NULL);
1880 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1881 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
1883 for (i = 0; i < demo->swapchainImageCount; i++) {
1884 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
1885 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1886 &demo->buffers[i].cmd);
1888 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1889 free(demo->buffers);
1893 demo_prepare(demo);
1897 struct demo demo;
1900 static void demo_run(struct demo *demo) {
1901 if (!demo->prepared)
1904 vkDeviceWaitIdle(demo->device);
1905 demo_update_data_buffer(demo);
1907 demo_draw(demo);
1910 vkDeviceWaitIdle(demo->device);
1912 demo->curFrame++;
1914 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
1915 demo->quit = true;
1916 demo_cleanup(demo);
1928 demo_run(&demo);
1935 demo.width = lParam & 0xffff;
1936 demo.height = lParam & 0xffff0000 >> 16;
1937 demo_resize(&demo);
1946 static void demo_create_window(struct demo *demo) {
1955 win_class.hInstance = demo->connection; // hInstance
1960 win_class.lpszClassName = demo->name;
1970 RECT wr = {0, 0, demo->width, demo->height};
1972 demo->window = CreateWindowEx(0,
1973 demo->name, // class name
1974 demo->name, // app name
1982 demo->connection, // hInstance
1984 if (!demo->window) {
1992 static void demo_handle_event(struct demo *demo,
2001 (*demo->atom_wm_delete_window).atom) {
2002 demo->quit = true;
2011 demo->quit = true;
2014 demo->spin_angle += demo->spin_increment;
2017 demo->spin_angle -= demo->spin_increment;
2020 demo->pause = !demo->pause;
2027 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2028 demo->width = cfg->width;
2029 demo->height = cfg->height;
2030 demo_resize(demo);
2038 static void demo_run(struct demo *demo) {
2039 xcb_flush(demo->connection);
2041 while (!demo->quit) {
2044 if (demo->pause) {
2045 event = xcb_wait_for_event(demo->connection);
2047 event = xcb_poll_for_event(demo->connection);
2050 demo_handle_event(demo, event);
2055 vkDeviceWaitIdle(demo->device);
2056 demo_update_data_buffer(demo);
2058 demo_draw(demo);
2061 vkDeviceWaitIdle(demo->device);
2062 demo->curFrame++;
2063 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2064 demo->quit = true;
2068 static void demo_create_window(struct demo *demo) {
2071 demo->window = xcb_generate_id(demo->connection);
2074 value_list[0] = demo->screen->black_pixel;
2078 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
2079 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2080 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2085 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2087 xcb_intern_atom_reply(demo->connection, cookie, 0);
2090 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2091 demo->atom_wm_delete_window =
2092 xcb_intern_atom_reply(demo->connection, cookie2, 0);
2094 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
2096 &(*demo->atom_wm_delete_window).atom);
2099 xcb_map_window(demo->connection, demo->window);
2104 xcb_configure_window(demo->connection, demo->window,
2132 static void demo_init_vk(struct demo *demo) {
2149 demo->device_validation_layers[0] = "VK_LAYER_GOOGLE_threading";
2150 demo->device_validation_layers[1] = "VK_LAYER_LUNARG_parameter_validation";
2151 demo->device_validation_layers[2] = "VK_LAYER_LUNARG_device_limits";
2152 demo->device_validation_layers[3] = "VK_LAYER_LUNARG_object_tracker";
2153 demo->device_validation_layers[4] = "VK_LAYER_LUNARG_image";
2154 demo->device_validation_layers[5] = "VK_LAYER_LUNARG_core_validation";
2155 demo->device_validation_layers[6] = "VK_LAYER_LUNARG_swapchain";
2156 demo->device_validation_layers[7] = "VK_LAYER_GOOGLE_unique_objects";
2171 if (demo->validate) {
2183 if (demo->validate && !validation_found) {
2230 if (demo->validate) {
2284 (const char *const *)((demo->validate) ? instance_validation_layers
2292 demo->inst);
2310 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2315 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2317 /* For cube demo we just grab the first physical device */
2318 demo->gpu = physical_devices[0];
2330 demo->enabled_layer_count = 0;
2333 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2339 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2343 if (demo->validate) {
2345 demo->device_validation_layers,
2348 demo->enabled_layer_count = device_validation_layer_count;
2354 if (demo->validate && !validation_found) {
2365 demo->enabled_extension_count = 0;
2368 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2376 demo->gpu, NULL, &device_extension_count, device_extensions);
2383 demo->extension_names[demo->enabled_extension_count++] =
2386 assert(demo->enabled_extension_count < 64);
2402 if (demo->validate) {
2403 demo->CreateDebugReportCallback =
2405 demo->inst, "vkCreateDebugReportCallbackEXT");
2406 demo->DestroyDebugReportCallback =
2408 demo->inst, "vkDestroyDebugReportCallbackEXT");
2409 if (!demo->CreateDebugReportCallback) {
2414 if (!demo->DestroyDebugReportCallback) {
2419 demo->DebugReportMessage =
2421 demo->inst, "vkDebugReportMessageEXT");
2422 if (!demo->DebugReportMessage) {
2429 if (!demo->use_break) {
2444 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2445 &demo->msg_callback);
2459 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
2462 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2464 assert(demo->queue_count >= 1);
2466 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2467 demo->queue_count * sizeof(VkQueueFamilyProperties));
2468 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2469 demo->queue_props);
2472 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2474 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2477 assert(gfx_queue_idx < demo->queue_count);
2482 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
2484 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2485 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2486 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2487 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2488 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2491 static void demo_create_device(struct demo *demo) {
2497 .queueFamilyIndex = demo->graphics_queue_node_index,
2506 .enabledLayerCount = demo->enabled_layer_count,
2508 (const char *const *)((demo->validate)
2509 ? demo->device_validation_layers
2511 .enabledExtensionCount = demo->enabled_extension_count,
2512 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2517 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
2521 static void demo_init_vk_swapchain(struct demo *demo) {
2531 createInfo.hinstance = demo->connection;
2532 createInfo.hwnd = demo->window;
2535 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2542 createInfo.connection = demo->connection;
2543 createInfo.window = demo->window;
2545 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2550 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2551 for (i = 0; i < demo->queue_count; i++) {
2552 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
2560 for (i = 0; i < demo->queue_count; i++) {
2561 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2576 for (uint32_t i = 0; i < demo->queue_count; ++i) {
2595 // and a present queues, this demo program assumes it is only using
2602 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
2604 demo_create_device(demo);
2606 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2607 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2608 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2609 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2610 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2612 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2613 &demo->queue);
2617 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
2622 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
2629 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2632 demo->format = surfFormats[0].format;
2634 demo->color_space = surfFormats[0].colorSpace;
2636 demo->quit = false;
2637 demo->curFrame = 0;
2640 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2643 static void demo_init_connection(struct demo *demo) {
2649 demo->connection = xcb_connect(NULL, &scr);
2650 if (demo->connection == NULL) {
2657 setup = xcb_get_setup(demo->connection);
2662 demo->screen = iter.data;
2666 static void demo_init(struct demo *demo, int argc, char **argv) {
2671 memset(demo, 0, sizeof(*demo));
2672 demo->frameCount = INT32_MAX;
2676 demo->use_staging_buffer = true;
2680 demo->use_break = true;
2684 demo->validate = true;
2687 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2688 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2689 demo->frameCount >= 0) {
2701 demo_init_connection(demo);
2702 demo_init_vk(demo);
2704 demo->width = 500;
2705 demo->height = 500;
2707 demo->spin_angle = 0.01f;
2708 demo
2709 demo->pause = false;
2711 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2713 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2714 mat4x4_identity(demo->model_matrix);
2759 demo_init(&demo, argc, argv);
2771 demo.connection = hInstance;
2772 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
2773 demo_create_window(&demo);
2774 demo_init_vk_swapchain(&demo);
2776 demo_prepare(&demo);
2791 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
2794 demo_cleanup(&demo);
2800 struct demo demo;
2802 demo_init(&demo, argc, argv);
2803 demo_create_window(&demo);
2804 demo_init_vk_swapchain(&demo);
2806 demo_prepare(&demo);
2807 demo_run(&demo);
2809 demo_cleanup(&demo);