Skip to content

Commit e33790f

Browse files
Render state cache: copy static resources when reloading PSO
1 parent 3104c66 commit e33790f

File tree

7 files changed

+154
-55
lines changed

7 files changed

+154
-55
lines changed

Graphics/GraphicsTools/src/RenderStateCache.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,11 @@ bool ReloadablePipelineState::Reload(ModifyPipelineReloadInfoCallbackType Modify
13731373

13741374
if (pNewPSO)
13751375
{
1376-
m_pPipeline = pNewPSO;
1376+
if (m_pPipeline != pNewPSO)
1377+
{
1378+
m_pPipeline->CopyStaticResources(pNewPSO);
1379+
m_pPipeline = pNewPSO;
1380+
}
13771381
}
13781382
else
13791383
{

Tests/DiligentCoreAPITest/assets/shaders/RenderStateCache/GraphicsCommon.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ struct PSInput
55
float4 Pos : SV_POSITION;
66
float3 Color : COLOR;
77
};
8+
9+
struct ReloadTestData
10+
{
11+
float4 VertColors[3];
12+
float4 RefTexColors[4];
13+
};
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
#include "GraphicsCommon.h"
22

3+
cbuffer Colors
4+
{
5+
ReloadTestData g_Data;
6+
};
7+
8+
Texture2D g_Tex2D_Static0;
9+
Texture2D g_Tex2D_Static1;
10+
Texture2D g_Tex2D_Mut;
11+
Texture2D g_Tex2D_Dyn;
12+
13+
SamplerState g_Tex2D_Static0_sampler;
14+
SamplerState g_Tex2D_Static1_sampler;
15+
SamplerState g_Tex2D_Mut_sampler;
16+
SamplerState g_Tex2D_Dyn_sampler;
17+
318
float4 main(in PSInput PSIn) : SV_Target
419
{
5-
return float4(0.0, 0.0, 0.0, PSIn.Color.r);
6-
}
20+
float2 UV = float2(0.5, 0.5);
21+
return float4(0.0, 0.0, 0.0, PSIn.Color.r) *
22+
g_Tex2D_Static0.Sample(g_Tex2D_Static0_sampler, UV.xy) *
23+
g_Tex2D_Static1.Sample(g_Tex2D_Static1_sampler, UV.xy) *
24+
g_Tex2D_Mut.Sample(g_Tex2D_Mut_sampler, UV.xy) *
25+
g_Tex2D_Dyn.Sample(g_Tex2D_Dyn_sampler, UV.xy) *
26+
g_Data.RefTexColors[0];
27+
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
#include "GraphicsCommon.h"
22

3+
cbuffer Colors
4+
{
5+
ReloadTestData g_Data;
6+
};
7+
8+
Texture2D g_Tex2D_Static0;
9+
Texture2D g_Tex2D_Static1;
10+
Texture2D g_Tex2D_Mut;
11+
Texture2D g_Tex2D_Dyn;
12+
13+
SamplerState g_Tex2D_Static0_sampler;
14+
SamplerState g_Tex2D_Static1_sampler;
15+
SamplerState g_Tex2D_Mut_sampler;
16+
SamplerState g_Tex2D_Dyn_sampler;
17+
18+
float4 CheckValue(float4 Val, float4 Expected)
19+
{
20+
return float4(Val.x == Expected.x ? 1.0 : 0.0,
21+
Val.y == Expected.y ? 1.0 : 0.0,
22+
Val.z == Expected.z ? 1.0 : 0.0,
23+
Val.w == Expected.w ? 1.0 : 0.0);
24+
}
25+
326
float4 main(in PSInput PSIn) : SV_Target
427
{
5-
return float4(PSIn.Color.rgb, 1.0);
28+
float2 UV = float2(0.5, 0.5);
29+
return float4(PSIn.Color.rgb, 1.0) *
30+
CheckValue(g_Tex2D_Static0.Sample(g_Tex2D_Static0_sampler, UV.xy), g_Data.RefTexColors[0]) *
31+
CheckValue(g_Tex2D_Static1.Sample(g_Tex2D_Static1_sampler, UV.xy), g_Data.RefTexColors[1]) *
32+
CheckValue(g_Tex2D_Mut.Sample(g_Tex2D_Mut_sampler, UV.xy), g_Data.RefTexColors[2]) *
33+
CheckValue(g_Tex2D_Dyn.Sample(g_Tex2D_Dyn_sampler, UV.xy), g_Data.RefTexColors[3]);
634
// NB: no new line at the end of file!
735
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#include "GraphicsCommon.h"
22

3+
cbuffer Colors
4+
{
5+
ReloadTestData g_Data;
6+
};
7+
38
struct VSInput
49
{
5-
float4 Pos : ATTRIB0;
6-
float3 Color : ATTRIB1;
10+
float4 Pos : ATTRIB0;
711
};
812

913
#if INTERNAL_MACROS == 1 && EXTERNAL_MACROS == 2
10-
void main(in VSInput VSIn,
14+
void main(in uint VertId : SV_VertexID,
15+
in VSInput VSIn,
1116
out PSInput PSIn)
1217
{
1318
PSIn.Pos = VSIn.Pos;
14-
PSIn.Color = VSIn.Color;
19+
PSIn.Color = g_Data.VertColors[VertId % 3].rgb;
1520
}
1621
// NB: no new line at the end of file!
1722
#endif
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#include "GraphicsCommon.h"
22

3+
cbuffer Colors
4+
{
5+
ReloadTestData g_Data;
6+
};
7+
38
struct VSInput
49
{
5-
float4 Pos : ATTRIB0;
6-
float3 Color : ATTRIB1;
10+
float4 Pos : ATTRIB0;
711
};
812

913
#if INTERNAL_MACROS == 1 && EXTERNAL_MACROS == 2
1014
void main(in VSInput VSIn,
1115
out PSInput PSIn)
1216
{
13-
PSIn.Pos = float4(0.0, 0.0, 0.0, VSIn.Pos.x);
14-
PSIn.Color = float3(0.0, 0.0, VSIn.Color.r);
17+
PSIn.Pos = VSIn.Pos * 1e-10;
18+
PSIn.Color = g_Data.VertColors[0].rgb * 1e-10;
1519
}
1620
// NB: no new line at the end of file!
1721
#endif

Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,28 @@
2424
* of the possibility of such damages.
2525
*/
2626

27+
#include <functional>
28+
2729
#include "GPUTestingEnvironment.hpp"
2830
#include "TestingSwapChainBase.hpp"
2931
#include "RenderStateCache.h"
3032
#include "RenderStateCache.hpp"
3133
#include "FastRand.hpp"
3234
#include "GraphicsTypesX.hpp"
3335
#include "CallbackWrapper.hpp"
36+
#include "ResourceLayoutTestCommon.hpp"
3437

3538
#include "InlineShaders/RayTracingTestHLSL.h"
3639

3740
#include "gtest/gtest.h"
3841

39-
namespace Diligent
40-
{
41-
namespace Testing
42-
{
43-
void RenderDrawCommandReference(ISwapChain* pSwapChain, const float* pClearColor = nullptr);
44-
void ComputeShaderReference(ISwapChain* pSwapChain);
45-
} // namespace Testing
46-
} // namespace Diligent
47-
4842
using namespace Diligent;
4943
using namespace Diligent::Testing;
5044

5145
namespace
5246
{
5347

54-
void TestDraw(IShader* pVS, IShader* pPS, IPipelineState* pPSO, bool UseRenderPass, IBuffer* pVertexBuffer = nullptr)
48+
void TestDraw(IShader* pVS, IShader* pPS, IPipelineState* pPSO, bool UseRenderPass, std::function<void()> PreDraw = nullptr)
5549
{
5650
VERIFY_EXPR((pVS != nullptr && pPS != nullptr) ^ (pPSO != nullptr));
5751

@@ -124,13 +118,11 @@ void TestDraw(IShader* pVS, IShader* pPS, IPipelineState* pPSO, bool UseRenderPa
124118
pCtx->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
125119
}
126120

127-
if (pVertexBuffer != nullptr)
128-
{
129-
IBuffer* pVBs[] = {pVertexBuffer};
130-
pCtx->SetVertexBuffers(0, _countof(pVBs), pVBs, nullptr, RESOURCE_STATE_TRANSITION_MODE_NONE);
131-
}
132-
133121
pCtx->SetPipelineState(pPSO);
122+
123+
if (PreDraw != nullptr)
124+
PreDraw();
125+
134126
pCtx->Draw({6, DRAW_FLAG_VERIFY_ALL});
135127

136128
if (UseRenderPass)
@@ -925,14 +917,24 @@ void TestPipelineReload(bool UseRenderPass)
925917

926918
constexpr auto HotReload = true;
927919

928-
RefCntAutoPtr<IBuffer> pVertBuff;
920+
ReferenceTextures RefTextures{
921+
4,
922+
128, 128,
923+
USAGE_DEFAULT,
924+
BIND_SHADER_RESOURCE,
925+
TEXTURE_VIEW_SHADER_RESOURCE //
926+
};
927+
929928
{
930-
struct Vertex
931-
{
932-
float4 Pos;
933-
float3 Color;
934-
};
929+
RefCntAutoPtr<ISampler> pSampler;
930+
pDevice->CreateSampler(SamplerDesc{}, &pSampler);
931+
RefTextures.GetView(1)->SetSampler(pSampler);
932+
RefTextures.GetView(3)->SetSampler(pSampler);
933+
}
935934

935+
RefCntAutoPtr<IBuffer> pVertBuff;
936+
RefCntAutoPtr<IBuffer> pConstBuff;
937+
{
936938
constexpr float4 Pos[] =
937939
{
938940
float4{-1.0, -0.5, 0.0, 1.0},
@@ -944,30 +946,33 @@ void TestPipelineReload(bool UseRenderPass)
944946
float4{+1.0, -0.5, 0.0, 1.0},
945947
};
946948

947-
constexpr float3 Color[] =
949+
const float4 Color[] =
948950
{
949-
float3{1.0, 0.0, 0.0},
950-
float3{0.0, 1.0, 0.0},
951-
float3{0.0, 0.0, 1.0},
952-
};
953-
954-
constexpr Vertex Vert[] =
955-
{
956-
{Pos[0], Color[0]},
957-
{Pos[1], Color[1]},
958-
{Pos[2], Color[2]},
959-
960-
{Pos[3], Color[0]},
961-
{Pos[4], Color[1]},
962-
{Pos[5], Color[2]},
951+
float4{1.0, 0.0, 0.0, 1.0},
952+
float4{0.0, 1.0, 0.0, 1.0},
953+
float4{0.0, 0.0, 1.0, 1.0},
954+
RefTextures.GetColor(0),
955+
RefTextures.GetColor(1),
956+
RefTextures.GetColor(2),
957+
RefTextures.GetColor(3),
963958
};
964959

965960
RenderDeviceX<> Device{pDevice};
966-
pVertBuff = Device.CreateBuffer("Vert buffer", sizeof(Vert), USAGE_DEFAULT, BIND_VERTEX_BUFFER, CPU_ACCESS_NONE, Vert);
961+
pVertBuff = Device.CreateBuffer("Pos buffer", sizeof(Pos), USAGE_DEFAULT, BIND_VERTEX_BUFFER, CPU_ACCESS_NONE, Pos);
962+
ASSERT_NE(pVertBuff, nullptr);
963+
964+
pConstBuff = Device.CreateBuffer("Color buffer", sizeof(Color), USAGE_DEFAULT, BIND_UNIFORM_BUFFER, CPU_ACCESS_NONE, Color);
967965
ASSERT_NE(pVertBuff, nullptr);
968966

969-
StateTransitionDesc Barrier{pVertBuff, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_VERTEX_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE};
970-
pCtx->TransitionResourceStates(1, &Barrier);
967+
StateTransitionDesc Barriers[] = {
968+
{pVertBuff, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_VERTEX_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE},
969+
{pConstBuff, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE},
970+
{RefTextures.GetView(0)->GetTexture(), RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, STATE_TRANSITION_FLAG_UPDATE_STATE},
971+
{RefTextures.GetView(1)->GetTexture(), RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, STATE_TRANSITION_FLAG_UPDATE_STATE},
972+
{RefTextures.GetView(2)->GetTexture(), RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, STATE_TRANSITION_FLAG_UPDATE_STATE},
973+
{RefTextures.GetView(3)->GetTexture(), RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, STATE_TRANSITION_FLAG_UPDATE_STATE},
974+
};
975+
pCtx->TransitionResourceStates(_countof(Barriers), Barriers);
971976
}
972977

973978
RefCntAutoPtr<IDataBlob> pData;
@@ -999,11 +1004,22 @@ void TestPipelineReload(bool UseRenderPass)
9991004

10001005
InputLayoutDescX InputLayout{
10011006
LayoutElement{0, 0, 4, VT_FLOAT32},
1002-
LayoutElement{1, 0, 3, VT_FLOAT32},
10031007
};
1004-
10051008
GraphicsPipeline.InputLayout = InputLayout;
10061009

1010+
PipelineResourceLayoutDescX ResLayout{
1011+
{
1012+
{SHADER_TYPE_VERTEX | SHADER_TYPE_PIXEL, "Colors", SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
1013+
{SHADER_TYPE_PIXEL, "g_Tex2D_Static1", SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
1014+
{SHADER_TYPE_PIXEL, "g_Tex2D_Mut", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE},
1015+
{SHADER_TYPE_PIXEL, "g_Tex2D_Dyn", SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC},
1016+
},
1017+
{
1018+
{SHADER_TYPE_PIXEL, "g_Tex2D_Static0", SamplerDesc{}},
1019+
{SHADER_TYPE_PIXEL, "g_Tex2D_Mut", SamplerDesc{}},
1020+
}};
1021+
PsoCI.PSODesc.ResourceLayout = ResLayout;
1022+
10071023
const auto ColorBufferFormat = pSwapChain->GetDesc().ColorBufferFormat;
10081024

10091025
RefCntAutoPtr<IRenderPass> pRenderPass;
@@ -1024,6 +1040,8 @@ void TestPipelineReload(bool UseRenderPass)
10241040
EXPECT_EQ(pCache->CreateGraphicsPipelineState(PsoCI, &pPSO), pData != nullptr);
10251041
}
10261042
ASSERT_NE(pPSO, nullptr);
1043+
pPSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "Colors")->Set(pConstBuff);
1044+
pPSO->GetStaticVariableByName(SHADER_TYPE_PIXEL, "g_Tex2D_Static0")->Set(RefTextures.GetView(0));
10271045

10281046
auto ModifyPSO = MakeCallback(
10291047
[&](PipelineStateCreateInfo& CreateInfo) {
@@ -1035,7 +1053,20 @@ void TestPipelineReload(bool UseRenderPass)
10351053

10361054
EXPECT_EQ(pCache->Reload(ModifyPSO, ModifyPSO), pass == 0 ? 3u : 0u);
10371055

1038-
TestDraw(nullptr, nullptr, pPSO, UseRenderPass, pVertBuff);
1056+
pPSO->GetStaticVariableByName(SHADER_TYPE_PIXEL, "g_Tex2D_Static1")->Set(RefTextures.GetView(1));
1057+
1058+
RefCntAutoPtr<IShaderResourceBinding> pSRB;
1059+
pPSO->CreateShaderResourceBinding(&pSRB, true);
1060+
1061+
pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_Tex2D_Mut")->Set(RefTextures.GetView(2));
1062+
pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_Tex2D_Dyn")->Set(RefTextures.GetView(3));
1063+
1064+
TestDraw(nullptr, nullptr, pPSO, UseRenderPass,
1065+
[&]() {
1066+
IBuffer* pVBs[] = {pVertBuff};
1067+
pCtx->SetVertexBuffers(0, _countof(pVBs), pVBs, nullptr, RESOURCE_STATE_TRANSITION_MODE_NONE);
1068+
pCtx->CommitShaderResources(pSRB, RESOURCE_STATE_TRANSITION_MODE_NONE);
1069+
});
10391070

10401071
pData.Release();
10411072
pCache->WriteToBlob(&pData);

0 commit comments

Comments
 (0)