Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://github.com/cheako/cheako-vulkan/tree/027b6c19d945660873a7cd893d198f8413ddc2ef
- cheako@debian:~/src/github/cheako-vulkan$ !di
- diff -u vulkan_tri.c vulkan_test.c
- --- vulkan_tri.c 2018-02-25 23:09:54.178026895 -0600
- +++ vulkan_test.c 2018-02-25 23:21:34.791710905 -0600
- @@ -6,7 +6,7 @@
- #include <assert.h>
- -#include "tri_data.h"
- +#include "test_data.h"
- VkExtent2D extent =
- { .width = 800, .height = 600 };
- @@ -45,6 +45,65 @@
- VkPipeline graphics_pipeline = VK_NULL_HANDLE;
- VkBuffer vertex_buffer = VK_NULL_HANDLE;
- VkDeviceMemory vertex_buffer_memmory = VK_NULL_HANDLE;
- +VkBuffer uniform_buffer = VK_NULL_HANDLE;
- +VkDeviceMemory uniform_buffer_memmory = VK_NULL_HANDLE;
- +VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
- +VkDescriptorSetLayout descriptor_set_layout = VK_NULL_HANDLE;
- +
- +// lightPos = vec4(0.0f, -250.0f, 250.0f, 1.0)
- +
- +/*******************
- + * World variables *
- + *******************/
- +uboVS_t *uboVS;
- +mat4 projection;
- +struct
- +{
- + bool forward;
- + bool backward;
- + bool turn_l;
- + bool turn_r;
- +} keys_state =
- + { false, false, false, false };
- +mat4 view = GLM_MAT4_IDENTITY_INIT;
- +
- +void
- +key_callback (GLFWwindow* window, int key, int scancode, int action, int mods)
- +{
- + bool *move;
- + /* Tech tip: Don't try and use both sets of keys. */
- + switch (key)
- + {
- + case GLFW_KEY_W:
- + case GLFW_KEY_UP:
- + move = &keys_state.forward;
- + break;
- + case GLFW_KEY_S:
- + case GLFW_KEY_DOWN:
- + move = &keys_state.backward;
- + break;
- + case GLFW_KEY_A:
- + case GLFW_KEY_LEFT:
- + move = &keys_state.turn_l;
- + break;
- + case GLFW_KEY_D:
- + case GLFW_KEY_RIGHT:
- + move = &keys_state.turn_r;
- + break;
- + default:
- + return;
- + }
- +
- + switch (action)
- + {
- + case GLFW_PRESS:
- + *move = true;
- + break;
- + case GLFW_RELEASE:
- + *move = false;
- + break;
- + }
- +}
- int
- main (int argc, char *argv[])
- @@ -71,6 +130,8 @@
- window = glfwCreateWindow (extent.width, extent.height,
- application_info.pApplicationName, NULL, NULL);
- + glfwSetKeyCallback (window, key_callback);
- +
- VkInstanceCreateInfo instance_create_info;
- static const VkInstanceCreateInfo EmptyVkInstanceCreateInfo;
- instance_create_info = EmptyVkInstanceCreateInfo;
- @@ -593,6 +654,153 @@
- vkUnmapMemory (device, vertex_buffer_memmory);
- }
- + /*************************
- + * Create uniform buffer *
- + *************************/
- + {
- + VkBufferCreateInfo buffer_info_create;
- + static const VkBufferCreateInfo EmptyVkBufferCreateInfo;
- + buffer_info_create = EmptyVkBufferCreateInfo;
- + buffer_info_create.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
- + buffer_info_create.size = sizeof(uboVS_t);
- + buffer_info_create.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
- + buffer_info_create.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
- +
- + err = vkCreateBuffer (device, &buffer_info_create,
- + NULL,
- + &uniform_buffer);
- + assert((err == VK_SUCCESS) && "vkCreateBuffer: Failed vertex buffer.");
- + }
- +
- + {
- + VkMemoryRequirements mememory_requirements;
- + vkGetBufferMemoryRequirements (device, uniform_buffer,
- + &mememory_requirements);
- +
- + VkMemoryAllocateInfo allocate_info;
- + static const VkMemoryAllocateInfo EmptyVkMemoryAllocateInfo;
- + allocate_info = EmptyVkMemoryAllocateInfo;
- + allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
- + allocate_info.allocationSize = mememory_requirements.size;
- +
- + allocate_info.memoryTypeIndex = UINT32_MAX;
- + for (uint32_t i = 0; i < gpu_memory_properties.memoryTypeCount; ++i)
- + {
- + if ((mememory_requirements.memoryTypeBits & (1 << i))
- + && ((gpu_memory_properties.memoryTypes[i].propertyFlags
- + & (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
- + | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
- + == (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
- + | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)))
- + {
- + allocate_info.memoryTypeIndex = i;
- + break;
- + }
- + }
- + assert(
- + ( allocate_info.memoryTypeIndex != UINT32_MAX) && "Failed to find suitable memory type.");
- +
- + err = vkAllocateMemory (device, &allocate_info, NULL,
- + &uniform_buffer_memmory);
- + assert((err == VK_SUCCESS) && "vkAllocateMemory: Failed vertex buffer.");
- +
- + err = vkBindBufferMemory (device, uniform_buffer, uniform_buffer_memmory,
- + 0);
- + assert(
- + (err == VK_SUCCESS) && "vkBindBufferMemory: Failed vertex buffer.");
- +
- + err = vkMapMemory (device, uniform_buffer_memmory, 0, data_model_size, 0,
- + (void**) &uboVS);
- + assert((err == VK_SUCCESS) && "vkMapMemory: Failed vertex buffer.");
- + }
- +
- + {
- + VkDescriptorSetLayoutBinding set_layout_binding;
- + static const VkDescriptorSetLayoutBinding EmptyVkDescriptorSetLayoutBinding;
- + set_layout_binding = EmptyVkDescriptorSetLayoutBinding;
- + set_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
- + set_layout_binding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
- + set_layout_binding.binding = 0;
- + set_layout_binding.descriptorCount = 1;
- +
- + VkDescriptorSetLayoutCreateInfo set_layout_create_info;
- + static const VkDescriptorSetLayoutCreateInfo EmptyVkDescriptorSetLayoutCreateInfo;
- + set_layout_create_info = EmptyVkDescriptorSetLayoutCreateInfo;
- + set_layout_create_info.sType =
- + VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
- + set_layout_create_info.pBindings = &set_layout_binding;
- + set_layout_create_info.bindingCount = 1;
- +
- + err = vkCreateDescriptorSetLayout (device, &set_layout_create_info, NULL,
- + &descriptor_set_layout);
- + assert((err == VK_SUCCESS) && "vkCreateDescriptorSetLayout: Failed.");
- + }
- +
- + {
- + VkDescriptorPool descriptor_pool;
- +
- + VkDescriptorPoolSize pool_size;
- + static const VkDescriptorPoolSize EmptyVkDescriptorPoolSize;
- + pool_size = EmptyVkDescriptorPoolSize;
- + pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
- + pool_size.descriptorCount = 1;
- +
- + VkDescriptorPoolCreateInfo pool_info;
- + static const VkDescriptorPoolCreateInfo EmptyVkDescriptorPoolCreateInfo;
- + pool_info = EmptyVkDescriptorPoolCreateInfo;
- + pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
- + pool_info.poolSizeCount = 1;
- + pool_info.pPoolSizes = &pool_size;
- + pool_info.maxSets = 1;
- +
- + err = vkCreateDescriptorPool (device, &pool_info,
- + NULL,
- + &descriptor_pool);
- + assert((err == VK_SUCCESS) && "vkCreateDescriptorPool: Failed.");
- +
- + VkDescriptorSetAllocateInfo alloc_info;
- + static const VkDescriptorSetAllocateInfo EmptyVkDescriptorSetAllocateInfo;
- + alloc_info = EmptyVkDescriptorSetAllocateInfo;
- + alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
- + alloc_info.descriptorPool = descriptor_pool;
- + alloc_info.pSetLayouts = &descriptor_set_layout;
- + alloc_info.descriptorSetCount = 1;
- +
- + err = vkAllocateDescriptorSets (device, &alloc_info, &descriptor_set);
- + assert((err == VK_SUCCESS) && "vkAllocateDescriptorSets: Failed.");
- +
- + VkDescriptorBufferInfo uniform_descriptor;
- + static const VkDescriptorBufferInfo EmptyVkDescriptorBufferInfo;
- + uniform_descriptor = EmptyVkDescriptorBufferInfo;
- + uniform_descriptor.buffer = uniform_buffer;
- + uniform_descriptor.offset = 0;
- + uniform_descriptor.range = sizeof(uboVS_t);
- +
- + VkWriteDescriptorSet write_descriptor_set;
- + static const VkWriteDescriptorSet EmptyVkWriteDescriptorSet;
- + write_descriptor_set = EmptyVkWriteDescriptorSet;
- + write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
- + write_descriptor_set.dstSet = descriptor_set;
- + write_descriptor_set.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
- + write_descriptor_set.dstBinding = 0;
- + write_descriptor_set.pBufferInfo = &uniform_descriptor;
- + write_descriptor_set.descriptorCount = 1;
- +
- + vkUpdateDescriptorSets (device, 1, &write_descriptor_set, 0, NULL);
- + }
- +
- + /***************
- + * Setup world *
- + ***************/
- + {
- + static const uboVS_t empty_uboVS_t;
- + *uboVS = empty_uboVS_t;
- +
- + glm_perspective_default ((float) extent.width / (float) extent.height,
- + projection);
- + glm_translate_z (view, -1.0f);
- + }
- +
- /*********************
- * Graphics pipeline *
- *********************/
- @@ -744,6 +952,8 @@
- pipeline_layout_create_info = EmptyVkPipelineLayoutCreateInfo;
- pipeline_layout_create_info.sType =
- VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
- + pipeline_layout_create_info.setLayoutCount = 1;
- + pipeline_layout_create_info.pSetLayouts = &descriptor_set_layout;
- err = vkCreatePipelineLayout (device, &pipeline_layout_create_info,
- NULL,
- @@ -865,10 +1075,56 @@
- /* Needed for clean up */
- VkQueue queue;
- vkGetDeviceQueue (device, queue_family_index, VK_NULL_HANDLE, &queue);
- + /* World variable */
- + double previous_frame = glfwGetTime ();
- + /*
- + * This code uses a float point frame counter. To make your code easier
- + * u may want to resolve this to an integer. One frame is the set of all
- + * player's turns.
- + */
- +
- + /* Recalculate if projection or view changes. */
- + mat4 vp_cache;
- + glm_mat4_mul (projection, view, vp_cache);
- +
- while (!glfwWindowShouldClose (window))
- {
- glfwPollEvents ();
- + {
- + double this_frame = glfwGetTime ();
- + if (keys_state.forward ^ keys_state.backward)
- + {
- + mat4 new;
- + glm_translate_z (
- + view,
- + keys_state.forward ?
- + this_frame - previous_frame :
- + previous_frame - this_frame);
- + glm_mat4_copy (new, view);
- + glm_mat4_mul (projection, view, vp_cache);
- + }
- +
- + if (keys_state.turn_l ^ keys_state.turn_r)
- + {
- + mat4 new;
- + glm_rotate_y (
- + view,
- + keys_state.turn_l ?
- + this_frame - previous_frame :
- + previous_frame - this_frame,
- + new);
- + glm_mat4_copy (new, view);
- + glm_mat4_mul (projection, view, vp_cache);
- + }
- +
- + /* TODO: Take turns. */
- +
- + glm_mat4_mul (vp_cache, data_model_location, uboVS->mvp);
- +
- + previous_frame = this_frame;
- + }
- +
- /* Get next swapchain image */
- /* Needed in record commands and presenting */
- uint32_t active_swapchain_image_id;
- @@ -949,6 +1205,12 @@
- vkCmdSetViewport (command_buffer, 0, 1, &viewport);
- }
- + vkCmdBindDescriptorSets (command_buffer,
- + VK_PIPELINE_BIND_POINT_GRAPHICS,
- + pipeline_layout, 0, 1,
- + &descriptor_set, 0,
- + NULL);
- +
- vkCmdBindPipeline (command_buffer,
- VK_PIPELINE_BIND_POINT_GRAPHICS,
- graphics_pipeline);
- @@ -1029,6 +1291,14 @@
- /************
- * Clean up *
- ************/
- + vkUnmapMemory (device, uniform_buffer_memmory);
- + uboVS = NULL;
- + vkDestroyDescriptorSetLayout (device, descriptor_set_layout, NULL);
- + descriptor_set_layout = VK_NULL_HANDLE;
- + vkFreeMemory (device, uniform_buffer_memmory, NULL);
- + uniform_buffer_memmory = VK_NULL_HANDLE;
- + vkDestroyBuffer (device, uniform_buffer, NULL);
- + vertex_buffer = VK_NULL_HANDLE;
- vkFreeMemory (device, vertex_buffer_memmory, NULL);
- vertex_buffer_memmory = VK_NULL_HANDLE;
- vkDestroyBuffer (device, vertex_buffer, NULL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement