Dynamic Buffer Offset : Vulkan Backend

In a typical application, most draws will use different uniforms values for
things like the world position and orientation. In the current state of WebGPU
this means that a new bind group needs to be created for each draw to set the
right uniforms. Bind group creation is expected to be more expensive than
recording draws because they incur an allocation.

This feature is to reduce the number of bind groups that need to be
created.

The patch implements dynamic buffer offset on vulkan backend. But Vulkan
takes dynamic offset as uint32_t type, which is not the same size as the VkDeviceSize
used to create buffers so we cast them for now.

Bug=dawn:55

Change-Id: I6163866feb040d1a653f9a20d2ce22d80509968e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/7461
Commit-Queue: Shaobo Yan <shaobo.yan@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Yan, Shaobo 2019-05-27 04:35:03 +00:00 committed by Commit Bot service account
parent 7777078fb9
commit 3dd6153eb7
4 changed files with 40 additions and 17 deletions

View File

@ -50,8 +50,9 @@ namespace dawn_native { namespace vulkan {
case dawn::BindingType::StorageBuffer:
return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
case dawn::BindingType::DynamicUniformBuffer:
return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
case dawn::BindingType::DynamicStorageBuffer:
UNREACHABLE();
return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
default:
UNREACHABLE();
}
@ -121,16 +122,15 @@ namespace dawn_native { namespace vulkan {
auto ToDescriptorType = [](dawn::BindingType type) -> DescriptorType {
switch (type) {
case dawn::BindingType::UniformBuffer:
case dawn::BindingType::DynamicUniformBuffer:
return UNIFORM_BUFFER;
case dawn::BindingType::Sampler:
return SAMPLER;
case dawn::BindingType::SampledTexture:
return SAMPLED_IMAGE;
case dawn::BindingType::StorageBuffer:
return STORAGE_BUFFER;
case dawn::BindingType::DynamicUniformBuffer:
case dawn::BindingType::DynamicStorageBuffer:
UNREACHABLE();
return STORAGE_BUFFER;
default:
UNREACHABLE();
}

View File

@ -81,7 +81,9 @@ namespace dawn_native { namespace vulkan {
switch (layoutInfo.types[bindingIndex]) {
case dawn::BindingType::UniformBuffer:
case dawn::BindingType::StorageBuffer: {
case dawn::BindingType::StorageBuffer:
case dawn::BindingType::DynamicUniformBuffer:
case dawn::BindingType::DynamicStorageBuffer: {
BufferBinding binding = GetBindingAsBufferBinding(bindingIndex);
writeBufferInfo[numWrites].buffer = ToBackend(binding.buffer)->GetHandle();
@ -109,10 +111,6 @@ namespace dawn_native { namespace vulkan {
write.pImageInfo = &writeImageInfo[numWrites];
} break;
case dawn::BindingType::DynamicUniformBuffer:
case dawn::BindingType::DynamicStorageBuffer:
UNREACHABLE();
break;
default:
UNREACHABLE();
}

View File

@ -105,9 +105,21 @@ namespace dawn_native { namespace vulkan {
class DescriptorSetTracker {
public:
void OnSetBindGroup(uint32_t index, VkDescriptorSet set) {
void OnSetBindGroup(uint32_t index,
VkDescriptorSet set,
uint32_t dynamicOffsetCount,
uint64_t* dynamicOffsets) {
mDirtySets.set(index);
mSets[index] = set;
mDynamicOffsetCounts[index] = dynamicOffsetCount;
if (dynamicOffsetCount > 0) {
// Vulkan backend use uint32_t as dynamic offsets type, it is not correct.
// Vulkan should use VkDeviceSize. Dawn vulkan backend has to handle this.
for (uint32_t i = 0; i < dynamicOffsetCount; ++i) {
ASSERT(dynamicOffsets[i] <= std::numeric_limits<uint32_t>::max());
mDynamicOffsets[index][i] = static_cast<uint32_t>(dynamicOffsets[i]);
}
}
}
void OnPipelineLayoutChange(PipelineLayout* layout) {
@ -131,9 +143,11 @@ namespace dawn_native { namespace vulkan {
void Flush(Device* device, VkCommandBuffer commands, VkPipelineBindPoint bindPoint) {
for (uint32_t dirtyIndex : IterateBitSet(mDirtySets)) {
device->fn.CmdBindDescriptorSets(commands, bindPoint,
mCurrentLayout->GetHandle(), dirtyIndex, 1,
&mSets[dirtyIndex], 0, nullptr);
device->fn.CmdBindDescriptorSets(
commands, bindPoint, mCurrentLayout->GetHandle(), dirtyIndex, 1,
&mSets[dirtyIndex], mDynamicOffsetCounts[dirtyIndex],
mDynamicOffsetCounts[dirtyIndex] > 0 ? mDynamicOffsets[dirtyIndex].data()
: nullptr);
}
mDirtySets.reset();
}
@ -142,6 +156,8 @@ namespace dawn_native { namespace vulkan {
PipelineLayout* mCurrentLayout = nullptr;
std::array<VkDescriptorSet, kMaxBindGroups> mSets;
std::bitset<kMaxBindGroups> mDirtySets;
std::array<uint32_t, kMaxBindGroups> mDynamicOffsetCounts;
std::array<std::array<uint32_t, kMaxBindingsPerGroup>, kMaxBindGroups> mDynamicOffsets;
};
void RecordBeginRenderPass(VkCommandBuffer commands,
@ -414,8 +430,13 @@ namespace dawn_native { namespace vulkan {
case Command::SetBindGroup: {
SetBindGroupCmd* cmd = mCommands.NextCommand<SetBindGroupCmd>();
VkDescriptorSet set = ToBackend(cmd->group.Get())->GetHandle();
uint64_t* dynamicOffsets = nullptr;
if (cmd->dynamicOffsetCount > 0) {
dynamicOffsets = mCommands.NextData<uint64_t>(cmd->dynamicOffsetCount);
}
descriptorSets.OnSetBindGroup(cmd->index, set);
descriptorSets.OnSetBindGroup(cmd->index, set, cmd->dynamicOffsetCount,
dynamicOffsets);
} break;
case Command::SetComputePipeline: {
@ -552,8 +573,13 @@ namespace dawn_native { namespace vulkan {
case Command::SetBindGroup: {
SetBindGroupCmd* cmd = mCommands.NextCommand<SetBindGroupCmd>();
VkDescriptorSet set = ToBackend(cmd->group.Get())->GetHandle();
uint64_t* dynamicOffsets = nullptr;
if (cmd->dynamicOffsetCount > 0) {
dynamicOffsets = mCommands.NextData<uint64_t>(cmd->dynamicOffsetCount);
}
descriptorSets.OnSetBindGroup(cmd->index, set);
descriptorSets.OnSetBindGroup(cmd->index, set, cmd->dynamicOffsetCount,
dynamicOffsets);
} break;
case Command::SetBlendColor: {

View File

@ -56,7 +56,6 @@ class DynamicBufferOffsetTests : public DawnTest {
}
// Create objects to use as resources inside test bind groups.
const void* mappedData = nullptr;
dawn::BindGroup mBindGroup;
dawn::BindGroupLayout mBindGroupLayout;
dawn::Buffer mUniformBuffer;
@ -212,4 +211,4 @@ TEST_P(DynamicBufferOffsetTests, SetDynamicOffestsComputePipeline) {
kMinDynamicBufferOffsetAlignment, expectedData.size());
}
DAWN_INSTANTIATE_TEST(DynamicBufferOffsetTests, MetalBackend);
DAWN_INSTANTIATE_TEST(DynamicBufferOffsetTests, MetalBackend, VulkanBackend);