-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathHDRBuffer.cpp
326 lines (239 loc) · 11.5 KB
/
HDRBuffer.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
////////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of CosmoScout VR //
////////////////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: German Aerospace Center (DLR) <[email protected]>
// SPDX-License-Identifier: MIT
#include "HDRBuffer.hpp"
#include "GlareMipMap.hpp"
#include "LuminanceMipMap.hpp"
#include "logger.hpp"
#include <VistaKernel/DisplayManager/VistaDisplayManager.h>
#include <VistaKernel/DisplayManager/VistaViewport.h>
#include <VistaKernel/VistaSystem.h>
#include <VistaOGLExt/VistaFramebufferObj.h>
#include <VistaOGLExt/VistaGLSLShader.h>
#include <chrono>
namespace cs::graphics {
////////////////////////////////////////////////////////////////////////////////////////////////////
HDRBuffer::HDRBuffer(uint32_t multiSamples, bool highPrecision)
: mMultiSamples(multiSamples)
, mHighPrecision(highPrecision) {
}
////////////////////////////////////////////////////////////////////////////////////////////////////
HDRBuffer::~HDRBuffer() {
// Destructor must not be inline as the std::unique_ptrs would otherwise not accept incomplete
// types in the header.
}
////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t HDRBuffer::getMultiSamples() const {
return mMultiSamples;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::bind() {
// There is a different framebuffer object for each viewport. Here wee retrieve the current one.
auto& hdrBuffer = getCurrentHDRBuffer();
auto size = getCurrentViewPortSize();
// If the size changed, we have to re-create the framebuffer object and its attachments.
if (size[0] != hdrBuffer.mWidth || size[1] != hdrBuffer.mHeight) {
hdrBuffer.mWidth = size[0];
hdrBuffer.mHeight = size[1];
// Create new framebuffer object.
hdrBuffer.mFBO.reset(new VistaFramebufferObj());
// Attaches a new texture to the hdrBuffer framebuffer object.
auto addAttachment = [&hdrBuffer, this](std::unique_ptr<VistaTexture>& texture, int attachment,
int internalFormat, int format, int type) {
auto target = (mMultiSamples > 0) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
if (!texture) {
texture = std::make_unique<VistaTexture>(target);
}
texture->Bind();
if (mMultiSamples > 0) {
glTexImage2DMultisample(
target, mMultiSamples, internalFormat, hdrBuffer.mWidth, hdrBuffer.mHeight, false);
} else {
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(target, 0, internalFormat, hdrBuffer.mWidth, hdrBuffer.mHeight, 0, format,
type, nullptr);
}
hdrBuffer.mFBO->Attach(texture.get(), attachment);
};
// Add HDR-attachment ping-pong A.
addAttachment(hdrBuffer.mColorAttachments[0], GL_COLOR_ATTACHMENT0,
mHighPrecision ? GL_RGBA32F : GL_RGBA16F, GL_RGBA, GL_FLOAT);
// Add HDR-attachment ping-pong B.
addAttachment(hdrBuffer.mColorAttachments[1], GL_COLOR_ATTACHMENT1,
mHighPrecision ? GL_RGBA32F : GL_RGBA16F, GL_RGBA, GL_FLOAT);
// Add depth-attachment.
addAttachment(hdrBuffer.mDepthAttachment, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT24,
GL_DEPTH_COMPONENT, GL_FLOAT);
// Create luminance mipmaps.
hdrBuffer.mLuminanceMipMap.reset(new LuminanceMipMap(mMultiSamples, size[0], size[1]));
// Create glare mipmaps.
hdrBuffer.mGlareMipMap.reset(new GlareMipMap(mMultiSamples, size[0], size[1]));
}
// Bind the framebuffer object for writing.
if (!hdrBuffer.mIsBound) {
glGetIntegerv(GL_VIEWPORT, hdrBuffer.mCachedViewport.data());
hdrBuffer.mFBO->Bind();
hdrBuffer.mIsBound = true;
}
// Select the write attachment based on the current ping-pong state.
if (hdrBuffer.mCompositePinpongState == 0) {
glDrawBuffer(GL_COLOR_ATTACHMENT0);
} else {
glDrawBuffer(GL_COLOR_ATTACHMENT1);
}
// Set the viewport to the entire hdrBuffer.
glViewport(0, 0, hdrBuffer.mWidth, hdrBuffer.mHeight);
glScissor(0, 0, hdrBuffer.mWidth, hdrBuffer.mHeight);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::unbind() {
// There is a different framebuffer object for each viewport. Here wee retrieve the current one.
auto& hdrBuffer = getCurrentHDRBuffer();
// If it is bound, unbind it.
if (hdrBuffer.mIsBound) {
hdrBuffer.mFBO->Release();
hdrBuffer.mIsBound = false;
// And restore the original viewport.
glViewport(hdrBuffer.mCachedViewport.at(0), hdrBuffer.mCachedViewport.at(1),
hdrBuffer.mCachedViewport.at(2), hdrBuffer.mCachedViewport.at(3));
glScissor(hdrBuffer.mCachedViewport.at(0), hdrBuffer.mCachedViewport.at(1),
hdrBuffer.mCachedViewport.at(2), hdrBuffer.mCachedViewport.at(3));
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::doPingPong() {
// There is a different framebuffer object for each viewport. Here wee retrieve the current one.
auto& hdrBuffer = getCurrentHDRBuffer();
hdrBuffer.mCompositePinpongState = (hdrBuffer.mCompositePinpongState + 1) % 2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::clear() {
bind();
std::array<GLenum, 2> bufs = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, bufs.data());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
VistaTexture* HDRBuffer::getDepthAttachment() const {
auto const& hdrBuffer = getCurrentHDRBuffer();
return hdrBuffer.mDepthAttachment.get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
VistaTexture* HDRBuffer::getCurrentWriteAttachment() const {
auto const& hdrBuffer = getCurrentHDRBuffer();
if (hdrBuffer.mCompositePinpongState == 0) {
return hdrBuffer.mColorAttachments.at(0).get();
}
return hdrBuffer.mColorAttachments.at(1).get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
VistaTexture* HDRBuffer::getCurrentReadAttachment() const {
auto const& hdrBuffer = getCurrentHDRBuffer();
if (hdrBuffer.mCompositePinpongState == 0) {
return hdrBuffer.mColorAttachments.at(1).get();
}
return hdrBuffer.mColorAttachments.at(0).get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
HDRBuffer::HDRBufferData& HDRBuffer::getCurrentHDRBuffer() {
auto* viewport = GetVistaSystem()->GetDisplayManager()->GetCurrentRenderInfo()->m_pViewport;
return mHDRBufferData[viewport];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
HDRBuffer::HDRBufferData const& HDRBuffer::getCurrentHDRBuffer() const {
auto* viewport = GetVistaSystem()->GetDisplayManager()->GetCurrentRenderInfo()->m_pViewport;
return mHDRBufferData.find(viewport)->second;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
std::array<int, 2> HDRBuffer::getCurrentViewPortSize() {
auto* viewport = GetVistaSystem()->GetDisplayManager()->GetCurrentRenderInfo()->m_pViewport;
std::array<int, 2> size{};
viewport->GetViewportProperties()->GetSize(size.at(0), size.at(1));
return size;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
std::array<int, 2> HDRBuffer::getCurrentViewPortPos() {
auto* viewport = GetVistaSystem()->GetDisplayManager()->GetCurrentRenderInfo()->m_pViewport;
std::array<int, 2> pos{};
viewport->GetViewportProperties()->GetPosition(pos.at(0), pos.at(1));
return pos;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::calculateLuminance() {
auto& hdrBuffer = getCurrentHDRBuffer();
VistaTexture* composite = nullptr;
if (hdrBuffer.mCompositePinpongState == 0) {
composite = hdrBuffer.mColorAttachments.at(0).get();
} else {
composite = hdrBuffer.mColorAttachments.at(1).get();
}
hdrBuffer.mLuminanceMipMap->update(composite);
if (hdrBuffer.mLuminanceMipMap->getLastTotalLuminance() != 0.0F) {
mTotalLuminance = hdrBuffer.mLuminanceMipMap->getLastTotalLuminance();
mMaximumLuminance = hdrBuffer.mLuminanceMipMap->getLastMaximumLuminance();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float HDRBuffer::getTotalLuminance() const {
return mTotalLuminance;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float HDRBuffer::getMaximumLuminance() const {
return mMaximumLuminance;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::updateGlareMipMap(float maxLuminance) {
auto& hdrBuffer = getCurrentHDRBuffer();
VistaTexture* composite = nullptr;
if (hdrBuffer.mCompositePinpongState == 0) {
composite = hdrBuffer.mColorAttachments.at(0).get();
} else {
composite = hdrBuffer.mColorAttachments.at(1).get();
}
hdrBuffer.mGlareMipMap->update(composite, maxLuminance, mGlareMode, mGlareQuality,
mEnableBicubicGlareFilter, mEnable32BitGlare);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
VistaTexture* HDRBuffer::getGlareMipMap() const {
auto const& hdrBuffer = getCurrentHDRBuffer();
return hdrBuffer.mGlareMipMap.get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::setGlareMode(HDRBuffer::GlareMode value) {
mGlareMode = value;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
HDRBuffer::GlareMode HDRBuffer::getGlareMode() const {
return mGlareMode;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::setGlareQuality(uint32_t quality) {
mGlareQuality = quality;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t HDRBuffer::getGlareQuality() const {
return mGlareQuality;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::setEnableBicubicGlareFilter(bool enable) {
mEnableBicubicGlareFilter = enable;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool HDRBuffer::getEnableBicubicGlareFilter() const {
return mEnableBicubicGlareFilter;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void HDRBuffer::setEnable32BitGlare(bool enable) {
mEnable32BitGlare = enable;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool HDRBuffer::getEnable32BitGlare() const {
return mEnable32BitGlare;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace cs::graphics