繁体   English   中英

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

[英]How would I set sampler2D uniforms in OpenGL in a SPIR-V Shader?

I am compiling this shader with glslangValidator, and am using OpenGL 4.3 Core Profile with the extension GL_ARB_gl_spirv, and using the GLAD webservice to generate function pointers to access the Core OpenGL API, and SDL2 to create the context, if that is of any use.

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

#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);
}

我将如何设置inTexture使用的纹理单元?

从我通过文档在线阅读的内容来看,我无法使用glGetUniformLocation来获取要在glUniform1i中使用的制服的位置,因为我使用的是 SPIR-V,而不是直接使用 GLSL。

我需要做什么才能以glUniform1i之类的方式设置它? 我需要在布局修改器中设置location吗? binding 我尝试使用统一缓冲区对象,但显然sampler2D只能是统一的。

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

layout(binding = #) uniform sampler2D inTexture;

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

如果你想动态修改绑定(你不应该),GLSL 4.30 提供了通过location layout限定符为任何非块uniform值设置位置的能力:

layout(location = #) uniform sampler2D inTexture;

这使得#制服的位置,您可以将其传递给任何glProgramUniform调用。

暂无
暂无

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

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