Skip to content

Emit Offset decoration for PhysicalStorage structs - #5388

Closed
Nathan Gauër (Keenuts) wants to merge 7 commits into
microsoft:mainfrom
Keenuts:fix-5327-lowering
Closed

Nathan Gauër (Keenuts) wants to merge 7 commits into
microsoft:mainfrom
Keenuts:fix-5327-lowering

Conversation

@Keenuts

Copy link
Copy Markdown
Collaborator

Depending on the struct layout, the Offset decoration is not always required.
When this storage class is setup, the decoration needs to be added.

The tricky part if the mix of AST types and SPIRV types with bitfields:

  • in the AST, bitfields are represented as their own fields.
  • in SPIRV, bitfields are meld into 1 field when possible.

This means we need to lower the type to SPIR-V to compute the correct offset.
This would be fine if we could always generate the Offset decoration, but some types like image samplers are structs, and computing an offset for them is not clean (maybe no legal?).
So we need to do it properly, and only generate offsets when the storage
type is physical.

Fixes #5327

@AppVeyorBot

Copy link
Copy Markdown

@s-perron Steven Perron (s-perron) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main concern is that you are creating a new layout rule, and I do not understand why. Is there one that matches the existing layout?

Not for this PR, but we should decide how the layout rules should change with the options:

  -fvk-use-dx-layout      Use DirectX memory layout for Vulkan resources
  -fvk-use-gl-layout      Use strict OpenGL std140/std430 memory layout for Vulkan resources
  -fvk-use-scalar-layout  Use scalar memory layout for Vulkan resources

Technically the physical storage buffers are not Vulkan resources (not set or binding), but it would make sense that they match. We should wait to see if anyone wants it. Until then, we can always use the same layout.

FxcCTBuffer, // fxc.exe layout rule for cbuffer/tbuffer
FxcSBuffer, // fxc.exe layout rule for structured buffers
Scalar, // VK_EXT_scalar_block_layout
Physical, // SPV_EXT_physical_storage_buffer

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should Physical be its own layout? Shouldn't the layout of the data in the physical storage buffer match the layout in say structured buffers?

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
HLSL/SPIR-V structs have some layout differences due to
bitfields being squashed.
The reconstruction logic was using the AST layout, and not the
new SPIR-V layout, meaning we could generate invalid indices
during extraction/construction.

This PR a potential bug with bitfields, while making the code
reusable.

Signed-off-by: Nathan Gauër <[email protected]>
Depending on the struct layout, the `Offset` decoration
is not always required.
When this storage class is setup, the decoration needs to be added.

The tricky part if the mix of AST types and SPIRV types with bitfields:
 - in the AST, bitfields are represented as their own fields.
 - in SPIRV, bitfields are meld into 1 field when possible.

This means we need to lower the type to SPIR-V to compute the correct
offset.
This would be fine if we could always generate the Offset decoration,
but some types like image samplers are structs, and computing an
offset for them is not clean (maybe no legal?).

So I wanted to wait to know the storage type to determine if
the decoration needed to be emitted of not.
Signed-off-by: Nathan Gauër <[email protected]>
@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

@Keenuts
Nathan Gauër (Keenuts) deleted the fix-5327-lowering branch July 6, 2023 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SPIR-V] DXC generates invalid SPIR-V when using RawBufferLoad with a struct.

3 participants