ShaderProgramCompiler.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/ShaderCompiler/ShaderProgramDump.h>
  7. #include <AnKi/Util/String.h>
  8. #include <AnKi/Gr/Common.h>
  9. namespace anki {
  10. /// @addtogroup shader_compiler
  11. /// @{
  12. inline constexpr const char* kShaderBinaryMagic = "ANKISP1"; // WARNING: If changed change kShaderBinaryVersion
  13. constexpr U32 kShaderBinaryVersion = 1;
  14. template<typename TFile>
  15. Error deserializeShaderProgramBinaryFromAnyFile(TFile& file, ShaderProgramBinary*& binary, BaseMemoryPool& pool)
  16. {
  17. BinaryDeserializer deserializer;
  18. ANKI_CHECK(deserializer.deserialize(binary, pool, file));
  19. if(memcmp(kShaderBinaryMagic, &binary->m_magic[0], strlen(kShaderBinaryMagic)) != 0)
  20. {
  21. ANKI_SHADER_COMPILER_LOGE("Corrupted or wrong version of shader binary.");
  22. return Error::kUserData;
  23. }
  24. return Error::kNone;
  25. }
  26. inline Error deserializeShaderProgramBinaryFromFile(CString fname, ShaderProgramBinary*& binary, BaseMemoryPool& pool)
  27. {
  28. File file;
  29. ANKI_CHECK(file.open(fname, FileOpenFlag::kRead | FileOpenFlag::kBinary));
  30. ANKI_CHECK(deserializeShaderProgramBinaryFromAnyFile(file, binary, pool));
  31. return Error::kNone;
  32. }
  33. /// Takes an AnKi special shader program and spits a binary.
  34. Error compileShaderProgram(CString fname, Bool spirv, ShaderProgramFilesystemInterface& fsystem, ShaderProgramPostParseInterface* postParseCallback,
  35. ShaderProgramAsyncTaskInterface* taskManager, ConstWeakArray<ShaderCompilerDefine> defines, ShaderProgramBinary*& binary);
  36. /// Free the binary created ONLY by compileShaderProgram.
  37. void freeShaderProgramBinary(ShaderProgramBinary*& binary);
  38. /// @}
  39. } // end namespace anki