ShaderProgramCompiler.cpp 6.8 KB

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