Android 设备上的 OpenGLES 3.0 着色器编译错误,用于输入和输出存储限定符
OpenGLES 3.0 Shader Compile error on Android Device for in and out storage qualifiers
所以我正在更新我的应用程序以使用 OpenGLES 3.0 来利用变换反馈,但着色器没有编译。
错误:
06-27 17:29:43.299 18593-18627/com.harmonicprocesses.penelopefree E/MyGLRenderer﹕ Could not compile shader 35633:
06-27 17:29:43.299 18593-18627/com.harmonicprocesses.penelopefree E/MyGLRenderer﹕ ERROR: 0:1: 'in' : Syntax error: syntax error
INTERNAL ERROR: no main() function!
ERROR: 1 compilation errors. No code generated.
这是顶点着色器代码:
private final String vertexShaderSrc =
"in float inValue;" +
"out float outValue;" +
"void main() {" +
" outValue = sqrt(inValue);" +
"}";
编译代码如下:
int vertexShader = MyGLRenderer.loadShader(GLES30.GL_VERTEX_SHADER,
vertexShaderSrc);
...
public static int loadShader(int shaderType, String source) {
int shader = GLES30.glCreateShader(shaderType);
if (shader != 0) {
GLES30.glShaderSource(shader, source);
GLES30.glCompileShader(shader);
int[] compiled = new int[1];
GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.e(TAG, "Could not compile shader " + shaderType + ":");
Log.e(TAG, GLES30.glGetShaderInfoLog(shader));
GLES30.glDeleteShader(shader);
shader = 0;
}
}
return shader;
}
有人看出这有什么问题吗?如果我将 in
和 out
分别更改为 attribute
varying
它不会抛出错误,但 glUseProgram 会抛出 gl 错误 1282 "Invalid Operation"。
这是我为 es 3.0 版设置 GL 的方式:
public MyGLSurfaceViewLegacy(Context context) {
super(context);
setListenForTouchOnTouchListener();
NoteSpectrum = DSPEngine.staticCalcNoteBins(AudioConstants.defaultBufferSize*2,
AudioConstants.sampleRate);
// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(3);
// Set the Renderer for drawing on the GLSurfaceView
mRenderer = new MyGLRenderer(context,this);
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
mContextFactory = new MyContextFactory();
setEGLContextFactory(mContextFactory);
//mEGLWindowSurfaceFactory = new MyEGLWindowSurfaceFactory();
//setEGLWindowSurfaceFactory(mEGLWindowSurfaceFactory);
setRenderer(mRenderer);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
mContext = context;
drawOverlay = PreferenceManager.getDefaultSharedPreferences(mContext)
.getBoolean("turn_on_visualization_key",true);
}
class MyContextFactory implements EGLContextFactory {
private int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
private EGLContext mEGLContext;
private EGLDisplay mEGLDisplay;
private MyGLSurfaceView mMyGLSurfaceView;
private EGL10 mEGL;
@Override
public EGLContext createContext(EGL10 egl, EGLDisplay display,
EGLConfig eglConfig) {
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 3,
EGL10.EGL_NONE };
mEGL = egl;
mEGLContext = egl.eglGetCurrentContext();
mEGLDisplay = display;
mEGLContext = egl.eglCreateContext(display, eglConfig,
egl.eglGetCurrentContext(), attrib_list);
return mEGLContext;
}
将 OpenGLES 3.0 版本说明符添加到着色器的开头:
#version 300 es
还要确保您在 GLSurfaceView
设置代码中调用了 setEGLContextClientVersion(3)
。
所以我正在更新我的应用程序以使用 OpenGLES 3.0 来利用变换反馈,但着色器没有编译。
错误:
06-27 17:29:43.299 18593-18627/com.harmonicprocesses.penelopefree E/MyGLRenderer﹕ Could not compile shader 35633:
06-27 17:29:43.299 18593-18627/com.harmonicprocesses.penelopefree E/MyGLRenderer﹕ ERROR: 0:1: 'in' : Syntax error: syntax error
INTERNAL ERROR: no main() function!
ERROR: 1 compilation errors. No code generated.
这是顶点着色器代码:
private final String vertexShaderSrc =
"in float inValue;" +
"out float outValue;" +
"void main() {" +
" outValue = sqrt(inValue);" +
"}";
编译代码如下:
int vertexShader = MyGLRenderer.loadShader(GLES30.GL_VERTEX_SHADER,
vertexShaderSrc);
...
public static int loadShader(int shaderType, String source) {
int shader = GLES30.glCreateShader(shaderType);
if (shader != 0) {
GLES30.glShaderSource(shader, source);
GLES30.glCompileShader(shader);
int[] compiled = new int[1];
GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.e(TAG, "Could not compile shader " + shaderType + ":");
Log.e(TAG, GLES30.glGetShaderInfoLog(shader));
GLES30.glDeleteShader(shader);
shader = 0;
}
}
return shader;
}
有人看出这有什么问题吗?如果我将 in
和 out
分别更改为 attribute
varying
它不会抛出错误,但 glUseProgram 会抛出 gl 错误 1282 "Invalid Operation"。
这是我为 es 3.0 版设置 GL 的方式:
public MyGLSurfaceViewLegacy(Context context) {
super(context);
setListenForTouchOnTouchListener();
NoteSpectrum = DSPEngine.staticCalcNoteBins(AudioConstants.defaultBufferSize*2,
AudioConstants.sampleRate);
// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(3);
// Set the Renderer for drawing on the GLSurfaceView
mRenderer = new MyGLRenderer(context,this);
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
mContextFactory = new MyContextFactory();
setEGLContextFactory(mContextFactory);
//mEGLWindowSurfaceFactory = new MyEGLWindowSurfaceFactory();
//setEGLWindowSurfaceFactory(mEGLWindowSurfaceFactory);
setRenderer(mRenderer);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
mContext = context;
drawOverlay = PreferenceManager.getDefaultSharedPreferences(mContext)
.getBoolean("turn_on_visualization_key",true);
}
class MyContextFactory implements EGLContextFactory {
private int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
private EGLContext mEGLContext;
private EGLDisplay mEGLDisplay;
private MyGLSurfaceView mMyGLSurfaceView;
private EGL10 mEGL;
@Override
public EGLContext createContext(EGL10 egl, EGLDisplay display,
EGLConfig eglConfig) {
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 3,
EGL10.EGL_NONE };
mEGL = egl;
mEGLContext = egl.eglGetCurrentContext();
mEGLDisplay = display;
mEGLContext = egl.eglCreateContext(display, eglConfig,
egl.eglGetCurrentContext(), attrib_list);
return mEGLContext;
}
将 OpenGLES 3.0 版本说明符添加到着色器的开头:
#version 300 es
还要确保您在 GLSurfaceView
设置代码中调用了 setEGLContextClientVersion(3)
。