Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tools/clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,17 @@ def HLSLHitObject : InheritableAttr {
let Documentation = [Undocumented];
}

// HLSL Dynamic Resource Attribute
// Marks the builtin `.Resource` / `.Sampler` placeholder record types used
// for descriptor heap indexing (ResourceDescriptorHeap[i] /
// SamplerDescriptorHeap[i]).
def HLSLDynamicResource : InheritableAttr {
let Spellings = []; // No spellings!
let Args = [BoolArgument<"IsSampler">];
let Subjects = SubjectList<[CXXRecord]>;
let Documentation = [Undocumented];
}

// HLSL Parameter Attributes

def HLSLMaxRecords : InheritableAttr {
Expand Down
4 changes: 4 additions & 0 deletions tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -8066,6 +8066,10 @@ def err_hlsl_linalg_unsupported_stage : Error<
def err_hlsl_pointers_unsupported : Error<
"%select{pointers|references}0 are unsupported in HLSL">;

def err_hlsl_auto_descriptor_heap : Error<
"'auto' cannot deduce the type of '%select{ResourceDescriptorHeap|SamplerDescriptorHeap}0'; "
"use a specific resource or sampler type instead">;

// HLSL Change Ends

// SPIRV Change Starts
Expand Down
2 changes: 2 additions & 0 deletions tools/clang/lib/AST/ASTContextHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,8 @@ CXXRecordDecl *hlsl::DeclareResourceType(ASTContext &context, bool bSampler) {
typeDeclBuilder.addField("h", GetHLSLObjectHandleType(context));

CXXRecordDecl *recordDecl = typeDeclBuilder.getRecordDecl();
recordDecl->addAttr(
HLSLDynamicResourceAttr::CreateImplicit(context, bSampler));

QualType indexType = context.UnsignedIntTy;
QualType resultType = context.getRecordType(recordDecl);
Expand Down
14 changes: 6 additions & 8 deletions tools/clang/lib/AST/HlslTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,18 +544,16 @@ bool IsHLSLNodeInputType(clang::QualType type) {
}

bool IsHLSLDynamicResourceType(clang::QualType type) {
if (const RecordType *RT = type->getAs<RecordType>()) {
StringRef name = RT->getDecl()->getName();
return name == ".Resource";
}
if (const HLSLDynamicResourceAttr *Attr =
getAttr<HLSLDynamicResourceAttr>(type))
return !Attr->getIsSampler();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: I suppose we could avoid a redundant attribute lookup in IsDynamicHeapInitializer by exposing a function IsHLSLDescriptorHeapType that takes a bool &IsSampler. These functions could use that, and so could IsDynamicHeapInitializer.

return false;
}

bool IsHLSLDynamicSamplerType(clang::QualType type) {
if (const RecordType *RT = type->getAs<RecordType>()) {
StringRef name = RT->getDecl()->getName();
return name == ".Sampler";
}
if (const HLSLDynamicResourceAttr *Attr =
getAttr<HLSLDynamicResourceAttr>(type))
return Attr->getIsSampler();
return false;
}

Expand Down
27 changes: 27 additions & 0 deletions tools/clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "TypeLocBuilder.h"
#include "dxc/DXIL/DxilSemantic.h" // HLSL Change
#include "dxc/HlslIntrinsicOp.h" // HLSL Change
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTLambda.h"
Expand Down Expand Up @@ -6417,6 +6418,21 @@ static bool checkForConflictWithNonVisibleExternC(Sema &S, const T *ND,
return false;
}

static bool IsDynamicHeapInitializer(const Expr *Init, bool &IsSampler) {
if (!Init)
return false;
QualType T = Init->IgnoreParenImpCasts()->getType();
if (hlsl::IsHLSLDynamicSamplerType(T)) {
IsSampler = true;
return true;
}
if (hlsl::IsHLSLDynamicResourceType(T)) {
IsSampler = false;
return true;
}
return false;
}

void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
// If the decl is already known invalid, don't check it.
if (NewVD->isInvalidDecl())
Expand Down Expand Up @@ -9026,6 +9042,17 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
RealDecl->setInvalidDecl();
return;
}

if (getLangOpts().HLSL) {
bool IsSampler = false;
if (IsDynamicHeapInitializer(DeduceInit, IsSampler)) {
Diag(VDecl->getLocation(), diag::err_hlsl_auto_descriptor_heap)
<< IsSampler;
VDecl->setInvalidDecl();
return;
}
}

VDecl->setType(DeducedType);
assert(VDecl->isLinkageValid());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %dxc -T ps_6_6 -ast-dump-implicit %s | FileCheck %s


float4 main(float4 pos : SV_Position) : SV_Target {
Texture2D<float4> tex = ResourceDescriptorHeap[0];
SamplerState samp = SamplerDescriptorHeap[0];
return tex.Sample(samp, pos.xy);
}

// CHECK: CXXRecordDecl {{0x[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> implicit struct .Resource definition
// CHECK: HLSLDynamicResourceAttr {{0x[0-9a-fA-F]+}} <<invalid sloc>> Implicit

// CHECK: CXXRecordDecl {{0x[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> implicit struct .Sampler definition
// CHECK: HLSLDynamicResourceAttr {{0x[0-9a-fA-F]+}} <<invalid sloc>> Implicit IsSampler
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %dxc -T ps_6_6 -HV 202x -verify %s


int someFunc() { return 7; }

float4 main(float4 pos : SV_Position) : SV_Target {
// expected-error@+1 {{'auto' cannot deduce the type of 'ResourceDescriptorHeap'}}
auto tex = ResourceDescriptorHeap[0];
// expected-error@+1 {{'auto' cannot deduce the type of 'SamplerDescriptorHeap'}}
auto samp = SamplerDescriptorHeap[0];

// expected-error@+1 {{'auto' cannot deduce the type of 'ResourceDescriptorHeap'}}
auto heap = ResourceDescriptorHeap;
// expected-error@+1 {{'auto' cannot deduce the type of 'SamplerDescriptorHeap'}}
auto sheap = SamplerDescriptorHeap;

// expected-error@+1 {{'auto' cannot deduce the type of 'SamplerDescriptorHeap'}}
auto pSamp = (SamplerDescriptorHeap[0]);
// expected-error@+1 {{'auto' cannot deduce the type of 'ResourceDescriptorHeap'}}
auto pHeap = (ResourceDescriptorHeap);

// Negative case
Texture2D<float4> tex2 = ResourceDescriptorHeap[1];
auto sampled = tex2.Sample((SamplerState)SamplerDescriptorHeap[0], pos.xy);

return float4(0, 0, 0, 0);
}
Loading