PDA

View Full Version : glLinkProgram failing with no logs



sowmyaa
05-02-2012, 12:29 AM
Hi all,

I am new to open GLES application development. I developed an Open GLES application using shaders. The shaders are compiled successfully, but linking the shader is failing with no log messages.

Following is the code snippet:
BOOL InitOGLES(NativeWindowType window)
{
EGLConfig configs;
EGLint matchingConfigs;

static const EGLint configAttribs[] =
{
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
EGL_ALPHA_SIZE, 0,
EGL_LUMINANCE_SIZE, EGL_DONT_CARE,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_SAMPLES, 0,
EGL_NONE
};

glesDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert(eglGetError()==EGL_SUCCESS);

eglInitialize(glesDisplay, NULL, NULL);
assert(eglGetError()==EGL_SUCCESS);
eglBindAPI(EGL_OPENGL_ES_API);

eglChooseConfig(glesDisplay, configAttribs, &configs, 1, &matchingConfigs);
assert(eglGetError()==EGL_SUCCESS);

assert(matchingConfigs == 1);

glesSurface = eglCreateWindowSurface(glesDisplay, configs, window, NULL);
assert(eglGetError()==EGL_SUCCESS);

glesContext=eglCreateContext(glesDisplay,configs,0 ,NULL);
assert(eglGetError()==EGL_SUCCESS);

eglMakeCurrent(glesDisplay, glesSurface, glesSurface, glesContext);
assert(eglGetError()==EGL_SUCCESS);

eglSwapBuffers(glesDisplay, glesSurface);
assert(eglGetError() == EGL_SUCCESS);

const CHAR* g_strVSProgram =
"attribute vec4 g_vVertex; \n"
"attribute vec4 g_vColor; \n"
"varying vec4 g_vVSColor; \n"
" \n"
"void main() \n"
"{ \n"
" gl_Position = vec4( g_vVertex.x, g_vVertex.y, \n"
" g_vVertex.z, g_vVertex.w ); \n"
" g_vVSColor = g_vColor; \n"
"} \n";
const CHAR* g_strFSProgram =
"#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
" precision highp float; \n"
"#else \n"
" precision mediump float; \n"
"#endif \n"
" \n"
"varying vec4 g_vVSColor; \n"
" \n"
"void main() \n"
"{ \n"
" gl_FragColor = g_vVSColor; \n"
"} \n";
GLuint vertexShader=0;
GLuint fragmentShader=0;
GLint linked;

// Create vertex shader
GLuint hVertexShader = glCreateShader( GL_VERTEX_SHADER );
if(hVertexShader == 0)
return 0;

glShaderSource( hVertexShader, 1, &g_strVSProgram, NULL );
glCompileShader( hVertexShader );

GLint nCompileVertexResult = 0;
glGetShaderiv(hVertexShader, GL_COMPILE_STATUS,&nCompileVertexResult);
if (!nCompileVertexResult)
{
GLint infoLen = 0;
glGetShaderiv(hVertexShader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
CHAR Log[1024];
GLint nLength;
glGetShaderInfoLog(hVertexShader, 1024, &nLength, Log);
return FALSE;
}
glDeleteShader(hVertexShader);
}

//Create fragment shader
GLuint hFragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( hFragmentShader, 1, &g_strFSProgram, NULL );
glCompileShader( hFragmentShader );

GLint nCompileResult = 0;
//Check the compile status
glGetShaderiv(hFragmentShader, GL_COMPILE_STATUS,&nCompileResult);
if (!nCompileResult)
{
GLint infoLen = 0;
glGetShaderiv(hFragmentShader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
CHAR Log[1024];
GLint nLength;
glGetShaderInfoLog(hFragmentShader, 1024, &nLength, Log);
return FALSE;
}
glDeleteShader(hFragmentShader);
}

// Create the program object
programObject = glCreateProgram();
if(programObject == 0)
return 0;
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "g_vVertex");
glBindAttribLocation(programObject, 1, "g_vColor");

