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 | |
| 8 | #include "SkSLContext.h" |
| 9 | #include "SkSLMemoryLayout.h" |
| 10 | |
| 11 | #include "Test.h" |
| 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 |
| 18 | REPORTER_ASSERT(r, 4 == layout.size(*context.fFloat_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 19 | REPORTER_ASSERT(r, 8 == layout.size(*context.fFloat2_Type)); |
| 20 | REPORTER_ASSERT(r, 12 == layout.size(*context.fFloat3_Type)); |
| 21 | REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 22 | REPORTER_ASSERT(r, 4 == layout.size(*context.fInt_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 23 | REPORTER_ASSERT(r, 8 == layout.size(*context.fInt2_Type)); |
| 24 | REPORTER_ASSERT(r, 12 == layout.size(*context.fInt3_Type)); |
| 25 | REPORTER_ASSERT(r, 16 == layout.size(*context.fInt4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 26 | REPORTER_ASSERT(r, 1 == layout.size(*context.fBool_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 27 | REPORTER_ASSERT(r, 2 == layout.size(*context.fBool2_Type)); |
| 28 | REPORTER_ASSERT(r, 3 == layout.size(*context.fBool3_Type)); |
| 29 | REPORTER_ASSERT(r, 4 == layout.size(*context.fBool4_Type)); |
| 30 | REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x2_Type)); |
| 31 | REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x4_Type)); |
| 32 | REPORTER_ASSERT(r, 48 == layout.size(*context.fFloat3x3_Type)); |
| 33 | REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x2_Type)); |
| 34 | REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 35 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fFloat_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 36 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2_Type)); |
| 37 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3_Type)); |
| 38 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 39 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fInt_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 40 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fInt2_Type)); |
| 41 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt3_Type)); |
| 42 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 43 | REPORTER_ASSERT(r, 1 == layout.alignment(*context.fBool_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 44 | REPORTER_ASSERT(r, 2 == layout.alignment(*context.fBool2_Type)); |
| 45 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool3_Type)); |
| 46 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool4_Type)); |
| 47 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x2_Type)); |
| 48 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x4_Type)); |
| 49 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3x3_Type)); |
| 50 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x2_Type)); |
| 51 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 52 | |
| 53 | // struct 1 |
| 54 | std::vector<SkSL::Type::Field> fields1; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 55 | fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fFloat3_Type.get()); |
| 56 | SkSL::Type s1(-1, SkSL::String("s1"), fields1); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 57 | REPORTER_ASSERT(r, 16 == layout.size(s1)); |
| 58 | REPORTER_ASSERT(r, 16 == layout.alignment(s1)); |
| 59 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 60 | fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat_Type.get()); |
| 61 | SkSL::Type s2(-1, SkSL::String("s2"), fields1); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 62 | REPORTER_ASSERT(r, 16 == layout.size(s2)); |
| 63 | REPORTER_ASSERT(r, 16 == layout.alignment(s2)); |
| 64 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 65 | fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fBool_Type.get()); |
| 66 | SkSL::Type s3(-1, SkSL::String("s3"), fields1); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 67 | REPORTER_ASSERT(r, 32 == layout.size(s3)); |
| 68 | REPORTER_ASSERT(r, 16 == layout.alignment(s3)); |
| 69 | |
| 70 | // struct 2 |
| 71 | std::vector<SkSL::Type::Field> fields2; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 72 | fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fInt_Type.get()); |
| 73 | SkSL::Type s4(-1, SkSL::String("s4"), fields2); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 74 | REPORTER_ASSERT(r, 16 == layout.size(s4)); |
| 75 | REPORTER_ASSERT(r, 16 == layout.alignment(s4)); |
| 76 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 77 | fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat3_Type.get()); |
| 78 | SkSL::Type s5(-1, SkSL::String("s5"), fields2); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 79 | REPORTER_ASSERT(r, 32 == layout.size(s5)); |
| 80 | REPORTER_ASSERT(r, 16 == layout.alignment(s5)); |
| 81 | |
| 82 | // arrays |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 83 | SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 84 | REPORTER_ASSERT(r, 64 == layout.size(array1)); |
| 85 | REPORTER_ASSERT(r, 16 == layout.alignment(array1)); |
| 86 | REPORTER_ASSERT(r, 16 == layout.stride(array1)); |
| 87 | |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 88 | SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 89 | REPORTER_ASSERT(r, 64 == layout.size(array2)); |
| 90 | REPORTER_ASSERT(r, 16 == layout.alignment(array2)); |
| 91 | REPORTER_ASSERT(r, 16 == layout.stride(array2)); |
| 92 | } |
| 93 | |
| 94 | DEF_TEST(SkSLMemoryLayout430Test, r) { |
| 95 | SkSL::Context context; |
| 96 | SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard); |
| 97 | |
| 98 | // basic types |
| 99 | REPORTER_ASSERT(r, 4 == layout.size(*context.fFloat_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 100 | REPORTER_ASSERT(r, 8 == layout.size(*context.fFloat2_Type)); |
| 101 | REPORTER_ASSERT(r, 12 == layout.size(*context.fFloat3_Type)); |
| 102 | REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 103 | REPORTER_ASSERT(r, 4 == layout.size(*context.fInt_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 104 | REPORTER_ASSERT(r, 8 == layout.size(*context.fInt2_Type)); |
| 105 | REPORTER_ASSERT(r, 12 == layout.size(*context.fInt3_Type)); |
| 106 | REPORTER_ASSERT(r, 16 == layout.size(*context.fInt4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 107 | REPORTER_ASSERT(r, 1 == layout.size(*context.fBool_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 108 | REPORTER_ASSERT(r, 2 == layout.size(*context.fBool2_Type)); |
| 109 | REPORTER_ASSERT(r, 3 == layout.size(*context.fBool3_Type)); |
| 110 | REPORTER_ASSERT(r, 4 == layout.size(*context.fBool4_Type)); |
| 111 | REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat2x2_Type)); |
| 112 | REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x4_Type)); |
| 113 | REPORTER_ASSERT(r, 48 == layout.size(*context.fFloat3x3_Type)); |
| 114 | REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat4x2_Type)); |
| 115 | REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 116 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fFloat_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 117 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2_Type)); |
| 118 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3_Type)); |
| 119 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 120 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fInt_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 121 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fInt2_Type)); |
| 122 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt3_Type)); |
| 123 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 124 | REPORTER_ASSERT(r, 1 == layout.alignment(*context.fBool_Type)); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 125 | REPORTER_ASSERT(r, 2 == layout.alignment(*context.fBool2_Type)); |
| 126 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool3_Type)); |
| 127 | REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool4_Type)); |
| 128 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2x2_Type)); |
| 129 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x4_Type)); |
| 130 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3x3_Type)); |
| 131 | REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat4x2_Type)); |
| 132 | REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x4_Type)); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 133 | |
| 134 | // struct 1 |
| 135 | std::vector<SkSL::Type::Field> fields1; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 136 | fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fFloat3_Type.get()); |
| 137 | SkSL::Type s1(-1, SkSL::String("s1"), fields1); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 138 | REPORTER_ASSERT(r, 16 == layout.size(s1)); |
| 139 | REPORTER_ASSERT(r, 16 == layout.alignment(s1)); |
| 140 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 141 | fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat_Type.get()); |
| 142 | SkSL::Type s2(-1, SkSL::String("s2"), fields1); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 143 | REPORTER_ASSERT(r, 16 == layout.size(s2)); |
| 144 | REPORTER_ASSERT(r, 16 == layout.alignment(s2)); |
| 145 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 146 | fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fBool_Type.get()); |
| 147 | SkSL::Type s3(-1, SkSL::String("s3"), fields1); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 148 | REPORTER_ASSERT(r, 32 == layout.size(s3)); |
| 149 | REPORTER_ASSERT(r, 16 == layout.alignment(s3)); |
| 150 | |
| 151 | // struct 2 |
| 152 | std::vector<SkSL::Type::Field> fields2; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 153 | fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fInt_Type.get()); |
| 154 | SkSL::Type s4(-1, SkSL::String("s4"), fields2); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 155 | REPORTER_ASSERT(r, 4 == layout.size(s4)); |
| 156 | REPORTER_ASSERT(r, 4 == layout.alignment(s4)); |
| 157 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 158 | fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat3_Type.get()); |
| 159 | SkSL::Type s5(-1, SkSL::String("s5"), fields2); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 160 | REPORTER_ASSERT(r, 32 == layout.size(s5)); |
| 161 | REPORTER_ASSERT(r, 16 == layout.alignment(s5)); |
| 162 | |
| 163 | // arrays |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 164 | SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 165 | REPORTER_ASSERT(r, 16 == layout.size(array1)); |
| 166 | REPORTER_ASSERT(r, 4 == layout.alignment(array1)); |
| 167 | REPORTER_ASSERT(r, 4 == layout.stride(array1)); |
| 168 | |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 169 | SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 170 | REPORTER_ASSERT(r, 64 == layout.size(array2)); |
| 171 | REPORTER_ASSERT(r, 16 == layout.alignment(array2)); |
| 172 | REPORTER_ASSERT(r, 16 == layout.stride(array2)); |
| 173 | } |