ShaderProgramCompiler.cpp 6.8 KB

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