blob: 4cabbce1dbffd610c3a9ec56dd06340900e12195 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2023 The Dawn & Tint Authors
James Price75aaa492023-05-18 02:30:39 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
James Price75aaa492023-05-18 02:30:39 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
James Price75aaa492023-05-18 02:30:39 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
James Price75aaa492023-05-18 02:30:39 +000027
dan sinclair352f8c82023-07-21 00:40:07 +000028#include "src/tint/lang/core/type/pointer.h"
29#include "src/tint/lang/core/type/sampled_texture.h"
Ben Claytond368f2c2023-08-01 00:37:35 +000030#include "src/tint/lang/spirv/writer/common/helper_test.h"
James Price75aaa492023-05-18 02:30:39 +000031
dan sinclair2dddb192023-07-26 20:31:48 +000032namespace tint::spirv::writer {
James Price75aaa492023-05-18 02:30:39 +000033namespace {
34
Ben Claytona87e5d62023-08-15 14:56:16 +000035using namespace tint::core::fluent_types; // NOLINT
dan sinclairce6dffe2023-08-14 21:01:40 +000036using namespace tint::core::number_suffixes; // NOLINT
Ben Clayton138d5b72023-06-15 08:42:46 +000037
James Price6dbf49f2023-07-21 03:46:26 +000038TEST_F(SpirvWriterTest, FunctionVar_NoInit) {
Ben Claytonc67a4fa2023-06-09 19:34:20 +000039 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +000040 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +000041 b.Var("v", ty.ptr<function, i32>());
Ben Clayton26fbdaa2023-06-22 20:46:13 +000042 b.Return(func);
43 });
James Price75aaa492023-05-18 02:30:39 +000044
James Pricee9cd7192023-06-29 19:47:49 +000045 ASSERT_TRUE(Generate()) << Error() << output_;
46 EXPECT_INST("%v = OpVariable %_ptr_Function_int Function");
James Price75aaa492023-05-18 02:30:39 +000047}
48
James Price6dbf49f2023-07-21 03:46:26 +000049TEST_F(SpirvWriterTest, FunctionVar_WithInit) {
Ben Claytonc67a4fa2023-06-09 19:34:20 +000050 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +000051 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +000052 auto* v = b.Var("v", ty.ptr<function, i32>());
Ben Clayton26fbdaa2023-06-22 20:46:13 +000053 v->SetInitializer(b.Constant(42_i));
Ben Clayton26fbdaa2023-06-22 20:46:13 +000054 b.Return(func);
55 });
James Price75aaa492023-05-18 02:30:39 +000056
James Pricee9cd7192023-06-29 19:47:49 +000057 ASSERT_TRUE(Generate()) << Error() << output_;
58 EXPECT_INST("%v = OpVariable %_ptr_Function_int Function");
59 EXPECT_INST("OpStore %v %int_42");
James Price75aaa492023-05-18 02:30:39 +000060}
61
James Price6dbf49f2023-07-21 03:46:26 +000062TEST_F(SpirvWriterTest, FunctionVar_DeclInsideBlock) {
Ben Claytonc67a4fa2023-06-09 19:34:20 +000063 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +000064 b.Append(func->Block(), [&] {
Ben Clayton26fbdaa2023-06-22 20:46:13 +000065 auto* i = b.If(true);
James Priceecbb3ec2023-07-26 22:07:39 +000066 b.Append(i->True(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +000067 auto* v = b.Var("v", ty.ptr<function, i32>());
Ben Clayton26fbdaa2023-06-22 20:46:13 +000068 v->SetInitializer(b.Constant(42_i));
69 b.ExitIf(i);
70 });
Ben Clayton26fbdaa2023-06-22 20:46:13 +000071 b.Return(func);
72 });
dan sinclaircff18f32023-06-05 21:36:29 +000073
James Pricee9cd7192023-06-29 19:47:49 +000074 ASSERT_TRUE(Generate()) << Error() << output_;
75 EXPECT_INST(R"(
76 %foo = OpFunction %void None %3
77 %4 = OpLabel
78 %v = OpVariable %_ptr_Function_int Function
79 OpSelectionMerge %5 None
80 OpBranchConditional %true %6 %5
81 %6 = OpLabel
82 OpStore %v %int_42
83 OpBranch %5
84 %5 = OpLabel
85 OpReturn
86 OpFunctionEnd
James Price75aaa492023-05-18 02:30:39 +000087)");
88}
89
James Price6dbf49f2023-07-21 03:46:26 +000090TEST_F(SpirvWriterTest, FunctionVar_Load) {
Ben Claytonc67a4fa2023-06-09 19:34:20 +000091 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +000092 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +000093 auto* v = b.Var("v", ty.ptr<function, i32>());
94 auto* result = b.Load(v);
Ben Clayton26fbdaa2023-06-22 20:46:13 +000095 b.Return(func);
James Pricee9cd7192023-06-29 19:47:49 +000096 mod.SetName(result, "result");
Ben Clayton26fbdaa2023-06-22 20:46:13 +000097 });
James Price92151b22023-05-18 12:06:19 +000098
James Pricee9cd7192023-06-29 19:47:49 +000099 ASSERT_TRUE(Generate()) << Error() << output_;
100 EXPECT_INST("%v = OpVariable %_ptr_Function_int Function");
101 EXPECT_INST("%result = OpLoad %int %v");
James Price92151b22023-05-18 12:06:19 +0000102}
103
James Price6dbf49f2023-07-21 03:46:26 +0000104TEST_F(SpirvWriterTest, FunctionVar_Store) {
Ben Claytonc67a4fa2023-06-09 19:34:20 +0000105 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +0000106 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +0000107 auto* v = b.Var("v", ty.ptr<function, i32>());
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000108 b.Store(v, 42_i);
109 b.Return(func);
110 });
James Price97ab6a32023-05-18 03:05:59 +0000111
James Pricee9cd7192023-06-29 19:47:49 +0000112 ASSERT_TRUE(Generate()) << Error() << output_;
113 EXPECT_INST("%v = OpVariable %_ptr_Function_int Function");
114 EXPECT_INST("OpStore %v %int_42");
James Price97ab6a32023-05-18 03:05:59 +0000115}
116
James Price6dbf49f2023-07-21 03:46:26 +0000117TEST_F(SpirvWriterTest, PrivateVar_NoInit) {
dan sinclairb133c642023-09-26 00:59:00 +0000118 mod.root_block->Append(b.Var("v", ty.ptr<private_, i32>()));
James Price5e2130a2023-05-31 15:10:34 +0000119
James Pricee9cd7192023-06-29 19:47:49 +0000120 ASSERT_TRUE(Generate()) << Error() << output_;
121 EXPECT_INST("%v = OpVariable %_ptr_Private_int Private");
James Price5e2130a2023-05-31 15:10:34 +0000122}
123
James Price6dbf49f2023-07-21 03:46:26 +0000124TEST_F(SpirvWriterTest, PrivateVar_WithInit) {
James Pricee9cd7192023-06-29 19:47:49 +0000125 auto* v = b.Var("v", ty.ptr<private_, i32>());
James Price5e2130a2023-05-31 15:10:34 +0000126 v->SetInitializer(b.Constant(42_i));
dan sinclairb133c642023-09-26 00:59:00 +0000127 mod.root_block->Append(v);
James Price5e2130a2023-05-31 15:10:34 +0000128
James Pricee9cd7192023-06-29 19:47:49 +0000129 ASSERT_TRUE(Generate()) << Error() << output_;
130 EXPECT_INST("%v = OpVariable %_ptr_Private_int Private %int_42");
James Price5e2130a2023-05-31 15:10:34 +0000131}
132
James Price6dbf49f2023-07-21 03:46:26 +0000133TEST_F(SpirvWriterTest, PrivateVar_LoadAndStore) {
James Pricee9cd7192023-06-29 19:47:49 +0000134 auto* v = b.Var("v", ty.ptr<private_, i32>());
James Price5e2130a2023-05-31 15:10:34 +0000135 v->SetInitializer(b.Constant(42_i));
dan sinclairb133c642023-09-26 00:59:00 +0000136 mod.root_block->Append(v);
James Price5e2130a2023-05-31 15:10:34 +0000137
dan sinclair6f138fe2023-08-15 21:29:34 +0000138 auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
James Priceecbb3ec2023-07-26 22:07:39 +0000139 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +0000140 auto* load = b.Load(v);
141 auto* add = b.Add(ty.i32(), load, 1_i);
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000142 b.Store(v, add);
143 b.Return(func);
James Pricee9cd7192023-06-29 19:47:49 +0000144 mod.SetName(load, "load");
145 mod.SetName(add, "add");
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000146 });
James Price5e2130a2023-05-31 15:10:34 +0000147
James Pricee9cd7192023-06-29 19:47:49 +0000148 ASSERT_TRUE(Generate()) << Error() << output_;
149 EXPECT_INST("%v = OpVariable %_ptr_Private_int Private %int_42");
150 EXPECT_INST("%load = OpLoad %int %v");
151 EXPECT_INST("OpStore %v %add");
James Price5e2130a2023-05-31 15:10:34 +0000152}
153
James Price6dbf49f2023-07-21 03:46:26 +0000154TEST_F(SpirvWriterTest, WorkgroupVar) {
dan sinclairb133c642023-09-26 00:59:00 +0000155 mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>()));
James Pricedced7532023-05-31 15:14:49 +0000156
James Pricee9cd7192023-06-29 19:47:49 +0000157 ASSERT_TRUE(Generate()) << Error() << output_;
158 EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup");
James Pricedced7532023-05-31 15:14:49 +0000159}
160
James Price6dbf49f2023-07-21 03:46:26 +0000161TEST_F(SpirvWriterTest, WorkgroupVar_LoadAndStore) {
dan sinclairb133c642023-09-26 00:59:00 +0000162 auto* v = mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>()));
James Pricee9cd7192023-06-29 19:47:49 +0000163
dan sinclair6f138fe2023-08-15 21:29:34 +0000164 auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute,
Ben Claytonc67a4fa2023-06-09 19:34:20 +0000165 std::array{1u, 1u, 1u});
James Priceecbb3ec2023-07-26 22:07:39 +0000166 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +0000167 auto* load = b.Load(v);
168 auto* add = b.Add(ty.i32(), load, 1_i);
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000169 b.Store(v, add);
170 b.Return(func);
James Pricee9cd7192023-06-29 19:47:49 +0000171 mod.SetName(load, "load");
172 mod.SetName(add, "add");
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000173 });
James Pricedced7532023-05-31 15:14:49 +0000174
James Pricee9cd7192023-06-29 19:47:49 +0000175 ASSERT_TRUE(Generate()) << Error() << output_;
176 EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup");
177 EXPECT_INST("%load = OpLoad %int %v");
178 EXPECT_INST("OpStore %v %add");
James Pricedced7532023-05-31 15:14:49 +0000179}
180
James Price6dbf49f2023-07-21 03:46:26 +0000181TEST_F(SpirvWriterTest, WorkgroupVar_ZeroInitializeWithExtension) {
dan sinclairb133c642023-09-26 00:59:00 +0000182 mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>()));
James Pricedced7532023-05-31 15:14:49 +0000183
James Price90579412023-07-21 03:51:44 +0000184 // Create a writer with the zero_init_workgroup_memory flag set to `true`.
Ben Clayton22581552023-10-26 14:17:18 +0000185 ASSERT_TRUE(Generate({}, /* zero_init_workgroup_memory */ true)) << Error() << output_;
James Pricee9cd7192023-06-29 19:47:49 +0000186 EXPECT_INST("%4 = OpConstantNull %int");
187 EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup %4");
James Pricedced7532023-05-31 15:14:49 +0000188}
189
James Price72643882023-09-16 00:28:22 +0000190TEST_F(SpirvWriterTest, StorageVar_ReadOnly) {
191 auto* v = b.Var("v", ty.ptr<storage, i32, read>());
James Price117d4f32023-06-07 23:48:21 +0000192 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000193 mod.root_block->Append(v);
James Price117d4f32023-06-07 23:48:21 +0000194
James Pricee9cd7192023-06-29 19:47:49 +0000195 ASSERT_TRUE(Generate()) << Error() << output_;
196 EXPECT_INST(R"(
197 OpDecorate %tint_symbol_1 Block
198 OpDecorate %1 DescriptorSet 0
199 OpDecorate %1 Binding 0
James Price72643882023-09-16 00:28:22 +0000200 OpDecorate %1 NonWritable
James Price117d4f32023-06-07 23:48:21 +0000201)");
James Pricee9cd7192023-06-29 19:47:49 +0000202 EXPECT_INST(R"(
203%tint_symbol_1 = OpTypeStruct %int
204%_ptr_StorageBuffer_tint_symbol_1 = OpTypePointer StorageBuffer %tint_symbol_1
205 %1 = OpVariable %_ptr_StorageBuffer_tint_symbol_1 StorageBuffer
James Price117d4f32023-06-07 23:48:21 +0000206)");
207}
208
James Price6dbf49f2023-07-21 03:46:26 +0000209TEST_F(SpirvWriterTest, StorageVar_LoadAndStore) {
James Price72643882023-09-16 00:28:22 +0000210 auto* v = b.Var("v", ty.ptr<storage, i32, read_write>());
James Price117d4f32023-06-07 23:48:21 +0000211 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000212 mod.root_block->Append(v);
James Price117d4f32023-06-07 23:48:21 +0000213
dan sinclair6f138fe2023-08-15 21:29:34 +0000214 auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute,
Ben Claytonc67a4fa2023-06-09 19:34:20 +0000215 std::array{1u, 1u, 1u});
James Priceecbb3ec2023-07-26 22:07:39 +0000216 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +0000217 auto* load = b.Load(v);
218 auto* add = b.Add(ty.i32(), load, 1_i);
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000219 b.Store(v, add);
220 b.Return(func);
James Pricee9cd7192023-06-29 19:47:49 +0000221 mod.SetName(load, "load");
222 mod.SetName(add, "add");
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000223 });
James Price117d4f32023-06-07 23:48:21 +0000224
James Pricee9cd7192023-06-29 19:47:49 +0000225 ASSERT_TRUE(Generate()) << Error() << output_;
226 EXPECT_INST(R"(
227 %9 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0
228 %load = OpLoad %int %9
229 %add = OpIAdd %int %load %int_1
230 %16 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0
231 OpStore %16 %add
James Price117d4f32023-06-07 23:48:21 +0000232)");
233}
234
James Price72643882023-09-16 00:28:22 +0000235TEST_F(SpirvWriterTest, StorageVar_WriteOnly) {
236 auto* v = b.Var("v", ty.ptr<storage, i32, write>());
237 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000238 mod.root_block->Append(v);
James Price72643882023-09-16 00:28:22 +0000239
240 auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute,
241 std::array{1u, 1u, 1u});
242 b.Append(func->Block(), [&] {
243 b.Store(v, 42_i);
244 b.Return(func);
245 });
246
247 ASSERT_TRUE(Generate()) << Error() << output_;
248 EXPECT_INST(R"(
249 OpDecorate %tint_symbol_1 Block
250 OpDecorate %1 DescriptorSet 0
251 OpDecorate %1 Binding 0
252 OpDecorate %1 NonReadable
253)");
254 EXPECT_INST(R"(
255 %9 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0
256 OpStore %9 %int_42
257)");
258}
259
James Price6dbf49f2023-07-21 03:46:26 +0000260TEST_F(SpirvWriterTest, UniformVar) {
James Pricee9cd7192023-06-29 19:47:49 +0000261 auto* v = b.Var("v", ty.ptr<uniform, i32>());
James Price117d4f32023-06-07 23:48:21 +0000262 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000263 mod.root_block->Append(v);
James Price117d4f32023-06-07 23:48:21 +0000264
James Pricee9cd7192023-06-29 19:47:49 +0000265 ASSERT_TRUE(Generate()) << Error() << output_;
266 EXPECT_INST(R"(
267 OpDecorate %tint_symbol_1 Block
268 OpDecorate %1 DescriptorSet 0
269 OpDecorate %1 Binding 0
James Price117d4f32023-06-07 23:48:21 +0000270)");
James Pricee9cd7192023-06-29 19:47:49 +0000271 EXPECT_INST(R"(
272%tint_symbol_1 = OpTypeStruct %int
273%_ptr_Uniform_tint_symbol_1 = OpTypePointer Uniform %tint_symbol_1
274 %1 = OpVariable %_ptr_Uniform_tint_symbol_1 Uniform
James Price117d4f32023-06-07 23:48:21 +0000275)");
276}
277
James Price6dbf49f2023-07-21 03:46:26 +0000278TEST_F(SpirvWriterTest, UniformVar_Load) {
James Pricee9cd7192023-06-29 19:47:49 +0000279 auto* v = b.Var("v", ty.ptr<uniform, i32>());
James Price117d4f32023-06-07 23:48:21 +0000280 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000281 mod.root_block->Append(v);
James Price117d4f32023-06-07 23:48:21 +0000282
dan sinclair6f138fe2023-08-15 21:29:34 +0000283 auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute,
Ben Claytonc67a4fa2023-06-09 19:34:20 +0000284 std::array{1u, 1u, 1u});
James Priceecbb3ec2023-07-26 22:07:39 +0000285 b.Append(func->Block(), [&] {
James Pricee9cd7192023-06-29 19:47:49 +0000286 auto* load = b.Load(v);
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000287 b.Return(func);
James Pricee9cd7192023-06-29 19:47:49 +0000288 mod.SetName(load, "load");
Ben Clayton26fbdaa2023-06-22 20:46:13 +0000289 });
James Price117d4f32023-06-07 23:48:21 +0000290
James Pricee9cd7192023-06-29 19:47:49 +0000291 ASSERT_TRUE(Generate()) << Error() << output_;
292 EXPECT_INST(R"(
293 %9 = OpAccessChain %_ptr_Uniform_int %1 %uint_0
294 %load = OpLoad %int %9
James Price117d4f32023-06-07 23:48:21 +0000295)");
296}
297
James Price2e593642023-07-21 15:23:18 +0000298TEST_F(SpirvWriterTest, PushConstantVar) {
299 auto* v = b.Var("v", ty.ptr<push_constant, i32>());
dan sinclairb133c642023-09-26 00:59:00 +0000300 mod.root_block->Append(v);
James Price2e593642023-07-21 15:23:18 +0000301
302 ASSERT_TRUE(Generate()) << Error() << output_;
303 EXPECT_INST(R"(
304 OpDecorate %tint_symbol_1 Block
305)");
306 EXPECT_INST(R"(
307%tint_symbol_1 = OpTypeStruct %int
308%_ptr_PushConstant_tint_symbol_1 = OpTypePointer PushConstant %tint_symbol_1
309 %1 = OpVariable %_ptr_PushConstant_tint_symbol_1 PushConstant
310)");
311}
312
313TEST_F(SpirvWriterTest, PushConstantVar_Load) {
314 auto* v = b.Var("v", ty.ptr<push_constant, i32>());
dan sinclairb133c642023-09-26 00:59:00 +0000315 mod.root_block->Append(v);
James Price2e593642023-07-21 15:23:18 +0000316
317 auto* func = b.Function("foo", ty.i32());
James Priceecbb3ec2023-07-26 22:07:39 +0000318 b.Append(func->Block(), [&] {
James Price2e593642023-07-21 15:23:18 +0000319 auto* load = b.Load(v);
320 b.Return(func, load);
321 mod.SetName(load, "load");
322 });
323
324 ASSERT_TRUE(Generate()) << Error() << output_;
325 EXPECT_INST(R"(
326 %8 = OpAccessChain %_ptr_PushConstant_int %1 %uint_0
327 %load = OpLoad %int %8
328 OpReturnValue %load
329)");
330}
331
James Price6dbf49f2023-07-21 03:46:26 +0000332TEST_F(SpirvWriterTest, SamplerVar) {
Ben Claytoncd52f382023-08-07 13:11:08 +0000333 auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, ty.sampler(), core::Access::kRead));
James Price8b64bd72023-06-30 13:09:32 +0000334 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000335 mod.root_block->Append(v);
James Price8b64bd72023-06-30 13:09:32 +0000336
337 ASSERT_TRUE(Generate()) << Error() << output_;
338 EXPECT_INST(R"(
339 OpDecorate %v DescriptorSet 0
340 OpDecorate %v Binding 0
341)");
342 EXPECT_INST(R"(
343 %3 = OpTypeSampler
344%_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3
345 %v = OpVariable %_ptr_UniformConstant_3 UniformConstant
346)");
347}
348
James Price6dbf49f2023-07-21 03:46:26 +0000349TEST_F(SpirvWriterTest, SamplerVar_Load) {
Ben Claytoncd52f382023-08-07 13:11:08 +0000350 auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, ty.sampler(), core::Access::kRead));
James Price8b64bd72023-06-30 13:09:32 +0000351 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000352 mod.root_block->Append(v);
James Price8b64bd72023-06-30 13:09:32 +0000353
354 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +0000355 b.Append(func->Block(), [&] {
James Price8b64bd72023-06-30 13:09:32 +0000356 auto* load = b.Load(v);
357 b.Return(func);
358 mod.SetName(load, "load");
359 });
360
361 ASSERT_TRUE(Generate()) << Error() << output_;
362 EXPECT_INST("%load = OpLoad %3 %v");
363}
364
James Price6dbf49f2023-07-21 03:46:26 +0000365TEST_F(SpirvWriterTest, TextureVar) {
dan sinclaircedcdf32023-08-10 02:39:48 +0000366 auto* v = b.Var(
367 "v", ty.ptr(core::AddressSpace::kHandle,
368 ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()),
369 core::Access::kRead));
James Price8b64bd72023-06-30 13:09:32 +0000370 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000371 mod.root_block->Append(v);
James Price8b64bd72023-06-30 13:09:32 +0000372
373 ASSERT_TRUE(Generate()) << Error() << output_;
374 EXPECT_INST(R"(
375 OpDecorate %v DescriptorSet 0
376 OpDecorate %v Binding 0
377)");
378 EXPECT_INST(R"(
379 %3 = OpTypeImage %float 2D 0 0 0 1 Unknown
380%_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3
381 %v = OpVariable %_ptr_UniformConstant_3 UniformConstant
382)");
383}
384
James Price6dbf49f2023-07-21 03:46:26 +0000385TEST_F(SpirvWriterTest, TextureVar_Load) {
dan sinclaircedcdf32023-08-10 02:39:48 +0000386 auto* v = b.Var(
387 "v", ty.ptr(core::AddressSpace::kHandle,
388 ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()),
389 core::Access::kRead));
James Price8b64bd72023-06-30 13:09:32 +0000390 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000391 mod.root_block->Append(v);
James Price8b64bd72023-06-30 13:09:32 +0000392
393 auto* func = b.Function("foo", ty.void_());
James Priceecbb3ec2023-07-26 22:07:39 +0000394 b.Append(func->Block(), [&] {
James Price8b64bd72023-06-30 13:09:32 +0000395 auto* load = b.Load(v);
396 b.Return(func);
397 mod.SetName(load, "load");
398 });
399
400 ASSERT_TRUE(Generate()) << Error() << output_;
401 EXPECT_INST("%load = OpLoad %3 %v");
402}
403
James Price72643882023-09-16 00:28:22 +0000404TEST_F(SpirvWriterTest, ReadOnlyStorageTextureVar) {
405 auto format = core::TexelFormat::kRgba8Unorm;
406 auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle,
407 ty.Get<core::type::StorageTexture>(
408 core::type::TextureDimension::k2d, format, read,
409 core::type::StorageTexture::SubtypeFor(format, ty)),
410 core::Access::kRead));
411 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000412 mod.root_block->Append(v);
James Price72643882023-09-16 00:28:22 +0000413
414 ASSERT_TRUE(Generate()) << Error() << output_;
415 EXPECT_INST(R"(
416 OpDecorate %v DescriptorSet 0
417 OpDecorate %v Binding 0
418 OpDecorate %v NonWritable
419)");
420 EXPECT_INST(R"(
421 %3 = OpTypeImage %float 2D 0 0 0 2 Rgba8
422%_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3
423 %v = OpVariable %_ptr_UniformConstant_3 UniformConstant
424)");
425}
426
427TEST_F(SpirvWriterTest, ReadWriteStorageTextureVar) {
428 auto format = core::TexelFormat::kRgba8Unorm;
429 auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle,
430 ty.Get<core::type::StorageTexture>(
431 core::type::TextureDimension::k2d, format, read_write,
432 core::type::StorageTexture::SubtypeFor(format, ty)),
433 core::Access::kRead));
434 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000435 mod.root_block->Append(v);
James Price72643882023-09-16 00:28:22 +0000436
437 ASSERT_TRUE(Generate()) << Error() << output_;
438 EXPECT_INST(R"(
439 OpDecorate %v DescriptorSet 0
440 OpDecorate %v Binding 0
441)");
442 EXPECT_INST(R"(
443 %3 = OpTypeImage %float 2D 0 0 0 2 Rgba8
444%_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3
445 %v = OpVariable %_ptr_UniformConstant_3 UniformConstant
446)");
447}
448
449TEST_F(SpirvWriterTest, WriteOnlyStorageTextureVar) {
450 auto format = core::TexelFormat::kRgba8Unorm;
451 auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle,
452 ty.Get<core::type::StorageTexture>(
453 core::type::TextureDimension::k2d, format, write,
454 core::type::StorageTexture::SubtypeFor(format, ty)),
455 core::Access::kRead));
456 v->SetBindingPoint(0, 0);
dan sinclairb133c642023-09-26 00:59:00 +0000457 mod.root_block->Append(v);
James Price72643882023-09-16 00:28:22 +0000458
459 ASSERT_TRUE(Generate()) << Error() << output_;
460 EXPECT_INST(R"(
461 OpDecorate %v DescriptorSet 0
462 OpDecorate %v Binding 0
463 OpDecorate %v NonReadable
464)");
465 EXPECT_INST(R"(
466 %3 = OpTypeImage %float 2D 0 0 0 2 Rgba8
467%_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3
468 %v = OpVariable %_ptr_UniformConstant_3 UniformConstant
469)");
470}
471
James Price75aaa492023-05-18 02:30:39 +0000472} // namespace
dan sinclair2dddb192023-07-26 20:31:48 +0000473} // namespace tint::spirv::writer