使用统一的sampler2d[]时出现无法解释的行为

发布于 2024-11-18 01:43:48 字数 23576 浏览 3 评论 0原文

抱歉,但这需要一些解释。我正在努力让它尽可能简单。

我想做的事情: 我正在想象高度场。一个高度字段可以有多个补丁。补丁是一个较小的纹理,可以改变高度场。

我使用的是 OpenGL 4.0,主要是曲面细分着色器。然而对于这个问题来说这应该是无关紧要的。

什么已经在工作了。 我可以对高度场(没有补丁)进行可视化。该问题的有趣部分是曲面细分评估着色器和片段着色器。

曲面细分评估着色器从高度场采样器中获取每个顶点的高度。

layout(quads, fractional_odd_spacing, ccw) in;

out float onEdge;
out float teDistanceToMinHeight;
out vec4 tcPosition;

void main()
{
    // bilinear interpolate: position
    vec4 pos_a    = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
    vec4 pos_b    = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
    vec4 position = mix(pos_a, pos_b, gl_TessCoord.y);

    // bilinear interpolate: hf texture coordinate
    vec2 tex_a    = mix(gl_in[0].gl_TexCoord[HFTexCoordID].xy, gl_in[1].gl_TexCoord[HFTexCoordID].xy, gl_TessCoord.x);
    vec2 tex_b    = mix(gl_in[3].gl_TexCoord[HFTexCoordID].xy, gl_in[2].gl_TexCoord[HFTexCoordID].xy, gl_TessCoord.x);
    vec2 hfTexCoord = mix(tex_a, tex_b, gl_TessCoord.y);

    float height = getHeightFieldHeight(hfTexCoord);
    position.y = height;
    //position.y = getHeightFieldHeightMin();

    gl_Position = gl_ModelViewProjectionMatrix * position;
    tcPosition = gl_Position;
    gl_TexCoord[HFTexCoordID].xy = hfTexCoord;

    // a vertex is on the edge of a patch if one of the tess coords is 0
    onEdge = float((gl_TessCoord.x == 0 || gl_TessCoord.y == 0));
    teDistanceToMinHeight = height - getHeightFieldHeightMin();
}

片段着色器使用属于当前片段的顶点的相对高度来访问 1D 高度调色板纹理。请暂时忽略高度梯度。它用于计算法线。这工作完全没问题。

in float onEdge;
in float teDistanceToMinHeight;
in vec4 tcPosition;

out vec4 FragColor;

vec3
getHeightMapGradient(in vec2      ts_position,
                     in vec2      texel_offset)
{
    vec3 x = vec3(2.0 * texel_offset.x, 0.0, getHeightFieldHeight(ts_position + vec2(texel_offset.x, 0.0))
                                           - getHeightFieldHeight(ts_position - vec2(texel_offset.x, 0.0)));


    vec3 y = vec3(0.0, 2.0 * texel_offset.y, getHeightFieldHeight(ts_position + vec2(0.0, texel_offset.y))
                                           - getHeightFieldHeight(ts_position - vec2(0.0, texel_offset.y)));
    return cross(x, y);
}

void main()
{
    if(teDistanceToMinHeight < 0.00001){
        // filter points near the zero position
        discard;
    }

    // attributes
    vec2 hfTexCoords = gl_TexCoord[HFTexCoordID].xy;
    //float hfPaletteTexCoord = getHeightFieldHeight(hfTexCoords) / HeightFieldHeight;
    float hfPaletteTexCoord = (texture(HeightField, hfTexCoords).r -HeightFieldLowering);


    vec2 heightMapSize      = vec2(textureSize(HeightField, 0).xy);
    vec2 heightMapTexelSize = vec2(1.0) / vec2(heightMapSize);


    // some standard colors
    vec4 white = vec4(1);
    vec4 yellow = vec4(1,1,0,1);
    vec4 blue = vec4(0,0,1,1);

    // get the color
    FragColor = texture(HeightFieldPalette, hfPaletteTexCoord ); // use height field  palette as color

    if(onEdge > 0.9 && ShowOriginalGrid == 1 ){
        FragColor = mix(FragColor, yellow, 0.5);
    }

    // shading
    vec3 n = normalize(getHeightMapGradient(hfTexCoords, heightMapTexelSize).xzy);
    vec3 l = vec3(1,1,0);
    vec3 v = normalize(CameraPosition - tcPosition.xyz);
    vec3 h = normalize(l + v);

    float df= dot(n, l);

    FragColor = (FragColor * (df * 0.5 + 0.5)) // diffuse
                + vec4(1) * pow(max(0.0, dot(n,h)), 60.0) // specular
                + 0.1; // ambient


}

每个着色器之前包含以下代码。它主要包含所有统一函数和辅助函数。

#version 400 compatibility

// defines
#define HFTexCoordID 0

// environment
uniform ivec2 ScreenSize = ivec2(800,600);
uniform vec3 CameraPosition = vec3(0);

// tesselation
uniform float MaxEdgeLength = 4;

// height field
uniform float   HeightFieldHeight = 1;
uniform float   HeightFieldLowering = 0.2;

uniform sampler2D   HeightField;
uniform sampler1D   HeightFieldPalette;

// density map
uniform sampler2D   DensityMap;

// patches
uniform sampler2D[20]   Patches;
uniform int             PatchesCount = 0;
uniform ivec2[20]       PatchesPositions;
uniform float[20]       PatchesHeights;
uniform ivec2[20]       PatchesSizes;

// show options
uniform int ShowOriginalGrid = 0;


// functions
ivec2 getHFPosition(vec2 hfTexCoords){
    return ivec2(textureSize(HeightField, 0).xy * hfTexCoords);
}

ivec2 getPatchSize(in int patchIndex){
    return PatchesSizes[patchIndex];
    //return textureSize(Patches[patchIndex], 0).xy;
}

vec2 toPatchCoords(in int patchIndex, in vec2 hfTexCoords){
    ivec2 hfPosition = getHFPosition(hfTexCoords);
    ivec2 patchStart = PatchesPositions[patchIndex];
    ivec2 patchPos = hfPosition - patchStart;
    //return textureSize(Patches[1], 0).xy;

    return  vec2(patchPos / getPatchSize(patchIndex));
}

float getPatchHeight(in int patchIndex, in vec2 hfTexCoords){
    vec2 patchCoords = toPatchCoords(patchIndex, hfTexCoords);
    // seams like the combination of the for loop with this 
    // texture access results in undefined behavior.
    float relHeight = (texture(Patches[patchIndex], patchCoords).r -0.5);
    return relHeight * PatchesHeights[patchIndex] * HeightFieldHeight;
}

float getPatchedHeight(in vec2 texCoords){
    float patchesHeight = 0;
    // working
    //patchesHeight += getPatchHeight(0, texCoords);
    //patchesHeight += getPatchHeight(1, texCoords);
    //patchesHeight += getPatchHeight(2, texCoords);

    // only works for i < 5 .
    for(int i = 0; i < 6 && i < PatchesCount-1; i++){
        patchesHeight += getPatchHeight(i, texCoords);
    }

    return patchesHeight;
}

float getHeightFieldHeight(in vec2 textureCoordinates){
    float height = (texture(HeightField, textureCoordinates).r -HeightFieldLowering) * HeightFieldHeight;

    height += getPatchedHeight(textureCoordinates);

    return height;
}

float getHeightFieldHeightMin(){
    return (-0.2 * HeightFieldHeight);
}

