某些设备不支持Unity Android Shader

时间:2015-07-17 15:51:04

标签: android unity3d opengl-es shader

我有一个Unity应用程序,它使用自定义着色器进行视频播放。在某些设备上,视频未显示,我在LogCat中打印出以下错误消息:

E/Unity   (11415): -------- Custom/AndroidVideoShader
E/Unity   (11415): 
E/Unity   (11415):  
E/Unity   (11415): (Filename: Runtime/GfxDevice/opengles/GlslGpuProgramGLES.cpp Line: 141)
E/Unity   (11415): 
E/Unity   (11415): -------- failed compiling fragment shader:
E/Unity   (11415): 
E/Unity   (11415):  
E/Unity   (11415): (Filename: Runtime/GfxDevice/opengles/GlslGpuProgramGLES.cpp Line: 142)
E/Unity   (11415): 
D/Unity   (11415): #ifndef SHADER_API_GLES
D/Unity   (11415):     #define SHADER_API_GLES 1
D/Unity   (11415): #endif
D/Unity   (11415): #ifndef SHADER_API_MOBILE
D/Unity   (11415):     #define SHADER_API_MOBILE 1
D/Unity   (11415): #endif
D/Unity   (11415): #line 7
D/Unity   (11415): #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
D/Unity   (11415): #endif
D/Unity   (11415): #line 7
D/Unity   (11415): #ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
D/Unity   (11415): #endif
D/Unity   (11415):      precision mediump float;
D/Unity   (11415): varying vec2 textureCoordinates;
D/Unity   (11415):      
D/Unity   (11415):      
D/Unity   (11415):      
D/Unity   (11415):      
D/Unity   (11415):   // require GL_OES_EGL_image_external so we can access the external texture data on android's GPU
D/Unity   (11415):   #extension GL_OES_EGL_image_external : require
D/Unity   (11415):   uniform samplerExternalOES _MainTex;
D/Unity   (11415):   void main()
D/Unity   (11415):   {
D/Unity   (11415):    gl_FragColor = texture2D(_MainTex, textureCoordinates);
D/Unity   (11415):   }
D/Unity   (11415):   
D/Unity   (11415):   
E/Unity   (11415): -------- GLSL error: 0:23: P0007: Extension directive must occur before any non-preprocessor tokens
E/Unity   (11415): 0:25: L0001: Typename expected, found 'samplerExternalOES'
E/Unity   (11415): 
E/Unity   (11415): 
E/Unity   (11415): 
E/Unity   (11415):  
E/Unity   (11415): (Filename: Runtime/GfxDevice/opengles/GlslGpuProgramGLES.cpp Line: 145)
E/Unity   (11415): 
D/Unity   (11415): Warning: Creation of shader 'Custom/AndroidVideoShader' failed.
D/Unity   (11415): WARNING: Shader 
D/Unity   (11415): Unsupported: 'Custom/AndroidVideoShader' - Pass '' shader state not supported

着色器代码如下:

Shader "Custom/AndroidVideoShader" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
    Pass{


    GLSLPROGRAM

    varying vec2 textureCoordinates;

    #ifdef VERTEX

    // get the texture offset ftom our material
    uniform mediump vec4 _MainTex_ST;

    void main()
    {
        // multiply the UV by texture scale and add the offset
        textureCoordinates.xy = (gl_MultiTexCoord0.xy * _MainTex_ST.xy) + _MainTex_ST.zw;

        //textureCoordinates = gl_MultiTexCoord0;

        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }

    #endif


    #ifdef FRAGMENT

    // require GL_OES_EGL_image_external so we can access the external texture data on android's GPU
    #extension GL_OES_EGL_image_external : require

    uniform samplerExternalOES _MainTex;

    void main()
    {
        gl_FragColor = texture2D(_MainTex, textureCoordinates);
    }

    #endif

    ENDGLSL
    }
} 
FallBack "Diffuse"
}

它似乎不是一个Android版本的东西,因为我收到错误的设备(客户端的注释2和华为Ascend Y330)正在运行不同版本的Android(4.4.2和分别为4.2.2)。我有一个Nexus 7(4.2.2),Xperia M2(4.4.4)和Galaxy Tab 8.4(5.1.1)都可以正常工作。

我没有使用着色器经验 - 是否有任何明显的问题?在Unity中,我尝试将图形级别设置为OpenGLES2.0和OpenGLES3.0,但它没有任何区别。

1 个答案:

答案 0 :(得分:0)

着色器中的语法错误

GLSL error: 0:23: P0007: Extension directive must occur before any non-preprocessor tokens

ESSL规范中的合法性是:

#extension GL_OES_EGL_image_external : require

......待定义:

varying vec2 textureCoordinates;

它在某些设备上运行的原因只是运气 - 一些着色器编译器比其他设备更不挑剔。更改着色器,以便#extension定义是源代码中的第一个内容,然后它应该都可以正常工作。