Advertisement
cheako

Triangle to UBO Test.

Feb 25th, 2018
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.50 KB | None | 0 0
  1. https://github.com/cheako/cheako-vulkan/tree/027b6c19d945660873a7cd893d198f8413ddc2ef
  2. cheako@debian:~/src/github/cheako-vulkan$ !di
  3. diff -u vulkan_tri.c vulkan_test.c
  4. --- vulkan_tri.c    2018-02-25 23:09:54.178026895 -0600
  5. +++ vulkan_test.c   2018-02-25 23:21:34.791710905 -0600
  6. @@ -6,7 +6,7 @@
  7.  
  8.  #include <assert.h>
  9.  
  10. -#include "tri_data.h"
  11. +#include "test_data.h"
  12.  
  13.  VkExtent2D extent =
  14.    { .width = 800, .height = 600 };
  15. @@ -45,6 +45,65 @@
  16.  VkPipeline graphics_pipeline = VK_NULL_HANDLE;
  17.  VkBuffer vertex_buffer = VK_NULL_HANDLE;
  18.  VkDeviceMemory vertex_buffer_memmory = VK_NULL_HANDLE;
  19. +VkBuffer uniform_buffer = VK_NULL_HANDLE;
  20. +VkDeviceMemory uniform_buffer_memmory = VK_NULL_HANDLE;
  21. +VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
  22. +VkDescriptorSetLayout descriptor_set_layout = VK_NULL_HANDLE;
  23. +
  24. +// lightPos = vec4(0.0f, -250.0f, 250.0f, 1.0)
  25. +
  26. +/*******************
  27. + * World variables *
  28. + *******************/
  29. +uboVS_t *uboVS;
  30. +mat4 projection;
  31. +struct
  32. +{
  33. +  bool forward;
  34. +  bool backward;
  35. +  bool turn_l;
  36. +  bool turn_r;
  37. +} keys_state =
  38. +  { false, false, false, false };
  39. +mat4 view = GLM_MAT4_IDENTITY_INIT;
  40. +
  41. +void
  42. +key_callback (GLFWwindow* window, int key, int scancode, int action, int mods)
  43. +{
  44. +  bool *move;
  45. +  /* Tech tip: Don't try and use both sets of keys. */
  46. +  switch (key)
  47. +    {
  48. +    case GLFW_KEY_W:
  49. +    case GLFW_KEY_UP:
  50. +      move = &keys_state.forward;
  51. +      break;
  52. +    case GLFW_KEY_S:
  53. +    case GLFW_KEY_DOWN:
  54. +      move = &keys_state.backward;
  55. +      break;
  56. +    case GLFW_KEY_A:
  57. +    case GLFW_KEY_LEFT:
  58. +      move = &keys_state.turn_l;
  59. +      break;
  60. +    case GLFW_KEY_D:
  61. +    case GLFW_KEY_RIGHT:
  62. +      move = &keys_state.turn_r;
  63. +      break;
  64. +    default:
  65. +      return;
  66. +    }
  67. +
  68. +  switch (action)
  69. +    {
  70. +    case GLFW_PRESS:
  71. +      *move = true;
  72. +      break;
  73. +    case GLFW_RELEASE:
  74. +      *move = false;
  75. +      break;
  76. +    }
  77. +}
  78.  
  79.  int
  80.  main (int argc, char *argv[])
  81. @@ -71,6 +130,8 @@
  82.        window = glfwCreateWindow (extent.width, extent.height,
  83.                  application_info.pApplicationName, NULL, NULL);
  84.  
  85. +      glfwSetKeyCallback (window, key_callback);
  86. +
  87.        VkInstanceCreateInfo instance_create_info;
  88.        static const VkInstanceCreateInfo EmptyVkInstanceCreateInfo;
  89.        instance_create_info = EmptyVkInstanceCreateInfo;
  90. @@ -593,6 +654,153 @@
  91.        vkUnmapMemory (device, vertex_buffer_memmory);
  92.      }
  93.  
  94. +  /*************************
  95. +   * Create uniform buffer *
  96. +   *************************/
  97. +    {
  98. +      VkBufferCreateInfo buffer_info_create;
  99. +      static const VkBufferCreateInfo EmptyVkBufferCreateInfo;
  100. +      buffer_info_create = EmptyVkBufferCreateInfo;
  101. +      buffer_info_create.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  102. +      buffer_info_create.size = sizeof(uboVS_t);
  103. +      buffer_info_create.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
  104. +      buffer_info_create.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  105. +
  106. +      err = vkCreateBuffer (device, &buffer_info_create,
  107. +      NULL,
  108. +               &uniform_buffer);
  109. +      assert((err == VK_SUCCESS) && "vkCreateBuffer: Failed vertex buffer.");
  110. +    }
  111. +
  112. +    {
  113. +      VkMemoryRequirements mememory_requirements;
  114. +      vkGetBufferMemoryRequirements (device, uniform_buffer,
  115. +                    &mememory_requirements);
  116. +
  117. +      VkMemoryAllocateInfo allocate_info;
  118. +      static const VkMemoryAllocateInfo EmptyVkMemoryAllocateInfo;
  119. +      allocate_info = EmptyVkMemoryAllocateInfo;
  120. +      allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  121. +      allocate_info.allocationSize = mememory_requirements.size;
  122. +
  123. +      allocate_info.memoryTypeIndex = UINT32_MAX;
  124. +      for (uint32_t i = 0; i < gpu_memory_properties.memoryTypeCount; ++i)
  125. +   {
  126. +     if ((mememory_requirements.memoryTypeBits & (1 << i))
  127. +         && ((gpu_memory_properties.memoryTypes[i].propertyFlags
  128. +         & (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
  129. +             | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
  130. +         == (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
  131. +             | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)))
  132. +       {
  133. +         allocate_info.memoryTypeIndex = i;
  134. +         break;
  135. +       }
  136. +   }
  137. +      assert(
  138. +     ( allocate_info.memoryTypeIndex != UINT32_MAX) && "Failed to find suitable memory type.");
  139. +
  140. +      err = vkAllocateMemory (device, &allocate_info, NULL,
  141. +                 &uniform_buffer_memmory);
  142. +      assert((err == VK_SUCCESS) && "vkAllocateMemory: Failed vertex buffer.");
  143. +
  144. +      err = vkBindBufferMemory (device, uniform_buffer, uniform_buffer_memmory,
  145. +               0);
  146. +      assert(
  147. +     (err == VK_SUCCESS) && "vkBindBufferMemory: Failed vertex buffer.");
  148. +
  149. +      err = vkMapMemory (device, uniform_buffer_memmory, 0, data_model_size, 0,
  150. +            (void**) &uboVS);
  151. +      assert((err == VK_SUCCESS) && "vkMapMemory: Failed vertex buffer.");
  152. +    }
  153. +
  154. +    {
  155. +      VkDescriptorSetLayoutBinding set_layout_binding;
  156. +      static const VkDescriptorSetLayoutBinding EmptyVkDescriptorSetLayoutBinding;
  157. +      set_layout_binding = EmptyVkDescriptorSetLayoutBinding;
  158. +      set_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  159. +      set_layout_binding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  160. +      set_layout_binding.binding = 0;
  161. +      set_layout_binding.descriptorCount = 1;
  162. +
  163. +      VkDescriptorSetLayoutCreateInfo set_layout_create_info;
  164. +      static const VkDescriptorSetLayoutCreateInfo EmptyVkDescriptorSetLayoutCreateInfo;
  165. +      set_layout_create_info = EmptyVkDescriptorSetLayoutCreateInfo;
  166. +      set_layout_create_info.sType =
  167. +     VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  168. +      set_layout_create_info.pBindings = &set_layout_binding;
  169. +      set_layout_create_info.bindingCount = 1;
  170. +
  171. +      err = vkCreateDescriptorSetLayout (device, &set_layout_create_info, NULL,
  172. +                    &descriptor_set_layout);
  173. +      assert((err == VK_SUCCESS) && "vkCreateDescriptorSetLayout: Failed.");
  174. +    }
  175. +
  176. +    {
  177. +      VkDescriptorPool descriptor_pool;
  178. +
  179. +      VkDescriptorPoolSize pool_size;
  180. +      static const VkDescriptorPoolSize EmptyVkDescriptorPoolSize;
  181. +      pool_size = EmptyVkDescriptorPoolSize;
  182. +      pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  183. +      pool_size.descriptorCount = 1;
  184. +
  185. +      VkDescriptorPoolCreateInfo pool_info;
  186. +      static const VkDescriptorPoolCreateInfo EmptyVkDescriptorPoolCreateInfo;
  187. +      pool_info = EmptyVkDescriptorPoolCreateInfo;
  188. +      pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  189. +      pool_info.poolSizeCount = 1;
  190. +      pool_info.pPoolSizes = &pool_size;
  191. +      pool_info.maxSets = 1;
  192. +
  193. +      err = vkCreateDescriptorPool (device, &pool_info,
  194. +      NULL,
  195. +                   &descriptor_pool);
  196. +      assert((err == VK_SUCCESS) && "vkCreateDescriptorPool: Failed.");
  197. +
  198. +      VkDescriptorSetAllocateInfo alloc_info;
  199. +      static const VkDescriptorSetAllocateInfo EmptyVkDescriptorSetAllocateInfo;
  200. +      alloc_info = EmptyVkDescriptorSetAllocateInfo;
  201. +      alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  202. +      alloc_info.descriptorPool = descriptor_pool;
  203. +      alloc_info.pSetLayouts = &descriptor_set_layout;
  204. +      alloc_info.descriptorSetCount = 1;
  205. +
  206. +      err = vkAllocateDescriptorSets (device, &alloc_info, &descriptor_set);
  207. +      assert((err == VK_SUCCESS) && "vkAllocateDescriptorSets: Failed.");
  208. +
  209. +      VkDescriptorBufferInfo uniform_descriptor;
  210. +      static const VkDescriptorBufferInfo EmptyVkDescriptorBufferInfo;
  211. +      uniform_descriptor = EmptyVkDescriptorBufferInfo;
  212. +      uniform_descriptor.buffer = uniform_buffer;
  213. +      uniform_descriptor.offset = 0;
  214. +      uniform_descriptor.range = sizeof(uboVS_t);
  215. +
  216. +      VkWriteDescriptorSet write_descriptor_set;
  217. +      static const VkWriteDescriptorSet EmptyVkWriteDescriptorSet;
  218. +      write_descriptor_set = EmptyVkWriteDescriptorSet;
  219. +      write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  220. +      write_descriptor_set.dstSet = descriptor_set;
  221. +      write_descriptor_set.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  222. +      write_descriptor_set.dstBinding = 0;
  223. +      write_descriptor_set.pBufferInfo = &uniform_descriptor;
  224. +      write_descriptor_set.descriptorCount = 1;
  225. +
  226. +      vkUpdateDescriptorSets (device, 1, &write_descriptor_set, 0, NULL);
  227. +    }
  228. +
  229. +  /***************
  230. +   * Setup world *
  231. +   ***************/
  232. +    {
  233. +      static const uboVS_t empty_uboVS_t;
  234. +      *uboVS = empty_uboVS_t;
  235. +
  236. +      glm_perspective_default ((float) extent.width / (float) extent.height,
  237. +                  projection);
  238. +      glm_translate_z (view, -1.0f);
  239. +    }
  240. +
  241.    /*********************
  242.     * Graphics pipeline *
  243.     *********************/
  244. @@ -744,6 +952,8 @@
  245.       pipeline_layout_create_info = EmptyVkPipelineLayoutCreateInfo;
  246.       pipeline_layout_create_info.sType =
  247.           VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  248. +     pipeline_layout_create_info.setLayoutCount = 1;
  249. +     pipeline_layout_create_info.pSetLayouts = &descriptor_set_layout;
  250.  
  251.       err = vkCreatePipelineLayout (device, &pipeline_layout_create_info,
  252.       NULL,
  253. @@ -865,10 +1075,56 @@
  254.       /* Needed for clean up */
  255.       VkQueue queue;
  256.       vkGetDeviceQueue (device, queue_family_index, VK_NULL_HANDLE, &queue);
  257. +     /* World variable */
  258. +     double previous_frame = glfwGetTime ();
  259. +     /*
  260. +      * This code uses a float point frame counter.  To make your code easier
  261. +      * u may want to resolve this to an integer.  One frame is the set of all
  262. +      * player's turns.
  263. +      */
  264. +
  265. +     /* Recalculate if projection or view changes. */
  266. +     mat4 vp_cache;
  267. +     glm_mat4_mul (projection, view, vp_cache);
  268. +
  269.       while (!glfwWindowShouldClose (window))
  270.         {
  271.           glfwPollEvents ();
  272.  
  273. +       {
  274. +         double this_frame = glfwGetTime ();
  275. +         if (keys_state.forward ^ keys_state.backward)
  276. +           {
  277. +             mat4 new;
  278. +             glm_translate_z (
  279. +             view,
  280. +             keys_state.forward ?
  281. +                 this_frame - previous_frame :
  282. +                 previous_frame - this_frame);
  283. +             glm_mat4_copy (new, view);
  284. +             glm_mat4_mul (projection, view, vp_cache);
  285. +           }
  286. +
  287. +         if (keys_state.turn_l ^ keys_state.turn_r)
  288. +           {
  289. +             mat4 new;
  290. +             glm_rotate_y (
  291. +             view,
  292. +             keys_state.turn_l ?
  293. +                 this_frame - previous_frame :
  294. +                 previous_frame - this_frame,
  295. +             new);
  296. +             glm_mat4_copy (new, view);
  297. +             glm_mat4_mul (projection, view, vp_cache);
  298. +           }
  299. +
  300. +         /* TODO: Take turns. */
  301. +
  302. +         glm_mat4_mul (vp_cache, data_model_location, uboVS->mvp);
  303. +
  304. +         previous_frame = this_frame;
  305. +       }
  306. +
  307.           /* Get next swapchain image */
  308.           /* Needed in record commands and presenting */
  309.           uint32_t active_swapchain_image_id;
  310. @@ -949,6 +1205,12 @@
  311.               vkCmdSetViewport (command_buffer, 0, 1, &viewport);
  312.             }
  313.  
  314. +         vkCmdBindDescriptorSets (command_buffer,
  315. +                      VK_PIPELINE_BIND_POINT_GRAPHICS,
  316. +                      pipeline_layout, 0, 1,
  317. +                      &descriptor_set, 0,
  318. +                      NULL);
  319. +
  320.           vkCmdBindPipeline (command_buffer,
  321.                      VK_PIPELINE_BIND_POINT_GRAPHICS,
  322.                      graphics_pipeline);
  323. @@ -1029,6 +1291,14 @@
  324.    /************
  325.     * Clean up *
  326.     ************/
  327. +  vkUnmapMemory (device, uniform_buffer_memmory);
  328. +  uboVS = NULL;
  329. +  vkDestroyDescriptorSetLayout (device, descriptor_set_layout, NULL);
  330. +  descriptor_set_layout = VK_NULL_HANDLE;
  331. +  vkFreeMemory (device, uniform_buffer_memmory, NULL);
  332. +  uniform_buffer_memmory = VK_NULL_HANDLE;
  333. +  vkDestroyBuffer (device, uniform_buffer, NULL);
  334. +  vertex_buffer = VK_NULL_HANDLE;
  335.    vkFreeMemory (device, vertex_buffer_memmory, NULL);
  336.    vertex_buffer_memmory = VK_NULL_HANDLE;
  337.    vkDestroyBuffer (device, vertex_buffer, NULL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement