unit-MeshPrimitiveHelpers.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #include <catch2/catch.hpp>
  2. #include <iostream>
  3. #include <gul/MeshPrimitiveFunctions.h>
  4. SCENARIO("Vertex Attribute Reshaping")
  5. {
  6. GIVEN("Vertex attribute of scalar floats")
  7. {
  8. gul::VertexAttribute V(gul::eComponentType::FLOAT, gul::eType::SCALAR);
  9. std::vector<float> raw = {0.5f,1.0f,0.8f,0.25f,0.75f,0.6f};
  10. V = raw;
  11. REQUIRE( V.attributeCount() == 6 );
  12. REQUIRE( V.getShape()[0] == 6);
  13. REQUIRE( V.getShape()[1] == 1);
  14. WHEN("We set the type to VEC2")
  15. {
  16. V.setType(gul::eType::VEC2);
  17. THEN("The size and shapes are different")
  18. {
  19. REQUIRE( V.attributeCount() == 3 );
  20. REQUIRE( V.getShape()[0] == 3);
  21. REQUIRE( V.getShape()[1] == 2);
  22. WHEN("We set the type back to SCALAR")
  23. {
  24. V.setType(gul::eType::SCALAR);
  25. THEN("The size and shapes are different")
  26. {
  27. REQUIRE( V.attributeCount() == 6 );
  28. REQUIRE( V.getShape()[0] == 6);
  29. REQUIRE( V.getShape()[1] == 1);
  30. }
  31. }
  32. }
  33. }
  34. WHEN("We set the type to VEC3")
  35. {
  36. V.setType(gul::eType::VEC3);
  37. THEN("The size and shapes are different")
  38. {
  39. REQUIRE( V.attributeCount() == 2 );
  40. REQUIRE( V.getShape()[0] == 2);
  41. REQUIRE( V.getShape()[1] == 3);
  42. WHEN("We set the type back to SCALAR")
  43. {
  44. V.setType(gul::eType::SCALAR);
  45. THEN("The size and shapes are different")
  46. {
  47. REQUIRE( V.attributeCount() == 6 );
  48. REQUIRE( V.getShape()[0] == 6);
  49. REQUIRE( V.getShape()[1] == 1);
  50. }
  51. }
  52. }
  53. }
  54. WHEN("We set the type to VEC4")
  55. {
  56. V.setType(gul::eType::VEC4);
  57. THEN("Then we lose information because there are not enough elements")
  58. {
  59. REQUIRE( V.attributeCount() == 1 );
  60. REQUIRE( V.getShape()[0] == 1);
  61. REQUIRE( V.getShape()[1] == 4);
  62. WHEN("We set the type back to SCALAR")
  63. {
  64. V.setType(gul::eType::SCALAR);
  65. THEN("The size has returned to normal")
  66. {
  67. REQUIRE( V.attributeCount() == 6 );
  68. REQUIRE( V.getShape()[0] == 6);
  69. REQUIRE( V.getShape()[1] == 1);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. uint32_t vec2toUint(glm::uvec2 const &v)
  77. {
  78. return ((v[0] << 16) & 0xFFFF0000) | (v[1] & 0x0000FFFF);
  79. }
  80. glm::uvec2 Uinttovec2(uint32_t v)
  81. {
  82. return glm::uvec2( v>>16 , v&0x0000FFFF );
  83. }
  84. glm::uvec3 vec4tovec3(glm::uvec4 const &v)
  85. {
  86. return glm::uvec3( v[0], v[1], vec2toUint( glm::vec2(v[2], v[3])));
  87. }
  88. glm::uvec4 vec3tovec4(glm::uvec3 const &v)
  89. {
  90. return glm::uvec4( v[0], v[1], Uinttovec2(v[2]) ) ;
  91. }
  92. SCENARIO("convertAttribe_t")
  93. {
  94. GIVEN("Vertex attribute of floats")
  95. {
  96. gul::VertexAttribute V(gul::eComponentType::UNSIGNED_INT, gul::eType::SCALAR);
  97. std::vector<uint32_t> raw = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
  98. V = raw;
  99. V.setType(gul::eType::VEC2);
  100. REQUIRE( V.attributeCount() == 3);
  101. WHEN("We compress it into uint16")
  102. {
  103. gul::convertAttribute_t<glm::uvec2, uint32_t>(V, vec2toUint);
  104. //gul::packUnorm2x16(V);
  105. THEN("The type changed")
  106. {
  107. REQUIRE(V.getType() == gul::eType::SCALAR);
  108. REQUIRE(V.getComponentType() == gul::eComponentType::UNSIGNED_INT);
  109. }
  110. THEN("The shape has changed")
  111. {
  112. REQUIRE(V.getShape()[0] == 3);
  113. REQUIRE(V.getShape()[1] == 1);
  114. }
  115. THEN("The total byte size is half its original length")
  116. {
  117. REQUIRE(V.getByteSize() == 12);
  118. REQUIRE(V.getAttributeSize() == 4);
  119. REQUIRE(V.getNumComponents() == 1);
  120. }
  121. THEN("We can get each of the values uint16")
  122. {
  123. REQUIRE(V.get<uint32_t>(0) == 0x00010002 );
  124. REQUIRE(V.get<uint32_t>(1) == 0x00030004 );
  125. REQUIRE(V.get<uint32_t>(2) == 0x00050006 );
  126. }
  127. WHEN("We uncompress")
  128. {
  129. gul::convertAttribute_t<uint32_t, glm::uvec2>(V, Uinttovec2);
  130. THEN("The type changed")
  131. {
  132. REQUIRE(V.getType() == gul::eType::VEC2);
  133. REQUIRE(V.getComponentType() == gul::eComponentType::UNSIGNED_INT);
  134. }
  135. THEN("The shape has reverted")
  136. {
  137. REQUIRE(V.getShape()[0] == 3);
  138. REQUIRE(V.getShape()[1] == 2);
  139. }
  140. THEN("The values are approximately back to their original")
  141. {
  142. REQUIRE(V.get<uint32_t>(0) == 0x01);
  143. REQUIRE(V.get<uint32_t>(1) == 0x02);
  144. REQUIRE(V.get<uint32_t>(2) == 0x03);
  145. REQUIRE(V.get<uint32_t>(3) == 0x04);
  146. REQUIRE(V.get<uint32_t>(4) == 0x05);
  147. REQUIRE(V.get<uint32_t>(5) == 0x06);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. SCENARIO("convertAttribe_t vec4 to vec3")
  154. {
  155. GIVEN("Vertex attribute of floats")
  156. {
  157. gul::VertexAttribute V(gul::eComponentType::UNSIGNED_INT, gul::eType::SCALAR);
  158. std::vector<uint32_t> raw = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  159. V = raw;
  160. V.setType(gul::eType::VEC4);
  161. REQUIRE( V.attributeCount() == 2);
  162. REQUIRE( V.getType() == gul::eType::VEC4);
  163. REQUIRE(V.getByteSize() == 32);
  164. REQUIRE(V.getAttributeSize() == 16);
  165. REQUIRE(V.getNumComponents() == 4);
  166. WHEN("We compress it into uint16")
  167. {
  168. gul::convertAttribute_t<glm::uvec4, glm::uvec3>(V, vec4tovec3);
  169. THEN("The type changed")
  170. {
  171. REQUIRE(V.getType() == gul::eType::VEC3);
  172. REQUIRE(V.getComponentType() == gul::eComponentType::UNSIGNED_INT);
  173. }
  174. THEN("The shape has changed")
  175. {
  176. REQUIRE(V.getShape()[0] == 2);
  177. REQUIRE(V.getShape()[1] == 3);
  178. }
  179. THEN("The total byte size is half its original length")
  180. {
  181. REQUIRE(V.getByteSize() == 24);
  182. REQUIRE(V.getAttributeSize() == 12);
  183. REQUIRE(V.getNumComponents() == 3);
  184. }
  185. THEN("We can get each of the values uint16")
  186. {
  187. REQUIRE(V.get<uint32_t>(0) == 0x01 );
  188. REQUIRE(V.get<uint32_t>(1) == 0x02 );
  189. REQUIRE(V.get<uint32_t>(2) == 0x00030004 );
  190. REQUIRE(V.get<uint32_t>(3) == 0x05 );
  191. REQUIRE(V.get<uint32_t>(4) == 0x06 );
  192. REQUIRE(V.get<uint32_t>(5) == 0x00070008 );
  193. }
  194. WHEN("We uncompress")
  195. {
  196. gul::convertAttribute_t<glm::uvec3, glm::uvec4>(V, vec3tovec4);
  197. REQUIRE( V.attributeCount() == 2);
  198. THEN("The type changed")
  199. {
  200. REQUIRE(V.getType() == gul::eType::VEC4);
  201. REQUIRE(V.getComponentType() == gul::eComponentType::UNSIGNED_INT);
  202. }
  203. THEN("The shape has reverted")
  204. {
  205. REQUIRE(V.getShape()[0] == 2);
  206. REQUIRE(V.getShape()[1] == 4);
  207. }
  208. THEN("The values are approximately back to their original")
  209. {
  210. REQUIRE(V.get<uint32_t>(0) == 0x01);
  211. REQUIRE(V.get<uint32_t>(1) == 0x02);
  212. REQUIRE(V.get<uint32_t>(2) == 0x03);
  213. REQUIRE(V.get<uint32_t>(3) == 0x04);
  214. REQUIRE(V.get<uint32_t>(4) == 0x05);
  215. REQUIRE(V.get<uint32_t>(5) == 0x06);
  216. REQUIRE(V.get<uint32_t>(6) == 0x07);
  217. REQUIRE(V.get<uint32_t>(7) == 0x08);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. SCENARIO("pack 2xfloats into 1 u32")
  224. {
  225. GIVEN("Vertex attribute of floats")
  226. {
  227. gul::VertexAttribute V(gul::eComponentType::FLOAT, gul::eType::SCALAR);
  228. std::vector<float> raw = {0.5f,1.0f,0.8f,0.25f,0.75f,0.f};
  229. V = raw;
  230. V.setType(gul::eType::VEC2);
  231. REQUIRE( V.attributeCount() == 3);
  232. WHEN("We compress it into uint16")
  233. {
  234. gul::convertAttribute_t<glm::vec2, uint32_t>(V, glm::packUnorm2x16);
  235. THEN("The type changed")
  236. {
  237. REQUIRE(V.getType() == gul::eType::SCALAR);
  238. REQUIRE(V.getComponentType() == gul::eComponentType::UNSIGNED_INT);
  239. }
  240. THEN("The shape has changed")
  241. {
  242. REQUIRE(V.getShape()[0] == 3);
  243. REQUIRE(V.getShape()[1] == 1);
  244. }
  245. THEN("The total byte size is half its original length")
  246. {
  247. REQUIRE(V.getByteSize() == 12);
  248. REQUIRE(V.getAttributeSize() == 4);
  249. REQUIRE(V.getNumComponents() == 1);
  250. }
  251. THEN("We can get each of the values uint16")
  252. {
  253. REQUIRE(V.get<uint16_t>(0) == 32768 );
  254. REQUIRE(V.get<uint16_t>(1) == 65535 );
  255. REQUIRE(V.get<uint16_t>(2) == 52428 );
  256. REQUIRE(V.get<uint16_t>(3) == 16384 );
  257. REQUIRE(V.get<uint16_t>(4) == 49151 );
  258. REQUIRE(V.get<uint16_t>(5) == 0 );
  259. }
  260. WHEN("We uncompress")
  261. {
  262. gul::convertAttribute_t<uint32_t, glm::vec2>(V, glm::unpackUnorm2x16);
  263. V.setType(gul::eType::VEC2);
  264. THEN("The type changed")
  265. {
  266. REQUIRE(V.getType() == gul::eType::VEC2);
  267. REQUIRE(V.getComponentType() == gul::eComponentType::FLOAT);
  268. }
  269. THEN("The shape has reverted")
  270. {
  271. REQUIRE(V.getShape()[0] == 3);
  272. REQUIRE(V.getShape()[1] == 2);
  273. }
  274. THEN("The values are approximately back to their original")
  275. {
  276. REQUIRE(V.get<float>(0) == Approx(0.5) .epsilon(0.01));
  277. REQUIRE(V.get<float>(1) == Approx(1.0) .epsilon(0.01));
  278. REQUIRE(V.get<float>(2) == Approx(0.8) .epsilon(0.01));
  279. REQUIRE(V.get<float>(3) == Approx(0.25).epsilon(0.01));
  280. REQUIRE(V.get<float>(4) == Approx(0.75).epsilon(0.01));
  281. REQUIRE(V.get<float>(5) == Approx(0.0) .epsilon(0.01));
  282. }
  283. }
  284. }
  285. }
  286. }
  287. SCENARIO("pack 3xfloats into 1 u32")
  288. {
  289. GIVEN("Vertex attribute of floats")
  290. {
  291. gul::VertexAttribute V(gul::eComponentType::FLOAT, gul::eType::SCALAR);
  292. std::vector<float> raw = {-1.f,-1.f,-1.f,0.f,0.f,0.f,1.f,1.f,1.f,0.5f,0.5f,0.5f,-0.5f,-0.5f,-0.5f};
  293. V = raw;
  294. V.setType(gul::eType::VEC3);
  295. REQUIRE( V.attributeCount() == 5);
  296. WHEN("We compress it into uint16")
  297. {
  298. gul::convertAttribute_t<glm::vec3, uint32_t>(V, gul::packSnorm3x10);
  299. //gul::unpackUnorm2x16(V);
  300. //gul::packSnorm3x10(V);
  301. THEN("The type changed")
  302. {
  303. REQUIRE(V.getType() == gul::eType::SCALAR);
  304. REQUIRE(V.getComponentType() == gul::eComponentType::UNSIGNED_INT);
  305. }
  306. THEN("The shape has changed")
  307. {
  308. REQUIRE(V.getShape()[0] == 5);
  309. REQUIRE(V.getShape()[1] == 1);
  310. }
  311. THEN("The total byte size is half its original length")
  312. {
  313. REQUIRE(V.getByteSize() == 5*4);
  314. REQUIRE(V.getAttributeSize() == 4);
  315. REQUIRE(V.getNumComponents() == 1);
  316. }
  317. WHEN("We uncompress")
  318. {
  319. gul::convertAttribute_t<uint32_t, glm::vec3>(V, gul::unpackSnorm3x10);
  320. V.setType(gul::eType::VEC3);
  321. //gul::unpackSnorm3x10(V);
  322. THEN("The type changed")
  323. {
  324. REQUIRE(V.getType() == gul::eType::VEC3);
  325. REQUIRE(V.getComponentType() == gul::eComponentType::FLOAT);
  326. }
  327. THEN("The shape has reverted")
  328. {
  329. REQUIRE(V.getShape()[0] == 5);
  330. REQUIRE(V.getShape()[1] == 3);
  331. }
  332. THEN("The values are approximately back to their original")
  333. {
  334. REQUIRE(V.get<float>(0) == Approx(-1.0f) .epsilon(0.01f));
  335. REQUIRE(V.get<float>(1) == Approx(-1.0f) .epsilon(0.01f));
  336. REQUIRE(V.get<float>(2) == Approx(-1.0f) .epsilon(0.01f));
  337. REQUIRE(V.get<float>(3) == Approx(-0.00f) .epsilon(0.03f));
  338. REQUIRE(V.get<float>(4) == Approx(-0.00f) .epsilon(0.03f));
  339. REQUIRE(V.get<float>(5) == Approx(-0.00f) .epsilon(0.03f));
  340. REQUIRE(V.get<float>(6) == Approx( 1.0f) .epsilon(0.01f));
  341. REQUIRE(V.get<float>(7) == Approx( 1.0f) .epsilon(0.01f));
  342. REQUIRE(V.get<float>(8) == Approx( 1.0f) .epsilon(0.01f));
  343. REQUIRE(V.get<float>(9) == Approx( 0.5f) .epsilon(0.01f));
  344. REQUIRE(V.get<float>(10) == Approx( 0.5f) .epsilon(0.01f));
  345. REQUIRE(V.get<float>(11) == Approx( 0.5f) .epsilon(0.01f));
  346. REQUIRE(V.get<float>(12) == Approx(-0.5f) .epsilon(0.01f));
  347. REQUIRE(V.get<float>(13) == Approx(-0.5f) .epsilon(0.01f));
  348. REQUIRE(V.get<float>(14) == Approx(-0.5f) .epsilon(0.01f));
  349. }
  350. }
  351. }
  352. }
  353. }
  354. SCENARIO("Pack Mesh")
  355. {
  356. GIVEN("A mesh of uncompressed data")
  357. {
  358. auto S = gul::Sphere(10, 60,60);
  359. auto precompressed_size = S.calculateDeviceSize();
  360. WHEN("We pack the mesh")
  361. {
  362. gul::packMesh(S);
  363. auto compressed_size = S.calculateDeviceSize();
  364. std::cout << "Raw : " << precompressed_size << std::endl;
  365. std::cout << "Comp: " << compressed_size << std::endl;
  366. THEN("the mesh takes up less space")
  367. {
  368. REQUIRE(compressed_size < precompressed_size);
  369. }
  370. THEN("We can copy attributes to separate interleaved buffers")
  371. {
  372. auto attrCount = S.vertexCount();
  373. REQUIRE( attrCount > 0);
  374. std::vector<uint8_t> data(compressed_size);
  375. REQUIRE(attrCount == S.copyVertexAttributesInterleaved(data.data(), {&S.POSITION, &S.NORMAL} ) );
  376. }
  377. }
  378. }
  379. }
  380. SCENARIO("Mesh Dump")
  381. {
  382. auto S = gul::Sphere(10, 60,60);
  383. GIVEN("A mesh of uncompressed data")
  384. {
  385. std::ofstream out("dump.mesh",std::ios_base::binary);
  386. S.dump(out);
  387. }
  388. }