blob: a5551782d2fa150066629bcc45229d2726e3ffa5 [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"
John Stilesb30151e2021-01-11 16:13:08 -05009#include "src/sksl/SkSLErrorReporter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLMemoryLayout.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tests/Test.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080013
ethannicholas8ac838d2016-11-22 08:39:36 -080014DEF_TEST(SkSLMemoryLayout140Test, r) {
John Stilesb30151e2021-01-11 16:13:08 -050015 SkSL::TestingOnly_AbortErrorReporter errors;
16 SkSL::Context context(errors);
ethannicholas8ac838d2016-11-22 08:39:36 -080017 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
18
19 // basic types
John Stiles54e7c052021-01-11 14:22:36 -050020 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080054
55 // struct 1
56 std::vector<SkSL::Type::Field> fields1;
John Stiles54e7c052021-01-11 14:22:36 -050057 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"),
58 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -050059 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080062
John Stiles54e7c052021-01-11 14:22:36 -050063 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fTypes.fFloat.get());
John Stilesad2d4942020-12-11 16:55:58 -050064 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080067
John Stiles54e7c052021-01-11 14:22:36 -050068 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fTypes.fBool.get());
John Stilesad2d4942020-12-11 16:55:58 -050069 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080072
73 // struct 2
74 std::vector<SkSL::Type::Field> fields2;
John Stiles54e7c052021-01-11 14:22:36 -050075 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fTypes.fInt.get());
John Stilesad2d4942020-12-11 16:55:58 -050076 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080079
John Stiles54e7c052021-01-11 14:22:36 -050080 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"),
81 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -050082 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080085
86 // arrays
John Stilesad2d4942020-12-11 16:55:58 -050087 std::unique_ptr<SkSL::Type> array1 =
John Stiles54e7c052021-01-11 14:22:36 -050088 SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4);
John Stilesad2d4942020-12-11 16:55:58 -050089 REPORTER_ASSERT(r, 64 == layout.size(*array1));
90 REPORTER_ASSERT(r, 16 == layout.alignment(*array1));
91 REPORTER_ASSERT(r, 16 == layout.stride(*array1));
ethannicholas8ac838d2016-11-22 08:39:36 -080092
John Stilesad2d4942020-12-11 16:55:58 -050093 std::unique_ptr<SkSL::Type> array2 =
John Stiles54e7c052021-01-11 14:22:36 -050094 SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4);
John Stilesad2d4942020-12-11 16:55:58 -050095 REPORTER_ASSERT(r, 64 == layout.size(*array2));
96 REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
97 REPORTER_ASSERT(r, 16 == layout.stride(*array2));
ethannicholas8ac838d2016-11-22 08:39:36 -080098}
99
100DEF_TEST(SkSLMemoryLayout430Test, r) {
John Stilesb30151e2021-01-11 16:13:08 -0500101 SkSL::TestingOnly_AbortErrorReporter errors;
102 SkSL::Context context(errors);
ethannicholas8ac838d2016-11-22 08:39:36 -0800103 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
104
105 // basic types
John Stiles54e7c052021-01-11 14:22:36 -0500106 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800140
141 // struct 1
142 std::vector<SkSL::Type::Field> fields1;
John Stiles54e7c052021-01-11 14:22:36 -0500143 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"),
144 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -0500145 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800148
John Stiles54e7c052021-01-11 14:22:36 -0500149 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fTypes.fFloat.get());
John Stilesad2d4942020-12-11 16:55:58 -0500150 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800153
John Stiles54e7c052021-01-11 14:22:36 -0500154 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fTypes.fBool.get());
John Stilesad2d4942020-12-11 16:55:58 -0500155 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800158
159 // struct 2
160 std::vector<SkSL::Type::Field> fields2;
John Stiles54e7c052021-01-11 14:22:36 -0500161 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fTypes.fInt.get());
John Stilesad2d4942020-12-11 16:55:58 -0500162 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800165
John Stiles54e7c052021-01-11 14:22:36 -0500166 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"),
167 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -0500168 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800171
172 // arrays
John Stilesad2d4942020-12-11 16:55:58 -0500173 std::unique_ptr<SkSL::Type> array1 =
John Stiles54e7c052021-01-11 14:22:36 -0500174 SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4);
John Stilesad2d4942020-12-11 16:55:58 -0500175 REPORTER_ASSERT(r, 16 == layout.size(*array1));
176 REPORTER_ASSERT(r, 4 == layout.alignment(*array1));
177 REPORTER_ASSERT(r, 4 == layout.stride(*array1));
ethannicholas8ac838d2016-11-22 08:39:36 -0800178
John Stilesad2d4942020-12-11 16:55:58 -0500179 std::unique_ptr<SkSL::Type> array2 =
John Stiles54e7c052021-01-11 14:22:36 -0500180 SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4);
John Stilesad2d4942020-12-11 16:55:58 -0500181 REPORTER_ASSERT(r, 64 == layout.size(*array2));
182 REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
183 REPORTER_ASSERT(r, 16 == layout.stride(*array2));
ethannicholas8ac838d2016-11-22 08:39:36 -0800184}