FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_renderTarget.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_GRAPHIC_C_RENDERTARGET_HPP_INCLUDED
18#define _FGE_GRAPHIC_C_RENDERTARGET_HPP_INCLUDED
19
20/*
21 * Original from : https://github.com/SFML/SFML
22 * Copyright (C) 2007-2022 Laurent Gomila
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "FastEngine/fge_extern.hpp"
28#include "FastEngine/C_rect.hpp"
29#include "FastEngine/C_vector.hpp"
30#include "FastEngine/graphic/C_color.hpp"
31#include "FastEngine/graphic/C_renderStates.hpp"
32#include "FastEngine/graphic/C_view.hpp"
33#include "FastEngine/manager/shader_manager.hpp"
34#include "FastEngine/vulkan/C_commandBuffer.hpp"
35#include "FastEngine/vulkan/C_contextAware.hpp"
36#include "FastEngine/vulkan/C_graphicPipeline.hpp"
37#include <unordered_map>
38
39#define FGE_RENDER_BAD_IMAGE_INDEX std::numeric_limits<uint32_t>::max()
40
41#define FGE_RENDER_DEFAULT_DESCRIPTOR_SET_TRANSFORM 0
42#define FGE_RENDER_DEFAULT_DESCRIPTOR_SET_TEXTURE 1
43
44#define FGE_RENDER_TIMEOUT_BLOCKING UINT64_MAX
45#define FGE_RENDER_NO_TIMEOUT 0
46
47namespace fge
48{
49
50class Texture;
51class Drawable;
52class Transformable;
53struct TransformUboData;
54
56{
57protected:
58 explicit RenderTarget(fge::vulkan::Context const& context);
59
60 void initialize();
61
62public:
63 RenderTarget(RenderTarget const& r);
64 RenderTarget(RenderTarget&& r) noexcept;
65 ~RenderTarget() override = default;
66
67 RenderTarget& operator=(RenderTarget const& r);
68 RenderTarget& operator=(RenderTarget&& r) noexcept;
69
70 void setClearColor(fge::Color const& color);
71 [[nodiscard]] fge::Color getClearColor() const;
72
73 void setView(View const& view);
74 [[nodiscard]] View const& getView() const;
75 [[nodiscard]] View const& getDefaultView() const;
76 [[nodiscard]] fge::vulkan::Viewport getViewport(View const& view) const;
77
78 [[nodiscard]] Vector2f mapFramebufferCoordsToViewSpace(Vector2i const& point) const;
79 [[nodiscard]] Vector2f mapFramebufferCoordsToViewSpace(Vector2i const& point, View const& view) const;
80 [[nodiscard]] Vector2f mapFramebufferCoordsToWorldSpace(Vector2i const& point) const;
81 [[nodiscard]] Vector2f mapFramebufferCoordsToWorldSpace(Vector2i const& point, View const& view) const;
82
83 [[nodiscard]] Vector2i mapViewCoordsToFramebufferSpace(Vector2f const& point) const;
84 [[nodiscard]] Vector2i mapViewCoordsToFramebufferSpace(Vector2f const& point, View const& view) const;
85 [[nodiscard]] Vector2i mapWorldCoordsToFramebufferSpace(Vector2f const& point) const;
86 [[nodiscard]] Vector2i mapWorldCoordsToFramebufferSpace(Vector2f const& point, View const& view) const;
87
88 [[nodiscard]] RectFloat mapFramebufferRectToViewSpace(RectInt const& rect) const;
89 [[nodiscard]] RectFloat mapFramebufferRectToViewSpace(RectInt const& rect, View const& view) const;
90 [[nodiscard]] RectFloat mapFramebufferRectToWorldSpace(RectInt const& rect) const;
91 [[nodiscard]] RectFloat mapFramebufferRectToWorldSpace(RectInt const& rect, View const& view) const;
92
93 [[nodiscard]] RectInt mapViewRectToFramebufferSpace(RectFloat const& rect) const;
94 [[nodiscard]] RectInt mapViewRectToFramebufferSpace(RectFloat const& rect, View const& view) const;
95 [[nodiscard]] RectInt mapWorldRectToFramebufferSpace(RectFloat const& rect) const;
96 [[nodiscard]] RectInt mapWorldRectToFramebufferSpace(RectFloat const& rect, View const& view) const;
97
98 virtual uint32_t prepareNextFrame(VkCommandBufferInheritanceInfo const* inheritanceInfo, uint64_t timeout_ns) = 0;
99 virtual void beginRenderPass(uint32_t imageIndex) = 0;
100 void draw(fge::RenderStates& states, fge::vulkan::GraphicPipeline* graphicPipeline = nullptr) const;
101 virtual void endRenderPass() = 0;
102 virtual void display(uint32_t imageIndex) = 0;
103
104 virtual Vector2u getSize() const = 0;
105
106 [[nodiscard]] virtual VkExtent2D getExtent2D() const = 0;
107 [[nodiscard]] virtual fge::vulkan::CommandBuffer& getCommandBuffer() const = 0;
108 [[nodiscard]] virtual VkRenderPass getRenderPass() const = 0;
109
110 enum class RequestResults
111 {
112 ALREADY_INITIALIZED,
113 UNINITIALIZED
114 };
115
116 [[nodiscard]] std::pair<fge::vulkan::GraphicPipeline&, RequestResults>
117 requestGraphicPipeline(vulkan::GraphicPipeline::Key const& key) const;
118 void clearGraphicPipelineCache();
119
120 [[nodiscard]] uint32_t requestGlobalTransform(fge::Transformable const& transformable,
121 uint32_t parentGlobalTransform) const;
122 [[nodiscard]] uint32_t requestGlobalTransform(fge::Transformable const& transformable,
123 fge::TransformUboData const& parentTransform) const;
124 [[nodiscard]] uint32_t requestGlobalTransform(fge::Transformable const& transformable,
125 fge::RenderResourceTransform const& resource) const;
126 [[nodiscard]] uint32_t requestGlobalTransform(fge::Transformable const& transformable) const;
127
128 [[nodiscard]] fge::TransformUboData const* getGlobalTransform(fge::RenderResourceTransform const& resource) const;
129
130private:
131 View g_defaultView;
132 View g_view;
133
134protected:
135 void refreshShaderCache();
136 void resetDefaultView();
137
138 shader::ShaderManager::DataBlockPointer _g_defaultFragmentShader;
139 shader::ShaderManager::DataBlockPointer _g_defaultNoTextureFragmentShader;
140 shader::ShaderManager::DataBlockPointer _g_defaultVertexShader;
141
142 VkClearColorValue _g_clearColor;
143
144 bool _g_forceGraphicPipelineUpdate;
145
146 mutable std::unordered_map<vulkan::GraphicPipeline::Key,
150 _g_graphicPipelineCache;
151};
152
153} // namespace fge
154
155#endif // _FGE_GRAPHIC_C_RENDERTARGET_HPP_INCLUDED
Definition C_color.hpp:35
Resource containing transform information for rendering.
Definition C_renderStates.hpp:54
The RenderStates class contains all the information needed to render something.
Definition C_renderStates.hpp:412
Definition C_renderTarget.hpp:56
Definition C_transformable.hpp:34
Define a camera in a 2D scene.
Definition C_view.hpp:43
Vulkan command buffer wrapper.
Definition C_commandBuffer.hpp:36
Definition C_contextAware.hpp:28
Vulkan context.
Definition C_context.hpp:70
Definition C_graphicPipeline.hpp:90
Definition C_viewport.hpp:27
Definition C_transform.hpp:26
Definition C_graphicPipeline.hpp:143
Definition C_graphicPipeline.hpp:138
Definition C_graphicPipeline.hpp:125