Skip to content

Commit fb9c7c4

Browse files
author
joaosaffran
committed
add validations
1 parent 9f51858 commit fb9c7c4

File tree

2 files changed

+170
-2
lines changed

2 files changed

+170
-2
lines changed

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "DXILRootSignature.h"
1414
#include "DirectX.h"
1515
#include "llvm/ADT/APFloat.h"
16+
#include "llvm/ADT/STLForwardCompat.h"
1617
#include "llvm/ADT/StringSwitch.h"
1718
#include "llvm/ADT/Twine.h"
1819
#include "llvm/Analysis/DXILMetadataAnalysis.h"
@@ -28,6 +29,7 @@
2829
#include "llvm/Support/Error.h"
2930
#include "llvm/Support/ErrorHandling.h"
3031
#include "llvm/Support/raw_ostream.h"
32+
#include <cmath>
3133
#include <cstdint>
3234
#include <optional>
3335
#include <utility>
@@ -478,6 +480,127 @@ static bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
478480
return (Flags & ~DataFlags) == FlagT::NONE;
479481
}
480482

483+
static bool verifySamplerFilter(uint32_t Filter) {
484+
switch (Filter) {
485+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_MIP_POINT):
486+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_POINT_MIP_LINEAR):
487+
case llvm::to_underlying(
488+
dxbc::StaticSamplerFilter::MIN_POINT_MAG_LINEAR_MIP_POINT):
489+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_POINT_MAG_MIP_LINEAR):
490+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_LINEAR_MAG_MIP_POINT):
491+
case llvm::to_underlying(
492+
dxbc::StaticSamplerFilter::MIN_LINEAR_MAG_POINT_MIP_LINEAR):
493+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_LINEAR_MIP_POINT):
494+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MIN_MAG_MIP_LINEAR):
495+
case llvm::to_underlying(dxbc::StaticSamplerFilter::ANISOTROPIC):
496+
case llvm::to_underlying(
497+
dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_MIP_POINT):
498+
case llvm::to_underlying(
499+
dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_POINT_MIP_LINEAR):
500+
case llvm::to_underlying(
501+
dxbc::StaticSamplerFilter::COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT):
502+
case llvm::to_underlying(
503+
dxbc::StaticSamplerFilter::COMPARISON_MIN_POINT_MAG_MIP_LINEAR):
504+
case llvm::to_underlying(
505+
dxbc::StaticSamplerFilter::COMPARISON_MIN_LINEAR_MAG_MIP_POINT):
506+
case llvm::to_underlying(
507+
dxbc::StaticSamplerFilter::COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR):
508+
case llvm::to_underlying(
509+
dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_LINEAR_MIP_POINT):
510+
case llvm::to_underlying(
511+
dxbc::StaticSamplerFilter::COMPARISON_MIN_MAG_MIP_LINEAR):
512+
case llvm::to_underlying(dxbc::StaticSamplerFilter::COMPARISON_ANISOTROPIC):
513+
case llvm::to_underlying(
514+
dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_MIP_POINT):
515+
case llvm::to_underlying(
516+
dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_POINT_MIP_LINEAR):
517+
case llvm::to_underlying(
518+
dxbc::StaticSamplerFilter::MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT):
519+
case llvm::to_underlying(
520+
dxbc::StaticSamplerFilter::MINIMUM_MIN_POINT_MAG_MIP_LINEAR):
521+
case llvm::to_underlying(
522+
dxbc::StaticSamplerFilter::MINIMUM_MIN_LINEAR_MAG_MIP_POINT):
523+
case llvm::to_underlying(
524+
dxbc::StaticSamplerFilter::MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR):
525+
case llvm::to_underlying(
526+
dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_LINEAR_MIP_POINT):
527+
case llvm::to_underlying(
528+
dxbc::StaticSamplerFilter::MINIMUM_MIN_MAG_MIP_LINEAR):
529+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MINIMUM_ANISOTROPIC):
530+
case llvm::to_underlying(
531+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_MIP_POINT):
532+
case llvm::to_underlying(
533+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_POINT_MIP_LINEAR):
534+
case llvm::to_underlying(
535+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT):
536+
case llvm::to_underlying(
537+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_POINT_MAG_MIP_LINEAR):
538+
case llvm::to_underlying(
539+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_LINEAR_MAG_MIP_POINT):
540+
case llvm::to_underlying(
541+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR):
542+
case llvm::to_underlying(
543+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_LINEAR_MIP_POINT):
544+
case llvm::to_underlying(
545+
dxbc::StaticSamplerFilter::MAXIMUM_MIN_MAG_MIP_LINEAR):
546+
case llvm::to_underlying(dxbc::StaticSamplerFilter::MAXIMUM_ANISOTROPIC):
547+
return true;
548+
}
549+
return false;
550+
}
551+
552+
// Values allowed here:
553+
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_address_mode#syntax
554+
static bool verifyAddress(uint32_t Address) {
555+
switch (Address) {
556+
case llvm::to_underlying(dxbc::TextureAddressMode::Border):
557+
case llvm::to_underlying(dxbc::TextureAddressMode::Clamp):
558+
case llvm::to_underlying(dxbc::TextureAddressMode::Mirror):
559+
case llvm::to_underlying(dxbc::TextureAddressMode::MirrorOnce):
560+
case llvm::to_underlying(dxbc::TextureAddressMode::Wrap):
561+
return true;
562+
}
563+
564+
return false;
565+
}
566+
567+
static bool verifyMipLODBias(float MipLODBias) {
568+
return MipLODBias >= -16.f && MipLODBias <= 16.f;
569+
}
570+
571+
static bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
572+
return MaxAnisotropy <= 16u;
573+
}
574+
575+
static bool verifyComparisonFunc(uint32_t ComparisonFunc) {
576+
switch (ComparisonFunc) {
577+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Never):
578+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Less):
579+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Equal):
580+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::LessEqual):
581+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Greater):
582+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::NotEqual):
583+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::GreaterEqual):
584+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Always):
585+
return true;
586+
}
587+
return false;
588+
}
589+
590+
static bool verifyBorderColor(uint32_t BorderColor) {
591+
switch (BorderColor) {
592+
case llvm::to_underlying(dxbc::SamplersBorderColor::TransparentBlack):
593+
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueBlack):
594+
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhite):
595+
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueBlackUint):
596+
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhiteUint):
597+
return true;
598+
}
599+
return false;
600+
}
601+
602+
static bool verifyLOD(float LOD) { return !std::isnan(LOD); }
603+
481604
static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
482605

