Changeset de32fda in opengl-game


Ignore:
Timestamp:
Jul 24, 2019, 4:34:02 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
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)
Message:

Create a ubo and update it with the MVP matrix every frame

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • shaders/shader.vert

    rcae7a2c rde32fda  
    11#version 450
     2#extension GL_ARB_separate_shader_objects : enable
     3
     4layout (binding = 0) uniform UniformBufferObject {
     5   mat4 model;
     6   mat4 view;
     7   mat4 proj;
     8} ubo;
    29
    310layout(location = 0) in vec2 inPosition;
     
    714
    815void 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);
    1017   fragColor = inColor;
    1118}
  • vulkan-game.cpp

    rcae7a2c rde32fda  
    22DESIGN GUIDE
    33
    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
    57*/
    68
     
    1214
    1315#define GLM_FORCE_RADIANS
    14 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
    1516#include <glm/glm.hpp>
     17#include <glm/gtc/matrix_transform.hpp>
    1618
    1719#include <iostream>
     
    2224#include <set>
    2325#include <optional>
     26#include <chrono>
    2427
    2528using namespace std;
     
    8891      return attributeDescriptions;
    8992   }
     93};
     94
     95struct UniformBufferObject {
     96   glm::mat4 model;
     97   glm::mat4 view;
     98   glm::mat4 proj;
    9099};
    91100
     
    158167      vector<VkImageView> swapChainImageViews;
    159168      VkRenderPass renderPass;
     169      VkDescriptorSetLayout descriptorSetLayout;
    160170      VkPipelineLayout pipelineLayout;
    161171      VkPipeline graphicsPipeline;
     
    164174      VkBuffer vertexBuffer;
    165175      VkDeviceMemory vertexBufferMemory;
     176
    166177      VkBuffer indexBuffer;
    167178      VkDeviceMemory indexBufferMemory;
     179
     180      vector<VkBuffer> uniformBuffers;
     181      vector<VkDeviceMemory> uniformBuffersMemory;
    168182
    169183      vector<VkFramebuffer> swapChainFramebuffers;
     
    203217         createImageViews();
    204218         createRenderPass();
     219         createDescriptorSetLayout();
    205220         createGraphicsPipeline();
    206221         createFramebuffers();
     
    208223         createVertexBuffer();
    209224         createIndexBuffer();
     225         createUniformBuffers();
    210226         createCommandBuffers();
    211227         createSyncObjects();
     
    231247         createGraphicsPipeline();
    232248         createFramebuffers();
     249         createUniformBuffers();
    233250         createCommandBuffers();
    234251      }
     
    250267
    251268         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         }
    252274      }
    253275
     
    699721      }
    700722
     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
    701741      void createGraphicsPipeline() {
    702742         auto vertShaderCode = readFile("shaders/vert.spv");
     
    787827         VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
    788828         pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    789          pipelineLayoutInfo.setLayoutCount = 0;
     829         pipelineLayoutInfo.setLayoutCount = 1;
     830         pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
    790831         pipelineLayoutInfo.pushConstantRangeCount = 0;
    791832
     
    914955         vkDestroyBuffer(device, stagingBuffer, nullptr);
    915956         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         }
    916970      }
    917971
     
    11131167         }
    11141168
     1169         updateUniformBuffer(imageIndex);
     1170
    11151171         VkSubmitInfo submitInfo = {};
    11161172         submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
     
    11601216      }
    11611217
     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
    11621236      void cleanup() {
    11631237         cleanupSwapChain();
     1238
     1239         vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
    11641240
    11651241         vkDestroyBuffer(device, indexBuffer, nullptr);
Note: See TracChangeset for help on using the changeset viewer.