Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
GraphicsObjectManager.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "DescriptorBuilder.hpp"
3 #include "GraphicsTypes.hpp"
4 #include "Mesh.hpp"
5 
6 #include <array>
7 #include <mutex>
8 #include <optional>
9 #include <shared_mutex>
10 #include <unordered_map>
11 #include <utility>
12 #include <vector>
13 
18 {
19 public:
30  GraphicsObjectManager(VkInstance instance, VkPhysicalDevice gpu, VkDevice logDevice, VkQueue graphicsQueue, std::shared_mutex& graphicsMutex, uint32_t graphicsQueueFamily, const uint32_t frameCount);
31 
36 
42  void addTransferQueueFamilyAccess(const uint32_t transferQueueFamily, VkQueue transferQueue);
43 
55  bool addCharacter(int characterID, const std::vector<CharacterVertex>& vertices, const std::vector<uint32_t>& indices, ImageData& texture, glm::mat4 transformation, glm::mat4 bindPose[MAX_BONES], VkSampler imgSampler, PhongMaterial material = PhongMaterial::getDefaultMaterial());
56 
67  bool addGameObject(int objectId, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices, ImageData& texture, glm::mat4 transformation, VkSampler imgSampler, PhongMaterial material = PhongMaterial::getDefaultMaterial());
68 
77  bool addTestObject(int id, const std::vector<SimpleVertex>& vertices, const std::vector<uint32_t>& indices, glm::mat4 transformation);
78 
86  bool addArea(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices, std::array<ImageData, 6> textures, VkSampler texSampler);
87 
94  bool cleanResources(int id, ObjectType type);
95 
102  void updateTestObject(int id, const void* srcData, uint32_t frame);
103 
110  void updateObjectDescriptor(int id, const void* srcData, uint32_t frame);
111 
119  void updateCharacterDescriptor(int id, const void* transformSrcData, const void* boneSrcData, uint32_t frame);
120 
126  VkDescriptorSet getSunDescriptorSet(uint32_t frame) const;
127 
132  VkDescriptorSetLayout getSunLayout() const;
133 
139  void updateSunDescriptor(const void* srcData, uint32_t frame);
140 
146  void updateCameraDescriptor(const void* srcData, uint32_t frame);
147 
155  AllocatedImage createDepthImage(uint32_t width, uint32_t height, VkFormat depthFormat);
156 
161  void destroyImage(AllocatedImage image);
162 
170  void transitionDepthImageLayout(AllocatedImage depthImage, VkFormat depthFormat, VkImageLayout srcLayout, VkImageLayout dstLayout);
171 
178  VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT);
179 
185  CombinedImage createCombinedImage(uint32_t width, uint32_t height, VkFormat format);
186 
192  void copyBuffer(AllocatedBuffer allocBuffer, const void* srcData);
193 
199  void copyImage(AllocatedImage allocImage, const ImageData& srcData);
200 
206  VkDescriptorSetLayout getLayout(ObjectType type) const;
207 
213  uint32_t getLayoutCount(ObjectType type) const;
214 
220  std::vector<DrawableObject> getDrawableObjects(ObjectType type, uint32_t frame) const;
221 
222 private:
226  struct CommandUnit
227  {
228  VkQueue queue;
229  VkCommandPool commandPool;
230  uint32_t queueFamilyIndex;
231  VkCommandBuffer startCommandBuffer(VkDevice logDevice);
232  bool endCommandBuffer(VkDevice logDevice, VkCommandBuffer commandBuffer, VkFence fence = VK_NULL_HANDLE);
233  };
234 
246  AllocatedBuffer createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaAllocationInfo& info, VmaMemoryUsage memUsage = VMA_MEMORY_USAGE_AUTO, VmaAllocationCreateFlags vmaCreationFlags = 0, VkMemoryPropertyFlags requiredFlags = 0, VkMemoryPropertyFlags preferredFlags = 0);
247  AllocatedBuffer createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaMemoryUsage memUsage = VMA_MEMORY_USAGE_AUTO, VmaAllocationCreateFlags vmaCreationFlags = 0, VkMemoryPropertyFlags requiredFlags = 0, VkMemoryPropertyFlags preferredFlags = 0);
248 
264  AllocatedImage createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL, VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VmaMemoryUsage memUsage = VMA_MEMORY_USAGE_AUTO, VmaAllocationCreateFlags vmaCreationFlags = 0, VkMemoryPropertyFlags requiredFlags = 0, VkMemoryPropertyFlags preferredFlags = 0);
265 
270 
275  uint32_t getQueueFamilyCount();
276 
283  VkSharingMode getSharingMode();
284 
290  VkCommandBuffer startCommandBuffer(bool preferTransfer);
291 
298  bool endCommandBuffer(VkCommandBuffer commandBuffer, VkFence fence = VK_NULL_HANDLE);
299 
309  void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, VkCommandBuffer commandBuffer, VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT, std::pair<uint32_t, uint32_t> srcAndDstQueueFamilies = {VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED});
310 
315  std::pair<uint32_t, uint32_t> getQueueTransitionFamilies();
316 
325  bool getPipelineAndAccessFlags(VkImageLayout srcLayout, VkImageLayout dstLayout, std::pair<VkAccessFlags, VkAccessFlags>& srcAndDstAccessMasks, std::pair<VkPipelineStageFlags, VkPipelineStageFlags>& srcAndDstStages);
326 
333  AllocatedBuffer createStagingBuffer(VkDeviceSize size, VmaAllocationInfo& allocInfo);
334 
338  void createSun();
339 
340  // Required Vulkan handles
341  VkDevice logDeviceM;
342  VmaAllocator allocatorM;
343  VkDeviceSize minOffsetM;
344 
345  std::shared_mutex& graphicsQueueMutexM;
347  std::optional<CommandUnit> transferCommandUnitM;
348  // Todo make a better system
349  std::optional<CommandUnit> activeCommandUnitM;
350  // Builder
351  std::unique_ptr<DescriptorBuilder> pBuilderM;
352  // Data maps
353  std::unordered_map<int, CharacterObject> characterObjectsM;
354  std::unordered_map<int, GameObject> gameObjectsM;
355  std::unordered_map<int, TestObject> testObjectsM;
356  std::vector<AreaObject> areaObjectsM;
358  VkDescriptorSetLayout sunLayoutM;
359 
360  std::vector<CombinedImage> allocatedImagesM;
361 
362  // Frame count
363  uint32_t frameCountM;
364  // Descriptor set layouts for common objects
365  std::array<VkDescriptorSetLayout, OBJECT_TYPE_COUNT> descriptorSetLayoutsM;
366 };
ObjectType
Definition: BasicTypes.hpp:29
constexpr uint32_t MAX_BONES
Definition: BasicTypes.hpp:18
Graphics object manager manages different types of objects that have memory and can be drawn.
Definition: GraphicsObjectManager.hpp:18
AllocatedImage createDepthImage(uint32_t width, uint32_t height, VkFormat depthFormat)
Creates a depth image.
Definition: GraphicsObjectManager.cpp:807
void updateCameraDescriptor(const void *srcData, uint32_t frame)
Updates the camera descriptor with the given data.
Definition: GraphicsObjectManager.cpp:798
bool cleanResources(int id, ObjectType type)
Cleans resources of the given id, if they exists.
Definition: GraphicsObjectManager.cpp:683
VkDescriptorSetLayout sunLayoutM
Definition: GraphicsObjectManager.hpp:358
std::vector< AreaObject > areaObjectsM
Definition: GraphicsObjectManager.hpp:356
AllocatedBuffer createStagingBuffer(VkDeviceSize size, VmaAllocationInfo &allocInfo)
Helper for creating a staging buffer.
Definition: GraphicsObjectManager.cpp:551
~GraphicsObjectManager()
Destructor.
Definition: GraphicsObjectManager.cpp:53
bool getPipelineAndAccessFlags(VkImageLayout srcLayout, VkImageLayout dstLayout, std::pair< VkAccessFlags, VkAccessFlags > &srcAndDstAccessMasks, std::pair< VkPipelineStageFlags, VkPipelineStageFlags > &srcAndDstStages)
Fills access masks and pipeline stages when given source and destination layouts for an image.
Definition: GraphicsObjectManager.cpp:504
void copyBuffer(AllocatedBuffer allocBuffer, const void *srcData)
Copies the given data to the given buffer.
Definition: GraphicsObjectManager.cpp:856
std::vector< DrawableObject > getDrawableObjects(ObjectType type, uint32_t frame) const
Returns drawable objects of the given type.
Definition: GraphicsObjectManager.cpp:991
uint32_t getLayoutCount(ObjectType type) const
Returns the number of descriptor layouts of the given type. Currently not a very useful function....
Definition: GraphicsObjectManager.cpp:979
VkSharingMode getSharingMode()
Returns the sharing mode of created resources.
Definition: GraphicsObjectManager.cpp:419
bool addArea(const std::vector< Vertex > &vertices, const std::vector< uint32_t > &indices, std::array< ImageData, 6 > textures, VkSampler texSampler)
Adds an area object to the buffer manager.
Definition: GraphicsObjectManager.cpp:651
uint32_t frameCountM
Definition: GraphicsObjectManager.hpp:363
void updateSunDescriptor(const void *srcData, uint32_t frame)
Updates the sun descriptor with the given data.
Definition: GraphicsObjectManager.cpp:792
std::shared_mutex & graphicsQueueMutexM
Definition: GraphicsObjectManager.hpp:345
std::unordered_map< int, GameObject > gameObjectsM
Definition: GraphicsObjectManager.hpp:354
std::pair< uint32_t, uint32_t > getQueueTransitionFamilies()
Queue families should be transferred, if the resources are using multiple queues, and the sharingmode...
Definition: GraphicsObjectManager.cpp:495
LightObject sunM
Definition: GraphicsObjectManager.hpp:357
uint32_t getQueueFamilyCount()
Returns number of queue families.
Definition: GraphicsObjectManager.cpp:409
std::optional< CommandUnit > transferCommandUnitM
Definition: GraphicsObjectManager.hpp:347
void createSun()
Creates necessary memory allocations and mappings for a sun.
Definition: GraphicsObjectManager.cpp:564
VkDevice logDeviceM
Definition: GraphicsObjectManager.hpp:341
void createDescriptorBuilder()
Initializes the descriptorBuilder member.
Definition: GraphicsObjectManager.cpp:402
VmaAllocator allocatorM
Definition: GraphicsObjectManager.hpp:342
std::vector< CombinedImage > allocatedImagesM
Definition: GraphicsObjectManager.hpp:360
VkCommandBuffer startCommandBuffer(bool preferTransfer)
Allocates and begins a command buffer.
Definition: GraphicsObjectManager.cpp:433
AllocatedImage createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling=VK_IMAGE_TILING_OPTIMAL, VkImageUsageFlags usage=VK_IMAGE_USAGE_TRANSFER_DST_BIT|VK_IMAGE_USAGE_SAMPLED_BIT, VmaMemoryUsage memUsage=VMA_MEMORY_USAGE_AUTO, VmaAllocationCreateFlags vmaCreationFlags=0, VkMemoryPropertyFlags requiredFlags=0, VkMemoryPropertyFlags preferredFlags=0)
Creates an allocated image. The standard parameters make it easy to create a texture image on the GPU...
Definition: GraphicsObjectManager.cpp:360
VkDeviceSize minOffsetM
Minimum offset alignment for uniform buffers.
Definition: GraphicsObjectManager.hpp:343
void addTransferQueueFamilyAccess(const uint32_t transferQueueFamily, VkQueue transferQueue)
Adds the given transfer queue to the manager.
Definition: GraphicsObjectManager.cpp:118
VkDescriptorSetLayout getSunLayout() const
Returns the descriptor set layout of the sun.
Definition: GraphicsObjectManager.cpp:787
VkDescriptorSetLayout getLayout(ObjectType type) const
Returns the descriptor set layout for the given object type.
Definition: GraphicsObjectManager.cpp:967
std::optional< CommandUnit > activeCommandUnitM
Definition: GraphicsObjectManager.hpp:349
void destroyImage(AllocatedImage image)
Interface for destroying an image That is owned by the user of this class.
Definition: GraphicsObjectManager.cpp:814
void updateObjectDescriptor(int id, const void *srcData, uint32_t frame)
Updates the object descriptor with the given data.
Definition: GraphicsObjectManager.cpp:762
bool endCommandBuffer(VkCommandBuffer commandBuffer, VkFence fence=VK_NULL_HANDLE)
Submits the given command buffer to be executed on the gpu.
Definition: GraphicsObjectManager.cpp:450
bool addTestObject(int id, const std::vector< SimpleVertex > &vertices, const std::vector< uint32_t > &indices, glm::mat4 transformation)
Adds a test object to the buffer manager.
Definition: GraphicsObjectManager.cpp:601
CombinedImage createCombinedImage(uint32_t width, uint32_t height, VkFormat format)
Creates an image for the given data and stores the memory to this class.
Definition: GraphicsObjectManager.cpp:847
void updateCharacterDescriptor(int id, const void *transformSrcData, const void *boneSrcData, uint32_t frame)
Definition: GraphicsObjectManager.cpp:769
std::unordered_map< int, TestObject > testObjectsM
Definition: GraphicsObjectManager.hpp:355
VkDescriptorSet getSunDescriptorSet(uint32_t frame) const
Returns the descriptor set of the sun for the given frame.
Definition: GraphicsObjectManager.cpp:778
void transitionDepthImageLayout(AllocatedImage depthImage, VkFormat depthFormat, VkImageLayout srcLayout, VkImageLayout dstLayout)
Transitions the given image to the given layout using graphics queue.
Definition: GraphicsObjectManager.cpp:819
void copyImage(AllocatedImage allocImage, const ImageData &srcData)
Copies the given data to the given image.
Definition: GraphicsObjectManager.cpp:898
VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags=VK_IMAGE_ASPECT_COLOR_BIT)
Creates an image view for the given image and format.
Definition: GraphicsObjectManager.cpp:826
std::unique_ptr< DescriptorBuilder > pBuilderM
Definition: GraphicsObjectManager.hpp:351
std::unordered_map< int, CharacterObject > characterObjectsM
Definition: GraphicsObjectManager.hpp:353
AllocatedBuffer createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaAllocationInfo &info, VmaMemoryUsage memUsage=VMA_MEMORY_USAGE_AUTO, VmaAllocationCreateFlags vmaCreationFlags=0, VkMemoryPropertyFlags requiredFlags=0, VkMemoryPropertyFlags preferredFlags=0)
Creates a buffer with the given parameters.
Definition: GraphicsObjectManager.cpp:288
bool addGameObject(int objectId, const std::vector< Vertex > &vertices, const std::vector< uint32_t > &indices, ImageData &texture, glm::mat4 transformation, VkSampler imgSampler, PhongMaterial material=PhongMaterial::getDefaultMaterial())
Adds a game object to the buffer manager.
Definition: GraphicsObjectManager.cpp:217
GraphicsObjectManager(VkInstance instance, VkPhysicalDevice gpu, VkDevice logDevice, VkQueue graphicsQueue, std::shared_mutex &graphicsMutex, uint32_t graphicsQueueFamily, const uint32_t frameCount)
Creates a graphics object manager.
Definition: GraphicsObjectManager.cpp:11
std::array< VkDescriptorSetLayout, OBJECT_TYPE_COUNT > descriptorSetLayoutsM
Definition: GraphicsObjectManager.hpp:365
bool addCharacter(int characterID, const std::vector< CharacterVertex > &vertices, const std::vector< uint32_t > &indices, ImageData &texture, glm::mat4 transformation, glm::mat4 bindPose[MAX_BONES], VkSampler imgSampler, PhongMaterial material=PhongMaterial::getDefaultMaterial())
Adds a character to the buffer manager with the given parameters.
Definition: GraphicsObjectManager.cpp:140
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, VkCommandBuffer commandBuffer, VkImageAspectFlags aspectFlags=VK_IMAGE_ASPECT_COLOR_BIT, std::pair< uint32_t, uint32_t > srcAndDstQueueFamilies={VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED})
Records image layout transition barrier to the given command buffer.
Definition: GraphicsObjectManager.cpp:461
void updateTestObject(int id, const void *srcData, uint32_t frame)
Updates test object data.
Definition: GraphicsObjectManager.cpp:755
CommandUnit graphicsCommandUnitM
Definition: GraphicsObjectManager.hpp:346
Definition: GraphicsTypes.hpp:28
Definition: GraphicsTypes.hpp:15
Definition: GraphicsTypes.hpp:22
Command unit represents everything needed to allocate, submit, record and execute commands.
Definition: GraphicsObjectManager.hpp:227
VkCommandBuffer startCommandBuffer(VkDevice logDevice)
Definition: GraphicsObjectManager.cpp:1061
VkCommandPool commandPool
Command pool for the command unit.
Definition: GraphicsObjectManager.hpp:229
uint32_t queueFamilyIndex
Queue family index of the command unit.
Definition: GraphicsObjectManager.hpp:230
bool endCommandBuffer(VkDevice logDevice, VkCommandBuffer commandBuffer, VkFence fence=VK_NULL_HANDLE)
Definition: GraphicsObjectManager.cpp:1087
VkQueue queue
Queue for the command unit.
Definition: GraphicsObjectManager.hpp:228
Definition: BasicTypes.hpp:161
Simple light object that contains a list of uniform buffers.
Definition: GraphicsTypes.hpp:109
Definition: BasicTypes.hpp:135
static PhongMaterial getDefaultMaterial()
Definition: GraphicsTypes.cpp:346