float getDensity(in vec2 coords){
    return texture(DensityMap, coords).r;
}

问题
只要我不访问补丁制服,上面描述和粘贴的着色器都可以正常工作。

uniform sampler2D[20]   Patches;

Sampler2D[] 背后的想法是拥有一种数组,而该数组的每个纹理可能具有不同的大小。我知道每个构造都使用(在本例中)20 个纹理单元。这个限制就好了。

当我访问补丁时,片段着色器仅输出黑色像素。我知道,曲面细分评估着色器工作正常,因为我可以在高度字段中看到山丘,但这不是它的一部分。

我非常感谢任何建议。关于这个问题。

我知道我可以使用sampler2DArray,但是每个元素(纹理)必须具有相同的大小。但我需要补丁大小是灵活的。将补丁切割成固定大小的纹理并在着色器中将它们缝合在一起,这应该是一种替代方案,但我不想实现这种组织开销,除非我真的必须这样做。

信息日志

Visualization Lib

rary v2011.5.1142 [f32]
Jun  9 2011 - 11:36:25 - GCC compiler [RELEASE] [x32]

 --- Environment ---
VL_LOGFILE_PATH <not present>
VL_DATA_PATH <not present>
VL_VERBOSITY_LEVEL = NORMAL
VL_CHECK_GL_STATES = YES

 --- Global Settings --- 
Log file  = log.txt
Data path = ../data
Verbosity level = NORMAL
Check OpenGL States = YES

 --- GLEW ---
GLEW version: 1.5.7

 --- OpenGL Info ---
OpenGL version: 4.1.0 NVIDIA 260.19.21
OpenGL vendor: NVIDIA Corporation
OpenGL renderer: Quadro 6000/PCI/SSE2
OpenGL profile: Compatible
GLSL version: 4.10 NVIDIA via Cg compiler
Max texture size: 16384
Texture coords: 8
Texture conventional units: 4
Texture image units: 32
Anisotropic texture filter: YES, 16X
S3 Texture Compression: YES
Vertex Buffer Object: YES
Pixel Buffer Object: YES
Framebuffer Object: YES
Max vertex attributes: 16
Max varying floats: 60
Max fragment uniform components: 2048
Max vertex uniform components: 4096
Max elements vertices: 1048576
Max elements indices: 1048576

 --- OpenGL Extensions --- 
GL_ARB_blend_func_extended              GL_ARB_color_buffer_float
GL_ARB_compatibility                    GL_ARB_copy_buffer
GL_ARB_depth_buffer_float               GL_ARB_depth_clamp
GL_ARB_depth_texture                    GL_ARB_draw_buffers
GL_ARB_draw_buffers_blend               GL_ARB_draw_indirect
GL_ARB_draw_elements_base_vertex        GL_ARB_draw_instanced
GL_ARB_ES2_compatibility                GL_ARB_explicit_attrib_location
GL_ARB_fragment_coord_conventions       GL_ARB_fragment_program
GL_ARB_fragment_program_shadow          GL_ARB_fragment_shader
GL_ARB_framebuffer_object               GL_ARB_framebuffer_sRGB
GL_ARB_geometry_shader4                 GL_ARB_get_program_binary
GL_ARB_gpu_shader5                      GL_ARB_gpu_shader_fp64
GL_ARB_half_float_pixel                 GL_ARB_half_float_vertex
GL_ARB_imaging                          GL_ARB_instanced_arrays
GL_ARB_map_buffer_range                 GL_ARB_multisample
GL_ARB_multitexture                     GL_ARB_occlusion_query
GL_ARB_occlusion_query2                 GL_ARB_pixel_buffer_object
GL_ARB_point_parameters                 GL_ARB_point_sprite
GL_ARB_provoking_vertex                 GL_ARB_robustness
GL_ARB_sample_shading                   GL_ARB_sampler_objects
GL_ARB_seamless_cube_map                GL_ARB_separate_shader_objects
GL_ARB_shader_bit_encoding              GL_ARB_shader_objects
GL_ARB_shader_precision                 GL_ARB_shader_subroutine
GL_ARB_shading_language_100             GL_ARB_shadow
GL_ARB_sync                             GL_ARB_tessellation_shader
GL_ARB_texture_border_clamp             GL_ARB_texture_buffer_object
GL_ARB_texture_buffer_object_rgb32      GL_ARB_texture_compression
GL_ARB_texture_compression_bptc         GL_ARB_texture_compression_rgtc
GL_ARB_texture_cube_map                 GL_ARB_texture_cube_map_array
GL_ARB_texture_env_add                  GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar             GL_ARB_texture_env_dot3
GL_ARB_texture_float                    GL_ARB_texture_gather
GL_ARB_texture_mirrored_repeat          GL_ARB_texture_multisample
GL_ARB_texture_non_power_of_two         GL_ARB_texture_query_lod
GL_ARB_texture_rectangle                GL_ARB_texture_rg
GL_ARB_texture_rgb10_a2ui               GL_ARB_texture_swizzle
GL_ARB_timer_query                      GL_ARB_transform_feedback2
GL_ARB_transform_feedback3              GL_ARB_transpose_matrix
GL_ARB_uniform_buffer_object            GL_ARB_vertex_array_bgra
GL_ARB_vertex_array_object              GL_ARB_vertex_attrib_64bit
GL_ARB_vertex_buffer_object             GL_ARB_vertex_program
GL_ARB_vertex_shader                    GL_ARB_vertex_type_2_10_10_10_rev
GL_ARB_viewport_array                   GL_ARB_window_pos
GL_ATI_draw_buffers                     GL_ATI_texture_float
GL_ATI_texture_mirror_once              GL_S3_s3tc
GL_EXT_texture_env_add                  GL_EXT_abgr
GL_EXT_bgra                             GL_EXT_bindable_uniform
GL_EXT_blend_color                      GL_EXT_blend_equation_separate
GL_EXT_blend_func_separate              GL_EXT_blend_minmax
GL_EXT_blend_subtract                   GL_EXT_compiled_vertex_array
GL_EXT_Cg_shader                        GL_EXT_depth_bounds_test
GL_EXT_direct_state_access              GL_EXT_draw_buffers2
GL_EXT_draw_instanced                   GL_EXT_draw_range_elements
GL_EXT_fog_coord                        GL_EXT_framebuffer_blit
GL_EXT_framebuffer_multisample          GL_EXTX_framebuffer_mixed_formats
GL_EXT_framebuffer_object               GL_EXT_framebuffer_sRGB
GL_EXT_geometry_shader4                 GL_EXT_gpu_program_parameters
GL_EXT_gpu_shader4                      GL_EXT_multi_draw_arrays
GL_EXT_packed_depth_stencil             GL_EXT_packed_float
GL_EXT_packed_pixels                    GL_EXT_pixel_buffer_object
GL_EXT_point_parameters                 GL_EXT_provoking_vertex
GL_EXT_rescale_normal                   GL_EXT_secondary_color
GL_EXT_separate_shader_objects          GL_EXT_separate_specular_color
GL_EXT_shader_image_load_store          GL_EXT_shadow_funcs
GL_EXT_stencil_two_side                 GL_EXT_stencil_wrap
GL_EXT_texture3D                        GL_EXT_texture_array
GL_EXT_texture_buffer_object            GL_EXT_texture_compression_latc
GL_EXT_texture_compression_rgtc         GL_EXT_texture_compression_s3tc
GL_EXT_texture_cube_map                 GL_EXT_texture_edge_clamp
GL_EXT_texture_env_combine              GL_EXT_texture_env_dot3
GL_EXT_texture_filter_anisotropic       GL_EXT_texture_integer
GL_EXT_texture_lod                      GL_EXT_texture_lod_bias
GL_EXT_texture_mirror_clamp             GL_EXT_texture_object
GL_EXT_texture_shared_exponent          GL_EXT_texture_sRGB
GL_EXT_texture_swizzle                  GL_EXT_timer_query
GL_EXT_transform_feedback2              GL_EXT_vertex_array
GL_EXT_vertex_array_bgra                GL_EXT_vertex_attrib_64bit
GL_IBM_rasterpos_clip                   GL_IBM_texture_mirrored_repeat
GL_KTX_buffer_region                    GL_NV_blend_square
GL_NV_conditional_render                GL_NV_copy_depth_to_color
GL_NV_copy_image                        GL_NV_depth_buffer_float
GL_NV_depth_clamp                       GL_NV_explicit_multisample
GL_NV_fence                             GL_NV_float_buffer
GL_NV_fog_distance                      GL_NV_fragment_program
GL_NV_fragment_program_option           GL_NV_fragment_program2
GL_NV_framebuffer_multisample_coverage  GL_NV_geometry_shader4
GL_NV_gpu_program4                      GL_NV_gpu_program4_1
GL_NV_gpu_program5                      GL_NV_gpu_program_fp64
GL_NV_gpu_shader5                       GL_NV_half_float
GL_NV_light_max_exponent                GL_NV_multisample_coverage
GL_NV_multisample_filter_hint           GL_NV_occlusion_query
GL_NV_packed_depth_stencil              GL_NV_parameter_buffer_object
GL_NV_parameter_buffer_object2          GL_NV_pixel_data_range
GL_NV_point_sprite                      GL_NV_primitive_restart
GL_NV_register_combiners                GL_NV_register_combiners2
GL_NV_shader_buffer_load                GL_NV_texgen_reflection
GL_NV_texture_barrier                   GL_NV_texture_compression_vtc
GL_NV_texture_env_combine4              GL_NV_texture_expand_normal
GL_NV_texture_multisample               GL_NV_texture_rectangle
GL_NV_texture_shader                    GL_NV_texture_shader2
GL_NV_texture_shader3                   GL_NV_transform_feedback
GL_NV_transform_feedback2               GL_NV_vdpau_interop
GL_NV_vertex_array_range                GL_NV_vertex_array_range2
GL_NV_vertex_attrib_integer_64bit       GL_NV_vertex_buffer_unified_memory
GL_NV_vertex_program                    GL_NV_vertex_program1_1
GL_NV_vertex_program2                   GL_NV_vertex_program2_option
GL_NV_vertex_program3                   GL_NV_video_capture
GL_NVX_conditional_render               GL_NVX_gpu_memory_info
GL_SGIS_generate_mipmap                 GL_SGIS_texture_lod
GL_SGIX_depth_texture                   GL_SGIX_shadow
GL_SUN_slice_accum

 --- GLEW ---
