Changeset 9d21aac in opengl-game for vulkan-game.hpp
- Timestamp:
- May 6, 2021, 3:24:42 AM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 996dd3e
- Parents:
- 756162f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.hpp
r756162f r9d21aac 142 142 template<class VertexType, class SSBOType> 143 143 struct EffectOverTime : public BaseEffectOverTime { 144 GraphicsPipeline_Vulkan<VertexType , SSBOType>& pipeline;144 GraphicsPipeline_Vulkan<VertexType>& pipeline; 145 145 vector<SceneObject<VertexType, SSBOType>>& objects; 146 146 unsigned int objectIndex; … … 150 150 float changePerSecond; 151 151 152 EffectOverTime(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 153 vector<SceneObject<VertexType, SSBOType>>& objects, unsigned int objectIndex, 154 size_t effectedFieldOffset, float startTime, float changePerSecond) : 155 pipeline(pipeline), 156 objects(objects), 157 objectIndex(objectIndex), 158 effectedFieldOffset(effectedFieldOffset), 159 startTime(startTime), 160 changePerSecond(changePerSecond) { 152 EffectOverTime(GraphicsPipeline_Vulkan<VertexType>& pipeline, vector<SceneObject<VertexType, SSBOType>>& objects, 153 unsigned int objectIndex, size_t effectedFieldOffset, float startTime, float changePerSecond) 154 : pipeline(pipeline) 155 , objects(objects) 156 , objectIndex(objectIndex) 157 , effectedFieldOffset(effectedFieldOffset) 158 , startTime(startTime) 159 , changePerSecond(changePerSecond) { 161 160 size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo); 162 161 … … 301 300 // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan 302 301 // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures 303 GraphicsPipeline_Vulkan<ModelVertex , SSBO_ModelObject> modelPipeline;304 GraphicsPipeline_Vulkan<ModelVertex , SSBO_ModelObject> shipPipeline;305 GraphicsPipeline_Vulkan<ModelVertex , SSBO_Asteroid> asteroidPipeline;306 GraphicsPipeline_Vulkan<LaserVertex , SSBO_Laser> laserPipeline;307 GraphicsPipeline_Vulkan<ExplosionVertex , SSBO_Explosion> explosionPipeline;302 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline; 303 GraphicsPipeline_Vulkan<ModelVertex> shipPipeline; 304 GraphicsPipeline_Vulkan<ModelVertex> asteroidPipeline; 305 GraphicsPipeline_Vulkan<LaserVertex> laserPipeline; 306 GraphicsPipeline_Vulkan<ExplosionVertex> explosionPipeline; 308 307 309 308 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo … … 420 419 void cleanupImGuiOverlay(); 421 420 422 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 423 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, 424 vector<VkDescriptorBufferInfo>& bufferInfoList); 421 // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class 422 423 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, VkMemoryPropertyFlags properties, 424 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, 425 vector<VkDescriptorBufferInfo>& bufferInfoList); 426 427 // TODO: See if it makes sense to rename this to resizeBufferSet() and use it to resize other types of buffers as well 428 // TODO: Remove the need for templating, which is only there so a GraphicsPupeline_Vulkan can be passed in 429 template<class VertexType, class SSBOType> 430 void resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue, 431 GraphicsPipeline_Vulkan<VertexType>& pipeline); 432 433 template<class SSBOType> 434 void updateStorageBuffer(StorageBufferSet& storageBufferSet, size_t objIndex, SSBOType& ssbo); 425 435 426 436 // TODO: Since addObject() returns a reference to the new object now, 427 437 // stop using objects.back() to access the object that was just created 428 438 template<class VertexType, class SSBOType> 429 SceneObject<VertexType, SSBOType>& addObject( 430 vector<SceneObject<VertexType, SSBOType>>& objects, 431 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 432 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo, 433 bool pipelinesCreated); 439 SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects, 440 GraphicsPipeline_Vulkan<VertexType>& pipeline, 441 const vector<VertexType>& vertices, vector<uint16_t> indices, 442 SSBOType ssbo, bool pipelinesCreated); 434 443 435 444 template<class VertexType> … … 444 453 template<class VertexType, class SSBOType> 445 454 void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, 446 GraphicsPipeline_Vulkan<VertexType , SSBOType>& pipeline, size_t index);455 GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index); 447 456 448 457 template<class VertexType, class SSBOType> 449 void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType , SSBOType>& pipeline,458 void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline, 450 459 SceneObject<VertexType, SSBOType>& obj, size_t index); 451 460 … … 485 494 // End of specialized no-op functions 486 495 496 template<class VertexType, class SSBOType> 497 void VulkanGame::resizeStorageBufferSet(StorageBufferSet& set, VkCommandPool commandPool, VkQueue graphicsQueue, 498 GraphicsPipeline_Vulkan<VertexType>& pipeline) { 499 pipeline.objectCapacity *= 2; 500 VkDeviceSize bufferSize = pipeline.objectCapacity * sizeof(SSBOType); 501 502 for (size_t i = 0; i < set.buffers.size(); i++) { 503 VkBuffer newStorageBuffer; 504 VkDeviceMemory newStorageBufferMemory; 505 506 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, 507 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 508 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 509 newStorageBuffer, newStorageBufferMemory); 510 511 VulkanUtils::copyBuffer(device, commandPool, set.buffers[i], newStorageBuffer, 512 0, 0, pipeline.numObjects * sizeof(SSBOType), graphicsQueue); 513 514 vkDestroyBuffer(device, set.buffers[i], nullptr); 515 vkFreeMemory(device, set.memory[i], nullptr); 516 517 set.buffers[i] = newStorageBuffer; 518 set.memory[i] = newStorageBufferMemory; 519 520 set.infoSet[i].buffer = set.buffers[i]; 521 set.infoSet[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now 522 set.infoSet[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE 523 } 524 } 525 526 // TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them 527 template<class SSBOType> 528 void VulkanGame::updateStorageBuffer(StorageBufferSet& storageBufferSet, size_t objIndex, SSBOType& ssbo) { 529 for (size_t i = 0; i < storageBufferSet.memory.size(); i++) { 530 VulkanUtils::copyDataToMemory(device, ssbo, storageBufferSet.memory[i], objIndex * sizeof(SSBOType)); 531 } 532 } 533 487 534 // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model 488 535 // and to change the model matrix later by setting model_transform and then calling updateObject() 489 // Figure out a better way to allow the model matrix to be set during object ingcreation536 // Figure out a better way to allow the model matrix to be set during object creation 490 537 491 538 // TODO: Maybe return a reference to the object from this method if I decide that updating it … … 494 541 // to account for scaling 495 542 template<class VertexType, class SSBOType> 496 SceneObject<VertexType, SSBOType>& VulkanGame::addObject( 497 vector<SceneObject<VertexType, SSBOType>>& objects, 498 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 499 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo, 500 bool pipelinesCreated) { 543 SceneObject<VertexType, SSBOType>& VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects, 544 GraphicsPipeline_Vulkan<VertexType>& pipeline, 545 const vector<VertexType>& vertices, vector<uint16_t> indices, 546 SSBOType ssbo, bool pipelinesCreated) { 501 547 // TODO: Use the model field of ssbo to set the object's model_base 502 548 // currently, the passed in model is useless since it gets overridden in updateObject() anyway … … 518 564 } 519 565 520 bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo, 521 resourceCommandPool, graphicsQueue); 566 pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue); 567 568 bool resizeStorageBuffer = pipeline.numObjects == pipeline.objectCapacity; 569 570 if (resizeStorageBuffer) { 571 resizeStorageBufferSet<VertexType, SSBOType>(pipeline.storageBufferSet, resourceCommandPool, graphicsQueue, pipeline); 572 pipeline.cleanup(); 573 574 // Assume the SSBO is always the 2nd binding 575 pipeline.updateDescriptorInfo(1, &pipeline.storageBufferSet.infoSet); 576 } 577 578 pipeline.numObjects++; 579 580 updateStorageBuffer(pipeline.storageBufferSet, pipeline.numObjects - 1, obj.ssbo); 581 582 // TODO: Figure out why I am destroying and recreating the ubos when the swap chain is recreated, 583 // but am reusing the same ssbos. Maybe I don't need to recreate the ubos. 522 584 523 585 if (pipelinesCreated) { … … 532 594 // Refactor the logic to check for any resized SSBOs after all objects for the frame 533 595 // are created and then recreate each of the corresponding pipelines only once per frame 534 if (storageBufferResized) { 596 597 // TODO: Also, verify if I actually need to recreate all of these, or maybe just the descriptor sets, for instance 598 599 if (resizeStorageBuffer) { 535 600 pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile); 536 601 pipeline.createDescriptorPool(swapChainImages); … … 628 693 template<class VertexType, class SSBOType> 629 694 void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, 630 GraphicsPipeline_Vulkan<VertexType , SSBOType>& pipeline, size_t index) {695 GraphicsPipeline_Vulkan<VertexType>& pipeline, size_t index) { 631 696 SceneObject<VertexType, SSBOType>& obj = objects[index]; 632 697 … … 634 699 obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 635 700 636 pipeline.updateObject(index, obj.ssbo);701 updateStorageBuffer(pipeline.storageBufferSet, index, obj.ssbo); 637 702 638 703 obj.modified = false; … … 640 705 641 706 template<class VertexType, class SSBOType> 642 void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType , SSBOType>& pipeline,707 void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline, 643 708 SceneObject<VertexType, SSBOType>& obj, size_t index) { 644 709 pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
Note:
See TracChangeset
for help on using the changeset viewer.