简体   繁体   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. 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.

If I have a fragment shader like so:如果我有这样的片段着色器:

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

How would I be able to set which texture unit inTexture uses?我将如何设置inTexture使用的纹理单元?

From what I have read online through documents, I cannot use glGetUniformLocation to get the location of this uniform to use in glUniform1i , because of the fact I am using SPIR-V, instead of GLSL directly.从我通过文档在线阅读的内容来看,我无法使用glGetUniformLocation来获取要在glUniform1i中使用的制服的位置,因为我使用的是 SPIR-V,而不是直接使用 GLSL。

What would I need to do to set it in a fashion like glUniform1i ?我需要做什么才能以glUniform1i之类的方式设置它? Do I need to set the location in the layout modifier?我需要在布局修改器中设置location吗? The binding ? binding I've tried to use Uniform Buffer Objects, but apparently sampler2D can only be a uniform.我尝试使用统一缓冲区对象,但显然sampler2D只能是统一的。

Since GLSL 4.20, you have the ability to set the binding point for any opaque type like samplers from GLSL code directly.从 GLSL 4.20 开始,您可以直接从 GLSL 代码中为任何不透明类型(例如采样器)设置绑定点。 This is done through the binding layout qualifier :这是通过binding layout限定符完成的:

layout(binding = #) uniform sampler2D inTexture;

The # here is whatever binding index you want to use.这里的#是您要使用的任何绑定索引。 This is the index you use when binding the texture: glActiveTexture(GL_TEXTURE0 + #) .这是绑定纹理时使用的索引: glActiveTexture(GL_TEXTURE0 + #) That is, you don't need to use glProgramUniform to set the uniform's value anymore;也就是说,您不再需要使用glProgramUniform来设置制服的值; you already set it in the shader.您已经在着色器中设置了它。

If you want to modify the binding dynamically (and you shouldn't ), GLSL 4.30 offers the ability to set the location for any non-block uniform value via the location layout qualifier :如果你想动态修改绑定(你不应该),GLSL 4.30 提供了通过location layout限定符为任何非块uniform值设置位置的能力:

layout(location = #) uniform sampler2D inTexture;

This makes # the uniform's location, which you can pass to any glProgramUniform call.这使得#制服的位置,您可以将其传递给任何glProgramUniform调用。

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

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