ShaderProgramCompiler.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2009-2022, 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. constexpr const char* SHADER_BINARY_MAGIC = "ANKISDR8"; ///< WARNING: If changed change SHADER_BINARY_VERSION
  13. constexpr U32 SHADER_BINARY_VERSION = 8;
  14. /// A wrapper over the POD ShaderProgramBinary class.
  15. /// @memberof ShaderProgramCompiler
  16. class ShaderProgramBinaryWrapper
  17. {
  18. friend Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterface& fsystem,
  19. ShaderProgramPostParseInterface* postParseCallback,
  20. ShaderProgramAsyncTaskInterface* taskManager, BaseMemoryPool& tempPool,
  21. const ShaderCompilerOptions& compilerOptions,
  22. ShaderProgramBinaryWrapper& binary);
  23. public:
  24. ShaderProgramBinaryWrapper(BaseMemoryPool* pool)
  25. : m_pool(pool)
  26. {
  27. }
  28. ShaderProgramBinaryWrapper(const ShaderProgramBinaryWrapper&) = delete; // Non-copyable
  29. ~ShaderProgramBinaryWrapper()
  30. {
  31. cleanup();
  32. }
  33. ShaderProgramBinaryWrapper& operator=(const ShaderProgramBinaryWrapper&) = delete; // Non-copyable
  34. Error serializeToFile(CString fname) const;
  35. Error deserializeFromFile(CString fname);
  36. template<typename TFile>
  37. Error deserializeFromAnyFile(TFile& fname);
  38. const ShaderProgramBinary& getBinary() const
  39. {
  40. ANKI_ASSERT(m_binary);
  41. return *m_binary;
  42. }
  43. private:
  44. BaseMemoryPool* m_pool = nullptr;
  45. ShaderProgramBinary* m_binary = nullptr;
  46. Bool m_singleAllocation = false;
  47. void cleanup();
  48. };
  49. template<typename TFile>
  50. Error ShaderProgramBinaryWrapper::deserializeFromAnyFile(TFile& file)
  51. {
  52. cleanup();
  53. BinaryDeserializer deserializer;
  54. ANKI_CHECK(deserializer.deserialize(m_binary, *m_pool, file));
  55. m_singleAllocation = true;
  56. if(memcmp(SHADER_BINARY_MAGIC, &m_binary->m_magic[0], strlen(SHADER_BINARY_MAGIC)) != 0)
  57. {
  58. ANKI_SHADER_COMPILER_LOGE("Corrupted or wrong version of shader binary.");
  59. return Error::kUserData;
  60. }
  61. return Error::kNone;
  62. }
  63. /// Takes an AnKi special shader program and spits a binary.
  64. Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem,
  65. ShaderProgramPostParseInterface* postParseCallback,
  66. ShaderProgramAsyncTaskInterface* taskManager, BaseMemoryPool& tempPool,
  67. const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binary);
  68. /// @}
  69. } // end namespace anki