Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint 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 "gmock/gmock.h" |
| 16 | #include "src/tint/ast/stage_attribute.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 17 | #include "src/tint/ast/variable_decl_statement.h" |
| 18 | #include "src/tint/ast/workgroup_attribute.h" |
| 19 | #include "src/tint/writer/glsl/test_helper.h" |
| 20 | |
| 21 | using ::testing::HasSubstr; |
| 22 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 23 | using namespace tint::number_suffixes; // NOLINT |
| 24 | |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 25 | namespace tint::writer::glsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | using GlslGeneratorImplTest_Function = TestHelper; |
| 29 | |
| 30 | TEST_F(GlslGeneratorImplTest_Function, Emit_Function) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 31 | Func("my_func", utils::Empty, ty.void_(), |
| 32 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 33 | Return(), |
| 34 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 35 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 36 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 38 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 40 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 41 | EXPECT_EQ(gen.result(), R"( #version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 42 | |
| 43 | void my_func() { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | )"); |
| 48 | } |
| 49 | |
| 50 | TEST_F(GlslGeneratorImplTest_Function, Emit_Function_Name_Collision) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 51 | Func("centroid", utils::Empty, ty.void_(), |
| 52 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 53 | Return(), |
| 54 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 56 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 57 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 60 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 61 | EXPECT_THAT(gen.result(), HasSubstr(R"( void tint_symbol() { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | return; |
| 63 | })")); |
| 64 | } |
| 65 | |
| 66 | TEST_F(GlslGeneratorImplTest_Function, Emit_Function_WithParams) { |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 67 | Func("my_func", |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 68 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 69 | Param("a", ty.f32()), |
| 70 | Param("b", ty.i32()), |
| 71 | }, |
| 72 | ty.void_(), |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 73 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 74 | Return(), |
| 75 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 76 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 77 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 78 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 79 | gen.increment_indent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 80 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 81 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 82 | EXPECT_EQ(gen.result(), R"( #version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
| 84 | void my_func(float a, int b) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | )"); |
| 89 | } |
| 90 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_NoReturn_Void) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 92 | Func("func", utils::Empty, ty.void_(), utils::Empty /* no explicit return */, |
| 93 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | Stage(ast::PipelineStage::kFragment), |
| 95 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 96 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 97 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 98 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 99 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 100 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 101 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 102 | |
| 103 | void func() { |
| 104 | return; |
| 105 | } |
| 106 | )"); |
| 107 | } |
| 108 | |
| 109 | TEST_F(GlslGeneratorImplTest_Function, PtrParameter) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 110 | // fn f(foo : ptr<function, f32>) -> f32 { |
| 111 | // return *foo; |
| 112 | // } |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 113 | Func("f", utils::Vector{Param("foo", ty.pointer<f32>(builtin::AddressSpace::kFunction))}, |
| 114 | ty.f32(), utils::Vector{Return(Deref("foo"))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 115 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 116 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 117 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 118 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 119 | EXPECT_THAT(gen.result(), HasSubstr(R"(float f(inout float foo) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 120 | return foo; |
| 121 | } |
| 122 | )")); |
| 123 | } |
| 124 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 125 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_WithInOutVars) { |
| 126 | // fn frag_main(@location(0) foo : f32) -> @location(1) f32 { |
| 127 | // return foo; |
| 128 | // } |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 129 | Func("frag_main", |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 130 | utils::Vector{ |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 131 | Param("foo", ty.f32(), utils::Vector{Location(0_a)}), |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 132 | }, |
| 133 | ty.f32(), |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 134 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 135 | Return("foo"), |
| 136 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 137 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 138 | Stage(ast::PipelineStage::kFragment), |
| 139 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 140 | utils::Vector{ |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 141 | Location(1_a), |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 142 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 143 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 144 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 145 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 146 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 147 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 148 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 149 | |
| 150 | layout(location = 0) in float foo_1; |
| 151 | layout(location = 1) out float value; |
| 152 | float frag_main(float foo) { |
| 153 | return foo; |
| 154 | } |
| 155 | |
| 156 | void main() { |
| 157 | float inner_result = frag_main(foo_1); |
| 158 | value = inner_result; |
| 159 | return; |
| 160 | } |
| 161 | )"); |
| 162 | } |
| 163 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 164 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_WithInOut_Builtins) { |
| 165 | // fn frag_main(@position(0) coord : vec4<f32>) -> @frag_depth f32 { |
| 166 | // return coord.x; |
| 167 | // } |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 168 | auto* coord_in = |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 169 | Param("coord", ty.vec4<f32>(), utils::Vector{Builtin(builtin::BuiltinValue::kPosition)}); |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 170 | Func("frag_main", |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 171 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 172 | coord_in, |
| 173 | }, |
| 174 | ty.f32(), |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 175 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 176 | Return(MemberAccessor("coord", "x")), |
| 177 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 178 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 179 | Stage(ast::PipelineStage::kFragment), |
| 180 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 181 | utils::Vector{ |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 182 | Builtin(builtin::BuiltinValue::kFragDepth), |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 183 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 184 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 185 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 186 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 187 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 188 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 189 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 190 | |
| 191 | float frag_main(vec4 coord) { |
| 192 | return coord.x; |
| 193 | } |
| 194 | |
| 195 | void main() { |
| 196 | float inner_result = frag_main(gl_FragCoord); |
| 197 | gl_FragDepth = inner_result; |
| 198 | return; |
| 199 | } |
| 200 | )"); |
| 201 | } |
| 202 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 203 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_SharedStruct_DifferentStages) { |
| 204 | // struct Interface { |
| 205 | // @builtin(position) pos : vec4<f32>; |
| 206 | // @location(1) col1 : f32; |
| 207 | // @location(2) col2 : f32; |
| 208 | // }; |
| 209 | // fn vert_main() -> Interface { |
| 210 | // return Interface(vec4<f32>(), 0.4, 0.6); |
| 211 | // } |
| 212 | // fn frag_main(inputs : Interface) { |
| 213 | // const r = inputs.col1; |
| 214 | // const g = inputs.col2; |
| 215 | // const p = inputs.pos; |
| 216 | // } |
| 217 | auto* interface_struct = Structure( |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 218 | "Interface", |
| 219 | utils::Vector{ |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 220 | Member("pos", ty.vec4<f32>(), utils::Vector{Builtin(builtin::BuiltinValue::kPosition)}), |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 221 | Member("col1", ty.f32(), utils::Vector{Location(1_a)}), |
| 222 | Member("col2", ty.f32(), utils::Vector{Location(2_a)}), |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 223 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 224 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 225 | Func("vert_main", utils::Empty, ty.Of(interface_struct), |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 226 | utils::Vector{Return( |
| 227 | Call(ty.Of(interface_struct), Call(ty.vec4<f32>()), Expr(0.5_f), Expr(0.25_f)))}, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 228 | utils::Vector{Stage(ast::PipelineStage::kVertex)}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 229 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 230 | Func("frag_main", utils::Vector{Param("inputs", ty.Of(interface_struct))}, ty.void_(), |
| 231 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 232 | Decl(Let("r", ty.f32(), MemberAccessor("inputs", "col1"))), |
| 233 | Decl(Let("g", ty.f32(), MemberAccessor("inputs", "col2"))), |
| 234 | Decl(Let("p", ty.vec4<f32>(), MemberAccessor("inputs", "pos"))), |
| 235 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 236 | utils::Vector{Stage(ast::PipelineStage::kFragment)}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 237 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 238 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 239 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 240 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 241 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 242 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 243 | |
| 244 | layout(location = 1) out float col1_1; |
| 245 | layout(location = 2) out float col2_1; |
| 246 | layout(location = 1) in float col1_2; |
| 247 | layout(location = 2) in float col2_2; |
| 248 | struct Interface { |
| 249 | vec4 pos; |
| 250 | float col1; |
| 251 | float col2; |
| 252 | }; |
| 253 | |
| 254 | Interface vert_main() { |
Ben Clayton | 25b7e98 | 2022-06-01 01:11:59 +0000 | [diff] [blame] | 255 | Interface tint_symbol = Interface(vec4(0.0f), 0.5f, 0.25f); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 256 | return tint_symbol; |
| 257 | } |
| 258 | |
| 259 | void main() { |
Stephen White | 790e4c2 | 2022-04-22 21:25:02 +0000 | [diff] [blame] | 260 | gl_PointSize = 1.0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 261 | Interface inner_result = vert_main(); |
| 262 | gl_Position = inner_result.pos; |
| 263 | col1_1 = inner_result.col1; |
| 264 | col2_1 = inner_result.col2; |
| 265 | gl_Position.y = -(gl_Position.y); |
| 266 | gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w); |
| 267 | return; |
| 268 | } |
| 269 | void frag_main(Interface inputs) { |
| 270 | float r = inputs.col1; |
| 271 | float g = inputs.col2; |
| 272 | vec4 p = inputs.pos; |
| 273 | } |
| 274 | |
| 275 | void main_1() { |
| 276 | Interface tint_symbol_1 = Interface(gl_FragCoord, col1_2, col2_2); |
| 277 | frag_main(tint_symbol_1); |
| 278 | return; |
| 279 | } |
| 280 | )"); |
| 281 | } |
| 282 | |
| 283 | #if 0 |
| 284 | TEST_F(GlslGeneratorImplTest_Function, |
| 285 | Emit_Attribute_EntryPoint_SharedStruct_HelperFunction) { |
| 286 | // struct VertexOutput { |
| 287 | // @builtin(position) pos : vec4<f32>; |
| 288 | // }; |
| 289 | // fn foo(x : f32) -> VertexOutput { |
| 290 | // return VertexOutput(vec4<f32>(x, x, x, 1.0)); |
| 291 | // } |
| 292 | // fn vert_main1() -> VertexOutput { |
| 293 | // return foo(0.5); |
| 294 | // } |
| 295 | // fn vert_main2() -> VertexOutput { |
| 296 | // return foo(0.25); |
| 297 | // } |
| 298 | auto* vertex_output_struct = Structure( |
| 299 | "VertexOutput", |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 300 | {Member("pos", ty.vec4<f32>(), {Builtin(builtin::BuiltinValue::kPosition)})}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 301 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 302 | Func("foo", utils::Vector{Param("x", ty.f32())}, ty.Of(vertex_output_struct), |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 303 | {Return(Call(ty.Of(vertex_output_struct), |
| 304 | Call(ty.vec4<f32>(), "x", "x", "x", Expr(1_f))))}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 305 | {}); |
| 306 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 307 | Func("vert_main1", utils::Empty, ty.Of(vertex_output_struct), |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 308 | {Return(Call(ty.Of(vertex_output_struct), |
Ben Clayton | 0a3cda9 | 2022-05-10 17:30:15 +0000 | [diff] [blame] | 309 | Expr(Call("foo", Expr(0.5_f)))))}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 310 | {Stage(ast::PipelineStage::kVertex)}); |
| 311 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 312 | Func("vert_main2", utils::Empty, ty.Of(vertex_output_struct), |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 313 | {Return(Call(ty.Of(vertex_output_struct), |
Ben Clayton | 0a3cda9 | 2022-05-10 17:30:15 +0000 | [diff] [blame] | 314 | Expr(Call("foo", Expr(0.25_f)))))}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 315 | {Stage(ast::PipelineStage::kVertex)}); |
| 316 | |
| 317 | GeneratorImpl& gen = SanitizeAndBuild(); |
| 318 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 319 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 320 | EXPECT_EQ(gen.result(), R"(struct VertexOutput { |
| 321 | float4 pos; |
| 322 | }; |
| 323 | |
| 324 | VertexOutput foo(float x) { |
| 325 | const VertexOutput tint_symbol_4 = {float4(x, x, x, 1.0f)}; |
| 326 | return tint_symbol_4; |
| 327 | } |
| 328 | |
| 329 | struct tint_symbol { |
| 330 | float4 pos : SV_Position; |
| 331 | }; |
| 332 | |
| 333 | tint_symbol vert_main1() { |
| 334 | const VertexOutput tint_symbol_1 = {foo(0.5f)}; |
| 335 | const tint_symbol tint_symbol_5 = {tint_symbol_1.pos}; |
| 336 | return tint_symbol_5; |
| 337 | } |
| 338 | |
| 339 | struct tint_symbol_2 { |
| 340 | float4 pos : SV_Position; |
| 341 | }; |
| 342 | |
| 343 | tint_symbol_2 vert_main2() { |
| 344 | const VertexOutput tint_symbol_3 = {foo(0.25f)}; |
| 345 | const tint_symbol_2 tint_symbol_6 = {tint_symbol_3.pos}; |
| 346 | return tint_symbol_6; |
| 347 | } |
| 348 | )"); |
| 349 | } |
| 350 | #endif |
| 351 | |
| 352 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_With_Uniform) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 353 | auto* ubo_ty = Structure("UBO", utils::Vector{Member("coord", ty.vec4<f32>())}); |
dan sinclair | f9b831c | 2022-08-29 21:13:00 +0000 | [diff] [blame] | 354 | auto* ubo = |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 355 | GlobalVar("ubo", ty.Of(ubo_ty), builtin::AddressSpace::kUniform, Binding(0_a), Group(1_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 356 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 357 | Func("sub_func", |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 358 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 359 | Param("param", ty.f32()), |
| 360 | }, |
| 361 | ty.f32(), |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 362 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 363 | Return(MemberAccessor(MemberAccessor(ubo, "coord"), "x")), |
| 364 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 365 | |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 366 | auto* var = Var("v", ty.f32(), Call("sub_func", 1_f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 367 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 368 | Func("frag_main", utils::Empty, ty.void_(), |
| 369 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 370 | Decl(var), |
| 371 | Return(), |
| 372 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 373 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 374 | Stage(ast::PipelineStage::kFragment), |
| 375 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 376 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 377 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 378 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 379 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 380 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 381 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 382 | |
| 383 | struct UBO { |
| 384 | vec4 coord; |
| 385 | }; |
| 386 | |
Stephen White | 05d8b02 | 2022-09-13 19:48:51 +0000 | [diff] [blame] | 387 | layout(binding = 0, std140) uniform UBO_ubo { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 388 | vec4 coord; |
| 389 | } ubo; |
| 390 | |
| 391 | float sub_func(float param) { |
| 392 | return ubo.coord.x; |
| 393 | } |
| 394 | |
| 395 | void frag_main() { |
| 396 | float v = sub_func(1.0f); |
| 397 | return; |
| 398 | } |
| 399 | )"); |
| 400 | } |
| 401 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 402 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_With_UniformStruct) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 403 | auto* s = Structure("Uniforms", utils::Vector{Member("coord", ty.vec4<f32>())}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 404 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 405 | GlobalVar("uniforms", ty.Of(s), builtin::AddressSpace::kUniform, Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 406 | |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 407 | auto* var = Var("v", ty.f32(), MemberAccessor(MemberAccessor("uniforms", "coord"), "x")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 408 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 409 | Func("frag_main", utils::Empty, ty.void_(), |
| 410 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 411 | Decl(var), |
| 412 | Return(), |
| 413 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 414 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 415 | Stage(ast::PipelineStage::kFragment), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 416 | }); |
| 417 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 418 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 419 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 420 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 421 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 422 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 423 | |
| 424 | struct Uniforms { |
| 425 | vec4 coord; |
| 426 | }; |
| 427 | |
Stephen White | 05d8b02 | 2022-09-13 19:48:51 +0000 | [diff] [blame] | 428 | layout(binding = 0, std140) uniform Uniforms_ubo { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 429 | vec4 coord; |
| 430 | } uniforms; |
| 431 | |
| 432 | void frag_main() { |
| 433 | float v = uniforms.coord.x; |
| 434 | return; |
| 435 | } |
| 436 | )"); |
| 437 | } |
| 438 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 439 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_With_RW_StorageBuffer_Read) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 440 | auto* s = Structure("Data", utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 441 | Member("a", ty.i32()), |
| 442 | Member("b", ty.f32()), |
| 443 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 444 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 445 | GlobalVar("coord", ty.Of(s), builtin::AddressSpace::kStorage, builtin::Access::kReadWrite, |
dan sinclair | 18b2158 | 2023-01-21 19:56:49 +0000 | [diff] [blame] | 446 | Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 447 | |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 448 | auto* var = Var("v", ty.f32(), MemberAccessor("coord", "b")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 449 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 450 | Func("frag_main", utils::Empty, ty.void_(), |
| 451 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 452 | Decl(var), |
| 453 | Return(), |
| 454 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 455 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 456 | Stage(ast::PipelineStage::kFragment), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 457 | }); |
| 458 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 459 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 460 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 461 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 462 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 463 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 464 | |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 465 | struct Data { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 466 | int a; |
| 467 | float b; |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | layout(binding = 0, std430) buffer coord_block_ssbo { |
| 471 | Data inner; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 472 | } coord; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 473 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 474 | void frag_main() { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 475 | float v = coord.inner.b; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | void main() { |
| 480 | frag_main(); |
| 481 | return; |
| 482 | } |
| 483 | )"); |
| 484 | } |
| 485 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 486 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_With_RO_StorageBuffer_Read) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 487 | auto* s = Structure("Data", utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 488 | Member("a", ty.i32()), |
| 489 | Member("b", ty.f32()), |
| 490 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 491 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 492 | GlobalVar("coord", ty.Of(s), builtin::AddressSpace::kStorage, builtin::Access::kRead, |
| 493 | Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 494 | |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 495 | auto* var = Var("v", ty.f32(), MemberAccessor("coord", "b")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 496 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 497 | Func("frag_main", utils::Empty, ty.void_(), |
| 498 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 499 | Decl(var), |
| 500 | Return(), |
| 501 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 502 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 503 | Stage(ast::PipelineStage::kFragment), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 504 | }); |
| 505 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 506 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 507 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 508 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 509 | EXPECT_EQ(gen.result(), |
| 510 | R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 511 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 512 | |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 513 | struct Data { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 514 | int a; |
| 515 | float b; |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | layout(binding = 0, std430) buffer coord_block_ssbo { |
| 519 | Data inner; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 520 | } coord; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 521 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 522 | void frag_main() { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 523 | float v = coord.inner.b; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
| 527 | void main() { |
| 528 | frag_main(); |
| 529 | return; |
| 530 | } |
| 531 | )"); |
| 532 | } |
| 533 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 534 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_With_WO_StorageBuffer_Store) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 535 | auto* s = Structure("Data", utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 536 | Member("a", ty.i32()), |
| 537 | Member("b", ty.f32()), |
| 538 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 539 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 540 | GlobalVar("coord", ty.Of(s), builtin::AddressSpace::kStorage, builtin::Access::kReadWrite, |
dan sinclair | 18b2158 | 2023-01-21 19:56:49 +0000 | [diff] [blame] | 541 | Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 542 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 543 | Func("frag_main", utils::Empty, ty.void_(), |
| 544 | utils::Vector{ |
Ben Clayton | 0a3cda9 | 2022-05-10 17:30:15 +0000 | [diff] [blame] | 545 | Assign(MemberAccessor("coord", "b"), Expr(2_f)), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 546 | Return(), |
| 547 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 548 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 549 | Stage(ast::PipelineStage::kFragment), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 550 | }); |
| 551 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 552 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 553 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 554 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 555 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 556 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 557 | |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 558 | struct Data { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 559 | int a; |
| 560 | float b; |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | layout(binding = 0, std430) buffer coord_block_ssbo { |
| 564 | Data inner; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 565 | } coord; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 566 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 567 | void frag_main() { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 568 | coord.inner.b = 2.0f; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | |
| 572 | void main() { |
| 573 | frag_main(); |
| 574 | return; |
| 575 | } |
| 576 | )"); |
| 577 | } |
| 578 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 579 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_With_StorageBuffer_Store) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 580 | auto* s = Structure("Data", utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 581 | Member("a", ty.i32()), |
| 582 | Member("b", ty.f32()), |
| 583 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 584 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 585 | GlobalVar("coord", ty.Of(s), builtin::AddressSpace::kStorage, builtin::Access::kReadWrite, |
dan sinclair | 18b2158 | 2023-01-21 19:56:49 +0000 | [diff] [blame] | 586 | Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 587 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 588 | Func("frag_main", utils::Empty, ty.void_(), |
| 589 | utils::Vector{ |
Ben Clayton | 0a3cda9 | 2022-05-10 17:30:15 +0000 | [diff] [blame] | 590 | Assign(MemberAccessor("coord", "b"), Expr(2_f)), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 591 | Return(), |
| 592 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 593 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 594 | Stage(ast::PipelineStage::kFragment), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 595 | }); |
| 596 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 597 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 598 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 599 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 600 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 601 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 602 | |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 603 | struct Data { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 604 | int a; |
| 605 | float b; |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | layout(binding = 0, std430) buffer coord_block_ssbo { |
| 609 | Data inner; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 610 | } coord; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 611 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 612 | void frag_main() { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 613 | coord.inner.b = 2.0f; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
| 617 | void main() { |
| 618 | frag_main(); |
| 619 | return; |
| 620 | } |
| 621 | )"); |
| 622 | } |
| 623 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 624 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_Called_By_EntryPoint_With_Uniform) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 625 | auto* s = Structure("S", utils::Vector{Member("x", ty.f32())}); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 626 | GlobalVar("coord", ty.Of(s), builtin::AddressSpace::kUniform, Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 627 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 628 | Func("sub_func", utils::Vector{Param("param", ty.f32())}, ty.f32(), |
| 629 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 630 | Return(MemberAccessor("coord", "x")), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 631 | }); |
| 632 | |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 633 | auto* var = Var("v", ty.f32(), Call("sub_func", 1_f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 634 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 635 | Func("frag_main", utils::Empty, ty.void_(), |
| 636 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 637 | Decl(var), |
| 638 | Return(), |
| 639 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 640 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 641 | Stage(ast::PipelineStage::kFragment), |
| 642 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 643 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 644 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 645 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 646 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 647 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 648 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 649 | |
| 650 | struct S { |
| 651 | float x; |
| 652 | }; |
| 653 | |
Stephen White | 05d8b02 | 2022-09-13 19:48:51 +0000 | [diff] [blame] | 654 | layout(binding = 0, std140) uniform S_ubo { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 655 | float x; |
| 656 | } coord; |
| 657 | |
| 658 | float sub_func(float param) { |
| 659 | return coord.x; |
| 660 | } |
| 661 | |
| 662 | void frag_main() { |
| 663 | float v = sub_func(1.0f); |
| 664 | return; |
| 665 | } |
| 666 | )"); |
| 667 | } |
| 668 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 669 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_Called_By_EntryPoint_With_StorageBuffer) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 670 | auto* s = Structure("S", utils::Vector{Member("x", ty.f32())}); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 671 | GlobalVar("coord", ty.Of(s), builtin::AddressSpace::kStorage, builtin::Access::kReadWrite, |
dan sinclair | 18b2158 | 2023-01-21 19:56:49 +0000 | [diff] [blame] | 672 | Binding(0_a), Group(1_a)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 673 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 674 | Func("sub_func", utils::Vector{Param("param", ty.f32())}, ty.f32(), |
| 675 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 676 | Return(MemberAccessor("coord", "x")), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 677 | }); |
| 678 | |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 679 | auto* var = Var("v", ty.f32(), Call("sub_func", 1_f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 680 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 681 | Func("frag_main", utils::Empty, ty.void_(), |
| 682 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 683 | Decl(var), |
| 684 | Return(), |
| 685 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 686 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 687 | Stage(ast::PipelineStage::kFragment), |
| 688 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 689 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 690 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 691 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 692 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 693 | EXPECT_EQ(gen.result(), |
| 694 | R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 695 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 696 | |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 697 | struct S { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 698 | float x; |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 699 | }; |
| 700 | |
| 701 | layout(binding = 0, std430) buffer coord_block_ssbo { |
| 702 | S inner; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 703 | } coord; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 704 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 705 | float sub_func(float param) { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 706 | return coord.inner.x; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void frag_main() { |
| 710 | float v = sub_func(1.0f); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | void main() { |
| 715 | frag_main(); |
| 716 | return; |
| 717 | } |
| 718 | )"); |
| 719 | } |
| 720 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 721 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_WithNameCollision) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 722 | Func("centroid", utils::Empty, ty.void_(), {}, |
| 723 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 724 | Stage(ast::PipelineStage::kFragment), |
| 725 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 726 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 727 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 728 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 729 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 730 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ben Clayton | f1f56b2 | 2023-03-09 19:58:23 +0000 | [diff] [blame] | 731 | precision highp float; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 732 | |
| 733 | void tint_symbol() { |
| 734 | } |
| 735 | |
| 736 | void main() { |
| 737 | tint_symbol(); |
| 738 | return; |
| 739 | } |
| 740 | )"); |
| 741 | } |
| 742 | |
| 743 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_Compute) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 744 | Func("main", utils::Empty, ty.void_(), |
| 745 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 746 | Return(), |
| 747 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 748 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 749 | Stage(ast::PipelineStage::kCompute), |
| 750 | WorkgroupSize(1_i), |
| 751 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 752 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 753 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 754 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 755 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 756 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 757 | |
| 758 | layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; |
| 759 | void main() { |
| 760 | return; |
| 761 | } |
| 762 | )"); |
| 763 | } |
| 764 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 765 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_Compute_WithWorkgroup_Literal) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 766 | Func("main", utils::Empty, ty.void_(), {}, |
| 767 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 768 | Stage(ast::PipelineStage::kCompute), |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 769 | WorkgroupSize(2_i, 4_i, 6_i), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 770 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 771 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 772 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 773 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 774 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 775 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 776 | |
| 777 | layout(local_size_x = 2, local_size_y = 4, local_size_z = 6) in; |
| 778 | void main() { |
| 779 | return; |
| 780 | } |
| 781 | )"); |
| 782 | } |
| 783 | |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 784 | TEST_F(GlslGeneratorImplTest_Function, Emit_Attribute_EntryPoint_Compute_WithWorkgroup_Const) { |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 785 | GlobalConst("width", ty.i32(), Call<i32>(2_i)); |
| 786 | GlobalConst("height", ty.i32(), Call<i32>(3_i)); |
| 787 | GlobalConst("depth", ty.i32(), Call<i32>(4_i)); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 788 | Func("main", utils::Empty, ty.void_(), {}, |
| 789 | utils::Vector{ |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 790 | Stage(ast::PipelineStage::kCompute), |
| 791 | WorkgroupSize("width", "height", "depth"), |
| 792 | }); |
| 793 | |
| 794 | GeneratorImpl& gen = Build(); |
| 795 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 796 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 797 | EXPECT_EQ(gen.result(), R"(#version 310 es |
| 798 | |
| 799 | layout(local_size_x = 2, local_size_y = 3, local_size_z = 4) in; |
| 800 | void main() { |
| 801 | return; |
| 802 | } |
| 803 | )"); |
| 804 | } |
| 805 | |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 806 | TEST_F(GlslGeneratorImplTest_Function, |
| 807 | Emit_Attribute_EntryPoint_Compute_WithWorkgroup_OverridableConst) { |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 808 | Override("width", ty.i32(), Call<i32>(2_i), Id(7_u)); |
| 809 | Override("height", ty.i32(), Call<i32>(3_i), Id(8_u)); |
| 810 | Override("depth", ty.i32(), Call<i32>(4_i), Id(9_u)); |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 811 | Func("main", utils::Empty, ty.void_(), {}, |
| 812 | utils::Vector{ |
| 813 | Stage(ast::PipelineStage::kCompute), |
| 814 | WorkgroupSize("width", "height", "depth"), |
| 815 | }); |
| 816 | |
| 817 | GeneratorImpl& gen = Build(); |
| 818 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 819 | EXPECT_FALSE(gen.Generate()) << gen.Diagnostics(); |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 820 | EXPECT_EQ( |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 821 | gen.Diagnostics().str(), |
Ben Clayton | f10a579 | 2022-10-13 13:47:39 +0000 | [diff] [blame] | 822 | R"(error: override-expressions should have been removed with the SubstituteOverride transform)"); |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 825 | TEST_F(GlslGeneratorImplTest_Function, Emit_Function_WithArrayParams) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 826 | Func("my_func", utils::Vector{Param("a", ty.array<f32, 5>())}, ty.void_(), |
| 827 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 828 | Return(), |
| 829 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 830 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 831 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 832 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 833 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 834 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 835 | |
| 836 | void my_func(float a[5]) { |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | )"); |
| 841 | } |
| 842 | |
| 843 | TEST_F(GlslGeneratorImplTest_Function, Emit_Function_WithArrayReturn) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 844 | Func("my_func", utils::Empty, ty.array<f32, 5>(), |
| 845 | utils::Vector{ |
Ben Clayton | 01ac21c | 2023-02-07 16:14:25 +0000 | [diff] [blame] | 846 | Return(Call(ty.array<f32, 5>())), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 847 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 848 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 849 | GeneratorImpl& gen = Build(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 850 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 851 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 852 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 853 | |
| 854 | float[5] my_func() { |
| 855 | return float[5](0.0f, 0.0f, 0.0f, 0.0f, 0.0f); |
| 856 | } |
| 857 | |
| 858 | )"); |
| 859 | } |
| 860 | |
| 861 | // https://crbug.com/tint/297 |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 862 | TEST_F(GlslGeneratorImplTest_Function, Emit_Multiple_EntryPoint_With_Same_ModuleVar) { |
| 863 | // struct Data { |
| 864 | // d : f32; |
| 865 | // }; |
| 866 | // @binding(0) @group(0) var<storage> data : Data; |
| 867 | // |
dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 868 | // @compute @workgroup_size(1) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 869 | // fn a() { |
| 870 | // var v = data.d; |
| 871 | // return; |
| 872 | // } |
| 873 | // |
dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 874 | // @compute @workgroup_size(1) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 875 | // fn b() { |
| 876 | // var v = data.d; |
| 877 | // return; |
| 878 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 879 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 880 | auto* s = Structure("Data", utils::Vector{Member("d", ty.f32())}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 881 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 882 | GlobalVar("data", ty.Of(s), builtin::AddressSpace::kStorage, builtin::Access::kReadWrite, |
dan sinclair | 61c16eb | 2023-01-21 23:44:38 +0000 | [diff] [blame] | 883 | Binding(0_a), Group(0_a)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 884 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 885 | { |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 886 | auto* var = Var("v", ty.f32(), MemberAccessor("data", "d")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 887 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 888 | Func("a", utils::Empty, ty.void_(), |
| 889 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 890 | Decl(var), |
| 891 | Return(), |
| 892 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 893 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 894 | Stage(ast::PipelineStage::kCompute), |
| 895 | WorkgroupSize(1_i), |
| 896 | }); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 897 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 898 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 899 | { |
Ben Clayton | 58794ae | 2022-08-19 17:28:53 +0000 | [diff] [blame] | 900 | auto* var = Var("v", ty.f32(), MemberAccessor("data", "d")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 901 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 902 | Func("b", utils::Empty, ty.void_(), |
| 903 | utils::Vector{ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 904 | Decl(var), |
| 905 | Return(), |
| 906 | }, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 907 | utils::Vector{ |
Ben Clayton | 7164b97 | 2022-06-15 10:02:37 +0000 | [diff] [blame] | 908 | Stage(ast::PipelineStage::kCompute), |
| 909 | WorkgroupSize(1_i), |
| 910 | }); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 911 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 912 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 913 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 914 | |
dan sinclair | 8a435a2 | 2023-04-12 11:59:24 +0000 | [diff] [blame] | 915 | ASSERT_TRUE(gen.Generate()) << gen.Diagnostics(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 916 | EXPECT_EQ(gen.result(), R"(#version 310 es |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 917 | |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 918 | struct Data { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 919 | float d; |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 920 | }; |
| 921 | |
| 922 | layout(binding = 0, std430) buffer data_block_ssbo { |
| 923 | Data inner; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 924 | } data; |
Stephen White | 863d9ed | 2022-09-02 19:19:10 +0000 | [diff] [blame] | 925 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 926 | void a() { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 927 | float v = data.inner.d; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
| 931 | layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; |
| 932 | void main() { |
| 933 | a(); |
| 934 | return; |
| 935 | } |
| 936 | void b() { |
Zhaoming Jiang | 6ab5d3c | 2022-11-02 02:25:38 +0000 | [diff] [blame] | 937 | float v = data.inner.d; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 938 | return; |
| 939 | } |
| 940 | |
| 941 | layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; |
| 942 | void main_1() { |
| 943 | b(); |
| 944 | return; |
| 945 | } |
| 946 | )"); |
| 947 | } |
| 948 | |
| 949 | } // namespace |
dan sinclair | 5d59059 | 2022-04-07 14:40:24 +0000 | [diff] [blame] | 950 | } // namespace tint::writer::glsl |