pizzaboy150
Posts: 18
Joined: Sat Sep 05, 2015 11:03 am

OpenGL ES2 shader error! Help!

Sat Sep 05, 2015 11:09 am

Hi Chaps,

Learning to write openGL ES2 but pretty much failed at the first hurdle, I am pretty certain its a shader error but the code and output is listed below!

Error
Created Vertex Shader!
Created Fragment Shader!
Segmentation fault

Code: Select all

#include <stdio.h>
#include "bcm_host.h"
#include "egl.h"
#include "gl2.h"

EGLDisplay egldisplay;
EGLConfig eglconfig;
EGLSurface eglsurface;
EGLContext eglcontext;
GLuint programObject;

GLuint LoadShader(const char *shaderSrc, GLenum type)
{
    GLuint shader;
    GLint compiled;
    // Create the shader object
    shader = glCreateShader(type);
    if(shader == 0)
    printf("Unable to create shader object!n");
    return 0;
    // Load the shader source
    glShaderSource(shader, 1, &shaderSrc, NULL); // Error happens here!!!
    // Compile the shader
    glCompileShader(shader);
    // Check the compile status
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
    if(!compiled)
    {
        GLint infoLen = 0;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
        glDeleteShader(shader);
        printf("Unable to compile shader!n");
        return 0;
    }
    return shader;
}

//
// Initialize the shader and program object
//
int Init()
{
    const char vShaderStr[] =
    "attribute vec4 vPosition;   n"
    "void main()                 n"
    "{                           n"
        "   gl_Position = vPosition; n"
    "}                           n";
    const char fShaderStr[] =
    "precision mediump float;                   n"
    "void main()                                n"
    "{                                          n"
        "  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); n"
    "}                                          n";
    GLuint vertexShader;
    GLuint fragmentShader;
    GLuint programObject;
    GLint linked;
    
    //Create the vertex shader
    vShader = glCreateShader(GL_VERTEX_SHADER);
    if(vShader == 0) {
        printf("Unable to create vertex shader object!n");
        return 1;
    }
    
    // Load the shader source
    glShaderSource(vShader, 1, &vShaderStr, NULL);
    
    // Compile the shader
    glCompileShader(vShader);
    
    // Check the compile status
    glGetShaderiv(vShader, GL_COMPILE_STATUS, &compiled);
    if(!compiled)
    {
        GLint infoLen = 0;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
        glDeleteShader(shader);
        printf("Unable to compile shader!n");
        return 0;
    }
    
    
    
    
    
    // Load the vertex/fragment shaders
    vertexShader = LoadShader(vShaderStr, GL_VERTEX_SHADER);
    fragmentShader = LoadShader(fShaderStr, GL_FRAGMENT_SHADER);
    
    // Create the program object
    programObject = glCreateProgram();
    
    if(programObject == 0) {
        printf("Unable to create Program Objectn");
        return 1;
    }
    glAttachShader(programObject, vertexShader);
    glAttachShader(programObject, fragmentShader);
    // Bind vPosition to attribute 0
    glBindAttribLocation(programObject, 0, "vPosition");
    // Link the program
    glLinkProgram(programObject);
    // Check the link status
    glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
    if(!linked)
    {
        glDeleteProgram(programObject);
        printf("Unable to link program");
        return 1;
    }
    // Store the program object
    proObj = programObject;
    return 0;
}

void DrawTriangle()
{
    GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
        -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f};
    
    // Use the program object
    glUseProgram(proObj);
    
    // Load the vertex data
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
    glEnableVertexAttribArray(0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

void render(int w, int h)
{
    //clear the buffer
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    //draw triangle
    DrawTriangle();
    //actually draw the stuff to the screen
    eglSwapBuffers(egldisplay, eglsurface);
}

void init(NativeWindowType window)
{
    static const EGLint s_configAttribs[] =
    {
        EGL_RED_SIZE,        8,
        EGL_GREEN_SIZE,     8,
        EGL_BLUE_SIZE,        8,
        EGL_ALPHA_SIZE,     8,
        EGL_LUMINANCE_SIZE, EGL_DONT_CARE,
        EGL_SURFACE_TYPE,    EGL_WINDOW_BIT,
        EGL_SAMPLES,        1,
        EGL_NONE
    };
    EGLint numconfigs;
    
    egldisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(egldisplay, NULL, NULL);
    eglBindAPI(EGL_OPENGL_ES_API);
    
    eglChooseConfig(egldisplay, s_configAttribs, &eglconfig, 1, &numconfigs);
    
    eglsurface = eglCreateWindowSurface(egldisplay, eglconfig, window, NULL);
    eglcontext = eglCreateContext(egldisplay, eglconfig, NULL, NULL);
    eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext);
}

