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