GLEW version: 1.5.7

 --- OpenGL Info ---
OpenGL version: 4.1.0 NVIDIA 260.19.21
OpenGL vendor: NVIDIA Corporation
OpenGL renderer: Quadro 6000/PCI/SSE2
OpenGL profile: Compatible
GLSL version: 4.10 NVIDIA via Cg compiler
Max texture size: 16384
Texture coords: 8
Texture conventional units: 4
Texture image units: 32
Anisotropic texture filter: YES, 16X
S3 Texture Compression: YES
Vertex Buffer Object: YES
Pixel Buffer Object: YES
Framebuffer Object: YES
Max vertex attributes: 16
Max varying floats: 60
Max fragment uniform components: 2048
Max vertex uniform components: 4096
Max elements vertices: 1048576
Max elements indices: 1048576

 --- OpenGL Extensions --- 
GL_ARB_blend_func_extended              GL_ARB_color_buffer_float
GL_ARB_compatibility                    GL_ARB_copy_buffer
GL_ARB_depth_buffer_float               GL_ARB_depth_clamp
GL_ARB_depth_texture                    GL_ARB_draw_buffers
GL_ARB_draw_buffers_blend               GL_ARB_draw_indirect
GL_ARB_draw_elements_base_vertex        GL_ARB_draw_instanced
GL_ARB_ES2_compatibility                GL_ARB_explicit_attrib_location
GL_ARB_fragment_coord_conventions       GL_ARB_fragment_program
GL_ARB_fragment_program_shadow          GL_ARB_fragment_shader
GL_ARB_framebuffer_object               GL_ARB_framebuffer_sRGB
GL_ARB_geometry_shader4                 GL_ARB_get_program_binary
GL_ARB_gpu_shader5                      GL_ARB_gpu_shader_fp64
GL_ARB_half_float_pixel                 GL_ARB_half_float_vertex
GL_ARB_imaging                          GL_ARB_instanced_arrays
GL_ARB_map_buffer_range                 GL_ARB_multisample
GL_ARB_multitexture                     GL_ARB_occlusion_query
GL_ARB_occlusion_query2                 GL_ARB_pixel_buffer_object
GL_ARB_point_parameters                 GL_ARB_point_sprite
GL_ARB_provoking_vertex                 GL_ARB_robustness
GL_ARB_sample_shading                   GL_ARB_sampler_objects
GL_ARB_seamless_cube_map                GL_ARB_separate_shader_objects
GL_ARB_shader_bit_encoding              GL_ARB_shader_objects
GL_ARB_shader_precision                 GL_ARB_shader_subroutine
GL_ARB_shading_language_100             GL_ARB_shadow
GL_ARB_sync                             GL_ARB_tessellation_shader
GL_ARB_texture_border_clamp             GL_ARB_texture_buffer_object
GL_ARB_texture_buffer_object_rgb32      GL_ARB_texture_compression
GL_ARB_texture_compression_bptc         GL_ARB_texture_compression_rgtc
GL_ARB_texture_cube_map                 GL_ARB_texture_cube_map_array
GL_ARB_texture_env_add                  GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar             GL_ARB_texture_env_dot3
GL_ARB_texture_float                    GL_ARB_texture_gather
GL_ARB_texture_mirrored_repeat          GL_ARB_texture_multisample
GL_ARB_texture_non_power_of_two         GL_ARB_texture_query_lod
GL_ARB_texture_rectangle                GL_ARB_texture_rg
GL_ARB_texture_rgb10_a2ui               GL_ARB_texture_swizzle
GL_ARB_timer_query                      GL_ARB_transform_feedback2
GL_ARB_transform_feedback3              GL_ARB_transpose_matrix
GL_ARB_uniform_buffer_object            GL_ARB_vertex_array_bgra
GL_ARB_vertex_array_object              GL_ARB_vertex_attrib_64bit
GL_ARB_vertex_buffer_object             GL_ARB_vertex_program
GL_ARB_vertex_shader                    GL_ARB_vertex_type_2_10_10_10_rev
GL_ARB_viewport_array                   GL_ARB_window_pos
GL_ATI_draw_buffers                     GL_ATI_texture_float
GL_ATI_texture_mirror_once              GL_S3_s3tc
GL_EXT_texture_env_add                  GL_EXT_abgr
GL_EXT_bgra                             GL_EXT_bindable_uniform
GL_EXT_blend_color                      GL_EXT_blend_equation_separate
GL_EXT_blend_func_separate              GL_EXT_blend_minmax
GL_EXT_blend_subtract                   GL_EXT_compiled_vertex_array
GL_EXT_Cg_shader                        GL_EXT_depth_bounds_test
GL_EXT_direct_state_access              GL_EXT_draw_buffers2
GL_EXT_draw_instanced                   GL_EXT_draw_range_elements
GL_EXT_fog_coord                        GL_EXT_framebuffer_blit
GL_EXT_framebuffer_multisample          GL_EXTX_framebuffer_mixed_formats
GL_EXT_framebuffer_object               GL_EXT_framebuffer_sRGB
GL_EXT_geometry_shader4                 GL_EXT_gpu_program_parameters
GL_EXT_gpu_shader4                      GL_EXT_multi_draw_arrays
GL_EXT_packed_depth_stencil             GL_EXT_packed_float
GL_EXT_packed_pixels                    GL_EXT_pixel_buffer_object
GL_EXT_point_parameters                 GL_EXT_provoking_vertex
GL_EXT_rescale_normal                   GL_EXT_secondary_color
GL_EXT_separate_shader_objects          GL_EXT_separate_specular_color
GL_EXT_shader_image_load_store          GL_EXT_shadow_funcs
GL_EXT_stencil_two_side                 GL_EXT_stencil_wrap
GL_EXT_texture3D                        GL_EXT_texture_array
GL_EXT_texture_buffer_object            GL_EXT_texture_compression_latc
GL_EXT_texture_compression_rgtc         GL_EXT_texture_compression_s3tc
GL_EXT_texture_cube_map                 GL_EXT_texture_edge_clamp
GL_EXT_texture_env_combine              GL_EXT_texture_env_dot3
GL_EXT_texture_filter_anisotropic       GL_EXT_texture_integer
GL_EXT_texture_lod                      GL_EXT_texture_lod_bias
GL_EXT_texture_mirror_clamp             GL_EXT_texture_object
GL_EXT_texture_shared_exponent          GL_EXT_texture_sRGB
GL_EXT_texture_swizzle                  GL_EXT_timer_query
GL_EXT_transform_feedback2              GL_EXT_vertex_array
GL_EXT_vertex_array_bgra                GL_EXT_vertex_attrib_64bit
GL_IBM_rasterpos_clip                   GL_IBM_texture_mirrored_repeat
GL_KTX_buffer_region                    GL_NV_blend_square
GL_NV_conditional_render                GL_NV_copy_depth_to_color
GL_NV_copy_image                        GL_NV_depth_buffer_float
GL_NV_depth_clamp                       GL_NV_explicit_multisample
GL_NV_fence                             GL_NV_float_buffer
GL_NV_fog_distance                      GL_NV_fragment_program
GL_NV_fragment_program_option           GL_NV_fragment_program2
GL_NV_framebuffer_multisample_coverage  GL_NV_geometry_shader4
GL_NV_gpu_program4                      GL_NV_gpu_program4_1
GL_NV_gpu_program5                      GL_NV_gpu_program_fp64
GL_NV_gpu_shader5                       GL_NV_half_float
GL_NV_light_max_exponent                GL_NV_multisample_coverage
GL_NV_multisample_filter_hint           GL_NV_occlusion_query
GL_NV_packed_depth_stencil              GL_NV_parameter_buffer_object
GL_NV_parameter_buffer_object2          GL_NV_pixel_data_range
GL_NV_point_sprite                      GL_NV_primitive_restart
GL_NV_register_combiners                GL_NV_register_combiners2
GL_NV_shader_buffer_load                GL_NV_texgen_reflection
GL_NV_texture_barrier                   GL_NV_texture_compression_vtc
GL_NV_texture_env_combine4              GL_NV_texture_expand_normal
GL_NV_texture_multisample               GL_NV_texture_rectangle
GL_NV_texture_shader                    GL_NV_texture_shader2
GL_NV_texture_shader3                   GL_NV_transform_feedback
GL_NV_transform_feedback2               GL_NV_vdpau_interop
GL_NV_vertex_array_range                GL_NV_vertex_array_range2
GL_NV_vertex_attrib_integer_64bit       GL_NV_vertex_buffer_unified_memory
GL_NV_vertex_program                    GL_NV_vertex_program1_1
GL_NV_vertex_program2                   GL_NV_vertex_program2_option
GL_NV_vertex_program3                   GL_NV_video_capture
GL_NVX_conditional_render               GL_NVX_gpu_memory_info
GL_SGIS_generate_mipmap                 GL_SGIS_texture_lod
GL_SGIX_depth_texture                   GL_SGIX_shadow
GL_SUN_slice_accum

