在几何着色器中将单个全局变量声明为 SSBO 会导致编译器错误

Declaring a single global variable as an SSBO in a geometry shader leads to a compiler error

read

a single global variable can be declared as an SSBO

并且我尝试在几何着色器 (#version 440) 中声明buffer vec2 name[];。编译失败,说明如下:

OpenGL does not allow declaring buffer variable 'name' in the global scope. Use buffer blocks instead.

那么,我错过了什么?

根据 GLSL 4.5 Specification (Section 4.3.7 Buffer Variables):

The buffer qualifier can be used to declare interface blocks (section 4.3.9 “Interface Blocks”), which are then referred to as shader storage blocks. It is a compile-time error to declare buffer variables at global scope (outside a block).

这似乎是 Wiki 中的错误,因为 GLSL 4.40 更新中的规范发生了变化。也就是GLSL 4.40原来的版本说:

The buffer qualifier can be used with any of the basic data types, or when declaring a variable whose type is a structure, or an array of any of these.

Buffer variables may only be declared inside interface blocks (section 4.3.9 “Interface Blocks”), which are then referred to as shader storage blocks. It is a compile-time error to declare buffer variables at global scope (outside a block). Buffer variables cannot have initializers.

显然,这是矛盾的。第一段建议声明裸缓冲区变量是可以的。第二段说缓冲区变量只能是buffer限定接口块的一部分。

对 4.40 的修订之一将其更改为:

The buffer qualifier can be used to declare interface blocks (section 4.3.9 “Interface Blocks”), which are then referred to as shader storage blocks. It is a compile-time error to declare buffer variables at global scope (outside a block).

很明显,这曾经是真的。 ARB_shader_storage_buffer_object 规范仍然使用旧的措辞。 Wiki 只需要根据当前行为进行更新。