markv.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // MARK-V is a compression format for SPIR-V binaries. It strips away
  15. // non-essential information (such as result ids which can be regenerated) and
  16. // uses various bit reduction techiniques to reduce the size of the binary and
  17. // make it more similar to other compressed SPIR-V files to further improve
  18. // compression of the dataset.
  19. #ifndef SOURCE_COMP_MARKV_H_
  20. #define SOURCE_COMP_MARKV_H_
  21. #include "spirv-tools/libspirv.hpp"
  22. namespace spvtools {
  23. namespace comp {
  24. class MarkvModel;
  25. struct MarkvCodecOptions {
  26. bool validate_spirv_binary = false;
  27. };
  28. // Debug callback. Called once per instruction.
  29. // |words| is instruction SPIR-V words.
  30. // |bits| is a textual representation of the MARK-V bit sequence used to encode
  31. // the instruction (char '0' for 0, char '1' for 1).
  32. // |comment| contains all logs generated while processing the instruction.
  33. using MarkvDebugConsumer =
  34. std::function<bool(const std::vector<uint32_t>& words,
  35. const std::string& bits, const std::string& comment)>;
  36. // Logging callback. Called often (if decoder reads a single bit, the log
  37. // consumer will receive 1 character string with that bit).
  38. // This callback is more suitable for continous output than MarkvDebugConsumer,
  39. // for example if the codec crashes it would allow to pinpoint on which operand
  40. // or bit the crash happened.
  41. // |snippet| could be any atomic fragment of text logged by the codec. It can
  42. // contain a paragraph of text with newlines, or can be just one character.
  43. using MarkvLogConsumer = std::function<void(const std::string& snippet)>;
  44. // Encodes the given SPIR-V binary to MARK-V binary.
  45. // |log_consumer| is optional (pass MarkvLogConsumer() to disable).
  46. // |debug_consumer| is optional (pass MarkvDebugConsumer() to disable).
  47. spv_result_t SpirvToMarkv(
  48. spv_const_context context, const std::vector<uint32_t>& spirv,
  49. const MarkvCodecOptions& options, const MarkvModel& markv_model,
  50. MessageConsumer message_consumer, MarkvLogConsumer log_consumer,
  51. MarkvDebugConsumer debug_consumer, std::vector<uint8_t>* markv);
  52. // Decodes a SPIR-V binary from the given MARK-V binary.
  53. // |log_consumer| is optional (pass MarkvLogConsumer() to disable).
  54. // |debug_consumer| is optional (pass MarkvDebugConsumer() to disable).
  55. spv_result_t MarkvToSpirv(
  56. spv_const_context context, const std::vector<uint8_t>& markv,
  57. const MarkvCodecOptions& options, const MarkvModel& markv_model,
  58. MessageConsumer message_consumer, MarkvLogConsumer log_consumer,
  59. MarkvDebugConsumer debug_consumer, std::vector<uint32_t>* spirv);
  60. } // namespace comp
  61. } // namespace spvtools
  62. #endif // SOURCE_COMP_MARKV_H_