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
|
#import "iosOGLContext.h"
#import "base/assert.hpp"
#import "base/logging.cpp"
#import "drape/glfunctions.hpp"
iosOGLContext::iosOGLContext(CAEAGLLayer * layer, iosOGLContext * contextToShareWith, bool needBuffers)
: m_layer(layer)
, m_nativeContext(NULL)
, m_needBuffers(needBuffers)
, m_hasBuffers(false)
, m_renderBufferId(0)
, m_depthBufferId(0)
, m_frameBufferId(0)
, m_presentAvailable(true)
{
if (contextToShareWith != NULL)
{
m_nativeContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2
sharegroup: contextToShareWith->m_nativeContext.sharegroup];
}
else
m_nativeContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
}
iosOGLContext::~iosOGLContext()
{
destroyBuffers();
}
void iosOGLContext::makeCurrent()
{
ASSERT(m_nativeContext != NULL, ());
[EAGLContext setCurrentContext: m_nativeContext];
if (m_needBuffers && !m_hasBuffers)
initBuffers();
}
void iosOGLContext::setPresentAvailable(bool available)
{
m_presentAvailable = available;
}
void iosOGLContext::present()
{
ASSERT(m_nativeContext != NULL, ());
ASSERT(m_renderBufferId, ());
GLenum const discards[] = { GL_DEPTH_ATTACHMENT, GL_COLOR_ATTACHMENT0 };
GLCHECK(glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, discards));
glBindRenderbuffer(GL_RENDERBUFFER, m_renderBufferId);
if (m_presentAvailable)
[m_nativeContext presentRenderbuffer: GL_RENDERBUFFER];
GLCHECK(glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, discards + 1));
}
void iosOGLContext::setDefaultFramebuffer()
{
ASSERT(m_frameBufferId, ());
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
}
void iosOGLContext::resize(int w, int h)
{
if (m_needBuffers && m_hasBuffers)
{
GLint width = 0;
GLint height = 0;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
if (width == w && height == h)
return;
destroyBuffers();
initBuffers();
}
}
void iosOGLContext::initBuffers()
{
ASSERT(m_needBuffers, ());
if (!m_hasBuffers)
{
// Color
glGenRenderbuffers(1, &m_renderBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, m_renderBufferId);
[m_nativeContext renderbufferStorage:GL_RENDERBUFFER fromDrawable: m_layer];
// color
// Depth
GLint width = 0;
GLint height = 0;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
glGenRenderbuffers(1, &m_depthBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthBufferId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
// depth
// Framebuffer
glGenFramebuffers(1, &m_frameBufferId);
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderBufferId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferId);
GLint fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fbStatus != GL_FRAMEBUFFER_COMPLETE)
LOG(LERROR, ("Incomplete framebuffer:", fbStatus));
// framebuffer
m_hasBuffers = true;
}
}
void iosOGLContext::destroyBuffers()
{
if (m_needBuffers && m_hasBuffers)
{
glFinish();
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glDeleteFramebuffers(1, &m_frameBufferId);
glDeleteRenderbuffers(1, &m_renderBufferId);
glDeleteRenderbuffers(1, &m_depthBufferId);
m_hasBuffers = false;
}
}
|