Skip to content

Commit

Permalink
[Metal] Add cross-platform gamma adjustment for texture rendered into…
Browse files Browse the repository at this point in the history
… QML

- Adapt the GammaAdjust element from QtGraphicalEffects to work with Metal
- Add shaders from Qt
- Create a Vulkan supported version of the fragment shader
- Generate a shader pack using qsb
- Refactor gamma adjustment QML
- Move to own QML file and locate with ignition/gui/qml
- Move shaders to a private subfolder
- Update resources.qrc
- Replace import QtGraphicalEffects 1.0 with import "qrc:/qml"

Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>
  • Loading branch information
srmainwaring committed Nov 30, 2021
1 parent f8af480 commit f665cbd
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 4 deletions.
106 changes: 106 additions & 0 deletions include/ignition/gui/qml/IgnGammaAdjust.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/*
* IgnGammaAdjust.qml is based upon GammaAdjust.qml from the QtGraphicalEffects
* module in Qt 5.15.2 which has the license listed below.
*
* The
*/

/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Graphical Effects module.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.9

Item {
id: rootItem

property variant source

property real gamma: 1.0

property bool cached: false

// Replaces SourceProxy in the QtGraphicalEffects version
ShaderEffectSource {
id: sourceProxy
sourceItem: rootItem.source
visible: rootItem.visible
smooth: true
live: true
hideSource: visible
}

// Output
ShaderEffectSource {
id: cacheItem
anchors.fill: parent
visible: rootItem.cached
smooth: true
sourceItem: shaderItem
live: true
hideSource: visible
}

// Apply the gamma adjustment to the source proxy
ShaderEffect {
id: shaderItem
property variant source: sourceProxy
property real gamma: 1.0 / Math.max(rootItem.gamma, 0.0001)

anchors.fill: parent

fragmentShader: "qrc:qml/private/shaders/gammaadjust.frag"
}
}
19 changes: 19 additions & 0 deletions include/ignition/gui/qml/private/gammaadjust.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 440

layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;

layout(std140, binding = 0) uniform buf {
mat4 qt_Matrix;
float qt_Opacity;
float gamma;
};

layout(binding = 1) uniform sampler2D source;

void main() {
vec4 originalColor = texture(source, qt_TexCoord0);
originalColor.rgb = originalColor.rgb / max(1.0/256.0, originalColor.a);
vec3 adjustedColor = pow(originalColor.rgb, vec3(gamma));
fragColor = vec4(adjustedColor * originalColor.a, originalColor.a) * qt_Opacity;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 150 core
in vec2 qt_TexCoord0;
uniform float qt_Opacity;
uniform sampler2D source;
uniform float gamma;
out vec4 fragColor;

void main(void) {
vec4 originalColor = texture(source, qt_TexCoord0.st);
originalColor.rgb = originalColor.rgb / max(1.0/256.0, originalColor.a);
vec3 adjustedColor = pow(originalColor.rgb, vec3(gamma));
fragColor = vec4(adjustedColor * originalColor.a, originalColor.a) * qt_Opacity;
}
Binary file not shown.
10 changes: 10 additions & 0 deletions include/ignition/gui/qml/private/shaders/gammaadjust.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
varying highp vec2 qt_TexCoord0;
uniform highp float qt_Opacity;
uniform lowp sampler2D source;
uniform highp float gamma;
void main(void) {
highp vec4 originalColor = texture2D(source, qt_TexCoord0.st);
originalColor.rgb = originalColor.rgb / max(1.0/256.0, originalColor.a);
highp vec3 adjustedColor = pow(originalColor.rgb, vec3(gamma));
gl_FragColor = vec4(adjustedColor * originalColor.a, originalColor.a) * qt_Opacity;
}
5 changes: 5 additions & 0 deletions include/ignition/gui/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@
<file>qml/images/menu.png</file>
<file>qml/images/export_icon.png</file>
<file>qml/images/search.svg</file>

<file>qml/IgnGammaAdjust.qml</file>
<file>qml/private/shaders/gammaadjust.frag</file>
<file>qml/private/shaders/+glslcore/gammaadjust.frag</file>
<file>qml/private/shaders/+qsb/gammaadjust.frag</file>
</qresource>
</RCC>
4 changes: 2 additions & 2 deletions src/plugins/minimal_scene/MinimalScene.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import QtQuick 2.9
import QtQuick.Controls 2.0
import RenderWindow 1.0
import QtGraphicalEffects 1.0
import "qrc:/qml"

Rectangle {
width: 1000
Expand Down Expand Up @@ -53,7 +53,7 @@ Rectangle {
/*
* Gamma correction for sRGB output. Enabled when engine is set to ogre2
*/
GammaAdjust {
IgnGammaAdjust {
anchors.fill: renderWindow
source: renderWindow
gamma: 2.4
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/scene3d/Scene3D.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import QtQuick 2.9
import QtQuick.Controls 2.0
import RenderWindow 1.0
import QtGraphicalEffects 1.0
import "qrc:/qml"

Rectangle {
width: 1000
Expand Down Expand Up @@ -53,7 +53,7 @@ Rectangle {
/*
* Gamma correction for sRGB output. Enabled when engine is set to ogre2
*/
GammaAdjust {
IgnGammaAdjust {
anchors.fill: renderWindow
source: renderWindow
gamma: 2.4
Expand Down

0 comments on commit f665cbd

Please sign in to comment.