blob: ef1383573e4fe8b4b5d665ed694c8762ada07076 [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
8#include "SkSLContext.h"
9#include "SkSLMemoryLayout.h"
10
11#include "Test.h"
12
13#if SK_SUPPORT_GPU
14
15DEF_TEST(SkSLMemoryLayout140Test, r) {
16 SkSL::Context context;
17 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
18
19 // basic types
20 REPORTER_ASSERT(r, 4 == layout.size(*context.fFloat_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040021 REPORTER_ASSERT(r, 8 == layout.size(*context.fFloat2_Type));
22 REPORTER_ASSERT(r, 12 == layout.size(*context.fFloat3_Type));
23 REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -080024 REPORTER_ASSERT(r, 4 == layout.size(*context.fInt_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040025 REPORTER_ASSERT(r, 8 == layout.size(*context.fInt2_Type));
26 REPORTER_ASSERT(r, 12 == layout.size(*context.fInt3_Type));
27 REPORTER_ASSERT(r, 16 == layout.size(*context.fInt4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -080028 REPORTER_ASSERT(r, 1 == layout.size(*context.fBool_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040029 REPORTER_ASSERT(r, 2 == layout.size(*context.fBool2_Type));
30 REPORTER_ASSERT(r, 3 == layout.size(*context.fBool3_Type));
31 REPORTER_ASSERT(r, 4 == layout.size(*context.fBool4_Type));
32 REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x2_Type));
33 REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x4_Type));
34 REPORTER_ASSERT(r, 48 == layout.size(*context.fFloat3x3_Type));
35 REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x2_Type));
36 REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -080037 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fFloat_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040038 REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2_Type));
39 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3_Type));
40 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -080041 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fInt_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040042 REPORTER_ASSERT(r, 8 == layout.alignment(*context.fInt2_Type));
43 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt3_Type));
44 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -080045 REPORTER_ASSERT(r, 1 == layout.alignment(*context.fBool_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040046 REPORTER_ASSERT(r, 2 == layout.alignment(*context.fBool2_Type));
47 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool3_Type));
48 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool4_Type));
49 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x2_Type));
50 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x4_Type));
51 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3x3_Type));
52 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x2_Type));
53 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -080054
55 // struct 1
56 std::vector<SkSL::Type::Field> fields1;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070057 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fFloat3_Type.get());
58 SkSL::Type s1(-1, SkSL::String("s1"), fields1);
ethannicholas8ac838d2016-11-22 08:39:36 -080059 REPORTER_ASSERT(r, 16 == layout.size(s1));
60 REPORTER_ASSERT(r, 16 == layout.alignment(s1));
61
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070062 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat_Type.get());
63 SkSL::Type s2(-1, SkSL::String("s2"), fields1);
ethannicholas8ac838d2016-11-22 08:39:36 -080064 REPORTER_ASSERT(r, 16 == layout.size(s2));
65 REPORTER_ASSERT(r, 16 == layout.alignment(s2));
66
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070067 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fBool_Type.get());
68 SkSL::Type s3(-1, SkSL::String("s3"), fields1);
ethannicholas8ac838d2016-11-22 08:39:36 -080069 REPORTER_ASSERT(r, 32 == layout.size(s3));
70 REPORTER_ASSERT(r, 16 == layout.alignment(s3));
71
72 // struct 2
73 std::vector<SkSL::Type::Field> fields2;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070074 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fInt_Type.get());
75 SkSL::Type s4(-1, SkSL::String("s4"), fields2);
ethannicholas8ac838d2016-11-22 08:39:36 -080076 REPORTER_ASSERT(r, 16 == layout.size(s4));
77 REPORTER_ASSERT(r, 16 == layout.alignment(s4));
78
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070079 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat3_Type.get());
80 SkSL::Type s5(-1, SkSL::String("s5"), fields2);
ethannicholas8ac838d2016-11-22 08:39:36 -080081 REPORTER_ASSERT(r, 32 == layout.size(s5));
82 REPORTER_ASSERT(r, 16 == layout.alignment(s5));
83
84 // arrays
Brian Osman93ba0a42017-08-14 14:48:10 -040085 SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
ethannicholas8ac838d2016-11-22 08:39:36 -080086 REPORTER_ASSERT(r, 64 == layout.size(array1));
87 REPORTER_ASSERT(r, 16 == layout.alignment(array1));
88 REPORTER_ASSERT(r, 16 == layout.stride(array1));
89
Brian Osman93ba0a42017-08-14 14:48:10 -040090 SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
ethannicholas8ac838d2016-11-22 08:39:36 -080091 REPORTER_ASSERT(r, 64 == layout.size(array2));
92 REPORTER_ASSERT(r, 16 == layout.alignment(array2));
93 REPORTER_ASSERT(r, 16 == layout.stride(array2));
94}
95
96DEF_TEST(SkSLMemoryLayout430Test, r) {
97 SkSL::Context context;
98 SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
99
100 // basic types
101 REPORTER_ASSERT(r, 4 == layout.size(*context.fFloat_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400102 REPORTER_ASSERT(r, 8 == layout.size(*context.fFloat2_Type));
103 REPORTER_ASSERT(r, 12 == layout.size(*context.fFloat3_Type));
104 REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -0800105 REPORTER_ASSERT(r, 4 == layout.size(*context.fInt_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400106 REPORTER_ASSERT(r, 8 == layout.size(*context.fInt2_Type));
107 REPORTER_ASSERT(r, 12 == layout.size(*context.fInt3_Type));
108 REPORTER_ASSERT(r, 16 == layout.size(*context.fInt4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -0800109 REPORTER_ASSERT(r, 1 == layout.size(*context.fBool_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400110 REPORTER_ASSERT(r, 2 == layout.size(*context.fBool2_Type));
111 REPORTER_ASSERT(r, 3 == layout.size(*context.fBool3_Type));
112 REPORTER_ASSERT(r, 4 == layout.size(*context.fBool4_Type));
113 REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat2x2_Type));
114 REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x4_Type));
115 REPORTER_ASSERT(r, 48 == layout.size(*context.fFloat3x3_Type));
116 REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat4x2_Type));
117 REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -0800118 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fFloat_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400119 REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2_Type));
120 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3_Type));
121 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -0800122 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fInt_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400123 REPORTER_ASSERT(r, 8 == layout.alignment(*context.fInt2_Type));
124 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt3_Type));
125 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -0800126 REPORTER_ASSERT(r, 1 == layout.alignment(*context.fBool_Type));
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400127 REPORTER_ASSERT(r, 2 == layout.alignment(*context.fBool2_Type));
128 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool3_Type));
129 REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool4_Type));
130 REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2x2_Type));
131 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x4_Type));
132 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3x3_Type));
133 REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat4x2_Type));
134 REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x4_Type));
ethannicholas8ac838d2016-11-22 08:39:36 -0800135
136 // struct 1
137 std::vector<SkSL::Type::Field> fields1;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700138 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fFloat3_Type.get());
139 SkSL::Type s1(-1, SkSL::String("s1"), fields1);
ethannicholas8ac838d2016-11-22 08:39:36 -0800140 REPORTER_ASSERT(r, 16 == layout.size(s1));
141 REPORTER_ASSERT(r, 16 == layout.alignment(s1));
142
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700143 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat_Type.get());
144 SkSL::Type s2(-1, SkSL::String("s2"), fields1);
ethannicholas8ac838d2016-11-22 08:39:36 -0800145 REPORTER_ASSERT(r, 16 == layout.size(s2));
146 REPORTER_ASSERT(r, 16 == layout.alignment(s2));
147
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700148 fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fBool_Type.get());
149 SkSL::Type s3(-1, SkSL::String("s3"), fields1);
ethannicholas8ac838d2016-11-22 08:39:36 -0800150 REPORTER_ASSERT(r, 32 == layout.size(s3));
151 REPORTER_ASSERT(r, 16 == layout.alignment(s3));
152
153 // struct 2
154 std::vector<SkSL::Type::Field> fields2;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700155 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fInt_Type.get());
156 SkSL::Type s4(-1, SkSL::String("s4"), fields2);
ethannicholas8ac838d2016-11-22 08:39:36 -0800157 REPORTER_ASSERT(r, 4 == layout.size(s4));
158 REPORTER_ASSERT(r, 4 == layout.alignment(s4));
159
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700160 fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat3_Type.get());
161 SkSL::Type s5(-1, SkSL::String("s5"), fields2);
ethannicholas8ac838d2016-11-22 08:39:36 -0800162 REPORTER_ASSERT(r, 32 == layout.size(s5));
163 REPORTER_ASSERT(r, 16 == layout.alignment(s5));
164
165 // arrays
Brian Osman93ba0a42017-08-14 14:48:10 -0400166 SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
ethannicholas8ac838d2016-11-22 08:39:36 -0800167 REPORTER_ASSERT(r, 16 == layout.size(array1));
168 REPORTER_ASSERT(r, 4 == layout.alignment(array1));
169 REPORTER_ASSERT(r, 4 == layout.stride(array1));
170
Brian Osman93ba0a42017-08-14 14:48:10 -0400171 SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
ethannicholas8ac838d2016-11-22 08:39:36 -0800172 REPORTER_ASSERT(r, 64 == layout.size(array2));
173 REPORTER_ASSERT(r, 16 == layout.alignment(array2));
174 REPORTER_ASSERT(r, 16 == layout.stride(array2));
175}
176#endif