1 // 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 <gtest/gtest.h>
16
17 #include "dawn_native/ChainUtils_autogen.h"
18 #include "dawn_native/dawn_platform.h"
19
20 // Checks that we cannot find any structs in an empty chain
TEST(ChainUtilsTests,FindEmptyChain)21 TEST(ChainUtilsTests, FindEmptyChain) {
22 const dawn_native::PrimitiveDepthClampingState* info = nullptr;
23 dawn_native::FindInChain(nullptr, &info);
24
25 ASSERT_EQ(nullptr, info);
26 }
27
28 // Checks that searching a chain for a present struct returns that struct
TEST(ChainUtilsTests,FindPresentInChain)29 TEST(ChainUtilsTests, FindPresentInChain) {
30 dawn_native::PrimitiveDepthClampingState chain1;
31 dawn_native::ShaderModuleSPIRVDescriptor chain2;
32 chain1.nextInChain = &chain2;
33 const dawn_native::PrimitiveDepthClampingState* info1 = nullptr;
34 const dawn_native::ShaderModuleSPIRVDescriptor* info2 = nullptr;
35 dawn_native::FindInChain(&chain1, &info1);
36 dawn_native::FindInChain(&chain1, &info2);
37
38 ASSERT_NE(nullptr, info1);
39 ASSERT_NE(nullptr, info2);
40 }
41
42 // Checks that searching a chain for a struct that doesn't exist returns a nullptr
TEST(ChainUtilsTests,FindMissingInChain)43 TEST(ChainUtilsTests, FindMissingInChain) {
44 dawn_native::PrimitiveDepthClampingState chain1;
45 dawn_native::ShaderModuleSPIRVDescriptor chain2;
46 chain1.nextInChain = &chain2;
47 const dawn_native::SurfaceDescriptorFromMetalLayer* info = nullptr;
48 dawn_native::FindInChain(&chain1, &info);
49
50 ASSERT_EQ(nullptr, info);
51 }
52
53 // Checks that validation rejects chains with duplicate STypes
TEST(ChainUtilsTests,ValidateDuplicateSTypes)54 TEST(ChainUtilsTests, ValidateDuplicateSTypes) {
55 dawn_native::PrimitiveDepthClampingState chain1;
56 dawn_native::ShaderModuleSPIRVDescriptor chain2;
57 dawn_native::PrimitiveDepthClampingState chain3;
58 chain1.nextInChain = &chain2;
59 chain2.nextInChain = &chain3;
60
61 dawn_native::MaybeError result = dawn_native::ValidateSTypes(&chain1, {});
62 ASSERT_TRUE(result.IsError());
63 result.AcquireError();
64 }
65
66 // Checks that validation rejects chains that contain unspecified STypes
TEST(ChainUtilsTests,ValidateUnspecifiedSTypes)67 TEST(ChainUtilsTests, ValidateUnspecifiedSTypes) {
68 dawn_native::PrimitiveDepthClampingState chain1;
69 dawn_native::ShaderModuleSPIRVDescriptor chain2;
70 dawn_native::ShaderModuleWGSLDescriptor chain3;
71 chain1.nextInChain = &chain2;
72 chain2.nextInChain = &chain3;
73
74 dawn_native::MaybeError result = dawn_native::ValidateSTypes(&chain1, {
75 {wgpu::SType::PrimitiveDepthClampingState},
76 {wgpu::SType::ShaderModuleSPIRVDescriptor},
77 });
78 ASSERT_TRUE(result.IsError());
79 result.AcquireError();
80 }
81
82 // Checks that validation rejects chains that contain multiple STypes from the same oneof
83 // constraint.
TEST(ChainUtilsTests,ValidateOneOfFailure)84 TEST(ChainUtilsTests, ValidateOneOfFailure) {
85 dawn_native::PrimitiveDepthClampingState chain1;
86 dawn_native::ShaderModuleSPIRVDescriptor chain2;
87 dawn_native::ShaderModuleWGSLDescriptor chain3;
88 chain1.nextInChain = &chain2;
89 chain2.nextInChain = &chain3;
90
91 dawn_native::MaybeError result = dawn_native::ValidateSTypes(&chain1,
92 {{wgpu::SType::ShaderModuleSPIRVDescriptor, wgpu::SType::ShaderModuleWGSLDescriptor}});
93 ASSERT_TRUE(result.IsError());
94 result.AcquireError();
95 }
96
97 // Checks that validation accepts chains that match the constraints.
TEST(ChainUtilsTests,ValidateSuccess)98 TEST(ChainUtilsTests, ValidateSuccess) {
99 dawn_native::PrimitiveDepthClampingState chain1;
100 dawn_native::ShaderModuleSPIRVDescriptor chain2;
101 chain1.nextInChain = &chain2;
102
103 dawn_native::MaybeError result = dawn_native::ValidateSTypes(&chain1, {
104 {wgpu::SType::ShaderModuleSPIRVDescriptor, wgpu::SType::ShaderModuleWGSLDescriptor},
105 {wgpu::SType::PrimitiveDepthClampingState},
106 {wgpu::SType::SurfaceDescriptorFromMetalLayer},
107 });
108 ASSERT_TRUE(result.IsSuccess());
109 }
110
111 // Checks that validation always passes on empty chains.
TEST(ChainUtilsTests,ValidateEmptyChain)112 TEST(ChainUtilsTests, ValidateEmptyChain) {
113 dawn_native::MaybeError result = dawn_native::ValidateSTypes(nullptr, {
114 {wgpu::SType::ShaderModuleSPIRVDescriptor},
115 {wgpu::SType::PrimitiveDepthClampingState},
116 });
117 ASSERT_TRUE(result.IsSuccess());
118
119 result = dawn_native::ValidateSTypes(nullptr, {});
120 ASSERT_TRUE(result.IsSuccess());
121 }
122
123 // Checks that singleton validation always passes on empty chains.
TEST(ChainUtilsTests,ValidateSingleEmptyChain)124 TEST(ChainUtilsTests, ValidateSingleEmptyChain) {
125 dawn_native::MaybeError result = dawn_native::ValidateSingleSType(nullptr,
126 wgpu::SType::ShaderModuleSPIRVDescriptor);
127 ASSERT_TRUE(result.IsSuccess());
128
129 result = dawn_native::ValidateSingleSType(nullptr,
130 wgpu::SType::ShaderModuleSPIRVDescriptor, wgpu::SType::PrimitiveDepthClampingState);
131 ASSERT_TRUE(result.IsSuccess());
132 }
133
134 // Checks that singleton validation always fails on chains with multiple children.
TEST(ChainUtilsTests,ValidateSingleMultiChain)135 TEST(ChainUtilsTests, ValidateSingleMultiChain) {
136 dawn_native::PrimitiveDepthClampingState chain1;
137 dawn_native::ShaderModuleSPIRVDescriptor chain2;
138 chain1.nextInChain = &chain2;
139
140 dawn_native::MaybeError result = dawn_native::ValidateSingleSType(&chain1,
141 wgpu::SType::PrimitiveDepthClampingState);
142 ASSERT_TRUE(result.IsError());
143 result.AcquireError();
144
145 result = dawn_native::ValidateSingleSType(&chain1,
146 wgpu::SType::PrimitiveDepthClampingState, wgpu::SType::ShaderModuleSPIRVDescriptor);
147 ASSERT_TRUE(result.IsError());
148 result.AcquireError();
149 }
150
151 // Checks that singleton validation passes when the oneof constraint is met.
TEST(ChainUtilsTests,ValidateSingleSatisfied)152 TEST(ChainUtilsTests, ValidateSingleSatisfied) {
153 dawn_native::ShaderModuleWGSLDescriptor chain1;
154
155 dawn_native::MaybeError result = dawn_native::ValidateSingleSType(&chain1,
156 wgpu::SType::ShaderModuleWGSLDescriptor);
157 ASSERT_TRUE(result.IsSuccess());
158
159 result = dawn_native::ValidateSingleSType(&chain1,
160 wgpu::SType::ShaderModuleSPIRVDescriptor, wgpu::SType::ShaderModuleWGSLDescriptor);
161 ASSERT_TRUE(result.IsSuccess());
162
163 result = dawn_native::ValidateSingleSType(&chain1,
164 wgpu::SType::ShaderModuleWGSLDescriptor, wgpu::SType::ShaderModuleSPIRVDescriptor);
165 ASSERT_TRUE(result.IsSuccess());
166 }
167
168 // Checks that singleton validation passes when the oneof constraint is not met.
TEST(ChainUtilsTests,ValidateSingleUnsatisfied)169 TEST(ChainUtilsTests, ValidateSingleUnsatisfied) {
170 dawn_native::PrimitiveDepthClampingState chain1;
171
172 dawn_native::MaybeError result = dawn_native::ValidateSingleSType(&chain1,
173 wgpu::SType::ShaderModuleWGSLDescriptor);
174 ASSERT_TRUE(result.IsError());
175 result.AcquireError();
176
177 result = dawn_native::ValidateSingleSType(&chain1,
178 wgpu::SType::ShaderModuleSPIRVDescriptor, wgpu::SType::ShaderModuleWGSLDescriptor);
179 ASSERT_TRUE(result.IsError());
180 result.AcquireError();
181 }
182