Skip to content

Commit 75db997

Browse files
authored
Prepare for sRGB (#2487)
Taken from #2483 no behavior change yet Fix a bug that iOS ES2 has OES_depth24 extension and will try to call this. GL_DEPTH_COMPONENT24 is not valid for ES2. ``` glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr); ``` I've tested viewport effects with: iOS (ES2, ES3) and macOS (desktop GL), with MSAA on/off (ES2 FBO's MSAA is always off), with float or non-float storage.
1 parent bae9049 commit 75db997

13 files changed

Lines changed: 139 additions & 70 deletions

File tree

src/celengine/framebuffer.cpp

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
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

7174
FramebufferObject& 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()
180209
void
181210
FramebufferObject::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

src/celengine/framebuffer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class FramebufferObject
2121
DepthAttachment = 0x2
2222
};
2323
FramebufferObject() = delete;
24-
FramebufferObject(GLuint width, GLuint height, unsigned int attachments, int samples = 1);
24+
FramebufferObject(GLuint width, GLuint height, unsigned int attachments, int samples = 1, bool useFloatColor = false);
2525
FramebufferObject(const FramebufferObject&) = delete;
2626
FramebufferObject(FramebufferObject&&) noexcept;
2727
FramebufferObject& operator=(const FramebufferObject&) = delete;
@@ -48,6 +48,11 @@ class FramebufferObject
4848
return m_samples;
4949
}
5050

51+
bool useFloatColor() const
52+
{
53+
return m_useFloatColor;
54+
}
55+
5156
GLuint colorTexture() const;
5257
GLuint depthTexture() const;
5358

@@ -74,6 +79,7 @@ class FramebufferObject
7479
GLuint m_colorRboId{ 0 };
7580
GLuint m_depthRboId{ 0 };
7681
int m_samples;
82+
bool m_useFloatColor;
7783
GLenum m_status;
7884
bool m_owned;
7985
};

src/celengine/glsupport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CELAPI bool OES_vertex_array_object = false;
1212
CELAPI bool OES_texture_border_clamp = false;
1313
CELAPI bool OES_geometry_shader = false;
1414
CELAPI bool OES_depth24 = false;
15+
CELAPI bool OES_texture_half_float = false;
1516
#else
1617
CELAPI bool ARB_vertex_array_object = false;
1718
CELAPI bool ARB_framebuffer_object = false;
@@ -83,6 +84,7 @@ bool init(util::array_view<std::string> ignore) noexcept
8384
OES_texture_border_clamp = check_extension(ignore, "GL_OES_texture_border_clamp") || check_extension(ignore, "GL_EXT_texture_border_clamp");
8485
OES_geometry_shader = check_extension(ignore, "GL_OES_geometry_shader") || check_extension(ignore, "GL_EXT_geometry_shader");
8586
OES_depth24 = check_extension(ignore, "GL_OES_depth24");
87+
OES_texture_half_float = check_extension(ignore, "GL_OES_texture_half_float");
8688
#else
8789
ARB_vertex_array_object = check_extension(ignore, "GL_ARB_vertex_array_object");
8890
if (!has_extension("GL_ARB_framebuffer_object"))

src/celengine/glsupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern CELAPI bool OES_vertex_array_object; //NOSONAR
4848
extern CELAPI bool OES_texture_border_clamp; //NOSONAR
4949
extern CELAPI bool OES_geometry_shader; //NOSONAR
5050
extern CELAPI bool OES_depth24; //NOSONAR
51+
extern CELAPI bool OES_texture_half_float; //NOSONAR
5152
#else
5253
extern CELAPI bool ARB_vertex_array_object; //NOSONAR
5354
#endif