// Link the program
glLinkProgram(programObject);
// Check the link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if(!linked)
{
GLint infoLen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen != 0)
{
CHAR Log[1024];
GLint nLength;
glGetProgramInfoLog(programObject, 1024, &nLength, Log);
RETAILMSG(DBG,(_T("Log %s\r\n"),Log));
return FALSE;
}
glDeleteProgram(programObject);
glDeleteShader( hVertexShader );
glDeleteShader( hFragmentShader );
return FALSE;
}

return TRUE;
}

void Render()
{

FLOAT fSize = 0.5f;
FLOAT VertexPositions[] =
{
0.0f, +fSize*g_fAspectRatio, 0.0f, 1.0f,
-fSize, -fSize*g_fAspectRatio, 0.0f, 1.0f,
+fSize, -fSize*g_fAspectRatio, 0.0f, 1.0f,
};
FLOAT VertexColors[] = {1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
// Clear the backbuffer and depth-buffer
glClearColor( 0.0f, 0.0f, 0.5f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Set the shader program and the texture
glUseProgram( g_hShaderProgram );

// Draw the colored triangle
glVertexAttribPointer( 0, 4, GL_FLOAT, 0, 0, VertexPositions );

glEnableVertexAttribArray( 0 );

glVertexAttribPointer( 1, 4, GL_FLOAT, 0, 0, VertexColors);

glEnableVertexAttribArray( 1 );

glDrawArrays( GL_TRIANGLE_STRIP, 0, 3 );

glDisableVertexAttribArray( 0 );

glDisableVertexAttribArray( 1 );

eglSwapBuffers(glesDisplay, glesSurface);
assert(eglGetError() == EGL_SUCCESS);

}

Can any one please tell me what is wrong in this code.

Thanks and regards,
Sowmya

debinair
05-07-2012, 12:20 AM
Hi,
while creating eglsurface() you have to pass some attribute as last parameter
EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
glesContext=eglCreateContext(glesDisplay,configs,E GL_NO_CONTEXT,context_attribs);

and you have to add EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
in configs[].
The reason is that ES2 requires this client version to be set explicitly to 2 while ES1 application takes by default it to 1 and no need to add these lines in opengles1 app. so the init function of yours will correctly run for opengles1 and not opengles2.
Also done some minor changes in your code i am copying it here


#include <GLES2/gl2.h>
#include <EGL/egl.h>


#include <assert.h>


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

EGLDisplay glesDisplay;
EGLSurface glesSurface;
EGLContext glesContext;

static int programObject;
bool InitOGLES(NativeWindowType window)
{
EGLConfig configs;
EGLint matchingConfigs;
// EGLDisplay glesDisplay;
// EGLSurface glesSurface;
// EGLContext glesContext;

EGLint configAttribs[] =
{
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
EGL_ALPHA_SIZE, 0,
EGL_LUMINANCE_SIZE, EGL_DONT_CARE,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_SAMPLES, 0,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,

EGL_NONE};

glesDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert(eglGetError()==EGL_SUCCESS);

eglInitialize(glesDisplay, NULL, NULL);
assert(eglGetError()==EGL_SUCCESS);
//eglBindAPI(EGL_OPENGL_ES_API);

eglChooseConfig(glesDisplay, configAttribs, &configs, 1, &matchingConfigs);
assert(eglGetError()==EGL_SUCCESS);

assert(matchingConfigs == 1);

glesSurface = eglCreateWindowSurface(glesDisplay, configs, window, NULL);
assert(eglGetError()==EGL_SUCCESS);
EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};

glesContext=eglCreateContext(glesDisplay,configs,E GL_NO_CONTEXT,context_attribs);

assert(eglGetError()==EGL_SUCCESS);

eglMakeCurrent(glesDisplay, glesSurface, glesSurface, glesContext);
assert(eglGetError()==EGL_SUCCESS);

//eglSwapBuffers(glesDisplay, glesSurface);
//assert(eglGetError() == EGL_SUCCESS);

const char* g_strVSProgram =
"attribute vec4 g_vVertex; \n"
"attribute vec4 g_vColor; \n"
"varying vec4 g_vVSColor; \n"
" \n"
"void main() \n"
"{ \n"
" gl_Position = vec4( g_vVertex.x, g_vVertex.y, \n"
" g_vVertex.z, g_vVertex.w ); \n"
" g_vVSColor = g_vColor; \n"
"} \n";
const char* g_strFSProgram =
"#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
" precision highp float; \n"
"#else \n"
" precision mediump float; \n"
"#endif \n"
" \n"
"varying vec4 g_vVSColor; \n"
" \n"
"void main() \n"
"{ \n"
" gl_FragColor = g_vVSColor; \n"
"} \n";
GLuint vertexShader=0;
GLuint fragmentShader=0;
GLint linked;

// Create vertex shader
vertexShader = glCreateShader( GL_VERTEX_SHADER );
assert(eglGetError()==EGL_SUCCESS);

if(vertexShader == 0)
{printf("\n hVertexShader");
return 0;
}
glShaderSource( vertexShader, 1, &g_strVSProgram, NULL );
glCompileShader( vertexShader );

GLint nCompileVertexResult = 0;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS,&nCompileVertexResult);
if (!nCompileVertexResult)
{
GLint infoLen = 0;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{

printf(" \n After glGetShaderiv()\n ");
char Log[1024];
GLint nLength;
glGetShaderInfoLog(vertexShader, 1024, &nLength, Log);
printf("\n -- %s -- \n",Log);
return GL_FALSE;
}
glDeleteShader(vertexShader);
}

//Create fragment shader
GLuint hFragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( hFragmentShader, 1, &g_strFSProgram, NULL );
glCompileShader( hFragmentShader );

GLint nCompileResult = 0;
//Check the compile status
glGetShaderiv(hFragmentShader, GL_COMPILE_STATUS,&nCompileResult);
if (!nCompileResult)
{
printf(" \n After nCompileResult()\n ");
GLint infoLen = 0;
glGetShaderiv(hFragmentShader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char Log[1024];
GLint nLength;
glGetShaderInfoLog(hFragmentShader, 1024, &nLength, Log);
printf("\n -- %s -- \n",Log);
return GL_FALSE;
}
glDeleteShader(hFragmentShader);
}

// Create the program object
programObject = glCreateProgram();
if(programObject == 0)
{
printf("\n programObject");
return 0;
}
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, hFragmentShader);
// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "g_vVertex");
glBindAttribLocation(programObject, 1, "g_vColor");

