ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 8 | #include "include/sksl/SkSLErrorReporter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/sksl/SkSLContext.h" |
John Stiles | 2dda4b6 | 2021-09-16 13:18:10 -0400 | [diff] [blame] | 10 | #include "src/sksl/SkSLMangler.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLMemoryLayout.h" |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "tests/Test.h" |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 14 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 15 | DEF_TEST(SkSLMemoryLayout140Test, r) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 16 | SkSL::TestingOnly_AbortErrorReporter errors; |
John Stiles | 2dda4b6 | 2021-09-16 13:18:10 -0400 | [diff] [blame] | 17 | GrShaderCaps caps(GrContextOptions{}); |
| 18 | SkSL::Mangler mangler; |
| 19 | SkSL::Context context(errors, caps, mangler); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 20 | SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard); |
| 21 | |
| 22 | // basic types |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 23 | REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fFloat)); |
| 24 | REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fFloat2)); |
| 25 | REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fFloat3)); |
| 26 | REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fFloat4)); |
| 27 | REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fInt)); |
| 28 | REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fInt2)); |
| 29 | REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fInt3)); |
| 30 | REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fInt4)); |
| 31 | REPORTER_ASSERT(r, 1 == layout.size(*context.fTypes.fBool)); |
| 32 | REPORTER_ASSERT(r, 2 == layout.size(*context.fTypes.fBool2)); |
| 33 | REPORTER_ASSERT(r, 3 == layout.size(*context.fTypes.fBool3)); |
| 34 | REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fBool4)); |
| 35 | REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat2x2)); |
| 36 | REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat2x4)); |
| 37 | REPORTER_ASSERT(r, 48 == layout.size(*context.fTypes.fFloat3x3)); |
| 38 | REPORTER_ASSERT(r, 64 == layout.size(*context.fTypes.fFloat4x2)); |
| 39 | REPORTER_ASSERT(r, 64 == layout.size(*context.fTypes.fFloat4x4)); |
| 40 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fFloat)); |
| 41 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat2)); |
| 42 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3)); |
| 43 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4)); |
| 44 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fInt)); |
| 45 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fInt2)); |
| 46 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt3)); |
| 47 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt4)); |
| 48 | REPORTER_ASSERT(r, 1 == layout.alignment(*context.fTypes.fBool)); |
| 49 | REPORTER_ASSERT(r, 2 == layout.alignment(*context.fTypes.fBool2)); |
| 50 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool3)); |
| 51 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool4)); |
| 52 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat2x2)); |
| 53 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat2x4)); |
| 54 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3x3)); |
| 55 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4x2)); |
| 56 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4x4)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 57 | |
| 58 | // struct 1 |
| 59 | std::vector<SkSL::Type::Field> fields1; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 60 | fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("a"), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 61 | context.fTypes.fFloat3.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 62 | std::unique_ptr<SkSL::Type> s1 = SkSL::Type::MakeStructType(-1, SkSL::String("s1"), fields1); |
| 63 | REPORTER_ASSERT(r, 16 == layout.size(*s1)); |
| 64 | REPORTER_ASSERT(r, 16 == layout.alignment(*s1)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 65 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 66 | fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("b"), context.fTypes.fFloat.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 67 | std::unique_ptr<SkSL::Type> s2 = SkSL::Type::MakeStructType(-1, SkSL::String("s2"), fields1); |
| 68 | REPORTER_ASSERT(r, 16 == layout.size(*s2)); |
| 69 | REPORTER_ASSERT(r, 16 == layout.alignment(*s2)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 70 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 71 | fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("c"), context.fTypes.fBool.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 72 | std::unique_ptr<SkSL::Type> s3 = SkSL::Type::MakeStructType(-1, SkSL::String("s3"), fields1); |
| 73 | REPORTER_ASSERT(r, 32 == layout.size(*s3)); |
| 74 | REPORTER_ASSERT(r, 16 == layout.alignment(*s3)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 75 | |
| 76 | // struct 2 |
| 77 | std::vector<SkSL::Type::Field> fields2; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 78 | fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("a"), context.fTypes.fInt.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 79 | std::unique_ptr<SkSL::Type> s4 = SkSL::Type::MakeStructType(-1, SkSL::String("s4"), fields2); |
| 80 | REPORTER_ASSERT(r, 16 == layout.size(*s4)); |
| 81 | REPORTER_ASSERT(r, 16 == layout.alignment(*s4)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 82 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 83 | fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("b"), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 84 | context.fTypes.fFloat3.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 85 | std::unique_ptr<SkSL::Type> s5 = SkSL::Type::MakeStructType(-1, SkSL::String("s5"), fields2); |
| 86 | REPORTER_ASSERT(r, 32 == layout.size(*s5)); |
| 87 | REPORTER_ASSERT(r, 16 == layout.alignment(*s5)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 88 | |
| 89 | // arrays |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 90 | std::unique_ptr<SkSL::Type> array1 = |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 91 | SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 92 | REPORTER_ASSERT(r, 64 == layout.size(*array1)); |
| 93 | REPORTER_ASSERT(r, 16 == layout.alignment(*array1)); |
| 94 | REPORTER_ASSERT(r, 16 == layout.stride(*array1)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 95 | |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 96 | std::unique_ptr<SkSL::Type> array2 = |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 97 | SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 98 | REPORTER_ASSERT(r, 64 == layout.size(*array2)); |
| 99 | REPORTER_ASSERT(r, 16 == layout.alignment(*array2)); |
| 100 | REPORTER_ASSERT(r, 16 == layout.stride(*array2)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | DEF_TEST(SkSLMemoryLayout430Test, r) { |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 104 | SkSL::TestingOnly_AbortErrorReporter errors; |
John Stiles | 2dda4b6 | 2021-09-16 13:18:10 -0400 | [diff] [blame] | 105 | GrShaderCaps caps(GrContextOptions{}); |
| 106 | SkSL::Mangler mangler; |
| 107 | SkSL::Context context(errors, caps, mangler); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 108 | SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard); |
| 109 | |
| 110 | // basic types |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 111 | REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fFloat)); |
| 112 | REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fFloat2)); |
| 113 | REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fFloat3)); |
| 114 | REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fFloat4)); |
| 115 | REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fInt)); |
| 116 | REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fInt2)); |
| 117 | REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fInt3)); |
| 118 | REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fInt4)); |
| 119 | REPORTER_ASSERT(r, 1 == layout.size(*context.fTypes.fBool)); |
| 120 | REPORTER_ASSERT(r, 2 == layout.size(*context.fTypes.fBool2)); |
| 121 | REPORTER_ASSERT(r, 3 == layout.size(*context.fTypes.fBool3)); |
| 122 | REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fBool4)); |
| 123 | REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fFloat2x2)); |
| 124 | REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat2x4)); |
| 125 | REPORTER_ASSERT(r, 48 == layout.size(*context.fTypes.fFloat3x3)); |
| 126 | REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat4x2)); |
| 127 | REPORTER_ASSERT(r, 64 == layout.size(*context.fTypes.fFloat4x4)); |
| 128 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fFloat)); |
| 129 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat2)); |
| 130 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3)); |
| 131 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4)); |
| 132 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fInt)); |
| 133 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fInt2)); |
| 134 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt3)); |
| 135 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt4)); |
| 136 | REPORTER_ASSERT(r, 1 == layout.alignment(*context.fTypes.fBool)); |
| 137 | REPORTER_ASSERT(r, 2 == layout.alignment(*context.fTypes.fBool2)); |
| 138 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool3)); |
| 139 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool4)); |
| 140 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat2x2)); |
| 141 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat2x4)); |
| 142 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3x3)); |
| 143 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat4x2)); |
| 144 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4x4)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 145 | |
| 146 | // struct 1 |
| 147 | std::vector<SkSL::Type::Field> fields1; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 148 | fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("a"), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 149 | context.fTypes.fFloat3.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 150 | std::unique_ptr<SkSL::Type> s1 = SkSL::Type::MakeStructType(-1, SkSL::String("s1"), fields1); |
| 151 | REPORTER_ASSERT(r, 16 == layout.size(*s1)); |
| 152 | REPORTER_ASSERT(r, 16 == layout.alignment(*s1)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 153 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 154 | fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("b"), context.fTypes.fFloat.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 155 | std::unique_ptr<SkSL::Type> s2 = SkSL::Type::MakeStructType(-1, SkSL::String("s2"), fields1); |
| 156 | REPORTER_ASSERT(r, 16 == layout.size(*s2)); |
| 157 | REPORTER_ASSERT(r, 16 == layout.alignment(*s2)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 158 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 159 | fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("c"), context.fTypes.fBool.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 160 | std::unique_ptr<SkSL::Type> s3 = SkSL::Type::MakeStructType(-1, SkSL::String("s3"), fields1); |
| 161 | REPORTER_ASSERT(r, 32 == layout.size(*s3)); |
| 162 | REPORTER_ASSERT(r, 16 == layout.alignment(*s3)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 163 | |
| 164 | // struct 2 |
| 165 | std::vector<SkSL::Type::Field> fields2; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 166 | fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("a"), context.fTypes.fInt.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 167 | std::unique_ptr<SkSL::Type> s4 = SkSL::Type::MakeStructType(-1, SkSL::String("s4"), fields2); |
| 168 | REPORTER_ASSERT(r, 4 == layout.size(*s4)); |
| 169 | REPORTER_ASSERT(r, 4 == layout.alignment(*s4)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 170 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 171 | fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("b"), |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 172 | context.fTypes.fFloat3.get()); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 173 | std::unique_ptr<SkSL::Type> s5 = SkSL::Type::MakeStructType(-1, SkSL::String("s5"), fields2); |
| 174 | REPORTER_ASSERT(r, 32 == layout.size(*s5)); |
| 175 | REPORTER_ASSERT(r, 16 == layout.alignment(*s5)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 176 | |
| 177 | // arrays |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 178 | std::unique_ptr<SkSL::Type> array1 = |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 179 | SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 180 | REPORTER_ASSERT(r, 16 == layout.size(*array1)); |
| 181 | REPORTER_ASSERT(r, 4 == layout.alignment(*array1)); |
| 182 | REPORTER_ASSERT(r, 4 == layout.stride(*array1)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 183 | |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 184 | std::unique_ptr<SkSL::Type> array2 = |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 185 | SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 186 | REPORTER_ASSERT(r, 64 == layout.size(*array2)); |
| 187 | REPORTER_ASSERT(r, 16 == layout.alignment(*array2)); |
| 188 | REPORTER_ASSERT(r, 16 == layout.stride(*array2)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 189 | } |