FindPermutationInBlob misses permutations when one define name is a prefix of another (A / A1)

Open Beginner friendly
#29 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
build-system

Research direction

The mismatch is between combinedDefines in ProcessConfigLine in ShaderMake.cpp, which sorts full NAME=value strings, and FindPermutationInBlob in ShaderBlob.cpp, which sorts only names. Start by reading both sort sites and reproducing the A / A1 case from the report with a small reader. Done means the reader finds all four permutations in that blob, and blobs already written by the writer still load.

Written by the indexing model from the issue text.

Description

FindPermutationInBlob fails to find permutations whose define names are prefixes of each other, such as A and A1. Every permutation of such a shader comes back as not found, even though the blob contains them.

The executable and the blob library build the permutation key in different orders:

  • ShaderMake.cpp (the combinedDefines construction in ProcessConfigLine) sorts the full NAME=value strings from the config line and joins them with spaces. That key is what goes into the blob.
  • FindPermutationInBlob in ShaderBlob.cpp sorts by the constant names only, then joins name=value.

The two orders are the same for most names. They differ when one name is a prefix of another and the next character sorts below = (0x3D), which covers digits, so TEXCOORD/TEXCOORD1 or LAYER/LAYER2 would both hit it. For A and A1:

  • writer: "A1=0" < "A=0" because '1' < '=', so the key is A1=0 A=0
  • reader: "A" < "A1", so it looks for A=0 A1=0

The comment above the writer's sort says the values do not matter as long as keys differ. With these names the = and the value take part in the comparison, so that does not hold.

Repro

ShaderMake main (4f1ffd5), built on macOS arm64 with Apple clang 21. I used a stand-in compiler script that writes a few bytes to the -Fo path, since only the blob layout matters here. Config line:

a.hlsl -T ps -D A={0,1} -D A1={0,1}

Run with --platform DXIL --binaryBlob. A small reader linked against ShaderBlob.cpp loads the blob and calls FindPermutationInBlob with {"A", a}, {"A1", a1}:

in blob: 'A1=0 A=0'
in blob: 'A1=1 A=0'
in blob: 'A1=0 A=1'
in blob: 'A1=1 A=1'
FindPermutationInBlob(A=0, A1=0) -> NOT FOUND
FindPermutationInBlob(A=0, A1=1) -> NOT FOUND
FindPermutationInBlob(A=1, A1=0) -> NOT FOUND
FindPermutationInBlob(A=1, A1=1) -> NOT FOUND

Passing the constants in a different order does not help, because the reader re-sorts them by name.

Possible fix

In FindPermutationInBlob, sort the name=value strings instead of the names, so the reader uses the same key as the writer. This keeps existing blobs readable. Changing the writer would invalidate blobs that were already built. I can send a PR for this.

Dominant language
C++
Stars
119
Forks
42
Avg merge
13d 13h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA-RTX/ShaderMake

All issues in NVIDIA-RTX/ShaderMake

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.