From 6fc386cfe9ab9d1c9ae777550b8adf6ca195b211 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 10 Jan 2025 11:40:38 +0200 Subject: [PATCH] Move shaders to separate files Signed-off-by: Trial97 --- launcher/CMakeLists.txt | 2 + launcher/main.cpp | 2 + launcher/resources/shaders/fshader.glsl | 21 ++++++++ launcher/resources/shaders/shaders.qrc | 6 +++ launcher/resources/shaders/vshader.glsl | 26 ++++++++++ .../dialogs/skins/draw/SkinOpenGLWidget.cpp | 49 +------------------ 6 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 launcher/resources/shaders/fshader.glsl create mode 100644 launcher/resources/shaders/shaders.qrc create mode 100644 launcher/resources/shaders/vshader.glsl diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 58560469c..daea70a2c 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -810,6 +810,7 @@ SET(LAUNCHER_SOURCES resources/flat/flat.qrc resources/flat_white/flat_white.qrc resources/documents/documents.qrc + resources/shaders/shaders.qrc ../${Launcher_Branding_LogoQRC} # Icons @@ -1252,6 +1253,7 @@ qt_add_resources(LAUNCHER_RESOURCES resources/iOS/iOS.qrc resources/flat/flat.qrc resources/documents/documents.qrc + resources/shaders/shaders.qrc ../${Launcher_Branding_LogoQRC} ) diff --git a/launcher/main.cpp b/launcher/main.cpp index 35f545f52..c41c510dd 100644 --- a/launcher/main.cpp +++ b/launcher/main.cpp @@ -84,6 +84,8 @@ int main(int argc, char* argv[]) Q_INIT_RESOURCE(iOS); Q_INIT_RESOURCE(flat); Q_INIT_RESOURCE(flat_white); + + Q_INIT_RESOURCE(shaders); return app.exec(); } case Application::Failed: diff --git a/launcher/resources/shaders/fshader.glsl b/launcher/resources/shaders/fshader.glsl new file mode 100644 index 000000000..7bdf6c178 --- /dev/null +++ b/launcher/resources/shaders/fshader.glsl @@ -0,0 +1,21 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +// https://code.qt.io/cgit/qt/qtbase.git/tree/examples/opengl/cube/fshader.glsl +#ifdef GL_ES +// Set default precision to medium +precision mediump int; +precision mediump float; +#endif + +uniform sampler2D texture; + +varying vec2 v_texcoord; + +void main() +{ + // Set fragment color from texture + // gl_FragColor = texture2D(texture, v_texcoord); + vec4 texColor = texture2D(texture, v_texcoord); + if (texColor.a < 0.1) discard; // Optional: Discard fully transparent pixels + gl_FragColor = texColor; +} diff --git a/launcher/resources/shaders/shaders.qrc b/launcher/resources/shaders/shaders.qrc new file mode 100644 index 000000000..835e0fea7 --- /dev/null +++ b/launcher/resources/shaders/shaders.qrc @@ -0,0 +1,6 @@ + + + vshader.glsl + fshader.glsl + + \ No newline at end of file diff --git a/launcher/resources/shaders/vshader.glsl b/launcher/resources/shaders/vshader.glsl new file mode 100644 index 000000000..2d5e2db30 --- /dev/null +++ b/launcher/resources/shaders/vshader.glsl @@ -0,0 +1,26 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +// https://code.qt.io/cgit/qt/qtbase.git/tree/examples/opengl/cube/vshader.glsl +#ifdef GL_ES +// Set default precision to medium +precision mediump int; +precision mediump float; +#endif + +uniform mat4 mvp_matrix; +uniform mat4 model_matrix; + +attribute vec4 a_position; +attribute vec2 a_texcoord; + +varying vec2 v_texcoord; + +void main() +{ + // Calculate vertex position in screen space + gl_Position = mvp_matrix * model_matrix * a_position; + + // Pass texture coordinate to fragment shader + // Value will be automatically interpolated to fragments inside polygon faces + v_texcoord = a_texcoord; +} diff --git a/launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.cpp b/launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.cpp index 776307aba..7a1eeac44 100644 --- a/launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.cpp +++ b/launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.cpp @@ -70,56 +70,11 @@ void SkinOpenGLWidget::initializeGL() void SkinOpenGLWidget::initShaders() { // Compile vertex shader - if (!m_program.addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, R"(// Copyright (C) 2024 The Qt Company Ltd. - // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - #ifdef GL_ES - // Set default precision to medium - precision mediump int; - precision mediump float; - #endif - - uniform mat4 mvp_matrix; - uniform mat4 model_matrix; - - attribute vec4 a_position; - attribute vec2 a_texcoord; - - varying vec2 v_texcoord; - - void main() - { - // Calculate vertex position in screen space - gl_Position = mvp_matrix * model_matrix * a_position; - - // Pass texture coordinate to fragment shader - // Value will be automatically interpolated to fragments inside polygon faces - v_texcoord = a_texcoord; - } - )")) + if (!m_program.addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl")) close(); // Compile fragment shader - if (!m_program.addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, R"(// Copyright (C) 2024 The Qt Company Ltd. - // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - #ifdef GL_ES - // Set default precision to medium - precision mediump int; - precision mediump float; - #endif - - uniform sampler2D texture; - - varying vec2 v_texcoord; - - void main() - { - // Set fragment color from texture - // gl_FragColor = texture2D(texture, v_texcoord); - vec4 texColor = texture2D(texture, v_texcoord); - if (texColor.a < 0.1) discard; // Optional: Discard fully transparent pixels - gl_FragColor = texColor; - } - )")) + if (!m_program.addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl")) close(); // Link shader pipeline