Skip to content

Commit 73be86c

Browse files
Inline Constants test: use constants in PS (#672)
1 parent aeeac03 commit 73be86c

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

Tests/DiligentCoreAPITest/src/InlineConstantsTest.cpp

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ void RenderDrawCommandReference(ISwapChain* pSwapChain, const float* pClearColor
4545
using namespace Diligent;
4646
using namespace Diligent::Testing;
4747

48-
#include "InlineShaders/DrawCommandTestHLSL.h"
49-
50-
5148
namespace
5249
{
5350

@@ -80,8 +77,31 @@ void main(uint VertexId : SV_VertexId,
8077
}
8178
)"};
8279

80+
const std::string InlineConstantsTest_PS{
81+
R"(
82+
struct PSInput
83+
{
84+
float4 Pos : SV_POSITION;
85+
float3 Color : COLOR;
86+
};
87+
88+
cbuffer cbInlineColors
89+
{
90+
float4 g_Colors[3];
8391
}
8492
93+
float4 main(in PSInput PSIn) : SV_Target
94+
{
95+
float3 Color = PSIn.Color.rgb;
96+
Color.r *= g_Colors[0].a;
97+
Color.g *= g_Colors[1].a;
98+
Color.b *= g_Colors[2].a;
99+
return float4(Color, 1.0);
100+
}
101+
)"};
102+
103+
} // namespace HLSL
104+
85105
float4 g_Positions[] = {
86106
float4{-1.0f, -0.5f, 0.f, 1.f},
87107
float4{-0.5f, +0.5f, 0.f, 1.f},
@@ -109,6 +129,11 @@ class InlineConstants : public ::testing::Test
109129
GPUTestingEnvironment* pEnv = GPUTestingEnvironment::GetInstance();
110130
IRenderDevice* pDevice = pEnv->GetDevice();
111131

132+
if (pDevice->GetDeviceInfo().Type != RENDER_DEVICE_TYPE_D3D12)
133+
{
134+
GTEST_SKIP();
135+
}
136+
112137
ShaderCreateInfo ShaderCI;
113138
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
114139
ShaderCI.ShaderCompiler = pEnv->GetDefaultCompiler(ShaderCI.SourceLanguage);
@@ -124,7 +149,7 @@ class InlineConstants : public ::testing::Test
124149
{
125150
ShaderCI.Desc = {"Inline constants test", SHADER_TYPE_PIXEL, true};
126151
ShaderCI.EntryPoint = "main";
127-
ShaderCI.Source = HLSL::DrawTest_PS.c_str();
152+
ShaderCI.Source = HLSL::InlineConstantsTest_PS.c_str();
128153
pDevice->CreateShader(ShaderCI, &sm_Res.pPS);
129154
ASSERT_NE(sm_Res.pPS, nullptr);
130155
}
@@ -175,10 +200,6 @@ TEST_F(InlineConstants, ResourceLayout)
175200
IRenderDevice* pDevice = pEnv->GetDevice();
176201
IDeviceContext* pContext = pEnv->GetDeviceContext();
177202
ISwapChain* pSwapChain = pEnv->GetSwapChain();
178-
if (pDevice->GetDeviceInfo().Type != RENDER_DEVICE_TYPE_D3D12)
179-
{
180-
GTEST_SKIP();
181-
}
182203

183204
for (Uint32 pos_type = 0; pos_type < SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES; ++pos_type)
184205
{
@@ -195,7 +216,7 @@ TEST_F(InlineConstants, ResourceLayout)
195216
PipelineResourceLayoutDescX ResLayoutDesc;
196217
ResLayoutDesc
197218
.AddVariable(SHADER_TYPE_VERTEX, "cbInlinePositions", PosType, SHADER_VARIABLE_FLAG_INLINE_CONSTANTS)
198-
.AddVariable(SHADER_TYPE_VERTEX, "cbInlineColors", ColType, SHADER_VARIABLE_FLAG_INLINE_CONSTANTS);
219+
.AddVariable(SHADER_TYPE_VS_PS, "cbInlineColors", ColType, SHADER_VARIABLE_FLAG_INLINE_CONSTANTS);
199220

200221
PsoCI
201222
.AddRenderTarget(pSwapChain->GetDesc().ColorBufferFormat)
@@ -234,30 +255,33 @@ TEST_F(InlineConstants, ResourceLayout)
234255
ASSERT_TRUE(pPosVar);
235256
}
236257

237-
IShaderResourceVariable* pColVar = nullptr;
258+
IShaderResourceVariable* pColVarVS = nullptr;
259+
IShaderResourceVariable* pColVarPS = nullptr;
238260
if (ColType != SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
239261
{
240-
pColVar = pSRB->GetVariableByName(SHADER_TYPE_VERTEX, "cbInlineColors");
241-
ASSERT_TRUE(pColVar);
262+
pColVarVS = pSRB->GetVariableByName(SHADER_TYPE_VERTEX, "cbInlineColors");
263+
ASSERT_TRUE(pColVarVS);
264+
pColVarPS = pSRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbInlineColors");
265+
ASSERT_TRUE(pColVarPS);
242266
}
243267

244268
ITextureView* pRTVs[] = {pSwapChain->GetCurrentBackBufferRTV()};
245269
pContext->SetRenderTargets(1, pRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
246270
pContext->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
247271

248272

249-
if (pColVar != nullptr)
273+
if (pColVarVS != nullptr)
250274
{
251275
// Set first half of color constants before committing SRB
252-
pColVar->SetInlineConstants(g_Colors, 0, kNumColConstants / 2);
276+
pColVarVS->SetInlineConstants(g_Colors, 0, kNumColConstants / 2);
253277
}
254278

255279
pContext->CommitShaderResources(pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
256280

257-
if (pColVar != nullptr)
281+
if (pColVarPS != nullptr)
258282
{
259283
// Set second half of color constants after committing SRB
260-
pColVar->SetInlineConstants(g_Colors[0].Data() + kNumColConstants / 2, kNumColConstants / 2, kNumColConstants / 2);
284+
pColVarPS->SetInlineConstants(g_Colors[0].Data() + kNumColConstants / 2, kNumColConstants / 2, kNumColConstants / 2);
261285
}
262286

263287
pContext->SetPipelineState(pPSO);
@@ -294,10 +318,6 @@ void InlineConstants::TestSignatures(Uint32 NumSignatures)
294318
IRenderDevice* pDevice = pEnv->GetDevice();
295319
IDeviceContext* pContext = pEnv->GetDeviceContext();
296320
ISwapChain* pSwapChain = pEnv->GetSwapChain();
297-
if (pDevice->GetDeviceInfo().Type != RENDER_DEVICE_TYPE_D3D12)
298-
{
299-
GTEST_SKIP();
300-
}
301321

302322
RefCntAutoPtr<IBuffer> pConstBuffer = pEnv->CreateBuffer({"InlineConstants - dummy const buffer", 256, BIND_UNIFORM_BUFFER});
303323
ASSERT_TRUE(pConstBuffer);
@@ -347,7 +367,7 @@ void InlineConstants::TestSignatures(Uint32 NumSignatures)
347367
.AddResource(SHADER_TYPE_VERTEX, "tex1_mut", SHADER_RESOURCE_TYPE_TEXTURE_SRV, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE)
348368
.AddResource(SHADER_TYPE_VERTEX, "tex1_dyn", SHADER_RESOURCE_TYPE_TEXTURE_SRV, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC)
349369

350-
.AddResource(SHADER_TYPE_VERTEX, "cbInlineColors", kNumColConstants, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, ColType, PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
370+
.AddResource(SHADER_TYPE_VS_PS, "cbInlineColors", kNumColConstants, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, ColType, PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
351371

352372
.AddResource(SHADER_TYPE_VERTEX, "cb2_stat", 1u, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
353373
.AddResource(SHADER_TYPE_VERTEX, "cb2_mut", 1u, SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE)
@@ -445,22 +465,25 @@ void InlineConstants::TestSignatures(Uint32 NumSignatures)
445465
ASSERT_TRUE(pPosVar);
446466
}
447467

448-
IShaderResourceVariable* pColVar = nullptr;
468+
IShaderResourceVariable* pColVarVS = nullptr;
469+
IShaderResourceVariable* pColVarPS = nullptr;
449470
if (ColType != SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
450471
{
451-
pColVar = pColSRB->GetVariableByName(SHADER_TYPE_VERTEX, "cbInlineColors");
452-
ASSERT_TRUE(pColVar);
472+
pColVarVS = pColSRB->GetVariableByName(SHADER_TYPE_VERTEX, "cbInlineColors");
473+
ASSERT_TRUE(pColVarVS);
474+
pColVarPS = pColSRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbInlineColors");
475+
ASSERT_TRUE(pColVarPS);
453476
}
454477

455478
ITextureView* pRTVs[] = {pSwapChain->GetCurrentBackBufferRTV()};
456479
pContext->SetRenderTargets(1, pRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
457480
pContext->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
458481

459482

460-
if (pColVar != nullptr)
483+
if (pColVarVS != nullptr)
461484
{
462485
// Set first half of color constants before committing SRB
463-
pColVar->SetInlineConstants(g_Colors, 0, kNumColConstants / 2);
486+
pColVarVS->SetInlineConstants(g_Colors, 0, kNumColConstants / 2);
464487
}
465488

466489
pContext->CommitShaderResources(pPosSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
@@ -471,10 +494,10 @@ void InlineConstants::TestSignatures(Uint32 NumSignatures)
471494
pContext->CommitShaderResources(pColSRB, RESOURCE_STATE_TRANSITION_MODE_VERIFY);
472495
}
473496

474-
if (pColVar != nullptr)
497+
if (pColVarPS != nullptr)
475498
{
476499
// Set second half of color constants after committing SRB
477-
pColVar->SetInlineConstants(g_Colors[0].Data() + kNumColConstants / 2, kNumColConstants / 2, kNumColConstants / 2);
500+
pColVarPS->SetInlineConstants(g_Colors[0].Data() + kNumColConstants / 2, kNumColConstants / 2, kNumColConstants / 2);
478501
}
479502

480503
pContext->SetPipelineState(pPSO);
@@ -567,7 +590,7 @@ void CreateShadersFromCache(IRenderStateCache* pCache, bool PresentInCache, ISha
567590
{
568591
ShaderCI.Desc = {"Inline constants test", SHADER_TYPE_PIXEL, true};
569592
ShaderCI.EntryPoint = "main";
570-
ShaderCI.Source = HLSL::DrawTest_PS.c_str();
593+
ShaderCI.Source = HLSL::InlineConstantsTest_PS.c_str();
571594
if (pCache != nullptr)
572595
{
573596
EXPECT_EQ(pCache->CreateShader(ShaderCI, ppPS), PresentInCache);
@@ -599,7 +622,7 @@ void CreatePSOFromCache(IRenderStateCache* pCache, bool PresentInCache, IShader*
599622
static ShaderResourceVariableDesc Vars[] =
600623
{
601624
{SHADER_TYPE_VERTEX, "cbInlinePositions", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_FLAG_INLINE_CONSTANTS},
602-
{SHADER_TYPE_VERTEX, "cbInlineColors", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_FLAG_INLINE_CONSTANTS},
625+
{SHADER_TYPE_VS_PS, "cbInlineColors", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_FLAG_INLINE_CONSTANTS},
603626
};
604627
PsoCI.PSODesc.ResourceLayout.Variables = Vars;
605628
PsoCI.PSODesc.ResourceLayout.NumVariables = _countof(Vars);
@@ -667,11 +690,6 @@ TEST_F(InlineConstants, RenderStateCache)
667690
GPUTestingEnvironment* pEnv = GPUTestingEnvironment::GetInstance();
668691
IRenderDevice* pDevice = pEnv->GetDevice();
669692

670-
if (pDevice->GetDeviceInfo().Type != RENDER_DEVICE_TYPE_D3D12)
671-
{
672-
GTEST_SKIP();
673-
}
674-
675693
GPUTestingEnvironment::ScopedReset AutoReset;
676694

677695
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceFactory;

0 commit comments

Comments
 (0)