Search code examples
androidopengl-esglslshader

Use 3D Texture for Fragment shader on Android


I need to use 3D Texture in my fragment shader on Android, but get compiling error. I use GLES3.0 and I think it should support 3D texture. My shader looks as follows:

#extension GL_OES_texture_3D : enable

precision mediump float;

uniform sampler3D u_Texture;

Without #extension GL_OES_texture_3D : enable I got error

L0003: Keyword 'sampler3D' is reserved

With #extension GL_OES_texture_3D : enable, I got error

S0032: no default precision defined for variable 'u_Texture'

Even I use uniform medium float sampler3D u_Texture, I got

Expect, found sampler3D error.

Hope get your help!

Many thanks.

YL


Solution

  • First of all you've to specify the glsl version by a version qualifier in the first line of the shader program:

    #version 300 es
    

    The precision qualifier is either highp, mediump or lowp and has to be specified before the type. The type is sampler3D rather than float.

    The declaration

    uniform medium float sampler3D u_Texture
    

    has 2 types (float and sampler3D), so it causes a syntax error.

    It has to be

    #version 300 es
    
    uniform medium sampler3D u_Texture;
    

    or by the use of a default precision qualifier

    #version 300 es
    precision mediump sampler3D;
    
    uniform sampler3D u_Texture;
    

    (Of course precision mediump float; has to be specified, too)

    See OpenGL ES Shading Language 3.20 Specification - 4.7.3. Precision Qualifiers