PatchesContainer::PatchesContainer: working dir: ./data/patches/pick_height_testing_png/
PatchesContainer::loadPatchesFromWorkingDir ignoring file = data/patches/pick_height_testing_png/.svn
Patch::initialize(): name   = data/patches/pick_height_testing_png/patch01-position=20x20-maxHeight=1.0.png
width  = 50
height = 50
depth  = 0
format = IF_LUMINANCE
type   = IT_UNSIGNED_BYTE
pitch  = 50
bytealign = 1
Patch::initialize(): name   = data/patches/pick_height_testing_png/patch02-position=20x2-maxHeight=1.5.png
width  = 20
height = 20
depth  = 0
format = IF_LUMINANCE
type   = IT_UNSIGNED_BYTE
pitch  = 20
bytealign = 1
Found matching densitymap ./data/density_maps/pick_height_testing_png.png
loading height field image
name   = ./data/horizons/pick_height_testing.png
width  = 200
height = 200
depth  = 0
format = IF_LUMINANCE
type   = IT_UNSIGNED_BYTE
pitch  = 200
bytealign = 1
name   = ./data/textures/tesselation_palette_blue_red.png
width  = 300
height = 0
depth  = 0
format = IF_RGB
type   = IT_UNSIGNED_BYTE
pitch  = 900
bytealign = 1
GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS: 32

I'm sorry but this will require a little bit of explanation. I'm trying to make it as simple as possible.

What I want to do:
I'm visualizing height fields. A height field may have multiple patches. A patch is a smaller texture that alters the height field.

I'm using OpenGL 4.0, mainly the tesselation shaders. However for this problem this should be irrelevant.

What is working allready.
I have the visualisation for the height field (without patches) working. The interessing parts in regard to the problem are the tesselation evaluation shader and the fragment shader.

The tesselation evaluation shader fetches for each vertex its height from the height field sampler.

layout(quads, fractional_odd_spacing, ccw) in;

out float onEdge;
out float teDistanceToMinHeight;
out vec4 tcPosition;

void main()
{
    // bilinear interpolate: position
    vec4 pos_a    = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
    vec4 pos_b    = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
    vec4 position = mix(pos_a, pos_b, gl_TessCoord.y);

    // bilinear interpolate: hf texture coordinate
    vec2 tex_a    = mix(gl_in[0].gl_TexCoord[HFTexCoordID].xy, gl_in[1].gl_TexCoord[HFTexCoordID].xy, gl_TessCoord.x);
    vec2 tex_b    = mix(gl_in[3].gl_TexCoord[HFTexCoordID].xy, gl_in[2].gl_TexCoord[HFTexCoordID].xy, gl_TessCoord.x);
    vec2 hfTexCoord = mix(tex_a, tex_b, gl_TessCoord.y);

    float height = getHeightFieldHeight(hfTexCoord);
    position.y = height;
    //position.y = getHeightFieldHeightMin();

    gl_Position = gl_ModelViewProjectionMatrix * position;
    tcPosition = gl_Position;
    gl_TexCoord[HFTexCoordID].xy = hfTexCoord;

    // a vertex is on the edge of a patch if one of the tess coords is 0
    onEdge = float((gl_TessCoord.x == 0 || gl_TessCoord.y == 0));
    teDistanceToMinHeight = height - getHeightFieldHeightMin();
}

