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