blob: 144d99504b45a889273c6d0b7ef2447f7855d6c6 [file] [log] [blame]
Austin Eng05d9e2c2021-02-05 21:08:44 +00001// Copyright 2021 The Dawn Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "tests/unittests/validation/ValidationTest.h"
16
17#include "tests/MockCallback.h"
18
19using namespace testing;
20
21class MultipleDeviceTest : public ValidationTest {};
22
23// Test that it is invalid to submit a command buffer created on a different device.
24TEST_F(MultipleDeviceTest, ValidatesSameDevice) {
25 wgpu::Device device2 = RegisterDevice(CreateTestDevice());
26 wgpu::CommandBuffer commandBuffer = device2.CreateCommandEncoder().Finish();
27
28 ASSERT_DEVICE_ERROR(device.GetQueue().Submit(1, &commandBuffer));
29}
30
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000031// Test that CreatePipelineAsync fails creation with an Error status if it uses
Austin Eng05d9e2c2021-02-05 21:08:44 +000032// objects from a different device.
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000033TEST_F(MultipleDeviceTest, ValidatesSameDeviceCreatePipelineAsync) {
Austin Eng05d9e2c2021-02-05 21:08:44 +000034 wgpu::ShaderModuleWGSLDescriptor wgslDesc = {};
35 wgslDesc.source = R"(
Corentin Wallez21bd02b2021-04-13 09:48:24 +000036 [[stage(compute)]] fn main() {
Austin Eng05d9e2c2021-02-05 21:08:44 +000037 }
38 )";
39
40 wgpu::ShaderModuleDescriptor shaderModuleDesc = {};
41 shaderModuleDesc.nextInChain = &wgslDesc;
42
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000043 // Base case: CreateComputePipelineAsync succeeds.
Austin Eng05d9e2c2021-02-05 21:08:44 +000044 {
45 wgpu::ShaderModule shaderModule = device.CreateShaderModule(&shaderModuleDesc);
46
47 wgpu::ComputePipelineDescriptor pipelineDesc = {};
48 pipelineDesc.computeStage.module = shaderModule;
49 pipelineDesc.computeStage.entryPoint = "main";
50
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000051 StrictMock<MockCallback<WGPUCreateComputePipelineAsyncCallback>> creationCallback;
Austin Eng05d9e2c2021-02-05 21:08:44 +000052 EXPECT_CALL(creationCallback,
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000053 Call(WGPUCreatePipelineAsyncStatus_Success, NotNull(), _, this))
Austin Eng05d9e2c2021-02-05 21:08:44 +000054 .WillOnce(WithArg<1>(Invoke(
55 [](WGPUComputePipeline pipeline) { wgpu::ComputePipeline::Acquire(pipeline); })));
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000056 device.CreateComputePipelineAsync(&pipelineDesc, creationCallback.Callback(),
Austin Eng05d9e2c2021-02-05 21:08:44 +000057 creationCallback.MakeUserdata(this));
58
59 WaitForAllOperations(device);
60 }
61
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000062 // CreateComputePipelineAsync errors if the shader module is created on a different device.
Austin Eng05d9e2c2021-02-05 21:08:44 +000063 {
64 wgpu::Device device2 = RegisterDevice(CreateTestDevice());
65 wgpu::ShaderModule shaderModule = device2.CreateShaderModule(&shaderModuleDesc);
66
67 wgpu::ComputePipelineDescriptor pipelineDesc = {};
68 pipelineDesc.computeStage.module = shaderModule;
69 pipelineDesc.computeStage.entryPoint = "main";
70
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000071 StrictMock<MockCallback<WGPUCreateComputePipelineAsyncCallback>> creationCallback;
Austin Eng05d9e2c2021-02-05 21:08:44 +000072 EXPECT_CALL(creationCallback,
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000073 Call(WGPUCreatePipelineAsyncStatus_Error, nullptr, _, this + 1))
Austin Eng05d9e2c2021-02-05 21:08:44 +000074 .Times(1);
Corentin Wallez2d3c2e32021-02-22 18:27:36 +000075 device.CreateComputePipelineAsync(&pipelineDesc, creationCallback.Callback(),
Austin Eng05d9e2c2021-02-05 21:08:44 +000076 creationCallback.MakeUserdata(this + 1));
77
78 WaitForAllOperations(device);
79 }
80}