1
votes

I am currently trying to get multiple textures working on android (with one fragment shader). It works fine on my Nexus 4 but on all Samsung devices I've tested it on, it just doesn't show the rectangle at all. The problem seems to be that texture2d is called twice within the fragment shader code:

precision mediump float;                
uniform sampler2D uTexture;
uniform sampler2D refractTexture;       
varying vec2 vTexCoordinate;
varying vec2 vRefTexCoordinate;

void main() {
    vec2 scaleVec = vec2(0.05, 0.05);
    vec4 bumpTex = 2.0 * texture2D(refractTexture, vRefTexCoordinate) - 1.0;
    vec2 refCoords = vTexCoordinate.xy + bumpTex.xy * scaleVec;

    gl_FragColor = texture2D(uTexture, refCoords);
}

In this shader I distort the texture (uTexture) with a normal map (refractTexture). I've tried out using the correct (not the calculated) coordinates for gl_FragColor and as soon as I delete vec4 bumpTex = 2.0 * texture2D(refractTexture, vRefTexCoordinate) - 1.0; it appears on the devices.

Any hint will help.

As requested here is the Java Code I use to set up the textures:

    mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram,
            "uTexture");

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);

    GLES20.glUniform1i(mTextureUniformHandle, 0);

    if (refractNormal && textureRefBuffer != null) {
        mTextureRefUniHandle = GLES20.glGetUniformLocation(mProgram,
                "refractTexture");

        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureRefDataHandle);

        GLES20.glUniform1i(mTextureRefUniHandle, 1);

        mTextureRefCoordinateHandle = GLES20.glGetAttribLocation(mProgram,
                "aRefTexCoordinate");

        GLES20.glVertexAttribPointer(mTextureRefCoordinateHandle,
                mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0,
                textureRefBuffer);
        GLES20.glEnableVertexAttribArray(mTextureRefCoordinateHandle);

    }

    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram,
            "aTexCoordinate");
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle,
            mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0,
            textureBuffer);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

And the C Code of the vertex shader:

        uniform mat4 uMVPMatrix;
        attribute vec4 vPosition;
        attribute vec2 aTexCoordinate;
        attribute vec2 aRefTexCoordinate;
        varying vec2 vTexCoordinate;
        varying vec2 vRefTexCoordinate;

        void main() {         
            vTexCoordinate = aTexCoordinate;
            vRefTexCoordinate = aRefTexCoordinate;
            gl_Position =   uMVPMatrix*vPosition;
        }
1
Can you post your Java/C code for setting up textures and samplers for shader? This can be caused by missing some parameters/values, and default values work different on different GPUs.keaukraine
@keau Thank you for your response, I added the code. What do you think?Agent T
Maybe its using a mipmap level you haven't setup properly. Everything else seams ok to me.Trax
@Trax I am using the same settings for both textures when I load them and just one texture works fine. But it seems that as soon as I call the texture2d() function a second time nothing gets rendered. I will test out some settings as soon as I can.Agent T
I multi texturing shaders running on Samsung devices. It should be something about that dependent texture lookup somehow. You can also go to ARM website and ask some advice at their forums. They also have some debugging apps that may help you track down the problem.Trax

1 Answers

0
votes

As suggested I asked for help at the ARM forums, there somebody asked me for a sample project. I heavily stripped down what I had and tested it. Suprisingly it worked without a problem on the S3, I tested it on. I tried to find the problem by copying Code from my original project, finally I ended up copying the shaders from the small project into the original project and that somehow did the job. I don't really know what exactly caused this but heres the shader code that worked for me:

        precision mediump float;
        uniform vec4 vColor;
        uniform sampler2D uTexture;
        uniform sampler2D refractTexture;   
        varying vec2 vTexCoordinate;
        varying vec2 vRefTexCoordinate;                     

        void main() {   

            vec2 scaleVec = vec2(0.05, 0.05);
            vec4 bumpTex = 2.0 * texture2D(refractTexture, vRefTexCoordinate) - 1.0;
            vec2 refCoords = vTexCoordinate.xy + bumpTex.xy * scaleVec;     
            gl_FragColor = texture2D(uTexture, refCoords);

        }

And the vertex shader:

        uniform mat4 uMVPMatrix;
        attribute vec4 vPosition;
        attribute vec2 aTexCoordinate;
        attribute vec2 aRefTexCoordinate;
        varying vec2 vTexCoordinate;
        varying vec2 vRefTexCoordinate;

        void main() {         
            vTexCoordinate = aTexCoordinate;
            vRefTexCoordinate = aRefTexCoordinate;
            gl_Position =   uMVPMatrix*vPosition;

        }