void deinit(void)
{
    eglMakeCurrent(egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglTerminate(egldisplay);
    eglReleaseThread();
}

int main(void)
{
    uint32_t width, height;
    bcm_host_init();
    int s;
    
    static EGL_DISPMANX_WINDOW_T nativewindow;
    
    DISPMANX_ELEMENT_HANDLE_T dispman_element;
    DISPMANX_DISPLAY_HANDLE_T dispman_display;
    DISPMANX_UPDATE_HANDLE_T dispman_update;
    VC_RECT_T dst_rect;
    VC_RECT_T src_rect;
    
    s = graphics_get_display_size(0 /* LCD */, &width, &height);
    
    dst_rect.x = 0;
    dst_rect.y = 0;
    dst_rect.width = width;
    dst_rect.height = height;
    
    src_rect.x = 0;
    src_rect.y = 0;
    src_rect.width = width << 16;
    src_rect.height = height << 16;
    
    dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
    dispman_update = vc_dispmanx_update_start( 0 );
    
    dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display,
    1/*layer*/, &dst_rect, 0/*src*/,
    &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/);
    
    nativewindow.element = dispman_element;
    nativewindow.width = width;
    nativewindow.height = height;
    vc_dispmanx_update_submit_sync( dispman_update );
    
    init(&nativewindow);
    
    // Set the viewport
    if(Init() != 0) {
        printf("Unable to INIT!n");
        return 1;
    }
    glViewport(0, 0, width, height);
    
    //60 frames per second at 600 = 10 seconds
    int count = 0;
    while (count < 600) {
        render(width, height);
        count++;
    }
    deinit();
    
    return 0;
}

pizzaboy150
Posts: 18
Joined: Sat Sep 05, 2015 11:03 am

Re: OpenGL ES2 shader error! Help!

Sat Sep 05, 2015 4:27 pm

Ignore this post sorry I posted the wrong code and when posting the right code I noticed the format of my inline shader code is wrong.

User avatar
PeterO
Posts: 5072
Joined: Sun Jul 22, 2012 4:14 pm

Re: OpenGL ES2 shader error! Help!

Sat Sep 05, 2015 5:00 pm

Inlined shader code is really a pain. I always put shaders into files these days. That makes them much easier to edit (and bug fix)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

pizzaboy150
Posts: 18
Joined: Sat Sep 05, 2015 11:03 am

Re: OpenGL ES2 shader error! Help!

Sun Sep 06, 2015 1:58 pm

Though if anyone wants a working OpenGL ES2 example then here is the code and makefile.

Makfile
IN=main.c
OUT=main.bin
FLAGS=-w
INC=-I/opt/vc/include/ -I/opt/vc/include/EGL -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/GLES2/
LIBS=-L/opt/vc/lib/ -lbcm_host -lEGL -lGLESv2 -lvcos -lvchiq_arm -lpthread -lrt
COMP=gcc

all:
$(COMP) $(FLAGS) $(IN) -o $(OUT) $(INC) $(LIBS)


The Code

Code: Select all

#include <stdio.h>
#include "bcm_host.h"
#include "egl.h"
#include "gl2.h"

EGLDisplay egldisplay;
EGLConfig eglconfig;
EGLSurface eglsurface;
EGLContext eglcontext;
GLuint programObject;

