ShaderProgramCompiler.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. layout(set = 1, binding = 0) uniform u_
  18. {
  19. Mat4 u_mvp[INSTANCE_COUNT];
  20. #if PASS > 1
  21. Mat3 u_normalMat[INSTANCE_COUNT];
  22. #endif
  23. };
  24. layout(set = 1, binding = 1) uniform u2_
  25. {
  26. Mat4 u_mvp2[INSTANCE_COUNT];
  27. #if PASS > 1
  28. Mat3 u_normalMat2[INSTANCE_COUNT];
  29. #endif
  30. };
  31. #if DIFFUSE_TEX == 1
  32. layout(set = 0, binding = 0) uniform texture2D u_tex[3];
  33. #endif
  34. layout(set = 0, binding = 1) uniform sampler u_sampler;
  35. #pragma anki start vert
  36. out gl_PerVertex
  37. {
  38. Vec4 gl_Position;
  39. };
  40. void main()
  41. {
  42. gl_Position = u_mvp[gl_InstanceID] * u_mvp2[gl_InstanceID] * Vec4(gl_VertexID);
  43. }
  44. #pragma anki end
  45. #pragma anki start frag
  46. layout(location = 0) out Vec4 out_color;
  47. void main()
  48. {
  49. #if DIFFUSE_TEX == 1
  50. out_color = texture(sampler2D(u_tex[0], u_sampler), Vec2(0));
  51. #else
  52. out_color = Vec4(0);
  53. #endif
  54. }
  55. #pragma anki end
  56. )";
  57. // Write the file
  58. {
  59. File file;
  60. ANKI_TEST_EXPECT_NO_ERR(file.open("test.glslp", FileOpenFlag::WRITE));
  61. ANKI_TEST_EXPECT_NO_ERR(file.writeText(sourceCode));
  62. }
  63. class Fsystem : public ShaderProgramFilesystemInterface
  64. {
  65. public:
  66. Error readAllText(CString filename, StringAuto& txt) final
  67. {
  68. File file;
  69. ANKI_CHECK(file.open(filename, FileOpenFlag::READ));
  70. ANKI_CHECK(file.readAllText(txt));
  71. return Error::NONE;
  72. }
  73. } fsystem;
  74. HeapAllocator<U8> alloc(allocAligned, nullptr);
  75. ShaderProgramBinaryWrapper binary(alloc);
  76. ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", fsystem, alloc, 128, 1, 1, GpuVendor::AMD, binary));
  77. #if 1
  78. StringAuto dis(alloc);
  79. disassembleShaderProgramBinary(binary.getBinary(), dis);
  80. ANKI_LOGI("Binary disassembly:\n%s\n", dis.cstr());
  81. #endif
  82. }