SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Renderer2D.h
1#pragma once
2
3#include "spark/math/Vector2.h"
4#include "spark/render/CommandBuffer.h"
5#include "spark/render/Scissor.h"
6#include "spark/render/Viewport.h"
7
8#include "glm/matrix.hpp"
9
10#include <memory>
11#include <span>
12#include <string>
13
14namespace spark::core
15{
20 template <typename Backend>
22 {
23 friend class Window;
24
25 public:
26 using backend_type = Backend;
27 using device_type = typename backend_type::device_type;
28 using buffer_type = typename device_type::buffer_type;
29 using command_buffer_type = typename device_type::command_buffer_type;
30 using descriptor_set_type = typename command_buffer_type::descriptor_set_type;
31 using adapter_type = typename device_type::adapter_type;
32 using surface_type = typename backend_type::surface_type;
33 using render_pass_type = typename backend_type::render_pass_type;
34 using render_pipeline_type = typename backend_type::render_pipeline_type;
35 using pipeline_layout_type = typename backend_type::pipeline_layout_type;
36 using shader_program_type = typename backend_type::shader_program_type;
37 using shader_module_type = typename shader_program_type::shader_module_type;
38 using input_assembler_type = typename backend_type::input_assembler_type;
39 using rasterizer_type = typename backend_type::rasterizer_type;
40 using factory_type = typename device_type::factory_type;
41 using vertex_buffer_layout_type = typename factory_type::vertex_buffer_layout_type;
42 using vertex_buffer_type = typename factory_type::vertex_buffer_type;
43 using index_buffer_layout_type = typename factory_type::index_buffer_layout_type;
44 using index_buffer_type = typename factory_type::index_buffer_type;
45
46 public:
53 explicit Renderer2D(const math::Vector2<unsigned>& render_area,
54 std::function<typename surface_type::handle_type(const typename backend_type::handle_type&)> surface_factory,
55 std::span<std::string> required_extensions);
56
58
59 Renderer2D(const Renderer2D& other) = delete;
60 Renderer2D(Renderer2D&& other) noexcept = delete;
61 Renderer2D& operator=(const Renderer2D& other) = delete;
62 Renderer2D& operator=(Renderer2D&& other) noexcept = delete;
63
68 void recreateSwapChain(const math::Vector2<unsigned>& new_size);
69
73 void render();
74
80 void drawQuad(const glm::mat4& transform_matrix, const spark::math::Vector4<float>& color = {1.f, 1.f, 1.f, 1.f});
81
92 void drawCircle(const glm::mat4& transform_matrix, float radius, const spark::math::Vector4<float>& color = {1.f, 1.f, 1.f, 1.f});
93
94 private:
98 void initRenderGraph();
99
104 void updateCamera(const render::ICommandBuffer& command_buffer);
105
109 void upload();
110
111 private:
112 inline static constexpr std::array s_rectangleVertices = {
113 glm::vec3(-0.5f, -0.5f, 0.f),
114 glm::vec3(-0.5f, 0.5f, 0.f),
115 glm::vec3(0.5f, 0.5f, 0.f),
116 glm::vec3(0.5f, -0.5f, 0.f)
117 };
118
119 inline static constexpr std::array<uint16_t, 6> s_rectangleIndices = {0, 1, 2, 0, 2, 3};
120
121 device_type* m_device;
122 std::unique_ptr<backend_type> m_renderBackend;
123 std::shared_ptr<input_assembler_type> m_inputAssembler;
124 std::unique_ptr<render::IViewport> m_viewport;
125 std::unique_ptr<render::IScissor> m_scissor;
126 std::vector<std::size_t> m_transferFences;
127
128 SPARK_WARNING_PUSH
129 SPARK_DISABLE_MSVC_WARNING(4324) // 'InstanceBuffer': structure was padded due to alignment specifier. This is intended to align the CPU buffer to GPU one.
130
131 struct alignas(sizeof(glm::vec4)) InstanceBuffer
132 {
133 glm::mat4 transform;
134 glm::vec4 color;
135 float radius;
136 };
137
138 SPARK_WARNING_POP
139
140 inline static constexpr std::size_t s_maxInstances = 1024 * 8;
141 std::vector<InstanceBuffer> m_instanceData;
142 };
143}
144
145#include "spark/core/impl/Renderer2D.h"
An object that can render 2D graphics on a surface.
Definition Renderer2D.h:22
void render()
Draws the current frame.
Definition Renderer2D.h:239
void drawQuad(const glm::mat4 &transform_matrix, const spark::math::Vector4< float > &color={1.f, 1.f, 1.f, 1.f})
Draws a 1x1 quad with the given transformation_matrix.
Definition Renderer2D.h:286
void drawCircle(const glm::mat4 &transform_matrix, float radius, const spark::math::Vector4< float > &color={1.f, 1.f, 1.f, 1.f})
Draws a circle with the given transformation_matrix and radius.
Definition Renderer2D.h:295
void recreateSwapChain(const math::Vector2< unsigned > &new_size)
Recreates the swap chain with frame buffers of the new size.
Definition Renderer2D.h:125
Renderer2D(const math::Vector2< unsigned > &render_area, std::function< typename surface_type::handle_type(const typename backend_type::handle_type &)> surface_factory, std::span< std::string > required_extensions)
Creates a new 2D renderer.
Definition Renderer2D.h:24
An interface representing any window of any platform.
Definition Window.h:19
A vector with two components.
Definition Vector2.h:13
A vector with four components.
Definition Vector4.h:13
Interface for a command buffer.
Definition CommandBuffer.h:24