如何在 SPIR-V 着色器中的 OpenGL 中设置 sampler2D 制服?

Kap*_*age 3 opengl spir-v

我正在使用 glslangValidator 编译此着色器,并使用带有扩展名 GL_ARB_gl_spirv 的 OpenGL 4.3 Core Profile,并使用 GLAD Webservice 生成函数指针以访问 Core OpenGL API,并使用 SDL2 创建上下文(如果有任何用处)。

如果我有一个像这样的片段着色器:

#version 430 core

//output Fragment Color
layout (location = 0) out vec4 outFragColor;

//Texture coordinates passed from the vertex shader
layout (location = 0) in vec2 inTexCoord;

uniform sampler2D inTexture;

void main()
{
    outFragColor = texture(inTexture, inTexCoord);
}
Run Code Online (Sandbox Code Playgroud)

我如何才能设置inTexture使用哪个纹理单元?

根据我在网上阅读的文档,我无法使用glGetUniformLocation来获取要在 中使用的制服的位置glUniform1i,因为我使用的是 SPIR-V,而不是直接使用 GLSL。

我需要做什么才能将其设置为类似的方式glUniform1ilocation我需要在布局修饰符中设置吗?这binding?我尝试过使用统一缓冲区对象,但显然sampler2D只能是统一的。

Nic*_*las 5

从 GLSL 4.20 开始,您可以直接从 GLSL 代码为任何不透明类型(例如采样器)设置绑定点。这是通过限定符完成binding layout的:

layout(binding = #) uniform sampler2D inTexture;
Run Code Online (Sandbox Code Playgroud)

这里#是您想要使用的任何绑定索引。这是绑定纹理时使用的索引:glActiveTexture(GL_TEXTURE0 + #)。也就是说,你不再需要使用glProgramUniform来设置uniform的值了;您已经在着色器中设置了它。

如果您想动态修改绑定(但您不应该这样做uniform),GLSL 4.30 提供了通过限定符设置任何非块值位置location layout功能

layout(location = #) uniform sampler2D inTexture;
Run Code Online (Sandbox Code Playgroud)

这将确定#制服的位置,您可以将其传递给任何glProgramUniform呼叫。