-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathRenderTarget.h
233 lines (200 loc) · 7.49 KB
/
RenderTarget.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#ifndef ESP_GFX_RENDERTARGET_H_
#define ESP_GFX_RENDERTARGET_H_
#include <Corrade/Containers/EnumSet.h>
#include <Magnum/Magnum.h>
#include "esp/core/Esp.h"
#include "esp/gfx/Renderer.h"
namespace esp {
namespace sensor {
class VisualSensor;
}
namespace gfx_batch {
class DepthShader;
}
namespace gfx {
/**
* Holds a framebuffer and encapsulates the logic of retrieving rendering
* results of various types (RGB, Depth, ObjectID) from the framebuffer.
*
* Reads the rendering results into either CPU or GPU, if compiled with CUDA,
* memory
*/
class RenderTarget {
public:
enum class Flag {
/**
* Create a color attachment for the rgba render buffer. No need to set it
* for depth sensor, semantic sensor etc. as it makes the rendering slower.
*/
RgbaAttachment = 1 << 0,
/**
* Create a color attachment for the objectId texture. No need to set it
* for color sensor, depth sensor etc. as it makes the rendering slower.
* Mutually exclusive with @ref Flag::Multisample.
*/
ObjectIdAttachment = 1 << 1,
/**
* Create a depth attachment for the depth texture. Has to be set for the
* depth sensor. No need to set it for color sensor, objectId sensor etc.
* as it makes the rendering slower (default depth buffer will be used in
* this case). Mutually exclusive with @ref Flag::Multisample.
*/
DepthTextureAttachment = 1 << 2,
/**
* Enable HBAO visual effect that adds soft shadows to corners and
* crevices. Mutually exclusive with @ref Flag::Multisample.
*/
HorizonBasedAmbientOcclusion = 1 << 3,
/**
* Enable color multisampling. Implies @ref Flag::Multisample, mutually
* exclusive with @ref Flag::ObjectIdAttachment,
* @ref Flag::DepthTextureAttachment and
* @ref Flag::HorizonBasedAmbientOcclusion.
*/
Multisample = RgbaAttachment | (1 << 4)
};
typedef Corrade::Containers::EnumSet<Flag> Flags;
CORRADE_ENUMSET_FRIEND_OPERATORS(Flags)
/**
* @brief Constructor
* @param size The size of the underlying framebuffers in WxH
* @param depthUnprojection Depth unprojection parameters. See @ref
* calculateDepthUnprojection()
* @param depthShader A DepthShader used to unproject depth on the GPU.
* Unprojects the depth on the CPU if nullptr.
* Must be not nullptr to use @ref
* readFrameDepthGPU()
* @param flags The flags of the renderer target
* @param visualSensor (optional) The visual sensor for this render
* target
*/
RenderTarget(const Magnum::Vector2i& size,
const Magnum::Vector2& depthUnprojection,
gfx_batch::DepthShader* depthShader,
Flags flags = {Flag::RgbaAttachment | Flag::ObjectIdAttachment |
Flag::DepthTextureAttachment},
const sensor::VisualSensor* visualSensor = nullptr);
/**
* @brief Constructor
* @param size The size of the underlying framebuffers in WxH
* @param depthUnprojection Depth unprojection parameters. See @ref
* calculateDepthUnprojection()
* @param visualSensor (optional) The visual sensor for this render
* target
*
* Equivalent to calling
* @ref RenderTarget(size, depthUnprojection, nullptr, {})
*/
RenderTarget(const Magnum::Vector2i& size,
const Magnum::Vector2& depthUnprojection,
const sensor::VisualSensor* visualSensor = nullptr)
: RenderTarget{size, depthUnprojection, nullptr, {}, visualSensor} {}
~RenderTarget() = default;
/**
* @brief Called before any draw calls that target this RenderTarget
* Clears the framebuffer to the color specified by the VisualSensorSpec and
* binds it.
*/
void renderEnter();
/**
* @brief Prepare for another render pass (e.g., to bind the framebuffer).
* Compared to @renderEnter, it will NOT clear the framebuffer.
*/
void renderReEnter();
/**
* @brief Called after any draw calls that target this RenderTarget
*/
void renderExit();
/**
* @brief The size of the framebuffer in WxH
*/
Magnum::Vector2i framebufferSize() const;
/**
* @brief Retrieve the RGBA rendering results.
*
* @param[in, out] view Preallocated memory that will be populated with the
* result. The result will be read as the pixel format of this view.
*/
void readFrameRgba(const Magnum::MutableImageView2D& view);
/**
* @brief Retrieve the depth rendering results.
*
* @param[in, out] view Preallocated memory that will be populated with the
* result. The PixelFormat of the image must only specify the R channel,
* generally @ref Magnum::PixelFormat::R32F
*/
void readFrameDepth(const Magnum::MutableImageView2D& view);
/**
* @brief Reads the ObjectID rendering results into the memory specified by
* view
*
* @param[in, out] view Preallocated memory that will be populated with the
* result. The PixelFormat of the image must only specify the R channel and
* be a format which a uint16_t can be interpreted as, generally @ref
* Magnum::PixelFormat::R32UI, @ref Magnum::PixelFormat::R32I, or @ref
* Magnum::PixelFormat::R16UI
*/
void readFrameObjectId(const Magnum::MutableImageView2D& view);
/**
* @brief Blits the rgba buffer from internal FBO to given framebuffer
* rectangle
*/
void blitRgbaTo(Magnum::GL::AbstractFramebuffer& target,
const Magnum::Range2Di& targetRectangle);
/**
* @brief Blits the rgba buffer from internal FBO to default frame buffer.
*/
void blitRgbaToDefault();
/**
* @brief get the depth texture
*/
Magnum::GL::Texture2D& getDepthTexture();
/**
* @brief get the object id texture
*/
Magnum::GL::Texture2D& getObjectIdTexture();
/**
* @brief draw HBAO effect if enabled for this render target
*/
void tryDrawHbao();
// @brief Delete copy Constructor
RenderTarget(const RenderTarget&) = delete;
// @brief Delete copy operator
RenderTarget& operator=(const RenderTarget&) = delete;
#ifdef ESP_BUILD_WITH_CUDA
/**
* @brief Reads the RGBA rendering result directly into CUDA memory. The
* caller is responsible for allocating memory and ensuring that the OpenGL
* context and the devPtr are on the same CUDA device.
*
* @param[in, out] devPtr CUDA memory pointer that points to a contiguous
* memory region of at least W*H*sizeof(uint8_t) bytes.
*/
void readFrameRgbaGPU(uint8_t* devPtr);
/**
* @brief Reads the depth rendering result directly into CUDA memory. See
* @ref readFrameRgbaGPU()
*
* Requires the rendering target to have a valid DepthShader
*
* @param[in, out] devPtr CUDA memory pointer that points to a contiguous
* memory region of at least W*H*sizeof(float) bytes.
*/
void readFrameDepthGPU(float* devPtr);
/**
* @brief Reads the ObjectID rendering result directly into CUDA memory. See
* @ref readFrameRgbaGPU()
*
* @param[in, out] devPtr CUDA memory pointer that points to a contiguous
* memory region of at least W*H*sizeof(int32_t) bytes.
*/
void readFrameObjectIdGPU(int32_t* devPtr);
#endif
ESP_SMART_POINTERS_WITH_UNIQUE_PIMPL(RenderTarget)
};
} // namespace gfx
} // namespace esp
#endif // ESP_GFX_RENDERTARGET_H_