简体   繁体   English

同一GLSL着色器中的texture1D和Texture 2D

[英]texture1D and texture 2D in the same GLSL shader

I would like to pass two textures to my fragment shader. 我想将两个纹理传递给片段着色器。 I succeed with two 2D textures, but not with one 1D and one 2D. 我成功使用了两个2D纹理,但没有成功使用一个1D和一个2D。

Here is a piece of the fragment shader code: 这是片段着色器代码的一部分:

uniform sampler2D heights;
uniform sampler1D bboxes;
....
vec4 t2 = texture2D(heights, vec2(0.0, 0.0));
vec4 t1 = texture1D(bboxes, 0.0);

Here a piece of the main program code (notice the "print 'here'": 这是一段主程序代码(请注意“在这里打印”:

sh = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
shaders.glUseProgram(sh)
print 'here'

glEnable(GL_TEXTURE_2D)
glEnable(GL_TEXTURE_1D)

glActiveTexture(GL_TEXTURE0)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
t_h = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, t_h)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 800, 600, 0, GL_RGB, GL_FLOAT, pts)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)


glActiveTexture(GL_TEXTURE0 + 2)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
t_bb = glGenTextures(1)
glBindTexture(GL_TEXTURE_1D, t_bb)
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB32F, len(bboxes), 0, GL_RGB, GL_FLOAT, bboxes)
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

loc_h = glGetUniformLocation(sh, "heights")
loc_bb = glGetUniformLocation(sh, "bboxes")
glUniform1i(loc_h, 0)
glUniform1i(loc_bb, 1)

The error I have is: 我的错误是:

File "forth.py", line 282, in <module>
shader = setup()
File "forth.py", line 128, in setup
sh = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
File "/Library/Python/2.7/site-packages/PyOpenGL-3.0.2-py2.7.egg/OpenGL/GL/shaders.py", line 196, in compileProgram
program.check_validate()
File "/Library/Python/2.7/site-packages/PyOpenGL-3.0.2-py2.7.egg/OpenGL/GL/shaders.py", line 108, in check_validate
glGetProgramInfoLog( self ),
RuntimeError: Validation failure (0): Validation Failed: Sampler error:
Samplers of different types use the same texture image unit.
- or -
A sampler's texture unit is out of range (greater than max allowed or negative).

And the 'here' is not printed. 并且“此处”未打印。

Please, any idea ? 拜托,有什么想法吗?

First issue is you seem to be using texture unit 2 for the 1D texture: glActiveTexture(GL_TEXTURE0 + 2) 第一个问题是您似乎将纹理单元2用于一维纹理: glActiveTexture(GL_TEXTURE0 + 2)

But you're setting the uniform to 1 with glUniform1i(loc_bb, 1) 但是,您要使用glUniform1i(loc_bb, 1)将统一设置为1

Make them consistent. 使它们一致。

The second problem is that the PyOpenGL wrapper tries to validate the shader right after linking and that won't work because at that point you haven't configured the Uniforms yet and so there is a conflict of texture units. 第二个问题是PyOpenGL包装器尝试在链接后立即验证着色器,但该方法不起作用,因为此时您尚未配置Uniforms,因此存在纹理单元冲突。 And if your GLSL version doesn't support layout to set a default binding, it can't possibly validate. 而且,如果您的GLSL版本不支持设置默认绑定的layout ,则可能无法验证。

The only way I see around this is to create your own helper function to compile the program and skip the 'validate' step : 我看到的唯一方法是创建自己的帮助函数来编译程序并跳过“验证”步骤:

from OpenGL.GL.shaders import ShaderProgram

def myCompileProgram(*shaders):
  program = glCreateProgram()
  for shader in shaders:
    glAttachShader(program, shader)
  program = ShaderProgram( program )
  glLinkProgram(program)
  program.check_linked()
  for shader in shaders:
    glDeleteShader(shader)
  return program

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM