Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to enable 16 bit precision in QML ShaderEffect Fragmentshader?



  • I am using the ShaderEffect Component in QML for ease of use and have run into the problem that all images passed to the shader (via the sampler) are 8bit, no matter how i format the image.

    If i format it to GrayScale16bit, and set a pixel value to be 256 for example, it would be converted to 1 as 8bit.

    Is there no easy/simple flag i can set to force it to be 16bit?

    Below is my frag shader code

    varying highp vec2 coord;
    uniform sampler2D src;
    uniform sampler2D kernel;
    uniform highp int  kernelOffset;
    uniform highp float qt_Opacity;
    uniform highp float horizontalStep;
    uniform highp float verticalStep;
    uniform highp float kernelHorizontalStep;
    uniform highp float kernelVerticalStep;
    uniform highp int filterSize;
    uniform highp float filterStrength;
    uniform highp vec3 test;
    
    
    void main() {
        highp float max16bit = 65535.0;
        highp float sampleCount = filterSize * filterSize;
        highp int halfSize = filterSize / 2; // round down
        highp float total = 0.0;
    
        highp float kernelTotal = 0.0;
    
        for (int i = 0; i < filterSize; ++i)
        {
            for (int j = 0; j < filterSize; ++j)
            {
                highp vec2 pos = coord;
                highp vec2 kernelPos;
                kernelPos.x = (float(i) * kernelHorizontalStep);
                kernelPos.y = (float(j) * kernelVerticalStep);
    
                pos.x = pos.x + (horizontalStep * (i - halfSize));
                pos.y = pos.y + (verticalStep * (j - halfSize));
    
                highp float tex = texture2D(src, pos).r;
                highp float coef = float(texture2D(kernel, kernelPos).r) * max16bit;
    
                kernelTotal += (coef + float(kernelOffset));
                total += ((coef + kernelOffset) * tex);
            }
        }
    
        total /= kernelTotal;
    
        gl_FragColor = vec4(total, total, total, 1.0);
    }
    

Log in to reply