ShaderProgramCompiler.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright (C) 2009-2023, 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 vert
  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 frag
  62. )";
  63. // Write the file
  64. {
  65. File file;
  66. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::kWrite));
  67. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  68. }
  69. class Fsystem : public ShaderProgramFilesystemInterface
  70. {
  71. public:
  72. Error readAllText(CString filename, ShaderCompilerString& txt) final
  73. {
  74. File file;
  75. ANKI_CHECK(file.open(filename, FileOpenFlag::kRead));
  76. ANKI_CHECK(file.readAllText(txt));
  77. return Error::kNone;
  78. }
  79. } fsystem;
  80. HeapMemoryPool pool(allocAligned, nullptr);
  81. const U32 threadCount = 8;
  82. ThreadHive hive(threadCount, &pool);
  83. class TaskManager : public ShaderProgramAsyncTaskInterface
  84. {
  85. public:
  86. ThreadHive* m_hive = nullptr;
  87. HeapMemoryPool* m_pool;
  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. HeapMemoryPool* m_pool;
  95. };
  96. Ctx* ctx = newInstance<Ctx>(*m_pool);
  97. ctx->m_callback = callback;
  98. ctx->m_userData = userData;
  99. ctx->m_pool = m_pool;
  100. m_hive->submitTask(
  101. [](void* userData, [[maybe_unused]] U32 threadId, [[maybe_unused]] ThreadHive& hive,
  102. [[maybe_unused]] ThreadHiveSemaphore* signalSemaphore) {
  103. Ctx* ctx = static_cast<Ctx*>(userData);
  104. ctx->m_callback(ctx->m_userData);
  105. deleteInstance(*ctx->m_pool, ctx);
  106. },
  107. ctx);
  108. }
  109. Error joinTasks()
  110. {
  111. m_hive->waitAllTasks();
  112. return Error::kNone;
  113. }
  114. } taskManager;
  115. taskManager.m_hive = &hive;
  116. taskManager.m_pool = &pool;
  117. ShaderProgramBinary* binary;
  118. ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", true, fsystem, nullptr, &taskManager, {}, binary));
  119. #if 1
  120. ShaderCompilerString dis;
  121. ShaderDumpOptions options;
  122. dumpShaderProgramBinary(options, *binary, dis);
  123. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  124. #endif
  125. }
  126. ANKI_TEST(ShaderCompiler, ShaderProgramCompiler)
  127. {
  128. const CString sourceCode = R"(
  129. #pragma anki mutator INSTANCE_COUNT 1 2 4 8 16 32 64
  130. #pragma anki mutator LOD 0 1 2
  131. #pragma anki mutator PASS 0 1 2 3
  132. #pragma anki mutator DIFFUSE_TEX 0 1
  133. #pragma anki mutator SPECULAR_TEX 0 1
  134. #pragma anki mutator ROUGHNESS_TEX 0 1
  135. #pragma anki mutator METAL_TEX 0 1
  136. #pragma anki mutator NORMAL_TEX 0 1
  137. #pragma anki mutator PARALLAX 0 1
  138. #pragma anki mutator EMISSIVE_TEX 0 1
  139. #pragma anki mutator BONES 0 1
  140. #pragma anki mutator VELOCITY 0 1
  141. #pragma anki rewrite_mutation PASS 1 DIFFUSE_TEX 1 to PASS 1 DIFFUSE_TEX 0
  142. #pragma anki rewrite_mutation PASS 2 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  143. #pragma anki rewrite_mutation PASS 3 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  144. #pragma anki rewrite_mutation PASS 1 SPECULAR_TEX 1 to PASS 1 SPECULAR_TEX 0
  145. #pragma anki rewrite_mutation PASS 2 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  146. #pragma anki rewrite_mutation PASS 3 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  147. #pragma anki rewrite_mutation PASS 1 ROUGHNESS_TEX 1 to PASS 1 ROUGHNESS_TEX 0
  148. #pragma anki rewrite_mutation PASS 2 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  149. #pragma anki rewrite_mutation PASS 3 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  150. #pragma anki rewrite_mutation PASS 1 METAL_TEX 1 to PASS 1 METAL_TEX 0
  151. #pragma anki rewrite_mutation PASS 2 METAL_TEX 1 to PASS 2 METAL_TEX 0
  152. #pragma anki rewrite_mutation PASS 3 METAL_TEX 1 to PASS 2 METAL_TEX 0
  153. #pragma anki rewrite_mutation PASS 1 NORMAL_TEX 1 to PASS 1 NORMAL_TEX 0
  154. #pragma anki rewrite_mutation PASS 2 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  155. #pragma anki rewrite_mutation PASS 3 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  156. #pragma anki rewrite_mutation PASS 1 EMISSIVE_TEX 1 to PASS 1 EMISSIVE_TEX 0
  157. #pragma anki rewrite_mutation PASS 2 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  158. #pragma anki rewrite_mutation PASS 3 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  159. #pragma anki rewrite_mutation PASS 1 VELOCITY 1 to PASS 1 VELOCITY 0
  160. #pragma anki rewrite_mutation PASS 2 VELOCITY 1 to PASS 2 VELOCITY 0
  161. #pragma anki rewrite_mutation PASS 3 VELOCITY 1 to PASS 2 VELOCITY 0
  162. layout(set = 0, binding = 0) uniform ankiMaterial
  163. {
  164. Mat4 u_ankiMvp[INSTANCE_COUNT];
  165. #if PASS == 0
  166. Mat3 u_ankiRotationMat[INSTANCE_COUNT];
  167. #endif
  168. #if PASS == 0 && PARALLAX == 1
  169. Mat4 u_ankiModelViewMat[INSTANCE_COUNT];
  170. #endif
  171. #if PASS == 0 && VELOCITY == 1
  172. Mat4 u_ankiPrevMvp[INSTANCE_COUNT];
  173. #endif
  174. };
  175. #if PASS == 0
  176. #if DIFFUSE_TEX == 0
  177. ANKI_SPECIALIZATION_CONSTANT_VEC3(diffColor, 0u);
  178. #else
  179. layout(set = 0, binding = 1) uniform texture2D diffTex;
  180. #endif
  181. #if SPECULAR_TEX == 0
  182. ANKI_SPECIALIZATION_CONSTANT_VEC3(specColor, 3u);
  183. #else
  184. layout(set = 0, binding = 2) uniform texture2D specTex;
  185. #endif
  186. #if ROUGHNESS_TEX == 0
  187. ANKI_SPECIALIZATION_CONSTANT_F32(roughness, 6u);
  188. #else
  189. layout(set = 0, binding = 3) uniform texture2D roughnessTex;
  190. #endif
  191. #if METAL_TEX == 0
  192. ANKI_SPECIALIZATION_CONSTANT_F32(metallic, 7u);
  193. #else
  194. layout(set = 0, binding = 4) uniform texture2D metallicTex;
  195. #endif
  196. #if EMISSIVE_TEX == 0
  197. ANKI_SPECIALIZATION_CONSTANT_VEC3(emission, 8u, Vec3(0.0));
  198. #else
  199. layout(set = 0, binding = 5) uniform texture2D emissiveTex;
  200. #endif
  201. #if PARALLAX == 1 && LOD == 0
  202. ANKI_SPECIALIZATION_CONSTANT_F32(heightMapScale, 11u);
  203. layout(set = 0, binding = 6) uniform texture2D heightTex;
  204. #endif
  205. #endif
  206. #pragma anki start vert
  207. out gl_PerVertex
  208. {
  209. Vec4 gl_Position;
  210. };
  211. void main()
  212. {
  213. gl_Position = Vec4(gl_VertexIndex);
  214. }
  215. #pragma anki end vert
  216. #pragma anki start frag
  217. layout(location = 0) out Vec3 out_color;
  218. void main()
  219. {
  220. out_color = Vec3(0.0);
  221. }
  222. #pragma anki end frag
  223. )";
  224. // Write the file
  225. {
  226. File file;
  227. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::kWrite));
  228. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  229. }
  230. class Fsystem : public ShaderProgramFilesystemInterface
  231. {
  232. public:
  233. Error readAllText(CString filename, ShaderCompilerString& txt) final
  234. {
  235. File file;
  236. ANKI_CHECK(file.open(filename, FileOpenFlag::kRead));
  237. ANKI_CHECK(file.readAllText(txt));
  238. return Error::kNone;
  239. }
  240. } fsystem;
  241. HeapMemoryPool pool(allocAligned, nullptr);
  242. const U32 threadCount = 24;
  243. ThreadHive hive(threadCount, &pool);
  244. class TaskManager : public ShaderProgramAsyncTaskInterface
  245. {
  246. public:
  247. ThreadHive* m_hive = nullptr;
  248. HeapMemoryPool* m_pool = nullptr;
  249. void enqueueTask(void (*callback)(void* userData), void* userData)
  250. {
  251. struct Ctx
  252. {
  253. void (*m_callback)(void* userData);
  254. void* m_userData;
  255. HeapMemoryPool* m_pool;
  256. };
  257. Ctx* ctx = newInstance<Ctx>(*m_pool);
  258. ctx->m_callback = callback;
  259. ctx->m_userData = userData;
  260. ctx->m_pool = m_pool;
  261. m_hive->submitTask(
  262. [](void* userData, [[maybe_unused]] U32 threadId, [[maybe_unused]] ThreadHive& hive,
  263. [[maybe_unused]] ThreadHiveSemaphore* signalSemaphore) {
  264. Ctx* ctx = static_cast<Ctx*>(userData);
  265. ctx->m_callback(ctx->m_userData);
  266. deleteInstance(*ctx->m_pool, ctx);
  267. },
  268. ctx);
  269. }
  270. Error joinTasks()
  271. {
  272. m_hive->waitAllTasks();
  273. return Error::kNone;
  274. }
  275. } taskManager;
  276. taskManager.m_hive = &hive;
  277. taskManager.m_pool = &pool;
  278. ShaderProgramBinary* binary;
  279. ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", true, fsystem, nullptr, &taskManager, {}, binary));
  280. #if 1
  281. ShaderCompilerString dis;
  282. ShaderDumpOptions options;
  283. dumpShaderProgramBinary(options, *binary, dis);
  284. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  285. #endif
  286. }