Skip to content

[SPIR-V] Fix texel selection in Gather with four constant offsets - #9158

Merged
Gregory Roth (pow2clk) merged 3 commits into
microsoft:mainfrom
dnovillo:fix-9031-gather-cst-offset
Dec 1, 2026
Merged

Gregory Roth (pow2clk) merged 3 commits into
microsoft:mainfrom
dnovillo:fix-9031-gather-cst-offset

Conversation

@dnovillo

@dnovillo Diego Novillo (dnovillo) commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

A four-offset Gather with constant offsets returns the wrong texels in SPIR-V. With four int2(0, 0) offsets, GatherRed returns the i0j0 texel in every element of the result.

DXC copies the four HLSL offsets into ConstOffsets unchanged, but the two are relative to different texels. In HLSL, offset k moves the texel that element k reads. In SPIR-V, every ConstOffsets entry moves texel i0j0, the one element w reads (Texel Gathering). So with zero offsets, every element reads i0j0.

This change adds each element's position in the 2x2 footprint, (0,1), (1,1), (1,0) and (0,0) for elements x, y, z and w, to its offset before building ConstOffsets. Non-constant offsets already use four gathers, so they don't need to be handled.

Tests:

  • texture.gather-const-offsets.hlsl checks zero and negative offsets and GatherCmpRed.
  • The texture.gather-* and texture.array.gather-* tests expect the adjusted constants.

Fixes #9031

Copilot AI balanced review requested due to automatic review settings November 20, 2026 19:41
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk) could you run CI and review? Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A SPIR-V release note is still missing from docs/ReleaseNotes.md for the user-visible correction to texel selection with four constant Gather offsets.

1 open issue
What changed in this PR

Fixes SPIR-V texel selection for gathers with four constant offsets, addressing #9031 by matching HLSL’s per-lane offset semantics.

Changes:

  • Adds each lane’s footprint position before constructing ConstOffsets.
  • Adds regression coverage for zero and negative offsets, including comparison gathers.
  • Updates existing texture and texture-array gather expectations.
File Description
tools/​clang/​test/​CodeGenSPIRV/​texture.gather-red.hlsl Updates red-channel offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.gather-green.hlsl Updates green-channel offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.gather-const-offsets.hlsl Adds zero, negative, and comparison-offset regression cases.
tools/​clang/​test/​CodeGenSPIRV/​texture.gather-cmp-red.hlsl Updates comparison-gather offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.gather-blue.hlsl Updates blue-channel offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.gather-alpha.hlsl Updates alpha-channel offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.array.gather-red.hlsl Updates array red-gather expectations while retaining unadjusted constants for other checks.
tools/​clang/​test/​CodeGenSPIRV/​texture.array.gather-green.hlsl Updates array green-gather offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.array.gather-cmp-red.hlsl Updates array comparison-gather offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.array.gather-blue.hlsl Updates array blue-gather offset expectations.
tools/​clang/​test/​CodeGenSPIRV/​texture.array.gather-alpha.hlsl Updates array alpha-gather offset expectations.
tools/​clang/​lib/​SPIRV/​SpirvEmitter.cpp Adjusts constant gather offsets by lane position.

Review effort: Balanced

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Copilot AI balanced review requested due to automatic review settings November 20, 2026 19:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the change correcting SPIR-V four-constant-offset Gather texel selection.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@dnovillo Diego Novillo (dnovillo) left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Gregory Roth (@pow2clk) ping for review. Thanks.

@pow2clk Gregory Roth (pow2clk) 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.

While this fixes a genuine problem, that problem is not #9031 because I don't think that is a real problem.
Edit: nevermind. I see that though it started saying the problem was that all the values were the same and not A,B,C,D like DXIL, it went on to explain this won't work with SPIR-V.

I'm going to have to insist on not calling elements of vectors lanes in spite of what agents might say. Please reword the description and title as well.

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
@github-project-automation github-project-automation Bot moved this from New to In progress in HLSL Roadmap Nov 30, 2026
@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@dnovillo Diego Novillo (dnovillo) changed the title [SPIR-V] Add lane positions to four constant Gather offsets [SPIR-V] Fix texel selection in Gather with four constant offsets Dec 1, 2026
Copilot AI balanced review requested due to automatic review settings December 1, 2026 21:22

@dnovillo Diego Novillo (dnovillo) left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Gregory Roth (@pow2clk) thanks for the review! PTAL

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Offset rebasing can exceed Vulkan’s supported gather-offset range for previously valid inputs; a fallback to the original offsets and an upper-bound regression test are needed.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Rebased gather offsets exceed Vulkan's guaranteed range

tools/​clang/​lib/​SPIRV/​SpirvEmitter.cpp:4930

Four int2(7, 7) offsets now produce (7, 8), (8, 8), (8, 7), and (7, 7), exceeding the supported range on Vulkan devices with maxTexelGatherOffset == 7. The original offsets fit that limit, so this adjustment makes previously supported inputs exceed it. Fall back to four gathers with the original offsets when rebasing exceeds the guaranteed range, and add an upper-bound regression test.

Review effort: Balanced

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@pow2clk Gregory Roth (pow2clk) 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.

Thanks!

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@pow2clk
Gregory Roth (pow2clk) enabled auto-merge (squash) December 1, 2026 21:54
@pow2clk
Gregory Roth (pow2clk) merged commit ddfb5fc into microsoft:main Dec 1, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[SPIR-V] Incorrect GatherRed/GatherGreen/GatherBlue/GatherAlpha texel selection with four compile-time constant offsets

4 participants