Changeset de32fda in opengl-game
- Timestamp:
- Jul 24, 2019, 4:34:02 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- c7fb883
- Parents:
- cae7a2c
- git-author:
- Dmitry Portnoy <dmp1488@…> (07/24/19 04:28:46)
- git-committer:
- Dmitry Portnoy <dmp1488@…> (07/24/19 04:34:02)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
shaders/shader.vert
rcae7a2c rde32fda 1 1 #version 450 2 #extension GL_ARB_separate_shader_objects : enable 3 4 layout (binding = 0) uniform UniformBufferObject { 5 mat4 model; 6 mat4 view; 7 mat4 proj; 8 } ubo; 2 9 3 10 layout(location = 0) in vec2 inPosition; … … 7 14 8 15 void main() { 9 gl_Position = vec4(inPosition, 0.0, 1.0);16 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); 10 17 fragColor = inColor; 11 18 } -
vulkan-game.cpp
rcae7a2c rde32fda 2 2 DESIGN GUIDE 3 3 4 I should store multiple buffers (e.g. vertex and index buffers) in the same VkBuffer and use offsets into it 4 -I should store multiple buffers (e.g. vertex and index buffers) in the same VkBuffer and use offsets into it 5 -For specifying a separate transform for each model, I can specify a descriptorCount > ` in the ubo layout binding 6 -Name class instance variables that are pointers (and possibly other pointer variables as well) like pVarName 5 7 */ 6 8 … … 12 14 13 15 #define GLM_FORCE_RADIANS 14 #define GLM_FORCE_DEPTH_ZERO_TO_ONE15 16 #include <glm/glm.hpp> 17 #include <glm/gtc/matrix_transform.hpp> 16 18 17 19 #include <iostream> … … 22 24 #include <set> 23 25 #include <optional> 26 #include <chrono> 24 27 25 28 using namespace std; … … 88 91 return attributeDescriptions; 89 92 } 93 }; 94 95 struct UniformBufferObject { 96 glm::mat4 model; 97 glm::mat4 view; 98 glm::mat4 proj; 90 99 }; 91 100 … … 158 167 vector<VkImageView> swapChainImageViews; 159 168 VkRenderPass renderPass; 169 VkDescriptorSetLayout descriptorSetLayout; 160 170 VkPipelineLayout pipelineLayout; 161 171 VkPipeline graphicsPipeline; … … 164 174 VkBuffer vertexBuffer; 165 175 VkDeviceMemory vertexBufferMemory; 176 166 177 VkBuffer indexBuffer; 167 178 VkDeviceMemory indexBufferMemory; 179 180 vector<VkBuffer> uniformBuffers; 181 vector<VkDeviceMemory> uniformBuffersMemory; 168 182 169 183 vector<VkFramebuffer> swapChainFramebuffers; … … 203 217 createImageViews(); 204 218 createRenderPass(); 219 createDescriptorSetLayout(); 205 220 createGraphicsPipeline(); 206 221 createFramebuffers(); … … 208 223 createVertexBuffer(); 209 224 createIndexBuffer(); 225 createUniformBuffers(); 210 226 createCommandBuffers(); 211 227 createSyncObjects(); … … 231 247 createGraphicsPipeline(); 232 248 createFramebuffers(); 249 createUniformBuffers(); 233 250 createCommandBuffers(); 234 251 } … … 250 267 251 268 vkDestroySwapchainKHR(device, swapChain, nullptr); 269 270 for (size_t i = 0; i < swapChainImages.size(); i++) { 271 vkDestroyBuffer(device, uniformBuffers[i], nullptr); 272 vkFreeMemory(device, uniformBuffersMemory[i], nullptr); 273 } 252 274 } 253 275 … … 699 721 } 700 722 723 void createDescriptorSetLayout() { 724 VkDescriptorSetLayoutBinding uboLayoutBinding = {}; 725 uboLayoutBinding.binding = 0; 726 uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 727 uboLayoutBinding.descriptorCount = 1; 728 uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; 729 uboLayoutBinding.pImmutableSamplers = nullptr; 730 731 VkDescriptorSetLayoutCreateInfo layoutInfo = {}; 732 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; 733 layoutInfo.bindingCount = 1; 734 layoutInfo.pBindings = &uboLayoutBinding; 735 736 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) { 737 throw runtime_error("failed to create descriptor set layout!"); 738 } 739 } 740 701 741 void createGraphicsPipeline() { 702 742 auto vertShaderCode = readFile("shaders/vert.spv"); … … 787 827 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; 788 828 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; 789 pipelineLayoutInfo.setLayoutCount = 0; 829 pipelineLayoutInfo.setLayoutCount = 1; 830 pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout; 790 831 pipelineLayoutInfo.pushConstantRangeCount = 0; 791 832 … … 914 955 vkDestroyBuffer(device, stagingBuffer, nullptr); 915 956 vkFreeMemory(device, stagingBufferMemory, nullptr); 957 } 958 959 void createUniformBuffers() { 960 VkDeviceSize bufferSize = sizeof(UniformBufferObject); 961 962 uniformBuffers.resize(swapChainImages.size()); 963 uniformBuffersMemory.resize(swapChainImages.size()); 964 965 for (size_t i = 0; i < swapChainImages.size(); i++) { 966 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 967 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 968 uniformBuffers[i], uniformBuffersMemory[i]); 969 } 916 970 } 917 971 … … 1113 1167 } 1114 1168 1169 updateUniformBuffer(imageIndex); 1170 1115 1171 VkSubmitInfo submitInfo = {}; 1116 1172 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; … … 1160 1216 } 1161 1217 1218 void updateUniformBuffer(uint32_t currentImage) { 1219 static auto startTime = chrono::high_resolution_clock::now(); 1220 1221 auto currentTime = chrono::high_resolution_clock::now(); 1222 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count(); 1223 1224 UniformBufferObject ubo = {}; 1225 ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); 1226 ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); 1227 ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float) swapChainExtent.height, 0.1f, 10.0f); 1228 ubo.proj[1][1] *= -1; 1229 1230 void* data; 1231 vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data); 1232 memcpy(data, &ubo, sizeof(ubo)); 1233 vkUnmapMemory(device, uniformBuffersMemory[currentImage]); 1234 } 1235 1162 1236 void cleanup() { 1163 1237 cleanupSwapChain(); 1238 1239 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); 1164 1240 1165 1241 vkDestroyBuffer(device, indexBuffer, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.