Skip to content

Commit 7eef7e1

Browse files
DXCompiler: comments + minor fix
1 parent 75baf86 commit 7eef7e1

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

Graphics/ShaderTools/src/DXCompiler.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ void DXCompilerImpl::PatchResourceDeclaration(const TResourceBindingMap& Resourc
15311531
if (IsWordSymbol(c))
15321532
continue; // name is partially equal, continue searching
15331533

1534-
VERIFY_EXPR((c == '*' && ResInfo.first->second.ArraySize == 1) || (c == ']' && ResInfo.first->second.ArraySize > 1));
1534+
VERIFY_EXPR((c == '*' && ResInfo.first->second.ArraySize == 1) || (c == ']' && ResInfo.first->second.ArraySize >= 1));
15351535

15361536
ResType = RES_TYPE_CBV;
15371537
break;
@@ -1685,6 +1685,43 @@ const DXCompilerImpl::TExtendedResourceMap::value_type* DXCompilerImpl::FindReso
16851685
return nullptr;
16861686
}
16871687

1688+
// Patch resource index in createHandle or createHandleFromBinding operation.
1689+
//
1690+
// Examples:
1691+
//
1692+
// Static Index
1693+
//
1694+
// StructuredBuffer<float4> g_Buffer1[5] : register(t29, space5);
1695+
// ...
1696+
// g_Buffer1[1][9]
1697+
//
1698+
// SM6.5
1699+
// %g_Buffer1_texture_structbuf41 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 2, i32 30, i1 false)
1700+
// ^
1701+
// 29+1
1702+
// SM6.5
1703+
// %g_Buffer1_texture_structbuf42 = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 29, i32 33, i32 5, i8 0 }, i32 30, i1 false)
1704+
// ^
1705+
// 29+1
1706+
//
1707+
// Dynamic Index
1708+
//
1709+
// StructuredBuffer<float4> g_Buffer1[5] : register(t29, space5);
1710+
// ...
1711+
// g_Buffer1[DynamicIndex + 1][26]
1712+
//
1713+
// SM6.5
1714+
// %69 = add i32 %68, 1 ;
1715+
// %70 = add i32 %69, 29 ; <- We will patch this index
1716+
// %g_Buffer1_texture_structbuf43 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 2, i32 %70, i1 false)
1717+
// ^
1718+
// SM6.6
1719+
// %85 = add i32 %84, 1 ;
1720+
// %86 = add i32 %85, 29 ; <- We will patch this index
1721+
// %g_Buffer1_texture_structbuf43 = call %dx.types.Handle @dx.op.createHandleFromBinding(i32 217, %dx.types.ResBind { i32 29, i32 33, i32 5, i8 0 }, i32 %86, i1 false)
1722+
// ^
1723+
1724+
16881725
void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, const BindInfo& Bind, String& DXIL, size_t& pos)
16891726
{
16901727
#define CHECK_PATCHING_ERROR(Cond, ...) \
@@ -1705,6 +1742,9 @@ void DXCompilerImpl::PatchResourceIndex(const ResourceExtendedInfo& ResInfo, con
17051742
VERIFY(SrcIndex >= ResInfo.SrcBindPoint,
17061743
"Source index (", SrcIndex, ") can't be less than the source bind point. (", ResInfo.SrcBindPoint,
17071744
"). Either the byte code is corrupted or the source bind point is incorrect.");
1745+
1746+
// Texture2D g_Textures[]; // SrcArraySize == 0
1747+
// ConstantBuffer<CBData> g_ConstantBuffers[]; // SrcArraySize == ~0u
17081748
VERIFY(ResInfo.SrcArraySize == ~0u || SrcIndex < ResInfo.SrcBindPoint + std::max(ResInfo.SrcArraySize, 1u),
17091749
"Source index (", SrcIndex, ") can't exceed the source bind point + array size. (", ResInfo.SrcBindPoint, " + ", ResInfo.SrcArraySize,
17101750
"). Either the byte code is corrupted or the source bind point is incorrect.");

Tests/DiligentCoreAPITest/src/DXCompilerTest.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ RWTexture2D<float4> g_ColorBuffer1;
467467
RWTexture2D<float4> g_ColorBuffer2;
468468
RWTexture2D<float4> g_ColorBuffer3;
469469
470-
StructuredBuffer<float4> g_Buffer1[5];
470+
StructuredBuffer<float4> g_Buffer1[5] : register(t29, space5);
471471
RWByteAddressBuffer g_Buffer2[] : register(u5, space1);
472472
473473
struct Matrix
@@ -484,6 +484,13 @@ cbuffer Texture2DConstants
484484
uint2 Range2;
485485
};
486486
487+
struct CBData
488+
{
489+
float4 f4;
490+
uint4 u4;
491+
};
492+
ConstantBuffer<CBData> g_CB[];
493+
487494
float4 main(in float4 f4Position : SV_Position) : SV_TARGET
488495
{
489496
float2 UV = f4Position.xy;
@@ -503,7 +510,10 @@ float4 main(in float4 f4Position : SV_Position) : SV_TARGET
503510
g_Tex[3].Sample(g_TexSampler, UV) +
504511
g_Tex3D.Sample(g_TexSampler, UV.xxy) +
505512
g_Buffer1[1][9] * g_Buffer1[4][100] +
506-
g_MatrixBuffer[3].m[0];
513+
g_MatrixBuffer[3].m[0] +
514+
g_Buffer1[Range2.x + 1][26] +
515+
g_CB[15].f4 +
516+
g_CB[Range2.y + 5].f4;
507517
}
508518
)hlsl";
509519

