Skip to content

[SPIRV] Missing gl_NumWorkGroups #4217

Closed

Description

@Eichenherz

I'm currently using this to atomically count the workgroups as such:

if( ( gl_LocalInvocationID.x == 0 ) && ( workgrAtomicCounterShared == gl_NumWorkGroups.x - 1 ) )
//...... DO some work on the final value of a counter

In SPIRV gl_NumWorkGroups would be:

OpDecorate %gl_NumWorkGroups BuiltIn NumWorkgroups

How do I access the same BuiltIn in HLSL as it's not exposed through any SV ?

Activity

  1. pow2clk commented on Feb 7, 2022

    @pow2clk
    Collaborator

    Hi Eichenherz!

    There is not currently an HLSL equivalent to g_NumWorkGroups. I'm inferring from the information provided that you'd like this to target SPIRV. As such, I'll leave it to the SPIRV folks Jaebaek Seo (@jaebaek) and Natalie Chouinard (@sudonatalie) if they plan to do something here.

    In the meantime, you might consider passing the information through a constant buffer (UBO in GL speak) good luck!

  2. added
    spirv
    on Feb 7, 2022
  3. changed the title [-]Missing gl_NumWorkGroups[/-] [+][SPIRV] Missing gl_NumWorkGroups[/+] on Feb 7, 2022
  4. Eichenherz commented on Feb 8, 2022

    @Eichenherz
    Author

    Yes, precisely for HLSL to SPIRV. I did try the vk::ext_decorate(24) as that's the NumWorkgroups ID. But it would not produce any "OpDecorate Builtin NumWorkgroups".

  5. jaebaek commented on Feb 8, 2022

    @jaebaek
    Collaborator

    Sorry for being late. We will take a look. Definitely, we have to consider adding some new intrinsics. We currently experience intense workloads. It can be delayed. Sorry about that in advance.

  6. sudonatalie commented on Apr 29, 2022

    @sudonatalie
    Collaborator

    [[vk::ext_decorate(/* BuiltIn */ 11, /* NumWorkGroups */ 24)]] does correctly generate the desired OpDecorate %var BuiltIn NumWorkgroups, but the problem is getting %var to be the correct type with our current implementation. If you decorate a global, then it gets wrapped in the Globals struct, and the decoration is incorrectly applied to the wrapper. If an entry point input variable is decorated, then it needs a semantic string. I think this issue should be handled along with #4195, but I'll leave it to Jaebaek Seo (@jaebaek) to determine if it's a duplicate before closing.

  7. jaebaek commented on May 3, 2022

    @jaebaek
    Collaborator

    I think we can define a variable like the following example:

    RWStructuredBuffer<int> A;
    
    [numthreads(1,1,1)]
    void main() {
      [[vk::ext_storage_class(1)]]
      [[vk::ext_decorate(11, 24)]]
      uint3 numWorkgroups;
    
      A[0] = numWorkgroups.x;
    }
    

    However, the validation layer report an error

    fatal error: generated SPIR-V is invalid: Interface variable id <2> is used by entry point 'main' id <1>, but is not listed as an interface
      %numWorkgroups = OpVariable %_ptr_Input_v3uint Input
    

    We have to check the variable storage class in this line and add it as an operand of the entry point if it is an Input storage class. Let me keep this issue open.

  8. forbiddencactus commented on Mar 17, 2023

    @forbiddencactus

    Jaebaek Seo (@jaebaek)

    Do you have any update on this issue? I'm keen to take advantage of the code snipped you posted above but I'm running into the same is not listed as an interface issue.

  9. s-perron commented on Jun 2, 2023

    @s-perron
    Collaborator

    This is going to be a bigger issues to fix. I don't want to just do another hack with the existing inline spir-v. I'm going to look into revamping the whole feature.

  10. s-perron commented on Jun 13, 2023

    @s-perron
    Collaborator

    I opened microsoft/hlsl-specs#59. This should add a new attribute that can be used to properly define builtin input and output.

  11. kpentaris commented on Jun 30, 2023

    @kpentaris

    Also looking for a similar fix regarding access to Compute Shader globals (gl_GlobalInvocationID, gl_LocalInvocationIndex etc). Please note that these need to be globally accessible from a shader file that is #included and has no access to any entry point!

    #include "utils.hlsl"
    // Contains the following
    void initializeArr() {
      [[vk::ext_storage_class(1)]]
      [[vk::ext_decorate(11, 28)]]
      uint3 gl_GlobalInvocationID;
      arr[gl_GlobalInvocationID.x] = 0;
    }
    // end of utils.hlsl
    
    void main() {
      initializeArr();
    }
    

    In complex shader implementations it's really cumbersome to pass around the gl_ globals, it'd be best to have access to them as we do in GLSL.

    Thanks in advance!

  12. s-perron commented on Jun 30, 2023

    @s-perron
    Collaborator

    pentakon (@kpentaris) That is the current plan in microsoft/hlsl-specs#59.

  13. devshgraphicsprogramming commented on Jul 31, 2023

    @devshgraphicsprogramming

    pentakon (@kpentaris) That is the current plan in microsoft/hlsl-specs#59.

    and when will it land in DXC?

  14. s-perron commented on Aug 8, 2023

    @s-perron
    Collaborator

    No idea when it will land.

  15. OlesenJonas commented on Dec 18, 2023

    @OlesenJonas

    It seems possible to override an existing input as a workaround. Eg: im currently (ab)using SV_GroupIndex to get the SubgroupID:
    [[vk::ext_decorate(11/*BuiltIn*/, 40/*SubgroupId*/)]] int waveIndex : SV_GroupIndex

  16. added a commit that references this issue on Jan 2, 2024
    9dacbe3
  17. devshgraphicsprogramming commented on Jan 2, 2024

    @devshgraphicsprogramming

    @kptentaris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

spirv

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions