MaterialLoader.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "tests/framework/Framework.h"
  6. #include "anki/resource/MaterialLoader.h"
  7. #include "anki/misc/Xml.h"
  8. #include <unordered_map>
  9. namespace anki
  10. {
  11. static const char* MTL = R"(
  12. <material>
  13. <levelsOfDetail>3</levelsOfDetail>
  14. <shadow>1</shadow>
  15. <programs>
  16. <program>
  17. <type>vert</type>
  18. <inputs>
  19. <input>
  20. <name>anki_mvp</name>
  21. <type>mat4</type>
  22. </input>
  23. <input>
  24. <name>c</name>
  25. <type>float</type>
  26. <value>1.0</value>
  27. <const>1</const>
  28. </input>
  29. <input>
  30. <name>anki_msDepthRt</name>
  31. <type>sampler2D</type>
  32. </input>
  33. <input>
  34. <name>sampl</name>
  35. <type>sampler2D</type>
  36. <value>aasdfasdf/asdfasdf</value>
  37. </input>
  38. <input>
  39. <name>mouse</name>
  40. <type>float</type>
  41. <value>1.0</value>
  42. <inShadow>0</inShadow>
  43. </input>
  44. <input>
  45. <name>cat</name>
  46. <type>vec3</type>
  47. <value>1.0 1.0 1.0</value>
  48. <inShadow>1</inShadow>
  49. </input>
  50. </inputs>
  51. <includes><include>file.glsl</include></includes>
  52. <operations>
  53. <operation>
  54. <id>12</id>
  55. <returnType>vec3</returnType>
  56. <function>ha</function>
  57. </operation>
  58. <operation>
  59. <id>123</id>
  60. <returnType>vec3</returnType>
  61. <function>foo</function>
  62. <arguments>
  63. <argument>anki_mvp</argument>
  64. <argument>c</argument>
  65. <argument>cat</argument>
  66. <argument>out12</argument>
  67. <argument>anki_msDepthRt</argument>
  68. </arguments>
  69. </operation>
  70. <operation>
  71. <id>124</id>
  72. <returnType>void</returnType>
  73. <function>boo</function>
  74. <arguments>
  75. <argument>out123</argument>
  76. <argument>mouse</argument>
  77. <argument>sampl</argument>
  78. </arguments>
  79. </operation>
  80. </operations>
  81. </program>
  82. </programs>
  83. </material>
  84. )";
  85. ANKI_TEST(Resource, MaterialLoader)
  86. {
  87. XmlDocument doc;
  88. HeapAllocator<U8> alloc(allocAligned, nullptr);
  89. MaterialLoader loader(alloc);
  90. ANKI_TEST_EXPECT_NO_ERR(doc.parse(MTL, alloc));
  91. ANKI_TEST_EXPECT_NO_ERR(loader.parseXmlDocument(doc));
  92. {
  93. RenderingKey key(Pass::SM, 1, false, 3);
  94. loader.mutate(key);
  95. printf("%s\n", &loader.getShaderSource(ShaderType::VERTEX)[0]);
  96. std::unordered_map<std::string, Bool> map;
  97. map["anki_mvp"] = true;
  98. map["c"] = false;
  99. map["anki_msDepthRt"] = false;
  100. map["sampl"] = true;
  101. map["mouse"] = true;
  102. map["cat"] = true;
  103. Error err = loader.iterateAllInputVariables([&](const MaterialLoaderInputVariable& in) -> Error {
  104. ANKI_TEST_EXPECT_EQ(map[&in.m_name.toCString()[0]], true);
  105. if(in.m_flags.m_inBlock)
  106. {
  107. printf("var in block: %s %d %d %d %d\n",
  108. &in.m_name[0],
  109. in.m_blockInfo.m_offset,
  110. in.m_blockInfo.m_arraySize,
  111. in.m_blockInfo.m_arrayStride,
  112. in.m_blockInfo.m_matrixStride);
  113. }
  114. return ErrorCode::NONE;
  115. });
  116. (void)err;
  117. }
  118. // Check block size
  119. /*{
  120. RenderingKey key(Pass::MS_FS, 1, false, 4);
  121. loader.mutate(key);
  122. ANKI_TEST_EXPECT_EQ(loader.getUniformBlockSize(),
  123. 16 * 4 * sizeof(F32) + 4 * sizeof(F32) + 3 * sizeof(F32));
  124. }*/
  125. }
  126. } // end namespace anki