diff --git a/playground/CMakeLists.txt b/playground/CMakeLists.txt index 98e4af85..8bab46b2 100644 --- a/playground/CMakeLists.txt +++ b/playground/CMakeLists.txt @@ -6,10 +6,7 @@ add_subdirectory(invoker) add_subdirectory(shadows) add_subdirectory(shapes) add_subdirectory(charts) - -if (QT_VERSION_MAJOR VERSION_LESS 6) - add_subdirectory(parrots) -endif() +add_subdirectory(parrots) if (BUILD_INPUTCONTEXT) add_subdirectory(inputpanel) diff --git a/playground/parrots/CMakeLists.txt b/playground/parrots/CMakeLists.txt index 2fa72156..94010312 100644 --- a/playground/parrots/CMakeLists.txt +++ b/playground/parrots/CMakeLists.txt @@ -4,6 +4,6 @@ ############################################################################ set(SOURCES main.cpp) -qt_add_resources(SOURCES images.qrc) +qt_add_resources(SOURCES images.qrc shaders.qrc) qsk_add_example(parrots ${SOURCES}) diff --git a/playground/parrots/main.cpp b/playground/parrots/main.cpp index bc2e3dc5..04404e93 100644 --- a/playground/parrots/main.cpp +++ b/playground/parrots/main.cpp @@ -23,31 +23,13 @@ #include #include -static const char* vertexShader = R"( - uniform highp mat4 qt_Matrix; - attribute highp vec4 qt_Vertex; - attribute highp vec2 qt_MultiTexCoord0; - varying highp vec2 coord; - void main() { - coord = qt_MultiTexCoord0; - gl_Position = qt_Matrix * qt_Vertex; - })"; +#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) + #include +#else + #include +#endif -static const char* fragmentShader = R"( - varying highp vec2 coord; - uniform sampler2D source; - uniform lowp float qt_Opacity; - - void main() { - vec2 delta = vec2(0.01, 0.01); - gl_FragColor =(0.0538 * texture2D(source, coord - 3.182 * delta) - + 0.3229 * texture2D(source, coord - 1.364 * delta) - + 0.2466 * texture2D(source, coord) - + 0.3229 * texture2D(source, coord + 1.364 * delta) - + 0.0538 * texture2D(source, coord + 3.182 * delta)) * qt_Opacity; - })"; - -class ButtonBox: public QskLinearBox +class ButtonBox : public QskLinearBox { public: ButtonBox( QQuickItem* parent = nullptr ) @@ -67,8 +49,13 @@ class ShaderEffect : public QQuickShaderEffect ShaderEffect( QQuickItem* parent = nullptr ) : QQuickShaderEffect( parent ) { - setVertexShader( ::vertexShader ); - setFragmentShader( ::fragmentShader ); +#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) + setVertexShader( loadShader( ":/shaders/blur.vert" ) ); + setFragmentShader( loadShader( ":/shaders/blur.frag" ) ); +#else + setVertexShader( QUrl( "qrc:/shaders/blur.vert.qsb" ) ); + setFragmentShader( QUrl( "qrc:/shaders/blur.frag.qsb" ) ); +#endif m_source = new QQuickShaderEffectSource( this ); m_source->setLive( true ); @@ -80,17 +67,37 @@ class ShaderEffect : public QQuickShaderEffect QQuickItem* sourceItem() const { return m_source->sourceItem(); } protected: +#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) void geometryChanged( const QRectF& newRect, const QRectF& oldRect ) override { Inherited::geometryChanged( newRect, oldRect ); + updateSourceRect( newRect ); + } +#else + void geometryChange( const QRectF& newRect, const QRectF& oldRect ) override + { + Inherited::geometryChange( newRect, oldRect ); + updateSourceRect( newRect ); + } +#endif - auto sourceRect = newRect; - sourceRect = m_source->sourceItem()->mapRectFromItem( this, sourceRect ); - + private: + void updateSourceRect( const QRectF& rect ) + { + const auto sourceRect = m_source->sourceItem()->mapRectFromItem( this, rect ); m_source->setSourceRect( sourceRect ); } - private: +#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) + QByteArray loadShader( const char* path ) const + { + QFile f( path ); + f.open( QFile::ReadOnly ); + + return f.readAll(); + } +#endif + void setSource( QQuickShaderEffectSource* source ) { setProperty( "source", QVariant::fromValue( source ) ); @@ -138,10 +145,10 @@ class BackgroundItem : public QskControl } void timerEvent( QTimerEvent* ) override - { + { static int counter = 0; - const auto angle = counter++ / 50.0 * M_PI * 2.0 ; + const auto angle = counter++ / 50.0 * M_PI * 2.0; const auto x = std::cos( angle ); const auto y = std::sin( angle ); diff --git a/playground/parrots/shaders.qrc b/playground/parrots/shaders.qrc new file mode 100644 index 00000000..b1f60720 --- /dev/null +++ b/playground/parrots/shaders.qrc @@ -0,0 +1,10 @@ + + + + shaders/blur.vert + shaders/blur.frag + + shaders/blur.vert.qsb + shaders/blur.frag.qsb + + diff --git a/playground/parrots/shaders/blur-vulkan.frag b/playground/parrots/shaders/blur-vulkan.frag new file mode 100644 index 00000000..5f5c6d48 --- /dev/null +++ b/playground/parrots/shaders/blur-vulkan.frag @@ -0,0 +1,24 @@ +#version 440 + +layout(location = 0) in vec2 qt_TexCoord0; +layout(location = 0) out vec4 fragColor; + +layout(binding = 1) uniform sampler2D source; + +layout(std140, binding = 0) uniform buf +{ + mat4 qt_Matrix; + float qt_Opacity; + +} ubuf; + +void main() +{ + vec2 delta = vec2(0.01, 0.01); + + fragColor =(0.0538 * texture(source, qt_TexCoord0 - 3.182 * delta) + + 0.3229 * texture(source, qt_TexCoord0 - 1.364 * delta) + + 0.2466 * texture(source, qt_TexCoord0) + + 0.3229 * texture(source, qt_TexCoord0 + 1.364 * delta) + + 0.0538 * texture(source, qt_TexCoord0 + 3.182 * delta)) * ubuf.qt_Opacity; +} diff --git a/playground/parrots/shaders/blur-vulkan.vert b/playground/parrots/shaders/blur-vulkan.vert new file mode 100644 index 00000000..6ca0bd3c --- /dev/null +++ b/playground/parrots/shaders/blur-vulkan.vert @@ -0,0 +1,21 @@ +#version 440 + +layout(location = 0) in vec4 qt_Vertex; +layout(location = 1) in vec2 qt_MultiTexCoord0; + +layout(location = 0) out vec2 qt_TexCoord0; + +layout(std140, binding = 0) uniform buf +{ + mat4 qt_Matrix; + float qt_Opacity; +} ubuf; + +out gl_PerVertex { vec4 gl_Position; }; + +void main() +{ + qt_TexCoord0 = qt_MultiTexCoord0; + gl_Position = ubuf.qt_Matrix * qt_Vertex; +} + diff --git a/playground/parrots/shaders/blur.frag b/playground/parrots/shaders/blur.frag new file mode 100644 index 00000000..4202dce2 --- /dev/null +++ b/playground/parrots/shaders/blur.frag @@ -0,0 +1,15 @@ +varying highp vec2 qt_TexCoord0; + +uniform sampler2D source; +uniform lowp float qt_Opacity; + +void main() +{ + vec2 delta = vec2(0.01, 0.01); + + gl_FragColor =(0.0538 * texture2D(source, qt_TexCoord0 - 3.182 * delta) + + 0.3229 * texture2D(source, qt_TexCoord0 - 1.364 * delta) + + 0.2466 * texture2D(source, qt_TexCoord0) + + 0.3229 * texture2D(source, qt_TexCoord0 + 1.364 * delta) + + 0.0538 * texture2D(source, qt_TexCoord0 + 3.182 * delta)) * qt_Opacity; +} diff --git a/playground/parrots/shaders/blur.frag.qsb b/playground/parrots/shaders/blur.frag.qsb new file mode 100644 index 00000000..6e909cc2 Binary files /dev/null and b/playground/parrots/shaders/blur.frag.qsb differ diff --git a/playground/parrots/shaders/blur.vert b/playground/parrots/shaders/blur.vert new file mode 100644 index 00000000..ae1e84a5 --- /dev/null +++ b/playground/parrots/shaders/blur.vert @@ -0,0 +1,12 @@ +uniform highp mat4 qt_Matrix; + +attribute highp vec4 qt_Vertex; +attribute highp vec2 qt_MultiTexCoord0; + +varying highp vec2 qt_TexCoord0; + +void main() +{ + qt_TexCoord0 = qt_MultiTexCoord0; + gl_Position = qt_Matrix * qt_Vertex; +} \ No newline at end of file diff --git a/playground/parrots/shaders/blur.vert.qsb b/playground/parrots/shaders/blur.vert.qsb new file mode 100644 index 00000000..323177db Binary files /dev/null and b/playground/parrots/shaders/blur.vert.qsb differ diff --git a/playground/parrots/shaders/vulkan2qsb.sh b/playground/parrots/shaders/vulkan2qsb.sh new file mode 100755 index 00000000..4b53dc70 --- /dev/null +++ b/playground/parrots/shaders/vulkan2qsb.sh @@ -0,0 +1,9 @@ +#! /bin/sh + +function qsbcompile { + qsbfile=`echo $1 | sed 's/-vulkan//'` + qsb --glsl 100es,120,150 --hlsl 50 --msl 12 -b -o ${qsbfile}.qsb $1 +} + +qsbcompile blur-vulkan.vert +qsbcompile blur-vulkan.frag