The fragment shader uses the reative height of the vertex belonging to the fragment at hand to access a 1D height palett texture. Please ignore the height gradient for now. It's used to calc the normals. This works completely fine.

in float onEdge;
in float teDistanceToMinHeight;
in vec4 tcPosition;

out vec4 FragColor;

vec3
getHeightMapGradient(in vec2      ts_position,
                     in vec2      texel_offset)
{
    vec3 x = vec3(2.0 * texel_offset.x, 0.0, getHeightFieldHeight(ts_position + vec2(texel_offset.x, 0.0))
                                           - getHeightFieldHeight(ts_position - vec2(texel_offset.x, 0.0)));


    vec3 y = vec3(0.0, 2.0 * texel_offset.y, getHeightFieldHeight(ts_position + vec2(0.0, texel_offset.y))
                                           - getHeightFieldHeight(ts_position - vec2(0.0, texel_offset.y)));
    return cross(x, y);
}

void main()
{
    if(teDistanceToMinHeight < 0.00001){
        // filter points near the zero position
        discard;
    }

    // attributes
    vec2 hfTexCoords = gl_TexCoord[HFTexCoordID].xy;
    //float hfPaletteTexCoord = getHeightFieldHeight(hfTexCoords) / HeightFieldHeight;
    float hfPaletteTexCoord = (texture(HeightField, hfTexCoords).r -HeightFieldLowering);


    vec2 heightMapSize      = vec2(textureSize(HeightField, 0).xy);
    vec2 heightMapTexelSize = vec2(1.0) / vec2(heightMapSize);


    // some standard colors
    vec4 white = vec4(1);
    vec4 yellow = vec4(1,1,0,1);
    vec4 blue = vec4(0,0,1,1);

    // get the color
    FragColor = texture(HeightFieldPalette, hfPaletteTexCoord ); // use height field  palette as color

    if(onEdge > 0.9 && ShowOriginalGrid == 1 ){
        FragColor = mix(FragColor, yellow, 0.5);
    }

    // shading
    vec3 n = normalize(getHeightMapGradient(hfTexCoords, heightMapTexelSize).xzy);
    vec3 l = vec3(1,1,0);
    vec3 v = normalize(CameraPosition - tcPosition.xyz);
    vec3 h = normalize(l + v);

    float df= dot(n, l);

    FragColor = (FragColor * (df * 0.5 + 0.5)) // diffuse
                + vec4(1) * pow(max(0.0, dot(n,h)), 60.0) // specular
                + 0.1; // ambient


}

Includes before each shader is the following code. It contains mainly all uniform and helper functions.

#version 400 compatibility

// defines
#define HFTexCoordID 0

// environment
uniform ivec2 ScreenSize = ivec2(800,600);
uniform vec3 CameraPosition = vec3(0);

// tesselation
uniform float MaxEdgeLength = 4;

// height field
uniform float   HeightFieldHeight = 1;
uniform float   HeightFieldLowering = 0.2;

uniform sampler2D   HeightField;
uniform sampler1D   HeightFieldPalette;

// density map
uniform sampler2D   DensityMap;

// patches
uniform sampler2D[20]   Patches;
uniform int             PatchesCount = 0;
uniform ivec2[20]       PatchesPositions;
uniform float[20]       PatchesHeights;
uniform ivec2[20]       PatchesSizes;

// show options
uniform int ShowOriginalGrid = 0;


// functions
ivec2 getHFPosition(vec2 hfTexCoords){
    return ivec2(textureSize(HeightField, 0).xy * hfTexCoords);
}

ivec2 getPatchSize(in int patchIndex){
    return PatchesSizes[patchIndex];
    //return textureSize(Patches[patchIndex], 0).xy;
}

vec2 toPatchCoords(in int patchIndex, in vec2 hfTexCoords){
    ivec2 hfPosition = getHFPosition(hfTexCoords);
    ivec2 patchStart = PatchesPositions[patchIndex];
    ivec2 patchPos = hfPosition - patchStart;
    //return textureSize(Patches[1], 0).xy;

    return  vec2(patchPos / getPatchSize(patchIndex));
}

float getPatchHeight(in int patchIndex, in vec2 hfTexCoords){
    vec2 patchCoords = toPatchCoords(patchIndex, hfTexCoords);
    // seams like the combination of the for loop with this 
    // texture access results in undefined behavior.
    float relHeight = (texture(Patches[patchIndex], patchCoords).r -0.5);
    return relHeight * PatchesHeights[patchIndex] * HeightFieldHeight;
}

float getPatchedHeight(in vec2 texCoords){
    float patchesHeight = 0;
    // working
    //patchesHeight += getPatchHeight(0, texCoords);
    //patchesHeight += getPatchHeight(1, texCoords);
    //patchesHeight += getPatchHeight(2, texCoords);

    // only works for i < 5 .
    for(int i = 0; i < 6 && i < PatchesCount-1; i++){
        patchesHeight += getPatchHeight(i, texCoords);
    }

    return patchesHeight;
}

float getHeightFieldHeight(in vec2 textureCoordinates){
    float height = (texture(HeightField, textureCoordinates).r -HeightFieldLowering) * HeightFieldHeight;

    height += getPatchedHeight(textureCoordinates);

    return height;
}

float getHeightFieldHeightMin(){
    return (-0.2 * HeightFieldHeight);
}

float getDensity(in vec2 coords){
    return texture(DensityMap, coords).r;
}

The problem
The shaders described and pasted above are all working correctly as long as I don't access the Patches uniform.

uniform sampler2D[20]   Patches;

The idea behind the sampler2D[] is to have a sort of array while each texture of this array may have a different size. I'm aware of the fact that each of this constuct uses (in this case) 20 texture units. This limitation of fine.

The moment I access the Patches uniform the fragment shader only outputs black pixels. The tessellation evaluation shader is working correctly, that I know, because I can see hills in the height field, that arn't part of it.

I'm very thankfull for any suggestions. Regarding this problem.

I know I could use a sampler2DArray, but then each element (texture) has to have the same size. But I need the patch size to be flexable. Cutting patches into textures of a fixed size and stiching them back together in the shader, whould be an alternative, but I don't want to implement this organisation overhead unless I really have to.

Infolog

Visualization Lib

rary v2011.5.1142 [f32]
Jun  9 2011 - 11:36:25 - GCC compiler [RELEASE] [x32]

 --- Environment ---
VL_LOGFILE_PATH <not present>
VL_DATA_PATH <not present>
VL_VERBOSITY_LEVEL = NORMAL
VL_CHECK_GL_STATES = YES

 --- Global Settings --- 
Log file  = log.txt
Data path = ../data
Verbosity level = NORMAL
Check OpenGL States = YES

 --- GLEW ---
GLEW version: 1.5.7

 --- OpenGL Info ---
