• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common/Constants.h"
16 #include "tests/unittests/validation/ValidationTest.h"
17 #include "utils/WGPUHelpers.h"
18 
19 class ComputePipelineOverridableConstantsValidationTest : public ValidationTest {
20   protected:
SetUpShadersWithDefaultValueConstants()21     void SetUpShadersWithDefaultValueConstants() {
22         computeModule = utils::CreateShaderModule(device, R"(
23 [[override]] let c0: bool = true;      // type: bool
24 [[override]] let c1: bool = false;      // default override
25 [[override]] let c2: f32 = 0.0;         // type: float32
26 [[override]] let c3: f32 = 0.0;         // default override
27 [[override]] let c4: f32 = 4.0;         // default
28 [[override]] let c5: i32 = 0;           // type: int32
29 [[override]] let c6: i32 = 0;           // default override
30 [[override]] let c7: i32 = 7;           // default
31 [[override]] let c8: u32 = 0u;          // type: uint32
32 [[override]] let c9: u32 = 0u;          // default override
33 [[override(1000)]] let c10: u32 = 10u;  // default
34 
35 [[stage(compute), workgroup_size(1)]] fn main() {
36     // make sure the overridable constants are not optimized out
37     _ = u32(c0);
38     _ = u32(c1);
39     _ = u32(c2);
40     _ = u32(c3);
41     _ = u32(c4);
42     _ = u32(c5);
43     _ = u32(c6);
44     _ = u32(c7);
45     _ = u32(c8);
46     _ = u32(c9);
47     _ = u32(c10);
48 })");
49     }
50 
SetUpShadersWithUninitializedConstants()51     void SetUpShadersWithUninitializedConstants() {
52         computeModule = utils::CreateShaderModule(device, R"(
53 [[override]] let c0: bool;              // type: bool
54 [[override]] let c1: bool = false;      // default override
55 [[override]] let c2: f32;               // type: float32
56 [[override]] let c3: f32 = 0.0;         // default override
57 [[override]] let c4: f32 = 4.0;         // default
58 [[override]] let c5: i32;               // type: int32
59 [[override]] let c6: i32 = 0;           // default override
60 [[override]] let c7: i32 = 7;           // default
61 [[override]] let c8: u32;               // type: uint32
62 [[override]] let c9: u32 = 0u;          // default override
63 [[override(1000)]] let c10: u32 = 10u;  // default
64 
65 [[stage(compute), workgroup_size(1)]] fn main() {
66     // make sure the overridable constants are not optimized out
67     _ = u32(c0);
68     _ = u32(c1);
69     _ = u32(c2);
70     _ = u32(c3);
71     _ = u32(c4);
72     _ = u32(c5);
73     _ = u32(c6);
74     _ = u32(c7);
75     _ = u32(c8);
76     _ = u32(c9);
77     _ = u32(c10);
78 })");
79     }
80 
TestCreatePipeline(const std::vector<wgpu::ConstantEntry> & constants)81     void TestCreatePipeline(const std::vector<wgpu::ConstantEntry>& constants) {
82         wgpu::ComputePipelineDescriptor csDesc;
83         csDesc.compute.module = computeModule;
84         csDesc.compute.entryPoint = "main";
85         csDesc.compute.constants = constants.data();
86         csDesc.compute.constantCount = constants.size();
87         wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&csDesc);
88     }
89 
90     wgpu::ShaderModule computeModule;
91     wgpu::Buffer buffer;
92 };
93 
94 // Basic constants lookup tests
TEST_F(ComputePipelineOverridableConstantsValidationTest,ConstantsIdentifierLookUp)95 TEST_F(ComputePipelineOverridableConstantsValidationTest, ConstantsIdentifierLookUp) {
96     SetUpShadersWithDefaultValueConstants();
97     {
98         // Valid: no constants specified
99         std::vector<wgpu::ConstantEntry> constants;
100         TestCreatePipeline(constants);
101     }
102     {
103         // Valid: find by constant name
104         std::vector<wgpu::ConstantEntry> constants{{nullptr, "c0", 0}};
105         TestCreatePipeline(constants);
106     }
107     {
108         // Error: set the same constant twice
109         std::vector<wgpu::ConstantEntry> constants{
110             {nullptr, "c0", 0},
111             {nullptr, "c0", 1},
112         };
113         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
114     }
115     {
116         // Valid: find by constant numeric id
117         std::vector<wgpu::ConstantEntry> constants{{nullptr, "1000", 0}};
118         TestCreatePipeline(constants);
119     }
120     {
121         // Error: constant numeric id not specified
122         std::vector<wgpu::ConstantEntry> constants{{nullptr, "9999", 0}};
123         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
124     }
125     {
126         // Error: constant name doesn't exit
127         std::vector<wgpu::ConstantEntry> constants{{nullptr, "c99", 0}};
128         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
129     }
130 }
131 
132 // Test that it is invalid to leave any constants uninitialized
TEST_F(ComputePipelineOverridableConstantsValidationTest,UninitializedConstants)133 TEST_F(ComputePipelineOverridableConstantsValidationTest, UninitializedConstants) {
134     SetUpShadersWithUninitializedConstants();
135     {
136         // Error: uninitialized constants exist
137         std::vector<wgpu::ConstantEntry> constants;
138         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
139     }
140     {
141         // Error: uninitialized constants exist
142         std::vector<wgpu::ConstantEntry> constants{
143             {nullptr, "c0", false},
144             {nullptr, "c2", 1},
145             // c5 is missing
146             {nullptr, "c8", 1},
147         };
148         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
149     }
150     {
151         // Valid: all constants initialized
152         std::vector<wgpu::ConstantEntry> constants{
153             {nullptr, "c0", false},
154             {nullptr, "c2", 1},
155             {nullptr, "c5", 1},
156             {nullptr, "c8", 1},
157         };
158         TestCreatePipeline(constants);
159     }
160     {
161         // Error: duplicate initializations
162         std::vector<wgpu::ConstantEntry> constants{
163             {nullptr, "c0", false}, {nullptr, "c2", 1}, {nullptr, "c5", 1},
164             {nullptr, "c8", 1},     {nullptr, "c2", 2},
165         };
166         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
167     }
168 }
169 
170 // Test that only explicitly specified numeric ID can be referenced
TEST_F(ComputePipelineOverridableConstantsValidationTest,ConstantsIdentifierExplicitNumericID)171 TEST_F(ComputePipelineOverridableConstantsValidationTest, ConstantsIdentifierExplicitNumericID) {
172     SetUpShadersWithDefaultValueConstants();
173     {
174         // Error: constant numeric id not explicitly specified
175         // But could be impliciltly assigned to one of the constants
176         std::vector<wgpu::ConstantEntry> constants{{nullptr, "0", 0}};
177         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
178     }
179     {
180         // Error: constant numeric id not explicitly specified
181         // But could be impliciltly assigned to one of the constants
182         std::vector<wgpu::ConstantEntry> constants{{nullptr, "1", 0}};
183         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
184     }
185     {
186         // Error: constant numeric id not explicitly specified
187         // But could be impliciltly assigned to one of the constants
188         std::vector<wgpu::ConstantEntry> constants{{nullptr, "2", 0}};
189         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
190     }
191     {
192         // Error: constant numeric id not explicitly specified
193         // But could be impliciltly assigned to one of the constants
194         std::vector<wgpu::ConstantEntry> constants{{nullptr, "3", 0}};
195         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
196     }
197 }
198 
199 // Test that identifiers are unique
TEST_F(ComputePipelineOverridableConstantsValidationTest,ConstantsIdentifierUnique)200 TEST_F(ComputePipelineOverridableConstantsValidationTest, ConstantsIdentifierUnique) {
201     SetUpShadersWithDefaultValueConstants();
202     {
203         // Valid: constant without numeric id can be referenced with variable name
204         std::vector<wgpu::ConstantEntry> constants{{nullptr, "c0", 0}};
205         TestCreatePipeline(constants);
206     }
207     {
208         // Error: constant with numeric id cannot be referenced with variable name
209         std::vector<wgpu::ConstantEntry> constants{{nullptr, "c10", 0}};
210         ASSERT_DEVICE_ERROR(TestCreatePipeline(constants));
211     }
212 }