Skip to content

Commit 0df2df2

Browse files
Render state cache: improved reload test
1 parent c3d9c08 commit 0df2df2

File tree

5 files changed

+158
-56
lines changed

5 files changed

+158
-56
lines changed

Graphics/GraphicsEngine/interface/DeviceContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@ DILIGENT_BEGIN_INTERFACE(IDeviceContext, IObject)
21882188
IBuffer** ppBuffers,
21892189
const Uint64* pOffsets,
21902190
RESOURCE_STATE_TRANSITION_MODE StateTransitionMode,
2191-
SET_VERTEX_BUFFERS_FLAGS Flags) PURE;
2191+
SET_VERTEX_BUFFERS_FLAGS Flags DEFAULT_VALUE(SET_VERTEX_BUFFERS_FLAG_NONE)) PURE;
21922192

21932193

21942194
/// Invalidates the cached context state.

Graphics/GraphicsEngine/interface/GraphicsTypesX.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ class RenderDeviceX
12411241
USAGE Usage = USAGE_DYNAMIC,
12421242
BIND_FLAGS BindFlags = BIND_UNIFORM_BUFFER,
12431243
CPU_ACCESS_FLAGS CPUAccessFlags = CPU_ACCESS_NONE,
1244-
void* pData = nullptr) noexcept(!ThrowOnError)
1244+
const void* pData = nullptr) noexcept(!ThrowOnError)
12451245
{
12461246
BufferDesc Desc;
12471247
Desc.Name = Name;
Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
#include "GraphicsCommon.h"
22

3+
struct VSInput
4+
{
5+
float4 Pos : ATTRIB0;
6+
float3 Color : ATTRIB1;
7+
};
8+
39
#if INTERNAL_MACROS == 1 && EXTERNAL_MACROS == 2
4-
void main(in uint VertId : SV_VertexID,
10+
void main(in VSInput VSIn,
511
out PSInput PSIn)
612
{
7-
float4 Pos[6];
8-
Pos[0] = float4(-1.0, -0.5, 0.0, 1.0);
9-
Pos[1] = float4(-0.5, +0.5, 0.0, 1.0);
10-
Pos[2] = float4( 0.0, -0.5, 0.0, 1.0);
11-
12-
Pos[3] = float4(+0.0, -0.5, 0.0, 1.0);
13-
Pos[4] = float4(+0.5, +0.5, 0.0, 1.0);
14-
Pos[5] = float4(+1.0, -0.5, 0.0, 1.0);
15-
16-
float3 Col[6];
17-
Col[0] = float3(1.0, 0.0, 0.0);
18-
Col[1] = float3(0.0, 1.0, 0.0);
19-
Col[2] = float3(0.0, 0.0, 1.0);
20-
21-
Col[3] = float3(1.0, 0.0, 0.0);
22-
Col[4] = float3(0.0, 1.0, 0.0);
23-
Col[5] = float3(0.0, 0.0, 1.0);
24-
25-
PSIn.Pos = Pos[VertId];
26-
PSIn.Color = Col[VertId];
13+
PSIn.Pos = VSIn.Pos;
14+
PSIn.Color = VSIn.Color;
2715
}
2816
// NB: no new line at the end of file!
2917
#endif
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#include "GraphicsCommon.h"
22

3+
struct VSInput
4+
{
5+
float4 Pos : ATTRIB0;
6+
float3 Color : ATTRIB1;
7+
};
8+
39
#if INTERNAL_MACROS == 1 && EXTERNAL_MACROS == 2
4-
void main(in uint VertId : SV_VertexID,
10+
void main(in VSInput VSIn,
511
out PSInput PSIn)
612
{
7-
PSIn.Pos = float4(0.0, 0.0, 0.0, 0.0);
8-
PSIn.Color = float3(0.0, 0.0, 0.0);
13+
PSIn.Pos = float4(0.0, 0.0, 0.0, VSIn.Pos.x);
14+
PSIn.Color = float3(0.0, 0.0, VSIn.Color.r);
915
}
1016
// NB: no new line at the end of file!
1117
#endif

Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp

Lines changed: 138 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "RenderStateCache.h"
3030
#include "RenderStateCache.hpp"
3131
#include "FastRand.hpp"
32+
#include "GraphicsTypesX.hpp"
33+
#include "CallbackWrapper.hpp"
3234

3335
#include "InlineShaders/RayTracingTestHLSL.h"
3436

@@ -49,7 +51,7 @@ using namespace Diligent::Testing;
4951
namespace
5052
{
5153

52-
void TestDraw(IShader* pVS, IShader* pPS, IPipelineState* pPSO, bool UseRenderPass)
54+
void TestDraw(IShader* pVS, IShader* pPS, IPipelineState* pPSO, bool UseRenderPass, IBuffer* pVertexBuffer = nullptr)
5355
{
5456
VERIFY_EXPR((pVS != nullptr && pPS != nullptr) ^ (pPSO != nullptr));
5557

@@ -122,6 +124,12 @@ void TestDraw(IShader* pVS, IShader* pPS, IPipelineState* pPSO, bool UseRenderPa
122124
pCtx->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
123125
}
124126

127+
if (pVertexBuffer != nullptr)
128+
{
129+
IBuffer* pVBs[] = {pVertexBuffer};
130+
pCtx->SetVertexBuffers(0, _countof(pVBs), pVBs, nullptr, RESOURCE_STATE_TRANSITION_MODE_NONE);
131+
}
132+
125133
pCtx->SetPipelineState(pPSO);
126134
pCtx->Draw({6, DRAW_FLAG_VERIFY_ALL});
127135

@@ -354,10 +362,41 @@ TEST(RenderStateCacheTest, BrokenShader)
354362
}
355363
}
356364

365+
RefCntAutoPtr<IRenderPass> CreateRenderPass(TEXTURE_FORMAT ColorBufferFormat)
366+
{
367+
auto* pEnv = GPUTestingEnvironment::GetInstance();
368+
auto* pDevice = pEnv->GetDevice();
369+
370+
RenderPassAttachmentDesc Attachments[1];
371+
Attachments[0].Format = ColorBufferFormat;
372+
Attachments[0].InitialState = RESOURCE_STATE_RENDER_TARGET;
373+
Attachments[0].FinalState = RESOURCE_STATE_RENDER_TARGET;
374+
Attachments[0].LoadOp = ATTACHMENT_LOAD_OP_CLEAR;
375+
Attachments[0].StoreOp = ATTACHMENT_STORE_OP_STORE;
376+
377+
constexpr AttachmentReference RTAttachmentRefs0[] =
378+
{
379+
{0, RESOURCE_STATE_RENDER_TARGET},
380+
};
381+
SubpassDesc Subpasses[1];
382+
Subpasses[0].RenderTargetAttachmentCount = _countof(RTAttachmentRefs0);
383+
Subpasses[0].pRenderTargetAttachments = RTAttachmentRefs0;
384+
385+
RenderPassDesc RPDesc;
386+
RPDesc.Name = "Render State Cache Test";
387+
RPDesc.AttachmentCount = _countof(Attachments);
388+
RPDesc.pAttachments = Attachments;
389+
RPDesc.SubpassCount = _countof(Subpasses);
390+
RPDesc.pSubpasses = Subpasses;
391+
392+
RefCntAutoPtr<IRenderPass> pRenderPass;
393+
pDevice->CreateRenderPass(RPDesc, &pRenderPass);
394+
return pRenderPass;
395+
}
396+
357397
void CreateGraphicsPSO(IRenderStateCache* pCache, bool PresentInCache, IShader* pVS, IShader* pPS, bool UseRenderPass, IPipelineState** ppPSO)
358398
{
359399
auto* pEnv = GPUTestingEnvironment::GetInstance();
360-
auto* pDevice = pEnv->GetDevice();
361400
auto* pSwapChain = pEnv->GetSwapChain();
362401

363402
GraphicsPipelineStateCreateInfo PsoCI;
@@ -371,29 +410,7 @@ void CreateGraphicsPSO(IRenderStateCache* pCache, bool PresentInCache, IShader*
371410
RefCntAutoPtr<IRenderPass> pRenderPass;
372411
if (UseRenderPass)
373412
{
374-
RenderPassAttachmentDesc Attachments[1];
375-
Attachments[0].Format = ColorBufferFormat;
376-
Attachments[0].InitialState = RESOURCE_STATE_RENDER_TARGET;
377-
Attachments[0].FinalState = RESOURCE_STATE_RENDER_TARGET;
378-
Attachments[0].LoadOp = ATTACHMENT_LOAD_OP_CLEAR;
379-
Attachments[0].StoreOp = ATTACHMENT_STORE_OP_STORE;
380-
381-
constexpr AttachmentReference RTAttachmentRefs0[] =
382-
{
383-
{0, RESOURCE_STATE_RENDER_TARGET},
384-
};
385-
SubpassDesc Subpasses[1];
386-
Subpasses[0].RenderTargetAttachmentCount = _countof(RTAttachmentRefs0);
387-
Subpasses[0].pRenderTargetAttachments = RTAttachmentRefs0;
388-
389-
RenderPassDesc RPDesc;
390-
RPDesc.Name = "Render State Cache Test";
391-
RPDesc.AttachmentCount = _countof(Attachments);
392-
RPDesc.pAttachments = Attachments;
393-
RPDesc.SubpassCount = _countof(Subpasses);
394-
RPDesc.pSubpasses = Subpasses;
395-
396-
pDevice->CreateRenderPass(RPDesc, &pRenderPass);
413+
pRenderPass = CreateRenderPass(ColorBufferFormat);
397414
ASSERT_NE(pRenderPass, nullptr);
398415
PsoCI.GraphicsPipeline.pRenderPass = pRenderPass;
399416
}
@@ -891,8 +908,10 @@ TEST(RenderStateCacheTest, RenderDeviceWithCache)
891908

892909
void TestPipelineReload(bool UseRenderPass)
893910
{
894-
auto* pEnv = GPUTestingEnvironment::GetInstance();
895-
auto* pDevice = pEnv->GetDevice();
911+
auto* pEnv = GPUTestingEnvironment::GetInstance();
912+
auto* pDevice = pEnv->GetDevice();
913+
auto* pCtx = pEnv->GetDeviceContext();
914+
auto* pSwapChain = pEnv->GetSwapChain();
896915

897916
GPUTestingEnvironment::ScopedReset AutoReset;
898917

@@ -906,6 +925,51 @@ void TestPipelineReload(bool UseRenderPass)
906925

907926
constexpr auto HotReload = true;
908927

928+
RefCntAutoPtr<IBuffer> pVertBuff;
929+
{
930+
struct Vertex
931+
{
932+
float4 Pos;
933+
float3 Color;
934+
};
935+
936+
constexpr float4 Pos[] =
937+
{
938+
float4{-1.0, -0.5, 0.0, 1.0},
939+
float4{-0.5, +0.5, 0.0, 1.0},
940+
float4{+0.0, -0.5, 0.0, 1.0},
941+
942+
float4{+0.0, -0.5, 0.0, 1.0},
943+
float4{+0.5, +0.5, 0.0, 1.0},
944+
float4{+1.0, -0.5, 0.0, 1.0},
945+
};
946+
947+
constexpr float3 Color[] =
948+
{
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]},
963+
};
964+
965+
RenderDeviceX<> Device{pDevice};
966+
pVertBuff = Device.CreateBuffer("Vert buffer", sizeof(Vert), USAGE_DEFAULT, BIND_VERTEX_BUFFER, CPU_ACCESS_NONE, Vert);
967+
ASSERT_NE(pVertBuff, nullptr);
968+
969+
StateTransitionDesc Barrier{pVertBuff, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_VERTEX_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE};
970+
pCtx->TransitionResourceStates(1, &Barrier);
971+
}
972+
909973
RefCntAutoPtr<IDataBlob> pData;
910974
for (Uint32 pass = 0; pass < 3; ++pass)
911975
{
@@ -921,13 +985,57 @@ void TestPipelineReload(bool UseRenderPass)
921985
ASSERT_NE(pVS, nullptr);
922986
ASSERT_NE(pPS, nullptr);
923987

988+
constexpr char PSOName[] = "Render State Cache Reload Test";
989+
924990
RefCntAutoPtr<IPipelineState> pPSO;
925-
CreateGraphicsPSO(pCache, pData != nullptr, pVS, pPS, UseRenderPass, &pPSO);
991+
{
992+
GraphicsPipelineStateCreateInfo PsoCI;
993+
PsoCI.PSODesc.Name = PSOName;
994+
995+
auto& GraphicsPipeline{PsoCI.GraphicsPipeline};
996+
GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
997+
GraphicsPipeline.RasterizerDesc.CullMode = CULL_MODE_NONE;
998+
GraphicsPipeline.DepthStencilDesc.DepthEnable = False;
999+
1000+
InputLayoutDescX InputLayout{
1001+
LayoutElement{0, 0, 4, VT_FLOAT32},
1002+
LayoutElement{1, 0, 3, VT_FLOAT32},
1003+
};
1004+
1005+
GraphicsPipeline.InputLayout = InputLayout;
1006+
1007+
const auto ColorBufferFormat = pSwapChain->GetDesc().ColorBufferFormat;
1008+
1009+
RefCntAutoPtr<IRenderPass> pRenderPass;
1010+
if (UseRenderPass)
1011+
{
1012+
pRenderPass = CreateRenderPass(ColorBufferFormat);
1013+
ASSERT_NE(pRenderPass, nullptr);
1014+
PsoCI.GraphicsPipeline.pRenderPass = pRenderPass;
1015+
}
1016+
else
1017+
{
1018+
PsoCI.GraphicsPipeline.NumRenderTargets = 1;
1019+
PsoCI.GraphicsPipeline.RTVFormats[0] = ColorBufferFormat;
1020+
}
1021+
PsoCI.pVS = pVS;
1022+
PsoCI.pPS = pPS;
1023+
1024+
EXPECT_EQ(pCache->CreateGraphicsPipelineState(PsoCI, &pPSO), pData != nullptr);
1025+
}
9261026
ASSERT_NE(pPSO, nullptr);
9271027

928-
EXPECT_EQ(pCache->Reload(), pass == 0 ? 3u : 0u);
1028+
auto ModifyPSO = MakeCallback(
1029+
[&](PipelineStateCreateInfo& CreateInfo) {
1030+
ASSERT_STREQ(CreateInfo.PSODesc.Name, PSOName);
1031+
auto& GraphicsPsoCI = static_cast<GraphicsPipelineStateCreateInfo&>(CreateInfo);
1032+
1033+
GraphicsPsoCI.GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
1034+
});
1035+
1036+
EXPECT_EQ(pCache->Reload(ModifyPSO, ModifyPSO), pass == 0 ? 3u : 0u);
9291037

930-
VerifyGraphicsPSO(pPSO, UseRenderPass);
1038+
TestDraw(nullptr, nullptr, pPSO, UseRenderPass, pVertBuff);
9311039

9321040
pData.Release();
9331041
pCache->WriteToBlob(&pData);

0 commit comments

Comments
 (0)