example_vulkan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // https://github.com/danilw/nanovg_vulkan
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #define GLFW_INCLUDE_VULKAN
  7. #include <GLFW/glfw3.h>
  8. #include <vulkan/vulkan.h>
  9. #ifndef DEMO_ANTIALIAS
  10. # define DEMO_ANTIALIAS 1
  11. #endif
  12. #ifndef DEMO_STENCIL_STROKES
  13. # define DEMO_STENCIL_STROKES 1
  14. #endif
  15. #ifndef DEMO_VULKAN_VALIDATON_LAYER
  16. # define DEMO_VULKAN_VALIDATON_LAYER 0
  17. #endif
  18. #include "nanovg.h"
  19. #include "nanovg_vk.h"
  20. #include "demo.h"
  21. #include "perf.h"
  22. #include "vulkan_util.h"
  23. void errorcb(int error, const char *desc) {
  24. printf("GLFW error %d: %s\n", error, desc);
  25. }
  26. int blowup = 0;
  27. int screenshot = 0;
  28. int premult = 0;
  29. bool resize_event = false;
  30. static void key(GLFWwindow *window, int key, int scancode, int action, int mods) {
  31. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  32. glfwSetWindowShouldClose(window, true);
  33. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  34. blowup = !blowup;
  35. if (key == GLFW_KEY_S && action == GLFW_PRESS)
  36. screenshot = 1;
  37. if (key == GLFW_KEY_P && action == GLFW_PRESS)
  38. premult = !premult;
  39. }
  40. void prepareFrame(VkDevice device, VkCommandBuffer cmd_buffer, FrameBuffers *fb) {
  41. VkResult res;
  42. // Get the index of the next available swapchain image:
  43. res = vkAcquireNextImageKHR(device, fb->swap_chain, UINT64_MAX,
  44. fb->present_complete_semaphore[fb->current_frame],
  45. VK_NULL_HANDLE,
  46. &fb->current_buffer);
  47. if (res == VK_ERROR_OUT_OF_DATE_KHR)
  48. {
  49. resize_event = true;
  50. res = 0;
  51. return;
  52. }
  53. assert(res == VK_SUCCESS);
  54. const VkCommandBufferBeginInfo cmd_buf_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
  55. res = vkBeginCommandBuffer(cmd_buffer, &cmd_buf_info);
  56. assert(res == VK_SUCCESS);
  57. VkClearValue clear_values[2];
  58. clear_values[0].color.float32[0] = 0.3f;
  59. clear_values[0].color.float32[1] = 0.3f;
  60. clear_values[0].color.float32[2] = 0.32f;
  61. clear_values[0].color.float32[3] = 1.0f;
  62. clear_values[1].depthStencil.depth = 1.0f;
  63. clear_values[1].depthStencil.stencil = 0;
  64. VkRenderPassBeginInfo rp_begin;
  65. rp_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  66. rp_begin.pNext = NULL;
  67. rp_begin.renderPass = fb->render_pass;
  68. rp_begin.framebuffer = fb->framebuffers[fb->current_buffer];
  69. rp_begin.renderArea.offset.x = 0;
  70. rp_begin.renderArea.offset.y = 0;
  71. rp_begin.renderArea.extent.width = fb->buffer_size.width;
  72. rp_begin.renderArea.extent.height = fb->buffer_size.height;
  73. rp_begin.clearValueCount = 2;
  74. rp_begin.pClearValues = clear_values;
  75. vkCmdBeginRenderPass(cmd_buffer, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
  76. VkViewport viewport;
  77. viewport.width = (float) fb->buffer_size.width;
  78. viewport.height = (float) fb->buffer_size.height;
  79. viewport.minDepth = (float) 0.0f;
  80. viewport.maxDepth = (float) 1.0f;
  81. viewport.x = (float) rp_begin.renderArea.offset.x;
  82. viewport.y = (float) rp_begin.renderArea.offset.y;
  83. vkCmdSetViewport(cmd_buffer, 0, 1, &viewport);
  84. VkRect2D scissor = rp_begin.renderArea;
  85. vkCmdSetScissor(cmd_buffer, 0, 1, &scissor);
  86. }
  87. void submitFrame(VkDevice device, VkQueue queue, VkCommandBuffer cmd_buffer, FrameBuffers *fb) {
  88. VkResult res;
  89. vkCmdEndRenderPass(cmd_buffer);
  90. VkImageMemoryBarrier image_barrier = {
  91. .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
  92. .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
  93. .dstAccessMask = 0,
  94. .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
  95. .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  96. .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
  97. .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
  98. .image = fb->swap_chain_buffers[fb->current_buffer].image,
  99. .subresourceRange = {
  100. .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
  101. .baseMipLevel = 0,
  102. .levelCount = 1,
  103. .baseArrayLayer = 0,
  104. .layerCount = 1,
  105. },
  106. };
  107. vkCmdPipelineBarrier(cmd_buffer,
  108. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
  109. VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
  110. 0,
  111. 0, NULL,
  112. 0, NULL,
  113. 1, &image_barrier);
  114. vkEndCommandBuffer(cmd_buffer);
  115. VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  116. VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO};
  117. submit_info.pNext = NULL;
  118. submit_info.waitSemaphoreCount = 1;
  119. submit_info.pWaitSemaphores = &fb->present_complete_semaphore[fb->current_frame];
  120. submit_info.pWaitDstStageMask = &pipe_stage_flags;
  121. submit_info.commandBufferCount = 1;
  122. submit_info.pCommandBuffers = &cmd_buffer;
  123. submit_info.signalSemaphoreCount = 1;
  124. submit_info.pSignalSemaphores = &fb->render_complete_semaphore[fb->current_frame];
  125. res = vkQueueSubmit(queue, 1, &submit_info, fb->flight_fence[fb->current_frame]);
  126. assert(res == VK_SUCCESS);
  127. /* Now present the image in the window */
  128. VkPresentInfoKHR present = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR};
  129. present.pNext = NULL;
  130. present.swapchainCount = 1;
  131. present.pSwapchains = &fb->swap_chain;
  132. present.pImageIndices = &fb->current_buffer;
  133. present.waitSemaphoreCount = 1;
  134. present.pWaitSemaphores = &fb->render_complete_semaphore[fb->current_frame];
  135. res = vkQueuePresentKHR(queue, &present);
  136. if (res == VK_ERROR_OUT_OF_DATE_KHR)
  137. {
  138. res = vkQueueWaitIdle(queue);
  139. resize_event = true;
  140. res = 0;
  141. return;
  142. }
  143. assert(res == VK_SUCCESS);
  144. fb->current_frame = (fb->current_frame + 1) % fb->swapchain_image_count;
  145. fb->num_swaps++;
  146. if (fb->num_swaps >= fb->swapchain_image_count) {
  147. res = vkWaitForFences(device, 1, &fb->flight_fence[fb->current_frame], true, UINT64_MAX);
  148. assert(res == VK_SUCCESS);
  149. res = vkResetFences(device, 1, &fb->flight_fence[fb->current_frame]);
  150. assert(res == VK_SUCCESS);
  151. }
  152. }
  153. int main() {
  154. GLFWwindow *window;
  155. if (!glfwInit()) {
  156. printf("Failed to init GLFW.");
  157. return -1;
  158. }
  159. if (!glfwVulkanSupported()) {
  160. printf("vulkan dose not supported\n");
  161. return 1;
  162. }
  163. glfwSetErrorCallback(errorcb);
  164. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  165. window = glfwCreateWindow(1000, 600, "NanoVG Example Vulkan", NULL, NULL);
  166. if (!window) {
  167. glfwTerminate();
  168. return -1;
  169. }
  170. glfwSetKeyCallback(window, key);
  171. glfwSetTime(0);
  172. bool enableValidationLayer = false;
  173. #if DEMO_VULKAN_VALIDATON_LAYER
  174. enableValidationLayer = true;
  175. #endif
  176. VkInstance instance = createVkInstance(enableValidationLayer);
  177. VkResult res;
  178. VkSurfaceKHR surface;
  179. res = glfwCreateWindowSurface(instance, window, 0, &surface);
  180. if (VK_SUCCESS != res) {
  181. printf("glfwCreateWindowSurface failed\n");
  182. exit(-1);
  183. }
  184. uint32_t gpu_count = 0;
  185. res = vkEnumeratePhysicalDevices(instance, &gpu_count, NULL);
  186. if (VK_SUCCESS != res && res != VK_INCOMPLETE) {
  187. printf("vkEnumeratePhysicalDevices failed %d \n", res);
  188. exit(-1);
  189. }
  190. if (gpu_count < 1){
  191. printf("No Vulkan device found.\n");
  192. exit(-1);
  193. }
  194. VkPhysicalDevice gpu[32];
  195. res = vkEnumeratePhysicalDevices(instance, &gpu_count, gpu);
  196. if (res != VK_SUCCESS && res != VK_INCOMPLETE) {
  197. printf("vkEnumeratePhysicalDevices failed %d \n", res);
  198. exit(-1);
  199. }
  200. uint32_t idx = 0;
  201. bool use_idx = false;
  202. bool discrete_idx = false;
  203. for (uint32_t i = 0; i < gpu_count && (!discrete_idx); i++)
  204. {
  205. uint32_t qfc = 0;
  206. vkGetPhysicalDeviceQueueFamilyProperties(gpu[i], &qfc, NULL);
  207. if (qfc < 1)continue;
  208. VkQueueFamilyProperties *queue_family_properties;
  209. queue_family_properties = malloc(qfc * sizeof(VkQueueFamilyProperties));
  210. vkGetPhysicalDeviceQueueFamilyProperties(gpu[i], &qfc, queue_family_properties);
  211. for (uint32_t j = 0; j < qfc; j++)
  212. {
  213. VkBool32 supports_present;
  214. vkGetPhysicalDeviceSurfaceSupportKHR(gpu[i], j, surface, &supports_present);
  215. if ((queue_family_properties[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) && supports_present)
  216. {
  217. VkPhysicalDeviceProperties pr;
  218. vkGetPhysicalDeviceProperties(gpu[i], &pr);
  219. idx = i;
  220. use_idx = true;
  221. if(pr.deviceType==VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU){
  222. discrete_idx = true;
  223. }
  224. break;
  225. }
  226. }
  227. free(queue_family_properties);
  228. }
  229. if (!use_idx){
  230. printf("Not found suitable queue which supports graphics.\n");
  231. exit(-1);
  232. }
  233. printf("Using GPU device %lu\n", (unsigned long) idx);
  234. VulkanDevice *device = createVulkanDevice(gpu[idx]);
  235. int winWidth, winHeight;
  236. glfwGetWindowSize(window, &winWidth, &winHeight);
  237. VkQueue queue;
  238. vkGetDeviceQueue(device->device, device->graphicsQueueFamilyIndex, 0, &queue);
  239. FrameBuffers fb = createFrameBuffers(device, surface, queue, winWidth, winHeight, 0);
  240. VkCommandBuffer *cmd_buffer = createCmdBuffer(device->device, device->commandPool, fb.swapchain_image_count);
  241. VKNVGCreateInfo create_info = {0};
  242. create_info.device = device->device;
  243. create_info.gpu = device->gpu;
  244. create_info.renderpass = fb.render_pass;
  245. create_info.cmdBuffer = cmd_buffer;
  246. create_info.swapchainImageCount = fb.swapchain_image_count;
  247. create_info.currentFrame = &fb.current_frame;
  248. int flags = 0;
  249. #ifndef NDEBUG
  250. flags |= NVG_DEBUG; // unused in nanovg_vk
  251. #endif
  252. #if DEMO_ANTIALIAS
  253. flags |= NVG_ANTIALIAS;
  254. #endif
  255. #if DEMO_STENCIL_STROKES
  256. flags |= NVG_STENCIL_STROKES;
  257. #endif
  258. NVGcontext *vg = nvgCreateVk(create_info, flags, queue);
  259. DemoData data;
  260. PerfGraph fps;//, cpuGraph, gpuGraph;
  261. if (loadDemoData(vg, &data) == -1)
  262. return -1;
  263. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  264. //initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  265. //initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  266. double prevt = glfwGetTime();
  267. while (!glfwWindowShouldClose(window)) {
  268. float pxRatio;
  269. double mx, my, t, dt;
  270. int cwinWidth, cwinHeight;
  271. glfwGetWindowSize(window, &cwinWidth, &cwinHeight);
  272. if ((resize_event)||(winWidth != cwinWidth || winHeight != cwinHeight)) {
  273. winWidth = cwinWidth;
  274. winHeight = cwinHeight;
  275. destroyFrameBuffers(device, &fb, queue);
  276. fb = createFrameBuffers(device, surface, queue, winWidth, winHeight, 0);
  277. resize_event=false;
  278. }else{
  279. prepareFrame(device->device, cmd_buffer[fb.current_frame], &fb);
  280. if(resize_event)continue;
  281. t = glfwGetTime();
  282. dt = t - prevt;
  283. prevt = t;
  284. updateGraph(&fps, (float)dt);
  285. pxRatio = (float)fb.buffer_size.width / (float)winWidth;
  286. glfwGetCursorPos(window, &mx, &my);
  287. nvgBeginFrame(vg, (float)winWidth, (float)winHeight, pxRatio);
  288. renderDemo(vg, (float)mx, (float)my, (float)winWidth, (float)winHeight, (float)t, blowup, &data);
  289. renderGraph(vg, 5, 5, &fps);
  290. nvgEndFrame(vg);
  291. submitFrame(device->device, queue, cmd_buffer[fb.current_frame], &fb);
  292. }
  293. glfwPollEvents();
  294. }
  295. res = vkQueueWaitIdle(queue);
  296. assert(res == VK_SUCCESS);
  297. freeDemoData(vg, &data);
  298. nvgDeleteVk(vg);
  299. destroyFrameBuffers(device, &fb, queue);
  300. destroyVulkanDevice(device);
  301. destroyDebugCallback(instance);
  302. vkDestroyInstance(instance, NULL);
  303. glfwDestroyWindow(window);
  304. free(cmd_buffer);
  305. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  306. //printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  307. //printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  308. glfwTerminate();
  309. return 0;
  310. }