GLSL sampler2D in struct

后端 未结 1 1606
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 22:55

In GLSL there seems to be linking error of shaders when I try to pass a uniform struct with a sampler2D attribute to a function which is forward declared. The code works if

相关标签:
1条回答
  • 2021-01-05 23:42

    This actually is not supposed to work because you cannot instantiate a struct that contains an opaque data type (sampler, image, atomic counter). It is acceptable to have a struct with an opaque type as a uniform, but you cannot implement the function add (...) by passing an instance of Material.

    Data Type (GLSL) - Opaque Types

    Variables of opaque types can only be declared in one of two ways. They can be declared at global scope, as a uniform​ variables. Such variables can be arrays of the opaque type. They can be declared as members of a struct, but if so, then the struct can only be used to declare a uniform​ variable (or to declare a member of a struct/array that itself a uniform variable). They cannot be part of a buffer-backed interface block or an input/output variable, either directly or indirectly.


    This change to your code should work across all compliant implementations of OpenGL:

    // Must be in, because you cannot assign values to an opaque type.
    vec4 add (in sampler2D tex) {
      return vec4(texture(tex, texcoords));
    }
    

    This would also work, since it uses the sampler in the material.tex uniform:

    vec4 add (void) {
      return vec4(texture(material.tex, texcoords));
    }
    
    0 讨论(0)
提交回复
热议问题