1

with the new Qt6.2 update, vertex and fragment shaders are required to be packaged in a .qsb file instead of embedded as strings in the ShaderEffect component.

I'm trying to change my vertexShader to use the new standard. Below is the code currently

vertexShader: "
                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;
                } 
"

How can I go about creating the .qsb file and using it in the ShaderEffect?

gustavo
  • 11
  • 1
  • 3

1 Answers1

0

In CMake, you can use qt6_add_shaders to bake shaders into qsb files. For example here is something I used in my project:

qt6_add_shaders(graph "graph-shaders"
  BATCHABLE
  PRECOMPILE
  OPTIMIZED
  PREFIX
  "/"
  FILES
  "shaders/cube/cube.frag"
  "shaders/cube/cube.vert"
  "shaders/line/line.frag"
  "shaders/line/line.vert"
  "shaders/noisy/noisy.frag"
  "shaders/noisy/noisy.vert"
  )

You can see that I have a folder where all of my shaders are located In there, and when I run CMake I receive the following information:

shaders/cube/cube.frag -> shaders/cube/cube.frag.qsb exposed as ://shaders/cube/cube.frag.qsb
shaders/cube/cube.vert -> shaders/cube/cube.vert.qsb exposed as ://shaders/cube/cube.vert.qsb
shaders/line/line.frag -> shaders/line/line.frag.qsb exposed as ://shaders/line/line.frag.qsb
shaders/line/line.vert -> shaders/line/line.vert.qsb exposed as ://shaders/line/line.vert.qsb
shaders/noisy/noisy.frag -> shaders/noisy/noisy.frag.qsb exposed as ://shaders/noisy/noisy.frag.qsb
shaders/noisy/noisy.vert -> shaders/noisy/noisy.vert.qsb exposed as ://shaders/noisy/noisy.vert.qsb

All of these compiled QSB files are located in the QT resource (which is included with the executable file). And Using it in QML or C++ is as simple as:

vertexShader: ":/shaders/noisy/noisy.vert.qsb"

It's so much cleaner now, as you can see :)

Saeed Masoomi
  • 1,703
  • 1
  • 19
  • 33
  • Despite being generated, the qbs files cannot be unserialized (not a valid qsb file, says the warning). Might it be possible that the shaders themselves aren't valid anymore ? I was used to the old Qt5 shader semantics. How do I know how to properly rewrite them for Qt6 ? – Michael Aug 01 '23 at 23:06