Skip to content

Commit 7c6a6bc

Browse files
authored
Only define global heap variables in 6.6+ (microsoft#4011)
RsourceDescriptorHeap and SamplerDescriptorHeap were predefined in all shader models, not just 6.6+. By checking the shader model version before declaring them, we can limit this to relevant shader models. Added tests for 6.6 and 6.5 behavior when these are redefined
1 parent ebdf8fb commit 7c6a6bc

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,6 +3508,8 @@ class HLSLExternalSource : public ExternalSemaSource {
35083508
TypeSourceInfo *float4TypeSourceInfo = m_context->getTrivialTypeSourceInfo(float4Type, NoLoc);
35093509
m_objectTypeLazyInitMask = 0;
35103510
unsigned effectKindIndex = 0;
3511+
const auto *SM =
3512+
hlsl::ShaderModel::GetByName(m_sema->getLangOpts().HLSLProfile.c_str());
35113513
for (unsigned i = 0; i < _countof(g_ArBasicKindsAsTypes); i++)
35123514
{
35133515
ArBasicKind kind = g_ArBasicKindsAsTypes[i];
@@ -3566,14 +3568,18 @@ class HLSLExternalSource : public ExternalSemaSource {
35663568
recordDecl = DeclareRayQueryType(*m_context);
35673569
} else if (kind == AR_OBJECT_HEAP_RESOURCE) {
35683570
recordDecl = DeclareResourceType(*m_context, /*bSampler*/false);
3569-
// create Resource ResourceDescriptorHeap;
3570-
DeclareBuiltinGlobal("ResourceDescriptorHeap",
3571-
m_context->getRecordType(recordDecl), *m_context);
3571+
if (SM->IsSM66Plus()) {
3572+
// create Resource ResourceDescriptorHeap;
3573+
DeclareBuiltinGlobal("ResourceDescriptorHeap",
3574+
m_context->getRecordType(recordDecl), *m_context);
3575+
}
35723576
} else if (kind == AR_OBJECT_HEAP_SAMPLER) {
35733577
recordDecl = DeclareResourceType(*m_context, /*bSampler*/true);
3574-
// create Resource SamplerDescriptorHeap;
3575-
DeclareBuiltinGlobal("SamplerDescriptorHeap",
3576-
m_context->getRecordType(recordDecl), *m_context);
3578+
if (SM->IsSM66Plus()) {
3579+
// create Resource SamplerDescriptorHeap;
3580+
DeclareBuiltinGlobal("SamplerDescriptorHeap",
3581+
m_context->getRecordType(recordDecl), *m_context);
3582+
}
35773583

35783584
}
35793585
else if (kind == AR_OBJECT_FEEDBACKTEXTURE2D) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %dxc -T ps_6_5 %s | %FileCheck %s
2+
// RUN: %dxc -T ps_6_6 %s | %FileCheck %s -check-prefix=FAIL
3+
4+
// Make sure that global heap variable redeclaration fails
5+
// on 6.6 and passes on previous shader models
6+
7+
// FAIL: error: redefinition of 'ResourceDescriptorHeap' with a different type: 'Texture2D<float> []' vs '.Resource'
8+
// FAIL: error: redefinition of 'SamplerDescriptorHeap' with a different type: 'SamplerState []' vs '.Sampler'
9+
10+
// Verify that feature bits aren't set and the shader produce roughly reasonable code
11+
// CHECK-NOT: Resource descriptor heap indexing
12+
// CHECK-NOT: Sampler descriptor heap indexing
13+
// CHECK: @main
14+
// CHECK: @dx.op.sample.f32
15+
16+
Texture2D<float> ResourceDescriptorHeap[] : register(t0);
17+
SamplerState SamplerDescriptorHeap[] : register(s0);
18+
19+
uint ID;
20+
float main(float4 pos: SV_Position): SV_Target {
21+
Texture2D<float> tex = ResourceDescriptorHeap[ID];
22+
SamplerState samp = SamplerDescriptorHeap[ID];
23+
return tex.Sample(samp, pos.xy);
24+
}
25+

0 commit comments

Comments
 (0)