ShaderProgramCompiler.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (C) 2009-2019, 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, ShaderProgramCompiler)
  8. {
  9. const CString sourceCode = R"(
  10. #pragma anki mutator instanceCount INSTANCE_COUNT 1 2 4 8 16 32 64
  11. #pragma anki mutator LOD 0 1 2
  12. #pragma anki mutator PASS 0 1 2 3
  13. #pragma anki mutator DIFFUSE_TEX 0 1
  14. #pragma anki mutator SPECULAR_TEX 0 1
  15. #pragma anki mutator ROUGHNESS_TEX 0 1
  16. #pragma anki mutator METAL_TEX 0 1
  17. #pragma anki mutator NORMAL_TEX 0 1
  18. #pragma anki mutator PARALLAX 0 1
  19. #pragma anki mutator EMISSIVE_TEX 0 1
  20. #pragma anki mutator BONES 0 1
  21. #pragma anki mutator VELOCITY 0 1
  22. #pragma anki rewrite_mutation PASS 1 DIFFUSE_TEX 1 to PASS 1 DIFFUSE_TEX 0
  23. #pragma anki rewrite_mutation PASS 2 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  24. #pragma anki rewrite_mutation PASS 3 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  25. #pragma anki rewrite_mutation PASS 1 SPECULAR_TEX 1 to PASS 1 SPECULAR_TEX 0
  26. #pragma anki rewrite_mutation PASS 2 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  27. #pragma anki rewrite_mutation PASS 3 SPECULAR_TEX 1 to PASS 2 SPECULAR_TEX 0
  28. #pragma anki rewrite_mutation PASS 1 ROUGHNESS_TEX 1 to PASS 1 ROUGHNESS_TEX 0
  29. #pragma anki rewrite_mutation PASS 2 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  30. #pragma anki rewrite_mutation PASS 3 ROUGHNESS_TEX 1 to PASS 2 ROUGHNESS_TEX 0
  31. #pragma anki rewrite_mutation PASS 1 METAL_TEX 1 to PASS 1 METAL_TEX 0
  32. #pragma anki rewrite_mutation PASS 2 METAL_TEX 1 to PASS 2 METAL_TEX 0
  33. #pragma anki rewrite_mutation PASS 3 METAL_TEX 1 to PASS 2 METAL_TEX 0
  34. #pragma anki rewrite_mutation PASS 1 NORMAL_TEX 1 to PASS 1 NORMAL_TEX 0
  35. #pragma anki rewrite_mutation PASS 2 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  36. #pragma anki rewrite_mutation PASS 3 NORMAL_TEX 1 to PASS 2 NORMAL_TEX 0
  37. #pragma anki rewrite_mutation PASS 1 EMISSIVE_TEX 1 to PASS 1 EMISSIVE_TEX 0
  38. #pragma anki rewrite_mutation PASS 2 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  39. #pragma anki rewrite_mutation PASS 3 EMISSIVE_TEX 1 to PASS 2 EMISSIVE_TEX 0
  40. #pragma anki rewrite_mutation PASS 1 VELOCITY 1 to PASS 1 VELOCITY 0
  41. #pragma anki rewrite_mutation PASS 2 VELOCITY 1 to PASS 2 VELOCITY 0
  42. #pragma anki rewrite_mutation PASS 3 VELOCITY 1 to PASS 2 VELOCITY 0
  43. #pragma anki input instanced Mat4 mvp
  44. #if PASS == 0
  45. # pragma anki input instanced Mat3 rotationMat
  46. #endif
  47. #if PASS == 0 && PARALLAX == 1
  48. # pragma anki input instanced Mat4 modelViewMat
  49. #endif
  50. #if PASS == 0 && VELOCITY == 1
  51. # pragma anki input instanced Mat4 prevMvp
  52. #endif
  53. #if DIFFUSE_TEX == 0 && PASS == 0
  54. # pragma anki input const Vec3 diffColor
  55. #endif
  56. #if SPECULAR_TEX == 0 && PASS == 0
  57. # pragma anki input const Vec3 specColor
  58. #endif
  59. #if ROUGHNESS_TEX == 0 && PASS == 0
  60. # pragma anki input const F32 roughness
  61. #endif
  62. #if METAL_TEX == 0 && PASS == 0
  63. # pragma anki input const F32 metallic
  64. #endif
  65. #if EMISSIVE_TEX == 0 && PASS == 0
  66. # pragma anki input const Vec3 emission
  67. #endif
  68. #if PARALLAX == 1 && PASS == 0 && LOD == 0
  69. # pragma anki input const F32 heightMapScale
  70. #endif
  71. #if PASS == 0
  72. # pragma anki input const F32 subsurface
  73. #endif
  74. #pragma anki input sampler globalSampler
  75. #if DIFFUSE_TEX == 1 && PASS == 0
  76. # pragma anki input texture2D diffTex
  77. #endif
  78. #if SPECULAR_TEX == 1 && PASS == 0
  79. # pragma anki input texture2D specTex
  80. #endif
  81. #if ROUGHNESS_TEX == 1 && PASS == 0
  82. # pragma anki input texture2D roughnessTex
  83. #endif
  84. #if METAL_TEX == 1 && PASS == 0
  85. # pragma anki input texture2D metallicTex
  86. #endif
  87. #if NORMAL_TEX == 1 && PASS == 0 && LOD < 2
  88. # pragma anki input texture2D normalTex
  89. #endif
  90. #if PARALLAX == 1 && PASS == 0 && LOD == 0
  91. # pragma anki input texture2D heightTex
  92. #endif
  93. #if EMISSIVE_TEX == 1 && PASS == 0
  94. # pragma anki input texture2D emissiveTex
  95. #endif
  96. #pragma anki start vert
  97. out gl_PerVertex
  98. {
  99. Vec4 gl_Position;
  100. };
  101. void main()
  102. {
  103. gl_Position = Vec4(gl_VertexID);
  104. }
  105. #pragma anki end
  106. #pragma anki start frag
  107. layout(location = 0) out Vec3 out_color;
  108. void main()
  109. {
  110. out_color = Vec3(0.0);
  111. }
  112. #pragma anki end
  113. )";
  114. // Write the file
  115. {
  116. File file;
  117. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::WRITE));
  118. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  119. }
  120. class Fsystem : public ShaderProgramFilesystemInterface
  121. {
  122. public:
  123. Error readAllText(CString filename, StringAuto& txt) final
  124. {
  125. File file;
  126. ANKI_CHECK(file.open(filename, FileOpenFlag::READ));
  127. ANKI_CHECK(file.readAllText(txt));
  128. return Error::NONE;
  129. }
  130. } fsystem;
  131. HeapAllocator<U8> alloc(allocAligned, nullptr);
  132. ShaderProgramBinaryWrapper binary(alloc);
  133. ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", fsystem, alloc, 128, 1, 1, GpuVendor::AMD, binary));
  134. /*StringAuto dis(alloc);
  135. disassembleShaderProgramBinary(binary.getBinary(), dis);
  136. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());*/
  137. }