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

[SPIR-V] "-fvk-use-dx-layout" alignment error #3945

Closed
galibzon opened this issue Sep 13, 2021 · 4 comments
Closed

[SPIR-V] "-fvk-use-dx-layout" alignment error #3945

galibzon opened this issue Sep 13, 2021 · 4 comments
Labels
spirv Work related to SPIR-V

Comments

@galibzon
Copy link

galibzon commented Sep 13, 2021

For the attached hlsl files:
float2x2_case.txt
float2x3_case.txt
float3x2_case.txt
float3x3_case.txt
float4x2_case.txt
float4x3_case.txt

They compile as usual for dx12, but when compiling for vulkan using the following command:
dxc.exe -E MainPS -T ps_6_2 -O3 -Zpr -spirv -DAZ_USE_SUBPASSINPUT -fvk-use-dx-position-w -fvk-use-dx-layout -Fo my_vulkan.spirv.bin -Fh float2x2_case.txt

I get this error:
fatal error: generated SPIR-V is invalid: Structure id 5 decorated as Block for variable in Uniform storage class must follow scalar uniform buffer layout rules: member 1 at offset 24 overlaps previous member ending at offset 31 %type_ConstantBuffer_MyData = OpTypeStruct %mat2v2float %float %v4float

Similar errors are produced when using any of the attached floatRxC_case.txt files.

I tried both 'master' branch at commit 29a5af5
And tag 'v1.6.2106' at commit dad1cfc

Which apparently had the fix mentioned in this PR:
#3672

@galibzon galibzon changed the title "-fvk-use-dx-layout" still doesn't work "-fvk-use-dx-layout" alignment error Sep 13, 2021
@jaebaek jaebaek changed the title "-fvk-use-dx-layout" alignment error [SPIR-V] "-fvk-use-dx-layout" alignment error Sep 13, 2021
@jaebaek jaebaek added the spirv Work related to SPIR-V label Sep 13, 2021
@jaebaek
Copy link
Collaborator

jaebaek commented Sep 13, 2021

@galibzon Thank you for reporting this issue. We will take a look.

@jaebaek
Copy link
Collaborator

jaebaek commented Sep 20, 2021

Basically, when we use -fvk-use-dx-layout option, it means "please use the FXC (or DXC/DXIL) memory layout" regardless of the spirv-val validation error.

If you see what FXC (or DXC/DXIL) generates for its memory layout
http://shader-playground.timjones.io/fda1ca3706bed9f4957c6f5dc9a3c066

it is the following memory layout for float2x2_case.txt:

; cbuffer MyCB
; {
;
;   struct dx.alignment.legacy.MyCB
;   {
;
;       struct dx.alignment.legacy.struct.MyData
;       {
;
;           row_major float2x2 m_matrix22;            ; Offset:    0
;           float m_fAfter22;                         ; Offset:   24
;           float4 m_color;                           ; Offset:   32
;       
;       } MyCB;                                       ; Offset:    0
;
;   
;   } MyCB;                                           ; Offset:    0 Size:    48
;
; }

what SPIR-V backend generates is the same.

I used dxc.exe -E MainPS -T ps_6_2 -O3 -Zpr -spirv -DAZ_USE_SUBPASSINPUT -fvk-use-dx-position-w -fvk-use-dx-layout -Fc float2x2_case.spv float2x2_case.txt -Vd to disable the spirv-val validation error check.

See

               OpMemberDecorate %type_ConstantBuffer_MyData 0 Offset 0
               OpMemberDecorate %type_ConstantBuffer_MyData 0 MatrixStride 16
               OpMemberDecorate %type_ConstantBuffer_MyData 0 ColMajor
               OpMemberDecorate %type_ConstantBuffer_MyData 1 Offset 24
               OpMemberDecorate %type_ConstantBuffer_MyData 2 Offset 32

It can be illegal based on spirv-val's validation rule, but it exactly follows the memory layout rule of FXC or DXC/DXIL that is the expected behavior of -fvk-use-dx-layout.

This is not a bug, but it is actually correct.

@jeremyong-az
Copy link

Thanks for looking into this @jaebaek, I suppose the right thing to do is simply disable the spirv-validation in this case?

@jaebaek
Copy link
Collaborator

jaebaek commented Sep 21, 2021

Since it violates the SPIR-V validation rule, we should keep the behavior.
(users can use -fvk-use-dx-layout option to match the DX memory layout rule but we do not guarantee the result will be valid SPIR-V)

If you want to turn off validation errors, you can use -Vd option.
Note that -Vd will disable all types of spirv-val errors.

@jaebaek jaebaek closed this as completed Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spirv Work related to SPIR-V
Projects
None yet
Development

No branches or pull requests

3 participants