SkSLInterpreterTest.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * Copyright 2019 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. #include "include/core/SkPoint3.h"
  8. #include "src/sksl/SkSLByteCode.h"
  9. #include "src/sksl/SkSLCompiler.h"
  10. #include "src/sksl/SkSLExternalValue.h"
  11. #include "src/utils/SkJSON.h"
  12. #include "tests/Test.h"
  13. static bool nearly_equal(const float a[], const float b[], int count) {
  14. for (int i = 0; i < count; ++i) {
  15. if (!SkScalarNearlyEqual(a[i], b[i])) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. void test(skiatest::Reporter* r, const char* src, float* in, int expectedCount, float* expected,
  22. bool exactCompare = true) {
  23. SkSL::Compiler compiler;
  24. SkSL::Program::Settings settings;
  25. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
  26. SkSL::Program::kGeneric_Kind,
  27. SkSL::String(src), settings);
  28. REPORTER_ASSERT(r, program);
  29. if (program) {
  30. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  31. program.reset();
  32. REPORTER_ASSERT(r, !compiler.errorCount());
  33. if (compiler.errorCount() > 0) {
  34. printf("%s\n%s", src, compiler.errorText().c_str());
  35. return;
  36. }
  37. SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
  38. std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[expectedCount]);
  39. SkAssertResult(byteCode->run(main, in, out.get(), 1, nullptr, 0));
  40. bool valid = exactCompare ? !memcmp(out.get(), expected, sizeof(float) * expectedCount)
  41. : nearly_equal(out.get(), expected, expectedCount);
  42. if (!valid) {
  43. printf("for program: %s\n", src);
  44. printf(" expected (");
  45. const char* separator = "";
  46. for (int i = 0; i < expectedCount; ++i) {
  47. printf("%s%f", separator, expected[i]);
  48. separator = ", ";
  49. }
  50. printf("), but received (");
  51. separator = "";
  52. for (int i = 0; i < expectedCount; ++i) {
  53. printf("%s%f", separator, out.get()[i]);
  54. separator = ", ";
  55. }
  56. printf(")\n");
  57. main->disassemble();
  58. }
  59. REPORTER_ASSERT(r, valid);
  60. } else {
  61. printf("%s\n%s", src, compiler.errorText().c_str());
  62. }
  63. }
  64. void vec_test(skiatest::Reporter* r, const char* src) {
  65. // Test on four different vectors (with varying orderings to get divergent control flow)
  66. const float input[16] = { 1, 2, 3, 4,
  67. 4, 3, 2, 1,
  68. 7, 5, 8, 6,
  69. 6, 8, 5, 7 };
  70. SkSL::Compiler compiler;
  71. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
  72. SkSL::Program::kGeneric_Kind, SkSL::String(src), SkSL::Program::Settings());
  73. if (!program) {
  74. REPORT_FAILURE(r, "!program", SkString(compiler.errorText().c_str()));
  75. return;
  76. }
  77. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  78. if (compiler.errorCount() > 0) {
  79. REPORT_FAILURE(r, "!toByteCode", SkString(compiler.errorText().c_str()));
  80. return;
  81. }
  82. const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
  83. float out_s[16], out_v[16];
  84. memcpy(out_s, input, sizeof(out_s));
  85. memcpy(out_v, input, sizeof(out_v));
  86. // First run in scalar mode to determine the expected output
  87. for (int i = 0; i < 4; ++i) {
  88. SkAssertResult(byteCode->run(main, out_s + i * 4, nullptr, 1, nullptr, 0));
  89. }
  90. // Now run in parallel and compare results
  91. SkAssertResult(byteCode->run(main, out_v, nullptr, 4, nullptr, 0));
  92. if (memcmp(out_s, out_v, sizeof(out_s)) != 0) {
  93. printf("for program: %s\n", src);
  94. for (int i = 0; i < 4; ++i) {
  95. printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n",
  96. input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3],
  97. out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3],
  98. out_s[4*i + 0], out_s[4*i + 1], out_s[4*i + 2], out_s[4*i + 3]);
  99. }
  100. main->disassemble();
  101. REPORT_FAILURE(r, "VecInterpreter mismatch", SkString());
  102. }
  103. }
  104. void test(skiatest::Reporter* r, const char* src, float inR, float inG, float inB, float inA,
  105. float expectedR, float expectedG, float expectedB, float expectedA) {
  106. SkSL::Compiler compiler;
  107. SkSL::Program::Settings settings;
  108. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
  109. SkSL::Program::kGeneric_Kind,
  110. SkSL::String(src), settings);
  111. REPORTER_ASSERT(r, program);
  112. if (program) {
  113. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  114. program.reset();
  115. REPORTER_ASSERT(r, !compiler.errorCount());
  116. if (compiler.errorCount() > 0) {
  117. printf("%s\n%s", src, compiler.errorText().c_str());
  118. return;
  119. }
  120. const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
  121. float inoutColor[4] = { inR, inG, inB, inA };
  122. SkAssertResult(byteCode->run(main, inoutColor, nullptr, 1, nullptr, 0));
  123. if (inoutColor[0] != expectedR || inoutColor[1] != expectedG ||
  124. inoutColor[2] != expectedB || inoutColor[3] != expectedA) {
  125. printf("for program: %s\n", src);
  126. printf(" expected (%f, %f, %f, %f), but received (%f, %f, %f, %f)\n", expectedR,
  127. expectedG, expectedB, expectedA, inoutColor[0], inoutColor[1], inoutColor[2],
  128. inoutColor[3]);
  129. main->disassemble();
  130. }
  131. REPORTER_ASSERT(r, inoutColor[0] == expectedR);
  132. REPORTER_ASSERT(r, inoutColor[1] == expectedG);
  133. REPORTER_ASSERT(r, inoutColor[2] == expectedB);
  134. REPORTER_ASSERT(r, inoutColor[3] == expectedA);
  135. } else {
  136. printf("%s\n%s", src, compiler.errorText().c_str());
  137. }
  138. // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc.
  139. vec_test(r, src);
  140. }
  141. DEF_TEST(SkSLInterpreterAdd, r) {
  142. test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
  143. 0.75, 0, 0);
  144. test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
  145. test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
  146. 0.5, 1, 1.5, 2);
  147. test(r, "void main(inout half4 color) { int a = 1; int b = 3; color.r = a + b; }", 1, 2, 3, 4,
  148. 4, 2, 3, 4);
  149. }
  150. DEF_TEST(SkSLInterpreterSubtract, r) {
  151. test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
  152. 0.75, 0, 0);
  153. test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
  154. test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
  155. 0, 0, 0, 0);
  156. test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
  157. test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
  158. test(r, "void main(inout half4 color) { int a = 3; int b = 1; color.r = a - b; }", 0, 0, 0, 0,
  159. 2, 0, 0, 0);
  160. }
  161. DEF_TEST(SkSLInterpreterMultiply, r) {
  162. test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
  163. 0);
  164. test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
  165. 20);
  166. test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
  167. 16, 9, 4, 1);
  168. test(r, "void main(inout half4 color) { int a = 3; int b = -2; color.r = a * b; }", 0, 0, 0, 0,
  169. -6, 0, 0, 0);
  170. }
  171. DEF_TEST(SkSLInterpreterDivide, r) {
  172. test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
  173. 0);
  174. test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
  175. 4, 3);
  176. test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
  177. 1, 1, 1, 1);
  178. test(r, "void main(inout half4 color) { int a = 8; int b = -2; color.r = a / b; }", 0, 0, 0, 0,
  179. -4, 0, 0, 0);
  180. }
  181. DEF_TEST(SkSLInterpreterRemainder, r) {
  182. test(r, "void main(inout half4 color) { color.r = color.r % color.g; }", 3.125, 2, 0, 0,
  183. 1.125, 2, 0, 0);
  184. test(r, "void main(inout half4 color) { color %= half4(1, 2, 3, 4); }", 9.5, 9.5, 9.5, 9.5,
  185. 0.5, 1.5, 0.5, 1.5);
  186. test(r, "void main(inout half4 color) { int a = 8; int b = 3; a %= b; color.r = a; }", 0, 0, 0,
  187. 0, 2, 0, 0, 0);
  188. test(r, "void main(inout half4 color) { int a = 8; int b = 3; color.r = a % b; }", 0, 0, 0, 0,
  189. 2, 0, 0, 0);
  190. test(r, "void main(inout half4 color) { int2 a = int2(8, 10); a %= 6; color.rg = a; }", 0, 0, 0,
  191. 0, 2, 4, 0, 0);
  192. }
  193. DEF_TEST(SkSLInterpreterMatrix, r) {
  194. float in[16];
  195. float expected[16];
  196. // Constructing matrix from scalar produces a diagonal matrix
  197. in[0] = 1.0f;
  198. expected[0] = 2.0f;
  199. test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }",
  200. in, 1, expected);
  201. // With non-square matrix
  202. test(r, "float main(float x) { float3x2 m = float3x2(x); return m[0][0] + m[1][1] + m[2][1]; }",
  203. in, 1, expected);
  204. // Constructing from a different-sized matrix fills the remaining space with the identity matrix
  205. test(r, "float main(float x) {"
  206. "float3x2 m = float3x2(x);"
  207. "float4x4 m2 = float4x4(m);"
  208. "return m2[0][0] + m2[3][3]; }",
  209. in, 1, expected);
  210. // Constructing a matrix from vectors or scalars fills in values in column-major order
  211. in[0] = 1.0f;
  212. in[1] = 2.0f;
  213. in[2] = 4.0f;
  214. in[3] = 8.0f;
  215. expected[0] = 6.0f;
  216. test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
  217. in, 1, expected);
  218. expected[0] = 10.0f;
  219. test(r, "float main(float4 v) {"
  220. "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
  221. "return m[0][1] + m[1][0]; }",
  222. in, 1, expected);
  223. // Initialize 16 values to be used as inputs to matrix tests
  224. for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
  225. // M+M, M-S, S-M
  226. for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
  227. test(r, "float4x4 main(float4x4 m) { return m + m; }", in, 16, expected);
  228. for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
  229. test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, 16, expected);
  230. test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, 16, expected);
  231. // M-M, M-S, S-M
  232. for (int i = 0; i < 8; ++i) { expected[i] = 8.0f; }
  233. test(r, "float4x2 main(float4x2 m1, float4x2 m2) { return m2 - m1; }", in, 8, expected);
  234. for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
  235. test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, 16, expected);
  236. for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
  237. test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, 16, expected);
  238. // M*S, S*M, M/S, S/M
  239. for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
  240. test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, 16, expected);
  241. test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, 16, expected);
  242. for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
  243. test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, 16, expected);
  244. for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
  245. test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, 16, expected);
  246. #if 0
  247. // Matrix negation - legal in GLSL, not in SkSL?
  248. for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
  249. test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected);
  250. #endif
  251. // M*V, V*M
  252. for (int i = 0; i < 4; ++i) {
  253. expected[i] = 12.0f*i + 13.0f*(i+4) + 14.0f*(i+8);
  254. }
  255. test(r, "float4 main(float3x4 m, float3 v) { return m * v; }", in, 4, expected);
  256. for (int i = 0; i < 4; ++i) {
  257. expected[i] = 12.0f*(3*i) + 13.0f*(3*i+1) + 14.0f*(3*i+2);
  258. }
  259. test(r, "float4 main(float4x3 m, float3 v) { return v * m; }", in, 4, expected);
  260. // M*M
  261. {
  262. SkMatrix44 m;
  263. m.setColMajorf(in);
  264. SkMatrix44 m2;
  265. for (int i = 0; i < 16; ++i) {
  266. m2.set(i % 4, i / 4, (i + 4) % 16);
  267. }
  268. m.setConcat(m, m2);
  269. // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
  270. test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
  271. in, 16, (float*)&m);
  272. }
  273. }
  274. DEF_TEST(SkSLInterpreterTernary, r) {
  275. test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
  276. 0, 1, 2, 0, 2, 1, 2, 0);
  277. test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
  278. 0, 3, 2, 0, 3, 3, 2, 0);
  279. }
  280. DEF_TEST(SkSLInterpreterCast, r) {
  281. union Val {
  282. float f;
  283. uint32_t u;
  284. int32_t s;
  285. };
  286. Val input[2];
  287. Val expected[2];
  288. input[0].s = 3;
  289. input[1].s = -5;
  290. expected[0].f = 3.0f;
  291. expected[1].f = -5.0f;
  292. test(r, "float main(int x) { return float (x); }", (float*)input, 1, (float*)expected);
  293. test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, 2, (float*)expected);
  294. input[0].u = 3;
  295. input[1].u = 5;
  296. expected[0].f = 3.0f;
  297. expected[1].f = 5.0f;
  298. test(r, "float main(uint x) { return float (x); }", (float*)input, 1, (float*)expected);
  299. test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, 2, (float*)expected);
  300. input[0].f = 3.0f;
  301. input[1].f = -5.0f;
  302. expected[0].s = 3;
  303. expected[1].s = -5;
  304. test(r, "int main(float x) { return int (x); }", (float*)input, 1, (float*)expected);
  305. test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, 2, (float*)expected);
  306. input[0].s = 3;
  307. expected[0].f = 3.0f;
  308. expected[1].f = 3.0f;
  309. test(r, "float2 main(int x) { return float2(x); }", (float*)input, 2, (float*)expected);
  310. }
  311. DEF_TEST(SkSLInterpreterIf, r) {
  312. test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
  313. 5, 3, 0, 1);
  314. test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
  315. 5, 5, 0, 0);
  316. test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
  317. 5, 6, 0, 0);
  318. test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
  319. 3, 5, 0, 1);
  320. test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
  321. 5, 5, 0, 0);
  322. test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
  323. 6, 5, 0, 0);
  324. test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
  325. 5, 3, 0, 1);
  326. test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
  327. 5, 5, 0, 1);
  328. test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
  329. 5, 6, 0, 0);
  330. test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
  331. 3, 5, 0, 1);
  332. test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
  333. 5, 5, 0, 1);
  334. test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
  335. 6, 5, 0, 0);
  336. test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
  337. 2, 2, 0, 1);
  338. test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
  339. 2, -2, 0, 0);
  340. test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
  341. 2, 2, 0, 0);
  342. test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
  343. 2, -2, 0, 1);
  344. test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
  345. "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
  346. test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
  347. "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
  348. }
  349. DEF_TEST(SkSLInterpreterIfVector, r) {
  350. test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
  351. 1, 2, 1, 2, 1, 2, 1, 1);
  352. test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
  353. 1, 2, 3, 2, 1, 2, 3, 2);
  354. test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
  355. 1, 2, 1, 2, 1, 2, 1, 2);
  356. test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
  357. 1, 2, 3, 2, 1, 2, 3, 1);
  358. }
  359. DEF_TEST(SkSLInterpreterWhile, r) {
  360. test(r, "void main(inout half4 color) { while (color.r < 8) { color.r++; } }",
  361. 1, 2, 3, 4, 8, 2, 3, 4);
  362. test(r, "void main(inout half4 color) { while (color.r < 1) color.r += 0.25; }", 0, 0, 0, 0, 1,
  363. 0, 0, 0);
  364. test(r, "void main(inout half4 color) { while (color.r > 1) color.r -= 0.25; }", 0, 0, 0, 0, 0,
  365. 0, 0, 0);
  366. test(r, "void main(inout half4 color) { while (true) { color.r += 0.5; "
  367. "if (color.r > 5) break; } }", 0, 0, 0, 0, 5.5, 0, 0, 0);
  368. test(r, "void main(inout half4 color) { while (color.r < 10) { color.r += 0.5; "
  369. "if (color.r < 5) continue; break; } }", 0, 0, 0, 0, 5, 0, 0, 0);
  370. test(r,
  371. "void main(inout half4 color) {"
  372. " while (true) {"
  373. " if (color.r > 4) { break; }"
  374. " while (true) { color.a = 1; break; }"
  375. " break;"
  376. " }"
  377. "}",
  378. 6, 5, 4, 3, 6, 5, 4, 3);
  379. }
  380. DEF_TEST(SkSLInterpreterDo, r) {
  381. test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r < 1); }", 0, 0, 0, 0,
  382. 1, 0, 0, 0);
  383. test(r, "void main(inout half4 color) { do color.r -= 0.25; while (color.r > 1); }", 0, 0, 0, 0,
  384. -0.25, 0, 0, 0);
  385. test(r, "void main(inout half4 color) { do { color.r += 0.5; if (color.r > 1) break; } while "
  386. "(true); }", 0, 0, 0, 0, 1.5, 0, 0, 0);
  387. test(r, "void main(inout half4 color) {do { color.r += 0.5; if (color.r < 5) "
  388. "continue; if (color.r >= 5) break; } while (true); }", 0, 0, 0, 0, 5, 0, 0, 0);
  389. test(r, "void main(inout half4 color) { do { color.r += 0.5; } while (false); }",
  390. 0, 0, 0, 0, 0.5, 0, 0, 0);
  391. }
  392. DEF_TEST(SkSLInterpreterFor, r) {
  393. test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += i; }", 0, 0, 0,
  394. 0, 55, 0, 0, 0);
  395. test(r,
  396. "void main(inout half4 color) {"
  397. " for (int i = 1; i <= 10; ++i)"
  398. " for (int j = i; j <= 10; ++j)"
  399. " color.r += j;"
  400. "}",
  401. 0, 0, 0, 0,
  402. 385, 0, 0, 0);
  403. test(r,
  404. "void main(inout half4 color) {"
  405. " for (int i = 1; i <= 10; ++i)"
  406. " for (int j = 1; ; ++j) {"
  407. " if (i == j) continue;"
  408. " if (j > 10) break;"
  409. " color.r += j;"
  410. " }"
  411. "}",
  412. 0, 0, 0, 0,
  413. 495, 0, 0, 0);
  414. }
  415. DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
  416. test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
  417. test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
  418. }
  419. DEF_TEST(SkSLInterpreterSwizzle, r) {
  420. test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
  421. test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
  422. 6, 4);
  423. test(r, "void main(inout half4 color) { color.bgr = int3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
  424. 5, 4);
  425. }
  426. DEF_TEST(SkSLInterpreterGlobal, r) {
  427. test(r, "int x; void main(inout half4 color) { x = 10; color.b = x; }", 1, 2, 3, 4, 1, 2, 10,
  428. 4);
  429. test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
  430. 1, 2, 3, 4, 2, 4, 6, 8);
  431. test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
  432. 1, 2, 3, 4, 8, 7, 6, 5);
  433. test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
  434. 1, 2, 3, 4, 8, 7, 6, 5);
  435. }
  436. DEF_TEST(SkSLInterpreterGeneric, r) {
  437. float value1 = 5;
  438. float expected1 = 25;
  439. test(r, "float main(float x) { return x * x; }", &value1, 1, &expected1);
  440. float value2[2] = { 5, 25 };
  441. float expected2[2] = { 25, 625 };
  442. test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, 2, expected2);
  443. }
  444. DEF_TEST(SkSLInterpreterCompound, r) {
  445. struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
  446. struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
  447. const char* src =
  448. // Some struct definitions
  449. "struct Point { int x; int y; };\n"
  450. "struct Rect { Point p0; Point p1; };\n"
  451. "struct RectAndColor { Rect r; float4 color; };\n"
  452. // Structs as globals, parameters, return values
  453. "RectAndColor temp;\n"
  454. "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
  455. "RectAndColor make_blue_rect(int w, int h) {\n"
  456. " temp.r.p0.x = temp.r.p0.y = 0;\n"
  457. " temp.r.p1.x = w; temp.r.p1.y = h;\n"
  458. " temp.color = float4(0, 1, 0, 1);\n"
  459. " return temp;\n"
  460. "}\n"
  461. // Initialization and assignment of types larger than 4 slots
  462. "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
  463. "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
  464. // Same for arrays, including some non-constant indexing
  465. "float tempFloats[8];\n"
  466. "int median(int a[15]) { return a[7]; }\n"
  467. "float[8] sums(float a[8]) {\n"
  468. " float tempFloats[8];\n"
  469. " tempFloats[0] = a[0];\n"
  470. " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
  471. " return tempFloats;\n"
  472. "}\n"
  473. // Uniforms, array-of-structs, dynamic indices
  474. "in uniform Rect gRects[4];\n"
  475. "Rect get_rect(int i) { return gRects[i]; }\n"
  476. // Kitchen sink (swizzles, inout, SoAoS)
  477. "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
  478. "void fill_rects(inout ManyRects mr) {\n"
  479. " for (int i = 0; i < mr.numRects; ++i) {\n"
  480. " mr.rects[i].r = gRects[i];\n"
  481. " float b = mr.rects[i].r.p1.y;\n"
  482. " mr.rects[i].color = float4(b, b, b, b);\n"
  483. " }\n"
  484. "}\n";
  485. SkSL::Compiler compiler;
  486. SkSL::Program::Settings settings;
  487. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
  488. SkSL::Program::kGeneric_Kind,
  489. SkSL::String(src), settings);
  490. REPORTER_ASSERT(r, program);
  491. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  492. REPORTER_ASSERT(r, !compiler.errorCount());
  493. auto rect_height = byteCode->getFunction("rect_height"),
  494. make_blue_rect = byteCode->getFunction("make_blue_rect"),
  495. median = byteCode->getFunction("median"),
  496. sums = byteCode->getFunction("sums"),
  497. get_rect = byteCode->getFunction("get_rect"),
  498. fill_rects = byteCode->getFunction("fill_rects");
  499. SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
  500. {
  501. SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
  502. int out = 0;
  503. SkAssertResult(byteCode->run(rect_height, (float*)&in, (float*)&out, 1, (float*)gRects, 16));
  504. REPORTER_ASSERT(r, out == 30);
  505. }
  506. {
  507. int in[2] = { 15, 25 };
  508. RectAndColor out;
  509. SkAssertResult(byteCode->run(make_blue_rect, (float*)in, (float*)&out, 1, (float*)gRects, 16));
  510. REPORTER_ASSERT(r, out.fRect.width() == 15);
  511. REPORTER_ASSERT(r, out.fRect.height() == 25);
  512. SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
  513. REPORTER_ASSERT(r, out.fColor == blue);
  514. }
  515. {
  516. int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  517. int out = 0;
  518. SkAssertResult(byteCode->run(median, (float*)in, (float*)&out, 1, (float*)gRects, 16));
  519. REPORTER_ASSERT(r, out == 8);
  520. }
  521. {
  522. float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  523. float out[8] = { 0 };
  524. SkAssertResult(byteCode->run(sums, in, out, 1, (float*)gRects, 16));
  525. for (int i = 0; i < 8; ++i) {
  526. REPORTER_ASSERT(r, out[i] == static_cast<float>((i + 1) * (i + 2) / 2));
  527. }
  528. }
  529. {
  530. int in = 2;
  531. SkIRect out = SkIRect::MakeEmpty();
  532. SkAssertResult(byteCode->run(get_rect, (float*)&in, (float*)&out, 1, (float*)gRects, 16));
  533. REPORTER_ASSERT(r, out == gRects[2]);
  534. }
  535. {
  536. ManyRects in;
  537. memset(&in, 0, sizeof(in));
  538. in.fNumRects = 2;
  539. SkAssertResult(byteCode->run(fill_rects, (float*)&in, nullptr, 1, (float*)gRects, 16));
  540. ManyRects expected;
  541. memset(&expected, 0, sizeof(expected));
  542. expected.fNumRects = 2;
  543. for (int i = 0; i < 2; ++i) {
  544. expected.fRects[i].fRect = gRects[i];
  545. float c = gRects[i].fBottom;
  546. expected.fRects[i].fColor = { c, c, c, c };
  547. }
  548. REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
  549. }
  550. }
  551. static void expect_failure(skiatest::Reporter* r, const char* src) {
  552. SkSL::Compiler compiler;
  553. auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
  554. SkSL::Program::Settings());
  555. REPORTER_ASSERT(r, program);
  556. auto byteCode = compiler.toByteCode(*program);
  557. REPORTER_ASSERT(r, compiler.errorCount() > 0);
  558. REPORTER_ASSERT(r, !byteCode);
  559. }
  560. static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in) {
  561. SkSL::Compiler compiler;
  562. auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src),
  563. SkSL::Program::Settings());
  564. REPORTER_ASSERT(r, program);
  565. auto byteCode = compiler.toByteCode(*program);
  566. REPORTER_ASSERT(r, byteCode);
  567. bool result = byteCode->run(byteCode->getFunction("main"), in, nullptr, 1, nullptr, 0);
  568. REPORTER_ASSERT(r, !result);
  569. }
  570. DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) {
  571. // Ensure that simple recursion is not allowed
  572. expect_failure(r, "float main() { return main() + 1; }");
  573. // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion)
  574. expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }");
  575. // returns are not allowed inside conditionals (or loops, which are effectively the same thing)
  576. expect_failure(r, "float main(float x, float y) { if (x < y) { return x; } return y; }");
  577. expect_failure(r, "float main(float x) { while (x > 1) { return x; } return 0; }");
  578. }
  579. DEF_TEST(SkSLInterpreterArrayBounds, r) {
  580. // Out of bounds array access at compile time
  581. expect_failure(r, "float main(float x[4]) { return x[-1]; }");
  582. expect_failure(r, "float2 main(float2 x[2]) { return x[2]; }");
  583. // Out of bounds array access at runtime is pinned, and we don't update any inout data
  584. float in[3] = { -1.0f, 1.0f, 2.0f };
  585. expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
  586. REPORTER_ASSERT(r, in[0] == -1.0f && in[1] == 1.0f && in[2] == 2.0f);
  587. in[0] = 3.0f;
  588. expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in);
  589. REPORTER_ASSERT(r, in[0] == 3.0f && in[1] == 1.0f && in[2] == 2.0f);
  590. }
  591. DEF_TEST(SkSLInterpreterFunctions, r) {
  592. const char* src =
  593. "float sqr(float x) { return x * x; }\n"
  594. "float sub(float x, float y) { return x - y; }\n"
  595. "float main(float x) { return sub(sqr(x), x); }\n"
  596. // Different signatures
  597. "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
  598. "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
  599. "float dot3_test(float x) { return dot(float3(x, x + 1, x + 2), float3(1, -1, 2)); }\n"
  600. "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
  601. SkSL::Compiler compiler;
  602. SkSL::Program::Settings settings;
  603. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
  604. SkSL::Program::kGeneric_Kind,
  605. SkSL::String(src), settings);
  606. REPORTER_ASSERT(r, program);
  607. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  608. REPORTER_ASSERT(r, !compiler.errorCount());
  609. auto sub = byteCode->getFunction("sub");
  610. auto sqr = byteCode->getFunction("sqr");
  611. auto main = byteCode->getFunction("main");
  612. auto tan = byteCode->getFunction("tan");
  613. auto dot3 = byteCode->getFunction("dot3_test");
  614. auto dot2 = byteCode->getFunction("dot2_test");
  615. REPORTER_ASSERT(r, sub);
  616. REPORTER_ASSERT(r, sqr);
  617. REPORTER_ASSERT(r, main);
  618. REPORTER_ASSERT(r, !tan);
  619. REPORTER_ASSERT(r, dot3);
  620. REPORTER_ASSERT(r, dot2);
  621. float out = 0.0f;
  622. float in = 3.0f;
  623. SkAssertResult(byteCode->run(main, &in, &out, 1, nullptr, 0));
  624. REPORTER_ASSERT(r, out = 6.0f);
  625. SkAssertResult(byteCode->run(dot3, &in, &out, 1, nullptr, 0));
  626. REPORTER_ASSERT(r, out = 9.0f);
  627. SkAssertResult(byteCode->run(dot2, &in, &out, 1, nullptr, 0));
  628. REPORTER_ASSERT(r, out = -1.0f);
  629. }
  630. DEF_TEST(SkSLInterpreterOutParams, r) {
  631. test(r,
  632. "void oneAlpha(inout half4 color) { color.a = 1; }"
  633. "void main(inout half4 color) { oneAlpha(color); }",
  634. 0, 0, 0, 0, 0, 0, 0, 1);
  635. test(r,
  636. "half2 tricky(half x, half y, inout half2 color, half z) {"
  637. " color.xy = color.yx;"
  638. " return half2(x + y, z);"
  639. "}"
  640. "void main(inout half4 color) {"
  641. " half2 t = tricky(1, 2, color.rb, 5);"
  642. " color.ga = t;"
  643. "}",
  644. 1, 2, 3, 4, 3, 3, 1, 5);
  645. }
  646. DEF_TEST(SkSLInterpreterMathFunctions, r) {
  647. float value, expected;
  648. value = 0.0f; expected = 0.0f;
  649. test(r, "float main(float x) { return sin(x); }", &value, 1, &expected);
  650. test(r, "float main(float x) { return tan(x); }", &value, 1, &expected);
  651. value = 0.0f; expected = 1.0f;
  652. test(r, "float main(float x) { return cos(x); }", &value, 1, &expected);
  653. value = 25.0f; expected = 5.0f;
  654. test(r, "float main(float x) { return sqrt(x); }", &value, 1, &expected);
  655. }
  656. DEF_TEST(SkSLInterpreterVoidFunction, r) {
  657. test(r,
  658. "half x; void foo() { x = 1.0; }"
  659. "void main(inout half4 color) { foo(); color.r = x; }",
  660. 0, 0, 0, 0, 1, 0, 0, 0);
  661. }
  662. DEF_TEST(SkSLInterpreterMix, r) {
  663. float value, expected;
  664. value = 0.5f; expected = 0.0f;
  665. test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
  666. value = 0.75f; expected = 5.0f;
  667. test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
  668. value = 2.0f; expected = 30.0f;
  669. test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
  670. float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
  671. expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
  672. test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors, 4,
  673. expectedVector);
  674. }
  675. DEF_TEST(SkSLInterpreterCross, r) {
  676. float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
  677. SkPoint3 cross = SkPoint3::CrossProduct(SkPoint3::Make(args[0], args[1], args[2]),
  678. SkPoint3::Make(args[3], args[4], args[5]));
  679. float expected[] = { cross.fX, cross.fY, cross.fZ };
  680. test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, 3, expected);
  681. }
  682. DEF_TEST(SkSLInterpreterInverse, r) {
  683. {
  684. SkMatrix m;
  685. m.setRotate(30).postScale(1, 2);
  686. float args[4] = { m[0], m[3], m[1], m[4] };
  687. SkAssertResult(m.invert(&m));
  688. float expt[4] = { m[0], m[3], m[1], m[4] };
  689. test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, 4, expt, false);
  690. }
  691. {
  692. SkMatrix m;
  693. m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
  694. float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
  695. SkAssertResult(m.invert(&m));
  696. float expt[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
  697. test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, 9, expt, false);
  698. }
  699. {
  700. float args[16], expt[16];
  701. SkMatrix44 m;
  702. // just some crazy thing that is invertible
  703. m.set4x4(1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0);
  704. m.asColMajorf(args);
  705. SkAssertResult(m.invert(&m));
  706. m.asColMajorf(expt);
  707. test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, 16, expt, false);
  708. }
  709. }
  710. DEF_TEST(SkSLInterpreterDot, r) {
  711. float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
  712. float expected = args[0] * args[2] +
  713. args[1] * args[3];
  714. test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, 1, &expected);
  715. expected = args[0] * args[3] +
  716. args[1] * args[4] +
  717. args[2] * args[5];
  718. test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, 1, &expected);
  719. expected = args[0] * args[4] +
  720. args[1] * args[5] +
  721. args[2] * args[6] +
  722. args[3] * args[7];
  723. test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, 1, &expected);
  724. }
  725. static const SkSL::Type& type_of(const skjson::Value* value, SkSL::Compiler* compiler) {
  726. switch (value->getType()) {
  727. case skjson::Value::Type::kNumber: {
  728. float f = *value->as<skjson::NumberValue>();
  729. if (f == (float) (int) f) {
  730. return *compiler->context().fInt_Type;
  731. }
  732. return *compiler->context().fFloat_Type;
  733. }
  734. case skjson::Value::Type::kBool:
  735. return *compiler->context().fBool_Type;
  736. default:
  737. return *compiler->context().fVoid_Type;
  738. }
  739. }
  740. class JSONExternalValue : public SkSL::ExternalValue {
  741. public:
  742. JSONExternalValue(const char* name, const skjson::Value* value, SkSL::Compiler* compiler)
  743. : INHERITED(name, type_of(value, compiler))
  744. , fValue(*value)
  745. , fCompiler(*compiler) {}
  746. bool canRead() const override {
  747. return type() != *fCompiler.context().fVoid_Type;
  748. }
  749. void read(int /*unusedIndex*/, float* target) override {
  750. if (type() == *fCompiler.context().fInt_Type) {
  751. *(int*) target = *fValue.as<skjson::NumberValue>();
  752. } else if (type() == *fCompiler.context().fFloat_Type) {
  753. *(float*) target = *fValue.as<skjson::NumberValue>();
  754. } else if (type() == *fCompiler.context().fBool_Type) {
  755. // ByteCode "booleans" are actually bit-masks
  756. *(int*) target = *fValue.as<skjson::BoolValue>() ? ~0 : 0;
  757. } else {
  758. SkASSERT(false);
  759. }
  760. }
  761. SkSL::ExternalValue* getChild(const char* name) const override {
  762. if (fValue.getType() == skjson::Value::Type::kObject) {
  763. const skjson::Value& v = fValue.as<skjson::ObjectValue>()[name];
  764. return (SkSL::ExternalValue*) fCompiler.takeOwnership(std::unique_ptr<Symbol>(
  765. new JSONExternalValue(name, &v, &fCompiler)));
  766. }
  767. return nullptr;
  768. }
  769. private:
  770. const skjson::Value& fValue;
  771. SkSL::Compiler& fCompiler;
  772. typedef SkSL::ExternalValue INHERITED;
  773. };
  774. class PointerExternalValue : public SkSL::ExternalValue {
  775. public:
  776. PointerExternalValue(const char* name, const SkSL::Type& type, void* data, size_t size)
  777. : INHERITED(name, type)
  778. , fData(data)
  779. , fSize(size) {}
  780. bool canRead() const override {
  781. return true;
  782. }
  783. bool canWrite() const override {
  784. return true;
  785. }
  786. void read(int /*unusedIndex*/, float* target) override {
  787. memcpy(target, fData, fSize);
  788. }
  789. void write(int /*unusedIndex*/, float* src) override {
  790. memcpy(fData, src, fSize);
  791. }
  792. private:
  793. void* fData;
  794. size_t fSize;
  795. typedef SkSL::ExternalValue INHERITED;
  796. };
  797. DEF_TEST(SkSLInterpreterExternalValues, r) {
  798. const char* json = "{ \"value1\": 12, \"child\": { \"value2\": true, \"value3\": 5.5 } }";
  799. skjson::DOM dom(json, strlen(json));
  800. SkSL::Compiler compiler;
  801. SkSL::Program::Settings settings;
  802. const char* src = "float main() {"
  803. " outValue = 152;"
  804. " return root.child.value2 ? root.value1 * root.child.value3 : -1;"
  805. "}";
  806. compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
  807. std::unique_ptr<SkSL::Symbol>(new JSONExternalValue("root", &dom.root(), &compiler))));
  808. int32_t outValue = -1;
  809. compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
  810. std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("outValue",
  811. *compiler.context().fInt_Type,
  812. &outValue,
  813. sizeof(outValue)))));
  814. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
  815. SkSL::Program::kGeneric_Kind,
  816. SkSL::String(src), settings);
  817. REPORTER_ASSERT(r, program);
  818. if (program) {
  819. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  820. REPORTER_ASSERT(r, !compiler.errorCount());
  821. if (compiler.errorCount() > 0) {
  822. printf("%s\n%s", src, compiler.errorText().c_str());
  823. return;
  824. }
  825. SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
  826. float out;
  827. SkAssertResult(byteCode->run(main, nullptr, &out, 1, nullptr, 0));
  828. REPORTER_ASSERT(r, out == 66.0);
  829. REPORTER_ASSERT(r, outValue == 152);
  830. } else {
  831. printf("%s\n%s", src, compiler.errorText().c_str());
  832. }
  833. }
  834. DEF_TEST(SkSLInterpreterExternalValuesVector, r) {
  835. SkSL::Compiler compiler;
  836. SkSL::Program::Settings settings;
  837. const char* src = "void main() {"
  838. " value *= 2;"
  839. "}";
  840. int32_t value[4] = { 1, 2, 3, 4 };
  841. compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
  842. std::unique_ptr<SkSL::Symbol>(new PointerExternalValue("value",
  843. *compiler.context().fInt4_Type,
  844. value,
  845. sizeof(value)))));
  846. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
  847. SkSL::String(src),
  848. settings);
  849. REPORTER_ASSERT(r, program);
  850. if (program) {
  851. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  852. REPORTER_ASSERT(r, !compiler.errorCount());
  853. if (compiler.errorCount() > 0) {
  854. printf("%s\n%s", src, compiler.errorText().c_str());
  855. return;
  856. }
  857. SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
  858. SkAssertResult(byteCode->run(main, nullptr, nullptr, 1, nullptr, 0));
  859. REPORTER_ASSERT(r, value[0] == 2);
  860. REPORTER_ASSERT(r, value[1] == 4);
  861. REPORTER_ASSERT(r, value[2] == 6);
  862. REPORTER_ASSERT(r, value[3] == 8);
  863. } else {
  864. printf("%s\n%s", src, compiler.errorText().c_str());
  865. }
  866. }
  867. class FunctionExternalValue : public SkSL::ExternalValue {
  868. public:
  869. FunctionExternalValue(const char* name, float(*function)(float), SkSL::Compiler& compiler)
  870. : INHERITED(name, *compiler.context().fFloat_Type)
  871. , fCompiler(compiler)
  872. , fFunction(function) {}
  873. bool canCall() const override {
  874. return true;
  875. }
  876. int callParameterCount() const override {
  877. return 1;
  878. }
  879. void getCallParameterTypes(const SkSL::Type** outTypes) const override {
  880. outTypes[0] = fCompiler.context().fFloat_Type.get();
  881. }
  882. void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
  883. outReturn[0] = fFunction(arguments[0]);
  884. }
  885. private:
  886. SkSL::Compiler& fCompiler;
  887. float (*fFunction)(float);
  888. typedef SkSL::ExternalValue INHERITED;
  889. };
  890. DEF_TEST(SkSLInterpreterExternalValuesCall, r) {
  891. SkSL::Compiler compiler;
  892. SkSL::Program::Settings settings;
  893. const char* src = "float main() {"
  894. " return external(25);"
  895. "}";
  896. compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
  897. std::unique_ptr<SkSL::Symbol>(new FunctionExternalValue("external",
  898. [] (float x) {
  899. return (float) sqrt(x);
  900. },
  901. compiler))));
  902. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
  903. SkSL::String(src),
  904. settings);
  905. REPORTER_ASSERT(r, program);
  906. if (program) {
  907. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  908. REPORTER_ASSERT(r, !compiler.errorCount());
  909. if (compiler.errorCount() > 0) {
  910. printf("%s\n%s", src, compiler.errorText().c_str());
  911. return;
  912. }
  913. SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
  914. float out;
  915. SkAssertResult(byteCode->run(main, nullptr, &out, 1, nullptr, 0));
  916. REPORTER_ASSERT(r, out == 5.0);
  917. } else {
  918. printf("%s\n%s", src, compiler.errorText().c_str());
  919. }
  920. }
  921. class VectorFunctionExternalValue : public SkSL::ExternalValue {
  922. public:
  923. VectorFunctionExternalValue(const char* name, void(*function)(float[4], float[4]),
  924. SkSL::Compiler& compiler)
  925. : INHERITED(name, *compiler.context().fFloat4_Type)
  926. , fCompiler(compiler)
  927. , fFunction(function) {}
  928. bool canCall() const override {
  929. return true;
  930. }
  931. int callParameterCount() const override {
  932. return 1;
  933. }
  934. void getCallParameterTypes(const SkSL::Type** outTypes) const override {
  935. outTypes[0] = fCompiler.context().fFloat4_Type.get();
  936. }
  937. void call(int /*unusedIndex*/, float* arguments, float* outReturn) override {
  938. fFunction(arguments, outReturn);
  939. }
  940. private:
  941. SkSL::Compiler& fCompiler;
  942. void (*fFunction)(float[4], float[4]);
  943. typedef SkSL::ExternalValue INHERITED;
  944. };
  945. DEF_TEST(SkSLInterpreterExternalValuesVectorCall, r) {
  946. SkSL::Compiler compiler;
  947. SkSL::Program::Settings settings;
  948. const char* src = "float4 main() {"
  949. " return external(float4(1, 4, 9, 16));"
  950. "}";
  951. compiler.registerExternalValue((SkSL::ExternalValue*) compiler.takeOwnership(
  952. std::unique_ptr<SkSL::Symbol>(new VectorFunctionExternalValue("external",
  953. [] (float in[4], float out[4]) {
  954. out[0] = sqrt(in[0]);
  955. out[1] = sqrt(in[1]);
  956. out[2] = sqrt(in[2]);
  957. out[3] = sqrt(in[3]);
  958. },
  959. compiler))));
  960. std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kGeneric_Kind,
  961. SkSL::String(src),
  962. settings);
  963. REPORTER_ASSERT(r, program);
  964. if (program) {
  965. std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
  966. REPORTER_ASSERT(r, !compiler.errorCount());
  967. if (compiler.errorCount() > 0) {
  968. printf("%s\n%s", src, compiler.errorText().c_str());
  969. return;
  970. }
  971. SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
  972. float out[4];
  973. SkAssertResult(byteCode->run(main, nullptr, out, 1, nullptr, 0));
  974. REPORTER_ASSERT(r, out[0] == 1.0);
  975. REPORTER_ASSERT(r, out[1] == 2.0);
  976. REPORTER_ASSERT(r, out[2] == 3.0);
  977. REPORTER_ASSERT(r, out[3] == 4.0);
  978. } else {
  979. printf("%s\n%s", src, compiler.errorText().c_str());
  980. }
  981. }