5

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

如果我使用代码gl_VertexID工作,如果我使用gl_VertexIndex代码工作,但如果我使用OpenGLspirvvulkangl_VertexIndex工作,gl_VertexIndex总是0.

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

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

不应该gl_VertexIndex0to5吗?

4

2 回答 2

8

无论您使用哪种着色语言gl_VertexIndex,OpenGL 中都没有。在将 GLSL 编译成 SPIR-V for OpenGL 时,你应该得到一个编译错误。

gl_VertexIndex话虽如此, and的值gl_VertexID(与gl_InstanceIndexvs.不同)之间没有区别gl_InstanceID

于 2019-08-29T13:23:11.707 回答
0

如果使用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

于 2021-11-27T23:14:29.803 回答