483606
if (!verifyVersion(RSD.Version)) {
@@ -535,6 +658,51 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
535658
}
536659
}
537660

661+
for (const dxbc::RTS0::v1::StaticSampler &Sampler : RSD.StaticSamplers) {
662+
if (!verifySamplerFilter(Sampler.Filter))
663+
return reportValueError(Ctx, "Filter", Sampler.Filter);
664+
665+
if (!verifyAddress(Sampler.AddressU))
666+
return reportValueError(Ctx, "AddressU", Sampler.AddressU);
667+
668+
if (!verifyAddress(Sampler.AddressV))
669+
return reportValueError(Ctx, "AddressU", Sampler.AddressV);
670+
671+
if (!verifyAddress(Sampler.AddressW))
672+
return reportValueError(Ctx, "AddressU", Sampler.AddressW);
673+
674+
if (!verifyMipLODBias(Sampler.MipLODBias))
675+
return reportValueError(Ctx, "MipLODBias", Sampler.MipLODBias);
676+
677+
if (!verifyMaxAnisotropy(Sampler.MaxAnisotropy))
678+
return reportValueError(Ctx, "MaxAnisotropy", Sampler.MaxAnisotropy);
679+
680+
if (!verifyComparisonFunc(Sampler.ComparisonFunc))
681+
return reportValueError(Ctx, "ComparisonFunc", Sampler.ComparisonFunc);
682+
683+
if (!verifyComparisonFunc(Sampler.ComparisonFunc))
684+
return reportValueError(Ctx, "ComparisonFunc", Sampler.ComparisonFunc);
685+
686+
if (!verifyBorderColor(Sampler.BorderColor))
687+
return reportValueError(Ctx, "BorderColor ", Sampler.BorderColor);
688+
689+
if (!verifyLOD(Sampler.MinLOD))
690+
return reportValueError(Ctx, "MinLOD ", Sampler.MinLOD);
691+
692+
if (!verifyLOD(Sampler.MaxLOD))
693+
return reportValueError(Ctx, "MaxLOD ", Sampler.MaxLOD);
694+
695+
if (!verifyRegisterValue(Sampler.ShaderRegister))
696+
return reportValueError(Ctx, "ShaderRegister", Sampler.ShaderRegister);
697+
698+
if (!verifyRegisterSpace(Sampler.RegisterSpace))
699+
return reportValueError(Ctx, "RegisterSpace", Sampler.RegisterSpace);
700+
701+
if (!dxbc::isValidShaderVisibility(Sampler.ShaderVisibility))
702+
return reportValueError(Ctx, "ShaderVisibility",
703+
Sampler.ShaderVisibility);
704+
}
705+
538706
return false;
539707
}
540708

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-StaticSamplers.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
1515
!dx.rootsignatures = !{!2} ; list of function/root signature pairs
1616
!2 = !{ ptr @main, !3 } ; function, root signature
1717
!3 = !{ !5 } ; list of root signature elements
18-
!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x40403999A0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 }
18+
!5 = !{ !"StaticSampler", i32 4, i32 2, i32 3, i32 5, float 0x3FF6CCCCC0000000, i32 9, i32 3, i32 2, float -1.280000e+02, float 1.280000e+02, i32 42, i32 0, i32 0 }
1919

2020
; DXC: - Name: RTS0
2121
; DXC-NEXT: Size: 76
@@ -31,7 +31,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
3131
; DXC-NEXT: AddressU: 2
3232
; DXC-NEXT: AddressV: 3
3333
; DXC-NEXT: AddressW: 5
34-
; DXC-NEXT: MipLODBias: 32.45
34+
; DXC-NEXT: MipLODBias: 1.425
3535
; DXC-NEXT: MaxAnisotropy: 9
3636
; DXC-NEXT: ComparisonFunc: 3
3737
; DXC-NEXT: BorderColor: 2

0 commit comments

Comments
 (0)