Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vulkan_object_manager.hpp « vulkan « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de76b97583f4ca9d0ba2393a0f663b384b46b3b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once

#include "drape/pointers.hpp"
#include "drape/vulkan/vulkan_gpu_program.hpp"
#include "drape/vulkan/vulkan_memory_manager.hpp"
#include "drape/vulkan/vulkan_param_descriptor.hpp"
#include "drape/vulkan/vulkan_utils.hpp"

#include "base/assert.hpp"

#include <vulkan_wrapper.h>
#include <vulkan/vulkan.h>

#include <array>
#include <cstdint>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

namespace dp
{
namespace vulkan
{
struct VulkanObject
{
  VkBuffer m_buffer = {};
  VkImage m_image = {};
  VkImageView m_imageView = {};
  VulkanMemoryManager::AllocationPtr m_allocation;

  VkDeviceMemory GetMemory() const
  {
    ASSERT(m_allocation != nullptr, ());
    ASSERT(m_allocation->m_memoryBlock != nullptr, ());
    return m_allocation->m_memoryBlock->m_memory;
  }

  uint32_t GetAlignedOffset() const
  {
    ASSERT(m_allocation != nullptr, ());
    return m_allocation->m_alignedOffset;
  }

  uint32_t GetAlignedSize() const
  {
    ASSERT(m_allocation != nullptr, ());
    return m_allocation->m_alignedSize;
  }
};

class VulkanStagingBuffer;

class VulkanObjectManager
{
public:
  VulkanObjectManager(VkDevice device, VkPhysicalDeviceLimits const & deviceLimits,
                      VkPhysicalDeviceMemoryProperties const & memoryProperties,
                      uint32_t queueFamilyIndex);
  ~VulkanObjectManager();

  enum ThreadType
  {
    Frontend = 0,
    Backend,
    Other,
    Count
  };
  void RegisterThread(ThreadType type);

  void SetCurrentInflightFrameIndex(uint32_t index);

  VulkanObject CreateBuffer(VulkanMemoryManager::ResourceType resourceType,
                            uint32_t sizeInBytes, uint64_t batcherHash);
  VulkanObject CreateImage(VkImageUsageFlags usageFlags, VkFormat format, VkImageTiling tiling,
                           VkImageAspectFlags aspectFlags, uint32_t width, uint32_t height);
  DescriptorSetGroup CreateDescriptorSetGroup(ref_ptr<VulkanGpuProgram> program);

  // Use unsafe function ONLY if an object exists on the only thread, otherwise
  // use safe Fill function.
  uint8_t * MapUnsafe(VulkanObject object);
  void FlushUnsafe(VulkanObject object, uint32_t offset = 0, uint32_t size = 0);
  void UnmapUnsafe(VulkanObject object);
  void Fill(VulkanObject object, void const * data, uint32_t sizeInBytes);

  void DestroyObject(VulkanObject object);
  void DestroyDescriptorSetGroup(DescriptorSetGroup group);
  void CollectDescriptorSetGroups(uint32_t inflightFrameIndex);
  void CollectObjects(uint32_t inflightFrameIndex);

  VkDevice GetDevice() const { return m_device; }
  VulkanMemoryManager const & GetMemoryManager() const { return m_memoryManager; };
  VkSampler GetSampler(SamplerKey const & key);

private:
  using DescriptorSetGroupArray = std::vector<DescriptorSetGroup>;
  using VulkanObjectArray = std::vector<VulkanObject>;

  void CreateDescriptorPool();
  void DestroyDescriptorPools();
  void CollectObjectsForThread(VulkanObjectArray & objects);
  void CollectObjectsImpl(VulkanObjectArray const & objects);
  void CollectDescriptorSetGroupsUnsafe(DescriptorSetGroupArray & descriptors);

  VkDevice const m_device;
  uint32_t const m_queueFamilyIndex;
  VulkanMemoryManager m_memoryManager;

  std::array<std::thread::id, ThreadType::Count> m_renderers = {};
  std::array<std::array<VulkanObjectArray, kMaxInflightFrames>, ThreadType::Count> m_queuesToDestroy = {};

  std::vector<VkDescriptorPool> m_descriptorPools;

  std::array<DescriptorSetGroupArray, kMaxInflightFrames> m_descriptorsToDestroy;

  std::map<SamplerKey, VkSampler> m_samplers;

  uint32_t m_currentInflightFrameIndex = 0;

  std::mutex m_mutex;
  std::mutex m_samplerMutex;
  std::mutex m_destroyMutex;
};
}  // namespace vulkan
}  // namespace dp