OpenGL version: 4.1.0 NVIDIA 260.19.21
OpenGL vendor: NVIDIA Corporation
OpenGL renderer: Quadro 6000/PCI/SSE2
OpenGL profile: Compatible
GLSL version: 4.10 NVIDIA via Cg compiler
Max texture size: 16384
Texture coords: 8
Texture conventional units: 4
Texture image units: 32
Anisotropic texture filter: YES, 16X
S3 Texture Compression: YES
Vertex Buffer Object: YES
Pixel Buffer Object: YES
Framebuffer Object: YES
Max vertex attributes: 16
Max varying floats: 60
Max fragment uniform components: 2048
Max vertex uniform components: 4096
Max elements vertices: 1048576
Max elements indices: 1048576

 --- OpenGL Extensions --- 
GL_ARB_blend_func_extended              GL_ARB_color_buffer_float
GL_ARB_compatibility                    GL_ARB_copy_buffer
GL_ARB_depth_buffer_float               GL_ARB_depth_clamp
GL_ARB_depth_texture                    GL_ARB_draw_buffers
GL_ARB_draw_buffers_blend               GL_ARB_draw_indirect
GL_ARB_draw_elements_base_vertex        GL_ARB_draw_instanced
GL_ARB_ES2_compatibility                GL_ARB_explicit_attrib_location
GL_ARB_fragment_coord_conventions       GL_ARB_fragment_program
GL_ARB_fragment_program_shadow          GL_ARB_fragment_shader
GL_ARB_framebuffer_object               GL_ARB_framebuffer_sRGB
GL_ARB_geometry_shader4                 GL_ARB_get_program_binary
GL_ARB_gpu_shader5                      GL_ARB_gpu_shader_fp64
GL_ARB_half_float_pixel                 GL_ARB_half_float_vertex
GL_ARB_imaging                          GL_ARB_instanced_arrays
GL_ARB_map_buffer_range                 GL_ARB_multisample
GL_ARB_multitexture                     GL_ARB_occlusion_query
GL_ARB_occlusion_query2                 GL_ARB_pixel_buffer_object
GL_ARB_point_parameters                 GL_ARB_point_sprite
GL_ARB_provoking_vertex                 GL_ARB_robustness
GL_ARB_sample_shading                   GL_ARB_sampler_objects
GL_ARB_seamless_cube_map                GL_ARB_separate_shader_objects
GL_ARB_shader_bit_encoding              GL_ARB_shader_objects
GL_ARB_shader_precision                 GL_ARB_shader_subroutine
GL_ARB_shading_language_100             GL_ARB_shadow
GL_ARB_sync                             GL_ARB_tessellation_shader
GL_ARB_texture_border_clamp             GL_ARB_texture_buffer_object
GL_ARB_texture_buffer_object_rgb32      GL_ARB_texture_compression
GL_ARB_texture_compression_bptc         GL_ARB_texture_compression_rgtc
GL_ARB_texture_cube_map                 GL_ARB_texture_cube_map_array
GL_ARB_texture_env_add                  GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar             GL_ARB_texture_env_dot3
GL_ARB_texture_float                    GL_ARB_texture_gather
GL_ARB_texture_mirrored_repeat          GL_ARB_texture_multisample
GL_ARB_texture_non_power_of_two         GL_ARB_texture_query_lod
GL_ARB_texture_rectangle                GL_ARB_texture_rg
GL_ARB_texture_rgb10_a2ui               GL_ARB_texture_swizzle
GL_ARB_timer_query                      GL_ARB_transform_feedback2
GL_ARB_transform_feedback3              GL_ARB_transpose_matrix
GL_ARB_uniform_buffer_object            GL_ARB_vertex_array_bgra
GL_ARB_vertex_array_object              GL_ARB_vertex_attrib_64bit
GL_ARB_vertex_buffer_object             GL_ARB_vertex_program
GL_ARB_vertex_shader                    GL_ARB_vertex_type_2_10_10_10_rev
GL_ARB_viewport_array                   GL_ARB_window_pos
GL_ATI_draw_buffers                     GL_ATI_texture_float
GL_ATI_texture_mirror_once              GL_S3_s3tc
GL_EXT_texture_env_add                  GL_EXT_abgr
GL_EXT_bgra                             GL_EXT_bindable_uniform
GL_EXT_blend_color                      GL_EXT_blend_equation_separate
GL_EXT_blend_func_separate              GL_EXT_blend_minmax
GL_EXT_blend_subtract                   GL_EXT_compiled_vertex_array
GL_EXT_Cg_shader                        GL_EXT_depth_bounds_test
GL_EXT_direct_state_access              GL_EXT_draw_buffers2
GL_EXT_draw_instanced                   GL_EXT_draw_range_elements
GL_EXT_fog_coord                        GL_EXT_framebuffer_blit
GL_EXT_framebuffer_multisample          GL_EXTX_framebuffer_mixed_formats
GL_EXT_framebuffer_object               GL_EXT_framebuffer_sRGB
GL_EXT_geometry_shader4                 GL_EXT_gpu_program_parameters
GL_EXT_gpu_shader4                      GL_EXT_multi_draw_arrays
GL_EXT_packed_depth_stencil             GL_EXT_packed_float
GL_EXT_packed_pixels                    GL_EXT_pixel_buffer_object
GL_EXT_point_parameters                 GL_EXT_provoking_vertex
GL_EXT_rescale_normal                   GL_EXT_secondary_color
GL_EXT_separate_shader_objects          GL_EXT_separate_specular_color
GL_EXT_shader_image_load_store          GL_EXT_shadow_funcs
GL_EXT_stencil_two_side                 GL_EXT_stencil_wrap
GL_EXT_texture3D                        GL_EXT_texture_array
GL_EXT_texture_buffer_object            GL_EXT_texture_compression_latc
GL_EXT_texture_compression_rgtc         GL_EXT_texture_compression_s3tc
GL_EXT_texture_cube_map                 GL_EXT_texture_edge_clamp
GL_EXT_texture_env_combine              GL_EXT_texture_env_dot3
GL_EXT_texture_filter_anisotropic       GL_EXT_texture_integer
GL_EXT_texture_lod                      GL_EXT_texture_lod_bias
GL_EXT_texture_mirror_clamp             GL_EXT_texture_object
GL_EXT_texture_shared_exponent          GL_EXT_texture_sRGB
GL_EXT_texture_swizzle                  GL_EXT_timer_query
GL_EXT_transform_feedback2              GL_EXT_vertex_array
GL_EXT_vertex_array_bgra                GL_EXT_vertex_attrib_64bit
GL_IBM_rasterpos_clip                   GL_IBM_texture_mirrored_repeat
GL_KTX_buffer_region                    GL_NV_blend_square
GL_NV_conditional_render                GL_NV_copy_depth_to_color
GL_NV_copy_image                        GL_NV_depth_buffer_float
GL_NV_depth_clamp                       GL_NV_explicit_multisample
GL_NV_fence                             GL_NV_float_buffer
GL_NV_fog_distance                      GL_NV_fragment_program
GL_NV_fragment_program_option           GL_NV_fragment_program2
GL_NV_framebuffer_multisample_coverage  GL_NV_geometry_shader4
GL_NV_gpu_program4                      GL_NV_gpu_program4_1
GL_NV_gpu_program5                      GL_NV_gpu_program_fp64
GL_NV_gpu_shader5                       GL_NV_half_float
GL_NV_light_max_exponent                GL_NV_multisample_coverage
GL_NV_multisample_filter_hint           GL_NV_occlusion_query
GL_NV_packed_depth_stencil              GL_NV_parameter_buffer_object
GL_NV_parameter_buffer_object2          GL_NV_pixel_data_range
GL_NV_point_sprite                      GL_NV_primitive_restart
GL_NV_register_combiners                GL_NV_register_combiners2
GL_NV_shader_buffer_load                GL_NV_texgen_reflection
GL_NV_texture_barrier                   GL_NV_texture_compression_vtc
GL_NV_texture_env_combine4              GL_NV_texture_expand_normal
GL_NV_texture_multisample               GL_NV_texture_rectangle
GL_NV_texture_shader                    GL_NV_texture_shader2
GL_NV_texture_shader3                   GL_NV_transform_feedback
GL_NV_transform_feedback2               GL_NV_vdpau_interop
GL_NV_vertex_array_range                GL_NV_vertex_array_range2
GL_NV_vertex_attrib_integer_64bit       GL_NV_vertex_buffer_unified_memory
GL_NV_vertex_program                    GL_NV_vertex_program1_1
GL_NV_vertex_program2                   GL_NV_vertex_program2_option
GL_NV_vertex_program3                   GL_NV_video_capture
GL_NVX_conditional_render               GL_NVX_gpu_memory_info
GL_SGIS_generate_mipmap                 GL_SGIS_texture_lod
GL_SGIX_depth_texture                   GL_SGIX_shadow
GL_SUN_slice_accum

 --- GLEW ---
