blob: 718ad704b7323a9c616d981d3152c843048b4165 [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 Google LLC
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 "src/gpu/GrDirectContextPriv.h"
9#include "src/gpu/GrGpu.h"
10#include "src/sksl/SkSLIRGenerator.h"
11#include "src/sksl/dsl/DSL.h"
12#include "src/sksl/dsl/priv/DSLWriter.h"
13
14#include "tests/Test.h"
15
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050016#include <limits>
17
Ethan Nicholas95046142021-01-07 10:57:27 -050018using namespace SkSL::dsl;
19
20class AutoDSLContext {
21public:
22 AutoDSLContext(GrGpu* gpu) {
23 Start(gpu->shaderCompiler());
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050024 DSLWriter::Instance().fMangle = false;
Ethan Nicholas95046142021-01-07 10:57:27 -050025 }
26
27 ~AutoDSLContext() {
28 End();
29 }
30};
31
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050032class ExpectError : public ErrorHandler {
33public:
34 ExpectError(skiatest::Reporter* reporter, const char* msg)
35 : fMsg(msg)
36 , fReporter(reporter) {
37 SetErrorHandler(this);
38 }
39
40 ~ExpectError() override {
41 REPORTER_ASSERT(fReporter, !fMsg);
42 SetErrorHandler(nullptr);
43 }
44
45 void handleError(const char* msg) override {
46 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
47 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
48 fMsg = nullptr;
49 }
50
51private:
52 const char* fMsg;
53 skiatest::Reporter* fReporter;
54};
55
56DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050057 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
58 Expression e1 = 1;
59 REPORTER_ASSERT(r, e1.release()->description() == "1");
60 Expression e2 = 1.0;
61 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
62 Expression e3 = true;
63 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050064 Var a(kInt, "a");
65 Expression e4 = a;
66 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholas95046142021-01-07 10:57:27 -050067}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050068
69DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
70 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
71 Expression e1 = Float(std::numeric_limits<float>::max());
72 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
73 std::numeric_limits<float>::max());
74
75 Expression e2 = Float(std::numeric_limits<float>::min());
76 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
77 std::numeric_limits<float>::min());
78
79 Expression e3 = Float2(0);
80 REPORTER_ASSERT(r, e3.release()->description() == "float2(0.0)");
81
82 Expression e4 = Float2(-0.5, 1);
83 REPORTER_ASSERT(r, e4.release()->description() == "float2(-0.5, 1.0)");
84
85 Expression e5 = Float3(0.75);
86 REPORTER_ASSERT(r, e5.release()->description() == "float3(0.75)");
87
88 Expression e6 = Float3(Float2(0, 1), -2);
89 REPORTER_ASSERT(r, e6.release()->description() == "float3(float2(0.0, 1.0), -2.0)");
90
91 Expression e7 = Float3(0, 1, 2);
92 REPORTER_ASSERT(r, e7.release()->description() == "float3(0.0, 1.0, 2.0)");
93
94 Expression e8 = Float4(0);
95 REPORTER_ASSERT(r, e8.release()->description() == "float4(0.0)");
96
97 Expression e9 = Float4(Float2(0, 1), Float2(2, 3));
98 REPORTER_ASSERT(r, e9.release()->description() == "float4(float2(0.0, 1.0), float2(2.0, 3.0))");
99
100 Expression e10 = Float4(0, 1, Float2(2, 3));
101 REPORTER_ASSERT(r, e10.release()->description() == "float4(0.0, 1.0, float2(2.0, 3.0))");
102
103 Expression e11 = Float4(0, 1, 2, 3);
104 REPORTER_ASSERT(r, e11.release()->description() == "float4(0.0, 1.0, 2.0, 3.0)");
105
106 {
107 ExpectError error(r, "error: floating point value is infinite\n");
108 Float(std::numeric_limits<float>::infinity()).release();
109 }
110
111 {
112 ExpectError error(r, "error: floating point value is NaN\n");
113 Float(std::numeric_limits<float>::quiet_NaN()).release();
114 }
115
116 {
117 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
118 " but found 4)\n");
119 Float2(Float4(1)).release();
120 }
121
122 {
123 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
124 " but found 3)\n");
125 Float4(Float3(1)).release();
126 }
127}
128
129DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
130 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
131 Expression e1 = Half(std::numeric_limits<float>::max());
132 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
133 std::numeric_limits<float>::max());
134
135 Expression e2 = Half(std::numeric_limits<float>::min());
136 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
137 std::numeric_limits<float>::min());
138
139 Expression e3 = Half2(0);
140 REPORTER_ASSERT(r, e3.release()->description() == "half2(0.0)");
141
142 Expression e4 = Half2(-0.5, 1);
143 REPORTER_ASSERT(r, e4.release()->description() == "half2(-0.5, 1.0)");
144
145 Expression e5 = Half3(0.75);
146 REPORTER_ASSERT(r, e5.release()->description() == "half3(0.75)");
147
148 Expression e6 = Half3(Half2(0, 1), -2);
149 REPORTER_ASSERT(r, e6.release()->description() == "half3(half2(0.0, 1.0), -2.0)");
150
151 Expression e7 = Half3(0, 1, 2);
152 REPORTER_ASSERT(r, e7.release()->description() == "half3(0.0, 1.0, 2.0)");
153
154 Expression e8 = Half4(0);
155 REPORTER_ASSERT(r, e8.release()->description() == "half4(0.0)");
156
157 Expression e9 = Half4(Half2(0, 1), Half2(2, 3));
158 REPORTER_ASSERT(r, e9.release()->description() == "half4(half2(0.0, 1.0), half2(2.0, 3.0))");
159
160 Expression e10 = Half4(0, 1, Half2(2, 3));
161 REPORTER_ASSERT(r, e10.release()->description() == "half4(0.0, 1.0, half2(2.0, 3.0))");
162
163 Expression e11 = Half4(0, 1, 2, 3);
164 REPORTER_ASSERT(r, e11.release()->description() == "half4(0.0, 1.0, 2.0, 3.0)");
165
166 {
167 ExpectError error(r, "error: floating point value is infinite\n");
168 Half(std::numeric_limits<float>::infinity()).release();
169 }
170
171 {
172 ExpectError error(r, "error: floating point value is NaN\n");
173 Half(std::numeric_limits<float>::quiet_NaN()).release();
174 }
175
176 {
177 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
178 " but found 4)\n");
179 Half2(Half4(1)).release();
180 }
181
182 {
183 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
184 " but found 3)\n");
185 Half4(Half3(1)).release();
186 }
187}
188
189DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
190 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
191 Expression e1 = Int(std::numeric_limits<int32_t>::max());
192 REPORTER_ASSERT(r, e1.release()->description() == "2147483647");
193
194 Expression e2 = Int2(std::numeric_limits<int32_t>::min());
195 REPORTER_ASSERT(r, e2.release()->description() == "int2(-2147483648)");
196
197 Expression e3 = Int2(0, 1);
198 REPORTER_ASSERT(r, e3.release()->description() == "int2(0, 1)");
199
200 Expression e4 = Int3(0);
201 REPORTER_ASSERT(r, e4.release()->description() == "int3(0)");
202
203 Expression e5 = Int3(Int2(0, 1), -2);
204 REPORTER_ASSERT(r, e5.release()->description() == "int3(int2(0, 1), -2)");
205
206 Expression e6 = Int3(0, 1, 2);
207 REPORTER_ASSERT(r, e6.release()->description() == "int3(0, 1, 2)");
208
209 Expression e7 = Int4(0);
210 REPORTER_ASSERT(r, e7.release()->description() == "int4(0)");
211
212 Expression e8 = Int4(Int2(0, 1), Int2(2, 3));
213 REPORTER_ASSERT(r, e8.release()->description() == "int4(int2(0, 1), int2(2, 3))");
214
215 Expression e9 = Int4(0, 1, Int2(2, 3));
216 REPORTER_ASSERT(r, e9.release()->description() == "int4(0, 1, int2(2, 3))");
217
218 Expression e10 = Int4(0, 1, 2, 3);
219 REPORTER_ASSERT(r, e10.release()->description() == "int4(0, 1, 2, 3)");
220
221 {
222 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
223 " but found 4)\n");
224 Int2(Int4(1)).release();
225 }
226
227 {
228 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
229 " but found 3)\n");
230 Int4(Int3(1)).release();
231 }
232}
233
234DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
235 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
236 Expression e1 = Short(std::numeric_limits<int16_t>::max());
237 REPORTER_ASSERT(r, e1.release()->description() == "32767");
238
239 Expression e2 = Short2(std::numeric_limits<int16_t>::min());
240 REPORTER_ASSERT(r, e2.release()->description() == "short2(-32768)");
241
242 Expression e3 = Short2(0, 1);
243 REPORTER_ASSERT(r, e3.release()->description() == "short2(0, 1)");
244
245 Expression e4 = Short3(0);
246 REPORTER_ASSERT(r, e4.release()->description() == "short3(0)");
247
248 Expression e5 = Short3(Short2(0, 1), -2);
249 REPORTER_ASSERT(r, e5.release()->description() == "short3(short2(0, 1), -2)");
250
251 Expression e6 = Short3(0, 1, 2);
252 REPORTER_ASSERT(r, e6.release()->description() == "short3(0, 1, 2)");
253
254 Expression e7 = Short4(0);
255 REPORTER_ASSERT(r, e7.release()->description() == "short4(0)");
256
257 Expression e8 = Short4(Short2(0, 1), Short2(2, 3));
258 REPORTER_ASSERT(r, e8.release()->description() == "short4(short2(0, 1), short2(2, 3))");
259
260 Expression e9 = Short4(0, 1, Short2(2, 3));
261 REPORTER_ASSERT(r, e9.release()->description() == "short4(0, 1, short2(2, 3))");
262
263 Expression e10 = Short4(0, 1, 2, 3);
264 REPORTER_ASSERT(r, e10.release()->description() == "short4(0, 1, 2, 3)");
265
266 {
267 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
268 " but found 4)\n");
269 Short2(Short4(1)).release();
270 }
271
272 {
273 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
274 " but found 3)\n");
275 Short4(Short3(1)).release();
276 }
277}
278
279DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
280 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
281 Expression e1 = Bool2(false);
282 REPORTER_ASSERT(r, e1.release()->description() == "bool2(false)");
283
284 Expression e2 = Bool2(false, true);
285 REPORTER_ASSERT(r, e2.release()->description() == "bool2(false, true)");
286
287 Expression e3 = Bool3(false);
288 REPORTER_ASSERT(r, e3.release()->description() == "bool3(false)");
289
290 Expression e4 = Bool3(Bool2(false, true), false);
291 REPORTER_ASSERT(r, e4.release()->description() == "bool3(bool2(false, true), false)");
292
293 Expression e5 = Bool3(false, true, false);
294 REPORTER_ASSERT(r, e5.release()->description() == "bool3(false, true, false)");
295
296 Expression e6 = Bool4(false);
297 REPORTER_ASSERT(r, e6.release()->description() == "bool4(false)");
298
299 Expression e7 = Bool4(Bool2(false, true), Bool2(false, true));
300 REPORTER_ASSERT(r, e7.release()->description() == "bool4(bool2(false, true), "
301 "bool2(false, true))");
302
303 Expression e8 = Bool4(false, true, Bool2(false, true));
304 REPORTER_ASSERT(r, e8.release()->description() == "bool4(false, true, bool2(false, true))");
305
306 Expression e9 = Bool4(false, true, false, true);
307 REPORTER_ASSERT(r, e9.release()->description() == "bool4(false, true, false, true)");
308
309 {
310 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
311 " but found 4)\n");
312 Bool2(Bool4(true)).release();
313 }
314
315 {
316 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
317 " but found 3)\n");
318 Bool4(Bool3(true)).release();
319 }
320}