From 26ad42b9657e9c348f4bd3f637a5da3bef57a79c Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Sun, 24 Feb 2019 19:07:56 -0800 Subject: [PATCH] FIxing the gamma correction for Quest, intoducing simple gpu lib conversion shaders --- .../display-plugins/OpenGLDisplayPlugin.cpp | 22 +++++++++++----- libraries/gpu/src/gpu/Color.slh | 21 ++++++++++++--- .../src/gpu/DrawTextureGammaLinearToSRGB.slf | 26 +++++++++++++++++++ .../src/gpu/DrawTextureGammaLinearToSRGB.slp | 1 + .../src/gpu/DrawTextureGammaSRGBToLinear.slf | 26 +++++++++++++++++++ .../src/gpu/DrawTextureGammaSRGBToLinear.slp | 1 + .../oculusMobile/src/ovr/Framebuffer.cpp | 11 ++++---- 7 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slf create mode 100644 libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slp create mode 100644 libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slf create mode 100644 libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slp diff --git a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp index 28de13d8c2..c536e6b6e2 100644 --- a/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp +++ b/libraries/display-plugins/src/display-plugins/OpenGLDisplayPlugin.cpp @@ -380,16 +380,26 @@ void OpenGLDisplayPlugin::customizeContext() { scissorState->setScissorEnable(true); { +#ifdef Q_OS_ANDROID + gpu::ShaderPointer program = gpu::Shader::createProgram(shader::gpu::program::DrawTextureGammaLinearToSRGB); +#else gpu::ShaderPointer program = gpu::Shader::createProgram(shader::gpu::program::DrawTexture); - _simplePipeline = gpu::Pipeline::create(program, scissorState); - _hudPipeline = gpu::Pipeline::create(program, blendState); +#endif + _simplePipeline = gpu::Pipeline::create(program, scissorState); } - { - gpu::ShaderPointer program = gpu::Shader::createProgram(shader::display_plugins::program::SrgbToLinear); +#ifdef Q_OS_ANDROID + gpu::ShaderPointer program = gpu::Shader::createProgram(shader::gpu::program::DrawTextureGammaLinearToSRGB); +#else + gpu::ShaderPointer program = gpu::Shader::createProgram(shader::gpu::program::DrawTextureGammaSRGBToLinear); +#endif _presentPipeline = gpu::Pipeline::create(program, scissorState); } + { + gpu::ShaderPointer program = gpu::Shader::createProgram(shader::gpu::program::DrawTexture); + _hudPipeline = gpu::Pipeline::create(program, blendState); + } { gpu::ShaderPointer program = gpu::Shader::createProgram(shader::gpu::program::DrawTextureMirroredX); _mirrorHUDPipeline = gpu::Pipeline::create(program, blendState); @@ -516,7 +526,6 @@ void OpenGLDisplayPlugin::renderFromTexture(gpu::Batch& batch, const gpu::Textur #ifndef USE_GLES batch.setPipeline(_presentPipeline); #else - //batch.setPipeline(_presentPipeline); batch.setPipeline(_simplePipeline); #endif batch.draw(gpu::TRIANGLE_STRIP, 4); @@ -631,8 +640,7 @@ void OpenGLDisplayPlugin::compositeScene() { batch.setStateScissorRect(ivec4(uvec2(), _compositeFramebuffer->getSize())); batch.resetViewTransform(); batch.setProjectionTransform(mat4()); - // batch.setPipeline(_simplePipeline); - batch.setPipeline(_presentPipeline); + batch.setPipeline(_simplePipeline); batch.setResourceTexture(0, _currentFrame->framebuffer->getRenderBuffer(0)); batch.draw(gpu::TRIANGLE_STRIP, 4); }); diff --git a/libraries/gpu/src/gpu/Color.slh b/libraries/gpu/src/gpu/Color.slh index 65ddc0b01e..af61e5a34b 100644 --- a/libraries/gpu/src/gpu/Color.slh +++ b/libraries/gpu/src/gpu/Color.slh @@ -16,10 +16,10 @@ // YCoCg =====> Luma (Y) chrominance green (Cg) and chrominance orange (Co) // https://software.intel.com/en-us/node/503873 +// sRGB ====> Linear float color_scalar_sRGBToLinear(float value) { - const float SRGB_ELBOW = 0.04045; - - return mix(pow((value + 0.055) / 1.055, 2.4), value / 12.92, float(value <= SRGB_ELBOW)); + // Same as pow(value, 2.2) + return mix(pow((value + 0.055) / 1.055, 2.4), value / 12.92, float(value <= 0.04045)); } vec3 color_sRGBToLinear(vec3 srgb) { @@ -30,6 +30,21 @@ vec4 color_sRGBAToLinear(vec4 srgba) { return vec4(color_sRGBToLinear(srgba.xyz), srgba.w); } +// Linear ====> sRGB +float color_scalar_LinearTosRGB(float value) { + // Same as return pow(value, 1/2.2) + return mix(1.055 * pow(value, 0.41666) - 0.055, value * 12.92, float(value < 0.0031308)); +} + +vec3 color_LinearTosRGB(vec3 lrgb) { + // Same as return pow(lrgb, 1/2.2) + return vec3(color_scalar_LinearTosRGB(lrgb.r), color_scalar_LinearTosRGB(lrgb.g), color_scalar_LinearTosRGB(lrgb.b)); +} + +vec4 color_LinearTosRGBA(vec4 lrgba) { + return vec4(color_LinearTosRGB(lrgba.xyz), lrgba.w); +} + vec3 color_LinearToYCoCg(vec3 rgb) { // Y = R/4 + G/2 + B/4 // Co = R/2 - B/2 diff --git a/libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slf b/libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slf new file mode 100644 index 0000000000..017df1d88d --- /dev/null +++ b/libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slf @@ -0,0 +1,26 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// DrawTexture.frag +// +// Draw texture 0 fetched at texcoord.xy +// +// Created by Sam Gateau on 6/22/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +<@include gpu/Color.slh@> + + +LAYOUT(binding=0) uniform sampler2D colorMap; + +layout(location=0) in vec2 varTexCoord0; +layout(location=0) out vec4 outFragColor; + +void main(void) { + outFragColor = color_LinearTosRGBA(texture(colorMap, varTexCoord0)); +} diff --git a/libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slp b/libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slp new file mode 100644 index 0000000000..f922364b75 --- /dev/null +++ b/libraries/gpu/src/gpu/DrawTextureGammaLinearToSRGB.slp @@ -0,0 +1 @@ +VERTEX DrawUnitQuadTexcoord diff --git a/libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slf b/libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slf new file mode 100644 index 0000000000..048384fe6c --- /dev/null +++ b/libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slf @@ -0,0 +1,26 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// DrawTexture.frag +// +// Draw texture 0 fetched at texcoord.xy +// +// Created by Sam Gateau on 6/22/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +<@include gpu/Color.slh@> + + +LAYOUT(binding=0) uniform sampler2D colorMap; + +layout(location=0) in vec2 varTexCoord0; +layout(location=0) out vec4 outFragColor; + +void main(void) { + outFragColor = color_sRGBAToLinear(texture(colorMap, varTexCoord0)); +} diff --git a/libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slp b/libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slp new file mode 100644 index 0000000000..f922364b75 --- /dev/null +++ b/libraries/gpu/src/gpu/DrawTextureGammaSRGBToLinear.slp @@ -0,0 +1 @@ +VERTEX DrawUnitQuadTexcoord diff --git a/libraries/oculusMobile/src/ovr/Framebuffer.cpp b/libraries/oculusMobile/src/ovr/Framebuffer.cpp index 4c4fd2a983..0f59eef614 100644 --- a/libraries/oculusMobile/src/ovr/Framebuffer.cpp +++ b/libraries/oculusMobile/src/ovr/Framebuffer.cpp @@ -32,18 +32,19 @@ void Framebuffer::create(const glm::uvec2& size) { _validTexture = false; // Depth renderbuffer - glGenRenderbuffers(1, &_depth); + /* glGenRenderbuffers(1, &_depth); glBindRenderbuffer(GL_RENDERBUFFER, _depth); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, _size.x, _size.y); glBindRenderbuffer(GL_RENDERBUFFER, 0); - +*/ // Framebuffer glGenFramebuffers(1, &_fbo); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo); - glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depth); - glBindFramebuffer(GL_FRAMEBUFFER, 0); + // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo); + // glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depth); + // glBindFramebuffer(GL_FRAMEBUFFER, 0); _swapChain = vrapi_CreateTextureSwapChain3(VRAPI_TEXTURE_TYPE_2D, GL_RGBA8, _size.x, _size.y, 1, 3); + _length = vrapi_GetTextureSwapChainLength(_swapChain); if (!_length) { __android_log_write(ANDROID_LOG_WARN, "QQQ_OVR", "Unable to count swap chain textures");