Quellcode durchsuchen

Merged PR 36: Fix StructuredBuffer GetDimensions stride

Fix StructuredBuffer GetDimensions stride
Tex Riddell vor 7 Jahren
Ursprung
Commit
fbbd1c1350

+ 2 - 2
lib/HLSL/HLOperationLower.cpp

@@ -2203,8 +2203,8 @@ Value *TranslateGetDimensions(CallInst *CI, IntrinsicOp IOP, OP::OpCode op,
     // Set stride.
     Value *stridePtr = CI->getArgOperand(widthOpIdx + 1);
     const DataLayout &DL = helper.dataLayout;
-    Value *buf = CI->getArgOperand(HLOperandIndex::kHandleOpIdx);
-    Type *bufTy = buf->getType();
+    Value *handle = CI->getArgOperand(HLOperandIndex::kHandleOpIdx);
+    Type *bufTy = pObjHelper->GetResourceType(handle);
     Type *bufRetTy = bufTy->getStructElementType(0);
     unsigned stride = DL.getTypeAllocSize(bufRetTy);
     Builder.CreateStore(hlslOP->GetU32Const(stride), stridePtr);

+ 39 - 0
tools/clang/test/CodeGenHLSL/quick-test/res_select3.hlsl

@@ -0,0 +1,39 @@
+// RUN: %dxc -T lib_6_1 %s | FileCheck %s
+
+// Make sure no phi of resource.
+// CHECK-NOT: phi %class.RWStructuredBuffer
+// CHECK: phi %dx.types.Handle
+
+// Make sure get dimensions returns 24
+// CHECK: ret i32 24
+
+struct MyStruct {
+  float2 a;
+  int b;
+  float3 c;
+};
+
+RWStructuredBuffer<MyStruct> a;
+RWStructuredBuffer<MyStruct> b;
+RWStructuredBuffer<MyStruct> c;
+
+uint test(int i, int j, int m) {
+  RWStructuredBuffer<MyStruct> buf = c;
+  while (i > 9) {
+     while (j < 4) {
+        if (i < m)
+          buf = b;
+        buf[j].b = i;
+        j++;
+     }
+     if (m > j)
+       buf = a;
+     buf[m].b = i;
+     i--;
+  }
+  buf[i].b = j;
+  uint dim = 0;
+  uint stride = 0;
+  buf.GetDimensions(dim, stride);
+  return stride;
+}

+ 24 - 0
tools/clang/test/CodeGenHLSL/quick-test/structured_buffer_getdim_stride.hlsl

@@ -0,0 +1,24 @@
+// RUN: %dxc -T lib_6_3 %s | FileCheck %s
+
+// CHECK: ret i32 20
+
+struct Foo
+{
+    float4 a;
+    uint b;
+};
+
+RWStructuredBuffer<Foo> g_buffer[2] : register(u0);
+
+uint GetStride(int i) {
+  RWStructuredBuffer<Foo> buf;
+  if (i < 0) {
+    buf = g_buffer[0];
+  } else {
+    buf = g_buffer[1];
+  }
+  uint a = 0;
+  uint elementStride = 0;
+  buf.GetDimensions(a, elementStride);
+  return elementStride;
+}