• Home
  • Raw
  • Download

Lines Matching refs:demo

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