smolv.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // smol-v - public domain - https://github.com/aras-p/smol-v
  2. // authored 2016-2020 by Aras Pranckevicius
  3. // no warranty implied; use at your own risk
  4. // See end of file for license information.
  5. //
  6. //
  7. // ### OVERVIEW:
  8. //
  9. // SMOL-V encodes Vulkan/Khronos SPIR-V format programs into a form that is smaller, and is more
  10. // compressible. Normally no changes to the programs are done; they decode
  11. // into exactly same program as what was encoded. Optionally, debug information
  12. // can be removed too.
  13. //
  14. // SPIR-V is a very verbose format, several times larger than same programs expressed in other
  15. // shader formats (e.g. DX11 bytecode, GLSL, DX9 bytecode etc.). The SSA-form with ever increasing
  16. // IDs is not very appreciated by regular data compressors either. SMOL-V does several things
  17. // to improve this:
  18. // - Many words, especially ones that most often have small values, are encoded using
  19. // "varint" scheme (1-5 bytes per word, with just one byte for values in 0..127 range).
  20. // See https://developers.google.com/protocol-buffers/docs/encoding
  21. // - Some IDs used in the program are delta-encoded, relative to previously seen IDs (e.g. Result
  22. // IDs). Often instructions reference things that were computed just before, so this results in
  23. // small deltas. These values are also encoded using "varint" scheme.
  24. // - Reordering instruction opcodes so that the most common ones are the smallest values, for smaller
  25. // varint encoding.
  26. // - Encoding several instructions in a more compact form, e.g. the "typical <=4 component swizzle"
  27. // shape of a VectorShuffle instruction, or sequences of MemberDecorate instructions.
  28. //
  29. // A somewhat similar utility is spirv-remap from glslang, see
  30. // https://github.com/KhronosGroup/glslang/blob/master/README-spirv-remap.txt
  31. //
  32. //
  33. // ### USAGE:
  34. //
  35. // Add source/smolv.h and source/smolv.cpp to your C++ project build.
  36. // Currently it might require C++11 or somesuch; I only tested with Visual Studio 2017/2019, Mac Xcode 11 and Gcc 5.4.
  37. //
  38. // smolv::Encode and smolv::Decode is the basic functionality.
  39. //
  40. // Other functions are for development/statistics purposes, to figure out frequencies and
  41. // distributions of the instructions.
  42. //
  43. // There's a test + compression benchmarking suite in testing/testmain.cpp; using that needs adding
  44. // other files under testing/external to the build too (3rd party code: glslang remapper, Zstd, LZ4).
  45. //
  46. //
  47. // ### LIMITATIONS / TODO:
  48. //
  49. // - SPIR-V where the words got stored in big-endian layout is not supported yet.
  50. // - The whole thing might not work on Big-Endian CPUs. It might, but I'm not 100% sure.
  51. // - Not much prevention is done against malformed/corrupted inputs, TODO.
  52. // - Out of memory cases are not handled. The code will either throw exception
  53. // or crash, depending on your compilation flags.
  54. #pragma once
  55. #include <stdint.h>
  56. #include <vector>
  57. #include <cstddef>
  58. namespace smolv
  59. {
  60. typedef std::vector<uint8_t> ByteArray;
  61. enum EncodeFlags
  62. {
  63. kEncodeFlagNone = 0,
  64. kEncodeFlagStripDebugInfo = (1<<0), // Strip all optional SPIR-V instructions (debug names etc.)
  65. };
  66. enum DecodeFlags
  67. {
  68. kDecodeFlagNone = 0,
  69. kDecodeFlagUse20160831AsZeroVersion = (1 << 0), // For "version zero" of SMOL-V encoding, use 2016 08 31 code path (this is what happens to be used by Unity 2017-2020)
  70. };
  71. // Preserve *some* OpName debug names.
  72. // Return true to preserve, false to strip.
  73. // This is really only used to implement a workaround for problems with some Vulkan drivers.
  74. typedef bool(*StripOpNameFilterFunc)(const char* name);
  75. // -------------------------------------------------------------------
  76. // Encoding / Decoding
  77. // Encode SPIR-V into SMOL-V.
  78. //
  79. // Resulting data is appended to outSmolv array (the array is not cleared).
  80. //
  81. // flags is bitset of EncodeFlags values.
  82. //
  83. // Returns false on malformed SPIR-V input; if that happens the output array might get
  84. // partial/broken SMOL-V program.
  85. bool Encode(const void* spirvData, size_t spirvSize, ByteArray& outSmolv, uint32_t flags = kEncodeFlagNone, StripOpNameFilterFunc stripFilter = 0);
  86. // Decode SMOL-V into SPIR-V.
  87. //
  88. // Resulting data is written into the passed buffer. Get required buffer space with
  89. // GetDecodeBufferSize; this is the size of decoded SPIR-V program.
  90. //
  91. // flags is bitset of DecodeFlags values.
  92. // Decoding does no memory allocations.
  93. //
  94. // Returns false on malformed input; if that happens the output buffer might be only partially
  95. // written to.
  96. bool Decode(const void* smolvData, size_t smolvSize, void* spirvOutputBuffer, size_t spirvOutputBufferSize, uint32_t flags = kDecodeFlagNone);
  97. // Given a SMOL-V program, get size of the decoded SPIR-V program.
  98. // This is the buffer size that Decode expects.
  99. //
  100. // Returns zero on malformed input (just checks the header, not the full input).
  101. size_t GetDecodedBufferSize(const void* smolvData, size_t smolvSize);
  102. // -------------------------------------------------------------------
  103. // Computing instruction statistics on SPIR-V/SMOL-V programs
  104. struct Stats;
  105. Stats* StatsCreate();
  106. void StatsDelete(Stats* s);
  107. bool StatsCalculate(Stats* stats, const void* spirvData, size_t spirvSize);
  108. bool StatsCalculateSmol(Stats* stats, const void* smolvData, size_t smolvSize);
  109. void StatsPrint(const Stats* stats);
  110. } // namespace smolv
  111. // ------------------------------------------------------------------------------
  112. // This software is available under 2 licenses -- choose whichever you prefer.
  113. // ------------------------------------------------------------------------------
  114. // ALTERNATIVE A - MIT License
  115. // Copyright (c) 2016-2020 Aras Pranckevicius
  116. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  117. // this software and associated documentation files (the "Software"), to deal in
  118. // the Software without restriction, including without limitation the rights to
  119. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  120. // of the Software, and to permit persons to whom the Software is furnished to do
  121. // so, subject to the following conditions:
  122. // The above copyright notice and this permission notice shall be included in all
  123. // copies or substantial portions of the Software.
  124. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  125. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  126. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  127. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  128. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  129. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  130. // SOFTWARE.
  131. // ------------------------------------------------------------------------------
  132. // ALTERNATIVE B - Public Domain (www.unlicense.org)
  133. // This is free and unencumbered software released into the public domain.
  134. // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  135. // software, either in source code form or as a compiled binary, for any purpose,
  136. // commercial or non-commercial, and by any means.
  137. // In jurisdictions that recognize copyright laws, the author or authors of this
  138. // software dedicate any and all copyright interest in the software to the public
  139. // domain. We make this dedication for the benefit of the public at large and to
  140. // the detriment of our heirs and successors. We intend this dedication to be an
  141. // overt act of relinquishment in perpetuity of all present and future rights to
  142. // this software under copyright law.
  143. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  144. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  145. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  146. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  147. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  148. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  149. // ------------------------------------------------------------------------------