mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-20 10:25:28 +00:00
Refactor APICreateComputePipelineAsync to support both sync and async path
This patch refactors the implementation of APICreateComputePipelineAsync as a preparation of the async path of the creation of compute pipeline. Now the code path of APICreateComputePipelineAsync() includes the following 3 parts: - When an error occurs in the front-end validations, the callback will be called at once in the main thread. - When we can find a proper compute pipeline object in the cache, the callback will be called at once in the main thread. - When we cannot find the proper comptue pipeline object in the cache, the newly-created pipeline object, the callback and userdata will be saved into the CreatePipelineAsyncTracker, and the callback will be called in device.Tick(). All the logic mentioned in this section has been put into one function CreateComputePipelineAsyncImpl(), which will be overrided by its asynchronous version on all the backends that support creating pipeline objects asynchronously. Note that APICreateRenderPipelineAsync is not changed in this patch because it is now under refactoring to match the current updates in WebGPU SPEC. BUG=dawn:529 TEST=dawn_end2end_tests Change-Id: Ie1cf2f9fc8e18c3e6ad723c6a0cefce29a0eb69c Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/45842 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
This commit is contained in:
committed by
Commit Bot service account
parent
40d1c83362
commit
c243f67d58
@@ -28,6 +28,46 @@ namespace {
|
||||
|
||||
class CreatePipelineAsyncTest : public DawnTest {
|
||||
protected:
|
||||
void ValidateCreateComputePipelineAsync(CreatePipelineAsyncTask* currentTask) {
|
||||
wgpu::BufferDescriptor bufferDesc;
|
||||
bufferDesc.size = sizeof(uint32_t);
|
||||
bufferDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc;
|
||||
wgpu::Buffer ssbo = device.CreateBuffer(&bufferDesc);
|
||||
|
||||
wgpu::CommandBuffer commands;
|
||||
{
|
||||
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
while (!currentTask->isCompleted) {
|
||||
WaitABit();
|
||||
}
|
||||
ASSERT_TRUE(currentTask->message.empty());
|
||||
ASSERT_NE(nullptr, currentTask->computePipeline.Get());
|
||||
wgpu::BindGroup bindGroup =
|
||||
utils::MakeBindGroup(device, currentTask->computePipeline.GetBindGroupLayout(0),
|
||||
{
|
||||
{0, ssbo, 0, sizeof(uint32_t)},
|
||||
});
|
||||
pass.SetBindGroup(0, bindGroup);
|
||||
pass.SetPipeline(currentTask->computePipeline);
|
||||
|
||||
pass.Dispatch(1);
|
||||
pass.EndPass();
|
||||
|
||||
commands = encoder.Finish();
|
||||
}
|
||||
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
constexpr uint32_t kExpected = 1u;
|
||||
EXPECT_BUFFER_U32_EQ(kExpected, ssbo, 0);
|
||||
}
|
||||
|
||||
void ValidateCreateComputePipelineAsync() {
|
||||
ValidateCreateComputePipelineAsync(&task);
|
||||
}
|
||||
|
||||
CreatePipelineAsyncTask task;
|
||||
};
|
||||
|
||||
@@ -58,39 +98,7 @@ TEST_P(CreatePipelineAsyncTest, BasicUseOfCreateComputePipelineAsync) {
|
||||
},
|
||||
&task);
|
||||
|
||||
wgpu::BufferDescriptor bufferDesc;
|
||||
bufferDesc.size = sizeof(uint32_t);
|
||||
bufferDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc;
|
||||
wgpu::Buffer ssbo = device.CreateBuffer(&bufferDesc);
|
||||
|
||||
wgpu::CommandBuffer commands;
|
||||
{
|
||||
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
while (!task.isCompleted) {
|
||||
WaitABit();
|
||||
}
|
||||
ASSERT_TRUE(task.message.empty());
|
||||
ASSERT_NE(nullptr, task.computePipeline.Get());
|
||||
wgpu::BindGroup bindGroup =
|
||||
utils::MakeBindGroup(device, task.computePipeline.GetBindGroupLayout(0),
|
||||
{
|
||||
{0, ssbo, 0, sizeof(uint32_t)},
|
||||
});
|
||||
pass.SetBindGroup(0, bindGroup);
|
||||
pass.SetPipeline(task.computePipeline);
|
||||
|
||||
pass.Dispatch(1);
|
||||
pass.EndPass();
|
||||
|
||||
commands = encoder.Finish();
|
||||
}
|
||||
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
constexpr uint32_t kExpected = 1u;
|
||||
EXPECT_BUFFER_U32_EQ(kExpected, ssbo, 0);
|
||||
ValidateCreateComputePipelineAsync();
|
||||
}
|
||||
|
||||
// Verify CreateComputePipelineAsync() works as expected when there is any error that happens during
|
||||
@@ -302,6 +310,88 @@ TEST_P(CreatePipelineAsyncTest, ReleaseDeviceBeforeCallbackOfCreateRenderPipelin
|
||||
&task);
|
||||
}
|
||||
|
||||
// Verify the code path of CreateComputePipelineAsync() to directly return the compute pipeline
|
||||
// object from cache works correctly.
|
||||
TEST_P(CreatePipelineAsyncTest, CreateSameComputePipelineTwice) {
|
||||
wgpu::ComputePipelineDescriptor csDesc;
|
||||
csDesc.computeStage.module = utils::CreateShaderModule(device, R"(
|
||||
[[block]] struct SSBO {
|
||||
value : u32;
|
||||
};
|
||||
[[group(0), binding(0)]] var<storage> ssbo : [[access(read_write)]] SSBO;
|
||||
|
||||
[[stage(compute)]] fn main() -> void {
|
||||
ssbo.value = 1u;
|
||||
})");
|
||||
csDesc.computeStage.entryPoint = "main";
|
||||
|
||||
auto callback = [](WGPUCreatePipelineAsyncStatus status, WGPUComputePipeline returnPipeline,
|
||||
const char* message, void* userdata) {
|
||||
EXPECT_EQ(WGPUCreatePipelineAsyncStatus::WGPUCreatePipelineAsyncStatus_Success, status);
|
||||
|
||||
CreatePipelineAsyncTask* task = static_cast<CreatePipelineAsyncTask*>(userdata);
|
||||
task->computePipeline = wgpu::ComputePipeline::Acquire(returnPipeline);
|
||||
task->isCompleted = true;
|
||||
task->message = message;
|
||||
};
|
||||
|
||||
// Create a pipeline object and save it into anotherTask.computePipeline.
|
||||
CreatePipelineAsyncTask anotherTask;
|
||||
device.CreateComputePipelineAsync(&csDesc, callback, &anotherTask);
|
||||
while (!anotherTask.isCompleted) {
|
||||
WaitABit();
|
||||
}
|
||||
ASSERT_TRUE(anotherTask.message.empty());
|
||||
ASSERT_NE(nullptr, anotherTask.computePipeline.Get());
|
||||
|
||||
// Create another pipeline object task.comnputepipeline with the same compute pipeline
|
||||
// descriptor used in the creation of anotherTask.computePipeline. This time the pipeline
|
||||
// object should be directly got from the pipeline object cache.
|
||||
device.CreateComputePipelineAsync(&csDesc, callback, &task);
|
||||
ValidateCreateComputePipelineAsync();
|
||||
}
|
||||
|
||||
// Verify creating compute pipeline with same descriptor and CreateComputePipelineAsync() at the
|
||||
// same time works correctly.
|
||||
TEST_P(CreatePipelineAsyncTest, CreateSamePipelineTwiceAtSameTime) {
|
||||
wgpu::ComputePipelineDescriptor csDesc;
|
||||
csDesc.computeStage.module = utils::CreateShaderModule(device, R"(
|
||||
[[block]] struct SSBO {
|
||||
value : u32;
|
||||
};
|
||||
[[group(0), binding(0)]] var<storage> ssbo : [[access(read_write)]] SSBO;
|
||||
|
||||
[[stage(compute)]] fn main() -> void {
|
||||
ssbo.value = 1u;
|
||||
})");
|
||||
csDesc.computeStage.entryPoint = "main";
|
||||
|
||||
auto callback = [](WGPUCreatePipelineAsyncStatus status, WGPUComputePipeline returnPipeline,
|
||||
const char* message, void* userdata) {
|
||||
EXPECT_EQ(WGPUCreatePipelineAsyncStatus::WGPUCreatePipelineAsyncStatus_Success, status);
|
||||
|
||||
CreatePipelineAsyncTask* task = static_cast<CreatePipelineAsyncTask*>(userdata);
|
||||
task->computePipeline = wgpu::ComputePipeline::Acquire(returnPipeline);
|
||||
task->isCompleted = true;
|
||||
task->message = message;
|
||||
};
|
||||
|
||||
// Create two pipeline objects with same descriptor.
|
||||
CreatePipelineAsyncTask anotherTask;
|
||||
device.CreateComputePipelineAsync(&csDesc, callback, &task);
|
||||
device.CreateComputePipelineAsync(&csDesc, callback, &anotherTask);
|
||||
|
||||
// Verify both task.computePipeline and anotherTask.computePipeline are created correctly.
|
||||
ValidateCreateComputePipelineAsync(&anotherTask);
|
||||
ValidateCreateComputePipelineAsync(&task);
|
||||
|
||||
// Verify task.computePipeline and anotherTask.computePipeline are pointing to the same Dawn
|
||||
// object.
|
||||
if (!UsesWire()) {
|
||||
EXPECT_EQ(task.computePipeline.Get(), anotherTask.computePipeline.Get());
|
||||
}
|
||||
}
|
||||
|
||||
DAWN_INSTANTIATE_TEST(CreatePipelineAsyncTest,
|
||||
D3D12Backend(),
|
||||
MetalBackend(),
|
||||
|
||||
Reference in New Issue
Block a user