1212
1313#include < cassert>
1414
15- FramebufferObject::FramebufferObject (GLuint width, GLuint height, unsigned int attachments, int samples) :
15+ FramebufferObject::FramebufferObject (GLuint width, GLuint height, unsigned int attachments, int samples, bool useFloatColor ) :
1616 m_width(width),
1717 m_height(height),
1818 m_colorTexId(0 ),
1919 m_depthTexId(0 ),
2020 m_fboId(0 ),
2121 m_samples(samples > 1 ? samples : 1 ),
22+ m_useFloatColor(useFloatColor),
2223 m_status(GL_FRAMEBUFFER_UNSUPPORTED),
2324 m_owned(true )
2425{
@@ -35,6 +36,7 @@ FramebufferObject::FramebufferObject(GLuint fboId) :
3536 m_depthTexId(0 ),
3637 m_fboId(fboId),
3738 m_samples(1 ),
39+ m_useFloatColor(false ),
3840 m_status(GL_FRAMEBUFFER_COMPLETE),
3941 m_owned(false )
4042{
@@ -57,6 +59,7 @@ FramebufferObject::FramebufferObject(FramebufferObject &&other) noexcept:
5759 m_colorRboId(other.m_colorRboId),
5860 m_depthRboId(other.m_depthRboId),
5961 m_samples(other.m_samples),
62+ m_useFloatColor(other.m_useFloatColor),
6063 m_status(other.m_status),
6164 m_owned(other.m_owned)
6265{
@@ -70,17 +73,18 @@ FramebufferObject::FramebufferObject(FramebufferObject &&other) noexcept:
7073
7174FramebufferObject& FramebufferObject::operator =(FramebufferObject &&other) noexcept
7275{
73- m_width = other.m_width ;
74- m_height = other.m_height ;
75- m_colorTexId = other.m_colorTexId ;
76- m_depthTexId = other.m_depthTexId ;
77- m_fboId = other.m_fboId ;
78- m_msaaFboId = other.m_msaaFboId ;
79- m_colorRboId = other.m_colorRboId ;
80- m_depthRboId = other.m_depthRboId ;
81- m_samples = other.m_samples ;
82- m_status = other.m_status ;
83- m_owned = other.m_owned ;
76+ m_width = other.m_width ;
77+ m_height = other.m_height ;
78+ m_colorTexId = other.m_colorTexId ;
79+ m_depthTexId = other.m_depthTexId ;
80+ m_fboId = other.m_fboId ;
81+ m_msaaFboId = other.m_msaaFboId ;
82+ m_colorRboId = other.m_colorRboId ;
83+ m_depthRboId = other.m_depthRboId ;
84+ m_samples = other.m_samples ;
85+ m_useFloatColor = other.m_useFloatColor ;
86+ m_status = other.m_status ;
87+ m_owned = other.m_owned ;
8488
8589 other.m_owned = false ;
8690 other.m_fboId = 0 ;
@@ -132,10 +136,25 @@ FramebufferObject::generateColorTexture()
132136
133137 // Set the texture dimensions
134138#ifdef GL_ES
135- glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, m_width, m_height, 0 , GL_RGBA, GL_UNSIGNED_BYTE, nullptr );
139+ GLenum format = GL_RGBA;
140+ GLint internalFormat;
141+ GLenum type;
142+ if (celestia::gl::checkVersion (celestia::gl::GLES_3_0))
143+ {
144+ internalFormat = m_useFloatColor ? GL_RGBA16F : GL_RGBA8;
145+ type = m_useFloatColor ? GL_HALF_FLOAT : GL_UNSIGNED_BYTE;
146+ }
147+ else
148+ {
149+ internalFormat = GL_RGBA;
150+ type = m_useFloatColor ? GL_HALF_FLOAT_OES : GL_UNSIGNED_BYTE;
151+ }
136152#else
137- glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGB8, m_width, m_height, 0 , GL_RGB, GL_UNSIGNED_BYTE, nullptr );
153+ GLint internalFormat = m_useFloatColor ? GL_RGBA16F : GL_RGB8;
154+ GLenum format = m_useFloatColor ? GL_RGBA : GL_RGB;
155+ GLenum type = m_useFloatColor ? GL_FLOAT : GL_UNSIGNED_BYTE;
138156#endif
157+ glTexImage2D (GL_TEXTURE_2D, 0 , internalFormat, m_width, m_height, 0 , format, type, nullptr );
139158
140159 // Unbind the texture
141160 glBindTexture (GL_TEXTURE_2D, 0 );
@@ -165,13 +184,23 @@ FramebufferObject::generateDepthTexture()
165184
166185 // Set the texture dimensions
167186#ifdef GL_ES
168- if (celestia::gl::checkVersion (celestia::gl::GLES_3_0) || celestia::gl::OES_depth24)
169- glTexImage2D (GL_TEXTURE_2D, 0 , GL_DEPTH_COMPONENT24, m_width, m_height, 0 , GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr );
187+ GLint internalFormat;
188+ GLenum type;
189+ if (celestia::gl::checkVersion (celestia::gl::GLES_3_0))
190+ {
191+ internalFormat = GL_DEPTH_COMPONENT24;
192+ type = GL_UNSIGNED_INT;
193+ }
170194 else
171- glTexImage2D (GL_TEXTURE_2D, 0 , GL_DEPTH_COMPONENT16, m_width, m_height, 0 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr );
195+ {
196+ internalFormat = GL_DEPTH_COMPONENT;
197+ type = celestia::gl::OES_depth24 ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
198+ }
172199#else
173- glTexImage2D (GL_TEXTURE_2D, 0 , GL_DEPTH_COMPONENT, m_width, m_height, 0 , GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr );
200+ GLint internalFormat = GL_DEPTH_COMPONENT;
201+ GLenum type = GL_UNSIGNED_BYTE;
174202#endif
203+ glTexImage2D (GL_TEXTURE_2D, 0 , internalFormat, m_width, m_height, 0 , GL_DEPTH_COMPONENT, type, nullptr );
175204
176205 // Unbind the texture
177206 glBindTexture (GL_TEXTURE_2D, 0 );
@@ -180,29 +209,7 @@ FramebufferObject::generateDepthTexture()
180209void
181210FramebufferObject::generateFbo (unsigned int attachments)
182211{
183- // Determine MSAA strategy.
184- //
185- // Desktop GL: renderbuffer MSAA + glBlitFramebuffer (ARB_framebuffer_object is required)
186- // GLES 3.0+: same as desktop GL
187- // GLES 2.0: no MSAA support
188- //
189- // The "renderbuffer MSAA" strategy uses two FBOs:
190- // m_msaaFboId – MSAA renderbuffers, scene is rendered here
191- // m_fboId – plain textures, used as resolve target and sampled by the effect shader
192-
193- bool useRenderbufferMSAA = false ;
194-
195- if (m_samples > 1 && (attachments & ColorAttachment) != 0 )
196- {
197- #ifdef GL_ES
198- if (celestia::gl::checkVersion (celestia::gl::GLES_3_0))
199- useRenderbufferMSAA = true ;
200- else
201- m_samples = 1 ; // no MSAA support on GLES2
202- #else
203- useRenderbufferMSAA = true ;
204- #endif
205- }
212+ bool useRenderbufferMSAA = m_samples > 1 && (attachments & ColorAttachment) != 0 ;
206213
207214 // Create the texture-based FBO (resolve target for renderbuffer MSAA,
208215 // or the sole FBO for non-MSAA paths).
@@ -291,10 +298,11 @@ FramebufferObject::generateMSAAFbo(unsigned int attachments)
291298 glGenRenderbuffers (1 , &m_colorRboId);
292299 glBindRenderbuffer (GL_RENDERBUFFER, m_colorRboId);
293300#ifdef GL_ES
294- glRenderbufferStorageMultisample (GL_RENDERBUFFER, m_samples, GL_RGBA8, m_width, m_height) ;
301+ GLenum fmt = m_useFloatColor ? GL_RGBA16F : GL_RGBA8 ;
295302#else
296- glRenderbufferStorageMultisample (GL_RENDERBUFFER, m_samples, GL_RGB8, m_width, m_height) ;
303+ GLenum fmt = m_useFloatColor ? GL_RGBA16F : GL_RGB8 ;
297304#endif
305+ glRenderbufferStorageMultisample (GL_RENDERBUFFER, m_samples, fmt, m_width, m_height);
298306 glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_colorRboId);
299307 }
300308
0 commit comments