Results 1 to 14 of 14

Thread: [QML] Pause recursive ShaderEffect

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default [QML] Pause recursive ShaderEffect

    Hi,

    I found some interesting code online about recursive shaders. It was not to difficult to build a mobile app with it but my app is not getting approved. The issue is that it is not going to sleep when the phone is.
    To send it to sleep I tried to build in an active property and binding the shader to it but that does not help.

    Qt Code:
    1. ShaderEffect {
    2. id: inputItem
    3. property variant source: ShaderEffectSource { sourceItem: sourceArea; smooth: false }
    4. property variant recursiveSource: recursiveSource
    5. property real ledWidthPercent: ledScreen.ledWidth/ledScreen.width
    6. anchors.fill: sourceArea
    7. visible: false
    8. enabled: root.active
    9.  
    10. fragmentShader: "
    11. varying mediump vec2 qt_TexCoord0;
    12. uniform highp float qt_Opacity;
    13. uniform highp float ledWidthPercent;
    14. uniform lowp sampler2D source;
    15. uniform lowp sampler2D recursiveSource;
    16. void main() {
    17. highp vec4 pix;
    18. if (qt_TexCoord0.x > (1.0 - ledWidthPercent)) {
    19. pix = texture2D(source, qt_TexCoord0);
    20. } else {
    21. highp vec2 pos = vec2(qt_TexCoord0.x+ledWidthPercent, qt_TexCoord0.y);
    22. pix = texture2D(recursiveSource, pos) * (1.0-ledWidthPercent*2.0);
    23. }
    24. gl_FragColor = pix * qt_Opacity;
    25. }
    26. "
    27. }
    28.  
    29. ShaderEffectSource {
    30. id: recursiveSource
    31. sourceItem: inputItem
    32. hideSource: true
    33. visible: false
    34. live: root.updateFrequency == 1//false
    35. recursive: true
    36. smooth: false
    37. enabled: root.active
    38. }
    To copy to clipboard, switch view to plain text mode 

    Can someone help me to understand how this code works and how I can pause it?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [QML] Pause recursive ShaderEffect

    enabled is a property that determines whether an element will receive user input.

    try to bind the active property to the visible property instead

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: [QML] Pause recursive ShaderEffect

    Thank you for the hint but I already tried that. As you can see above the visibility is already set to false. There is another shader in the code and I added an active property here as well that should control the visibility:
    Qt Code:
    1. ShaderEffect {
    2. id: effectItem
    3. width: screenWidth * root.ledWidth
    4. height: screenHeight * root.ledHeight
    5. anchors.centerIn: parent
    6. smooth: false
    7. visible: root.active
    8.  
    9. property real screenWidth: Math.floor(root.width / root.ledWidth)
    10. property real screenHeight: Math.floor(root.height / root.ledHeight)
    11. property var source: effectSource
    12. property var sledOn: Image { source: "qrc:/images/led_on2.png"; sourceSize.width: root.ledWidth; sourceSize.height: root.ledHeight; visible: false }
    13. property var sledOff: Image { source: "qrc:/images/led_off2.png"; sourceSize.width: root.ledWidth; sourceSize.height: root.ledHeight; visible: false }
    14. property point screenSize: Qt.point(screenWidth, screenHeight)
    15. property alias ledColor: root.ledColor
    16. property real useSourceColors: root.useSourceColors ? 1.0 : 0.0
    17. property alias threshold: root.threshold
    18.  
    19. fragmentShader: "
    20. varying highp vec2 qt_TexCoord0;
    21. uniform lowp float qt_Opacity;
    22. uniform sampler2D source;
    23. uniform sampler2D sledOn;
    24. uniform sampler2D sledOff;
    25. uniform highp vec2 screenSize;
    26. uniform highp vec4 ledColor;
    27. uniform lowp float useSourceColors;
    28. uniform lowp float threshold;
    29.  
    30. void main() {
    31. highp vec2 cpos = (floor(qt_TexCoord0 * screenSize) + 0.5) / screenSize;
    32. highp vec4 tex = texture2D(source, cpos);
    33. highp vec2 lpos = fract(qt_TexCoord0 * screenSize);
    34. lowp float isOn = step(threshold, tex.r);
    35. highp vec4 pix = mix(texture2D(sledOff, lpos), texture2D(sledOn, lpos), isOn);
    36. highp vec4 color = mix(ledColor, tex, isOn * useSourceColors);
    37. gl_FragColor = pix * color * qt_Opacity;
    38. }"
    39. }
    To copy to clipboard, switch view to plain text mode 

    But the app is still active and causes several wake-ups when it should be idle. =(

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [QML] Pause recursive ShaderEffect

    Quote Originally Posted by KeineAhnung View Post
    But the app is still active and causes several wake-ups when it should be idle. =(
    Are you sure the cause is the ShaderEffect?
    Does it also happen if you don't have it?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: [QML] Pause recursive ShaderEffect

    If I take out the graph class(?) I do not have any issues. The Sensor is turned off correctly. Unfortunately I do not understand the code and cannot see what is active. I assumed it would be enough to stop the sensor because of the onValueChanged but something else sees to be permanently active within the shader.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QML] Pause recursive ShaderEffect

    Try breaking the loop between the two textures (should be enough to set recursive and/or live to false). Of course that will break your effect too so when it comes up again it will have to start from scratch. However making the shader effect invisible should have stopped its recalculation as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: [QML] Pause recursive ShaderEffect

    Thanks for the suggestions but they are not helping. The app is active as before. I will send it in and ask them to check if it is a bug on their side.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QML] Pause recursive ShaderEffect

    Quote Originally Posted by KeineAhnung View Post
    The app is active as before.
    Is the shader active as well? Maybe you are looking for the problem in the wrong place?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: [QML] Pause recursive ShaderEffect

    I manually turned off the shader. Made sure that the screen is clear. Locked the phone and did the test. Same result as before. =(
    If I comment out the graph and do the same, the app passes the test.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QML] Pause recursive ShaderEffect

    Can you specify the difference between the "graph" and the "shader"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: [QML] Pause recursive ShaderEffect

    The main file consists of basically three parts. The graph, the sensor and the reflection. The graph is build by two shader effects if I understand the code correctly.

    Qt Code:
    1. QUItMeterComponent {
    2. id: meter
    3.  
    4. width: parent.width
    5. height: parent.height/3*2
    6. ledWidth: ledWidthSlider.value
    7. ledHeight: ledHeightSlider.value
    8. updateFrequency: meterSpeedSlider.value
    9. active: root.active
    10.  
    11. }
    12.  
    13. Sensors.Accelerometer {
    14. id: accel
    15. dataRate: 100
    16. active: root.sensor === 1
    17. onReadingChanged: {
    18. var v = Math.sqrt(accel.reading.x * accel.reading.x + accel.reading.y * accel.reading.y + accel.reading.z * accel.reading.z);
    19. // Max 3G force
    20. v = v / (3*9.81);
    21. meter.value = v;
    22. }
    23. }
    24.  
    25. // Add mirror effect
    26. ShaderEffect {
    27. property variant source: ShaderEffectSource { sourceItem: meter; hideSource: false }
    28.  
    29. visible: root.active
    30.  
    31. anchors.top: meter.bottom
    32. width: meter.width
    33. height: parent.height/3
    34.  
    35. fragmentShader: "
    36. varying highp vec2 qt_TexCoord0;
    37. uniform highp float qt_Opacity;
    38. uniform sampler2D source;
    39. void main(void) {
    40. highp vec2 pos = vec2(qt_TexCoord0.x, (1.0 - qt_TexCoord0.y*0.8));
    41. pos.x += (qt_TexCoord0.y*0.2) * (pos.x * -1.0 + 1.0);
    42. highp vec4 pix = texture2D(source, pos);
    43. pix *= (0.4 - qt_TexCoord0.y*0.5) * min(qt_TexCoord0.y*5.0, 1.0);
    44. gl_FragColor = pix * qt_Opacity;
    45. }"
    46. }
    To copy to clipboard, switch view to plain text mode 

    If I comment out the QUItMeterComponent meter I have no issues but no graph as well. I added the active property and bound visible, live and enabled to it but that does not help to send the app into an idle state.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QML] Pause recursive ShaderEffect

    So regarding post #9 what exactly did you turn off and what you didn't? Did you for example remove the accelerator object? What is the type of "root.sensor"? Does it take other values than 1? In what conditions?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: [QML] Pause recursive ShaderEffect

    Wow! Thanks for taking so much time to look into this!

    Regarding root.sensor. I wanted to use different sensors with the graph. root.sensor tells the sensor to be active or not.
    What do you mean by removing the accelerator object? Commenting out the sensor accel? No I did not do that. But so far if only the sensor is active the app is okay. Meaning that the sensor gets turned off incase the app is idle.

    This is my modifed LedScreen.qml:
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item {
    4. id: root
    5. // QML content used as a source
    6. property alias sourceItem: effectSource.sourceItem
    7. // Size of one led, in pixels
    8. property real ledWidth: 64
    9. property real ledHeight: 32
    10. // Color of a led, default yellow
    11. property color ledColor: Qt.rgba(1.0, 1.0, 0.0, 1.0);
    12. // Set this true to use colors from sourceItem instead of ledColor
    13. property bool useSourceColors: false
    14. // Amount of opacity required to switch a led on
    15. property real threshold: 0.5
    16. // Stop ShaderEffect
    17. property bool active: true
    18.  
    19. ShaderEffectSource {
    20. id: effectSource
    21. hideSource: true
    22. smooth: false
    23. live: root.active
    24. recursive: root.active
    25. }
    26.  
    27. ShaderEffect {
    28. id: effectItem
    29. width: screenWidth * root.ledWidth
    30. height: screenHeight * root.ledHeight
    31. anchors.centerIn: parent
    32. smooth: false
    33. visible: root.active
    34.  
    35. property real screenWidth: Math.floor(root.width / root.ledWidth)
    36. property real screenHeight: Math.floor(root.height / root.ledHeight)
    37. property var source: effectSource
    38. property var sledOn: Image { source: "qrc:/images/led_on2.png"; sourceSize.width: root.ledWidth; sourceSize.height: root.ledHeight; visible: false }
    39. property var sledOff: Image { source: "qrc:/images/led_off2.png"; sourceSize.width: root.ledWidth; sourceSize.height: root.ledHeight; visible: false }
    40. property point screenSize: Qt.point(screenWidth, screenHeight)
    41. property alias ledColor: root.ledColor
    42. property real useSourceColors: root.useSourceColors ? 1.0 : 0.0
    43. property alias threshold: root.threshold
    44.  
    45. fragmentShader: "
    46. varying highp vec2 qt_TexCoord0;
    47. uniform lowp float qt_Opacity;
    48. uniform sampler2D source;
    49. uniform sampler2D sledOn;
    50. uniform sampler2D sledOff;
    51. uniform highp vec2 screenSize;
    52. uniform highp vec4 ledColor;
    53. uniform lowp float useSourceColors;
    54. uniform lowp float threshold;
    55.  
    56. void main() {
    57. highp vec2 cpos = (floor(qt_TexCoord0 * screenSize) + 0.5) / screenSize;
    58. highp vec4 tex = texture2D(source, cpos);
    59. highp vec2 lpos = fract(qt_TexCoord0 * screenSize);
    60. lowp float isOn = step(threshold, tex.r);
    61. highp vec4 pix = mix(texture2D(sledOff, lpos), texture2D(sledOn, lpos), isOn);
    62. highp vec4 color = mix(ledColor, tex, isOn * useSourceColors);
    63. gl_FragColor = pix * color * qt_Opacity;
    64. }"
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

    I added the active property ad all root.active things. Within the QUItMeterComponet.qml I did the same if the default values were not flase:

    Qt Code:
    1. import QtQuick 2.0
    2. import "ledscreencomponent"
    3.  
    4. Item {
    5. id: root
    6.  
    7. // Current value 0...1
    8. property real value: 0.5
    9. // Width and height of individual leds
    10. property alias ledWidth: ledScreen.ledWidth
    11. property alias ledHeight: ledScreen.ledHeight
    12. // How frequently ledscreen progresses, 60fps/updateFrequency
    13. property int updateFrequency: 5;
    14. // is aktive
    15. property bool active: true
    16. // *** private ***
    17. property int _updateFrequencyCounter: 0;
    18. property real _peakValue: root.value
    19.  
    20. onValueChanged: {
    21. root.value = Math.min(1.0, root.value)
    22. root._peakValue = Math.max(root._peakValue, root.value);
    23. }
    24.  
    25. width: 400
    26. height: 300
    27.  
    28. LedScreen {
    29. id: ledScreen
    30. anchors.fill: parent
    31. sourceItem: inputItem
    32. useSourceColors: true
    33. ledColor: Qt.rgba(0.4, 0.4, 0.4, 0.2)
    34. threshold: 0.01
    35. ledWidth: 32
    36. ledHeight: 24
    37. active: root.active
    38. visible: root.active
    39. }
    40.  
    41. // This area is the source, one pixel is one led
    42. Rectangle {
    43. id: sourceArea
    44. width: Math.floor(root.width / ledScreen.ledWidth)
    45. height: Math.floor(root.height / ledScreen.ledHeight)
    46. color:"#000000"
    47. visible: false
    48.  
    49. Image {
    50. anchors.right: parent.right
    51. anchors.bottom: parent.bottom
    52. width: 1
    53. height: parent.height*root.value
    54. source: ("qrc:/images/eqline.png")
    55. smooth: false
    56. }
    57.  
    58. Rectangle {
    59. anchors.right: parent.right
    60. color: "#ffffff"
    61. width: 1
    62. height: 1
    63. y: Math.floor(parent.height * (1 - root._peakValue))
    64. }
    65. }
    66.  
    67. ShaderEffect {
    68. id: inputItem
    69. property variant source: ShaderEffectSource { sourceItem: sourceArea; smooth: false }
    70. property variant recursiveSource: recursiveSource
    71. property real ledWidthPercent: ledScreen.ledWidth/ledScreen.width
    72. anchors.fill: sourceArea
    73. visible: false
    74.  
    75. fragmentShader: "
    76. varying mediump vec2 qt_TexCoord0;
    77. uniform highp float qt_Opacity;
    78. uniform highp float ledWidthPercent;
    79. uniform lowp sampler2D source;
    80. uniform lowp sampler2D recursiveSource;
    81. void main() {
    82. highp vec4 pix;
    83. if (qt_TexCoord0.x > (1.0 - ledWidthPercent)) {
    84. pix = texture2D(source, qt_TexCoord0);
    85. } else {
    86. highp vec2 pos = vec2(qt_TexCoord0.x+ledWidthPercent, qt_TexCoord0.y);
    87. pix = texture2D(recursiveSource, pos) * (1.0-ledWidthPercent*2.0);
    88. }
    89. gl_FragColor = pix * qt_Opacity;
    90. }
    91. "
    92. }
    93.  
    94. ShaderEffectSource {
    95. id: recursiveSource
    96. sourceItem: inputItem
    97. hideSource: true
    98. visible: false
    99. live: root.updateFrequency == 1//false
    100. recursive: root.active
    101. smooth: false
    102. }
    103.  
    104. // Dummy element for syncing updates
    105. Item {
    106. id: dummyItem
    107. NumberAnimation on rotation {
    108. from:0
    109. to: 360
    110. duration: 1000
    111. loops: Animation.Infinite
    112. }
    113. onRotationChanged: {
    114. root._updateFrequencyCounter++;
    115. if (root._updateFrequencyCounter == 1) {
    116. // Reduce peak in first iteratio after repaint
    117. var reduced_peakValue = root._peakValue - (root._peakValue - root.value)/2;
    118. root._peakValue = Math.max(root.value, reduced_peakValue);
    119. }
    120. if (root._updateFrequencyCounter >= root.updateFrequency) {
    121. recursiveSource.scheduleUpdate();
    122. root._updateFrequencyCounter = 0;
    123. }
    124. }
    125. }
    126. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QML] Pause recursive ShaderEffect

    First of all get rid of root.active and disable all what you want disabled by explicitly setting property values to false. See if that makes your app pass the test. Make sure the problem is not root.active never becoming false in the first place (I don't see any condition when it is set to false). In my opinion explicitly setting the shader's live property to false should disable updating the texture.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 12th June 2013, 06:44
  2. Create reuseable shaderEffect
    By stereoMatching in forum Newbie
    Replies: 0
    Last Post: 5th June 2013, 03:40
  3. QtConcurrent pause
    By MarKac in forum Newbie
    Replies: 2
    Last Post: 23rd April 2013, 14:49
  4. I need a pause (QTimer)
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2008, 05:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.