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