Skip to content

[DirectX] Add static sampler support to root signature #143422

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

Merged
merged 85 commits into from
Jun 27, 2025

Conversation

joaosaffran
Copy link
Contributor

@joaosaffran joaosaffran commented Jun 9, 2025

Implements static samplers parsing from root signature metadata representation. This is required to support Root Signatures in HLSL.
Closes: #126641

@joaosaffran joaosaffran requested review from a team as code owners June 20, 2025 18:25
@QuietMisdreavus QuietMisdreavus removed request for a team and QuietMisdreavus June 20, 2025 18:39
@joaosaffran joaosaffran changed the base branch from users/joaosaffran/142492 to main June 20, 2025 19:12
@joaosaffran joaosaffran requested a review from inbelic June 20, 2025 19:19
Copy link
Contributor

@inbelic inbelic left a comment

Choose a reason for hiding this comment

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

I think we should decide if we want to use the bitmask technique for all enums or not. Or if it is just for flags.

support::endian::write(BOS, S.RegisterSpace, llvm::endianness::little);
support::endian::write(BOS, S.ShaderVisibility, llvm::endianness::little);
if (NumSamplers > 0) {
rewriteOffsetToCurrentByte(BOS, SSO);
Copy link
Contributor

Choose a reason for hiding this comment

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

So when NumSampler == 0 is required to be 0 right? Or is it just ignored? Or do we set it 0 to match DXC?

Just want to double-check that is defined and correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I set it to zero just to make sure we are not jumping into a random file location by mistake. I don't think DXC forces it to be zero. But if you have no ranges, the offset should be ignored.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW DXC writes the value where samplers would be unconditionally, so there might be some argument to match that just so that we're binary identical.

That said, you are correct that this offset should be ignored when there aren't any samplers, and I think that explicitly zeroing it makes that clearer, so I kind of like it as is.

return reportError(Ctx, "Invalid value for AddressW");

if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 5))
Sampler.MipLODBias = Val->convertToFloat();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can convertToFloat fail? It might be better to have this as part of extract and return nullopt on failure.

You might need to check opStatus or so?

Even if it doesn't fail, imo, it makes sense to invoke convertToFloat in the extract function anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did some research, I don't think convertToFloat to float, at least, I didn't see any error handling for it specifically. I moved to inside the extract function anyway.

}

static bool verifyMipLODBias(float MipLODBias) {
return MipLODBias >= -16.f && MipLODBias <= 16.f;
Copy link
Contributor

Choose a reason for hiding this comment

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

@joaosaffran joaosaffran requested a review from inbelic June 25, 2025 17:54
Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

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

Looks good. A few comments inline but nothing functional.

support::endian::write(BOS, S.RegisterSpace, llvm::endianness::little);
support::endian::write(BOS, S.ShaderVisibility, llvm::endianness::little);
if (NumSamplers > 0) {
rewriteOffsetToCurrentByte(BOS, SSO);
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW DXC writes the value where samplers would be unconditionally, so there might be some argument to match that just so that we're binary identical.

That said, you are correct that this offset should be ignored when there aren't any samplers, and I think that explicitly zeroing it makes that clearer, so I kind of like it as is.

}

static bool verifyMipLODBias(float MipLODBias) {
return MipLODBias >= -16.f && MipLODBias <= 15.99f;
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this matches what DXC's validator does so it's probably "correct", but 15.99 isn't actually representable in float and this literal is subject to rounding.

if (!verifySamplerFilter(Sampler.Filter))
return reportValueError(Ctx, "Filter", Sampler.Filter);

if (!verifyAddress(Sampler.AddressU))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll take a look at the other PR and think about it there, but FWIW we don't expect to be getting here from a user's input per se - I would expect we only really get here from a root signature read from a binary format or from a frontend that's presumably already attempted to validate the input.

@joaosaffran joaosaffran merged commit 171aa34 into llvm:main Jun 27, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants