PDA

View Full Version : Tesselation shader is not working



DreamCore
08-18-2013, 12:32 PM
Hi, I am trying to create a simple Tesselation shader (TCS + TES). I have the following code:


#include "sb6.h"

#include <math.h>

class my_application : public sb6::application
{
private:
GLuint rendering_program;
GLuint vertex_array_object;

public:
void startup(void)
{
rendering_program = compile_shaders();
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
}

void shutdown(void)
{
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(rendering_program);
}

void render(double currentTime)
{
const GLfloat clear_color[] = { 0.0f, 0.2f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, clear_color);

// Use the program object for rendering
glUseProgram(rendering_program);

GLfloat attrib_offset[] = { (float)sin(currentTime) * 0.5f,
(float)cos(currentTime) * 0.6f,
0.0f, 0.0f };

// Update the value of input attribute 0
glVertexAttrib4fv(0, attrib_offset);

// Draw one point
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_TRIANGLES, 0, 3);
}

GLuint compile_shaders(void)
{
GLuint vertex_shader;
GLuint tesselation_control_shader;
GLuint tesselation_evaluation_shader;
GLuint fragment_shader;
GLuint program;

// Source code for the vertex shader
static const GLchar *vertex_shader_source[] =
{
"#version 430 core \n"
" \n"
"layout (location = 0) in vec4 offset; \n"
" \n"
"void main(void) \n"
"{ \n"
" const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0), \n"
" vec4(-0.25, -0.25, 0.5, 1.0), \n"
" vec4( 0.25, 0.25, 0.5, 1.0)); \n"
" \n"
" gl_Position = vertices[gl_VertexID] + offset; \n"
"} \n"
};

// Source code for the tesselation control shader
static const GLchar *tesselation_control_shader_source[] =
{
"#version 430 core \n"
" \n"
"layout (vertices = 3) out; \n"
" \n"
"void main(void) \n"
"{ \n"
" if(gl_InvocationID == 0) \n"
" { \n"
" gl_TessLevelInner[0] = 5.0; \n"
" gl_TessLevelOuter[0] = 5.0; \n"
" gl_TessLevelOuter[1] = 5.0; \n"
" gl_TessLevelOuter[2] = 5.0; \n"
" } \n"
" gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; \n"
"} \n"
};

// Source code for the tesselation evaluation shader
static const GLchar *tesselation_evaluation_shader_source[] =
{
"#version 430 core \n"
" \n"
"layout (triangles, equal_spacing, cw) in; \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = (gl_TessCord.x * gl_in[0].gl_Position + \n"
" gl_TessCord.y * gl_in[1].gl_Position + \n"
" gl_TessCord.z * gl_in[2].gl_Position); \n"
"} \n"
};

// Source code for the fragment shader
static const GLchar *fragment_shader_source[] =
{
"#version 430 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(1.0, 1.0, 1.0, 1.0); \n"
"} \n"
};

// Create and compile the vertex shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);

// Create and compile the tesselation control shader
tesselation_control_shader = glCreateShader(GL_TESS_CONTROL_SHADER);
glShaderSource(tesselation_control_shader, 1, tesselation_control_shader_source, NULL);
glCompileShader(tesselation_control_shader);

// Create and compile the tesselation evaluation shader
tesselation_evaluation_shader = glCreateShader(GL_TESS_EVALUATION_SHADER);
glShaderSource(tesselation_evaluation_shader, 1, tesselation_evaluation_shader_source, NULL);
glCompileShader(tesselation_evaluation_shader);

// Create and compile the fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);

// Create program, attach shaders to it, and link it
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, tesselation_control_shader);
glAttachShader(program, tesselation_evaluation_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);

// Delete the shaders as the program has them now
glDeleteShader(vertex_shader);
glDeleteShader(tesselation_control_shader);
glDeleteShader(tesselation_evaluation_shader);
glDeleteShader(fragment_shader);

return program;
}
};

DECLARE_MAIN(my_application);

However the triangle is not displayed on screen. If I comment out the lines that attaches the TCS and TES, then the triangle is drawn correctly which must mean there is something wrong with my tesselation shaders. However I can not understand what it is!

PS. The forum screws up the formatting of my code in some places...

Thanks/
Robin

arekkusu
08-19-2013, 09:57 AM
glGetError() should tell you GL_INVALID_OPERATION on your glDrawArrays call. You must draw GL_PATCHES when using tessellation shaders.

DreamCore
08-19-2013, 11:31 AM
I changed:

glDrawArrays(GL_TRIANGLES, 0, 3);

into:

glDrawArrays(GL_PATCHES, 0, 3);

but unfortunately it still does not work.

arekkusu
08-19-2013, 02:14 PM
Check the compile status and glGetProgramInfoLog. You've typo'd
gl_TessCord instead of gl_TessCoord.

DreamCore
08-19-2013, 02:18 PM
Oh, didn't notice that typo. Now it is working! Thanks a lot :)