Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bindings don't get picked up properly #1718

Closed
Jasper-Bekkers opened this issue Aug 8, 2021 · 6 comments
Closed

Bindings don't get picked up properly #1718

Jasper-Bekkers opened this issue Aug 8, 2021 · 6 comments
Labels
not a bug Not a bug, nothing to do.

Comments

@Jasper-Bekkers
Copy link
Contributor

This is a shader I'm trying to round-trip through DXC & spirv-cross as an experiment (ultimately DXC will be replaced by rust-gpu instead).

struct RenderResourceHandle {
    uint handle;

    uint readIndex() { return this.handle & ((1 << 23) - 1); }
    bool isValid() { return this.handle != ~0; }

    uint writeIndex() {
        uint uav_idx = this.handle + 1;
        return uav_idx & ((1 << 23) - 1);
    }
};

struct BindingsOffset {
    RenderResourceHandle bindingsOffset;
    uint userData0;
    uint userData1;
    uint userData2;
};

ConstantBuffer<BindingsOffset> g_bindingsOffset : register(b0, space10);
ByteAddressBuffer g_byteAddressBuffer[] : register(t0, space3);
RWByteAddressBuffer g_rwByteAddressBuffer[] : register(u0, space4);

#define byteBuffer(handle)                                                     \
    g_byteAddressBuffer[NonUniformResourceIndex(handle.readIndex())]
#define rwByteBuffer(handle) g_rwByteAddressBuffer[handle.writeIndex()]

struct Bindings {
    RenderResourceHandle input;
    RenderResourceHandle output;
};

[numthreads(64, 1, 1)]
void main(int threadId: SV_DispatchThreadID)
{
    Bindings bnd = byteBuffer(g_bindingsOffset.bindingsOffset).Load<Bindings>(0);

    rwByteBuffer(bnd.output).Store(threadId, byteBuffer(bnd.input).Load(threadId));
}

Invoked with:

dxc -Tcs_6_5 -spirv test.hlsl -spirv > test.spirv

I'd assemble the test.spirv as usual with spirv-as test.spirv

Which leads to the out.spv file (attached) - I feed that into spirv-cross like so:

out.zip

spirv-cross --dump-resources --hlsl --shader-model 65 out.spv

Which leads to the following hlsl output:

struct RenderResourceHandle
{
    uint handle;
};

static uint3 gl_GlobalInvocationID;
struct SPIRV_Cross_Input
{
    uint3 gl_GlobalInvocationID : SV_DispatchThreadID;
};

void comp_main()
{
    uint _11 = g_bindingsOffset.bindingsOffset.handle & 8388607u;
    uint _45 = uint(int3(gl_GlobalInvocationID).x) >> 2u;
    uint _16 = g_byteAddressBuffer[NonUniformResourceIndex(_11)].Load<uint>(0) & 8388607u;
    g_rwByteAddressBuffer[(g_byteAddressBuffer[NonUniformResourceIndex(_11)].Load<uint>(4) + 1u) & 8388607u].Store<uint>(_45 * 4 + 0, g_byteAddressBuffer[NonUniformResourceIndex(_16)].Load<uint>(_45 * 4 + 0));
}

[numthreads(64, 1, 1)]
void main(SPIRV_Cross_Input stage_input)
{
    gl_GlobalInvocationID = stage_input.gl_GlobalInvocationID;
    comp_main();
}

This fails to compile because all of the bindings are missing.

@Jasper-Bekkers
Copy link
Contributor Author

Jasper-Bekkers commented Aug 8, 2021

I've also noticed that the spirv-cross I have installed on my system (621884d) will output the bindings with --dump-bindings, but the latest one from bab4e59 (lastest master branch) that I built just now doesn't display them anymore. However, neither build emit them in the final HLSL.

@HansKristian-Work
Copy link
Contributor

Sorry for late reply, was on holiday.

@HansKristian-Work HansKristian-Work added the needs triage Needs to be reproduced before it can become a different issue type. label Aug 23, 2021
@HansKristian-Work
Copy link
Contributor

The SPIR-V is invalid. In SPIR-V 1.4 and later, global active variables (not just stage IO) must be part of the OpEntryPoint list, otherwise, they are filtered out.

@HansKristian-Work HansKristian-Work added not a bug Not a bug, nothing to do. and removed needs triage Needs to be reproduced before it can become a different issue type. labels Aug 23, 2021
@Jasper-Bekkers
Copy link
Contributor Author

@HansKristian-Work I'm not entirely sure what the problem is with the SPIR-V binary? It's not a 1.4 binary (DXC doesn't generate SPIRV 1.4 iirc)? Does spirv-cross only support the latest SPIR-V version?

@HansKristian-Work
Copy link
Contributor

@HansKristian-Work I'm not entirely sure what the problem is with the SPIR-V binary? It's not a 1.4 binary (DXC doesn't generate SPIRV 1.4 iirc)? Does spirv-cross only support the latest SPIR-V version?

The SPIR-V you linked is 1.5:

; SPIR-V
; Version: 1.5
; Generator: Khronos SPIR-V Tools Assembler; 0
; Bound: 48
; Schema: 0
               OpCapability Shader
               OpCapability RuntimeDescriptorArray
               OpCapability ShaderNonUniform
               OpExtension "SPV_EXT_descriptor_indexing"
               OpMemoryModel Logical GLSL450

SPIRV-Cross supports all SPIR-V versions, but the semantics in SPIR-V 1.4 and up changed.

@HansKristian-Work
Copy link
Contributor

I guess the problem is that spirv-as targets 1.5 by default, unless overridden with --target-env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug Not a bug, nothing to do.
Projects
None yet
Development

No branches or pull requests

2 participants