1 // Copyright 2020 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 #include "utils/ComboRenderBundleEncoderDescriptor.h"
19 #include "utils/ComboRenderPipelineDescriptor.h"
20 #include "utils/WGPUHelpers.h"
21
22 class UnsafeAPIValidationTest : public ValidationTest {
23 protected:
CreateTestDevice()24 WGPUDevice CreateTestDevice() override {
25 dawn_native::DawnDeviceDescriptor descriptor;
26 descriptor.forceEnabledToggles.push_back("disallow_unsafe_apis");
27 return adapter.CreateDevice(&descriptor);
28 }
29 };
30
31 // Check that pipeline overridable constants are disallowed as part of unsafe APIs.
32 // TODO(dawn:1041) Remove when implementation for all backend is added
TEST_F(UnsafeAPIValidationTest,PipelineOverridableConstants)33 TEST_F(UnsafeAPIValidationTest, PipelineOverridableConstants) {
34 // Create the dummy compute pipeline.
35 wgpu::ComputePipelineDescriptor pipelineDescBase;
36 pipelineDescBase.compute.entryPoint = "main";
37
38 // Control case: shader without overridable constant is allowed.
39 {
40 wgpu::ComputePipelineDescriptor pipelineDesc = pipelineDescBase;
41 pipelineDesc.compute.module =
42 utils::CreateShaderModule(device, "[[stage(compute), workgroup_size(1)]] fn main() {}");
43
44 device.CreateComputePipeline(&pipelineDesc);
45 }
46
47 // Error case: shader with overridable constant with default value
48 {
49 ASSERT_DEVICE_ERROR(utils::CreateShaderModule(device, R"(
50 [[override(1000)]] let c0: u32 = 1u;
51 [[override(1000)]] let c1: u32;
52
53 [[stage(compute), workgroup_size(1)]] fn main() {
54 _ = c0;
55 _ = c1;
56 })"));
57 }
58
59 // Error case: pipeline stage with constant entry is disallowed
60 {
61 wgpu::ComputePipelineDescriptor pipelineDesc = pipelineDescBase;
62 pipelineDesc.compute.module =
63 utils::CreateShaderModule(device, "[[stage(compute), workgroup_size(1)]] fn main() {}");
64 std::vector<wgpu::ConstantEntry> constants{{nullptr, "c", 1u}};
65 pipelineDesc.compute.constants = constants.data();
66 pipelineDesc.compute.constantCount = constants.size();
67 ASSERT_DEVICE_ERROR(device.CreateComputePipeline(&pipelineDesc));
68 }
69 }
70
71 class UnsafeQueryAPIValidationTest : public ValidationTest {
72 protected:
CreateTestDevice()73 WGPUDevice CreateTestDevice() override {
74 dawn_native::DawnDeviceDescriptor descriptor;
75 descriptor.requiredFeatures.push_back("pipeline-statistics-query");
76 descriptor.requiredFeatures.push_back("timestamp-query");
77 descriptor.forceEnabledToggles.push_back("disallow_unsafe_apis");
78 return adapter.CreateDevice(&descriptor);
79 }
80 };
81
82 // Check that pipeline statistics query are disallowed.
TEST_F(UnsafeQueryAPIValidationTest,PipelineStatisticsDisallowed)83 TEST_F(UnsafeQueryAPIValidationTest, PipelineStatisticsDisallowed) {
84 wgpu::QuerySetDescriptor descriptor;
85 descriptor.count = 1;
86
87 // Control case: occlusion query creation is allowed.
88 {
89 descriptor.type = wgpu::QueryType::Occlusion;
90 device.CreateQuerySet(&descriptor);
91 }
92
93 // Error case: pipeline statistics query creation is disallowed.
94 {
95 descriptor.type = wgpu::QueryType::PipelineStatistics;
96 std::vector<wgpu::PipelineStatisticName> pipelineStatistics = {
97 wgpu::PipelineStatisticName::VertexShaderInvocations};
98 descriptor.pipelineStatistics = pipelineStatistics.data();
99 descriptor.pipelineStatisticsCount = pipelineStatistics.size();
100 ASSERT_DEVICE_ERROR(device.CreateQuerySet(&descriptor));
101 }
102 }
103
104 // Check timestamp queries are disallowed.
TEST_F(UnsafeQueryAPIValidationTest,TimestampQueryDisallowed)105 TEST_F(UnsafeQueryAPIValidationTest, TimestampQueryDisallowed) {
106 wgpu::QuerySetDescriptor descriptor;
107 descriptor.count = 1;
108
109 // Control case: occlusion query creation is allowed.
110 {
111 descriptor.type = wgpu::QueryType::Occlusion;
112 device.CreateQuerySet(&descriptor);
113 }
114
115 // Error case: timestamp query creation is disallowed.
116 {
117 descriptor.type = wgpu::QueryType::Timestamp;
118 ASSERT_DEVICE_ERROR(device.CreateQuerySet(&descriptor));
119 }
120 }
121