blob: 0bb83a6c30485d0282fd731b9be172c93aef4f6c [file] [log] [blame]
ethannicholas8ac838d2016-11-22 08:39:36 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLContext.h"
9#include "src/sksl/SkSLMemoryLayout.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "tests/Test.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080012
ethannicholas8ac838d2016-11-22 08:39:36 -080013DEF_TEST(SkSLMemoryLayout140Test, r) {
14 SkSL::Context context;
15 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
16
17 // basic types
John Stiles54e7c052021-01-11 14:22:36 -050018 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080052
53 // struct 1
54 std::vector<SkSL::Type::Field> fields1;
John Stiles54e7c052021-01-11 14:22:36 -050055 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"),
56 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -050057 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080060
John Stiles54e7c052021-01-11 14:22:36 -050061 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fTypes.fFloat.get());
John Stilesad2d4942020-12-11 16:55:58 -050062 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080065
John Stiles54e7c052021-01-11 14:22:36 -050066 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fTypes.fBool.get());
John Stilesad2d4942020-12-11 16:55:58 -050067 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080070
71 // struct 2
72 std::vector<SkSL::Type::Field> fields2;
John Stiles54e7c052021-01-11 14:22:36 -050073 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fTypes.fInt.get());
John Stilesad2d4942020-12-11 16:55:58 -050074 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080077
John Stiles54e7c052021-01-11 14:22:36 -050078 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"),
79 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -050080 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080083
84 // arrays
John Stilesad2d4942020-12-11 16:55:58 -050085 std::unique_ptr<SkSL::Type> array1 =
John Stiles54e7c052021-01-11 14:22:36 -050086 SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4);
John Stilesad2d4942020-12-11 16:55:58 -050087 REPORTER_ASSERT(r, 64 == layout.size(*array1));
88 REPORTER_ASSERT(r, 16 == layout.alignment(*array1));
89 REPORTER_ASSERT(r, 16 == layout.stride(*array1));
ethannicholas8ac838d2016-11-22 08:39:36 -080090
John Stilesad2d4942020-12-11 16:55:58 -050091 std::unique_ptr<SkSL::Type> array2 =
John Stiles54e7c052021-01-11 14:22:36 -050092 SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4);
John Stilesad2d4942020-12-11 16:55:58 -050093 REPORTER_ASSERT(r, 64 == layout.size(*array2));
94 REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
95 REPORTER_ASSERT(r, 16 == layout.stride(*array2));
ethannicholas8ac838d2016-11-22 08:39:36 -080096}
97
98DEF_TEST(SkSLMemoryLayout430Test, r) {
99 SkSL::Context context;
100 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
101
102 // basic types
John Stiles54e7c052021-01-11 14:22:36 -0500103 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800137
138 // struct 1
139 std::vector<SkSL::Type::Field> fields1;
John Stiles54e7c052021-01-11 14:22:36 -0500140 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"),
141 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -0500142 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800145
John Stiles54e7c052021-01-11 14:22:36 -0500146 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fTypes.fFloat.get());
John Stilesad2d4942020-12-11 16:55:58 -0500147 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800150
John Stiles54e7c052021-01-11 14:22:36 -0500151 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fTypes.fBool.get());
John Stilesad2d4942020-12-11 16:55:58 -0500152 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800155
156 // struct 2
157 std::vector<SkSL::Type::Field> fields2;
John Stiles54e7c052021-01-11 14:22:36 -0500158 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fTypes.fInt.get());
John Stilesad2d4942020-12-11 16:55:58 -0500159 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800162
John Stiles54e7c052021-01-11 14:22:36 -0500163 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"),
164 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -0500165 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800168
169 // arrays
John Stilesad2d4942020-12-11 16:55:58 -0500170 std::unique_ptr<SkSL::Type> array1 =
John Stiles54e7c052021-01-11 14:22:36 -0500171 SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4);
John Stilesad2d4942020-12-11 16:55:58 -0500172 REPORTER_ASSERT(r, 16 == layout.size(*array1));
173 REPORTER_ASSERT(r, 4 == layout.alignment(*array1));
174 REPORTER_ASSERT(r, 4 == layout.stride(*array1));
ethannicholas8ac838d2016-11-22 08:39:36 -0800175
John Stilesad2d4942020-12-11 16:55:58 -0500176 std::unique_ptr<SkSL::Type> array2 =
John Stiles54e7c052021-01-11 14:22:36 -0500177 SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4);
John Stilesad2d4942020-12-11 16:55:58 -0500178 REPORTER_ASSERT(r, 64 == layout.size(*array2));
179 REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
180 REPORTER_ASSERT(r, 16 == layout.stride(*array2));
ethannicholas8ac838d2016-11-22 08:39:36 -0800181}