// Link the program
glLinkProgram(programObject);
// Check the link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if(!linked)
{
printf("\n Not linked");
GLint infoLen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen != 0)
{


char Log[1024];
GLint nLength;
glGetProgramInfoLog(programObject, 1024, &nLength, Log);
//RETAILMSG(DBG,(_T("Log %s\r\n"),Log));
printf("\n -- %s -- \n",Log);

return GL_FALSE;
}
glDeleteProgram(programObject);
glDeleteShader( vertexShader );
glDeleteShader( hFragmentShader );
return GL_FALSE;
}
printf("\n -- END of INIT \n ");
return GL_TRUE;
}

void Render()
{

GLfloat fSize = 0.5f;
GLfloat VertexPositions[] =
{
-0.8, -0.8,
-0.8, 0.8,
0.8,0.8,
0.8,-0.8


};
GLfloat VertexColors[] = {1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
// Clear the backbuffer and depth-buffer
glClearColor( 0.0f, 0.0f, 0.5f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Set the shader program and the texture
glUseProgram(programObject);

// Draw the colored triangle
glVertexAttribPointer( 0, 2, GL_FLOAT, 0, 0, VertexPositions );

glEnableVertexAttribArray( 0 );

glVertexAttribPointer( 1, 4, GL_FLOAT, 0, 0, VertexColors);

glEnableVertexAttribArray( 1 );

glDrawArrays( GL_TRIANGLE_FAN, 0, 3 );

glDisableVertexAttribArray( 0 );

glDisableVertexAttribArray( 1 );

eglSwapBuffers(glesDisplay, glesSurface);
assert(eglGetError() == EGL_SUCCESS);

}

int main()
{
if(!InitOGLES(0))
{
printf(" \n Failed in InitOGLES \n ");
return 0;
}
while(1)
Render();
return 0;

}