SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Backend.h
1#pragma once
2
3#include "spark/render/Export.h"
4#include "spark/render/GraphicsAdapter.h"
5#include "spark/render/GraphicsDevice.h"
6
7#include <optional>
8
9namespace spark::core
10{
11 class Application;
12}
13
14namespace spark::render
15{
19 enum class BackendType
20 {
21 Rendering = 0x01,
22 Other = 0xFF
23 };
24
28 class SPARK_RENDER_EXPORT IBackend
29 {
30 friend class spark::core::Application;
31
32 public:
33 virtual ~IBackend() noexcept = default;
34
39 [[nodiscard]] virtual BackendType type() const noexcept = 0;
40 };
41
45 class SPARK_RENDER_EXPORT IRenderBackend : public IBackend
46 {
47 public:
48 ~IRenderBackend() noexcept override = default;
49
51 [[nodiscard]] BackendType type() const noexcept override { return BackendType::Rendering; }
52
57 [[nodiscard]] std::vector<const IGraphicsAdapter*> adapters() const noexcept { return genericAdapters(); }
58
64 SPARK_SUPPRESS_MSVC_WARNING(4324) // 'std::optional': structure was padded due to alignment specifier
65 [[nodiscard]] virtual const IGraphicsAdapter* findAdapter(const std::optional<lib::Uuid>& id = std::nullopt) const = 0;
66
72 [[nodiscard]] virtual IGraphicsDevice* device(const std::string& name) noexcept = 0;
73
75 [[nodiscard]] virtual const IGraphicsDevice* device(const std::string& name) const noexcept = 0;
76
77 private:
79 [[nodiscard]] virtual std::vector<const IGraphicsAdapter*> genericAdapters() const noexcept = 0;
80 };
81
86 template <typename GraphicsDeviceType>
88 {
89 public:
90 using device_type = GraphicsDeviceType;
91 using surface_type = typename device_type::surface_type;
92 using adapter_type = typename device_type::adapter_type;
93 using swap_chain_type = typename device_type::swap_chain_type;
94 using command_queue_type = typename device_type::command_queue_type;
95 using command_buffer_type = typename device_type::command_buffer_type;
96 using frame_buffer_type = typename device_type::frame_buffer_type;
97 using render_pass_type = typename device_type::render_pass_type;
98 using pipeline_layout_type = typename device_type::pipeline_layout_type;
99 using render_pipeline_type = typename device_type::render_pipeline_type;
100 using shader_program_type = typename device_type::shader_program_type;
101 using input_assembler_type = typename device_type::input_assembler_type;
102 using rasterizer_type = typename device_type::rasterizer_type;
103
104 public:
105 ~RenderBackend() noexcept override = default;
106
107 [[nodiscard]] virtual std::vector<const adapter_type*> adapters() const noexcept = 0;
108
110 [[nodiscard]] const adapter_type* findAdapter(const std::optional<lib::Uuid>& id = std::nullopt) const override = 0;
111
113 [[nodiscard]] device_type* device(const std::string& name) noexcept override = 0;
114
116 [[nodiscard]] const device_type* device(const std::string& name) const noexcept override = 0;
117
124 virtual void registerDevice(std::string name, std::unique_ptr<device_type>&& device) = 0;
125
130 virtual void releaseDevice(const std::string& name) = 0;
131
142 template <typename Self, typename... Args>
143 device_type* createDevice(std::string name, const adapter_type& adapter, std::unique_ptr<surface_type>&& surface, Args&&... args)
144 {
145 auto device = std::make_unique<device_type>(adapter, std::move(surface), std::forward<Args>(args)...);
146 auto* ptr = device.get();
147 static_cast<Self*>(this)->registerDevice(name, std::move(device));
148 return ptr;
149 }
150
151 private:
152 [[nodiscard]] std::vector<const IGraphicsAdapter*> genericAdapters() const noexcept final
153 {
154 auto tmp = adapters();
155 std::vector<const IGraphicsAdapter*> result;
156 result.reserve(tmp.size());
157 std::ranges::transform(tmp, std::back_inserter(result), [](const auto& adapter) { return static_cast<const IGraphicsAdapter*>(adapter); });
158 return result;
159 }
160 };
161}
A class representing a SPARK application.
Definition Application.h:19
The base class for a backend.
Definition Backend.h:29
virtual BackendType type() const noexcept=0
Gets the type of the backend.
Represents a physical graphics adapter.
Definition GraphicsAdapter.h:37
Interface for a graphics device.
Definition GraphicsDevice.h:18
Describes a rendering backend. (backend with type BackendType::Rendering)
Definition Backend.h:46
virtual const IGraphicsAdapter * findAdapter(const std::optional< lib::Uuid > &id=std::nullopt) const =0
Finds a graphics adapter by unique id.
virtual IGraphicsDevice * device(const std::string &name) noexcept=0
Searches for a graphics device by name.
virtual const IGraphicsDevice * device(const std::string &name) const noexcept=0
Searches for a graphics device by name.
BackendType type() const noexcept override
Gets the type of the backend.
Definition Backend.h:51
std::vector< const IGraphicsAdapter * > adapters() const noexcept
Lists all available graphics adapters.
Definition Backend.h:57
Defines a back-end, that provides a device instance for a certain surface and graphics adapter.
Definition Backend.h:88
device_type * device(const std::string &name) noexcept override=0
Searches for a graphics device by name.
const adapter_type * findAdapter(const std::optional< lib::Uuid > &id=std::nullopt) const override=0
Finds a graphics adapter by unique id.
const device_type * device(const std::string &name) const noexcept override=0
Searches for a graphics device by name.
device_type * createDevice(std::string name, const adapter_type &adapter, std::unique_ptr< surface_type > &&surface, Args &&... args)
Creates a new graphics device.
Definition Backend.h:143
virtual void releaseDevice(const std::string &name)=0
Unregisters a graphics device.
virtual void registerDevice(std::string name, std::unique_ptr< device_type > &&device)=0
Registers a new graphics device.