jayfreck
jayfreck

Reputation: 21

shader producing wrong results on tablets

ive only been working with shaders for a few days and have written a simple edge detection shader that adds a drop shadow and an inner shadow. It works great on my galaxy s2 and iphone 4, but the galaxy tab and ipad2 produce only a very thin and rough looking shadow on both sides of the edge. Ive spent hours trying to figure out why but had no joy, please help! If i simulate an ipad2 resolution in the simulator, the effect is correct.

The shader works on the same size off screen buffer on all devices (640x480 pixels).

vertex shader:
{
    attribute highp vec4    inVert; //vertex stream
    uniform highp mat4   inPMVMat; //transform to projected space
    attribute mediump vec2 inUV0;
    attribute lowp vec4 inCol;
    uniform mediump vec2 inUVOffset;
            uniform mediump vec2 inUVScale;
    varying mediump vec2 vTexCoord;
    varying mediump float FragColor;
    void main(void)
    {
        gl_Position = inPMVMat * inVert;//set vertex position in projected space
        vTexCoord = inUV0*inUVScale;// + inUVOffset;//pass texture to fragment shader
        FragColor = inCol.a;
    }
}


fragment shader:
{
    uniform sampler2D myTexture;
    varying mediump vec2 vTexCoord;
    varying mediump float FragColor;
    lowp float MaxDistance, Distance;
    lowp float Shift;
    void main(void)
    {
        gl_FragColor = texture2D(myTexture, vTexCoord);
        mediump vec2 PixelSize = vec2(1.0 / 640.0, 1.0 / 480.0);
        mediump vec2 Direction = vec2(PixelSize.x*0.5,PixelSize.y*0.5);

        if(gl_FragColor.a >= 0.5)
        {
            //inner shadow
            MaxDistance = 15.0;
        } else {
            //drop shadow
            MaxDistance = 12.0;
            Direction = -Direction;
        }


        mediump vec2 Position = vTexCoord;
        mediump float c;
        for(Distance = MaxDistance; Distance > 0.0;Distance -= 1.0)
        {
            Position += Direction;
            c = texture2D(myTexture,Position).a;
            if(c < 0.5)
            {
                if(gl_FragColor.a >= 0.5)
                {
                    //found transparent edge - do inner shadow
                    Shift = 1.0-((0.5/MaxDistance)*Distance);
                    gl_FragColor.r *= Shift;
                    gl_FragColor.g *= Shift;
                    gl_FragColor.b *= Shift;
                    break;
                }
            } else {
                if(gl_FragColor.a < 0.5)
                {
                    //found opaque edge - do dropshadow
                    gl_FragColor.a = ((0.8/MaxDistance)*Distance);
                    break;
                }
            }
        }
        gl_FragColor.a *= FragColor;
    }
}

images:

gs2

tab

Upvotes: 1

Views: 178

Answers (1)

jayfreck
jayfreck

Reputation: 21

I eventually found the solution to this after a lot of experimenting. The lowp MaxDistance, Distance and Shift variables were the culprits - it works fine after on all devices after changing to mediump. I guess there is some variation in precision between devices

Upvotes: 1

Related Questions