Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2023 The Dawn & Tint Authors |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 27 | |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/core/type/pointer.h" |
| 29 | #include "src/tint/lang/core/type/sampled_texture.h" |
Ben Clayton | d368f2c | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 30 | #include "src/tint/lang/spirv/writer/common/helper_test.h" |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 31 | |
dan sinclair | 2dddb19 | 2023-07-26 20:31:48 +0000 | [diff] [blame] | 32 | namespace tint::spirv::writer { |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Ben Clayton | a87e5d6 | 2023-08-15 14:56:16 +0000 | [diff] [blame] | 35 | using namespace tint::core::fluent_types; // NOLINT |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 36 | using namespace tint::core::number_suffixes; // NOLINT |
Ben Clayton | 138d5b7 | 2023-06-15 08:42:46 +0000 | [diff] [blame] | 37 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 38 | TEST_F(SpirvWriterTest, FunctionVar_NoInit) { |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 39 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 40 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 41 | b.Var("v", ty.ptr<function, i32>()); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 42 | b.Return(func); |
| 43 | }); |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 44 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 45 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 46 | EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 49 | TEST_F(SpirvWriterTest, FunctionVar_WithInit) { |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 50 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 51 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 52 | auto* v = b.Var("v", ty.ptr<function, i32>()); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 53 | v->SetInitializer(b.Constant(42_i)); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 54 | b.Return(func); |
| 55 | }); |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 56 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 57 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 58 | EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| 59 | EXPECT_INST("OpStore %v %int_42"); |
James Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 62 | TEST_F(SpirvWriterTest, FunctionVar_DeclInsideBlock) { |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 63 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 64 | b.Append(func->Block(), [&] { |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 65 | auto* i = b.If(true); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 66 | b.Append(i->True(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 67 | auto* v = b.Var("v", ty.ptr<function, i32>()); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 68 | v->SetInitializer(b.Constant(42_i)); |
| 69 | b.ExitIf(i); |
| 70 | }); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 71 | b.Return(func); |
| 72 | }); |
dan sinclair | cff18f3 | 2023-06-05 21:36:29 +0000 | [diff] [blame] | 73 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 74 | 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 Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 87 | )"); |
| 88 | } |
| 89 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 90 | TEST_F(SpirvWriterTest, FunctionVar_Load) { |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 91 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 92 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 93 | auto* v = b.Var("v", ty.ptr<function, i32>()); |
| 94 | auto* result = b.Load(v); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 95 | b.Return(func); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 96 | mod.SetName(result, "result"); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 97 | }); |
James Price | 92151b2 | 2023-05-18 12:06:19 +0000 | [diff] [blame] | 98 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 99 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 100 | EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| 101 | EXPECT_INST("%result = OpLoad %int %v"); |
James Price | 92151b2 | 2023-05-18 12:06:19 +0000 | [diff] [blame] | 102 | } |
| 103 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 104 | TEST_F(SpirvWriterTest, FunctionVar_Store) { |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 105 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 106 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 107 | auto* v = b.Var("v", ty.ptr<function, i32>()); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 108 | b.Store(v, 42_i); |
| 109 | b.Return(func); |
| 110 | }); |
James Price | 97ab6a3 | 2023-05-18 03:05:59 +0000 | [diff] [blame] | 111 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 112 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 113 | EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| 114 | EXPECT_INST("OpStore %v %int_42"); |
James Price | 97ab6a3 | 2023-05-18 03:05:59 +0000 | [diff] [blame] | 115 | } |
| 116 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 117 | TEST_F(SpirvWriterTest, PrivateVar_NoInit) { |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 118 | mod.root_block->Append(b.Var("v", ty.ptr<private_, i32>())); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 119 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 120 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 121 | EXPECT_INST("%v = OpVariable %_ptr_Private_int Private"); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 122 | } |
| 123 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 124 | TEST_F(SpirvWriterTest, PrivateVar_WithInit) { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 125 | auto* v = b.Var("v", ty.ptr<private_, i32>()); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 126 | v->SetInitializer(b.Constant(42_i)); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 127 | mod.root_block->Append(v); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 128 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 129 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 130 | EXPECT_INST("%v = OpVariable %_ptr_Private_int Private %int_42"); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 131 | } |
| 132 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 133 | TEST_F(SpirvWriterTest, PrivateVar_LoadAndStore) { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 134 | auto* v = b.Var("v", ty.ptr<private_, i32>()); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 135 | v->SetInitializer(b.Constant(42_i)); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 136 | mod.root_block->Append(v); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 137 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 138 | auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 139 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 140 | auto* load = b.Load(v); |
| 141 | auto* add = b.Add(ty.i32(), load, 1_i); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 142 | b.Store(v, add); |
| 143 | b.Return(func); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 144 | mod.SetName(load, "load"); |
| 145 | mod.SetName(add, "add"); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 146 | }); |
James Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 147 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 148 | 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 Price | 5e2130a | 2023-05-31 15:10:34 +0000 | [diff] [blame] | 152 | } |
| 153 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 154 | TEST_F(SpirvWriterTest, WorkgroupVar) { |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 155 | mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>())); |
James Price | dced753 | 2023-05-31 15:14:49 +0000 | [diff] [blame] | 156 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 157 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 158 | EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup"); |
James Price | dced753 | 2023-05-31 15:14:49 +0000 | [diff] [blame] | 159 | } |
| 160 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 161 | TEST_F(SpirvWriterTest, WorkgroupVar_LoadAndStore) { |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 162 | auto* v = mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>())); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 163 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 164 | auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 165 | std::array{1u, 1u, 1u}); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 166 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 167 | auto* load = b.Load(v); |
| 168 | auto* add = b.Add(ty.i32(), load, 1_i); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 169 | b.Store(v, add); |
| 170 | b.Return(func); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 171 | mod.SetName(load, "load"); |
| 172 | mod.SetName(add, "add"); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 173 | }); |
James Price | dced753 | 2023-05-31 15:14:49 +0000 | [diff] [blame] | 174 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 175 | 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 Price | dced753 | 2023-05-31 15:14:49 +0000 | [diff] [blame] | 179 | } |
| 180 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 181 | TEST_F(SpirvWriterTest, WorkgroupVar_ZeroInitializeWithExtension) { |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 182 | mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>())); |
James Price | dced753 | 2023-05-31 15:14:49 +0000 | [diff] [blame] | 183 | |
James Price | 9057941 | 2023-07-21 03:51:44 +0000 | [diff] [blame] | 184 | // Create a writer with the zero_init_workgroup_memory flag set to `true`. |
Ben Clayton | 2258155 | 2023-10-26 14:17:18 +0000 | [diff] [blame] | 185 | ASSERT_TRUE(Generate({}, /* zero_init_workgroup_memory */ true)) << Error() << output_; |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 186 | EXPECT_INST("%4 = OpConstantNull %int"); |
| 187 | EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup %4"); |
James Price | dced753 | 2023-05-31 15:14:49 +0000 | [diff] [blame] | 188 | } |
| 189 | |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 190 | TEST_F(SpirvWriterTest, StorageVar_ReadOnly) { |
| 191 | auto* v = b.Var("v", ty.ptr<storage, i32, read>()); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 192 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 193 | mod.root_block->Append(v); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 194 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 195 | 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 Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 200 | OpDecorate %1 NonWritable |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 201 | )"); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 202 | 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 Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 206 | )"); |
| 207 | } |
| 208 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 209 | TEST_F(SpirvWriterTest, StorageVar_LoadAndStore) { |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 210 | auto* v = b.Var("v", ty.ptr<storage, i32, read_write>()); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 211 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 212 | mod.root_block->Append(v); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 213 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 214 | auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 215 | std::array{1u, 1u, 1u}); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 216 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 217 | auto* load = b.Load(v); |
| 218 | auto* add = b.Add(ty.i32(), load, 1_i); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 219 | b.Store(v, add); |
| 220 | b.Return(func); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 221 | mod.SetName(load, "load"); |
| 222 | mod.SetName(add, "add"); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 223 | }); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 224 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 225 | 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 Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 232 | )"); |
| 233 | } |
| 234 | |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 235 | TEST_F(SpirvWriterTest, StorageVar_WriteOnly) { |
| 236 | auto* v = b.Var("v", ty.ptr<storage, i32, write>()); |
| 237 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 238 | mod.root_block->Append(v); |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 239 | |
| 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 Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 260 | TEST_F(SpirvWriterTest, UniformVar) { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 261 | auto* v = b.Var("v", ty.ptr<uniform, i32>()); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 262 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 263 | mod.root_block->Append(v); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 264 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 265 | 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 Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 270 | )"); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 271 | 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 Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 275 | )"); |
| 276 | } |
| 277 | |
James Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 278 | TEST_F(SpirvWriterTest, UniformVar_Load) { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 279 | auto* v = b.Var("v", ty.ptr<uniform, i32>()); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 280 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 281 | mod.root_block->Append(v); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 282 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 283 | auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
Ben Clayton | c67a4fa | 2023-06-09 19:34:20 +0000 | [diff] [blame] | 284 | std::array{1u, 1u, 1u}); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 285 | b.Append(func->Block(), [&] { |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 286 | auto* load = b.Load(v); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 287 | b.Return(func); |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 288 | mod.SetName(load, "load"); |
Ben Clayton | 26fbdaa | 2023-06-22 20:46:13 +0000 | [diff] [blame] | 289 | }); |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 290 | |
James Price | e9cd719 | 2023-06-29 19:47:49 +0000 | [diff] [blame] | 291 | ASSERT_TRUE(Generate()) << Error() << output_; |
| 292 | EXPECT_INST(R"( |
| 293 | %9 = OpAccessChain %_ptr_Uniform_int %1 %uint_0 |
| 294 | %load = OpLoad %int %9 |
James Price | 117d4f3 | 2023-06-07 23:48:21 +0000 | [diff] [blame] | 295 | )"); |
| 296 | } |
| 297 | |
James Price | 2e59364 | 2023-07-21 15:23:18 +0000 | [diff] [blame] | 298 | TEST_F(SpirvWriterTest, PushConstantVar) { |
| 299 | auto* v = b.Var("v", ty.ptr<push_constant, i32>()); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 300 | mod.root_block->Append(v); |
James Price | 2e59364 | 2023-07-21 15:23:18 +0000 | [diff] [blame] | 301 | |
| 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 | |
| 313 | TEST_F(SpirvWriterTest, PushConstantVar_Load) { |
| 314 | auto* v = b.Var("v", ty.ptr<push_constant, i32>()); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 315 | mod.root_block->Append(v); |
James Price | 2e59364 | 2023-07-21 15:23:18 +0000 | [diff] [blame] | 316 | |
| 317 | auto* func = b.Function("foo", ty.i32()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 318 | b.Append(func->Block(), [&] { |
James Price | 2e59364 | 2023-07-21 15:23:18 +0000 | [diff] [blame] | 319 | 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 Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 332 | TEST_F(SpirvWriterTest, SamplerVar) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 333 | auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, ty.sampler(), core::Access::kRead)); |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 334 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 335 | mod.root_block->Append(v); |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 336 | |
| 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 Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 349 | TEST_F(SpirvWriterTest, SamplerVar_Load) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 350 | auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, ty.sampler(), core::Access::kRead)); |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 351 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 352 | mod.root_block->Append(v); |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 353 | |
| 354 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 355 | b.Append(func->Block(), [&] { |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 356 | 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 Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 365 | TEST_F(SpirvWriterTest, TextureVar) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 366 | 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 Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 370 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 371 | mod.root_block->Append(v); |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 372 | |
| 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 Price | 6dbf49f | 2023-07-21 03:46:26 +0000 | [diff] [blame] | 385 | TEST_F(SpirvWriterTest, TextureVar_Load) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 386 | 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 Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 390 | v->SetBindingPoint(0, 0); |
dan sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 391 | mod.root_block->Append(v); |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 392 | |
| 393 | auto* func = b.Function("foo", ty.void_()); |
James Price | ecbb3ec | 2023-07-26 22:07:39 +0000 | [diff] [blame] | 394 | b.Append(func->Block(), [&] { |
James Price | 8b64bd7 | 2023-06-30 13:09:32 +0000 | [diff] [blame] | 395 | 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 Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 404 | TEST_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 sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 412 | mod.root_block->Append(v); |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 413 | |
| 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 | |
| 427 | TEST_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 sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 435 | mod.root_block->Append(v); |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 436 | |
| 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 | |
| 449 | TEST_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 sinclair | b133c64 | 2023-09-26 00:59:00 +0000 | [diff] [blame] | 457 | mod.root_block->Append(v); |
James Price | 7264388 | 2023-09-16 00:28:22 +0000 | [diff] [blame] | 458 | |
| 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 Price | 75aaa49 | 2023-05-18 02:30:39 +0000 | [diff] [blame] | 472 | } // namespace |
dan sinclair | 2dddb19 | 2023-07-26 20:31:48 +0000 | [diff] [blame] | 473 | } // namespace tint::spirv::writer |