Changeset 80edd70 in opengl-game


Ignore:
Timestamp:
Jul 23, 2019, 4:02:06 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
d9ef6ab
Parents:
8667f76
Message:

Use a vertex buffer to store the points to be rendered

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • README.txt

    r8667f76 r80edd70  
    5959 - Copy the SDL2 include folder into /include and rename it SDL2
    6060 - Add the location of the lib/x64 folder to the VS2019 project properties under Linker/General/Addition Library DIrectories
     61 - You can also just copy the contents of that folder to lib
     62 - TODO: Figure out how to do static compilation with SDL2
    6163
    6264Download the vulkan sdk
  • VulkanGame.vcxproj

    r8667f76 r80edd70  
    7070  </ImportGroup>
    7171  <PropertyGroup Label="UserMacros" />
    72   <PropertyGroup />
     72  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
     73    <IncludePath>include;$(IncludePath)</IncludePath>
     74    <LibraryPath>lib;$(LibraryPath)</LibraryPath>
     75  </PropertyGroup>
    7376  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    7477    <ClCompile>
     
    8285    <Link>
    8386      <SubSystem>Console</SubSystem>
    84       <AdditionalLibraryDirectories>C:\Users\dportnoy\Desktop\SDL2-2.0.9\lib\x64;D:\VulkanSDK\1.1.108.0\Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    85       <AdditionalDependencies>SDL2.lib;SDL2main.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
     87      <AdditionalLibraryDirectories>D:\VulkanSDK\1.1.108.0\Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
     88      <AdditionalDependencies>SDL2.lib;SDL2main.lib;glfw3.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
    8689    </Link>
    8790  </ItemDefinitionGroup>
     
    131134  </ItemDefinitionGroup>
    132135  <ItemGroup>
     136    <ClCompile Include="game-gui-glfw.cpp" />
    133137    <ClCompile Include="game-gui-sdl.cpp" />
    134138    <ClCompile Include="vulkan-game.cpp" />
    135139  </ItemGroup>
    136140  <ItemGroup>
     141    <ClInclude Include="game-gui-glfw.hpp" />
    137142    <ClInclude Include="game-gui-sdl.hpp" />
    138143    <ClInclude Include="game-gui.hpp" />
  • shaders/shader.vert

    r8667f76 r80edd70  
    11#version 450
     2
     3layout(location = 0) in vec2 inPosition;
     4layout(location = 1) in vec3 inColor;
    25
    36layout(location = 0) out vec3 fragColor;
    47
    5 vec2 positions[3] = vec2[](
    6    vec2(0.0, -0.5),
    7    vec2(0.5, 0.5),
    8    vec2(-0.5, 0.5)
    9 );
    10 
    11 vec3 colors[3] = vec3[](
    12    vec3(1.0, 0.0, 0.0),
    13    vec3(0.0, 1.0, 0.0),
    14    vec3(0.0, 0.0, 1.0)
    15 );
    16 
    178void main() {
    18    gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
    19    fragColor = colors[gl_VertexIndex];
     9   gl_Position = vec4(inPosition, 0.0, 1.0);
     10   fragColor = inColor;
    2011}
  • vulkan-game.cpp

    r8667f76 r80edd70  
    77#define GLM_FORCE_RADIANS
    88#define GLM_FORCE_DEPTH_ZERO_TO_ONE
    9 #include <glm/vec4.hpp>
    10 #include <glm/mat4x4.hpp>
    11 
     9#include <glm/glm.hpp>
     10
     11#include <iostream>
    1212#include <fstream>
    13 #include <iostream>
     13#include <algorithm>
     14#include <vector>
     15#include <array>
     16#include <set>
    1417#include <optional>
    15 #include <set>
    1618
    1719using namespace std;
    18 //using namespace glm;
    1920
    2021const int SCREEN_WIDTH = 800;
     
    5051    vector<VkSurfaceFormatKHR> formats;
    5152    vector<VkPresentModeKHR> presentModes;
     53};
     54
     55struct Vertex {
     56   glm::vec2 pos;
     57   glm::vec3 color;
     58
     59   static VkVertexInputBindingDescription getBindingDescription() {
     60      VkVertexInputBindingDescription bindingDescription = {};
     61
     62      bindingDescription.binding = 0;
     63      bindingDescription.stride = sizeof(Vertex);
     64      bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
     65
     66      return bindingDescription;
     67   }
     68
     69   static array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions() {
     70      array<VkVertexInputAttributeDescription, 2> attributeDescriptions = {};
     71
     72      attributeDescriptions[0].binding = 0;
     73      attributeDescriptions[0].location = 0;
     74      attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
     75      attributeDescriptions[0].offset = offsetof(Vertex, pos);
     76
     77      attributeDescriptions[1].binding = 0;
     78      attributeDescriptions[1].location = 1;
     79      attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
     80      attributeDescriptions[1].offset = offsetof(Vertex, color);
     81
     82      return attributeDescriptions;
     83   }
     84};
     85
     86const vector<Vertex> vertices = {
     87   {{ 0.0f, -0.5f}, {1.0f, 1.0f, 1.0f}},
     88   {{ 0.5f,  0.5f}, {0.0f, 1.0f, 0.0f}},
     89   {{-0.5f,  0.5f}, {0.0f, 0.0f, 1.0f}}
    5290};
    5391
     
    112150      VkPipeline graphicsPipeline;
    113151      VkCommandPool commandPool;
     152
     153      VkBuffer vertexBuffer;
     154      VkDeviceMemory vertexBufferMemory;
    114155
    115156      vector<VkFramebuffer> swapChainFramebuffers;
     
    152193         createFramebuffers();
    153194         createCommandPool();
     195         createVertexBuffer();
    154196         createCommandBuffers();
    155197         createSyncObjects();
     
    666708         VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
    667709         vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
    668          vertexInputInfo.vertexBindingDescriptionCount = 0;
    669          vertexInputInfo.vertexAttributeDescriptionCount = 0;
     710
     711         auto bindingDescription = Vertex::getBindingDescription();
     712         auto attributeDescriptions = Vertex::getAttributeDescriptions();
     713
     714         vertexInputInfo.vertexBindingDescriptionCount = 1;
     715         vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
     716         vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
     717         vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
    670718
    671719         VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
     
    808856      }
    809857
     858      void createVertexBuffer() {
     859         VkBufferCreateInfo bufferInfo = {};
     860         bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
     861         bufferInfo.size = sizeof(vertices[0]) * vertices.size();
     862         bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
     863         bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
     864
     865         if (vkCreateBuffer(device, &bufferInfo, nullptr, &vertexBuffer) != VK_SUCCESS) {
     866            throw runtime_error("failed to create vertex buffer!");
     867         }
     868
     869         VkMemoryRequirements memoryRequirements;
     870         vkGetBufferMemoryRequirements(device, vertexBuffer, &memoryRequirements);
     871
     872         VkMemoryAllocateInfo allocInfo = {};
     873         allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
     874         allocInfo.allocationSize = memoryRequirements.size;
     875         allocInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
     876
     877         if (vkAllocateMemory(device, &allocInfo, nullptr, &vertexBufferMemory) != VK_SUCCESS) {
     878            throw runtime_error("failed to allocate vertex buffer memory!");
     879         }
     880
     881         vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);
     882
     883         void* data;
     884         vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, &data);
     885         memcpy(data, vertices.data(), (size_t)bufferInfo.size);
     886         vkUnmapMemory(device, vertexBufferMemory);
     887      }
     888
     889      uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
     890         VkPhysicalDeviceMemoryProperties memProperties;
     891         vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
     892
     893         for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
     894            if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
     895               return i;
     896            }
     897         }
     898
     899         throw runtime_error("failed to find suitable memory type!");
     900      }
     901
    810902      void createCommandBuffers() {
    811903         commandBuffers.resize(swapChainFramebuffers.size());
     
    844936            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    845937            vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
    846             vkCmdDraw(commandBuffers[i], 3, 1, 0, 0);
     938
     939            VkBuffer vertexBuffers[] = { vertexBuffer };
     940            VkDeviceSize offsets[] = { 0 };
     941            vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
     942
     943            vkCmdDraw(commandBuffers[i], static_cast<uint32_t>(vertices.size()), 1, 0, 0);
    847944            vkCmdEndRenderPass(commandBuffers[i]);
    848945
     
    9721069         cleanupSwapChain();
    9731070
     1071         vkDestroyBuffer(device, vertexBuffer, nullptr);
     1072         vkFreeMemory(device, vertexBufferMemory, nullptr);
     1073
    9741074         for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
    9751075            vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
     
    10311131#endif
    10321132
    1033    glm::mat4 matrix;
    1034    glm::vec4 vec;
    1035    glm::vec4 test = matrix * vec;
    1036 
    10371133   cout << "Starting Vulkan game..." << endl;
    10381134
Note: See TracChangeset for help on using the changeset viewer.