0
\$\begingroup\$

I've been trying to play around with tessellation shaders in OpenGL for a couple of hours now, but I'm having trouble to get it to work. I've been following The Little Grasshopper Triangle Tessellation with OpenGL 4.0, and I believed I had done the necessary OpenGL bureaucracy to get it to execute (but apparently not). My setup geometry is basically two triangles combined into a square, with just two attributes: position and texture coords. I have a vertex, tessellation control + evaluation, and also a fragment shader. You'll find all of the shaders below, as well as the setup code for the shaders (it's a wrapper, but there is nothing too surprising in there). Anyway, I've also added a screenshot of when I have the tessellation shaders on and off, and when they aren't enabled (just the vertex and the frag. shader), everything works OK. I've on purpose done so the TCS doesn't give new geometry, it just tries to mirror the input given (I'm aware it won't tesselate anything yet), but that doesn't work either.

I've also tried to move over the transformations done in the vertex shader (i.e. the MVP-transform) to the TES, but that didn't seem to help either. I thought it might have been the cause since I was doing tessellation work in camera-space (that is what is being done below, but I've tried the latter as well).

I'm honestly baffled, I can't for the life of me figure out what could be wrong. Maybe it's just some typo or some sort of name conflict in the interface blocks but I can't spot it. Any help is greatly appreciated!

Thanks for the help!


#version 410

// Vertex shader

in vec3 position;
in vec2 texture_coordinate;

uniform mat4 model;
uniform mat4 projection_view;

out PipelineData {
    vec4 position;
    vec2 texture_coordinate;
} vs_out;

void main() {
    mat4 pvm = projection_view * model;
    vs_out.texture_coordinate = texture_coordinate;
    vec4 homogenous_position = vec4(position, 1.0);
    vs_out.position = pvm * homogenous_position;

    gl_Position = vs_out.position;
}

#version 410

// TessControl shader

layout(vertices = 3) out;

in PipelineData {
    vec4 position;
    vec2 texture_coordinate;
} tc_in[];

out PipelineData {
    vec4 position;
    vec2 texture_coordinate;
} tc_out[];

void main() {
    tc_out[gl_InvocationID].texture_coordinate = tc_in[gl_InvocationID].texture_coordinate;
    tc_out[gl_InvocationID].position = tc_in[gl_InvocationID].position;

    gl_TessLevelInner[0] = 1;
    gl_TessLevelOuter[0] = 1;
    gl_TessLevelOuter[1] = 1;
    gl_TessLevelOuter[2] = 1;
}

#version 410

// TessEval shader

layout(triangles) in;

in PipelineData {
    vec4 position;
    vec2 texture_coordinate;
} te_in[];

out PipelineData {
    vec4 position;
    vec2 texture_coordinate;
} te_out;

void main() {
    te_out.texture_coordinate  = gl_TessCoord.x * te_in[0].texture_coordinate;
    te_out.texture_coordinate += gl_TessCoord.y * te_in[1].texture_coordinate;
    te_out.texture_coordinate += gl_TessCoord.z * te_in[2].texture_coordinate;

    te_out.position  = gl_TessCoord.x * te_in[0].position;
    te_out.position += gl_TessCoord.y * te_in[1].position;
    te_out.position += gl_TessCoord.z * te_in[2].position;

    gl_Position = te_out.position;
}

#version 410

// Fragment shader

in PipelineData {
    vec4 position;
    vec2 texture_coordinate;
} fs_in;

uniform sampler2D diffuse_texture;

out vec4 color;

void main() {
    vec4 diffuse_texel = texture(diffuse_texture, fs_in.texture_coordinate);
    color = vec4(1.0); // color = vec4(diffuse_texel);
}

// Excerpt of the setup code, with tessellation (see image below).
glPatchParameteri(GL_PATCH_VERTICES, 3);
Shader vertex_shader { PATH("shader/triangle.vert"), Shader::Type::Vertex },
       tesselation_control_shader { PATH("shader/triangle.tesc"), Shader::Type::TessControl },
       tesselation_evaluation_shader { PATH("shader/triangle.tese"), Shader::Type::TessEvaluation },
       fragment_shader { PATH("shader/triangle.frag"), Shader::Type::Fragment };

ShaderProgram shader_program { vertex_shader,
                              tesselation_control_shader, tesselation_evaluation_shader,
                              fragment_shader };

with the tesselation stages


// Excerpt of the setup code, tessellation disabled (see image below).
// glPatchParameteri(GL_PATCH_VERTICES, 3);
Shader vertex_shader { PATH("shader/triangle.vert"), Shader::Type::Vertex },
       // tesselation_control_shader { PATH("shader/triangle.tesc"), Shader::Type::TessControl },
       // tesselation_evaluation_shader { PATH("shader/triangle.tese"), Shader::Type::TessEvaluation },
       fragment_shader { PATH("shader/triangle.frag"), Shader::Type::Fragment };

ShaderProgram shader_program { vertex_shader,
                              // tesselation_control_shader, tesselation_evaluation_shader,
                              fragment_shader };

without the tesselation stages


GLFW version: 3.2.1 X11 GLX EGL clock_gettime /dev/js Xf86vm shared
GLEW version: 2.1.0
OpenGL vendor: X.Org
OpenGL renderer: AMD CYPRESS (DRM 2.50.0 / 4.14.13-1-ARCH, LLVM 5.0.1)
OpenGL version: 4.1 (Core Profile) Mesa 17.3.1
OpenGL GLSL version: 4.10
OpenGL extensions: GL_AMD_conservative_depth GL_AMD_draw_buffers_blend GL_AMD_performance_monitor GL_AMD_pinned_memory GL_AMD_seamless_cubemap_per_texture GL_AMD_shader_stencil_export GL_AMD_shader_trinary_minmax GL_AMD_vertex_shader_layer GL_AMD_vertex_shader_viewport_index GL_ANGLE_texture_compression_dxt3 GL_ANGLE_texture_compression_dxt5 GL_ARB_ES2_compatibility GL_ARB_ES3_compatibility GL_ARB_arrays_of_arrays GL_ARB_base_instance GL_ARB_blend_func_extended GL_ARB_buffer_storage GL_ARB_clear_buffer_object GL_ARB_clear_texture GL_ARB_clip_control GL_ARB_compressed_texture_pixel_storage GL_ARB_conditional_render_inverted GL_ARB_conservative_depth GL_ARB_copy_buffer GL_ARB_copy_image GL_ARB_debug_output GL_ARB_depth_buffer_float GL_ARB_depth_clamp GL_ARB_derivative_control GL_ARB_direct_state_access GL_ARB_draw_buffers GL_ARB_draw_buffers_blend GL_ARB_draw_elements_base_vertex GL_ARB_draw_indirect GL_ARB_draw_instanced GL_ARB_explicit_attrib_location GL_ARB_explicit_uniform_location GL_ARB_fragment_coord_conventions GL_ARB_fragment_layer_viewport GL_ARB_fragment_shader GL_ARB_framebuffer_no_attachments GL_ARB_framebuffer_object GL_ARB_framebuffer_sRGB GL_ARB_get_program_binary GL_ARB_get_texture_sub_image GL_ARB_gpu_shader5 GL_ARB_gpu_shader_fp64 GL_ARB_half_float_pixel GL_ARB_half_float_vertex GL_ARB_instanced_arrays GL_ARB_internalformat_query GL_ARB_internalformat_query2 GL_ARB_invalidate_subdata GL_ARB_map_buffer_alignment GL_ARB_map_buffer_range GL_ARB_multi_bind GL_ARB_multi_draw_indirect GL_ARB_occlusion_query2 GL_ARB_pipeline_statistics_query GL_ARB_pixel_buffer_object GL_ARB_point_sprite GL_ARB_polygon_offset_clamp GL_ARB_program_interface_query GL_ARB_provoking_vertex GL_ARB_robustness GL_ARB_sample_shading GL_ARB_sampler_objects GL_ARB_seamless_cube_map GL_ARB_seamless_cubemap_per_texture GL_ARB_separate_shader_objects GL_ARB_shader_bit_encoding GL_ARB_shader_objects GL_ARB_shader_precision GL_ARB_shader_stencil_export GL_ARB_shader_subroutine GL_ARB_shader_texture_image_samples GL_ARB_shader_texture_lod GL_ARB_shading_language_420pack GL_ARB_shading_language_packing GL_ARB_stencil_texturing GL_ARB_sync GL_ARB_tessellation_shader GL_ARB_texture_barrier GL_ARB_texture_buffer_object GL_ARB_texture_buffer_object_rgb32 GL_ARB_texture_buffer_range GL_ARB_texture_compression_bptc GL_ARB_texture_compression_rgtc GL_ARB_texture_cube_map_array GL_ARB_texture_filter_anisotropic GL_ARB_texture_float GL_ARB_texture_gather GL_ARB_texture_mirror_clamp_to_edge GL_ARB_texture_multisample GL_ARB_texture_non_power_of_two GL_ARB_texture_query_levels GL_ARB_texture_query_lod GL_ARB_texture_rectangle GL_ARB_texture_rg GL_ARB_texture_rgb10_a2ui GL_ARB_texture_stencil8 GL_ARB_texture_storage GL_ARB_texture_storage_multisample GL_ARB_texture_swizzle GL_ARB_texture_view GL_ARB_timer_query GL_ARB_transform_feedback2 GL_ARB_transform_feedback3 GL_ARB_transform_feedback_instanced GL_ARB_uniform_buffer_object GL_ARB_vertex_array_bgra GL_ARB_vertex_array_object GL_ARB_vertex_attrib_64bit GL_ARB_vertex_attrib_binding GL_ARB_vertex_shader GL_ARB_vertex_type_10f_11f_11f_rev GL_ARB_vertex_type_2_10_10_10_rev GL_ARB_viewport_array GL_ATI_blend_equation_separate GL_ATI_meminfo GL_ATI_texture_float GL_ATI_texture_mirror_once GL_EXT_abgr GL_EXT_blend_equation_separate GL_EXT_draw_buffers2 GL_EXT_draw_instanced GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample GL_EXT_framebuffer_multisample_blit_scaled GL_EXT_framebuffer_sRGB GL_EXT_packed_depth_stencil GL_EXT_packed_float GL_EXT_pixel_buffer_object GL_EXT_polygon_offset_clamp GL_EXT_provoking_vertex GL_EXT_shader_integer_mix GL_EXT_texture_array GL_EXT_texture_compression_dxt1 GL_EXT_texture_compression_rgtc GL_EXT_texture_compression_s3tc GL_EXT_texture_filter_anisotropic GL_EXT_texture_integer GL_EXT_texture_mirror_clamp GL_EXT_texture_sRGB GL_EXT_texture_sRGB_decode GL_EXT_texture_shared_exponent GL_EXT_texture_snorm GL_EXT_texture_swizzle GL_EXT_timer_query GL_EXT_transform_feedback GL_EXT_vertex_array_bgra GL_IBM_multimode_draw_arrays GL_KHR_context_flush_control GL_KHR_debug GL_KHR_no_error GL_MESA_pack_invert GL_MESA_shader_integer_functions GL_MESA_texture_signed_rgba GL_NVX_gpu_memory_info GL_NV_conditional_render GL_NV_depth_clamp GL_NV_packed_depth_stencil GL_NV_texture_barrier GL_NV_vdpau_interop GL_OES_EGL_image GL_S3_s3tc 
\$\endgroup\$
1
\$\begingroup\$

After a couple of minutes after asking the question I was finally able to figure it out. It seems I missed to change the draw call to use patches instead of triangle primitives. For future reference to other ppl, when working with tessellation shaders, always use patches when drawing and set the amount too.

Below is the fix.

glPatchParameteri(GL_PATCH_VERTICES, 3);
Shader vertex_shader { PATH("shader/triangle.vert"), Shader::Type::Vertex },
       tesselation_control_shader { PATH("shader/triangle.tesc"), Shader::Type::TessControl },
       tesselation_evaluation_shader { PATH("shader/triangle.tese"), Shader::Type::TessEvaluation },
       fragment_shader { PATH("shader/triangle.frag"), Shader::Type::Fragment };

ShaderProgram shader_program { vertex_shader,
                              tesselation_control_shader, tesselation_evaluation_shader,
                              fragment_shader };

// in a galaxy far, far away.... from this code.

// Don't use! Tessellation shaders use GL_PATCHES!!!
// glDrawElements(GL_TRIANGLES, vertex_array.size(), // <-- ISSUE
//               GL_UNSIGNED_INT, nullptr);

// Use instead a call like below or DrawArrays.
// In conjunction with a glPatchParameter data.
glDrawElements(GL_PATCHES, vertex_array.size(), // <-------- FIX!
               GL_UNSIGNED_INT, nullptr);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.