ShaderProgramCompiler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (C) 2009-2020, 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/shader_compiler/ShaderProgramCompiler.h>
  7. ANKI_TEST(ShaderCompiler, ShaderProgramCompilerSimple)
  8. {
  9. const CString sourceCode = R"(
  10. #pragma anki mutator INSTANCE_COUNT 1 2 4
  11. struct Foo
  12. {
  13. Mat4 m_mat;
  14. };
  15. struct Instanced
  16. {
  17. Mat4 m_ankiMvp;
  18. Mat3 m_ankiRotationMat;
  19. Mat4 m_ankiModelViewMat;
  20. Mat4 m_ankiPrevMvp;
  21. Foo m_foo[2];
  22. };
  23. layout(push_constant) uniform ankiMaterial
  24. {
  25. Vec4 u_whatever;
  26. Instanced u_ankiPerInstance[INSTANCE_COUNT];
  27. Vec4 u_color;
  28. };
  29. #pragma anki start vert
  30. out gl_PerVertex
  31. {
  32. Vec4 gl_Position;
  33. };
  34. void main()
  35. {
  36. gl_Position = u_ankiPerInstance[gl_InstanceID].m_ankiMvp * Vec4(gl_VertexID);
  37. }
  38. #pragma anki end
  39. #pragma anki start frag
  40. layout(location = 0) out Vec3 out_color;
  41. void main()
  42. {
  43. out_color = Vec3(0.0) + u_color.xyz;
  44. }
  45. #pragma anki end
  46. )";
  47. // Write the file
  48. {
  49. File file;
  50. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::WRITE));
  51. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  52. }
  53. class Fsystem : public ShaderProgramFilesystemInterface
  54. {
  55. public:
  56. Error readAllText(CString filename, StringAuto& txt) final
  57. {
  58. File file;
  59. ANKI_CHECK(file.open(filename, FileOpenFlag::READ));
  60. ANKI_CHECK(file.readAllText(txt));
  61. return Error::NONE;
  62. }
  63. } fsystem;
  64. HeapAllocator<U8> alloc(allocAligned, nullptr);
  65. ShaderProgramBinaryWrapper binary(alloc);
  66. BindlessLimits bindlessLimits;
  67. GpuDeviceCapabilities gpuCapabilities;
  68. ANKI_TEST_EXPECT_NO_ERR(
  69. compileShaderProgram("test.glslp", fsystem, nullptr, alloc, gpuCapabilities, bindlessLimits, binary));
  70. #if 1
  71. StringAuto dis(alloc);
  72. dumpShaderProgramBinary(binary.getBinary(), dis);
  73. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  74. #endif
  75. }
  76. ANKI_TEST(ShaderCompiler, ShaderProgramCompiler)
  77. {
  78. const CString sourceCode = R"(
  79. #pragma anki mutator INSTANCE_COUNT 1 2 4 8 16 32 64
  80. #pragma anki mutator LOD 0 1 2
  81. #pragma anki mutator PASS 0 1 2 3
  82. #pragma anki mutator DIFFUSE_TEX 0 1
  83. #pragma anki mutator SPECULAR_TEX 0 1
  84. #pragma anki mutator ROUGHNESS_TEX 0 1
  85. #pragma anki mutator METAL_TEX 0 1
  86. #pragma anki mutator NORMAL_TEX 0 1
  87. #pragma anki mutator PARALLAX 0 1
  88. #pragma anki mutator EMISSIVE_TEX 0 1
  89. #pragma anki mutator BONES 0 1
  90. #pragma anki mutator VELOCITY 0 1
  91. #pragma anki rewrite_mutation PASS 1 DIFFUSE_TEX 1 to PASS 1 DIFFUSE_TEX 0
  92. #pragma anki rewrite_mutation PASS 2 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  93. #pragma anki rewrite_mutation PASS 3 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  94. #pragma anki rewrite_mutation PASS 1 SPECULAR_TEX 1 to PASS 1 SPECULAR_TEX 0
  95. #pragma anki rewrite_mutation PASS 2 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  96. #pragma anki rewrite_mutation PASS 3 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  97. #pragma anki rewrite_mutation PASS 1 ROUGHNESS_TEX 1 to PASS 1 ROUGHNESS_TEX 0
  98. #pragma anki rewrite_mutation PASS 2 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  99. #pragma anki rewrite_mutation PASS 3 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  100. #pragma anki rewrite_mutation PASS 1 METAL_TEX 1 to PASS 1 METAL_TEX 0
  101. #pragma anki rewrite_mutation PASS 2 METAL_TEX 1 to PASS 2 METAL_TEX 0
  102. #pragma anki rewrite_mutation PASS 3 METAL_TEX 1 to PASS 2 METAL_TEX 0
  103. #pragma anki rewrite_mutation PASS 1 NORMAL_TEX 1 to PASS 1 NORMAL_TEX 0
  104. #pragma anki rewrite_mutation PASS 2 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  105. #pragma anki rewrite_mutation PASS 3 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  106. #pragma anki rewrite_mutation PASS 1 EMISSIVE_TEX 1 to PASS 1 EMISSIVE_TEX 0
  107. #pragma anki rewrite_mutation PASS 2 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  108. #pragma anki rewrite_mutation PASS 3 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  109. #pragma anki rewrite_mutation PASS 1 VELOCITY 1 to PASS 1 VELOCITY 0
  110. #pragma anki rewrite_mutation PASS 2 VELOCITY 1 to PASS 2 VELOCITY 0
  111. #pragma anki rewrite_mutation PASS 3 VELOCITY 1 to PASS 2 VELOCITY 0
  112. layout(set = 0, binding = 0) uniform ankiMaterial
  113. {
  114. Mat4 u_ankiMvp[INSTANCE_COUNT];
  115. #if PASS == 0
  116. Mat3 u_ankiRotationMat[INSTANCE_COUNT];
  117. #endif
  118. #if PASS == 0 && PARALLAX == 1
  119. Mat4 u_ankiModelViewMat[INSTANCE_COUNT];
  120. #endif
  121. #if PASS == 0 && VELOCITY == 1
  122. Mat4 u_ankiPrevMvp[INSTANCE_COUNT];
  123. #endif
  124. };
  125. #if PASS == 0
  126. #if DIFFUSE_TEX == 0
  127. ANKI_SPECIALIZATION_CONSTANT_VEC3(diffColor, 0, Vec3(0));
  128. #else
  129. layout(set = 0, binding = 1) uniform texture2D diffTex;
  130. #endif
  131. #if SPECULAR_TEX == 0
  132. ANKI_SPECIALIZATION_CONSTANT_VEC3(specColor, 3, Vec3(0));
  133. #else
  134. layout(set = 0, binding = 2) uniform texture2D specTex;
  135. #endif
  136. #if ROUGHNESS_TEX == 0
  137. ANKI_SPECIALIZATION_CONSTANT_F32(roughness, 6, 0.0);
  138. #else
  139. layout(set = 0, binding = 3) uniform texture2D roughnessTex;
  140. #endif
  141. #if METAL_TEX == 0
  142. ANKI_SPECIALIZATION_CONSTANT_F32(metallic, 7, 0.0);
  143. #else
  144. layout(set = 0, binding = 4) uniform texture2D metallicTex;
  145. #endif
  146. #if EMISSIVE_TEX == 0
  147. ANKI_SPECIALIZATION_CONSTANT_VEC3(emission, 8, Vec3(0.0));
  148. #else
  149. layout(set = 0, binding = 5) uniform texture2D emissiveTex;
  150. #endif
  151. #if PARALLAX == 1 && LOD == 0
  152. ANKI_SPECIALIZATION_CONSTANT_F32(heightMapScale, 11, 0.0);
  153. layout(set = 0, binding = 6) uniform texture2D heightTex;
  154. #endif
  155. #endif
  156. #pragma anki start vert
  157. out gl_PerVertex
  158. {
  159. Vec4 gl_Position;
  160. };
  161. void main()
  162. {
  163. gl_Position = Vec4(gl_VertexID);
  164. }
  165. #pragma anki end
  166. #pragma anki start frag
  167. layout(location = 0) out Vec3 out_color;
  168. void main()
  169. {
  170. out_color = Vec3(0.0);
  171. }
  172. #pragma anki end
  173. )";
  174. // Write the file
  175. {
  176. File file;
  177. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::WRITE));
  178. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  179. }
  180. class Fsystem : public ShaderProgramFilesystemInterface
  181. {
  182. public:
  183. Error readAllText(CString filename, StringAuto& txt) final
  184. {
  185. File file;
  186. ANKI_CHECK(file.open(filename, FileOpenFlag::READ));
  187. ANKI_CHECK(file.readAllText(txt));
  188. return Error::NONE;
  189. }
  190. } fsystem;
  191. HeapAllocator<U8> alloc(allocAligned, nullptr);
  192. ShaderProgramBinaryWrapper binary(alloc);
  193. BindlessLimits bindlessLimits;
  194. GpuDeviceCapabilities gpuCapabilities;
  195. ANKI_TEST_EXPECT_NO_ERR(
  196. compileShaderProgram("test.glslp", fsystem, nullptr, alloc, gpuCapabilities, bindlessLimits, binary));
  197. #if 1
  198. StringAuto dis(alloc);
  199. dumpShaderProgramBinary(binary.getBinary(), dis);
  200. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  201. #endif
  202. }