diff --git a/tools/clang/include/clang/AST/HlslTypes.h b/tools/clang/include/clang/AST/HlslTypes.h index 4b0e93fdba..77aabdeb61 100644 --- a/tools/clang/include/clang/AST/HlslTypes.h +++ b/tools/clang/include/clang/AST/HlslTypes.h @@ -480,6 +480,7 @@ bool IsHLSLNodeOutputType(clang::QualType type); DXIL::NodeIOKind GetNodeIOType(clang::QualType type); +bool IsHLSLSamplerType(clang::QualType type); bool IsHLSLStructuredBufferType(clang::QualType type); bool IsHLSLNumericOrAggregateOfNumericType(clang::QualType type); bool IsHLSLNumericUserDefinedType(clang::QualType type); diff --git a/tools/clang/lib/AST/HlslTypes.cpp b/tools/clang/lib/AST/HlslTypes.cpp index 34275a48f1..374c17f32f 100644 --- a/tools/clang/lib/AST/HlslTypes.cpp +++ b/tools/clang/lib/AST/HlslTypes.cpp @@ -709,6 +709,16 @@ bool IsHLSLNodeOutputType(clang::QualType type) { static_cast(DXIL::NodeIOFlags::Output); } +bool IsHLSLSamplerType(clang::QualType type) { + if (const RecordType *RT = type->getAs()) { + StringRef name = RT->getDecl()->getName(); + + if (name == "SamplerState" || name == "SamplerComparisonState") + return true; + } + return false; +} + bool IsHLSLStructuredBufferType(clang::QualType type) { if (const RecordType *RT = type->getAs()) { StringRef name = RT->getDecl()->getName(); diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index 4df32f9a65..0341c2e987 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -9555,11 +9555,16 @@ bool HLSLExternalSource::CanConvert(SourceLocation loc, Expr *sourceExpr, goto lSuccess; } - // Cast from Resource to Object types. - if (SourceInfo.EltKind == AR_OBJECT_HEAP_RESOURCE || - SourceInfo.EltKind == AR_OBJECT_HEAP_SAMPLER) { - // TODO: skip things like PointStream. - if (TargetInfo.ShapeKind == AR_TOBJ_OBJECT) { + if (SourceInfo.EltKind == AR_OBJECT_HEAP_SAMPLER) { + if (TargetInfo.ShapeKind == AR_TOBJ_OBJECT && + hlsl::IsHLSLSamplerType(target)) { + Second = ICK_Flat_Conversion; + goto lSuccess; + } + } + if (SourceInfo.EltKind == AR_OBJECT_HEAP_RESOURCE) { + if (TargetInfo.ShapeKind == AR_TOBJ_OBJECT && + hlsl::IsHLSLResourceType(target) && !hlsl::IsHLSLSamplerType(target)) { Second = ICK_Flat_Conversion; goto lSuccess; } diff --git a/tools/clang/test/SemaHLSL/heap-assignments.hlsl b/tools/clang/test/SemaHLSL/heap-assignments.hlsl new file mode 100644 index 0000000000..635f85121b --- /dev/null +++ b/tools/clang/test/SemaHLSL/heap-assignments.hlsl @@ -0,0 +1,115 @@ +// RUN: %dxc -Tcs_6_6 -verify %s + +struct MyStruct +{ + float f; +}; + +void TestSamplerDescriptorHeap() +{ + SamplerState s1 = SamplerDescriptorHeap[0]; + SamplerComparisonState s2 = SamplerDescriptorHeap[0]; + + Texture1D t1 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture1D' with an rvalue of type 'const .Sampler'}} + RWTexture1D t2 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture1D' with an rvalue of type 'const .Sampler'}} + Texture2D t3 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture2D' with an rvalue of type 'const .Sampler'}} + RWTexture2D t4 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture2D' with an rvalue of type 'const .Sampler'}} + Texture3D t5 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture3D' with an rvalue of type 'const .Sampler'}} + RWTexture3D t6 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture3D' with an rvalue of type 'const .Sampler'}} + + Texture2DMS t7 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture2DMS' with an rvalue of type 'const .Sampler'}} + RWTexture2DMS t8 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture2DMS' with an rvalue of type 'const .Sampler'}} + TextureCube t9 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'TextureCube' with an rvalue of type 'const .Sampler'}} + + Texture1DArray t10 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture1DArray' with an rvalue of type 'const .Sampler'}} + RWTexture1DArray t11 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture1DArray' with an rvalue of type 'const .Sampler'}} + Texture2DArray t12 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture2DArray' with an rvalue of type 'const .Sampler'}} + RWTexture2DArray t13 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture2DArray' with an rvalue of type 'const .Sampler'}} + Texture2DMSArray t14 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Texture2DMSArray' with an rvalue of type 'const .Sampler'}} + RWTexture2DMSArray t15 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWTexture2DMSArray' with an rvalue of type 'const .Sampler'}} + TextureCubeArray t16 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'TextureCubeArray' with an rvalue of type 'const .Sampler'}} + + FeedbackTexture2D t17 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'FeedbackTexture2D' with an rvalue of type 'const .Sampler'}} + FeedbackTexture2DArray t18 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'FeedbackTexture2DArray' with an rvalue of type 'const .Sampler'}} + + RasterizerOrderedTexture1D t19 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedTexture1D' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedTexture2D t20 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedTexture2D' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedTexture3D t21 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedTexture3D' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedTexture1DArray t22 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedTexture1DArray' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedTexture2DArray t23 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedTexture2DArray' with an rvalue of type 'const .Sampler'}} + + ByteAddressBuffer b1 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'ByteAddressBuffer' with an rvalue of type 'const .Sampler'}} + RWByteAddressBuffer b2 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWByteAddressBuffer' with an rvalue of type 'const .Sampler'}} + StructuredBuffer b3 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'StructuredBuffer' with an rvalue of type 'const .Sampler'}} + RWStructuredBuffer b4 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWStructuredBuffer' with an rvalue of type 'const .Sampler'}} + AppendStructuredBuffer b5 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'AppendStructuredBuffer' with an rvalue of type 'const .Sampler'}} + ConsumeStructuredBuffer b6 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'ConsumeStructuredBuffer' with an rvalue of type 'const .Sampler'}} + Buffer b7 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'Buffer' with an rvalue of type 'const .Sampler'}} + RWBuffer b8 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RWBuffer' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedBuffer b9 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedBuffer' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedByteAddressBuffer b10 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedByteAddressBuffer' with an rvalue of type 'const .Sampler'}} + RasterizerOrderedStructuredBuffer b11 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RasterizerOrderedStructuredBuffer' with an rvalue of type 'const .Sampler'}} + + ConstantBuffer cb0 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'ConstantBuffer' with an rvalue of type 'const .Sampler'}} + TextureBuffer tb0 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'TextureBuffer' with an rvalue of type 'const .Sampler'}} + + RaytracingAccelerationStructure as0 = SamplerDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'RaytracingAccelerationStructure' with an rvalue of type 'const .Sampler'}} +} + +void TestResourceDescriptorHeap() +{ + SamplerState s1 = ResourceDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'SamplerState' with an rvalue of type 'const .Resource'}} + SamplerComparisonState s2 = ResourceDescriptorHeap[0]; // expected-error{{cannot initialize a variable of type 'SamplerComparisonState' with an rvalue of type 'const .Resource'}} + + Texture1D t1 = ResourceDescriptorHeap[0]; + RWTexture1D t2 = ResourceDescriptorHeap[0]; + Texture2D t3 = ResourceDescriptorHeap[0]; + RWTexture2D t4 = ResourceDescriptorHeap[0]; + Texture3D t5 = ResourceDescriptorHeap[0]; + RWTexture3D t6 = ResourceDescriptorHeap[0]; + + Texture2DMS t7 = ResourceDescriptorHeap[0]; + RWTexture2DMS t8 = ResourceDescriptorHeap[0]; + TextureCube t9 = ResourceDescriptorHeap[0]; + + Texture1DArray t10 = ResourceDescriptorHeap[0]; + RWTexture1DArray t11 = ResourceDescriptorHeap[0]; + Texture2DArray t12 = ResourceDescriptorHeap[0]; + RWTexture2DArray t13 = ResourceDescriptorHeap[0]; + Texture2DMSArray t14 = ResourceDescriptorHeap[0]; + RWTexture2DMSArray t15 = ResourceDescriptorHeap[0]; + TextureCubeArray t16 = ResourceDescriptorHeap[0]; + + FeedbackTexture2D t17 = ResourceDescriptorHeap[0]; + FeedbackTexture2DArray t18 = ResourceDescriptorHeap[0]; + + RasterizerOrderedTexture1D t19 = ResourceDescriptorHeap[0]; + RasterizerOrderedTexture2D t20 = ResourceDescriptorHeap[0]; + RasterizerOrderedTexture3D t21 = ResourceDescriptorHeap[0]; + RasterizerOrderedTexture1DArray t22 = ResourceDescriptorHeap[0]; + RasterizerOrderedTexture2DArray t23 = ResourceDescriptorHeap[0]; + + ByteAddressBuffer b1 = ResourceDescriptorHeap[0]; + RWByteAddressBuffer b2 = ResourceDescriptorHeap[0]; + StructuredBuffer b3 = ResourceDescriptorHeap[0]; + RWStructuredBuffer b4 = ResourceDescriptorHeap[0]; + AppendStructuredBuffer b5 = ResourceDescriptorHeap[0]; + ConsumeStructuredBuffer b6 = ResourceDescriptorHeap[0]; + Buffer b7 = ResourceDescriptorHeap[0]; + RWBuffer b8 = ResourceDescriptorHeap[0]; + RasterizerOrderedBuffer b9 = ResourceDescriptorHeap[0]; + RasterizerOrderedByteAddressBuffer b10 = ResourceDescriptorHeap[0]; + RasterizerOrderedStructuredBuffer b11 = ResourceDescriptorHeap[0]; + + ConstantBuffer cb0 = ResourceDescriptorHeap[0]; + TextureBuffer tb0 = ResourceDescriptorHeap[0]; + + RaytracingAccelerationStructure as0 = ResourceDescriptorHeap[0]; +} + +[numthreads(1, 1, 1)] +void main() +{ + TestSamplerDescriptorHeap(); + TestResourceDescriptorHeap(); +} \ No newline at end of file