SkSLMemoryLayoutTest.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  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. #include "src/sksl/SkSLContext.h"
  8. #include "src/sksl/SkSLMemoryLayout.h"
  9. #include "tests/Test.h"
  10. DEF_TEST(SkSLMemoryLayout140Test, r) {
  11. SkSL::Context context;
  12. SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
  13. // basic types
  14. REPORTER_ASSERT(r, 4 == layout.size(*context.fFloat_Type));
  15. REPORTER_ASSERT(r, 8 == layout.size(*context.fFloat2_Type));
  16. REPORTER_ASSERT(r, 12 == layout.size(*context.fFloat3_Type));
  17. REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat4_Type));
  18. REPORTER_ASSERT(r, 4 == layout.size(*context.fInt_Type));
  19. REPORTER_ASSERT(r, 8 == layout.size(*context.fInt2_Type));
  20. REPORTER_ASSERT(r, 12 == layout.size(*context.fInt3_Type));
  21. REPORTER_ASSERT(r, 16 == layout.size(*context.fInt4_Type));
  22. REPORTER_ASSERT(r, 1 == layout.size(*context.fBool_Type));
  23. REPORTER_ASSERT(r, 2 == layout.size(*context.fBool2_Type));
  24. REPORTER_ASSERT(r, 3 == layout.size(*context.fBool3_Type));
  25. REPORTER_ASSERT(r, 4 == layout.size(*context.fBool4_Type));
  26. REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x2_Type));
  27. REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x4_Type));
  28. REPORTER_ASSERT(r, 48 == layout.size(*context.fFloat3x3_Type));
  29. REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x2_Type));
  30. REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x4_Type));
  31. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fFloat_Type));
  32. REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2_Type));
  33. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3_Type));
  34. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4_Type));
  35. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fInt_Type));
  36. REPORTER_ASSERT(r, 8 == layout.alignment(*context.fInt2_Type));
  37. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt3_Type));
  38. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt4_Type));
  39. REPORTER_ASSERT(r, 1 == layout.alignment(*context.fBool_Type));
  40. REPORTER_ASSERT(r, 2 == layout.alignment(*context.fBool2_Type));
  41. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool3_Type));
  42. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool4_Type));
  43. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x2_Type));
  44. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x4_Type));
  45. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3x3_Type));
  46. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x2_Type));
  47. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x4_Type));
  48. // struct 1
  49. std::vector<SkSL::Type::Field> fields1;
  50. fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fFloat3_Type.get());
  51. SkSL::Type s1(-1, SkSL::String("s1"), fields1);
  52. REPORTER_ASSERT(r, 16 == layout.size(s1));
  53. REPORTER_ASSERT(r, 16 == layout.alignment(s1));
  54. fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat_Type.get());
  55. SkSL::Type s2(-1, SkSL::String("s2"), fields1);
  56. REPORTER_ASSERT(r, 16 == layout.size(s2));
  57. REPORTER_ASSERT(r, 16 == layout.alignment(s2));
  58. fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fBool_Type.get());
  59. SkSL::Type s3(-1, SkSL::String("s3"), fields1);
  60. REPORTER_ASSERT(r, 32 == layout.size(s3));
  61. REPORTER_ASSERT(r, 16 == layout.alignment(s3));
  62. // struct 2
  63. std::vector<SkSL::Type::Field> fields2;
  64. fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fInt_Type.get());
  65. SkSL::Type s4(-1, SkSL::String("s4"), fields2);
  66. REPORTER_ASSERT(r, 16 == layout.size(s4));
  67. REPORTER_ASSERT(r, 16 == layout.alignment(s4));
  68. fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat3_Type.get());
  69. SkSL::Type s5(-1, SkSL::String("s5"), fields2);
  70. REPORTER_ASSERT(r, 32 == layout.size(s5));
  71. REPORTER_ASSERT(r, 16 == layout.alignment(s5));
  72. // arrays
  73. SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
  74. REPORTER_ASSERT(r, 64 == layout.size(array1));
  75. REPORTER_ASSERT(r, 16 == layout.alignment(array1));
  76. REPORTER_ASSERT(r, 16 == layout.stride(array1));
  77. SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
  78. REPORTER_ASSERT(r, 64 == layout.size(array2));
  79. REPORTER_ASSERT(r, 16 == layout.alignment(array2));
  80. REPORTER_ASSERT(r, 16 == layout.stride(array2));
  81. }
  82. DEF_TEST(SkSLMemoryLayout430Test, r) {
  83. SkSL::Context context;
  84. SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
  85. // basic types
  86. REPORTER_ASSERT(r, 4 == layout.size(*context.fFloat_Type));
  87. REPORTER_ASSERT(r, 8 == layout.size(*context.fFloat2_Type));
  88. REPORTER_ASSERT(r, 12 == layout.size(*context.fFloat3_Type));
  89. REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat4_Type));
  90. REPORTER_ASSERT(r, 4 == layout.size(*context.fInt_Type));
  91. REPORTER_ASSERT(r, 8 == layout.size(*context.fInt2_Type));
  92. REPORTER_ASSERT(r, 12 == layout.size(*context.fInt3_Type));
  93. REPORTER_ASSERT(r, 16 == layout.size(*context.fInt4_Type));
  94. REPORTER_ASSERT(r, 1 == layout.size(*context.fBool_Type));
  95. REPORTER_ASSERT(r, 2 == layout.size(*context.fBool2_Type));
  96. REPORTER_ASSERT(r, 3 == layout.size(*context.fBool3_Type));
  97. REPORTER_ASSERT(r, 4 == layout.size(*context.fBool4_Type));
  98. REPORTER_ASSERT(r, 16 == layout.size(*context.fFloat2x2_Type));
  99. REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat2x4_Type));
  100. REPORTER_ASSERT(r, 48 == layout.size(*context.fFloat3x3_Type));
  101. REPORTER_ASSERT(r, 32 == layout.size(*context.fFloat4x2_Type));
  102. REPORTER_ASSERT(r, 64 == layout.size(*context.fFloat4x4_Type));
  103. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fFloat_Type));
  104. REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2_Type));
  105. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3_Type));
  106. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4_Type));
  107. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fInt_Type));
  108. REPORTER_ASSERT(r, 8 == layout.alignment(*context.fInt2_Type));
  109. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt3_Type));
  110. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fInt4_Type));
  111. REPORTER_ASSERT(r, 1 == layout.alignment(*context.fBool_Type));
  112. REPORTER_ASSERT(r, 2 == layout.alignment(*context.fBool2_Type));
  113. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool3_Type));
  114. REPORTER_ASSERT(r, 4 == layout.alignment(*context.fBool4_Type));
  115. REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat2x2_Type));
  116. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat2x4_Type));
  117. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat3x3_Type));
  118. REPORTER_ASSERT(r, 8 == layout.alignment(*context.fFloat4x2_Type));
  119. REPORTER_ASSERT(r, 16 == layout.alignment(*context.fFloat4x4_Type));
  120. // struct 1
  121. std::vector<SkSL::Type::Field> fields1;
  122. fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fFloat3_Type.get());
  123. SkSL::Type s1(-1, SkSL::String("s1"), fields1);
  124. REPORTER_ASSERT(r, 16 == layout.size(s1));
  125. REPORTER_ASSERT(r, 16 == layout.alignment(s1));
  126. fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat_Type.get());
  127. SkSL::Type s2(-1, SkSL::String("s2"), fields1);
  128. REPORTER_ASSERT(r, 16 == layout.size(s2));
  129. REPORTER_ASSERT(r, 16 == layout.alignment(s2));
  130. fields1.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("c"), context.fBool_Type.get());
  131. SkSL::Type s3(-1, SkSL::String("s3"), fields1);
  132. REPORTER_ASSERT(r, 32 == layout.size(s3));
  133. REPORTER_ASSERT(r, 16 == layout.alignment(s3));
  134. // struct 2
  135. std::vector<SkSL::Type::Field> fields2;
  136. fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("a"), context.fInt_Type.get());
  137. SkSL::Type s4(-1, SkSL::String("s4"), fields2);
  138. REPORTER_ASSERT(r, 4 == layout.size(s4));
  139. REPORTER_ASSERT(r, 4 == layout.alignment(s4));
  140. fields2.emplace_back(SkSL::Modifiers(), SkSL::StringFragment("b"), context.fFloat3_Type.get());
  141. SkSL::Type s5(-1, SkSL::String("s5"), fields2);
  142. REPORTER_ASSERT(r, 32 == layout.size(s5));
  143. REPORTER_ASSERT(r, 16 == layout.alignment(s5));
  144. // arrays
  145. SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
  146. REPORTER_ASSERT(r, 16 == layout.size(array1));
  147. REPORTER_ASSERT(r, 4 == layout.alignment(array1));
  148. REPORTER_ASSERT(r, 4 == layout.stride(array1));
  149. SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
  150. REPORTER_ASSERT(r, 64 == layout.size(array2));
  151. REPORTER_ASSERT(r, 16 == layout.alignment(array2));
  152. REPORTER_ASSERT(r, 16 == layout.stride(array2));
  153. }