| 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 | |
| 55 | #pragma once |
| 56 | |
| 57 | #include <stdint.h> |
| 58 | #include <vector> |
| 59 | #include <cstddef> |
| 60 | |
| 61 | namespace smolv |
| 62 | { |
| 63 | typedef std::vector<uint8_t> ByteArray; |
| 64 | |
| 65 | enum EncodeFlags |
| 66 | { |
| 67 | kEncodeFlagNone = 0, |
| 68 | kEncodeFlagStripDebugInfo = (1<<0), // Strip all optional SPIR-V instructions (debug names etc.) |
| 69 | }; |
| 70 | enum DecodeFlags |
| 71 | { |
| 72 | kDecodeFlagNone = 0, |
| 73 | 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) |
| 74 | }; |
| 75 | |
| 76 | // Preserve *some* OpName debug names. |
| 77 | // Return true to preserve, false to strip. |
| 78 | // This is really only used to implement a workaround for problems with some Vulkan drivers. |
| 79 | typedef bool(*StripOpNameFilterFunc)(const char* name); |
| 80 | |
| 81 | // ------------------------------------------------------------------- |
| 82 | // Encoding / Decoding |
| 83 | |
| 84 | // Encode SPIR-V into SMOL-V. |
| 85 | // |
| 86 | // Resulting data is appended to outSmolv array (the array is not cleared). |
| 87 | // |
| 88 | // flags is bitset of EncodeFlags values. |
| 89 | // |
| 90 | // Returns false on malformed SPIR-V input; if that happens the output array might get |
| 91 | // partial/broken SMOL-V program. |
| 92 | bool Encode(const void* spirvData, size_t spirvSize, ByteArray& outSmolv, uint32_t flags = kEncodeFlagNone, StripOpNameFilterFunc stripFilter = 0); |
| 93 | |
| 94 | |
| 95 | // Decode SMOL-V into SPIR-V. |
| 96 | // |
| 97 | // Resulting data is written into the passed buffer. Get required buffer space with |
| 98 | // GetDecodeBufferSize; this is the size of decoded SPIR-V program. |
| 99 | // |
| 100 | // flags is bitset of DecodeFlags values. |
| 101 | |
| 102 | // Decoding does no memory allocations. |
| 103 | // |
| 104 | // Returns false on malformed input; if that happens the output buffer might be only partially |
| 105 | // written to. |
| 106 | bool Decode(const void* smolvData, size_t smolvSize, void* spirvOutputBuffer, size_t spirvOutputBufferSize, uint32_t flags = kDecodeFlagNone); |
| 107 | |
| 108 | |
| 109 | // Given a SMOL-V program, get size of the decoded SPIR-V program. |
| 110 | // This is the buffer size that Decode expects. |
| 111 | // |
| 112 | // Returns zero on malformed input (just checks the header, not the full input). |
| 113 | size_t GetDecodedBufferSize(const void* smolvData, size_t smolvSize); |
| 114 | |
| 115 | |
| 116 | // ------------------------------------------------------------------- |
| 117 | // Computing instruction statistics on SPIR-V/SMOL-V programs |
| 118 | |
| 119 | struct Stats; |
| 120 | |
| 121 | Stats* StatsCreate(); |
| 122 | void StatsDelete(Stats* s); |
| 123 | |
| 124 | bool StatsCalculate(Stats* stats, const void* spirvData, size_t spirvSize); |
| 125 | bool StatsCalculateSmol(Stats* stats, const void* smolvData, size_t smolvSize); |
| 126 | void StatsPrint(const Stats* stats); |
| 127 | |
| 128 | } // namespace smolv |
| 129 | |
| 130 | |
| 131 | // ------------------------------------------------------------------------------ |
| 132 | // This software is available under 2 licenses -- choose whichever you prefer. |
| 133 | // ------------------------------------------------------------------------------ |
| 134 | // ALTERNATIVE A - MIT License |
| 135 | // Copyright (c) 2016-2020 Aras Pranckevicius |
| 136 | // Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 137 | // this software and associated documentation files (the "Software"), to deal in |
| 138 | // the Software without restriction, including without limitation the rights to |
| 139 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 140 | // of the Software, and to permit persons to whom the Software is furnished to do |
| 141 | // so, subject to the following conditions: |
| 142 | // The above copyright notice and this permission notice shall be included in all |
| 143 | // copies or substantial portions of the Software. |
| 144 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 145 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 146 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 147 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 148 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 149 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 150 | // SOFTWARE. |
| 151 | // ------------------------------------------------------------------------------ |
| 152 | // ALTERNATIVE B - Public Domain (www.unlicense.org) |
| 153 | // This is free and unencumbered software released into the public domain. |
| 154 | // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this |
| 155 | // software, either in source code form or as a compiled binary, for any purpose, |
| 156 | // commercial or non-commercial, and by any means. |
| 157 | // In jurisdictions that recognize copyright laws, the author or authors of this |
| 158 | // software dedicate any and all copyright interest in the software to the public |
| 159 | // domain. We make this dedication for the benefit of the public at large and to |
| 160 | // the detriment of our heirs and successors. We intend this dedication to be an |
| 161 | // overt act of relinquishment in perpetuity of all present and future rights to |
| 162 | // this software under copyright law. |
| 163 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 164 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 165 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 166 | // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 167 | // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 168 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 169 | // ------------------------------------------------------------------------------ |
| 170 | |