这是indexloc提供的服务,不要输入任何密码
Skip to content

Vertex and Instance ID and base ID builtins. #901

@Kangz

Description

@Kangz

Spawned from this bug report. The availability of builtins to gets the index of the current instance and vertex in the vertex shader vary between APIs.

D3D12

  • SV_InstanceID: starts from 0, irrespective of the firstInstance in Draw / DrawIndexed
  • SV_VertexID: starts from 0, irrespective of the firstVertex in Draw and baseVertex in DrawIndexed

@RafaelCintron the docs aren't super clear, can you check for SV_VertexID? (edit: confirmed that it starts from 0)

Metal (section "5.2.3.1 Vertex Function Input Attributes" of the MSL 2.3 "spec")

  • [[base_instance]]: the firstInstance in DrawIndexed and Draw
  • [[base_vertex]]: the firstVertex in Draw and baseVertex in DrawIndexed
  • [[instance_id]]: starts from [[base_instance]]
  • [[vertex_id]]: starts from [[base_vertex]]

Vulkan

  • BaseInstance: the firstInstance in DrawIndexed and Draw, requires the VK_KHR_shader_draw_parameters extension / feature
  • BaseVertex: the firstVertex in Draw and baseVertex in DrawIndexed, requires the VK_KHR_shader_draw_parameters extension / feature
  • InstanceIndex: starts from [[base_instance]]
  • VertexIndex: starts from [[base_vertex]]

OpenGL

  • gl_VertexID: starts from baseVertex/firstVertex.
  • gl_InstanceID: starts from 0.
  • gl_BaseVertex / gl_BaseInstance: require GL_ARB_shader_draw_parameters or OpenGL 4.6

Conclusion

It's a mess and we're heavily limited by what D3D12 does, so we don't really have a choice. We should name builtins clearly to indicate if they start at 0 or not, to avoid the gl_InstanceID vs. gl_InstanceIndex confusion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgslWebGPU Shading Language Issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions