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

Throw Exception When Data Layout Will Encounter Alignment Differences With DXC '-fvk-use-dx-layout' #8

Open
galibzon opened this issue Sep 21, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@galibzon
Copy link
Contributor

galibzon commented Sep 21, 2021

AZSLc is used to transpile AZSL code into HLSL code, during this process AZSLc can generate reflection data along with size and offset information for all structs, classes and SRGs. AZSLc follows DX12 rules to calculate size and offset of the reflected data.

The idea is that eventually when compiling the HLSL code with DXC, the DXC-calculated sizes and offsets match those calculated by AZSLc. This is true when compiling HLSL code to DX12 byte code.

Natively the layout rules of SPIRV (Vulkan) are different than DX12, hence We request DXC to use DX12 layout rules when compiling for SPIRV with the option '-fvk-use-dx-layout'. The problem is that there are some cases where the expected layout don't match causing crashes or unexpected behavior at runtime.

The idea is to add a feature that checks for those error cases, treat them as errors and provide a message with a data padding solution. Additionally, provide a command line option '--no-alignment-validation' to avoid check for these kind of errors.

The most common error cases are:

  1. A 'float' or 'float2' variable preceded by matrices or structs that end in matrices of type:
    float2x2, float3x2, float4x2
    In such case the 'float' or 'float2' should be be pre-padded by a 'float3' variable.

  2. A 'float' type variable is preceded by a matrix or struct that ends in matrix of type:
    float2x3, float3x3, float4x3
    In such case the 'float' should be pre-padded by a 'float2' variables.

When AZSLc finds cases in the code that will have layout differences between dx12 and vulkan it will print this message:

tests\Emission\AsError\matRC_padding.azsl(,)
: IR error #131: Detected potential alignment issues related with DXC
flag '-fvk-use-dx-layout'.
Alternatively you can use the option '--no-alignment-validation' to void this error and compile as is:

A 'float3' variable should be added before the variable 'm_f3' in
'struct /A/SD2/m_f3' at Line number 21 of
'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
A 'float3' variable should be added before the variable 'm_f5' in
'struct /A/SD2/m_f5' at Line number 23 of
'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'

NOTE: Allegedly the most recent versions of DXC, in particular v1.6.2106+, fixes the issues when using '-fvk-use-dx-layout'.
This needs to be validated. See: microsoft/DirectXShaderCompiler#3945

@galibzon galibzon added the enhancement New feature or request label Sep 21, 2021
@galibzon galibzon self-assigned this Sep 21, 2021
galibzon pushed a commit to aws-lumberyard-dev/o3de-azslc that referenced this issue Sep 21, 2021
Throw Exception When Data Layout Will Encounter Alignment Differences
With DXC '-fvk-use-dx-layout'

This is v1.7.29

This is the exhaustive solution to validate the struct/class/SRG Layout data alignment problem
when:
1- There are float or float2 variables
preceded by matrices or structs that end in matrices of type:
float2x2, float3x2, float4x2
In such case the "float" or "float2" should be prepadded by a float3
variable and the solution is describbed in the error produced
by AZSLc.

2- The other cases are when "float" type variables
are preceded by matrices or structs that end in matrices of type:
float2x3, float3x3, float4x3 In such case the "float" must be prepadded by a float2 variables.
This solution is describbed in the error produced
by AZSLc.

Alternatively the user can skip this aligment error validation with
'--no-aligment-validation' command line option.

Error message example:
-------------------------------------------------------------
tests\Emission\AsError\matRC_padding.azsl(,)
: IR error #131: Detected potential alignment issues related with DXC
flag '-fvk-use-dx-layout':
- A 'float3' variable should be added before the variable 'm_f3' in
  'struct /A/SD2/m_f3' at Line number 21 of
  'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
- A 'float3' variable should be added before the variable 'm_f5' in
  'struct /A/SD2/m_f5' at Line number 23 of
    'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
-------------------------------------------------------------

Signed-off-by: garrieta <[email protected]>
galibzon added a commit that referenced this issue Sep 22, 2021
Throw Exception When Data Layout Will Encounter Alignment Differences
With DXC '-fvk-use-dx-layout'

This is v1.7.29

This is the exhaustive solution to validate the struct/class/SRG Layout data alignment problem
when:
1- There are float or float2 variables
preceded by matrices or structs that end in matrices of type:
float2x2, float3x2, float4x2
In such case the "float" or "float2" should be prepadded by a float3
variable and the solution is describbed in the error produced
by AZSLc.

2- The other cases are when "float" type variables
are preceded by matrices or structs that end in matrices of type:
float2x3, float3x3, float4x3 In such case the "float" must be prepadded by a float2 variables.
This solution is describbed in the error produced
by AZSLc.

Alternatively the user can skip this aligment error validation with
'--no-aligment-validation' command line option.

Error message example:
-------------------------------------------------------------
tests\Emission\AsError\matRC_padding.azsl(,)
: IR error #131: Detected potential alignment issues related with DXC
flag '-fvk-use-dx-layout'.
Alternatively you can use the option '--no-alignment-validation' to void this error and compile as is.:
- A 'float3' variable should be added before the variable 'm_f3' in
  'struct /A/SD2/m_f3' at Line number 21 of
  'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
- A 'float3' variable should be added before the variable 'm_f5' in
  'struct /A/SD2/m_f5' at Line number 23 of
    'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
-------------------------------------------------------------

Signed-off-by: garrieta <[email protected]>
galibzon added a commit to aws-lumberyard-dev/o3de-azslc that referenced this issue Oct 14, 2021
Throw Exception When Data Layout Will Encounter Alignment Differences
With DXC '-fvk-use-dx-layout'

This is v1.7.29

This is the exhaustive solution to validate the struct/class/SRG Layout data alignment problem
when:
1- There are float or float2 variables
preceded by matrices or structs that end in matrices of type:
float2x2, float3x2, float4x2
In such case the "float" or "float2" should be prepadded by a float3
variable and the solution is describbed in the error produced
by AZSLc.

2- The other cases are when "float" type variables
are preceded by matrices or structs that end in matrices of type:
float2x3, float3x3, float4x3 In such case the "float" must be prepadded by a float2 variables.
This solution is describbed in the error produced
by AZSLc.

Alternatively the user can skip this aligment error validation with
'--no-aligment-validation' command line option.

Error message example:
-------------------------------------------------------------
tests\Emission\AsError\matRC_padding.azsl(,)
: IR error #131: Detected potential alignment issues related with DXC
flag '-fvk-use-dx-layout'.
Alternatively you can use the option '--no-alignment-validation' to void this error and compile as is.:
- A 'float3' variable should be added before the variable 'm_f3' in
  'struct /A/SD2/m_f3' at Line number 21 of
  'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
- A 'float3' variable should be added before the variable 'm_f5' in
  'struct /A/SD2/m_f5' at Line number 23 of
    'D:\o3de-azslc\tests\Emission\AsError\matRC_padding.azsl'
-------------------------------------------------------------

Signed-off-by: garrieta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant