Changeset 90880fb in opengl-game
- Timestamp:
- Jun 17, 2021, 11:59:14 PM (3 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 2f4ff8c
- Parents:
- 5ea0a37
- git-author:
- Dmitry Portnoy <dportnoy@…> (06/13/21 23:57:17)
- git-committer:
- Dmitry Portnoy <dportnoy@…> (06/17/21 23:59:14)
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sdl-game.cpp
r5ea0a37 r90880fb 70 70 , gui(nullptr) 71 71 , window(nullptr) 72 , vp_mats_modelPipeline() 72 73 , objects_modelPipeline() 73 74 , score(0) … … 91 92 VkPhysicalDeviceProperties deviceProperties; 92 93 vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties); 94 95 vp_mats_modelPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange, 96 deviceProperties.limits.minUniformBufferOffsetAlignment); 93 97 94 98 objects_modelPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange, … … 110 114 modelPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex)); 111 115 112 createBufferSet( sizeof(UBO_VP_mats),116 createBufferSet(vp_mats_modelPipeline.memorySize(), 113 117 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 114 118 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 115 119 uniformBuffers_modelPipeline); 116 120 117 createBufferSet(objects_modelPipeline. capacity * sizeof(SSBO_ModelObject),121 createBufferSet(objects_modelPipeline.memorySize(), 118 122 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT 119 123 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, … … 289 293 projMat = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP); 290 294 projMat[1][1] *= -1; // flip the y-axis so that +y is up 291 292 object_VP_mats.view = viewMat;293 object_VP_mats.proj = projMat;294 295 } 295 296 … … 444 445 445 446 void VulkanGame::updateScene() { 446 // TODO: Probably move the resizing to the VulkanBuffer class 447 vp_mats_modelPipeline.data()->view = viewMat; 448 vp_mats_modelPipeline.data()->proj = projMat; 449 450 // TODO: Probably move the resizing to the VulkanBuffer class's add() method 451 // Remember that we want to avoid resizing the actuall ssbo or ubo more than once per frame 452 // TODO: Figure out a way to make updateDescriptorInfo easier to use, maybe store the binding index in the buffer set 453 447 454 if (objects_modelPipeline.resized) { 448 455 resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline.memorySize(), resourceCommandPool, … … 467 474 } 468 475 476 VulkanUtils::copyDataToMemory(device, vp_mats_modelPipeline.data(), uniformBuffers_modelPipeline.memory[imageIndex], 477 0, vp_mats_modelPipeline.memorySize(), false); 478 469 479 VulkanUtils::copyDataToMemory(device, objects_modelPipeline.data(), storageBuffers_modelPipeline.memory[imageIndex], 470 480 0, objects_modelPipeline.memorySize(), false); 471 472 VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,473 sizeof(object_VP_mats), false);474 481 } 475 482 … … 1275 1282 createSyncObjects(); 1276 1283 1277 createBufferSet( sizeof(UBO_VP_mats),1284 createBufferSet(vp_mats_modelPipeline.memorySize(), 1278 1285 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1279 1286 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, -
sdl-game.hpp
r5ea0a37 r90880fb 225 225 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline; 226 226 227 BufferSet uniformBuffers_modelPipeline; 227 228 BufferSet storageBuffers_modelPipeline; 229 230 VulkanBuffer<UBO_VP_mats> vp_mats_modelPipeline; 228 231 VulkanBuffer<SSBO_ModelObject> objects_modelPipeline; 229 BufferSet uniformBuffers_modelPipeline;230 232 231 233 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo … … 238 240 239 241 vector<SceneObject<ModelVertex>> modelObjects; 240 241 UBO_VP_mats object_VP_mats;242 242 243 243 /*** High-level vars ***/ -
vulkan-buffer.hpp
r5ea0a37 r90880fb 37 37 T& get(uint32_t index); 38 38 void add(T obj); 39 40 void* mapped(size_t idx); 41 42 void map(vector<VkDeviceMemory>& deviceMemory, VkDevice device); 43 void unmap(vector<VkDeviceMemory>& deviceMemory, VkDevice device); 39 44 40 45 void resize(); … … 182 187 183 188 template<class T> 189 void* VulkanBuffer<T>::mapped(size_t idx) { 190 return mappedData[idx]; 191 } 192 193 template<class T> 194 void VulkanBuffer<T>::map(vector<VkDeviceMemory>& deviceMemory, VkDevice device) { 195 // TODO: Make sure that mappedData initally has size 0. If not, it means the memory is already mapped 196 // and I should return some kind of error or warning 197 mappedData.resize(deviceMemory.size()); 198 199 for (size_t i = 0; i < deviceMemory.size(); i++) { 200 vkMapMemory(device, deviceMemory[i], 0, memorySize(), 0, &mappedData[i]); 201 } 202 } 203 204 template<class T> 205 void VulkanBuffer<T>::unmap(vector<VkDeviceMemory>& deviceMemory, VkDevice device) { 206 for (size_t i = 0; i < deviceMemory.size(); i++) { 207 vkUnmapMemory(device, deviceMemory[i]); 208 } 209 210 mappedData.clear(); 211 } 212 213 template<class T> 184 214 void VulkanBuffer<T>::resize() { 185 215 resized = false; -
vulkan-game.cpp
r5ea0a37 r90880fb 77 77 , gui(nullptr) 78 78 , window(nullptr) 79 , vp_mats_modelPipeline() 79 80 , objects_modelPipeline() 81 , vp_mats_shipPipeline() 80 82 , objects_shipPipeline() 83 , vp_mats_asteroidPipeline() 81 84 , objects_asteroidPipeline() 85 , vp_mats_laserPipeline() 82 86 , objects_laserPipeline() 87 , uniforms_explosionPipeline() 83 88 , objects_explosionPipeline() 84 89 , score(0) 85 90 , fps(0.0f) { 86 object_VP_mats = {};87 ship_VP_mats = {};88 asteroid_VP_mats = {};89 laser_VP_mats = {};90 explosion_UBO = {};91 91 } 92 92 … … 110 110 vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties); 111 111 112 vp_mats_modelPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange, 113 deviceProperties.limits.minUniformBufferOffsetAlignment); 114 112 115 objects_modelPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange, 113 116 deviceProperties.limits.minStorageBufferOffsetAlignment); 117 118 vp_mats_shipPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange, 119 deviceProperties.limits.minUniformBufferOffsetAlignment); 120 114 121 objects_shipPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange, 115 122 deviceProperties.limits.minStorageBufferOffsetAlignment); 123 124 vp_mats_asteroidPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange, 125 deviceProperties.limits.minUniformBufferOffsetAlignment); 126 116 127 objects_asteroidPipeline = VulkanBuffer<SSBO_Asteroid>(10, deviceProperties.limits.maxStorageBufferRange, 117 128 deviceProperties.limits.minStorageBufferOffsetAlignment); 129 130 vp_mats_laserPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange, 131 deviceProperties.limits.minUniformBufferOffsetAlignment); 132 118 133 objects_laserPipeline = VulkanBuffer<SSBO_Laser>(2, deviceProperties.limits.maxStorageBufferRange, 119 134 deviceProperties.limits.minStorageBufferOffsetAlignment); 135 136 uniforms_explosionPipeline = VulkanBuffer<UBO_Explosion>(1, deviceProperties.limits.maxUniformBufferRange, 137 deviceProperties.limits.minUniformBufferOffsetAlignment); 138 120 139 objects_explosionPipeline = VulkanBuffer<SSBO_Explosion>(2, deviceProperties.limits.maxStorageBufferRange, 121 140 deviceProperties.limits.minStorageBufferOffsetAlignment); … … 138 157 modelPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex)); 139 158 140 createBufferSet( sizeof(UBO_VP_mats),159 createBufferSet(vp_mats_modelPipeline.memorySize(), 141 160 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 142 161 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 208 227 shipPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex)); 209 228 210 createBufferSet( sizeof(UBO_VP_mats),229 createBufferSet(vp_mats_shipPipeline.memorySize(), 211 230 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 212 231 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 475 494 asteroidPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex)); 476 495 477 createBufferSet( sizeof(UBO_VP_mats),496 createBufferSet(vp_mats_asteroidPipeline.memorySize(), 478 497 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 479 498 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 500 519 laserPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&LaserVertex::objIndex)); 501 520 502 createBufferSet( sizeof(UBO_VP_mats),521 createBufferSet(vp_mats_laserPipeline.memorySize(), 503 522 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 504 523 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 527 546 explosionPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ExplosionVertex::objIndex)); 528 547 529 createBufferSet( sizeof(UBO_Explosion),548 createBufferSet(uniforms_explosionPipeline.memorySize(), 530 549 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 531 550 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 684 703 projMat = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP); 685 704 projMat[1][1] *= -1; // flip the y-axis so that +y is up 686 687 object_VP_mats.view = viewMat;688 object_VP_mats.proj = projMat;689 690 ship_VP_mats.view = viewMat;691 ship_VP_mats.proj = projMat;692 693 asteroid_VP_mats.view = viewMat;694 asteroid_VP_mats.proj = projMat;695 696 laser_VP_mats.view = viewMat;697 laser_VP_mats.proj = projMat;698 699 explosion_UBO.view = viewMat;700 explosion_UBO.proj = projMat;701 705 } 702 706 … … 940 944 // where it will run just once per frame 941 945 void VulkanGame::updateScene() { 946 947 vp_mats_modelPipeline.data()->view = viewMat; 948 vp_mats_modelPipeline.data()->proj = projMat; 949 950 vp_mats_shipPipeline.data()->view = viewMat; 951 vp_mats_shipPipeline.data()->proj = projMat; 952 953 vp_mats_asteroidPipeline.data()->view = viewMat; 954 vp_mats_asteroidPipeline.data()->proj = projMat; 955 956 vp_mats_laserPipeline.data()->view = viewMat; 957 vp_mats_laserPipeline.data()->proj = projMat; 958 959 uniforms_explosionPipeline.data()->view = viewMat; 960 uniforms_explosionPipeline.data()->proj = projMat; 961 uniforms_explosionPipeline.data()->cur_time = curTime; 962 963 for (vector<BaseEffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) { 964 if ((*it)->deleted) { 965 delete *it; 966 it = effects.erase(it); 967 } else { 968 BaseEffectOverTime* eot = *it; 969 970 eot->applyEffect(curTime); 971 972 it++; 973 } 974 } 975 942 976 if (curTime - lastSpawn_asteroid > spawnRate_asteroid) { 943 977 lastSpawn_asteroid = curTime; … … 1046 1080 } 1047 1081 1082 VulkanUtils::copyDataToMemory(device, vp_mats_modelPipeline.data(), uniformBuffers_modelPipeline.memory[imageIndex], 1083 0, vp_mats_modelPipeline.memorySize(), false); 1084 1048 1085 VulkanUtils::copyDataToMemory(device, objects_modelPipeline.data(), storageBuffers_modelPipeline.memory[imageIndex], 1049 1086 0, objects_modelPipeline.memorySize(), false); … … 1066 1103 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1067 1104 } 1105 1106 VulkanUtils::copyDataToMemory(device, vp_mats_shipPipeline.data(), uniformBuffers_shipPipeline.memory[imageIndex], 1107 0, vp_mats_shipPipeline.memorySize(), false); 1068 1108 1069 1109 VulkanUtils::copyDataToMemory(device, objects_shipPipeline.data(), storageBuffers_shipPipeline.memory[imageIndex], … … 1111 1151 } 1112 1152 1153 VulkanUtils::copyDataToMemory(device, vp_mats_asteroidPipeline.data(), uniformBuffers_asteroidPipeline.memory[imageIndex], 1154 0, vp_mats_asteroidPipeline.memorySize(), false); 1155 1113 1156 VulkanUtils::copyDataToMemory(device, objects_asteroidPipeline.data(), storageBuffers_asteroidPipeline.memory[imageIndex], 1114 1157 0, objects_asteroidPipeline.memorySize(), false); … … 1137 1180 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1138 1181 } 1182 1183 VulkanUtils::copyDataToMemory(device, vp_mats_laserPipeline.data(), uniformBuffers_laserPipeline.memory[imageIndex], 1184 0, vp_mats_laserPipeline.memorySize(), false); 1139 1185 1140 1186 VulkanUtils::copyDataToMemory(device, objects_laserPipeline.data(), storageBuffers_laserPipeline.memory[imageIndex], … … 1164 1210 } 1165 1211 1212 VulkanUtils::copyDataToMemory(device, uniforms_explosionPipeline.data(), uniformBuffers_explosionPipeline.memory[imageIndex], 1213 0, uniforms_explosionPipeline.memorySize(), false); 1214 1166 1215 VulkanUtils::copyDataToMemory(device, objects_explosionPipeline.data(), storageBuffers_explosionPipeline.memory[imageIndex], 1167 1216 0, objects_explosionPipeline.memorySize(), false); 1168 1169 explosion_UBO.cur_time = curTime;1170 1171 for (vector<BaseEffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) {1172 if ((*it)->deleted) {1173 delete *it;1174 it = effects.erase(it);1175 } else {1176 BaseEffectOverTime* eot = *it;1177 1178 eot->applyEffect(curTime);1179 1180 it++;1181 }1182 }1183 1184 VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,1185 sizeof(object_VP_mats), false);1186 1187 VulkanUtils::copyDataToMemory(device, &ship_VP_mats, uniformBuffers_shipPipeline.memory[imageIndex], 0,1188 sizeof(ship_VP_mats), false);1189 1190 VulkanUtils::copyDataToMemory(device, &asteroid_VP_mats, uniformBuffers_asteroidPipeline.memory[imageIndex], 0,1191 sizeof(asteroid_VP_mats), false);1192 1193 VulkanUtils::copyDataToMemory(device, &laser_VP_mats, uniformBuffers_laserPipeline.memory[imageIndex], 0,1194 sizeof(laser_VP_mats), false);1195 1196 VulkanUtils::copyDataToMemory(device, &explosion_UBO, uniformBuffers_explosionPipeline.memory[imageIndex], 0,1197 sizeof(explosion_UBO), false);1198 1217 } 1199 1218 … … 2234 2253 createSyncObjects(); 2235 2254 2236 createBufferSet( sizeof(UBO_VP_mats),2255 createBufferSet(vp_mats_modelPipeline.memorySize(), 2237 2256 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 2238 2257 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 2250 2269 modelPipeline.createDescriptorSets(swapChainImages.size()); 2251 2270 2252 createBufferSet( sizeof(UBO_VP_mats),2271 createBufferSet(vp_mats_shipPipeline.memorySize(), 2253 2272 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 2254 2273 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 2266 2285 shipPipeline.createDescriptorSets(swapChainImages.size()); 2267 2286 2268 createBufferSet( sizeof(UBO_VP_mats),2287 createBufferSet(vp_mats_asteroidPipeline.memorySize(), 2269 2288 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 2270 2289 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 2282 2301 asteroidPipeline.createDescriptorSets(swapChainImages.size()); 2283 2302 2284 createBufferSet( sizeof(UBO_VP_mats),2303 createBufferSet(vp_mats_laserPipeline.memorySize(), 2285 2304 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 2286 2305 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 2298 2317 laserPipeline.createDescriptorSets(swapChainImages.size()); 2299 2318 2300 createBufferSet( sizeof(UBO_Explosion),2319 createBufferSet(uniforms_explosionPipeline.memorySize(), 2301 2320 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 2302 2321 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, -
vulkan-game.hpp
r5ea0a37 r90880fb 311 311 GraphicsPipeline_Vulkan<ExplosionVertex> explosionPipeline; 312 312 313 BufferSet uniformBuffers_modelPipeline; 313 314 BufferSet storageBuffers_modelPipeline; 315 316 VulkanBuffer<UBO_VP_mats> vp_mats_modelPipeline; 314 317 VulkanBuffer<SSBO_ModelObject> objects_modelPipeline; 315 BufferSet uniformBuffers_modelPipeline; 316 318 319 BufferSet uniformBuffers_shipPipeline; 317 320 BufferSet storageBuffers_shipPipeline; 321 322 VulkanBuffer<UBO_VP_mats> vp_mats_shipPipeline; 318 323 VulkanBuffer<SSBO_ModelObject> objects_shipPipeline; 319 BufferSet uniformBuffers_shipPipeline; 320 324 325 BufferSet uniformBuffers_asteroidPipeline; 321 326 BufferSet storageBuffers_asteroidPipeline; 327 328 VulkanBuffer<UBO_VP_mats> vp_mats_asteroidPipeline; 322 329 VulkanBuffer<SSBO_Asteroid> objects_asteroidPipeline; 323 BufferSet uniformBuffers_asteroidPipeline; 324 330 331 BufferSet uniformBuffers_laserPipeline; 325 332 BufferSet storageBuffers_laserPipeline; 333 334 VulkanBuffer<UBO_VP_mats> vp_mats_laserPipeline; 326 335 VulkanBuffer<SSBO_Laser> objects_laserPipeline; 327 BufferSet uniformBuffers_laserPipeline; 328 336 337 BufferSet uniformBuffers_explosionPipeline; 329 338 BufferSet storageBuffers_explosionPipeline; 339 340 VulkanBuffer<UBO_Explosion> uniforms_explosionPipeline; 330 341 VulkanBuffer<SSBO_Explosion> objects_explosionPipeline; 331 BufferSet uniformBuffers_explosionPipeline;332 342 333 343 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo … … 340 350 341 351 vector<SceneObject<ModelVertex>> modelObjects; 342 343 UBO_VP_mats object_VP_mats;344 345 352 vector<SceneObject<ModelVertex>> shipObjects; 346 347 UBO_VP_mats ship_VP_mats;348 349 353 vector<SceneObject<ModelVertex>> asteroidObjects; 350 351 UBO_VP_mats asteroid_VP_mats;352 353 354 vector<SceneObject<LaserVertex>> laserObjects; 354 355 UBO_VP_mats laser_VP_mats;356 357 355 vector<SceneObject<ExplosionVertex>> explosionObjects; 358 356 359 UBO_Explosion explosion_UBO;357 //UBO_Explosion explosion_UBO; 360 358 361 359 vector<BaseEffectOverTime*> effects;
Note:
See TracChangeset
for help on using the changeset viewer.