Lugdunum  0.1.0
Renderer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstring>
4 #include <memory>
5 #include <unordered_map>
6 
16 
17 namespace lug {
18 namespace Graphics {
19 namespace Vulkan {
20 
22 public:
23  struct Requirements {
24  const std::vector<const char*> mandatoryInstanceExtensions;
25  const std::vector<const char*> mandatoryInstanceLayers;
26  const std::vector<const char*> mandatoryDeviceExtensions;
27 
28  const std::vector<const char*> optionalInstanceExtensions;
29  const std::vector<const char*> optionalInstanceLayers;
30  const std::vector<const char*> optionalDeviceExtensions;
31 
32  const VkPhysicalDeviceFeatures mandatoryFeatures;
33  const VkPhysicalDeviceFeatures optionalFeatures;
34 
35  // TODO: PhysicalDeviceLimits / PhysicalDeviceSparseProperties
36  // TODO: Memory properties
37  // TODO: Better support for queues
38  };
39 
40  struct Preferences {
42 
43  struct Swapchain {
44  std::vector<VkPresentModeKHR> presentModes; // By order of preference
45  std::vector<VkFormat> formats; // By order of preference
46  std::vector<VkCompositeAlphaFlagBitsKHR> compositeAlphas; // By order of preference
47  } swapchain;
48  };
49 
50 public:
51  explicit Renderer(Graphics& graphics);
52 
53  Renderer(const Renderer&) = delete;
54  Renderer(Renderer&&) = delete;
55 
56  Renderer& operator=(const Renderer&) = delete;
57  Renderer& operator=(Renderer&&) = delete;
58 
59  ~Renderer();
60 
61  bool beginInit(const std::string& appName, const Core::Version& appVersion, const Renderer::InitInfo& initInfo) override final;
62  bool finishInit() override final;
63 
64  bool isInstanceLayerLoaded(const char* name) const;
65  bool isInstanceExtensionLoaded(const char* name) const;
66  bool isDeviceExtensionLoaded(const char* name) const;
67 
68  ::lug::Graphics::Render::Window* createWindow(Render::Window::InitInfo& initInfo) override final;
69  ::lug::Graphics::Render::Window* getWindow() override final;
70 
71  const API::Instance& getInstance() const;
72  API::Device& getDevice();
73  const API::Device& getDevice() const;
74 
75  InstanceInfo& getInstanceInfo();
76  const InstanceInfo& getInstanceInfo() const;
77 
78  PhysicalDeviceInfo* getPhysicalDeviceInfo();
79  const PhysicalDeviceInfo* getPhysicalDeviceInfo() const;
80 
81  std::vector<PhysicalDeviceInfo>& getPhysicalDeviceInfos();
82  const std::vector<PhysicalDeviceInfo>& getPhysicalDeviceInfos() const;
83 
84  Preferences& getPreferences();
85  const Preferences& getPreferences() const;
86 
87  void addPipeline(Resource::SharedPtr<Render::Pipeline> pipeline);
88  bool containsPipeline(Render::Pipeline::Id id) const;
89  Resource::SharedPtr<Render::Pipeline> getPipeline(Render::Pipeline::Id id);
90 
91  Render::Window* getRenderWindow() const;
92 
93  void destroy();
94 
95  bool beginFrame(const lug::System::Time& elapsedTime) override final;
96  bool endFrame() override final;
97 
98 private:
99  bool initInstance(const std::string& appName, const Core::Version& appVersion);
100  bool initDevice();
101 
102  bool checkRequirementsInstance(const std::set<Module::Type> &modulesToCheck);
103  bool checkRequirementsDevice(const PhysicalDeviceInfo& physicalDeviceInfo, const std::set<Module::Type> &modulesToCheck, bool finalization, bool quiet);
104 
105  template <typename Info>
106  std::vector<const char*> checkRequirementsLayers(const Info& info, const std::vector<const char*>& layers, std::vector<const char*>& layersFound);
107 
108  template <typename Info>
109  std::vector<const char*> checkRequirementsExtensions(const Info& info, const std::vector<const char*>& extensions, std::vector<const char*>& extensionsFound);
110 
111 private:
112  API::Loader _loader;
113 
114  API::Instance _instance{};
115  API::Device _device{};
116 
117  InstanceInfo _instanceInfo{};
118  PhysicalDeviceInfo* _physicalDeviceInfo{nullptr};
119  std::vector<PhysicalDeviceInfo> _physicalDeviceInfos{};
120 
121  VkDebugReportCallbackEXT _debugReportCallback{VK_NULL_HANDLE};
122 
123  std::unique_ptr<Render::Window> _window;
124 
125  std::vector<const char*> _loadedInstanceLayers{};
126  std::vector<const char*> _loadedInstanceExtensions{};
127  std::vector<const char*> _loadedDeviceExtensions{};
128  VkPhysicalDeviceFeatures _loadedDeviceFeatures{};
129  std::vector<Render::Mesh*> _attachedMeshes{};
130 
131  Preferences _preferences{
132  nullptr, // device
133 
134  { // swapchain
135  { // presentModes
136  VK_PRESENT_MODE_MAILBOX_KHR,
137  VK_PRESENT_MODE_FIFO_KHR
138  },
139  { // formats
140  VK_FORMAT_B8G8R8A8_UNORM,
141  VK_FORMAT_R8G8B8A8_UNORM
142  },
143  { // compositeAlphas
144  VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
145  VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
146  }
147  }
148  };
149 
150  std::unordered_map<Render::Pipeline::Id, Resource::WeakPtr<Render::Pipeline>> _pipelines;
151 
152 private:
153  static const std::unordered_map<Module::Type, Requirements> modulesRequirements;
154 };
155 
157 
158 } // Vulkan
159 } // Graphics
160 } // lug
Stores the version of the Application.
Definition: Version.hpp:9
Class for loading a type of file.
Definition: Loader.hpp:16
const std::vector< const char * > optionalInstanceExtensions
Definition: Renderer.hpp:28
const std::vector< const char * > mandatoryInstanceLayers
Definition: Renderer.hpp:25
const std::vector< const char * > optionalDeviceExtensions
Definition: Renderer.hpp:30
std::vector< VkCompositeAlphaFlagBitsKHR > compositeAlphas
Definition: Renderer.hpp:46
#define LUG_GRAPHICS_API
Definition: Export.hpp:11
static const std::unordered_map< Module::Type, Requirements > modulesRequirements
Definition: Renderer.hpp:153
const VkPhysicalDeviceFeatures mandatoryFeatures
Definition: Renderer.hpp:32
std::unordered_map< Render::Pipeline::Id, Resource::WeakPtr< Render::Pipeline > > _pipelines
Definition: Renderer.hpp:150
const std::vector< const char * > mandatoryDeviceExtensions
Definition: Renderer.hpp:26
Class for resource.
Definition: Resource.hpp:17
std::unique_ptr< Render::Window > _window
Definition: Renderer.hpp:123
const VkPhysicalDeviceFeatures optionalFeatures
Definition: Renderer.hpp:33
const std::vector< const char * > optionalInstanceLayers
Definition: Renderer.hpp:29
const std::vector< const char * > mandatoryInstanceExtensions
Definition: Renderer.hpp:24
std::vector< VkPresentModeKHR > presentModes
Definition: Renderer.hpp:44
Class for graphics.
Definition: Graphics.hpp:25