//
// Initialize the shader and program object
//
int initShader()
{
	const GLchar *vShaderStr[] = {
                        "attribute vec4 vPosition;   \n"
                        "void main(){                \n"
                        "   gl_Position = vPosition; \n"
                        "}                           \n"};
	const GLchar *fShaderStr[] = {
                        "precision mediump float;                    \n"
                        "void main(){                                \n"
                        "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n"
                        "}                                           \n"};

	GLint linked;
	GLuint vShader;
	GLuint fShader;
	GLint vCompiled;
	GLint fCompiled;

	//Create shader objects
	vShader = glCreateShader(GL_VERTEX_SHADER);
	if(vShader == 0) {
		printf("Unable to create vertex shader\n");
		return 1;
	}

	fShader = glCreateShader(GL_FRAGMENT_SHADER);
	if(fShader == 0) {
		printf("Unable to create fragment shader\n");
		return 1;
	}
    printf("Created Fragment Shader!\n");

    //load vertex shader source
    glShaderSource(vShader, 1, vShaderStr, NULL);
    printf("Loaded Vertex Shader Source!\n");

    //Compile vertex shader
	glCompileShader(vShader);
    printf("Compiled Vertex Shader!\n");

	// Check the compile status
	glGetShaderiv(vShader, GL_COMPILE_STATUS, &vCompiled);
	if(!vCompiled)
	{
		printf("Unable to compile vertex shader!\n");
		return 1;
	}
    printf("Compiled Vertex Shader!\n");

	//load fragment shader source
	glShaderSource(fShader, 1, fShaderStr, NULL);
	printf("Loaded Fragment Shader Source!\n");

	//compile fragment shader
	glCompileShader(fShader);
    printf("Compiled Fragment Shader!\n");

	// Check the compile status
	glGetShaderiv(fShader, GL_COMPILE_STATUS, &fCompiled);
	if(!fCompiled)
	{
		printf("Unable to compile fragment shader!\n");
		return 1;
	}
    printf("Compiled Fragment Shader!\n");

	// Create the program object
	programObject = glCreateProgram();

	if(programObject == 0) {
		printf("Unable to create Program Object\n");
		return 1;
	}
    printf("Created Program Object!\n");

	//Attach shaders to the program object
	glAttachShader(programObject, vShader);
	glAttachShader(programObject, fShader);
	printf("Attached Shaders!\n");

	// 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 > 1)
         {
             char* infoLog = malloc(sizeof(char) * infoLen);
             glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
             printf("Error linking program:\n%s\n", infoLog);

             free(infoLog);
         }
         glDeleteProgram(programObject);
         return 1;
	}
    printf("Linked Program!\n");
    return 0;
}

void init(NativeWindowType window)
{
	static const EGLint s_configAttribs[] =
	{
		EGL_RED_SIZE,		8,
		EGL_GREEN_SIZE, 	8,
		EGL_BLUE_SIZE,		8,
		EGL_ALPHA_SIZE, 	8,
		EGL_LUMINANCE_SIZE, EGL_DONT_CARE,
		EGL_SURFACE_TYPE,	EGL_WINDOW_BIT,
		EGL_SAMPLES,		1,
		EGL_NONE
	};

	static const EGLint context_attributes[] =
	{
		EGL_CONTEXT_CLIENT_VERSION, 2,
		EGL_NONE
	};

	EGLint numconfigs;

	egldisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
	eglInitialize(egldisplay, NULL, NULL);
	eglBindAPI(EGL_OPENGL_ES_API);

	eglChooseConfig(egldisplay, s_configAttribs, &eglconfig, 1, &numconfigs);

	eglsurface = eglCreateWindowSurface(egldisplay, eglconfig, window, NULL);
	eglcontext = eglCreateContext(egldisplay, eglconfig, EGL_NO_CONTEXT, context_attributes);
	eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext);
}

void deinit(void)
{
	eglMakeCurrent(egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
	eglTerminate(egldisplay);
	eglReleaseThread();
}

int main(void)
{
	uint32_t width, height;
	bcm_host_init();
	int s;

	static EGL_DISPMANX_WINDOW_T nativewindow;

	DISPMANX_ELEMENT_HANDLE_T dispman_element;
	DISPMANX_DISPLAY_HANDLE_T dispman_display;
	DISPMANX_UPDATE_HANDLE_T dispman_update;
	VC_RECT_T dst_rect;
	VC_RECT_T src_rect;

	s = graphics_get_display_size(0 /* LCD */, &width, &height);

	dst_rect.x = 0;
	dst_rect.y = 0;
	dst_rect.width = width;
	dst_rect.height = height;

	src_rect.x = 0;
	src_rect.y = 0;
	src_rect.width = width << 16;
	src_rect.height = height << 16;

	dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
	dispman_update = vc_dispmanx_update_start( 0 );

	dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display,
		1/*layer*/, &dst_rect, 0/*src*/,
		&src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/);

	nativewindow.element = dispman_element;
	nativewindow.width = width;
	nativewindow.height = height;
	vc_dispmanx_update_submit_sync( dispman_update );

	init(&nativewindow);

	//Create the shader programs
	if(initShader() != 0) {
		printf("Unable to INIT!\n");
		return 1;
	}

	//Triangle Data
	GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
                           -0.5f, -0.5f, 0.0f,
                           0.5f, -0.5f, 0.0f};

   	// Not sure what this does
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
	glEnableVertexAttribArray(0);
	glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
	glUseProgram(programObject);

   //60 frames per second at 600 = 10 seconds
   //Assuming vsync 60hz here
   int count = 0;
   while (count < 600) {
		//clear the buffer
		glClear(GL_COLOR_BUFFER_BIT);

		//Draw the triangles
		glDrawArrays(GL_TRIANGLES, 0, 3);
		
		//actually draw the stuff to the screen
		eglSwapBuffers(egldisplay, eglsurface);

		//Increment timer
		count++;
   }
   deinit();

   return 0;
}

Return to “C/C++”