ShaderProgramCompiler.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright (C) 2009-2021, 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/ShaderCompiler/ShaderProgramCompiler.h>
  7. #include <AnKi/Util/ThreadHive.h>
  8. ANKI_TEST(ShaderCompiler, ShaderProgramCompilerSimple)
  9. {
  10. const CString sourceCode = R"(
  11. #pragma anki mutator INSTANCE_COUNT 1 2 4
  12. struct Foo
  13. {
  14. Mat4 m_mat;
  15. };
  16. struct Instanced
  17. {
  18. Mat4 m_ankiMvp;
  19. Mat3 m_ankiRotationMat;
  20. Mat4 m_ankiModelViewMat;
  21. Mat4 m_ankiPrevMvp;
  22. Foo m_foo[2];
  23. };
  24. layout(push_constant) uniform ankiMaterial
  25. {
  26. Vec4 u_whatever;
  27. Instanced u_ankiPerInstance[INSTANCE_COUNT];
  28. Vec4 u_color;
  29. };
  30. layout(set = 0, binding = 1) uniform texture2D u_tex2d;
  31. layout(set = 0, binding = 1) uniform texture3D u_tex3d;
  32. layout(set = 0, binding = 2) uniform sampler u_sampler;
  33. layout(set = 0, binding = 3) buffer ssbo
  34. {
  35. Foo u_mats[];
  36. };
  37. #pragma anki start vert
  38. out gl_PerVertex
  39. {
  40. Vec4 gl_Position;
  41. };
  42. void main()
  43. {
  44. gl_Position = u_ankiPerInstance[gl_InstanceIndex].m_ankiMvp * u_mats[gl_InstanceIndex].m_mat * Vec4(gl_VertexID);
  45. }
  46. #pragma anki end
  47. #pragma anki start frag
  48. layout(location = 0) out Vec3 out_color;
  49. void main()
  50. {
  51. out_color = Vec3(0);
  52. if(INSTANCE_COUNT == 1)
  53. out_color += textureLod(sampler2D(u_tex2d, u_sampler), Vec2(0), 0.0).rgb;
  54. else if(INSTANCE_COUNT == 2)
  55. out_color += textureLod(sampler3D(u_tex3d, u_sampler), Vec3(0), 0.0).rgb;
  56. else
  57. out_color += textureLod(sampler2D(u_tex2d, u_sampler), Vec2(0), 0.0).rgb
  58. + textureLod(sampler3D(u_tex3d, u_sampler), Vec3(0), 0.0).rgb;
  59. out_color += u_color.xyz;
  60. }
  61. #pragma anki end
  62. )";
  63. // Write the file
  64. {
  65. File file;
  66. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::WRITE));
  67. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  68. }
  69. class Fsystem : public ShaderProgramFilesystemInterface
  70. {
  71. public:
  72. Error readAllText(CString filename, StringAuto& txt) final
  73. {
  74. File file;
  75. ANKI_CHECK(file.open(filename, FileOpenFlag::READ));
  76. ANKI_CHECK(file.readAllText(txt));
  77. return Error::NONE;
  78. }
  79. } fsystem;
  80. HeapAllocator<U8> alloc(allocAligned, nullptr);
  81. const U32 threadCount = 8;
  82. ThreadHive hive(threadCount, alloc);
  83. class TaskManager : public ShaderProgramAsyncTaskInterface
  84. {
  85. public:
  86. ThreadHive* m_hive = nullptr;
  87. HeapAllocator<U8> m_alloc;
  88. void enqueueTask(void (*callback)(void* userData), void* userData)
  89. {
  90. struct Ctx
  91. {
  92. void (*m_callback)(void* userData);
  93. void* m_userData;
  94. HeapAllocator<U8> m_alloc;
  95. };
  96. Ctx* ctx = m_alloc.newInstance<Ctx>();
  97. ctx->m_callback = callback;
  98. ctx->m_userData = userData;
  99. ctx->m_alloc = m_alloc;
  100. m_hive->submitTask(
  101. [](void* userData, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* signalSemaphore) {
  102. Ctx* ctx = static_cast<Ctx*>(userData);
  103. ctx->m_callback(ctx->m_userData);
  104. auto alloc = ctx->m_alloc;
  105. alloc.deleteInstance(ctx);
  106. },
  107. ctx);
  108. }
  109. Error joinTasks()
  110. {
  111. m_hive->waitAllTasks();
  112. return Error::NONE;
  113. }
  114. } taskManager;
  115. taskManager.m_hive = &hive;
  116. taskManager.m_alloc = alloc;
  117. ShaderProgramBinaryWrapper binary(alloc);
  118. ShaderCompilerOptions compilerOptions;
  119. ANKI_TEST_EXPECT_NO_ERR(
  120. compileShaderProgram("test.glslp", fsystem, nullptr, &taskManager, alloc, compilerOptions, binary));
  121. #if 1
  122. StringAuto dis(alloc);
  123. dumpShaderProgramBinary(binary.getBinary(), dis);
  124. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  125. #endif
  126. }
  127. ANKI_TEST(ShaderCompiler, ShaderProgramCompiler)
  128. {
  129. const CString sourceCode = R"(
  130. #pragma anki mutator INSTANCE_COUNT 1 2 4 8 16 32 64
  131. #pragma anki mutator LOD 0 1 2
  132. #pragma anki mutator PASS 0 1 2 3
  133. #pragma anki mutator DIFFUSE_TEX 0 1
  134. #pragma anki mutator SPECULAR_TEX 0 1
  135. #pragma anki mutator ROUGHNESS_TEX 0 1
  136. #pragma anki mutator METAL_TEX 0 1
  137. #pragma anki mutator NORMAL_TEX 0 1
  138. #pragma anki mutator PARALLAX 0 1
  139. #pragma anki mutator EMISSIVE_TEX 0 1
  140. #pragma anki mutator BONES 0 1
  141. #pragma anki mutator VELOCITY 0 1
  142. #pragma anki rewrite_mutation PASS 1 DIFFUSE_TEX 1 to PASS 1 DIFFUSE_TEX 0
  143. #pragma anki rewrite_mutation PASS 2 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  144. #pragma anki rewrite_mutation PASS 3 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  145. #pragma anki rewrite_mutation PASS 1 SPECULAR_TEX 1 to PASS 1 SPECULAR_TEX 0
  146. #pragma anki rewrite_mutation PASS 2 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  147. #pragma anki rewrite_mutation PASS 3 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  148. #pragma anki rewrite_mutation PASS 1 ROUGHNESS_TEX 1 to PASS 1 ROUGHNESS_TEX 0
  149. #pragma anki rewrite_mutation PASS 2 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  150. #pragma anki rewrite_mutation PASS 3 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  151. #pragma anki rewrite_mutation PASS 1 METAL_TEX 1 to PASS 1 METAL_TEX 0
  152. #pragma anki rewrite_mutation PASS 2 METAL_TEX 1 to PASS 2 METAL_TEX 0
  153. #pragma anki rewrite_mutation PASS 3 METAL_TEX 1 to PASS 2 METAL_TEX 0
  154. #pragma anki rewrite_mutation PASS 1 NORMAL_TEX 1 to PASS 1 NORMAL_TEX 0
  155. #pragma anki rewrite_mutation PASS 2 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  156. #pragma anki rewrite_mutation PASS 3 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  157. #pragma anki rewrite_mutation PASS 1 EMISSIVE_TEX 1 to PASS 1 EMISSIVE_TEX 0
  158. #pragma anki rewrite_mutation PASS 2 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  159. #pragma anki rewrite_mutation PASS 3 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  160. #pragma anki rewrite_mutation PASS 1 VELOCITY 1 to PASS 1 VELOCITY 0
  161. #pragma anki rewrite_mutation PASS 2 VELOCITY 1 to PASS 2 VELOCITY 0
  162. #pragma anki rewrite_mutation PASS 3 VELOCITY 1 to PASS 2 VELOCITY 0
  163. layout(set = 0, binding = 0) uniform ankiMaterial
  164. {
  165. Mat4 u_ankiMvp[INSTANCE_COUNT];
  166. #if PASS == 0
  167. Mat3 u_ankiRotationMat[INSTANCE_COUNT];
  168. #endif
  169. #if PASS == 0 && PARALLAX == 1
  170. Mat4 u_ankiModelViewMat[INSTANCE_COUNT];
  171. #endif
  172. #if PASS == 0 && VELOCITY == 1
  173. Mat4 u_ankiPrevMvp[INSTANCE_COUNT];
  174. #endif
  175. };
  176. #if PASS == 0
  177. #if DIFFUSE_TEX == 0
  178. ANKI_SPECIALIZATION_CONSTANT_VEC3(diffColor, 0u);
  179. #else
  180. layout(set = 0, binding = 1) uniform texture2D diffTex;
  181. #endif
  182. #if SPECULAR_TEX == 0
  183. ANKI_SPECIALIZATION_CONSTANT_VEC3(specColor, 3u);
  184. #else
  185. layout(set = 0, binding = 2) uniform texture2D specTex;
  186. #endif
  187. #if ROUGHNESS_TEX == 0
  188. ANKI_SPECIALIZATION_CONSTANT_F32(roughness, 6u);
  189. #else
  190. layout(set = 0, binding = 3) uniform texture2D roughnessTex;
  191. #endif
  192. #if METAL_TEX == 0
  193. ANKI_SPECIALIZATION_CONSTANT_F32(metallic, 7u);
  194. #else
  195. layout(set = 0, binding = 4) uniform texture2D metallicTex;
  196. #endif
  197. #if EMISSIVE_TEX == 0
  198. ANKI_SPECIALIZATION_CONSTANT_VEC3(emission, 8u, Vec3(0.0));
  199. #else
  200. layout(set = 0, binding = 5) uniform texture2D emissiveTex;
  201. #endif
  202. #if PARALLAX == 1 && LOD == 0
  203. ANKI_SPECIALIZATION_CONSTANT_F32(heightMapScale, 11u);
  204. layout(set = 0, binding = 6) uniform texture2D heightTex;
  205. #endif
  206. #endif
  207. #pragma anki start vert
  208. out gl_PerVertex
  209. {
  210. Vec4 gl_Position;
  211. };
  212. void main()
  213. {
  214. gl_Position = Vec4(gl_VertexID);
  215. }
  216. #pragma anki end
  217. #pragma anki start frag
  218. layout(location = 0) out Vec3 out_color;
  219. void main()
  220. {
  221. out_color = Vec3(0.0);
  222. }
  223. #pragma anki end
  224. )";
  225. // Write the file
  226. {
  227. File file;
  228. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::WRITE));
  229. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  230. }
  231. class Fsystem : public ShaderProgramFilesystemInterface
  232. {
  233. public:
  234. Error readAllText(CString filename, StringAuto& txt) final
  235. {
  236. File file;
  237. ANKI_CHECK(file.open(filename, FileOpenFlag::READ));
  238. ANKI_CHECK(file.readAllText(txt));
  239. return Error::NONE;
  240. }
  241. } fsystem;
  242. HeapAllocator<U8> alloc(allocAligned, nullptr);
  243. const U32 threadCount = 24;
  244. ThreadHive hive(threadCount, alloc);
  245. class TaskManager : public ShaderProgramAsyncTaskInterface
  246. {
  247. public:
  248. ThreadHive* m_hive = nullptr;
  249. HeapAllocator<U8> m_alloc;
  250. void enqueueTask(void (*callback)(void* userData), void* userData)
  251. {
  252. struct Ctx
  253. {
  254. void (*m_callback)(void* userData);
  255. void* m_userData;
  256. HeapAllocator<U8> m_alloc;
  257. };
  258. Ctx* ctx = m_alloc.newInstance<Ctx>();
  259. ctx->m_callback = callback;
  260. ctx->m_userData = userData;
  261. ctx->m_alloc = m_alloc;
  262. m_hive->submitTask(
  263. [](void* userData, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* signalSemaphore) {
  264. Ctx* ctx = static_cast<Ctx*>(userData);
  265. ctx->m_callback(ctx->m_userData);
  266. auto alloc = ctx->m_alloc;
  267. alloc.deleteInstance(ctx);
  268. },
  269. ctx);
  270. }
  271. Error joinTasks()
  272. {
  273. m_hive->waitAllTasks();
  274. return Error::NONE;
  275. }
  276. } taskManager;
  277. taskManager.m_hive = &hive;
  278. taskManager.m_alloc = alloc;
  279. ShaderProgramBinaryWrapper binary(alloc);
  280. ANKI_TEST_EXPECT_NO_ERR(
  281. compileShaderProgram("test.glslp", fsystem, nullptr, &taskManager, alloc, ShaderCompilerOptions(), binary));
  282. #if 1
  283. StringAuto dis(alloc);
  284. dumpShaderProgramBinary(binary.getBinary(), dis);
  285. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  286. #endif
  287. }