Преглед на файлове

Update BaseAlignLog2 field in ResourceProperties for StructuredBuffer (#3652)

Vishal Sharma преди 4 години
родител
ревизия
3daa429525

+ 4 - 0
include/dxc/DXIL/DxilResource.h

@@ -45,6 +45,9 @@ public:
   unsigned GetElementStride() const;
   void SetElementStride(unsigned ElemStride);
 
+  unsigned GetBaseAlignLog2() const;
+  void SetBaseAlignLog2(unsigned baseAlignLog2);
+
   DXIL::SamplerFeedbackType GetSamplerFeedbackType() const;
   void SetSamplerFeedbackType(DXIL::SamplerFeedbackType Value);
 
@@ -76,6 +79,7 @@ public:
 private:
   unsigned m_SampleCount;
   unsigned m_ElementStride; // in bytes
+  unsigned m_baseAlignLog2 = 0; // worst-case alignment
   CompType m_CompType;
   DXIL::SamplerFeedbackType m_SamplerFeedbackType;
   bool m_bGloballyCoherent;

+ 8 - 0
lib/DXIL/DxilResource.cpp

@@ -77,6 +77,14 @@ void DxilResource::SetElementStride(unsigned ElemStride) {
   m_ElementStride = ElemStride;
 }
 
+unsigned DxilResource::GetBaseAlignLog2() const {
+  return m_baseAlignLog2;
+}
+
+void DxilResource::SetBaseAlignLog2(unsigned baseAlignLog2) {
+  m_baseAlignLog2 = baseAlignLog2;
+}
+
 DXIL::SamplerFeedbackType DxilResource::GetSamplerFeedbackType() const {
   return m_SamplerFeedbackType;
 }

+ 3 - 0
lib/DXIL/DxilResourceProperties.cpp

@@ -155,8 +155,11 @@ DxilResourceProperties loadPropsFromResourceBase(const DxilResourceBase *Res) {
 
       break;
     case DXIL::ResourceKind::StructuredBuffer:
+    {
       RP.StructStrideInBytes = Res.GetElementStride();
+      RP.Basic.BaseAlignLog2 = Res.GetBaseAlignLog2();
       break;
+    }
     case DXIL::ResourceKind::Texture2DMS:
     case DXIL::ResourceKind::Texture2DMSArray:
     case DXIL::ResourceKind::TypedBuffer:

+ 6 - 0
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -3194,6 +3194,12 @@ bool CGMSHLSLRuntime::SetUAVSRV(SourceLocation loc,
 
     uint32_t strideInBytes = dataLayout.getTypeAllocSize(retTy);
     hlslRes->SetElementStride(strideInBytes);
+    if (kind == hlsl::DxilResource::Kind::StructuredBuffer) {
+      if (StructType* ST = dyn_cast<StructType>(retTy)) {
+        const StructLayout* SL = dataLayout.getStructLayout(ST);
+        hlslRes->SetBaseAlignLog2(Log2_32(SL->getAlignment()));
+      }
+    }
   }
   if (HasHLSLGloballyCoherent(QualTy)) {
     hlslRes->SetGloballyCoherent(true);

+ 20 - 0
tools/clang/test/HLSLFileCheck/hlsl/objects/StructuredBuffer/alignment/struct_buf_16bit_type_2byte_alignment.hlsl

@@ -0,0 +1,20 @@
+// RUN: %dxc -E main -T vs_6_6 -enable-16bit-types  %s | FileCheck %s
+// CHECK: %dx.types.ResourceProperties { i32 4364, i32 16 })  ; AnnotateHandle(res,props)  resource: RWStructuredBuffer<stride=16>
+// CHECK: %dx.types.ResourceProperties { i32 268, i32 16 })  ; AnnotateHandle(res,props)  resource: StructuredBuffer<stride=16>
+
+struct S1 {
+   float16_t4 v1;
+};
+
+struct S {
+  uint16_t4 v0;
+  S1 v2;
+};
+
+StructuredBuffer<S> srv;
+RWStructuredBuffer<S> uav;
+
+void main(uint i : IN0)
+{
+    uav[i] = srv[i];
+}

+ 21 - 0
tools/clang/test/HLSLFileCheck/hlsl/objects/StructuredBuffer/alignment/struct_buf_32bit_type_4byte_alignment.hlsl

@@ -0,0 +1,21 @@
+// RUN: %dxc -E main -T vs_6_6 -enable-16bit-types  %s | FileCheck %s
+// CHECK: %dx.types.ResourceProperties { i32 4620, i32 28 })  ; AnnotateHandle(res,props)  resource: RWStructuredBuffer<stride=28>
+// CHECK: %dx.types.ResourceProperties { i32 524, i32 28 })  ; AnnotateHandle(res,props)  resource: StructuredBuffer<stride=28>
+
+struct S1 {
+   float16_t4 v1;
+   float v2;
+};
+
+struct S {
+  uint4 v0;
+  S1 v3;
+};
+
+StructuredBuffer<S> srv;
+RWStructuredBuffer<S> uav;
+
+void main(uint i : IN0)
+{
+    uav[i] = srv[i];
+}

+ 22 - 0
tools/clang/test/HLSLFileCheck/hlsl/objects/StructuredBuffer/alignment/struct_buf_64bit_type_8byte_alignment.hlsl

@@ -0,0 +1,22 @@
+// RUN: %dxc -E main -T vs_6_6 -enable-16bit-types  %s | FileCheck %s
+// CHECK: %dx.types.ResourceProperties { i32 4876, i32 32 })  ; AnnotateHandle(res,props)  resource: RWStructuredBuffer<stride=32>
+// CHECK: %dx.types.ResourceProperties { i32 780, i32 32 })  ; AnnotateHandle(res,props)  resource: StructuredBuffer<stride=32>
+
+struct S1 {
+   float16_t4 v1;
+   float v2; 
+   double v3;
+};
+
+struct S {
+  uint16_t4 v0;
+  S1 v4;
+};
+
+StructuredBuffer<S> srv;
+RWStructuredBuffer<S> uav;
+
+void main(uint i : IN0)
+{
+    uav[i] = srv[i];
+}