blob: a6fb79dd0845c1a4d628bde42d843f7d7cc65dba [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
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04008#include "include/sksl/SkSLErrorReporter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/sksl/SkSLContext.h"
John Stiles2dda4b62021-09-16 13:18:10 -040010#include "src/sksl/SkSLMangler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/SkSLMemoryLayout.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "tests/Test.h"
ethannicholas8ac838d2016-11-22 08:39:36 -080014
ethannicholas8ac838d2016-11-22 08:39:36 -080015DEF_TEST(SkSLMemoryLayout140Test, r) {
John Stilesb30151e2021-01-11 16:13:08 -050016 SkSL::TestingOnly_AbortErrorReporter errors;
John Stiles2dda4b62021-09-16 13:18:10 -040017 GrShaderCaps caps(GrContextOptions{});
18 SkSL::Mangler mangler;
19 SkSL::Context context(errors, caps, mangler);
ethannicholas8ac838d2016-11-22 08:39:36 -080020 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
21
22 // basic types
John Stiles54e7c052021-01-11 14:22:36 -050023 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080057
58 // struct 1
59 std::vector<SkSL::Type::Field> fields1;
Ethan Nicholas962dec42021-06-10 13:06:39 -040060 fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("a"),
John Stiles54e7c052021-01-11 14:22:36 -050061 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -050062 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080065
Ethan Nicholas962dec42021-06-10 13:06:39 -040066 fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("b"), context.fTypes.fFloat.get());
John Stilesad2d4942020-12-11 16:55:58 -050067 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080070
Ethan Nicholas962dec42021-06-10 13:06:39 -040071 fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("c"), context.fTypes.fBool.get());
John Stilesad2d4942020-12-11 16:55:58 -050072 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080075
76 // struct 2
77 std::vector<SkSL::Type::Field> fields2;
Ethan Nicholas962dec42021-06-10 13:06:39 -040078 fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("a"), context.fTypes.fInt.get());
John Stilesad2d4942020-12-11 16:55:58 -050079 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080082
Ethan Nicholas962dec42021-06-10 13:06:39 -040083 fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("b"),
John Stiles54e7c052021-01-11 14:22:36 -050084 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -050085 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));
ethannicholas8ac838d2016-11-22 08:39:36 -080088
89 // arrays
John Stilesad2d4942020-12-11 16:55:58 -050090 std::unique_ptr<SkSL::Type> array1 =
John Stiles54e7c052021-01-11 14:22:36 -050091 SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4);
John Stilesad2d4942020-12-11 16:55:58 -050092 REPORTER_ASSERT(r, 64 == layout.size(*array1));
93 REPORTER_ASSERT(r, 16 == layout.alignment(*array1));
94 REPORTER_ASSERT(r, 16 == layout.stride(*array1));
ethannicholas8ac838d2016-11-22 08:39:36 -080095
John Stilesad2d4942020-12-11 16:55:58 -050096 std::unique_ptr<SkSL::Type> array2 =
John Stiles54e7c052021-01-11 14:22:36 -050097 SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4);
John Stilesad2d4942020-12-11 16:55:58 -050098 REPORTER_ASSERT(r, 64 == layout.size(*array2));
99 REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
100 REPORTER_ASSERT(r, 16 == layout.stride(*array2));
ethannicholas8ac838d2016-11-22 08:39:36 -0800101}
102
103DEF_TEST(SkSLMemoryLayout430Test, r) {
John Stilesb30151e2021-01-11 16:13:08 -0500104 SkSL::TestingOnly_AbortErrorReporter errors;
John Stiles2dda4b62021-09-16 13:18:10 -0400105 GrShaderCaps caps(GrContextOptions{});
106 SkSL::Mangler mangler;
107 SkSL::Context context(errors, caps, mangler);
ethannicholas8ac838d2016-11-22 08:39:36 -0800108 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
109
110 // basic types
John Stiles54e7c052021-01-11 14:22:36 -0500111 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800145
146 // struct 1
147 std::vector<SkSL::Type::Field> fields1;
Ethan Nicholas962dec42021-06-10 13:06:39 -0400148 fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("a"),
John Stiles54e7c052021-01-11 14:22:36 -0500149 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -0500150 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800153
Ethan Nicholas962dec42021-06-10 13:06:39 -0400154 fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("b"), context.fTypes.fFloat.get());
John Stilesad2d4942020-12-11 16:55:58 -0500155 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800158
Ethan Nicholas962dec42021-06-10 13:06:39 -0400159 fields1.emplace_back(SkSL::Modifiers(), skstd::string_view("c"), context.fTypes.fBool.get());
John Stilesad2d4942020-12-11 16:55:58 -0500160 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800163
164 // struct 2
165 std::vector<SkSL::Type::Field> fields2;
Ethan Nicholas962dec42021-06-10 13:06:39 -0400166 fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("a"), context.fTypes.fInt.get());
John Stilesad2d4942020-12-11 16:55:58 -0500167 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800170
Ethan Nicholas962dec42021-06-10 13:06:39 -0400171 fields2.emplace_back(SkSL::Modifiers(), skstd::string_view("b"),
John Stiles54e7c052021-01-11 14:22:36 -0500172 context.fTypes.fFloat3.get());
John Stilesad2d4942020-12-11 16:55:58 -0500173 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));
ethannicholas8ac838d2016-11-22 08:39:36 -0800176
177 // arrays
John Stilesad2d4942020-12-11 16:55:58 -0500178 std::unique_ptr<SkSL::Type> array1 =
John Stiles54e7c052021-01-11 14:22:36 -0500179 SkSL::Type::MakeArrayType(SkSL::String("float[4]"), *context.fTypes.fFloat, 4);
John Stilesad2d4942020-12-11 16:55:58 -0500180 REPORTER_ASSERT(r, 16 == layout.size(*array1));
181 REPORTER_ASSERT(r, 4 == layout.alignment(*array1));
182 REPORTER_ASSERT(r, 4 == layout.stride(*array1));
ethannicholas8ac838d2016-11-22 08:39:36 -0800183
John Stilesad2d4942020-12-11 16:55:58 -0500184 std::unique_ptr<SkSL::Type> array2 =
John Stiles54e7c052021-01-11 14:22:36 -0500185 SkSL::Type::MakeArrayType(SkSL::String("float4[4]"), *context.fTypes.fFloat4, 4);
John Stilesad2d4942020-12-11 16:55:58 -0500186 REPORTER_ASSERT(r, 64 == layout.size(*array2));
187 REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
188 REPORTER_ASSERT(r, 16 == layout.stride(*array2));
ethannicholas8ac838d2016-11-22 08:39:36 -0800189}