Changeset 6bac215 in opengl-game
- Timestamp:
- Jun 9, 2021, 12:38:14 AM (3 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- bb76950
- Parents:
- 8dcbf62
- git-author:
- Dmitry Portnoy <dportnoy@…> (06/09/21 00:38:09)
- git-committer:
- Dmitry Portnoy <dportnoy@…> (06/09/21 00:38:14)
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sdl-game.cpp
r8dcbf62 r6bac215 269 269 270 270 createBufferSet(objects_modelPipeline.capacity * sizeof(SSBO_ModelObject), 271 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 272 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 273 storageBuffers_modelPipeline); 271 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 272 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 273 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 274 storageBuffers_modelPipeline); 274 275 } 275 276 … … 456 457 457 458 // TODO: Probably move the resizing to the VulkanBuffer class 458 if (objects_modelPipeline. numObjects > objects_modelPipeline.capacity) {459 if (objects_modelPipeline.resized) { 459 460 // TODO: Also resize the dynamic ubo 460 461 resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline, modelPipeline, resourceCommandPool, 461 462 graphicsQueue); 463 464 objects_modelPipeline.resize(); 462 465 } 463 466 -
sdl-game.hpp
r8dcbf62 r6bac215 348 348 GraphicsPipeline_Vulkan<VertexType>& pipeline, VkCommandPool commandPool, 349 349 VkQueue graphicsQueue) { 350 size_t numObjects = buffer.numObjects < buffer.capacity ? buffer.numObjects : buffer.capacity; 351 352 do { 353 buffer.capacity *= 2; 354 } while (buffer.capacity < buffer.numObjects); 355 356 VkDeviceSize bufferSize = buffer.capacity * sizeof(SSBOType); 350 VkDeviceSize newSize = buffer.capacity * sizeof(SSBOType); 357 351 358 352 for (size_t i = 0; i < set.buffers.size(); i++) { 359 VkBuffer newStorageBuffer; 360 VkDeviceMemory newStorageBufferMemory; 361 362 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, 363 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 364 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 365 newStorageBuffer, newStorageBufferMemory); 366 367 VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer, 368 0, 0, numObjects * sizeof(SSBOType), graphicsQueue); 353 VkBuffer newBuffer; 354 VkDeviceMemory newMemory; 355 356 VulkanUtils::createBuffer(device, physicalDevice, newSize, set.usages, set.properties, newBuffer, newMemory); 357 358 VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newBuffer, 0, 0, set.infoSet[i].range, 359 graphicsQueue); 369 360 370 361 vkDestroyBuffer(device, set.buffers[i], nullptr); 371 362 vkFreeMemory(device, set.memory[i], nullptr); 372 363 373 set.buffers[i] = new StorageBuffer;374 set.memory[i] = new StorageBufferMemory;364 set.buffers[i] = newBuffer; 365 set.memory[i] = newMemory; 375 366 376 367 set.infoSet[i].buffer = set.buffers[i]; 377 368 set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now 378 set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE369 set.infoSet[i].range = newSize; // Size of the update starting from offset, or VK_WHOLE_SIZE 379 370 } 380 371 -
vulkan-buffer.hpp
r8dcbf62 r6bac215 14 14 // Externally, they are only used in resizeBufferSet 15 15 size_t capacity; 16 size_t numObjects; 16 17 // temp field to help with ubo+ssbo resizing until they are added to this class 18 // See if I need a separate field for this or if I can use other fields to check for this 19 // Maybe compare uniform or storage buffer size to the size of the memory allocated here 20 bool resized; 17 21 18 22 VulkanBuffer(); 19 23 VulkanBuffer(size_t capacity, size_t minOffsetAlignment); 24 25 VulkanBuffer(const VulkanBuffer<T>&) = delete; 26 VulkanBuffer(VulkanBuffer<T>&& other); 27 20 28 ~VulkanBuffer(); 21 29 22 VulkanBuffer<T>& operator=(const VulkanBuffer<T>& other); 30 VulkanBuffer<T>& operator=(const VulkanBuffer<T>&) = delete; 31 VulkanBuffer<T>& operator=(VulkanBuffer<T>&& other) noexcept; 32 33 void resize(); 23 34 24 35 void add(T obj); … … 29 40 30 41 size_t alignment; 42 size_t numObjects; 31 43 32 44 T* srcData; // TODO: Rename this to something else probably and rename rawData to data … … 56 68 , capacity(0) 57 69 , numObjects(0) 70 , resized(false) 58 71 , srcData(nullptr) 59 72 , rawData(nullptr) … … 66 79 , capacity(capacity) 67 80 , numObjects(0) 81 , resized(false) 68 82 , srcData(nullptr) 69 83 , rawData(nullptr) … … 77 91 78 92 template<class T> 93 VulkanBuffer<T>::VulkanBuffer(VulkanBuffer<T>&& other) { 94 // TODO: Implement 95 } 96 97 template<class T> 79 98 VulkanBuffer<T>::~VulkanBuffer() { 80 99 if (srcData != nullptr) { … … 84 103 85 104 template<class T> 86 VulkanBuffer<T>& VulkanBuffer<T>::operator=(const VulkanBuffer<T>& other) { 87 if (this == &other) { 88 return *this; 105 VulkanBuffer<T>& VulkanBuffer<T>::operator=(VulkanBuffer<T>&& other) noexcept { 106 if (this != &other) { 107 capacity = other.capacity; 108 numObjects = other.numObjects; 109 resized = other.resized; 110 111 alignment = other.alignment; 112 113 if (srcData != nullptr) { 114 free(srcData); 115 } 116 117 srcData = other.srcData; 118 119 other.capacity = 0; 120 other.numObjects = 0; 121 // TODO: Maybe set rnage to 0 as well 122 123 other.srcData = nullptr; 89 124 } 90 91 /*92 // assume *this manages a reusable resource, such as a heap-allocated buffer mArray93 if (size != other.size) { // resource in *this cannot be reused94 delete[] mArray; // release resource in *this95 mArray = nullptr;96 size = 0; // preserve invariants in case next line throws97 mArray = new int[other.size]; // allocate resource in *this98 size = other.size;99 }100 */101 102 if (srcData != nullptr) {103 free(srcData);104 srcData = nullptr;105 }106 107 alignment = other.alignment;108 capacity = other.capacity;109 110 srcData = (T*)malloc(capacity * alignment);111 // TODO: Check for failure112 113 memcpy(srcData, other.srcData, capacity * alignment);114 125 115 126 return *this; … … 117 128 118 129 template<class T> 130 void VulkanBuffer<T>::resize() { 131 resized = false; 132 } 133 134 template<class T> 119 135 void VulkanBuffer<T>::add(T obj) { 136 if (numObjects == capacity) { 137 // Once I add Vulkan buffer objects in here, make sure this doesn't overlap with resizeBufferSet 138 resized = true; 139 140 capacity *= 2; 141 } 142 120 143 numObjects++; 121 144 } -
vulkan-game.cpp
r8dcbf62 r6bac215 616 616 617 617 createBufferSet(objects_modelPipeline.capacity * sizeof(SSBO_ModelObject), 618 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 619 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 620 storageBuffers_modelPipeline); 618 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 619 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 620 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 621 storageBuffers_modelPipeline); 621 622 622 623 shipPipeline = GraphicsPipeline_Vulkan<ModelVertex>( … … 625 626 626 627 createBufferSet(objects_shipPipeline.capacity * sizeof(SSBO_ModelObject), 627 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 628 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 629 storageBuffers_shipPipeline); 628 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 629 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 630 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 631 storageBuffers_shipPipeline); 630 632 631 633 asteroidPipeline = GraphicsPipeline_Vulkan<ModelVertex>( … … 634 636 635 637 createBufferSet(objects_asteroidPipeline.capacity * sizeof(SSBO_Asteroid), 636 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 637 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 638 storageBuffers_asteroidPipeline); 638 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 639 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 640 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 641 storageBuffers_asteroidPipeline); 639 642 640 643 laserPipeline = GraphicsPipeline_Vulkan<LaserVertex>( … … 643 646 644 647 createBufferSet(objects_laserPipeline.capacity * sizeof(SSBO_Laser), 645 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 646 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 647 storageBuffers_laserPipeline); 648 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 649 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 650 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 651 storageBuffers_laserPipeline); 648 652 649 653 explosionPipeline = GraphicsPipeline_Vulkan<ExplosionVertex>( … … 653 657 654 658 createBufferSet(objects_explosionPipeline.capacity * sizeof(SSBO_Explosion), 655 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 656 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 657 storageBuffers_explosionPipeline); 659 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 660 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 661 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 662 storageBuffers_explosionPipeline); 658 663 } 659 664 … … 1085 1090 1086 1091 // TODO: Probably move the resizing to the VulkanBuffer class 1087 if (objects_modelPipeline. numObjects > objects_modelPipeline.capacity) {1092 if (objects_modelPipeline.resized) { 1088 1093 resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline, modelPipeline, resourceCommandPool, 1089 1094 graphicsQueue); 1095 1096 objects_modelPipeline.resize(); 1090 1097 } 1091 1098 … … 1098 1105 1099 1106 // TODO: Probably move the resizing to the VulkanBuffer class 1100 if (objects_shipPipeline. numObjects > objects_shipPipeline.capacity) {1107 if (objects_shipPipeline.resized) { 1101 1108 resizeBufferSet(storageBuffers_shipPipeline, objects_shipPipeline, shipPipeline, resourceCommandPool, 1102 1109 graphicsQueue); 1110 1111 objects_shipPipeline.resize(); 1103 1112 } 1104 1113 … … 1111 1120 1112 1121 // TODO: Probably move the resizing to the VulkanBuffer class 1113 if (objects_asteroidPipeline. numObjects > objects_asteroidPipeline.capacity) {1122 if (objects_asteroidPipeline.resized) { 1114 1123 resizeBufferSet(storageBuffers_asteroidPipeline, objects_asteroidPipeline, asteroidPipeline, 1115 1124 resourceCommandPool, graphicsQueue); 1125 1126 objects_asteroidPipeline.resize(); 1116 1127 } 1117 1128 … … 1124 1135 1125 1136 // TODO: Probably move the resizing to the VulkanBuffer class 1126 if (objects_laserPipeline. numObjects > objects_laserPipeline.capacity) {1137 if (objects_laserPipeline.resized) { 1127 1138 resizeBufferSet(storageBuffers_laserPipeline, objects_laserPipeline, laserPipeline, resourceCommandPool, 1128 1139 graphicsQueue); 1140 1141 objects_laserPipeline.resize(); 1129 1142 } 1130 1143 … … 1137 1150 1138 1151 // TODO: Probably move the resizing to the VulkanBuffer class 1139 if (objects_explosionPipeline. numObjects > objects_explosionPipeline.capacity) {1152 if (objects_explosionPipeline.resized) { 1140 1153 resizeBufferSet(storageBuffers_explosionPipeline, objects_explosionPipeline, explosionPipeline, 1141 1154 resourceCommandPool, graphicsQueue); 1155 1156 objects_explosionPipeline.resize(); 1142 1157 } 1143 1158 -
vulkan-game.hpp
r8dcbf62 r6bac215 503 503 GraphicsPipeline_Vulkan<VertexType>& pipeline, VkCommandPool commandPool, 504 504 VkQueue graphicsQueue) { 505 size_t numObjects = buffer.numObjects < buffer.capacity ? buffer.numObjects : buffer.capacity; 506 507 do { 508 buffer.capacity *= 2; 509 } while (buffer.capacity < buffer.numObjects); 510 511 VkDeviceSize bufferSize = buffer.capacity * sizeof(SSBOType); 505 VkDeviceSize newSize = buffer.capacity * sizeof(SSBOType); 512 506 513 507 for (size_t i = 0; i < set.buffers.size(); i++) { 514 VkBuffer newStorageBuffer; 515 VkDeviceMemory newStorageBufferMemory; 516 517 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, 518 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 519 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 520 newStorageBuffer, newStorageBufferMemory); 521 522 VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer, 523 0, 0, numObjects * sizeof(SSBOType), graphicsQueue); 508 VkBuffer newBuffer; 509 VkDeviceMemory newMemory; 510 511 VulkanUtils::createBuffer(device, physicalDevice, newSize, set.usages, set.properties, newBuffer, newMemory); 512 513 VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newBuffer, 0, 0, set.infoSet[i].range, 514 graphicsQueue); 524 515 525 516 vkDestroyBuffer(device, set.buffers[i], nullptr); 526 517 vkFreeMemory(device, set.memory[i], nullptr); 527 518 528 set.buffers[i] = new StorageBuffer;529 set.memory[i] = new StorageBufferMemory;519 set.buffers[i] = newBuffer; 520 set.memory[i] = newMemory; 530 521 531 522 set.infoSet[i].buffer = set.buffers[i]; 532 523 set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now 533 set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE524 set.infoSet[i].range = newSize; // Size of the update starting from offset, or VK_WHOLE_SIZE 534 525 } 535 526
Note:
See TracChangeset
for help on using the changeset viewer.