Skip to content

Commit 7040af7

Browse files
author
joaosaffran
committed
improve validation
1 parent 91cb708 commit 7040af7

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ static std::optional<uint32_t> extractMdIntValue(MDNode *Node,
5656
return std::nullopt;
5757
}
5858

59-
static std::optional<APFloat> extractMdFloatValue(MDNode *Node,
60-
unsigned int OpId) {
59+
static std::optional<float> extractMdFloatValue(MDNode *Node,
60+
unsigned int OpId) {
6161
if (auto *CI = mdconst::dyn_extract<ConstantFP>(Node->getOperand(OpId).get()))
62-
return CI->getValue();
62+
return CI->getValueAPF().convertToFloat();
6363
return std::nullopt;
6464
}
6565

@@ -295,8 +295,8 @@ static bool parseStaticSampler(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
295295
else
296296
return reportError(Ctx, "Invalid value for AddressW");
297297

298-
if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 5))
299-
Sampler.MipLODBias = Val->convertToFloat();
298+
if (std::optional<float> Val = extractMdFloatValue(StaticSamplerNode, 5))
299+
Sampler.MipLODBias = *Val;
300300
else
301301
return reportError(Ctx, "Invalid value for MipLODBias");
302302

@@ -315,13 +315,13 @@ static bool parseStaticSampler(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
315315
else
316316
return reportError(Ctx, "Invalid value for ComparisonFunc ");
317317

318-
if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 9))
319-
Sampler.MinLOD = Val->convertToFloat();
318+
if (std::optional<float> Val = extractMdFloatValue(StaticSamplerNode, 9))
319+
Sampler.MinLOD = *Val;
320320
else
321321
return reportError(Ctx, "Invalid value for MinLOD");
322322

323-
if (std::optional<APFloat> Val = extractMdFloatValue(StaticSamplerNode, 10))
324-
Sampler.MaxLOD = Val->convertToFloat();
323+
if (std::optional<float> Val = extractMdFloatValue(StaticSamplerNode, 10))
324+
Sampler.MaxLOD = *Val;
325325
else
326326
return reportError(Ctx, "Invalid value for MaxLOD");
327327

@@ -493,33 +493,29 @@ static bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
493493
}
494494

495495
static bool verifySamplerFilter(uint32_t Value) {
496-
dxbc::StaticSamplerFilter Filter = dxbc::StaticSamplerFilter(Value);
497-
498-
dxbc::StaticSamplerFilter Mask = dxbc::StaticSamplerFilter::MIN_MAG_MIP_POINT;
499-
500-
#define STATIC_SAMPLER_FILTER(Num, Val) Mask |= dxbc::StaticSamplerFilter::Val;
496+
switch (Value) {
497+
#define STATIC_SAMPLER_FILTER(Num, Val) \
498+
case llvm::to_underlying(dxbc::StaticSamplerFilter::Val):
501499
#include "llvm/BinaryFormat/DXContainerConstants.def"
502-
503-
return popcount(llvm::to_underlying(Filter & Mask)) == 1;
500+
return true;
501+
}
502+
return false;
504503
}
505504

506505
// Values allowed here:
507506
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_address_mode#syntax
508507
static bool verifyAddress(uint32_t Address) {
509508
switch (Address) {
510-
case llvm::to_underlying(dxbc::TextureAddressMode::Border):
511-
case llvm::to_underlying(dxbc::TextureAddressMode::Clamp):
512-
case llvm::to_underlying(dxbc::TextureAddressMode::Mirror):
513-
case llvm::to_underlying(dxbc::TextureAddressMode::MirrorOnce):
514-
case llvm::to_underlying(dxbc::TextureAddressMode::Wrap):
509+
#define TEXTURE_ADDRESS_MODE(Num, Val) \
510+
case llvm::to_underlying(dxbc::TextureAddressMode::Val):
511+
#include "llvm/BinaryFormat/DXContainerConstants.def"
515512
return true;
516513
}
517-
518514
return false;
519515
}
520516

521517
static bool verifyMipLODBias(float MipLODBias) {
522-
return MipLODBias >= -16.f && MipLODBias <= 16.f;
518+
return MipLODBias >= -16.f && MipLODBias <= 15.99f;
523519
}
524520

525521
static bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
@@ -528,26 +524,19 @@ static bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
528524

529525
static bool verifyComparisonFunc(uint32_t ComparisonFunc) {
530526
switch (ComparisonFunc) {
531-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Never):
532-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Less):
533-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Equal):
534-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::LessEqual):
535-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Greater):
536-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::NotEqual):
537-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::GreaterEqual):
538-
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Always):
527+
#define COMPARISON_FUNCTION(Num, Val) \
528+
case llvm::to_underlying(dxbc::SamplersComparisonFunction::Val):
529+
#include "llvm/BinaryFormat/DXContainerConstants.def"
539530
return true;
540531
}
541532
return false;
542533
}
543534

544535
static bool verifyBorderColor(uint32_t BorderColor) {
545536
switch (BorderColor) {
546-
case llvm::to_underlying(dxbc::SamplersBorderColor::TransparentBlack):
547-
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueBlack):
548-
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhite):
549-
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueBlackUint):
550-
case llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhiteUint):
537+
#define STATIC_BORDER_COLOR(Num, Val) \
538+
case llvm::to_underlying(dxbc::SamplersBorderColor::Val):
539+
#include "llvm/BinaryFormat/DXContainerConstants.def"
551540
return true;
552541
}
553542
return false;

0 commit comments

Comments
 (0)