Changeset 055750a in opengl-game
- Timestamp:
- Dec 4, 2019, 9:33:43 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 785333b, cf727ca
- Parents:
- e1308e8
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.hpp
re1308e8 r055750a 376 376 switch (descriptorWrites[j].descriptorType) { 377 377 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: 378 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: 378 379 descriptorWrites[j].pBufferInfo = &(*this->descriptorInfoList[j].bufferDataList)[i]; 379 380 break; -
shaders/scene.frag
re1308e8 r055750a 2 2 #extension GL_ARB_separate_shader_objects : enable 3 3 4 layout(binding = 1) uniform sampler2D texSampler;4 layout(binding = 2) uniform sampler2D texSampler; 5 5 6 6 layout(location = 0) in vec3 fragColor; -
shaders/scene.vert
re1308e8 r055750a 2 2 #extension GL_ARB_separate_shader_objects : enable 3 3 4 struct Object { 5 mat4 model; 6 }; 7 4 8 layout (binding = 0) uniform UniformBufferObject { 5 mat4 model;6 9 mat4 view; 7 10 mat4 proj; 8 11 } ubo; 12 13 layout(binding = 1) readonly buffer StorageBufferObject { 14 Object objects[]; 15 } sbo; 9 16 10 17 layout(location = 0) in vec3 inPosition; … … 19 26 fragTexCoord = inTexCoord; 20 27 21 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);28 gl_Position = ubo.proj * ubo.view * sbo.objects[0].model * vec4(inPosition, 1.0); 22 29 } -
shaders/ship.vert
re1308e8 r055750a 2 2 #extension GL_ARB_separate_shader_objects : enable 3 3 4 struct Object { 5 mat4 model; 6 }; 7 4 8 layout (binding = 0) uniform UniformBufferObject { 5 mat4 model;6 9 mat4 view; 7 10 mat4 proj; 8 11 } ubo; 12 13 layout(binding = 1) readonly buffer StorageBufferObject { 14 Object objects[]; 15 } sbo; 9 16 10 17 layout(location = 0) in vec3 vertex_position; … … 26 33 // Check Anton's book to see how to fix this 27 34 void main() { 28 position_eye = vec3(ubo.view * ubo.model * vec4(vertex_position, 1.0));35 position_eye = vec3(ubo.view * sbo.objects[0].model * vec4(vertex_position, 1.0)); 29 36 //position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0)); 30 37 31 38 // Using 0.0 instead of 1.0 means translations won't effect the normal 32 normal_eye = normalize(vec3(ubo.view * ubo.model * vec4(vertex_normal, 0.0)));39 normal_eye = normalize(vec3(ubo.view * sbo.objects[0].model * vec4(vertex_normal, 0.0))); 33 40 //normal_eye = normalize(vec3(view * model_mats[ubo_index] * vec4(vertex_normal, 0.0))); 34 41 color = vertex_color; -
vulkan-game.cpp
re1308e8 r055750a 26 26 framebufferResized = false; 27 27 28 modelMvpMats = {};29 ship MvpMats = {};28 object_VP_mats = {}; 29 ship_VP_mats = {}; 30 30 } 31 31 … … 196 196 modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord)); 197 197 198 createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList); 198 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 199 uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline); 200 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 201 storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline); 199 202 200 203 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 201 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList); 204 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_scenePipeline); 205 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 206 VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_scenePipeline); 207 202 208 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 203 209 VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor); … … 250 256 shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::normal)); 251 257 252 createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 258 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 259 uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 260 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 261 storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline); 253 262 254 263 shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 255 264 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline); 265 shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 266 VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_shipPipeline); 256 267 257 268 // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly … … 525 536 proj[1][1] *= -1; // flip the y-axis so that +y is up 526 537 527 modelMvpMats.model = mat4(1.0f); 528 modelMvpMats.view = view; 529 modelMvpMats.proj = proj; 530 531 mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)); 532 mat4 R_model(1.0f); 533 534 shipMvpMats.model = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 535 shipMvpMats.view = view; 536 shipMvpMats.proj = proj; 538 object_VP_mats.view = view; 539 object_VP_mats.proj = proj; 540 541 ship_VP_mats.view = view; 542 ship_VP_mats.proj = proj; 537 543 } 538 544 … … 607 613 } 608 614 615 // TODO: The view and projection mats only need to be updated once. 616 // Create a separate function that can do this once per Vulkan image at the beginning 609 617 void VulkanGame::updateScene(uint32_t currentImage) { 610 618 static auto startTime = chrono::high_resolution_clock::now(); … … 613 621 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count(); 614 622 615 modelMvpMats.model =623 so_Object.model = 616 624 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) * 617 625 rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f)); 618 626 619 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory[currentImage], modelMvpMats); 620 621 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], shipMvpMats); 627 so_Ship.model = 628 translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) * 629 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 630 631 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats); 632 633 VulkanUtils::copyDataToMemory(device, storageBuffersMemory_scenePipeline[currentImage], so_Object); 634 635 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], ship_VP_mats); 636 637 VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], so_Ship); 622 638 } 623 639 … … 901 917 902 918 void VulkanGame::createLogicalDevice( 903 const vector<const char*> validationLayers, 904 const vector<const char*>& deviceExtensions) { 919 const vector<const char*> validationLayers, const vector<const char*>& deviceExtensions) { 905 920 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 906 921 … … 1237 1252 } 1238 1253 1254 void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 1255 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) { 1256 buffers.resize(swapChainImages.size()); 1257 buffersMemory.resize(swapChainImages.size()); 1258 bufferInfoList.resize(swapChainImages.size()); 1259 1260 for (size_t i = 0; i < swapChainImages.size(); i++) { 1261 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, flags, 1262 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 1263 buffers[i], buffersMemory[i]); 1264 1265 bufferInfoList[i].buffer = buffers[i]; 1266 bufferInfoList[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now 1267 bufferInfoList[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE 1268 } 1269 } 1270 1239 1271 // TODO: Fix the crash that happens when alt-tabbing 1240 1272 void VulkanGame::recreateSwapChain() { … … 1260 1292 createFramebuffers(); 1261 1293 1262 createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList); 1294 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1295 uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline); 1296 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1297 storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline); 1263 1298 1264 1299 modelPipeline.updateRenderPass(renderPass); … … 1272 1307 overlayPipeline.createDescriptorSets(swapChainImages); 1273 1308 1274 createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 1309 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1310 uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 1311 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1312 storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline); 1275 1313 1276 1314 shipPipeline.updateRenderPass(renderPass); … … 1303 1341 vkDestroySwapchainKHR(device, swapChain, nullptr); 1304 1342 1305 for (size_t i = 0; i < uniformBuffers.size(); i++) { 1306 vkDestroyBuffer(device, uniformBuffers[i], nullptr); 1307 vkFreeMemory(device, uniformBuffersMemory[i], nullptr); 1343 for (size_t i = 0; i < uniformBuffers_scenePipeline.size(); i++) { 1344 vkDestroyBuffer(device, uniformBuffers_scenePipeline[i], nullptr); 1345 vkFreeMemory(device, uniformBuffersMemory_scenePipeline[i], nullptr); 1346 } 1347 1348 for (size_t i = 0; i < storageBuffers_scenePipeline.size(); i++) { 1349 vkDestroyBuffer(device, storageBuffers_scenePipeline[i], nullptr); 1350 vkFreeMemory(device, storageBuffersMemory_scenePipeline[i], nullptr); 1308 1351 } 1309 1352 … … 1312 1355 vkFreeMemory(device, uniformBuffersMemory_shipPipeline[i], nullptr); 1313 1356 } 1314 } 1357 1358 for (size_t i = 0; i < storageBuffers_shipPipeline.size(); i++) { 1359 vkDestroyBuffer(device, storageBuffers_shipPipeline[i], nullptr); 1360 vkFreeMemory(device, storageBuffersMemory_shipPipeline[i], nullptr); 1361 } 1362 } -
vulkan-game.hpp
re1308e8 r055750a 35 35 }; 36 36 37 struct UBO_MvpMat { 38 alignas(16) mat4 model; 37 struct UBO_VP_mats { 39 38 alignas(16) mat4 view; 40 39 alignas(16) mat4 proj; 40 }; 41 42 struct SBO_SceneObject { 43 alignas(16) mat4 model; 41 44 }; 42 45 … … 57 60 vec3 cam_pos; 58 61 59 UBO_MvpMat modelMvpMats; 60 UBO_MvpMat shipMvpMats; 62 UBO_VP_mats object_VP_mats; 63 SBO_SceneObject so_Object; 64 65 UBO_VP_mats ship_VP_mats; 66 SBO_SceneObject so_Ship; 61 67 62 68 GameGui* gui; … … 96 102 VkSampler textureSampler; 97 103 98 // These are currently to store the MVP matrix 99 // I should figure out if it makes sense to use them for other uniforms in the future 100 // If not, I should rename them to better indicate their purpose. 101 vector<VkBuffer> uniformBuffers; 102 vector<VkDeviceMemory> uniformBuffersMemory; 103 104 vector<VkDescriptorBufferInfo> uniformBufferInfoList; 104 // TODO: I should probably rename the uniformBuffer* and storageBuffer* 105 // variables to better reflect the data they hold 106 107 vector<VkBuffer> uniformBuffers_scenePipeline; 108 vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline; 109 110 vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline; 111 112 vector<VkBuffer> storageBuffers_scenePipeline; 113 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline; 114 115 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline; 105 116 106 117 vector<VkBuffer> uniformBuffers_shipPipeline; … … 108 119 109 120 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline; 121 122 vector<VkBuffer> storageBuffers_shipPipeline; 123 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline; 124 125 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline; 110 126 111 127 VulkanImage floorTextureImage; … … 162 178 vector<VertexType> addVertexNormals(vector<VertexType> vertices); 163 179 164 template<class UniformType> 165 void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, 166 vector<VkDescriptorBufferInfo>& bufferInfoList); 180 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 181 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList); 167 182 168 183 void recreateSwapChain(); … … 176 191 void* pUserData); 177 192 }; 178 179 template<class UniformType>180 void VulkanGame::createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,181 vector<VkDescriptorBufferInfo>& bufferInfoList) {182 buffers.resize(swapChainImages.size());183 buffersMemory.resize(swapChainImages.size());184 bufferInfoList.resize(swapChainImages.size());185 186 for (size_t i = 0; i < swapChainImages.size(); i++) {187 VulkanUtils::createBuffer(device, physicalDevice, sizeof(UniformType), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,188 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,189 buffers[i], buffersMemory[i]);190 191 bufferInfoList[i].buffer = buffers[i];192 bufferInfoList[i].offset = 0;193 bufferInfoList[i].range = sizeof(UniformType);194 }195 }196 193 197 194 template<class VertexType> -
vulkan-utils.hpp
re1308e8 r055750a 125 125 } 126 126 127 // TODO: Change copyDataToMemory so it can accept offsets for the data source and destination 127 128 template<class DataType> 128 129 void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData) {
Note:
See TracChangeset
for help on using the changeset viewer.