summaryrefslogtreecommitdiff
path: root/modules/openxr/extensions/openxr_opengl_extension.cpp
diff options
context:
space:
mode:
authorBastiaan Olij <mux213@gmail.com>2023-03-14 14:17:24 +1100
committerYuri Sizov <yuris@humnom.net>2023-03-27 17:29:30 +0200
commitf089782411eaecde5a858607b6c6c7d218ee162a (patch)
treed9312a1926d282b4acfbe7466315db9f9814b9a1 /modules/openxr/extensions/openxr_opengl_extension.cpp
parentdd94380b11a741a692948e496c34e5e5c24cf930 (diff)
XR: When an sRGB target is used, check hardware sRGB conversion
(cherry picked from commit a1a52c5ba19efee004b34cf2e64278aef9af70b6)
Diffstat (limited to 'modules/openxr/extensions/openxr_opengl_extension.cpp')
-rw-r--r--modules/openxr/extensions/openxr_opengl_extension.cpp41
1 files changed, 40 insertions, 1 deletions
diff --git a/modules/openxr/extensions/openxr_opengl_extension.cpp b/modules/openxr/extensions/openxr_opengl_extension.cpp
index 0d201161f1..20ccfe3906 100644
--- a/modules/openxr/extensions/openxr_opengl_extension.cpp
+++ b/modules/openxr/extensions/openxr_opengl_extension.cpp
@@ -37,6 +37,28 @@
#include "servers/rendering/rendering_server_globals.h"
#include "servers/rendering_server.h"
+// OpenXR requires us to submit sRGB textures so that it recognises the content
+// as being in sRGB color space. We do fall back on "normal" textures but this
+// will likely result in incorrect colors as OpenXR will double the sRGB conversion.
+// All major XR runtimes support sRGB textures.
+
+// In OpenGL output of the fragment shader is assumed to be in the color space of
+// the developers choice, however a linear to sRGB HW conversion can be enabled
+// through enabling GL_FRAMEBUFFER_SRGB if an sRGB color attachment is used.
+// This is a global setting.
+// See: https://www.khronos.org/opengl/wiki/Framebuffer
+
+// In OpenGLES output of the fragment shader is assumed to be in linear color space
+// and will be converted by default to sRGB if an sRGB color attachment is used.
+// The extension GL_EXT_sRGB_write_control was introduced to enable turning this
+// feature off.
+// See: https://registry.khronos.org/OpenGL/extensions/EXT/EXT_sRGB_write_control.txt
+
+// On OpenGLES this is not defined in our standard headers..
+#ifndef GL_FRAMEBUFFER_SRGB
+#define GL_FRAMEBUFFER_SRGB 0x8DB9
+#endif
+
HashMap<String, bool *> OpenXROpenGLExtension::get_requested_extensions() {
HashMap<String, bool *> request_extensions;
@@ -157,8 +179,8 @@ void *OpenXROpenGLExtension::set_session_create_and_get_next_pointer(void *p_nex
}
void OpenXROpenGLExtension::get_usable_swapchain_formats(Vector<int64_t> &p_usable_swap_chains) {
- p_usable_swap_chains.push_back(GL_RGBA8);
p_usable_swap_chains.push_back(GL_SRGB8_ALPHA8);
+ p_usable_swap_chains.push_back(GL_RGBA8);
}
void OpenXROpenGLExtension::get_usable_depth_formats(Vector<int64_t> &p_usable_depth_formats) {
@@ -168,6 +190,23 @@ void OpenXROpenGLExtension::get_usable_depth_formats(Vector<int64_t> &p_usable_d
p_usable_depth_formats.push_back(GL_DEPTH_COMPONENT24);
}
+void OpenXROpenGLExtension::on_pre_draw_viewport(RID p_render_target) {
+ if (srgb_ext_is_available) {
+ hw_linear_to_srgb_is_enabled = glIsEnabled(GL_FRAMEBUFFER_SRGB);
+ if (hw_linear_to_srgb_is_enabled) {
+ // Disable this.
+ glDisable(GL_FRAMEBUFFER_SRGB);
+ }
+ }
+}
+
+void OpenXROpenGLExtension::on_post_draw_viewport(RID p_render_target) {
+ if (srgb_ext_is_available && hw_linear_to_srgb_is_enabled) {
+ // Re-enable this.
+ glEnable(GL_FRAMEBUFFER_SRGB);
+ }
+}
+
bool OpenXROpenGLExtension::get_swapchain_image_data(XrSwapchain p_swapchain, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size, void **r_swapchain_graphics_data) {
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
ERR_FAIL_NULL_V(texture_storage, false);