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