@@ -517,7 +527,7 @@ float4 main(in float4 f4Position : SV_Position) : SV_TARGET
517527
MaxSM = ShaderVersion{6, 6};
518528
}
519529

520-
for (Uint32 MinorVersion = 5; MinorVersion <= MaxSM.Minor; ++MinorVersion)
530+
for (Uint32 MinorVersion = 0; MinorVersion <= MaxSM.Minor; ++MinorVersion)
521531
{
522532
std::wstring Profile = L"ps_6_" + std::to_wstring(MinorVersion);
523533
LOG_INFO_MESSAGE("Testing shader profile ", NarrowString(Profile));
@@ -548,6 +558,7 @@ float4 main(in float4 f4Position : SV_Position) : SV_TARGET
548558
BindigMap["g_ColorBuffer3"] = { 1, 0, 1, SHADER_RESOURCE_TYPE_TEXTURE_UAV };
549559
BindigMap["Texture2DConstants"] = { 8, 0, 1, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER};
550560
BindigMap["g_MatrixBuffer"] = { 14, 0, 1, SHADER_RESOURCE_TYPE_BUFFER_SRV };
561+
BindigMap["g_CB"] = {316, 0, 1, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER };
551562
BindigMap["g_AnotherRes"] = {567, 0, 1, SHADER_RESOURCE_TYPE_TEXTURE_UAV };
552563
// clang-format on
553564
CComPtr<IDxcBlob> pRemappedDXIL;
@@ -596,6 +607,10 @@ float4 main(in float4 f4Position : SV_Position) : SV_TARGET
596607
EXPECT_EQ(BindDesc.BindPoint, 8U);
597608
EXPECT_EQ(BindDesc.Space, 0U);
598609

610+
EXPECT_HRESULT_SUCCEEDED(pReflection->GetResourceBindingDescByName("g_CB", &BindDesc));
611+
EXPECT_EQ(BindDesc.BindPoint, 316U);
612+
EXPECT_EQ(BindDesc.Space, 0U);
613+
599614
EXPECT_HRESULT_SUCCEEDED(pReflection->GetResourceBindingDescByName("g_MatrixBuffer", &BindDesc));
600615
EXPECT_EQ(BindDesc.BindPoint, 14U);
601616
EXPECT_EQ(BindDesc.Space, 0U);
@@ -613,6 +628,7 @@ float4 main(in float4 f4Position : SV_Position) : SV_TARGET
613628
BindigMap["g_ColorBuffer3"] = { 11, 100, 1, SHADER_RESOURCE_TYPE_TEXTURE_UAV };
614629
BindigMap["Texture2DConstants"] = { 9, 3, 1, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER};
615630
BindigMap["g_MatrixBuffer"] = { 10, 5, 1, SHADER_RESOURCE_TYPE_BUFFER_SRV };
631+
BindigMap["g_CB"] = {132, 8, 1, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER };
616632
BindigMap["g_AnotherRes"] = {567, 0, 1, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER};
617633
// clang-format on
618634
pRemappedDXIL = nullptr;
@@ -661,6 +677,10 @@ float4 main(in float4 f4Position : SV_Position) : SV_TARGET
661677
EXPECT_EQ(BindDesc.BindPoint, 9U);
662678
EXPECT_EQ(BindDesc.Space, 3U);
663679

680+
EXPECT_HRESULT_SUCCEEDED(pReflection->GetResourceBindingDescByName("g_CB", &BindDesc));
681+
EXPECT_EQ(BindDesc.BindPoint, 132U);
682+
EXPECT_EQ(BindDesc.Space, 8U);
683+
664684
EXPECT_HRESULT_SUCCEEDED(pReflection->GetResourceBindingDescByName("g_MatrixBuffer", &BindDesc));
665685
EXPECT_EQ(BindDesc.BindPoint, 10U);
666686
EXPECT_EQ(BindDesc.Space, 5U);

0 commit comments

Comments
 (0)