src/celengine/render.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,13 +2886,14 @@ void Renderer::renderPlanet(Body& body,
28862886

28872887
if (body.isVisibleAsPoint())
28882888
{
2889-
if (float maxCoeff = body.getSurface().color.toVector3().maxCoeff(); maxCoeff > 0.0f) // ignore [ 0 0 0 ]; used by old addons to make objects not get rendered as point
2889+
const auto& surfaceColor = body.getSurface().color;
2890+
if (float maxCoeff = surfaceColor.toVector3().maxCoeff(); maxCoeff > 0.0f) // ignore [ 0 0 0 ]; used by old addons to make objects not get rendered as point
28902891
{
28912892
renderObjectAsPoint(pos,
28922893
body.getRadius(),
28932894
appMag,
28942895
discSizeInPixels,
2895-
body.getSurface().color * (1.0f / maxCoeff), // normalize point color; 'darkness' is handled by size of point determined by GeomAlbedo.
2896+
surfaceColor * (1.0f / maxCoeff), // normalize point color; 'darkness' is handled by size of point determined by GeomAlbedo.
28962897
false, false, m);
28972898
}
28982899
}

src/celengine/viewporteffect.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ bool ViewportEffect::preprocess(Renderer* renderer, FramebufferObject* fbo)
2828
bool ViewportEffect::prerender(Renderer* renderer, FramebufferObject* fbo, FramebufferObject* dst)
2929
{
3030
// For renderbuffer MSAA (desktop GL / GLES3), blit the MSAA color buffer into
31-
// the resolve texture before switching to the destination framebuffer.
32-
// For the GLES2 EXT path the resolve happens implicitly when the FBO is unbound.
31+
// the resolve texture before switching to the destination framebuffer
3332
if (!fbo->resolve())
3433
return false;
3534

src/celengine/viewporteffect.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class ViewportEffect
3030
virtual bool render(Renderer*, FramebufferObject*, int width, int height) = 0;
3131
virtual bool distortXY(float& x, float& y);
3232

33+
// Whether this effect needs its source FBO to use a floating-point
34+
// color buffer (GL_RGBA16F) instead of the default GL_RGBA8.
35+
virtual bool needsFloatSource() const { return false; }
3336
};
3437

3538
class PassthroughViewportEffect : public ViewportEffect

src/celestia/celestiacore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ void CelestiaCore::draw(View* view)
20002000
if (nEffects > 0)
20012001
{
20022002
// create/update FBOs for viewport effect chain
2003-
view->updateFBOs(nEffects, metrics.width, metrics.height);
2003+
view->updateFBOs(viewportEffects, metrics.width, metrics.height);
20042004
fbo = view->getFBO(0);
20052005
}
20062006
bool process = fbo != nullptr && viewportEffects[0]->preprocess(renderer, fbo);

src/celestia/hud.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ namespace celestia
5959
namespace
6060
{
6161

62+
constexpr Color InfoColor = Color(0.7f, 0.7f, 1.0f, 1.0f);
63+
constexpr Color FrameInfoColor = Color(0.6f, 0.6f, 1.0f, 1.0f);
64+
constexpr Color LightTravelColor = Color(0.42f, 1.0f, 1.0f, 1.0f);
65+
constexpr Color WarningColor = Color(1.0f, 0.0f, 0.0f, 1.0f);
66+
constexpr Color EditModeColor = Color(1.0f, 0.0f, 1.0f, 1.0f);
67+
6268
// Ye olde wolde conſtantes for ye olde wolde units
6369
constexpr double OneMiInKm = 1.609344;
6470
constexpr double OneFtInKm = 0.0003048;
@@ -943,7 +949,7 @@ Hud::renderOverlay(const WindowMetrics& metrics,
943949
m_overlay->savePos();
944950
m_overlay->moveBy(metrics.getSafeAreaStart(),
945951
metrics.getSafeAreaBottom(m_hudFonts.fontHeight() * 2 + static_cast<int>(static_cast<float>(metrics.screenDpi) / 25.4f * 1.3f)));
946-
m_overlay->setColor(0.7f, 0.7f, 1.0f, 1.0f);
952+
m_overlay->setColor(InfoColor);
947953

948954
m_overlay->beginText();
949955
m_overlay->print("\n");
@@ -983,7 +989,7 @@ Hud::renderOverlay(const WindowMetrics& metrics,
983989
m_overlay->beginText();
984990
int x = (metrics.getSafeAreaWidth() - engine::TextLayout::getTextWidth(_("Edit Mode"), m_hudFonts.font().get())) / 2;
985991
m_overlay->moveBy(metrics.getSafeAreaStart(x), metrics.getSafeAreaTop(m_hudFonts.fontHeight()));
986-
m_overlay->setColor(1, 0, 1, 1);
992+
m_overlay->setColor(EditModeColor);
987993
m_overlay->print(_("Edit Mode"));
988994
m_overlay->endText();
989995
m_overlay->restorePos();
@@ -1014,17 +1020,17 @@ Hud::renderTimeInfo(const WindowMetrics& metrics, const Simulation* sim, const T
10141020

10151021
// Time and date
10161022
m_overlay->savePos();
1017-
m_overlay->setColor(0.7f, 0.7f, 1.0f, 1.0f);
1023+
m_overlay->setColor(InfoColor);
10181024
m_overlay->moveBy(metrics.getSafeAreaEnd(m_dateStrWidth), metrics.getSafeAreaTop(m_hudFonts.fontHeight()));
10191025
m_overlay->beginText();
10201026

10211027
m_overlay->print(dateStr);
10221028

10231029
if (timeInfo.lightTravelFlag && lt > 0.0)
10241030
{
1025-
m_overlay->setColor(0.42f, 1.0f, 1.0f, 1.0f);
1031+
m_overlay->setColor(LightTravelColor);
10261032
m_overlay->print(_(" LT"));
1027-
m_overlay->setColor(0.7f, 0.7f, 1.0f, 1.0f);
1033+
m_overlay->setColor(InfoColor);
10281034
}
10291035
m_overlay->print("\n");
10301036

@@ -1050,7 +1056,7 @@ Hud::renderTimeInfo(const WindowMetrics& metrics, const Simulation* sim, const T
10501056

10511057
if (sim->getPauseState() == true)
10521058
{
1053-
m_overlay->setColor(1.0f, 0.0f, 0.0f, 1.0f);
1059+
m_overlay->setColor(WarningColor);
10541060
m_overlay->print(_(" (Paused)"));
10551061
}
10561062

@@ -1067,7 +1073,7 @@ Hud::renderFrameInfo(const WindowMetrics& metrics, const Simulation* sim)
10671073
metrics.getSafeAreaBottom(m_hudFonts.fontHeight() * 3 +
10681074
static_cast<int>(static_cast<float>(metrics.screenDpi) / 25.4f * 1.3f)));
10691075
m_overlay->beginText();
1070-
m_overlay->setColor(0.6f, 0.6f, 1.0f, 1);
1076+
m_overlay->setColor(FrameInfoColor);
10711077

10721078
if (sim->getObserverMode() == Observer::ObserverMode::Travelling)
10731079
{
@@ -1116,7 +1122,7 @@ Hud::renderFrameInfo(const WindowMetrics& metrics, const Simulation* sim)
11161122
break;
11171123
}
11181124

1119-
m_overlay->setColor(0.7f, 0.7f, 1.0f, 1.0f);
1125+
m_overlay->setColor(InfoColor);
11201126

11211127
// Field of view
11221128
const Observer* activeObserver = sim->getActiveObserver();
@@ -1133,7 +1139,7 @@ Hud::renderSelectionInfo(const WindowMetrics& metrics,
11331139
const Eigen::Vector3d& v)
11341140
{
11351141
m_overlay->savePos();
1136-
m_overlay->setColor(0.7f, 0.7f, 1.0f, 1.0f);
1142+
m_overlay->setColor(InfoColor);
11371143
m_overlay->moveBy(metrics.getSafeAreaStart(), metrics.getSafeAreaTop(m_hudFonts.titleFontHeight()));
11381144

11391145
m_overlay->beginText();
@@ -1282,7 +1288,7 @@ Hud::renderMovieCapture(const WindowMetrics& metrics, const MovieCapture& movieC
12821288
int movieWidth = movieCapture.getWidth();
12831289
int movieHeight = movieCapture.getHeight();
12841290
m_overlay->savePos();
1285-
Color color(1.0f, 0.0f, 0.0f, 1.0f);
1291+
const Color& color = WarningColor;
12861292
m_overlay->setColor(color);
12871293
celestia::Rect r(static_cast<float>((metrics.width - movieWidth) / 2 - 1),
12881294
static_cast<float>((metrics.height - movieHeight) / 2 - 1),

src/celestia/textinput.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace celestia
3030

3131
namespace
3232
{
33-
constexpr Color consoleColor{ 0.7f, 0.7f, 1.0f, 0.2f };
33+
constexpr Color consoleColor = Color(0.7f, 0.7f, 1.0f, 0.2f);
34+
constexpr Color inputTextColor = Color(0.6f, 0.6f, 1.0f, 1.0f);
35+
constexpr Color selectedCompletion = Color(1.0f, 0.6f, 0.6f, 1.0f);
3436
}
3537

3638
std::string_view
@@ -188,7 +190,7 @@ TextInput::render(Overlay* overlay,
188190
r.setColor(consoleColor);
189191
overlay->drawRectangle(r);
190192
overlay->moveBy(metrics.getSafeAreaStart(), metrics.getSafeAreaBottom(rectHeight - hudFonts.titleFontHeight()));
191-
overlay->setColor(0.6f, 0.6f, 1.0f, 1.0f);
193+
overlay->setColor(inputTextColor);
192194
overlay->beginText();
193195
overlay->print(fmt::runtime(_("Target name: {}")), m_text);
194196
overlay->endText();
@@ -224,9 +226,9 @@ TextInput::renderCompletion(Overlay* overlay, const WindowMetrics& metrics, int
224226
for (int j = 0; iter < m_completion.end() && j < nb_lines; iter++, j++)
225227
{
226228
if (i * nb_lines + j == typedTextCompletionIdx - start)
227-
overlay->setColor(1.0f, 0.6f, 0.6f, 1);
229+
overlay->setColor(selectedCompletion);
228230
else
229-
overlay->setColor(0.6f, 0.6f, 1.0f, 1);
231+
overlay->setColor(inputTextColor);
230232
overlay->print(iter->getName());
231233
overlay->print("\n");
232234
}

0 commit comments

Comments
 (0)