Changeset 80edd70 in opengl-game
- Timestamp:
- Jul 23, 2019, 4:02:06 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- d9ef6ab
- Parents:
- 8667f76
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
README.txt
r8667f76 r80edd70 59 59 - Copy the SDL2 include folder into /include and rename it SDL2 60 60 - 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 61 63 62 64 Download the vulkan sdk -
VulkanGame.vcxproj
r8667f76 r80edd70 70 70 </ImportGroup> 71 71 <PropertyGroup Label="UserMacros" /> 72 <PropertyGroup /> 72 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> 73 <IncludePath>include;$(IncludePath)</IncludePath> 74 <LibraryPath>lib;$(LibraryPath)</LibraryPath> 75 </PropertyGroup> 73 76 <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> 74 77 <ClCompile> … … 82 85 <Link> 83 86 <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> 86 89 </Link> 87 90 </ItemDefinitionGroup> … … 131 134 </ItemDefinitionGroup> 132 135 <ItemGroup> 136 <ClCompile Include="game-gui-glfw.cpp" /> 133 137 <ClCompile Include="game-gui-sdl.cpp" /> 134 138 <ClCompile Include="vulkan-game.cpp" /> 135 139 </ItemGroup> 136 140 <ItemGroup> 141 <ClInclude Include="game-gui-glfw.hpp" /> 137 142 <ClInclude Include="game-gui-sdl.hpp" /> 138 143 <ClInclude Include="game-gui.hpp" /> -
shaders/shader.vert
r8667f76 r80edd70 1 1 #version 450 2 3 layout(location = 0) in vec2 inPosition; 4 layout(location = 1) in vec3 inColor; 2 5 3 6 layout(location = 0) out vec3 fragColor; 4 7 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 17 8 void 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; 20 11 } -
vulkan-game.cpp
r8667f76 r80edd70 7 7 #define GLM_FORCE_RADIANS 8 8 #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> 12 12 #include <fstream> 13 #include <iostream> 13 #include <algorithm> 14 #include <vector> 15 #include <array> 16 #include <set> 14 17 #include <optional> 15 #include <set>16 18 17 19 using namespace std; 18 //using namespace glm;19 20 20 21 const int SCREEN_WIDTH = 800; … … 50 51 vector<VkSurfaceFormatKHR> formats; 51 52 vector<VkPresentModeKHR> presentModes; 53 }; 54 55 struct 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 86 const 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}} 52 90 }; 53 91 … … 112 150 VkPipeline graphicsPipeline; 113 151 VkCommandPool commandPool; 152 153 VkBuffer vertexBuffer; 154 VkDeviceMemory vertexBufferMemory; 114 155 115 156 vector<VkFramebuffer> swapChainFramebuffers; … … 152 193 createFramebuffers(); 153 194 createCommandPool(); 195 createVertexBuffer(); 154 196 createCommandBuffers(); 155 197 createSyncObjects(); … … 666 708 VkPipelineVertexInputStateCreateInfo vertexInputInfo = {}; 667 709 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(); 670 718 671 719 VkPipelineInputAssemblyStateCreateInfo inputAssembly = {}; … … 808 856 } 809 857 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 810 902 void createCommandBuffers() { 811 903 commandBuffers.resize(swapChainFramebuffers.size()); … … 844 936 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 845 937 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); 847 944 vkCmdEndRenderPass(commandBuffers[i]); 848 945 … … 972 1069 cleanupSwapChain(); 973 1070 1071 vkDestroyBuffer(device, vertexBuffer, nullptr); 1072 vkFreeMemory(device, vertexBufferMemory, nullptr); 1073 974 1074 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { 975 1075 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr); … … 1031 1131 #endif 1032 1132 1033 glm::mat4 matrix;1034 glm::vec4 vec;1035 glm::vec4 test = matrix * vec;1036 1037 1133 cout << "Starting Vulkan game..." << endl; 1038 1134
Note:
See TracChangeset
for help on using the changeset viewer.