GLEW version: 1.5.7

 --- OpenGL Info ---
OpenGL version: 4.1.0 NVIDIA 260.19.21
OpenGL vendor: NVIDIA Corporation
OpenGL renderer: Quadro 6000/PCI/SSE2
OpenGL profile: Compatible
GLSL version: 4.10 NVIDIA via Cg compiler
Max texture size: 16384
Texture coords: 8
Texture conventional units: 4
Texture image units: 32
Anisotropic texture filter: YES, 16X
S3 Texture Compression: YES
Vertex Buffer Object: YES
Pixel Buffer Object: YES
Framebuffer Object: YES
Max vertex attributes: 16
Max varying floats: 60
Max fragment uniform components: 2048
Max vertex uniform components: 4096
Max elements vertices: 1048576
Max elements indices: 1048576

 --- OpenGL Extensions --- 
GL_ARB_blend_func_extended              GL_ARB_color_buffer_float
GL_ARB_compatibility                    GL_ARB_copy_buffer
GL_ARB_depth_buffer_float               GL_ARB_depth_clamp
GL_ARB_depth_texture                    GL_ARB_draw_buffers
GL_ARB_draw_buffers_blend               GL_ARB_draw_indirect
GL_ARB_draw_elements_base_vertex        GL_ARB_draw_instanced
GL_ARB_ES2_compatibility                GL_ARB_explicit_attrib_location
GL_ARB_fragment_coord_conventions       GL_ARB_fragment_program
GL_ARB_fragment_program_shadow          GL_ARB_fragment_shader
GL_ARB_framebuffer_object               GL_ARB_framebuffer_sRGB
GL_ARB_geometry_shader4                 GL_ARB_get_program_binary
GL_ARB_gpu_shader5                      GL_ARB_gpu_shader_fp64
GL_ARB_half_float_pixel                 GL_ARB_half_float_vertex
GL_ARB_imaging                          GL_ARB_instanced_arrays
GL_ARB_map_buffer_range                 GL_ARB_multisample
GL_ARB_multitexture                     GL_ARB_occlusion_query
GL_ARB_occlusion_query2                 GL_ARB_pixel_buffer_object
GL_ARB_point_parameters                 GL_ARB_point_sprite
GL_ARB_provoking_vertex                 GL_ARB_robustness
GL_ARB_sample_shading                   GL_ARB_sampler_objects
GL_ARB_seamless_cube_map                GL_ARB_separate_shader_objects
GL_ARB_shader_bit_encoding              GL_ARB_shader_objects
GL_ARB_shader_precision                 GL_ARB_shader_subroutine
GL_ARB_shading_language_100             GL_ARB_shadow
GL_ARB_sync                             GL_ARB_tessellation_shader
GL_ARB_texture_border_clamp             GL_ARB_texture_buffer_object
GL_ARB_texture_buffer_object_rgb32      GL_ARB_texture_compression
GL_ARB_texture_compression_bptc         GL_ARB_texture_compression_rgtc
GL_ARB_texture_cube_map                 GL_ARB_texture_cube_map_array
GL_ARB_texture_env_add                  GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar             GL_ARB_texture_env_dot3
GL_ARB_texture_float                    GL_ARB_texture_gather
GL_ARB_texture_mirrored_repeat          GL_ARB_texture_multisample
GL_ARB_texture_non_power_of_two         GL_ARB_texture_query_lod
GL_ARB_texture_rectangle                GL_ARB_texture_rg
GL_ARB_texture_rgb10_a2ui               GL_ARB_texture_swizzle
GL_ARB_timer_query                      GL_ARB_transform_feedback2
GL_ARB_transform_feedback3              GL_ARB_transpose_matrix
GL_ARB_uniform_buffer_object            GL_ARB_vertex_array_bgra
GL_ARB_vertex_array_object              GL_ARB_vertex_attrib_64bit
GL_ARB_vertex_buffer_object             GL_ARB_vertex_program
GL_ARB_vertex_shader                    GL_ARB_vertex_type_2_10_10_10_rev
GL_ARB_viewport_array                   GL_ARB_window_pos
GL_ATI_draw_buffers                     GL_ATI_texture_float
GL_ATI_texture_mirror_once              GL_S3_s3tc
GL_EXT_texture_env_add                  GL_EXT_abgr
GL_EXT_bgra                             GL_EXT_bindable_uniform
GL_EXT_blend_color                      GL_EXT_blend_equation_separate
GL_EXT_blend_func_separate              GL_EXT_blend_minmax
GL_EXT_blend_subtract                   GL_EXT_compiled_vertex_array
GL_EXT_Cg_shader                        GL_EXT_depth_bounds_test
GL_EXT_direct_state_access              GL_EXT_draw_buffers2
GL_EXT_draw_instanced                   GL_EXT_draw_range_elements
GL_EXT_fog_coord                        GL_EXT_framebuffer_blit
GL_EXT_framebuffer_multisample          GL_EXTX_framebuffer_mixed_formats
GL_EXT_framebuffer_object               GL_EXT_framebuffer_sRGB
GL_EXT_geometry_shader4                 GL_EXT_gpu_program_parameters
GL_EXT_gpu_shader4                      GL_EXT_multi_draw_arrays
GL_EXT_packed_depth_stencil             GL_EXT_packed_float
GL_EXT_packed_pixels                    GL_EXT_pixel_buffer_object
GL_EXT_point_parameters                 GL_EXT_provoking_vertex
GL_EXT_rescale_normal                   GL_EXT_secondary_color
GL_EXT_separate_shader_objects          GL_EXT_separate_specular_color
GL_EXT_shader_image_load_store          GL_EXT_shadow_funcs
GL_EXT_stencil_two_side                 GL_EXT_stencil_wrap
GL_EXT_texture3D                        GL_EXT_texture_array
GL_EXT_texture_buffer_object            GL_EXT_texture_compression_latc
GL_EXT_texture_compression_rgtc         GL_EXT_texture_compression_s3tc
GL_EXT_texture_cube_map                 GL_EXT_texture_edge_clamp
GL_EXT_texture_env_combine              GL_EXT_texture_env_dot3
GL_EXT_texture_filter_anisotropic       GL_EXT_texture_integer
GL_EXT_texture_lod                      GL_EXT_texture_lod_bias
GL_EXT_texture_mirror_clamp             GL_EXT_texture_object
GL_EXT_texture_shared_exponent          GL_EXT_texture_sRGB
GL_EXT_texture_swizzle                  GL_EXT_timer_query
GL_EXT_transform_feedback2              GL_EXT_vertex_array
GL_EXT_vertex_array_bgra                GL_EXT_vertex_attrib_64bit
GL_IBM_rasterpos_clip                   GL_IBM_texture_mirrored_repeat
GL_KTX_buffer_region                    GL_NV_blend_square
GL_NV_conditional_render                GL_NV_copy_depth_to_color
GL_NV_copy_image                        GL_NV_depth_buffer_float
GL_NV_depth_clamp                       GL_NV_explicit_multisample
GL_NV_fence                             GL_NV_float_buffer
GL_NV_fog_distance                      GL_NV_fragment_program
GL_NV_fragment_program_option           GL_NV_fragment_program2
GL_NV_framebuffer_multisample_coverage  GL_NV_geometry_shader4
GL_NV_gpu_program4                      GL_NV_gpu_program4_1
GL_NV_gpu_program5                      GL_NV_gpu_program_fp64
GL_NV_gpu_shader5                       GL_NV_half_float
GL_NV_light_max_exponent                GL_NV_multisample_coverage
GL_NV_multisample_filter_hint           GL_NV_occlusion_query
GL_NV_packed_depth_stencil              GL_NV_parameter_buffer_object
GL_NV_parameter_buffer_object2          GL_NV_pixel_data_range
GL_NV_point_sprite                      GL_NV_primitive_restart
GL_NV_register_combiners                GL_NV_register_combiners2
GL_NV_shader_buffer_load                GL_NV_texgen_reflection
GL_NV_texture_barrier                   GL_NV_texture_compression_vtc
GL_NV_texture_env_combine4              GL_NV_texture_expand_normal
GL_NV_texture_multisample               GL_NV_texture_rectangle
GL_NV_texture_shader                    GL_NV_texture_shader2
GL_NV_texture_shader3                   GL_NV_transform_feedback
GL_NV_transform_feedback2               GL_NV_vdpau_interop
GL_NV_vertex_array_range                GL_NV_vertex_array_range2
GL_NV_vertex_attrib_integer_64bit       GL_NV_vertex_buffer_unified_memory
GL_NV_vertex_program                    GL_NV_vertex_program1_1
GL_NV_vertex_program2                   GL_NV_vertex_program2_option
GL_NV_vertex_program3                   GL_NV_video_capture
GL_NVX_conditional_render               GL_NVX_gpu_memory_info
GL_SGIS_generate_mipmap                 GL_SGIS_texture_lod
GL_SGIX_depth_texture                   GL_SGIX_shadow
GL_SUN_slice_accum

