ShaderProgramCompiler.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 LOD 0 1 2
  11. #pragma anki mutator PASS 0 1 2 3
  12. #pragma anki mutator DIFFUSE_TEX 0 1
  13. #pragma anki rewrite_mutation PASS 1 DIFFUSE_TEX 1 to PASS 1 DIFFUSE_TEX 0
  14. #pragma anki rewrite_mutation PASS 2 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  15. #pragma anki rewrite_mutation PASS 3 DIFFUSE_TEX 1 to PASS 2 DIFFUSE_TEX 0
  16. ANKI_SPECIALIZATION_CONSTANT_I32(INSTANCE_COUNT, 0, 1);
  17. struct PerInstance
  18. {
  19. Mat4 m_mvp;
  20. #if PASS > 1
  21. Mat3 m_normalMat[2];
  22. #endif
  23. };
  24. layout(set = 1, binding = 0) uniform perInstance
  25. {
  26. PerInstance u_perInstance[INSTANCE_COUNT];
  27. };
  28. layout(set = 1, binding = 1) buffer perDrawcall
  29. {
  30. Vec4 u_color;
  31. #if PASS > 1
  32. Mat3 u_someMat;
  33. #endif
  34. };
  35. #if DIFFUSE_TEX == 1
  36. layout(set = 0, binding = 0) uniform texture2D u_tex[3];
  37. #endif
  38. layout(set = 0, binding = 1) uniform sampler u_sampler;
  39. #pragma anki start vert
  40. ANKI_SPECIALIZATION_CONSTANT_F32(specConst, 1, 0);
  41. out gl_PerVertex
  42. {
  43. Vec4 gl_Position;
  44. };
  45. void main()
  46. {
  47. gl_Position = u_perInstance[gl_InstanceID].m_mvp * Vec4(specConst);
  48. }
  49. #pragma anki end
  50. #pragma anki start frag
  51. layout(location = 0) out Vec4 out_color;
  52. void main()
  53. {
  54. #if DIFFUSE_TEX == 1
  55. out_color = texture(sampler2D(u_tex[0], u_sampler), Vec2(0));
  56. #else
  57. out_color = u_color;
  58. #endif
  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. ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", fsystem, alloc, 128, 1, 1, GpuVendor::AMD, binary));
  82. #if 1
  83. StringAuto dis(alloc);
  84. disassembleShaderProgramBinary(binary.getBinary(), dis);
  85. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  86. #endif
  87. }