ShaderProgramCompiler.cpp 3.3 KB

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