PatchesContainer::PatchesContainer: working dir: ./data/patches/pick_height_testing_png/
PatchesContainer::loadPatchesFromWorkingDir ignoring file = data/patches/pick_height_testing_png/.svn
Patch::initialize(): name   = data/patches/pick_height_testing_png/patch01-position=20x20-maxHeight=1.0.png
width  = 50
height = 50
depth  = 0
format = IF_LUMINANCE
type   = IT_UNSIGNED_BYTE
pitch  = 50
bytealign = 1
Patch::initialize(): name   = data/patches/pick_height_testing_png/patch02-position=20x2-maxHeight=1.5.png
width  = 20
height = 20
depth  = 0
format = IF_LUMINANCE
type   = IT_UNSIGNED_BYTE
pitch  = 20
bytealign = 1
Found matching densitymap ./data/density_maps/pick_height_testing_png.png
loading height field image
name   = ./data/horizons/pick_height_testing.png
width  = 200
height = 200
depth  = 0
format = IF_LUMINANCE
type   = IT_UNSIGNED_BYTE
pitch  = 200
bytealign = 1
name   = ./data/textures/tesselation_palette_blue_red.png
width  = 300
height = 0
depth  = 0
format = IF_RGB
type   = IT_UNSIGNED_BYTE
pitch  = 900
bytealign = 1
GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS: 32

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2024-11-25 01:43:48

好吧,我可以看到一个问题:

统一sampler2D[20]补丁;

我大胆猜测您的 OpenGL 4.0 级硬件无法在单个着色器阶段使用超过 16 个纹理。如果您想验证这一点,请检查GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS;我敢打赌它是 16。

此外,访问采样器数组的规则非常严格(我敢打赌您正在破坏它们)。在 GLSL 4.00 中,采样器数组的索引必须是:

1:编译时常量表达式

2:解析为统一值的表达式。不是关键字中的uniform,而是基于编译时常量或uniform值的表达式。它们不能基于从纹理、着色器阶段输入或任何类似内容检索的值。

这是合法的:

for(int i = 0; i < someUniform; i++)
{
    texture(Patches[i], texCoord);
}

这是合法的:

texture(Patches[positionOfSomeVertex * uniformScaleFactor], texCoord);

人们使用数组纹理而不是采样器数组是有原因的。


附录:也许引用 GLSL 版本 4.10 规范会有所帮助:

片段着色器表达式是
如果所有片段动态均匀
评估它得到相同的结果
价值。当涉及循环时,这
指的是表达式的值
相同的循环迭代。什么时候
涉及到函数,this指的是
来自同一呼叫点的呼叫。

这对于其他的也有类似的定义
着色器阶段,基于
他们处理的每个实例的数据。

请注意,常量表达式是
微不足道的动态均匀。它
遵循典型的循环计数器
基于这些也是动态的
制服。

您的表达式涉及基于非动态统一的条件分支。因此,所使用的索引不是动态统一的。

“动态均匀”的基本思想是:给定相同的统一值,我应该能够将任何东西作为着色器阶段输入传递,并且可以从以下位置访问相同的纹理:数组。您的代码不能保证这一点。

Well, I can see one problem:

uniform sampler2D[20] Patches;

I'm going to go out on a limb and guess that your OpenGL 4.0-class hardware is incapable of using more than 16 textures within a single shader stage. If you want to verify this, check GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS; I'd bet it's 16.

Also, the rules for accessing sampler arrays are very strict (and I'm betting you're breaking them). The indices for sampler arrays must, in GLSL 4.00, be either:

1: Compile-time constant expressions

2: Expressions that resolve to uniform values. Not uniform as in the keyword, but expressions that are based on compile-time constants or uniform values. They cannot be based on values retrieved from textures, from shader-stage inputs, or anything of the kind.

This is legal:

for(int i = 0; i < someUniform; i++)
{
    texture(Patches[i], texCoord);
}

This is not legal:

texture(Patches[positionOfSomeVertex * uniformScaleFactor], texCoord);

There's a reason why people use array textures instead of sampler arrays.


Adendum: Perhaps a quote from the GLSL version 4.10 specification would help:

A fragment-shader expression is
dynamically uniform if all fragments
evaluating it get the same resulting
value. When loops are involved, this
refers to the expression's value for
the same loop iteration. When
functions are involved, this refers to
calls from the same call point.

This is similarly defined for other
shader stages, based on the
per-instance data they process.

Note that constant expressions are
trivially dynamically uniform. It
follows that typical loop counters
based on these are also dynamically
uniform.

Your expression involves a conditional branch based on something that is not dynamically uniform. Therefore, the index used is not dynamically uniform.

The basic idea with "dynamically uniform" is this: given the same uniform values, I should be able to pass anything as shader stage inputs and the same texture will be accessed from the array. Your code does not guarantee this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文