-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathhost_device_rt.cpp
292 lines (247 loc) · 6.28 KB
/
host_device_rt.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
// This file is distributed under the MIT license.
// See the LICENSE file for details.
#include <common/config.h>
#include <cassert>
#include <utility>
#include <GL/glew.h>
#include <common/cpu_buffer_rt.h>
#if VSNRAY_COMMON_HAVE_CUDA
#include <common/gpu_buffer_rt.h>
#include <common/pixel_unpack_buffer_rt.h>
#endif
#include "host_device_rt.h"
namespace visionaray
{
//-------------------------------------------------------------------------------------------------
// Private implementation
//
struct host_device_rt::impl
{
// CPU or GPU rendering
mode_type mode;
// If true, render target uses double buffering
bool double_buffering;
// If true, use PBO, otherwise copy over host
bool direct_rendering;
// Framebuffer color space, either RGB or SRGB
color_space_type color_space;
// Host render target
cpu_buffer_rt<PF_RGBA8, PF_UNSPECIFIED, PF_RGBA32F> host_rt[2];
#if VSNRAY_COMMON_HAVE_CUDA
// Device render target, uses PBO
pixel_unpack_buffer_rt<PF_RGBA8, PF_UNSPECIFIED, PF_RGBA32F> direct_rt[2];
// Device render target, copy pixels over host
gpu_buffer_rt<PF_RGBA8, PF_UNSPECIFIED, PF_RGBA32F> indirect_rt[2];
#endif
// Index of front and back buffer, either 0 or 1
bool buffer_index[2];
};
//-------------------------------------------------------------------------------------------------
// host_device_rt
//
host_device_rt::host_device_rt(
mode_type mode,
bool double_buffering,
bool direct_rendering,
color_space_type color_space
)
: impl_(new impl)
{
impl_->mode = mode;
set_double_buffering(double_buffering);
impl_->direct_rendering = direct_rendering;
impl_->color_space = color_space;
}
host_device_rt::~host_device_rt()
{
}
host_device_rt::mode_type& host_device_rt::mode()
{
return impl_->mode;
}
host_device_rt::mode_type const& host_device_rt::mode() const
{
return impl_->mode;
}
void host_device_rt::set_double_buffering(bool double_buffering)
{
impl_->double_buffering = double_buffering;
if (impl_->double_buffering)
{
impl_->buffer_index[0] = 0;
impl_->buffer_index[1] = 1;
}
else
{
// Both indices the same, so when we swap buffers, in the
// single buffering case nothing will ever get swapped
impl_->buffer_index[0] = 0;
impl_->buffer_index[1] = 0;
}
}
bool host_device_rt::get_double_buffering() const
{
return impl_->double_buffering;
}
bool& host_device_rt::direct_rendering()
{
return impl_->direct_rendering;
}
bool const& host_device_rt::direct_rendering() const
{
return impl_->direct_rendering;
}
host_device_rt::color_space_type& host_device_rt::color_space()
{
return impl_->color_space;
}
host_device_rt::color_space_type const& host_device_rt::color_space() const
{
return impl_->color_space;
}
void host_device_rt::swap_buffers()
{
std::swap(impl_->buffer_index[Front], impl_->buffer_index[Back]);
}
host_device_rt::color_type const* host_device_rt::color(buffer buf) const
{
return impl_->host_rt[impl_->buffer_index[buf]].color();
}
host_device_rt::ref_type host_device_rt::ref(buffer buf)
{
if (impl_->mode == CPU)
{
return impl_->host_rt[impl_->buffer_index[buf]].ref();
}
else
{
#if VSNRAY_COMMON_HAVE_CUDA
if (impl_->direct_rendering)
{
return impl_->direct_rt[impl_->buffer_index[buf]].ref();
}
else
{
return impl_->indirect_rt[impl_->buffer_index[buf]].ref();
}
#else
assert(0);
return {};
#endif
}
}
void host_device_rt::clear(vec4 const& color, buffer buf)
{
impl_->host_rt[impl_->buffer_index[buf]].clear_color_buffer(color);
impl_->host_rt[impl_->buffer_index[buf]].clear_accum_buffer(color);
#if VSNRAY_COMMON_HAVE_CUDA
if (impl_->direct_rendering)
{
impl_->direct_rt[impl_->buffer_index[buf]].clear_color_buffer(color);
}
else
{
impl_->indirect_rt[impl_->buffer_index[buf]].clear_color_buffer(color);
}
#endif
}
void host_device_rt::begin_frame(buffer buf)
{
if (impl_->mode == CPU)
{
impl_->host_rt[impl_->buffer_index[buf]].begin_frame();
}
#if VSNRAY_COMMON_HAVE_CUDA
else
{
if (impl_->direct_rendering)
{
impl_->direct_rt[impl_->buffer_index[buf]].begin_frame();
}
else
{
impl_->indirect_rt[impl_->buffer_index[buf]].begin_frame();
}
}
#endif
}
void host_device_rt::end_frame(buffer buf)
{
if (impl_->mode == CPU)
{
impl_->host_rt[impl_->buffer_index[buf]].end_frame();
}
#if VSNRAY_COMMON_HAVE_CUDA
else
{
if (impl_->direct_rendering)
{
impl_->direct_rt[impl_->buffer_index[buf]].end_frame();
}
else
{
impl_->indirect_rt[impl_->buffer_index[buf]].end_frame();
}
}
#endif
}
void host_device_rt::resize(int w, int h)
{
render_target::resize(w, h);
int num_buffers = impl_->double_buffering ? 2 : 1;
for (int buf = 0; buf < num_buffers; ++buf)
{
impl_->host_rt[buf].resize(w, h);
#if VSNRAY_COMMON_HAVE_CUDA
if (impl_->direct_rendering)
{
impl_->direct_rt[buf].resize(w, h);
}
else
{
impl_->indirect_rt[buf].resize(w, h);
}
#endif
}
}
void host_device_rt::display_color_buffer(buffer buf) const
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Store OpenGL state
GLboolean prev_srgb_enabled = glIsEnabled(GL_FRAMEBUFFER_SRGB);
if (impl_->color_space == SRGB)
{
glEnable(GL_FRAMEBUFFER_SRGB);
}
else
{
glDisable(GL_FRAMEBUFFER_SRGB);
}
if (impl_->mode == CPU)
{
impl_->host_rt[impl_->buffer_index[buf]].display_color_buffer();
}
#if VSNRAY_COMMON_HAVE_CUDA
else
{
if (impl_->direct_rendering)
{
impl_->direct_rt[impl_->buffer_index[buf]].display_color_buffer();
}
else
{
impl_->indirect_rt[impl_->buffer_index[buf]].display_color_buffer();
}
}
#endif
if (prev_srgb_enabled)
{
glEnable(GL_FRAMEBUFFER_SRGB);
}
else
{
glDisable(GL_FRAMEBUFFER_SRGB);
}
}
} // visionaray