ShaderProgramCompiler.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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,
  21. GenericMemoryPoolAllocator<U8> tempAllocator,
  22. const ShaderCompilerOptions& compilerOptions,
  23. ShaderProgramBinaryWrapper& binary);
  24. public:
  25. ShaderProgramBinaryWrapper(GenericMemoryPoolAllocator<U8> alloc)
  26. : m_alloc(alloc)
  27. {
  28. }
  29. ShaderProgramBinaryWrapper(const ShaderProgramBinaryWrapper&) = delete; // Non-copyable
  30. ~ShaderProgramBinaryWrapper()
  31. {
  32. cleanup();
  33. }
  34. ShaderProgramBinaryWrapper& operator=(const ShaderProgramBinaryWrapper&) = delete; // Non-copyable
  35. ANKI_USE_RESULT Error serializeToFile(CString fname) const;
  36. ANKI_USE_RESULT Error deserializeFromFile(CString fname);
  37. template<typename TFile>
  38. ANKI_USE_RESULT Error deserializeFromAnyFile(TFile& fname);
  39. const ShaderProgramBinary& getBinary() const
  40. {
  41. ANKI_ASSERT(m_binary);
  42. return *m_binary;
  43. }
  44. private:
  45. GenericMemoryPoolAllocator<U8> m_alloc;
  46. ShaderProgramBinary* m_binary = nullptr;
  47. Bool m_singleAllocation = false;
  48. void cleanup();
  49. };
  50. template<typename TFile>
  51. Error ShaderProgramBinaryWrapper::deserializeFromAnyFile(TFile& file)
  52. {
  53. cleanup();
  54. BinaryDeserializer deserializer;
  55. ANKI_CHECK(deserializer.deserialize(m_binary, m_alloc, file));
  56. m_singleAllocation = true;
  57. if(memcmp(SHADER_BINARY_MAGIC, &m_binary->m_magic[0], strlen(SHADER_BINARY_MAGIC)) != 0)
  58. {
  59. ANKI_SHADER_COMPILER_LOGE("Corrupted or wrong version of shader binary.");
  60. return Error::USER_DATA;
  61. }
  62. return Error::NONE;
  63. }
  64. /// Takes an AnKi special shader program and spits a binary.
  65. ANKI_USE_RESULT Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem,
  66. ShaderProgramPostParseInterface* postParseCallback,
  67. ShaderProgramAsyncTaskInterface* taskManager,
  68. GenericMemoryPoolAllocator<U8> tempAllocator,
  69. const ShaderCompilerOptions& compilerOptions,
  70. ShaderProgramBinaryWrapper& binary);
  71. /// @}
  72. } // end namespace anki