Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | /// Copyright 2020 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 "src/tint/writer/hlsl/generator_impl.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <cmath> |
| 19 | #include <functional> |
| 20 | #include <iomanip> |
| 21 | #include <set> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "src/tint/ast/call_statement.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 26 | #include "src/tint/ast/id_attribute.h" |
| 27 | #include "src/tint/ast/internal_attribute.h" |
| 28 | #include "src/tint/ast/interpolate_attribute.h" |
| 29 | #include "src/tint/ast/variable_decl_statement.h" |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 30 | #include "src/tint/constant/value.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | #include "src/tint/debug.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 32 | #include "src/tint/sem/block_statement.h" |
| 33 | #include "src/tint/sem/call.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | #include "src/tint/sem/function.h" |
| 35 | #include "src/tint/sem/member_accessor_expression.h" |
| 36 | #include "src/tint/sem/module.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | #include "src/tint/sem/statement.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | #include "src/tint/sem/struct.h" |
dan sinclair | d32fbe0 | 2022-10-19 00:43:41 +0000 | [diff] [blame] | 39 | #include "src/tint/sem/switch_statement.h" |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 40 | #include "src/tint/sem/value_constructor.h" |
| 41 | #include "src/tint/sem/value_conversion.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 42 | #include "src/tint/sem/variable.h" |
Ben Clayton | 23946b3 | 2023-03-09 16:50:19 +0000 | [diff] [blame] | 43 | #include "src/tint/switch.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 44 | #include "src/tint/transform/add_empty_entry_point.h" |
| 45 | #include "src/tint/transform/array_length_from_uniform.h" |
dan sinclair | 39d4065 | 2023-03-15 13:41:46 +0000 | [diff] [blame] | 46 | #include "src/tint/transform/binding_remapper.h" |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 47 | #include "src/tint/transform/builtin_polyfill.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 48 | #include "src/tint/transform/calculate_array_length.h" |
| 49 | #include "src/tint/transform/canonicalize_entry_point_io.h" |
| 50 | #include "src/tint/transform/decompose_memory_access.h" |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 51 | #include "src/tint/transform/demote_to_helper.h" |
Ben Clayton | 6992f51 | 2022-12-01 18:49:09 +0000 | [diff] [blame] | 52 | #include "src/tint/transform/direct_variable_access.h" |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 53 | #include "src/tint/transform/disable_uniformity_analysis.h" |
James Price | 508a966 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 54 | #include "src/tint/transform/expand_compound_assignment.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | #include "src/tint/transform/localize_struct_array_assignment.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | #include "src/tint/transform/manager.h" |
dan sinclair | 6521a35 | 2023-03-14 16:18:24 +0000 | [diff] [blame] | 57 | #include "src/tint/transform/multiplanar_external_texture.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | #include "src/tint/transform/num_workgroups_from_uniform.h" |
Ben Clayton | 7ebcfc7 | 2022-06-27 20:20:25 +0000 | [diff] [blame] | 59 | #include "src/tint/transform/promote_initializers_to_let.h" |
Antonio Maiorano | 93baaae | 2022-03-15 15:35:13 +0000 | [diff] [blame] | 60 | #include "src/tint/transform/promote_side_effects_to_decl.h" |
Antonio Maiorano | b349710 | 2022-03-31 15:02:25 +0000 | [diff] [blame] | 61 | #include "src/tint/transform/remove_continue_in_switch.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | #include "src/tint/transform/remove_phonies.h" |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 63 | #include "src/tint/transform/robustness.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | #include "src/tint/transform/simplify_pointers.h" |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 65 | #include "src/tint/transform/truncate_interstage_variables.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 66 | #include "src/tint/transform/unshadow.h" |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 67 | #include "src/tint/transform/vectorize_scalar_matrix_initializers.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | #include "src/tint/transform/zero_init_workgroup_memory.h" |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 69 | #include "src/tint/type/array.h" |
dan sinclair | d8a0845 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 70 | #include "src/tint/type/atomic.h" |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 71 | #include "src/tint/type/depth_multisampled_texture.h" |
| 72 | #include "src/tint/type/depth_texture.h" |
| 73 | #include "src/tint/type/multisampled_texture.h" |
| 74 | #include "src/tint/type/sampled_texture.h" |
| 75 | #include "src/tint/type/storage_texture.h" |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 76 | #include "src/tint/type/texture_dimension.h" |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 77 | #include "src/tint/utils/compiler_macros.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 78 | #include "src/tint/utils/defer.h" |
| 79 | #include "src/tint/utils/map.h" |
| 80 | #include "src/tint/utils/scoped_assignment.h" |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 81 | #include "src/tint/utils/string.h" |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 82 | #include "src/tint/utils/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | #include "src/tint/writer/append_vector.h" |
Ben Clayton | 1a56778 | 2022-10-14 13:38:27 +0000 | [diff] [blame] | 84 | #include "src/tint/writer/check_supported_extensions.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 85 | #include "src/tint/writer/float_to_string.h" |
| 86 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 87 | using namespace tint::number_suffixes; // NOLINT |
| 88 | |
dan sinclair | 6a5bef1 | 2022-04-07 14:30:24 +0000 | [diff] [blame] | 89 | namespace tint::writer::hlsl { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 90 | namespace { |
| 91 | |
| 92 | const char kTempNamePrefix[] = "tint_tmp"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 93 | |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 94 | const char* image_format_to_rwtexture_type(builtin::TexelFormat image_format) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 95 | switch (image_format) { |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 96 | case builtin::TexelFormat::kBgra8Unorm: |
| 97 | case builtin::TexelFormat::kRgba8Unorm: |
| 98 | case builtin::TexelFormat::kRgba8Snorm: |
| 99 | case builtin::TexelFormat::kRgba16Float: |
| 100 | case builtin::TexelFormat::kR32Float: |
| 101 | case builtin::TexelFormat::kRg32Float: |
| 102 | case builtin::TexelFormat::kRgba32Float: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 103 | return "float4"; |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 104 | case builtin::TexelFormat::kRgba8Uint: |
| 105 | case builtin::TexelFormat::kRgba16Uint: |
| 106 | case builtin::TexelFormat::kR32Uint: |
| 107 | case builtin::TexelFormat::kRg32Uint: |
| 108 | case builtin::TexelFormat::kRgba32Uint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 109 | return "uint4"; |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 110 | case builtin::TexelFormat::kRgba8Sint: |
| 111 | case builtin::TexelFormat::kRgba16Sint: |
| 112 | case builtin::TexelFormat::kR32Sint: |
| 113 | case builtin::TexelFormat::kRg32Sint: |
| 114 | case builtin::TexelFormat::kRgba32Sint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 115 | return "int4"; |
| 116 | default: |
| 117 | return nullptr; |
| 118 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 119 | } |
| 120 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 121 | void PrintF32(utils::StringStream& out, float value) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 122 | if (std::isinf(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 123 | out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 124 | } else if (std::isnan(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 125 | out << "0.0f /* nan */"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 126 | } else { |
| 127 | out << FloatToString(value) << "f"; |
| 128 | } |
| 129 | } |
| 130 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 131 | void PrintF16(utils::StringStream& out, float value) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 132 | if (std::isinf(value)) { |
| 133 | out << "0.0h " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
| 134 | } else if (std::isnan(value)) { |
| 135 | out << "0.0h /* nan */"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 136 | } else { |
| 137 | out << FloatToString(value) << "h"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 141 | // Helper for writing " : register(RX, spaceY)", where R is the register, X is |
| 142 | // the binding point binding value, and Y is the binding point group value. |
| 143 | struct RegisterAndSpace { |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 144 | RegisterAndSpace(char r, sem::BindingPoint bp) : reg(r), binding_point(bp) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 145 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 146 | const char reg; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 147 | sem::BindingPoint const binding_point; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 150 | utils::StringStream& operator<<(utils::StringStream& s, const RegisterAndSpace& rs) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 151 | s << " : register(" << rs.reg << rs.binding_point.binding; |
| 152 | // Omit the space if it's 0, as it's the default. |
| 153 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better compatibility. |
| 154 | if (rs.binding_point.group == 0) { |
| 155 | s << ")"; |
| 156 | } else { |
| 157 | s << ", space" << rs.binding_point.group << ")"; |
| 158 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 159 | return s; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 162 | } // namespace |
| 163 | |
| 164 | SanitizedResult::SanitizedResult() = default; |
| 165 | SanitizedResult::~SanitizedResult() = default; |
| 166 | SanitizedResult::SanitizedResult(SanitizedResult&&) = default; |
| 167 | |
Antonio Maiorano | 7eaab38 | 2022-04-11 16:33:30 +0000 | [diff] [blame] | 168 | SanitizedResult Sanitize(const Program* in, const Options& options) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 169 | transform::Manager manager; |
| 170 | transform::DataMap data; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 171 | |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 172 | manager.Add<transform::DisableUniformityAnalysis>(); |
| 173 | |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 174 | // ExpandCompoundAssignment must come before BuiltinPolyfill |
| 175 | manager.Add<transform::ExpandCompoundAssignment>(); |
| 176 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 177 | manager.Add<transform::Unshadow>(); // Must come before DirectVariableAccess |
| 178 | |
| 179 | manager.Add<transform::DirectVariableAccess>(); |
| 180 | |
| 181 | // LocalizeStructArrayAssignment must come after: |
| 182 | // * SimplifyPointers, because it assumes assignment to arrays in structs are |
| 183 | // done directly, not indirectly. |
| 184 | // TODO(crbug.com/tint/1340): See if we can get rid of the duplicate |
| 185 | // SimplifyPointers transform. Can't do it right now because |
| 186 | // LocalizeStructArrayAssignment introduces pointers. |
| 187 | manager.Add<transform::SimplifyPointers>(); |
| 188 | manager.Add<transform::LocalizeStructArrayAssignment>(); |
| 189 | |
| 190 | manager.Add<transform::PromoteSideEffectsToDecl>(); |
| 191 | |
| 192 | if (!options.disable_robustness) { |
Ben Clayton | 8525ff2 | 2023-03-06 21:05:01 +0000 | [diff] [blame] | 193 | // Robustness must come after PromoteSideEffectsToDecl |
| 194 | // Robustness must come before BuiltinPolyfill and CanonicalizeEntryPointIO |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 195 | manager.Add<transform::Robustness>(); |
| 196 | } |
| 197 | |
dan sinclair | 04d61c8 | 2023-04-11 23:02:45 +0000 | [diff] [blame] | 198 | // Note: it is more efficient for MultiplanarExternalTexture to come after Robustness |
| 199 | data.Add<transform::MultiplanarExternalTexture::NewBindingPoints>( |
| 200 | options.external_texture_options.bindings_map); |
| 201 | manager.Add<transform::MultiplanarExternalTexture>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 202 | |
dan sinclair | 39d4065 | 2023-03-15 13:41:46 +0000 | [diff] [blame] | 203 | // BindingRemapper must come after MultiplanarExternalTexture |
| 204 | manager.Add<transform::BindingRemapper>(); |
| 205 | data.Add<transform::BindingRemapper::Remappings>( |
| 206 | options.binding_remapper_options.binding_points, |
| 207 | options.binding_remapper_options.access_controls, |
| 208 | options.binding_remapper_options.allow_collisions); |
| 209 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 210 | { // Builtin polyfills |
| 211 | transform::BuiltinPolyfill::Builtins polyfills; |
dan sinclair | d23f296 | 2022-06-28 15:27:44 +0000 | [diff] [blame] | 212 | polyfills.acosh = transform::BuiltinPolyfill::Level::kFull; |
| 213 | polyfills.asinh = true; |
| 214 | polyfills.atanh = transform::BuiltinPolyfill::Level::kFull; |
Ben Clayton | 02f04d9 | 2022-11-03 19:15:17 +0000 | [diff] [blame] | 215 | polyfills.bitshift_modulo = true; |
Ben Clayton | 6dbb463 | 2022-10-31 17:54:49 +0000 | [diff] [blame] | 216 | polyfills.clamp_int = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 217 | // TODO(crbug.com/tint/1449): Some of these can map to HLSL's `firstbitlow` |
| 218 | // and `firstbithigh`. |
Ben Clayton | cc3f851 | 2023-03-09 21:26:12 +0000 | [diff] [blame] | 219 | polyfills.conv_f32_to_iu32 = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 220 | polyfills.count_leading_zeros = true; |
| 221 | polyfills.count_trailing_zeros = true; |
| 222 | polyfills.extract_bits = transform::BuiltinPolyfill::Level::kFull; |
| 223 | polyfills.first_leading_bit = true; |
| 224 | polyfills.first_trailing_bit = true; |
| 225 | polyfills.insert_bits = transform::BuiltinPolyfill::Level::kFull; |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 226 | polyfills.int_div_mod = true; |
Antonio Maiorano | ee665a4 | 2023-02-14 16:12:59 +0000 | [diff] [blame] | 227 | polyfills.precise_float_mod = true; |
Zhaoming Jiang | 04529be | 2023-02-27 02:59:50 +0000 | [diff] [blame] | 228 | polyfills.reflect_vec2_f32 = options.polyfill_reflect_vec2_f32; |
Ben Clayton | c4ebf2c | 2022-09-22 22:59:16 +0000 | [diff] [blame] | 229 | polyfills.texture_sample_base_clamp_to_edge_2d_f32 = true; |
James Price | 128980f | 2023-01-06 02:25:06 +0000 | [diff] [blame] | 230 | polyfills.workgroup_uniform_load = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 231 | data.Add<transform::BuiltinPolyfill::Config>(polyfills); |
| 232 | manager.Add<transform::BuiltinPolyfill>(); |
| 233 | } |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 234 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 235 | if (!options.disable_workgroup_init) { |
| 236 | // ZeroInitWorkgroupMemory must come before CanonicalizeEntryPointIO as |
| 237 | // ZeroInitWorkgroupMemory may inject new builtin parameters. |
| 238 | manager.Add<transform::ZeroInitWorkgroupMemory>(); |
| 239 | } |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 240 | |
| 241 | // CanonicalizeEntryPointIO must come after Robustness |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 242 | manager.Add<transform::CanonicalizeEntryPointIO>(); |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 243 | |
shrekshao | 8f0607a | 2023-04-18 01:34:41 +0000 | [diff] [blame^] | 244 | if (options.truncate_interstage_variables) { |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 245 | // When interstage_locations is empty, it means there's no user-defined interstage variables |
shrekshao | 8f0607a | 2023-04-18 01:34:41 +0000 | [diff] [blame^] | 246 | // being used in the next stage. Still, HLSL compiler register mismatch could happen, if |
| 247 | // there's builtin inputs used in the next stage. So we still run |
| 248 | // TruncateInterstageVariables transform. |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 249 | |
| 250 | // TruncateInterstageVariables itself will skip when interstage_locations matches exactly |
| 251 | // with the current stage output. |
| 252 | |
| 253 | // Build the config for internal TruncateInterstageVariables transform. |
| 254 | transform::TruncateInterstageVariables::Config truncate_interstage_variables_cfg; |
| 255 | truncate_interstage_variables_cfg.interstage_locations = |
| 256 | std::move(options.interstage_locations); |
| 257 | manager.Add<transform::TruncateInterstageVariables>(); |
| 258 | data.Add<transform::TruncateInterstageVariables::Config>( |
| 259 | std::move(truncate_interstage_variables_cfg)); |
| 260 | } |
| 261 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 262 | // NumWorkgroupsFromUniform must come after CanonicalizeEntryPointIO, as it |
| 263 | // assumes that num_workgroups builtins only appear as struct members and are |
| 264 | // only accessed directly via member accessors. |
| 265 | manager.Add<transform::NumWorkgroupsFromUniform>(); |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 266 | manager.Add<transform::VectorizeScalarMatrixInitializers>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 267 | manager.Add<transform::SimplifyPointers>(); |
| 268 | manager.Add<transform::RemovePhonies>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 269 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 270 | // Build the config for the internal ArrayLengthFromUniform transform. |
| 271 | auto& array_length_from_uniform = options.array_length_from_uniform; |
| 272 | transform::ArrayLengthFromUniform::Config array_length_from_uniform_cfg( |
| 273 | array_length_from_uniform.ubo_binding); |
| 274 | array_length_from_uniform_cfg.bindpoint_to_size_index = |
| 275 | array_length_from_uniform.bindpoint_to_size_index; |
| 276 | |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 277 | // DemoteToHelper must come after CanonicalizeEntryPointIO, PromoteSideEffectsToDecl, and |
| 278 | // ExpandCompoundAssignment. |
| 279 | // TODO(crbug.com/tint/1752): This is only necessary when FXC is being used. |
| 280 | manager.Add<transform::DemoteToHelper>(); |
| 281 | |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 282 | // ArrayLengthFromUniform must come after SimplifyPointers as it assumes that the form of the |
| 283 | // array length argument is &var.array. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 284 | manager.Add<transform::ArrayLengthFromUniform>(); |
| 285 | data.Add<transform::ArrayLengthFromUniform::Config>(std::move(array_length_from_uniform_cfg)); |
| 286 | // DecomposeMemoryAccess must come after: |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 287 | // * SimplifyPointers, as we cannot take the address of calls to |
| 288 | // DecomposeMemoryAccess::Intrinsic and we need to fold away the address-of and dereferences |
| 289 | // of `*(&(intrinsic_load()))` expressions. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 290 | // * RemovePhonies, as phonies can be assigned a pointer to a |
| 291 | // non-constructible buffer, or dynamic array, which DMA cannot cope with. |
| 292 | manager.Add<transform::DecomposeMemoryAccess>(); |
| 293 | // CalculateArrayLength must come after DecomposeMemoryAccess, as |
| 294 | // DecomposeMemoryAccess special-cases the arrayLength() intrinsic, which |
| 295 | // will be transformed by CalculateArrayLength |
| 296 | manager.Add<transform::CalculateArrayLength>(); |
Ben Clayton | 7ebcfc7 | 2022-06-27 20:20:25 +0000 | [diff] [blame] | 297 | manager.Add<transform::PromoteInitializersToLet>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 298 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 299 | manager.Add<transform::RemoveContinueInSwitch>(); |
Antonio Maiorano | b349710 | 2022-03-31 15:02:25 +0000 | [diff] [blame] | 300 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 301 | manager.Add<transform::AddEmptyEntryPoint>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 302 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 303 | data.Add<transform::CanonicalizeEntryPointIO::Config>( |
| 304 | transform::CanonicalizeEntryPointIO::ShaderStyle::kHlsl); |
| 305 | data.Add<transform::NumWorkgroupsFromUniform::Config>(options.root_constant_binding_point); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 306 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 307 | auto out = manager.Run(in, data); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 308 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 309 | SanitizedResult result; |
| 310 | result.program = std::move(out.program); |
| 311 | if (auto* res = out.data.Get<transform::ArrayLengthFromUniform::Result>()) { |
| 312 | result.used_array_length_from_uniform_indices = std::move(res->used_size_indices); |
| 313 | } |
| 314 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | GeneratorImpl::GeneratorImpl(const Program* program) : TextGenerator(program) {} |
| 318 | |
| 319 | GeneratorImpl::~GeneratorImpl() = default; |
| 320 | |
| 321 | bool GeneratorImpl::Generate() { |
Ben Clayton | 1a56778 | 2022-10-14 13:38:27 +0000 | [diff] [blame] | 322 | if (!CheckSupportedExtensions("HLSL", program_->AST(), diagnostics_, |
| 323 | utils::Vector{ |
dan sinclair | e4039c7 | 2023-02-17 21:58:59 +0000 | [diff] [blame] | 324 | builtin::Extension::kChromiumDisableUniformityAnalysis, |
| 325 | builtin::Extension::kChromiumExperimentalDp4A, |
| 326 | builtin::Extension::kChromiumExperimentalFullPtrParameters, |
| 327 | builtin::Extension::kChromiumExperimentalPushConstant, |
| 328 | builtin::Extension::kF16, |
Ben Clayton | 1a56778 | 2022-10-14 13:38:27 +0000 | [diff] [blame] | 329 | })) { |
| 330 | return false; |
| 331 | } |
| 332 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 333 | const TypeInfo* last_kind = nullptr; |
| 334 | size_t last_padding_line = 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 335 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 336 | auto* mod = builder_.Sem().Module(); |
| 337 | for (auto* decl : mod->DependencyOrderedDeclarations()) { |
James Price | 98dc5a8 | 2023-02-04 12:20:55 +0000 | [diff] [blame] | 338 | if (decl->IsAnyOf<ast::Alias, ast::DiagnosticDirective, ast::Enable, ast::ConstAssert>()) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 339 | continue; // These are not emitted. |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 340 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 341 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 342 | // Emit a new line between declarations if the type of declaration has |
| 343 | // changed, or we're about to emit a function |
| 344 | auto* kind = &decl->TypeInfo(); |
| 345 | if (current_buffer_->lines.size() != last_padding_line) { |
| 346 | if (last_kind && (last_kind != kind || decl->Is<ast::Function>())) { |
| 347 | line(); |
| 348 | last_padding_line = current_buffer_->lines.size(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 349 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 350 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 351 | last_kind = kind; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 352 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 353 | bool ok = Switch( |
| 354 | decl, |
| 355 | [&](const ast::Variable* global) { // |
| 356 | return EmitGlobalVariable(global); |
| 357 | }, |
| 358 | [&](const ast::Struct* str) { |
| 359 | auto* ty = builder_.Sem().Get(str); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 360 | auto address_space_uses = ty->AddressSpaceUsage(); |
| 361 | if (address_space_uses.size() != |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 362 | (address_space_uses.count(builtin::AddressSpace::kStorage) + |
| 363 | address_space_uses.count(builtin::AddressSpace::kUniform))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 364 | // The structure is used as something other than a storage buffer or |
| 365 | // uniform buffer, so it needs to be emitted. |
| 366 | // Storage buffer are read and written to via a ByteAddressBuffer |
| 367 | // instead of true structure. |
| 368 | // Structures used as uniform buffer are read from an array of |
| 369 | // vectors instead of true structure. |
| 370 | return EmitStructType(current_buffer_, ty); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 371 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 372 | return true; |
| 373 | }, |
| 374 | [&](const ast::Function* func) { |
| 375 | if (func->IsEntryPoint()) { |
| 376 | return EmitEntryPointFunction(func); |
| 377 | } |
| 378 | return EmitFunction(func); |
| 379 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 380 | [&](Default) { |
| 381 | TINT_ICE(Writer, diagnostics_) |
| 382 | << "unhandled module-scope declaration: " << decl->TypeInfo().name; |
| 383 | return false; |
| 384 | }); |
| 385 | |
| 386 | if (!ok) { |
| 387 | return false; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (!helpers_.lines.empty()) { |
| 392 | current_buffer_->Insert(helpers_, 0, 0); |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | bool GeneratorImpl::EmitDynamicVectorAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 399 | const type::Vector* vec) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 400 | auto name = utils::GetOrCreate(dynamic_vector_write_, vec, [&]() -> std::string { |
| 401 | std::string fn; |
| 402 | { |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 403 | utils::StringStream ss; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 404 | if (!EmitType(ss, vec, tint::builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 405 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 406 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 407 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 408 | fn = UniqueIdentifier("set_" + ss.str()); |
| 409 | } |
| 410 | { |
| 411 | auto out = line(&helpers_); |
| 412 | out << "void " << fn << "(inout "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 413 | if (!EmitTypeAndName(out, vec, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 414 | builtin::Access::kUndefined, "vec")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 415 | return ""; |
| 416 | } |
| 417 | out << ", int idx, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 418 | if (!EmitTypeAndName(out, vec->type(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 419 | builtin::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 420 | return ""; |
| 421 | } |
| 422 | out << ") {"; |
| 423 | } |
| 424 | { |
| 425 | ScopedIndent si(&helpers_); |
| 426 | auto out = line(&helpers_); |
| 427 | switch (vec->Width()) { |
| 428 | case 2: |
| 429 | out << "vec = (idx.xx == int2(0, 1)) ? val.xx : vec;"; |
| 430 | break; |
| 431 | case 3: |
| 432 | out << "vec = (idx.xxx == int3(0, 1, 2)) ? val.xxx : vec;"; |
| 433 | break; |
| 434 | case 4: |
| 435 | out << "vec = (idx.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : vec;"; |
| 436 | break; |
| 437 | default: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 438 | TINT_UNREACHABLE(Writer, diagnostics_) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 439 | << "invalid vector size " << vec->Width(); |
| 440 | break; |
| 441 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 442 | } |
| 443 | line(&helpers_) << "}"; |
| 444 | line(&helpers_); |
| 445 | return fn; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 446 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 447 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 448 | if (name.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 449 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 450 | } |
| 451 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 452 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 453 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 454 | auto out = line(); |
| 455 | out << name << "("; |
| 456 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 457 | return false; |
| 458 | } |
| 459 | out << ", "; |
| 460 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 461 | return false; |
| 462 | } |
| 463 | out << ", "; |
| 464 | if (!EmitExpression(out, stmt->rhs)) { |
| 465 | return false; |
| 466 | } |
| 467 | out << ");"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 468 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 471 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 472 | bool GeneratorImpl::EmitDynamicMatrixVectorAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 473 | const type::Matrix* mat) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 474 | auto name = utils::GetOrCreate(dynamic_matrix_vector_write_, mat, [&]() -> std::string { |
| 475 | std::string fn; |
| 476 | { |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 477 | utils::StringStream ss; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 478 | if (!EmitType(ss, mat, tint::builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 479 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 480 | return ""; |
| 481 | } |
| 482 | fn = UniqueIdentifier("set_vector_" + ss.str()); |
| 483 | } |
| 484 | { |
| 485 | auto out = line(&helpers_); |
| 486 | out << "void " << fn << "(inout "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 487 | if (!EmitTypeAndName(out, mat, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 488 | builtin::Access::kUndefined, "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 489 | return ""; |
| 490 | } |
| 491 | out << ", int col, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 492 | if (!EmitTypeAndName(out, mat->ColumnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 493 | builtin::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 494 | return ""; |
| 495 | } |
| 496 | out << ") {"; |
| 497 | } |
| 498 | { |
| 499 | ScopedIndent si(&helpers_); |
| 500 | line(&helpers_) << "switch (col) {"; |
| 501 | { |
| 502 | ScopedIndent si2(&helpers_); |
| 503 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
| 504 | line(&helpers_) << "case " << i << ": mat[" << i << "] = val; break;"; |
| 505 | } |
| 506 | } |
| 507 | line(&helpers_) << "}"; |
| 508 | } |
| 509 | line(&helpers_) << "}"; |
| 510 | line(&helpers_); |
| 511 | return fn; |
| 512 | }); |
| 513 | |
| 514 | if (name.empty()) { |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 519 | |
| 520 | auto out = line(); |
| 521 | out << name << "("; |
| 522 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 523 | return false; |
| 524 | } |
| 525 | out << ", "; |
| 526 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 527 | return false; |
| 528 | } |
| 529 | out << ", "; |
| 530 | if (!EmitExpression(out, stmt->rhs)) { |
| 531 | return false; |
| 532 | } |
| 533 | out << ");"; |
| 534 | |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | bool GeneratorImpl::EmitDynamicMatrixScalarAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 539 | const type::Matrix* mat) { |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 540 | auto* lhs_row_access = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 541 | auto* lhs_col_access = lhs_row_access->object->As<ast::IndexAccessorExpression>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 542 | |
| 543 | auto name = utils::GetOrCreate(dynamic_matrix_scalar_write_, mat, [&]() -> std::string { |
| 544 | std::string fn; |
| 545 | { |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 546 | utils::StringStream ss; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 547 | if (!EmitType(ss, mat, tint::builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 548 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 549 | return ""; |
| 550 | } |
| 551 | fn = UniqueIdentifier("set_scalar_" + ss.str()); |
| 552 | } |
| 553 | { |
| 554 | auto out = line(&helpers_); |
| 555 | out << "void " << fn << "(inout "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 556 | if (!EmitTypeAndName(out, mat, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 557 | builtin::Access::kUndefined, "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 558 | return ""; |
| 559 | } |
| 560 | out << ", int col, int row, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 561 | if (!EmitTypeAndName(out, mat->type(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 562 | builtin::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 563 | return ""; |
| 564 | } |
| 565 | out << ") {"; |
| 566 | } |
| 567 | { |
| 568 | ScopedIndent si(&helpers_); |
| 569 | line(&helpers_) << "switch (col) {"; |
| 570 | { |
| 571 | ScopedIndent si2(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 572 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
| 573 | line(&helpers_) << "case " << i << ":"; |
| 574 | { |
| 575 | auto vec_name = "mat[" + std::to_string(i) + "]"; |
| 576 | ScopedIndent si3(&helpers_); |
| 577 | { |
| 578 | auto out = line(&helpers_); |
| 579 | switch (mat->rows()) { |
| 580 | case 2: |
| 581 | out << vec_name |
| 582 | << " = (row.xx == int2(0, 1)) ? val.xx : " << vec_name |
| 583 | << ";"; |
| 584 | break; |
| 585 | case 3: |
| 586 | out << vec_name |
| 587 | << " = (row.xxx == int3(0, 1, 2)) ? val.xxx : " << vec_name |
| 588 | << ";"; |
| 589 | break; |
| 590 | case 4: |
| 591 | out << vec_name |
| 592 | << " = (row.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : " |
| 593 | << vec_name << ";"; |
| 594 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 595 | default: { |
| 596 | auto* vec = TypeOf(lhs_row_access->object) |
| 597 | ->UnwrapRef() |
| 598 | ->As<type::Vector>(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 599 | TINT_UNREACHABLE(Writer, diagnostics_) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 600 | << "invalid vector size " << vec->Width(); |
| 601 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 602 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | line(&helpers_) << "break;"; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | line(&helpers_) << "}"; |
| 610 | } |
| 611 | line(&helpers_) << "}"; |
| 612 | line(&helpers_); |
| 613 | return fn; |
| 614 | }); |
| 615 | |
| 616 | if (name.empty()) { |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | auto out = line(); |
| 621 | out << name << "("; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 622 | if (!EmitExpression(out, lhs_col_access->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 623 | return false; |
| 624 | } |
| 625 | out << ", "; |
| 626 | if (!EmitExpression(out, lhs_col_access->index)) { |
| 627 | return false; |
| 628 | } |
| 629 | out << ", "; |
| 630 | if (!EmitExpression(out, lhs_row_access->index)) { |
| 631 | return false; |
| 632 | } |
| 633 | out << ", "; |
| 634 | if (!EmitExpression(out, stmt->rhs)) { |
| 635 | return false; |
| 636 | } |
| 637 | out << ");"; |
| 638 | |
| 639 | return true; |
| 640 | } |
| 641 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 642 | bool GeneratorImpl::EmitIndexAccessor(utils::StringStream& out, |
| 643 | const ast::IndexAccessorExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 644 | if (!EmitExpression(out, expr->object)) { |
| 645 | return false; |
| 646 | } |
| 647 | out << "["; |
| 648 | |
| 649 | if (!EmitExpression(out, expr->index)) { |
| 650 | return false; |
| 651 | } |
| 652 | out << "]"; |
| 653 | |
| 654 | return true; |
| 655 | } |
| 656 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 657 | bool GeneratorImpl::EmitBitcast(utils::StringStream& out, const ast::BitcastExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 658 | auto* type = TypeOf(expr); |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 659 | if (auto* vec = type->UnwrapRef()->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 660 | type = vec->type(); |
| 661 | } |
| 662 | |
| 663 | if (!type->is_integer_scalar() && !type->is_float_scalar()) { |
| 664 | diagnostics_.add_error(diag::System::Writer, "Unable to do bitcast to type " + |
| 665 | type->FriendlyName(builder_.Symbols())); |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | out << "as"; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 670 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 673 | out << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 674 | if (!EmitExpression(out, expr->expr)) { |
| 675 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 676 | } |
| 677 | out << ")"; |
| 678 | return true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 679 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 680 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 681 | bool GeneratorImpl::EmitAssign(const ast::AssignmentStatement* stmt) { |
| 682 | if (auto* lhs_access = stmt->lhs->As<ast::IndexAccessorExpression>()) { |
| 683 | // BUG(crbug.com/tint/1333): work around assignment of scalar to matrices |
| 684 | // with at least one dynamic index |
| 685 | if (auto* lhs_sub_access = lhs_access->object->As<ast::IndexAccessorExpression>()) { |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 686 | if (auto* mat = TypeOf(lhs_sub_access->object)->UnwrapRef()->As<type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 687 | auto* rhs_row_idx_sem = builder_.Sem().GetVal(lhs_access->index); |
| 688 | auto* rhs_col_idx_sem = builder_.Sem().GetVal(lhs_sub_access->index); |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 689 | if (!rhs_row_idx_sem->ConstantValue() || !rhs_col_idx_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 690 | return EmitDynamicMatrixScalarAssignment(stmt, mat); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | // BUG(crbug.com/tint/1333): work around assignment of vector to matrices |
| 695 | // with dynamic indices |
| 696 | const auto* lhs_access_type = TypeOf(lhs_access->object)->UnwrapRef(); |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 697 | if (auto* mat = lhs_access_type->As<type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 698 | auto* lhs_index_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 699 | if (!lhs_index_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 700 | return EmitDynamicMatrixVectorAssignment(stmt, mat); |
| 701 | } |
| 702 | } |
| 703 | // BUG(crbug.com/tint/534): work around assignment to vectors with dynamic |
| 704 | // indices |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 705 | if (auto* vec = lhs_access_type->As<type::Vector>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 706 | auto* rhs_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 707 | if (!rhs_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 708 | return EmitDynamicVectorAssignment(stmt, vec); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | auto out = line(); |
| 714 | if (!EmitExpression(out, stmt->lhs)) { |
| 715 | return false; |
| 716 | } |
| 717 | out << " = "; |
| 718 | if (!EmitExpression(out, stmt->rhs)) { |
| 719 | return false; |
| 720 | } |
| 721 | out << ";"; |
| 722 | return true; |
| 723 | } |
| 724 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 725 | bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 726 | if (expr->op == ast::BinaryOp::kLogicalAnd || expr->op == ast::BinaryOp::kLogicalOr) { |
| 727 | auto name = UniqueIdentifier(kTempNamePrefix); |
| 728 | |
| 729 | { |
| 730 | auto pre = line(); |
| 731 | pre << "bool " << name << " = "; |
| 732 | if (!EmitExpression(pre, expr->lhs)) { |
| 733 | return false; |
| 734 | } |
| 735 | pre << ";"; |
| 736 | } |
| 737 | |
| 738 | if (expr->op == ast::BinaryOp::kLogicalOr) { |
| 739 | line() << "if (!" << name << ") {"; |
| 740 | } else { |
| 741 | line() << "if (" << name << ") {"; |
| 742 | } |
| 743 | |
| 744 | { |
| 745 | ScopedIndent si(this); |
| 746 | auto pre = line(); |
| 747 | pre << name << " = "; |
| 748 | if (!EmitExpression(pre, expr->rhs)) { |
| 749 | return false; |
| 750 | } |
| 751 | pre << ";"; |
| 752 | } |
| 753 | |
| 754 | line() << "}"; |
| 755 | |
| 756 | out << "(" << name << ")"; |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef(); |
| 761 | auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef(); |
| 762 | // Multiplying by a matrix requires the use of `mul` in order to get the |
| 763 | // type of multiply we desire. |
| 764 | if (expr->op == ast::BinaryOp::kMultiply && |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 765 | ((lhs_type->Is<type::Vector>() && rhs_type->Is<type::Matrix>()) || |
| 766 | (lhs_type->Is<type::Matrix>() && rhs_type->Is<type::Vector>()) || |
| 767 | (lhs_type->Is<type::Matrix>() && rhs_type->Is<type::Matrix>()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 768 | // Matrices are transposed, so swap LHS and RHS. |
| 769 | out << "mul("; |
| 770 | if (!EmitExpression(out, expr->rhs)) { |
| 771 | return false; |
| 772 | } |
| 773 | out << ", "; |
| 774 | if (!EmitExpression(out, expr->lhs)) { |
| 775 | return false; |
| 776 | } |
| 777 | out << ")"; |
| 778 | |
| 779 | return true; |
| 780 | } |
| 781 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 782 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 783 | |
| 784 | if (!EmitExpression(out, expr->lhs)) { |
| 785 | return false; |
| 786 | } |
| 787 | out << " "; |
| 788 | |
| 789 | switch (expr->op) { |
| 790 | case ast::BinaryOp::kAnd: |
| 791 | out << "&"; |
| 792 | break; |
| 793 | case ast::BinaryOp::kOr: |
| 794 | out << "|"; |
| 795 | break; |
| 796 | case ast::BinaryOp::kXor: |
| 797 | out << "^"; |
| 798 | break; |
| 799 | case ast::BinaryOp::kLogicalAnd: |
| 800 | case ast::BinaryOp::kLogicalOr: { |
| 801 | // These are both handled above. |
| 802 | TINT_UNREACHABLE(Writer, diagnostics_); |
| 803 | return false; |
| 804 | } |
| 805 | case ast::BinaryOp::kEqual: |
| 806 | out << "=="; |
| 807 | break; |
| 808 | case ast::BinaryOp::kNotEqual: |
| 809 | out << "!="; |
| 810 | break; |
| 811 | case ast::BinaryOp::kLessThan: |
| 812 | out << "<"; |
| 813 | break; |
| 814 | case ast::BinaryOp::kGreaterThan: |
| 815 | out << ">"; |
| 816 | break; |
| 817 | case ast::BinaryOp::kLessThanEqual: |
| 818 | out << "<="; |
| 819 | break; |
| 820 | case ast::BinaryOp::kGreaterThanEqual: |
| 821 | out << ">="; |
| 822 | break; |
| 823 | case ast::BinaryOp::kShiftLeft: |
| 824 | out << "<<"; |
| 825 | break; |
| 826 | case ast::BinaryOp::kShiftRight: |
| 827 | // TODO(dsinclair): MSL is based on C++14, and >> in C++14 has |
| 828 | // implementation-defined behaviour for negative LHS. We may have to |
| 829 | // generate extra code to implement WGSL-specified behaviour for negative |
| 830 | // LHS. |
| 831 | out << R"(>>)"; |
| 832 | break; |
| 833 | |
| 834 | case ast::BinaryOp::kAdd: |
| 835 | out << "+"; |
| 836 | break; |
| 837 | case ast::BinaryOp::kSubtract: |
| 838 | out << "-"; |
| 839 | break; |
| 840 | case ast::BinaryOp::kMultiply: |
| 841 | out << "*"; |
| 842 | break; |
| 843 | case ast::BinaryOp::kDivide: |
| 844 | out << "/"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 845 | break; |
| 846 | case ast::BinaryOp::kModulo: |
| 847 | out << "%"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 848 | break; |
| 849 | case ast::BinaryOp::kNone: |
| 850 | diagnostics_.add_error(diag::System::Writer, "missing binary operation type"); |
| 851 | return false; |
| 852 | } |
| 853 | out << " "; |
| 854 | |
| 855 | if (!EmitExpression(out, expr->rhs)) { |
| 856 | return false; |
| 857 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 858 | |
| 859 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 862 | bool GeneratorImpl::EmitStatements(utils::VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 863 | for (auto* s : stmts) { |
| 864 | if (!EmitStatement(s)) { |
| 865 | return false; |
| 866 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 867 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 868 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 871 | bool GeneratorImpl::EmitStatementsWithIndent(utils::VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 872 | ScopedIndent si(this); |
| 873 | return EmitStatements(stmts); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 877 | line() << "{"; |
| 878 | if (!EmitStatementsWithIndent(stmt->statements)) { |
| 879 | return false; |
| 880 | } |
| 881 | line() << "}"; |
| 882 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | bool GeneratorImpl::EmitBreak(const ast::BreakStatement*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 886 | line() << "break;"; |
| 887 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 888 | } |
| 889 | |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 890 | bool GeneratorImpl::EmitBreakIf(const ast::BreakIfStatement* b) { |
| 891 | auto out = line(); |
| 892 | out << "if ("; |
| 893 | if (!EmitExpression(out, b->condition)) { |
| 894 | return false; |
| 895 | } |
| 896 | out << ") { break; }"; |
| 897 | return true; |
| 898 | } |
| 899 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 900 | bool GeneratorImpl::EmitCall(utils::StringStream& out, const ast::CallExpression* expr) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 901 | auto* call = builder_.Sem().Get<sem::Call>(expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 902 | auto* target = call->Target(); |
| 903 | return Switch( |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 904 | target, // |
| 905 | [&](const sem::Function* func) { return EmitFunctionCall(out, call, func); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 906 | [&](const sem::Builtin* builtin) { return EmitBuiltinCall(out, call, builtin); }, |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 907 | [&](const sem::ValueConversion* conv) { return EmitValueConversion(out, call, conv); }, |
| 908 | [&](const sem::ValueConstructor* ctor) { return EmitValueConstructor(out, call, ctor); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 909 | [&](Default) { |
| 910 | TINT_ICE(Writer, diagnostics_) << "unhandled call target: " << target->TypeInfo().name; |
| 911 | return false; |
| 912 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 913 | } |
| 914 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 915 | bool GeneratorImpl::EmitFunctionCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 916 | const sem::Call* call, |
| 917 | const sem::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 918 | auto* expr = call->Declaration(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 919 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 920 | if (ast::HasAttribute<transform::CalculateArrayLength::BufferSizeIntrinsic>( |
| 921 | func->Declaration()->attributes)) { |
| 922 | // Special function generated by the CalculateArrayLength transform for |
| 923 | // calling X.GetDimensions(Y) |
| 924 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 925 | return false; |
| 926 | } |
| 927 | out << ".GetDimensions("; |
| 928 | if (!EmitExpression(out, call->Arguments()[1]->Declaration())) { |
| 929 | return false; |
| 930 | } |
| 931 | out << ")"; |
| 932 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 933 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 934 | |
| 935 | if (auto* intrinsic = ast::GetAttribute<transform::DecomposeMemoryAccess::Intrinsic>( |
| 936 | func->Declaration()->attributes)) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 937 | switch (intrinsic->address_space) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 938 | case builtin::AddressSpace::kUniform: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 939 | return EmitUniformBufferAccess(out, expr, intrinsic); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 940 | case builtin::AddressSpace::kStorage: |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 941 | if (!intrinsic->IsAtomic()) { |
| 942 | return EmitStorageBufferAccess(out, expr, intrinsic); |
| 943 | } |
| 944 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 945 | default: |
| 946 | TINT_UNREACHABLE(Writer, diagnostics_) |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 947 | << "unsupported DecomposeMemoryAccess::Intrinsic address space:" |
| 948 | << intrinsic->address_space; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 949 | return false; |
| 950 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 951 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 952 | |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 953 | out << builder_.Symbols().NameFor(func->Declaration()->name->symbol) << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 954 | |
| 955 | bool first = true; |
| 956 | for (auto* arg : call->Arguments()) { |
| 957 | if (!first) { |
| 958 | out << ", "; |
| 959 | } |
| 960 | first = false; |
| 961 | |
| 962 | if (!EmitExpression(out, arg->Declaration())) { |
| 963 | return false; |
| 964 | } |
| 965 | } |
| 966 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 967 | out << ")"; |
| 968 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 969 | } |
| 970 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 971 | bool GeneratorImpl::EmitBuiltinCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 972 | const sem::Call* call, |
| 973 | const sem::Builtin* builtin) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 974 | const auto type = builtin->Type(); |
| 975 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 976 | auto* expr = call->Declaration(); |
| 977 | if (builtin->IsTexture()) { |
| 978 | return EmitTextureCall(out, call, builtin); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 979 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 980 | if (type == builtin::Function::kSelect) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 981 | return EmitSelectCall(out, expr); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 982 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 983 | if (type == builtin::Function::kModf) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 984 | return EmitModfCall(out, expr, builtin); |
| 985 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 986 | if (type == builtin::Function::kFrexp) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 987 | return EmitFrexpCall(out, expr, builtin); |
| 988 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 989 | if (type == builtin::Function::kDegrees) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 990 | return EmitDegreesCall(out, expr, builtin); |
| 991 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 992 | if (type == builtin::Function::kRadians) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 993 | return EmitRadiansCall(out, expr, builtin); |
| 994 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 995 | if (type == builtin::Function::kSign) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 996 | return EmitSignCall(out, call, builtin); |
| 997 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 998 | if (type == builtin::Function::kQuantizeToF16) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 999 | return EmitQuantizeToF16Call(out, expr, builtin); |
| 1000 | } |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 1001 | if (type == builtin::Function::kTrunc) { |
| 1002 | return EmitTruncCall(out, expr, builtin); |
| 1003 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1004 | if (builtin->IsDataPacking()) { |
| 1005 | return EmitDataPackingCall(out, expr, builtin); |
| 1006 | } |
| 1007 | if (builtin->IsDataUnpacking()) { |
| 1008 | return EmitDataUnpackingCall(out, expr, builtin); |
| 1009 | } |
| 1010 | if (builtin->IsBarrier()) { |
| 1011 | return EmitBarrierCall(out, builtin); |
| 1012 | } |
| 1013 | if (builtin->IsAtomic()) { |
| 1014 | return EmitWorkgroupAtomicCall(out, expr, builtin); |
| 1015 | } |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 1016 | if (builtin->IsDP4a()) { |
| 1017 | return EmitDP4aCall(out, expr, builtin); |
| 1018 | } |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1019 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1020 | auto name = generate_builtin_name(builtin); |
| 1021 | if (name.empty()) { |
| 1022 | return false; |
| 1023 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1024 | |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1025 | // Handle single argument builtins that only accept and return uint (not int overload). We need |
| 1026 | // to explicitly cast the return value (we also cast the arg for good measure). See |
| 1027 | // crbug.com/tint/1550 |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1028 | if (type == builtin::Function::kCountOneBits || type == builtin::Function::kReverseBits) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1029 | auto* arg = call->Arguments()[0]; |
Ben Clayton | 6c337aa | 2022-12-07 19:25:17 +0000 | [diff] [blame] | 1030 | if (arg->Type()->UnwrapRef()->is_signed_integer_scalar_or_vector()) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1031 | out << "asint(" << name << "(asuint("; |
| 1032 | if (!EmitExpression(out, arg->Declaration())) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | out << ")))"; |
| 1036 | return true; |
| 1037 | } |
| 1038 | } |
| 1039 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1040 | out << name << "("; |
| 1041 | |
| 1042 | bool first = true; |
| 1043 | for (auto* arg : call->Arguments()) { |
| 1044 | if (!first) { |
| 1045 | out << ", "; |
| 1046 | } |
| 1047 | first = false; |
| 1048 | |
| 1049 | if (!EmitExpression(out, arg->Declaration())) { |
| 1050 | return false; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | out << ")"; |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1055 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1056 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1059 | bool GeneratorImpl::EmitValueConversion(utils::StringStream& out, |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1060 | const sem::Call* call, |
| 1061 | const sem::ValueConversion* conv) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1062 | if (!EmitType(out, conv->Target(), builtin::AddressSpace::kUndefined, |
| 1063 | builtin::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
| 1066 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1067 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1068 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1069 | return false; |
| 1070 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1071 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1072 | out << ")"; |
| 1073 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1076 | bool GeneratorImpl::EmitValueConstructor(utils::StringStream& out, |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1077 | const sem::Call* call, |
| 1078 | const sem::ValueConstructor* ctor) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1079 | auto* type = call->Type(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1080 | |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1081 | // If the value constructor arguments are empty then we need to construct with the zero value |
| 1082 | // for all components. |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 1083 | if (call->Arguments().IsEmpty()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1084 | return EmitZeroValue(out, type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1085 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1086 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1087 | // Single parameter matrix initializers must be identity initializer. |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1088 | // It could also be conversions between f16 and f32 matrix when f16 is properly supported. |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 1089 | if (type->Is<type::Matrix>() && call->Arguments().Length() == 1) { |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1090 | if (!ctor->Parameters()[0]->Type()->UnwrapRef()->is_float_matrix()) { |
| 1091 | TINT_UNREACHABLE(Writer, diagnostics_) |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1092 | << "found a single-parameter matrix initializer that is not identity initializer"; |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1093 | return false; |
Ben Clayton | 3b5edf1 | 2022-05-16 21:14:11 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 1097 | bool brackets = type->IsAnyOf<type::Array, sem::Struct>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1098 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1099 | // For single-value vector initializers, swizzle the scalar to the right |
| 1100 | // vector dimension using .x |
| 1101 | const bool is_single_value_vector_init = type->is_scalar_vector() && |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 1102 | call->Arguments().Length() == 1 && |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1103 | ctor->Parameters()[0]->Type()->is_scalar(); |
| 1104 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 1105 | if (brackets) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1106 | out << "{"; |
| 1107 | } else { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1108 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
| 1109 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1110 | return false; |
| 1111 | } |
| 1112 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1113 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1114 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1115 | if (is_single_value_vector_init) { |
| 1116 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1117 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1118 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1119 | bool first = true; |
| 1120 | for (auto* e : call->Arguments()) { |
| 1121 | if (!first) { |
| 1122 | out << ", "; |
| 1123 | } |
| 1124 | first = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1125 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1126 | if (!EmitExpression(out, e->Declaration())) { |
| 1127 | return false; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if (is_single_value_vector_init) { |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 1132 | out << ")." << std::string(type->As<type::Vector>()->Width(), 'x'); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | out << (brackets ? "}" : ")"); |
| 1136 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | bool GeneratorImpl::EmitUniformBufferAccess( |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1140 | utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1141 | const ast::CallExpression* expr, |
| 1142 | const transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 1143 | auto const buffer = program_->Symbols().NameFor(intrinsic->Buffer()->identifier->symbol); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1144 | auto* const offset = expr->args[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1145 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1146 | // offset in bytes |
| 1147 | uint32_t scalar_offset_bytes = 0; |
| 1148 | // offset in uint (4 bytes) |
| 1149 | uint32_t scalar_offset_index = 0; |
| 1150 | // expression to calculate offset in bytes |
| 1151 | std::string scalar_offset_bytes_expr; |
| 1152 | // expression to calculate offset in uint, by dividing scalar_offset_bytes_expr by 4 |
| 1153 | std::string scalar_offset_index_expr; |
| 1154 | // expression to calculate offset in uint, independently |
| 1155 | std::string scalar_offset_index_unified_expr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1156 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1157 | // If true, use scalar_offset_index, otherwise use scalar_offset_index_expr |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1158 | bool scalar_offset_constant = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1159 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1160 | if (auto* val = builder_.Sem().GetVal(offset)->ConstantValue()) { |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 1161 | TINT_ASSERT(Writer, val->Type()->Is<type::U32>()); |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 1162 | scalar_offset_bytes = static_cast<uint32_t>(val->ValueAs<AInt>()); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1163 | scalar_offset_index = scalar_offset_bytes / 4; // bytes -> scalar index |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1164 | scalar_offset_constant = true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1165 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1166 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1167 | // If true, scalar_offset_bytes or scalar_offset_bytes_expr should be used, otherwise only use |
| 1168 | // scalar_offset_index or scalar_offset_index_unified_expr. Currently only loading f16 scalar |
| 1169 | // require using offset in bytes. |
| 1170 | const bool need_offset_in_bytes = |
| 1171 | intrinsic->type == transform::DecomposeMemoryAccess::Intrinsic::DataType::kF16; |
| 1172 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1173 | if (!scalar_offset_constant) { |
| 1174 | // UBO offset not compile-time known. |
| 1175 | // Calculate the scalar offset into a temporary. |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1176 | if (need_offset_in_bytes) { |
| 1177 | scalar_offset_bytes_expr = UniqueIdentifier("scalar_offset_bytes"); |
| 1178 | scalar_offset_index_expr = UniqueIdentifier("scalar_offset_index"); |
| 1179 | { |
| 1180 | auto pre = line(); |
| 1181 | pre << "const uint " << scalar_offset_bytes_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1182 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1183 | return false; |
| 1184 | } |
| 1185 | pre << ");"; |
| 1186 | } |
| 1187 | line() << "const uint " << scalar_offset_index_expr << " = " << scalar_offset_bytes_expr |
| 1188 | << " / 4;"; |
| 1189 | } else { |
| 1190 | scalar_offset_index_unified_expr = UniqueIdentifier("scalar_offset"); |
| 1191 | auto pre = line(); |
| 1192 | pre << "const uint " << scalar_offset_index_unified_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1193 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1194 | return false; |
| 1195 | } |
| 1196 | pre << ") / 4;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1197 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1198 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1199 | |
Austin Eng | a0e96b5 | 2023-02-18 00:39:01 +0000 | [diff] [blame] | 1200 | const char swizzle[] = {'x', 'y', 'z', 'w'}; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1201 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1202 | using Op = transform::DecomposeMemoryAccess::Intrinsic::Op; |
| 1203 | using DataType = transform::DecomposeMemoryAccess::Intrinsic::DataType; |
| 1204 | switch (intrinsic->op) { |
| 1205 | case Op::kLoad: { |
| 1206 | auto cast = [&](const char* to, auto&& load) { |
| 1207 | out << to << "("; |
| 1208 | auto result = load(); |
| 1209 | out << ")"; |
| 1210 | return result; |
| 1211 | }; |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1212 | auto load_u32_to = [&](utils::StringStream& target) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1213 | target << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1214 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1215 | target << "[" << (scalar_offset_index / 4) << "]." |
| 1216 | << swizzle[scalar_offset_index & 3]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1217 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1218 | target << "[" << scalar_offset_index_unified_expr << " / 4][" |
| 1219 | << scalar_offset_index_unified_expr << " % 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1220 | } |
| 1221 | return true; |
| 1222 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1223 | auto load_u32 = [&] { return load_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1224 | // Has a minimum alignment of 8 bytes, so is either .xy or .zw |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1225 | auto load_vec2_u32_to = [&](utils::StringStream& target) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1226 | if (scalar_offset_constant) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1227 | target << buffer << "[" << (scalar_offset_index / 4) << "]" |
| 1228 | << ((scalar_offset_index & 2) == 0 ? ".xy" : ".zw"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1229 | } else { |
| 1230 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1231 | { |
| 1232 | auto pre = line(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1233 | pre << "uint4 " << ubo_load << " = " << buffer << "[" |
| 1234 | << scalar_offset_index_unified_expr << " / 4];"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1235 | } |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1236 | target << "((" << scalar_offset_index_unified_expr << " & 2) ? " << ubo_load |
| 1237 | << ".zw : " << ubo_load << ".xy)"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1238 | } |
| 1239 | return true; |
| 1240 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1241 | auto load_vec2_u32 = [&] { return load_vec2_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1242 | // vec4 has a minimum alignment of 16 bytes, easiest case |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1243 | auto load_vec4_u32 = [&] { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1244 | out << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1245 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1246 | out << "[" << (scalar_offset_index / 4) << "]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1247 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1248 | out << "[" << scalar_offset_index_unified_expr << " / 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1249 | } |
| 1250 | return true; |
| 1251 | }; |
| 1252 | // vec3 has a minimum alignment of 16 bytes, so is just a .xyz swizzle |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1253 | auto load_vec3_u32 = [&] { |
| 1254 | if (!load_vec4_u32()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1255 | return false; |
| 1256 | } |
| 1257 | out << ".xyz"; |
| 1258 | return true; |
| 1259 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1260 | auto load_scalar_f16 = [&] { |
| 1261 | // offset bytes = 4k, ((buffer[index].x) & 0xFFFF) |
| 1262 | // offset bytes = 4k+2, ((buffer[index].x >> 16) & 0xFFFF) |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1263 | out << "float16_t(f16tof32(((" << buffer; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1264 | if (scalar_offset_constant) { |
| 1265 | out << "[" << (scalar_offset_index / 4) << "]." |
| 1266 | << swizzle[scalar_offset_index & 3]; |
| 1267 | // WGSL spec ensure little endian memory layout. |
| 1268 | if (scalar_offset_bytes % 4 == 0) { |
| 1269 | out << ") & 0xFFFF)"; |
| 1270 | } else { |
| 1271 | out << " >> 16) & 0xFFFF)"; |
| 1272 | } |
| 1273 | } else { |
| 1274 | out << "[" << scalar_offset_index_expr << " / 4][" << scalar_offset_index_expr |
| 1275 | << " % 4] >> (" << scalar_offset_bytes_expr |
| 1276 | << " % 4 == 0 ? 0 : 16)) & 0xFFFF)"; |
| 1277 | } |
| 1278 | out << "))"; |
| 1279 | return true; |
| 1280 | }; |
| 1281 | auto load_vec2_f16 = [&] { |
| 1282 | // vec2<f16> is aligned to 4 bytes |
| 1283 | // Preclude code load the vec2<f16> data as a uint: |
| 1284 | // uint ubo_load = buffer[id0][id1]; |
| 1285 | // Loading code convert it to vec2<f16>: |
| 1286 | // vector<float16_t, 2>(float16_t(f16tof32(ubo_load & 0xFFFF)), |
| 1287 | // float16_t(f16tof32(ubo_load >> 16))) |
| 1288 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1289 | { |
| 1290 | auto pre = line(); |
| 1291 | // Load the 4 bytes f16 vector as an uint |
| 1292 | pre << "uint " << ubo_load << " = "; |
| 1293 | if (!load_u32_to(pre)) { |
| 1294 | return false; |
| 1295 | } |
| 1296 | pre << ";"; |
| 1297 | } |
| 1298 | out << "vector<float16_t, 2>(float16_t(f16tof32(" << ubo_load |
| 1299 | << " & 0xFFFF)), float16_t(f16tof32(" << ubo_load << " >> 16)))"; |
| 1300 | return true; |
| 1301 | }; |
| 1302 | auto load_vec3_f16 = [&] { |
| 1303 | // vec3<f16> is aligned to 8 bytes |
| 1304 | // Preclude code load the vec3<f16> data as uint2 and convert its elements to |
| 1305 | // float16_t: |
| 1306 | // uint2 ubo_load = buffer[id0].xy; |
| 1307 | // /* The low 8 bits of two uint are the x and z elements of vec3<f16> */ |
| 1308 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1309 | // 0xFFFF)); |
| 1310 | // /* The high 8 bits of first uint is the y element of vec3<f16> */ |
| 1311 | // float16_t ubo_load_y = f16tof32(ubo_load[0] >> 16); |
| 1312 | // Loading code convert it to vec3<f16>: |
| 1313 | // vector<float16_t, 3>(ubo_load_xz[0], ubo_load_y, ubo_load_xz[1]) |
| 1314 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1315 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1316 | std::string ubo_load_y = UniqueIdentifier(ubo_load + "_y"); |
| 1317 | { |
| 1318 | auto pre = line(); |
| 1319 | // Load the 8 bytes uint2 with the f16 vector at lower 6 bytes |
| 1320 | pre << "uint2 " << ubo_load << " = "; |
| 1321 | if (!load_vec2_u32_to(pre)) { |
| 1322 | return false; |
| 1323 | } |
| 1324 | pre << ";"; |
| 1325 | } |
| 1326 | { |
| 1327 | auto pre = line(); |
| 1328 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1329 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1330 | } |
| 1331 | { |
| 1332 | auto pre = line(); |
| 1333 | pre << "float16_t " << ubo_load_y << " = f16tof32(" << ubo_load |
| 1334 | << "[0] >> 16);"; |
| 1335 | } |
| 1336 | out << "vector<float16_t, 3>(" << ubo_load_xz << "[0], " << ubo_load_y << ", " |
| 1337 | << ubo_load_xz << "[1])"; |
| 1338 | return true; |
| 1339 | }; |
| 1340 | auto load_vec4_f16 = [&] { |
| 1341 | // vec4<f16> is aligned to 8 bytes |
| 1342 | // Preclude code load the vec4<f16> data as uint2 and convert its elements to |
| 1343 | // float16_t: |
| 1344 | // uint2 ubo_load = buffer[id0].xy; |
| 1345 | // /* The low 8 bits of two uint are the x and z elements of vec4<f16> */ |
| 1346 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1347 | // 0xFFFF)); |
| 1348 | // /* The high 8 bits of two uint are the y and w elements of vec4<f16> */ |
| 1349 | // vector<float16_t, 2> ubo_load_yw = vector<float16_t, 2>(f16tof32(ubo_load >> |
| 1350 | // 16)); |
| 1351 | // Loading code convert it to vec4<f16>: |
| 1352 | // vector<float16_t, 4>(ubo_load_xz[0], ubo_load_yw[0], ubo_load_xz[1], |
| 1353 | // ubo_load_yw[1]) |
| 1354 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1355 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1356 | std::string ubo_load_yw = UniqueIdentifier(ubo_load + "_yw"); |
| 1357 | { |
| 1358 | auto pre = line(); |
| 1359 | // Load the 8 bytes f16 vector as an uint2 |
| 1360 | pre << "uint2 " << ubo_load << " = "; |
| 1361 | if (!load_vec2_u32_to(pre)) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | pre << ";"; |
| 1365 | } |
| 1366 | { |
| 1367 | auto pre = line(); |
| 1368 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1369 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1370 | } |
| 1371 | { |
| 1372 | auto pre = line(); |
| 1373 | pre << "vector<float16_t, 2> " << ubo_load_yw |
| 1374 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " >> 16));"; |
| 1375 | } |
| 1376 | out << "vector<float16_t, 4>(" << ubo_load_xz << "[0], " << ubo_load_yw << "[0], " |
| 1377 | << ubo_load_xz << "[1], " << ubo_load_yw << "[1])"; |
| 1378 | return true; |
| 1379 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1380 | switch (intrinsic->type) { |
| 1381 | case DataType::kU32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1382 | return load_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1383 | case DataType::kF32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1384 | return cast("asfloat", load_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1385 | case DataType::kI32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1386 | return cast("asint", load_u32); |
| 1387 | case DataType::kF16: |
| 1388 | return load_scalar_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1389 | case DataType::kVec2U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1390 | return load_vec2_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1391 | case DataType::kVec2F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1392 | return cast("asfloat", load_vec2_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1393 | case DataType::kVec2I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1394 | return cast("asint", load_vec2_u32); |
| 1395 | case DataType::kVec2F16: |
| 1396 | return load_vec2_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1397 | case DataType::kVec3U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1398 | return load_vec3_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1399 | case DataType::kVec3F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1400 | return cast("asfloat", load_vec3_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1401 | case DataType::kVec3I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1402 | return cast("asint", load_vec3_u32); |
| 1403 | case DataType::kVec3F16: |
| 1404 | return load_vec3_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1405 | case DataType::kVec4U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1406 | return load_vec4_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1407 | case DataType::kVec4F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1408 | return cast("asfloat", load_vec4_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1409 | case DataType::kVec4I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1410 | return cast("asint", load_vec4_u32); |
| 1411 | case DataType::kVec4F16: |
| 1412 | return load_vec4_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1413 | } |
| 1414 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 1415 | << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1416 | << static_cast<int>(intrinsic->type); |
| 1417 | return false; |
| 1418 | } |
| 1419 | default: |
| 1420 | break; |
| 1421 | } |
| 1422 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 1423 | << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " << static_cast<int>(intrinsic->op); |
| 1424 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | bool GeneratorImpl::EmitStorageBufferAccess( |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1428 | utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1429 | const ast::CallExpression* expr, |
| 1430 | const transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 1431 | auto const buffer = program_->Symbols().NameFor(intrinsic->Buffer()->identifier->symbol); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1432 | auto* const offset = expr->args[0]; |
| 1433 | auto* const value = expr->args[1]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1434 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1435 | using Op = transform::DecomposeMemoryAccess::Intrinsic::Op; |
| 1436 | using DataType = transform::DecomposeMemoryAccess::Intrinsic::DataType; |
| 1437 | switch (intrinsic->op) { |
| 1438 | case Op::kLoad: { |
| 1439 | auto load = [&](const char* cast, int n) { |
| 1440 | if (cast) { |
| 1441 | out << cast << "("; |
| 1442 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1443 | out << buffer << ".Load"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1444 | if (n > 1) { |
| 1445 | out << n; |
| 1446 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1447 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1448 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | if (cast) { |
| 1452 | out << ")"; |
| 1453 | } |
| 1454 | return true; |
| 1455 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1456 | // Templated load used for f16 types, requires SM6.2 or higher and DXC |
| 1457 | // Used by loading f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1458 | // to emit `buffer.Load<float16_t>(offset)`. |
| 1459 | auto templated_load = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1460 | out << buffer << ".Load<" << type << ">"; // templated load |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1461 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1462 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1463 | return false; |
| 1464 | } |
| 1465 | return true; |
| 1466 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1467 | switch (intrinsic->type) { |
| 1468 | case DataType::kU32: |
| 1469 | return load(nullptr, 1); |
| 1470 | case DataType::kF32: |
| 1471 | return load("asfloat", 1); |
| 1472 | case DataType::kI32: |
| 1473 | return load("asint", 1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1474 | case DataType::kF16: |
| 1475 | return templated_load("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1476 | case DataType::kVec2U32: |
| 1477 | return load(nullptr, 2); |
| 1478 | case DataType::kVec2F32: |
| 1479 | return load("asfloat", 2); |
| 1480 | case DataType::kVec2I32: |
| 1481 | return load("asint", 2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1482 | case DataType::kVec2F16: |
| 1483 | return templated_load("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1484 | case DataType::kVec3U32: |
| 1485 | return load(nullptr, 3); |
| 1486 | case DataType::kVec3F32: |
| 1487 | return load("asfloat", 3); |
| 1488 | case DataType::kVec3I32: |
| 1489 | return load("asint", 3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1490 | case DataType::kVec3F16: |
| 1491 | return templated_load("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1492 | case DataType::kVec4U32: |
| 1493 | return load(nullptr, 4); |
| 1494 | case DataType::kVec4F32: |
| 1495 | return load("asfloat", 4); |
| 1496 | case DataType::kVec4I32: |
| 1497 | return load("asint", 4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1498 | case DataType::kVec4F16: |
| 1499 | return templated_load("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1500 | } |
| 1501 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 1502 | << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1503 | << static_cast<int>(intrinsic->type); |
| 1504 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1505 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1506 | |
| 1507 | case Op::kStore: { |
| 1508 | auto store = [&](int n) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1509 | out << buffer << ".Store"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1510 | if (n > 1) { |
| 1511 | out << n; |
| 1512 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1513 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1514 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1515 | return false; |
| 1516 | } |
| 1517 | out << ", asuint"; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1518 | ScopedParen sp2(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1519 | if (!EmitExpression(out, value)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1520 | return false; |
| 1521 | } |
| 1522 | return true; |
| 1523 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1524 | // Templated stored used for f16 types, requires SM6.2 or higher and DXC |
| 1525 | // Used by storing f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1526 | // to emit `buffer.Store<float16_t>(offset)`. |
| 1527 | auto templated_store = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1528 | out << buffer << ".Store<" << type << ">"; // templated store |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1529 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1530 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
| 1533 | out << ", "; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1534 | if (!EmitExpression(out, value)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1535 | return false; |
| 1536 | } |
| 1537 | return true; |
| 1538 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1539 | switch (intrinsic->type) { |
| 1540 | case DataType::kU32: |
| 1541 | return store(1); |
| 1542 | case DataType::kF32: |
| 1543 | return store(1); |
| 1544 | case DataType::kI32: |
| 1545 | return store(1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1546 | case DataType::kF16: |
| 1547 | return templated_store("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1548 | case DataType::kVec2U32: |
| 1549 | return store(2); |
| 1550 | case DataType::kVec2F32: |
| 1551 | return store(2); |
| 1552 | case DataType::kVec2I32: |
| 1553 | return store(2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1554 | case DataType::kVec2F16: |
| 1555 | return templated_store("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1556 | case DataType::kVec3U32: |
| 1557 | return store(3); |
| 1558 | case DataType::kVec3F32: |
| 1559 | return store(3); |
| 1560 | case DataType::kVec3I32: |
| 1561 | return store(3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1562 | case DataType::kVec3F16: |
| 1563 | return templated_store("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1564 | case DataType::kVec4U32: |
| 1565 | return store(4); |
| 1566 | case DataType::kVec4F32: |
| 1567 | return store(4); |
| 1568 | case DataType::kVec4I32: |
| 1569 | return store(4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1570 | case DataType::kVec4F16: |
| 1571 | return templated_store("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1572 | } |
| 1573 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 1574 | << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1575 | << static_cast<int>(intrinsic->type); |
| 1576 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1577 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1578 | default: |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1579 | // Break out to error case below |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1580 | // Note that atomic intrinsics are generated as functions. |
| 1581 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1584 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 1585 | << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " << static_cast<int>(intrinsic->op); |
| 1586 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1589 | bool GeneratorImpl::EmitStorageAtomicIntrinsic( |
| 1590 | const ast::Function* func, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1591 | const transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1592 | using Op = transform::DecomposeMemoryAccess::Intrinsic::Op; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1593 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1594 | const sem::Function* sem_func = builder_.Sem().Get(func); |
| 1595 | auto* result_ty = sem_func->ReturnType(); |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 1596 | const auto name = builder_.Symbols().NameFor(func->name->symbol); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1597 | auto& buf = *current_buffer_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1598 | |
Ben Clayton | 63d0fab | 2023-03-06 15:43:16 +0000 | [diff] [blame] | 1599 | auto const buffer = program_->Symbols().NameFor(intrinsic->Buffer()->identifier->symbol); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1600 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1601 | auto rmw = [&](const char* hlsl) -> bool { |
| 1602 | { |
| 1603 | auto fn = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1604 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1605 | builtin::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1606 | return false; |
| 1607 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1608 | fn << "(uint offset, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1609 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1610 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1611 | return false; |
| 1612 | } |
| 1613 | fn << ") {"; |
| 1614 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1615 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1616 | buf.IncrementIndent(); |
| 1617 | TINT_DEFER({ |
| 1618 | buf.DecrementIndent(); |
| 1619 | line(&buf) << "}"; |
| 1620 | line(&buf); |
| 1621 | }); |
| 1622 | |
| 1623 | { |
| 1624 | auto l = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1625 | if (!EmitTypeAndName(l, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1626 | builtin::Access::kUndefined, "original_value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1627 | return false; |
| 1628 | } |
| 1629 | l << " = 0;"; |
| 1630 | } |
| 1631 | { |
| 1632 | auto l = line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1633 | l << buffer << "." << hlsl << "(offset, "; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1634 | if (intrinsic->op == Op::kAtomicSub) { |
| 1635 | l << "-"; |
| 1636 | } |
| 1637 | l << "value, original_value);"; |
| 1638 | } |
| 1639 | line(&buf) << "return original_value;"; |
| 1640 | return true; |
| 1641 | }; |
| 1642 | |
| 1643 | switch (intrinsic->op) { |
| 1644 | case Op::kAtomicAdd: |
| 1645 | return rmw("InterlockedAdd"); |
| 1646 | |
| 1647 | case Op::kAtomicSub: |
| 1648 | // Use add with the operand negated. |
| 1649 | return rmw("InterlockedAdd"); |
| 1650 | |
| 1651 | case Op::kAtomicMax: |
| 1652 | return rmw("InterlockedMax"); |
| 1653 | |
| 1654 | case Op::kAtomicMin: |
| 1655 | return rmw("InterlockedMin"); |
| 1656 | |
| 1657 | case Op::kAtomicAnd: |
| 1658 | return rmw("InterlockedAnd"); |
| 1659 | |
| 1660 | case Op::kAtomicOr: |
| 1661 | return rmw("InterlockedOr"); |
| 1662 | |
| 1663 | case Op::kAtomicXor: |
| 1664 | return rmw("InterlockedXor"); |
| 1665 | |
| 1666 | case Op::kAtomicExchange: |
| 1667 | return rmw("InterlockedExchange"); |
| 1668 | |
| 1669 | case Op::kAtomicLoad: { |
| 1670 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 1671 | // InterlockedOr using 0 as the OR value |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1672 | { |
| 1673 | auto fn = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1674 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1675 | builtin::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1676 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1677 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1678 | fn << "(uint offset) {"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | buf.IncrementIndent(); |
| 1682 | TINT_DEFER({ |
| 1683 | buf.DecrementIndent(); |
| 1684 | line(&buf) << "}"; |
| 1685 | line(&buf); |
| 1686 | }); |
| 1687 | |
| 1688 | { |
| 1689 | auto l = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1690 | if (!EmitTypeAndName(l, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1691 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1692 | return false; |
| 1693 | } |
| 1694 | l << " = 0;"; |
| 1695 | } |
| 1696 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1697 | line(&buf) << buffer << ".InterlockedOr(offset, 0, value);"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1698 | line(&buf) << "return value;"; |
| 1699 | return true; |
| 1700 | } |
| 1701 | case Op::kAtomicStore: { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1702 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1703 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 1704 | // InterlockedExchange and discard the returned value |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1705 | { |
| 1706 | auto fn = line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1707 | fn << "void " << name << "(uint offset, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1708 | if (!EmitTypeAndName(fn, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1709 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1710 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1711 | } |
| 1712 | fn << ") {"; |
| 1713 | } |
| 1714 | |
| 1715 | buf.IncrementIndent(); |
| 1716 | TINT_DEFER({ |
| 1717 | buf.DecrementIndent(); |
| 1718 | line(&buf) << "}"; |
| 1719 | line(&buf); |
| 1720 | }); |
| 1721 | |
| 1722 | { |
| 1723 | auto l = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1724 | if (!EmitTypeAndName(l, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1725 | builtin::Access::kUndefined, "ignored")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1726 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1727 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1728 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1729 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1730 | line(&buf) << buffer << ".InterlockedExchange(offset, value, ignored);"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1731 | return true; |
| 1732 | } |
| 1733 | case Op::kAtomicCompareExchangeWeak: { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1734 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1735 | // NOTE: We don't need to emit the return type struct here as DecomposeMemoryAccess |
| 1736 | // already added it to the AST, and it should have already been emitted by now. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1737 | { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1738 | auto fn = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1739 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1740 | builtin::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1741 | return false; |
| 1742 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1743 | fn << "(uint offset, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1744 | if (!EmitTypeAndName(fn, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1745 | builtin::Access::kUndefined, "compare")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1746 | return false; |
| 1747 | } |
| 1748 | fn << ", "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1749 | if (!EmitTypeAndName(fn, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1750 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | } |
| 1753 | fn << ") {"; |
| 1754 | } |
| 1755 | |
| 1756 | buf.IncrementIndent(); |
| 1757 | TINT_DEFER({ |
| 1758 | buf.DecrementIndent(); |
| 1759 | line(&buf) << "}"; |
| 1760 | line(&buf); |
| 1761 | }); |
| 1762 | |
| 1763 | { // T result = {0}; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1764 | auto l = line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1765 | if (!EmitTypeAndName(l, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1766 | builtin::Access::kUndefined, "result")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1767 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1768 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1769 | l << "="; |
| 1770 | if (!EmitZeroValue(l, result_ty)) { |
| 1771 | return false; |
| 1772 | } |
| 1773 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1774 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1775 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1776 | line(&buf) << buffer |
| 1777 | << ".InterlockedCompareExchange(offset, compare, value, result.old_value);"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1778 | line(&buf) << "result.exchanged = result.old_value == compare;"; |
| 1779 | line(&buf) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1780 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1781 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1782 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1783 | default: |
| 1784 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1785 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1786 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1787 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 1788 | << "unsupported atomic DecomposeMemoryAccess::Intrinsic::Op: " |
| 1789 | << static_cast<int>(intrinsic->op); |
| 1790 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1793 | bool GeneratorImpl::EmitWorkgroupAtomicCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1794 | const ast::CallExpression* expr, |
| 1795 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1796 | std::string result = UniqueIdentifier("atomic_result"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1797 | |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 1798 | if (!builtin->ReturnType()->Is<type::Void>()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1799 | auto pre = line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1800 | if (!EmitTypeAndName(pre, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1801 | builtin::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1802 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1803 | } |
| 1804 | pre << " = "; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1805 | if (!EmitZeroValue(pre, builtin->ReturnType())) { |
| 1806 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1807 | } |
| 1808 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1809 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1810 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1811 | auto call = [&](const char* name) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1812 | auto pre = line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1813 | pre << name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1814 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1815 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1816 | ScopedParen sp(pre); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 1817 | for (size_t i = 0; i < expr->args.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1818 | auto* arg = expr->args[i]; |
| 1819 | if (i > 0) { |
| 1820 | pre << ", "; |
| 1821 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1822 | if (i == 1 && builtin->Type() == builtin::Function::kAtomicSub) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1823 | // Sub uses InterlockedAdd with the operand negated. |
| 1824 | pre << "-"; |
| 1825 | } |
| 1826 | if (!EmitExpression(pre, arg)) { |
| 1827 | return false; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | pre << ", " << result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1832 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1833 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1834 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1835 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1836 | out << result; |
| 1837 | return true; |
| 1838 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1839 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1840 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1841 | case builtin::Function::kAtomicLoad: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1842 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 1843 | // InterlockedOr using 0 as the OR value |
| 1844 | auto pre = line(); |
| 1845 | pre << "InterlockedOr"; |
| 1846 | { |
| 1847 | ScopedParen sp(pre); |
| 1848 | if (!EmitExpression(pre, expr->args[0])) { |
| 1849 | return false; |
| 1850 | } |
| 1851 | pre << ", 0, " << result; |
| 1852 | } |
| 1853 | pre << ";"; |
| 1854 | |
| 1855 | out << result; |
| 1856 | return true; |
| 1857 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1858 | case builtin::Function::kAtomicStore: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1859 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 1860 | // InterlockedExchange and discard the returned value |
| 1861 | { // T result = 0; |
| 1862 | auto pre = line(); |
| 1863 | auto* value_ty = builtin->Parameters()[1]->Type()->UnwrapRef(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1864 | if (!EmitTypeAndName(pre, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1865 | builtin::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1866 | return false; |
| 1867 | } |
| 1868 | pre << " = "; |
| 1869 | if (!EmitZeroValue(pre, value_ty)) { |
| 1870 | return false; |
| 1871 | } |
| 1872 | pre << ";"; |
| 1873 | } |
| 1874 | |
| 1875 | out << "InterlockedExchange"; |
| 1876 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1877 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1878 | if (!EmitExpression(out, expr->args[0])) { |
| 1879 | return false; |
| 1880 | } |
| 1881 | out << ", "; |
| 1882 | if (!EmitExpression(out, expr->args[1])) { |
| 1883 | return false; |
| 1884 | } |
| 1885 | out << ", " << result; |
| 1886 | } |
| 1887 | return true; |
| 1888 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1889 | case builtin::Function::kAtomicCompareExchangeWeak: { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 1890 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<sem::Struct>())) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1891 | return false; |
| 1892 | } |
| 1893 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1894 | auto* dest = expr->args[0]; |
| 1895 | auto* compare_value = expr->args[1]; |
| 1896 | auto* value = expr->args[2]; |
| 1897 | |
| 1898 | std::string compare = UniqueIdentifier("atomic_compare_value"); |
| 1899 | |
| 1900 | { // T compare_value = <compare_value>; |
| 1901 | auto pre = line(); |
Antonio Maiorano | f99671b | 2022-06-23 13:14:54 +0000 | [diff] [blame] | 1902 | if (!EmitTypeAndName(pre, TypeOf(compare_value)->UnwrapRef(), |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1903 | builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | 61c16eb | 2023-01-21 23:44:38 +0000 | [diff] [blame] | 1904 | compare)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | } |
| 1907 | pre << " = "; |
| 1908 | if (!EmitExpression(pre, compare_value)) { |
| 1909 | return false; |
| 1910 | } |
| 1911 | pre << ";"; |
| 1912 | } |
| 1913 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1914 | { // InterlockedCompareExchange(dst, compare, value, result.old_value); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1915 | auto pre = line(); |
| 1916 | pre << "InterlockedCompareExchange"; |
| 1917 | { |
| 1918 | ScopedParen sp(pre); |
| 1919 | if (!EmitExpression(pre, dest)) { |
| 1920 | return false; |
| 1921 | } |
| 1922 | pre << ", " << compare << ", "; |
| 1923 | if (!EmitExpression(pre, value)) { |
| 1924 | return false; |
| 1925 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1926 | pre << ", " << result << ".old_value"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1927 | } |
| 1928 | pre << ";"; |
| 1929 | } |
| 1930 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1931 | // result.exchanged = result.old_value == compare; |
| 1932 | line() << result << ".exchanged = " << result << ".old_value == " << compare << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1933 | |
| 1934 | out << result; |
| 1935 | return true; |
| 1936 | } |
| 1937 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1938 | case builtin::Function::kAtomicAdd: |
| 1939 | case builtin::Function::kAtomicSub: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1940 | return call("InterlockedAdd"); |
| 1941 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1942 | case builtin::Function::kAtomicMax: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1943 | return call("InterlockedMax"); |
| 1944 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1945 | case builtin::Function::kAtomicMin: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1946 | return call("InterlockedMin"); |
| 1947 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1948 | case builtin::Function::kAtomicAnd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1949 | return call("InterlockedAnd"); |
| 1950 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1951 | case builtin::Function::kAtomicOr: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1952 | return call("InterlockedOr"); |
| 1953 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1954 | case builtin::Function::kAtomicXor: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1955 | return call("InterlockedXor"); |
| 1956 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1957 | case builtin::Function::kAtomicExchange: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1958 | return call("InterlockedExchange"); |
| 1959 | |
| 1960 | default: |
| 1961 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1964 | TINT_UNREACHABLE(Writer, diagnostics_) << "unsupported atomic builtin: " << builtin->Type(); |
| 1965 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1968 | bool GeneratorImpl::EmitSelectCall(utils::StringStream& out, const ast::CallExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1969 | auto* expr_false = expr->args[0]; |
| 1970 | auto* expr_true = expr->args[1]; |
| 1971 | auto* expr_cond = expr->args[2]; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1972 | ScopedParen paren(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1973 | if (!EmitExpression(out, expr_cond)) { |
| 1974 | return false; |
| 1975 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1976 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1977 | out << " ? "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1978 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1979 | if (!EmitExpression(out, expr_true)) { |
| 1980 | return false; |
| 1981 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1982 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1983 | out << " : "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1984 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1985 | if (!EmitExpression(out, expr_false)) { |
| 1986 | return false; |
| 1987 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1988 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1989 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 1992 | bool GeneratorImpl::EmitModfCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1993 | const ast::CallExpression* expr, |
| 1994 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1995 | return CallBuiltinHelper( |
| 1996 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 1997 | auto* ty = builtin->Parameters()[0]->Type(); |
| 1998 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1999 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2000 | std::string width; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2001 | if (auto* vec = ty->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2002 | width = std::to_string(vec->Width()); |
| 2003 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2004 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2005 | // Emit the builtin return type unique to this overload. This does not |
| 2006 | // exist in the AST, so it will not be generated in Generate(). |
| 2007 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<sem::Struct>())) { |
| 2008 | return false; |
| 2009 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2010 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2011 | { |
| 2012 | auto l = line(b); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2013 | if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2014 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2015 | return false; |
| 2016 | } |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2017 | l << " result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2018 | } |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2019 | line(b) << "result.fract = modf(" << params[0] << ", result.whole);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2020 | line(b) << "return result;"; |
| 2021 | return true; |
| 2022 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2025 | bool GeneratorImpl::EmitFrexpCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2026 | const ast::CallExpression* expr, |
| 2027 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2028 | return CallBuiltinHelper( |
| 2029 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2030 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2031 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2032 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2033 | std::string width; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2034 | if (auto* vec = ty->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2035 | width = std::to_string(vec->Width()); |
| 2036 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2037 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2038 | // Emit the builtin return type unique to this overload. This does not |
| 2039 | // exist in the AST, so it will not be generated in Generate(). |
| 2040 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<sem::Struct>())) { |
| 2041 | return false; |
| 2042 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2043 | |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2044 | std::string member_type; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 2045 | if (Is<type::F16>(type::Type::DeepestElementOf(ty))) { |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2046 | member_type = width.empty() ? "float16_t" : ("vector<float16_t, " + width + ">"); |
| 2047 | } else { |
| 2048 | member_type = "float" + width; |
| 2049 | } |
| 2050 | |
| 2051 | line(b) << member_type << " exp;"; |
Antonio Maiorano | 52cd8ca | 2023-03-16 21:31:15 +0000 | [diff] [blame] | 2052 | line(b) << member_type << " fract = sign(" << in << ") * frexp(" << in << ", exp);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2053 | { |
| 2054 | auto l = line(b); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2055 | if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2056 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2057 | return false; |
| 2058 | } |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 2059 | l << " result = {fract, int" << width << "(exp)};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2060 | } |
| 2061 | line(b) << "return result;"; |
| 2062 | return true; |
| 2063 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2066 | bool GeneratorImpl::EmitDegreesCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2067 | const ast::CallExpression* expr, |
| 2068 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2069 | return CallBuiltinHelper(out, expr, builtin, |
| 2070 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2071 | line(b) << "return " << params[0] << " * " << std::setprecision(20) |
| 2072 | << sem::kRadToDeg << ";"; |
| 2073 | return true; |
| 2074 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2077 | bool GeneratorImpl::EmitRadiansCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2078 | const ast::CallExpression* expr, |
| 2079 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2080 | return CallBuiltinHelper(out, expr, builtin, |
| 2081 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2082 | line(b) << "return " << params[0] << " * " << std::setprecision(20) |
| 2083 | << sem::kDegToRad << ";"; |
| 2084 | return true; |
| 2085 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2088 | // The HLSL `sign` method always returns an `int` result (scalar or vector). In WGSL the result is |
| 2089 | // expected to be the same type as the argument. This injects a cast to the expected WGSL result |
| 2090 | // type after the call to `sign`. |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2091 | bool GeneratorImpl::EmitSignCall(utils::StringStream& out, |
| 2092 | const sem::Call* call, |
| 2093 | const sem::Builtin*) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2094 | auto* arg = call->Arguments()[0]; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2095 | if (!EmitType(out, arg->Type(), builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2096 | "")) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2097 | return false; |
| 2098 | } |
| 2099 | out << "(sign("; |
| 2100 | if (!EmitExpression(out, arg->Declaration())) { |
| 2101 | return false; |
| 2102 | } |
| 2103 | out << "))"; |
| 2104 | return true; |
| 2105 | } |
| 2106 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2107 | bool GeneratorImpl::EmitQuantizeToF16Call(utils::StringStream& out, |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2108 | const ast::CallExpression* expr, |
| 2109 | const sem::Builtin* builtin) { |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2110 | // Cast to f16 and back |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2111 | std::string width; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2112 | if (auto* vec = builtin->ReturnType()->As<type::Vector>()) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2113 | width = std::to_string(vec->Width()); |
| 2114 | } |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2115 | out << "f16tof32(f32tof16" |
| 2116 | << "("; |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2117 | if (!EmitExpression(out, expr->args[0])) { |
| 2118 | return false; |
| 2119 | } |
| 2120 | out << "))"; |
| 2121 | return true; |
| 2122 | } |
| 2123 | |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2124 | bool GeneratorImpl::EmitTruncCall(utils::StringStream& out, |
| 2125 | const ast::CallExpression* expr, |
| 2126 | const sem::Builtin* builtin) { |
| 2127 | // HLSL's trunc is broken for very large/small float values. |
| 2128 | // See crbug.com/tint/1883 |
| 2129 | return CallBuiltinHelper( // |
| 2130 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2131 | // value < 0 ? ceil(value) : floor(value) |
| 2132 | line(b) << "return " << params[0] << " < 0 ? ceil(" << params[0] << ") : floor(" |
| 2133 | << params[0] << ");"; |
| 2134 | return true; |
| 2135 | }); |
| 2136 | } |
| 2137 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2138 | bool GeneratorImpl::EmitDataPackingCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2139 | const ast::CallExpression* expr, |
| 2140 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2141 | return CallBuiltinHelper( |
| 2142 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2143 | uint32_t dims = 2; |
| 2144 | bool is_signed = false; |
| 2145 | uint32_t scale = 65535; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2146 | if (builtin->Type() == builtin::Function::kPack4X8Snorm || |
| 2147 | builtin->Type() == builtin::Function::kPack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2148 | dims = 4; |
| 2149 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2150 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2151 | if (builtin->Type() == builtin::Function::kPack4X8Snorm || |
| 2152 | builtin->Type() == builtin::Function::kPack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2153 | is_signed = true; |
| 2154 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2155 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2156 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2157 | case builtin::Function::kPack4X8Snorm: |
| 2158 | case builtin::Function::kPack4X8Unorm: |
| 2159 | case builtin::Function::kPack2X16Snorm: |
| 2160 | case builtin::Function::kPack2X16Unorm: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2161 | { |
| 2162 | auto l = line(b); |
| 2163 | l << (is_signed ? "" : "u") << "int" << dims |
| 2164 | << " i = " << (is_signed ? "" : "u") << "int" << dims << "(round(clamp(" |
| 2165 | << params[0] << ", " << (is_signed ? "-1.0" : "0.0") << ", 1.0) * " |
| 2166 | << scale << ".0))"; |
| 2167 | if (is_signed) { |
| 2168 | l << " & " << (dims == 4 ? "0xff" : "0xffff"); |
| 2169 | } |
| 2170 | l << ";"; |
| 2171 | } |
| 2172 | { |
| 2173 | auto l = line(b); |
| 2174 | l << "return "; |
| 2175 | if (is_signed) { |
| 2176 | l << "asuint"; |
| 2177 | } |
| 2178 | l << "(i.x | i.y << " << (32 / dims); |
| 2179 | if (dims == 4) { |
| 2180 | l << " | i.z << 16 | i.w << 24"; |
| 2181 | } |
| 2182 | l << ");"; |
| 2183 | } |
| 2184 | break; |
| 2185 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2186 | case builtin::Function::kPack2X16Float: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2187 | line(b) << "uint2 i = f32tof16(" << params[0] << ");"; |
| 2188 | line(b) << "return i.x | (i.y << 16);"; |
| 2189 | break; |
| 2190 | } |
| 2191 | default: |
| 2192 | diagnostics_.add_error(diag::System::Writer, |
| 2193 | "Internal error: unhandled data packing builtin"); |
| 2194 | return false; |
| 2195 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2196 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2197 | return true; |
| 2198 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2201 | bool GeneratorImpl::EmitDataUnpackingCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2202 | const ast::CallExpression* expr, |
| 2203 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2204 | return CallBuiltinHelper( |
| 2205 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2206 | uint32_t dims = 2; |
| 2207 | bool is_signed = false; |
| 2208 | uint32_t scale = 65535; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2209 | if (builtin->Type() == builtin::Function::kUnpack4X8Snorm || |
| 2210 | builtin->Type() == builtin::Function::kUnpack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2211 | dims = 4; |
| 2212 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2213 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2214 | if (builtin->Type() == builtin::Function::kUnpack4X8Snorm || |
| 2215 | builtin->Type() == builtin::Function::kUnpack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2216 | is_signed = true; |
| 2217 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2218 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2219 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2220 | case builtin::Function::kUnpack4X8Snorm: |
| 2221 | case builtin::Function::kUnpack2X16Snorm: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2222 | line(b) << "int j = int(" << params[0] << ");"; |
| 2223 | { // Perform sign extension on the converted values. |
| 2224 | auto l = line(b); |
| 2225 | l << "int" << dims << " i = int" << dims << "("; |
| 2226 | if (dims == 2) { |
| 2227 | l << "j << 16, j) >> 16"; |
| 2228 | } else { |
| 2229 | l << "j << 24, j << 16, j << 8, j) >> 24"; |
| 2230 | } |
| 2231 | l << ";"; |
| 2232 | } |
| 2233 | line(b) << "return clamp(float" << dims << "(i) / " << scale << ".0, " |
| 2234 | << (is_signed ? "-1.0" : "0.0") << ", 1.0);"; |
| 2235 | break; |
| 2236 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2237 | case builtin::Function::kUnpack4X8Unorm: |
| 2238 | case builtin::Function::kUnpack2X16Unorm: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2239 | line(b) << "uint j = " << params[0] << ";"; |
| 2240 | { |
| 2241 | auto l = line(b); |
| 2242 | l << "uint" << dims << " i = uint" << dims << "("; |
| 2243 | l << "j & " << (dims == 2 ? "0xffff" : "0xff") << ", "; |
| 2244 | if (dims == 4) { |
| 2245 | l << "(j >> " << (32 / dims) << ") & 0xff, (j >> 16) & 0xff, j >> 24"; |
| 2246 | } else { |
| 2247 | l << "j >> " << (32 / dims); |
| 2248 | } |
| 2249 | l << ");"; |
| 2250 | } |
| 2251 | line(b) << "return float" << dims << "(i) / " << scale << ".0;"; |
| 2252 | break; |
| 2253 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2254 | case builtin::Function::kUnpack2X16Float: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2255 | line(b) << "uint i = " << params[0] << ";"; |
| 2256 | line(b) << "return f16tof32(uint2(i & 0xffff, i >> 16));"; |
| 2257 | break; |
| 2258 | default: |
| 2259 | diagnostics_.add_error(diag::System::Writer, |
| 2260 | "Internal error: unhandled data packing builtin"); |
| 2261 | return false; |
| 2262 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2263 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2264 | return true; |
| 2265 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2268 | bool GeneratorImpl::EmitDP4aCall(utils::StringStream& out, |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2269 | const ast::CallExpression* expr, |
| 2270 | const sem::Builtin* builtin) { |
| 2271 | // TODO(crbug.com/tint/1497): support the polyfill version of DP4a functions. |
| 2272 | return CallBuiltinHelper( |
| 2273 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2274 | std::string functionName; |
| 2275 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2276 | case builtin::Function::kDot4I8Packed: |
Jiawei Shao | 1c75921 | 2022-05-15 13:53:21 +0000 | [diff] [blame] | 2277 | line(b) << "int accumulator = 0;"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2278 | functionName = "dot4add_i8packed"; |
| 2279 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2280 | case builtin::Function::kDot4U8Packed: |
Jiawei Shao | 1c75921 | 2022-05-15 13:53:21 +0000 | [diff] [blame] | 2281 | line(b) << "uint accumulator = 0u;"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2282 | functionName = "dot4add_u8packed"; |
| 2283 | break; |
| 2284 | default: |
| 2285 | diagnostics_.add_error(diag::System::Writer, |
| 2286 | "Internal error: unhandled DP4a builtin"); |
| 2287 | return false; |
| 2288 | } |
| 2289 | line(b) << "return " << functionName << "(" << params[0] << ", " << params[1] |
Jiawei Shao | 1c75921 | 2022-05-15 13:53:21 +0000 | [diff] [blame] | 2290 | << ", accumulator);"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2291 | |
| 2292 | return true; |
| 2293 | }); |
| 2294 | } |
| 2295 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2296 | bool GeneratorImpl::EmitBarrierCall(utils::StringStream& out, const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2297 | // TODO(crbug.com/tint/661): Combine sequential barriers to a single |
| 2298 | // instruction. |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2299 | if (builtin->Type() == builtin::Function::kWorkgroupBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2300 | out << "GroupMemoryBarrierWithGroupSync()"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2301 | } else if (builtin->Type() == builtin::Function::kStorageBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2302 | out << "DeviceMemoryBarrierWithGroupSync()"; |
| 2303 | } else { |
| 2304 | TINT_UNREACHABLE(Writer, diagnostics_) |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2305 | << "unexpected barrier builtin type " << builtin::str(builtin->Type()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2306 | return false; |
| 2307 | } |
| 2308 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2311 | bool GeneratorImpl::EmitTextureCall(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2312 | const sem::Call* call, |
| 2313 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2314 | using Usage = sem::ParameterUsage; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2315 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2316 | auto& signature = builtin->Signature(); |
| 2317 | auto* expr = call->Declaration(); |
| 2318 | auto arguments = expr->args; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2319 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2320 | // Returns the argument with the given usage |
| 2321 | auto arg = [&](Usage usage) { |
| 2322 | int idx = signature.IndexOf(usage); |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 2323 | return (idx >= 0) ? arguments[static_cast<size_t>(idx)] : nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2324 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2325 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2326 | auto* texture = arg(Usage::kTexture); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2327 | if (TINT_UNLIKELY(!texture)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2328 | TINT_ICE(Writer, diagnostics_) << "missing texture argument"; |
| 2329 | return false; |
| 2330 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2331 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 2332 | auto* texture_type = TypeOf(texture)->UnwrapRef()->As<type::Texture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2333 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2334 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2335 | case builtin::Function::kTextureDimensions: |
| 2336 | case builtin::Function::kTextureNumLayers: |
| 2337 | case builtin::Function::kTextureNumLevels: |
| 2338 | case builtin::Function::kTextureNumSamples: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2339 | // All of these builtins use the GetDimensions() method on the texture |
| 2340 | bool is_ms = |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 2341 | texture_type->IsAnyOf<type::MultisampledTexture, type::DepthMultisampledTexture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2342 | int num_dimensions = 0; |
| 2343 | std::string swizzle; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2344 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2345 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2346 | case builtin::Function::kTextureDimensions: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2347 | switch (texture_type->dim()) { |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2348 | case type::TextureDimension::kNone: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2349 | TINT_ICE(Writer, diagnostics_) << "texture dimension is kNone"; |
| 2350 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2351 | case type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2352 | num_dimensions = 1; |
| 2353 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2354 | case type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2355 | num_dimensions = is_ms ? 3 : 2; |
| 2356 | swizzle = is_ms ? ".xy" : ""; |
| 2357 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2358 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2359 | num_dimensions = is_ms ? 4 : 3; |
| 2360 | swizzle = ".xy"; |
| 2361 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2362 | case type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2363 | num_dimensions = 3; |
| 2364 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2365 | case type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2366 | num_dimensions = 2; |
| 2367 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2368 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2369 | num_dimensions = 3; |
| 2370 | swizzle = ".xy"; |
| 2371 | break; |
| 2372 | } |
| 2373 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2374 | case builtin::Function::kTextureNumLayers: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2375 | switch (texture_type->dim()) { |
| 2376 | default: |
| 2377 | TINT_ICE(Writer, diagnostics_) << "texture dimension is not arrayed"; |
| 2378 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2379 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2380 | num_dimensions = is_ms ? 4 : 3; |
| 2381 | swizzle = ".z"; |
| 2382 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2383 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2384 | num_dimensions = 3; |
| 2385 | swizzle = ".z"; |
| 2386 | break; |
| 2387 | } |
| 2388 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2389 | case builtin::Function::kTextureNumLevels: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2390 | switch (texture_type->dim()) { |
| 2391 | default: |
| 2392 | TINT_ICE(Writer, diagnostics_) |
| 2393 | << "texture dimension does not support mips"; |
| 2394 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2395 | case type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2396 | num_dimensions = 2; |
| 2397 | swizzle = ".y"; |
| 2398 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2399 | case type::TextureDimension::k2d: |
| 2400 | case type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2401 | num_dimensions = 3; |
| 2402 | swizzle = ".z"; |
| 2403 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2404 | case type::TextureDimension::k2dArray: |
| 2405 | case type::TextureDimension::k3d: |
| 2406 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2407 | num_dimensions = 4; |
| 2408 | swizzle = ".w"; |
| 2409 | break; |
| 2410 | } |
| 2411 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2412 | case builtin::Function::kTextureNumSamples: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2413 | switch (texture_type->dim()) { |
| 2414 | default: |
| 2415 | TINT_ICE(Writer, diagnostics_) |
| 2416 | << "texture dimension does not support multisampling"; |
| 2417 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2418 | case type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2419 | num_dimensions = 3; |
| 2420 | swizzle = ".z"; |
| 2421 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2422 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2423 | num_dimensions = 4; |
| 2424 | swizzle = ".w"; |
| 2425 | break; |
| 2426 | } |
| 2427 | break; |
| 2428 | default: |
| 2429 | TINT_ICE(Writer, diagnostics_) << "unexpected builtin"; |
| 2430 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2431 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2432 | |
| 2433 | auto* level_arg = arg(Usage::kLevel); |
| 2434 | |
| 2435 | if (level_arg) { |
| 2436 | // `NumberOfLevels` is a non-optional argument if `MipLevel` was passed. |
| 2437 | // Increment the number of dimensions for the temporary vector to |
| 2438 | // accommodate this. |
| 2439 | num_dimensions++; |
| 2440 | |
| 2441 | // If the swizzle was empty, the expression will evaluate to the whole |
| 2442 | // vector. As we've grown the vector by one element, we now need to |
| 2443 | // swizzle to keep the result expression equivalent. |
| 2444 | if (swizzle.empty()) { |
| 2445 | static constexpr const char* swizzles[] = {"", ".x", ".xy", ".xyz"}; |
| 2446 | swizzle = swizzles[num_dimensions - 1]; |
| 2447 | } |
| 2448 | } |
| 2449 | |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2450 | if (TINT_UNLIKELY(num_dimensions > 4)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2451 | TINT_ICE(Writer, diagnostics_) << "Texture query builtin temporary vector has " |
| 2452 | << num_dimensions << " dimensions"; |
| 2453 | return false; |
| 2454 | } |
| 2455 | |
| 2456 | // Declare a variable to hold the queried texture info |
| 2457 | auto dims = UniqueIdentifier(kTempNamePrefix); |
| 2458 | if (num_dimensions == 1) { |
Ben Clayton | 068eb3e | 2023-03-08 02:48:34 +0000 | [diff] [blame] | 2459 | line() << "uint " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2460 | } else { |
Ben Clayton | 068eb3e | 2023-03-08 02:48:34 +0000 | [diff] [blame] | 2461 | line() << "uint" << num_dimensions << " " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | { // texture.GetDimensions(...) |
| 2465 | auto pre = line(); |
| 2466 | if (!EmitExpression(pre, texture)) { |
| 2467 | return false; |
| 2468 | } |
| 2469 | pre << ".GetDimensions("; |
| 2470 | |
| 2471 | if (level_arg) { |
| 2472 | if (!EmitExpression(pre, level_arg)) { |
| 2473 | return false; |
| 2474 | } |
| 2475 | pre << ", "; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2476 | } else if (builtin->Type() == builtin::Function::kTextureNumLevels) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2477 | pre << "0, "; |
| 2478 | } |
| 2479 | |
| 2480 | if (num_dimensions == 1) { |
| 2481 | pre << dims; |
| 2482 | } else { |
| 2483 | static constexpr char xyzw[] = {'x', 'y', 'z', 'w'}; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2484 | if (TINT_UNLIKELY(num_dimensions < 0 || num_dimensions > 4)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2485 | TINT_ICE(Writer, diagnostics_) |
| 2486 | << "vector dimensions are " << num_dimensions; |
| 2487 | return false; |
| 2488 | } |
| 2489 | for (int i = 0; i < num_dimensions; i++) { |
| 2490 | if (i > 0) { |
| 2491 | pre << ", "; |
| 2492 | } |
| 2493 | pre << dims << "." << xyzw[i]; |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | pre << ");"; |
| 2498 | } |
| 2499 | |
| 2500 | // The out parameters of the GetDimensions() call is now in temporary |
| 2501 | // `dims` variable. This may be packed with other data, so the final |
| 2502 | // expression may require a swizzle. |
| 2503 | out << dims << swizzle; |
| 2504 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2505 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2506 | default: |
| 2507 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2508 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2509 | |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2510 | if (!EmitExpression(out, texture)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2511 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2512 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2513 | |
| 2514 | // If pack_level_in_coords is true, then the mip level will be appended as the |
| 2515 | // last value of the coordinates argument. If the WGSL builtin overload does |
| 2516 | // not have a level parameter and pack_level_in_coords is true, then a zero |
| 2517 | // mip level will be inserted. |
| 2518 | bool pack_level_in_coords = false; |
| 2519 | |
| 2520 | uint32_t hlsl_ret_width = 4u; |
| 2521 | |
| 2522 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2523 | case builtin::Function::kTextureSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2524 | out << ".Sample("; |
| 2525 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2526 | case builtin::Function::kTextureSampleBias: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2527 | out << ".SampleBias("; |
| 2528 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2529 | case builtin::Function::kTextureSampleLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2530 | out << ".SampleLevel("; |
| 2531 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2532 | case builtin::Function::kTextureSampleGrad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2533 | out << ".SampleGrad("; |
| 2534 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2535 | case builtin::Function::kTextureSampleCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2536 | out << ".SampleCmp("; |
| 2537 | hlsl_ret_width = 1; |
| 2538 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2539 | case builtin::Function::kTextureSampleCompareLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2540 | out << ".SampleCmpLevelZero("; |
| 2541 | hlsl_ret_width = 1; |
| 2542 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2543 | case builtin::Function::kTextureLoad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2544 | out << ".Load("; |
| 2545 | // Multisampled textures do not support mip-levels. |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 2546 | if (!texture_type->Is<type::MultisampledTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2547 | pack_level_in_coords = true; |
| 2548 | } |
| 2549 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2550 | case builtin::Function::kTextureGather: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2551 | out << ".Gather"; |
| 2552 | if (builtin->Parameters()[0]->Usage() == sem::ParameterUsage::kComponent) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 2553 | switch (call->Arguments()[0]->ConstantValue()->ValueAs<AInt>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2554 | case 0: |
| 2555 | out << "Red"; |
| 2556 | break; |
| 2557 | case 1: |
| 2558 | out << "Green"; |
| 2559 | break; |
| 2560 | case 2: |
| 2561 | out << "Blue"; |
| 2562 | break; |
| 2563 | case 3: |
| 2564 | out << "Alpha"; |
| 2565 | break; |
| 2566 | } |
| 2567 | } |
| 2568 | out << "("; |
| 2569 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2570 | case builtin::Function::kTextureGatherCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2571 | out << ".GatherCmp("; |
| 2572 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2573 | case builtin::Function::kTextureStore: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2574 | out << "["; |
| 2575 | break; |
| 2576 | default: |
| 2577 | diagnostics_.add_error(diag::System::Writer, |
| 2578 | "Internal compiler error: Unhandled texture builtin '" + |
| 2579 | std::string(builtin->str()) + "'"); |
| 2580 | return false; |
| 2581 | } |
| 2582 | |
| 2583 | if (auto* sampler = arg(Usage::kSampler)) { |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2584 | if (!EmitExpression(out, sampler)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2585 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2586 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2587 | out << ", "; |
| 2588 | } |
| 2589 | |
| 2590 | auto* param_coords = arg(Usage::kCoords); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2591 | if (TINT_UNLIKELY(!param_coords)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2592 | TINT_ICE(Writer, diagnostics_) << "missing coords argument"; |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
| 2596 | auto emit_vector_appended_with_i32_zero = [&](const ast::Expression* vector) { |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 2597 | auto* i32 = builder_.create<type::I32>(); |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 2598 | auto* zero = builder_.Expr(0_i); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2599 | auto* stmt = builder_.Sem().Get(vector)->Stmt(); |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 2600 | builder_.Sem().Add(zero, builder_.create<sem::ValueExpression>( |
| 2601 | zero, i32, sem::EvaluationStage::kRuntime, stmt, |
| 2602 | /* constant_value */ nullptr, |
| 2603 | /* has_side_effects */ false)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2604 | auto* packed = AppendVector(&builder_, vector, zero); |
| 2605 | return EmitExpression(out, packed->Declaration()); |
| 2606 | }; |
| 2607 | |
| 2608 | auto emit_vector_appended_with_level = [&](const ast::Expression* vector) { |
| 2609 | if (auto* level = arg(Usage::kLevel)) { |
| 2610 | auto* packed = AppendVector(&builder_, vector, level); |
| 2611 | return EmitExpression(out, packed->Declaration()); |
| 2612 | } |
| 2613 | return emit_vector_appended_with_i32_zero(vector); |
| 2614 | }; |
| 2615 | |
| 2616 | if (auto* array_index = arg(Usage::kArrayIndex)) { |
| 2617 | // Array index needs to be appended to the coordinates. |
| 2618 | auto* packed = AppendVector(&builder_, param_coords, array_index); |
| 2619 | if (pack_level_in_coords) { |
| 2620 | // Then mip level needs to be appended to the coordinates. |
| 2621 | if (!emit_vector_appended_with_level(packed->Declaration())) { |
| 2622 | return false; |
| 2623 | } |
| 2624 | } else { |
| 2625 | if (!EmitExpression(out, packed->Declaration())) { |
| 2626 | return false; |
| 2627 | } |
| 2628 | } |
| 2629 | } else if (pack_level_in_coords) { |
| 2630 | // Mip level needs to be appended to the coordinates. |
| 2631 | if (!emit_vector_appended_with_level(param_coords)) { |
| 2632 | return false; |
| 2633 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2634 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2635 | if (!EmitExpression(out, param_coords)) { |
| 2636 | return false; |
| 2637 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2638 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2639 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2640 | for (auto usage : {Usage::kDepthRef, Usage::kBias, Usage::kLevel, Usage::kDdx, Usage::kDdy, |
| 2641 | Usage::kSampleIndex, Usage::kOffset}) { |
| 2642 | if (usage == Usage::kLevel && pack_level_in_coords) { |
| 2643 | continue; // mip level already packed in coordinates. |
| 2644 | } |
| 2645 | if (auto* e = arg(usage)) { |
| 2646 | out << ", "; |
| 2647 | if (!EmitExpression(out, e)) { |
| 2648 | return false; |
| 2649 | } |
| 2650 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2651 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2652 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2653 | if (builtin->Type() == builtin::Function::kTextureStore) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2654 | out << "] = "; |
| 2655 | if (!EmitExpression(out, arg(Usage::kValue))) { |
| 2656 | return false; |
| 2657 | } |
| 2658 | } else { |
| 2659 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2660 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2661 | // If the builtin return type does not match the number of elements of the |
| 2662 | // HLSL builtin, we need to swizzle the expression to generate the correct |
| 2663 | // number of components. |
| 2664 | uint32_t wgsl_ret_width = 1; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2665 | if (auto* vec = builtin->ReturnType()->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2666 | wgsl_ret_width = vec->Width(); |
| 2667 | } |
| 2668 | if (wgsl_ret_width < hlsl_ret_width) { |
| 2669 | out << "."; |
| 2670 | for (uint32_t i = 0; i < wgsl_ret_width; i++) { |
| 2671 | out << "xyz"[i]; |
| 2672 | } |
| 2673 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2674 | if (TINT_UNLIKELY(wgsl_ret_width > hlsl_ret_width)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2675 | TINT_ICE(Writer, diagnostics_) |
| 2676 | << "WGSL return width (" << wgsl_ret_width << ") is wider than HLSL return width (" |
| 2677 | << hlsl_ret_width << ") for " << builtin->Type(); |
| 2678 | return false; |
| 2679 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2680 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2681 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2682 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | std::string GeneratorImpl::generate_builtin_name(const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2686 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2687 | case builtin::Function::kAbs: |
| 2688 | case builtin::Function::kAcos: |
| 2689 | case builtin::Function::kAll: |
| 2690 | case builtin::Function::kAny: |
| 2691 | case builtin::Function::kAsin: |
| 2692 | case builtin::Function::kAtan: |
| 2693 | case builtin::Function::kAtan2: |
| 2694 | case builtin::Function::kCeil: |
| 2695 | case builtin::Function::kClamp: |
| 2696 | case builtin::Function::kCos: |
| 2697 | case builtin::Function::kCosh: |
| 2698 | case builtin::Function::kCross: |
| 2699 | case builtin::Function::kDeterminant: |
| 2700 | case builtin::Function::kDistance: |
| 2701 | case builtin::Function::kDot: |
| 2702 | case builtin::Function::kExp: |
| 2703 | case builtin::Function::kExp2: |
| 2704 | case builtin::Function::kFloor: |
| 2705 | case builtin::Function::kFrexp: |
| 2706 | case builtin::Function::kLdexp: |
| 2707 | case builtin::Function::kLength: |
| 2708 | case builtin::Function::kLog: |
| 2709 | case builtin::Function::kLog2: |
| 2710 | case builtin::Function::kMax: |
| 2711 | case builtin::Function::kMin: |
| 2712 | case builtin::Function::kModf: |
| 2713 | case builtin::Function::kNormalize: |
| 2714 | case builtin::Function::kPow: |
| 2715 | case builtin::Function::kReflect: |
| 2716 | case builtin::Function::kRefract: |
| 2717 | case builtin::Function::kRound: |
| 2718 | case builtin::Function::kSaturate: |
| 2719 | case builtin::Function::kSin: |
| 2720 | case builtin::Function::kSinh: |
| 2721 | case builtin::Function::kSqrt: |
| 2722 | case builtin::Function::kStep: |
| 2723 | case builtin::Function::kTan: |
| 2724 | case builtin::Function::kTanh: |
| 2725 | case builtin::Function::kTranspose: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2726 | return builtin->str(); |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2727 | case builtin::Function::kCountOneBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2728 | return "countbits"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2729 | case builtin::Function::kDpdx: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2730 | return "ddx"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2731 | case builtin::Function::kDpdxCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2732 | return "ddx_coarse"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2733 | case builtin::Function::kDpdxFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2734 | return "ddx_fine"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2735 | case builtin::Function::kDpdy: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2736 | return "ddy"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2737 | case builtin::Function::kDpdyCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2738 | return "ddy_coarse"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2739 | case builtin::Function::kDpdyFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2740 | return "ddy_fine"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2741 | case builtin::Function::kFaceForward: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2742 | return "faceforward"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2743 | case builtin::Function::kFract: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2744 | return "frac"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2745 | case builtin::Function::kFma: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2746 | return "mad"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2747 | case builtin::Function::kFwidth: |
| 2748 | case builtin::Function::kFwidthCoarse: |
| 2749 | case builtin::Function::kFwidthFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2750 | return "fwidth"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2751 | case builtin::Function::kInverseSqrt: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2752 | return "rsqrt"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2753 | case builtin::Function::kMix: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2754 | return "lerp"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2755 | case builtin::Function::kReverseBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2756 | return "reversebits"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2757 | case builtin::Function::kSmoothstep: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2758 | return "smoothstep"; |
| 2759 | default: |
| 2760 | diagnostics_.add_error(diag::System::Writer, |
| 2761 | "Unknown builtin method: " + std::string(builtin->str())); |
| 2762 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2763 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2764 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | bool GeneratorImpl::EmitCase(const ast::SwitchStatement* s, size_t case_idx) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2768 | auto* stmt = s->body[case_idx]; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 2769 | auto* sem = builder_.Sem().Get<sem::CaseStatement>(stmt); |
| 2770 | for (auto* selector : sem->Selectors()) { |
| 2771 | auto out = line(); |
| 2772 | if (selector->IsDefault()) { |
| 2773 | out << "default"; |
| 2774 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2775 | out << "case "; |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 2776 | if (!EmitConstant(out, selector->Value(), /* is_variable_initializer */ false)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2777 | return false; |
| 2778 | } |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 2779 | } |
| 2780 | out << ":"; |
| 2781 | if (selector == sem->Selectors().back()) { |
| 2782 | out << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | increment_indent(); |
| 2787 | TINT_DEFER({ |
| 2788 | decrement_indent(); |
| 2789 | line() << "}"; |
| 2790 | }); |
| 2791 | |
| 2792 | // Emit the case statement |
| 2793 | if (!EmitStatements(stmt->body->statements)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2794 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2795 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2796 | |
dan sinclair | 267f174 | 2022-11-15 00:30:33 +0000 | [diff] [blame] | 2797 | if (!tint::IsAnyOf<ast::BreakStatement>(stmt->body->Last())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2798 | line() << "break;"; |
| 2799 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2800 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2801 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | bool GeneratorImpl::EmitContinue(const ast::ContinueStatement*) { |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 2805 | if (!emit_continuing_ || !emit_continuing_()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2806 | return false; |
| 2807 | } |
| 2808 | line() << "continue;"; |
| 2809 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | bool GeneratorImpl::EmitDiscard(const ast::DiscardStatement*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2813 | // TODO(dsinclair): Verify this is correct when the discard semantics are |
| 2814 | // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) |
| 2815 | line() << "discard;"; |
| 2816 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2819 | bool GeneratorImpl::EmitExpression(utils::StringStream& out, const ast::Expression* expr) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 2820 | if (auto* sem = builder_.Sem().GetVal(expr)) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 2821 | if (auto* constant = sem->ConstantValue()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 2822 | bool is_variable_initializer = false; |
| 2823 | if (auto* stmt = sem->Stmt()) { |
| 2824 | if (auto* decl = As<ast::VariableDeclStatement>(stmt->Declaration())) { |
| 2825 | is_variable_initializer = decl->variable->initializer == expr; |
| 2826 | } |
| 2827 | } |
| 2828 | return EmitConstant(out, constant, is_variable_initializer); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 2829 | } |
| 2830 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2831 | return Switch( |
Ben Clayton | b90b6bf | 2022-08-23 16:23:05 +0000 | [diff] [blame] | 2832 | expr, // |
| 2833 | [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, |
| 2834 | [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, |
| 2835 | [&](const ast::BitcastExpression* b) { return EmitBitcast(out, b); }, |
| 2836 | [&](const ast::CallExpression* c) { return EmitCall(out, c); }, |
| 2837 | [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, |
| 2838 | [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, |
| 2839 | [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, |
| 2840 | [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, |
| 2841 | [&](Default) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2842 | diagnostics_.add_error(diag::System::Writer, "unknown expression type: " + |
| 2843 | std::string(expr->TypeInfo().name)); |
| 2844 | return false; |
| 2845 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 2848 | bool GeneratorImpl::EmitIdentifier(utils::StringStream& out, |
| 2849 | const ast::IdentifierExpression* expr) { |
Ben Clayton | 6cba18b | 2023-02-02 20:37:19 +0000 | [diff] [blame] | 2850 | out << builder_.Symbols().NameFor(expr->identifier->symbol); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2851 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | bool GeneratorImpl::EmitIf(const ast::IfStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2855 | { |
| 2856 | auto out = line(); |
| 2857 | out << "if ("; |
| 2858 | if (!EmitExpression(out, stmt->condition)) { |
| 2859 | return false; |
| 2860 | } |
| 2861 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2862 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2863 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2864 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
James Price | 26ebe5e | 2022-04-29 00:14:53 +0000 | [diff] [blame] | 2865 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2866 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2867 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2868 | if (stmt->else_statement) { |
| 2869 | line() << "} else {"; |
| 2870 | if (auto* block = stmt->else_statement->As<ast::BlockStatement>()) { |
| 2871 | if (!EmitStatementsWithIndent(block->statements)) { |
| 2872 | return false; |
| 2873 | } |
| 2874 | } else { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 2875 | if (!EmitStatementsWithIndent(utils::Vector{stmt->else_statement})) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2876 | return false; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | line() << "}"; |
| 2881 | |
| 2882 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | bool GeneratorImpl::EmitFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2886 | auto* sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2887 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2888 | // Emit storage atomic helpers |
| 2889 | if (auto* intrinsic = |
| 2890 | ast::GetAttribute<transform::DecomposeMemoryAccess::Intrinsic>(func->attributes)) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2891 | if (intrinsic->address_space == builtin::AddressSpace::kStorage && intrinsic->IsAtomic()) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2892 | if (!EmitStorageAtomicIntrinsic(func, intrinsic)) { |
| 2893 | return false; |
| 2894 | } |
| 2895 | } |
| 2896 | return true; |
| 2897 | } |
| 2898 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2899 | if (ast::HasAttribute<ast::InternalAttribute>(func->attributes)) { |
| 2900 | // An internal function. Do not emit. |
| 2901 | return true; |
| 2902 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2903 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2904 | { |
| 2905 | auto out = line(); |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 2906 | auto name = builder_.Symbols().NameFor(func->name->symbol); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2907 | // If the function returns an array, then we need to declare a typedef for |
| 2908 | // this. |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2909 | if (sem->ReturnType()->Is<type::Array>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2910 | auto typedef_name = UniqueIdentifier(name + "_ret"); |
| 2911 | auto pre = line(); |
| 2912 | pre << "typedef "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2913 | if (!EmitTypeAndName(pre, sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2914 | builtin::Access::kReadWrite, typedef_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2915 | return false; |
| 2916 | } |
| 2917 | pre << ";"; |
| 2918 | out << typedef_name; |
| 2919 | } else { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2920 | if (!EmitType(out, sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2921 | builtin::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2922 | return false; |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | out << " " << name << "("; |
| 2927 | |
| 2928 | bool first = true; |
| 2929 | |
| 2930 | for (auto* v : sem->Parameters()) { |
| 2931 | if (!first) { |
| 2932 | out << ", "; |
| 2933 | } |
| 2934 | first = false; |
| 2935 | |
| 2936 | auto const* type = v->Type(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2937 | auto address_space = builtin::AddressSpace::kUndefined; |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2938 | auto access = builtin::Access::kUndefined; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2939 | |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 2940 | if (auto* ptr = type->As<type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2941 | type = ptr->StoreType(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 2942 | switch (ptr->AddressSpace()) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2943 | case builtin::AddressSpace::kStorage: |
| 2944 | case builtin::AddressSpace::kUniform: |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 2945 | // Not allowed by WGSL, but is used by certain transforms (e.g. DMA) to pass |
| 2946 | // storage buffers and uniform buffers down into transform-generated |
| 2947 | // functions. In this situation we want to generate the parameter without an |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 2948 | // 'inout', using the address space and access from the pointer. |
| 2949 | address_space = ptr->AddressSpace(); |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 2950 | access = ptr->Access(); |
| 2951 | break; |
| 2952 | default: |
| 2953 | // Transform regular WGSL pointer parameters in to `inout` parameters. |
| 2954 | out << "inout "; |
| 2955 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 2958 | // Note: WGSL only allows for AddressSpace::kUndefined on parameters, however |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2959 | // the sanitizer transforms generates load / store functions for storage |
| 2960 | // or uniform buffers. These functions have a buffer parameter with |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 2961 | // AddressSpace::kStorage or AddressSpace::kUniform. This is required to |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2962 | // correctly translate the parameter to a [RW]ByteAddressBuffer for |
| 2963 | // storage buffers and a uint4[N] for uniform buffers. |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 2964 | if (!EmitTypeAndName(out, type, address_space, access, |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 2965 | builder_.Symbols().NameFor(v->Declaration()->name->symbol))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2966 | return false; |
| 2967 | } |
| 2968 | } |
| 2969 | out << ") {"; |
| 2970 | } |
| 2971 | |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 2972 | if (sem->DiscardStatement() && !sem->ReturnType()->Is<type::Void>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2973 | // BUG(crbug.com/tint/1081): work around non-void functions with discard |
| 2974 | // failing compilation sometimes |
| 2975 | if (!EmitFunctionBodyWithDiscard(func)) { |
| 2976 | return false; |
| 2977 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2978 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2979 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 2980 | return false; |
| 2981 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2984 | line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2985 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2986 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | bool GeneratorImpl::EmitFunctionBodyWithDiscard(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2990 | // FXC sometimes fails to compile functions that discard with 'Not all control |
| 2991 | // paths return a value'. We work around this by wrapping the function body |
| 2992 | // within an "if (true) { <body> } return <default return type obj>;" so that |
| 2993 | // there is always an (unused) return statement. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2994 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2995 | auto* sem = builder_.Sem().Get(func); |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 2996 | TINT_ASSERT(Writer, sem->DiscardStatement() && !sem->ReturnType()->Is<type::Void>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2997 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2998 | ScopedIndent si(this); |
| 2999 | line() << "if (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3000 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3001 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3002 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3003 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3004 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3005 | line() << "}"; |
| 3006 | |
| 3007 | // Return an unused result that matches the type of the return value |
| 3008 | auto name = builder_.Symbols().NameFor(builder_.Symbols().New("unused")); |
| 3009 | { |
| 3010 | auto out = line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3011 | if (!EmitTypeAndName(out, sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3012 | builtin::Access::kReadWrite, name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3013 | return false; |
| 3014 | } |
| 3015 | out << ";"; |
| 3016 | } |
| 3017 | line() << "return " << name << ";"; |
| 3018 | |
| 3019 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | bool GeneratorImpl::EmitGlobalVariable(const ast::Variable* global) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3023 | return Switch( |
| 3024 | global, // |
| 3025 | [&](const ast::Var* var) { |
| 3026 | auto* sem = builder_.Sem().Get(global); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3027 | switch (sem->AddressSpace()) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3028 | case builtin::AddressSpace::kUniform: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3029 | return EmitUniformVariable(var, sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3030 | case builtin::AddressSpace::kStorage: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3031 | return EmitStorageVariable(var, sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3032 | case builtin::AddressSpace::kHandle: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3033 | return EmitHandleVariable(var, sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3034 | case builtin::AddressSpace::kPrivate: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3035 | return EmitPrivateVariable(sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3036 | case builtin::AddressSpace::kWorkgroup: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3037 | return EmitWorkgroupVariable(sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3038 | case builtin::AddressSpace::kPushConstant: |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3039 | diagnostics_.add_error( |
| 3040 | diag::System::Writer, |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3041 | "unhandled address space " + utils::ToString(sem->AddressSpace())); |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3042 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3043 | default: { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3044 | TINT_ICE(Writer, diagnostics_) |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3045 | << "unhandled address space " << sem->AddressSpace(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3046 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3047 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3048 | } |
| 3049 | }, |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3050 | [&](const ast::Override*) { |
| 3051 | // Override is removed with SubstituteOverride |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3052 | diagnostics_.add_error(diag::System::Writer, |
Ben Clayton | f10a579 | 2022-10-13 13:47:39 +0000 | [diff] [blame] | 3053 | "override-expressions should have been removed with the " |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3054 | "SubstituteOverride transform"); |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3055 | return false; |
| 3056 | }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3057 | [&](const ast::Const*) { |
| 3058 | return true; // Constants are embedded at their use |
| 3059 | }, |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3060 | [&](Default) { |
| 3061 | TINT_ICE(Writer, diagnostics_) |
| 3062 | << "unhandled global variable type " << global->TypeInfo().name; |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3063 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3064 | return false; |
| 3065 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3068 | bool GeneratorImpl::EmitUniformVariable(const ast::Var* var, const sem::Variable* sem) { |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3069 | auto binding_point = sem->As<sem::GlobalVariable>()->BindingPoint(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3070 | auto* type = sem->Type()->UnwrapRef(); |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3071 | auto name = builder_.Symbols().NameFor(var->name->symbol); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3072 | line() << "cbuffer cbuffer_" << name << RegisterAndSpace('b', binding_point) << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3073 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3074 | { |
| 3075 | ScopedIndent si(this); |
| 3076 | auto out = line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3077 | if (!EmitTypeAndName(out, type, builtin::AddressSpace::kUniform, sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3078 | return false; |
| 3079 | } |
| 3080 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3081 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3082 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3083 | line() << "};"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3084 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3085 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3088 | bool GeneratorImpl::EmitStorageVariable(const ast::Var* var, const sem::Variable* sem) { |
| 3089 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3090 | auto out = line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3091 | if (!EmitTypeAndName(out, type, builtin::AddressSpace::kStorage, sem->Access(), |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3092 | builder_.Symbols().NameFor(var->name->symbol))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3093 | return false; |
| 3094 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3095 | |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3096 | auto* global_sem = sem->As<sem::GlobalVariable>(); |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3097 | out << RegisterAndSpace(sem->Access() == builtin::Access::kRead ? 't' : 'u', |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3098 | global_sem->BindingPoint()) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3099 | << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3100 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3101 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3104 | bool GeneratorImpl::EmitHandleVariable(const ast::Var* var, const sem::Variable* sem) { |
| 3105 | auto* unwrapped_type = sem->Type()->UnwrapRef(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3106 | auto out = line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3107 | |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3108 | auto name = builder_.Symbols().NameFor(var->name->symbol); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3109 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3110 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3111 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3112 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3113 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3114 | const char* register_space = nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3115 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 3116 | if (unwrapped_type->Is<type::Texture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3117 | register_space = "t"; |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 3118 | if (unwrapped_type->Is<type::StorageTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3119 | register_space = "u"; |
| 3120 | } |
dan sinclair | 5ee58b6 | 2022-12-08 15:25:18 +0000 | [diff] [blame] | 3121 | } else if (unwrapped_type->Is<type::Sampler>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3122 | register_space = "s"; |
| 3123 | } |
| 3124 | |
| 3125 | if (register_space) { |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3126 | auto bp = sem->As<sem::GlobalVariable>()->BindingPoint(); |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3127 | out << " : register(" << register_space << bp.binding; |
| 3128 | // Omit the space if it's 0, as it's the default. |
| 3129 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better |
| 3130 | // compatibility. |
| 3131 | if (bp.group == 0) { |
| 3132 | out << ")"; |
| 3133 | } else { |
| 3134 | out << ", space" << bp.group << ")"; |
| 3135 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | out << ";"; |
| 3139 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | bool GeneratorImpl::EmitPrivateVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3143 | auto* decl = var->Declaration(); |
| 3144 | auto out = line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3145 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3146 | out << "static "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3147 | |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3148 | auto name = builder_.Symbols().NameFor(decl->name->symbol); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3149 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3150 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3151 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3152 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3153 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3154 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3155 | if (auto* initializer = decl->initializer) { |
| 3156 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3157 | return false; |
| 3158 | } |
| 3159 | } else { |
| 3160 | if (!EmitZeroValue(out, var->Type()->UnwrapRef())) { |
| 3161 | return false; |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | out << ";"; |
| 3166 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | bool GeneratorImpl::EmitWorkgroupVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3170 | auto* decl = var->Declaration(); |
| 3171 | auto out = line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3172 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3173 | out << "groupshared "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3174 | |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3175 | auto name = builder_.Symbols().NameFor(decl->name->symbol); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3176 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3177 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3178 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3179 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3180 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3181 | if (auto* initializer = decl->initializer) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3182 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3183 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3184 | return false; |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | out << ";"; |
| 3189 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3192 | std::string GeneratorImpl::builtin_to_attribute(builtin::BuiltinValue builtin) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3193 | switch (builtin) { |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3194 | case builtin::BuiltinValue::kPosition: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3195 | return "SV_Position"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3196 | case builtin::BuiltinValue::kVertexIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3197 | return "SV_VertexID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3198 | case builtin::BuiltinValue::kInstanceIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3199 | return "SV_InstanceID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3200 | case builtin::BuiltinValue::kFrontFacing: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3201 | return "SV_IsFrontFace"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3202 | case builtin::BuiltinValue::kFragDepth: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3203 | return "SV_Depth"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3204 | case builtin::BuiltinValue::kLocalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3205 | return "SV_GroupThreadID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3206 | case builtin::BuiltinValue::kLocalInvocationIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3207 | return "SV_GroupIndex"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3208 | case builtin::BuiltinValue::kGlobalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3209 | return "SV_DispatchThreadID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3210 | case builtin::BuiltinValue::kWorkgroupId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3211 | return "SV_GroupID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3212 | case builtin::BuiltinValue::kSampleIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3213 | return "SV_SampleIndex"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3214 | case builtin::BuiltinValue::kSampleMask: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3215 | return "SV_Coverage"; |
| 3216 | default: |
| 3217 | break; |
| 3218 | } |
| 3219 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3222 | std::string GeneratorImpl::interpolation_to_modifiers( |
| 3223 | builtin::InterpolationType type, |
| 3224 | builtin::InterpolationSampling sampling) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3225 | std::string modifiers; |
| 3226 | switch (type) { |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3227 | case builtin::InterpolationType::kPerspective: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3228 | modifiers += "linear "; |
| 3229 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3230 | case builtin::InterpolationType::kLinear: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3231 | modifiers += "noperspective "; |
| 3232 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3233 | case builtin::InterpolationType::kFlat: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3234 | modifiers += "nointerpolation "; |
| 3235 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3236 | case builtin::InterpolationType::kUndefined: |
Ben Clayton | f9ed9d3 | 2022-10-11 19:49:17 +0000 | [diff] [blame] | 3237 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3238 | } |
| 3239 | switch (sampling) { |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3240 | case builtin::InterpolationSampling::kCentroid: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3241 | modifiers += "centroid "; |
| 3242 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3243 | case builtin::InterpolationSampling::kSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3244 | modifiers += "sample "; |
| 3245 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3246 | case builtin::InterpolationSampling::kCenter: |
| 3247 | case builtin::InterpolationSampling::kUndefined: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3248 | break; |
| 3249 | } |
| 3250 | return modifiers; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3254 | auto* func_sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3255 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3256 | { |
| 3257 | auto out = line(); |
| 3258 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 3259 | // Emit the workgroup_size attribute. |
| 3260 | auto wgsize = func_sem->WorkgroupSize(); |
| 3261 | out << "[numthreads("; |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 3262 | for (size_t i = 0; i < 3; i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3263 | if (i > 0) { |
| 3264 | out << ", "; |
| 3265 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3266 | if (!wgsize[i].has_value()) { |
| 3267 | diagnostics_.add_error( |
| 3268 | diag::System::Writer, |
Ben Clayton | f10a579 | 2022-10-13 13:47:39 +0000 | [diff] [blame] | 3269 | "override-expressions should have been removed with the SubstituteOverride " |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3270 | "transform"); |
| 3271 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3272 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3273 | out << std::to_string(wgsize[i].value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3274 | } |
| 3275 | out << ")]" << std::endl; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3278 | if (!EmitTypeAndName(out, func_sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3279 | builtin::Access::kUndefined, |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3280 | builder_.Symbols().NameFor(func->name->symbol))) { |
Ben Clayton | 1906857 | 2023-02-07 21:28:09 +0000 | [diff] [blame] | 3281 | return false; |
| 3282 | } |
| 3283 | out << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3284 | |
| 3285 | bool first = true; |
| 3286 | |
| 3287 | // Emit entry point parameters. |
| 3288 | for (auto* var : func->params) { |
| 3289 | auto* sem = builder_.Sem().Get(var); |
| 3290 | auto* type = sem->Type(); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 3291 | if (TINT_UNLIKELY(!type->Is<sem::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3292 | // ICE likely indicates that the CanonicalizeEntryPointIO transform was |
| 3293 | // not run, or a builtin parameter was added after it was run. |
| 3294 | TINT_ICE(Writer, diagnostics_) << "Unsupported non-struct entry point parameter"; |
| 3295 | } |
| 3296 | |
| 3297 | if (!first) { |
| 3298 | out << ", "; |
| 3299 | } |
| 3300 | first = false; |
| 3301 | |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3302 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 3303 | builder_.Symbols().NameFor(var->name->symbol))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3304 | return false; |
| 3305 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3306 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3307 | |
| 3308 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3311 | { |
| 3312 | ScopedIndent si(this); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3313 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3314 | if (!EmitStatements(func->body->statements)) { |
| 3315 | return false; |
| 3316 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3317 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3318 | if (!Is<ast::ReturnStatement>(func->body->Last())) { |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 3319 | ast::ReturnStatement ret(ProgramID(), ast::NodeID{}, Source{}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3320 | if (!EmitStatement(&ret)) { |
| 3321 | return false; |
| 3322 | } |
| 3323 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3326 | line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3327 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3328 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3331 | bool GeneratorImpl::EmitConstant(utils::StringStream& out, |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 3332 | const constant::Value* constant, |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3333 | bool is_variable_initializer) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3334 | return Switch( |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3335 | constant->Type(), // |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3336 | [&](const type::Bool*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3337 | out << (constant->ValueAs<AInt>() ? "true" : "false"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3338 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3339 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3340 | [&](const type::F32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3341 | PrintF32(out, constant->ValueAs<f32>()); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3342 | return true; |
| 3343 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3344 | [&](const type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3345 | // emit a f16 scalar with explicit float16_t type declaration. |
| 3346 | out << "float16_t("; |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3347 | PrintF16(out, constant->ValueAs<f16>()); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3348 | out << ")"; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3349 | return true; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3350 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3351 | [&](const type::I32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3352 | out << constant->ValueAs<AInt>(); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3353 | return true; |
| 3354 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3355 | [&](const type::U32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3356 | out << constant->ValueAs<AInt>() << "u"; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3357 | return true; |
| 3358 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3359 | [&](const type::Vector* v) { |
James Price | 7bca4d7 | 2023-03-13 19:05:16 +0000 | [diff] [blame] | 3360 | if (auto* splat = constant->As<constant::Splat>()) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3361 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3362 | ScopedParen sp(out); |
James Price | 7bca4d7 | 2023-03-13 19:05:16 +0000 | [diff] [blame] | 3363 | if (!EmitConstant(out, splat->el, is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3364 | return false; |
| 3365 | } |
| 3366 | } |
| 3367 | out << "."; |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3368 | for (size_t i = 0; i < v->Width(); i++) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3369 | out << "x"; |
| 3370 | } |
| 3371 | return true; |
| 3372 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3373 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3374 | if (!EmitType(out, v, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3375 | "")) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3376 | return false; |
| 3377 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3378 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3379 | ScopedParen sp(out); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3380 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3381 | for (size_t i = 0; i < v->Width(); i++) { |
| 3382 | if (i > 0) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3383 | out << ", "; |
| 3384 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3385 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3386 | return false; |
| 3387 | } |
| 3388 | } |
| 3389 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3390 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3391 | [&](const type::Matrix* m) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3392 | if (!EmitType(out, m, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3393 | "")) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3394 | return false; |
| 3395 | } |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3396 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3397 | ScopedParen sp(out); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3398 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3399 | for (size_t i = 0; i < m->columns(); i++) { |
| 3400 | if (i > 0) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3401 | out << ", "; |
| 3402 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3403 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3404 | return false; |
| 3405 | } |
| 3406 | } |
| 3407 | return true; |
| 3408 | }, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3409 | [&](const type::Array* a) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3410 | if (constant->AllZero()) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3411 | out << "("; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3412 | if (!EmitType(out, a, builtin::AddressSpace::kUndefined, |
| 3413 | builtin::Access::kUndefined, "")) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3414 | return false; |
| 3415 | } |
| 3416 | out << ")0"; |
| 3417 | return true; |
| 3418 | } |
| 3419 | |
| 3420 | out << "{"; |
| 3421 | TINT_DEFER(out << "}"); |
| 3422 | |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3423 | auto count = a->ConstantCount(); |
| 3424 | if (!count) { |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3425 | diagnostics_.add_error(diag::System::Writer, |
| 3426 | type::Array::kErrExpectedConstantCount); |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3427 | return false; |
| 3428 | } |
| 3429 | |
| 3430 | for (size_t i = 0; i < count; i++) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3431 | if (i > 0) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3432 | out << ", "; |
| 3433 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3434 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3435 | return false; |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | return true; |
| 3440 | }, |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3441 | [&](const sem::Struct* s) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3442 | if (!EmitStructType(&helpers_, s)) { |
| 3443 | return false; |
| 3444 | } |
| 3445 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3446 | if (constant->AllZero()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3447 | out << "(" << StructName(s) << ")0"; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3448 | return true; |
| 3449 | } |
| 3450 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3451 | auto emit_member_values = [&](utils::StringStream& o) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3452 | o << "{"; |
dan sinclair | ad9cd0a | 2022-12-06 20:01:54 +0000 | [diff] [blame] | 3453 | for (size_t i = 0; i < s->Members().Length(); i++) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3454 | if (i > 0) { |
| 3455 | o << ", "; |
| 3456 | } |
| 3457 | if (!EmitConstant(o, constant->Index(i), is_variable_initializer)) { |
| 3458 | return false; |
| 3459 | } |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3460 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3461 | o << "}"; |
| 3462 | return true; |
| 3463 | }; |
| 3464 | |
| 3465 | if (is_variable_initializer) { |
| 3466 | if (!emit_member_values(out)) { |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3467 | return false; |
| 3468 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3469 | } else { |
| 3470 | // HLSL requires structure initializers to be assigned directly to a variable. |
| 3471 | auto name = UniqueIdentifier("c"); |
| 3472 | { |
| 3473 | auto decl = line(); |
| 3474 | decl << "const " << StructName(s) << " " << name << " = "; |
| 3475 | if (!emit_member_values(decl)) { |
| 3476 | return false; |
| 3477 | } |
| 3478 | decl << ";"; |
| 3479 | } |
| 3480 | out << name; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | return true; |
| 3484 | }, |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3485 | [&](Default) { |
| 3486 | diagnostics_.add_error( |
| 3487 | diag::System::Writer, |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3488 | "unhandled constant type: " + builder_.FriendlyName(constant->Type())); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3489 | return false; |
| 3490 | }); |
| 3491 | } |
| 3492 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3493 | bool GeneratorImpl::EmitLiteral(utils::StringStream& out, const ast::LiteralExpression* lit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3494 | return Switch( |
| 3495 | lit, |
| 3496 | [&](const ast::BoolLiteralExpression* l) { |
| 3497 | out << (l->value ? "true" : "false"); |
| 3498 | return true; |
| 3499 | }, |
Ben Clayton | 3ad927c | 2022-05-25 23:12:14 +0000 | [diff] [blame] | 3500 | [&](const ast::FloatLiteralExpression* l) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3501 | if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) { |
| 3502 | // Emit f16 literal with explicit float16_t type declaration. |
| 3503 | out << "float16_t("; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3504 | PrintF16(out, static_cast<float>(l->value)); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3505 | out << ")"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3506 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3507 | PrintF32(out, static_cast<float>(l->value)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3508 | return true; |
| 3509 | }, |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 3510 | [&](const ast::IntLiteralExpression* i) { |
| 3511 | out << i->value; |
| 3512 | switch (i->suffix) { |
| 3513 | case ast::IntLiteralExpression::Suffix::kNone: |
| 3514 | case ast::IntLiteralExpression::Suffix::kI: |
| 3515 | return true; |
| 3516 | case ast::IntLiteralExpression::Suffix::kU: |
| 3517 | out << "u"; |
| 3518 | return true; |
| 3519 | } |
| 3520 | diagnostics_.add_error(diag::System::Writer, "unknown integer literal suffix type"); |
| 3521 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3522 | }, |
| 3523 | [&](Default) { |
| 3524 | diagnostics_.add_error(diag::System::Writer, "unknown literal type"); |
| 3525 | return false; |
| 3526 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3529 | bool GeneratorImpl::EmitValue(utils::StringStream& out, const type::Type* type, int value) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3530 | return Switch( |
| 3531 | type, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3532 | [&](const type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3533 | out << (value == 0 ? "false" : "true"); |
| 3534 | return true; |
| 3535 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3536 | [&](const type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3537 | out << value << ".0f"; |
| 3538 | return true; |
| 3539 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3540 | [&](const type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3541 | out << "float16_t(" << value << ".0h)"; |
| 3542 | return true; |
| 3543 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3544 | [&](const type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3545 | out << value; |
| 3546 | return true; |
| 3547 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3548 | [&](const type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3549 | out << value << "u"; |
| 3550 | return true; |
| 3551 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3552 | [&](const type::Vector* vec) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3553 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3554 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3555 | return false; |
| 3556 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3557 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3558 | for (uint32_t i = 0; i < vec->Width(); i++) { |
| 3559 | if (i != 0) { |
| 3560 | out << ", "; |
| 3561 | } |
| 3562 | if (!EmitValue(out, vec->type(), value)) { |
| 3563 | return false; |
| 3564 | } |
| 3565 | } |
| 3566 | return true; |
| 3567 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3568 | [&](const type::Matrix* mat) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3569 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3570 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3571 | return false; |
| 3572 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3573 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3574 | for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) { |
| 3575 | if (i != 0) { |
| 3576 | out << ", "; |
| 3577 | } |
| 3578 | if (!EmitValue(out, mat->type(), value)) { |
| 3579 | return false; |
| 3580 | } |
| 3581 | } |
| 3582 | return true; |
| 3583 | }, |
| 3584 | [&](const sem::Struct*) { |
| 3585 | out << "("; |
| 3586 | TINT_DEFER(out << ")" << value); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3587 | return EmitType(out, type, builtin::AddressSpace::kUndefined, |
| 3588 | builtin::Access::kUndefined, ""); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3589 | }, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3590 | [&](const type::Array*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3591 | out << "("; |
| 3592 | TINT_DEFER(out << ")" << value); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3593 | return EmitType(out, type, builtin::AddressSpace::kUndefined, |
| 3594 | builtin::Access::kUndefined, ""); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3595 | }, |
| 3596 | [&](Default) { |
| 3597 | diagnostics_.add_error( |
| 3598 | diag::System::Writer, |
| 3599 | "Invalid type for value emission: " + type->FriendlyName(builder_.Symbols())); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3600 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3601 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3604 | bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* type) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3605 | return EmitValue(out, type, 0); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | bool GeneratorImpl::EmitLoop(const ast::LoopStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3609 | auto emit_continuing = [this, stmt]() { |
| 3610 | if (stmt->continuing && !stmt->continuing->Empty()) { |
| 3611 | if (!EmitBlock(stmt->continuing)) { |
| 3612 | return false; |
| 3613 | } |
| 3614 | } |
| 3615 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3616 | }; |
| 3617 | |
| 3618 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3619 | line() << "while (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3620 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3621 | ScopedIndent si(this); |
| 3622 | if (!EmitStatements(stmt->body->statements)) { |
| 3623 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3624 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3625 | if (!emit_continuing_()) { |
| 3626 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3627 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3628 | } |
| 3629 | line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3630 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3631 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3634 | bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { |
| 3635 | // Nest a for loop with a new block. In HLSL the initializer scope is not |
| 3636 | // nested by the for-loop, so we may get variable redefinitions. |
| 3637 | line() << "{"; |
| 3638 | increment_indent(); |
| 3639 | TINT_DEFER({ |
| 3640 | decrement_indent(); |
| 3641 | line() << "}"; |
| 3642 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3643 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3644 | TextBuffer init_buf; |
| 3645 | if (auto* init = stmt->initializer) { |
| 3646 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf); |
| 3647 | if (!EmitStatement(init)) { |
| 3648 | return false; |
| 3649 | } |
| 3650 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3651 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3652 | TextBuffer cond_pre; |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3653 | utils::StringStream cond_buf; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3654 | if (auto* cond = stmt->condition) { |
| 3655 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 3656 | if (!EmitExpression(cond_buf, cond)) { |
| 3657 | return false; |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | TextBuffer cont_buf; |
| 3662 | if (auto* cont = stmt->continuing) { |
| 3663 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cont_buf); |
| 3664 | if (!EmitStatement(cont)) { |
| 3665 | return false; |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | // If the for-loop has a multi-statement conditional and / or continuing, then |
| 3670 | // we cannot emit this as a regular for-loop in HLSL. Instead we need to |
| 3671 | // generate a `while(true)` loop. |
| 3672 | bool emit_as_loop = cond_pre.lines.size() > 0 || cont_buf.lines.size() > 1; |
| 3673 | |
| 3674 | // If the for-loop has multi-statement initializer, or is going to be emitted |
| 3675 | // as a `while(true)` loop, then declare the initializer statement(s) before |
| 3676 | // the loop. |
| 3677 | if (init_buf.lines.size() > 1 || (stmt->initializer && emit_as_loop)) { |
| 3678 | current_buffer_->Append(init_buf); |
| 3679 | init_buf.lines.clear(); // Don't emit the initializer again in the 'for' |
| 3680 | } |
| 3681 | |
| 3682 | if (emit_as_loop) { |
| 3683 | auto emit_continuing = [&]() { |
| 3684 | current_buffer_->Append(cont_buf); |
| 3685 | return true; |
| 3686 | }; |
| 3687 | |
| 3688 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3689 | line() << "while (true) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3690 | increment_indent(); |
| 3691 | TINT_DEFER({ |
| 3692 | decrement_indent(); |
| 3693 | line() << "}"; |
| 3694 | }); |
| 3695 | |
| 3696 | if (stmt->condition) { |
| 3697 | current_buffer_->Append(cond_pre); |
| 3698 | line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
| 3699 | } |
| 3700 | |
| 3701 | if (!EmitStatements(stmt->body->statements)) { |
| 3702 | return false; |
| 3703 | } |
| 3704 | |
| 3705 | if (!emit_continuing_()) { |
| 3706 | return false; |
| 3707 | } |
| 3708 | } else { |
| 3709 | // For-loop can be generated. |
| 3710 | { |
| 3711 | auto out = line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3712 | out << "for"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3713 | { |
| 3714 | ScopedParen sp(out); |
| 3715 | |
| 3716 | if (!init_buf.lines.empty()) { |
| 3717 | out << init_buf.lines[0].content << " "; |
| 3718 | } else { |
| 3719 | out << "; "; |
| 3720 | } |
| 3721 | |
| 3722 | out << cond_buf.str() << "; "; |
| 3723 | |
| 3724 | if (!cont_buf.lines.empty()) { |
| 3725 | out << TrimSuffix(cont_buf.lines[0].content, ";"); |
| 3726 | } |
| 3727 | } |
| 3728 | out << " {"; |
| 3729 | } |
| 3730 | { |
| 3731 | auto emit_continuing = [] { return true; }; |
| 3732 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 3733 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 3734 | return false; |
| 3735 | } |
| 3736 | } |
| 3737 | line() << "}"; |
| 3738 | } |
| 3739 | |
| 3740 | return true; |
| 3741 | } |
| 3742 | |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3743 | bool GeneratorImpl::EmitWhile(const ast::WhileStatement* stmt) { |
| 3744 | TextBuffer cond_pre; |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3745 | utils::StringStream cond_buf; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3746 | { |
| 3747 | auto* cond = stmt->condition; |
| 3748 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 3749 | if (!EmitExpression(cond_buf, cond)) { |
| 3750 | return false; |
| 3751 | } |
| 3752 | } |
| 3753 | |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 3754 | auto emit_continuing = [&]() { return true; }; |
| 3755 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 3756 | |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3757 | // If the while has a multi-statement conditional, then we cannot emit this |
| 3758 | // as a regular while in HLSL. Instead we need to generate a `while(true)` loop. |
| 3759 | bool emit_as_loop = cond_pre.lines.size() > 0; |
| 3760 | if (emit_as_loop) { |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3761 | line() << "while (true) {"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3762 | increment_indent(); |
| 3763 | TINT_DEFER({ |
| 3764 | decrement_indent(); |
| 3765 | line() << "}"; |
| 3766 | }); |
| 3767 | |
| 3768 | current_buffer_->Append(cond_pre); |
| 3769 | line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
| 3770 | if (!EmitStatements(stmt->body->statements)) { |
| 3771 | return false; |
| 3772 | } |
| 3773 | } else { |
| 3774 | // While can be generated. |
| 3775 | { |
| 3776 | auto out = line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3777 | out << "while"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3778 | { |
| 3779 | ScopedParen sp(out); |
| 3780 | out << cond_buf.str(); |
| 3781 | } |
| 3782 | out << " {"; |
| 3783 | } |
| 3784 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 3785 | return false; |
| 3786 | } |
| 3787 | line() << "}"; |
| 3788 | } |
| 3789 | |
| 3790 | return true; |
| 3791 | } |
| 3792 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3793 | bool GeneratorImpl::EmitMemberAccessor(utils::StringStream& out, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3794 | const ast::MemberAccessorExpression* expr) { |
Ben Clayton | ad31565 | 2023-02-05 12:36:50 +0000 | [diff] [blame] | 3795 | if (!EmitExpression(out, expr->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3796 | return false; |
| 3797 | } |
| 3798 | out << "."; |
| 3799 | |
Ben Clayton | 2f9a988 | 2022-12-17 02:20:04 +0000 | [diff] [blame] | 3800 | auto* sem = builder_.Sem().Get(expr)->UnwrapLoad(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3801 | |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 3802 | return Switch( |
| 3803 | sem, |
| 3804 | [&](const sem::Swizzle*) { |
| 3805 | // Swizzles output the name directly |
Ben Clayton | a4117ca | 2023-02-02 20:44:53 +0000 | [diff] [blame] | 3806 | out << builder_.Symbols().NameFor(expr->member->symbol); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 3807 | return true; |
| 3808 | }, |
| 3809 | [&](const sem::StructMemberAccess* member_access) { |
| 3810 | out << program_->Symbols().NameFor(member_access->Member()->Name()); |
| 3811 | return true; |
| 3812 | }, |
| 3813 | [&](Default) { |
| 3814 | TINT_ICE(Writer, diagnostics_) |
| 3815 | << "unknown member access type: " << sem->TypeInfo().name; |
| 3816 | return false; |
| 3817 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | bool GeneratorImpl::EmitReturn(const ast::ReturnStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3821 | if (stmt->value) { |
| 3822 | auto out = line(); |
| 3823 | out << "return "; |
| 3824 | if (!EmitExpression(out, stmt->value)) { |
| 3825 | return false; |
| 3826 | } |
| 3827 | out << ";"; |
| 3828 | } else { |
| 3829 | line() << "return;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3830 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3831 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | bool GeneratorImpl::EmitStatement(const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3835 | return Switch( |
| 3836 | stmt, |
| 3837 | [&](const ast::AssignmentStatement* a) { // |
| 3838 | return EmitAssign(a); |
| 3839 | }, |
| 3840 | [&](const ast::BlockStatement* b) { // |
| 3841 | return EmitBlock(b); |
| 3842 | }, |
| 3843 | [&](const ast::BreakStatement* b) { // |
| 3844 | return EmitBreak(b); |
| 3845 | }, |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 3846 | [&](const ast::BreakIfStatement* b) { // |
| 3847 | return EmitBreakIf(b); |
| 3848 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3849 | [&](const ast::CallStatement* c) { // |
| 3850 | auto out = line(); |
| 3851 | if (!EmitCall(out, c->expr)) { |
| 3852 | return false; |
| 3853 | } |
| 3854 | out << ";"; |
| 3855 | return true; |
| 3856 | }, |
| 3857 | [&](const ast::ContinueStatement* c) { // |
| 3858 | return EmitContinue(c); |
| 3859 | }, |
| 3860 | [&](const ast::DiscardStatement* d) { // |
| 3861 | return EmitDiscard(d); |
| 3862 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3863 | [&](const ast::IfStatement* i) { // |
| 3864 | return EmitIf(i); |
| 3865 | }, |
| 3866 | [&](const ast::LoopStatement* l) { // |
| 3867 | return EmitLoop(l); |
| 3868 | }, |
| 3869 | [&](const ast::ForLoopStatement* l) { // |
| 3870 | return EmitForLoop(l); |
| 3871 | }, |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3872 | [&](const ast::WhileStatement* l) { // |
| 3873 | return EmitWhile(l); |
| 3874 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3875 | [&](const ast::ReturnStatement* r) { // |
| 3876 | return EmitReturn(r); |
| 3877 | }, |
| 3878 | [&](const ast::SwitchStatement* s) { // |
| 3879 | return EmitSwitch(s); |
| 3880 | }, |
| 3881 | [&](const ast::VariableDeclStatement* v) { // |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3882 | return Switch( |
| 3883 | v->variable, // |
| 3884 | [&](const ast::Var* var) { return EmitVar(var); }, |
| 3885 | [&](const ast::Let* let) { return EmitLet(let); }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3886 | [&](const ast::Const*) { |
| 3887 | return true; // Constants are embedded at their use |
| 3888 | }, |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3889 | [&](Default) { // |
| 3890 | TINT_ICE(Writer, diagnostics_) |
| 3891 | << "unknown variable type: " << v->variable->TypeInfo().name; |
| 3892 | return false; |
| 3893 | }); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3894 | }, |
Ben Clayton | c98d57d | 2023-01-24 14:59:43 +0000 | [diff] [blame] | 3895 | [&](const ast::ConstAssert*) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 3896 | return true; // Not emitted |
| 3897 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3898 | [&](Default) { // |
| 3899 | diagnostics_.add_error(diag::System::Writer, |
| 3900 | "unknown statement type: " + std::string(stmt->TypeInfo().name)); |
| 3901 | return false; |
| 3902 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | bool GeneratorImpl::EmitDefaultOnlySwitch(const ast::SwitchStatement* stmt) { |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3906 | TINT_ASSERT(Writer, stmt->body.Length() == 1 && stmt->body[0]->ContainsDefault()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3907 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3908 | // FXC fails to compile a switch with just a default case, ignoring the |
| 3909 | // default case body. We work around this here by emitting the default case |
| 3910 | // without the switch. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3911 | |
Antonio Maiorano | eab1f62 | 2023-02-01 15:46:34 +0000 | [diff] [blame] | 3912 | // Emit the switch condition as-is if it has side-effects (e.g. |
| 3913 | // function call). Note that we can ignore the result of the expression (if any). |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 3914 | if (auto* sem_cond = builder_.Sem().GetVal(stmt->condition); sem_cond->HasSideEffects()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3915 | auto out = line(); |
| 3916 | if (!EmitExpression(out, stmt->condition)) { |
| 3917 | return false; |
| 3918 | } |
| 3919 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3920 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3921 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3922 | // Emit "do { <default case body> } while(false);". We use a 'do' loop so |
| 3923 | // that break statements work as expected, and make it 'while (false)' in |
| 3924 | // case there isn't a break statement. |
| 3925 | line() << "do {"; |
| 3926 | { |
| 3927 | ScopedIndent si(this); |
| 3928 | if (!EmitStatements(stmt->body[0]->body->statements)) { |
| 3929 | return false; |
| 3930 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3931 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3932 | line() << "} while (false);"; |
| 3933 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | bool GeneratorImpl::EmitSwitch(const ast::SwitchStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3937 | // BUG(crbug.com/tint/1188): work around default-only switches |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3938 | if (stmt->body.Length() == 1 && stmt->body[0]->selectors.Length() == 1 && |
| 3939 | stmt->body[0]->ContainsDefault()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3940 | return EmitDefaultOnlySwitch(stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3941 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3942 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3943 | { // switch(expr) { |
| 3944 | auto out = line(); |
| 3945 | out << "switch("; |
| 3946 | if (!EmitExpression(out, stmt->condition)) { |
| 3947 | return false; |
| 3948 | } |
| 3949 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3950 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3951 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3952 | { |
| 3953 | ScopedIndent si(this); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 3954 | for (size_t i = 0; i < stmt->body.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3955 | if (!EmitCase(stmt, i)) { |
| 3956 | return false; |
| 3957 | } |
| 3958 | } |
| 3959 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3960 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3961 | line() << "}"; |
| 3962 | |
| 3963 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 3966 | bool GeneratorImpl::EmitType(utils::StringStream& out, |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 3967 | const type::Type* type, |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3968 | builtin::AddressSpace address_space, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3969 | builtin::Access access, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3970 | const std::string& name, |
| 3971 | bool* name_printed /* = nullptr */) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3972 | if (name_printed) { |
| 3973 | *name_printed = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3974 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3975 | switch (address_space) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3976 | case builtin::AddressSpace::kStorage: |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3977 | if (access != builtin::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3978 | out << "RW"; |
| 3979 | } |
| 3980 | out << "ByteAddressBuffer"; |
| 3981 | return true; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3982 | case builtin::AddressSpace::kUniform: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3983 | auto array_length = (type->Size() + 15) / 16; |
| 3984 | out << "uint4 " << name << "[" << array_length << "]"; |
| 3985 | if (name_printed) { |
| 3986 | *name_printed = true; |
| 3987 | } |
| 3988 | return true; |
| 3989 | } |
| 3990 | default: |
| 3991 | break; |
| 3992 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3993 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3994 | return Switch( |
| 3995 | type, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3996 | [&](const type::Array* ary) { |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 3997 | const type::Type* base_type = ary; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3998 | std::vector<uint32_t> sizes; |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3999 | while (auto* arr = base_type->As<type::Array>()) { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4000 | if (TINT_UNLIKELY(arr->Count()->Is<type::RuntimeArrayCount>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4001 | TINT_ICE(Writer, diagnostics_) |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4002 | << "runtime arrays may only exist in storage buffers, which should have " |
Ben Clayton | 3a68ab4 | 2022-06-24 08:30:28 +0000 | [diff] [blame] | 4003 | "been transformed into a ByteAddressBuffer"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4004 | return false; |
| 4005 | } |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4006 | const auto count = arr->ConstantCount(); |
| 4007 | if (!count) { |
| 4008 | diagnostics_.add_error(diag::System::Writer, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4009 | type::Array::kErrExpectedConstantCount); |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4010 | return false; |
| 4011 | } |
| 4012 | |
| 4013 | sizes.push_back(count.value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4014 | base_type = arr->ElemType(); |
| 4015 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4016 | if (!EmitType(out, base_type, address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4017 | return false; |
| 4018 | } |
| 4019 | if (!name.empty()) { |
| 4020 | out << " " << name; |
| 4021 | if (name_printed) { |
| 4022 | *name_printed = true; |
| 4023 | } |
| 4024 | } |
| 4025 | for (uint32_t size : sizes) { |
| 4026 | out << "[" << size << "]"; |
| 4027 | } |
| 4028 | return true; |
| 4029 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4030 | [&](const type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4031 | out << "bool"; |
| 4032 | return true; |
| 4033 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4034 | [&](const type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4035 | out << "float"; |
| 4036 | return true; |
| 4037 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4038 | [&](const type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4039 | out << "float16_t"; |
| 4040 | return true; |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 4041 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4042 | [&](const type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4043 | out << "int"; |
| 4044 | return true; |
| 4045 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4046 | [&](const type::Matrix* mat) { |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4047 | if (mat->type()->Is<type::F16>()) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4048 | // Use matrix<type, N, M> for f16 matrix |
| 4049 | out << "matrix<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4050 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4051 | return false; |
| 4052 | } |
| 4053 | out << ", " << mat->columns() << ", " << mat->rows() << ">"; |
| 4054 | return true; |
| 4055 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4056 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4057 | return false; |
| 4058 | } |
| 4059 | // Note: HLSL's matrices are declared as <type>NxM, where N is the |
| 4060 | // number of rows and M is the number of columns. Despite HLSL's |
| 4061 | // matrices being column-major by default, the index operator and |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4062 | // initializers actually operate on row-vectors, where as WGSL operates |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4063 | // on column vectors. To simplify everything we use the transpose of the |
| 4064 | // matrices. See: |
| 4065 | // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math#matrix-ordering |
| 4066 | out << mat->columns() << "x" << mat->rows(); |
| 4067 | return true; |
| 4068 | }, |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 4069 | [&](const type::Pointer*) { |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 4070 | TINT_ICE(Writer, diagnostics_) << "Attempting to emit pointer type. These should have " |
| 4071 | "been removed with the SimplifyPointers transform"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4072 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4073 | }, |
dan sinclair | 5ee58b6 | 2022-12-08 15:25:18 +0000 | [diff] [blame] | 4074 | [&](const type::Sampler* sampler) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4075 | out << "Sampler"; |
| 4076 | if (sampler->IsComparison()) { |
| 4077 | out << "Comparison"; |
| 4078 | } |
| 4079 | out << "State"; |
| 4080 | return true; |
| 4081 | }, |
| 4082 | [&](const sem::Struct* str) { |
| 4083 | out << StructName(str); |
| 4084 | return true; |
| 4085 | }, |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 4086 | [&](const type::Texture* tex) { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4087 | if (TINT_UNLIKELY(tex->Is<type::ExternalTexture>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4088 | TINT_ICE(Writer, diagnostics_) |
| 4089 | << "Multiplanar external texture transform was not run."; |
| 4090 | return false; |
| 4091 | } |
Brandon Jones | 6661b28 | 2022-02-25 20:14:52 +0000 | [diff] [blame] | 4092 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 4093 | auto* storage = tex->As<type::StorageTexture>(); |
| 4094 | auto* ms = tex->As<type::MultisampledTexture>(); |
| 4095 | auto* depth_ms = tex->As<type::DepthMultisampledTexture>(); |
| 4096 | auto* sampled = tex->As<type::SampledTexture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4097 | |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4098 | if (storage && storage->access() != builtin::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4099 | out << "RW"; |
| 4100 | } |
| 4101 | out << "Texture"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4102 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4103 | switch (tex->dim()) { |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4104 | case type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4105 | out << "1D"; |
| 4106 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4107 | case type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4108 | out << ((ms || depth_ms) ? "2DMS" : "2D"); |
| 4109 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4110 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4111 | out << ((ms || depth_ms) ? "2DMSArray" : "2DArray"); |
| 4112 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4113 | case type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4114 | out << "3D"; |
| 4115 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4116 | case type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4117 | out << "Cube"; |
| 4118 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4119 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4120 | out << "CubeArray"; |
| 4121 | break; |
| 4122 | default: |
| 4123 | TINT_UNREACHABLE(Writer, diagnostics_) |
| 4124 | << "unexpected TextureDimension " << tex->dim(); |
| 4125 | return false; |
| 4126 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4127 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4128 | if (storage) { |
| 4129 | auto* component = image_format_to_rwtexture_type(storage->texel_format()); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4130 | if (TINT_UNLIKELY(!component)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4131 | TINT_ICE(Writer, diagnostics_) << "Unsupported StorageTexture TexelFormat: " |
| 4132 | << static_cast<int>(storage->texel_format()); |
| 4133 | return false; |
| 4134 | } |
| 4135 | out << "<" << component << ">"; |
| 4136 | } else if (depth_ms) { |
| 4137 | out << "<float4>"; |
| 4138 | } else if (sampled || ms) { |
| 4139 | auto* subtype = sampled ? sampled->type() : ms->type(); |
| 4140 | out << "<"; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4141 | if (subtype->Is<type::F32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4142 | out << "float4"; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4143 | } else if (subtype->Is<type::I32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4144 | out << "int4"; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4145 | } else if (TINT_LIKELY(subtype->Is<type::U32>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4146 | out << "uint4"; |
| 4147 | } else { |
| 4148 | TINT_ICE(Writer, diagnostics_) << "Unsupported multisampled texture type"; |
| 4149 | return false; |
| 4150 | } |
| 4151 | out << ">"; |
| 4152 | } |
| 4153 | return true; |
| 4154 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4155 | [&](const type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4156 | out << "uint"; |
| 4157 | return true; |
| 4158 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4159 | [&](const type::Vector* vec) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4160 | auto width = vec->Width(); |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4161 | if (vec->type()->Is<type::F32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4162 | out << "float" << width; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4163 | } else if (vec->type()->Is<type::I32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4164 | out << "int" << width; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4165 | } else if (vec->type()->Is<type::U32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4166 | out << "uint" << width; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4167 | } else if (vec->type()->Is<type::Bool>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4168 | out << "bool" << width; |
| 4169 | } else { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4170 | // For example, use "vector<float16_t, N>" for f16 vector. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4171 | out << "vector<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4172 | if (!EmitType(out, vec->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4173 | return false; |
| 4174 | } |
| 4175 | out << ", " << width << ">"; |
| 4176 | } |
| 4177 | return true; |
| 4178 | }, |
dan sinclair | d8a0845 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4179 | [&](const type::Atomic* atomic) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4180 | return EmitType(out, atomic->Type(), address_space, access, name); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4181 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4182 | [&](const type::Void*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4183 | out << "void"; |
| 4184 | return true; |
| 4185 | }, |
| 4186 | [&](Default) { |
| 4187 | diagnostics_.add_error(diag::System::Writer, "unknown type in EmitType"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4188 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4189 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 4192 | bool GeneratorImpl::EmitTypeAndName(utils::StringStream& out, |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 4193 | const type::Type* type, |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4194 | builtin::AddressSpace address_space, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4195 | builtin::Access access, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4196 | const std::string& name) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4197 | bool name_printed = false; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4198 | if (!EmitType(out, type, address_space, access, name, &name_printed)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4199 | return false; |
| 4200 | } |
| 4201 | if (!name.empty() && !name_printed) { |
| 4202 | out << " " << name; |
| 4203 | } |
| 4204 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | bool GeneratorImpl::EmitStructType(TextBuffer* b, const sem::Struct* str) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 4208 | auto it = emitted_structs_.emplace(str); |
| 4209 | if (!it.second) { |
| 4210 | return true; |
| 4211 | } |
| 4212 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4213 | line(b) << "struct " << StructName(str) << " {"; |
| 4214 | { |
| 4215 | ScopedIndent si(b); |
| 4216 | for (auto* mem : str->Members()) { |
| 4217 | auto mem_name = builder_.Symbols().NameFor(mem->Name()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4218 | auto* ty = mem->Type(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4219 | auto out = line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4220 | std::string pre, post; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4221 | if (auto* decl = mem->Declaration()) { |
| 4222 | for (auto* attr : decl->attributes) { |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 4223 | if (attr->Is<ast::LocationAttribute>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4224 | auto& pipeline_stage_uses = str->PipelineStageUses(); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4225 | if (TINT_UNLIKELY(pipeline_stage_uses.size() != 1)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4226 | TINT_ICE(Writer, diagnostics_) << "invalid entry point IO struct uses"; |
| 4227 | } |
| 4228 | |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 4229 | auto loc = mem->Location().value(); |
dan sinclair | f53b2b5 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4230 | if (pipeline_stage_uses.count(type::PipelineStageUsage::kVertexInput)) { |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 4231 | post += " : TEXCOORD" + std::to_string(loc); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4232 | } else if (pipeline_stage_uses.count( |
dan sinclair | f53b2b5 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4233 | type::PipelineStageUsage::kVertexOutput)) { |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 4234 | post += " : TEXCOORD" + std::to_string(loc); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4235 | } else if (pipeline_stage_uses.count( |
dan sinclair | f53b2b5 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4236 | type::PipelineStageUsage::kFragmentInput)) { |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 4237 | post += " : TEXCOORD" + std::to_string(loc); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4238 | } else if (TINT_LIKELY(pipeline_stage_uses.count( |
| 4239 | type::PipelineStageUsage::kFragmentOutput))) { |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 4240 | post += " : SV_Target" + std::to_string(loc); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4241 | } else { |
| 4242 | TINT_ICE(Writer, diagnostics_) << "invalid use of location attribute"; |
| 4243 | } |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 4244 | } else if (auto* builtin_attr = attr->As<ast::BuiltinAttribute>()) { |
| 4245 | auto builtin = program_->Sem().Get(builtin_attr)->Value(); |
| 4246 | auto name = builtin_to_attribute(builtin); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4247 | if (name.empty()) { |
| 4248 | diagnostics_.add_error(diag::System::Writer, "unsupported builtin"); |
| 4249 | return false; |
| 4250 | } |
| 4251 | post += " : " + name; |
| 4252 | } else if (auto* interpolate = attr->As<ast::InterpolateAttribute>()) { |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 4253 | auto& sem = program_->Sem(); |
| 4254 | auto i_type = |
| 4255 | sem.Get<sem::BuiltinEnumExpression<builtin::InterpolationType>>( |
| 4256 | interpolate->type) |
| 4257 | ->Value(); |
| 4258 | |
| 4259 | auto i_smpl = builtin::InterpolationSampling::kUndefined; |
| 4260 | if (interpolate->sampling) { |
| 4261 | i_smpl = |
| 4262 | sem.Get<sem::BuiltinEnumExpression<builtin::InterpolationSampling>>( |
| 4263 | interpolate->sampling) |
| 4264 | ->Value(); |
| 4265 | } |
| 4266 | |
| 4267 | auto mod = interpolation_to_modifiers(i_type, i_smpl); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4268 | if (mod.empty()) { |
| 4269 | diagnostics_.add_error(diag::System::Writer, |
| 4270 | "unsupported interpolation"); |
| 4271 | return false; |
| 4272 | } |
| 4273 | pre += mod; |
| 4274 | |
| 4275 | } else if (attr->Is<ast::InvariantAttribute>()) { |
| 4276 | // Note: `precise` is not exactly the same as `invariant`, but is |
| 4277 | // stricter and therefore provides the necessary guarantees. |
| 4278 | // See discussion here: https://github.com/gpuweb/gpuweb/issues/893 |
| 4279 | pre += "precise "; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4280 | } else if (TINT_UNLIKELY((!attr->IsAnyOf<ast::StructMemberAlignAttribute, |
| 4281 | ast::StructMemberOffsetAttribute, |
| 4282 | ast::StructMemberSizeAttribute>()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4283 | TINT_ICE(Writer, diagnostics_) |
| 4284 | << "unhandled struct member attribute: " << attr->Name(); |
| 4285 | return false; |
| 4286 | } |
| 4287 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4290 | out << pre; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4291 | if (!EmitTypeAndName(out, ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4292 | builtin::Access::kReadWrite, mem_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4293 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4294 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4295 | out << post << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4296 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4297 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4298 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4299 | line(b) << "};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4300 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 4303 | bool GeneratorImpl::EmitUnaryOp(utils::StringStream& out, const ast::UnaryOpExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4304 | switch (expr->op) { |
| 4305 | case ast::UnaryOp::kIndirection: |
| 4306 | case ast::UnaryOp::kAddressOf: |
| 4307 | return EmitExpression(out, expr->expr); |
| 4308 | case ast::UnaryOp::kComplement: |
| 4309 | out << "~"; |
| 4310 | break; |
| 4311 | case ast::UnaryOp::kNot: |
| 4312 | out << "!"; |
| 4313 | break; |
| 4314 | case ast::UnaryOp::kNegation: |
| 4315 | out << "-"; |
| 4316 | break; |
| 4317 | } |
| 4318 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4319 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4320 | if (!EmitExpression(out, expr->expr)) { |
| 4321 | return false; |
| 4322 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4323 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4324 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4325 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4326 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4329 | bool GeneratorImpl::EmitVar(const ast::Var* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4330 | auto* sem = builder_.Sem().Get(var); |
| 4331 | auto* type = sem->Type()->UnwrapRef(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4332 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4333 | auto out = line(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4334 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 4335 | builder_.Symbols().NameFor(var->name->symbol))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4336 | return false; |
| 4337 | } |
| 4338 | |
| 4339 | out << " = "; |
| 4340 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4341 | if (var->initializer) { |
| 4342 | if (!EmitExpression(out, var->initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4343 | return false; |
| 4344 | } |
| 4345 | } else { |
| 4346 | if (!EmitZeroValue(out, type)) { |
| 4347 | return false; |
| 4348 | } |
| 4349 | } |
| 4350 | out << ";"; |
| 4351 | |
| 4352 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4355 | bool GeneratorImpl::EmitLet(const ast::Let* let) { |
| 4356 | auto* sem = builder_.Sem().Get(let); |
| 4357 | auto* type = sem->Type()->UnwrapRef(); |
| 4358 | |
| 4359 | auto out = line(); |
| 4360 | out << "const "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4361 | if (!EmitTypeAndName(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
Ben Clayton | 651d9e2 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 4362 | builder_.Symbols().NameFor(let->name->symbol))) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4363 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4364 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4365 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4366 | if (!EmitExpression(out, let->initializer)) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4367 | return false; |
| 4368 | } |
| 4369 | out << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4370 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4371 | return true; |
| 4372 | } |
| 4373 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4374 | template <typename F> |
dan sinclair | 2b9d5b3 | 2023-02-28 14:49:25 +0000 | [diff] [blame] | 4375 | bool GeneratorImpl::CallBuiltinHelper(utils::StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4376 | const ast::CallExpression* call, |
| 4377 | const sem::Builtin* builtin, |
| 4378 | F&& build) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4379 | // Generate the helper function if it hasn't been created already |
| 4380 | auto fn = utils::GetOrCreate(builtins_, builtin, [&]() -> std::string { |
| 4381 | TextBuffer b; |
| 4382 | TINT_DEFER(helpers_.Append(b)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4383 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 4384 | auto fn_name = UniqueIdentifier(std::string("tint_") + builtin::str(builtin->Type())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4385 | std::vector<std::string> parameter_names; |
| 4386 | { |
| 4387 | auto decl = line(&b); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4388 | if (!EmitTypeAndName(decl, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4389 | builtin::Access::kUndefined, fn_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4390 | return ""; |
| 4391 | } |
| 4392 | { |
| 4393 | ScopedParen sp(decl); |
| 4394 | for (auto* param : builtin->Parameters()) { |
| 4395 | if (!parameter_names.empty()) { |
| 4396 | decl << ", "; |
| 4397 | } |
| 4398 | auto param_name = "param_" + std::to_string(parameter_names.size()); |
| 4399 | const auto* ty = param->Type(); |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 4400 | if (auto* ptr = ty->As<type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4401 | decl << "inout "; |
| 4402 | ty = ptr->StoreType(); |
| 4403 | } |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4404 | if (!EmitTypeAndName(decl, ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4405 | builtin::Access::kUndefined, param_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4406 | return ""; |
| 4407 | } |
| 4408 | parameter_names.emplace_back(std::move(param_name)); |
| 4409 | } |
| 4410 | } |
| 4411 | decl << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4412 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4413 | { |
| 4414 | ScopedIndent si(&b); |
| 4415 | if (!build(&b, parameter_names)) { |
| 4416 | return ""; |
| 4417 | } |
| 4418 | } |
| 4419 | line(&b) << "}"; |
| 4420 | line(&b); |
| 4421 | return fn_name; |
| 4422 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4423 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4424 | if (fn.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4425 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4426 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4427 | |
| 4428 | // Call the helper |
| 4429 | out << fn; |
| 4430 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 4431 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4432 | bool first = true; |
| 4433 | for (auto* arg : call->args) { |
| 4434 | if (!first) { |
| 4435 | out << ", "; |
| 4436 | } |
| 4437 | first = false; |
| 4438 | if (!EmitExpression(out, arg)) { |
| 4439 | return false; |
| 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
dan sinclair | 6a5bef1 | 2022-04-07 14:30:24 +0000 | [diff] [blame] | 4446 | } // namespace tint::writer::hlsl |