在 OpenGL 中使用 spirv,gl_VertexIndex 始终为 0

Using spirv with OpenGL, gl_VertexIndex is always 0

我正在尝试将 spirv 与 OpenGL 一起使用。之前在我的着色器中我有 gl_VertexID 来计算矩形的 uv,现在我用 gl_VertexIndex.

替换它

如果我使用 gl_VertexID 代码有效,如果我在 spirv 中使用 gl_VertexIndexvulkan 代码有效,但如果我使用 gl_VertexIndex使用 OpenGL gl_VertexIndex 总是 0.

这是我正在使用的测试绘图命令:

glDrawArraysInstancedBaseInstance(GL_TRIANGLES, 0, 6, 1, 2);

gl_VertexIndex 不应该从 0 变成 5 吗?

OpenGL 中没有gl_VertexIndex无论 您使用何种着色语言。将GLSL编译成SPIR-V for OpenGL时,应该会出现编译错误。

也就是说,gl_VertexIndexgl_VertexID 的值之间没有区别(不同于 gl_InstanceIndexgl_InstanceID)。

如果使用 vulkan 1.2

命令行:

$ ./glslangValidator.exe my_input.vert.glsl -S vert --target-env vulkan1.2 -o my_output.vert.spv

my_input.vert.glsl

#version 450

//: -------------------------------------------------------- ://
//: How to compile:                                          ://
//: $ ./glslangValidator.exe MCV.vert.glsl                   ://
//:         -S vert                                          ://
//:         --target-env vulkan1.2                           ://
//:         -o mcv.vert.spv                                  ://
//:                                                          ://
//: Formatted for readability. Command should all be on      ://
//: one line when given to console.                          ://
//: -------------------------------------------------------- ://
//:                                                          ://
//: Got Help From Vulkan Discord:                            ://
//:                                                          ://
//: Rodrigo#1643 :                                           ://
//:                                                          ://
//: gl_VertexIndex is gl_VertexID                            ://
//: gl_BaseVertex in other words,gl_VertexID                 ://
//: does not include the base vertex,which was               ://
//: confusing for some people,specially because              ://
//: D3D always included the base vertex                      ://
//:                                                          ://
//:                                                          ://
//:                                                          ://
//: Implodee#0345 :                                          ://
//:                                                          ://
//: In Vulkan dialect glsl there's no gl_VertexID            ://
//: and gl_InstanceID they are replaced by                   ://
//: gl_VertexIndex and gl_InstanceIndex as seen here         ://
//: SC[ IMPLODE_GL_VERTEX_INDEX_SEEN_HERE_PDF ]              ://
//: SC[ IMPLODE_GL_VERTEX_INDEX_SEEN_HERE_TXT ]              ://
//: https://raw.githubusercontent.com/KhronosGroup/GLSL      ://
//:       /master/extensions/khr/GL_KHR_vulkan_glsl.txt      ://
//:                                                          ://
//:                                                          ://
//: Me :                                                     ://
//:                                                          ://
//: $ ./glslangValidator MCV.vert.glsl -S vert               ://
//: MCV.vert.glsl                                            ://
//: ERROR: 0:45: 'gl_InstanceIndex' : undeclared identifier  ://
//: ERROR: 0:45: '[]' : scalar integer expression required   ://
//: ERROR: 0:45: '' : compilation terminated                 ://
//: ERROR: 3 compilation errors.  No code generated.         ://
//:                                                          ://
//:                                                          ://
//: Implodee#0345 :                                          ://
//:                                                          ://
//: call it with --target-env vulkan1.2                      ://
//: (or your used vulkan version rather)                     ://
//: see if that works                                        ://
//:                                                          ://
//:                                                          ://
//: #define V_I ( gl_VertexID    + gl_BaseVertex )    //:WRONG 
//: #define V_I ( gl_VertexIndex + gl_InstanceIndex ) //:1.2
//: -------------------------------------------------------- ://

#define V_I gl_VertexIndex
 
layout( location = 0 ) out vec3 fragColor ;

vec2 positions[3] = vec2[](
    vec2( 0.0 , -0.5 )
,   vec2( 0.5 ,  0.5 )
,   vec2(-0.5 ,  0.5 )
);

vec3 colors[3] = vec3[](
    vec3( 1.0 , 0.0 , 0.0 )
,   vec3( 0.0 , 1.0 , 0.0 )
,   vec3( 0.0 , 0.0 , 1.0 )
);

void main(){
    gl_Position = vec4( 
        positions[ V_I ]   //: XY ://
    ,   0.0                //: Z  ://
    ,   1.0                //: W  ://
    );;
    fragColor = colors[ V_I ];
}

#undef  V_I

原始源代码来自: https://vulkan-tutorial.com/Drawing_a_triangle/Graphics_pipeline_basics/Shader_modules