Skip to content

Commit 04810cc

Browse files
DX Compiler: a few minor updates
1 parent d96bc8f commit 04810cc

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

Graphics/ShaderTools/src/DXCompiler.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class DXCompilerImpl final : public DXCompilerBase
170170
struct ResourceExtendedInfo
171171
{
172172
Uint32 SrcBindPoint = ~0u;
173+
Uint32 SrcArraySize = ~0u;
173174
Uint32 SrcSpace = ~0u;
174175
Uint32 RecordId = ~0u;
175176
RES_TYPE Type = RES_TYPE_INVALID;
@@ -926,6 +927,7 @@ bool DXCompilerImpl::RemapResourceBindings(const TResourceBindingMap& ResourceMa
926927
{
927928
auto& Ext = ExtResourceMap[&NameAndBinding];
928929
Ext.SrcBindPoint = ResDesc.BindPoint;
930+
Ext.SrcArraySize = ResDesc.BindCount;
929931
Ext.SrcSpace = ResDesc.Space;
930932

931933
# ifdef NO_D3D_SIT_ACCELSTRUCT_FEEDBACK_TEX
@@ -1068,25 +1070,30 @@ bool DXCompilerImpl::PatchDXIL(const TResourceBindingMap& ResourceMap, TExtended
10681070
namespace
10691071
{
10701072

1071-
bool IsWordSymbol(char c)
1073+
inline bool IsAlpha(char c)
10721074
{
1073-
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_');
1075+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
10741076
}
10751077

1076-
bool IsNumberSymbol(char c)
1078+
inline bool IsNumber(char c)
10771079
{
10781080
return (c >= '0' && c <= '9');
10791081
}
10801082

1081-
bool SkipSpaces(const String& DXIL, size_t& pos)
1083+
inline bool IsWordSymbol(char c)
1084+
{
1085+
return IsAlpha(c) || IsNumber(c) || (c == '_');
1086+
}
1087+
1088+
inline bool SkipSpaces(const String& DXIL, size_t& pos)
10821089
{
10831090
while (pos < DXIL.length() && DXIL[pos] == ' ')
10841091
++pos;
10851092

10861093
return pos < DXIL.length();
10871094
}
10881095

1089-
bool SkipCommaAndSpaces(const String& DXIL, size_t& pos)
1096+
inline bool SkipCommaAndSpaces(const String& DXIL, size_t& pos)
10901097
{
10911098
// , i32 -1
10921099
// ^
@@ -1139,7 +1146,7 @@ size_t ParseIntRecord(const String& DXIL, size_t& pos, VALUE_TYPE Type, const ch
11391146

11401147
if (DXIL[pos] == '-' || DXIL[pos] == '+')
11411148
++pos;
1142-
while (IsNumberSymbol(DXIL[pos]))
1149+
while (IsNumber(DXIL[pos]))
11431150
++pos;
11441151

11451152
CHECK_PARSING_ERROR(pos > ValueStartPos, "number is expected")
@@ -1431,7 +1438,7 @@ void DXCompilerImpl::PatchResourceDeclaration(const TResourceBindingMap& Resourc
14311438
for (; pos < EndOfResTypeRecord; ++pos)
14321439
{
14331440
const char c = DXIL[pos];
1434-
if (!(IsNumberSymbol(c) || (c == ' ') || (c == 'x')))
1441+
if (!(IsNumber(c) || (c == ' ') || (c == 'x')))
14351442
break;
14361443
}
14371444
}
@@ -1689,16 +1696,26 @@ void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, con
16891696
const auto ReplaceBindPoint = [&DXIL](const ResourceExtendedInfo& ResInfo, const BindInfo& Bind, size_t IndexStartPos, size_t IndexEndPos) //
16901697
{
16911698
const String SrcIndexStr = DXIL.substr(IndexStartPos, IndexEndPos - IndexStartPos);
1692-
VERIFY_EXPR(IsNumberSymbol(SrcIndexStr.front()));
1699+
VERIFY_EXPR(IsNumber(SrcIndexStr.front()));
16931700

16941701
const Uint32 SrcIndex = static_cast<Uint32>(std::stoi(SrcIndexStr));
16951702

1696-
VERIFY_EXPR(SrcIndex >= ResInfo.SrcBindPoint && SrcIndex < ResInfo.SrcBindPoint + Bind.ArraySize);
16971703
VERIFY_EXPR(ResInfo.SrcBindPoint != ~0u);
16981704

16991705
VERIFY(SrcIndex >= ResInfo.SrcBindPoint,
1700-
"Source index can't be less than the source bind point. "
1701-
"Either the byte code is corrupted or the source bind point is incorrect.");
1706+
"Source index (", SrcIndex, ") can't be less than the source bind point. (", ResInfo.SrcBindPoint,
1707+
"). Either the byte code is corrupted or the source bind point is incorrect.");
1708+
VERIFY(ResInfo.SrcArraySize == ~0u || SrcIndex < ResInfo.SrcBindPoint + std::max(ResInfo.SrcArraySize, 1u),
1709+
"Source index (", SrcIndex, ") can't exceed the source bind point + array size. (", ResInfo.SrcBindPoint, " + ", ResInfo.SrcArraySize,
1710+
"). Either the byte code is corrupted or the source bind point is incorrect.");
1711+
// Texture2D g_Tex[4] : register(t8);
1712+
// ...
1713+
// g_Tex[2].Sample(...);
1714+
//
1715+
// ResInfo.SrcBindPoint: 8
1716+
// ResInfo.SrcArraySize: 4
1717+
// SrcIndex: 10
1718+
// IndexOffset: 2
17021719
const Uint32 IndexOffset = SrcIndex - ResInfo.SrcBindPoint;
17031720

17041721
const String NewIndexStr = std::to_string(Bind.BindPoint + IndexOffset);
@@ -1772,13 +1789,13 @@ void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, con
17721789
// skip ", "
17731790

17741791
// second arg must be a constant
1775-
CHECK_PATCHING_ERROR(IsNumberSymbol(DXIL[pos]), "second argument expected to be an integer constant");
1792+
CHECK_PATCHING_ERROR(IsNumber(DXIL[pos]), "second argument expected to be an integer constant");
17761793

17771794
ArgStart = pos;
17781795
for (; pos < DXIL.size(); ++pos)
17791796
{
17801797
const char c = DXIL[pos];
1781-
if (!IsNumberSymbol(c))
1798+
if (!IsNumber(c))
17821799
break;
17831800
}
17841801
CHECK_PATCHING_ERROR(DXIL[pos] == ',' || DXIL[pos] == '\n', "failed to parse second argument");
@@ -1789,13 +1806,13 @@ void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, con
17891806
else
17901807
{
17911808
// first arg is a constant
1792-
VERIFY_EXPR(IsNumberSymbol(DXIL[pos]));
1809+
VERIFY_EXPR(IsNumber(DXIL[pos]));
17931810

17941811
ArgStart = pos;
17951812
for (; pos < DXIL.size(); ++pos)
17961813
{
17971814
const char c = DXIL[pos];
1798-
if (!IsNumberSymbol(c))
1815+
if (!IsNumber(c))
17991816
break;
18001817
}
18011818
CHECK_PATCHING_ERROR(DXIL[pos] == ',' || DXIL[pos] == '\n', "failed to parse second argument");

0 commit comments

Comments
 (0)