From d36a34a8b6df785ddbb1c974aeddf82a42137854 Mon Sep 17 00:00:00 2001 From: Rhys Mainwaring Date: Sat, 23 Oct 2021 08:33:37 +0100 Subject: [PATCH] [Metal] Add cross-platform gamma adjustment for texture rendered into 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 Move header files with git mv (#395) Signed-off-by: methylDragon Create redirection aliases (#395) Signed-off-by: methylDragon Migrate sources in src, test, examples, and include (#395) Signed-off-by: methylDragon Migrate CMake files (#395) Signed-off-by: methylDragon Update name and logo on UI, examples and API docs (#398) Signed-off-by: Louise Poubel --- include/gz/gui/qml/IgnGammaAdjust.qml | 106 ++++++++++++++++++ include/gz/gui/qml/private/gammaadjust.frag | 19 ++++ .../gz/gui/qml/private/gammaadjust.frag.qsb | Bin 0 -> 1634 bytes .../shaders/+glslcore/gammaadjust.frag | 13 +++ .../qml/private/shaders/+qsb/gammaadjust.frag | Bin 0 -> 1634 bytes .../gui/qml/private/shaders/gammaadjust.frag | 10 ++ include/gz/gui/resources.qrc | 5 + src/plugins/minimal_scene/MinimalScene.qml | 3 +- src/plugins/scene3d/Scene3D.qml | 3 +- 9 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 include/gz/gui/qml/IgnGammaAdjust.qml create mode 100644 include/gz/gui/qml/private/gammaadjust.frag create mode 100644 include/gz/gui/qml/private/gammaadjust.frag.qsb create mode 100644 include/gz/gui/qml/private/shaders/+glslcore/gammaadjust.frag create mode 100644 include/gz/gui/qml/private/shaders/+qsb/gammaadjust.frag create mode 100644 include/gz/gui/qml/private/shaders/gammaadjust.frag diff --git a/include/gz/gui/qml/IgnGammaAdjust.qml b/include/gz/gui/qml/IgnGammaAdjust.qml new file mode 100644 index 000000000..07d3ba427 --- /dev/null +++ b/include/gz/gui/qml/IgnGammaAdjust.qml @@ -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" + } +} diff --git a/include/gz/gui/qml/private/gammaadjust.frag b/include/gz/gui/qml/private/gammaadjust.frag new file mode 100644 index 000000000..e85d4aa09 --- /dev/null +++ b/include/gz/gui/qml/private/gammaadjust.frag @@ -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; +} diff --git a/include/gz/gui/qml/private/gammaadjust.frag.qsb b/include/gz/gui/qml/private/gammaadjust.frag.qsb new file mode 100644 index 0000000000000000000000000000000000000000..8c7b8a2c6627fce158917ff85bd3c0806073093c GIT binary patch literal 1634 zcmV-o2A%l;02YaOob6cMZ_`#3ze&?(&G_i}T44iR%D{olb<>nVLrPJ*4Kz?0t=Ocf zvRuarX0aV?2T~By9{0rK{?a|{W!lcU_t?31(rR>K)3mvA?Q`z=_?@rY>oUeB8Dj;w zb8yeH=PY6d+h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5m{ZMYI8LN8x#ie8lt|*+Clh zF3|ctps&U#IX#)s^NGxmS7f3hF)v4QYZ5O{ybbPeBH^L$at2$Ou*Gy$!gdn0@_c=Q zX9C~Pz#Ar5f%{{SFH*t_oL7MC3XFK4Wz&#vv7RklOn(CQuGA8ME$=`4u>mq>J^MmTX~s^F%j*#yT9M8Z;>XCn(3?ffbZHNixV^1rO82YfyijCLf#N z4fA$}c&DXaK#X_6-Z|bALjEk-z_>pp{t>W<^m)=#sh+t02v$xp(ZA=V`zNrA{EOuK zf`m;;uuXG%j(8U-XOmL@#Iemu`j<1YeMY>?V`9TPeonQ&F2$x1KgPd6y!ghLk*|}z zbL3}%FwN6igZh1mY8dCUHYT6T#D7bNwNuiZ%v!obHi=dt|rw#UA zDGQ2g4pd#!^`%?-jp}l{k`a1Y)&_%tu$4t4K&oI{8&uGRwkIC1My;gKn5}1hC}1l# z)bn4eBiJg#jzS2rg(R1W|FpGhS=XajKqlSSP>ND%w2BHZ$w-GN&{Q4UaPU2@SE^4M zvV@jy$T$Wnw_uEMEiCw)5JECXOQm7h)qLJ<7&0$o;7v)|LvTYG4EK=y+4&cC5IVl6 zELQcTt6@W-;9P9QWWSJSUQXeV%WEduyYD$|KXCc(1UZ*D)U?$NYHFMXGuU&yj`G5` zvU4ovBsC%+H98e!h8&4VTam(}!GjP_7@{74|EcNqu{$i}U!d4v)K>fjNJ7tZ>Wx@UE7mfi#Ihk2vY`{@%d$E zr=nQC2c*ZNzk&sTb@9!?VO5fTZi!=ka;k4Oqml/images/export_icon.png qml/images/search.svg + qml/IgnGammaAdjust.qml + qml/private/shaders/gammaadjust.frag + qml/private/shaders/+glslcore/gammaadjust.frag + qml/private/shaders/+qsb/gammaadjust.frag + qml/GzCard.qml qml/GzCardSettings.qml qml/GzHelpers.qml diff --git a/src/plugins/minimal_scene/MinimalScene.qml b/src/plugins/minimal_scene/MinimalScene.qml index 5969a31c6..15ea4a48d 100644 --- a/src/plugins/minimal_scene/MinimalScene.qml +++ b/src/plugins/minimal_scene/MinimalScene.qml @@ -19,6 +19,7 @@ import QtQuick 2.9 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import RenderWindow 1.0 +import "qrc:/qml" Rectangle { Layout.minimumWidth: 200 @@ -57,7 +58,7 @@ Rectangle { /* * Gamma correction for sRGB output. Enabled when engine is set to ogre2 */ - GammaAdjust { + IgnGammaAdjust { anchors.fill: renderWindow source: renderWindow gamma: 2.4 diff --git a/src/plugins/scene3d/Scene3D.qml b/src/plugins/scene3d/Scene3D.qml index 817dc9413..2f18acdfb 100644 --- a/src/plugins/scene3d/Scene3D.qml +++ b/src/plugins/scene3d/Scene3D.qml @@ -19,6 +19,7 @@ import QtQuick 2.9 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import RenderWindow 1.0 +import "qrc:/qml" Rectangle { Layout.minimumWidth: 200 @@ -57,7 +58,7 @@ Rectangle { /* * Gamma correction for sRGB output. Enabled when engine is set to ogre2 */ - GammaAdjust { + IgnGammaAdjust { anchors.fill: renderWindow source: renderWindow gamma: 2.4