@@ -1112,6 +1112,19 @@ inline bool SkipCommaAndSpaces(const String& DXIL, size_t& pos)
11121112 // ^
11131113}
11141114
1115+ // Parses i32/i8 record
1116+ //
1117+ // Input:
1118+ // i32 78
1119+ // ^
1120+ // pos
1121+ //
1122+ // Output:
1123+ // i32 78
1124+ // ^ ^
1125+ // | pos
1126+ // Return value
1127+ // Value = 78
11151128template <typename T>
11161129size_t ParseIntRecord (const String& DXIL, size_t & pos, VALUE_TYPE Type, const char * RecordName, T* Value = nullptr )
11171130{
@@ -1164,7 +1177,17 @@ size_t ParseIntRecord(const String& DXIL, size_t& pos, VALUE_TYPE Type, const ch
11641177#undef CHECK_PARSING_ERROR
11651178}
11661179
1167-
1180+ // Replaces i32 record
1181+ //
1182+ // Input:
1183+ // , i32 -1
1184+ // ^
1185+ // pos
1186+ //
1187+ // Output:
1188+ // , i32 1
1189+ // ^
1190+ // pos
11681191void ReplaceRecord (String& DXIL, size_t & pos, const String& NewValue, const char * Name, const char * RecordName, const Uint32 ExpectedPrevValue)
11691192{
11701193#define CHECK_PATCHING_ERROR (Cond, ...) \
@@ -1182,6 +1205,10 @@ void ReplaceRecord(String& DXIL, size_t& pos, const String& NewValue, const char
11821205
11831206 Uint32 PrevValue = 0 ;
11841207 const size_t ValueStartPos = ParseIntRecord (DXIL, pos, VT_INT32, RecordName, &PrevValue);
1208+ // , i32 -1
1209+ // ^ ^
1210+ // | pos
1211+ // ValueStartPos
11851212 CHECK_PATCHING_ERROR (PrevValue == ExpectedPrevValue, " previous value does not match the expected" );
11861213
11871214 DXIL.replace (ValueStartPos, pos - ValueStartPos, NewValue);
@@ -1320,10 +1347,9 @@ void DXCompilerImpl::PatchResourceDeclaration(const TResourceBindingMap& Resourc
13201347 {
13211348 // , i32 -1
13221349 // ^
1323- if (pos + 1 >= DXIL. length () || DXIL[pos] != ' , ' || DXIL[ pos + 1 ] != ' ' )
1350+ if (! SkipCommaAndSpaces ( DXIL, pos) )
13241351 return false ;
13251352
1326- pos += 2 ;
13271353 // , i32 -1
13281354 // ^
13291355
@@ -1608,6 +1634,17 @@ void DXCompilerImpl::PatchResourceDeclaration(const TResourceBindingMap& Resourc
16081634#undef CHECK_PATCHING_ERROR
16091635}
16101636
1637+ // Finds position of the next argument
1638+ //
1639+ // Input:
1640+ // i32 78, i32 79, i32 80)
1641+ // ^
1642+ // pos
1643+ //
1644+ // Output:
1645+ // i32 78, i32 79, i32 80)
1646+ // ^
1647+ // pos
16111648static bool NextArg (String& DXIL, size_t & pos)
16121649{
16131650 for (; pos < DXIL.size (); ++pos)
@@ -1693,7 +1730,7 @@ const DXCompilerImpl::TExtendedResourceMap::value_type* DXCompilerImpl::FindReso
16931730//
16941731// StructuredBuffer<float4> g_Buffer1[5] : register(t29, space5);
16951732// ...
1696- // g_Buffer1[1][9]
1733+ // g_Buffer1[1][9];
16971734//
16981735// SM6.5
16991736// %g_Buffer1_texture_structbuf41 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 2, i32 30, i1 false)
@@ -1708,7 +1745,7 @@ const DXCompilerImpl::TExtendedResourceMap::value_type* DXCompilerImpl::FindReso
17081745//
17091746// StructuredBuffer<float4> g_Buffer1[5] : register(t29, space5);
17101747// ...
1711- // g_Buffer1[DynamicIndex + 1][26]
1748+ // g_Buffer1[DynamicIndex + 1][26];
17121749//
17131750// SM6.5
17141751// %69 = add i32 %68, 1 ;
@@ -1797,13 +1834,15 @@ void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, con
17971834 // dynamic bind point
17981835 // SrcIndexStr == "%22"
17991836
1800- // %22 = add i32 %17, 7 ;
1801- // %g_Buffer2_UAV_rawbuf38 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 1, i32 3, i32 %22, i1 false) ;
1802- // ^
1837+ // SM6.5
1838+ // %22 = add i32 %17, 7 ;
1839+ // %g_Buffer2_UAV_rawbuf38 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 1, i32 3, i32 %22, i1 false) ;
1840+ // ^
18031841
1804- // %28 = add i32 %17, 7 ;
1805- // %g_Buffer2_UAV_rawbuf38 = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 0, i32 -1, i32 1, i8 1 }, i32 %28, i1 false) ;
1806- // ^
1842+ // SM6.6
1843+ // %28 = add i32 %17, 7 ;
1844+ // %g_Buffer2_UAV_rawbuf38 = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 0, i32 -1, i32 1, i8 1 }, i32 %28, i1 false) ;
1845+ // ^
18071846
18081847 const String IndexDecl = SrcIndexStr + " = add i32 " ;
18091848 // IndexDecl == "%22 = add i32 "
@@ -1874,7 +1913,7 @@ void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, con
18741913 if (DXIL[pos] == ' ' || DXIL[pos] == ' ,' )
18751914 ++IndexVarUsageCount;
18761915 }
1877- DEV_CHECK_ERR (IndexVarUsageCount == 2 , " Temp variable '" , SrcIndexStr, " ' with resource bind point used more than 2 times, patching for this variable may lead to UB" );
1916+ DEV_CHECK_ERR (IndexVarUsageCount == 2 , " Temp variable '" , SrcIndexStr, " ' with resource bind point is used more than 2 times, patching for this variable may lead to UB" );
18781917#endif
18791918 }
18801919 else
@@ -1957,13 +1996,12 @@ void DXCompilerImpl::PatchCreateHandle(const TResourceBindingMap& ResourceMap, T
19571996 const ResourceExtendedInfo& ResInfo = pResInfo->second ;
19581997 const BindInfo& Bind = pResInfo->first ->second ;
19591998
1960- // Read index in range.
1999+ // Patch index in range.
19612000
19622001 CHECK_PATCHING_ERROR (SkipCommaAndSpaces (DXIL, pos), " Index record is not found" );
19632002 // @dx.op.createHandle(i32 57, i8 2, i32 0, i32 0, i1 false)
19642003 // ^
19652004
1966- // Replace index.
19672005 PatchResourceIndex (ResInfo, Bind, DXIL, pos);
19682006 }
19692007#undef CHECK_PATCHING_ERROR
@@ -2113,6 +2151,10 @@ void DXCompilerImpl::PatchCreateHandleFromBinding(const TResourceBindingMap& Res
21132151 // ^ ^
21142152 // ResBindRecordStartPos pos
21152153
2154+ VERIFY_EXPR (RangeMin >= 0 && (RangeMax == -1 || RangeMax >= RangeMin));
2155+ VERIFY_EXPR (Space >= 0 );
2156+ VERIFY_EXPR (ResClass >= 0 && ResClass < 4 );
2157+
21162158 // Register range and space are unique for each resource, so we can reliably find the resource by these values
21172159 const auto * pResInfo = FindResourceByBindPoint (ExtResMap, ResClass, static_cast <Uint32>(RangeMin), static_cast <Uint32>(Space));
21182160 CHECK_PATCHING_ERROR (pResInfo != nullptr , " Index record for resource class " , ResClass, " bind point " , RangeMin, " and space " , Space, " is not found" );
0 commit comments