Skip to content

Commit

Permalink
Add StructuredBuffer SRV Test (#30)
Browse files Browse the repository at this point in the history
Vulkan doesn't have the same division between SRV and UAV that DirectX
does, so this refactors the buffer handling code to merge SRVs and UAVs.
We will need different handling for other types of buffers in the future
(i.e. CBVs which map to uniform buffers), but for now this gets pairity
between DX and VK.

The SRV test for structured buffers that @bogner added in his last PR
miscompiles to DXC SPIR-V, so this PR adds a new test to handle more
basic StructuredBuffer SRV use.
  • Loading branch information
llvm-beanz authored Jan 25, 2025
1 parent aef41c0 commit 1a2f2c7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 deletions.
50 changes: 20 additions & 30 deletions lib/API/VK/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class VKDevice : public offloadtest::Device {
VkDeviceMemory Memory;
};

struct UAVRef {
struct ResourceRef {
BufferRef Host;
BufferRef Device;
uint64_t Size;
Expand All @@ -72,7 +72,7 @@ class VKDevice : public offloadtest::Device {
VkPipeline Pipeline;

llvm::SmallVector<VkDescriptorSetLayout> DescriptorSetLayouts;
llvm::SmallVector<UAVRef> UAVs;
llvm::SmallVector<ResourceRef> Buffers;
llvm::SmallVector<VkDescriptorSet> DescriptorSets;
llvm::SmallVector<VkBufferView> BufferViews;
};
Expand Down Expand Up @@ -286,8 +286,8 @@ class VKDevice : public offloadtest::Device {
return BufferRef{Buffer, Memory};
}

llvm::Error createUAV(Resource &R, InvocationState &IS,
const uint32_t HeapIdx) {
llvm::Error createBuffer(Resource &R, InvocationState &IS,
const uint32_t HeapIdx) {
auto ExHostBuf = createBuffer(
IS, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, R.Size, R.Data.get());
Expand All @@ -308,17 +308,11 @@ class VKDevice : public offloadtest::Device {
vkCmdCopyBuffer(IS.CmdBuffer, ExHostBuf->Buffer, ExDeviceBuf->Buffer, 1,
&Copy);

IS.UAVs.push_back(UAVRef{*ExHostBuf, *ExDeviceBuf, R.Size});
IS.Buffers.push_back(ResourceRef{*ExHostBuf, *ExDeviceBuf, R.Size});

return llvm::Error::success();
}

llvm::Error createSRV(Resource &R, InvocationState &IS,
const uint32_t HeapIdx) {
return llvm::createStringError(std::errc::not_supported,
"VXDevice::createSRV not supported.");
}

llvm::Error createCBV(Resource &R, InvocationState &IS,
const uint32_t HeapIdx) {
return llvm::createStringError(std::errc::not_supported,
Expand All @@ -331,13 +325,11 @@ class VKDevice : public offloadtest::Device {
for (auto &R : D.Resources) {
switch (R.Access) {
case DataAccess::ReadOnly:
if (auto Err = createSRV(R, IS, HeapIndex++))
return Err;
break;
case DataAccess::ReadWrite:
if (auto Err = createUAV(R, IS, HeapIndex++))
if (auto Err = createBuffer(R, IS, HeapIndex++))
return Err;
break;

case DataAccess::Constant:
if (auto Err = createCBV(R, IS, HeapIndex++))
return Err;
Expand Down Expand Up @@ -384,12 +376,10 @@ class VKDevice : public offloadtest::Device {
uint32_t StorageBufferCount = 0;
for (const auto &S : P.Sets) {
for (const auto &R : S.Resources) {
if (R.Access == DataAccess::ReadWrite) {
if (R.isRaw())
StorageBufferCount += 1;
else
TexelBufferCount += 1;
}
if (R.isRaw())
StorageBufferCount += 1;
else
TexelBufferCount += 1;
}
}
assert(TexelBufferCount + StorageBufferCount == P.getDescriptorCount() &&
Expand Down Expand Up @@ -489,11 +479,11 @@ class VKDevice : public offloadtest::Device {
: getVKFormat(P.Sets[SetIdx].Resources[RIdx].Format,
P.Sets[SetIdx].Resources[RIdx].Channels);
ViewCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
ViewCreateInfo.buffer = IS.UAVs[UAVIdx].Device.Buffer;
ViewCreateInfo.buffer = IS.Buffers[UAVIdx].Device.Buffer;
ViewCreateInfo.format = Format;
ViewCreateInfo.range = VK_WHOLE_SIZE;
if (IsRaw) {
VkDescriptorBufferInfo BI = {IS.UAVs[UAVIdx].Device.Buffer, 0,
VkDescriptorBufferInfo BI = {IS.Buffers[UAVIdx].Device.Buffer, 0,
VK_WHOLE_SIZE};
RawBufferInfos.push_back(BI);
} else {
Expand Down Expand Up @@ -564,7 +554,7 @@ class VKDevice : public offloadtest::Device {
}

llvm::Error createComputeCommands(Pipeline &P, InvocationState &IS) {
for (auto &UAV : IS.UAVs) {
for (auto &UAV : IS.Buffers) {
VkBufferMemoryBarrier Barrier = {};
Barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
Barrier.buffer = UAV.Device.Buffer;
Expand All @@ -586,7 +576,7 @@ class VKDevice : public offloadtest::Device {
vkCmdDispatch(IS.CmdBuffer, P.DispatchSize[0], P.DispatchSize[1],
P.DispatchSize[2]);

for (auto &UAV : IS.UAVs) {
for (auto &UAV : IS.Buffers) {
VkBufferMemoryBarrier Barrier = {};
Barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
Barrier.buffer = UAV.Device.Buffer;
Expand Down Expand Up @@ -624,16 +614,16 @@ class VKDevice : public offloadtest::Device {
if (R.Access != DataAccess::ReadWrite)
continue;
void *Mapped = nullptr;
vkMapMemory(IS.Device, IS.UAVs[UAVIdx].Host.Memory, 0, VK_WHOLE_SIZE, 0,
&Mapped);
vkMapMemory(IS.Device, IS.Buffers[UAVIdx].Host.Memory, 0, VK_WHOLE_SIZE,
0, &Mapped);
VkMappedMemoryRange Range = {};
Range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
Range.memory = IS.UAVs[UAVIdx].Host.Memory;
Range.memory = IS.Buffers[UAVIdx].Host.Memory;
Range.offset = 0;
Range.size = VK_WHOLE_SIZE;
vkInvalidateMappedMemoryRanges(IS.Device, 1, &Range);
memcpy(R.Data.get(), Mapped, R.Size);
vkUnmapMemory(IS.Device, IS.UAVs[UAVIdx].Host.Memory);
vkUnmapMemory(IS.Device, IS.Buffers[UAVIdx].Host.Memory);
UAVIdx++;
}
}
Expand All @@ -645,7 +635,7 @@ class VKDevice : public offloadtest::Device {
for (auto &V : IS.BufferViews)
vkDestroyBufferView(IS.Device, V, nullptr);

for (auto &R : IS.UAVs) {
for (auto &R : IS.Buffers) {
vkDestroyBuffer(IS.Device, R.Device.Buffer, nullptr);
vkFreeMemory(IS.Device, R.Device.Memory, nullptr);
vkDestroyBuffer(IS.Device, R.Host.Buffer, nullptr);
Expand Down
59 changes: 59 additions & 0 deletions test/Basic/StructuredBuffer-SRV.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#--- source.hlsl
struct S1 {
int4 i;
};

StructuredBuffer<S1> In : register(t0);
RWStructuredBuffer<S1> Out : register(u0);

[numthreads(1,1,1)]
void main(uint GI : SV_GroupIndex) {
Out[GI].i = In[GI].i * 2.0;
}
//--- pipeline.yaml
---
DispatchSize: [1, 1, 1]
DescriptorSets:
- Resources:
- Access: ReadOnly
Format: Hex32
RawSize: 16
Data: [0x00000000, 0x00000001, 0x00000002, 0x00000003]
DirectXBinding:
Register: 0
Space: 0
- Access: ReadWrite
Format: Hex32
RawSize: 16
ZeroInitSize: 16
DirectXBinding:
Register: 0
Space: 0
...
#--- end

# RUN: split-file %s %t
# RUN: %if DirectX %{ dxc -T cs_6_0 -Fo %t.dxil %t/source.hlsl %}
# RUN: %if DirectX %{ %offloader %t/pipeline.yaml %t.dxil | FileCheck %s %}
# RUN: %if Vulkan %{ dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.3 -fvk-use-scalar-layout -Fo %t.spv %t/source.hlsl %}
# RUN: %if Vulkan %{ %offloader %t/pipeline.yaml %t.spv | FileCheck %s %}

# RUN: %if Metal %{ dxc -T cs_6_0 -Fo %t.dxil %t/source.hlsl %}
# RUN: %if Metal %{ metal-shaderconverter %t.dxil -o=%t.metallib %}
# RUN: %if Metal %{ %offloader %t/pipeline.yaml %t.metallib | FileCheck %s %}

# CHECK: Access: ReadOnly
# CHECK: Data: [
# CHECK: 0x0,
# CHECK: 0x1,
# CHECK: 0x2,
# CHECK: 0x3
# CHECK: ]

# CHECK: Access: ReadWrite
# CHECK: Data: [
# CHECK: 0x0,
# CHECK: 0x2,
# CHECK: 0x4,
# CHECK: 0x6
# CHECK: ]
2 changes: 1 addition & 1 deletion test/Basic/StructuredBuffer.test
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DescriptorSets:
# RUN: %if DirectX %{ %offloader %t/pipeline.yaml %t.dxil | FileCheck %s %}
# RUN: %if Vulkan %{ dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.3 -fvk-use-scalar-layout -Fo %t.spv %t/source.hlsl %}
# RUN: %if Vulkan %{ %offloader %t/pipeline.yaml %t.spv | FileCheck %s %}
# XFAIL: Vulkan
# XFAIL: DXC-Vulkan

# RUN: %if Metal %{ dxc -T cs_6_0 -Fo %t.dxil %t/source.hlsl %}
# RUN: %if Metal %{ metal-shaderconverter %t.dxil -o=%t.metallib %}
Expand Down
9 changes: 8 additions & 1 deletion test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@
else:
tools.append(ToolSubst("%offloader", FindTool("offloader")))

HLSLCompiler = ''
if config.offloadtest_test_clang:
if os.path.exists(config.offloadtest_dxc_dir):
tools.append(ToolSubst("dxc", FindTool("clang-dxc"), extra_args=["--dxv-path=%s" % config.offloadtest_dxc_dir]))
else:
tools.append(ToolSubst("dxc", FindTool("clang-dxc")))
config.available_features.add("Clang")
HLSLCompiler = 'Clang'
else:
tools.append(ToolSubst("dxc", config.offloadtest_dxc))
HLSLCompiler = 'DXC'

config.available_features.add(HLSLCompiler)

llvm_config.add_tool_substitutions(tools, config.llvm_tools_dir)

Expand All @@ -66,12 +70,15 @@
for device in devices['Devices']:
if device['API'] == "DirectX" and config.offloadtest_enable_d3d12:
config.available_features.add("DirectX")
config.available_features.add(HLSLCompiler + "-DirectX")
if "Intel" in device['Description']:
config.available_features.add("DirectX-Intel")
if device['API'] == "Metal" and config.offloadtest_enable_metal:
config.available_features.add("Metal")
config.available_features.add(HLSLCompiler + "-Metal")
if device['API'] == "Vulkan" and config.offloadtest_enable_vulkan:
config.available_features.add("Vulkan")
config.available_features.add(HLSLCompiler + "-Vulkan")
if "NVIDIA" in device['Description']:
config.available_features.add("Vulkan-NV")

Expand Down

0 comments on commit 1a2f2c7

Please sign in to comment.