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
|
/************************************************************************************
Filename : Util_GL_Blitter.cpp
Content : GL implementation for blitting, supporting scaling & rotation
Created : February 24, 2015
Authors : Reza Nourai
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
https://developer.oculus.com/licenses/oculusmastersdk-1.0
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "Util_GL_Blitter.h"
namespace OVR {
namespace GLUtil {
#define AssertOnGLError() \
{ \
GLuint err = glGetError(); \
OVR_ASSERT_AND_UNUSED(!err, err); \
}
//-------------------------------------------------------------------------------------
// ***** CAPI::Blitter
Blitter::Blitter() : ReadFBO(0) {}
Blitter::~Blitter() {
glDeleteFramebuffers(1, &ReadFBO);
ReadFBO = 0;
}
bool Blitter::Initialize() {
glGenFramebuffers(1, &ReadFBO);
AssertOnGLError();
return true;
}
bool Blitter::Blt(GLuint sourceTexId) {
GLenum status = 0;
GLint currentTex2D = 0;
GLint sourceWidth = 0, sourceHeight = 0;
// Store off currently selected tex2d
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤tTex2D);
// Get source texture dimensions
glBindTexture(GL_TEXTURE_2D, sourceTexId);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &sourceWidth);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &sourceHeight);
// Put old texture back
glBindTexture(GL_TEXTURE_2D, currentTex2D);
// Save off the current FBOs
GLint currentReadFB;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, ¤tReadFB);
AssertOnGLError();
// setup draw buffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glDrawBuffer(GL_BACK);
// setup read buffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, ReadFBO);
AssertOnGLError();
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourceTexId, 0);
glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
AssertOnGLError();
status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
OVR_ASSERT_AND_UNUSED(status == GL_FRAMEBUFFER_COMPLETE, status);
// Do the blt
glBlitFramebuffer(
0,
sourceHeight,
sourceWidth,
0,
0,
0,
sourceWidth,
sourceHeight,
GL_COLOR_BUFFER_BIT,
GL_NEAREST);
AssertOnGLError();
// Restore the previous FBOs
glBindFramebuffer(GL_READ_FRAMEBUFFER, currentReadFB);
AssertOnGLError();
return true;
}
bool Blitter::Blt(
GLuint sourceTexId,
uint32_t topLeftX,
uint32_t topLeftY,
uint32_t width,
uint32_t height) {
GLenum status = 0;
GLint currentTex2D = 0;
GLint sourceWidth = 0, sourceHeight = 0;
// Store off currently selected tex2d
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤tTex2D);
// Get source texture dimensions
glBindTexture(GL_TEXTURE_2D, sourceTexId);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &sourceWidth);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &sourceHeight);
// Put old texture back
glBindTexture(GL_TEXTURE_2D, currentTex2D);
// Save off the current FBOs
GLint currentReadFB;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, ¤tReadFB);
AssertOnGLError();
// setup draw buffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glDrawBuffer(GL_BACK);
// Get the default framebuffer size :
GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, dims);
GLint fbHeight = dims[3];
// setup read buffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, ReadFBO);
AssertOnGLError();
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourceTexId, 0);
glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
AssertOnGLError();
status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
OVR_ASSERT_AND_UNUSED(status == GL_FRAMEBUFFER_COMPLETE, status);
// Do the blt
glBlitFramebuffer(
0,
sourceHeight,
sourceWidth,
0,
(GLint)topLeftX,
fbHeight - (GLint)topLeftY - (GLint)height,
(GLint)topLeftX + (GLint)width,
fbHeight - (GLint)topLeftY,
GL_COLOR_BUFFER_BIT,
GL_NEAREST);
AssertOnGLError();
// Restore the previous FBOs
glBindFramebuffer(GL_READ_FRAMEBUFFER, currentReadFB);
AssertOnGLError();
return true;
}
bool Blitter::BltCubemap(GLuint sourceTexId, GLuint tempTexId, uint32_t cubeMapSize) {
GLenum status = 0;
GLint currentTex2D = 0;
GLint size = (GLint)cubeMapSize;
const int numFaces = 6;
// Store off currently selected tex2d
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤tTex2D);
// Save off the current FBOs
GLint currentReadFB;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, ¤tReadFB);
AssertOnGLError();
for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
// Create framebuffers
GLuint drawCubeFBO;
glGenFramebuffers(1, &drawCubeFBO);
AssertOnGLError();
GLuint readCubeFBO;
glGenFramebuffers(1, &readCubeFBO);
AssertOnGLError();
GLuint drawFinalCubeFBO;
glGenFramebuffers(1, &drawFinalCubeFBO);
AssertOnGLError();
// Setup draw buffer with temp texture to hold flipped face
// tempTexId holds a Texture2D the size of one cubemap face (cubeMapSize)
// It is used to temporarily hold a flipped face before blitting it to the original cubemap
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, drawCubeFBO);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tempTexId, 0);
// Setup read buffer with original cubemap texture
glBindFramebuffer(GL_READ_FRAMEBUFFER, readCubeFBO);
AssertOnGLError();
glFramebufferTexture2D(
GL_READ_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex,
sourceTexId,
0);
AssertOnGLError();
status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
OVR_ASSERT_AND_UNUSED(status == GL_FRAMEBUFFER_COMPLETE, status);
// Do the blt
glBlitFramebuffer(0, size, size, 0, 0, size, size, 0, GL_COLOR_BUFFER_BIT, GL_LINEAR);
AssertOnGLError();
// Setup final draw buffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, drawFinalCubeFBO);
glFramebufferTexture2D(
GL_DRAW_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex,
sourceTexId,
0);
// Setup read buffer to be the previous draw buffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, drawCubeFBO); // flipped FBO
AssertOnGLError();
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tempTexId, 0);
AssertOnGLError();
status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
OVR_ASSERT_AND_UNUSED(status == GL_FRAMEBUFFER_COMPLETE, status);
// Do the blt again
glBlitFramebuffer(0, size, size, 0, 0, 0, size, size, GL_COLOR_BUFFER_BIT, GL_LINEAR);
AssertOnGLError();
// Delete framebuffers
glDeleteFramebuffers(1, &drawCubeFBO);
drawCubeFBO = 0;
glDeleteFramebuffers(1, &readCubeFBO);
readCubeFBO = 0;
glDeleteFramebuffers(1, &drawFinalCubeFBO);
drawFinalCubeFBO = 0;
}
// Restore the previous FBOs
glBindFramebuffer(GL_READ_FRAMEBUFFER, currentReadFB);
AssertOnGLError();
return true;
}
} // namespace GLUtil
} // namespace OVR
|